Fixed closing menu, added bookmarks operations in different thread
[mdictionary] / trunk / src / base / backbone / backbone.h
index 61db1bc..21cb3df 100644 (file)
 
 *******************************************************************************/
 
-// Created by Bartosz Szatkowski
+/*! /file backbone.cpp
+\brief Backbone/core main header \see Backbone
+
+
+\author Bartosz Szatkowski <bulislaw@linux.com>
+*/
 
 #ifndef BACKBONE_H
 #define BACKBONE_H
 #include <QPluginLoader>
 #include <QFuture>
 #include <QtConcurrentRun>
+#include <QtConcurrentMap>
+#include <QFutureIterator>
 #include <QTimer>
 #include <QTime>
 #include <QDir>
+#include <QThread>
 #include <QSettings>
+#include <QFutureWatcher>
 #include "../../includes/CommonDictInterface.h"
 #include "../../includes/settings.h"
 #include "../../includes/translation.h"
+#include "../../includes/History.h"
+#include "Bookmarks.h"
 
 
 /*! Inner part of dictionary - glues together GUI and plugins
@@ -56,7 +67,8 @@ class Backbone : public QObject
 public:
     /*!\param pluginPath path to plugins (leave blank for default)
       \param configPath path to folder with configuration files*/
-    Backbone(QString pluginPath="", QString configPath="", QObject *parent = 0);
+    Backbone(QString pluginPath="", QString configPath="",
+             bool dry = 0, QObject *parent = 0);
     ~Backbone();
     Backbone(const Backbone& b);
 
@@ -67,7 +79,7 @@ public:
     QList<CommonDictInterface*> getPlugins();
 
     //! \return history of performed searches
-    QList<QString> getHistory(); //TODO implementation needed (in future)
+    History* history();
 
     //! \return return search fesult
     QMultiHash<QString, Translation*> result();
@@ -75,14 +87,6 @@ public:
     //! \return maximum number of word that plugin could find
     int searchLimit() const;
 
-    //! \return number of active searches
-    int activeSearches() const;
-
-    /*! Performs search for final translation (html/xml) form
-      \param list of Translation* to be searched for
-      */
-    void searchHtml(QList<Translation*>);
-
     //! \return final translation (after searching for html)
     QStringList htmls();
 
@@ -92,9 +96,11 @@ public Q_SLOTS:
     void stopSearching();
 
     /*! search for a word translation
-       \param word list of words to be translated
+       \param word to be translated
+       \param dicts searching in dicionaries
+       \param bookmarks searching in bookmarks
       */
-    void search(QStringList word);
+    void search(QString word);
 
     /*! sets active dictionaries (searches are performed only in active dicts
        \param List of dictionaris to be activated
@@ -130,8 +136,79 @@ public Q_SLOTS:
     /*! saves plugins new state/configuration after each change */
     void dictUpdated();
 
-    // TODO addToBookmark(Translation*);
-    // TODO removeFromBookmark(Translation*);
+    /*! Performs search for final translation (html/xml) form
+      \param list of Translation* to be searched for
+      */
+    void searchHtml(QList<Translation*>);
+
+
+    /*! add bookmarks to given translations (translation object is fetched and
+      added to bookmarks data base (key and translation stored in db)
+      \param translation translation object  to be stored in db
+      */
+    void addBookmark(QList<Translation*> translations) {
+        foreach(Translation* translation, translations)
+            //_bookmarks.add(translation);
+            QtConcurrent::run(_bookmarks, &Bookmarks::add, translation);
+    }
+
+
+    /*! Remove bookmarks to given translatios
+      \param translation remove bookmark to this translation
+      */
+    void removeBookmark(QList<Translation*> translations) {
+        foreach(Translation* translation, translations)
+            _bookmarks.remove(translation);
+    }
+
+
+
+    /*! Remove all bookmarks
+      */
+    void removeAllBookmark(){
+        _bookmarks.clear();
+    }
+
+
+   /*! Searching for list of bookmarks may take some time, so i moved it to
+       new thread (to avoid gui blocking), when ready bookmarksReady is emited
+       and result is returned after calling getBookmarks()
+       */
+   void fetchBookmarks() {
+        _result.clear();
+
+        stopped = false;
+        dictFin = 1;
+        bookmarkFin = 0;
+
+        if(_searchBookmarks) {
+           _innerBookmarks = QtConcurrent::run(_bookmarks,
+                   &Bookmarks::searchWordList, QString("*"));
+           _bookmarkSearchWatcher.setFuture(_innerBookmarks);
+        }
+   }
+
+   /*! \return list of all bookmarks
+     */
+   QList<Translation*> bookmarks() {
+       return _bookmarksResult;
+   }
+
+
+   /*! Sets settings for backbone: history_size, search_limit,
+       searching backends (search_bookmarks, search_dictionaries)
+       \param settings settings object with opitons set
+       */
+    void setSettings(Settings* settings);
+
+
+    /*! \return coresponding settings object with history_size, search_limit,
+       searching backends (search_bookmarks, search_dictionaries)
+       */
+    Settings* settings();
+
+
+
 
 Q_SIGNALS:
     /*! emmited when backbone is ready to close - after getting stop signal it
@@ -144,33 +221,69 @@ Q_SIGNALS:
     //! emitted when html result is ready to fetch
     void htmlReady();
 
+    //! throwed when searches are stopped
+    void searchCanceled();
+
+    //! emmited when bookmark list is ready to fetch
+    void bookmarksReady();
+
+private Q_SLOTS:
+    void bookmarksListReady();
 
 
 private:
-    QHash<CommonDictInterface*, bool> _dicts;
-    QList<CommonDictInterface*> _plugins;
-    QList<QFuture<QList<Translation*> > > _innerResult;
-    QList<QFuture<QString> > _innerHtmlResult;
-    QMultiHash<QString, Translation*> _result;
-    QStringList _htmlResult;
-    QTimer _timerSearch, _timerHtmlSearch;
-    QTime _time;
-    QString _pluginPath;
+    QHash<CommonDictInterface*, bool> _dicts; // List of dictionaries
+    QList<CommonDictInterface*> _plugins;  // List of plugins
+
+
+    QFuture<QList<Translation*> > _innerResult; //Res of concurent word search
+    QFuture<QString> _innerHtmlResult;  // Result of html search
+    QFuture<QList<Translation*> > _innerBookmarks; //Res of search in bookmarks
+    QFuture<QList<Translation*> > _innerListBookmarks; //Res of search in bookmarks
+    QFuture<QStringList> _innerHtmlBookmarks; //Html result of bookmarks search
+
+    QMultiHash<QString, Translation*> _result; //Final result of word search
+    QStringList _htmlResult; // Final result of html search
+    QList<Translation*> _bookmarksResult; // Final result of search in bookmarks
+
+
+    // Keeps track of concurent computations
+    QFutureWatcher<QList<Translation*> > _resultWatcher;
+    QFutureWatcher<QList<Translation*> > _bookmarkWatcher;
+    QFutureWatcher<QList<Translation*> > _bookmarkSearchWatcher;
+    QFutureWatcher<QString> _htmlResultWatcher;
+
+
+    QString _pluginPath, _defaultPluginPath;
     QString _configPath;
-    int _searchLimit;
+    QString _defaultConfigPath;
+    int _searchLimit, _defaultSearchLimit;
     int _activeSearchNum;
-    int _interval; //Search fetching timer.timeout interval in msec
-    int _historyLen;
+    int _historyLen, _defaultHistoryLen;
+    bool dryRun;
+    bool stopped;
+    bool bookmarkFin, dictFin; // inform whether givent search type is ready
+    bool _searchDicts, _searchBookmarks;
+    Bookmarks _bookmarks;
+
 
     void init();
+
     QStringList getFilesFromDir(QString dir, QStringList nameFilter);
     void loadPlugins(); //< locate and load plugins
-    void loadPrefs();
-    void loadDicts();
+    void loadPrefs(QString fileName);
+    void loadDicts(QString fileName, bool _default=false);
+
     void saveState(QSettings*, Settings*, bool, uint);
     void addInternalDictionary(CommonDictInterface*, bool);
+    void savePrefs(QSettings*);
+    void saveDefaultPrefs(QSettings*);
+
     CommonDictInterface* plugin(QString type); //< search for given type plugin
-    //void writeConfig(QString key, QString value);
+    QList<CommonDictInterface*> activeDicts();
+
+
+    History* _history;
 
 };