File indexing completed on 2024-05-12 17:08:37

0001 /*
0002  *  SPDX-FileCopyrightText: 2021 Felipe Kinoshita <kinofhek@gmail.com>
0003  *  SPDX-FileCopyrightText: 2022 Nate Graham <nate@kde.org>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include <QDir>
0009 #include <QFile>
0010 #include <QNetworkInterface>
0011 #include <QProcess>
0012 #include <QStandardPaths>
0013 #include <QString>
0014 
0015 #include "controller.h"
0016 #include "kuserfeedbacksettings.h"
0017 
0018 #include <KIO/ApplicationLauncherJob>
0019 #include <KIO/CommandLauncherJob>
0020 #include <KNotificationJobUiDelegate>
0021 #include <KService>
0022 
0023 void Controller::launchApp(const QString &program)
0024 {
0025     auto *job = new KIO::ApplicationLauncherJob(KService::serviceByDesktopName(program));
0026     job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled));
0027     job->start();
0028 }
0029 
0030 void Controller::runCommand(const QString &command)
0031 {
0032     runCommand(command, QString());
0033 }
0034 
0035 void Controller::runCommand(const QString &command, const QString &desktopFilename)
0036 {
0037     auto *job = new KIO::CommandLauncherJob(command);
0038     if (!desktopFilename.isEmpty()) {
0039         job->setDesktopName(desktopFilename);
0040     }
0041     job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled));
0042     job->start();
0043 }
0044 
0045 bool Controller::networkAlreadyConnected()
0046 {
0047     for (QNetworkInterface interface : QNetworkInterface::allInterfaces()) {
0048         const QFlags flags = interface.flags();
0049         if (flags.testFlag(QNetworkInterface::IsUp) && !flags.testFlag(QNetworkInterface::IsLoopBack)) {
0050             if (interface.addressEntries().count() >= 1) {
0051                 return true;
0052             }
0053         }
0054     }
0055     return false;
0056 }
0057 
0058 bool Controller::userFeedbackAvailable()
0059 {
0060 #if HAVE_KUSERFEEDBACK
0061     return true;
0062 #else
0063     return false;
0064 #endif
0065 }
0066 
0067 QStringList Controller::distroPages()
0068 {
0069     const QString dirname = QStringLiteral(DISTRO_PAGE_PATH);
0070     const QDir distroPagePath = QDir(dirname);
0071 
0072     if (!distroPagePath.exists() || distroPagePath.isEmpty()) {
0073         return {};
0074     }
0075 
0076     QStringList pages = distroPagePath.entryList(QDir::NoDotAndDotDot | QDir::Files | QDir::Readable, QDir::Name);
0077     for (int i = 0; i < pages.length(); i++) {
0078         pages[i] = QStringLiteral("file://") + dirname + pages[i];
0079     }
0080 
0081     return pages;
0082 }
0083 
0084 void Controller::setPlasmaUpgradeVersion(const QString &version)
0085 {
0086     if (m_newPlasmaVersion == version) {
0087         return;
0088     }
0089 
0090     m_newPlasmaVersion = version;
0091     Q_EMIT newPlasmaVersionChanged();
0092 }