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>
|
2017-05-24 21:55:14 +01:00
|
|
|
#include <QProcess>
|
2019-06-18 21:51:13 +01:00
|
|
|
#include <QThread>
|
2020-11-14 08:24:21 +00:00
|
|
|
#include <QPointer>
|
2017-03-27 19:44:12 +01:00
|
|
|
|
|
|
|
#ifdef QSYSTRAY
|
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
|
|
|
|
2017-05-24 21:52:14 +01:00
|
|
|
#include <KDBusService>
|
2016-11-23 16:49:41 +00:00
|
|
|
#include <KAboutData>
|
2017-05-24 18:59:04 +01:00
|
|
|
#include <KCMultiDialog>
|
2016-11-23 16:49:41 +00:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
|
|
|
#include "interfaces/devicesmodel.h"
|
|
|
|
#include "interfaces/dbusinterfaces.h"
|
|
|
|
#include "kdeconnect-version.h"
|
|
|
|
#include "deviceindicator.h"
|
|
|
|
|
2019-06-09 16:28:49 +01:00
|
|
|
#include <dbushelper.h>
|
|
|
|
|
2019-12-11 15:00:39 +00:00
|
|
|
#include "indicatorhelper.h"
|
|
|
|
|
2016-11-23 16:49:41 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
2021-01-29 15:04:51 +00:00
|
|
|
app.setAttribute(Qt::AA_UseHighDpiPixmaps, true);
|
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-06-07 22:04:26 +01:00
|
|
|
// Trigger loading the KIconLoader plugin
|
|
|
|
about.setProgramLogo(QIcon(QStringLiteral(":/icons/kdeconnect/kdeconnect.svg")));
|
|
|
|
|
2021-05-28 19:42:26 +01:00
|
|
|
#ifdef Q_OS_WIN
|
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
|
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
|
|
|
|
2017-05-24 22:22:30 +01:00
|
|
|
KDBusService dbusService(KDBusService::Unique);
|
|
|
|
|
|
|
|
DevicesModel model;
|
|
|
|
model.setDisplayFilter(DevicesModel::Reachable | DevicesModel::Paired);
|
2017-09-03 20:39:44 +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
|
|
|
|
|
|
|
auto refreshMenu = [&iface, &model, &menu, &helper, &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;
|
|
|
|
dialog->addModule(QStringLiteral("kcm_kdeconnect"));
|
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
dialog->show();
|
|
|
|
} else {
|
|
|
|
dialog->activateWindow();
|
|
|
|
}
|
2017-01-11 16:19:17 +00:00
|
|
|
});
|
|
|
|
for (int i=0, count = model.rowCount(); i<count; ++i) {
|
2016-11-23 18:00:20 +00:00
|
|
|
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"));
|
|
|
|
|
2017-09-03 20:39:44 +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());
|
|
|
|
pairMenu->addAction(i18n("Pair"), dev, &DeviceDbusInterface::acceptPairing);
|
|
|
|
pairMenu->addAction(i18n("Reject"), dev, &DeviceDbusInterface::rejectPairing);
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 22:36:42 +01:00
|
|
|
// Add quit menu
|
2020-10-23 06:55:36 +01:00
|
|
|
#if defined Q_OS_MAC
|
|
|
|
|
2019-06-09 22:36:42 +01:00
|
|
|
menu->addAction(i18n("Quit"), [](){
|
2019-07-20 10:17:03 +01:00
|
|
|
auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect.daemon"),
|
|
|
|
QStringLiteral("/MainApplication"),
|
|
|
|
QStringLiteral("org.qtproject.Qt.QCoreApplication"),
|
|
|
|
QStringLiteral("quit"));
|
2019-09-12 18:46:10 +01:00
|
|
|
DBusHelper::sessionBus().call(message, QDBus::NoBlock); // Close our daemon
|
|
|
|
message = QDBusMessage::createMethodCall(qApp->applicationName(),
|
|
|
|
QStringLiteral("/MainApplication"),
|
|
|
|
QStringLiteral("org.qtproject.Qt.QCoreApplication"),
|
|
|
|
QStringLiteral("quit"));
|
|
|
|
DBusHelper::sessionBus().call(message, QDBus::NoBlock); // Close our indicator
|
2019-06-09 22:36:42 +01:00
|
|
|
});
|
2020-10-23 06:55:36 +01:00
|
|
|
#elif defined Q_OS_WIN
|
|
|
|
|
2021-06-12 00:52:54 +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
|
|
|
};
|
|
|
|
|
2017-01-25 00:19:44 +00:00
|
|
|
QObject::connect(&iface, &DaemonDbusInterface::pairingRequestsChangedProxy, &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
|
|
|
|
2017-03-27 19:44:12 +01:00
|
|
|
#ifdef QSYSTRAY
|
|
|
|
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()));
|
|
|
|
});
|
2020-11-14 08:24:21 +00:00
|
|
|
QObject::connect(&systray, &QSystemTrayIcon::activated, [&iface](QSystemTrayIcon::ActivationReason reason) {
|
|
|
|
if (reason == QSystemTrayIcon::DoubleClick) {
|
|
|
|
iface.openConfiguration();
|
|
|
|
}
|
|
|
|
});
|
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();
|
|
|
|
systray.setStatus(count == 0 ? KStatusNotifierItem::Passive : KStatusNotifierItem::Active);
|
|
|
|
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();
|
|
|
|
}
|