From cbf7e7cdbcab44b10371c562f935789b4f3e3d6b Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Fri, 24 Apr 2020 11:18:26 +0300 Subject: [PATCH] Split project into GUI application and CLI service --- LICENSE.md => application/LICENSE.md | 0 README.md => application/README.md | 0 application/application.pro | 83 ++++++++++++++++ .../harbour-batterybuddy.desktop | 0 .../harbour-batterybuddy.xcf | Bin .../icons}/108x108/harbour-batterybuddy.png | Bin .../icons}/128x128/harbour-batterybuddy.png | Bin .../icons}/172x172/harbour-batterybuddy.png | Bin .../icons}/86x86/harbour-batterybuddy.png | Bin .../qml}/components/AboutLabel.qml | 0 .../qml}/components/BatteryGraph.qml | 0 .../qml}/components/MyDetailItem.qml | 0 .../qml}/components/MyLabel.qml | 0 .../qml}/components/MySlider.qml | 0 {qml => application/qml}/cover/CoverPage.qml | 0 .../qml}/harbour-batterybuddy.qml | 0 {qml => application/qml}/pages/AboutPage.qml | 0 {qml => application/qml}/pages/InfoPage.qml | 0 .../qml}/pages/LicensePage.qml | 0 {qml => application/qml}/pages/MainPage.qml | 0 .../qml}/pages/SettingsPage.qml | 0 .../screenshot1.jpg | Bin .../screenshot2.jpg | Bin .../screenshot3.jpg | Bin .../screenshot4.jpg | Bin {sounds => application/sounds}/lowerLimit.ogg | Bin {sounds => application/sounds}/upperLimit.ogg | Bin {src => application/src}/battery.cpp | 0 {src => application/src}/battery.h | 0 .../src}/harbour-batterybuddy.cpp | 0 {src => application/src}/settings.cpp | 0 {src => application/src}/settings.h | 0 .../translations}/harbour-batterybuddy-fi.ts | 0 .../translations}/harbour-batterybuddy-fr.ts | 0 .../translations}/harbour-batterybuddy-sv.ts | 0 .../harbour-batterybuddy-zh_CN.ts | 0 .../translations}/harbour-batterybuddy.ts | 0 .../update-spec-version.sh | 0 harbour-batterybuddy.pro | 84 +--------------- service/service.pro | 22 ++++ service/src/filewatcher.cpp | 94 ++++++++++++++++++ service/src/filewatcher.h | 28 ++++++ service/src/harbour-batterybuddy-daemon.cpp | 6 ++ 43 files changed, 236 insertions(+), 81 deletions(-) rename LICENSE.md => application/LICENSE.md (100%) rename README.md => application/README.md (100%) create mode 100644 application/application.pro rename harbour-batterybuddy.desktop => application/harbour-batterybuddy.desktop (100%) rename harbour-batterybuddy.xcf => application/harbour-batterybuddy.xcf (100%) rename {icons => application/icons}/108x108/harbour-batterybuddy.png (100%) rename {icons => application/icons}/128x128/harbour-batterybuddy.png (100%) rename {icons => application/icons}/172x172/harbour-batterybuddy.png (100%) rename {icons => application/icons}/86x86/harbour-batterybuddy.png (100%) rename {qml => application/qml}/components/AboutLabel.qml (100%) rename {qml => application/qml}/components/BatteryGraph.qml (100%) rename {qml => application/qml}/components/MyDetailItem.qml (100%) rename {qml => application/qml}/components/MyLabel.qml (100%) rename {qml => application/qml}/components/MySlider.qml (100%) rename {qml => application/qml}/cover/CoverPage.qml (100%) rename {qml => application/qml}/harbour-batterybuddy.qml (100%) rename {qml => application/qml}/pages/AboutPage.qml (100%) rename {qml => application/qml}/pages/InfoPage.qml (100%) rename {qml => application/qml}/pages/LicensePage.qml (100%) rename {qml => application/qml}/pages/MainPage.qml (100%) rename {qml => application/qml}/pages/SettingsPage.qml (100%) rename screenshot1.jpg => application/screenshot1.jpg (100%) rename screenshot2.jpg => application/screenshot2.jpg (100%) rename screenshot3.jpg => application/screenshot3.jpg (100%) rename screenshot4.jpg => application/screenshot4.jpg (100%) rename {sounds => application/sounds}/lowerLimit.ogg (100%) rename {sounds => application/sounds}/upperLimit.ogg (100%) rename {src => application/src}/battery.cpp (100%) rename {src => application/src}/battery.h (100%) rename {src => application/src}/harbour-batterybuddy.cpp (100%) rename {src => application/src}/settings.cpp (100%) rename {src => application/src}/settings.h (100%) rename {translations => application/translations}/harbour-batterybuddy-fi.ts (100%) rename {translations => application/translations}/harbour-batterybuddy-fr.ts (100%) rename {translations => application/translations}/harbour-batterybuddy-sv.ts (100%) rename {translations => application/translations}/harbour-batterybuddy-zh_CN.ts (100%) rename {translations => application/translations}/harbour-batterybuddy.ts (100%) rename update-spec-version.sh => application/update-spec-version.sh (100%) create mode 100644 service/service.pro create mode 100644 service/src/filewatcher.cpp create mode 100644 service/src/filewatcher.h create mode 100644 service/src/harbour-batterybuddy-daemon.cpp diff --git a/LICENSE.md b/application/LICENSE.md similarity index 100% rename from LICENSE.md rename to application/LICENSE.md diff --git a/README.md b/application/README.md similarity index 100% rename from README.md rename to application/README.md diff --git a/application/application.pro b/application/application.pro new file mode 100644 index 0000000..cf833d0 --- /dev/null +++ b/application/application.pro @@ -0,0 +1,83 @@ +# NOTICE: +# +# Application name defined in TARGET has a corresponding QML filename. +# If name defined in TARGET is changed, the following needs to be done +# to match new name: +# - corresponding QML filename must be changed +# - desktop icon filename must be changed +# - desktop filename must be changed +# - icon definition filename in desktop file must be changed +# - translation filenames have to be changed + +# The name of your application +TARGET = harbour-batterybuddy + +CONFIG += console sailfishapp sailfishapp_i18n + +VER = 2.2 +REL = 2 + +VERSION = $${VER}-$${REL} +$$system(bash update-spec-version.sh $$TARGET $$VER $$REL) +DEFINES += APP_VERSION=\"\\\"$$VERSION\\\"\" +DEFINES += APP_NAME=\"\\\"$$TARGET\\\"\" + +# Do not define QT_NO_DEBUG_OUTPUT! +# Use "--verbose" and "--debug" at runtime instead. +# See main() for details. +#DEFINES += QT_NO_DEBUG_OUTPUT + +HEADERS += \ + src/battery.h \ + src/settings.h + +SOURCES += src/harbour-batterybuddy.cpp \ + src/battery.cpp \ + src/settings.cpp + +DISTFILES += qml/harbour-batterybuddy.qml \ + qml/components/AboutLabel.qml \ + qml/components/MyLabel.qml \ + qml/components/MySlider.qml \ + qml/pages\LicensePage.qml \ + qml/cover/CoverPage.qml \ + ../rpm/harbour-batterybuddy.spec \ + service/clear-write-permissions.sh \ + service/harbour-batterybuddy.service \ + service/set-write-permissions.sh \ + translations/*.ts \ + harbour-batterybuddy.desktop \ + qml/pages/MainPage.qml \ + qml/pages/AboutPage.qml \ + qml/pages/InfoPage.qml \ + ../rpm/harbour-batterybuddy.changes \ + qml/components/BatteryGraph.qml \ + qml/pages/SettingsPage.qml \ + qml/components/MyDetailItem.qml + +SAILFISHAPP_ICONS = 86x86 108x108 128x128 172x172 + +TRANSLATIONS += translations/*.ts + +OTHER_FILES += service/clear-write-permissions.sh \ + service/harbour-batterybuddy.service \ + service/set-write-permissions.sh + +service.files = service +service.path = /usr/share/$${TARGET} + +INSTALLS += service + +SUBDIRS += service + +# Begin: include sound files + +#OTHER_FILES += sounds/upperLimit.ogg \ +# sounds/lowerLimit.ogg \ + +#sounds.files = sounds +#sounds.path = /usr/share/$${TARGET} + +#INSTALLS += sounds + +# End: include sound files diff --git a/harbour-batterybuddy.desktop b/application/harbour-batterybuddy.desktop similarity index 100% rename from harbour-batterybuddy.desktop rename to application/harbour-batterybuddy.desktop diff --git a/harbour-batterybuddy.xcf b/application/harbour-batterybuddy.xcf similarity index 100% rename from harbour-batterybuddy.xcf rename to application/harbour-batterybuddy.xcf diff --git a/icons/108x108/harbour-batterybuddy.png b/application/icons/108x108/harbour-batterybuddy.png similarity index 100% rename from icons/108x108/harbour-batterybuddy.png rename to application/icons/108x108/harbour-batterybuddy.png diff --git a/icons/128x128/harbour-batterybuddy.png b/application/icons/128x128/harbour-batterybuddy.png similarity index 100% rename from icons/128x128/harbour-batterybuddy.png rename to application/icons/128x128/harbour-batterybuddy.png diff --git a/icons/172x172/harbour-batterybuddy.png b/application/icons/172x172/harbour-batterybuddy.png similarity index 100% rename from icons/172x172/harbour-batterybuddy.png rename to application/icons/172x172/harbour-batterybuddy.png diff --git a/icons/86x86/harbour-batterybuddy.png b/application/icons/86x86/harbour-batterybuddy.png similarity index 100% rename from icons/86x86/harbour-batterybuddy.png rename to application/icons/86x86/harbour-batterybuddy.png diff --git a/qml/components/AboutLabel.qml b/application/qml/components/AboutLabel.qml similarity index 100% rename from qml/components/AboutLabel.qml rename to application/qml/components/AboutLabel.qml diff --git a/qml/components/BatteryGraph.qml b/application/qml/components/BatteryGraph.qml similarity index 100% rename from qml/components/BatteryGraph.qml rename to application/qml/components/BatteryGraph.qml diff --git a/qml/components/MyDetailItem.qml b/application/qml/components/MyDetailItem.qml similarity index 100% rename from qml/components/MyDetailItem.qml rename to application/qml/components/MyDetailItem.qml diff --git a/qml/components/MyLabel.qml b/application/qml/components/MyLabel.qml similarity index 100% rename from qml/components/MyLabel.qml rename to application/qml/components/MyLabel.qml diff --git a/qml/components/MySlider.qml b/application/qml/components/MySlider.qml similarity index 100% rename from qml/components/MySlider.qml rename to application/qml/components/MySlider.qml diff --git a/qml/cover/CoverPage.qml b/application/qml/cover/CoverPage.qml similarity index 100% rename from qml/cover/CoverPage.qml rename to application/qml/cover/CoverPage.qml diff --git a/qml/harbour-batterybuddy.qml b/application/qml/harbour-batterybuddy.qml similarity index 100% rename from qml/harbour-batterybuddy.qml rename to application/qml/harbour-batterybuddy.qml diff --git a/qml/pages/AboutPage.qml b/application/qml/pages/AboutPage.qml similarity index 100% rename from qml/pages/AboutPage.qml rename to application/qml/pages/AboutPage.qml diff --git a/qml/pages/InfoPage.qml b/application/qml/pages/InfoPage.qml similarity index 100% rename from qml/pages/InfoPage.qml rename to application/qml/pages/InfoPage.qml diff --git a/qml/pages/LicensePage.qml b/application/qml/pages/LicensePage.qml similarity index 100% rename from qml/pages/LicensePage.qml rename to application/qml/pages/LicensePage.qml diff --git a/qml/pages/MainPage.qml b/application/qml/pages/MainPage.qml similarity index 100% rename from qml/pages/MainPage.qml rename to application/qml/pages/MainPage.qml diff --git a/qml/pages/SettingsPage.qml b/application/qml/pages/SettingsPage.qml similarity index 100% rename from qml/pages/SettingsPage.qml rename to application/qml/pages/SettingsPage.qml diff --git a/screenshot1.jpg b/application/screenshot1.jpg similarity index 100% rename from screenshot1.jpg rename to application/screenshot1.jpg diff --git a/screenshot2.jpg b/application/screenshot2.jpg similarity index 100% rename from screenshot2.jpg rename to application/screenshot2.jpg diff --git a/screenshot3.jpg b/application/screenshot3.jpg similarity index 100% rename from screenshot3.jpg rename to application/screenshot3.jpg diff --git a/screenshot4.jpg b/application/screenshot4.jpg similarity index 100% rename from screenshot4.jpg rename to application/screenshot4.jpg diff --git a/sounds/lowerLimit.ogg b/application/sounds/lowerLimit.ogg similarity index 100% rename from sounds/lowerLimit.ogg rename to application/sounds/lowerLimit.ogg diff --git a/sounds/upperLimit.ogg b/application/sounds/upperLimit.ogg similarity index 100% rename from sounds/upperLimit.ogg rename to application/sounds/upperLimit.ogg diff --git a/src/battery.cpp b/application/src/battery.cpp similarity index 100% rename from src/battery.cpp rename to application/src/battery.cpp diff --git a/src/battery.h b/application/src/battery.h similarity index 100% rename from src/battery.h rename to application/src/battery.h diff --git a/src/harbour-batterybuddy.cpp b/application/src/harbour-batterybuddy.cpp similarity index 100% rename from src/harbour-batterybuddy.cpp rename to application/src/harbour-batterybuddy.cpp diff --git a/src/settings.cpp b/application/src/settings.cpp similarity index 100% rename from src/settings.cpp rename to application/src/settings.cpp diff --git a/src/settings.h b/application/src/settings.h similarity index 100% rename from src/settings.h rename to application/src/settings.h diff --git a/translations/harbour-batterybuddy-fi.ts b/application/translations/harbour-batterybuddy-fi.ts similarity index 100% rename from translations/harbour-batterybuddy-fi.ts rename to application/translations/harbour-batterybuddy-fi.ts diff --git a/translations/harbour-batterybuddy-fr.ts b/application/translations/harbour-batterybuddy-fr.ts similarity index 100% rename from translations/harbour-batterybuddy-fr.ts rename to application/translations/harbour-batterybuddy-fr.ts diff --git a/translations/harbour-batterybuddy-sv.ts b/application/translations/harbour-batterybuddy-sv.ts similarity index 100% rename from translations/harbour-batterybuddy-sv.ts rename to application/translations/harbour-batterybuddy-sv.ts diff --git a/translations/harbour-batterybuddy-zh_CN.ts b/application/translations/harbour-batterybuddy-zh_CN.ts similarity index 100% rename from translations/harbour-batterybuddy-zh_CN.ts rename to application/translations/harbour-batterybuddy-zh_CN.ts diff --git a/translations/harbour-batterybuddy.ts b/application/translations/harbour-batterybuddy.ts similarity index 100% rename from translations/harbour-batterybuddy.ts rename to application/translations/harbour-batterybuddy.ts diff --git a/update-spec-version.sh b/application/update-spec-version.sh similarity index 100% rename from update-spec-version.sh rename to application/update-spec-version.sh diff --git a/harbour-batterybuddy.pro b/harbour-batterybuddy.pro index a07e56f..31b01a9 100644 --- a/harbour-batterybuddy.pro +++ b/harbour-batterybuddy.pro @@ -1,81 +1,3 @@ -# NOTICE: -# -# Application name defined in TARGET has a corresponding QML filename. -# If name defined in TARGET is changed, the following needs to be done -# to match new name: -# - corresponding QML filename must be changed -# - desktop icon filename must be changed -# - desktop filename must be changed -# - icon definition filename in desktop file must be changed -# - translation filenames have to be changed - -# The name of your application -TARGET = harbour-batterybuddy - -CONFIG += console sailfishapp sailfishapp_i18n - -VER = 2.2 -REL = 2 - -VERSION = $${VER}-$${REL} -$$system(bash update-spec-version.sh $$TARGET $$VER $$REL) -DEFINES += APP_VERSION=\"\\\"$$VERSION\\\"\" -DEFINES += APP_NAME=\"\\\"$$TARGET\\\"\" - -# Do not define QT_NO_DEBUG_OUTPUT! -# Use "--verbose" and "--debug" at runtime instead. -# See main() for details. -#DEFINES += QT_NO_DEBUG_OUTPUT - -HEADERS += \ - src/battery.h \ - src/settings.h - -SOURCES += src/harbour-batterybuddy.cpp \ - src/battery.cpp \ - src/settings.cpp - -DISTFILES += qml/harbour-batterybuddy.qml \ - qml/components/AboutLabel.qml \ - qml/components/MyLabel.qml \ - qml/components/MySlider.qml \ - qml/pages\LicensePage.qml \ - qml/cover/CoverPage.qml \ - rpm/harbour-batterybuddy.spec \ - service/clear-write-permissions.sh \ - service/harbour-batterybuddy.service \ - service/set-write-permissions.sh \ - translations/*.ts \ - harbour-batterybuddy.desktop \ - qml/pages/MainPage.qml \ - qml/pages/AboutPage.qml \ - qml/pages/InfoPage.qml \ - rpm/harbour-batterybuddy.changes \ - qml/components/BatteryGraph.qml \ - qml/pages/SettingsPage.qml \ - qml/components/MyDetailItem.qml - -SAILFISHAPP_ICONS = 86x86 108x108 128x128 172x172 - -TRANSLATIONS += translations/*.ts - -OTHER_FILES += service/clear-write-permissions.sh \ - service/harbour-batterybuddy.service \ - service/set-write-permissions.sh - -service.files = service -service.path = /usr/share/$${TARGET} - -INSTALLS += service - -# Begin: include sound files - -#OTHER_FILES += sounds/upperLimit.ogg \ -# sounds/lowerLimit.ogg \ - -#sounds.files = sounds -#sounds.path = /usr/share/$${TARGET} - -#INSTALLS += sounds - -# End: include sound files +TEMPLATE = subdirs +CONFIG += ordered +SUBDIRS = application service diff --git a/service/service.pro b/service/service.pro new file mode 100644 index 0000000..bf82242 --- /dev/null +++ b/service/service.pro @@ -0,0 +1,22 @@ +TARGET = harbour-batterybuddy-daemon + +CONFIG = qt c++11 sailfish_build + +QT = dbus network xml + +HEADERS += \ + src/filewatcher.h + +SOURCES += \ + src/filewatcher.cpp \ + src/harbour-batterybuddy-daemon.cpp + +OTHER_FILES += harbour-batterybuddy-daemon.service + +#include($$PWD/../common/common.pri) + +service.path = /usr/lib/systemd/user/ +service.files += harbour-batterybuddy-daemon.service +target.path = /usr/bin/ + +INSTALLS += target service diff --git a/service/src/filewatcher.cpp b/service/src/filewatcher.cpp new file mode 100644 index 0000000..c654ec5 --- /dev/null +++ b/service/src/filewatcher.cpp @@ -0,0 +1,94 @@ +#include "filewatcher.h" + +FileWatcher::FileWatcher(QObject *parent) : QObject(parent) +{ + QString filename; + + // Number: charge percentage, e.g. 42 + chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this); + qInfo() << "Reading capacity from" << chargeFile->fileName(); + + // String: charging, discharging, full, empty, unknown (others?) + stateFile = new QFile("/sys/class/power_supply/battery/status", this); + qInfo() << "Reading charge state from" << stateFile->fileName(); + + // Number: 0 or 1 + chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this); + qInfo() << "Reading charger status from" << chargerConnectedFile->fileName(); + + // Find and use the file to control the charger + + // e.g. for Sony Xperia XA2 + filename = "/sys/class/power_supply/battery/input_suspend"; + if(!chargerControlFile && QFile::exists(filename)) { + chargerControlFile = new QFile(filename, this); + enableChargingValue = 0; + disableChargingValue = 1; + } + + // e.g. for Sony Xperia Z3 Compact Tablet + filename = "/sys/class/power_supply/battery/charging_enabled"; + if(!chargerControlFile && QFile::exists(filename)) { + chargerControlFile = new QFile(filename, this); + enableChargingValue = 1; + disableChargingValue = 0; + } + + // e.g. for Jolla Phone + filename = "/sys/class/power_supply/usb/charger_disable"; + if(!chargerControlFile && QFile::exists(filename)) { + chargerControlFile = new QFile(filename, this); + enableChargingValue = 0; + disableChargingValue = 1; + } + + + if(!chargerControlFile) { + qWarning() << "Charger control file not found!"; + qWarning() << "Please contact the developer with your device model!"; + } + + // If we found a usable file, check that it is writable + if(chargerControlFile) { + if(chargerControlFile->open(QIODevice::WriteOnly)) { + qInfo() << "Controlling charging via" << chargerControlFile->fileName(); + chargerControlFile->close(); + } + else { + delete chargerControlFile; + chargerControlFile = Q_NULLPTR; + qWarning() << "Charger control file" << chargerControlFile->fileName() << "is not writable"; + qWarning() << "Charger control feature disabled"; + } + } + + chargeWatcher = new QFileSystemWatcher(QStringList(chargeFile->fileName()), this); + stateWatcher = new QFileSystemWatcher(QStringList(stateFile->fileName()), this); + chargerConnectedWatcher = new QFileSystemWatcher(QStringList(chargerConnectedFile->fileName()), this); + + connect(chargeWatcher, SIGNAL(fileChanged(QString)), this, SLOT(updateCharge())); + connect(stateWatcher, SIGNAL(fileChanged(QString)), this, SLOT(updateState())); + connect(chargerConnectedWatcher, SIGNAL(fileChanged(QString)), this, SLOT(updateChargerConnected())); + + return; +} + +FileWatcher::~FileWatcher() +{ + +} + +void FileWatcher::updateCharge() +{ + +} + +void FileWatcher::updateState() +{ + +} + +void FileWatcher::updateChargerConnected() +{ + +} diff --git a/service/src/filewatcher.h b/service/src/filewatcher.h new file mode 100644 index 0000000..729f5cf --- /dev/null +++ b/service/src/filewatcher.h @@ -0,0 +1,28 @@ +#ifndef FILEWATCHER_H +#define FILEWATCHER_H + +#include +#include +#include + +#include + +class FileWatcher : public QObject +{ + Q_OBJECT +public: + FileWatcher(QObject* parent = nullptr); + ~FileWatcher(); + +private: + QFileSystemWatcher *chargeWatcher, *stateWatcher, *chargerConnectedWatcher; + QFile *chargeFile, *stateFile, *chargerConnectedFile, *chargerControlFile; + int enableChargingValue = 1, disableChargingValue = 0; + +private slots: + void updateCharge(); + void updateState(); + void updateChargerConnected(); +}; + +#endif // FILEWATCHER_H diff --git a/service/src/harbour-batterybuddy-daemon.cpp b/service/src/harbour-batterybuddy-daemon.cpp new file mode 100644 index 0000000..23c2b25 --- /dev/null +++ b/service/src/harbour-batterybuddy-daemon.cpp @@ -0,0 +1,6 @@ +#include "filewatcher.h" + +int main() +{ + return 0; +}