diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index 899d865..a85c92e 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -19,6 +19,7 @@ PKGCONFIG += nemonotifications-qt5 ngf-qt5 QT += core dbus SOURCES += src/harbour-fernschreiber.cpp \ + src/appsettings.cpp \ src/chatlistmodel.cpp \ src/chatmodel.cpp \ src/dbusadaptor.cpp \ @@ -102,6 +103,7 @@ INSTALLS += telegram 86.png 108.png 128.png 172.png 256.png \ fernschreiber.desktop gui images HEADERS += \ + src/appsettings.h \ src/chatlistmodel.h \ src/chatmodel.h \ src/dbusadaptor.h \ diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index fa9b5bc..2848dc7 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -1078,20 +1078,15 @@ Page { } } EnterKey.onClicked: { - if (tdLibWrapper.getSendByEnter()) { + if (appSettings.sendByEnter) { sendMessage(); newMessageTextField.text = ""; newMessageTextField.focus = false; } } - EnterKey.enabled: tdLibWrapper.getSendByEnter() ? text.length > 0 : true; - - Component.onCompleted: { - if (tdLibWrapper.getSendByEnter()) { - EnterKey.iconSource = "image://theme/icon-m-chat"; - } - } + EnterKey.enabled: !appSettings.sendByEnter || text.length + EnterKey.iconSource: appSettings.sendByEnter ? "image://theme/icon-m-chat" : "image://theme/icon-m-enter" onTextChanged: { controlSendButton(); diff --git a/qml/pages/SettingsPage.qml b/qml/pages/SettingsPage.qml index 1e8d2e1..90d3ddb 100644 --- a/qml/pages/SettingsPage.qml +++ b/qml/pages/SettingsPage.qml @@ -44,11 +44,12 @@ Page { } TextSwitch { - checked: tdLibWrapper.getSendByEnter() + checked: appSettings.sendByEnter text: qsTr("Send message by enter") description: qsTr("Send your message by pressing the enter key") - onCheckedChanged: { - tdLibWrapper.setSendByEnter(checked); + automaticCheck: false + onClicked: { + appSettings.sendByEnter = !checked } } diff --git a/src/appsettings.cpp b/src/appsettings.cpp new file mode 100644 index 0000000..858e768 --- /dev/null +++ b/src/appsettings.cpp @@ -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 . +*/ + +#include "appsettings.h" +#include + +#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(); + } +} diff --git a/src/appsettings.h b/src/appsettings.h new file mode 100644 index 0000000..96124c5 --- /dev/null +++ b/src/appsettings.h @@ -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 . +*/ + +#ifndef APPSETTINGS_H +#define APPSETTINGS_H + +#include +#include + +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 diff --git a/src/harbour-fernschreiber.cpp b/src/harbour-fernschreiber.cpp index 9b2a402..b020546 100644 --- a/src/harbour-fernschreiber.cpp +++ b/src/harbour-fernschreiber.cpp @@ -29,6 +29,7 @@ #include #include +#include "appsettings.h" #include "tdlibwrapper.h" #include "chatlistmodel.h" #include "chatmodel.h" @@ -43,6 +44,9 @@ int main(int argc, char *argv[]) QQmlContext *context = view.data()->rootContext(); + AppSettings *appSettings = new AppSettings(view.data()); + context->setContextProperty("appSettings", appSettings); + TDLibWrapper *tdLibWrapper = new TDLibWrapper(view.data()); context->setContextProperty("tdLibWrapper", tdLibWrapper); qmlRegisterType("WerkWolf.Fernschreiber", 1, 0, "TelegramAPI"); diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 89dc8c5..e77cae7 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -25,7 +25,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -43,7 +44,7 @@ namespace { const QString _TYPE("@type"); } -TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent), settings("harbour-fernschreiber", "settings") +TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent) { LOG("Initializing TD Lib..."); 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() { return this->dbusInterface->getDBusAdaptor(); diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index a6665b7..2e4405c 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -20,11 +20,6 @@ #define TDLIBWRAPPER_H #include -#include -#include -#include -#include -#include #include #include "tdlibreceiver.h" #include "dbusadaptor.h" @@ -104,8 +99,6 @@ public: Q_INVOKABLE void copyFileToDownloads(const QString &filePath); Q_INVOKABLE void openFileOnDevice(const QString &filePath); Q_INVOKABLE void controlScreenSaver(const bool &enabled); - Q_INVOKABLE void setSendByEnter(const bool &sendByEnter); - Q_INVOKABLE bool getSendByEnter(); DBusAdaptor *getDBusAdaptor(); @@ -231,7 +224,6 @@ private: QVariantMap unreadChatInformation; QHash basicGroups; QHash superGroups; - QSettings settings; }; #endif // TDLIBWRAPPER_H