Messagelib

openurlwithmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2022-2023 Laurent Montel <[email protected]>
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>
12 using namespace MessageViewer;
13 namespace
14 {
15 static const char myOpenUrlWithGroupName[] = "OpenUrlWith";
16 }
17 OpenUrlWithManager::OpenUrlWithManager(QObject *parent)
18  : QObject{parent}
19 {
20  loadSettings();
21 }
22 
23 OpenUrlWithManager::~OpenUrlWithManager() = default;
24 
25 OpenUrlWithManager *OpenUrlWithManager::self()
26 {
27  static OpenUrlWithManager s_self;
28  return &s_self;
29 }
30 
31 void OpenUrlWithManager::clear()
32 {
33  mOpenWithUrlInfo.clear();
34 }
35 
36 void OpenUrlWithManager::loadSettings()
37 {
38  mOpenWithUrlInfo.clear();
39  KSharedConfig::Ptr config = KSharedConfig::openConfig();
40  KConfigGroup group(config, myOpenUrlWithGroupName);
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(info);
53  }
54 }
55 
56 void OpenUrlWithManager::saveRules()
57 {
58  KSharedConfig::Ptr config = KSharedConfig::openConfig();
59  KConfigGroup group(config, myOpenUrlWithGroupName);
60  QStringList openWithUrls;
61  QStringList commands;
62  QStringList commandLines;
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 
77 OpenWithUrlInfo 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 
88 const QVector<OpenWithUrlInfo> &OpenUrlWithManager::openWithUrlInfo() const
89 {
90  return mOpenWithUrlInfo;
91 }
92 
93 void OpenUrlWithManager::setOpenWithUrlInfo(const QVector<OpenWithUrlInfo> &newOpenWithUrlInfo)
94 {
95  mOpenWithUrlInfo = newOpenWithUrlInfo;
96 }
void append(const T &value)
The OpenWithUrlInfo class.
int count(const T &value) const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void reserve(int alloc)
const T & at(int i) const const
KSharedConfigPtr config()
QString host(QUrl::ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:01:57 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.