FrameworkIntegration

kns/main.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
4 SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
5
6 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7*/
8
9#include <QCoreApplication>
10#include <QDebug>
11#include <QFile>
12#include <QFileInfo>
13#include <QStandardPaths>
14#include <QTimer>
15#include <QUrl>
16#include <QUrlQuery>
17
18#include <KLocalizedString>
19
20#include <KNotification>
21
22#include <KNSCore/EngineBase>
23#include <KNSCore/Question>
24#include <KNSCore/QuestionManager>
25#include <KNSCore/ResultsStream>
26#include <KNSCore/Transaction>
27
28#include "knshandlerversion.h"
29
30/**
31 * Unfortunately there are two knsrc files for the window decorations, but only one is used in the KCM.
32 * But both are used by third parties, consequently we can not remove one. To solve this we create a symlink
33 * which links the old cache file to the new cache file, which is exposed on the GUI.
34 * This way users can again remove window decorations that are installed as a dependency of a global theme.
35 * BUG: 414570
36 *
37 * TODO: knsrc aliases can be specified now, drop this workaround some time later.
38 */
39void createSymlinkForWindowDecorations()
40{
42 // If we have created the symbolic link already we can exit the function here
43 if (info.isSymbolicLink()) {
44 return;
45 }
46 // Delete this file, it the KNS entries are not exposed in any GUI
47 if (info.exists()) {
48 QFile::remove(info.absoluteFilePath());
49 }
50 QFileInfo newFileInfo(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/knewstuff3/window-decorations.knsregistry"));
51 QFile file(newFileInfo.absoluteFilePath());
52 // Make sure that the file exists
53 if (!newFileInfo.exists()) {
54 file.open(QFile::WriteOnly);
55 file.close();
56 }
57 file.link(info.absoluteFilePath());
58}
59
60int main(int argc, char **argv)
61{
62 createSymlinkForWindowDecorations();
63 QCoreApplication app(argc, argv);
64 app.setApplicationName(QStringLiteral("kpackage-knshandler"));
65 app.setApplicationVersion(knshandlerversion);
66 app.setQuitLockEnabled(false);
67 Q_ASSERT(app.arguments().count() == 2);
68
69#ifdef TEST
71#endif
72
73 const QUrl url(app.arguments().last());
74 Q_ASSERT(url.isValid());
75 Q_ASSERT(url.scheme() == QLatin1String("kns"));
76
77 QString knsname;
78 const QStringList availableConfigFiles = KNSCore::EngineBase::availableConfigFiles();
79 auto knsNameIt = std::find_if(availableConfigFiles.begin(), availableConfigFiles.end(), [&url](const QString &availableFile) {
80 return availableFile.endsWith(QLatin1String("/") + url.host());
81 });
82
83 if (knsNameIt == availableConfigFiles.end()) {
84 qWarning() << "couldn't find knsrc file for" << url.host();
85 return 1;
86 } else {
87 knsname = *knsNameIt;
88 }
89
90 const auto pathParts = url.path().split(QLatin1Char('/'), Qt::SkipEmptyParts);
91 if (pathParts.size() != 2) {
92 qWarning() << "wrong format in the url path" << url << pathParts;
93 return 1;
94 }
95 const auto providerid = pathParts.at(0);
96 const auto entryid = pathParts.at(1);
97 int linkid = 1;
98 if (url.hasQuery()) {
99 QUrlQuery query(url);
100 if (query.hasQueryItem(QStringLiteral("linkid"))) {
101 bool ok;
102 linkid = query.queryItemValue(QStringLiteral("linkid")).toInt(&ok);
103 if (!ok) {
104 qWarning() << "linkid is not an integer" << url << pathParts;
105 return 1;
106 }
107 }
108 }
109
110 KNSCore::EngineBase engine;
111 int installedCount = 0;
112 QObject::connect(KNSCore::QuestionManager::instance(), &KNSCore::QuestionManager::askQuestion, &engine, [](KNSCore::Question *question) {
113 auto discardQuestion = [question]() {
114 question->setResponse(KNSCore::Question::InvalidResponse);
115 };
116 switch (question->questionType()) {
117 case KNSCore::Question::YesNoQuestion: {
118 auto f = KNotification::event(KNotification::StandardEvent::Notification, question->title(), question->question());
119
120 auto *yes = f->addAction(i18n("Yes"));
121 QObject::connect(yes, &KNotificationAction::activated, question, [question] {
122 question->setResponse(KNSCore::Question::YesResponse);
123 });
124
125 auto *no = f->addAction(i18n("No"));
126 QObject::connect(no, &KNotificationAction::activated, question, [question] {
127 question->setResponse(KNSCore::Question::NoResponse);
128 });
129
130 QObject::connect(f, &KNotification::closed, question, discardQuestion);
131 } break;
132 case KNSCore::Question::ContinueCancelQuestion: {
133 auto f = KNotification::event(KNotification::StandardEvent::Notification, question->title(), question->question());
134
135 auto *continueAction = f->addAction(i18n("Continue"));
136 QObject::connect(continueAction, &KNotificationAction::activated, question, [question]() {
137 question->setResponse(KNSCore::Question::ContinueResponse);
138 });
139
140 auto *cancelAction = f->addAction(i18n("Cancel"));
141 QObject::connect(cancelAction, &KNotificationAction::activated, question, [question]() {
142 question->setResponse(KNSCore::Question::CancelResponse);
143 });
144
145 QObject::connect(f, &KNotification::closed, question, discardQuestion);
146 } break;
147 case KNSCore::Question::InputTextQuestion:
148 case KNSCore::Question::SelectFromListQuestion:
149 case KNSCore::Question::PasswordQuestion:
150 discardQuestion();
151 break;
152 }
153 });
154
155 const auto onError = [](KNSCore::ErrorCode::ErrorCode errorCode, const QString &message, const QVariant &metadata) {
156 qWarning() << "kns error:" << errorCode << message << metadata;
158 };
159
160 bool entryWasFound = false;
161 const auto onEntriesLoded = [providerid, linkid, &installedCount, &engine, &entryWasFound, onError](const KNSCore::Entry::List list) {
162 Q_ASSERT(list.size() == 1);
163 entryWasFound = true;
164 const auto entry = list.first();
165 if (providerid != entry.providerId()) {
166 qWarning() << "Wrong provider" << providerid << "instead of" << entry.providerId();
168 } else if (entry.status() == KNSCore::Entry::Downloadable) {
169 qDebug() << "installing...";
170 installedCount++;
171 auto transaction = KNSCore::Transaction::installLinkId(&engine, entry, linkid);
173 QObject::connect(transaction, &KNSCore::Transaction::signalEntryEvent, [&installedCount](auto entry, auto event) {
175 if (entry.status() == KNSCore::Entry::Installed) {
176 installedCount--;
177 }
178 if (installedCount == 0) {
180 }
181 }
182 });
183 } else if (installedCount == 0) {
184 qDebug() << "already installed.";
186 }
187 };
188 QObject::connect(&engine, &KNSCore::EngineBase::signalProvidersLoaded, &engine, [&engine, &entryid, onEntriesLoded, &entryWasFound]() {
189 qWarning() << "providers are loaded";
190 KNSCore::SearchRequest request(KNSCore::SortMode::Newest, KNSCore::Filter::ExactEntryId, entryid, QStringList{}, 0);
191 KNSCore::ResultsStream *results = engine.search(request);
192 QObject::connect(results, &KNSCore::ResultsStream::entriesFound, &engine, onEntriesLoded);
193 QObject::connect(results, &KNSCore::ResultsStream::finished, &engine, [&entryWasFound, &entryid]() {
194 if (!entryWasFound) {
195 qWarning() << "Entry with id" << entryid << "could not be found";
197 }
198 });
199 results->fetch();
200 });
201
202 QObject::connect(&engine, &KNSCore::EngineBase::signalErrorCode, &engine, onError);
203 if (!engine.init(knsname)) {
204 qWarning() << "couldn't initialize" << knsname;
205 return 1;
206 }
207 return app.exec();
208}
void signalErrorCode(KNSCore::ErrorCode::ErrorCode errorCode, const QString &message, const QVariant &metadata)
static QStringList availableConfigFiles()
void setResponse(const QString &response)
void signalEntryEvent(const KNSCore::Entry &entry, KNSCore::Entry::EntryEvent event)
void signalErrorCode(KNSCore::ErrorCode::ErrorCode errorCode, const QString &message, const QVariant &metadata)
static Transaction * installLinkId(EngineBase *engine, const Entry &entry, quint8 linkId)
static KNotification * event(const QString &eventId, const QString &text=QString(), const QPixmap &pixmap=QPixmap(), const NotificationFlags &flags=CloseOnTimeout, const QString &componentName=QString())
QString i18n(const char *text, const TYPE &arg...)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void exit(int returnCode)
bool remove()
iterator begin()
iterator end()
T & first()
qsizetype size() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setTestModeEnabled(bool testMode)
QString writableLocation(StandardLocation type)
SkipEmptyParts
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:53:48 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.