new c++ settings, which are much nicer to handle :) regards #31

This commit is contained in:
Hauke Schade 2013-02-09 15:13:05 +01:00
parent 7383b3b5aa
commit 1a12b63e4e
7 changed files with 167 additions and 68 deletions

View file

@ -4,11 +4,15 @@
#include <QtDeclarative/QDeclarativeContext>
#include "qmlapplicationviewer.h"
#include "settings.hh"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
app->setApplicationVersion(APP_VERSION);
app->setApplicationName("ttrss");
app->setOrganizationName("ttrss");
QString locale = QLocale::system().name();
QTranslator translator;
@ -27,6 +31,8 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
viewer.rootContext()->setContextProperty("APP_VERSION", APP_VERSION);
viewer.rootContext()->setContextProperty("settings", Settings::instance());
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/ttrss/main.qml"));
viewer.showExpanded();

View file

@ -90,10 +90,9 @@ Page {
text: qsTr("Login")
anchors.right: menuButton.left
onClicked: {
var settings = rootWindow.settingsObject();
settings.set("server", server.text);
settings.set("username", username.text);
settings.set("password", password.text);
settings.servername = server.text
settings.username = username.text
settings.password = password.text
startLogin();
}
@ -137,11 +136,9 @@ Page {
}
function loginSuccessfull(retcode, text) {
var settings = rootWindow.settingsObject();
if(retcode) {
//login failed....don't autlogin
settings.set("dologin", "false");
settings.autologin = false
//stop the loading anim
loading = false;
@ -152,7 +149,7 @@ Page {
}
else {
//Login succeeded, auto login next Time
settings.set("dologin", "true");
settings.autologin = true
rootWindow.getTTRSS().updateConfig(configSuccessfull);
}
}
@ -179,14 +176,11 @@ Page {
}
Component.onCompleted: {
var settings = rootWindow.settingsObject();
settings.initialize();
server.text = settings.get("server", "http://");
username.text = settings.get("username", "");
password.text = settings.get("password", "");
var dologin = settings.get("dologin", "false");
server.text = settings.servername
username.text = settings.username
password.text = settings.password
if(dologin === "true")
if(settings.autologin)
startLogin();
}
}

View file

@ -11,7 +11,6 @@
import QtQuick 1.1
import com.nokia.meego 1.0
import "settings.js" as Settings
import "tinytinyrss.js" as TTRss
PageStackWindow {
@ -27,8 +26,6 @@ PageStackWindow {
function getTTRSS() {
return TTRss;
}
function settingsObject() {
return Settings;
}
initialPage: mainPage

View file

@ -1,49 +0,0 @@
//Based on the example code at:
//http://www.developer.nokia.com/Community/Wiki/How-to_create_a_persistent_settings_database_in_Qt_Quick_%28QML%29
function getDatabase() {
return openDatabaseSync("TTRss", "1.0", "Saved state for TTRss", 1000);
}
// At the start of the application, we can initialize the tables we need if they haven't been created yet
function initialize() {
var db = getDatabase();
db.transaction(
function(tx) {
// Create the settings table if it doesn't already exist
// If the table exists, this is skipped
tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
});
}
// This function is used to write a setting into the database
function set(setting, value) {
// setting: string representing the setting name (eg: “username”)
// value: string representing the value of the setting (eg: “myUsername”)
var db = getDatabase();
var success = false;
db.transaction(function(tx) {
var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
//console.log(rs.rowsAffected)
if (rs.rowsAffected > 0) {
success = true;
}
});
// The function returns true if it was successful, or false if it wasn't
return success;
}
// This function is used to retrieve a setting from the database
function get(setting, defaultValue) {
var db = getDatabase();
var result = defaultValue;
db.transaction(function(tx) {
var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
if (rs.rows.length > 0) {
result = rs.rows.item(0).value;
}});
// The function returns defaultValue if no setting is found
return result;
}

72
settings.cpp Normal file
View file

@ -0,0 +1,72 @@
//Copyright Hauke Schade, 2012-2013
//
//This file is part of TTRss.
//
//TTRss 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 2 of the License, or (at your option) any later version.
//TTRss 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 TTRss (on a Maemo/Meego system there is a copy
//in /usr/share/common-licenses. If not, see http://www.gnu.org/licenses/.
#include "settings.hh"
#include <QtCore/QSettings>
QScopedPointer<Settings> Settings::m_instance(0);
Settings *Settings::instance() {
if (m_instance.isNull())
m_instance.reset(new Settings);
return m_instance.data();
}
void Settings::setServername(QString servername) {
if (_servername != servername) {
_servername = servername;
m_settings->setValue("servername", _servername);
emit servernameChanged();
}
}
void Settings::setUsername(QString username) {
if (_username != username) {
_username = username;
m_settings->setValue("username", _username);
emit usernameChanged();
}
}
void Settings::setPassword(QString password) {
if (_password != password) {
_password = password;
m_settings->setValue("password", _password);
emit passwordChanged();
}
}
void Settings::setAutologin(bool autologin) {
if (_autologin != autologin) {
_autologin = autologin;
m_settings->setValue("autologin", _autologin);
emit autologinChanged();
}
}
void Settings::setWhiteTheme(bool whiteTheme) {
if (_whiteTheme != whiteTheme) {
_whiteTheme = whiteTheme;
m_settings->setValue("whiteTheme", _whiteTheme);
emit whiteThemeChanged();
}
}
Settings::Settings(QObject *parent) : QObject(parent), m_settings(new QSettings(this)) {
_servername = m_settings->value("servername", "http://").toString();
_username = m_settings->value("username", "").toString();
_password = m_settings->value("password", "").toString();
_autologin = m_settings->value("autologin", false).toBool();
_whiteTheme = m_settings->value("whiteTheme", true).toBool();
}

75
settings.hh Normal file
View file

@ -0,0 +1,75 @@
#ifndef SETTINGS_HH
#define SETTINGS_HH
#include <QtCore/QObject>
#include <QtCore/QScopedPointer>
#include <QtCore/qstring.h>
class QSettings;
class Settings : public QObject
{
Q_OBJECT
Q_PROPERTY(QString servername READ servername WRITE setServername NOTIFY servernameChanged)
Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged)
Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
Q_PROPERTY(bool autologin READ hasAutologin WRITE setAutologin NOTIFY autologinChanged)
Q_PROPERTY(bool whiteTheme READ isWhiteTheme WRITE setWhiteTheme NOTIFY whiteThemeChanged)
public:
static Settings *instance();
QString servername() const {
QString s("returning ");
s += this->_servername;
s += " for servername req";
qDebug(s.toStdString().c_str());
return this->_servername;
}
void setServername(QString servername);
QString username() const {
return this->_username;
}
void setUsername(QString username);
QString password() const {
return this->_password;
}
void setPassword(QString password);
bool hasAutologin() const {
return this->_autologin;
}
void setAutologin(bool autologin);
bool isWhiteTheme() const {
return this->_whiteTheme;
}
void setWhiteTheme(bool whiteTheme);
signals:
void servernameChanged();
void usernameChanged();
void passwordChanged();
void autologinChanged();
void whiteThemeChanged();
private:
static QScopedPointer<Settings> m_instance;
explicit Settings(QObject *parent = 0);
Q_DISABLE_COPY(Settings)
QSettings *m_settings;
QString _servername;
QString _username;
QString _password;
bool _autologin;
bool _whiteTheme;
};
#endif // SETTINGS_HH

View file

@ -33,7 +33,8 @@ CONFIG += qdeclarative-boostable
# CONFIG += qt-components
# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp
SOURCES += main.cpp \
settings.cpp
# Please do not modify the following two lines. Required for deployment.
include(qmlapplicationviewer/qmlapplicationviewer.pri)
@ -41,3 +42,6 @@ qtcAddDeployment()
RESOURCES += \
harmattan.qrc
HEADERS += \
settings.hh