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 // Created by Bartosz Szatkowski
30 #include <QPluginLoader>
32 #include <QtConcurrentRun>
37 #include "../../includes/CommonDictInterface.h"
38 #include "../../includes/settings.h"
39 #include "../../includes/translation.h"
40 #include "../../includes/History.h"
43 /*! Inner part of dictionary - glues together GUI and plugins
45 Backbone is responsible for managing plugins and dictionaries, starting
46 new searches and threads, merging search results from multiple dictionaries.
48 Each plugin may live in multiple instances - each with its own dictionary,
49 backbone must provide way to create them at start (with specific Settings) and
50 distinguich each ditionary.
53 class Backbone : public QObject
58 /*!\param pluginPath path to plugins (leave blank for default)
59 \param configPath path to folder with configuration files*/
60 Backbone(QString pluginPath="", QString configPath="",
61 bool dry = 0, QObject *parent = 0);
63 Backbone(const Backbone& b);
65 //! \return all loadded dictionaries with activity state flag
66 QHash<CommonDictInterface*, bool> getDictionaries();
68 //! \return all loadded plugins
69 QList<CommonDictInterface*> getPlugins();
71 //! \return history of performed searches
74 //! \return return search fesult
75 QMultiHash<QString, Translation*> result();
77 //! \return maximum number of word that plugin could find
78 int searchLimit() const;
80 //! \return number of active searches
81 int activeSearches() const;
85 //! \return final translation (after searching for html)
90 //! stops all current searches
93 /*! search for a word translation
94 \param word to be translated
96 void search(QString word);
98 /*! sets active dictionaries (searches are performed only in active dicts
99 \param List of dictionaris to be activated
101 void selectedDictionaries(QList<CommonDictInterface* >);
103 /*! adds new dictionary and activate it
104 \param dict dictionary to be added
105 \param active decides whether searches are perfomed in given dictionaries
107 void addDictionary(CommonDictInterface* dict, bool active = 1);
110 //! stops all current activity - emiting signal \see closeOk
114 /*! Fired with given interval during searches -
115 checking if translation is ready
117 void translationReady();
119 /*! Fired with given interval during html searches -
120 checking if html is ready
122 void htmlTranslationReady();
124 /*! Removes given dictionary
125 \param dict dictionary to be deleted
127 void removeDictionary(CommonDictInterface* dict);
129 /*! saves plugins new state/configuration after each change */
132 /*! Performs search for final translation (html/xml) form
133 \param list of Translation* to be searched for
135 void searchHtml(QList<Translation*>);
137 // TODO addToBookmark(Translation*);
138 // TODO removeFromBookmark(Translation*);
141 /*! emmited when backbone is ready to close - after getting stop signal it
142 should kill all threads and so on */
145 //! emitted when there are search result ready to fetch
148 //! emitted when html result is ready to fetch
154 QHash<CommonDictInterface*, bool> _dicts;
155 QList<CommonDictInterface*> _plugins;
156 QList<QFuture<QList<Translation*> > > _innerResult;
157 QList<QFuture<QString> > _innerHtmlResult;
158 QMultiHash<QString, Translation*> _result;
159 QStringList _htmlResult;
160 QTimer _timerSearch, _timerHtmlSearch;
162 QString _pluginPath, _defaultPluginPath;
164 QString _defaultConfigPath;
165 int _searchLimit, _defaultSearchLimit;
166 int _activeSearchNum;
167 int _interval; //Search fetching timer.timeout interval in msec
168 int _historyLen, _defaultHistoryLen;
172 QStringList getFilesFromDir(QString dir, QStringList nameFilter);
173 void loadPlugins(); //< locate and load plugins
174 void loadPrefs(QString fileName);
175 void loadDicts(QString fileName, bool _default=false);
176 void saveState(QSettings*, Settings*, bool, uint);
177 void addInternalDictionary(CommonDictInterface*, bool);
178 void savePrefs(QSettings*);
179 void saveDefaultPrefs(QSettings*);
180 CommonDictInterface* plugin(QString type); //< search for given type plugin