File indexing completed on 2025-02-16 04:25:46

0001 #include "mauimanutils.h"
0002 
0003 #if !defined Q_OS_ANDROID
0004 #include <QtDBus/QDBusConnection>
0005 #include <QtDBus/QDBusConnectionInterface>
0006 #include <QtDBus/QDBusServiceWatcher>
0007 #endif
0008 
0009 #include <QDebug>
0010 #include <QStringList>
0011 
0012 #include <QProcess>
0013 
0014 static const QString mauimanInterface(QStringLiteral("org.mauiman.Manager"));
0015 
0016 MauiManUtils::MauiManUtils(QObject *parent)
0017 : QObject{parent}
0018 {
0019     #if !defined Q_OS_ANDROID
0020     const QDBusConnection bus = QDBusConnection::sessionBus();
0021     const auto registeredServices = bus.interface()->registeredServiceNames();
0022 
0023     if (registeredServices.isValid())
0024     {
0025         m_serverRunning = registeredServices.value().contains(mauimanInterface);
0026     }
0027 
0028     auto watcher = new QDBusServiceWatcher(mauimanInterface, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration, this);
0029 
0030     connect(watcher, &QDBusServiceWatcher::serviceRegistered, [=](const QString &name) {
0031         qDebug() << "Connected to MauiMan server" << name;
0032         m_serverRunning = true;
0033         Q_EMIT serverRunningChanged(m_serverRunning);
0034     });
0035 
0036     connect(watcher, &QDBusServiceWatcher::serviceUnregistered, [=](const QString &name) {
0037         qDebug() << "Disconnected to MauiMan server" << name;
0038         m_serverRunning = false;
0039         Q_EMIT serverRunningChanged(m_serverRunning);
0040     });
0041     #endif
0042 }
0043 
0044 bool MauiManUtils::serverRunning() const
0045 {
0046     return m_serverRunning;
0047 }
0048 
0049 void MauiManUtils::startServer()
0050 {
0051     #if !defined Q_OS_ANDROID
0052     QProcess::startDetached(QStringLiteral("MauiManServer"), QStringList());
0053     #endif
0054 }
0055 
0056 void MauiManUtils::invokeManager(const QString &module)
0057 {
0058     QProcess::startDetached(QStringLiteral("MauiSettings"), QStringList {QStringLiteral("-m"), module});
0059 }
0060 
0061 QString MauiManUtils::currentDesktopSession()
0062 {
0063     if(qEnvironmentVariableIsSet("XDG_CURRENT_DESKTOP"))
0064     {
0065         const auto names = qEnvironmentVariable("XDG_CURRENT_DESKTOP").split(QStringLiteral(";"));
0066         return names.first();
0067     }
0068 
0069     return QString();
0070 }
0071 
0072 bool MauiManUtils::isMauiSession()
0073 {
0074     return currentDesktopSession() == QStringLiteral("CASK");
0075 }
0076 
0077 bool MauiManUtils::isPlasmaSession()
0078 {
0079     return currentDesktopSession() == QStringLiteral("KDE");
0080 }
0081 
0082 bool MauiManUtils::isGnomeSession()
0083 {
0084     return currentDesktopSession() == QStringLiteral("Gnome");
0085 }