Added documentation
[mdictionary] / trunk / src / base / backbone / backbone.h
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*! /file backbone.cpp
23 \brief Backbone/core main header \see Backbone
24
25
26 \author Bartosz Szatkowski <bulislaw@linux.com>
27 */
28
29 #ifndef BACKBONE_H
30 #define BACKBONE_H
31
32 #include <QObject>
33 #include <QList>
34 #include <QHash>
35 #include <QPluginLoader>
36 #include <QFuture>
37 #include <QtConcurrentRun>
38 #include <QtConcurrentMap>
39 #include <QFutureIterator>
40 #include <QTimer>
41 #include <QTime>
42 #include <QDir>
43 #include <QThread>
44 #include <QSettings>
45 #include <QFutureWatcher>
46 #include "../../includes/CommonDictInterface.h"
47 #include "../../includes/settings.h"
48 #include "../../includes/translation.h"
49 #include "../../includes/History.h"
50 #include "Bookmarks.h"
51
52
53 /*! Inner part of dictionary - glues together GUI and plugins, also kind of
54     GoF facade (for GUI) cover few classes \see Bookmarks \see History
55
56   Backbone is responsible for managing plugins and dictionaries, starting
57   new searches and threads, merging search results from multiple dictionaries.
58
59   Each plugin may live in multiple instances - each with its own dictionary,
60   backbone must provide way to create them at start (with specific Settings) and
61   distinguich each ditionary.
62
63   Backbone also manage bookmarks and history: providing interface to gui
64
65   Backbone is also responsible for saving and spawning session via configs
66   file (stored in ~/.mdictionary) -> configs are kind of tricky because
67   mDictionary is delivered with two simple dicts -> its necessary to separate default
68   configs from user configs (updating/reinstaling app results in overwriten
69   default config file), moreover config file there is general mdictionary
70   configuration (aparto from dictionaries and plugin ones).
71
72   Other modules may set some internal backbone behaviour via \see setSettings():
73   Settings object with option given:
74      * history_size - int, size of stored searches
75      * search_limit - int, how many different word may each dictionary returns
76      * search_dictionaries - true/false, whether search in dictionaries
77      * search_bookmarks - true/false, whether search in bookmarks
78 */
79 class Backbone : public QObject
80 {
81     Q_OBJECT
82
83 public:
84     /*!\param pluginPath path to plugins (leave blank for default)
85       \param configPath path to folder with configuration files*/
86     Backbone(QString pluginPath="", QString configPath="",
87              bool dry = 0, QObject *parent = 0);
88     ~Backbone();
89     Backbone(const Backbone& b);
90
91     //! \return all loadded dictionaries with activity state flag
92     QHash<CommonDictInterface*, bool> getDictionaries();
93
94     //! \return all loadded plugins
95     QList<CommonDictInterface*> getPlugins();
96
97     //! \return history of performed searches
98     History* history();
99
100     //! \return return search fesult
101     QMultiHash<QString, Translation*> result();
102
103     //! \return maximum number of word that plugin could find
104     int searchLimit() const;
105
106     //! \return final translation (after searching for html)
107     QStringList htmls();
108
109
110 public Q_SLOTS:
111     //! stops all current searches and emiting searchCanceled signal
112     void stopSearching();
113
114     /*! search for a word translation
115        \param word to be translated
116       */
117     void search(QString word);
118
119     /*! sets active dictionaries (searches are performed only in active dicts
120        \param List of dictionaris to be activated
121       */
122     void selectedDictionaries(QList<CommonDictInterface* >);
123
124     /*! adds new dictionary and activate it
125       \param dict dictionary to be added
126       \param active decides whether searches are perfomed in given dictionaries
127       */
128     void addDictionary(CommonDictInterface* dict, bool active = 1);
129
130
131     //! stops all current activity - emiting signal \see closeOk
132     void quit();
133
134
135     /*! Fired with given interval during searches -
136         checking if translation is ready
137       */
138     void translationReady();
139
140     /*! Fired with given interval during html searches -
141         checking if html is ready
142       */
143     void htmlTranslationReady();
144
145     /*! Removes given dictionary
146         \param dict dictionary to be deleted
147       */
148     void removeDictionary(CommonDictInterface* dict);
149
150     /*! saves plugins new state/configuration after each change */
151     void dictUpdated();
152
153     /*! Performs search for final translation (html/xml) form
154       \param list of Translation* to be searched for
155       */
156     void searchHtml(QList<Translation*>);
157
158
159     /*! add bookmarks to given translations (translation object is fetched and
160       added to bookmarks data base (key and translation stored in db)
161       \param translation translation object  to be stored in db
162       */
163     void addBookmark(QList<Translation*> translations) {
164         foreach(Translation* translation, translations)
165             //_bookmarks.add(translation);
166             QtConcurrent::run(_bookmarks, &Bookmarks::add, translation);
167     }
168
169
170     /*! Remove bookmarks to given translatios
171       \param translation remove bookmark to this translation
172       */
173     void removeBookmark(QList<Translation*> translations) {
174         foreach(Translation* translation, translations)
175             _bookmarks.remove(translation);
176     }
177
178
179
180     /*! Remove all bookmarks
181       */
182     void removeAllBookmark(){
183         _bookmarks.clear();
184     }
185
186
187    /*! Searching for list of bookmarks may take some time, so i moved it to
188        new thread (to avoid gui blocking), futher its consistent with ordinary
189        searching for list of word (\see search)
190        */
191    void fetchBookmarks() {
192         _result.clear();
193
194         stopped = false;
195         dictFin = 1;
196         bookmarkFin = 0;
197
198         if(_searchBookmarks) {
199            _innerBookmarks = QtConcurrent::run(_bookmarks,
200                    &Bookmarks::searchWordList, QString("*"));
201            _bookmarkSearchWatcher.setFuture(_innerBookmarks);
202         }
203    }
204
205
206
207    /*! Sets settings for backbone: history_size, search_limit,
208        searching backends (search_bookmarks, search_dictionaries)
209        \param settings settings object with opitons set
210        */
211     void setSettings(Settings* settings);
212
213
214     /*! \return coresponding settings object with history_size, search_limit,
215        searching backends (search_bookmarks, search_dictionaries)
216        */
217     Settings* settings();
218
219
220
221
222 Q_SIGNALS:
223     /*! emmited when backbone is ready to close - after getting stop signal it
224         should kill all threads and so on */
225     void closeOk();
226
227     //! emitted when there are search result ready to fetch
228     void ready();
229
230     //! emitted when html result is ready to fetch
231     void htmlReady();
232
233     //! throwed when searches are stopped
234     void searchCanceled();
235
236     //! emmited when bookmark list is ready to fetch
237     void bookmarksReady();
238
239 private Q_SLOTS:
240     void bookmarksListReady();
241
242
243 private:
244     QHash<CommonDictInterface*, bool> _dicts; // List of dictionaries
245     QList<CommonDictInterface*> _plugins;  // List of plugins
246
247
248     QFuture<QList<Translation*> > _innerResult; //Res of concurent word search
249     QFuture<QString> _innerHtmlResult;  // Result of html search
250     QFuture<QList<Translation*> > _innerBookmarks; //Res of search in bookmarks
251     QFuture<QList<Translation*> > _innerListBookmarks; //Res of search in bookmarks
252     QFuture<QStringList> _innerHtmlBookmarks; //Html result of bookmarks search
253
254     QMultiHash<QString, Translation*> _result; //Final result of word search
255     QStringList _htmlResult; // Final result of html search
256     QList<Translation*> _bookmarksResult; // Final result of search in bookmarks
257
258
259     // Keeps track of concurent computations
260     QFutureWatcher<QList<Translation*> > _resultWatcher;
261     QFutureWatcher<QList<Translation*> > _bookmarkWatcher;
262     QFutureWatcher<QList<Translation*> > _bookmarkSearchWatcher;
263     QFutureWatcher<QString> _htmlResultWatcher;
264
265
266     QString _pluginPath, _defaultPluginPath;
267     QString _configPath;
268     QString _defaultConfigPath;
269     int _searchLimit, _defaultSearchLimit;
270     int _historyLen, _defaultHistoryLen;
271
272     bool dryRun; // mainly for testing - when true then dosent bother configs etc
273     bool stopped; // true when user stops searching/fetching
274     bool bookmarkFin, dictFin; // inform whether givent search type is ready
275     bool _searchDicts, _searchBookmarks; // whether perform search in given source
276
277     Bookmarks _bookmarks;
278
279
280     void init();
281
282     QStringList getFilesFromDir(QString dir, QStringList nameFilter);
283     void loadPlugins(); //< locate and load plugins
284     void loadPrefs(QString fileName);
285     void loadDicts(QString fileName, bool _default=false);
286
287     void saveState(QSettings*, Settings*, bool, uint);
288     void addInternalDictionary(CommonDictInterface*, bool);
289     void savePrefs(QSettings*);
290     void saveDefaultPrefs(QSettings*);
291
292     CommonDictInterface* plugin(QString type); // search for given type plugin
293     QList<CommonDictInterface*> activeDicts();
294
295     History* _history;
296
297 };
298
299 #endif // BACKBONE_H