7#include "openurlwithmanager.h"
8#include "messageviewer_debug.h"
10#include <KSharedConfig>
13#include <QStandardPaths>
16using namespace MessageViewer;
19static const char myOpenUrlWithGroupName[] =
"OpenUrlWith";
21OpenUrlWithManager::OpenUrlWithManager(
QObject *parent)
27OpenUrlWithManager::~OpenUrlWithManager() =
default;
29OpenUrlWithManager *OpenUrlWithManager::self()
31 static OpenUrlWithManager s_self;
35void OpenUrlWithManager::clear()
37 mOpenWithUrlInfo.clear();
40void OpenUrlWithManager::loadUserSettings()
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) {
52 info.setCommand(commands.
at(i));
53 info.setUrl(openWithUrls.
at(i));
54 if (i < commandLines.
count()) {
55 info.setCommandLine(commandLines.
at(i));
57 if (i < enabledCommandLines.
count()) {
58 info.setEnabled(enabledCommandLines.
at(i));
60 mOpenWithUrlInfo.append(std::move(info));
64QStringList OpenUrlWithManager::openUrlWithListPath()
const
69void OpenUrlWithManager::loadGlobalSettings()
71 for (
const QString &path : openUrlWithListPath()) {
73 const QStringList entries =
dir.entryList({QStringLiteral(
"*.openurl")});
74 for (
const QString &entry : entries) {
75 qCDebug(MESSAGEVIEWER_LOG) <<
"files: " << entry;
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());
83 mOpenWithUrlInfo.append(std::move(info));
89void OpenUrlWithManager::loadSettings()
91 mOpenWithUrlInfo.clear();
96void OpenUrlWithManager::saveRules()
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);
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());
116 group.writeEntry(
"Urls", openWithUrls);
117 group.writeEntry(
"Commands", commands);
118 group.writeEntry(
"CommandLines", commandLines);
119 group.writeEntry(
"EnabledCommandLines", enabledCommandLines);
123bool OpenUrlWithManager::alwaysRuleForHost(
const QUrl &url)
125 for (
const OpenWithUrlInfo &info : std::as_const(mOpenWithUrlInfo)) {
127 if (info.enabled()) {
128 if (QUrl(info.url()).host() == url.
host()) {
138 for (
const OpenWithUrlInfo &info : std::as_const(mOpenWithUrlInfo)) {
140 if (info.enabled()) {
141 if (QUrl(info.url()).host() == url.
host()) {
149QStringList OpenUrlWithManager::hosts()
const
152 for (
const OpenWithUrlInfo &info : std::as_const(mOpenWithUrlInfo)) {
158const QList<OpenWithUrlInfo> &OpenUrlWithManager::openWithUrlInfo()
const
160 return mOpenWithUrlInfo;
163void OpenUrlWithManager::setOpenWithUrlInfo(
const QList<OpenWithUrlInfo> &newOpenWithUrlInfo)
165 mOpenWithUrlInfo = newOpenWithUrlInfo;
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