fe3b178a0fb704d7ad7015bb2ceee1086af60671
[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 "../../includes/Notify.h"
51 #include "Bookmarks.h"
52
53
54 /*! Inner part of dictionary - glues together GUI and plugins, also kind of
55     GoF facade (for GUI) cover few classes \see Bookmarks \see History
56
57   Backbone is responsible for managing plugins and dictionaries, starting
58   new searches and threads, merging search results from multiple dictionaries.
59
60   Each plugin may live in multiple instances - each with its own dictionary,
61   backbone must provide way to create them at start (with specific Settings) and
62   distinguish each ditionary.
63
64   Backbone also manages bookmarks and history: providing interface to gui.
65
66   Backbone is also responsible for saving and spawning session via configs
67   file (stored in ~/.mdictionary) -> configs are kind of tricky because
68   mDictionary is delivered with two simple dicts -> it's necessary to separate default
69   configs from user configs (updating/reinstalling app results in overwritten
70   default config file), moreover config file there is general mdictionary
71   configuration (apart from dictionaries and plugin ones).
72
73   Other modules may set some internal backbone behaviour via \see setSettings():
74   Settings object with option given:
75      * history_size - int, size of stored searches
76      * search_limit - int, how many different words each dictionary may return
77      * search_dictionaries - true/false, whether search in dictionaries
78      * search_bookmarks - true/false, whether search in bookmarks
79
80     Searching schema:
81         First GUI should ask for list of words matching given pattern
82         then each Translation object is capable of finding its own final translation
83
84       List of words:
85         - Gui calls search(...)
86         - Backbone calls plugins searchWordList(...) in idealThreadCount()+1 threads
87         - Backbone sets  the FutureWatcher to be notifed when plugins are done
88         - Backbone fetches results from Future<..> and formats it for gui then
89            emits ready()
90         - Gui calls result()
91
92       Final translation:
93          - Gui calls searchHtml()
94          - Backbone starts for each translation object toHtml in separate threads
95          - Backbone sets FutureWatcher to be notified after last toHtml returns
96          - Backbone fetches translation from Future<...> objects and calls
97              htmlReady()
98          - Gui calls htmlResult()
99
100 */
101 class Backbone : public QObject
102 {
103     Q_OBJECT
104
105 public:
106     /*!\param pluginPath path to plugins (leave blank for default)
107       \param configPath path to folder with configuration files
108       \param dry dry run is mode without paying attention to configuration etc
109           mainly for testing
110       */
111     Backbone(QString pluginPath="", QString configPath="",
112              bool dry = 0, QObject *parent = 0);
113     ~Backbone();
114     Backbone(const Backbone& b);
115
116     //! \return all loaded dictionaries with activity state flag
117     QHash<CommonDictInterface*, bool> getDictionaries();
118
119     //! \return all loaded plugins
120     QList<CommonDictInterface*> getPlugins();
121
122     //! \return history of performed searches
123     History* history();
124
125     //! \return return search fesult
126     QMultiHash<QString, Translation*> result();
127
128     //! \return maximum number of words that plugin could find
129     int searchLimit() const;
130
131     //! \return final translation (after searching for html)
132     QStringList htmls();
133
134     /*! maximum number of translations that each plugin may return; it must be
135         public static because of QtConcurent::mapped restrictions about
136         what kind of function may be used there see Qt docs */
137     static int _searchLimit;
138
139
140
141 public Q_SLOTS:
142     //! stops all current searches and emits searchCanceled signal
143     void stopSearching();
144
145     /*! searches for a word translation
146        \param word to be translated
147       */
148     void search(QString word);
149
150     /*! sets active dictionaries (searches are performed only in active dicts
151        \param List of dictionaries to be activated
152       */
153     void selectedDictionaries(QList<CommonDictInterface* >);
154
155     /*! adds new dictionary and activates it
156       \param dict dictionary to be added
157       \param active decides whether searches are perfomed in given dictionaries
158       */
159     void addDictionary(CommonDictInterface* dict, bool active = 1);
160
161
162     //! stops all current activity - emitting signal \see closeOk
163     void quit();
164
165
166     /*! Fired by FutureWatcher when list of words is ready (after calling search)
167         fetch Future<...> to final result
168       */
169     void translationReady();
170
171     /*! Fired by FutureWatcher when search result is ready, fetch Future to
172         final result
173       */
174     void htmlTranslationReady();
175
176     /*! Removes given dictionary
177         \param dict dictionary to be deleted
178       */
179     void removeDictionary(CommonDictInterface* dict);
180
181     /*! Saves plugins new state/configuration after each change */
182     void dictUpdated();
183
184     /*! Performs search for final translation (html/xml) form
185       \param list of Translation* to be searched for
186       */
187     void searchHtml(QList<Translation*>);
188
189
190     /*! adds bookmarks to given translations (translation object is fetched and
191       added to bookmarks data base (key and translation stored in db))
192       \param translation translation object to be stored in db
193       */
194     void addBookmark(QList<Translation*> translations) {
195         foreach(Translation* translation, translations)
196             //_bookmarks.add(translation);
197             QtConcurrent::run(_bookmarks, &Bookmarks::add, translation);
198     }
199
200
201     /*! Removes bookmarks to given translations
202       \param translation remove bookmark to this translation
203       */
204     void removeBookmark(QList<Translation*> translations) {
205         foreach(Translation* translation, translations)
206             _bookmarks.remove(translation);
207     }
208
209
210
211     /*! Removes all bookmarks
212       */
213     void removeAllBookmarks(){
214         _bookmarks.clear();
215     }
216
217
218    /*! Searching for list of bookmarks may take some time, so I moved it to
219        new thread (to avoid gui blocking), further it's consistent with ordinary
220        searching for list of words (\see search)
221        */
222    void fetchBookmarks() {
223         _result.clear();
224
225         stopped = false;
226         dictFin = 1;
227         bookmarkFin = 0;
228
229         _innerBookmarks = QtConcurrent::run(_bookmarks,
230                 &Bookmarks::list);
231         _bookmarkSearchWatcher.setFuture(_innerBookmarks);
232    }
233
234
235
236    /*! Sets settings for backbone: history_size, search_limit,
237        searching backends (search_bookmarks, search_dictionaries)
238        \param settings settings object with options set
239        */
240     void setSettings(Settings* settings);
241
242
243     /*! \return corresponding settings object with history_size, search_limit,
244        searching backends (search_bookmarks, search_dictionaries)
245        */
246     Settings* settings();
247
248
249
250
251
252
253 Q_SIGNALS:
254     /*! emitted when backbone is ready to close - after getting stop signal it
255         should kill all threads and so on */
256     void closeOk();
257
258     //! emitted when there are search results ready to fetch
259     void ready();
260
261     //! emitted when html result is ready to fetch
262     void htmlReady();
263
264     //! thrown when searches are stopped
265     void searchCanceled();
266
267     //! emitted when bookmark list is ready to fetch
268     void bookmarksReady();
269
270     /*! emitted by direct connection to plugins notifying signals
271         \param Notify::NotifyType gui may decide to show different types in
272             different ways
273         \param QString text of the notification
274     */
275     void notify(Notify::NotifyType, QString);
276
277 private Q_SLOTS:
278     void bookmarksListReady();
279
280
281 private:
282     QHash<CommonDictInterface*, bool> _dicts; // List of dictionaries
283     QList<CommonDictInterface*> _plugins;  // List of plugins
284
285
286     QFuture<QList<Translation*> > _innerResult; //Res of concurrent word search
287     QFuture<QString> _innerHtmlResult;  // Result of html search
288     QFuture<QList<Translation*> > _innerBookmarks; //Res of search in bookmarks
289     QFuture<QList<Translation*> > _innerListBookmarks; //Res of search in bookmarks
290     QFuture<QStringList> _innerHtmlBookmarks; //Html result of bookmarks search
291
292     QMultiHash<QString, Translation*> _result; //Final result of word search
293     QStringList _htmlResult; // Final result of html search
294     QList<Translation*> _bookmarksResult; // Final result of search in bookmarks
295
296
297     // Keeps track of concurent computations
298     QFutureWatcher<QList<Translation*> > _resultWatcher;
299     QFutureWatcher<QList<Translation*> > _bookmarkWatcher;
300     QFutureWatcher<QList<Translation*> > _bookmarkSearchWatcher;
301     QFutureWatcher<QString> _htmlResultWatcher;
302
303
304     QString _pluginPath, _defaultPluginPath;
305     QString _configPath;
306     QString _defaultConfigPath;
307     int _defaultSearchLimit;
308     int _historyLen, _defaultHistoryLen;
309
310     bool dryRun; // mainly for testing - when true then doesn't bother configs etc
311     bool stopped; // true when user stops searching/fetching
312     bool bookmarkFin, dictFin; // inform whether given search type is ready
313     bool _searchDicts, _searchBookmarks; // whether search performed in given source
314
315     Bookmarks _bookmarks;
316
317
318     void init();
319
320     QStringList getFilesFromDir(QString dir, QStringList nameFilter);
321     void loadPlugins(); //< locate and load plugins
322     void loadPrefs(QString fileName);
323     void loadDicts(QString fileName, bool _default=false);
324
325     void saveState(QSettings*, Settings*, bool, uint);
326     void addInternalDictionary(CommonDictInterface*, bool);
327     void savePrefs(QSettings*);
328     void saveDefaultPrefs(QSettings*);
329
330     CommonDictInterface* plugin(QString type); // search for given type plugin
331     QList<CommonDictInterface*> activeDicts();
332     bool containsDict(uint hash) const;
333     int _dictNum;
334
335     History* _history;
336
337     friend class BackboneTest;
338
339 };
340
341 #endif // BACKBONE_H