From 0d67eb33164fa295990dd7f52d687034bacfd69a Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Sun, 18 Apr 2021 13:55:30 +0300 Subject: [PATCH] Fix notifications (again), get rid of useless code --- service/service.pro | 2 -- service/src/battery.cpp | 2 +- service/src/mynotification.cpp | 32 ++++--------------------- service/src/mynotification.h | 14 +---------- service/src/profile.cpp | 44 ---------------------------------- service/src/profile.h | 37 ---------------------------- 6 files changed, 6 insertions(+), 125 deletions(-) delete mode 100644 service/src/profile.cpp delete mode 100644 service/src/profile.h diff --git a/service/service.pro b/service/service.pro index 0d4fa29..f0c0f85 100644 --- a/service/service.pro +++ b/service/service.pro @@ -21,14 +21,12 @@ DEFINES += QT_NO_DEBUG_OUTPUT HEADERS += \ src/battery.h \ src/mynotification.h \ - src/profile.h \ src/logger.h \ src/settings.h SOURCES += \ src/battery.cpp \ src/mynotification.cpp \ - src/profile.cpp \ src/logger.cpp \ src/settings.cpp \ src/harbour-batterybuddy-daemon.cpp diff --git a/service/src/battery.cpp b/service/src/battery.cpp index 41ba2bb..645e460 100644 --- a/service/src/battery.cpp +++ b/service/src/battery.cpp @@ -25,7 +25,7 @@ Battery::Battery(Logger* newLogger, QObject *parent) : QObject(parent) updateTimer = new QTimer(this); highNotifyTimer = new QTimer(this); lowNotifyTimer = new QTimer(this); - notification = new MyNotification(logger, this); + notification = new MyNotification(this); // Number: charge percentage, e.g. 42 chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this); diff --git a/service/src/mynotification.cpp b/service/src/mynotification.cpp index a4bd4f0..300ba7f 100644 --- a/service/src/mynotification.cpp +++ b/service/src/mynotification.cpp @@ -17,21 +17,16 @@ */ #include "mynotification.h" -MyNotification::MyNotification(Logger* newLogger, QObject* parent) : QObject(parent) +MyNotification::MyNotification(QObject* parent) : QObject(parent) { - logger = newLogger; notification.setAppName("Battery Buddy"); // Set this manually, so that the correct icon is used. notification.setAppIcon("harbour-batterybuddy"); - playSound = false; - sound.setAudioRole(QAudio::NotificationRole); - connect(&sound, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), - this, SLOT(soundLoadedChanged(QMediaPlayer::MediaStatus))); } MyNotification::~MyNotification() { - close(); + notification.close(); } void MyNotification::send(QString title, QString body, QString soundFile) @@ -39,24 +34,12 @@ void MyNotification::send(QString title, QString body, QString soundFile) title = title.replace("\"", "\\\""); body = body.replace("\"", "\\\""); - int vol = profile->getRingtoneVolume(); - sound.setVolume(vol); - - playSound = true; - if(sound.media() != QUrl::fromLocalFile(soundFile)) { - // Signalled to play() - sound.setMedia(QUrl::fromLocalFile(soundFile)); - } - else if (playSound){ - // Must manually trigger play() - sound.play(); - playSound = false; - } - notification.setSummary(title); notification.setBody(body); notification.setPreviewSummary(title); notification.setPreviewBody(body); + notification.setSound(soundFile); + notification.setUrgency(Notification::Normal); notification.publish(); return; @@ -67,10 +50,3 @@ void MyNotification::close() notification.close(); return; } - -void MyNotification::soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus) { - if(playSound && newStatus == QMediaPlayer::LoadedMedia) { - sound.play(); - playSound = false; - } -} diff --git a/service/src/mynotification.h b/service/src/mynotification.h index bda4b77..7998b55 100644 --- a/service/src/mynotification.h +++ b/service/src/mynotification.h @@ -19,18 +19,14 @@ #define MYNOTIFICATION_H #include -#include -#include #include -#include "profile.h" -#include "logger.h" class MyNotification : public QObject { Q_OBJECT public: - MyNotification(Logger* newLogger, QObject* parent = nullptr); + MyNotification(QObject* parent = nullptr); ~MyNotification(); public slots: @@ -38,15 +34,7 @@ public slots: void close(); private: - Logger* logger; - QString noteID = "1"; Notification notification; - QMediaPlayer sound; - bool playSound; - Profile* profile; - -private slots: - void soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus); }; #endif // MYNOTIFICATION_H diff --git a/service/src/profile.cpp b/service/src/profile.cpp deleted file mode 100644 index 61ac8e7..0000000 --- a/service/src/profile.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Battery Buddy, a Sailfish application to prolong battery lifetime - * - * Copyright (C) 2019-2020 Matti Viljanen - * - * Battery Buddy is free software: you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * Battery Buddy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * See the GNU General Public License for more details. You should have received a copy of the GNU - * General Public License along with Battery Buddy. If not, see . - * - * Author: Matti Viljanen - */ -#include "profile.h" - -Profile::Profile(Logger* newLogger, QObject *parent) : QObject(parent) -{ - logger = newLogger; -} - -int Profile::getRingtoneVolume() { - const QString dots = "com.nokia.profiled"; - const QString slashes = "/com/nokia/profiled"; - QDBusInterface interface(dots, slashes, dots, connection); - - QDBusMessage message = interface.call("get_profile"); - QString profile = message.arguments().at(0).toString(); - logD("Active profile:" + profile); - - if(profile == "silent") { - logD("Returning volume: 0"); - return 0; - } - - message = interface.call("get_value", "profile", "ringing.alert.volume"); - int volume = message.arguments().at(0).toInt(); - logD(QString("Ringtone volume: %1").arg(volume)); - - return (volume >= 0 && volume <= 100 ? volume : 0); -} diff --git a/service/src/profile.h b/service/src/profile.h deleted file mode 100644 index e0da0dc..0000000 --- a/service/src/profile.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Battery Buddy, a Sailfish application to prolong battery lifetime - * - * Copyright (C) 2019-2020 Matti Viljanen - * - * Battery Buddy is free software: you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * Battery Buddy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * See the GNU General Public License for more details. You should have received a copy of the GNU - * General Public License along with Battery Buddy. If not, see . - * - * Author: Matti Viljanen - */ -#ifndef PROFILE_H -#define PROFILE_H - -#include -#include -#include "logger.h" - -class Profile : public QObject -{ - Q_OBJECT -public: - int getRingtoneVolume(); - explicit Profile(Logger* newLogger, QObject *parent = nullptr); - -private: - Logger* logger; - QDBusConnection connection = QDBusConnection::sessionBus(); -}; - -#endif // PROFILE_H