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

View file

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

View file

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

View file

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

View file

@ -39,7 +39,7 @@ void MyNotification::send(QString title, QString body, QString soundFile)
title = title.replace("\"", "\\\"");
body = body.replace("\"", "\\\"");
int vol = profile.getRingtoneVolume();
int vol = profile->getRingtoneVolume();
sound.setVolume(vol);
playSound = true;
@ -68,7 +68,7 @@ void MyNotification::close()
return;
}
void MyNotification::soundLoadedChanged(QMediaPlayer::MediaStatus newStatus) {
void MyNotification::soundLoadedChanged(const QMediaPlayer::MediaStatus newStatus) {
if(playSound && newStatus == QMediaPlayer::LoadedMedia) {
sound.play();
playSound = false;

View file

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

View file

@ -22,11 +22,10 @@ Profile::Profile(Logger* newLogger, QObject *parent) : QObject(parent)
logger = newLogger;
}
uint Profile::getRingtoneVolume() {
QDBusInterface interface("com.nokia.profiled",
"/com/nokia/profiled",
"com.nokia.profiled",
connection);
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();

View file

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

View file

@ -92,10 +92,10 @@ private:
const char* sLogFilename = "logFilename";
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:
void updateConfig(QString path);
void updateConfig(const QString path);
signals:
void resetTimers();