Warning, file /network/kdeconnect-kde/indicator/indicatorhelper_mac.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Weixuan XIAO <veyx.shaw@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include <QApplication> 0008 #include <QDebug> 0009 #include <QFile> 0010 #include <QIcon> 0011 #include <QMessageBox> 0012 #include <QStandardPaths> 0013 #include <QThread> 0014 0015 #include <KLocalizedString> 0016 0017 #include <dbushelper.h> 0018 0019 #include "indicatorhelper.h" 0020 0021 #include "serviceregister_mac.h" 0022 0023 #include <kdeconnectconfig.h> 0024 0025 IndicatorHelper::IndicatorHelper() 0026 { 0027 registerServices(); 0028 0029 // Do not create QIcon before D-Bus setup, use a QPixmap from a hardcoded icon now 0030 const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("kdeconnect-icons"), QStandardPaths::LocateDirectory); 0031 QPixmap splashPixmap(iconPath + QStringLiteral("/hicolor/scalable/apps/kdeconnect.svg")); 0032 m_splashScreen = new QSplashScreen(splashPixmap); 0033 0034 // Icon is white, set the text color to black 0035 m_splashScreen->showMessage( 0036 i18n("Launching") + QStringLiteral("\n"), 0037 Qt::AlignHCenter | Qt::AlignBottom, 0038 Qt::black 0039 ); 0040 m_splashScreen->show(); 0041 } 0042 0043 IndicatorHelper::~IndicatorHelper() 0044 { 0045 if (m_splashScreen != nullptr) { 0046 delete m_splashScreen; 0047 m_splashScreen = nullptr; 0048 } 0049 } 0050 0051 void IndicatorHelper::preInit() 0052 { 0053 } 0054 0055 void IndicatorHelper::postInit() 0056 { 0057 m_splashScreen->finish(nullptr); 0058 } 0059 0060 void IndicatorHelper::iconPathHook() 0061 { 0062 const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("kdeconnect-icons"), QStandardPaths::LocateDirectory); 0063 if (!iconPath.isNull()) { 0064 QStringList themeSearchPaths = QIcon::themeSearchPaths(); 0065 themeSearchPaths << iconPath; 0066 QIcon::setThemeSearchPaths(themeSearchPaths); 0067 } 0068 } 0069 0070 int IndicatorHelper::daemonHook(QProcess &kdeconnectd) 0071 { 0072 // This flag marks whether a session DBus daemon is installed and run 0073 bool hasUsableSessionBus = true; 0074 // Use another bus instance for detecting, avoid session bus cache in Qt 0075 if (!QDBusConnection::connectToBus(QDBusConnection::SessionBus, QStringLiteral("kdeconnect-test-client")).isConnected()) { 0076 qDebug() << "Default session bus not detected, will use private D-Bus."; 0077 0078 // Unset launchctl env and private dbus addr file, avoid block 0079 DBusHelper::macosUnsetLaunchctlEnv(); 0080 QFile privateDBusAddressFile(KdeConnectConfig::instance().privateDBusAddressPath()); 0081 if (privateDBusAddressFile.exists()) 0082 privateDBusAddressFile.resize(0); 0083 0084 // Update session bus usability state 0085 hasUsableSessionBus = false; 0086 } 0087 0088 // Start daemon 0089 m_splashScreen->showMessage( 0090 i18n("Launching daemon") + QStringLiteral("\n"), 0091 Qt::AlignHCenter | Qt::AlignBottom, 0092 Qt::black 0093 ); 0094 0095 // Here we will try to bring our private session D-Bus 0096 if (!hasUsableSessionBus) { 0097 qDebug() << "Launching private session D-Bus."; 0098 DBusHelper::macosUnsetLaunchctlEnv(); 0099 DBusHelper::launchDBusDaemon(); 0100 // Wait for dbus daemon env 0101 QProcess getLaunchdDBusEnv; 0102 m_splashScreen->showMessage( 0103 i18n("Waiting D-Bus") + QStringLiteral("\n"), 0104 Qt::AlignHCenter | Qt::AlignBottom, 0105 Qt::black 0106 ); 0107 int retry = 0; 0108 getLaunchdDBusEnv.setProgram(QStringLiteral("launchctl")); 0109 getLaunchdDBusEnv.setArguments({QStringLiteral("getenv"), QStringLiteral(KDECONNECT_SESSION_DBUS_LAUNCHD_ENV)}); 0110 getLaunchdDBusEnv.start(); 0111 getLaunchdDBusEnv.waitForFinished(); 0112 0113 QString launchdDBusEnv = QString::fromLocal8Bit(getLaunchdDBusEnv.readAllStandardOutput()); 0114 0115 if (!launchdDBusEnv.isEmpty() && QDBusConnection::sessionBus().isConnected()) { 0116 qDebug() << "Private D-Bus daemon launched and connected."; 0117 hasUsableSessionBus = true; 0118 } else if (!launchdDBusEnv.isEmpty()) { 0119 // Show a warning and exit 0120 qCritical() << "Invalid " << KDECONNECT_SESSION_DBUS_LAUNCHD_ENV << "env: \"" << launchdDBusEnv << "\""; 0121 0122 QMessageBox::critical(nullptr, 0123 i18n("KDE Connect"), 0124 i18n("Cannot connect to DBus\n" 0125 "KDE Connect will quit"), 0126 QMessageBox::Abort, 0127 QMessageBox::Abort); 0128 // End the program 0129 return -1; 0130 } else { 0131 // Show a warning and exit 0132 qCritical() << "Fail to get launchctl" << KDECONNECT_SESSION_DBUS_LAUNCHD_ENV << "env"; 0133 0134 QMessageBox::critical(nullptr, 0135 i18n("KDE Connect"), 0136 i18n("Cannot connect to DBus\n" 0137 "KDE Connect will quit"), 0138 QMessageBox::Abort, 0139 QMessageBox::Abort); 0140 return -2; 0141 } 0142 0143 // After D-Bus setting up, everything should go fine 0144 QIcon kdeconnectIcon = QIcon::fromTheme(QStringLiteral("kdeconnect")); 0145 m_splashScreen->setPixmap(QPixmap(kdeconnectIcon.pixmap(256, 256))); 0146 } 0147 0148 // Start kdeconnectd, the daemon will not duplicate when there is already one 0149 if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"))) { 0150 kdeconnectd.setProgram(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd")); 0151 } else if (QFile::exists(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd"))) { 0152 kdeconnectd.setProgram(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd")); 0153 } else { 0154 QMessageBox::critical(nullptr, i18n("KDE Connect"), i18n("Cannot find kdeconnectd"), QMessageBox::Abort, QMessageBox::Abort); 0155 return -1; 0156 } 0157 kdeconnectd.startDetached(); 0158 0159 m_splashScreen->showMessage(i18n("Loading modules") + QStringLiteral("\n"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white); 0160 0161 return 0; 0162 } 0163 0164 void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray) 0165 { 0166 const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("kdeconnect-icons"), QStandardPaths::LocateDirectory); 0167 if (!iconPath.isNull()) { 0168 auto icon = QIcon::fromTheme(QStringLiteral("kdeconnectindicator")); 0169 icon.setIsMask(true); // Make icon adapt to menu bar color 0170 systray.setIconByPixmap(icon); 0171 } else { 0172 // We are in macOS dev env, just continue 0173 qWarning() << "Fail to find indicator icon, continue anyway"; 0174 } 0175 }