Fixed test handling for backbone
[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
43   Backbone is responsible for managing plugins and dictionaries, starting
44   new searches and threads, merging search results from multiple dictionaries.
45
46   Each plugin may live in multiple instances - each with its own dictionary,
47   backbone must provide way to create them at start (with specific Settings) and
48   distinguich each ditionary.
49
50 */
51 class Backbone : public QObject
52 {
53     Q_OBJECT
54
55 public:
56     Backbone(QObject *parent = 0);
57     //! \param pluginPath path to plugins
58     Backbone(QString pluginPath, QObject *parent = 0);
59     ~Backbone();
60     Backbone(const Backbone& b);
61
62     //! \return all loadded dictionaries with activity state flag
63     QHash<CommonDictInterface*, bool> getDictionaries();
64
65     //! \return all loadded plugins
66     QList<CommonDictInterface*> getPlugins();
67
68     //! \return history of performed searches
69     QList<QString> getHistory(); //TODO implementation needed (in future)
70
71     //! \return return search fesult
72     QMultiHash<QString, Translation*> result();
73
74     //! \return maximum number of word that plugin could find
75     int searchLimit() const;
76
77     //! \return number of active searches
78     int activeSearches() const;
79
80 public Q_SLOTS:
81     //! stops all current searches
82     void stopSearching();
83
84     /*! search for a word translation
85        \param word word to be translated
86       */
87     void search(QString word);
88
89     /*! sets active dictionaries (searches are performed only in active dicts
90        \param List of dictionaris to be activated
91       */
92     void selectedDictionaries(QList<CommonDictInterface* >);
93
94     /*! adds new dictionary and activate it
95       \param dict dictionary to be added
96       */
97     void addDictionary(CommonDictInterface* dict);
98
99
100     //! stops all current activity - emiting signal \see closeOk
101     void quit();
102
103
104     /*! Fired with given interval during searches -
105         checking if translation is ready
106       */
107     void translation();
108
109     /*! Removes given dictionary
110         \param dict dictionary to be deleted
111       */
112     void removeDictionary(CommonDictInterface* dict);
113
114     // TODO addToBookmark(Translation*);
115     // TODO removeFromBookmark(Translation*);
116
117 Q_SIGNALS:
118     /*! emmited when backbone is ready to close - after getting stop signal it
119         should kill all threads and so on */
120     void closeOk();
121
122     //! emitted when there are search result ready to fetch
123     void ready();
124
125
126
127 private:
128     void loadPlugins(); //< locate and load plugins
129     QHash<CommonDictInterface*, bool> _dicts;
130     QList<CommonDictInterface*> _plugins;
131     QList<QFuture<QList<Translation*> > > _innerResult;
132     QMultiHash<QString, Translation*> _result;
133     QTimer _timer;
134     int _searchLimit;
135     int _activeSearchNum;
136     QTime _time;
137     int _interval; //Search fetching timer.timeout interval in msec
138     QString _pluginPath;
139
140     void init();
141
142 };
143
144 #endif // BACKBONE_H