From 2a48167bab68561391a61ad3d66525af8b00b5bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole-Andr=C3=A9=20Rodlie?= Date: Mon, 5 Feb 2024 22:46:53 +0100 Subject: [PATCH] powerkit_systray => powerkit_app --- src/powerkit.cpp | 4 +- ...{powerkit_systray.cpp => powerkit_app.cpp} | 108 +++++++------- src/powerkit_app.h | 137 ++++++++++++++++++ src/powerkit_systray.h | 134 ----------------- 4 files changed, 193 insertions(+), 190 deletions(-) rename src/{powerkit_systray.cpp => powerkit_app.cpp} (93%) create mode 100644 src/powerkit_app.h delete mode 100644 src/powerkit_systray.h diff --git a/src/powerkit.cpp b/src/powerkit.cpp index ced4929..13c652d 100644 --- a/src/powerkit.cpp +++ b/src/powerkit.cpp @@ -8,7 +8,7 @@ #include -#include "powerkit_systray.h" +#include "powerkit_app.h" #include "powerkit_dialog.h" #include "powerkit_common.h" @@ -35,6 +35,6 @@ int main(int argc, char *argv[]) return 1; } - SysTray powerkit(a.parent()); + PowerKit::App powerkit(a.parent()); return a.exec(); } diff --git a/src/powerkit_systray.cpp b/src/powerkit_app.cpp similarity index 93% rename from src/powerkit_systray.cpp rename to src/powerkit_app.cpp index 0ef3345..ce83e64 100644 --- a/src/powerkit_systray.cpp +++ b/src/powerkit_app.cpp @@ -6,7 +6,7 @@ # See the LICENSE file for full details */ -#include "powerkit_systray.h" +#include "powerkit_app.h" #include "powerkit_common.h" #include "powerkit_theme.h" #include "powerkit_notify.h" @@ -27,7 +27,7 @@ using namespace PowerKit; -SysTray::SysTray(QObject *parent) +App::App(QObject *parent) : QObject(parent) , tray(nullptr) , man(nullptr) @@ -209,19 +209,19 @@ SysTray::SysTray(QObject *parent) SLOT(handleConfChanged(QString))); } -SysTray::~SysTray() +App::~App() { } // what to do when user clicks systray -void SysTray::trayActivated(QSystemTrayIcon::ActivationReason reason) +void App::trayActivated(QSystemTrayIcon::ActivationReason reason) { Q_UNUSED(reason) openSettings(); } -void SysTray::checkDevices() +void App::checkDevices() { // show/hide tray if (tray->isSystemTrayAvailable() && @@ -273,7 +273,7 @@ void SysTray::checkDevices() } // what to do when user close lid -void SysTray::handleClosedLid() +void App::handleClosedLid() { qDebug() << "lid closed"; lidWasClosed = true; @@ -314,7 +314,7 @@ void SysTray::handleClosedLid() } // what to do when user open lid -void SysTray::handleOpenedLid() +void App::handleOpenedLid() { qDebug() << "lid is now open"; lidWasClosed = false; @@ -324,7 +324,7 @@ void SysTray::handleOpenedLid() } // do something when switched to battery power -void SysTray::handleOnBattery() +void App::handleOnBattery() { if (notifyOnBattery) { showMessage(tr("On Battery"), @@ -350,7 +350,7 @@ void SysTray::handleOnBattery() } // do something when switched to ac power -void SysTray::handleOnAC() +void App::handleOnAC() { if (notifyOnAC) { showMessage(tr("On AC"), @@ -379,7 +379,7 @@ void SysTray::handleOnAC() } // load default settings -void SysTray::loadSettings() +void App::loadSettings() { qDebug() << "(re)load settings..."; @@ -494,7 +494,7 @@ void SysTray::loadSettings() } // register session services -void SysTray::registerService() +void App::registerService() { if (hasService) { return; } if (!QDBusConnection::sessionBus().isConnected()) { @@ -573,12 +573,12 @@ void SysTray::registerService() } // dbus session inhibit status handler -void SysTray::handleHasInhibitChanged(bool has_inhibit) +void App::handleHasInhibitChanged(bool has_inhibit) { if (has_inhibit) { resetTimer(); } } -void SysTray::handleLow(double left) +void App::handleLow(double left) { if (!warnOnLowBattery) { return; } double batteryLow = (double)(lowBatteryValue+critBatteryValue); @@ -594,7 +594,7 @@ void SysTray::handleLow(double left) } } -void SysTray::handleVeryLow(double left) +void App::handleVeryLow(double left) { if (!warnOnVeryLowBattery) { return; } double batteryVeryLow = (double)(critBatteryValue+1); @@ -611,7 +611,7 @@ void SysTray::handleVeryLow(double left) } // handle critical battery -void SysTray::handleCritical(double left) +void App::handleCritical(double left) { if (left<=0 || left>(double)critBatteryValue || @@ -632,7 +632,7 @@ void SysTray::handleCritical(double left) } // draw battery tray icon -void SysTray::drawBattery(double left) +void App::drawBattery(double left) { if (!showTray && tray->isVisible()) { @@ -673,7 +673,7 @@ void SysTray::drawBattery(double left) // timeout, check if idle // timeouts and xss must be >= user value and service has to be empty before suspend -void SysTray::timeout() +void App::timeout() { if (!showTray && tray->isVisible()) { tray->hide(); } @@ -724,13 +724,13 @@ void SysTray::timeout() } // reset the idle timer -void SysTray::resetTimer() +void App::resetTimer() { timeouts = 0; } // set "internal" monitor -void SysTray::setInternalMonitor() +void App::setInternalMonitor() { internalMonitor = ss->GetInternalDisplay(); qDebug() << "internal monitor set to" << internalMonitor; @@ -738,7 +738,7 @@ void SysTray::setInternalMonitor() } // is "internal" monitor connected? -bool SysTray::internalMonitorIsConnected() +bool App::internalMonitorIsConnected() { QMapIterator i(ss->GetDisplays()); while (i.hasNext()) { @@ -752,7 +752,7 @@ bool SysTray::internalMonitorIsConnected() } // is "external" monitor(s) connected? -bool SysTray::externalMonitorIsConnected() +bool App::externalMonitorIsConnected() { QMapIterator i(ss->GetDisplays()); while (i.hasNext()) { @@ -767,9 +767,9 @@ bool SysTray::externalMonitorIsConnected() } // handle new inhibits -void SysTray::handleNewInhibitScreenSaver(const QString &application, - const QString &reason, - quint32 cookie) +void App::handleNewInhibitScreenSaver(const QString &application, + const QString &reason, + quint32 cookie) { Q_UNUSED(cookie) if (notifyNewInhibitor) { @@ -780,9 +780,9 @@ void SysTray::handleNewInhibitScreenSaver(const QString &application, checkDevices(); } -void SysTray::handleNewInhibitPowerManagement(const QString &application, - const QString &reason, - quint32 cookie) +void App::handleNewInhibitPowerManagement(const QString &application, + const QString &reason, + quint32 cookie) { Q_UNUSED(cookie) if (notifyNewInhibitor) { @@ -793,41 +793,41 @@ void SysTray::handleNewInhibitPowerManagement(const QString &application, checkDevices(); } -void SysTray::handleDelInhibitScreenSaver(quint32 cookie) +void App::handleDelInhibitScreenSaver(quint32 cookie) { qDebug() << "SS INHIBITOR REMOVED" << cookie; checkDevices(); } -void SysTray::handleDelInhibitPowerManagement(quint32 cookie) +void App::handleDelInhibitPowerManagement(quint32 cookie) { qDebug() << "PM INHIBITOR REMOVED" << cookie; checkDevices(); } // show notifications -void SysTray::showMessage(const QString &title, - const QString &msg, - bool critical) -{ - if (showNotifications) { - SystemNotification notifier; - if (notifier.valid) { - notifier.sendMessage(title, msg, critical); - } else if (tray->isVisible()) { - if (critical) { - tray->showMessage(title, msg, - QSystemTrayIcon::Critical, - 900000); - } else { - tray->showMessage(title, msg); - } +void App::showMessage(const QString &title, + const QString &msg, + bool critical) +{ + if (!showNotifications) { return; } + SystemNotification notifier; + if (notifier.valid) { + notifier.sendMessage(title, msg, critical); + } else if (tray->isVisible()) { + if (critical) { + tray->showMessage(title, + msg, + QSystemTrayIcon::Critical, + 900000); + } else { + tray->showMessage(title, msg); } } } // reload settings if conf changed -void SysTray::handleConfChanged(const QString &file) +void App::handleConfChanged(const QString &file) { Q_UNUSED(file) qDebug() << "CONFIG CHANGED" << file; @@ -835,7 +835,7 @@ void SysTray::handleConfChanged(const QString &file) } // disable hibernate if enabled -void SysTray::disableHibernate() +void App::disableHibernate() { if (criticalAction == criticalHibernate) { qWarning() << "reset critical action to shutdown"; @@ -870,7 +870,7 @@ void SysTray::disableHibernate() } // disable suspend if enabled -void SysTray::disableSuspend() +void App::disableSuspend() { if (lidActionBattery == lidSleep) { qWarning() << "reset lid battery action to lock"; @@ -899,7 +899,7 @@ void SysTray::disableSuspend() } // prepare for suspend -void SysTray::handlePrepareForSuspend() +void App::handlePrepareForSuspend() { /*qDebug() << "prepare for suspend"; resetTimer(); @@ -908,7 +908,7 @@ void SysTray::handlePrepareForSuspend() } // prepare for resume -void SysTray::handlePrepareForResume() +void App::handlePrepareForResume() { qDebug() << "prepare for resume ..."; resetTimer(); @@ -917,7 +917,7 @@ void SysTray::handlePrepareForResume() } // turn off/on monitor using xrandr -void SysTray::switchInternalMonitor(bool toggle) +void App::switchInternalMonitor(bool toggle) { if (!lidXrandr) { return; } qDebug() << "using xrandr to turn on/off internal monitor" << toggle; @@ -927,7 +927,7 @@ void SysTray::switchInternalMonitor(bool toggle) } // adjust backlight on wheel event (on systray) -void SysTray::handleTrayWheel(TrayIcon::WheelAction action) +void App::handleTrayWheel(TrayIcon::WheelAction action) { if (!backlightMouseWheel) { return; } switch (action) { @@ -944,14 +944,14 @@ void SysTray::handleTrayWheel(TrayIcon::WheelAction action) } // check devices if changed -void SysTray::handleDeviceChanged(const QString &path) +void App::handleDeviceChanged(const QString &path) { Q_UNUSED(path) qDebug() << "DEVICE CHANGED" << path; checkDevices(); } -void SysTray::openSettings() +void App::openSettings() { QProcess proc; proc.startDetached(qApp->applicationFilePath(), diff --git a/src/powerkit_app.h b/src/powerkit_app.h new file mode 100644 index 0000000..924e7e8 --- /dev/null +++ b/src/powerkit_app.h @@ -0,0 +1,137 @@ +/* +# PowerKit +# Copyright (c) Ole-André Rodlie All rights reserved. +# +# Available under the 3-clause BSD license +# See the LICENSE file for full details +*/ + +#ifndef POWERKIT_APP_H +#define POWERKIT_APP_H + +#include +#include +#include +#include +#include +#include +#include + +#include "powerkit_freedesktop_pm.h" +#include "powerkit_screensaver.h" +#include "powerkit_manager.h" + +namespace PowerKit +{ + class TrayIcon : public QSystemTrayIcon + { + Q_OBJECT + public: + enum WheelAction { + WheelUp, + WheelDown + }; + TrayIcon(QObject *parent = 0) + : QSystemTrayIcon(parent), wheel_delta(0) {} + TrayIcon(const QIcon &icon, QObject *parent = 0) + : QSystemTrayIcon(icon, parent), wheel_delta(0) {} + bool event(QEvent *event); + signals: + void wheel(TrayIcon::WheelAction action); + private: + int wheel_delta; + }; + + class App : public QObject + { + Q_OBJECT + + public: + explicit App(QObject *parent = NULL); + ~App(); + + private: + TrayIcon *tray; + PowerKit::Manager *man; + PowerManagement *pm; + PowerKit::ScreenSaver *ss; + bool wasLowBattery; + bool wasVeryLowBattery; + int lowBatteryValue; + int critBatteryValue; + bool hasService; + int lidActionBattery; + int lidActionAC; + int criticalAction; + int autoSuspendBattery; + int autoSuspendAC; + QTimer *timer; + int timeouts; + bool showNotifications; + bool showTray; + bool disableLidOnExternalMonitors; + int autoSuspendBatteryAction; + int autoSuspendACAction; + QString internalMonitor; + QFileSystemWatcher *watcher; + bool lidXrandr; + bool lidWasClosed; + QString backlightDevice; + bool hasBacklight; + bool backlightOnBattery; + bool backlightOnAC; + int backlightBatteryValue; + int backlightACValue; + bool backlightBatteryDisableIfLower; + bool backlightACDisableIfHigher; + bool warnOnLowBattery; + bool warnOnVeryLowBattery; + bool notifyOnBattery; + bool notifyOnAC; + bool notifyNewInhibitor; + bool backlightMouseWheel; + bool ignoreKernelResume; + + private slots: + void trayActivated(QSystemTrayIcon::ActivationReason reason); + void checkDevices(); + void handleClosedLid(); + void handleOpenedLid(); + void handleOnBattery(); + void handleOnAC(); + void loadSettings(); + void registerService(); + void handleHasInhibitChanged(bool has_inhibit); + void handleLow(double left); + void handleVeryLow(double left); + void handleCritical(double left); + void drawBattery(double left); + void timeout(); + void resetTimer(); + void setInternalMonitor(); + bool internalMonitorIsConnected(); + bool externalMonitorIsConnected(); + void handleNewInhibitScreenSaver(const QString &application, + const QString &reason, + quint32 cookie); + void handleNewInhibitPowerManagement(const QString &application, + const QString &reason, + quint32 cookie); + void handleDelInhibitScreenSaver(quint32 cookie); + void handleDelInhibitPowerManagement(quint32 cookie); + void showMessage(const QString &title, + const QString &msg, + bool critical = false); + void handleConfChanged(const QString &file); + void disableHibernate(); + void disableSuspend(); + void handlePrepareForSuspend(); + void handlePrepareForResume(); + void switchInternalMonitor(bool toggle); + void handleTrayWheel(TrayIcon::WheelAction action); + void handleDeviceChanged(const QString &path); + void openSettings(); + }; +} + +#endif // POWERKIT_APP_H diff --git a/src/powerkit_systray.h b/src/powerkit_systray.h deleted file mode 100644 index f0dc626..0000000 --- a/src/powerkit_systray.h +++ /dev/null @@ -1,134 +0,0 @@ -/* -# PowerKit -# Copyright (c) Ole-André Rodlie All rights reserved. -# -# Available under the 3-clause BSD license -# See the LICENSE file for full details -*/ - -#ifndef POWERKIT_APP_H -#define POWERKIT_APP_H - -#include -#include -#include -#include -#include -#include -#include - -#include "powerkit_freedesktop_pm.h" -#include "powerkit_screensaver.h" -#include "powerkit_manager.h" - -class TrayIcon : public QSystemTrayIcon -{ - Q_OBJECT -public: - enum WheelAction { - WheelUp, - WheelDown - }; - TrayIcon(QObject *parent = 0) - : QSystemTrayIcon(parent), wheel_delta(0) {} - TrayIcon(const QIcon &icon, QObject *parent = 0) - : QSystemTrayIcon(icon, parent), wheel_delta(0) {} - bool event(QEvent *event); -signals: - void wheel(TrayIcon::WheelAction action); -private: - int wheel_delta; -}; - -class SysTray : public QObject -{ - Q_OBJECT - -public: - explicit SysTray(QObject *parent = NULL); - ~SysTray(); - -private: - TrayIcon *tray; - PowerKit::Manager *man; - PowerManagement *pm; - PowerKit::ScreenSaver *ss; - bool wasLowBattery; - bool wasVeryLowBattery; - int lowBatteryValue; - int critBatteryValue; - bool hasService; - int lidActionBattery; - int lidActionAC; - int criticalAction; - int autoSuspendBattery; - int autoSuspendAC; - QTimer *timer; - int timeouts; - bool showNotifications; - bool showTray; - bool disableLidOnExternalMonitors; - int autoSuspendBatteryAction; - int autoSuspendACAction; - QString internalMonitor; - QFileSystemWatcher *watcher; - bool lidXrandr; - bool lidWasClosed; - QString backlightDevice; - bool hasBacklight; - bool backlightOnBattery; - bool backlightOnAC; - int backlightBatteryValue; - int backlightACValue; - bool backlightBatteryDisableIfLower; - bool backlightACDisableIfHigher; - bool warnOnLowBattery; - bool warnOnVeryLowBattery; - bool notifyOnBattery; - bool notifyOnAC; - bool notifyNewInhibitor; - bool backlightMouseWheel; - bool ignoreKernelResume; - -private slots: - void trayActivated(QSystemTrayIcon::ActivationReason reason); - void checkDevices(); - void handleClosedLid(); - void handleOpenedLid(); - void handleOnBattery(); - void handleOnAC(); - void loadSettings(); - void registerService(); - void handleHasInhibitChanged(bool has_inhibit); - void handleLow(double left); - void handleVeryLow(double left); - void handleCritical(double left); - void drawBattery(double left); - void timeout(); - void resetTimer(); - void setInternalMonitor(); - bool internalMonitorIsConnected(); - bool externalMonitorIsConnected(); - void handleNewInhibitScreenSaver(const QString &application, - const QString &reason, - quint32 cookie); - void handleNewInhibitPowerManagement(const QString &application, - const QString &reason, - quint32 cookie); - void handleDelInhibitScreenSaver(quint32 cookie); - void handleDelInhibitPowerManagement(quint32 cookie); - void showMessage(const QString &title, - const QString &msg, - bool critical = false); - void handleConfChanged(const QString &file); - void disableHibernate(); - void disableSuspend(); - void handlePrepareForSuspend(); - void handlePrepareForResume(); - void switchInternalMonitor(bool toggle); - void handleTrayWheel(TrayIcon::WheelAction action); - void handleDeviceChanged(const QString &path); - void openSettings(); -}; - -#endif // POWERKIT_APP_H