Use const keywords and references a lot more

This commit is contained in:
Matti Viljanen 2021-04-17 22:13:57 +03:00
parent d3132a453d
commit f4bf1d0763
No known key found for this signature in database
GPG key ID: CF32A1495158F888
9 changed files with 76 additions and 77 deletions

View file

@ -29,13 +29,13 @@ Settings::Settings(Logger *newLogger, QObject *parent) : QObject(parent)
logV("Using " + mySettings->fileName()); logV("Using " + mySettings->fileName());
// Read in the values // Read in the values
loadInteger(sLowAlert, &lowAlert, 5, 99); loadInteger(sLowAlert, lowAlert, 5, 99);
loadInteger(sHighAlert, &highAlert, 6, 100); loadInteger(sHighAlert, highAlert, 6, 100);
loadInteger(sHighNotificationsInterval, &highNotificationsInterval, 50, 610); loadInteger(sHighNotificationsInterval, highNotificationsInterval, 50, 610);
loadInteger(sLowNotificationsInterval, &lowNotificationsInterval, 50, 610); loadInteger(sLowNotificationsInterval, lowNotificationsInterval, 50, 610);
loadInteger(sLimitEnabled, &limitEnabled, 0, 1); loadInteger(sLimitEnabled, limitEnabled, 0, 1);
loadInteger(sLowLimit, &lowLimit, 5, 99); loadInteger(sLowLimit, lowLimit, 5, 99);
loadInteger(sHighLimit, &highLimit, 6, 100); loadInteger(sHighLimit, highLimit, 6, 100);
notificationTitle = tr("Battery charge %1%"); notificationTitle = tr("Battery charge %1%");
notificationLowText = tr("Please connect the charger."); notificationLowText = tr("Please connect the charger.");
@ -44,13 +44,13 @@ Settings::Settings(Logger *newLogger, QObject *parent) : QObject(parent)
Settings::~Settings() Settings::~Settings()
{ {
saveInteger(sLowAlert, &lowAlert); saveInteger(sLowAlert, lowAlert);
saveInteger(sHighAlert, &highAlert); saveInteger(sHighAlert, highAlert);
saveInteger(sHighNotificationsInterval, &highNotificationsInterval); saveInteger(sHighNotificationsInterval, highNotificationsInterval);
saveInteger(sLowNotificationsInterval, &lowNotificationsInterval); saveInteger(sLowNotificationsInterval, lowNotificationsInterval);
saveInteger(sLimitEnabled, &limitEnabled); saveInteger(sLimitEnabled, limitEnabled);
saveInteger(sLowLimit, &lowLimit); saveInteger(sLowLimit, lowLimit);
saveInteger(sHighLimit, &highLimit); saveInteger(sHighLimit, highLimit);
mySettings->setValue(sNotificationTitle, notificationTitle); mySettings->setValue(sNotificationTitle, notificationTitle);
mySettings->setValue(sNotificationLowText, notificationLowText); mySettings->setValue(sNotificationLowText, notificationLowText);
mySettings->setValue(sNotificationHighText, notificationHighText); mySettings->setValue(sNotificationHighText, notificationHighText);
@ -72,74 +72,74 @@ QString Settings::getNotificationTitle() { return notificationTitle; }
QString Settings::getNotificationLowText() { return notificationLowText; } QString Settings::getNotificationLowText() { return notificationLowText; }
QString Settings::getNotificationHighText() { return notificationHighText; } QString Settings::getNotificationHighText() { return notificationHighText; }
void Settings::setLowAlert(int newLimit) { void Settings::setLowAlert(const int newLimit) {
lowAlert = newLimit; lowAlert = newLimit;
saveInteger(sLowAlert, &lowAlert); saveInteger(sLowAlert, lowAlert);
// 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);
logD(QString("%1 %2").arg(sLowAlert).arg(newLimit)); logD(QString("%1 %2").arg(sLowAlert).arg(newLimit));
} }
void Settings::setHighAlert(int newLimit) { void Settings::setHighAlert(const int newLimit) {
highAlert = newLimit; highAlert = newLimit;
saveInteger(sHighAlert, &highAlert); saveInteger(sHighAlert, highAlert);
mySettings->sync(); mySettings->sync();
emit highAlertChanged(highAlert); emit highAlertChanged(highAlert);
} }
void Settings::setHighNotificationsInterval(int newInterval) { void Settings::setHighNotificationsInterval(const int newInterval) {
highNotificationsInterval = newInterval; highNotificationsInterval = newInterval;
saveInteger(sHighNotificationsInterval, &highNotificationsInterval); saveInteger(sHighNotificationsInterval, highNotificationsInterval);
mySettings->sync(); mySettings->sync();
emit highNotificationsIntervalChanged(highNotificationsInterval); emit highNotificationsIntervalChanged(highNotificationsInterval);
} }
void Settings::setLowNotificationsInterval(int newInterval) { void Settings::setLowNotificationsInterval(const int newInterval) {
lowNotificationsInterval = newInterval; lowNotificationsInterval = newInterval;
saveInteger(sLowNotificationsInterval, &lowNotificationsInterval); saveInteger(sLowNotificationsInterval, lowNotificationsInterval);
mySettings->sync(); mySettings->sync();
emit highNotificationsIntervalChanged(lowNotificationsInterval); emit highNotificationsIntervalChanged(lowNotificationsInterval);
} }
void Settings::setLowLimit(int newLimit) { void Settings::setLowLimit(const int newLimit) {
lowLimit = newLimit; lowLimit = newLimit;
saveInteger(sLowLimit, &lowLimit); saveInteger(sLowLimit, lowLimit);
// 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);
} }
void Settings::setHighLimit(int newLimit) { void Settings::setHighLimit(const int newLimit) {
highLimit = newLimit; highLimit = newLimit;
saveInteger(sHighLimit, &highLimit); saveInteger(sHighLimit, highLimit);
mySettings->sync(); mySettings->sync();
emit highLimitChanged(highLimit); emit highLimitChanged(highLimit);
} }
void Settings::setLimitEnabled(bool newEnabled) { void Settings::setLimitEnabled(const bool newEnabled) {
limitEnabled = (newEnabled ? 1 : 0); limitEnabled = (newEnabled ? 1 : 0);
saveInteger(sLimitEnabled, &limitEnabled); saveInteger(sLimitEnabled, limitEnabled);
mySettings->sync(); mySettings->sync();
emit limitEnabledChanged(limitEnabled); emit limitEnabledChanged(limitEnabled);
} }
void Settings::setNotificationTitle(QString newText) { void Settings::setNotificationTitle(const QString newText) {
notificationTitle = newText; notificationTitle = QString(newText).replace("\"", "\\\"");
mySettings->setValue(sNotificationTitle, notificationTitle); mySettings->setValue(sNotificationTitle, notificationTitle);
mySettings->sync(); mySettings->sync();
emit notificationTitleChanged(notificationTitle); emit notificationTitleChanged(notificationTitle);
} }
void Settings::setNotificationLowText(QString newText) { void Settings::setNotificationLowText(const QString newText) {
notificationLowText = newText; notificationLowText = QString(newText).replace("\"", "\\\"");
mySettings->setValue(sNotificationLowText, notificationLowText); mySettings->setValue(sNotificationLowText, notificationLowText);
mySettings->sync(); mySettings->sync();
emit notificationLowTextChanged(notificationLowText); emit notificationLowTextChanged(notificationLowText);
} }
void Settings::setNotificationHighText(QString newText) { void Settings::setNotificationHighText(const QString newText) {
notificationHighText = newText; notificationHighText = QString(newText).replace("\"", "\\\"");
mySettings->setValue(sNotificationHighText, notificationHighText); mySettings->setValue(sNotificationHighText, notificationHighText);
mySettings->sync(); mySettings->sync();
emit notificationHighTextChanged(notificationHighText); emit notificationHighTextChanged(notificationHighText);
@ -147,14 +147,13 @@ void Settings::setNotificationHighText(QString newText) {
int Settings::bound(int value, int min, int max) { int Settings::bound(int value, int min, int max) {
return (value <= min ? min : (value >= max ? max : value)); return (value <= min ? min : (value >= max ? max : value));
void Settings::loadInteger(const char *key, int &value, const int min, const int max) {
int newValue = mySettings->value(key, value).toInt();
value = (newValue <= min ? min : (newValue >= max ? max : newValue));
logV(QString("Load: %1 %2").arg(key).arg(value)); logV(QString("Load: %1 %2").arg(key).arg(value));
} }
void Settings::loadInteger(const char* key, int *value, int min, int max) { void Settings::saveInteger(const char* key, const int &value) {
*value = bound(mySettings->value(key, *value).toInt(), min, max); mySettings->setValue(key, QByteArray::number(value));
}
void Settings::saveInteger(const char* key, int *value) {
mySettings->setValue(key, QByteArray::number(*value));
logV(QString("Save: %1 %2").arg(key).arg(value)); logV(QString("Save: %1 %2").arg(key).arg(value));
} }

View file

@ -55,16 +55,16 @@ public:
QString getNotificationLowText(); QString getNotificationLowText();
QString getNotificationHighText(); QString getNotificationHighText();
void setLowAlert(int newLimit); void setLowAlert(const int newLimit);
void setHighAlert(int newLimit); void setHighAlert(const int newLimit);
void setHighNotificationsInterval(int newInterval); void setHighNotificationsInterval(const int newInterval);
void setLowNotificationsInterval(int newInterval); void setLowNotificationsInterval(const int newInterval);
void setLowLimit(int newLimit); void setLowLimit(const int newLimit);
void setHighLimit(int newLimit); void setHighLimit(const int newLimit);
void setLimitEnabled(bool newEnabled); void setLimitEnabled(const bool newEnabled);
void setNotificationTitle(QString newText); void setNotificationTitle(const QString newText);
void setNotificationLowText(QString newText); void setNotificationLowText(const QString newText);
void setNotificationHighText(QString newText); void setNotificationHighText(const QString newText);
private: private:
QSettings *mySettings = nullptr; QSettings *mySettings = nullptr;
@ -100,8 +100,8 @@ private:
const char* sNotificationHighText = "notificationHighText"; const char* sNotificationHighText = "notificationHighText";
int bound(int value, int min, int max); int bound(int value, int min, int max);
void loadInteger(const char *key, int *value, int min, int max); void loadInteger(const char *key, int &value, const int min, const int max);
void saveInteger(const char *key, int *value); void saveInteger(const char *key, const int &value);
signals: signals:
void lowAlertChanged(int); void lowAlertChanged(int);

View file

@ -207,7 +207,7 @@ QString Battery::getState() { return state; }
bool Battery::getChargingEnabled() { return chargingEnabled; } bool Battery::getChargingEnabled() { return chargingEnabled; }
bool Battery::setChargingEnabled(bool isEnabled) { bool Battery::setChargingEnabled(const bool isEnabled) {
bool success = false; bool success = false;
if(chargingEnabledFile) { if(chargingEnabledFile) {
if(chargingEnabledFile->open(QIODevice::WriteOnly)) { if(chargingEnabledFile->open(QIODevice::WriteOnly)) {

View file

@ -42,7 +42,7 @@ public:
QString getState(); QString getState();
bool getChargingEnabled(); bool getChargingEnabled();
bool setChargingEnabled(bool); bool setChargingEnabled(const bool isEnabled);
public slots: public slots:
void updateData(); void updateData();

View file

@ -39,7 +39,7 @@ 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(); int vol = profile->getRingtoneVolume();
sound.setVolume(vol); sound.setVolume(vol);
playSound = true; playSound = true;
@ -68,7 +68,7 @@ void MyNotification::close()
return; return;
} }
void MyNotification::soundLoadedChanged(QMediaPlayer::MediaStatus newStatus) { void MyNotification::soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus) {
if(playSound && newStatus == QMediaPlayer::LoadedMedia) { if(playSound && newStatus == QMediaPlayer::LoadedMedia) {
sound.play(); sound.play();
playSound = false; playSound = false;

View file

@ -46,7 +46,7 @@ private:
Profile* profile; Profile* profile;
private slots: private slots:
void soundLoadedChanged(QMediaPlayer::MediaStatus newStatus); void soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus);
}; };
#endif // MYNOTIFICATION_H #endif // MYNOTIFICATION_H

View file

@ -22,11 +22,10 @@ Profile::Profile(Logger* newLogger, QObject *parent) : QObject(parent)
logger = newLogger; logger = newLogger;
} }
uint Profile::getRingtoneVolume() { int Profile::getRingtoneVolume() {
QDBusInterface interface("com.nokia.profiled", const QString dots = "com.nokia.profiled";
"/com/nokia/profiled", const QString slashes = "/com/nokia/profiled";
"com.nokia.profiled", QDBusInterface interface(dots, slashes, dots, connection);
connection);
QDBusMessage message = interface.call("get_profile"); QDBusMessage message = interface.call("get_profile");
QString profile = message.arguments().at(0).toString(); QString profile = message.arguments().at(0).toString();

View file

@ -96,16 +96,17 @@ Settings::Settings(Logger* newLogger, QObject *parent) : QObject(parent)
Settings::~Settings() { } Settings::~Settings() { }
bool Settings::loadInteger(const char* key, int *value, int min, int max) { bool Settings::loadInteger(const char *key, int &value, const int min, const int max) {
oldValue = *value; oldValue = value;
*value = bound(mySettings->value(key, *value).toInt(), min, max); value = mySettings->value(key, value).toInt();
if(oldValue != *value) { value = (value <= min ? min : (value >= max ? max : value));
if(oldValue != value) {
logV(QString("Load: %1 %2").arg(key).arg(value)); logV(QString("Load: %1 %2").arg(key).arg(value));
} }
return oldValue != *value; return oldValue != value;
} }
void Settings::updateConfig(QString path) { void Settings::updateConfig(const QString path) {
// Use the same file location as GUI for data exchange // Use the same file location as GUI for data exchange
if(!mySettings) { if(!mySettings) {
@ -116,13 +117,13 @@ void Settings::updateConfig(QString path) {
// Read in the values // Read in the values
bool restartTimers = false; bool restartTimers = false;
loadInteger(sLowAlert, &lowAlert, 5, 99); loadInteger(sLowAlert, lowAlert, 5, 99);
loadInteger(sHighAlert, &highAlert, 6, 100); loadInteger(sHighAlert, highAlert, 6, 100);
restartTimers |= loadInteger(sHighNotificationsInterval, &highNotificationsInterval, 50, 610); restartTimers |= loadInteger(sHighNotificationsInterval, highNotificationsInterval, 50, 610);
restartTimers |= loadInteger(sLowNotificationsInterval, &lowNotificationsInterval, 50, 610); restartTimers |= loadInteger(sLowNotificationsInterval, lowNotificationsInterval, 50, 610);
loadInteger(sLimitEnabled, &limitEnabled, 0, 1); loadInteger(sLimitEnabled, limitEnabled, 0, 1);
loadInteger(sLowLimit, &lowLimit, 5, 99); loadInteger(sLowLimit, lowLimit, 5, 99);
loadInteger(sHighLimit, &highLimit, 6, 100); loadInteger(sHighLimit, highLimit, 6, 100);
// These are translated in the GUI application // These are translated in the GUI application
// and delivered here via the config file // and delivered here via the config file

View file

@ -92,10 +92,10 @@ private:
const char* sLogFilename = "logFilename"; const char* sLogFilename = "logFilename";
int bound(int value, int min, int max); int bound(int value, int min, int max);
bool loadInteger(const char *key, int *value, int min, int max); bool loadInteger(const char *key, int &value, const int min, const int max);
private slots: private slots:
void updateConfig(QString path); void updateConfig(const QString path);
signals: signals:
void resetTimers(); void resetTimers();