From d187495fd3566da1ee1eb94cc313c2f561a0fac5 Mon Sep 17 00:00:00 2001 From: eshe Date: Sun, 13 Jun 2010 23:22:10 +0100 Subject: [PATCH] Architecture changed to allow easier addition of new phone books. Norwegian phonebook added. --- Makefile | 135 ------------------ src/common/cache.cpp | 4 +- src/common/cache.h | 6 +- src/common/db.cpp | 5 + src/common/emptycoreconfig.cpp | 53 +++++++ src/common/emptycoreconfig.h | 42 ++++++ src/common/eniro.cpp | 239 +++++++++----------------------- src/common/eniro.h | 67 ++------- src/common/enirocoreconfig.cpp | 140 +++++++++++++++++++ src/common/enirocoreconfig.h | 52 +++++++ src/common/mobil1881.cpp | 283 ++++++++++++++++++++++++++++++++++++++ src/common/mobil1881.h | 40 ++++++ src/common/settings.cpp | 106 ++++++++++---- src/common/settings.h | 15 +- src/common/source.cpp | 229 ++++++++++++++++++++++++++++++ src/common/source.h | 122 ++++++++++++++++ src/common/sourcecoreconfig.cpp | 47 +++++++ src/common/sourcecoreconfig.h | 41 ++++++ src/common/translations/fi_FI.qm | Bin 7992 -> 8773 bytes src/common/translations/fi_FI.ts | 171 ++++++++++++++--------- src/daemon/calllistener.cpp | 73 +++++----- src/daemon/calllistener.h | 11 +- src/daemon/daemon.pro | 29 +++- src/daemon/main.cpp | 7 +- src/gui/buttonselector.cpp | 8 +- src/gui/buttonselector.h | 6 + src/gui/detailwindow.cpp | 2 +- src/gui/detailwindow.h | 2 +- src/gui/emptyguiconfig.cpp | 40 ++++++ src/gui/emptyguiconfig.h | 39 ++++++ src/gui/eniroguiconfig.cpp | 103 ++++++++++++++ src/gui/eniroguiconfig.h | 44 ++++++ src/gui/gui.pro | 49 ++++++- src/gui/main.cpp | 11 +- src/gui/mainwindow.cpp | 15 +- src/gui/resultwindow.cpp | 90 ++++++------ src/gui/resultwindow.h | 11 +- src/gui/searchdialog.cpp | 10 +- src/gui/settingsdialog.cpp | 146 ++++++++++---------- src/gui/settingsdialog.h | 27 ++-- src/gui/sourceguiconfig.cpp | 49 +++++++ src/gui/sourceguiconfig.h | 38 +++++ 42 files changed, 1953 insertions(+), 654 deletions(-) delete mode 100644 Makefile create mode 100644 src/common/emptycoreconfig.cpp create mode 100644 src/common/emptycoreconfig.h create mode 100644 src/common/enirocoreconfig.cpp create mode 100644 src/common/enirocoreconfig.h create mode 100644 src/common/mobil1881.cpp create mode 100644 src/common/mobil1881.h create mode 100644 src/common/source.cpp create mode 100644 src/common/source.h create mode 100644 src/common/sourcecoreconfig.cpp create mode 100644 src/common/sourcecoreconfig.h create mode 100644 src/gui/emptyguiconfig.cpp create mode 100644 src/gui/emptyguiconfig.h create mode 100644 src/gui/eniroguiconfig.cpp create mode 100644 src/gui/eniroguiconfig.h create mode 100644 src/gui/sourceguiconfig.cpp create mode 100644 src/gui/sourceguiconfig.h diff --git a/Makefile b/Makefile deleted file mode 100644 index 79a8684..0000000 --- a/Makefile +++ /dev/null @@ -1,135 +0,0 @@ -############################################################################# -# Makefile for building: jenirok -# Generated by qmake (2.01a) (Qt 4.6.2) on: Thu Jun 10 20:41:54 2010 -# Project: jenirok.pro -# Template: subdirs -# Command: /usr/bin/qmake -unix -o Makefile jenirok.pro -############################################################################# - -first: make_default -MAKEFILE = Makefile -QMAKE = /usr/bin/qmake -DEL_FILE = rm -f -CHK_DIR_EXISTS= test -d -MKDIR = mkdir -p -COPY = cp -f -COPY_FILE = $(COPY) -COPY_DIR = $(COPY) -r -INSTALL_FILE = install -m 644 -p -INSTALL_PROGRAM = install -m 755 -p -INSTALL_DIR = $(COPY_DIR) -DEL_FILE = rm -f -SYMLINK = ln -f -s -DEL_DIR = rmdir -MOVE = mv -f -CHK_DIR_EXISTS= test -d -MKDIR = mkdir -p -SUBTARGETS = \ - sub-src-gui \ - sub-src-daemon - -src/gui/$(MAKEFILE): - @$(CHK_DIR_EXISTS) src/gui/ || $(MKDIR) src/gui/ - cd src/gui/ && $(QMAKE) /home/maemo/workspace/jenirok/src/gui/gui.pro -unix -o $(MAKEFILE) -sub-src-gui-qmake_all: FORCE - @$(CHK_DIR_EXISTS) src/gui/ || $(MKDIR) src/gui/ - cd src/gui/ && $(QMAKE) /home/maemo/workspace/jenirok/src/gui/gui.pro -unix -o $(MAKEFILE) -sub-src-gui: src/gui/$(MAKEFILE) FORCE - cd src/gui/ && $(MAKE) -f $(MAKEFILE) -sub-src-gui-make_default: src/gui/$(MAKEFILE) FORCE - cd src/gui/ && $(MAKE) -f $(MAKEFILE) -sub-src-gui-make_first: src/gui/$(MAKEFILE) FORCE - cd src/gui/ && $(MAKE) -f $(MAKEFILE) first -sub-src-gui-all: src/gui/$(MAKEFILE) FORCE - cd src/gui/ && $(MAKE) -f $(MAKEFILE) all -sub-src-gui-clean: src/gui/$(MAKEFILE) FORCE - cd src/gui/ && $(MAKE) -f $(MAKEFILE) clean -sub-src-gui-distclean: src/gui/$(MAKEFILE) FORCE - cd src/gui/ && $(MAKE) -f $(MAKEFILE) distclean -sub-src-gui-install_subtargets: src/gui/$(MAKEFILE) FORCE - cd src/gui/ && $(MAKE) -f $(MAKEFILE) install -sub-src-gui-uninstall_subtargets: src/gui/$(MAKEFILE) FORCE - cd src/gui/ && $(MAKE) -f $(MAKEFILE) uninstall -src/daemon/$(MAKEFILE): - @$(CHK_DIR_EXISTS) src/daemon/ || $(MKDIR) src/daemon/ - cd src/daemon/ && $(QMAKE) /home/maemo/workspace/jenirok/src/daemon/daemon.pro -unix -o $(MAKEFILE) -sub-src-daemon-qmake_all: FORCE - @$(CHK_DIR_EXISTS) src/daemon/ || $(MKDIR) src/daemon/ - cd src/daemon/ && $(QMAKE) /home/maemo/workspace/jenirok/src/daemon/daemon.pro -unix -o $(MAKEFILE) -sub-src-daemon: src/daemon/$(MAKEFILE) FORCE - cd src/daemon/ && $(MAKE) -f $(MAKEFILE) -sub-src-daemon-make_default: src/daemon/$(MAKEFILE) FORCE - cd src/daemon/ && $(MAKE) -f $(MAKEFILE) -sub-src-daemon-make_first: src/daemon/$(MAKEFILE) FORCE - cd src/daemon/ && $(MAKE) -f $(MAKEFILE) first -sub-src-daemon-all: src/daemon/$(MAKEFILE) FORCE - cd src/daemon/ && $(MAKE) -f $(MAKEFILE) all -sub-src-daemon-clean: src/daemon/$(MAKEFILE) FORCE - cd src/daemon/ && $(MAKE) -f $(MAKEFILE) clean -sub-src-daemon-distclean: src/daemon/$(MAKEFILE) FORCE - cd src/daemon/ && $(MAKE) -f $(MAKEFILE) distclean -sub-src-daemon-install_subtargets: src/daemon/$(MAKEFILE) FORCE - cd src/daemon/ && $(MAKE) -f $(MAKEFILE) install -sub-src-daemon-uninstall_subtargets: src/daemon/$(MAKEFILE) FORCE - cd src/daemon/ && $(MAKE) -f $(MAKEFILE) uninstall - -Makefile: jenirok.pro /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/linux-g++-maemo5/qmake.conf /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/common/unix.conf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/common/linux.conf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/qconfig.pri \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/qt_functions.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/qt_config.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/exclusive_builds.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/default_pre.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/release.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/default_post.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/warn_on.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/qt.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/unix/thread.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/moc.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/resources.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/uic.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/yacc.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/lex.prf \ - /targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/include_source_dir.prf - $(QMAKE) -unix -o Makefile jenirok.pro -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/common/unix.conf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/common/linux.conf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/qconfig.pri: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/qt_functions.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/qt_config.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/exclusive_builds.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/default_pre.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/release.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/default_post.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/warn_on.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/qt.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/unix/thread.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/moc.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/resources.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/uic.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/yacc.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/lex.prf: -/targets/FREMANTLE_ARMEL/usr/share/qt4/mkspecs/features/include_source_dir.prf: -qmake: qmake_all FORCE - @$(QMAKE) -unix -o Makefile jenirok.pro - -qmake_all: sub-src-gui-qmake_all sub-src-daemon-qmake_all FORCE - -make_default: sub-src-gui-make_default sub-src-daemon-make_default FORCE -make_first: sub-src-gui-make_first sub-src-daemon-make_first FORCE -all: sub-src-gui-all sub-src-daemon-all FORCE -clean: sub-src-gui-clean sub-src-daemon-clean FORCE -distclean: sub-src-gui-distclean sub-src-daemon-distclean FORCE - -$(DEL_FILE) Makefile -install_subtargets: sub-src-gui-install_subtargets sub-src-daemon-install_subtargets FORCE -uninstall_subtargets: sub-src-gui-uninstall_subtargets sub-src-daemon-uninstall_subtargets FORCE - -mocclean: compiler_moc_header_clean compiler_moc_source_clean - -mocables: compiler_moc_header_make_all compiler_moc_source_make_all -install: install_subtargets FORCE - -uninstall: uninstall_subtargets FORCE - -FORCE: - diff --git a/src/common/cache.cpp b/src/common/cache.cpp index 56e37d0..fcbbcb5 100644 --- a/src/common/cache.cpp +++ b/src/common/cache.cpp @@ -54,7 +54,7 @@ int Cache::clear() return ret; } -bool Cache::findItem(QString const& number, Eniro::Result& result) +bool Cache::findItem(QString const& number, Source::Result& result) { bool connected = DB::connected(); @@ -87,7 +87,7 @@ bool Cache::findItem(QString const& number, Eniro::Result& result) return ret; } -bool Cache::addItem(Eniro::Result const& result) +bool Cache::addItem(Source::Result const& result) { bool connected = DB::connected(); diff --git a/src/common/cache.h b/src/common/cache.h index dca1369..f08b9d9 100644 --- a/src/common/cache.h +++ b/src/common/cache.h @@ -19,7 +19,7 @@ #ifndef CACHE_H #define CACHE_H -#include "eniro.h" +#include "source.h" class Cache { @@ -27,8 +27,8 @@ class Cache public: static Cache& instance(); int clear(); - bool findItem(QString const& number, Eniro::Result& result); - bool addItem(Eniro::Result const& result); + bool findItem(QString const& number, Source::Result& result); + bool addItem(Source::Result const& result); private: Cache(); diff --git a/src/common/db.cpp b/src/common/db.cpp index 83aa85f..ce0cf97 100644 --- a/src/common/db.cpp +++ b/src/common/db.cpp @@ -73,6 +73,11 @@ void DB::disconnect() void DB::removeDatabase() { + if(!initialized_) + { + return; + } + db_.close(); db_ = QSqlDatabase(); QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection); diff --git a/src/common/emptycoreconfig.cpp b/src/common/emptycoreconfig.cpp new file mode 100644 index 0000000..aa3b284 --- /dev/null +++ b/src/common/emptycoreconfig.cpp @@ -0,0 +1,53 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#include "emptycoreconfig.h" + +EmptyCoreConfig::EmptyCoreConfig(): SourceCoreConfig() +{ +} + +EmptyCoreConfig::~EmptyCoreConfig() +{ +} + +bool EmptyCoreConfig::apply(Source* source) +{ + Q_UNUSED(source); + return true; +} + +void EmptyCoreConfig::getConfig(QMap& config) +{ + Q_UNUSED(config); +} + +void EmptyCoreConfig::loadFromConfig(QMap const& config) +{ + Q_UNUSED(config); +} + +void EmptyCoreConfig::store() +{ +} + +bool EmptyCoreConfig::readyToSearch() +{ + return true; +} + diff --git a/src/common/emptycoreconfig.h b/src/common/emptycoreconfig.h new file mode 100644 index 0000000..72f60e9 --- /dev/null +++ b/src/common/emptycoreconfig.h @@ -0,0 +1,42 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#ifndef EMPTYCORECONFIG_H +#define EMPTYCORECONFIG_H + +#include +#include +#include "sourcecoreconfig.h" + +class EmptyCoreConfig : public SourceCoreConfig +{ + +public: + EmptyCoreConfig(); + virtual ~EmptyCoreConfig(); + virtual bool apply(Source* source); + void store(); + virtual void getConfig(QMap& config); + virtual void loadFromConfig(QMap const& config); + virtual bool readyToSearch(); + +private: + +}; + +#endif diff --git a/src/common/eniro.cpp b/src/common/eniro.cpp index acf5e6d..0958893 100644 --- a/src/common/eniro.cpp +++ b/src/common/eniro.cpp @@ -50,28 +50,19 @@ namespace static const QString LOGIN_CHECK = "]+)>"); - -Eniro::Eniro(Site site, QObject *parent): QObject(parent), site_(site), -username_(""), password_(""), loggedIn_(false), error_(NO_ERROR), -errorString_(""), maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), -findNumber_(true), pendingSearches_(), pendingNumberRequests_() +Eniro::Eniro(QObject *parent): Source(parent), site_(Eniro::FI), +loggedIn_(false), username_(""), password_(""), +timerId_(0), pendingSearches_(), pendingNumberRequests_() { - connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool))); } Eniro::~Eniro() { - abort(); } void Eniro::abort() { - http_.abort(); + Source::abort(); for(searchMap::iterator sit = pendingSearches_.begin(); sit != pendingSearches_.end(); sit++) @@ -99,63 +90,32 @@ void Eniro::abort() pendingLoginRequests_.clear(); } -void Eniro::setMaxResults(unsigned int value) -{ - maxResults_ = value; -} - -void Eniro::setFindNumber(bool value) -{ - findNumber_ = value; -} - void Eniro::setSite(Eniro::Site site) { site_ = site; } -void Eniro::setTimeout(unsigned int ms) +void Eniro::timerEvent(QTimerEvent* t) { - timeout_ = ms; - resetTimeout(); -} + Q_UNUSED(t); -void Eniro::resetTimeout() -{ - if(timerId_) - { - killTimer(timerId_); - } - if(timeout_) - { - timerId_ = startTimer(timeout_); - } -} + int currentId = http_.currentId(); -void Eniro::timerEvent(QTimerEvent* t) -{ - if(t->timerId() == timerId_) + if(currentId) { - int currentId = http_.currentId(); + searchMap::const_iterator it = pendingSearches_.find(currentId); - if(currentId) + if(it != pendingSearches_.end()) { - searchMap::const_iterator it = pendingSearches_.find(currentId); + QVector results = it.value()->results; + SearchDetails details = it.value()->details; - if(it != pendingSearches_.end()) - { - QVector results = it.value()->results; - SearchDetails details = it.value()->details; + abort(); - abort(); + setError(TIMEOUT, TIMEOUT_STRING); - error_ = TIMEOUT; - errorString_ = TIMEOUT_STRING; - - emit requestFinished(results, details, true); - } + emit requestFinished(results, details, true); } - } } @@ -174,18 +134,7 @@ void Eniro::logout() loggedIn_ = false; } -void Eniro::testLogin() -{ - QUrl url = createUrl("", ""); - - url.addQueryItem("what", "mobwp"); - http_.setHost(url.host(), url.port(80)); - int id = http_.get(url.encodedPath() + '?' + url.encodedQuery()); - - pendingLoginRequests_.insert(id); -} - -bool Eniro::search(SearchDetails const& details) +void Eniro::search(SearchDetails const& details) { resetTimeout(); @@ -227,7 +176,7 @@ bool Eniro::search(SearchDetails const& details) http_.setHost(url.host(), url.port(80)); int id = http_.get(url.encodedPath() + '?' + url.encodedQuery()); - QVector results; + QVector results; // Store search data for later identification SearchData* newData = new SearchData; @@ -239,26 +188,11 @@ bool Eniro::search(SearchDetails const& details) // Store request id so that it can be identified later pendingSearches_[id] = newData; - return true; -} - -Eniro::Error Eniro::error() const -{ - return error_; } -const QString& Eniro::errorString() const +void Eniro::handleHttpData(int id, QString const& data) { - return errorString_; -} - -void Eniro::httpReady(int id, bool error) -{ - if(error) - { - qDebug() << "Error: " << http_.errorString(); - } - + qDebug() << "Handle:" << id; searchMap::const_iterator searchIt; numberMap::const_iterator numberIt; @@ -266,37 +200,17 @@ void Eniro::httpReady(int id, bool error) if((searchIt = pendingSearches_.find(id)) != pendingSearches_.end()) { - if(error) - { - error_ = CONNECTION_FAILURE; - errorString_ = http_.errorString(); - emitRequestFinished(id, searchIt.value(), true); - return; - } - - QString result(http_.readAll()); - + qDebug() << "Load results"; // Load results from html data - loadResults(id, result); + loadResults(id, data); } // Check if request is pending number requests else if((numberIt = pendingNumberRequests_.find(id)) != pendingNumberRequests_.end()) { - if(error) - { - error_ = CONNECTION_FAILURE; - errorString_ = http_.errorString(); - delete pendingNumberRequests_[id]; - pendingNumberRequests_.remove(id); - return; - } - - QString result(http_.readAll()); - // Load number from html data - loadNumber(id, result); + loadNumber(id, data); } // Check for login request @@ -305,17 +219,8 @@ void Eniro::httpReady(int id, bool error) { bool success = true; - if(!error) - { - QString result(http_.readAll()); - - // If html source contains LOGIN_CHECK, login failed - if(result.indexOf(LOGIN_CHECK) != -1) - { - success = false; - } - } - else + // If html source contains LOGIN_CHECK, login failed + if(data.indexOf(LOGIN_CHECK) != -1) { success = false; } @@ -325,6 +230,37 @@ void Eniro::httpReady(int id, bool error) } +void Eniro::handleHttpError(int id) +{ + searchMap::const_iterator searchIt; + numberMap::const_iterator numberIt; + + // Check if request is pending search request + if((searchIt = pendingSearches_.find(id)) != + pendingSearches_.end()) + { + setError(CONNECTION_FAILURE, http_.errorString()); + emitRequestFinished(id, searchIt.value(), true); + } + + // Check if request is pending number requests + else if((numberIt = pendingNumberRequests_.find(id)) != + pendingNumberRequests_.end()) + { + setError(CONNECTION_FAILURE, http_.errorString()); + delete pendingNumberRequests_[id]; + pendingNumberRequests_.remove(id); + } + + // Check for login request + else if(pendingLoginRequests_.find(id) != + pendingLoginRequests_.end()) + { + emit loginStatus(false); + + } +} + // Loads results from html source code void Eniro::loadResults(int id, QString const& httpData) { @@ -417,7 +353,7 @@ void Eniro::loadResults(int id, QString const& httpData) // If phone number searh is enabled, we have to make another // request to find it out - if(findNumber_ && size < 4 && loggedIn_ && + if(getFindNumber() && size < 4 && loggedIn_ && it.value()->details.type != YELLOW_PAGES) { requestsPending = true; @@ -429,8 +365,10 @@ void Eniro::loadResults(int id, QString const& httpData) emit resultAvailable(result, it.value()->details); } + unsigned int maxResults = getMaxResults(); + // Stop searching if max results is reached - if(maxResults_ && (foundResults >= maxResults_)) + if(maxResults && (foundResults >= maxResults)) { break; } @@ -444,8 +382,7 @@ void Eniro::loadResults(int id, QString const& httpData) if(httpData.indexOf(LOGIN_CHECK) != -1) { - error_ = INVALID_LOGIN; - errorString_ = INVALID_LOGIN_STRING; + setError(INVALID_LOGIN, INVALID_LOGIN_STRING), error = true; } @@ -506,8 +443,7 @@ void Eniro::loadNumber(int id, QString const& result) if(error) { - error_ = INVALID_LOGIN; - errorString_ = INVALID_LOGIN; + setError(INVALID_LOGIN, INVALID_LOGIN_STRING); emitRequestFinished(searchIt.key(), searchIt.value(), true); } @@ -534,9 +470,11 @@ QUrl Eniro::createUrl(QString const& query, QString const& location) url.addQueryItem("geo_area", location); } - if(maxResults_) + unsigned int maxResults = getMaxResults(); + + if(maxResults) { - url.addQueryItem("hpp", QString::number(maxResults_)); + url.addQueryItem("hpp", QString::number(maxResults)); } if(loggedIn_) { @@ -544,8 +482,7 @@ QUrl Eniro::createUrl(QString const& query, QString const& location) url.addQueryItem("login_password", password_); } - QByteArray path = url.encodedQuery().replace('+', "%2B"); - url.setEncodedQuery(path); + fixUrl(url); return url; } @@ -568,44 +505,12 @@ void Eniro::getNumberForResult(int id, int index, SearchDetails const& details) void Eniro::emitRequestFinished(int key, SearchData* data, bool error) { - - // Do not emit "Request aborted" error - if(!(error && (http_.error() == QHttp::Aborted))) - { - emit requestFinished(data->results, data->details, error); - } - + emit requestFinished(data->results, data->details, error); delete pendingSearches_[key]; pendingSearches_[key] = 0; pendingSearches_.remove(key); } -QString Eniro::ucFirst(QString& str) -{ - if (str.size() < 1) { - return ""; - } - - QStringList tokens = str.split(" "); - QList::iterator tokItr; - - for (tokItr = tokens.begin(); tokItr != tokens.end(); ++tokItr) - { - (*tokItr) = (*tokItr).at(0).toUpper() + (*tokItr).mid(1); - } - - return tokens.join(" "); -} - -QString& Eniro::cleanUpNumber(QString& number) -{ - return number.replace(numberCleaner_, ""); -} - -QString& Eniro::stripTags(QString& string) -{ - return string.replace(tagStripper_, ""); -} QMap Eniro::getSites() { @@ -633,17 +538,9 @@ Eniro::Site Eniro::stringToSite(QString const& str) if(lower == SITE_NAMES[i] || lower == SITE_IDS[i]) { site = static_cast (i); + break; } } return site; } - -Eniro::SearchDetails::SearchDetails(QString const& q, - QString const& loc, - SearchType t) -{ - query = q; - location = loc; - type = t; -} diff --git a/src/common/eniro.h b/src/common/eniro.h index f29a1bc..bbec9fe 100644 --- a/src/common/eniro.h +++ b/src/common/eniro.h @@ -27,37 +27,15 @@ #include #include #include +#include "source.h" -class Eniro: public QObject +class Eniro: public Source { Q_OBJECT public: enum Site {FI, SE, DK}; - static const int SITE_COUNT = 3; - - enum SearchType {YELLOW_PAGES, PERSONS}; - - enum Error {NO_ERROR, CONNECTION_FAILURE, INVALID_LOGIN, TIMEOUT}; - - struct Result - { - QString name; - QString street; - QString city; - QString number; - }; - - struct SearchDetails - { - QString query; - QString location; - SearchType type; - SearchDetails(QString const& query = "", - QString const& location = "", - SearchType type = PERSONS); - }; struct SiteDetails { @@ -65,34 +43,21 @@ public: QString id; }; - static unsigned int const DEFAULT_MAX_RESULTS = 30; - - Eniro(Site site, QObject *parent = 0); + static const int SITE_COUNT = 3; + Eniro(QObject *parent = 0); ~Eniro(); - void login(QString const& username, QString const& password); void logout(); - void testLogin(); void setSite(Site); - void setMaxResults(unsigned int value); - void setFindNumber(bool value); - void setTimeout(unsigned int ms); - bool search(SearchDetails const& details); - void abort(); - Error error() const; - const QString& errorString() const; + virtual void search(Source::SearchDetails const& details); + virtual void abort(); static QMap getSites(); static Site stringToSite(QString const& str); signals: - void resultAvailable(Eniro::Result const& result, Eniro::SearchDetails const& details); - void requestFinished(QVector const& results, Eniro::SearchDetails const& details, bool error); void loginStatus(bool success); -private slots: - void httpReady(int id, bool error); - private: Q_DISABLE_COPY(Eniro); @@ -105,34 +70,26 @@ private: struct SearchData { - SearchDetails details; - QVector results; + Source::SearchDetails details; + QVector results; unsigned int foundNumbers; unsigned int numbersTotal; }; + virtual void handleHttpData(int id, QString const& data); + virtual void handleHttpError(int id); QUrl createUrl(QString const& query, QString const& location); void loadResults(int id, QString const& data); void loadNumber(int id, QString const& data); void getNumberForResult(int id, int index, SearchDetails const& details); void emitRequestFinished(int key, SearchData* data, bool error); - void resetTimeout(); - void timerEvent(QTimerEvent *te); - QString ucFirst(QString& string); - QString& cleanUpNumber(QString& number); - QString& stripTags(QString& string); + void timerEvent(QTimerEvent *t); - QHttp http_; Site site_; + bool loggedIn_; QString username_; QString password_; - bool loggedIn_; - Error error_; - QString errorString_; - unsigned int maxResults_; - unsigned int timeout_; int timerId_; - bool findNumber_; typedef QMap searchMap; typedef QMap numberMap; searchMap pendingSearches_; diff --git a/src/common/enirocoreconfig.cpp b/src/common/enirocoreconfig.cpp new file mode 100644 index 0000000..e272bce --- /dev/null +++ b/src/common/enirocoreconfig.cpp @@ -0,0 +1,140 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#include +#include "enirocoreconfig.h" +#include "eniro.h" +#include "settings.h" + +namespace +{ + QString const COLUMN_PREFIX = "eniro_"; + QString const USERNAME_COLUMN = COLUMN_PREFIX + "username"; + QString const PASSWORD_COLUMN = COLUMN_PREFIX + "password"; + QString const SITE_COLUMN = COLUMN_PREFIX + "site"; +} + +EniroCoreConfig::EniroCoreConfig(): SourceCoreConfig(), loaded_(false) +{ +} + +EniroCoreConfig::~EniroCoreConfig() +{ +} + +bool EniroCoreConfig::apply(Source* source) +{ + Eniro* eniro = dynamic_cast(source); + + if(!eniro) + { + return false; + } + + load(); + + QString username = config_[USERNAME_COLUMN]; + QString password = config_[PASSWORD_COLUMN]; + + if(!username.isEmpty() && !password.isEmpty()) + { + eniro->login(username, password); + } + + eniro->setSite(Eniro::stringToSite(config_[SITE_COLUMN])); + + return true; +} + +void EniroCoreConfig::getConfig(QMap& config) +{ + load(); + + config = config_; +} + +void EniroCoreConfig::loadFromConfig(QMap const& config) +{ + config_[USERNAME_COLUMN] = config[USERNAME_COLUMN]; + config_[PASSWORD_COLUMN] = config[PASSWORD_COLUMN]; + config_[SITE_COLUMN] = config[SITE_COLUMN]; + + loaded_ = true; +} + +void EniroCoreConfig::store() +{ + Settings::instance()->set(USERNAME_COLUMN, config_[USERNAME_COLUMN]); + Settings::instance()->set(PASSWORD_COLUMN, config_[PASSWORD_COLUMN]); + Settings::instance()->set(SITE_COLUMN, config_[SITE_COLUMN]); +} + +bool EniroCoreConfig::readyToSearch() +{ + load(); + + return !config_[USERNAME_COLUMN].isEmpty() && !config_[PASSWORD_COLUMN].isEmpty(); +} + +void EniroCoreConfig::setUsername(QString const& username) +{ + config_[USERNAME_COLUMN] = username; +} + +void EniroCoreConfig::setPassword(QString const& password) +{ + config_[PASSWORD_COLUMN] = password; +} + +void EniroCoreConfig::setSite(QString const& site) +{ + config_[SITE_COLUMN] = site; +} + +QString EniroCoreConfig::getUsername() const +{ + return config_[USERNAME_COLUMN]; +} + +QString EniroCoreConfig::getPassword() const +{ + return config_[PASSWORD_COLUMN]; +} + +QString EniroCoreConfig::getSite() const +{ + return config_[SITE_COLUMN]; +} + +void EniroCoreConfig::load() +{ + if(loaded_) + { + return; + } + + Settings::instance()->startEdit(); + + config_[USERNAME_COLUMN] = Settings::instance()->get(USERNAME_COLUMN); + config_[PASSWORD_COLUMN] = Settings::instance()->get(PASSWORD_COLUMN); + config_[SITE_COLUMN] = Settings::instance()->get(SITE_COLUMN); + + Settings::instance()->endEdit(); + + loaded_ = true; +} diff --git a/src/common/enirocoreconfig.h b/src/common/enirocoreconfig.h new file mode 100644 index 0000000..df4ce77 --- /dev/null +++ b/src/common/enirocoreconfig.h @@ -0,0 +1,52 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#ifndef ENIROCORECONFIG_H +#define ENIROCORECONFIG_H + +#include +#include +#include "sourcecoreconfig.h" + +class EniroCoreConfig : public SourceCoreConfig +{ + +public: + EniroCoreConfig(); + virtual ~EniroCoreConfig(); + virtual bool apply(Source* source); + void store(); + virtual void getConfig(QMap& config); + virtual void loadFromConfig(QMap const& config); + virtual bool readyToSearch(); + +protected: + void setUsername(QString const& username); + void setPassword(QString const& password); + void setSite(QString const& site); + void load(); + QString getUsername() const; + QString getPassword() const; + QString getSite() const; + +private: + QMap config_; + bool loaded_; +}; + +#endif diff --git a/src/common/mobil1881.cpp b/src/common/mobil1881.cpp new file mode 100644 index 0000000..ba1907a --- /dev/null +++ b/src/common/mobil1881.cpp @@ -0,0 +1,283 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#include +#include "mobil1881.h" + + +Mobil1881::Mobil1881(QObject* parent): Source(parent) +{ +} + +Mobil1881::~Mobil1881() +{ + abort(); +} + +void Mobil1881::abort() +{ + Source::abort(); + + for(int i = 0; i < pendingSearches_.size(); i++) + { + delete pendingSearches_[i]; + pendingSearches_[i] = 0; + } + + pendingSearches_.clear(); + +} + +void Mobil1881::search(Source::SearchDetails const& details) +{ + resetTimeout(); + + int id = sendQuery(details, 1); + + SearchData* newData = new SearchData; + newData->details = details; + newData->searchIds.insert(id); + newData->currentPage = 1; + + pendingSearches_.push_back(newData); +} + +void Mobil1881::handleHttpData(int id, QString const& data) +{ + for(int i = 0; i < pendingSearches_.size(); i++) + { + if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) != + pendingSearches_.at(i)->searchIds.end()) + { + + addNumbers(pendingSearches_.at(i), data, i); + break; + } + } +} + +void Mobil1881::handleHttpError(int id) +{ + for(int i = 0; i < pendingSearches_.size(); i++) + { + if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) != + pendingSearches_.at(i)->searchIds.end()) + { + + setError(Source::CONNECTION_FAILURE, http_.errorString()); + emitRequestFinished(pendingSearches_.at(i), true, i); + break; + } + } +} + +void Mobil1881::addNumbers(SearchData* searchData, + QString const& data, + int index) +{ + if(data.indexOf("Last ned vCard") > 0) + { + addOnlyNumber(searchData, data, index); + return; + } + + int pos = 0; + QRegExp rx("(.*)"); + QRegExp name("
(.*)
"); + QRegExp address("
(.*),
(.*)
"); + QRegExp number("
(.*)
"); + rx.setMinimal(true); + name.setMinimal(true); + address.setMinimal(true); + number.setMinimal(true); + + int maxResults = getMaxResults(); + + while((pos = rx.indexIn(data, pos)) != -1) + { + pos += rx.matchedLength(); + + if(searchData->results.size() >= maxResults) + { + break; + } + + QString part = rx.cap(1); + Source::Result result; + QString nameStr; + QString numberStr; + QString streetStr; + QString cityStr; + + if(name.indexIn(part) != -1) + { + nameStr = name.cap(1); + } + + if(address.indexIn(part) != -1) + { + streetStr = address.cap(1); + cityStr = address.cap(2); + } + + if(number.indexIn(part) != -1) + { + numberStr = number.cap(1); + } + + if(formatResult(nameStr, numberStr, streetStr, + cityStr, result)) + { + emit resultAvailable(result, searchData->details); + searchData->results.push_back(result); + } + + } + + if(searchData->results.size() >= maxResults) + { + emitRequestFinished(searchData, false, index); + } + else + { + + if(data.indexOf("Neste") > 0) + { + searchData->currentPage++; + int id = sendQuery(searchData->details, searchData->currentPage); + searchData->searchIds.insert(id); + } + else + { + emitRequestFinished(searchData, false, index); + } + } + +} + +void Mobil1881::addOnlyNumber(SearchData* searchData, + QString const& data, + int index) +{ + QRegExp name("
(.*)
"); + QRegExp number("class=\"srlttxt\">(.*)"); + QRegExp address("class=\"srlttxt\">(.*),
(.*)
"); + name.setMinimal(true); + number.setMinimal(true); + address.setMinimal(true); + + Source::Result result; + + QString nameStr; + QString numberStr; + QString streetStr; + QString cityStr; + + if(name.indexIn(data) != -1) + { + nameStr = name.cap(1); + } + + if(number.indexIn(data) != -1) + { + numberStr = number.cap(1); + } + + if(address.indexIn(data) != -1) + { + streetStr = address.cap(1); + cityStr = address.cap(2); + } + + if(formatResult(nameStr, numberStr, streetStr, + cityStr, result)) + { + searchData->results.push_back(result); + emit resultAvailable(result, searchData->details); + } + + emitRequestFinished(searchData, false, index); +} + +bool Mobil1881::formatResult(QString& name, QString& number, + QString& street, QString& city, + Source::Result& result) +{ + name = stripTags(name); + result.name = name.trimmed(); + number = stripTags(number); + number = cleanUpNumber(number); + result.number = number.trimmed(); + street = stripTags(street); + city = stripTags(city); + result.street = street.trimmed(); + result.city = city.trimmed(); + + if(!result.name.isEmpty() && !result.number.isEmpty()) + { + return true; + } + + return false; +} + +void Mobil1881::emitRequestFinished(SearchData* data, + bool error, int index) +{ + QVector results = data->results; + Source::SearchDetails details = data->details; + + emit requestFinished(results, details, error); + delete pendingSearches_[index]; + pendingSearches_[index] = 0; + pendingSearches_.removeAt(index); +} + +int Mobil1881::sendQuery(Source::SearchDetails const& details, + int page) +{ + QUrl url("http://wap.1881.no/"); + url.addQueryItem("i", "4854"); + url.addQueryItem("showonly", "1"); + QString query = details.query; + + if(!details.location.isEmpty()) + { + query += details.location; + } + + url.addQueryItem("s", query); + if(details.type == Source::YELLOW_PAGES) + { + url.addQueryItem("t", "c"); + } + else + { + url.addQueryItem("t", "p"); + } + + if(page > 1) + { + url.addQueryItem("p", QString::number(page)); + } + + fixUrl(url); + + http_.setHost(url.host(), url.port(80)); + return http_.get(url.encodedPath() + '?' + url.encodedQuery()); +} diff --git a/src/common/mobil1881.h b/src/common/mobil1881.h new file mode 100644 index 0000000..1444e32 --- /dev/null +++ b/src/common/mobil1881.h @@ -0,0 +1,40 @@ +#ifndef MOBIL1881_H +#define MOBIL1881_H + +#include +#include +#include +#include "source.h" + +class Mobil1881 : public Source +{ +public: + Mobil1881(QObject* parent = 0); + ~Mobil1881(); + virtual void abort(); + virtual void search(Source::SearchDetails const& details); + +private: + + struct SearchData + { + QSet searchIds; + Source::SearchDetails details; + QVector results; + int currentPage; + }; + + void addNumbers(SearchData* searchData, QString const& data, int index); + void addOnlyNumber(SearchData* searchData, QString const& data, int index); + bool formatResult(QString& name, QString& number, QString& street, QString& city, Source::Result& result); + void emitRequestFinished(SearchData* data, bool error, int index); + int sendQuery(Source::SearchDetails const& details, int page = 1); + + QList pendingSearches_; + + Q_DISABLE_COPY(Mobil1881); + virtual void handleHttpData(int id, QString const& data); + virtual void handleHttpError(int id); +}; + +#endif // MOBIL1881_H diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 956173e..89c10cd 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -19,10 +19,27 @@ #include #include #include +#include +#include #include #include "settings.h" #include "db.h" +namespace +{ + static int const LANGUAGE_COUNT = 2; + + static QString const LANGUAGE_NAMES[LANGUAGE_COUNT] = { + "English", + "Suomi" + }; + + static QString const LANGUAGE_IDS[LANGUAGE_COUNT] = { + "en_US", + "fi_FI" + }; +} + Settings* Settings::instance_ = 0; Settings* Settings::instance() @@ -35,23 +52,59 @@ Settings* Settings::instance() return instance_; } +void Settings::getLanguages(QList& languages) +{ + for(int i = 0; i < LANGUAGE_COUNT; i++) + { + Language lang; + lang.name = LANGUAGE_NAMES[i]; + lang.id = LANGUAGE_IDS[i]; + languages.push_back(lang); + } +} + +void Settings::loadLanguage(QApplication& app) +{ + QString language = get("language"); + + if(language.isEmpty()) + { + language = QLocale::system().name(); + } + + QTranslator* translator = new QTranslator(&app); + translator->load(":/translations/" + language); + app.installTranslator(translator); +} + void Settings::close() { delete instance_; instance_ = 0; } -bool Settings::set(QString const& name, QString const& value) +void Settings::startEdit() { - bool connected = DB::connected(); + if(!editing_ && !DB::connected()) + { + editing_ = DB::connect(); + } +} - if(!connected) +void Settings::endEdit() +{ + if(editing_) { - if(!DB::connect()) - { - return false; - } + DB::disconnect(); + editing_ = false; } +} + +bool Settings::set(QString const& name, QString const& value) +{ + bool close = !editing_; + + startEdit(); QSqlQuery deleteQuery; deleteQuery.prepare("DELETE FROM settings WHERE name = :name"); @@ -65,9 +118,9 @@ bool Settings::set(QString const& name, QString const& value) bool returnValue = query.exec(); - if(!connected) + if(close) { - DB::disconnect(); + endEdit(); } return returnValue; @@ -77,15 +130,9 @@ QString Settings::get(QString const& name) { QString result = ""; - bool connected = DB::connected(); + bool close = !editing_; - if(!connected) - { - if(!DB::connect()) - { - return result; - } - } + startEdit(); QSqlQuery query; @@ -101,9 +148,9 @@ QString Settings::get(QString const& name) result = getDefaultValue(name); } - if(!connected) + if(close) { - DB::disconnect(); + endEdit(); } return result; @@ -137,23 +184,17 @@ QString Settings::getDefaultValue(QString const& name) bool Settings::reset() { - bool connected = DB::connected(); + bool close = !editing_; - if(!connected) - { - if(!DB::connect()) - { - return false; - } - } + startEdit(); QSqlQuery query; bool ret = query.exec("DELETE FROM settings"); - if(!connected) + if(close) { - DB::disconnect(); + endEdit(); } return ret; @@ -244,6 +285,11 @@ Settings::ConnectionType Settings::getConnectionType() return ALWAYS_ASK; } -Settings::Settings() +Settings::Settings(): editing_(false) +{ +} + +Settings::~Settings() { + DB::removeDatabase(); } diff --git a/src/common/settings.h b/src/common/settings.h index 92e6e3d..c732299 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -20,7 +20,8 @@ #define SETTINGS_H #include -#include +#include +#include class Settings; @@ -32,8 +33,19 @@ public: enum ConnectionType {ALWAYS_ASK, WLAN, GPRS, ANY}; + struct Language + { + QString name; + QString id; + }; + static Settings* instance(); + static void getLanguages(QList& languages); + ~Settings(); static void close(); + void startEdit(); + void endEdit(); + void loadLanguage(QApplication& app); bool set(QString const& name, QString const& value); QString get(QString const& name); QString getDefaultValue(QString const& name); @@ -43,6 +55,7 @@ public: private: Settings(); static Settings* instance_; + bool editing_; }; diff --git a/src/common/source.cpp b/src/common/source.cpp new file mode 100644 index 0000000..4fc513e --- /dev/null +++ b/src/common/source.cpp @@ -0,0 +1,229 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#include +#include "source.h" +#include "eniro.h" +#include "mobil1881.h" + +namespace +{ + static const QString SOURCE_NAMES[Source::SOURCE_COUNT] = + { + "Eniro (FI, SE, DK)", + "1881 Mobil (NO)" + }; + + static const QString SOURCE_IDS[Source::SOURCE_COUNT] = + { + "eniro", + "1881mobil" + }; + +} + +// Regexp used to remove everything except numbers from string +QRegExp Source::numberCleaner_ = QRegExp("([^0-9]+)"); + +// Removes html tags from string +QRegExp Source::tagStripper_ = QRegExp("<([^>]+)>"); + +Source* Source::getSource(Source::SourceId id, QObject* parent) +{ + switch(id) + { + case ENIRO: + return new Eniro(parent); + break; + case MOBIL1881: + return new Mobil1881(parent); + break; + default: + qDebug() << "Unknown source:" << id; + } + + return 0; +} + +Source::SourceId Source::stringToId(QString const& str) +{ + for(int i = 0; i < SOURCE_COUNT; i++) + { + if(SOURCE_IDS[i] == str || SOURCE_NAMES[i] == str) + { + return static_cast(i); + } + } + + return ENIRO; +} + +void Source::getSources(QList& list) +{ + for(int i = 0; i < SOURCE_COUNT; i++) + { + SourceDetails details; + details.type = static_cast(i); + details.name = SOURCE_NAMES[i]; + details.id = SOURCE_IDS[i]; + list.push_back(details); + } +} + +Source::Source(QObject* parent): QObject(parent), +maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), findNumber_(false), +error_(NO_ERROR), loggedIn_(false) +{ + connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool))); +} + +Source::~Source() +{ + abort(); +} + +void Source::abort() +{ + http_.abort(); +} + +void Source::setMaxResults(unsigned int results) +{ + maxResults_ = results; +} + +unsigned int Source::getMaxResults() const +{ + return maxResults_; +} + +void Source::setTimeout(unsigned int timeout) +{ + timeout_ = timeout; + resetTimeout(); +} + +unsigned int Source::getTimeout() const +{ + return timeout_; +} + +void Source::resetTimeout() +{ + if(timerId_) + { + killTimer(timerId_); + } + if(timeout_) + { + timerId_ = startTimer(timeout_); + } +} + +void Source::timerEvent(QTimerEvent* t) +{ + Q_UNUSED(t); +} + +void Source::setFindNumber(bool value) +{ + findNumber_ = value; +} + +bool Source::getFindNumber() const +{ + return findNumber_; +} + +Source::Error Source::error() const +{ + return error_; +} + +const QString& Source::errorString() const +{ + return errorString_; +} + +void Source::setError(Source::Error error, QString const& errorString) +{ + error_ = error; + errorString_ = errorString; +} + +void Source::httpReady(int id, bool error) +{ + + if(error) + { + if(http_.error() == QHttp::Aborted) + { + return; + } + + qDebug() << "Error: " << http_.errorString(); + handleHttpError(id); + } + else + { + QString result(http_.readAll()); + handleHttpData(id, result); + } +} + +QString Source::ucFirst(QString& str) +{ + if (str.size() < 1) { + return ""; + } + + QStringList tokens = str.split(" "); + QList::iterator tokItr; + + for (tokItr = tokens.begin(); tokItr != tokens.end(); ++tokItr) + { + (*tokItr) = (*tokItr).at(0).toUpper() + (*tokItr).mid(1); + } + + return tokens.join(" "); +} + +QString& Source::cleanUpNumber(QString& number) +{ + return number.replace(numberCleaner_, ""); +} + +QString& Source::stripTags(QString& string) +{ + return string.replace(tagStripper_, ""); +} + +void Source::fixUrl(QUrl& url) +{ + QByteArray path = url.encodedQuery().replace('+', "%2B"); + url.setEncodedQuery(path); +} + +Source::SearchDetails::SearchDetails(QString const& q, + QString const& loc, + SearchType t) +{ + query = q; + location = loc; + type = t; +} diff --git a/src/common/source.h b/src/common/source.h new file mode 100644 index 0000000..34cd356 --- /dev/null +++ b/src/common/source.h @@ -0,0 +1,122 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#ifndef SOURCE_H +#define SOURCE_H + +#include +#include +#include +#include +#include + +class Source : public QObject +{ + + Q_OBJECT + +public: + + struct Result + { + QString name; + QString street; + QString city; + QString number; + }; + + enum SearchType {YELLOW_PAGES, PERSONS}; + + struct SearchDetails + { + QString query; + QString location; + SearchType type; + SearchDetails(QString const& query = "", + QString const& location = "", + SearchType type = PERSONS); + }; + + + enum Error {NO_ERROR, CONNECTION_FAILURE, INVALID_LOGIN, TIMEOUT}; + enum SourceId {ENIRO, MOBIL1881}; + static int const SOURCE_COUNT = 2; + + struct SourceDetails + { + SourceId type; + QString name; + QString id; + }; + + static unsigned int const DEFAULT_MAX_RESULTS = 30; + + static Source* getSource(SourceId id, QObject* parent = 0); + static void getSources(QList& list); + static SourceId stringToId(QString const& str); + Source(QObject* parent = 0); + virtual ~Source(); + static Source* getSource(); + virtual void abort(); + virtual void search(SearchDetails const& details) = 0; + void setMaxResults(unsigned int results); + unsigned int getMaxResults() const; + void setTimeout(unsigned int ms); + unsigned int getTimeout() const; + void resetTimeout(); + void setFindNumber(bool value); + bool getFindNumber() const; + Error error() const; + const QString& errorString() const; + +signals: + void resultAvailable(Source::Result const& result, Source::SearchDetails const& details); + void requestFinished(QVector const& results, Source::SearchDetails const& details, bool error); + +protected: + void setError(Error error, QString const& errorString = ""); + virtual void timerEvent(QTimerEvent *te); + QString ucFirst(QString& string); + QString& cleanUpNumber(QString& number); + QString& stripTags(QString& string); + void fixUrl(QUrl& url); + QHttp http_; + +private slots: + void httpReady(int id, bool error); + +private: + Q_DISABLE_COPY(Source); + virtual void handleHttpData(int id, QString const& data) = 0; + virtual void handleHttpError(int id) = 0; + unsigned int maxResults_; + unsigned int timeout_; + int timerId_; + bool findNumber_; + Error error_; + QString errorString_; + QString username_; + QString password_; + bool loggedIn_; + + static QRegExp numberCleaner_; + static QRegExp tagStripper_; + +}; + +#endif diff --git a/src/common/sourcecoreconfig.cpp b/src/common/sourcecoreconfig.cpp new file mode 100644 index 0000000..79c88c6 --- /dev/null +++ b/src/common/sourcecoreconfig.cpp @@ -0,0 +1,47 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#include +#include "sourcecoreconfig.h" +#include "enirocoreconfig.h" +#include "emptycoreconfig.h" + +SourceCoreConfig::SourceCoreConfig() +{ +} + +SourceCoreConfig::~SourceCoreConfig() +{ +} + +SourceCoreConfig* SourceCoreConfig::getCoreConfig(Source::SourceId id) +{ + switch(id) + { + case Source::ENIRO: + return new EniroCoreConfig(); + break; + case Source::MOBIL1881: + return new EmptyCoreConfig(); // No configuration + break; + default: + qDebug() << "Unknown source id: " << id; + } + + return 0; +} diff --git a/src/common/sourcecoreconfig.h b/src/common/sourcecoreconfig.h new file mode 100644 index 0000000..4e18b93 --- /dev/null +++ b/src/common/sourcecoreconfig.h @@ -0,0 +1,41 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#ifndef SOURCECORECONFIG_H +#define SOURCECORECONFIG_H + +#include +#include +#include "source.h" + +class SourceCoreConfig +{ + +public: + + static SourceCoreConfig* getCoreConfig(Source::SourceId id); + SourceCoreConfig(); + virtual ~SourceCoreConfig(); + virtual bool apply(Source* source) = 0; + virtual void getConfig(QMap& config) = 0; + virtual void loadFromConfig(QMap const& config) = 0; + virtual void store() = 0; + virtual bool readyToSearch() = 0; +}; + +#endif diff --git a/src/common/translations/fi_FI.qm b/src/common/translations/fi_FI.qm index b0be15eb68f7de78dfd864f49a65baa12bc07ad7..8c9f0c6079d81950ca0b458189ac0af69fc847fa 100644 GIT binary patch delta 1571 zcma)6ZA@EL7=CZtTUz=71qv;r-8CbdM1!K>SI9&rvH)2y3=uWPyY|vvTJ9yiw;(^H z2A4Ru#5pfxI2UBu)Fu4bvSy3P7Q+~1dEU4EYU|vKAC=fAiLCQP{@Me9V9HZR{PsGcydQ}lpCbyoiJz7bHB;r#_eADb zsd;Rc*e7FMHPrIR2+@WHYI*7)kxQhuPj;dHbk=(Evevsy?@tXA6)e-CF$+<}W;*)w z5)o&iIlF_%^A;`ssZc8g49@BPkjSEPubsF`#Fuh%426Xw+^rP=2#%P}54hnUH2uZL;Cn~>U`EvG6VEw^zqJ5mm zamI3Y8CMmb<-O&_M5R^y_>@BA+r*EbUjw!s{HJ@6=ed!!zHj-dec*1d=jXpWOk}#4 zwPl92@LVJ6-mtbVOo7`U)~?xapw(i^I{$?kfeGts=`gUpV_m&;9O7)Vm2?Ah{*vvn zF#sw$W7~21cNAW*?R=;nFNoPjhjI3I*^8ymiJVW{wU<#}QfJ>c{WYYo$-ipe1Yo22 z*H2wWe8pj!1aSKeN7?lWwDY7RH1i|~r)nI%H+Nz)=txx|U@mjM)R88#{pD=__&2<; z(|L~503a`GgH6tvPITWn?EGnF2}-@~;@hv`e93j}N+lG#MH7Dh5%xzRvJnU*xk-KI=C|(m_|$+rtrV$Vcabdt2?{Zq@YNU z0c1g3h^rA<5yFx#%5hCl6N0KoB|Q6eSyj@5-czQ{MbwS=vs;HU%0lCuuJ&zeU%#lx zl4jiTm8Q4&Je={Kf0ZjYrcGX-0))!_+!vC%+9&FA$f()iOTSxwk~6{;A>&%$nLs}N z6c``OM=syiCMuDn7?D`|6G3VRxBr}#T$R23UVm9x?f)$+%9RP4aWlBlC=A{XOi~MD z0Az{G#4(*0paN53$nX?# delta 836 zcmXAnYe-XJ7{~wH&Y8QOqf9s5bR92!2uTZ6NW{SMf{G{_Wl3OplIhsYO+_dN(v5^} z$Gjz>XesT3;y?@~#3pHwAA&Nnq(bn6qNqto%zAVO-t&9U`##VA{NLw!XU|S#J_=XY z0m^y6xcfN9>fyG*=A{B5zrc3Q0+s;SX_0oDkpQX+R+-upcX2-}uVHFC7xB;UJ6aiLoZeL*G(1Qb~rr9k+;Vm*iAnfFX= z;ts`aF{u?}fNFy2?~Ed&#mtcZG8w2~hKsw1{mjlD>4mx22(!Th7?#*BuLFqaX1k{L z$zc)u>>dU3-9Z`)S#KLvK%K$Pf9(O}Z-b1;R)$VyQQk-8dH(`cYE?P^QbCmqR|bq^ zC^TIecsEFuIipJMAOoTGsy# zM2M8ho6HbUgGzcIhl^;ZaX<$ur5j-usn%2xpI`255FDadCAgh}OLPm_4qLrbusf@5 z4xvhPSK92pUDK%C_tP9EOR=H=BI&qE#YS2p%><$ijc9;db#PymG<4;n-R^7>>ME;6 z>88abZCe7q)R+;O + EmptyGuiConfig + + + Selected phonebook has no settings to configure. + Valitussa puhelinluettelossa ei ole muokattavia asetuksia. + + + + EniroGuiConfig + + + Eniro username + Eniro-tunnus + + + + Eniro password + Eniro-salasana + + + + Eniro site + Eniro-sivusto + + + + Finnish + Suomi + + + + Swedish + Ruotsi + + + + Danish + Tanska + + + MainWindow - + Jenirok Jenirok - - + + Stop daemon Pysäytä - - + + Start daemon Käynnistä - + Search Hae - + Settings Asetukset - + About Tietoa - + Daemon was successfully stopped. Palvelu pysäytettiin onnistuneesti. - + Unable to stop daemon. Palvelun pysäyttäminen ei onnistunut. - + Unable to start daemon Palvelun käynnistäminen ei onnistunut - + Daemon cannot be started because it's not allowed to connect to the Internet. You have to either allow automatic Internet connection in Jenirok settings or in global Maemo settings. Palvelua ei voida käynnistää, koska sillä ei ole oikeutta yhdistää Internetiin. Sinun täytyy sallia automaattinen yhteyden muodostaminen joko Jenirokin asetuksista tai Maemon omista Internet-asetuksista. - + Open settings Avaa asetukset - + Close Sulje - + Daemon was successfully started. Palvelu käynnistettiin onnistuneesti. - + Unable to start daemon. Palvelun käynnistäminen ei onnistunut. - + Error Virhe - + Info Info - - You need to set Eniro login details in settings before using this feature. - Sinun täytyy asettaa Eniro-tunnus ja -salasana asetuksista ennen kuin voit käyttää tätä ominaisuutta. + + You need to set login details or other options in settings before using this feature. + Sinun täytyy asettaa kirjautumistiedot tai muita asetuksia ennen kuin voit käyttää tätä ominaisuutta. ResultWindow - + Search results Hakutulokset - + Connection to server failed Palvelimelle yhdistäminen epäonnistui - + Invalid login details Virheellinen tunnus tai salasana - + Request timed out Pyyntö aikakatkaistiin - + Searching failed: Haku epäonnistui: - + Error Virhe - + No results found Ei hakutuloksia @@ -311,14 +352,14 @@ - Yellow pages - Keltaiset sivut + Companies + Yritykset Settings - + fi fi @@ -326,92 +367,88 @@ SettingsDialog - + Settings Asetukset - - Eniro username - Eniro-tunnus - - - - Eniro password - Eniro-salasana - - - + Cache size (numbers) Välimuistin koko (numeroa) - + Clear Tyhjennä - - Eniro site - Eniro-sivusto + + Language + Kieli - - Finnish - Suomi - - - - Swedish - Ruotsi + + Automatic + Automaattinen - - Danish - Tanska + + Phonebook + Puhelinluettelo - + Autostart Käynnistä automaattisesti - + Enabled Kyllä - + Disabled Ei - + Connect automatically on Yhdistä automaattisesti - + Save Tallenna - + General Yleiset - + Daemon Taustaprosessi - + + + Phonebook settings + Puhelinluettelo + + + Restarting daemon... Käynnistetään palvelu uudelleen... + + + You need to restart Jenirok for language change to take effect. + Jenirok täytyy käynnistää uudelleen, jotta uudet kieliasetukset tulevat voimaan. + - + %n number(s) were deleted from cache Poistettiin %n numero välimuistista diff --git a/src/daemon/calllistener.cpp b/src/daemon/calllistener.cpp index 4307347..c44e5ce 100644 --- a/src/daemon/calllistener.cpp +++ b/src/daemon/calllistener.cpp @@ -23,6 +23,7 @@ #include "cache.h" #include "contactmanager.h" #include "connectionmanager.h" +#include "sourcecoreconfig.h" #include "db.h" namespace @@ -38,16 +39,15 @@ namespace QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus(); -CallListener::CallListener(): eniro_(0), +CallListener::CallListener(): source_(0), closeConnection_(false), initialized_(false), box_(0), label_(0), -retries_(-1), site_(Eniro::FI), timer_(0) +retries_(-1), timer_(0) { } CallListener::~CallListener() { end(); - DB::removeDatabase(); } bool CallListener::begin() @@ -58,6 +58,11 @@ bool CallListener::begin() return false; } + sourceId_ = Source::stringToId(Settings::instance()->get("source")); + SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_); + config->getConfig(sourceConfig_); + delete config; + systemBus_.connect(CALL_SERVICE_NAME, CALL_SERVICE_PATH, CALL_SERVICE_INTERFACE, @@ -72,7 +77,6 @@ bool CallListener::begin() this, SLOT(callTerminate())); - site_ = Eniro::stringToSite(Settings::instance()->get("site")); qDebug() << "Starting..."; @@ -97,16 +101,17 @@ void CallListener::end() SLOT(callTerminate())); searchClose(); + sourceConfig_.clear(); } -void CallListener::search(Eniro::SearchDetails const& details) +void CallListener::search(Source::SearchDetails const& details) { qDebug() << "Search called"; searchInit(); - Eniro::Result result; + Source::Result result; if(Cache::instance().findItem(details.query, result)) { @@ -132,13 +137,13 @@ void CallListener::search(Eniro::SearchDetails const& details) qDebug() << "Starting to search..."; - eniro_->search(details); + source_->search(details); } } -void CallListener::requestFinished(QVector const& results, - Eniro::SearchDetails const& details, +void CallListener::requestFinished(QVector const& results, + Source::SearchDetails const& details, bool error) { // If box is not visible, the call must have been terminated already @@ -151,27 +156,27 @@ void CallListener::requestFinished(QVector const& results, if(error) { - qDebug() << "Error: " << eniro_->errorString(); + qDebug() << "Error: " << source_->errorString(); if(retries_ < SEARCH_RETRIES && retries_ >= 0) { retries_++; - eniro_->search(Eniro::SearchDetails(currentSearch_)); + source_->search(Source::SearchDetails(currentSearch_)); return; } else { timedMessage_ = ""; QString errorString; - Eniro::Error error = eniro_->error(); + Source::Error error = source_->error(); switch(error) { - case Eniro::TIMEOUT: + case Source::TIMEOUT: errorString = tr("Request timed out"); break; default: - errorString = eniro_->errorString(); + errorString = source_->errorString(); break; } @@ -192,7 +197,7 @@ void CallListener::requestFinished(QVector const& results, message = createResult(results.at(0).name, results.at(0).street, results.at(0).city); showResult(message); - Eniro::Result result = results.at(0); + Source::Result result = results.at(0); result.number = details.query; Cache::instance().addItem(result); } @@ -262,7 +267,7 @@ void CallListener::incomingCall(QDBusObjectPath path, QString number) this, SLOT(callTerminate())); - search(Eniro::SearchDetails(number)); + search(Source::SearchDetails(number)); } else { @@ -315,15 +320,19 @@ void CallListener::searchInit() return; } - eniro_ = new Eniro(site_); - eniro_->setMaxResults(1); - eniro_->setFindNumber(false); - eniro_->setTimeout(REQUEST_TIMEOUT); - - connect(eniro_, SIGNAL(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool)), - this, SLOT(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool))); + source_ = Source::getSource(sourceId_); + SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_); + config->loadFromConfig(sourceConfig_); + config->apply(source_); + delete config; + source_->setMaxResults(1); + source_->setFindNumber(false); + source_->setTimeout(REQUEST_TIMEOUT); + + connect(source_, SIGNAL(requestFinished(QVector const&, + Source::SearchDetails const&, bool)), + this, SLOT(requestFinished(QVector const&, + Source::SearchDetails const&, bool))); box_ = new InformationBox; label_ = new QLabel("", box_); label_->setMargin(8); @@ -338,16 +347,16 @@ void CallListener::searchClose() qDebug() << "Closing search..."; - if(eniro_) + if(source_) { - disconnect(eniro_, SIGNAL(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool)), - this, SLOT(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool))); + disconnect(source_, SIGNAL(requestFinished(QVector const&, + Source::SearchDetails const&, bool)), + this, SLOT(requestFinished(QVector const&, + Source::SearchDetails const&, bool))); } - delete eniro_; - eniro_ = 0; + delete source_; + source_ = 0; delete box_; box_ = 0; label_ = 0; diff --git a/src/daemon/calllistener.h b/src/daemon/calllistener.h index 0cd70ed..d842f81 100644 --- a/src/daemon/calllistener.h +++ b/src/daemon/calllistener.h @@ -25,7 +25,7 @@ #include #include #include "informationbox.h" -#include "eniro.h" +#include "source.h" class CallListener: public QObject { @@ -48,7 +48,7 @@ protected: virtual void timerEvent(QTimerEvent* event); private slots: - void requestFinished(QVector const& results, Eniro::SearchDetails const& details, bool error); + void requestFinished(QVector const& results, Source::SearchDetails const& details, bool error); void incomingCall(QDBusObjectPath path, QString numbe); void callTerminate(); void showTimedMessage(); @@ -56,7 +56,7 @@ private slots: private: Q_DISABLE_COPY(CallListener); - void search(Eniro::SearchDetails const& details); + void search(Source::SearchDetails const& details); void showResult(QString const& text); void showDelayedResult(QString const& text, int delay); void searchInit(); @@ -66,7 +66,9 @@ private: void sleep(int ms); QString createResult(QString const& name, QString const& street, QString const& city); QString timedMessage_; - Eniro* eniro_; + Source* source_; + Source::SourceId sourceId_; + QMap sourceConfig_; bool closeConnection_; bool initialized_; InformationBox* box_; @@ -74,7 +76,6 @@ private: static QDBusConnection systemBus_; int retries_; QString currentSearch_; - Eniro::Site site_; int timer_; }; diff --git a/src/daemon/daemon.pro b/src/daemon/daemon.pro index 07edb06..26485e3 100644 --- a/src/daemon/daemon.pro +++ b/src/daemon/daemon.pro @@ -2,8 +2,33 @@ QT += network sql maemo5 TARGET = jenirokd CONFIG += qdbus TEMPLATE = app -SOURCES += main.cpp calllistener.cpp informationbox.cpp ../common/eniro.cpp ../common/contactmanager.cpp ../common/db.cpp ../common/settings.cpp ../common/connectionmanager.cpp ../common/cache.cpp -HEADERS += calllistener.h informationbox.h ../common/eniro.h ../common/contactmanager.h ../common/db.h ../common/settings.h ../common/connectionmanager.h ../common/cache.h +SOURCES += main.cpp \ + calllistener.cpp \ + informationbox.cpp \ + ../common/source.cpp \ + ../common/eniro.cpp \ + ../common/mobil1881.cpp \ + ../common/sourcecoreconfig.cpp \ + ../common/enirocoreconfig.cpp \ + ../common/emptycoreconfig.cpp \ + ../common/contactmanager.cpp \ + ../common/db.cpp \ + ../common/settings.cpp \ + ../common/connectionmanager.cpp \ + ../common/cache.cpp +HEADERS += calllistener.h \ + informationbox.h \ + ../common/source.h \ + ../common/eniro.h \ + ../common/mobil1881.h \ + ../common/enirocoreconfig.h \ + ../common/sourcecoreconfig.h \ + ../common/emptycoreconfig.h \ + ../common/contactmanager.h \ + ../common/db.h \ + ../common/settings.h \ + ../common/connectionmanager.h \ + ../common/cache.h TRANSLATIONS = ../common/translations/fi_FI.ts RESOURCES = ../common/translations.grc INCLUDEPATH += ../common diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index cc9b642..f011649 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -16,9 +16,6 @@ * */ -#include -#include -#include #include #include #include "calllistener.h" @@ -38,9 +35,7 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); - QTranslator translator; - translator.load(":/translations/" + QLocale::system().name()); - app.installTranslator(&translator); + Settings::instance()->loadLanguage(app); CallListener listener; diff --git a/src/gui/buttonselector.cpp b/src/gui/buttonselector.cpp index 6ba49da..9c7ff39 100644 --- a/src/gui/buttonselector.cpp +++ b/src/gui/buttonselector.cpp @@ -24,6 +24,7 @@ QMaemo5ValueButton(text, parent), selector_(0), model_(0) { setValueLayout(QMaemo5ValueButton::ValueBesideText); selector_ = new QMaemo5ListPickSelector(this); + connect(selector_, SIGNAL(selected(QString const&)), this, SLOT(onSelected(QString const&))); model_ = new QStandardItemModel(0, 1); selector_->setModel(model_); setPickSelector(selector_); @@ -114,7 +115,7 @@ QVariant ButtonSelector::value() const bool ButtonSelector::changeItem(unsigned int index, QString const& text) { - changeItem(index, text, QVariant(text)); + return changeItem(index, text, QVariant(text)); } bool ButtonSelector::changeItem(unsigned int index, @@ -138,3 +139,8 @@ bool ButtonSelector::removeItem(unsigned int index) { return model_->removeRow(index); } + +void ButtonSelector::onSelected(QString const& text) +{ + emit selected(currentIndex(), text, value()); +} diff --git a/src/gui/buttonselector.h b/src/gui/buttonselector.h index ae494a2..d5c6980 100644 --- a/src/gui/buttonselector.h +++ b/src/gui/buttonselector.h @@ -46,6 +46,12 @@ public: QString text() const; QVariant value() const; +signals: + void selected(unsigned int index, QString const& text, QVariant const& value); + +private slots: + void onSelected(QString const& text); + private: QMaemo5ListPickSelector* selector_; QStandardItemModel* model_; diff --git a/src/gui/detailwindow.cpp b/src/gui/detailwindow.cpp index d34d282..1e5b6c4 100644 --- a/src/gui/detailwindow.cpp +++ b/src/gui/detailwindow.cpp @@ -74,7 +74,7 @@ DetailWindow::DetailWindow(QWidget* parent): QMainWindow(parent), addDialog_(0) setCentralWidget(area_); } -void DetailWindow::loadData(Eniro::Result const& details) +void DetailWindow::loadData(Source::Result const& details) { setWindowTitle(details.name); nameButton_->setValueText(details.name); diff --git a/src/gui/detailwindow.h b/src/gui/detailwindow.h index 73f8232..a267e6b 100644 --- a/src/gui/detailwindow.h +++ b/src/gui/detailwindow.h @@ -36,7 +36,7 @@ public: DetailWindow(QWidget* parent = 0); public slots: - void loadData(Eniro::Result const& details); + void loadData(Source::Result const& details); private slots: void makeCall(); diff --git a/src/gui/emptyguiconfig.cpp b/src/gui/emptyguiconfig.cpp new file mode 100644 index 0000000..cec5254 --- /dev/null +++ b/src/gui/emptyguiconfig.cpp @@ -0,0 +1,40 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#include +#include +#include "emptyguiconfig.h" + +EmptyGuiConfig::EmptyGuiConfig(QWidget* parent): +SourceGuiConfig(parent), EmptyCoreConfig() +{ + QVBoxLayout* layout = new QVBoxLayout(this); + QLabel* info = new QLabel(tr("Selected phonebook has no settings to configure.")); + info->setAlignment(Qt::AlignCenter); + info->setWordWrap(true); + layout->addWidget(info); + setLayout(layout); +} + +EmptyGuiConfig::~EmptyGuiConfig() +{ +} + +void EmptyGuiConfig::save() +{ +} diff --git a/src/gui/emptyguiconfig.h b/src/gui/emptyguiconfig.h new file mode 100644 index 0000000..968b4dc --- /dev/null +++ b/src/gui/emptyguiconfig.h @@ -0,0 +1,39 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#ifndef EMPTYGUICONFIG_H +#define EMPTYGUICONFIG_H + +#include "sourceguiconfig.h" +#include "emptycoreconfig.h" + +class EmptyGuiConfig : public SourceGuiConfig, public EmptyCoreConfig +{ + + Q_OBJECT + +public: + EmptyGuiConfig(QWidget* parent = 0); + virtual ~EmptyGuiConfig(); + virtual void save(); + +private: + +}; + +#endif diff --git a/src/gui/eniroguiconfig.cpp b/src/gui/eniroguiconfig.cpp new file mode 100644 index 0000000..126d059 --- /dev/null +++ b/src/gui/eniroguiconfig.cpp @@ -0,0 +1,103 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#include +#include +#include +#include +#include "eniroguiconfig.h" +#include "eniro.h" + +EniroGuiConfig::EniroGuiConfig(QWidget* parent): +SourceGuiConfig(parent), EniroCoreConfig() +{ + load(); + + QVBoxLayout* layout = new QVBoxLayout; + + QHBoxLayout* username = new QHBoxLayout; + QHBoxLayout* password = new QHBoxLayout; + + QLabel* usernameLabel = new QLabel(tr("Eniro username")); + usernameInput_ = new QLineEdit(getUsername()); + + QLabel* passwordLabel = new QLabel(tr("Eniro password")); + passwordInput_ = new QLineEdit(getPassword()); + passwordInput_->setEchoMode(QLineEdit::PasswordEchoOnEdit); + + QMap sites = Eniro::getSites(); + + siteSelector_ = new ButtonSelector(tr("Eniro site"), this); + QString site = getSite(); + int i = 0; + + QMap ::const_iterator it; + for(it = sites.begin(); it != sites.end(); it++) + { + QString name; + + switch(it.key()) + { + case Eniro::FI: + name = tr("Finnish"); + break; + case Eniro::SE: + name = tr("Swedish"); + break; + case Eniro::DK: + name = tr("Danish"); + break; + default: + qDebug() << "Unknown site"; + continue; + + } + siteSelector_->addItem(name, it.value().id); + + if(it.value().id == site) + { + siteSelector_->setCurrentIndex(i); + } + + i++; + } + + username->addWidget(usernameLabel); + username->addWidget(usernameInput_); + password->addWidget(passwordLabel); + password->addWidget(passwordInput_); + + layout->addLayout(username); + layout->addLayout(password); + layout->addWidget(siteSelector_); + + setLayout(layout); +} + +EniroGuiConfig::~EniroGuiConfig() +{ +} + +void EniroGuiConfig::save() +{ + setUsername(usernameInput_->text()); + setPassword(passwordInput_->text()); + setSite(siteSelector_->value().toString()); + + store(); +} diff --git a/src/gui/eniroguiconfig.h b/src/gui/eniroguiconfig.h new file mode 100644 index 0000000..d8cab97 --- /dev/null +++ b/src/gui/eniroguiconfig.h @@ -0,0 +1,44 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#ifndef ENIROGUICONFIG_H +#define ENIROGUICONFIG_H + +#include +#include "buttonselector.h" +#include "enirocoreconfig.h" +#include "sourceguiconfig.h" + +class EniroGuiConfig : public SourceGuiConfig, public EniroCoreConfig +{ + + Q_OBJECT + +public: + EniroGuiConfig(QWidget* parent = 0); + virtual ~EniroGuiConfig(); + virtual void save(); + +private: + QLineEdit* usernameInput_; + QLineEdit* passwordInput_; + ButtonSelector* siteSelector_; + +}; + +#endif diff --git a/src/gui/gui.pro b/src/gui/gui.pro index 4ed5de4..d93bd09 100644 --- a/src/gui/gui.pro +++ b/src/gui/gui.pro @@ -1,8 +1,53 @@ QT += network sql maemo5 TARGET = jenirok TEMPLATE = app -SOURCES += main.cpp mainwindow.cpp searchdialog.cpp resultwindow.cpp detailwindow.cpp settingsdialog.cpp aboutdialog.cpp buttonselector.cpp connectionselector.cpp daemon.cpp ../common/eniro.cpp ../common/contactmanager.cpp ../common/db.cpp ../common/settings.cpp ../common/connectionmanager.cpp ../common/cache.cpp -HEADERS += mainwindow.h searchdialog.h resultwindow.h detailwindow.h settingsdialog.h aboutdialog.h buttonselector.h connectionselector.h daemon.h ../common/eniro.h ../common/contactmanager.h ../common/db.h ../common/settings.h ../common/connectionmanager.h ../common/cache.h +SOURCES += main.cpp \ + mainwindow.cpp \ + searchdialog.cpp \ + resultwindow.cpp \ + detailwindow.cpp \ + settingsdialog.cpp \ + aboutdialog.cpp \ + buttonselector.cpp \ + connectionselector.cpp \ + daemon.cpp \ + ../common/source.cpp \ + ../common/eniro.cpp \ + ../common/mobil1881.cpp \ + ../common/sourcecoreconfig.cpp \ + ../common/emptycoreconfig.cpp \ + sourceguiconfig.cpp \ + ../common/enirocoreconfig.cpp \ + eniroguiconfig.cpp \ + emptyguiconfig.cpp \ + ../common/contactmanager.cpp \ + ../common/db.cpp \ + ../common/settings.cpp \ + ../common/connectionmanager.cpp \ + ../common/cache.cpp +HEADERS += mainwindow.h \ + searchdialog.h \ + resultwindow.h \ + detailwindow.h \ + settingsdialog.h \ + aboutdialog.h \ + buttonselector.h \ + connectionselector.h \ + daemon.h \ + ../common/source.h \ + ../common/eniro.h \ + ../common/mobil1881.h \ + ../common/sourcecoreconfig.h \ + ../common/emptycoreconfig.h \ + sourceguiconfig.h \ + ../common/enirocoreconfig.h \ + eniroguiconfig.h \ + emptyguiconfig.h \ + ../common/contactmanager.h \ + ../common/db.h \ + ../common/settings.h \ + ../common/connectionmanager.h \ + ../common/cache.h TRANSLATIONS = ../common/translations/fi_FI.ts RESOURCES = icons.grc ../common/translations.grc INCLUDEPATH += ../common diff --git a/src/gui/main.cpp b/src/gui/main.cpp index 0bce49a..215c254 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -16,8 +16,6 @@ * */ -#include -#include #include #include #include "mainwindow.h" @@ -25,14 +23,13 @@ #include "resultwindow.h" #include "detailwindow.h" #include "eniro.h" +#include "settings.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); - QTranslator translator; - translator.load(":/translations/" + QLocale::system().name()); - app.installTranslator(&translator); + Settings::instance()->loadLanguage(app); MainWindow window; ResultWindow results(&window); @@ -43,8 +40,8 @@ int main(int argc, char *argv[]) QObject::connect(&window, SIGNAL(search(SearchDialog::SearchDetails&)), &results, SLOT(search(SearchDialog::SearchDetails&))); - QObject::connect(&results, SIGNAL(itemSelected(Eniro::Result const&)), - &details, SLOT(loadData(Eniro::Result const&))); + QObject::connect(&results, SIGNAL(itemSelected(Source::Result const&)), + &details, SLOT(loadData(Source::Result const&))); return app.exec(); } diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 7c0221a..8d75fea 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -32,6 +32,8 @@ #include "daemon.h" #include "settings.h" #include "db.h" +#include "source.h" +#include "sourcecoreconfig.h" namespace { @@ -166,16 +168,17 @@ void MainWindow::toggleDaemon() void MainWindow::openSearch() { - DB::connect(); + Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source")); + SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId); - QString username = Settings::instance()->get("eniro_username"); - QString password = Settings::instance()->get("eniro_password"); + Q_ASSERT(config != 0); - DB::disconnect(); + bool readyToSearch = config->readyToSearch(); + delete config; - if(username.isEmpty() || password.isEmpty()) + if(!readyToSearch) { - QMessageBox::information(this, tr("Info"), tr("You need to set Eniro login details in settings before using this feature.")); + QMessageBox::information(this, tr("Info"), tr("You need to set login details or other options in settings before using this feature.")); return; } diff --git a/src/gui/resultwindow.cpp b/src/gui/resultwindow.cpp index 1a3d954..2640bbe 100644 --- a/src/gui/resultwindow.cpp +++ b/src/gui/resultwindow.cpp @@ -26,9 +26,11 @@ #include "settings.h" #include "db.h" #include "cache.h" +#include "source.h" +#include "sourcecoreconfig.h" ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent), -eniro_(0), list_(0), connectionManager_(0) +source_(0), list_(0), connectionManager_(0) { setAttribute(Qt::WA_Maemo5StackedWindow); setWindowTitle(tr("Search results")); @@ -54,49 +56,44 @@ void ResultWindow::search(SearchDialog::SearchDetails& details) list_->clear(); } - DB::connect(); + Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source")); - Eniro::Site selectedSite = Eniro::stringToSite(Settings::instance()->get("site")); - - if(!eniro_) + if(!source_) { - eniro_ = new Eniro(selectedSite, this); - eniro_->setTimeout(REQUEST_TIMEOUT); - - connect(eniro_, SIGNAL(resultAvailable(Eniro::Result const&, - Eniro::SearchDetails const&)), - this, SLOT(resultAvailable(Eniro::Result const&, - Eniro::SearchDetails const&))); - - connect(eniro_, SIGNAL(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool)), - this, SLOT(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool))); + source_ = Source::getSource(sourceId); + Q_ASSERT(source_ != 0); + source_->setTimeout(REQUEST_TIMEOUT); + + connect(source_, SIGNAL(resultAvailable(Source::Result const&, + Source::SearchDetails const&)), + this, SLOT(resultAvailable(Source::Result const&, + Source::SearchDetails const&))); + + connect(source_, SIGNAL(requestFinished(QVector const&, + Source::SearchDetails const&, bool)), + this, SLOT(requestFinished(QVector const&, + Source::SearchDetails const&, bool))); } - QString username = Settings::instance()->get("eniro_username"); - QString password = Settings::instance()->get("eniro_password"); - - if(!username.isEmpty() && !password.isEmpty()) - { - eniro_->login(username, password); - } + SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId); - eniro_->setSite(selectedSite); + Q_ASSERT(config != 0); - DB::disconnect(); + config->apply(source_); + delete config; - Eniro::SearchType type; + Source::SearchType type; switch(details.type) { case 0: - type = Eniro::PERSONS; + type = Source::PERSONS; break; case 1: - type = Eniro::YELLOW_PAGES; + type = Source::YELLOW_PAGES; break; default: + qDebug() << "Unknown search type: " << details.type; return; } @@ -110,13 +107,13 @@ void ResultWindow::search(SearchDialog::SearchDetails& details) connectionManager_->connect(); - eniro_->abort(); - eniro_->search(Eniro::SearchDetails(details.name, details.location, type)); + source_->abort(); + source_->search(Source::SearchDetails(details.name, details.location, type)); } -void ResultWindow::resultAvailable(Eniro::Result const& result, - Eniro::SearchDetails const& details) +void ResultWindow::resultAvailable(Source::Result const& result, + Source::SearchDetails const& details) { Q_UNUSED(details); @@ -149,8 +146,8 @@ void ResultWindow::resultAvailable(Eniro::Result const& result, list_->addItem(item); } -void ResultWindow::requestFinished(QVector const& results, - Eniro::SearchDetails const& details, +void ResultWindow::requestFinished(QVector const& results, + Source::SearchDetails const& details, bool error) { Q_UNUSED(details); @@ -158,21 +155,21 @@ void ResultWindow::requestFinished(QVector const& results, if(error) { QString errorString; - Eniro::Error error = eniro_->error(); + Source::Error error = source_->error(); switch(error) { - case Eniro::CONNECTION_FAILURE: + case Source::CONNECTION_FAILURE: errorString = tr("Connection to server failed"); break; - case Eniro::INVALID_LOGIN: + case Source::INVALID_LOGIN: errorString = tr("Invalid login details"); break; - case Eniro::TIMEOUT: + case Source::TIMEOUT: errorString = tr("Request timed out"); break; default: - errorString = tr("Searching failed:") + " " + eniro_->errorString(); + errorString = tr("Searching failed:") + " " + source_->errorString(); break; } @@ -194,7 +191,7 @@ void ResultWindow::requestFinished(QVector const& results, void ResultWindow::itemClicked(QListWidgetItem* item) { QMap data = item->data(Qt::UserRole).toMap(); - Eniro::Result details; + Source::Result details; details.name = data["name"].toString(); details.street = data["street"].toString(); details.city = data["city"].toString(); @@ -203,4 +200,15 @@ void ResultWindow::itemClicked(QListWidgetItem* item) emit itemSelected(details); } +void ResultWindow::setVisible(bool visible) +{ + QMainWindow::setVisible(visible); + + if(!visible && source_) + { + source_->abort(); + } +} + + diff --git a/src/gui/resultwindow.h b/src/gui/resultwindow.h index 8bd3228..76b1a1b 100644 --- a/src/gui/resultwindow.h +++ b/src/gui/resultwindow.h @@ -24,7 +24,7 @@ #include #include #include "searchdialog.h" -#include "eniro.h" +#include "source.h" #include "connectionmanager.h" class ResultWindow : public QMainWindow @@ -37,18 +37,19 @@ public: static const int REQUEST_TIMEOUT = 30000; signals: - void itemSelected(Eniro::Result const& result); + void itemSelected(Source::Result const& result); public slots: void search(SearchDialog::SearchDetails& details); + virtual void setVisible(bool visible); private slots: - void resultAvailable(Eniro::Result const& result, Eniro::SearchDetails const& details); - void requestFinished(QVector const& results, Eniro::SearchDetails const& details, bool error); + void resultAvailable(Source::Result const& result, Source::SearchDetails const& details); + void requestFinished(QVector const& results, Source::SearchDetails const& details, bool error); void itemClicked(QListWidgetItem* item); private: - Eniro* eniro_; + Source* source_; QListWidget* list_; ConnectionManager* connectionManager_; diff --git a/src/gui/searchdialog.cpp b/src/gui/searchdialog.cpp index 1097b80..58dd94f 100644 --- a/src/gui/searchdialog.cpp +++ b/src/gui/searchdialog.cpp @@ -43,7 +43,7 @@ numberInput_(0), locationInput_(0), selector_(0) selector_ = new ButtonSelector(tr("Type"), this); selector_->addItem(tr("Persons")); - selector_->addItem(tr("Yellow pages")); + selector_->addItem(tr("Companies")); QVBoxLayout* leftLayout = new QVBoxLayout; leftLayout->addLayout(numberLayout); @@ -82,6 +82,10 @@ void SearchDialog::searchPressed() void SearchDialog::setVisible(bool visible) { - QDialog::setVisible(visible); - numberInput_->setFocus(); + QDialog::setVisible(visible); + + if(visible) + { + numberInput_->setFocus(); + } } diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index bb2d351..337a9bd 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -31,30 +31,29 @@ #include "db.h" #include "daemon.h" #include "cache.h" +#include "buttonselector.h" +#include "connectionselector.h" +#include "sourceguiconfig.h" -QMap SettingsDialog::sites_; +QList SettingsDialog::sources_; SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent), -usernameInput_(0), passwordInput_(0), cacheInput_(0), siteSelector_(0), -autostartSelector_(0) +sourceConfig_(0), cacheInput_(0), sourceSelector_(0), +autostartSelector_(0), connectionSelector_(0), tabs_(0) { setWindowTitle(tr("Settings")); - DB::connect(); - QVBoxLayout* general = new QVBoxLayout; QVBoxLayout* daemon = new QVBoxLayout; QHBoxLayout* mainLayout = new QHBoxLayout; - QHBoxLayout* username = new QHBoxLayout; - QHBoxLayout* password = new QHBoxLayout; + QHBoxLayout* cache = new QHBoxLayout; - QLabel* usernameLabel = new QLabel(tr("Eniro username")); - usernameInput_ = new QLineEdit(Settings::instance()->get("eniro_username")); + currentSource_ = Settings::instance()->get("source"); + Source::SourceId sourceId = Source::stringToId(currentSource_); + sourceConfig_ = SourceGuiConfig::getGuiConfig(sourceId, this); - QLabel* passwordLabel = new QLabel(tr("Eniro password")); - passwordInput_ = new QLineEdit(Settings::instance()->get("eniro_password")); - passwordInput_->setEchoMode(QLineEdit::PasswordEchoOnEdit); + Q_ASSERT(sourceConfig_ != 0); QLabel* cacheLabel = new QLabel(tr("Cache size (numbers)")); cacheInput_ = new QLineEdit(Settings::instance()->get("cache_size")); @@ -62,46 +61,44 @@ autostartSelector_(0) QPushButton* cacheResetButton = new QPushButton(tr("Clear"), this); connect(cacheResetButton, SIGNAL(pressed()), this, SLOT(resetCache())); - siteSelector_ = new ButtonSelector(tr("Eniro site"), this); - QString site = Settings::instance()->get("eniro_site"); - int i = 0; + languageSelector_ = new ButtonSelector(tr("Language"), this); + languageSelector_->addItem(tr("Automatic"), ""); - if(sites_.empty()) - { - sites_ = Eniro::getSites(); - } + selectedLanguage_ = Settings::instance()->get("language"); - QMap ::const_iterator it; - for(it = sites_.begin(); it != sites_.end(); it++) + QList langs; + Settings::getLanguages(langs); + + for(int i = 0; i < langs.size(); i++) { - QString name; + languageSelector_->addItem(langs.at(i).name, langs.at(i).id); - switch(it.key()) + if(langs.at(i).id == selectedLanguage_) { - case Eniro::FI: - name = tr("Finnish"); - break; - case Eniro::SE: - name = tr("Swedish"); - break; - case Eniro::DK: - name = tr("Danish"); - break; - default: - qDebug() << "Unknown site"; - continue; - + languageSelector_->setCurrentIndex(i + 1); } - siteSelector_->addItem(name, it.value().id); + } + + sourceSelector_ = new ButtonSelector(tr("Phonebook"), this); + + if(sources_.isEmpty()) + { + Source::getSources(sources_); + } + + for(int i = 0; i < sources_.size(); i++) + { + sourceSelector_->addItem(sources_.at(i).name, sources_.at(i).id); - if(it.value().id == site) + if(sources_.at(i).id == currentSource_) { - siteSelector_->setCurrentIndex(i); + sourceSelector_->setCurrentIndex(i); } - - i++; } + connect(sourceSelector_, SIGNAL(selected(unsigned int, QString const&, QVariant const&)), + this, SLOT(onSourceSelected(unsigned int, QString const&, QVariant const&))); + autostartSelector_ = new ButtonSelector(tr("Autostart"), this); QString autostart = Settings::instance()->get("autostart"); autostartSelector_->addItem(tr("Enabled"), "1"); @@ -115,17 +112,12 @@ autostartSelector_(0) QPushButton* submitButton = new QPushButton(tr("Save"), this); connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings())); - username->addWidget(usernameLabel); - username->addWidget(usernameInput_); - password->addWidget(passwordLabel); - password->addWidget(passwordInput_); cache->addWidget(cacheLabel); cache->addWidget(cacheInput_); cache->addWidget(cacheResetButton); - general->addLayout(username); - general->addLayout(password); general->addLayout(cache); - general->addWidget(siteSelector_); + general->addWidget(languageSelector_); + general->addWidget(sourceSelector_); daemon->addWidget(autostartSelector_); daemon->addWidget(connectionSelector_); @@ -134,7 +126,7 @@ autostartSelector_(0) buttons->setCenterButtons(false); buttons->addButton(submitButton, QDialogButtonBox::AcceptRole); - QTabWidget* tabs = new QTabWidget; + tabs_ = new QTabWidget; QWidget* generalTab = new QWidget; generalTab->setLayout(general); @@ -142,52 +134,47 @@ autostartSelector_(0) QWidget* daemonTab = new QWidget; daemonTab->setLayout(daemon); - tabs->addTab(generalTab, tr("General")); - tabs->addTab(daemonTab, tr("Daemon")); + tabs_->addTab(generalTab, tr("General")); + tabs_->addTab(daemonTab, tr("Daemon")); + tabs_->addTab(sourceConfig_, tr("Phonebook settings")); - mainLayout->addWidget(tabs); + mainLayout->addWidget(tabs_); mainLayout->addWidget(buttons); setLayout(mainLayout); - DB::disconnect(); } void SettingsDialog::saveSettings() { hide(); - DB::connect(); + Settings::instance()->startEdit(); - Settings::instance()->set("eniro_username", usernameInput_->text()); - Settings::instance()->set("eniro_password", passwordInput_->text()); + sourceConfig_->save(); Settings::instance()->set("cache_size", cacheInput_->text()); - QString site = siteSelector_->value().toString(); - Settings::instance()->set("site", site); + QString source = sourceSelector_->value().toString(); + Settings::instance()->set("source", source); QString autostart = autostartSelector_->value().toString(); Settings::instance()->set("autostart", autostart); QString connection = connectionSelector_->value().toString(); Settings::instance()->set("connection", connection); Settings::instance()->set("connection_name", connectionSelector_->text()); + QString language = languageSelector_->value().toString(); + Settings::instance()->set("language", language); - DB::disconnect(); + Settings::instance()->endEdit(); - if(site != currentSite_ && Daemon::isRunning()) + if(Daemon::isRunning()) { QMaemo5InformationBox::information(this, tr("Restarting daemon...")); Daemon::restart(); - currentSite_ = site; } -} - -void SettingsDialog::setVisible(bool visible) -{ - QDialog::setVisible(visible); - - if(visible) + if(language != selectedLanguage_) { - currentSite_ = siteSelector_->value().toString(); + QMaemo5InformationBox::information(this, tr("You need to restart Jenirok for language change to take effect.")); + selectedLanguage_ = language; } } @@ -201,3 +188,24 @@ void SettingsDialog::resetCache() QMaemo5InformationBox::information(this, tr("%n number(s) were deleted from cache", "", ret)); } } + +void SettingsDialog::onSourceSelected(unsigned int index, + QString const& text, + QVariant const& value) +{ + Q_UNUSED(index); + Q_UNUSED(text); + + QString source = value.toString(); + + if(source != currentSource_) + { + tabs_->removeTab(tabs_->count() - 1); + delete sourceConfig_; + Source::SourceId sourceId = Source::stringToId(value.toString()); + sourceConfig_ = SourceGuiConfig::getGuiConfig(sourceId, this); + Q_ASSERT(sourceConfig_ != 0); + tabs_->addTab(sourceConfig_, tr("Phonebook settings")); + currentSource_ = source; + } +} diff --git a/src/gui/settingsdialog.h b/src/gui/settingsdialog.h index d45aae7..9686e37 100644 --- a/src/gui/settingsdialog.h +++ b/src/gui/settingsdialog.h @@ -21,14 +21,19 @@ #include #include +#include #include #include #include #include +#include #include -#include "eniro.h" -#include "buttonselector.h" -#include "connectionselector.h" +#include "source.h" + +class SourceGuiConfig; +class ButtonSelector; +class ConnectionSelector; + class SettingsDialog : public QDialog { @@ -39,18 +44,22 @@ public: public slots: void saveSettings(); - void setVisible(bool visible); void resetCache(); +private slots: + void onSourceSelected(unsigned int index, QString const& text, QVariant const& value); + private: - static QMap sites_; - QLineEdit* usernameInput_; - QLineEdit* passwordInput_; + static QList sources_; + SourceGuiConfig* sourceConfig_; QLineEdit* cacheInput_; - ButtonSelector* siteSelector_; + ButtonSelector* languageSelector_; + ButtonSelector* sourceSelector_; ButtonSelector* autostartSelector_; ConnectionSelector* connectionSelector_; - QString currentSite_; + QString currentSource_; + QString selectedLanguage_; + QTabWidget* tabs_; }; #endif diff --git a/src/gui/sourceguiconfig.cpp b/src/gui/sourceguiconfig.cpp new file mode 100644 index 0000000..70f214a --- /dev/null +++ b/src/gui/sourceguiconfig.cpp @@ -0,0 +1,49 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#include +#include +#include +#include "sourceguiconfig.h" +#include "eniroguiconfig.h" +#include "emptyguiconfig.h" + +SourceGuiConfig::SourceGuiConfig(QWidget* parent): QWidget(parent) +{ +} + +SourceGuiConfig::~SourceGuiConfig() +{ +} + +SourceGuiConfig* SourceGuiConfig::getGuiConfig(Source::SourceId id, QWidget* parent) +{ + switch(id) + { + case Source::ENIRO: + return new EniroGuiConfig(parent); + break; + case Source::MOBIL1881: + return new EmptyGuiConfig(parent); // No configuration + break; + default: + qDebug() << "Unknown source id: " << id; + } + + return 0; +} diff --git a/src/gui/sourceguiconfig.h b/src/gui/sourceguiconfig.h new file mode 100644 index 0000000..f670cbb --- /dev/null +++ b/src/gui/sourceguiconfig.h @@ -0,0 +1,38 @@ +/* + * This file is part of Jenirok. + * + * Jenirok 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. + * + * Jenirok 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 Jenirok. If not, see . + * + */ + +#ifndef SOURCEGUICONFIG_H +#define SOURCEGUICONFIG_H + +#include +#include "source.h" + +class SourceGuiConfig : public QWidget +{ + + Q_OBJECT + +public: + + static SourceGuiConfig* getGuiConfig(Source::SourceId id, QWidget* parent = 0); + SourceGuiConfig(QWidget* parent = 0); + virtual ~SourceGuiConfig(); + virtual void save() = 0; +}; + +#endif -- 1.7.9.5