File indexing completed on 2024-05-12 05:36:09

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 
0011 #include <KConfigGroup>
0012 #include <KFileUtils>
0013 #include <KLocalizedString>
0014 #include <KNotification>
0015 #include <KNotificationJobUiDelegate>
0016 
0017 #include <QDBusPendingReply>
0018 #include <QDateTime>
0019 #include <QDebug>
0020 #include <QFile>
0021 #include <QProcess>
0022 
0023 #define FORMAT24H "HH:mm:ss"
0024 
0025 ShellUtil::ShellUtil(QObject *parent)
0026     : QObject{parent}
0027     , m_localeConfig{KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::SimpleConfig)}
0028 {
0029 }
0030 
0031 void ShellUtil::stackItemBefore(QQuickItem *item1, QQuickItem *item2)
0032 {
0033     if (!item1 || !item2 || item1 == item2 || item1->parentItem() != item2->parentItem()) {
0034         return;
0035     }
0036 
0037     item1->stackBefore(item2);
0038 }
0039 
0040 void ShellUtil::stackItemAfter(QQuickItem *item1, QQuickItem *item2)
0041 {
0042     if (!item1 || !item2 || item1 == item2 || item1->parentItem() != item2->parentItem()) {
0043         return;
0044     }
0045 
0046     item1->stackAfter(item2);
0047 }
0048 
0049 void ShellUtil::executeCommand(const QString &command)
0050 {
0051     qWarning() << "Executing" << command;
0052     const QStringList commandAndArguments = QProcess::splitCommand(command);
0053     QProcess::startDetached(commandAndArguments.front(), commandAndArguments.mid(1));
0054 }
0055 
0056 bool ShellUtil::isSystem24HourFormat()
0057 {
0058     // only load the config watcher if this function is actually used once
0059     if (!m_localeConfigWatcher) {
0060         m_localeConfigWatcher = KConfigWatcher::create(m_localeConfig);
0061 
0062         // watch for changes to locale config, to update 12/24 hour time
0063         connect(m_localeConfigWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &names) -> void {
0064             if (group.name() == "Locale") {
0065                 // we have to reparse for new changes (from system settings)
0066                 m_localeConfig->reparseConfiguration();
0067                 Q_EMIT isSystem24HourFormatChanged();
0068             }
0069         });
0070     }
0071 
0072     KConfigGroup localeSettings = KConfigGroup(m_localeConfig, "Locale");
0073 
0074     QString timeFormat = localeSettings.readEntry("TimeFormat", QStringLiteral(FORMAT24H));
0075     return timeFormat == QStringLiteral(FORMAT24H);
0076 }
0077 
0078 void ShellUtil::launchApp(const QString &storageId)
0079 {
0080     KService::Ptr service = KService::serviceByStorageId(storageId);
0081     if (!service) {
0082         qWarning() << "Could not find" << storageId;
0083         return;
0084     }
0085 
0086     auto job = new KIO::ApplicationLauncherJob(service, this);
0087     job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
0088     job->start();
0089 }