Fix notifications (again), get rid of useless code

This commit is contained in:
Matti Viljanen 2021-04-18 13:55:30 +03:00
parent 957bee21df
commit 0d67eb3316
No known key found for this signature in database
GPG key ID: CF32A1495158F888
6 changed files with 6 additions and 125 deletions

View file

@ -21,14 +21,12 @@ DEFINES += QT_NO_DEBUG_OUTPUT
HEADERS += \ HEADERS += \
src/battery.h \ src/battery.h \
src/mynotification.h \ src/mynotification.h \
src/profile.h \
src/logger.h \ src/logger.h \
src/settings.h src/settings.h
SOURCES += \ SOURCES += \
src/battery.cpp \ src/battery.cpp \
src/mynotification.cpp \ src/mynotification.cpp \
src/profile.cpp \
src/logger.cpp \ src/logger.cpp \
src/settings.cpp \ src/settings.cpp \
src/harbour-batterybuddy-daemon.cpp src/harbour-batterybuddy-daemon.cpp

View file

@ -25,7 +25,7 @@ Battery::Battery(Logger* newLogger, QObject *parent) : QObject(parent)
updateTimer = new QTimer(this); updateTimer = new QTimer(this);
highNotifyTimer = new QTimer(this); highNotifyTimer = new QTimer(this);
lowNotifyTimer = new QTimer(this); lowNotifyTimer = new QTimer(this);
notification = new MyNotification(logger, this); notification = new MyNotification(this);
// Number: charge percentage, e.g. 42 // Number: charge percentage, e.g. 42
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this); chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);

View file

@ -17,21 +17,16 @@
*/ */
#include "mynotification.h" #include "mynotification.h"
MyNotification::MyNotification(Logger* newLogger, QObject* parent) : QObject(parent) MyNotification::MyNotification(QObject* parent) : QObject(parent)
{ {
logger = newLogger;
notification.setAppName("Battery Buddy"); notification.setAppName("Battery Buddy");
// Set this manually, so that the correct icon is used. // Set this manually, so that the correct icon is used.
notification.setAppIcon("harbour-batterybuddy"); notification.setAppIcon("harbour-batterybuddy");
playSound = false;
sound.setAudioRole(QAudio::NotificationRole);
connect(&sound, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
this, SLOT(soundLoadedChanged(QMediaPlayer::MediaStatus)));
} }
MyNotification::~MyNotification() MyNotification::~MyNotification()
{ {
close(); notification.close();
} }
void MyNotification::send(QString title, QString body, QString soundFile) 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("\"", "\\\""); title = title.replace("\"", "\\\"");
body = body.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.setSummary(title);
notification.setBody(body); notification.setBody(body);
notification.setPreviewSummary(title); notification.setPreviewSummary(title);
notification.setPreviewBody(body); notification.setPreviewBody(body);
notification.setSound(soundFile);
notification.setUrgency(Notification::Normal);
notification.publish(); notification.publish();
return; return;
@ -67,10 +50,3 @@ void MyNotification::close()
notification.close(); notification.close();
return; return;
} }
void MyNotification::soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus) {
if(playSound && newStatus == QMediaPlayer::LoadedMedia) {
sound.play();
playSound = false;
}
}

View file

@ -19,18 +19,14 @@
#define MYNOTIFICATION_H #define MYNOTIFICATION_H
#include <QObject> #include <QObject>
#include <QTimer>
#include <QMediaPlayer>
#include <nemonotifications-qt5/notification.h> #include <nemonotifications-qt5/notification.h>
#include "profile.h"
#include "logger.h"
class MyNotification : public QObject class MyNotification : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
MyNotification(Logger* newLogger, QObject* parent = nullptr); MyNotification(QObject* parent = nullptr);
~MyNotification(); ~MyNotification();
public slots: public slots:
@ -38,15 +34,7 @@ public slots:
void close(); void close();
private: private:
Logger* logger;
QString noteID = "1";
Notification notification; Notification notification;
QMediaPlayer sound;
bool playSound;
Profile* profile;
private slots:
void soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus);
}; };
#endif // MYNOTIFICATION_H #endif // MYNOTIFICATION_H

View file

@ -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 <http://www.gnu.org/licenses/>.
*
* 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);
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*
* Author: Matti Viljanen
*/
#ifndef PROFILE_H
#define PROFILE_H
#include <QObject>
#include <QtDBus/QtDBus>
#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