1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
Copyright (c) 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
Copyright (c) 2000 Matthias Elter <elter@kde.org>
Copyright (c) 2004 Frans Englich <frans.englich@telia.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config-kde-cli-tools.h>
#include "main.h"
#include <iostream>
#include <QtDBus>
#include <KAboutData>
#include <QApplication>
#include <KAuthorized>
#include <KCModuleInfo>
#include <KCMultiDialog>
#include <KCModuleProxy>
#include <QDebug>
#include <KLocalizedString>
#include <KServiceTypeTrader>
#include <KStartupInfo>
#include <KActivities/ResourceInstance>
#include <kworkspace.h>
#include <QIcon>
#include <QCommandLineParser>
#include <QCommandLineOption>
using namespace std;
KService::List m_modules;
static bool caseInsensitiveLessThan(const KService::Ptr s1, const KService::Ptr s2)
{
const int compare = QString::compare(s1->desktopEntryName(),
s2->desktopEntryName(),
Qt::CaseInsensitive);
return (compare < 0);
}
static void listModules()
{
// First condition is what systemsettings does, second what kinfocenter does, make sure this is kept in sync
// We need the exist calls because otherwise the trader language aborts if the property doesn't exist and the second part of the or is not evaluated
const KService::List services = KServiceTypeTrader::self()->query( QStringLiteral("KCModule"), QStringLiteral("(exist [X-KDE-System-Settings-Parent-Category] and [X-KDE-System-Settings-Parent-Category] != '') or (exist [X-KDE-ParentApp] and [X-KDE-ParentApp] == 'kinfocenter')") );
for( KService::List::const_iterator it = services.constBegin(); it != services.constEnd(); ++it) {
const KService::Ptr s = (*it);
if (!KAuthorized::authorizeControlModule(s->menuId()))
continue;
m_modules.append(s);
}
std::stable_sort(m_modules.begin(), m_modules.end(), caseInsensitiveLessThan);
}
static KService::Ptr locateModule(const QString& module)
{
QString path = module;
if (!path.endsWith(QLatin1String(".desktop")))
path += QStringLiteral(".desktop");
KService::Ptr service = KService::serviceByStorageId( path );
if (!service) {
return KService::Ptr();
}
if (!service->hasServiceType(QStringLiteral("KCModule"))) {
// Not a KCModule. E.g. "kcmshell5 akonadi" finds services/kresources/kabc/akonadi.desktop, unrelated.
return KService::Ptr();
}
if (service->noDisplay()) {
qDebug() << module << "should not be loaded.";
return KService::Ptr();
}
return service;
}
bool KCMShell::isRunning()
{
const QString owner = QDBusConnection::sessionBus().interface()->serviceOwner(m_serviceName);
if (owner == QDBusConnection::sessionBus().baseService()) {
return false; // We are the one and only.
}
qDebug() << "kcmshell5 with modules '" << m_serviceName << "' is already running.";
QDBusInterface iface(m_serviceName, QStringLiteral("/KCModule/dialog"), QStringLiteral("org.kde.KCMShellMultiDialog"));
QDBusReply<void> reply = iface.call(QStringLiteral("activate"), KStartupInfo::startupId());
if (!reply.isValid())
{
qDebug() << "Calling D-Bus function dialog::activate() failed.";
return false; // Error, we have to do it ourselves.
}
return true;
}
KCMShellMultiDialog::KCMShellMultiDialog(KPageDialog::FaceType dialogFace, QWidget *parent)
: KCMultiDialog(parent)
{
setFaceType(dialogFace);
setModal(false);
QDBusConnection::sessionBus().registerObject(QStringLiteral("/KCModule/dialog"), this, QDBusConnection::ExportScriptableSlots);
connect(this, &KCMShellMultiDialog::currentPageChanged,
this, [this](KPageWidgetItem *newPage,KPageWidgetItem *oldPage) {
Q_UNUSED(oldPage);
KCModuleProxy *activeModule = newPage->widget()->findChild<KCModuleProxy *>();
if (activeModule) {
KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("kcm:") + activeModule->moduleInfo().service()->storageId()),
QStringLiteral("org.kde.systemsettings"));
}
});
}
void KCMShellMultiDialog::activate(const QByteArray& asn_id)
{
#ifdef HAVE_X11
KStartupInfo::setNewStartupId(this, asn_id);
#endif
}
void KCMShell::setServiceName(const QString &dbusName)
{
m_serviceName = QLatin1String( "org.kde.kcmshell_" ) + dbusName;
QDBusConnection::sessionBus().registerService(m_serviceName);
}
void KCMShell::waitForExit()
{
QDBusServiceWatcher *watcher = new QDBusServiceWatcher(this);
watcher->setConnection(QDBusConnection::sessionBus());
watcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange);
watcher->addWatchedService(m_serviceName);
connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
SLOT(appExit(QString,QString,QString)));
exec();
}
void KCMShell::appExit(const QString &appId, const QString &oldName, const QString &newName)
{
Q_UNUSED(appId);
Q_UNUSED(newName);
if (!oldName.isEmpty())
{
qDebug() << "'" << appId << "' closed, quitting.";
qApp->quit();
}
}
extern "C" Q_DECL_EXPORT int kdemain(int _argc, char *_argv[])
{
const bool qpaVariable = qEnvironmentVariableIsSet("QT_QPA_PLATFORM");
KWorkSpace::detectPlatform(_argc, _argv);
KCMShell app(_argc, _argv);
if (!qpaVariable) {
// don't leak the env variable to processes we start
qunsetenv("QT_QPA_PLATFORM");
}
KLocalizedString::setApplicationDomain("kcmshell5");
app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
KAboutData aboutData(QStringLiteral("kcmshell5"), i18n("System Settings Module"),
QLatin1String(PROJECT_VERSION),
i18n("A tool to start single system settings modules"),
KAboutLicense::GPL,
i18n("(c) 1999-2016, The KDE Developers"));
aboutData.addAuthor(i18n("Frans Englich"), i18n("Maintainer"), QStringLiteral("frans.englich@kde.org"));
aboutData.addAuthor(i18n("Daniel Molkentin"), QString(), QStringLiteral("molkentin@kde.org"));
aboutData.addAuthor(i18n("Matthias Hoelzer-Kluepfel"),QString(), QStringLiteral("hoelzer@kde.org"));
aboutData.addAuthor(i18n("Matthias Elter"),QString(), QStringLiteral("elter@kde.org"));
aboutData.addAuthor(i18n("Matthias Ettrich"),QString(), QStringLiteral("ettrich@kde.org"));
aboutData.addAuthor(i18n("Waldo Bastian"),QString(), QStringLiteral("bastian@kde.org"));
KAboutData::setApplicationData(aboutData);
QCommandLineParser parser;
aboutData.setupCommandLine(&parser);
parser.addOption(QCommandLineOption(QStringLiteral("list"), i18n("List all possible modules")));
parser.addPositionalArgument(QStringLiteral("module"), i18n("Configuration module to open"));
parser.addOption(QCommandLineOption(QStringLiteral("lang"), i18n("Specify a particular language"), QLatin1String("language")));
parser.addOption(QCommandLineOption(QStringLiteral("silent"), i18n("Do not display main window")));
parser.addOption(QCommandLineOption(QStringLiteral("args"), i18n("Arguments for the module"), QLatin1String("arguments")));
parser.addOption(QCommandLineOption(QStringLiteral("icon"), i18n("Use a specific icon for the window"), QLatin1String("icon")));
parser.addOption(QCommandLineOption(QStringLiteral("caption"), i18n("Use a specific caption for the window"), QLatin1String("caption")));
parser.parse(app.arguments());
aboutData.processCommandLine(&parser);
parser.process(app);
const QString lang = parser.value(QStringLiteral("lang"));
if (!lang.isEmpty()) {
cout << i18n("--lang is deprecated. Please set the LANGUAGE environment variable instead").toLocal8Bit().data() << endl;
}
if (parser.isSet(QStringLiteral("list"))) {
cout << i18n("The following modules are available:").toLocal8Bit().data() << endl;
listModules();
int maxLen=0;
for (KService::List::ConstIterator it = m_modules.constBegin(); it != m_modules.constEnd(); ++it) {
int len = (*it)->desktopEntryName().length();
if (len > maxLen)
maxLen = len;
}
for (KService::List::ConstIterator it = m_modules.constBegin(); it != m_modules.constEnd(); ++it) {
QString entry(QStringLiteral("%1 - %2"));
entry = entry.arg((*it)->desktopEntryName().leftJustified(maxLen, QLatin1Char(' ')))
.arg(!(*it)->comment().isEmpty() ? (*it)->comment()
: i18n("No description available"));
cout << entry.toLocal8Bit().data() << endl;
}
return 0;
}
if (parser.positionalArguments().count() < 1) {
parser.showHelp();
return -1;
}
QString serviceName;
KService::List modules;
for (int i = 0; i < parser.positionalArguments().count(); i++) {
const QString arg = parser.positionalArguments().at(i);
KService::Ptr service = locateModule(arg);
if (!service) {
service = locateModule(QStringLiteral("kcm_") + arg);
}
if (!service) {
service = locateModule(QStringLiteral("kcm") + arg);
}
if (service) {
modules.append(service);
if (!serviceName.isEmpty()) {
serviceName += QLatin1Char('_');
}
serviceName += arg;
} else {
cerr << i18n("Could not find module '%1'. See kcmshell5 --list for the full list of modules.", arg).toLocal8Bit().constData() << endl;
}
}
/* Check if this particular module combination is already running */
app.setServiceName(serviceName);
if (app.isRunning()) {
app.waitForExit();
return 0;
}
KPageDialog::FaceType ftype = KPageDialog::Plain;
if (modules.count() < 1) {
return -1;
} else if (modules.count() > 1) {
ftype = KPageDialog::List;
}
QStringList moduleArgs;
const QString x = parser.value(QStringLiteral("args"));
moduleArgs << x.split(QRegExp(QStringLiteral(" +")));
KCMShellMultiDialog *dlg = new KCMShellMultiDialog(ftype);
dlg->setAttribute(Qt::WA_DeleteOnClose);
if (parser.isSet(QStringLiteral("caption"))) {
dlg->setWindowTitle(parser.value(QStringLiteral("caption")));
} else if (modules.count() == 1) {
dlg->setWindowTitle(modules.first()->name());
}
for (KService::List::ConstIterator it = modules.constBegin(); it != modules.constEnd(); ++it) {
dlg->addModule(*it, nullptr, moduleArgs);
}
if (parser.isSet(QStringLiteral("icon"))) {
dlg->setWindowIcon(QIcon::fromTheme(parser.value(QStringLiteral("icon"))));
} else if (!parser.isSet(QStringLiteral("icon")) && !modules.isEmpty()) {
const QString iconName = KCModuleInfo(modules.first()).icon();
dlg->setWindowIcon( QIcon::fromTheme(iconName) );
}
if (modules.count() == 1 && app.desktopFileName() == QLatin1String("org.kde.kcmshell5")) {
const QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
QStringLiteral("kservices5/%1.desktop").arg(modules.first()->desktopEntryName()));
if (!path.isEmpty()) {
app.setDesktopFileName(path);
}
}
dlg->show();
app.exec();
return 0;
}
// vim: sw=4 et sts=4
|