Code cleanup

This commit is contained in:
Matti Viljanen 2020-04-26 18:39:27 +03:00
parent a5d8ae0fa3
commit 55e701122e
No known key found for this signature in database
GPG key ID: CF32A1495158F888
3 changed files with 27 additions and 32 deletions

View file

@ -85,7 +85,7 @@ void Settings::setLowAlert(int newLimit) {
// Lows and highs are always saved in pairs! // Lows and highs are always saved in pairs!
//mySettings->sync(); //mySettings->sync();
emit lowAlertChanged(lowAlert); emit lowAlertChanged(lowAlert);
qDebug() << "Change" << sLowAlert << newLimit; qDebug() << sLowAlert << newLimit;
} }
void Settings::setHighAlert(int newLimit) { void Settings::setHighAlert(int newLimit) {
@ -93,7 +93,7 @@ void Settings::setHighAlert(int newLimit) {
saveInteger(sHighAlert, &highAlert); saveInteger(sHighAlert, &highAlert);
mySettings->sync(); mySettings->sync();
emit highAlertChanged(highAlert); emit highAlertChanged(highAlert);
qDebug() << "Change" << sHighAlert << newLimit; qDebug() << sHighAlert << newLimit;
} }
void Settings::setInterval(int newInterval) { void Settings::setInterval(int newInterval) {
@ -101,7 +101,7 @@ void Settings::setInterval(int newInterval) {
saveInteger(sInterval, &interval); saveInteger(sInterval, &interval);
mySettings->sync(); mySettings->sync();
emit intervalChanged(interval); emit intervalChanged(interval);
qDebug() << "Change" << sInterval << newInterval; qDebug() << sInterval << newInterval;
} }
void Settings::setLowLimit(int newLimit) { void Settings::setLowLimit(int newLimit) {
@ -110,7 +110,7 @@ void Settings::setLowLimit(int newLimit) {
// Lows and highs are always saved in pairs! // Lows and highs are always saved in pairs!
//mySettings->sync(); //mySettings->sync();
emit lowLimitChanged(lowLimit); emit lowLimitChanged(lowLimit);
qDebug() << "Change" << sLowLimit << newLimit; qDebug() << sLowLimit << newLimit;
} }
void Settings::setHighLimit(int newLimit) { void Settings::setHighLimit(int newLimit) {
@ -118,7 +118,7 @@ void Settings::setHighLimit(int newLimit) {
saveInteger(sHighLimit, &highLimit); saveInteger(sHighLimit, &highLimit);
mySettings->sync(); mySettings->sync();
emit highLimitChanged(highLimit); emit highLimitChanged(highLimit);
qDebug() << "Change" << sHighLimit << newLimit; qDebug() << sHighLimit << newLimit;
} }
void Settings::setLimitEnabled(bool newEnabled) { void Settings::setLimitEnabled(bool newEnabled) {
@ -126,7 +126,7 @@ void Settings::setLimitEnabled(bool newEnabled) {
saveInteger(sLimitEnabled, &limitEnabled); saveInteger(sLimitEnabled, &limitEnabled);
mySettings->sync(); mySettings->sync();
emit limitEnabledChanged(limitEnabled); emit limitEnabledChanged(limitEnabled);
qDebug() << "Change" << sLimitEnabled << newEnabled; qDebug() << sLimitEnabled << newEnabled;
} }
void Settings::setNotificationsEnabled(bool newEnabled) { void Settings::setNotificationsEnabled(bool newEnabled) {
@ -134,7 +134,7 @@ void Settings::setNotificationsEnabled(bool newEnabled) {
saveInteger(sNotificationsEnabled, &notificationsEnabled); saveInteger(sNotificationsEnabled, &notificationsEnabled);
mySettings->sync(); mySettings->sync();
emit notificationsEnabledChanged(notificationsEnabled); emit notificationsEnabledChanged(notificationsEnabled);
qDebug() << "Change" << sNotificationsEnabled << newEnabled; qDebug() << sNotificationsEnabled << newEnabled;
} }
void Settings::setNotificationTitle(QString newText) { void Settings::setNotificationTitle(QString newText) {

View file

@ -39,12 +39,7 @@ Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier,
// ENABLE/DISABLE CHARGING // ENABLE/DISABLE CHARGING
if(QHostInfo::localHostName().contains("SailfishEmul")) { if(QHostInfo::localHostName().contains("SailfishEmul")) {
qInfo() << "Sailfish SDK detected"; qInfo() << "Sailfish SDK detected, not using charger control file";
qInfo() << "Using dummy control file";
filename = QStandardPaths::writableLocation(QStandardPaths::TempLocation)+"/charging_enabled_dummy";
chargingEnabledFile = new QFile(filename, this);
enableChargingValue = 1;
disableChargingValue = 0;
} }
else { else {
// e.g. for Sony Xperia XA2 // e.g. for Sony Xperia XA2
@ -71,7 +66,6 @@ Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier,
disableChargingValue = 1; disableChargingValue = 1;
} }
if(!chargingEnabledFile) { if(!chargingEnabledFile) {
qWarning() << "Charger control file not found!"; qWarning() << "Charger control file not found!";
qWarning() << "Please contact the developer with your device model!"; 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(); updateData();
connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateData())); connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateData()));
@ -175,19 +164,19 @@ void Battery::showNotification() {
if(!settings->getNotificationsEnabled()) if(!settings->getNotificationsEnabled())
return; 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")) { 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()); notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile());
} }
else if((charge >= settings->getHighAlert() && state.compare("discharging")) else if((charge >= settings->getHighAlert() && state.compare("discharging"))
|| (charge == 100 && !state.compare("idle"))) { || (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()); notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationHighText(), settings->getHighAlertFile());
} }
else { else {
qInfo() << "Battery notification timer: close notification"; qDebug() << "Battery notification timer: close notification";
notification->close(); notification->close();
} }
} }
@ -225,6 +214,11 @@ void Battery::shutdown() {
updateTimer->stop(); updateTimer->stop();
qDebug() << "Timer stopped"; qDebug() << "Timer stopped";
} }
notification->close();
if(notifyTimer) {
notifyTimer->stop();
qDebug() << "Notification stopped";
}
setChargingEnabled(true); setChargingEnabled(true);
chargingEnabledFile->setPermissions(originalPerms); chargingEnabledFile->setPermissions(originalPerms);
qDebug() << "Charger control file permissions restored."; qDebug() << "Charger control file permissions restored.";

View file

@ -33,30 +33,30 @@ void Notification::send(QString title, QString body, QString soundFile)
body = body.replace("\"", "\\\""); body = body.replace("\"", "\\\"");
QStringList args; QStringList args;
QString command;
// Using 'update' works always; it creates a new one if needed // Using 'update' works always; it creates a new notification if the ID doesn't match.
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"
<< 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;
QProcess aplay; QProcess aplay;
if(!soundFile.isEmpty()) { if(!soundFile.isEmpty()) {
QStringList aplayArgs; QStringList aplayArgs;
aplayArgs << "-l" << "nemo" << "-c" << QString("paplay %1").arg(soundFile); aplayArgs << "-l" << "nemo" << "-c" << QString("paplay %1").arg(soundFile);
aplay.start("runuser", aplayArgs); aplay.start("runuser", aplayArgs);
qDebug() << "runuser" << aplayArgs;
} }
QProcess notificationtool; QProcess notificationtool;
notificationtool.start("runuser", args); notificationtool.start("runuser", args);
qDebug() << "runuser" << args;
notificationtool.waitForFinished(); notificationtool.waitForFinished();
aplay.waitForFinished();
QString result(notificationtool.readAll()); QString result(notificationtool.readAll());
if(!result.isEmpty()) if(!result.isEmpty())
noteID = result.split(' ').last().trimmed(); 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; return;
} }
@ -66,7 +66,8 @@ void Notification::close()
return; return;
QStringList args; QStringList args;
args << "-o" << "remove" << "-i" << noteID; args << "-l" << "nemo" << "-c"
<< QString("notificationtool -o remove -i %1").arg(noteID);
QProcess proc; QProcess proc;
proc.start("runuser", args); proc.start("runuser", args);
proc.waitForFinished(); proc.waitForFinished();