File indexing completed on 2024-04-28 16:52:24

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0003  *  SPDX-FileCopyrightText: 2018 Bhushan Shah <bshah@kde.org>
0004  *  SPDX-FileCopyrightText: 2021 Devin Lin <devin@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "shellutil.h"
0010 #include "mobileshellsettings.h"
0011 #include "windowutil.h"
0012 
0013 #include <KConfigGroup>
0014 #include <KFileUtils>
0015 #include <KLocalizedString>
0016 #include <KNotification>
0017 #include <KNotificationJobUiDelegate>
0018 
0019 #include <QDBusPendingReply>
0020 #include <QDateTime>
0021 #include <QDebug>
0022 #include <QFile>
0023 #include <QProcess>
0024 
0025 #define FORMAT24H "HH:mm:ss"
0026 
0027 ShellUtil::ShellUtil(QObject *parent)
0028     : QObject{parent}
0029     , m_localeConfig{KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::SimpleConfig)}
0030 {
0031     m_localeConfigWatcher = KConfigWatcher::create(m_localeConfig);
0032 
0033     // watch for changes to locale config, to update 12/24 hour time
0034     connect(m_localeConfigWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &names) -> void {
0035         if (group.name() == "Locale") {
0036             // we have to reparse for new changes (from system settings)
0037             m_localeConfig->reparseConfiguration();
0038             Q_EMIT isSystem24HourFormatChanged();
0039         }
0040     });
0041 }
0042 
0043 ShellUtil *ShellUtil::instance()
0044 {
0045     static ShellUtil *inst = new ShellUtil(nullptr);
0046     return inst;
0047 }
0048 
0049 void ShellUtil::stackItemBefore(QQuickItem *item1, QQuickItem *item2)
0050 {
0051     if (!item1 || !item2 || item1 == item2 || item1->parentItem() != item2->parentItem()) {
0052         return;
0053     }
0054 
0055     item1->stackBefore(item2);
0056 }
0057 
0058 void ShellUtil::stackItemAfter(QQuickItem *item1, QQuickItem *item2)
0059 {
0060     if (!item1 || !item2 || item1 == item2 || item1->parentItem() != item2->parentItem()) {
0061         return;
0062     }
0063 
0064     item1->stackAfter(item2);
0065 }
0066 
0067 void ShellUtil::executeCommand(const QString &command)
0068 {
0069     qWarning() << "Executing" << command;
0070     const QStringList commandAndArguments = QProcess::splitCommand(command);
0071     QProcess::startDetached(commandAndArguments.front(), commandAndArguments.mid(1));
0072 }
0073 
0074 bool ShellUtil::isSystem24HourFormat()
0075 {
0076     KConfigGroup localeSettings = KConfigGroup(m_localeConfig, "Locale");
0077 
0078     QString timeFormat = localeSettings.readEntry("TimeFormat", QStringLiteral(FORMAT24H));
0079     return timeFormat == QStringLiteral(FORMAT24H);
0080 }
0081 
0082 void ShellUtil::launchApp(const QString &storageId)
0083 {
0084     // try to activate a running window first
0085     auto windows = WindowUtil::instance()->windowsFromStorageId(storageId);
0086 
0087     if (!windows.empty()) {
0088         windows[0]->requestActivate();
0089         return;
0090     }
0091 
0092     // now try launching the window
0093     KService::Ptr service = KService::serviceByStorageId(storageId);
0094     if (!service) {
0095         qWarning() << "Could not find" << storageId;
0096         return;
0097     }
0098 
0099     auto job = new KIO::ApplicationLauncherJob(service, this);
0100     job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
0101     job->start();
0102 }