1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 /*! /file backbone.cpp
23 \brief Backbone/core main header \see Backbone
26 \author Bartosz Szatkowski <bulislaw@linux.com>
35 #include <QPluginLoader>
37 #include <QtConcurrentRun>
38 #include <QtConcurrentMap>
39 #include <QFutureIterator>
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"
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
56 Backbone is responsible for managing plugins and dictionaries, starting
57 new searches and threads, merging search results from multiple dictionaries.
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.
63 Backbone also manage bookmarks and history: providing interface to gui
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).
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
79 class Backbone : public QObject
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);
89 Backbone(const Backbone& b);
91 //! \return all loadded dictionaries with activity state flag
92 QHash<CommonDictInterface*, bool> getDictionaries();
94 //! \return all loadded plugins
95 QList<CommonDictInterface*> getPlugins();
97 //! \return history of performed searches
100 //! \return return search fesult
101 QMultiHash<QString, Translation*> result();
103 //! \return maximum number of word that plugin could find
104 int searchLimit() const;
106 //! \return final translation (after searching for html)
111 //! stops all current searches and emiting searchCanceled signal
112 void stopSearching();
114 /*! search for a word translation
115 \param word to be translated
117 void search(QString word);
119 /*! sets active dictionaries (searches are performed only in active dicts
120 \param List of dictionaris to be activated
122 void selectedDictionaries(QList<CommonDictInterface* >);
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
128 void addDictionary(CommonDictInterface* dict, bool active = 1);
131 //! stops all current activity - emiting signal \see closeOk
135 /*! Fired with given interval during searches -
136 checking if translation is ready
138 void translationReady();
140 /*! Fired with given interval during html searches -
141 checking if html is ready
143 void htmlTranslationReady();
145 /*! Removes given dictionary
146 \param dict dictionary to be deleted
148 void removeDictionary(CommonDictInterface* dict);
150 /*! saves plugins new state/configuration after each change */
153 /*! Performs search for final translation (html/xml) form
154 \param list of Translation* to be searched for
156 void searchHtml(QList<Translation*>);
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
163 void addBookmark(QList<Translation*> translations) {
164 foreach(Translation* translation, translations)
165 //_bookmarks.add(translation);
166 QtConcurrent::run(_bookmarks, &Bookmarks::add, translation);
170 /*! Remove bookmarks to given translatios
171 \param translation remove bookmark to this translation
173 void removeBookmark(QList<Translation*> translations) {
174 foreach(Translation* translation, translations)
175 _bookmarks.remove(translation);
180 /*! Remove all bookmarks
182 void removeAllBookmark(){
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)
191 void fetchBookmarks() {
198 if(_searchBookmarks) {
199 _innerBookmarks = QtConcurrent::run(_bookmarks,
200 &Bookmarks::searchWordList, QString("*"));
201 _bookmarkSearchWatcher.setFuture(_innerBookmarks);
207 /*! Sets settings for backbone: history_size, search_limit,
208 searching backends (search_bookmarks, search_dictionaries)
209 \param settings settings object with opitons set
211 void setSettings(Settings* settings);
214 /*! \return coresponding settings object with history_size, search_limit,
215 searching backends (search_bookmarks, search_dictionaries)
217 Settings* settings();
223 /*! emmited when backbone is ready to close - after getting stop signal it
224 should kill all threads and so on */
227 //! emitted when there are search result ready to fetch
230 //! emitted when html result is ready to fetch
233 //! throwed when searches are stopped
234 void searchCanceled();
236 //! emmited when bookmark list is ready to fetch
237 void bookmarksReady();
240 void bookmarksListReady();
244 QHash<CommonDictInterface*, bool> _dicts; // List of dictionaries
245 QList<CommonDictInterface*> _plugins; // List of plugins
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
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
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;
266 QString _pluginPath, _defaultPluginPath;
268 QString _defaultConfigPath;
269 int _searchLimit, _defaultSearchLimit;
270 int _historyLen, _defaultHistoryLen;
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
277 Bookmarks _bookmarks;
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);
287 void saveState(QSettings*, Settings*, bool, uint);
288 void addInternalDictionary(CommonDictInterface*, bool);
289 void savePrefs(QSettings*);
290 void saveDefaultPrefs(QSettings*);
292 CommonDictInterface* plugin(QString type); // search for given type plugin
293 QList<CommonDictInterface*> activeDicts();