Added welcome screen
[mdictionary] / trunk / src / base / backbone / backbone.h
index fdcce17..947dcbe 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
 
-//! Inner part of dictionary - glues together GUI and plugins
+  Backbone is responsible for managing plugins and dictionaries, starting
+  new searches and threads, merging search results from multiple dictionaries.
+
+  Each plugin may live in multiple instances - each with its own dictionary,
+  backbone must provide way to create them at start (with specific Settings) and
+  distinguich each ditionary.
+
+*/
 class Backbone : public QObject
 {
     Q_OBJECT
 
 public:
-    Backbone(QObject *parent = 0);
+    /*!\param pluginPath path to plugins (leave blank for default)
+      \param configPath path to folder with configuration files*/
+    Backbone(QString pluginPath="", QString configPath="",
+             bool dry = 0, QObject *parent = 0);
     ~Backbone();
     Backbone(const Backbone& b);
 
@@ -55,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();
@@ -63,61 +87,204 @@ public:
     //! \return maximum number of word that plugin could find
     int searchLimit() const;
 
-    //! \return number of active searches
-    int activeSearches() const;
+    //! \return final translation (after searching for html)
+    QStringList htmls();
+
 
 public Q_SLOTS:
     //! stops all current searches
     void stopSearching();
 
-    //! search for a word translation
+    /*! search for a word translation
+       \param word to be translated
+       \param dicts searching in dicionaries
+       \param bookmarks searching in bookmarks
+      */
     void search(QString word);
 
-    //! sets active dictionaries
+    /*! sets active dictionaries (searches are performed only in active dicts
+       \param List of dictionaris to be activated
+      */
     void selectedDictionaries(QList<CommonDictInterface* >);
 
-    //! adds new dictionary
-    void addDictionary(CommonDictInterface* dict);
+    /*! adds new dictionary and activate it
+      \param dict dictionary to be added
+      \param active decides whether searches are perfomed in given dictionaries
+      */
+    void addDictionary(CommonDictInterface* dict, bool active = 1);
 
 
-    //! stops all current activity and kill plugins - be ready to exit
+    //! stops all current activity - emiting signal \see closeOk
     void quit();
 
 
-    //! Fired when dictionary call finalTranslation(..) with translation ready
-    void translation();
+    /*! Fired with given interval during searches -
+        checking if translation is ready
+      */
+    void translationReady();
 
-    //! Removes given dictionary
+    /*! Fired with given interval during html searches -
+        checking if html is ready
+      */
+    void htmlTranslationReady();
+
+    /*! Removes given dictionary
+        \param dict dictionary to be deleted
+      */
     void removeDictionary(CommonDictInterface* dict);
 
-    // TODO addToBookmark(Translation*);
-    // TODO removeFromBookmark(Translation*);
+    /*! saves plugins new state/configuration after each change */
+    void dictUpdated();
+
+    /*! 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
-    //!     should kill all threads and so on
+    /*! emmited when backbone is ready to close - after getting stop signal it
+        should kill all threads and so on */
     void closeOk();
 
     //! emitted when there are search result ready to fetch
     void ready();
 
+    //! 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:
-    void loadPlugins(); //< locate and load plugins
-    QHash<CommonDictInterface*, bool> _dicts;
-    QList<CommonDictInterface*> _plugins;
-    QList<QFuture<QList<Translation*> > > _innerResult;
-    QMultiHash<QString, Translation*> _result;
-    QTimer _timer;
-    int _searchLimit;
+    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;
+    QString _defaultConfigPath;
+    int _searchLimit, _defaultSearchLimit;
     int _activeSearchNum;
-    QTime _time;
-    int _interval; //Search fetching timer.timeout interval in msec
-    QString _pluginPath;
+    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(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
+    QList<CommonDictInterface*> activeDicts();
+
+
+    History* _history;
+
 };
 
 #endif // BACKBONE_H