From 3b37aa8df5a24db67a20e32e951e283bbd118607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole-Andr=C3=A9=20Rodlie?= Date: Tue, 6 Feb 2024 21:06:00 +0100 Subject: [PATCH] Misc * handle some errors/warnings * minor dialog fixes * other minor --- src/powerkit_app.cpp | 44 +++++++++++++++++++++++++++++----------- src/powerkit_app.h | 2 ++ src/powerkit_dialog.cpp | 7 ++++++- src/powerkit_manager.cpp | 37 +++++++++++++++++++-------------- src/powerkit_manager.h | 2 ++ 5 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/powerkit_app.cpp b/src/powerkit_app.cpp index ce83e64..152078c 100644 --- a/src/powerkit_app.cpp +++ b/src/powerkit_app.cpp @@ -121,6 +121,14 @@ App::App(QObject *parent) SIGNAL(Update()), this, SLOT(loadSettings())); + connect(man, + SIGNAL(Error(QString)), + this, + SLOT(handleError(QString))); + connect(man, + SIGNAL(Warning(QString)), + this, + SLOT(handleWarning(QString))); // setup org.freedesktop.PowerManagement pm = new PowerManagement(this); @@ -570,6 +578,8 @@ void App::registerService() qWarning() << "Enabled org.freedesktop.PowerKit" << hasDesktopPK; if (!hasDesktopPK || !hasDesktopPM || !hasScreenSaver) { hasService = false; } + + if (!hasService) { handleWarning(tr("Failed to setup and/or connect required services!")); } } // dbus session inhibit status handler @@ -795,13 +805,13 @@ void App::handleNewInhibitPowerManagement(const QString &application, void App::handleDelInhibitScreenSaver(quint32 cookie) { - qDebug() << "SS INHIBITOR REMOVED" << cookie; + Q_UNUSED(cookie) checkDevices(); } void App::handleDelInhibitPowerManagement(quint32 cookie) { - qDebug() << "PM INHIBITOR REMOVED" << cookie; + Q_UNUSED(cookie) checkDevices(); } @@ -830,7 +840,6 @@ void App::showMessage(const QString &title, void App::handleConfChanged(const QString &file) { Q_UNUSED(file) - qDebug() << "CONFIG CHANGED" << file; loadSettings(); } @@ -876,25 +885,25 @@ void App::disableSuspend() qWarning() << "reset lid battery action to lock"; lidActionBattery = lidLock; Settings::setValue(CONF_LID_BATTERY_ACTION, - lidActionBattery); + lidActionBattery); } if (lidActionAC == lidSleep) { qWarning() << "reset lid ac action to lock"; lidActionAC = lidLock; Settings::setValue(CONF_LID_AC_ACTION, - lidActionAC); + lidActionAC); } if (autoSuspendBatteryAction == suspendSleep) { qWarning() << "reset auto suspend battery action to none"; autoSuspendBatteryAction = suspendNone; Settings::setValue(CONF_SUSPEND_BATTERY_ACTION, - autoSuspendBatteryAction); + autoSuspendBatteryAction); } if (autoSuspendACAction == suspendSleep) { qWarning() << "reset auto suspend ac action to none"; autoSuspendACAction = suspendNone; Settings::setValue(CONF_SUSPEND_AC_ACTION, - autoSuspendACAction); + autoSuspendACAction); } } @@ -912,7 +921,6 @@ void App::handlePrepareForResume() { qDebug() << "prepare for resume ..."; resetTimer(); - tray->showMessage(QString(), QString()); ss->SimulateUserActivity(); } @@ -920,7 +928,7 @@ void App::handlePrepareForResume() void App::switchInternalMonitor(bool toggle) { if (!lidXrandr) { return; } - qDebug() << "using xrandr to turn on/off internal monitor" << toggle; + qDebug() << "using xrandr to turn on/off internal monitor" << internalMonitor << toggle; QStringList args; args << "--output" << internalMonitor << (toggle ? "--auto" : "--off"); QProcess::startDetached("xrandr", args); @@ -953,9 +961,21 @@ void App::handleDeviceChanged(const QString &path) void App::openSettings() { - QProcess proc; - proc.startDetached(qApp->applicationFilePath(), - QStringList() << "--config"); + QProcess::startDetached(qApp->applicationFilePath(), + QStringList() << "--config"); +} + +void App::handleError(const QString &message) +{ + qWarning() << "error" << message; + showMessage(tr("Error"), message, true); + QTimer::singleShot(5000, qApp, SLOT(quit())); +} + +void App::handleWarning(const QString &message) +{ + qWarning() << "warning" << message; + showMessage(tr("Warning"), message, true); } // catch wheel events diff --git a/src/powerkit_app.h b/src/powerkit_app.h index 924e7e8..44d08c5 100644 --- a/src/powerkit_app.h +++ b/src/powerkit_app.h @@ -131,6 +131,8 @@ namespace PowerKit void handleTrayWheel(TrayIcon::WheelAction action); void handleDeviceChanged(const QString &path); void openSettings(); + void handleError(const QString &message); + void handleWarning(const QString &message); }; } diff --git a/src/powerkit_dialog.cpp b/src/powerkit_dialog.cpp index c79a4f7..557b2d5 100644 --- a/src/powerkit_dialog.cpp +++ b/src/powerkit_dialog.cpp @@ -99,7 +99,11 @@ Dialog::Dialog(QWidget *parent, Dialog::~Dialog() { - Settings::setValue(CONF_DIALOG, saveGeometry()); + const auto lastGeo = Settings::getValue(CONF_DIALOG).toByteArray(); + const auto newGeo = saveGeometry(); + if (lastGeo != newGeo) { + Settings::setValue(CONF_DIALOG, newGeo); + } } void Dialog::setupWidgets() @@ -928,6 +932,7 @@ void Dialog::handleUpdatedDevices() hasBattery = Client::hasBattery(dbus); batteryStatusLabel->setVisible(hasBattery); drawBattery(); + drawCpu(); } // save current value and update power manager diff --git a/src/powerkit_manager.cpp b/src/powerkit_manager.cpp index c514df5..802869a 100644 --- a/src/powerkit_manager.cpp +++ b/src/powerkit_manager.cpp @@ -325,16 +325,21 @@ void Manager::setup() SIGNAL(PrepareForSleep(bool)), this, SLOT(handlePrepareForSuspend(bool))); + emit Error(tr("Failed to connect to logind")); } if (!suspendLock) { registerSuspendLock(); } if (!lidLock) { registerLidLock(); } scan(); - } else { qWarning() << "Failed to connect to system bus"; } + } else { + emit Error(tr("Failed to connect to the system bus")); + } } void Manager::check() { - if (!QDBusConnection::systemBus().isConnected()) { + if (!QDBusConnection::systemBus().isConnected() || + !upower || + !logind) { setup(); return; } @@ -395,7 +400,6 @@ void Manager::deviceRemoved(const QString &path) void Manager::deviceChanged() { - qDebug() << "a device changed, tell the world!"; emit UpdatedDevices(); } @@ -410,10 +414,10 @@ void Manager::propertiesChanged() if (wasLidClosed != isLidClosed) { if (!wasLidClosed && isLidClosed) { - qDebug() << "lid changed status to closed"; + qDebug() << "lid changed to closed"; emit LidClosed(); } else if (wasLidClosed && !isLidClosed) { - qDebug() << "lid changed status to open"; + qDebug() << "lid changed to open"; emit LidOpened(); } } @@ -451,8 +455,7 @@ void Manager::handleSuspend() { if (HasLogind()) { return; } qDebug() << "handle suspend from upower"; - LockScreen(); - emit PrepareForSuspend(); + handlePrepareForSuspend(true); } void Manager::handlePrepareForSuspend(bool prepare) @@ -496,17 +499,21 @@ void Manager::clearDevices() devices.clear(); } -void Manager::handleNewInhibitScreenSaver(const QString &application, const QString &reason, quint32 cookie) +void Manager::handleNewInhibitScreenSaver(const QString &application, + const QString &reason, + quint32 cookie) { - qDebug() << "PK HANDLE NEW SCREEN SAVER INHIBITOR" << application << reason << cookie; + qDebug() << "new screensaver cookie" << application << reason << cookie; Q_UNUSED(reason) ssInhibitors[cookie] = application; emit UpdatedInhibitors(); } -void Manager::handleNewInhibitPowerManagement(const QString &application, const QString &reason, quint32 cookie) +void Manager::handleNewInhibitPowerManagement(const QString &application, + const QString &reason, + quint32 cookie) { - qDebug() << "PK HANDLE NEW POWER INHIBITOR" << application << reason << cookie; + qDebug() << "new power cookie" << application << reason << cookie; Q_UNUSED(reason) pmInhibitors[cookie] = application; emit UpdatedInhibitors(); @@ -514,7 +521,7 @@ void Manager::handleNewInhibitPowerManagement(const QString &application, const void Manager::handleDelInhibitScreenSaver(quint32 cookie) { - qDebug() << "PK HANDLE REMOVE SCREEN SAVER COOKIE" << cookie; + qDebug() << "remove screensaver cookie" << cookie; if (ssInhibitors.contains(cookie)) { ssInhibitors.remove(cookie); emit UpdatedInhibitors(); @@ -523,7 +530,7 @@ void Manager::handleDelInhibitScreenSaver(quint32 cookie) void Manager::handleDelInhibitPowerManagement(quint32 cookie) { - qDebug() << "PK HANDLE REMOVE POWER COOKIE" << cookie; + qDebug() << "remove power cookie" << cookie; if (pmInhibitors.contains(cookie)) { pmInhibitors.remove(cookie); emit UpdatedInhibitors(); @@ -546,7 +553,7 @@ bool Manager::registerSuspendLock() suspendLock.reset(new QDBusUnixFileDescriptor(reply.value())); return true; } else { - qWarning() << reply.error(); + emit Warning(tr("Failed to set suspend lock: %1").arg(reply.error().message())); } return false; } @@ -568,7 +575,7 @@ bool Manager::registerLidLock() qDebug() << "lidLock" << lidLock->fileDescriptor(); return true; } else { - qWarning() << reply.error(); + emit Warning(tr("Failed to set lid lock: %1").arg(reply.error().message())); } return false; } diff --git a/src/powerkit_manager.h b/src/powerkit_manager.h index 74c879f..676cd5b 100644 --- a/src/powerkit_manager.h +++ b/src/powerkit_manager.h @@ -94,6 +94,8 @@ namespace PowerKit void DeviceWasRemoved(const QString &path); void DeviceWasAdded(const QString &path); void UpdatedInhibitors(); + void Error(const QString &message); + void Warning(const QString &message); private slots: bool availableService(const QString &service,