Set number of thred to idealCount+1=cores number+1
[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 <QThread>
44 #include <QSettings>
45 #include "../../includes/CommonDictInterface.h"
46 #include "../../includes/settings.h"
47 #include "../../includes/translation.h"
48 #include "../../includes/History.h"
49
50
51 /*! Inner part of dictionary - glues together GUI and plugins
52
53   Backbone is responsible for managing plugins and dictionaries, starting
54   new searches and threads, merging search results from multiple dictionaries.
55
56   Each plugin may live in multiple instances - each with its own dictionary,
57   backbone must provide way to create them at start (with specific Settings) and
58   distinguich each ditionary.
59
60 */
61 class Backbone : public QObject
62 {
63     Q_OBJECT
64
65 public:
66     /*!\param pluginPath path to plugins (leave blank for default)
67       \param configPath path to folder with configuration files*/
68     Backbone(QString pluginPath="", QString configPath="",
69              bool dry = 0, QObject *parent = 0);
70     ~Backbone();
71     Backbone(const Backbone& b);
72
73     //! \return all loadded dictionaries with activity state flag
74     QHash<CommonDictInterface*, bool> getDictionaries();
75
76     //! \return all loadded plugins
77     QList<CommonDictInterface*> getPlugins();
78
79     //! \return history of performed searches
80     History* history();
81
82     //! \return return search fesult
83     QMultiHash<QString, Translation*> result();
84
85     //! \return maximum number of word that plugin could find
86     int searchLimit() const;
87
88     /*! Performs search for final translation (html/xml) form
89       \param list of Translation* to be searched for
90       */
91     void searchHtml(QList<Translation*>);
92
93     //! \return final translation (after searching for html)
94     QStringList htmls();
95
96
97 public Q_SLOTS:
98     //! stops all current searches
99     void stopSearching();
100
101     /*! search for a word translation
102        \param word to be translated
103       */
104     void search(QString word);
105
106     /*! sets active dictionaries (searches are performed only in active dicts
107        \param List of dictionaris to be activated
108       */
109     void selectedDictionaries(QList<CommonDictInterface* >);
110
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
114       */
115     void addDictionary(CommonDictInterface* dict, bool active = 1);
116
117
118     //! stops all current activity - emiting signal \see closeOk
119     void quit();
120
121
122     /*! Fired with given interval during searches -
123         checking if translation is ready
124       */
125     void translationReady();
126
127     /*! Fired with given interval during html searches -
128         checking if html is ready
129       */
130     void htmlTranslationReady();
131
132     /*! Removes given dictionary
133         \param dict dictionary to be deleted
134       */
135     void removeDictionary(CommonDictInterface* dict);
136
137     /*! saves plugins new state/configuration after each change */
138     void dictUpdated();
139
140     // TODO addToBookmark(Translation*);
141     // TODO removeFromBookmark(Translation*);
142
143 Q_SIGNALS:
144     /*! emmited when backbone is ready to close - after getting stop signal it
145         should kill all threads and so on */
146     void closeOk();
147
148     //! emitted when there are search result ready to fetch
149     void ready();
150
151     //! emitted when html result is ready to fetch
152     void htmlReady();
153
154
155
156 private:
157     QHash<CommonDictInterface*, bool> _dicts;
158     QList<CommonDictInterface*> _plugins;
159     QFuture<QList<Translation*> > _innerResult;
160     QFuture<QString> _innerHtmlResult;
161     QMultiHash<QString, Translation*> _result;
162     QStringList _htmlResult;
163     QTimer _timerSearch, _timerHtmlSearch;
164     QTime _time;
165     QString _pluginPath, _defaultPluginPath;
166     QString _configPath;
167     QString _defaultConfigPath;
168     //QString _mappedSearch;
169     int _searchLimit, _defaultSearchLimit;
170     int _activeSearchNum;
171     int _interval; //Search fetching timer.timeout interval in msec
172     int _historyLen, _defaultHistoryLen;
173     bool dryRun;
174
175
176     void init();
177     QStringList getFilesFromDir(QString dir, QStringList nameFilter);
178     void loadPlugins(); //< locate and load plugins
179     void loadPrefs(QString fileName);
180     void loadDicts(QString fileName, bool _default=false);
181     void saveState(QSettings*, Settings*, bool, uint);
182     void addInternalDictionary(CommonDictInterface*, bool);
183     void savePrefs(QSettings*);
184     void saveDefaultPrefs(QSettings*);
185     CommonDictInterface* plugin(QString type); //< search for given type plugin
186     QList<CommonDictInterface*> activeDicts();
187
188     //QList<Translation*> mapSearch(CommonDictInterface*) const;
189
190     History* _history;
191
192 };
193
194 #endif // BACKBONE_H