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 <QFile>
#include <QCoreApplication>
#include <QStandardPaths>
#include "kdeconnectconfig.h"
#ifdef Q_OS_MAC
#include <CoreFoundation/CFBundle.h>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#endif
@ -104,35 +104,23 @@ void DBusInstancePrivate::launchDBusDaemon()
// Start dbus daemon
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);
m_dbusProcess->setArguments({QStringLiteral("--print-address"),
QStringLiteral("--nofork"),
QStringLiteral("--config-file=") + kdeconnectDBusConfiguration,
QStringLiteral("--address=") + QStringLiteral(KDECONNECT_PRIVATE_DBUS_ADDR)});
m_dbusProcess->setWorkingDirectory(basePath);
#elif defined(Q_OS_WIN)
// On Windows
m_dbusProcess->setProgram(QStringLiteral("dbus-daemon.exe"));
m_dbusProcess->setArguments({QStringLiteral("--session"),
QStringLiteral("--print-address"),
QStringLiteral("--nofork"),
QStringLiteral("--address=") + QStringLiteral(KDECONNECT_PRIVATE_DBUS_ADDR)});
#else
// On Linux or other unix-like system
m_dbusProcess->setProgram(QStringLiteral("dbus-daemon"));
m_dbusProcess->setArguments({QStringLiteral("--session"),
QStringLiteral("--print-address"),
QStringLiteral("--nofork"),
QStringLiteral("--address=") + QStringLiteral(KDECONNECT_PRIVATE_DBUS_ADDR)});
#endif
QString kdeconnectDBusConfiguration;
QString dbusDaemonExecutable = QStandardPaths::findExecutable(QStringLiteral("dbus-daemon"), { QCoreApplication::applicationDirPath() });
if (!dbusDaemonExecutable.isNull()) {
kdeconnectDBusConfiguration = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("dbus-1/session.conf"));
} else {
// macOS Debug env
dbusDaemonExecutable = QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../bin/dbus-daemon");
kdeconnectDBusConfiguration = QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../share/dbus-1/session.conf");
}
m_dbusProcess->setProgram(dbusDaemonExecutable);
m_dbusProcess->setArguments({QStringLiteral("--print-address"),
QStringLiteral("--nofork"),
QStringLiteral("--config-file=") + kdeconnectDBusConfiguration,
QStringLiteral("--address=") + QStringLiteral(KDECONNECT_PRIVATE_DBUS_ADDR)
});
m_dbusProcess->setWorkingDirectory(QCoreApplication::applicationDirPath());
m_dbusProcess->setStandardOutputFile(KdeConnectConfig::instance()->privateDBusAddressPath());
m_dbusProcess->setStandardErrorFile(QProcess::nullDevice());
m_dbusProcess->start();

View file

@ -17,9 +17,6 @@ ecm_add_app_icon(indicator_SRCS ICONS
add_executable(kdeconnect-indicator ${indicator_SRCS})
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)
if (APPLE)
target_link_libraries(kdeconnect-indicator "-framework CoreFoundation")
endif()
if (WIN32)
add_compile_definitions(QSYSTRAY)
endif()

View file

@ -39,10 +39,6 @@
#include "kdeconnect-version.h"
#include "deviceindicator.h"
#ifdef Q_OS_MAC
#include <CoreFoundation/CFBundle.h>
#endif
#include <dbushelper.h>
int main(int argc, char** argv)
@ -64,14 +60,20 @@ int main(int argc, char** argv)
#ifdef Q_OS_MAC
// Unset launchctl env, avoid block
DbusHelper::macosUnsetLaunchctlEnv();
// Get bundle path
CFURLRef url = (CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
QString basePath = QUrl::fromCFURL(url).path();
// Start kdeconnectd
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
QProcess getLaunchdDBusEnv;
@ -160,9 +162,12 @@ int main(int argc, char** argv)
QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu);
#ifdef Q_OS_MAC
QStringList themeSearchPaths = QIcon::themeSearchPaths();
themeSearchPaths << basePath + QStringLiteral("Contents/Resources/icons/");
QIcon::setThemeSearchPaths(themeSearchPaths);
const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory);
if (!iconPath.isNull()) {
QStringList themeSearchPaths = QIcon::themeSearchPaths();
themeSearchPaths << iconPath;
QIcon::setThemeSearchPaths(themeSearchPaths);
}
#endif
#ifdef QSYSTRAY
@ -181,7 +186,16 @@ int main(int argc, char** argv)
systray.setContextMenu(menu);
#else
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"));
#endif
systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), QStringLiteral("KDE Connect"));
systray.setCategory(KStatusNotifierItem::Communications);
systray.setStatus(KStatusNotifierItem::Passive);