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#include "messageviewer_debug.h"
9#include <KConfigGroup>
10#include <KSharedConfig>
11#include <QDir>
12#include <QSettings>
13#include <QStandardPaths>
14#include <QUrl>
15
16using namespace MessageViewer;
17namespace
18{
19static const char myOpenUrlWithGroupName[] = "OpenUrlWith";
20}
21OpenUrlWithManager::OpenUrlWithManager(QObject *parent)
22 : QObject{parent}
23{
24 loadSettings();
25}
26
27OpenUrlWithManager::~OpenUrlWithManager() = default;
28
29OpenUrlWithManager *OpenUrlWithManager::self()
30{
31 static OpenUrlWithManager s_self;
32 return &s_self;
33}
34
35void OpenUrlWithManager::clear()
36{
37 mOpenWithUrlInfo.clear();
38}
39
40void OpenUrlWithManager::loadUserSettings()
41{
42 KSharedConfig::Ptr config = KSharedConfig::openConfig();
43 KConfigGroup group(config, QLatin1StringView(myOpenUrlWithGroupName));
44 const QStringList openWithUrls = group.readEntry("Urls", QStringList());
45 const QStringList commands = group.readEntry("Commands", QStringList());
46 const QStringList commandLines = group.readEntry("CommandLines", QStringList());
47 const QList<bool> enabledCommandLines = group.readEntry("EnabledCommandLines", QList<bool>());
48 const auto nbElement{openWithUrls.count()};
49 mOpenWithUrlInfo.reserve(nbElement);
50 for (int i = 0; i < nbElement; ++i) {
51 OpenWithUrlInfo info;
52 info.setCommand(commands.at(i));
53 info.setUrl(openWithUrls.at(i));
54 if (i < commandLines.count()) {
55 info.setCommandLine(commandLines.at(i));
56 }
57 if (i < enabledCommandLines.count()) {
58 info.setEnabled(enabledCommandLines.at(i));
59 }
60 mOpenWithUrlInfo.append(std::move(info));
61 }
62}
63
64QStringList OpenUrlWithManager::openUrlWithListPath() const
65{
66 return QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("messageviewer/openurlwith/"), QStandardPaths::LocateDirectory);
67}
68
69void OpenUrlWithManager::loadGlobalSettings()
70{
71 for (const QString &path : openUrlWithListPath()) {
72 QDir dir(path);
73 const QStringList entries = dir.entryList({QStringLiteral("*.openurl")});
74 for (const QString &entry : entries) {
75 qCDebug(MESSAGEVIEWER_LOG) << "files: " << entry;
76 QSettings settings(path + QLatin1Char('/') + entry, QSettings::IniFormat);
77 OpenWithUrlInfo info;
78 info.setIsLocalOpenWithInfo(false);
79 info.setCommand(settings.value("Command").toString());
80 info.setCommandLine(settings.value("CommandLine").toString());
81 info.setUrl(settings.value("Url").toString());
82 if (info.isValid()) {
83 mOpenWithUrlInfo.append(std::move(info));
84 }
85 }
86 }
87}
88
89void OpenUrlWithManager::loadSettings()
90{
91 mOpenWithUrlInfo.clear();
92 loadUserSettings();
93 loadGlobalSettings();
94}
95
96void OpenUrlWithManager::saveRules()
97{
98 KSharedConfig::Ptr config = KSharedConfig::openConfig();
99 KConfigGroup group(config, QLatin1StringView(myOpenUrlWithGroupName));
100 QStringList openWithUrls;
101 QStringList commands;
102 QStringList commandLines;
103 QList<bool> enabledCommandLines;
104 const auto nbElement{mOpenWithUrlInfo.count()};
105 openWithUrls.reserve(nbElement);
106 commands.reserve(nbElement);
107 for (int i = 0; i < nbElement; ++i) {
108 const auto &openWith(mOpenWithUrlInfo.at(i));
109 if (openWith.isLocalOpenWithInfo()) {
110 commands.append(openWith.command());
111 openWithUrls.append(openWith.url());
112 commandLines.append(openWith.commandLine());
113 enabledCommandLines.append(openWith.enabled());
114 }
115 }
116 group.writeEntry("Urls", openWithUrls);
117 group.writeEntry("Commands", commands);
118 group.writeEntry("CommandLines", commandLines);
119 group.writeEntry("EnabledCommandLines", enabledCommandLines);
120 group.sync();
121}
122
123bool OpenUrlWithManager::alwaysRuleForHost(const QUrl &url)
124{
125 for (const OpenWithUrlInfo &info : std::as_const(mOpenWithUrlInfo)) {
126 // qDebug() << " info.url()" << info.url() << " url.host() " << url.host();
127 if (info.enabled()) {
128 if (QUrl(info.url()).host() == url.host()) {
129 return true;
130 }
131 }
132 }
133 return false;
134}
135
136OpenWithUrlInfo OpenUrlWithManager::openWith(const QUrl &url)
137{
138 for (const OpenWithUrlInfo &info : std::as_const(mOpenWithUrlInfo)) {
139 // qDebug() << " info.url()" << info.url() << " url.host() " << url.host();
140 if (info.enabled()) {
141 if (QUrl(info.url()).host() == url.host()) {
142 return info;
143 }
144 }
145 }
146 return {};
147}
148
149QStringList OpenUrlWithManager::hosts() const
150{
151 QStringList lst;
152 for (const OpenWithUrlInfo &info : std::as_const(mOpenWithUrlInfo)) {
153 lst.append(info.url());
154 }
155 return lst;
156}
157
158const QList<OpenWithUrlInfo> &OpenUrlWithManager::openWithUrlInfo() const
159{
160 return mOpenWithUrlInfo;
161}
162
163void OpenUrlWithManager::setOpenWithUrlInfo(const QList<OpenWithUrlInfo> &newOpenWithUrlInfo)
164{
165 mOpenWithUrlInfo = newOpenWithUrlInfo;
166}
167
168#include "moc_openurlwithmanager.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
The OpenWithUrlInfo class.
KIOCORE_EXPORT QString dir(const QString &fileClass)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
qsizetype count() const const
void reserve(qsizetype size)
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
QString host(ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.