Changed thread handling for translation fetching
[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 /*! /file backbone.cpp
22 \brief Backbone/core main file \see Backbone
23
24
25 \author Bartosz Szatkowski <bulislaw@linux.com>
26 */
27
28 #include "backbone.h"
29 #include <QDebug>
30
31 QString mappedSearch;
32 QList<Translation*> mapSearch(CommonDictInterface *dict) {
33     if(dict)
34         return dict->searchWordList(mappedSearch, 15);
35     return QList<Translation*>();
36 }
37
38 class TranslationPtr {
39     Translation* _tr;
40 public:
41     TranslationPtr(Translation* tr) :_tr(tr) {}
42     QString toHtml() const {return _tr->toHtml();}
43 };
44
45 void Backbone::init() {
46    _interval = 250; //msec
47    dryRun = false;
48
49    if(!_configPath.size())
50        _configPath = QDir::homePath() + "/.mdictionary/mdictionary.config";
51    if(!_defaultConfigPath.size())
52        _defaultConfigPath = QDir::homePath() + "/.mdictionary/mdictionary.defaults";
53    if(!_pluginPath.size())
54        _pluginPath = "/usr/lib/mdictionary";
55    _historyLen = 10;
56    _searchLimit = 15;
57
58    loadPrefs(_defaultConfigPath);
59    _defaultPluginPath = _pluginPath;
60    _defaultHistoryLen = _historyLen;
61    _defaultSearchLimit = _searchLimit;
62    loadPrefs(_configPath);
63
64    loadPlugins();
65
66    loadDicts(_defaultConfigPath, true);
67    loadDicts(_configPath);
68
69    connect(&_timerSearch, SIGNAL(timeout()), this, SLOT(translationReady()));
70    connect(&_timerHtmlSearch, SIGNAL(timeout()), this,
71            SLOT(htmlTranslationReady()));
72
73    _history = new History(5, this);
74 }
75
76 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
77                    QObject *parent)
78     : QObject(parent)
79 {
80     _pluginPath = pluginPath;
81     _configPath = configPath;
82     _defaultConfigPath = configPath;
83     init();
84     if(dry)
85         dryRun = true;
86 }
87
88
89
90 Backbone::~Backbone()
91 {
92     QListIterator<CommonDictInterface*> it(_dicts.keys());
93
94     while(it.hasNext())
95         delete it.next();
96
97     it = QListIterator<CommonDictInterface*>(_plugins);
98     while(it.hasNext())
99         delete it.next();
100
101     QHashIterator<QString, Translation*> it2(_result);
102     while(it2.hasNext())
103         delete it2.next().value();
104
105 }
106
107
108
109
110 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
111    // init();
112     _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
113     _plugins = QList<CommonDictInterface* > (b._plugins);
114     _result = QHash<QString, Translation* > (b._result);
115     _searchLimit = b.searchLimit();
116 }
117
118
119
120
121 int Backbone::searchLimit() const {
122     return _searchLimit;
123 }
124
125
126
127
128 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
129     return _dicts;
130 }
131
132
133
134
135 QList<CommonDictInterface* > Backbone::getPlugins() {
136     return _plugins;
137 }
138
139
140
141
142 History* Backbone::history() {
143     return _history;
144 }
145
146
147
148
149 QMultiHash<QString, Translation*> Backbone::result() {
150     return _result;
151 }
152
153
154
155
156 void Backbone::stopSearching() {
157     _timerSearch.stop();
158     _timerHtmlSearch.stop();
159     foreach(CommonDictInterface* dict, _dicts.keys())
160         dict->stop();
161 }
162
163
164
165
166
167
168 void Backbone::search(QString word) {
169     _timerSearch.stop();
170     _result.clear();
171     mappedSearch = word.toLower();
172
173     _innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
174     _timerSearch.start(_interval);
175
176
177
178     //foreach(CommonDictInterface* dict, _dicts.keys())
179     //   if(_dicts[dict] == 1) {
180     //        QFuture<QList<Translation*> > tr =
181     //                QtConcurrent::run(dict,
182     //                              &CommonDictInterface::searchWordList,word,
183     //                                                     searchLimit());
184     //        _innerResult.append(tr);
185     //    }
186
187 }
188
189
190
191
192 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
193     foreach(CommonDictInterface* dict, _dicts.keys())
194         if(activeDicts.contains(dict))
195             _dicts[dict] = 1;
196         else
197             _dicts[dict] = 0;
198     dictUpdated();
199  }
200
201
202
203 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
204     addInternalDictionary(dict,active);
205     dictUpdated();
206 }
207
208
209
210  void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
211      dict->setHash(_dicts.size()+1);
212      _dicts[dict] = active;
213      connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
214  }
215
216  void Backbone::removeDictionary(CommonDictInterface *dict) {
217      _dicts.remove(dict);
218      dictUpdated();
219
220  }
221
222
223
224  void Backbone::quit() {
225     stopSearching();
226     Q_EMIT closeOk();
227 }
228
229
230
231 int Backbone::activeSearches() const {
232     return -1; //TODO
233 }
234
235
236
237 void Backbone::translationReady() {
238     if(!_innerResult.isFinished())
239         return;
240     QFutureIterator<QList<Translation*> > it(_innerResult);
241
242     while(it.hasNext()) {
243         QList<Translation* > list = it.next();
244         foreach(Translation* trans, list)
245             _result.insert(trans->key().toLower(), trans);
246     }
247
248     _timerSearch.stop();
249     Q_EMIT ready();
250 }
251
252 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
253     QDir plug(QDir::toNativeSeparators(dir));
254     if(!plug.exists()) {
255         qDebug() << plug.absolutePath() << " folder dosen't exists";
256         return QStringList();
257     }
258     plug.setFilter(QDir::Files);
259     QStringList list = plug.entryList(nameFilter);
260
261     for(int i = 0; i < list.size(); i++)
262         list[i] = plug.absoluteFilePath(list.at(i));
263     return list;
264 }
265
266
267 void Backbone::loadPlugins() {
268     if(dryRun)
269         return;
270     QStringList nameFilter;
271     nameFilter << "*.so";
272     QStringList files = getFilesFromDir(_pluginPath, nameFilter);
273
274     foreach(QString file, files) {
275         QPluginLoader loader(file);
276         if(!loader.load()) {
277             qDebug()<< file << " " << loader.errorString();
278             continue;
279         }
280         QObject *pl = loader.instance();
281
282         CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
283         _plugins.append(plugin);
284     }
285 }
286
287
288
289 CommonDictInterface* Backbone::plugin(QString type) {
290     foreach(CommonDictInterface* plugin, _plugins)
291         if(plugin->type() == type)
292             return plugin;
293     return 0;
294 }
295
296
297
298 void Backbone::loadPrefs(QString fileName) {
299     if(dryRun)
300         return;
301     QFileInfo file(QDir::toNativeSeparators(fileName));
302     QDir confDir(file.dir());
303     if(!confDir.exists()){
304         qDebug() << "Configuration file dosn't exists ("
305                 << file.filePath() << ")";
306         return;
307     }
308     QSettings set(file.filePath(), QSettings::IniFormat);
309     _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
310     _historyLen = set.value("general/history_length", 10).toInt();
311     _searchLimit = set.value("general/search_limit", 15).toInt();
312 }
313
314
315
316 void Backbone::savePrefs(QSettings *set) {
317     if(dryRun)
318         return;
319     set->setValue("general/plugin_path", _pluginPath);
320     set->setValue("general/history_length", _historyLen);
321     set->setValue("general/search_limit", _searchLimit);
322 }
323
324
325
326 void Backbone::saveDefaultPrefs(QSettings *set) {
327     if(dryRun)
328         return;
329     set->setValue("general/plugin_path", _defaultPluginPath);
330     set->setValue("general/history_length", _defaultHistoryLen);
331     set->setValue("general/search_limit", _defaultSearchLimit);
332 }
333
334
335
336 void Backbone::loadDicts(QString fileName, bool _default) {
337     if(dryRun)
338         return;
339     QFileInfo file(QDir::toNativeSeparators(fileName));
340     QDir confDir(file.dir());
341     if(!confDir.exists()){
342         qDebug() << "Configuration file dosn't exists ("
343                 << file.filePath() << ")";
344         return;
345     }
346
347     QSettings set(file.filePath(), QSettings::IniFormat);
348     QStringList dicts = set.childGroups();
349     foreach(QString dict, dicts) {
350         if(!dict.contains("dictionary_"))
351             continue;
352         CommonDictInterface* plug = plugin
353                                     (set.value(dict + "/type", "").toString());
354         if(!plug) {
355             qDebug() << "Config file error: "
356                     << set.value(dict + "/type", "").toString()
357                     << " dosen't exists";
358             continue;
359         }
360         Settings* plugSet = new Settings();
361         set.beginGroup(dict);
362         QStringList items = set.childKeys();
363         foreach(QString item, items) {
364             plugSet->setValue(item, set.value(item, "").toString());
365         }
366         bool active = set.value("active",1).toBool();
367
368         if(_default)
369             plugSet->setValue("_default_", "true");
370
371         set.endGroup();
372         addInternalDictionary(plug->getNew(plugSet), active);
373     }
374 }
375
376
377
378 void Backbone::dictUpdated() {
379     if(dryRun)
380         return;
381     QFileInfo file(QDir::toNativeSeparators(_configPath));
382     QDir confDir(file.dir());
383     if(!confDir.exists())
384         confDir.mkpath(file.dir().path());
385     QSettings set(file.filePath(), QSettings::IniFormat);
386     set.clear();
387
388     QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
389     QDir defConfDir(defFile.dir());
390     if(!defConfDir.exists())
391         defConfDir.mkpath(defFile.dir().path());
392     QSettings defSet(defFile.filePath(), QSettings::IniFormat);
393     defSet.clear();
394     savePrefs(&set);
395     saveDefaultPrefs(&defSet);
396
397     foreach(CommonDictInterface* dict, _dicts.keys()){
398         if(!dict || !dict->settings())
399             continue;
400         if(!dict->settings()->keys().contains("_default_"))
401             saveState(&set, dict->settings(), _dicts[dict], dict->hash());
402         else
403             saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
404     }
405 }
406
407
408
409 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
410                          , uint hash) {
411     if(dryRun)
412         return;
413     if(!set || !plugSet)
414         return;
415     QString section;
416     section.append(QString("dictionary_%1").arg(hash));
417     QList<QString> keys = plugSet->keys();
418     foreach(QString key, keys)
419         set->setValue(section + "/" + key, plugSet->value(key));
420     set->setValue(section + "/active", active);
421 }
422
423
424
425 QStringList Backbone::htmls() {
426     return _htmlResult;
427 }
428
429
430
431 void Backbone::searchHtml(QList<Translation *> translations) {
432     _timerHtmlSearch.stop();
433     _htmlResult.clear();
434     QList<TranslationPtr> dummy;
435     foreach(Translation* tr, translations)
436         dummy.append(TranslationPtr(tr));
437     _timerHtmlSearch.start(_interval);
438
439    _innerHtmlResult = QtConcurrent::mapped(dummy,
440                                             &TranslationPtr::toHtml);
441 }
442
443 void Backbone::htmlTranslationReady() {
444     if(!_innerHtmlResult.isFinished())
445         return;
446
447    _timerHtmlSearch.stop();
448    QFutureIterator<QString> it(_innerHtmlResult);
449    while(it.hasNext())
450        _htmlResult.append(it.next());
451
452     Q_EMIT htmlReady();
453
454 }
455
456
457 QList<CommonDictInterface*> Backbone::activeDicts() {
458     QList<CommonDictInterface*>res;
459     foreach(CommonDictInterface* dict, _dicts.keys())
460         if(_dicts[dict])
461             res.append(dict);
462     return res;
463
464 }