21cb3df2e7f9486891a2615f5e8e48b6fcd50a00
[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
54
55   Backbone is responsible for managing plugins and dictionaries, starting
56   new searches and threads, merging search results from multiple dictionaries.
57
58   Each plugin may live in multiple instances - each with its own dictionary,
59   backbone must provide way to create them at start (with specific Settings) and
60   distinguich each ditionary.
61
62 */
63 class Backbone : public QObject
64 {
65     Q_OBJECT
66
67 public:
68     /*!\param pluginPath path to plugins (leave blank for default)
69       \param configPath path to folder with configuration files*/
70     Backbone(QString pluginPath="", QString configPath="",
71              bool dry = 0, QObject *parent = 0);
72     ~Backbone();
73     Backbone(const Backbone& b);
74
75     //! \return all loadded dictionaries with activity state flag
76     QHash<CommonDictInterface*, bool> getDictionaries();
77
78     //! \return all loadded plugins
79     QList<CommonDictInterface*> getPlugins();
80
81     //! \return history of performed searches
82     History* history();
83
84     //! \return return search fesult
85     QMultiHash<QString, Translation*> result();
86
87     //! \return maximum number of word that plugin could find
88     int searchLimit() const;
89
90     //! \return final translation (after searching for html)
91     QStringList htmls();
92
93
94 public Q_SLOTS:
95     //! stops all current searches
96     void stopSearching();
97
98     /*! search for a word translation
99        \param word to be translated
100        \param dicts searching in dicionaries
101        \param bookmarks searching in bookmarks
102       */
103     void search(QString word);
104
105     /*! sets active dictionaries (searches are performed only in active dicts
106        \param List of dictionaris to be activated
107       */
108     void selectedDictionaries(QList<CommonDictInterface* >);
109
110     /*! adds new dictionary and activate it
111       \param dict dictionary to be added
112       \param active decides whether searches are perfomed in given dictionaries
113       */
114     void addDictionary(CommonDictInterface* dict, bool active = 1);
115
116
117     //! stops all current activity - emiting signal \see closeOk
118     void quit();
119
120
121     /*! Fired with given interval during searches -
122         checking if translation is ready
123       */
124     void translationReady();
125
126     /*! Fired with given interval during html searches -
127         checking if html is ready
128       */
129     void htmlTranslationReady();
130
131     /*! Removes given dictionary
132         \param dict dictionary to be deleted
133       */
134     void removeDictionary(CommonDictInterface* dict);
135
136     /*! saves plugins new state/configuration after each change */
137     void dictUpdated();
138
139     /*! Performs search for final translation (html/xml) form
140       \param list of Translation* to be searched for
141       */
142     void searchHtml(QList<Translation*>);
143
144
145     /*! add bookmarks to given translations (translation object is fetched and
146       added to bookmarks data base (key and translation stored in db)
147       \param translation translation object  to be stored in db
148       */
149     void addBookmark(QList<Translation*> translations) {
150         foreach(Translation* translation, translations)
151             //_bookmarks.add(translation);
152             QtConcurrent::run(_bookmarks, &Bookmarks::add, translation);
153     }
154
155
156     /*! Remove bookmarks to given translatios
157       \param translation remove bookmark to this translation
158       */
159     void removeBookmark(QList<Translation*> translations) {
160         foreach(Translation* translation, translations)
161             _bookmarks.remove(translation);
162     }
163
164
165
166     /*! Remove all bookmarks
167       */
168     void removeAllBookmark(){
169         _bookmarks.clear();
170     }
171
172
173    /*! Searching for list of bookmarks may take some time, so i moved it to
174        new thread (to avoid gui blocking), when ready bookmarksReady is emited
175        and result is returned after calling getBookmarks()
176        */
177    void fetchBookmarks() {
178         _result.clear();
179
180         stopped = false;
181         dictFin = 1;
182         bookmarkFin = 0;
183
184         if(_searchBookmarks) {
185            _innerBookmarks = QtConcurrent::run(_bookmarks,
186                    &Bookmarks::searchWordList, QString("*"));
187            _bookmarkSearchWatcher.setFuture(_innerBookmarks);
188         }
189    }
190
191    /*! \return list of all bookmarks
192      */
193    QList<Translation*> bookmarks() {
194        return _bookmarksResult;
195    }
196
197
198    /*! Sets settings for backbone: history_size, search_limit,
199        searching backends (search_bookmarks, search_dictionaries)
200        \param settings settings object with opitons set
201        */
202     void setSettings(Settings* settings);
203
204
205     /*! \return coresponding settings object with history_size, search_limit,
206        searching backends (search_bookmarks, search_dictionaries)
207        */
208     Settings* settings();
209
210
211
212
213 Q_SIGNALS:
214     /*! emmited when backbone is ready to close - after getting stop signal it
215         should kill all threads and so on */
216     void closeOk();
217
218     //! emitted when there are search result ready to fetch
219     void ready();
220
221     //! emitted when html result is ready to fetch
222     void htmlReady();
223
224     //! throwed when searches are stopped
225     void searchCanceled();
226
227     //! emmited when bookmark list is ready to fetch
228     void bookmarksReady();
229
230 private Q_SLOTS:
231     void bookmarksListReady();
232
233
234 private:
235     QHash<CommonDictInterface*, bool> _dicts; // List of dictionaries
236     QList<CommonDictInterface*> _plugins;  // List of plugins
237
238
239     QFuture<QList<Translation*> > _innerResult; //Res of concurent word search
240     QFuture<QString> _innerHtmlResult;  // Result of html search
241     QFuture<QList<Translation*> > _innerBookmarks; //Res of search in bookmarks
242     QFuture<QList<Translation*> > _innerListBookmarks; //Res of search in bookmarks
243     QFuture<QStringList> _innerHtmlBookmarks; //Html result of bookmarks search
244
245     QMultiHash<QString, Translation*> _result; //Final result of word search
246     QStringList _htmlResult; // Final result of html search
247     QList<Translation*> _bookmarksResult; // Final result of search in bookmarks
248
249
250     // Keeps track of concurent computations
251     QFutureWatcher<QList<Translation*> > _resultWatcher;
252     QFutureWatcher<QList<Translation*> > _bookmarkWatcher;
253     QFutureWatcher<QList<Translation*> > _bookmarkSearchWatcher;
254     QFutureWatcher<QString> _htmlResultWatcher;
255
256
257     QString _pluginPath, _defaultPluginPath;
258     QString _configPath;
259     QString _defaultConfigPath;
260     int _searchLimit, _defaultSearchLimit;
261     int _activeSearchNum;
262     int _historyLen, _defaultHistoryLen;
263     bool dryRun;
264     bool stopped;
265     bool bookmarkFin, dictFin; // inform whether givent search type is ready
266     bool _searchDicts, _searchBookmarks;
267     Bookmarks _bookmarks;
268
269
270     void init();
271
272     QStringList getFilesFromDir(QString dir, QStringList nameFilter);
273     void loadPlugins(); //< locate and load plugins
274     void loadPrefs(QString fileName);
275     void loadDicts(QString fileName, bool _default=false);
276
277     void saveState(QSettings*, Settings*, bool, uint);
278     void addInternalDictionary(CommonDictInterface*, bool);
279     void savePrefs(QSettings*);
280     void saveDefaultPrefs(QSettings*);
281
282     CommonDictInterface* plugin(QString type); //< search for given type plugin
283     QList<CommonDictInterface*> activeDicts();
284
285
286     History* _history;
287
288 };
289
290 #endif // BACKBONE_H