2016-11-23 16:49:41 +00:00
|
|
|
/*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
|
2016-11-23 16:49:41 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2016-11-23 16:49:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QApplication>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QPointer>
|
2017-05-24 21:55:14 +01:00
|
|
|
#include <QProcess>
|
2019-06-18 21:51:13 +01:00
|
|
|
#include <QThread>
|
2017-03-27 19:44:12 +01:00
|
|
|
|
2023-06-12 14:30:30 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2023-06-12 14:34:43 +01:00
|
|
|
#ifdef Q_OS_WIN
|
2016-11-23 16:49:41 +00:00
|
|
|
#include <QSystemTrayIcon>
|
2017-03-27 19:44:12 +01:00
|
|
|
#else
|
|
|
|
#include <KStatusNotifierItem>
|
|
|
|
#endif
|
2016-11-23 16:49:41 +00:00
|
|
|
|
|
|
|
#include <KAboutData>
|
2017-05-24 18:59:04 +01:00
|
|
|
#include <KCMultiDialog>
|
2021-06-13 09:19:12 +01:00
|
|
|
#include <KColorSchemeManager>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <KDBusService>
|
|
|
|
#include <KLocalizedString>
|
2016-11-23 16:49:41 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "deviceindicator.h"
|
2016-11-23 16:49:41 +00:00
|
|
|
#include "interfaces/dbusinterfaces.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "interfaces/devicesmodel.h"
|
2016-11-23 16:49:41 +00:00
|
|
|
#include "kdeconnect-version.h"
|
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2019-12-11 15:00:39 +00:00
|
|
|
#include "indicatorhelper.h"
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
int main(int argc, char **argv)
|
2016-11-23 16:49:41 +00:00
|
|
|
{
|
2023-06-12 14:30:30 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
// If ran from a console, redirect the output there
|
|
|
|
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
|
|
|
|
freopen("CONOUT$", "w", stdout);
|
|
|
|
freopen("CONOUT$", "w", stderr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-27 22:03:51 +01:00
|
|
|
QIcon::setFallbackThemeName(QStringLiteral("breeze"));
|
|
|
|
|
2016-11-23 16:49:41 +00:00
|
|
|
QApplication app(argc, argv);
|
2019-06-10 15:40:28 +01:00
|
|
|
KAboutData about(QStringLiteral("kdeconnect-indicator"),
|
2016-11-23 16:49:41 +00:00
|
|
|
i18n("KDE Connect Indicator"),
|
|
|
|
QStringLiteral(KDECONNECT_VERSION_STRING),
|
|
|
|
i18n("KDE Connect Indicator tool"),
|
|
|
|
KAboutLicense::GPL,
|
|
|
|
i18n("(C) 2016 Aleix Pol Gonzalez"));
|
|
|
|
KAboutData::setApplicationData(about);
|
|
|
|
|
2021-05-28 19:42:26 +01:00
|
|
|
#ifdef Q_OS_WIN
|
2021-06-13 09:19:12 +01:00
|
|
|
KColorSchemeManager manager;
|
2021-06-02 22:45:21 +01:00
|
|
|
QApplication::setStyle(QStringLiteral("breeze"));
|
2021-05-28 19:42:26 +01:00
|
|
|
IndicatorHelper helper(QUrl::fromLocalFile(qApp->applicationDirPath()));
|
|
|
|
#else
|
2019-12-11 15:00:39 +00:00
|
|
|
IndicatorHelper helper;
|
2021-05-28 19:42:26 +01:00
|
|
|
#endif
|
2017-05-24 21:55:14 +01:00
|
|
|
|
2019-12-11 15:00:39 +00:00
|
|
|
helper.preInit();
|
|
|
|
|
2019-12-22 07:49:55 +00:00
|
|
|
// Run Daemon initialization step
|
2021-06-22 14:03:46 +01:00
|
|
|
// When run from macOS app bundle, D-Bus call should be later than kdeconnectd and D-Bus daemon
|
2019-12-11 15:00:39 +00:00
|
|
|
QProcess kdeconnectd;
|
|
|
|
if (helper.daemonHook(kdeconnectd)) {
|
2019-07-30 14:46:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2019-07-09 20:42:08 +01:00
|
|
|
|
2021-06-22 14:03:46 +01:00
|
|
|
KDBusService dbusService(KDBusService::Unique);
|
|
|
|
|
|
|
|
// Trigger loading the KIconLoader plugin
|
|
|
|
about.setProgramLogo(QIcon(QStringLiteral(":/icons/kdeconnect/kdeconnect.svg")));
|
|
|
|
|
2017-05-24 22:22:30 +01:00
|
|
|
DevicesModel model;
|
|
|
|
model.setDisplayFilter(DevicesModel::Reachable | DevicesModel::Paired);
|
2022-09-10 22:23:52 +01:00
|
|
|
QMenu *menu = new QMenu;
|
2017-01-30 23:53:26 +00:00
|
|
|
|
2020-11-14 08:24:21 +00:00
|
|
|
QPointer<KCMultiDialog> dialog;
|
|
|
|
|
2017-01-25 00:19:44 +00:00
|
|
|
DaemonDbusInterface iface;
|
2020-11-14 08:24:21 +00:00
|
|
|
|
2023-08-28 17:47:50 +01:00
|
|
|
auto refreshMenu = [&iface, &model, &menu, &dialog]() {
|
2017-01-30 23:53:26 +00:00
|
|
|
menu->clear();
|
2018-12-07 14:53:07 +00:00
|
|
|
auto configure = menu->addAction(QIcon::fromTheme(QStringLiteral("configure")), i18n("Configure..."));
|
2020-11-14 08:24:21 +00:00
|
|
|
QObject::connect(configure, &QAction::triggered, configure, [&dialog]() {
|
|
|
|
if (dialog == nullptr) {
|
|
|
|
dialog = new KCMultiDialog;
|
2023-03-08 23:35:00 +00:00
|
|
|
dialog->addModule(KPluginMetaData(QStringLiteral("plasma/kcms/systemsettings_qwidgets/kcm_kdeconnect")));
|
2020-11-14 08:24:21 +00:00
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
dialog->show();
|
2022-02-26 10:23:20 +00:00
|
|
|
dialog->raise();
|
2020-11-14 08:24:21 +00:00
|
|
|
} else {
|
2022-02-26 10:23:20 +00:00
|
|
|
dialog->raise();
|
2020-11-14 08:24:21 +00:00
|
|
|
dialog->activateWindow();
|
|
|
|
}
|
2017-01-11 16:19:17 +00:00
|
|
|
});
|
2022-09-10 22:23:52 +01:00
|
|
|
for (int i = 0, count = model.rowCount(); i < count; ++i) {
|
|
|
|
DeviceDbusInterface *device = model.getDevice(i);
|
2016-11-23 16:49:41 +00:00
|
|
|
auto indicator = new DeviceIndicator(device);
|
|
|
|
QObject::connect(device, &DeviceDbusInterface::destroyed, indicator, &QObject::deleteLater);
|
|
|
|
|
2017-01-11 16:19:17 +00:00
|
|
|
menu->addMenu(indicator);
|
2016-11-23 16:49:41 +00:00
|
|
|
}
|
2017-01-25 00:19:44 +00:00
|
|
|
const QStringList requests = iface.pairingRequests();
|
|
|
|
if (!requests.isEmpty()) {
|
|
|
|
menu->addSection(i18n("Pairing requests"));
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
for (const auto &req : requests) {
|
|
|
|
DeviceDbusInterface *dev = new DeviceDbusInterface(req, menu);
|
2017-01-25 00:19:44 +00:00
|
|
|
auto pairMenu = menu->addMenu(dev->name());
|
2023-07-22 18:58:18 +01:00
|
|
|
pairMenu->addAction(i18nc("Accept a pairing request", "Pair"), dev, &DeviceDbusInterface::acceptPairing);
|
2023-06-01 01:25:37 +01:00
|
|
|
pairMenu->addAction(i18n("Reject"), dev, &DeviceDbusInterface::cancelPairing);
|
2017-01-25 00:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-09 22:36:42 +01:00
|
|
|
// Add quit menu
|
2020-10-23 06:55:36 +01:00
|
|
|
#if defined Q_OS_MAC
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
menu->addAction(i18n("Quit"), []() {
|
2019-07-20 10:17:03 +01:00
|
|
|
auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect.daemon"),
|
2022-09-10 22:23:52 +01:00
|
|
|
QStringLiteral("/MainApplication"),
|
|
|
|
QStringLiteral("org.qtproject.Qt.QCoreApplication"),
|
|
|
|
QStringLiteral("quit"));
|
2022-04-12 06:40:03 +01:00
|
|
|
QDBusConnection::sessionBus().call(message, QDBus::NoBlock);
|
|
|
|
qApp->quit();
|
2019-06-09 22:36:42 +01:00
|
|
|
});
|
2020-10-23 06:55:36 +01:00
|
|
|
#elif defined Q_OS_WIN
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
menu->addAction(QIcon::fromTheme(QStringLiteral("application-exit")), i18n("Quit"), []() {
|
|
|
|
qApp->quit();
|
|
|
|
});
|
2019-06-09 22:36:42 +01:00
|
|
|
#endif
|
2017-01-11 16:19:17 +00:00
|
|
|
};
|
|
|
|
|
2023-08-08 19:23:25 +01:00
|
|
|
QObject::connect(&iface, &DaemonDbusInterface::pairingRequestsChanged, &model, refreshMenu);
|
2017-01-11 16:19:17 +00:00
|
|
|
QObject::connect(&model, &DevicesModel::rowsInserted, &model, refreshMenu);
|
|
|
|
QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu);
|
2016-11-23 16:49:41 +00:00
|
|
|
|
2019-12-11 15:00:39 +00:00
|
|
|
// Run icon to add icon path (if necessary)
|
|
|
|
helper.iconPathHook();
|
2019-06-18 21:51:13 +01:00
|
|
|
|
2023-06-12 14:34:43 +01:00
|
|
|
#ifdef Q_OS_WIN
|
2017-03-27 19:44:12 +01:00
|
|
|
QSystemTrayIcon systray;
|
2019-12-11 15:00:39 +00:00
|
|
|
helper.systrayIconHook(systray);
|
2017-03-27 19:44:12 +01:00
|
|
|
systray.setVisible(true);
|
2019-06-18 21:51:13 +01:00
|
|
|
systray.setToolTip(QStringLiteral("KDE Connect"));
|
2016-11-23 18:35:39 +00:00
|
|
|
QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
|
|
|
|
systray.setToolTip(i18np("%1 device connected", "%1 devices connected", model.rowCount()));
|
|
|
|
});
|
2021-07-12 01:05:16 +01:00
|
|
|
QObject::connect(&systray, &QSystemTrayIcon::activated, [](QSystemTrayIcon::ActivationReason reason) {
|
2021-07-02 18:39:41 +01:00
|
|
|
if (reason == QSystemTrayIcon::Trigger) {
|
2022-09-10 22:23:52 +01:00
|
|
|
const QString kdeconnectAppExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-app"), {QCoreApplication::applicationDirPath()});
|
2021-07-02 18:39:41 +01:00
|
|
|
if (!kdeconnectAppExecutable.isEmpty()) {
|
2021-07-12 01:05:16 +01:00
|
|
|
QProcess::startDetached(kdeconnectAppExecutable, {});
|
2021-07-02 18:39:41 +01:00
|
|
|
}
|
2020-11-14 08:24:21 +00:00
|
|
|
}
|
|
|
|
});
|
2016-11-23 18:35:39 +00:00
|
|
|
|
2017-01-30 23:53:26 +00:00
|
|
|
systray.setContextMenu(menu);
|
2017-03-27 19:44:12 +01:00
|
|
|
#else
|
|
|
|
KStatusNotifierItem systray;
|
2019-12-11 15:00:39 +00:00
|
|
|
helper.systrayIconHook(systray);
|
2019-06-10 15:40:28 +01:00
|
|
|
systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), QStringLiteral("KDE Connect"));
|
2017-03-27 19:44:12 +01:00
|
|
|
systray.setCategory(KStatusNotifierItem::Communications);
|
|
|
|
systray.setStatus(KStatusNotifierItem::Passive);
|
|
|
|
systray.setStandardActionsEnabled(false);
|
|
|
|
QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
|
|
|
|
const auto count = model.rowCount();
|
2022-03-02 12:06:36 +00:00
|
|
|
#ifndef Q_OS_MACOS // On MacOS, setting status to Active disables color theme syncing of the menu icon
|
2017-03-27 19:44:12 +01:00
|
|
|
systray.setStatus(count == 0 ? KStatusNotifierItem::Passive : KStatusNotifierItem::Active);
|
2022-03-02 12:06:36 +00:00
|
|
|
#endif
|
2017-03-27 19:44:12 +01:00
|
|
|
systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), i18np("%1 device connected", "%1 devices connected", count));
|
|
|
|
});
|
|
|
|
|
|
|
|
systray.setContextMenu(menu);
|
|
|
|
#endif
|
2017-01-30 23:53:26 +00:00
|
|
|
|
|
|
|
refreshMenu();
|
2017-01-26 23:54:57 +00:00
|
|
|
|
2017-05-24 18:59:04 +01:00
|
|
|
app.setQuitOnLastWindowClosed(false);
|
|
|
|
|
2019-12-11 15:00:39 +00:00
|
|
|
// Finish init
|
|
|
|
helper.postInit();
|
|
|
|
|
2016-11-23 16:49:41 +00:00
|
|
|
return app.exec();
|
|
|
|
}
|