Added tests and some maintenance
[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 "../../includes/CommonDictInterface.h"
37 #include "../../includes/settings.h"
38 #include "../../includes/translation.h"
39
40
41 //! Inner part of dictionary - glues together GUI and plugins
42 class Backbone : public QObject
43 {
44     Q_OBJECT
45
46 public:
47     Backbone(QObject *parent = 0);
48     ~Backbone();
49     Backbone(const Backbone& b);
50
51     //! \return all loadded dictionaries with activity state flag
52     QHash<CommonDictInterface*, bool> getDictionaries();
53
54     //! \return all loadded plugins
55     QList<CommonDictInterface*> getPlugins();
56
57     //! \return history of performed searches
58     QList<QString> getHistory(); //TODO implementation needed (in future)
59
60     //! \return return search fesult
61     QMultiHash<QString, Translation*> result();
62
63     //! \return maximum number of word that plugin could find
64     int searchLimit() const;
65
66     //! \return number of active searches
67     int activeSearches() const;
68
69 public Q_SLOTS:
70     //! stops all current searches
71     void stopSearching();
72
73     //! search for a word translation
74     void search(QString word);
75
76     //! sets active dictionaries
77     void selectedDictionaries(QList<CommonDictInterface* >);
78
79     //! adds new dictionary
80     void addDictionary(CommonDictInterface* dict);
81
82
83     //! stops all current activity and kill plugins - be ready to exit
84     void quit();
85
86
87     //! Fired when dictionary call finalTranslation(..) with translation ready
88     void translation();
89
90     //! Removes given dictionary
91     void removeDictionary(CommonDictInterface* dict);
92
93     // TODO addToBookmark(Translation*);
94     // TODO removeFromBookmark(Translation*);
95
96 Q_SIGNALS:
97     //! emmited when backbone is ready to close - after getting stop signal it
98     //!     should kill all threads and so on
99     void closeOk();
100
101     //! emitted when there are search result ready to fetch
102     void ready();
103
104
105
106 private:
107     void loadPlugins(); //< locate and load plugins
108     QHash<CommonDictInterface*, bool> _dicts;
109     QList<CommonDictInterface*> _plugins;
110     QList<QFuture<QList<Translation*> > > _innerResult;
111     QMultiHash<QString, Translation*> _result;
112     QTimer _timer;
113     int _searchLimit;
114     int _activeSearchNum;
115     QTime _time;
116     int _interval; //Search fetching timer.timeout interval in msec
117     QString _pluginPath;
118
119     void init();
120
121 };
122
123 #endif // BACKBONE_H