Messagelib

openurlwithmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "openurlwithmanager.h"
8
9#include <KConfigGroup>
10#include <KSharedConfig>
11#include <QUrl>
12using namespace MessageViewer;
13namespace
14{
15static const char myOpenUrlWithGroupName[] = "OpenUrlWith";
16}
17OpenUrlWithManager::OpenUrlWithManager(QObject *parent)
18 : QObject{parent}
19{
20 loadSettings();
21}
22
23OpenUrlWithManager::~OpenUrlWithManager() = default;
24
25OpenUrlWithManager *OpenUrlWithManager::self()
26{
27 static OpenUrlWithManager s_self;
28 return &s_self;
29}
30
31void OpenUrlWithManager::clear()
32{
33 mOpenWithUrlInfo.clear();
34}
35
36void OpenUrlWithManager::loadSettings()
37{
38 mOpenWithUrlInfo.clear();
39 KSharedConfig::Ptr config = KSharedConfig::openConfig();
41 const QStringList openWithUrls = group.readEntry("Urls", QStringList());
42 const QStringList commands = group.readEntry("Commands", QStringList());
43 const QStringList commandLines = group.readEntry("CommandLines", QStringList());
44 mOpenWithUrlInfo.reserve(commands.count());
45 for (int i = 0; i < openWithUrls.count(); ++i) {
46 OpenWithUrlInfo info;
47 info.setCommand(commands.at(i));
48 info.setUrl(openWithUrls.at(i));
49 if (i < commandLines.count()) {
50 info.setCommandLine(commandLines.at(i));
51 }
52 mOpenWithUrlInfo.append(std::move(info));
53 }
54}
55
56void OpenUrlWithManager::saveRules()
57{
58 KSharedConfig::Ptr config = KSharedConfig::openConfig();
61 QStringList commands;
63 const auto nbElement{mOpenWithUrlInfo.count()};
64 openWithUrls.reserve(nbElement);
65 commands.reserve(nbElement);
66 for (int i = 0; i < nbElement; ++i) {
67 commands.append(mOpenWithUrlInfo.at(i).command());
68 openWithUrls.append(mOpenWithUrlInfo.at(i).url());
69 commandLines.append(mOpenWithUrlInfo.at(i).commandLine());
70 }
71 group.writeEntry("Urls", openWithUrls);
72 group.writeEntry("Commands", commands);
73 group.writeEntry("CommandLines", commandLines);
74 group.sync();
75}
76
77OpenWithUrlInfo OpenUrlWithManager::openWith(const QUrl &url)
78{
79 for (const OpenWithUrlInfo &info : std::as_const(mOpenWithUrlInfo)) {
80 // qDebug() << " info.url()" << info.url() << " url.host() " << url.host();
81 if (QUrl(info.url()).host() == url.host()) {
82 return info;
83 }
84 }
85 return {};
86}
87
88const QList<OpenWithUrlInfo> &OpenUrlWithManager::openWithUrlInfo() const
89{
90 return mOpenWithUrlInfo;
91}
92
93void OpenUrlWithManager::setOpenWithUrlInfo(const QList<OpenWithUrlInfo> &newOpenWithUrlInfo)
94{
95 mOpenWithUrlInfo = newOpenWithUrlInfo;
96}
97
98#include "moc_openurlwithmanager.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
The OpenWithUrlInfo class.
void append(const T &value)
const T & at(int i) const const
int count(const T &value) const const
void reserve(int alloc)
T qobject_cast(QObject *object)
QString host(QUrl::ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.