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