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/>.
*/
#include <QApplication>
#include <QFile>
#include <QIcon>
#include <QMessageBox>
#include <QStandardPaths>
#include <QThread>
#include <QDebug>
#include <KLocalizedString>
#include <dbushelper.h>
#include "indicatorhelper.h"
IndicatorHelper::IndicatorHelper() {}
@ -29,18 +41,22 @@ void IndicatorHelper::postInit() {}
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")));
}
void IndicatorHelper::kStatusNotifierItemHook(KStatusNotifierItem &systray)
#else
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
{
systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
}
#endif
MacOSIndicatorHelper::MacOSIndicatorHelper()
{
@ -54,10 +70,6 @@ MacOSIndicatorHelper::MacOSIndicatorHelper()
MacOSIndicatorHelper::~MacOSIndicatorHelper() {}
void MacOSIndicatorHelper::preInit() {}
void MacOSIndicatorHelper::postInit() {}
void MacOSIndicatorHelper::iconPathHook()
{
const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory);
@ -76,7 +88,6 @@ void MacOSIndicatorHelper::daemonHook(QProcess &kdeconnectd)
// Start kdeconnectd
m_splashScreen->showMessage(i18n("Launching daemon") + QStringLiteral("\n"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white);
QProcess kdeconnectd;
if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"))) {
kdeconnectd.startDetached(QCoreApplication::applicationDirPath() + QStringLiteral("/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"),
QMessageBox::Abort,
QMessageBox::Abort);
return -1;
}
// Wait for dbus daemon env
@ -115,8 +125,6 @@ void MacOSIndicatorHelper::daemonHook(QProcess &kdeconnectd)
"KDE Connect will quit"),
QMessageBox::Abort,
QMessageBox::Abort);
return -1;
} else {
QThread::sleep(3); // Retry after 3s
retry++;
@ -126,8 +134,15 @@ void MacOSIndicatorHelper::daemonHook(QProcess &kdeconnectd)
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()) {
systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
} else {
@ -135,6 +150,7 @@ void MacOSIndicatorHelper::kStatusNotifierItemHook(KStatusNotifierItem &systray)
qWarning() << "Fail to find indicator icon, continue anyway";
}
}
#endif
WindowsIndicatorHelper::WindowsIndicatorHelper() {}
WindowsIndicatorHelper::~WindowsIndicatorHelper() {}
@ -144,8 +160,14 @@ void WindowsIndicatorHelper::daemonHook(QProcess &kdeconnectd)
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"))));
}
#else
void WindowsIndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
{
Q_UNUSED(systray);
}
#endif

View file

@ -40,8 +40,11 @@ public:
virtual void daemonHook(QProcess &kdeconnectd);
virtual void qSystemTrayIconHook(QSystemTrayIcon &systray);
virtual void kStatusNotifierItemHook(KStatusNotifierItem &systray);
#ifdef QSYSTRAY
virtual void systrayIconHook(QSystemTrayIcon &systray);
#else
virtual void systrayIconHook(KStatusNotifierItem &systray);
#endif
};
@ -50,6 +53,16 @@ class MacOSIndicatorHelper : public IndicatorHelper
public:
MacOSIndicatorHelper();
~MacOSIndicatorHelper();
void iconPathHook();
void daemonHook(QProcess &kdeconnectd);
#ifdef QSYSTRAY
void systrayIconHook(QSystemTrayIcon &systray);
#else
void systrayIconHook(KStatusNotifierItem &systray);
#endif
private:
QSplashScreen *m_splashScreen;
};
@ -60,4 +73,12 @@ class WindowsIndicatorHelper : public IndicatorHelper
public:
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 <QProcess>
#include <QThread>
#include <QMessageBox>
#ifdef QSYSTRAY
#include <QSystemTrayIcon>
@ -56,9 +55,9 @@ int main(int argc, char** argv)
IndicatorHelper *helper;
#ifdef Q_OS_WIN
#elif (defined Q_OS_MAC)
helper = new WindowsIndicatorHelper();
#elif defined Q_OS_MAC
helper = new MacOSIndicatorHelper();
#else
helper = new IndicatorHelper();
#endif
@ -130,7 +129,7 @@ int main(int argc, char** argv)
#ifdef QSYSTRAY
QSystemTrayIcon systray;
helper->qSystemTrayIconHook(systray);
helper->systrayIconHook(systray);
systray.setVisible(true);
systray.setToolTip(QStringLiteral("KDE Connect"));
QObject::connect(&model, &DevicesModel::rowsChanged, &model, [&systray, &model]() {
@ -140,7 +139,7 @@ int main(int argc, char** argv)
systray.setContextMenu(menu);
#else
KStatusNotifierItem systray;
helper->kStatusNotifierItemHook(systray);
helper->systrayIconHook(systray);
systray.setToolTip(QStringLiteral("kdeconnect"), QStringLiteral("KDE Connect"), QStringLiteral("KDE Connect"));
systray.setCategory(KStatusNotifierItem::Communications);
systray.setStatus(KStatusNotifierItem::Passive);