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>
44 #include "../../includes/CommonDictInterface.h"
45 #include "../../includes/settings.h"
46 #include "../../includes/translation.h"
47 #include "../../includes/History.h"
50 /*! Inner part of dictionary - glues together GUI and plugins
52 Backbone is responsible for managing plugins and dictionaries, starting
53 new searches and threads, merging search results from multiple dictionaries.
55 Each plugin may live in multiple instances - each with its own dictionary,
56 backbone must provide way to create them at start (with specific Settings) and
57 distinguich each ditionary.
60 class Backbone : public QObject
65 /*!\param pluginPath path to plugins (leave blank for default)
66 \param configPath path to folder with configuration files*/
67 Backbone(QString pluginPath="", QString configPath="",
68 bool dry = 0, QObject *parent = 0);
70 Backbone(const Backbone& b);
72 //! \return all loadded dictionaries with activity state flag
73 QHash<CommonDictInterface*, bool> getDictionaries();
75 //! \return all loadded plugins
76 QList<CommonDictInterface*> getPlugins();
78 //! \return history of performed searches
81 //! \return return search fesult
82 QMultiHash<QString, Translation*> result();
84 //! \return maximum number of word that plugin could find
85 int searchLimit() const;
87 //! \return number of active searches
88 int activeSearches() const;
90 /*! Performs search for final translation (html/xml) form
91 \param list of Translation* to be searched for
93 void searchHtml(QList<Translation*>);
95 //! \return final translation (after searching for html)
100 //! stops all current searches
101 void stopSearching();
103 /*! search for a word translation
104 \param word to be translated
106 void search(QString word);
108 /*! sets active dictionaries (searches are performed only in active dicts
109 \param List of dictionaris to be activated
111 void selectedDictionaries(QList<CommonDictInterface* >);
113 /*! adds new dictionary and activate it
114 \param dict dictionary to be added
115 \param active decides whether searches are perfomed in given dictionaries
117 void addDictionary(CommonDictInterface* dict, bool active = 1);
120 //! stops all current activity - emiting signal \see closeOk
124 /*! Fired with given interval during searches -
125 checking if translation is ready
127 void translationReady();
129 /*! Fired with given interval during html searches -
130 checking if html is ready
132 void htmlTranslationReady();
134 /*! Removes given dictionary
135 \param dict dictionary to be deleted
137 void removeDictionary(CommonDictInterface* dict);
139 /*! saves plugins new state/configuration after each change */
142 // TODO addToBookmark(Translation*);
143 // TODO removeFromBookmark(Translation*);
146 /*! emmited when backbone is ready to close - after getting stop signal it
147 should kill all threads and so on */
150 //! emitted when there are search result ready to fetch
153 //! emitted when html result is ready to fetch
159 QHash<CommonDictInterface*, bool> _dicts;
160 QList<CommonDictInterface*> _plugins;
161 QFuture<QList<Translation*> > _innerResult;
162 QFuture<QString> _innerHtmlResult;
163 QMultiHash<QString, Translation*> _result;
164 QStringList _htmlResult;
165 QTimer _timerSearch, _timerHtmlSearch;
167 QString _pluginPath, _defaultPluginPath;
169 QString _defaultConfigPath;
170 //QString _mappedSearch;
171 int _searchLimit, _defaultSearchLimit;
172 int _activeSearchNum;
173 int _interval; //Search fetching timer.timeout interval in msec
174 int _historyLen, _defaultHistoryLen;
179 QStringList getFilesFromDir(QString dir, QStringList nameFilter);
180 void loadPlugins(); //< locate and load plugins
181 void loadPrefs(QString fileName);
182 void loadDicts(QString fileName, bool _default=false);
183 void saveState(QSettings*, Settings*, bool, uint);
184 void addInternalDictionary(CommonDictInterface*, bool);
185 void savePrefs(QSettings*);
186 void saveDefaultPrefs(QSettings*);
187 CommonDictInterface* plugin(QString type); //< search for given type plugin
188 QList<CommonDictInterface*> activeDicts();
190 //QList<Translation*> mapSearch(CommonDictInterface*) const;