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>
42 #include "../../includes/CommonDictInterface.h"
43 #include "../../includes/settings.h"
44 #include "../../includes/translation.h"
45 #include "../../includes/History.h"
48 /*! Inner part of dictionary - glues together GUI and plugins
50 Backbone is responsible for managing plugins and dictionaries, starting
51 new searches and threads, merging search results from multiple dictionaries.
53 Each plugin may live in multiple instances - each with its own dictionary,
54 backbone must provide way to create them at start (with specific Settings) and
55 distinguich each ditionary.
58 class Backbone : public QObject
63 /*!\param pluginPath path to plugins (leave blank for default)
64 \param configPath path to folder with configuration files*/
65 Backbone(QString pluginPath="", QString configPath="",
66 bool dry = 0, QObject *parent = 0);
68 Backbone(const Backbone& b);
70 //! \return all loadded dictionaries with activity state flag
71 QHash<CommonDictInterface*, bool> getDictionaries();
73 //! \return all loadded plugins
74 QList<CommonDictInterface*> getPlugins();
76 //! \return history of performed searches
79 //! \return return search fesult
80 QMultiHash<QString, Translation*> result();
82 //! \return maximum number of word that plugin could find
83 int searchLimit() const;
85 //! \return number of active searches
86 int activeSearches() const;
88 /*! Performs search for final translation (html/xml) form
89 \param list of Translation* to be searched for
91 void searchHtml(QList<Translation*>);
93 //! \return final translation (after searching for html)
98 //! stops all current searches
101 /*! search for a word translation
102 \param word to be translated
104 void search(QString word);
106 /*! sets active dictionaries (searches are performed only in active dicts
107 \param List of dictionaris to be activated
109 void selectedDictionaries(QList<CommonDictInterface* >);
111 /*! adds new dictionary and activate it
112 \param dict dictionary to be added
113 \param active decides whether searches are perfomed in given dictionaries
115 void addDictionary(CommonDictInterface* dict, bool active = 1);
118 //! stops all current activity - emiting signal \see closeOk
122 /*! Fired with given interval during searches -
123 checking if translation is ready
125 void translationReady();
127 /*! Fired with given interval during html searches -
128 checking if html is ready
130 void htmlTranslationReady();
132 /*! Removes given dictionary
133 \param dict dictionary to be deleted
135 void removeDictionary(CommonDictInterface* dict);
137 /*! saves plugins new state/configuration after each change */
140 // TODO addToBookmark(Translation*);
141 // TODO removeFromBookmark(Translation*);
144 /*! emmited when backbone is ready to close - after getting stop signal it
145 should kill all threads and so on */
148 //! emitted when there are search result ready to fetch
151 //! emitted when html result is ready to fetch
157 QHash<CommonDictInterface*, bool> _dicts;
158 QList<CommonDictInterface*> _plugins;
159 QList<QFuture<QList<Translation*> > > _innerResult;
160 QList<QFuture<QString> > _innerHtmlResult;
161 QMultiHash<QString, Translation*> _result;
162 QStringList _htmlResult;
163 QTimer _timerSearch, _timerHtmlSearch;
165 QString _pluginPath, _defaultPluginPath;
167 QString _defaultConfigPath;
168 int _searchLimit, _defaultSearchLimit;
169 int _activeSearchNum;
170 int _interval; //Search fetching timer.timeout interval in msec
171 int _historyLen, _defaultHistoryLen;
175 QStringList getFilesFromDir(QString dir, QStringList nameFilter);
176 void loadPlugins(); //< locate and load plugins
177 void loadPrefs(QString fileName);
178 void loadDicts(QString fileName, bool _default=false);
179 void saveState(QSettings*, Settings*, bool, uint);
180 void addInternalDictionary(CommonDictInterface*, bool);
181 void savePrefs(QSettings*);
182 void saveDefaultPrefs(QSettings*);
183 CommonDictInterface* plugin(QString type); //< search for given type plugin