From 55e701122e0d2373eea34fbc38fc16a0eb47a9f3 Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Sun, 26 Apr 2020 18:39:27 +0300 Subject: [PATCH] Code cleanup --- application/src/settings.cpp | 14 +++++++------- service/src/battery.cpp | 26 ++++++++++---------------- service/src/notification.cpp | 19 ++++++++++--------- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/application/src/settings.cpp b/application/src/settings.cpp index 60274c9..bf019d9 100644 --- a/application/src/settings.cpp +++ b/application/src/settings.cpp @@ -85,7 +85,7 @@ void Settings::setLowAlert(int newLimit) { // Lows and highs are always saved in pairs! //mySettings->sync(); emit lowAlertChanged(lowAlert); - qDebug() << "Change" << sLowAlert << newLimit; + qDebug() << sLowAlert << newLimit; } void Settings::setHighAlert(int newLimit) { @@ -93,7 +93,7 @@ void Settings::setHighAlert(int newLimit) { saveInteger(sHighAlert, &highAlert); mySettings->sync(); emit highAlertChanged(highAlert); - qDebug() << "Change" << sHighAlert << newLimit; + qDebug() << sHighAlert << newLimit; } void Settings::setInterval(int newInterval) { @@ -101,7 +101,7 @@ void Settings::setInterval(int newInterval) { saveInteger(sInterval, &interval); mySettings->sync(); emit intervalChanged(interval); - qDebug() << "Change" << sInterval << newInterval; + qDebug() << sInterval << newInterval; } void Settings::setLowLimit(int newLimit) { @@ -110,7 +110,7 @@ void Settings::setLowLimit(int newLimit) { // Lows and highs are always saved in pairs! //mySettings->sync(); emit lowLimitChanged(lowLimit); - qDebug() << "Change" << sLowLimit << newLimit; + qDebug() << sLowLimit << newLimit; } void Settings::setHighLimit(int newLimit) { @@ -118,7 +118,7 @@ void Settings::setHighLimit(int newLimit) { saveInteger(sHighLimit, &highLimit); mySettings->sync(); emit highLimitChanged(highLimit); - qDebug() << "Change" << sHighLimit << newLimit; + qDebug() << sHighLimit << newLimit; } void Settings::setLimitEnabled(bool newEnabled) { @@ -126,7 +126,7 @@ void Settings::setLimitEnabled(bool newEnabled) { saveInteger(sLimitEnabled, &limitEnabled); mySettings->sync(); emit limitEnabledChanged(limitEnabled); - qDebug() << "Change" << sLimitEnabled << newEnabled; + qDebug() << sLimitEnabled << newEnabled; } void Settings::setNotificationsEnabled(bool newEnabled) { @@ -134,7 +134,7 @@ void Settings::setNotificationsEnabled(bool newEnabled) { saveInteger(sNotificationsEnabled, ¬ificationsEnabled); mySettings->sync(); emit notificationsEnabledChanged(notificationsEnabled); - qDebug() << "Change" << sNotificationsEnabled << newEnabled; + qDebug() << sNotificationsEnabled << newEnabled; } void Settings::setNotificationTitle(QString newText) { diff --git a/service/src/battery.cpp b/service/src/battery.cpp index f60668e..9db1806 100644 --- a/service/src/battery.cpp +++ b/service/src/battery.cpp @@ -39,12 +39,7 @@ Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier, // ENABLE/DISABLE CHARGING if(QHostInfo::localHostName().contains("SailfishEmul")) { - qInfo() << "Sailfish SDK detected"; - qInfo() << "Using dummy control file"; - filename = QStandardPaths::writableLocation(QStandardPaths::TempLocation)+"/charging_enabled_dummy"; - chargingEnabledFile = new QFile(filename, this); - enableChargingValue = 1; - disableChargingValue = 0; + qInfo() << "Sailfish SDK detected, not using charger control file"; } else { // e.g. for Sony Xperia XA2 @@ -71,7 +66,6 @@ Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier, disableChargingValue = 1; } - if(!chargingEnabledFile) { qWarning() << "Charger control file not found!"; qWarning() << "Please contact the developer with your device model!"; @@ -100,11 +94,6 @@ Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier, } } - // TODO - // Implement DBus mechanism for reading battery status, or try - // QFileSystemWatcher again without /run/state/namespaces/Battery/ - // thingamabob - it is deprecated anyway. - updateData(); connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateData())); @@ -175,19 +164,19 @@ void Battery::showNotification() { if(!settings->getNotificationsEnabled()) return; - qDebug() << "battery" << charge << "low" << settings->getLowAlert() << "high" << settings->getHighAlert() << "state" << state; + qInfo() << "battery" << charge << "low" << settings->getLowAlert() << "high" << settings->getHighAlert() << "state" << state; if(charge <= settings->getLowAlert() && state.compare("charging")) { - qInfo() << "Battery notification timer: empty enough battery"; + qDebug() << "Battery notification timer: empty enough battery"; notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile()); } else if((charge >= settings->getHighAlert() && state.compare("discharging")) || (charge == 100 && !state.compare("idle"))) { - qInfo() << "Battery notification timer: full enough battery"; + qDebug() << "Battery notification timer: full enough battery"; notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationHighText(), settings->getHighAlertFile()); } else { - qInfo() << "Battery notification timer: close notification"; + qDebug() << "Battery notification timer: close notification"; notification->close(); } } @@ -225,6 +214,11 @@ void Battery::shutdown() { updateTimer->stop(); qDebug() << "Timer stopped"; } + notification->close(); + if(notifyTimer) { + notifyTimer->stop(); + qDebug() << "Notification stopped"; + } setChargingEnabled(true); chargingEnabledFile->setPermissions(originalPerms); qDebug() << "Charger control file permissions restored."; diff --git a/service/src/notification.cpp b/service/src/notification.cpp index cc35325..bb26d6a 100644 --- a/service/src/notification.cpp +++ b/service/src/notification.cpp @@ -33,30 +33,30 @@ void Notification::send(QString title, QString body, QString soundFile) body = body.replace("\"", "\\\""); QStringList args; - QString command; - // Using 'update' works always; it creates a new one if needed - command = QString("notificationtool -o update -i %1 -I /usr/share/icons/hicolor/128x128/apps/harbour-batterybuddy.png -A \"Battery Buddy\" \"%2\" \"%3\" \"%2\" \"%3\"").arg(noteID).arg(title).arg(body); - - args << "-l" << "nemo" << "-c" << command; + // Using 'update' works always; it creates a new notification if the ID doesn't match. + args << "-l" << "nemo" << "-c" + << QString("notificationtool -o update -i %1 -I /usr/share/icons/hicolor/128x128/apps/harbour-batterybuddy.png -A \"Battery Buddy\" \"%2\" \"%3\" \"%2\" \"%3\"").arg(noteID).arg(title).arg(body); QProcess aplay; if(!soundFile.isEmpty()) { QStringList aplayArgs; aplayArgs << "-l" << "nemo" << "-c" << QString("paplay %1").arg(soundFile); aplay.start("runuser", aplayArgs); - qDebug() << "runuser" << aplayArgs; } QProcess notificationtool; notificationtool.start("runuser", args); - qDebug() << "runuser" << args; notificationtool.waitForFinished(); - aplay.waitForFinished(); QString result(notificationtool.readAll()); if(!result.isEmpty()) noteID = result.split(' ').last().trimmed(); + + // Playing the sound may take a while, so let's do this as late as possible. + // Shouldn't matter though, because the minimum delay is 1:00 + // and the sound plays for a few seconds. + aplay.waitForFinished(); return; } @@ -66,7 +66,8 @@ void Notification::close() return; QStringList args; - args << "-o" << "remove" << "-i" << noteID; + args << "-l" << "nemo" << "-c" + << QString("notificationtool -o remove -i %1").arg(noteID); QProcess proc; proc.start("runuser", args); proc.waitForFinished();