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
55 Backbone is responsible for managing plugins and dictionaries, starting
56 new searches and threads, merging search results from multiple dictionaries.
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.
63 class Backbone : public QObject
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);
73 Backbone(const Backbone& b);
75 //! \return all loadded dictionaries with activity state flag
76 QHash<CommonDictInterface*, bool> getDictionaries();
78 //! \return all loadded plugins
79 QList<CommonDictInterface*> getPlugins();
81 //! \return history of performed searches
84 //! \return return search fesult
85 QMultiHash<QString, Translation*> result();
87 //! \return maximum number of word that plugin could find
88 int searchLimit() const;
90 //! \return final translation (after searching for html)
95 //! stops all current searches
98 /*! search for a word translation
99 \param word to be translated
100 \param dicts searching in dicionaries
101 \param bookmarks searching in bookmarks
103 void search(QString word);
105 /*! sets active dictionaries (searches are performed only in active dicts
106 \param List of dictionaris to be activated
108 void selectedDictionaries(QList<CommonDictInterface* >);
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
114 void addDictionary(CommonDictInterface* dict, bool active = 1);
117 //! stops all current activity - emiting signal \see closeOk
121 /*! Fired with given interval during searches -
122 checking if translation is ready
124 void translationReady();
126 /*! Fired with given interval during html searches -
127 checking if html is ready
129 void htmlTranslationReady();
131 /*! Removes given dictionary
132 \param dict dictionary to be deleted
134 void removeDictionary(CommonDictInterface* dict);
136 /*! saves plugins new state/configuration after each change */
139 /*! Performs search for final translation (html/xml) form
140 \param list of Translation* to be searched for
142 void searchHtml(QList<Translation*>);
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
149 void addBookmark(QList<Translation*> translations) {
150 foreach(Translation* translation, translations)
151 _bookmarks.add(translation);
152 //QtConcurrent::run(&bookmarks, &Bookmarks::add, translation);
156 /*! Remove bookmarks to given translatios
157 \param translation remove bookmark to this translation
159 void removeBookmark(QList<Translation*> translations) {
160 foreach(Translation* translation, translations)
161 _bookmarks.remove(translation);
165 /*! Searching for list of bookmarks may take some time, so i moved it to
166 new thread (to avoid gui blocking), when ready bookmarksReady is emited
167 and result is returned after calling getBookmarks()
169 void fetchBookmarks() {
170 _bookmarksResult.clear();
171 _innerListBookmarks = QtConcurrent::run(_bookmarks, &Bookmarks::list);
172 _bookmarkWatcher.setFuture(_innerListBookmarks);
175 /*! \return list of all bookmarks
177 QList<Translation*> getBookmarks() {
178 return _bookmarksResult;
182 /*! Sets settings for backbone: history_size, search_limit,
183 searching backends (search_bookmarks, search_dictionaries)
184 \param settings settings object with opitons set
186 void setSettings(Settings* settings);
189 /*! \return coresponding settings object with history_size, search_limit,
190 searching backends (search_bookmarks, search_dictionaries)
192 Settings* settings();
198 /*! emmited when backbone is ready to close - after getting stop signal it
199 should kill all threads and so on */
202 //! emitted when there are search result ready to fetch
205 //! emitted when html result is ready to fetch
208 //! throwed when searches are stopped
209 void searchCanceled();
211 //! emmited when bookmark list is ready to fetch
212 void bookmarksReady();
215 void bookmarksListReady();
219 QHash<CommonDictInterface*, bool> _dicts; // List of dictionaries
220 QList<CommonDictInterface*> _plugins; // List of plugins
223 QFuture<QList<Translation*> > _innerResult; //Res of concurent word search
224 QFuture<QString> _innerHtmlResult; // Result of html search
225 QFuture<QList<Translation*> > _innerBookmarks; //Res of search in bookmarks
226 QFuture<QList<Translation*> > _innerListBookmarks; //Res of search in bookmarks
227 QFuture<QStringList> _innerHtmlBookmarks; //Html result of bookmarks search
229 QMultiHash<QString, Translation*> _result; //Final result of word search
230 QStringList _htmlResult; // Final result of html search
231 QList<Translation*> _bookmarksResult; // Final result of search in bookmarks
234 // Keeps track of concurent computations
235 QFutureWatcher<QList<Translation*> > _resultWatcher;
236 QFutureWatcher<QList<Translation*> > _bookmarkWatcher;
237 QFutureWatcher<QList<Translation*> > _bookmarkSearchWatcher;
238 QFutureWatcher<QString> _htmlResultWatcher;
241 QString _pluginPath, _defaultPluginPath;
243 QString _defaultConfigPath;
244 int _searchLimit, _defaultSearchLimit;
245 int _activeSearchNum;
246 int _historyLen, _defaultHistoryLen;
249 bool bookmarkFin, dictFin; // inform whether givent search type is ready
250 bool _searchDicts, _searchBookmarks;
251 Bookmarks _bookmarks;
256 QStringList getFilesFromDir(QString dir, QStringList nameFilter);
257 void loadPlugins(); //< locate and load plugins
258 void loadPrefs(QString fileName);
259 void loadDicts(QString fileName, bool _default=false);
261 void saveState(QSettings*, Settings*, bool, uint);
262 void addInternalDictionary(CommonDictInterface*, bool);
263 void savePrefs(QSettings*);
264 void saveDefaultPrefs(QSettings*);
266 CommonDictInterface* plugin(QString type); //< search for given type plugin
267 QList<CommonDictInterface*> activeDicts();