Merge pull request #56 from monich/settings

Moved app settings to a separate object
This commit is contained in:
Sebastian Wolf 2020-10-06 21:42:57 +02:00 committed by GitHub
commit c76240e914
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 100 additions and 31 deletions

View file

@ -19,6 +19,7 @@ PKGCONFIG += nemonotifications-qt5 ngf-qt5
QT += core dbus QT += core dbus
SOURCES += src/harbour-fernschreiber.cpp \ SOURCES += src/harbour-fernschreiber.cpp \
src/appsettings.cpp \
src/chatlistmodel.cpp \ src/chatlistmodel.cpp \
src/chatmodel.cpp \ src/chatmodel.cpp \
src/dbusadaptor.cpp \ src/dbusadaptor.cpp \
@ -102,6 +103,7 @@ INSTALLS += telegram 86.png 108.png 128.png 172.png 256.png \
fernschreiber.desktop gui images fernschreiber.desktop gui images
HEADERS += \ HEADERS += \
src/appsettings.h \
src/chatlistmodel.h \ src/chatlistmodel.h \
src/chatmodel.h \ src/chatmodel.h \
src/dbusadaptor.h \ src/dbusadaptor.h \

View file

@ -1078,20 +1078,15 @@ Page {
} }
} }
EnterKey.onClicked: { EnterKey.onClicked: {
if (tdLibWrapper.getSendByEnter()) { if (appSettings.sendByEnter) {
sendMessage(); sendMessage();
newMessageTextField.text = ""; newMessageTextField.text = "";
newMessageTextField.focus = false; newMessageTextField.focus = false;
} }
} }
EnterKey.enabled: tdLibWrapper.getSendByEnter() ? text.length > 0 : true; EnterKey.enabled: !appSettings.sendByEnter || text.length
EnterKey.iconSource: appSettings.sendByEnter ? "image://theme/icon-m-chat" : "image://theme/icon-m-enter"
Component.onCompleted: {
if (tdLibWrapper.getSendByEnter()) {
EnterKey.iconSource = "image://theme/icon-m-chat";
}
}
onTextChanged: { onTextChanged: {
controlSendButton(); controlSendButton();

View file

@ -44,11 +44,12 @@ Page {
} }
TextSwitch { TextSwitch {
checked: tdLibWrapper.getSendByEnter() checked: appSettings.sendByEnter
text: qsTr("Send message by enter") text: qsTr("Send message by enter")
description: qsTr("Send your message by pressing the enter key") description: qsTr("Send your message by pressing the enter key")
onCheckedChanged: { automaticCheck: false
tdLibWrapper.setSendByEnter(checked); onClicked: {
appSettings.sendByEnter = !checked
} }
} }

43
src/appsettings.cpp Normal file
View file

@ -0,0 +1,43 @@
/*
This file is part of Fernschreiber.
Fernschreiber 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.
Fernschreiber 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 Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
#include "appsettings.h"
#include <QDebug>
#define LOG(x) qDebug() << "[AppSettings]" << x
namespace {
const QString KEY_SEND_BY_ENTER("sendByEnter");
}
AppSettings::AppSettings(QObject *parent) : QObject(parent), settings("harbour-fernschreiber", "settings")
{
}
bool AppSettings::getSendByEnter() const
{
return settings.value(KEY_SEND_BY_ENTER, false).toBool();
}
void AppSettings::setSendByEnter(bool sendByEnter)
{
if (getSendByEnter() != sendByEnter) {
LOG(KEY_SEND_BY_ENTER << sendByEnter);
settings.setValue(KEY_SEND_BY_ENTER, sendByEnter);
emit sendByEnterChanged();
}
}

41
src/appsettings.h Normal file
View file

@ -0,0 +1,41 @@
/*
This file is part of Fernschreiber.
Fernschreiber 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.
Fernschreiber 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 Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef APPSETTINGS_H
#define APPSETTINGS_H
#include <QObject>
#include <QSettings>
class AppSettings : public QObject {
Q_OBJECT
Q_PROPERTY(bool sendByEnter READ getSendByEnter WRITE setSendByEnter NOTIFY sendByEnterChanged)
public:
AppSettings(QObject *parent = Q_NULLPTR);
bool getSendByEnter() const;
void setSendByEnter(bool sendByEnter);
signals:
void sendByEnterChanged();
private:
QSettings settings;
};
#endif // APPSETTINGS_H

View file

@ -29,6 +29,7 @@
#include <QQmlEngine> #include <QQmlEngine>
#include <QGuiApplication> #include <QGuiApplication>
#include "appsettings.h"
#include "tdlibwrapper.h" #include "tdlibwrapper.h"
#include "chatlistmodel.h" #include "chatlistmodel.h"
#include "chatmodel.h" #include "chatmodel.h"
@ -43,6 +44,9 @@ int main(int argc, char *argv[])
QQmlContext *context = view.data()->rootContext(); QQmlContext *context = view.data()->rootContext();
AppSettings *appSettings = new AppSettings(view.data());
context->setContextProperty("appSettings", appSettings);
TDLibWrapper *tdLibWrapper = new TDLibWrapper(view.data()); TDLibWrapper *tdLibWrapper = new TDLibWrapper(view.data());
context->setContextProperty("tdLibWrapper", tdLibWrapper); context->setContextProperty("tdLibWrapper", tdLibWrapper);
qmlRegisterType<TDLibWrapper>("WerkWolf.Fernschreiber", 1, 0, "TelegramAPI"); qmlRegisterType<TDLibWrapper>("WerkWolf.Fernschreiber", 1, 0, "TelegramAPI");

View file

@ -25,7 +25,8 @@
#include <QLocale> #include <QLocale>
#include <QProcess> #include <QProcess>
#include <QSysInfo> #include <QSysInfo>
#include <QSettings> #include <QDebug>
#include <QJsonDocument>
#include <QStandardPaths> #include <QStandardPaths>
#include <QDBusConnection> #include <QDBusConnection>
#include <QDBusInterface> #include <QDBusInterface>
@ -43,7 +44,7 @@ namespace {
const QString _TYPE("@type"); const QString _TYPE("@type");
} }
TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent), settings("harbour-fernschreiber", "settings") TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent)
{ {
LOG("Initializing TD Lib..."); LOG("Initializing TD Lib...");
this->tdLibClient = td_json_client_create(); this->tdLibClient = td_json_client_create();
@ -529,16 +530,6 @@ void TDLibWrapper::controlScreenSaver(const bool &enabled)
} }
} }
void TDLibWrapper::setSendByEnter(const bool &sendByEnter)
{
settings.setValue("sendByEnter", sendByEnter);
}
bool TDLibWrapper::getSendByEnter()
{
return settings.value("sendByEnter", false).toBool();
}
DBusAdaptor *TDLibWrapper::getDBusAdaptor() DBusAdaptor *TDLibWrapper::getDBusAdaptor()
{ {
return this->dbusInterface->getDBusAdaptor(); return this->dbusInterface->getDBusAdaptor();

View file

@ -20,11 +20,6 @@
#define TDLIBWRAPPER_H #define TDLIBWRAPPER_H
#include <QCoreApplication> #include <QCoreApplication>
#include <QObject>
#include <QDebug>
#include <QJsonDocument>
#include <QStandardPaths>
#include <QSettings>
#include <td/telegram/td_json_client.h> #include <td/telegram/td_json_client.h>
#include "tdlibreceiver.h" #include "tdlibreceiver.h"
#include "dbusadaptor.h" #include "dbusadaptor.h"
@ -104,8 +99,6 @@ public:
Q_INVOKABLE void copyFileToDownloads(const QString &filePath); Q_INVOKABLE void copyFileToDownloads(const QString &filePath);
Q_INVOKABLE void openFileOnDevice(const QString &filePath); Q_INVOKABLE void openFileOnDevice(const QString &filePath);
Q_INVOKABLE void controlScreenSaver(const bool &enabled); Q_INVOKABLE void controlScreenSaver(const bool &enabled);
Q_INVOKABLE void setSendByEnter(const bool &sendByEnter);
Q_INVOKABLE bool getSendByEnter();
DBusAdaptor *getDBusAdaptor(); DBusAdaptor *getDBusAdaptor();
@ -231,7 +224,6 @@ private:
QVariantMap unreadChatInformation; QVariantMap unreadChatInformation;
QHash<qlonglong,Group*> basicGroups; QHash<qlonglong,Group*> basicGroups;
QHash<qlonglong,Group*> superGroups; QHash<qlonglong,Group*> superGroups;
QSettings settings;
}; };
#endif // TDLIBWRAPPER_H #endif // TDLIBWRAPPER_H