Indicator helper ready

This commit is contained in:
Weixuan XIAO 2019-11-16 13:13:21 +01:00 committed by Nicolas Fella
parent eb06c43bff
commit a8f080968c
3 changed files with 66 additions and 24 deletions

View file

@ -18,6 +18,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <QApplication>
#include <QFile>
#include <QIcon>
#include <QMessageBox>
#include <QStandardPaths>
#include <QThread>
#include <QDebug>
#include <KLocalizedString>
#include <dbushelper.h>
#include "indicatorhelper.h" #include "indicatorhelper.h"
IndicatorHelper::IndicatorHelper() {} IndicatorHelper::IndicatorHelper() {}
@ -29,18 +41,22 @@ void IndicatorHelper::postInit() {}
void IndicatorHelper::iconPathHook() {} void IndicatorHelper::iconPathHook() {}
void IndicatorHelper::dbusHook() {} void IndicatorHelper::daemonHook(QProcess &kdeconnectd)
{
Q_UNUSED(kdeconnectd);
}
void IndicatorHelper::qSystemTrayIconHook(QSystemTrayIcon &systray) #ifdef QSYSTRAY
void IndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
{ {
systray.setIcon(QIcon::fromTheme(QStringLiteral("kdeconnectindicatordark"))); systray.setIcon(QIcon::fromTheme(QStringLiteral("kdeconnectindicatordark")));
} }
#else
void IndicatorHelper::kStatusNotifierItemHook(KStatusNotifierItem &systray) void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
{ {
systray.setIconByName(QStringLiteral("kdeconnectindicatordark")); systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
} }
#endif
MacOSIndicatorHelper::MacOSIndicatorHelper() MacOSIndicatorHelper::MacOSIndicatorHelper()
{ {
@ -54,10 +70,6 @@ MacOSIndicatorHelper::MacOSIndicatorHelper()
MacOSIndicatorHelper::~MacOSIndicatorHelper() {} MacOSIndicatorHelper::~MacOSIndicatorHelper() {}
void MacOSIndicatorHelper::preInit() {}
void MacOSIndicatorHelper::postInit() {}
void MacOSIndicatorHelper::iconPathHook() void MacOSIndicatorHelper::iconPathHook()
{ {
const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory); const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory);
@ -76,7 +88,6 @@ void MacOSIndicatorHelper::daemonHook(QProcess &kdeconnectd)
// Start kdeconnectd // Start kdeconnectd
m_splashScreen->showMessage(i18n("Launching daemon") + QStringLiteral("\n"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white); m_splashScreen->showMessage(i18n("Launching daemon") + QStringLiteral("\n"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white);
QProcess kdeconnectd;
if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"))) { if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"))) {
kdeconnectd.startDetached(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd")); kdeconnectd.startDetached(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"));
} else if (QFile::exists(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd"))) { } else if (QFile::exists(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd"))) {
@ -86,7 +97,6 @@ void MacOSIndicatorHelper::daemonHook(QProcess &kdeconnectd)
i18n("Cannot find kdeconnectd"), i18n("Cannot find kdeconnectd"),
QMessageBox::Abort, QMessageBox::Abort,
QMessageBox::Abort); QMessageBox::Abort);
return -1;
} }
// Wait for dbus daemon env // Wait for dbus daemon env
@ -115,8 +125,6 @@ void MacOSIndicatorHelper::daemonHook(QProcess &kdeconnectd)
"KDE Connect will quit"), "KDE Connect will quit"),
QMessageBox::Abort, QMessageBox::Abort,
QMessageBox::Abort); QMessageBox::Abort);
return -1;
} else { } else {
QThread::sleep(3); // Retry after 3s QThread::sleep(3); // Retry after 3s
retry++; retry++;
@ -126,8 +134,15 @@ void MacOSIndicatorHelper::daemonHook(QProcess &kdeconnectd)
m_splashScreen->showMessage(i18n("Loading modules") + QStringLiteral("\n"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white); m_splashScreen->showMessage(i18n("Loading modules") + QStringLiteral("\n"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white);
} }
void MacOSIndicatorHelper::kStatusNotifierItemHook(KStatusNotifierItem &systray) #ifdef QSYSTRAY
void MacOSIndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
{ {
Q_UNUSED(systray);
}
#else
void MacOSIndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
{
const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory);
if (!iconPath.isNull()) { if (!iconPath.isNull()) {
systray.setIconByName(QStringLiteral("kdeconnectindicatordark")); systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
} else { } else {
@ -135,6 +150,7 @@ void MacOSIndicatorHelper::kStatusNotifierItemHook(KStatusNotifierItem &systray)
qWarning() << "Fail to find indicator icon, continue anyway"; qWarning() << "Fail to find indicator icon, continue anyway";
} }
} }
#endif
WindowsIndicatorHelper::WindowsIndicatorHelper() {} WindowsIndicatorHelper::WindowsIndicatorHelper() {}
WindowsIndicatorHelper::~WindowsIndicatorHelper() {} WindowsIndicatorHelper::~WindowsIndicatorHelper() {}
@ -144,8 +160,14 @@ void WindowsIndicatorHelper::daemonHook(QProcess &kdeconnectd)
kdeconnectd.start(QStringLiteral("kdeconnectd.exe")); kdeconnectd.start(QStringLiteral("kdeconnectd.exe"));
} }
void WindowsIndicatorHelper::qSystemTrayIconHook(QSystemTrayIcon &systray) #ifdef QSYSTRAY
void WindowsIndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
{ {
systray.setIcon(QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnectindicatorwin.svg")))); systray.setIcon(QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnectindicatorwin.svg"))));
} }
#else
void WindowsIndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
{
Q_UNUSED(systray);
}
#endif

View file

@ -40,8 +40,11 @@ public:
virtual void daemonHook(QProcess &kdeconnectd); virtual void daemonHook(QProcess &kdeconnectd);
virtual void qSystemTrayIconHook(QSystemTrayIcon &systray); #ifdef QSYSTRAY
virtual void kStatusNotifierItemHook(KStatusNotifierItem &systray); virtual void systrayIconHook(QSystemTrayIcon &systray);
#else
virtual void systrayIconHook(KStatusNotifierItem &systray);
#endif
}; };
@ -50,6 +53,16 @@ class MacOSIndicatorHelper : public IndicatorHelper
public: public:
MacOSIndicatorHelper(); MacOSIndicatorHelper();
~MacOSIndicatorHelper(); ~MacOSIndicatorHelper();
void iconPathHook();
void daemonHook(QProcess &kdeconnectd);
#ifdef QSYSTRAY
void systrayIconHook(QSystemTrayIcon &systray);
#else
void systrayIconHook(KStatusNotifierItem &systray);
#endif
private: private:
QSplashScreen *m_splashScreen; QSplashScreen *m_splashScreen;
}; };
@ -60,4 +73,12 @@ class WindowsIndicatorHelper : public IndicatorHelper
public: public:
WindowsIndicatorHelper(); WindowsIndicatorHelper();
~WindowsIndicatorHelper(); ~WindowsIndicatorHelper();
void daemonHook(QProcess &kdeconnectd);
#ifdef QSYSTRAY
void systrayIconHook(QSystemTrayIcon &systray);
#else
void systrayIconHook(KStatusNotifierItem &systray);
#endif
}; };

View file

@ -21,7 +21,6 @@
#include <QApplication> #include <QApplication>
#include <QProcess> #include <QProcess>
#include <QThread> #include <QThread>
#include <QMessageBox>
#ifdef QSYSTRAY #ifdef QSYSTRAY
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
@ -56,9 +55,9 @@ int main(int argc, char** argv)
IndicatorHelper *helper; IndicatorHelper *helper;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
helper = new WindowsIndicatorHelper();
#elif (defined Q_OS_MAC) #elif defined Q_OS_MAC
helper = new MacOSIndicatorHelper();
#else #else
helper = new IndicatorHelper(); helper = new IndicatorHelper();
#endif #endif
@ -130,7 +129,7 @@ int main(int argc, char** argv)
#ifdef QSYSTRAY #ifdef QSYSTRAY
QSystemTrayIcon systray; QSystemTrayIcon systray;
helper->qSystemTrayIconHook(systray); helper->systrayIconHook(systray);
systray.setVisible(true); systray.setVisible(true);
systray.setToolTip(QStringLiteral("KDE Connect")); systray.setToolTip(QStringLiteral("KDE Connect"));
QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() { QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
@ -140,7 +139,7 @@ int main(int argc, char** argv)
systray.setContextMenu(menu); systray.setContextMenu(menu);
#else #else
KStatusNotifierItem systray; KStatusNotifierItem systray;
helper->kStatusNotifierItemHook(systray); helper->systrayIconHook(systray);
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);