kdeconnect-kde/indicator/main.cpp

202 lines
7.2 KiB
C++
Raw Normal View History

/*
* SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include <QApplication>
#include <QPointer>
#include <QProcess>
2019-06-18 21:51:13 +01:00
#include <QThread>
#ifdef Q_OS_WIN
#include <Windows.h>
#endif
#ifdef Q_OS_WIN
#include <QSystemTrayIcon>
#else
#include <KStatusNotifierItem>
#endif
#include <KAboutData>
#include <KCMultiDialog>
#include <KColorSchemeManager>
#include <KCrash>
#include <KDBusService>
#include <KLocalizedString>
#include "deviceindicator.h"
#include "interfaces/dbusinterfaces.h"
#include "interfaces/devicesmodel.h"
#include "interfaces/devicessortproxymodel.h"
#include "kdeconnect-version.h"
2019-06-09 16:28:49 +01:00
#include <dbushelper.h>
#include "indicatorhelper.h"
int main(int argc, char **argv)
{
#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
QIcon::setFallbackThemeName(QStringLiteral("breeze"));
QApplication app(argc, argv);
KAboutData about(QStringLiteral("kdeconnect-indicator"),
i18n("KDE Connect Indicator"),
QStringLiteral(KDECONNECT_VERSION_STRING),
i18n("KDE Connect Indicator tool"),
KAboutLicense::GPL,
i18n("(C) 2016 Aleix Pol Gonzalez"));
KAboutData::setApplicationData(about);
KCrash::initialize();
#ifdef Q_OS_WIN
KColorSchemeManager manager;
QApplication::setStyle(QStringLiteral("breeze"));
IndicatorHelper helper(QUrl::fromLocalFile(qApp->applicationDirPath()));
#else
IndicatorHelper helper;
#endif
helper.preInit();
2019-12-22 07:49:55 +00:00
// Run Daemon initialization step
// When run from macOS app bundle, D-Bus call should be later than kdeconnectd and D-Bus daemon
QProcess kdeconnectd;
if (helper.daemonHook(kdeconnectd)) {
return -1;
}
KDBusService dbusService(KDBusService::Unique);
// Trigger loading the KIconLoader plugin
about.setProgramLogo(QIcon(QStringLiteral(":/icons/kdeconnect/kdeconnect.svg")));
DevicesModel model;
model.setDisplayFilter(DevicesModel::Reachable | DevicesModel::Paired);
DevicesSortProxyModel proxyModel;
proxyModel.setSourceModel(&model);
QMenu *menu = new QMenu;
QPointer<KCMultiDialog> dialog;
DaemonDbusInterface iface;
auto refreshMenu = [&iface, &proxyModel, &menu, &dialog]() {
menu->clear();
auto configure = menu->addAction(QIcon::fromTheme(QStringLiteral("configure")), i18n("Configure..."));
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")));
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
dialog->raise();
} else {
dialog->raise();
dialog->activateWindow();
}
});
for (int i = 0, count = proxyModel.rowCount(); i < count; ++i) {
QObject *deviceObject = proxyModel.data(proxyModel.index(i, 0), DevicesModel::DeviceRole).value<QObject *>();
DeviceDbusInterface *device = qobject_cast<DeviceDbusInterface *>(deviceObject);
if (device == nullptr)
continue;
auto indicator = new DeviceIndicator(device);
QObject::connect(device, &DeviceDbusInterface::destroyed, indicator, &QObject::deleteLater);
menu->addMenu(indicator);
}
const QStringList requests = iface.pairingRequests();
if (!requests.isEmpty()) {
menu->addSection(i18n("Pairing requests"));
for (const auto &req : requests) {
DeviceDbusInterface *dev = new DeviceDbusInterface(req, menu);
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);
pairMenu->addAction(i18n("Reject"), dev, &DeviceDbusInterface::cancelPairing);
}
}
// Add quit menu
#if defined Q_OS_MAC
menu->addAction(i18n("Quit"), []() {
auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect.daemon"),
QStringLiteral("/MainApplication"),
QStringLiteral("org.qtproject.Qt.QCoreApplication"),
QStringLiteral("quit"));
QDBusConnection::sessionBus().call(message, QDBus::NoBlock);
qApp->quit();
});
#elif defined Q_OS_WIN
menu->addAction(QIcon::fromTheme(QStringLiteral("application-exit")), i18n("Quit"), []() {
qApp->quit();
});
#endif
};
QObject::connect(&iface, &DaemonDbusInterface::pairingRequestsChanged, &model, refreshMenu);
QObject::connect(&model, &DevicesModel::rowsInserted, &model, refreshMenu);
QObject::connect(&model, &DevicesModel::rowsRemoved, &model, refreshMenu);
// Run icon to add icon path (if necessary)
helper.iconPathHook();
2019-06-18 21:51:13 +01:00
#ifdef Q_OS_WIN
QSystemTrayIcon systray;
helper.systrayIconHook(systray);
systray.setVisible(true);
2019-06-18 21:51:13 +01:00
systray.setToolTip(QStringLiteral("KDE Connect"));
QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
systray.setToolTip(i18np("%1 device connected", "%1 devices connected", model.rowCount()));
});
QObject::connect(&systray, &QSystemTrayIcon::activated, [](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) {
const QString kdeconnectAppExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-app"), {QCoreApplication::applicationDirPath()});
if (!kdeconnectAppExecutable.isEmpty()) {
QProcess::startDetached(kdeconnectAppExecutable, {});
}
}
});
systray.setContextMenu(menu);
#else
KStatusNotifierItem systray;
helper.systrayIconHook(systray);
systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), QStringLiteral("KDE Connect"));
systray.setCategory(KStatusNotifierItem::Communications);
systray.setStatus(KStatusNotifierItem::Passive);
systray.setStandardActionsEnabled(false);
QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
const auto count = model.rowCount();
Change tray icon so it is the right color on MacOS ## Summary On MacOS the system tray icon is off-color because it uses the gray-colored `kdeconnectindicatordark` icon which does not match the rest of MacOS. ![image](/uploads/430933399d9570dc1c59807e4715e87b/image.png) BUG: 430226 I've used two patches to fix this: 1. Always use the status `KStatusNotifierItem::Passive` on MacOS. `KStatusNotifierItem` will only mark the icon as a mask (which is needed to allow it to dynamically switch between light and dark theme) [if the status is passive](https://invent.kde.org/frameworks/knotifications/-/blob/cff7c337ab9a78708ac5594badb54e8f88f61a8d/src/kstatusnotifieritem.cpp#L1079-1081). 2. The above should theoretically be enough to fix the issue and I swear at one point it was all that was needed. However, to fix this issue in my dev environment I also needed to pass in a `QIcon` with `setIsMask(true)` instead of setting the icon by its name. And I also use the `kdeconnectindicator` instead of `kdeconnectindicatordark` icon. ## Test Plan The icon now renders in the correct color, regardless of whether devices are connected: ![image](/uploads/5010a07cbb5f23a286ece641c6b3879c/image.png) ![image](/uploads/2ae5d3d8aa633ebafb260febe313057c/image.png) ## Future work Once I've verified this PR is working in the right direction, I want to look into making the icon gray (and hopefully a much easier-to-see gray) when no devices are connected! For example, WireGuard, when not connected, looks like this: ![image](/uploads/43c2ef6bc7261431e878c9c1c05174f9/image.png) ![image](/uploads/f7587190648606df77ad3e3dde84098f/image.png) P.S. I've been testing off the v21.12.2 tag since the master branch doesn't compile for me, so I haven't tested this change on the latest dev commit. But there should be no conflicts.
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
systray.setStatus(count == 0 ? KStatusNotifierItem::Passive : KStatusNotifierItem::Active);
Change tray icon so it is the right color on MacOS ## Summary On MacOS the system tray icon is off-color because it uses the gray-colored `kdeconnectindicatordark` icon which does not match the rest of MacOS. ![image](/uploads/430933399d9570dc1c59807e4715e87b/image.png) BUG: 430226 I've used two patches to fix this: 1. Always use the status `KStatusNotifierItem::Passive` on MacOS. `KStatusNotifierItem` will only mark the icon as a mask (which is needed to allow it to dynamically switch between light and dark theme) [if the status is passive](https://invent.kde.org/frameworks/knotifications/-/blob/cff7c337ab9a78708ac5594badb54e8f88f61a8d/src/kstatusnotifieritem.cpp#L1079-1081). 2. The above should theoretically be enough to fix the issue and I swear at one point it was all that was needed. However, to fix this issue in my dev environment I also needed to pass in a `QIcon` with `setIsMask(true)` instead of setting the icon by its name. And I also use the `kdeconnectindicator` instead of `kdeconnectindicatordark` icon. ## Test Plan The icon now renders in the correct color, regardless of whether devices are connected: ![image](/uploads/5010a07cbb5f23a286ece641c6b3879c/image.png) ![image](/uploads/2ae5d3d8aa633ebafb260febe313057c/image.png) ## Future work Once I've verified this PR is working in the right direction, I want to look into making the icon gray (and hopefully a much easier-to-see gray) when no devices are connected! For example, WireGuard, when not connected, looks like this: ![image](/uploads/43c2ef6bc7261431e878c9c1c05174f9/image.png) ![image](/uploads/f7587190648606df77ad3e3dde84098f/image.png) P.S. I've been testing off the v21.12.2 tag since the master branch doesn't compile for me, so I haven't tested this change on the latest dev commit. But there should be no conflicts.
2022-03-02 12:06:36 +00:00
#endif
systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), i18np("%1 device connected", "%1 devices connected", count));
});
systray.setContextMenu(menu);
#endif
refreshMenu();
app.setQuitOnLastWindowClosed(false);
// Finish init
helper.postInit();
return app.exec();
}