Added docstrings and changed file header (license + file doc)
[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 <QTimer>
39 #include <QTime>
40 #include <QDir>
41 #include <QSettings>
42 #include "../../includes/CommonDictInterface.h"
43 #include "../../includes/settings.h"
44 #include "../../includes/translation.h"
45 #include "../../includes/History.h"
46
47
48 /*! Inner part of dictionary - glues together GUI and plugins
49
50   Backbone is responsible for managing plugins and dictionaries, starting
51   new searches and threads, merging search results from multiple dictionaries.
52
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.
56
57 */
58 class Backbone : public QObject
59 {
60     Q_OBJECT
61
62 public:
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);
67     ~Backbone();
68     Backbone(const Backbone& b);
69
70     //! \return all loadded dictionaries with activity state flag
71     QHash<CommonDictInterface*, bool> getDictionaries();
72
73     //! \return all loadded plugins
74     QList<CommonDictInterface*> getPlugins();
75
76     //! \return history of performed searches
77     History* history();
78
79     //! \return return search fesult
80     QMultiHash<QString, Translation*> result();
81
82     //! \return maximum number of word that plugin could find
83     int searchLimit() const;
84
85     //! \return number of active searches
86     int activeSearches() 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     QList<QFuture<QList<Translation*> > > _innerResult;
160     QList<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     int _searchLimit, _defaultSearchLimit;
169     int _activeSearchNum;
170     int _interval; //Search fetching timer.timeout interval in msec
171     int _historyLen, _defaultHistoryLen;
172     bool dryRun;
173
174     void init();
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
184
185     History* _history;
186
187 };
188
189 #endif // BACKBONE_H