Changed thread handling for translation fetching
[mdictionary] / trunk / src / base / backbone / backbone.h
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
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.
9
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.
14
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/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*! /file backbone.cpp
23 \brief Backbone/core main header \see Backbone
24
25
26 \author Bartosz Szatkowski <bulislaw@linux.com>
27 */
28
29 #ifndef BACKBONE_H
30 #define BACKBONE_H
31
32 #include <QObject>
33 #include <QList>
34 #include <QHash>
35 #include <QPluginLoader>
36 #include <QFuture>
37 #include <QtConcurrentRun>
38 #include <QtConcurrentMap>
39 #include <QFutureIterator>
40 #include <QTimer>
41 #include <QTime>
42 #include <QDir>
43 #include <QSettings>
44 #include "../../includes/CommonDictInterface.h"
45 #include "../../includes/settings.h"
46 #include "../../includes/translation.h"
47 #include "../../includes/History.h"
48
49
50 /*! Inner part of dictionary - glues together GUI and plugins
51
52   Backbone is responsible for managing plugins and dictionaries, starting
53   new searches and threads, merging search results from multiple dictionaries.
54
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.
58
59 */
60 class Backbone : public QObject
61 {
62     Q_OBJECT
63
64 public:
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);
69     ~Backbone();
70     Backbone(const Backbone& b);
71
72     //! \return all loadded dictionaries with activity state flag
73     QHash<CommonDictInterface*, bool> getDictionaries();
74
75     //! \return all loadded plugins
76     QList<CommonDictInterface*> getPlugins();
77
78     //! \return history of performed searches
79     History* history();
80
81     //! \return return search fesult
82     QMultiHash<QString, Translation*> result();
83
84     //! \return maximum number of word that plugin could find
85     int searchLimit() const;
86
87     //! \return number of active searches
88     int activeSearches() const;
89
90     /*! Performs search for final translation (html/xml) form
91       \param list of Translation* to be searched for
92       */
93     void searchHtml(QList<Translation*>);
94
95     //! \return final translation (after searching for html)
96     QStringList htmls();
97
98
99 public Q_SLOTS:
100     //! stops all current searches
101     void stopSearching();
102
103     /*! search for a word translation
104        \param word to be translated
105       */
106     void search(QString word);
107
108     /*! sets active dictionaries (searches are performed only in active dicts
109        \param List of dictionaris to be activated
110       */
111     void selectedDictionaries(QList<CommonDictInterface* >);
112
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
116       */
117     void addDictionary(CommonDictInterface* dict, bool active = 1);
118
119
120     //! stops all current activity - emiting signal \see closeOk
121     void quit();
122
123
124     /*! Fired with given interval during searches -
125         checking if translation is ready
126       */
127     void translationReady();
128
129     /*! Fired with given interval during html searches -
130         checking if html is ready
131       */
132     void htmlTranslationReady();
133
134     /*! Removes given dictionary
135         \param dict dictionary to be deleted
136       */
137     void removeDictionary(CommonDictInterface* dict);
138
139     /*! saves plugins new state/configuration after each change */
140     void dictUpdated();
141
142     // TODO addToBookmark(Translation*);
143     // TODO removeFromBookmark(Translation*);
144
145 Q_SIGNALS:
146     /*! emmited when backbone is ready to close - after getting stop signal it
147         should kill all threads and so on */
148     void closeOk();
149
150     //! emitted when there are search result ready to fetch
151     void ready();
152
153     //! emitted when html result is ready to fetch
154     void htmlReady();
155
156
157
158 private:
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;
166     QTime _time;
167     QString _pluginPath, _defaultPluginPath;
168     QString _configPath;
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;
175     bool dryRun;
176
177
178     void init();
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();
189
190     //QList<Translation*> mapSearch(CommonDictInterface*) const;
191
192     History* _history;
193
194 };
195
196 #endif // BACKBONE_H