Optimization for executable path and resource path on macOS

This commit is contained in:
Weixuan Xiao 2019-07-30 13:46:18 +00:00
parent 3c783c4505
commit 5fadeabce8
3 changed files with 44 additions and 45 deletions

View file

@ -27,11 +27,11 @@
#include <QUrl> #include <QUrl>
#include <QFile> #include <QFile>
#include <QCoreApplication> #include <QCoreApplication>
#include <QStandardPaths>
#include "kdeconnectconfig.h" #include "kdeconnectconfig.h"
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
#include <CoreFoundation/CFBundle.h>
#include <QRegularExpression> #include <QRegularExpression>
#include <QRegularExpressionMatch> #include <QRegularExpressionMatch>
#endif #endif
@ -104,35 +104,23 @@ void DBusInstancePrivate::launchDBusDaemon()
// Start dbus daemon // Start dbus daemon
m_dbusProcess = new QProcess(); m_dbusProcess = new QProcess();
#ifdef Q_OS_MAC
// On macOS, assuming the executable is in Contents/MacOS
CFURLRef url = (CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
QString basePath = QUrl::fromCFURL(url).path();
QString kdeconnectDBusExecutable = basePath + QStringLiteral("Contents/MacOS/dbus-daemon"),
kdeconnectDBusConfiguration = basePath + QStringLiteral("Contents/Resources/dbus-1/session.conf");
qCDebug(KDECONNECT_CORE) << "App package path: " << basePath;
m_dbusProcess->setProgram(kdeconnectDBusExecutable); QString kdeconnectDBusConfiguration;
m_dbusProcess->setArguments({QStringLiteral("--print-address"), QString dbusDaemonExecutable = QStandardPaths::findExecutable(QStringLiteral("dbus-daemon"), { QCoreApplication::applicationDirPath() });
QStringLiteral("--nofork"), if (!dbusDaemonExecutable.isNull()) {
QStringLiteral("--config-file=") + kdeconnectDBusConfiguration, kdeconnectDBusConfiguration = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("dbus-1/session.conf"));
QStringLiteral("--address=") + QStringLiteral(KDECONNECT_PRIVATE_DBUS_ADDR)}); } else {
m_dbusProcess->setWorkingDirectory(basePath); // macOS Debug env
#elif defined(Q_OS_WIN) dbusDaemonExecutable = QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../bin/dbus-daemon");
// On Windows kdeconnectDBusConfiguration = QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../share/dbus-1/session.conf");
m_dbusProcess->setProgram(QStringLiteral("dbus-daemon.exe")); }
m_dbusProcess->setArguments({QStringLiteral("--session"), m_dbusProcess->setProgram(dbusDaemonExecutable);
QStringLiteral("--print-address"), m_dbusProcess->setArguments({QStringLiteral("--print-address"),
QStringLiteral("--nofork"), QStringLiteral("--nofork"),
QStringLiteral("--address=") + QStringLiteral(KDECONNECT_PRIVATE_DBUS_ADDR)}); QStringLiteral("--config-file=") + kdeconnectDBusConfiguration,
#else QStringLiteral("--address=") + QStringLiteral(KDECONNECT_PRIVATE_DBUS_ADDR)
// On Linux or other unix-like system });
m_dbusProcess->setProgram(QStringLiteral("dbus-daemon")); m_dbusProcess->setWorkingDirectory(QCoreApplication::applicationDirPath());
m_dbusProcess->setArguments({QStringLiteral("--session"),
QStringLiteral("--print-address"),
QStringLiteral("--nofork"),
QStringLiteral("--address=") + QStringLiteral(KDECONNECT_PRIVATE_DBUS_ADDR)});
#endif
m_dbusProcess->setStandardOutputFile(KdeConnectConfig::instance()->privateDBusAddressPath()); m_dbusProcess->setStandardOutputFile(KdeConnectConfig::instance()->privateDBusAddressPath());
m_dbusProcess->setStandardErrorFile(QProcess::nullDevice()); m_dbusProcess->setStandardErrorFile(QProcess::nullDevice());
m_dbusProcess->start(); m_dbusProcess->start();

View file

@ -17,9 +17,6 @@ ecm_add_app_icon(indicator_SRCS ICONS
add_executable(kdeconnect-indicator ${indicator_SRCS}) add_executable(kdeconnect-indicator ${indicator_SRCS})
target_include_directories(kdeconnect-indicator PUBLIC ${CMAKE_BINARY_DIR}) target_include_directories(kdeconnect-indicator PUBLIC ${CMAKE_BINARY_DIR})
target_link_libraries(kdeconnect-indicator Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::Notifications KF5::DBusAddons KF5::KCMUtils kdeconnectinterfaces kdeconnectcore) target_link_libraries(kdeconnect-indicator Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::Notifications KF5::DBusAddons KF5::KCMUtils kdeconnectinterfaces kdeconnectcore)
if (APPLE)
target_link_libraries(kdeconnect-indicator "-framework CoreFoundation")
endif()
if (WIN32) if (WIN32)
add_compile_definitions(QSYSTRAY) add_compile_definitions(QSYSTRAY)
endif() endif()

View file

@ -39,10 +39,6 @@
#include "kdeconnect-version.h" #include "kdeconnect-version.h"
#include "deviceindicator.h" #include "deviceindicator.h"
#ifdef Q_OS_MAC
#include <CoreFoundation/CFBundle.h>
#endif
#include <dbushelper.h> #include <dbushelper.h>
int main(int argc, char** argv) int main(int argc, char** argv)
@ -64,14 +60,20 @@ int main(int argc, char** argv)
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
// Unset launchctl env, avoid block // Unset launchctl env, avoid block
DbusHelper::macosUnsetLaunchctlEnv(); DbusHelper::macosUnsetLaunchctlEnv();
// Get bundle path
CFURLRef url = (CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
QString basePath = QUrl::fromCFURL(url).path();
// Start kdeconnectd // Start kdeconnectd
QProcess kdeconnectdProcess; QProcess kdeconnectdProcess;
kdeconnectdProcess.startDetached(basePath + QStringLiteral("Contents/MacOS/kdeconnectd")); if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"))) {
kdeconnectdProcess.startDetached(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"));
} else if (QFile::exists(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd"))) {
kdeconnectdProcess.startDetached(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd"));
} else {
QMessageBox::critical(nullptr, i18n("KDE Connect"),
i18n("Cannot find kdeconnectd"),
QMessageBox::Abort,
QMessageBox::Abort);
return -1;
}
// Wait for dbus daemon env // Wait for dbus daemon env
QProcess getLaunchdDBusEnv; QProcess getLaunchdDBusEnv;
@ -160,9 +162,12 @@ int main(int argc, char** argv)
QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu); QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu);
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
QStringList themeSearchPaths = QIcon::themeSearchPaths(); const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory);
themeSearchPaths << basePath + QStringLiteral("Contents/Resources/icons/"); if (!iconPath.isNull()) {
QIcon::setThemeSearchPaths(themeSearchPaths); QStringList themeSearchPaths = QIcon::themeSearchPaths();
themeSearchPaths << iconPath;
QIcon::setThemeSearchPaths(themeSearchPaths);
}
#endif #endif
#ifdef QSYSTRAY #ifdef QSYSTRAY
@ -181,7 +186,16 @@ int main(int argc, char** argv)
systray.setContextMenu(menu); systray.setContextMenu(menu);
#else #else
KStatusNotifierItem systray; KStatusNotifierItem systray;
#ifdef Q_OS_MAC
if (!iconPath.isNull()) {
systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
} else {
// We are in macOS dev env, just continue
qWarning() << "Fail to find indicator icon, continue anyway";
}
#else
systray.setIconByName(QStringLiteral("kdeconnectindicatordark")); systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
#endif
systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), QStringLiteral("KDE Connect")); systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), QStringLiteral("KDE Connect"));
systray.setCategory(KStatusNotifierItem::Communications); systray.setCategory(KStatusNotifierItem::Communications);
systray.setStatus(KStatusNotifierItem::Passive); systray.setStatus(KStatusNotifierItem::Passive);