Make building for SFOS2 easier

This commit is contained in:
Matti Viljanen 2021-05-23 15:27:26 +03:00
parent c9d4642de8
commit 0c9ef27cc4
6 changed files with 237 additions and 6 deletions

View file

@ -1,18 +1,24 @@
# 0 = Build for SFOS 3.4+ (use the latest Sailfish Application SDK)
# 1 = Build for SFOS 2.2+ (use Sailfish Application SDK 1807)
LEGACY_BUILD = 0
TARGET = harbour-batterybuddy-daemon
CONFIG += sailfishapp console
QT = core dbus
contains(LEGACY_BUILD, 0) { QT = core dbus }
contains(LEGACY_BUILD, 1) { QT = core dbus multimedia }
PKGCONFIG += nemonotifications-qt5
# Keep this in sync with "application.pro"
VER = 3.12
REL = 3
VER = 3.13
REL = 1
VERSION = $${VER}-$${REL}
DEFINES += APP_VERSION=\"\\\"$$VERSION\\\"\"
DEFINES += APP_NAME=\"\\\"$$TARGET\\\"\"
DEFINES += LEGACY_BUILD=$${LEGACY_BUILD}
# Use "--verbose" and "--debug" at runtime.
# See main() and logger.h for details.
@ -20,17 +26,27 @@ DEFINES += QT_NO_DEBUG_OUTPUT
HEADERS += \
src/battery.h \
src/mynotification.h \
src/logger.h \
src/settings.h
SOURCES += \
src/battery.cpp \
src/mynotification.cpp \
src/logger.cpp \
src/settings.cpp \
src/harbour-batterybuddy-daemon.cpp
contains(LEGACY_BUILD, 1) {
HEADERS += src/profile.h \
src/mynotification_sfos2.h
SOURCES += src/profile.cpp \
src/mynotification_sfos2.cpp
}
contains(LEGACY_BUILD, 0) {
HEADERS += src/mynotification.h
SOURCES += src/mynotification.cpp
}
OTHER_FILES += harbour-batterybuddy-daemon.service
service.path = /usr/lib/systemd/user/

View file

@ -34,9 +34,13 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject
highNotifyTimer = new QTimer(this);
lowNotifyTimer = new QTimer(this);
healthNotifyTimer = new QTimer(this);
#if LEGACY_BUILD == 1
chargeNotification = new MyNotification(logger, this);
healthNotification = new MyNotification(logger, this);
#else
chargeNotification = new MyNotification(this);
healthNotification = new MyNotification(this);
#endif
// Number: charge percentage, e.g. 42
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
logE("Capacity file: " + chargeFile->fileName() + (chargeFile->exists() ? " OK" : " doesn't exist"));

View file

@ -0,0 +1,78 @@
/**
* 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 "mynotification.h"
MyNotification::MyNotification(Logger *newLogger, QObject* parent) : QObject(parent)
{
notification.setAppName("Battery Buddy");
// Set this manually, so that the correct icon is used.
notification.setAppIcon("harbour-batterybuddy");
logger = newLogger;
profile = new Profile(logger, this);
playSound = false;
sound.setAudioRole(QAudio::NotificationRole);
connect(&sound, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
this, SLOT(soundLoadedChanged(QMediaPlayer::MediaStatus)));
}
MyNotification::~MyNotification() { }
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;
}
void MyNotification::close()
{
notification.close();
return;
}
void MyNotification::soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus) {
if(playSound && newStatus == QMediaPlayer::LoadedMedia) {
sound.play();
playSound = false;
}
}

View file

@ -0,0 +1,52 @@
/**
* 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 MYNOTIFICATION_H
#define MYNOTIFICATION_H
#include <QObject>
#include <QTimer>
#include <QMediaPlayer>
#include <nemonotifications-qt5/notification.h>
#include "profile.h"
#include "logger.h"
class MyNotification : public QObject
{
Q_OBJECT
public:
MyNotification(Logger* newLogger, QObject* parent = nullptr);
~MyNotification();
public slots:
void send(QString title, QString body, QString soundFile);
void close();
private:
Notification notification;
Logger* logger;
QString noteID = "1";
QMediaPlayer sound;
bool playSound;
Profile* profile;
private slots:
void soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus);
};
#endif // MYNOTIFICATION_H

44
service/src/profile.cpp Normal file
View file

@ -0,0 +1,44 @@
/**
* 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);
}

37
service/src/profile.h Normal file
View file

@ -0,0 +1,37 @@
/**
* 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