Merge branch 'searching' into backbone
[mdictionary] / trunk / src / base / backbone / backbone.cpp
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 #include "backbone.h"
25 #include <QDebug>
26
27 void Backbone::init() {
28    _interval = 250; //msec
29
30    if(!_configPath.size())
31        _configPath = QDir::homePath() + "/.mdictionary";
32
33    loadPrefs();
34
35
36    if(!_pluginPath.size())
37        _pluginPath = "/usr/lib/mdictionary/";
38
39    loadPlugins();
40    loadDicts();
41
42    if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
43        qDebug() << "Timer signal not connected";
44 }
45
46 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
47     : QObject(parent)
48 {
49     _pluginPath = pluginPath;
50     _configPath = configPath;
51     init();
52 }
53
54
55
56 Backbone::~Backbone()
57 {
58     QListIterator<CommonDictInterface*> it(_dicts.keys());
59
60     while(it.hasNext())
61         delete it.next();
62
63     it = QListIterator<CommonDictInterface*>(_plugins);
64     while(it.hasNext())
65         delete it.next();
66
67     QHashIterator<QString, Translation*> it2(_result);
68     while(it2.hasNext())
69         delete it2.next().value();
70
71 }
72
73
74
75
76 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
77    // init();
78     _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
79     _plugins = QList<CommonDictInterface* > (b._plugins);
80     _result = QHash<QString, Translation* > (b._result);
81     _searchLimit = b.searchLimit();
82 }
83
84
85
86
87 int Backbone::searchLimit() const {
88     return _searchLimit;
89 }
90
91
92
93
94 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
95     return _dicts;
96 }
97
98
99
100
101 QList<CommonDictInterface* > Backbone::getPlugins() {
102     return _plugins;
103 }
104
105
106
107
108 QList<QString> Backbone::getHistory() {
109     //TODO code needed
110 }
111
112
113
114
115 QMultiHash<QString, Translation*> Backbone::result() {
116     return _result;
117 }
118
119
120
121
122 void Backbone::stopSearching() {
123     _timer.stop();
124     _innerResult.clear();
125     foreach(CommonDictInterface* dict, _dicts.keys())
126         dict->stop();
127 }
128
129
130
131
132 void Backbone::search(QStringList words) {
133     _timer.stop();
134     _result.clear();
135     _innerResult.clear();
136
137     _timer.start(_interval);
138     foreach(QString word, words)
139         foreach(CommonDictInterface* dict, _dicts.keys())
140             if(_dicts[dict] == 1) {
141                 QFuture<QList<Translation*> > tr =
142                         QtConcurrent::run(dict,
143                                       &CommonDictInterface::searchWordList,word,
144                                                              searchLimit());
145                 _innerResult.append(tr);
146             }
147
148 }
149
150
151
152
153  void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
154      foreach(CommonDictInterface* dict, _dicts.keys())
155          if(activeDicts.contains(dict))
156              _dicts[dict] = 1;
157          else
158              _dicts[dict] = 0;
159  }
160
161
162
163
164  void Backbone::addDictionary(CommonDictInterface* dict, bool active) {
165      dict->setHash(_dicts.size()+1);
166      _dicts[dict] = active;
167      connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
168  }
169
170  void Backbone::removeDictionary(CommonDictInterface *dict) {
171      _dicts.remove(dict);
172
173  }
174
175
176
177  void Backbone::quit() {
178     stopSearching();
179     Q_EMIT closeOk();
180 }
181
182
183
184 int Backbone::activeSearches() const {
185     return _innerResult.size();
186 }
187
188
189
190 void Backbone::translation() {
191     foreach(QFuture<QList<Translation*> > trans, _innerResult) {
192         if(!trans.isFinished())
193             continue;
194         QList<Translation*> tList = trans.result();
195         foreach(Translation* t, tList) {
196             _result.insert(t->key().toLower(), t);
197         }
198         _innerResult.removeOne(trans);
199     }
200     if(!_innerResult.size()) {
201         _timer.stop();
202         Q_EMIT ready();
203     }
204 }
205
206 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
207     QDir plug(QDir::toNativeSeparators(dir));
208     if(!plug.exists()) {
209         qDebug() << plug.absolutePath() << " folder dosen't exists";
210         return QStringList();
211     }
212     plug.setFilter(QDir::Files);
213     QStringList list = plug.entryList(nameFilter);
214
215     for(int i = 0; i < list.size(); i++)
216         list[i] = plug.absoluteFilePath(list.at(i));
217     return list;
218 }
219
220
221 void Backbone::loadPlugins() {
222     QStringList nameFilter;
223     nameFilter << "*.so";
224     QStringList files = getFilesFromDir(_pluginPath, nameFilter);
225
226     foreach(QString file, files) {
227         QPluginLoader loader(file);
228         if(!loader.load()) {
229             qDebug()<< file << " " << loader.errorString();
230             continue;
231         }
232         QObject *pl = loader.instance();
233
234         CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
235         _plugins.append(plugin);
236        // addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
237         //Settings* set = new Settings();
238         //set->setValue("path", "dict2.xdxf");
239         //addDictionary(plugin->getNew(set));
240     }
241 }
242
243
244
245 CommonDictInterface* Backbone::plugin(QString type) {
246     foreach(CommonDictInterface* plugin, _plugins)
247         if(plugin->type() == type)
248             return plugin;
249     return 0;
250 }
251
252
253
254 void Backbone::loadPrefs() {
255     QDir confDir(_configPath);
256     if(!confDir.exists())
257         qDebug() << "Configuration file dosn't exists (" << _configPath << ")";
258     QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
259     _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
260     _historyLen = set.value("general/history_length", 10).toInt();
261     _searchLimit = set.value("general/search_limit", 15).toInt();
262 }
263
264
265
266 void Backbone::loadDicts() {
267     QDir confDir(_configPath);
268     if(!confDir.exists())
269         qDebug() << confDir.mkpath(_configPath);
270     QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
271     _pluginPath = set.value("general/path", _pluginPath).toString();
272     QStringList dicts = set.childGroups();
273     foreach(QString dict, dicts) {
274         if(!dict.contains("dictionary_"))
275             continue;
276         CommonDictInterface* plug = plugin
277                                     (set.value(dict + "/type", "").toString());
278         if(!plug) {
279             qDebug() << "Config file error: "
280                     << set.value(dict + "/type", "").toString()
281                     << " dosen't exists";
282             continue;
283         }
284         Settings* plugSet = new Settings();
285         set.beginGroup(dict);
286         QStringList items = set.childKeys();
287         foreach(QString item, items)
288             plugSet->setValue(item, set.value(item, "").toString());
289         addDictionary(plug->getNew(plugSet), set.value("active",1).toBool());
290         set.endGroup();
291     }
292 }
293
294 void Backbone::dictUpdated() {
295     qDebug() << "UPDATE";
296     QDir confDir(_configPath);
297     if(!confDir.exists())
298         qDebug() << confDir.mkpath(_configPath);
299     QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
300     foreach(CommonDictInterface* dict, _dicts.keys())
301         saveState(&set, dict->settings(), _dicts[dict], dict->hash());
302 }
303
304 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
305                          , uint hash) {
306     QString section;
307     section.append("dictionary_%1").arg(hash);
308     QList<QString> keys = plugSet->keys();
309     foreach(QString key, keys)
310         set->setValue(section + "/" + key, plugSet->value(key));
311     set->setValue(section + "/active", active);
312
313 }
314
315 //saving dictionaries state
316 //saving selection state
317
318