74a37373cd617ed21e706a888ecfd8b7d6e09868
[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 // Created by Bartosz Szatkowski
23
24 #ifndef BACKBONE_H
25 #define BACKBONE_H
26
27 #include <QObject>
28 #include <QList>
29 #include <QHash>
30 #include <QPluginLoader>
31 #include <QFuture>
32 #include <QtConcurrentRun>
33 #include <QTimer>
34 #include <QTime>
35 #include <QDir>
36 #include <QSettings>
37 #include "../../includes/CommonDictInterface.h"
38 #include "../../includes/settings.h"
39 #include "../../includes/translation.h"
40 #include "../../includes/History.h"
41
42
43 /*! Inner part of dictionary - glues together GUI and plugins
44
45   Backbone is responsible for managing plugins and dictionaries, starting
46   new searches and threads, merging search results from multiple dictionaries.
47
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.
51
52 */
53 class Backbone : public QObject
54 {
55     Q_OBJECT
56
57 public:
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="", QObject *parent = 0);
61     ~Backbone();
62     Backbone(const Backbone& b);
63
64     //! \return all loadded dictionaries with activity state flag
65     QHash<CommonDictInterface*, bool> getDictionaries();
66
67     //! \return all loadded plugins
68     QList<CommonDictInterface*> getPlugins();
69
70     //! \return history of performed searches
71     History* history();
72
73     //! \return return search fesult
74     QMultiHash<QString, Translation*> result();
75
76     //! \return maximum number of word that plugin could find
77     int searchLimit() const;
78
79     //! \return number of active searches
80     int activeSearches() const;
81
82     /*! Performs search for final translation (html/xml) form
83       \param list of Translation* to be searched for
84       */
85     void searchHtml(QList<Translation*>);
86
87     //! \return final translation (after searching for html)
88     QStringList htmls();
89
90
91 public Q_SLOTS:
92     //! stops all current searches
93     void stopSearching();
94
95     /*! search for a word translation
96        \param word to be translated
97       */
98     void search(QString word);
99
100     /*! sets active dictionaries (searches are performed only in active dicts
101        \param List of dictionaris to be activated
102       */
103     void selectedDictionaries(QList<CommonDictInterface* >);
104
105     /*! adds new dictionary and activate it
106       \param dict dictionary to be added
107       \param active decides whether searches are perfomed in given dictionaries
108       */
109     void addDictionary(CommonDictInterface* dict, bool active = 1);
110
111
112     //! stops all current activity - emiting signal \see closeOk
113     void quit();
114
115
116     /*! Fired with given interval during searches -
117         checking if translation is ready
118       */
119     void translationReady();
120
121     /*! Fired with given interval during html searches -
122         checking if html is ready
123       */
124     void htmlTranslationReady();
125
126     /*! Removes given dictionary
127         \param dict dictionary to be deleted
128       */
129     void removeDictionary(CommonDictInterface* dict);
130
131     /*! saves plugins new state/configuration after each change */
132     void dictUpdated();
133
134     // TODO addToBookmark(Translation*);
135     // TODO removeFromBookmark(Translation*);
136
137 Q_SIGNALS:
138     /*! emmited when backbone is ready to close - after getting stop signal it
139         should kill all threads and so on */
140     void closeOk();
141
142     //! emitted when there are search result ready to fetch
143     void ready();
144
145     //! emitted when html result is ready to fetch
146     void htmlReady();
147
148
149
150 private:
151     QHash<CommonDictInterface*, bool> _dicts;
152     QList<CommonDictInterface*> _plugins;
153     QList<QFuture<QList<Translation*> > > _innerResult;
154     QList<QFuture<QString> > _innerHtmlResult;
155     QMultiHash<QString, Translation*> _result;
156     QStringList _htmlResult;
157     QTimer _timerSearch, _timerHtmlSearch;
158     QTime _time;
159     QString _pluginPath, _defaultPluginPath;
160     QString _configPath;
161     QString _defaultConfigPath;
162     int _searchLimit, _defaultSearchLimit;
163     int _activeSearchNum;
164     int _interval; //Search fetching timer.timeout interval in msec
165     int _historyLen, _defaultHistoryLen;
166     bool dryRun;
167
168     void init();
169     QStringList getFilesFromDir(QString dir, QStringList nameFilter);
170     void loadPlugins(); //< locate and load plugins
171     void loadPrefs(QString fileName);
172     void loadDicts(QString fileName, bool _default=false);
173     void saveState(QSettings*, Settings*, bool, uint);
174     void addInternalDictionary(CommonDictInterface*, bool);
175     void savePrefs(QSettings*);
176     void saveDefaultPrefs(QSettings*);
177     CommonDictInterface* plugin(QString type); //< search for given type plugin
178
179     History* _history;
180
181 };
182
183 #endif // BACKBONE_H