From b61abed083e52468d05036cf53eb9a1b49028f7e Mon Sep 17 00:00:00 2001 From: Maciej Janiszewski Date: Sat, 14 Dec 2013 10:32:51 +0100 Subject: [PATCH] Code cleanup Everybody loves code cleanups! CLEANUPS CLEANUPS CLEANUPS CLEANUPS. And headers. Headers are so awesome. OMG. Replaced MySettings.cpp and SettingsDBWrapper with one class which does exactly the same but in more... logical way. Removed stuff from MessageWrapper which shouldn't be there, I think I could remove it as soon as I find a way to do exactly the same in JavaScript. Merged AccountsListModel.h and AccountsListModel.cpp cause who needs cpp file if you can do all this in header? --- Lightbulb.pro | 11 +- qml/Dialogs/Chats.qml | 5 +- qml/main.qml | 21 +-- src/accountsitemmodel.cpp | 58 +++--- src/accountsitemmodel.h | 36 ++-- src/accountslistmodel.cpp | 23 --- src/accountslistmodel.h | 41 ++++- src/database/DatabaseManager.cpp | 172 +++++++----------- src/database/DatabaseWorker.cpp | 13 +- src/database/Settings.cpp | 166 +++++++++++++++++ src/database/Settings.h | 82 +++++++++ src/database/SettingsDBWrapper.cpp | 239 ------------------------ src/database/SettingsDBWrapper.h | 2 +- src/database/databasemanager.h | 24 +++ src/database/mysettings.cpp | 283 ----------------------------- src/database/mysettings.h | 81 --------- src/listmodel.cpp | 25 +++ src/listmodel.h | 29 ++- src/main.cpp | 10 +- src/xmpp/MyXmppClient.cpp | 102 ++++------- src/xmpp/MyXmppClient.h | 75 +------- src/xmpp/messagewrapper.cpp | 35 +++- src/xmpp/messagewrapper.h | 41 +++-- 23 files changed, 592 insertions(+), 982 deletions(-) delete mode 100644 src/accountslistmodel.cpp create mode 100644 src/database/Settings.cpp create mode 100644 src/database/Settings.h delete mode 100644 src/database/SettingsDBWrapper.cpp delete mode 100644 src/database/mysettings.cpp delete mode 100644 src/database/mysettings.h diff --git a/Lightbulb.pro b/Lightbulb.pro index e1d9969..2bd69a3 100644 --- a/Lightbulb.pro +++ b/Lightbulb.pro @@ -81,23 +81,20 @@ SOURCES += src/main.cpp \ src/ListModel.cpp \ src/cache/MyCache.cpp \ src/cache/StoreVCard.cpp \ - src/database/MySettings.cpp \ src/xmpp/MessageWrapper.cpp \ src/AccountsItemModel.cpp \ - src/AccountsListModel.cpp \ src/cache/QMLVCard.cpp \ src/avkon/LightbulbHSWidget.cpp \ src/database/DatabaseManager.cpp \ src/avkon/QAvkonHelper.cpp \ - src/database/SettingsDBWrapper.cpp \ src/xmpp/XmppConnectivity.cpp \ - src/database/DatabaseWorker.cpp + src/database/DatabaseWorker.cpp \ + src/database/Settings.cpp HEADERS += src/xmpp/MyXmppClient.h \ src/ListModel.h \ src/cache/MyCache.h \ src/cache/StoreVCard.h \ - src/database/MySettings.h \ src/xmpp/MessageWrapper.h \ src/AccountsItemModel.h \ src/AccountsListModel.h \ @@ -106,10 +103,10 @@ HEADERS += src/xmpp/MyXmppClient.h \ src/avkon/LightbulbHSWidget.h \ src/database/DatabaseManager.h \ src/avkon/QAvkonHelper.h \ - src/database/SettingsDBWrapper.h \ src/avkon/SymbiosisAPIClient.h \ src/xmpp/XmppConnectivity.h \ - src/database/DatabaseWorker.h + src/database/DatabaseWorker.h \ + src/database/Settings.h OTHER_FILES += README.md \ qml/Dialogs/AddContact.qml \ diff --git a/qml/Dialogs/Chats.qml b/qml/Dialogs/Chats.qml index 2e43ad3..b024013 100644 --- a/qml/Dialogs/Chats.qml +++ b/qml/Dialogs/Chats.qml @@ -56,12 +56,11 @@ CommonDialog { } //imgPresence Text { id: txtJid - property string contact: xmppClient.getPropertyByJid(jid,"name") property int unreadMsg: parseInt(xmppClient.getPropertyByJid(jid,"unreadMsg")) anchors { left: imgPresence.right; right: imgPresenceR.left; leftMargin: 10; rightMargin: 10; verticalCenter: parent.verticalCenter } width: parent.width maximumLineCount: (rosterItemHeight/22) > 1 ? (rosterItemHeight/22) : 1 - text: (contact === "" ? jid : contact) + (unreadMsg > 0 ? " [" + unreadMsg + "]" : "") + text: (name === "" ? jid : name) + (unreadMsg > 0 ? " [" + unreadMsg + "]" : "") onLinkActivated: { main.url=link; linkContextMenu.open()} wrapMode: Text.Wrap font.pixelSize: 16 @@ -75,7 +74,7 @@ CommonDialog { onClicked: { listViewChats.currentIndex = index xmppClient.chatJid = jid - xmppClient.contactName = txtJid.contact + xmppClient.contactName = name main.globalUnreadCount = main.globalUnreadCount - txtJid.unreadMsg xmppClient.resetUnreadMessages( jid ) if (settings.gBool("behavior","enableHsWidget")) { diff --git a/qml/main.qml b/qml/main.qml index 890ffb4..fd5d2af 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -157,7 +157,6 @@ PageStackWindow { initAccount() checkIfFirstRun() xmppClient.keepAlive = settings.gInt("behavior", "keepAliveInterval") - xmppClient.archiveIncMessage = settings.gBool("behavior", "archiveIncMessage") if (settings.gBool("behavior","goOnlineOnStart")) { xmppClient.setMyPresence( XmppClient.Online, lastStatus ) } } @@ -185,23 +184,19 @@ PageStackWindow { _existDefaultAccount = false for( var j=0; j. + +*********************************************************************/ + #include "accountsitemmodel.h" AccountsItemModel::AccountsItemModel( const QString &_accountJid, const QString &_accountPasswd, - const QString &_accountIcon, - const QString &_accountType, const QString &_accountResource, const QString &_accountHost, const int _accountPort, @@ -14,8 +36,6 @@ AccountsItemModel::AccountsItemModel( const QString &_accountJid, ListItem(parent), m_jid(_accountJid), m_passwd(_accountPasswd), - m_icon(_accountIcon), - m_type(_accountType), m_resource(_accountResource), m_host(_accountHost), m_port(_accountPort), @@ -40,14 +60,6 @@ void AccountsItemModel::setPasswd(QString &_accountPasswd) } } -void AccountsItemModel::setIcon(QString &_accountIcon) -{ - if(m_icon != _accountIcon) { - m_icon = _accountIcon; - emit dataChanged(); - } -} - void AccountsItemModel::setDefault(bool &_accountDefault) { if(m_default != _accountDefault) { @@ -56,22 +68,6 @@ void AccountsItemModel::setDefault(bool &_accountDefault) } } -void AccountsItemModel::setType(QString &_accountType) -{ - if(m_type!= _accountType) { - m_type = _accountType; - emit dataChanged(); - } -} - -void AccountsItemModel::setResource(QString &_accountResource) -{ - if(m_type!= _accountResource) { - m_type = _accountResource; - emit dataChanged(); - } -} - void AccountsItemModel::setHost(QString &_accountHost) { if(m_host!= _accountHost) { @@ -104,9 +100,7 @@ QHash AccountsItemModel::roleNames() const QHash names; names[accJid] = "accJid"; names[accPasswd] = "accPasswd"; - names[accIcon] = "accIcon"; names[accDefault] = "accDefault"; - names[accType] = "accType"; names[accResource] = "accResource"; names[accHost] = "accHost"; names[accPort] = "accPort"; @@ -121,12 +115,8 @@ QVariant AccountsItemModel::data(int role) const return jid(); case accPasswd: return passwd(); - case accIcon: - return icon(); case accDefault: return isDefault(); - case accType: - return type(); case accResource: return resource(); case accHost: diff --git a/src/accountsitemmodel.h b/src/accountsitemmodel.h index fddc04a..d5a99f3 100644 --- a/src/accountsitemmodel.h +++ b/src/accountsitemmodel.h @@ -1,3 +1,27 @@ +/******************************************************************** + +src/AccountsItemModel.h +-- implements item model for account + +Copyright (c) 2012 Anatoliy Kozlov + +This file is part of Lightbulb and was derived from MeegIM. + +Lightbulb 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. + +This program 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 this program. If not, see . + +*********************************************************************/ + #ifndef ACCOUNTSITEMMODEL_H #define ACCOUNTSITEMMODEL_H @@ -11,9 +35,7 @@ class AccountsItemModel : public ListItem enum Roles { accJid = Qt::UserRole+1, accPasswd, - accIcon, accDefault, - accType, accResource, accHost, accPort, @@ -24,8 +46,6 @@ class AccountsItemModel : public ListItem AccountsItemModel(QObject *parent = 0): ListItem(parent) {} explicit AccountsItemModel( const QString &_accountJid, const QString &_accountPasswd, - const QString &_accountIcon, - const QString &_accountType, const QString &_accountResource, const QString &_accountHost, const int _accountPort, @@ -40,19 +60,15 @@ class AccountsItemModel : public ListItem void setJid( QString &_accountJid ); void setPasswd( QString &_accountPasswd ); - void setIcon( QString &_accountIcon ); + void setDefault( bool &_accountDefault ); - void setType( QString &_accountType ); - void setResource( QString &_accountResource ); void setHost( QString &_accountHost ); void setPort( int _accountPort ); void setManuallyHostPort( bool _manuallyHostPort ); inline QString jid() const { return m_jid; } inline QString passwd() const { return m_passwd; } - inline QString icon() const { return m_icon; } inline bool isDefault() const { return m_default; } - inline QString type() const { return m_type; } inline QString resource() const { return m_resource; } inline QString host() const { return m_host; } inline int port() const { return m_port; } @@ -62,8 +78,6 @@ class AccountsItemModel : public ListItem private: QString m_jid; QString m_passwd; - QString m_icon; - QString m_type; QString m_resource; QString m_host; int m_port; diff --git a/src/accountslistmodel.cpp b/src/accountslistmodel.cpp deleted file mode 100644 index 47d6cfc..0000000 --- a/src/accountslistmodel.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "accountslistmodel.h" - -AccountsListModel::AccountsListModel( QObject *parent ) :ListModel( new AccountsItemModel, parent ) -{ -} - - -void AccountsListModel::append( AccountsItemModel *item ) { - this->appendRow( item ); -} - -void AccountsListModel::remove( int index ) { - this->removeRow( index ); -} - -int AccountsListModel::count() { - return this->rowCount(); -} - -void AccountsListModel::clearList() -{ - this->clear(); -} diff --git a/src/accountslistmodel.h b/src/accountslistmodel.h index dcf7eea..978eb43 100644 --- a/src/accountslistmodel.h +++ b/src/accountslistmodel.h @@ -1,19 +1,46 @@ +/******************************************************************** + +src/AccountsListModel.h +-- implements list model for accounts + +Copyright (c) 2012 Anatoliy Kozlov, + Maciej Janiszewski + +This file is part of Lightbulb and was derived from MeegIM. + +Lightbulb 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. + +This program 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 this program. If not, see . + +*********************************************************************/ + #ifndef ACCOUNTSLISTMODEL_H #define ACCOUNTSLISTMODEL_H -#include "listmodel.h" -#include "accountsitemmodel.h" +#include "ListModel.h" +#include "AccountsItemModel.h" class AccountsListModel : public ListModel { Q_OBJECT public: - explicit AccountsListModel( QObject *parent = 0 ); + AccountsListModel( QObject *parent ) :ListModel( new AccountsItemModel, parent ) + { + } - Q_INVOKABLE void append( AccountsItemModel *item ); - Q_INVOKABLE void remove( int index ); - Q_INVOKABLE int count(); - Q_INVOKABLE void clearList(); + Q_INVOKABLE void append( AccountsItemModel *item ) { this->appendRow(item); } + Q_INVOKABLE void remove( int index ) { this->removeRow( index ); } + Q_INVOKABLE int count() { return this->rowCount(); } + Q_INVOKABLE void clearList() { this->clear(); } }; #endif // ACCOUNTSLISTMODEL_H diff --git a/src/database/DatabaseManager.cpp b/src/database/DatabaseManager.cpp index cf503be..6ae560b 100644 --- a/src/database/DatabaseManager.cpp +++ b/src/database/DatabaseManager.cpp @@ -1,71 +1,38 @@ -#include "DatabaseManager.h" -#include -#include -#include -#include -#include +/******************************************************************** +src/database/DatabaseManager.cpp +-- accesses and manages the SQLite database. -//-------------------------------- -// SQL QUERY MODEL -// -// DOES SOME FUN STUFF TO DISPLAY STUFF IN QML -//-------------------------------- +Copyright (c) 2013 Maciej Janiszewski -SqlQueryModel::SqlQueryModel(QObject *parent) : - QSqlQueryModel(parent) -{ - -} +This file is part of Lightbulb. -void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db) -{ - if (db.isOpen()) - QSqlQueryModel::setQuery(query,db); - generateRoleNames(); -} +Lightbulb 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. -void SqlQueryModel::setQuery(const QSqlQuery & query) -{ - QSqlQueryModel::setQuery(query); - generateRoleNames(); -} +This program 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. -void SqlQueryModel::generateRoleNames() -{ - QHash roleNames; - for( int i = 0; i < record().count(); i++) { - roleNames[Qt::UserRole + i + 1] = record().fieldName(i).toAscii(); - } - setRoleNames(roleNames); -} +You should have received a copy of the GNU General Public License +along with this program. If not, see . -QVariant SqlQueryModel::data(const QModelIndex &index, int role) const -{ - QVariant value = QSqlQueryModel::data(index, role); - if(role < Qt::UserRole) - { - value = QSqlQueryModel::data(index, role); - } - else - { - int columnIdx = role - Qt::UserRole - 1; - QModelIndex modelIndex = this->index(index.row(), columnIdx); - value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole); - } - return value; -} +*********************************************************************/ -//-------------------------------- -// DATABASE -// MANAGER -// -// APPENDS DATA TO TEH DATABSE AND DOES OTHER USEFUL STUFF -//-------------------------------- +#include "DatabaseManager.h" +#include +#include +#include +#include +#include DatabaseManager::DatabaseManager(QObject *parent) : QObject(parent) { + // threaded way to initialize the database if ( !QSqlDatabase::contains("Database")) { db = QSqlDatabase::addDatabase("QSQLITE","Database"); db.setDatabaseName("com.lightbulb.db"); @@ -74,18 +41,18 @@ DatabaseManager::DatabaseManager(QObject *parent) : db.setDatabaseName("com.lightbulb.db"); } - if ( !db.isOpen() ) - { + if ( !db.isOpen() ) { if (!db.open()) { - qWarning() << "Unable to connect to database, giving up:" << db.lastError().text(); databaseOpen = false; return; } } databaseOpen = true; + + // set up some pragma parameters to get this thing working faster QSqlQuery("PRAGMA journal_mode = OFF",db); QSqlQuery("PRAGMA page_size = 16384",db); - QSqlQuery("PRAGMA cache_size = 16384",db); + QSqlQuery("PRAGMA cache_size = 163840",db); QSqlQuery("PRAGMA temp_store = MEMORY",db); QSqlQuery("PRAGMA locking_mode = EXCLUSIVE",db); connect(this,SIGNAL(finished()), this, SLOT(getLastError())); @@ -94,17 +61,11 @@ DatabaseManager::DatabaseManager(QObject *parent) : DatabaseManager::~DatabaseManager() { } -QSqlError DatabaseManager::lastError() -{ - return db.lastError(); -} +QSqlError DatabaseManager::lastError() { return db.lastError(); } -void DatabaseManager::getLastError() { - qDebug () << this->lastError(); -} +void DatabaseManager::getLastError() { if (this->lastError().text() != " ") qDebug () << this->lastError(); } -bool DatabaseManager::deleteDB() -{ +bool DatabaseManager::deleteDB() { // Close database db.close(); @@ -112,36 +73,14 @@ bool DatabaseManager::deleteDB() return QFile::remove("com.lightbulb.db"); } -bool DatabaseManager::initDB() -{ - mkAccTable(); +bool DatabaseManager::initDB() { mkRosterTable(); mkMessagesTable(); emit finished(); - return true; } -bool DatabaseManager::mkAccTable() -{ - bool ret = false; - if (db.isOpen()) { - QSqlQuery query(db); - ret = query.exec("create table accounts " - "(id integer primary key, " - "jid varchar(50), " - "pass varchar(30), " - "resource varchar(30), " - "manualHostPort integer, " - "enabled integer, " - "host varchar(50), " - "port integer)"); - } - emit finished(); - return ret; -} - bool DatabaseManager::mkRosterTable() { bool ret = false; @@ -162,26 +101,17 @@ bool DatabaseManager::mkRosterTable() return ret; } -bool DatabaseManager::setChatInProgress() -{ +bool DatabaseManager::setChatInProgress() { QStringList params = parameters; bool ret = false; QSqlQuery query(db); - QString queryStr; - queryStr = "UPDATE roster SET isChatInProgress='"; - queryStr += params.at(2); - queryStr += "' where jid='"; - queryStr += params.at(1); - queryStr += "' and id_account="; - queryStr += params.at(0); - ret = query.exec(queryStr); + ret = query.exec("UPDATE roster SET isChatInProgress='" + params.at(2) + "' where jid='" + params.at(1) + "' and id_account=" + params.at(0)); emit finished(); emit chatsChanged(); return ret; } -bool DatabaseManager::mkMessagesTable() -{ +bool DatabaseManager::mkMessagesTable() { bool ret = false; if (db.isOpen()) { QSqlQuery query(db); @@ -347,8 +277,6 @@ bool DatabaseManager::incUnreadMessage() } emit finished(); emit rosterChanged(); - qDebug() << query.lastError(); - qDebug() << query.lastQuery(); return ret; } @@ -365,3 +293,35 @@ int DatabaseManager::getUnreadCount(int acc) emit finished(); return count; } + +/*******************************************************************************/ + +SqlQueryModel::SqlQueryModel(QObject *parent) : + QSqlQueryModel(parent) { } + +void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db) { + if (db.isOpen()) QSqlQueryModel::setQuery(query,db); + generateRoleNames(); +} + +void SqlQueryModel::setQuery(const QSqlQuery & query) { + QSqlQueryModel::setQuery(query); + generateRoleNames(); +} + +void SqlQueryModel::generateRoleNames() { + QHash roleNames; + for( int i = 0; i < record().count(); i++) roleNames[Qt::UserRole + i + 1] = record().fieldName(i).toAscii(); + setRoleNames(roleNames); +} + +QVariant SqlQueryModel::data(const QModelIndex &index, int role) const { + QVariant value = QSqlQueryModel::data(index, role); + if(role < Qt::UserRole) value = QSqlQueryModel::data(index, role); + else { + int columnIdx = role - Qt::UserRole - 1; + QModelIndex modelIndex = this->index(index.row(), columnIdx); + value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole); + } + return value; +} diff --git a/src/database/DatabaseWorker.cpp b/src/database/DatabaseWorker.cpp index dba12cf..4d6d5d4 100644 --- a/src/database/DatabaseWorker.cpp +++ b/src/database/DatabaseWorker.cpp @@ -53,11 +53,14 @@ DatabaseWorker::DatabaseWorker(QObject *parent) : } void DatabaseWorker::executeQuery(QStringList& query) { - // pass the parameters to DatabaseManager + // Pass the parameters to DatabaseManager database->parameters.clear(); for (int j=1;jparameters.append(query.at(j)); - // check the type of query and execute + // Used for debugging. I like debugging. Debugging is nice. + qDebug() << "DatabaseWorker::executeQuery(): executing query with parameters: " << database->parameters; + + // Check the type of query and execute switch (queryType.indexOf(query.at(0))) { case 0: qDebug() << "DatabaseWorker::executeQuery(): beginning transaction"; @@ -81,13 +84,11 @@ void DatabaseWorker::executeQuery(QStringList& query) { } } -void DatabaseWorker::chatsMustBeUpdated() { - this->updateChats(accountId); -} +void DatabaseWorker::chatsMustBeUpdated() { this->updateChats(accountId); } //updates chat list void DatabaseWorker::updateChats(int m_accountId) { qDebug() << "DatabaseWorker::updateChats(): updating chats list."; - sqlChats->setQuery("select jid from roster where isChatInProgress=1 and id_account=" + QString::number(m_accountId),database->db); + sqlChats->setQuery("select jid, name from roster where isChatInProgress=1 and id_account=" + QString::number(m_accountId),database->db); emit sqlChatsUpdated(); } diff --git a/src/database/Settings.cpp b/src/database/Settings.cpp new file mode 100644 index 0000000..3bcd0d9 --- /dev/null +++ b/src/database/Settings.cpp @@ -0,0 +1,166 @@ +/******************************************************************** + +src/database/Settings.cpp +-- holds settings of the app and accounts details + +Copyright (c) 2013 Maciej Janiszewski + +This file is part of Lightbulb. + +Lightbulb 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. + +This program 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 this program. If not, see . + +*********************************************************************/ + +#include "Settings.h" + +#include +#include "AccountsListModel.h" + +QString Settings::appName = "Lightbulb"; +QString Settings::confFolder = QDir::homePath() + QDir::separator() + ".config" + QDir::separator() + appName; +QString Settings::cacheFolder = confFolder + QDir::separator() + QString("cache"); +QString Settings::confFile = confFolder + QDir::separator() + Settings::appName + ".conf"; + +Settings::Settings(QObject *parent) : QSettings(Settings::confFile, QSettings::NativeFormat , parent) +{ + jid_indx0 = ""; + pass_indx0 = ""; + dflt_indx0 = ""; + + alm = new AccountsListModel(this); + this->initListOfAccounts(); +} + +/*************************** (generic settings) **************************/ +bool Settings::gBool(QString group, QString key) { + beginGroup( group ); + QVariant ret = value( key, false ); + endGroup(); + return ret.toBool(); +} +void Settings::sBool(const bool isSet, QString group, QString key) { + beginGroup( group ); + setValue( key, QVariant(isSet) ); + endGroup(); +} +int Settings::gInt(QString group, QString key) { + beginGroup( group ); + QVariant ret = value( key, false ); + endGroup(); + return ret.toInt(); +} +void Settings::sInt(const int isSet, QString group, QString key) { + beginGroup( group ); + setValue( key, QVariant(isSet) ); + endGroup(); +} +QString Settings::gStr(QString group, QString key) { + beginGroup( group ); + QVariant ret = value( key, false ); + endGroup(); + return ret.toString(); +} +void Settings::sStr(const QString isSet, QString group, QString key) { + beginGroup( group ); + setValue( key, QVariant(isSet) ); + endGroup(); +} + +/******** ACCOUNT RELATED SHIT *******/ +QStringList Settings::getListAccounts() +{ + beginGroup( "accounts" ); + QVariant ret = value( "accounts", QStringList() ); + endGroup(); + return ret.toStringList(); +} +/*-------------------*/ +void Settings::addAccount( const QString &acc ) +{ + beginGroup( "accounts" ); + QVariant retList = value( "accounts", QStringList() ); + QStringList sl = retList.toStringList(); + if( sl.indexOf(acc) < 0 ) { + sl.append(acc); + setValue( "accounts", QVariant(sl) ); + } + endGroup(); +} +void Settings::removeAccount( const QString &acc ) +{ + beginGroup( "accounts" ); + QVariant retList = value( "accounts", QStringList() ); + QStringList sl = retList.toStringList(); + if( sl.indexOf(acc) >= 0 ) { + sl.removeOne(acc); + setValue( "accounts", QVariant(sl) ); + } + endGroup(); +} + +void Settings::initListOfAccounts() { + QStringList listAcc = getListAccounts(); + + alm->removeRows( 0, alm->count() ); + + QStringList::const_iterator itr = listAcc.begin(); + int i = 0; + while ( itr != listAcc.end() ) { + QString jid = *itr; + itr++; + + QString passwd = gStr(jid,"passwd"); + bool isDefault = gBool(jid,"is_default"); + + QString host = gStr(jid,"host"); + int port = gInt(jid,"port"); + QString resource = gStr(jid,"resource"); + bool isManuallyHostPort = gBool(jid,"use_host_port"); + + AccountsItemModel *aim = new AccountsItemModel( jid, passwd, resource, host, port, isDefault, isManuallyHostPort, this ); + alm->append(aim); + + if(i==0) { jid_indx0 = jid; pass_indx0 = passwd; dflt_indx0 = isDefault; } + i++; + } + + emit accountsListChanged(); +} + + +void Settings::setAccount( + QString _jid, + QString _pass, + bool _isDflt, + QString _resource, + QString _host, + QString _port, + bool manuallyHostPort) //Q_INVOKABLE +{ + addAccount( _jid ); + sStr(_pass,_jid,"passwd"); + sBool(_isDflt,_jid,"is_default"); + + sStr(_resource,_jid,"resource"); + sStr(_host,_jid,"host"); + sBool(manuallyHostPort,_jid,"use_host_port"); + + bool ok = false; + int p = _port.toInt(&ok); + if( ok ) { sInt( p, _jid, "port" ); } +} + +QString Settings::getJidByIndex(int index) { + return getListAccounts().at(index); +} diff --git a/src/database/Settings.h b/src/database/Settings.h new file mode 100644 index 0000000..7aba4a0 --- /dev/null +++ b/src/database/Settings.h @@ -0,0 +1,82 @@ +/******************************************************************** + +src/database/Settings.h +-- holds settings of the app and accounts details + +Copyright (c) 2013 Maciej Janiszewski + +This file is part of Lightbulb. + +Lightbulb 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. + +This program 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 this program. If not, see . + +*********************************************************************/ + +#ifndef MYSETTINGS_H +#define MYSETTINGS_H + +#include +#include "AccountsListModel.h" + +class Settings : public QSettings +{ + Q_OBJECT + Q_DISABLE_COPY( Settings ) + + Q_PROPERTY( AccountsListModel* accounts READ getAccounts NOTIFY accountsListChanged ) + AccountsListModel *alm; + + QString jid_indx0; + QString pass_indx0; + bool dflt_indx0; + +protected: + void addAccount( const QString& acc ); + QStringList getListAccounts(); + +public: + explicit Settings(QObject *parent = 0); + + static QString appName; + static QString confFolder; + static QString cacheFolder; + static QString confFile; + + Q_INVOKABLE bool gBool(QString group, QString key); + Q_INVOKABLE void sBool(const bool isSet, QString group, QString key); + Q_INVOKABLE int gInt(QString group, QString key); + Q_INVOKABLE void sInt(const int isSet, QString group, QString key); + Q_INVOKABLE QString gStr(QString group, QString key); + Q_INVOKABLE void sStr(const QString isSet, QString group, QString key); + + Q_INVOKABLE void removeAccount( const QString& acc ); + + Q_INVOKABLE void initListOfAccounts(); + Q_INVOKABLE QString getJidByIndex( int index ); + Q_INVOKABLE void setAccount( QString _jid, QString _pass, bool isDflt, QString _resource = "", QString _host = "", QString _port = "", bool manuallyHostPort = false ); + + Q_INVOKABLE QString getJid_indx0() { return jid_indx0; } + Q_INVOKABLE QString getPass_indx0() { return pass_indx0; } + Q_INVOKABLE bool getDef_indx0() { return dflt_indx0; } + + + AccountsListModel* getAccounts() const { return alm; } + + +signals: + void accountsListChanged(); +public slots: + +}; + +#endif // MYSETTINGS_H diff --git a/src/database/SettingsDBWrapper.cpp b/src/database/SettingsDBWrapper.cpp deleted file mode 100644 index 846cd0a..0000000 --- a/src/database/SettingsDBWrapper.cpp +++ /dev/null @@ -1,239 +0,0 @@ -#include "SettingsDBWrapper.h" - -#include "MyXmppClient.h" - -SettingsDBWrapper::SettingsDBWrapper() : MySettings(0) -{ - - jid_indx0 = ""; - pass_indx0 = ""; - dflt_indx0 = ""; - - alm = new AccountsListModel( this ); - this->initListOfAccounts(); -} - -QXmppConfiguration SettingsDBWrapper::getDefaultAccount() -{ - QXmppConfiguration xmppConfig; - - //QString passwd(""); - - QStringList listAcc = getListAccounts(); - QStringList::const_iterator itr = listAcc.begin(); - while ( itr != listAcc.end() ) - { - QString jid = *itr; - itr++; - - QString passwd = getPasswd( jid ); - QString host = getHost( jid ); - int port = getPort( jid ); - QString resource = getResource( jid ); - - bool isDefault = isAccDefault( jid ); - if( isDefault ) { - //MyXmppClient::myPass = passwd; - //MyXmppClient::myJid = jid; - - xmppConfig.setJid( jid ); - xmppConfig.setPassword( passwd ); - - if( !resource.isEmpty() ) { - xmppConfig.setResource( resource ); - } - - if( !host.isEmpty() ) { - xmppConfig.setHost( host ); - } - - if( port > 0 ) { - xmppConfig.setPort( port ); - } - - return xmppConfig; - } - } - return xmppConfig; -} - - -void SettingsDBWrapper::initListOfAccounts() //Q_INVOKABLE -{ - QStringList listAcc = getListAccounts(); - - alm->removeRows( 0, alm->count() ); - - QStringList::const_iterator itr = listAcc.begin(); - int i = 0; - while ( itr != listAcc.end() ) - { - QString jid = *itr; - itr++; - - QString passwd = getPasswd( jid ); - QString icon = "qrc:/qml/images/accXMPP.png"; - - bool isDefault = isAccDefault( jid ); - - QString type = "xmpp"; - QString host = getHost( jid ); - int port = getPort( jid ); - QString resource = getResource( jid ); - bool isManuallyHostPort = isHostPortManually( jid ); - - AccountsItemModel *aim = new AccountsItemModel( jid, passwd, icon, type, resource, host, port, isDefault, isManuallyHostPort, this ); - alm->append(aim); - - if(i==0) { - jid_indx0 = jid; - pass_indx0 = passwd; - dflt_indx0 = isDefault; - } - i++; - } - - emit accountsListChanged(); -} - - -void SettingsDBWrapper::setAccount( - QString _jid, - QString _pass, - bool _isDflt, - QString _resource, - QString _host, - QString _port, - bool manuallyHostPort) //Q_INVOKABLE -{ - this->addAccount( _jid ); - this->setPasswd( _jid, _pass ); - this->setAccDefault( _jid, _isDflt ); - - this->setResource( _jid, _resource ); - this->setHost( _jid, _host ); - this->setHostPortManually( _jid, manuallyHostPort ); - - bool ok = false; - int p = _port.toInt(&ok); - if( ok ) { - this->setPort( _jid, p ); - } - - this->getDefaultAccount(); -} - - -void SettingsDBWrapper::removeAccount( QString _jid ) //Q_INVOKABLE -{ - this->remAccount( _jid ); - this->remove( _jid ); -} - - -bool SettingsDBWrapper::accIsDefault(int index) -{ - bool val = false; - if( (index>=0) and (indexcount()) ) { - AccountsItemModel *aim = reinterpret_cast( alm->value( index ) ); - val = aim->isDefault(); - } - return val; -} - -QString SettingsDBWrapper::accGetJid(int index) -{ - QString val = ""; - if( (index>=0) and (indexcount()) ) { - AccountsItemModel *aim = reinterpret_cast( alm->value( index ) ); - val = aim->jid(); - } - return val; -} - -QString SettingsDBWrapper::accGetPassword(int index) -{ - QString val = ""; - if( (index>=0) and (indexcount()) ) { - AccountsItemModel *aim = reinterpret_cast( alm->value( index ) ); - val = aim->passwd(); - } - return val; -} - -QString SettingsDBWrapper::accGetResource(int index) -{ - QString val = ""; - if( (index>=0) and (indexcount()) ) { - AccountsItemModel *aim = reinterpret_cast( alm->value( index ) ); - val = aim->resource(); - } - return val; -} - -QString SettingsDBWrapper::accGetHost(int index) -{ - QString val = ""; - if( (index>=0) and (indexcount()) ) { - AccountsItemModel *aim = reinterpret_cast( alm->value( index ) ); - val = aim->host(); - } - return val; -} - -int SettingsDBWrapper::accGetPort(int index) -{ - int val = 0; - if( (index>=0) and (indexcount()) ) { - AccountsItemModel *aim = reinterpret_cast( alm->value( index ) ); - val = aim->port(); - } - return val; -} - -bool SettingsDBWrapper::accIsManuallyHostPort(int index) -{ - bool val = false; - if( (index>=0) and (indexcount()) ) { - AccountsItemModel *aim = reinterpret_cast( alm->value( index ) ); - val = aim->isManuallyHostPort(); - } - return val; -} - -void SettingsDBWrapper::saveStatusText(QString statusText) -{ - if( statusText != this->getStatusText() ) - { - this->setStatusText( statusText ); - } -} - -bool SettingsDBWrapper::gBool(QString group, QString key) // Q_INVOKABLE -{ - return this->getBool(group,key); -} -void SettingsDBWrapper::sBool(const bool isSet, QString group, QString key) // Q_INVOKABLE -{ - this->setBool(isSet,group,key); -} - - -int SettingsDBWrapper::gInt(QString group, QString key) // Q_INVOKABLE -{ - return this->getInt(group,key); -} -void SettingsDBWrapper::sInt(const int isSet, QString group, QString key) // Q_INVOKABLE -{ - this->setInt(isSet,group,key); -} - - -QString SettingsDBWrapper::gStr(QString group, QString key) // Q_INVOKABLE -{ - return this->getString(group,key); -} -void SettingsDBWrapper::sStr(const QString isSet, QString group, QString key) // Q_INVOKABLE -{ - this->setString(isSet,group,key); -} diff --git a/src/database/SettingsDBWrapper.h b/src/database/SettingsDBWrapper.h index 5e76670..4ef2586 100644 --- a/src/database/SettingsDBWrapper.h +++ b/src/database/SettingsDBWrapper.h @@ -10,7 +10,7 @@ #include -class SettingsDBWrapper : public MySettings +class SettingsDBWrapper : public Settings { Q_OBJECT Q_DISABLE_COPY( SettingsDBWrapper ) diff --git a/src/database/databasemanager.h b/src/database/databasemanager.h index bea68d0..833f8e7 100644 --- a/src/database/databasemanager.h +++ b/src/database/databasemanager.h @@ -1,3 +1,27 @@ +/******************************************************************** + +src/database/DatabaseManager.h +-- accesses and manages the SQLite database. + +Copyright (c) 2013 Maciej Janiszewski + +This file is part of Lightbulb. + +Lightbulb 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. + +This program 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 this program. If not, see . + +*********************************************************************/ + #ifndef DATABASEMANAGER_H #define DATABASEMANAGER_H diff --git a/src/database/mysettings.cpp b/src/database/mysettings.cpp deleted file mode 100644 index 3304ede..0000000 --- a/src/database/mysettings.cpp +++ /dev/null @@ -1,283 +0,0 @@ -#include "mysettings.h" - -#include - -QString MySettings::appName = "Lightbulb"; -QString MySettings::pathMeegIMHome = QDir::homePath() + QDir::separator() + ".config" + QDir::separator() + appName; -QString MySettings::pathMeegIMCache = pathMeegIMHome + QDir::separator() + QString("cache"); -QString MySettings::fileConfig = pathMeegIMHome + QDir::separator() + MySettings::appName + ".conf"; - -MySettings::MySettings(QObject *parent) : QSettings(MySettings::fileConfig, QSettings::NativeFormat , parent) -{ - group_notifications = "notifications"; - group_gui = "ui"; - group_behavior = "behavior"; - - group_xmpp = "xmpp"; - group_accounts = "accounts"; - - key_jid = "jid"; - key_passwd = "passwd"; - key_showGroup = "show_groups"; - key_lastPresence = "show_last_presence"; - key_accounts = "accounts"; - key_resource = "resource"; - key_status_text = "status_text"; - key_status = "status"; - key_default = "is_default"; - key_useHostPort = "use_host_port"; - key_host = "host"; - key_port = "port"; - - /*************************** (new message) *************************** - key_vibraMsgRecv = "vibraMsgRecv"; - key_vibraMsgRecvDuration = "vibraMsgRecvDuration"; - key_vibraMsgRecvIntensity = "vibraMsgRecvIntensity"; - - key_soundMsgRecv = "soundMsgRecv"; - key_soundMsgRecvFile = "soundMsgRecvFile"; - key_soundMsgRecvVol = "soundMsgRecvVol"; - - key_notifyMsgRecv = "notifyMsgRecv"; - key_blinkScrOnMsgRecv = "blinkScrOnMsgRecv"; - key_useGlobalNote = "useGlobalNote"; - *************************** (message sent) *************************** - key_vibraMsgSent = "vibraMsgSent"; - key_vibraMsgSentDuration = "vibraMsgSentDuration"; - key_vibraMsgSentIntensity = "vibraMsgSentIntensity"; - - key_soundMsgSent = "soundMsgSent"; - key_soundMsgSentFile = "soundMsgSentFile"; - key_soundMsgSentVol = "soundMsgSentVol"; - ************************ (connection changed) ************************ - key_notifyConnection = "notifyConnection"; - - key_soundNotifyConn = "soundNotifyConn"; - key_soundNotifyConnFile = "soundNotifyConnFile"; - key_soundNotifyConnVol = "soundNotifyConnVol"; - *********************** (subscription request) *********************** - key_notifySubscription = "notifySubscription"; - - key_vibraMsgSub = "vibraMsgSub"; - key_vibraMsgSubDuration = "vibraMsgSubDuration"; - key_vibraMsgSubIntensity = "vibraMsgSubIntensity"; - - key_soundNotifySub = "soundNotifySub"; - key_soundNotifySubFile = "soundNotifySubFile"; - key_soundNotifySubVol = "soundNotifySubVol"; - ************************* (contact is typing) ************************ - key_notifyTyping = "notifyTyping"; - ******************************* ( UI ) ******************************* - key_hideOffline = "hideOffline"; - key_markUnread = "markUnread"; - key_showUnreadCount = "showUnreadCount"; - key_rosterItemHeight = "rosterItemHeight"; - key_showContactStatusText = "showContactStatusText"; - - key_rosterLayoutAvatar = "rosterLayoutAvatar"; //display avatar if true, only state if false - key_platformInvert = "platformInvert"; - key_splitscreenAnimation = "splitscreenAnimation"; - **************************** ( behavior ) **************************** - key_reconnectOnError = "reconnectOnError"; - key_keepAliveInterval = "keepAliveInterval"; - - key_storeStatusText = "storeStatusText"; - key_lastStatusText = "lastStatusText"; - - key_archiveIncMsg = "archiveIncMsg"; - key_enableHsWidget = "enableHsWidget"; - key_showStatusRow = "showStatusRow"; - key_showLastUpdate = "showLastUpdate"; - - - key_disableNotify = "disableNotifications"; - key_disableNotifyDuration = "disableNotificationsDuration"; - */ -} - -/*************************** (new message) **************************/ -bool MySettings::getBool(QString group, QString key) -{ - beginGroup( group ); - QVariant ret = value( key, false ); - endGroup(); - return ret.toBool(); -} -void MySettings::setBool(const bool isSet, QString group, QString key) -{ - beginGroup( group ); - setValue( key, QVariant(isSet) ); - endGroup(); -} - -int MySettings::getInt(QString group, QString key) -{ - beginGroup( group ); - QVariant ret = value( key, false ); - endGroup(); - return ret.toInt(); -} -void MySettings::setInt(const int isSet, QString group, QString key) -{ - beginGroup( group ); - setValue( key, QVariant(isSet) ); - endGroup(); -} - -QString MySettings::getString(QString group, QString key) -{ - beginGroup( group ); - QVariant ret = value( key, false ); - endGroup(); - return ret.toString(); -} -void MySettings::setString(const QString isSet, QString group, QString key) -{ - beginGroup( group ); - setValue( key, QVariant(isSet) ); - endGroup(); -} -/******** ACCOUNT RELATED SHIT *******/ -QStringList MySettings::getListAccounts() -{ - beginGroup( group_accounts ); - QVariant ret = value( key_accounts, QStringList() ); - endGroup(); - return ret.toStringList(); -} -/*-------------------*/ -void MySettings::addAccount( const QString &acc ) -{ - beginGroup( group_accounts ); - QVariant retList = value( key_accounts, QStringList() ); - QStringList sl = retList.toStringList(); - if( sl.indexOf(acc) < 0 ) { - sl.append(acc); - setValue( key_accounts, QVariant(sl) ); - } - endGroup(); -} -void MySettings::remAccount( const QString &acc ) -{ - beginGroup( group_accounts ); - QVariant retList = value( key_accounts, QStringList() ); - QStringList sl = retList.toStringList(); - if( sl.indexOf(acc) >= 0 ) { - sl.removeOne(acc); - setValue( key_accounts, QVariant(sl) ); - } - endGroup(); -} -/*-------------------*/ -QString MySettings::getPasswd(const QString &jid) -{ - beginGroup( jid ); - QVariant ret = value( key_passwd, QString("") ); - endGroup(); - return ret.toString(); -} -void MySettings::setPasswd(const QString &jid, const QString &passwd) -{ - beginGroup( jid ); - setValue( key_passwd, QVariant(passwd) ); - endGroup(); -} -/*-----------*/ -QString MySettings::getResource(const QString &jid) -{ - beginGroup( jid ); - QVariant ret = value( key_resource, QString("") ); - endGroup(); - return ret.toString(); -} -void MySettings::setResource(const QString &jid, const QString &resource) -{ - beginGroup( jid ); - setValue( key_resource, QVariant(resource) ); - endGroup(); -} -/*-----------*/ -bool MySettings::isAccDefault(const QString &jid) -{ - beginGroup( jid ); - QVariant ret = value( key_default, false ); - endGroup(); - return ret.toBool(); -} -void MySettings::setAccDefault(const QString &jid, const bool &def) -{ - beginGroup( jid ); - setValue( key_default, QVariant(def) ); - endGroup(); -} -/*-----------*/ -bool MySettings::isHostPortManually(const QString &jid) -{ - beginGroup( jid ); - QVariant ret = value( key_useHostPort, false ); - endGroup(); - return ret.toBool(); -} -void MySettings::setHostPortManually(const QString &jid, const bool &def) -{ - beginGroup( jid ); - setValue( key_useHostPort, QVariant(def) ); - endGroup(); -} -/*-----------*/ -QString MySettings::getHost(const QString &jid) -{ - beginGroup( jid ); - QVariant ret = value( key_host, QString("") ); - endGroup(); - return ret.toString(); -} -void MySettings::setHost(const QString &jid, const QString &host) -{ - beginGroup( jid ); - setValue( key_host, QVariant(host) ); - endGroup(); -} -/*-----------*/ -int MySettings::getPort(const QString &jid) -{ - beginGroup( jid ); - QVariant ret = value( key_port, 0 ); - endGroup(); - return ret.toInt(); -} -void MySettings::setPort(const QString &jid, const int port) -{ - beginGroup( jid ); - setValue( key_port, QVariant(port) ); - endGroup(); -} -/*-----------*/ -QString MySettings::getStatus() -{ - beginGroup( group_behavior ); - QVariant ret = value( key_status, QString("") ); - endGroup(); - return ret.toString(); -} -void MySettings::setStatus( const QString &status) -{ - beginGroup( group_behavior ); - setValue( key_status, QVariant(status) ); - endGroup(); -} -/*-----------*/ -QString MySettings::getStatusText() -{ - beginGroup( group_behavior ); - QVariant ret = value( key_status_text, QString("") ); - endGroup(); - return ret.toString(); -} -void MySettings::setStatusText( const QString &status_text) -{ - beginGroup( group_behavior ); - setValue( key_status_text, QVariant(status_text) ); - endGroup(); -} - diff --git a/src/database/mysettings.h b/src/database/mysettings.h deleted file mode 100644 index 2988039..0000000 --- a/src/database/mysettings.h +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef MYSETTINGS_H -#define MYSETTINGS_H - -#include - -class MySettings : public QSettings -{ - Q_OBJECT - - QString group_notifications; - QString group_gui; - QString group_behavior; - QString group_xmpp; - QString group_accounts; - - QString key_jid; - QString key_passwd; - QString key_showGroup; - QString key_lastPresence; - QString key_accounts; - QString key_resource; - QString key_status_text; - QString key_status; - QString key_default; - QString key_host; - QString key_port; - QString key_useHostPort; - -protected: - void addAccount( const QString& acc ); - QStringList getListAccounts(); - void remAccount( const QString& acc ); - -public: - explicit MySettings(QObject *parent = 0); - - static QString appName; - static QString pathMeegIMHome; - static QString pathMeegIMCache; - - static QString fileConfig; - - QString getPasswd( const QString &jid ); - void setPasswd( const QString &jid, const QString& passwd ); - - QString getResource( const QString &jid ); - void setResource( const QString &jid, const QString& resource ); - - QString getHost( const QString &jid ); - void setHost( const QString &jid, const QString& host ); - - int getPort( const QString &jid ); - void setPort( const QString &jid, const int port ); - - bool isAccDefault( const QString &jid ); - void setAccDefault( const QString &jid, const bool& def ); - - QString getStatus(); - void setStatus( const QString& status ); - - QString getStatusText(); - void setStatusText( const QString& status_text ); - - bool isHostPortManually( const QString &jid ); - void setHostPortManually( const QString &jid, const bool& def ); - - bool getBool(QString group, QString key); - void setBool(const bool isSet, QString group, QString key); - int getInt(QString group, QString key); - void setInt(const int isSet, QString group, QString key); - QString getString(QString group, QString key); - void setString(const QString isSet, QString group, QString key); - - -signals: - -public slots: - -}; - -#endif // MYSETTINGS_H diff --git a/src/listmodel.cpp b/src/listmodel.cpp index 2380c1b..b266381 100644 --- a/src/listmodel.cpp +++ b/src/listmodel.cpp @@ -1,3 +1,28 @@ +/******************************************************************** + +src/ListModel.cpp +-- reimplements QAbstractListModel to make use of it in QML. +http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html + +Copyright (c) 2010 Christophe Dumez + +This file is part of Lightbulb. + +Lightbulb 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. + +This program 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 this program. If not, see . + +*********************************************************************/ + #include "listmodel.h" #include diff --git a/src/listmodel.h b/src/listmodel.h index 10d2940..5c848aa 100644 --- a/src/listmodel.h +++ b/src/listmodel.h @@ -1,3 +1,28 @@ +/******************************************************************** + +src/ListModel.h +-- reimplements QAbstractListModel to make use of it in QML. +http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html + +Copyright (c) 2010 Christophe Dumez + +This file is part of Lightbulb. + +Lightbulb 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. + +This program 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 this program. If not, see . + +*********************************************************************/ + #ifndef LISTMODEL_H #define LISTMODEL_H @@ -5,10 +30,6 @@ #include #include -/* - * Source code from: http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html - */ - class ListItem: public QObject { Q_OBJECT diff --git a/src/main.cpp b/src/main.cpp index 624449c..c52dcb7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,10 +27,10 @@ along with this program. If not, see . #include "MyXmppClient.h" -#include "accountslistmodel.h" -#include "qmlvcard.h" -#include "SettingsDBWrapper.h" -#include "lightbulbhswidget.h" +#include "AccountsListModel.h" +#include "QMLVCard.h" +#include "Settings.h" +#include "LightbulbHSWidget.h" #include "QAvkonHelper.h" #include "DatabaseManager.h" #include "SymbiosisAPIClient.h" @@ -40,7 +40,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[]) { // expose C++ classes to QML qmlRegisterType("lightbulb", 1, 0, "XmppClient" ); - qmlRegisterType("lightbulb", 1, 0, "Settings" ); + qmlRegisterType("lightbulb", 1, 0, "Settings" ); qmlRegisterType("lightbulb", 1, 0, "XmppVCard" ); qmlRegisterType("lightbulb", 1, 0, "Clipboard" ); qmlRegisterType("lightbulb", 1, 0, "HSWidget" ); diff --git a/src/xmpp/MyXmppClient.cpp b/src/xmpp/MyXmppClient.cpp index bbaf742..ad3fee9 100644 --- a/src/xmpp/MyXmppClient.cpp +++ b/src/xmpp/MyXmppClient.cpp @@ -5,6 +5,7 @@ #include "QXmppConfiguration.h" #include "QXmppClient.h" #include "DatabaseManager.h" +#include "QXmppMessage.h" #include #include @@ -22,6 +23,7 @@ MyXmppClient::MyXmppClient() : QObject(0) { cacheIM = new MyCache(this); msgWrapper = new MessageWrapper(this); + // initialize DatabaseWorker and QThread dbWorker = new DatabaseWorker; dbThread = new QThread(this); dbWorker->moveToThread(dbThread); @@ -52,7 +54,6 @@ MyXmppClient::MyXmppClient() : QObject(0) { m_chatJid = ""; m_contactName = ""; m_keepAlive = 60; - accounts = 0; page = 1; flVCardRequest = ""; @@ -643,7 +644,7 @@ void MyXmppClient::messageReceivedSlot( const QXmppMessage &xmppMsg ) if( xmppMsg.isAttentionRequested() ) { //qDebug() << "ZZZ: attentionRequest !!! from:" <attention( bareJid_from, false ); + //msgWrapper->attention( bareJid_from, false ); } qDebug() << "MessageWrapper::messageReceived(): xmppMsg.state():" << xmppMsg.state(); } @@ -666,25 +667,18 @@ void MyXmppClient::messageReceivedSlot( const QXmppMessage &xmppMsg ) dbWorker->executeQuery(QStringList() << "incUnreadMessage" << QString::number(m_accountId) << bareJid_from); rosterNeedsUpdate = true; - archiveIncMessage(xmppMsg, false); - emit this->messageReceived( bareJid_from, bareJid_to ); - } -} -void MyXmppClient::archiveIncMessage( const QXmppMessage &xmppMsg, bool mine ) { - QDateTime currTime = QDateTime::currentDateTime(); - QString from = this->getBareJidByJid(xmppMsg.from()); - QString to = this->getBareJidByJid(xmppMsg.to()); - QString time = currTime.toString("dd-MM-yy hh:mm"); + QString body = xmppMsg.body(); + body = body.replace(">", ">"); //fix for > stuff + body = body.replace("<", "<"); //and < stuff too ^^ + body = msgWrapper->parseMsgOnLink(body); - QString body = xmppMsg.body(); - body = body.replace(">", ">"); //fix for > stuff - body = body.replace("<", "<"); //and < stuff too ^^ - body = msgWrapper->parseMsgOnLink(body); + dbWorker->executeQuery(QStringList() << "insertMessage" << QString::number(m_accountId) << this->getBareJidByJid(xmppMsg.from()) + << body << QDateTime::currentDateTime().toString("dd-MM-yy hh:mm") << "0"); + latestMessage = xmppMsg.body().left(30); - if (mine) dbWorker->executeQuery(QStringList() << "insertMessage" << QString::number(m_accountId) << to << body << time << QString::number(mine)); - else { dbWorker->executeQuery(QStringList() << "insertMessage" << QString::number(m_accountId) << from << body << time << QString::number(mine)); - latestMessage = xmppMsg.body().left(30); } + emit this->messageReceived( bareJid_from, bareJid_to ); + } } bool MyXmppClient::sendMyMessage(QString bareJid, QString resource, QString msgBody) //Q_INVOKABLE @@ -694,11 +688,7 @@ bool MyXmppClient::sendMyMessage(QString bareJid, QString resource, QString msgB QXmppMessage xmppMsg; QString jid_from = bareJid; - if( resource == "" ) { - jid_from += "/resource"; - } else { - jid_from += "/" + resource; - } + if( resource == "" ) jid_from += "/resource"; else jid_from += "/" + resource; xmppMsg.setTo( jid_from ); QString jid_to = m_myjid + "/" + xmppClient->configuration().resource(); @@ -712,7 +702,12 @@ bool MyXmppClient::sendMyMessage(QString bareJid, QString resource, QString msgB this->messageReceivedSlot( xmppMsg ); - archiveIncMessage(xmppMsg, true); + QString body = msgBody.replace(">", ">"); //fix for > stuff + body = body.replace("<", "<"); //and < stuff too ^^ + body = msgWrapper->parseMsgOnLink(body); + + dbWorker->executeQuery(QStringList() << "insertMessage" << QString::number(m_accountId) << this->getBareJidByJid(xmppMsg.to()) + << body << QDateTime::currentDateTime().toString("dd-MM-yy hh:mm") << "1"); return true; } @@ -728,48 +723,29 @@ void MyXmppClient::presenceReceived( const QXmppPresence & presence ) } QString myResource = xmppClient->configuration().resource(); - //qDebug() << "### MyXmppClient::presenceReceived():" << bareJid << "|" << resource << "|" << myResource << "|" << presence.from() << "|" << presence.type()<< "|" << presence.availableStatusType(); - if( (((presence.from()).indexOf( m_myjid ) >= 0) && (resource == myResource)) || ((bareJid == "") && (resource == "")) ) - { + if( (((presence.from()).indexOf( m_myjid ) >= 0) && (resource == myResource)) || ((bareJid == "") && (resource == "")) ) { QXmppPresence::Type __type = presence.type(); - if( __type == QXmppPresence::Unavailable ) - { - m_status = Offline; - } - else - { + if( __type == QXmppPresence::Unavailable ) m_status = Offline; + else { QXmppPresence::AvailableStatusType __status = presence.availableStatusType(); - if( __status == QXmppPresence::Online ) { - m_status = Online; - } else if( __status == QXmppPresence::Chat ) { - m_status = Chat; - } else if ( __status == QXmppPresence::Away ) { - m_status = Away; - } else if ( __status == QXmppPresence::XA ) { - m_status = XA; - } else if( __status == QXmppPresence::DND ) { - m_status = DND; - } + if( __status == QXmppPresence::Online ) m_status = Online; + else if( __status == QXmppPresence::Chat ) m_status = Chat; + else if ( __status == QXmppPresence::Away ) m_status = Away; + else if ( __status == QXmppPresence::XA ) m_status = XA; + else if( __status == QXmppPresence::DND ) m_status = DND; } - emit statusChanged(); } } -void MyXmppClient::error(QXmppClient::Error e) -{ +void MyXmppClient::error(QXmppClient::Error e) { QString errString; - if( e == QXmppClient::SocketError ) { - errString = "SOCKET_ERROR"; - } else if( e == QXmppClient::KeepAliveError ) { - errString = "KEEP_ALIVE_ERROR"; - } else if( e == QXmppClient::XmppStreamError ) { - errString = "XMPP_STREAM_ERROR"; - } + if( e == QXmppClient::SocketError ) errString = "SOCKET_ERROR"; + else if( e == QXmppClient::KeepAliveError ) errString = "KEEP_ALIVE_ERROR"; + else if( e == QXmppClient::XmppStreamError ) errString = "XMPP_STREAM_ERROR"; - if( !errString.isNull() ) - { + if( !errString.isNull() ) { QXmppPresence pr = xmppClient->clientPresence(); this->presenceReceived( pr ); QXmppPresence presence( QXmppPresence::Unavailable ); @@ -797,13 +773,7 @@ void MyXmppClient::addContact( QString bareJid, QString nick, QString group, boo void MyXmppClient::removeContact( QString bareJid ) { if( rosterManager ) rosterManager->removeItem( bareJid ); } -void MyXmppClient::renameContact(QString bareJid, QString name) //Q_INVOKABLE -{ - //qDebug() << "MyXmppClient::renameContact(" << bareJid << ", " << name << ")" ; - if( rosterManager ) { - rosterManager->renameItem( bareJid, name ); - } -} +void MyXmppClient::renameContact(QString bareJid, QString name) { if( rosterManager ) rosterManager->renameItem( bareJid, name ); } bool MyXmppClient::subscribe(const QString bareJid) //Q_INVOKABLE { @@ -865,8 +835,6 @@ void MyXmppClient::attentionSend( QString bareJid, QString resource ) xmppMsg.setAttentionRequested( true ); xmppClient->sendPacket( xmppMsg ); - - msgWrapper->attention( bareJid, true ); } void MyXmppClient::changeSqlRoster() { @@ -894,7 +862,7 @@ void MyXmppClient::changeSqlRoster() { } } -void MyXmppClient::gotoPage(int nPage) { if (page != nPage || m_chatJid != m_lastChatJid ) { page = nPage; m_lastChatJid = m_chatJid; this->updateMessages(); } emit pageChanged(); } +void MyXmppClient::gotoPage(int nPage) { page = nPage; this->updateMessages(); emit pageChanged(); } /* --- diagnostics --- */ bool MyXmppClient::dbRemoveDb() { @@ -926,5 +894,5 @@ bool MyXmppClient::removeDir(const QString &dirName) { return result; } -bool MyXmppClient::resetSettings() { return QFile::remove(mimOpt->fileConfig); } +bool MyXmppClient::resetSettings() { return QFile::remove(mimOpt->confFile); } diff --git a/src/xmpp/MyXmppClient.h b/src/xmpp/MyXmppClient.h index bcaffbd..e897266 100644 --- a/src/xmpp/MyXmppClient.h +++ b/src/xmpp/MyXmppClient.h @@ -14,47 +14,16 @@ #include #include -#include "DatabaseManager.h" #include "DatabaseWorker.h" #include "MyCache.h" #include "MessageWrapper.h" -#include "SettingsDBWrapper.h" +#include "Settings.h" #include #include "QXmppRosterManager.h" #include "QMLVCard.h" - -/****************/ -/* http://www.developer.nokia.com/Community/Wiki/Workaround_to_hide_VKB_in_QML_apps_%28Known_Issue%29 */ -class EventFilter : public QObject -{ -protected: - bool eventFilter(QObject *obj, QEvent *event) { - QInputContext *ic = qApp->inputContext(); - if (ic) - { - if ( (ic->focusWidget() == 0) && prevFocusWidget) - { - QEvent closeSIPEvent( QEvent::CloseSoftwareInputPanel ); - ic->filterEvent(&closeSIPEvent); - } - else if ( (prevFocusWidget == 0) && (ic->focusWidget()) ) - { - QEvent openSIPEvent( QEvent::RequestSoftwareInputPanel ); - ic->filterEvent(&openSIPEvent); - } - prevFocusWidget = ic->focusWidget(); - } - return QObject::eventFilter(obj,event); - } - -private: - QWidget *prevFocusWidget; -}; -/****************/ - typedef QMap Map; class MyXmppClient : public QObject @@ -86,7 +55,6 @@ class MyXmppClient : public QObject Q_PROPERTY( QMLVCard* vcard READ getVCard NOTIFY vCardChanged ) Q_PROPERTY( int keepAlive READ getKeepAlive WRITE setKeepAlive NOTIFY keepAliveChanged ) Q_PROPERTY( bool reconnectOnError READ getReconnectOnError WRITE setReconnectOnError NOTIFY reconnectOnErrorChanged ) - Q_PROPERTY( bool archiveIncMessage READ getArchiveIncMessage WRITE setArchiveIncMessage NOTIFY archiveIncMessageChanged ) MyCache *cacheIM; MessageWrapper *msgWrapper; @@ -95,7 +63,7 @@ class MyXmppClient : public QObject QXmppRosterManager *rosterManager; QXmppVCardManager *vCardManager; - SettingsDBWrapper *mimOpt; + Settings *mimOpt; QStringList jidCache; @@ -247,42 +215,15 @@ public : } QString getContactName() const { return m_contactName; } - void setContactName( const QString & value ) - { - if(value!=m_contactName) { - m_contactName=value; - emit contactNameChanged(); - } - } + void setContactName( const QString & value ) { if(value!=m_contactName) { m_contactName=value; emit contactNameChanged(); } } QMLVCard* getVCard() const { return qmlVCard; } int getKeepAlive() const { return m_keepAlive; } - void setKeepAlive(int arg) - { - if (m_keepAlive != arg) { - m_keepAlive = arg; - emit keepAliveChanged(); - } - } + void setKeepAlive(int arg) { if (m_keepAlive != arg) { m_keepAlive = arg; emit keepAliveChanged(); } } bool getReconnectOnError() const { return m_reconnectOnError; } - void setReconnectOnError(bool arg) - { - if (m_reconnectOnError != arg) { - m_reconnectOnError = arg; - emit reconnectOnErrorChanged(); - } - } - - bool getArchiveIncMessage() const { return m_archiveIncMessage; } - void setArchiveIncMessage(bool arg) - { - if (m_archiveIncMessage != arg) { - m_archiveIncMessage = arg; - emit archiveIncMessageChanged(); - } - } + void setReconnectOnError(bool arg) { if (m_reconnectOnError != arg) { m_reconnectOnError = arg; emit reconnectOnErrorChanged(); } } signals: void versionChanged(); @@ -344,6 +285,7 @@ private slots: private: QString m_bareJidLastMessage; QString m_resourceLastMessage; + StateConnect m_stateConnect; StatusXmpp m_status; QString m_statusText; @@ -356,8 +298,6 @@ private slots: QString m_chatJid; QString m_lastChatJid; QString m_contactName; - int accounts; - void archiveIncMessage( const QXmppMessage &xmppMsg, bool mine ); bool rosterAvailable; bool rosterNeedsUpdate; @@ -373,11 +313,8 @@ private slots: int m_keepAlive; bool m_reconnectOnError; - bool m_archiveIncMessage; bool flSetPresenceWithoutAck; }; -QML_DECLARE_TYPE( MyXmppClient ) - #endif diff --git a/src/xmpp/messagewrapper.cpp b/src/xmpp/messagewrapper.cpp index de17eee..1f188aa 100644 --- a/src/xmpp/messagewrapper.cpp +++ b/src/xmpp/messagewrapper.cpp @@ -1,6 +1,30 @@ +/******************************************************************** + +src/xmpp/MessageWrapper.cpp +-- looks for strings beginning with http, https and then makes them +hyperlinks with ". + +*********************************************************************/ + #include "messagewrapper.h" -#include "MyXmppClient.h" -//#include "qmlxmppclient.h" #include MessageWrapper::MessageWrapper(QObject *parent) : QObject(parent) { @@ -52,10 +76,3 @@ QString MessageWrapper::parseMsgOnLink( const QString &inString ) const //qDebug() << outString; return outString; } - -void MessageWrapper::attention(const QString &bareJid, const bool isMsgMine) -{ - QString dataTimeMsg = QDateTime::currentDateTime().toString("hh:mm:ss"); - bufAttentions[ bareJid ] = dataTimeMsg; - //qDebug() << "*** " << Q_FUNC_INFO <<": "<< bufAttentions.contains(bareJid) << ": "<< bufAttentions[ bareJid ] << ": " << openChatJid; -} diff --git a/src/xmpp/messagewrapper.h b/src/xmpp/messagewrapper.h index 7400d37..a8d1b85 100644 --- a/src/xmpp/messagewrapper.h +++ b/src/xmpp/messagewrapper.h @@ -1,28 +1,41 @@ +/******************************************************************** + +src/xmpp/MessageWrapper.h +-- looks for strings beginning with http, https and then makes them +hyperlinks with ". + +*********************************************************************/ + #ifndef MESSAGEWRAPPER_H #define MESSAGEWRAPPER_H -#include "QXmppMessage.h" -//#include "QXmppPacket.h" -#include -#include -#include -#include -#include "DatabaseManager.h" - -class MyXmppClient; +#include class MessageWrapper : public QObject { Q_OBJECT - QXmppMessage *xmppMessage; - - QMap< QString, QString > bufAttentions; public: explicit MessageWrapper(QObject *parent = 0); - void attention( const QString &bareJid, const bool isMsgMine ); - QString parseMsgOnLink( const QString &inString ) const; };