Fixed doubling ready() signal when searching for word list
[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 int Backbone::_searchLimit;
32
33 // Sadly QtConcurent mapped dont let me use something like calling method of
34 // some class with supplied argument
35 QString mappedSearch;
36 QList<Translation*> mapSearch(CommonDictInterface *dict) {
37     if(dict)
38         return dict->searchWordList(mappedSearch, Backbone::_searchLimit);
39     return QList<Translation*>();
40 }
41
42 class TranslationPtr {
43     Translation* _tr;
44 public:
45     TranslationPtr(Translation* tr) :_tr(tr) {}
46     QString toHtml() const {
47         QString trans;
48         trans = _tr->toHtml();
49         return trans;
50
51     }
52 };
53
54 void Backbone::init() {
55
56    if(!_configPath.size())
57        _configPath = QDir::homePath() + "/.mdictionary/mdictionary.config";
58    if(!_defaultConfigPath.size())
59        _defaultConfigPath = QDir::homePath() + "/.mdictionary/mdictionary.defaults";
60    if(!_pluginPath.size())
61        _pluginPath = "/usr/lib/mdictionary";
62    _historyLen = 10;
63    _searchLimit = 15;
64
65    loadPrefs(_defaultConfigPath);
66    _defaultPluginPath = _pluginPath;
67    _defaultHistoryLen = _historyLen;
68    _defaultSearchLimit = _searchLimit;
69    loadPrefs(_configPath);
70
71    loadPlugins();
72
73    loadDicts(_defaultConfigPath, true);
74    loadDicts(_configPath);
75
76    connect(&_resultWatcher, SIGNAL(finished()), this, SLOT(translationReady()));
77    connect(&_htmlResultWatcher, SIGNAL(finished()), this,
78            SLOT(htmlTranslationReady()));
79    connect(&_bookmarkWatcher, SIGNAL(finished()), this,
80            SLOT(bookmarksListReady()));
81    connect(&_bookmarkSearchWatcher, SIGNAL(finished()), this,
82            SLOT(translationReady()));
83
84    QThreadPool::globalInstance()->setMaxThreadCount(
85            QThreadPool::globalInstance()->maxThreadCount()+1);
86
87    _history = new History(5, this);
88    _dictNum = 0;
89 }
90
91
92
93 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
94                    QObject *parent)
95     : QObject(parent)
96 {
97     _pluginPath = pluginPath;
98     _configPath = configPath;
99     _defaultConfigPath = configPath;
100     dryRun = false;
101     if(dry)
102         dryRun = true;
103     init();
104 }
105
106
107
108 Backbone::~Backbone()
109 {
110     QListIterator<CommonDictInterface*> it(_dicts.keys());
111
112     while(it.hasNext())
113         delete it.next();
114
115     it = QListIterator<CommonDictInterface*>(_plugins);
116     while(it.hasNext())
117         delete it.next();
118
119     QHashIterator<QString, Translation*> it2(_result);
120     while(it2.hasNext())
121         delete it2.next().value();
122
123 }
124
125
126
127
128 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
129     _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
130     _plugins = QList<CommonDictInterface* > (b._plugins);
131     _result = QHash<QString, Translation* > (b._result);
132     _searchLimit = b.searchLimit();
133 }
134
135
136
137
138 int Backbone::searchLimit() const {
139     return _searchLimit;
140 }
141
142
143
144 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
145     return _dicts;
146 }
147
148
149
150 QList<CommonDictInterface* > Backbone::getPlugins() {
151     return _plugins;
152 }
153
154
155
156 History* Backbone::history() {
157     return _history;
158 }
159
160
161
162 QMultiHash<QString, Translation*> Backbone::result() {
163     return _result;
164 }
165
166
167
168 void Backbone::stopSearching() {
169     if(stopped)
170         return;
171
172     foreach(CommonDictInterface* dict, _dicts.keys())
173         dict->stop();
174     stopped = true;
175     _innerHtmlResult.cancel();
176     _innerResult.cancel();
177     Q_EMIT searchCanceled();
178 }
179
180
181
182 void Backbone::search(QString word){
183     qDebug() << "SEEEEEEEEEARCH";
184     _result.clear();
185     mappedSearch = word.toLower();
186
187     stopped = false;
188     dictFin = !_searchDicts;
189     bookmarkFin = !_searchBookmarks;
190
191     if (_searchDicts) {
192         _innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
193         _resultWatcher.setFuture(_innerResult);
194     }
195
196     if(_searchBookmarks) {
197         _innerBookmarks = QtConcurrent::run(_bookmarks,
198                 &Bookmarks::searchWordList, word);
199         _bookmarkSearchWatcher.setFuture(_innerBookmarks);
200     }
201 }
202
203
204
205 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
206     foreach(CommonDictInterface* dict, _dicts.keys())
207         if(activeDicts.contains(dict))
208             _dicts[dict] = 1;
209         else
210             _dicts[dict] = 0;
211     dictUpdated();
212  }
213
214
215
216 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
217     addInternalDictionary(dict,active);
218     dictUpdated();
219 }
220
221
222
223  void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
224      dict->setHash(++_dictNum);
225      _dicts[dict] = active;
226      connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
227      connect(dict, SIGNAL(notify(Notify::NotifyType,QString)), this,
228              SIGNAL(notify(Notify::NotifyType,QString)));
229  }
230
231  void Backbone::removeDictionary(CommonDictInterface *dict) {
232      _dicts.remove(dict);
233      delete dict;
234      dictUpdated();
235
236  }
237
238
239
240  void Backbone::quit() {
241     stopSearching();
242     Q_EMIT closeOk();
243 }
244
245
246
247 void Backbone::translationReady() {
248     bool changed = 0; // prevents doubling ready() signal, when both if are
249                       //  executed in one translationReady() call then second
250                       // call doubles ready*() emit without any new data
251     if(!dictFin && _innerResult.isFinished()) {
252         changed = 1;
253         dictFin = 1;
254         QFutureIterator<QList<Translation*> > it(_innerResult);
255
256         while(it.hasNext()) {
257             QList<Translation* > list = it.next();
258             foreach(Translation* trans, list)
259                 _result.insert(trans->key().toLower(), trans);
260         }
261     }
262
263     if(!bookmarkFin && _innerBookmarks.isFinished()) {
264         changed = 1;
265         bookmarkFin = 1;
266         QList<Translation*> list = _innerBookmarks.result();
267
268         foreach(Translation* trans, list)
269                 _result.insert(trans->key().toLower(), trans);
270     }
271
272     if(!stopped && bookmarkFin && dictFin && changed) {
273         qDebug() << "EMITTTTTT";
274         Q_EMIT ready();
275         }
276 }
277
278 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
279     QDir plug(QDir::toNativeSeparators(dir));
280     if(!plug.exists()) {
281         qDebug() << plug.absolutePath() << " folder dosen't exists";
282         Q_EMIT notify(Notify::Warning,
283                 QString("%1 folder dosen't exists.").arg(plug.path()));
284         return QStringList();
285     }
286     plug.setFilter(QDir::Files);
287     QStringList list = plug.entryList(nameFilter);
288
289     for(int i = 0; i < list.size(); i++)
290         list[i] = plug.absoluteFilePath(list.at(i));
291     return list;
292 }
293
294
295 void Backbone::loadPlugins() {
296     if(dryRun)
297         return;
298     QStringList nameFilter;
299     nameFilter << "*.so";
300     QStringList files = getFilesFromDir(_pluginPath, nameFilter);
301
302     foreach(QString file, files) {
303         QPluginLoader loader(file);
304         if(!loader.load()) {
305             Q_EMIT notify(Notify::Error,
306                     QString("%1 plugin cannot be loaded: %2.")
307                     .arg(file).arg(loader.errorString()));
308             qDebug()<< file << " " << loader.errorString();
309             continue;
310         }
311         QObject *pl = loader.instance();
312
313         CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
314         _plugins.append(plugin);
315     }
316 }
317
318
319
320 CommonDictInterface* Backbone::plugin(QString type) {
321     foreach(CommonDictInterface* plugin, _plugins)
322         if(plugin->type() == type)
323             return plugin;
324     return 0;
325 }
326
327
328
329 void Backbone::loadPrefs(QString fileName) {
330     if(dryRun)
331         return;
332     QFileInfo file(QDir::toNativeSeparators(fileName));
333     QDir confDir(file.dir());
334     if(!confDir.exists()){
335         qDebug() << "Configuration file dosn't exists ("
336                 << file.filePath() << ")";
337         Q_EMIT notify(Notify::Warning,
338                 QString("%1 configurationfile dosen't exists.")
339                 .arg(file.filePath()));
340         return;
341     }
342     QSettings set(file.filePath(), QSettings::IniFormat);
343     _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
344     _historyLen = set.value("general/history_size", 10).toInt();
345     _searchLimit = set.value("general/search_limit", 15).toInt();
346     _searchBookmarks = set.value("general/search_bookmarks",1).toBool();
347     _searchDicts = set.value("general/search_dictionaries",1).toBool();
348 }
349
350
351
352 void Backbone::savePrefs(QSettings *set) {
353     if(dryRun)
354         return;
355     set->setValue("general/plugin_path", _pluginPath);
356     set->setValue("general/history_size", _historyLen);
357     set->setValue("general/search_limit", _searchLimit);
358     set->setValue("general/search_bookmarks", _searchBookmarks);
359     set->setValue("general/search_dictionaries", _searchDicts);
360 }
361
362
363
364 void Backbone::saveDefaultPrefs(QSettings *set) {
365     if(dryRun)
366         return;
367     set->setValue("general/plugin_path", _defaultPluginPath);
368     set->setValue("general/history_size", _defaultHistoryLen);
369     set->setValue("general/search_limit", _defaultSearchLimit);
370 }
371
372
373
374 void Backbone::loadDicts(QString fileName, bool _default) {
375     if(dryRun)
376         return;
377     QFileInfo file(QDir::toNativeSeparators(fileName));
378     QDir confDir(file.dir());
379     if(!confDir.exists()){
380         qDebug() << "Configuration file dosn't exists ("
381                 << file.filePath() << ")";
382         Q_EMIT notify(Notify::Warning,
383                 QString("%1 configurationfile dosen't exists.")
384                 .arg(file.filePath()));
385         return;
386     }
387
388     QSettings set(file.filePath(), QSettings::IniFormat);
389     QStringList dicts = set.childGroups();
390     foreach(QString dict, dicts) {
391         if(!dict.contains("dictionary_"))
392             continue;
393         CommonDictInterface* plug = plugin
394                                     (set.value(dict + "/type", "").toString());
395         if(!plug) {
396             qDebug() << "Config file error: "
397                     << set.value(dict + "/type", "").toString()
398                     << " dosen't exists";
399             Q_EMIT notify(Notify::Warning,
400                     QString("Configuration file error. %2 plugin dosen't exists.")
401                     .arg(set.value(dict + "/type", "").toString()));
402             continue;
403         }
404         Settings* plugSet = new Settings();
405         set.beginGroup(dict);
406         QStringList items = set.childKeys();
407         foreach(QString item, items) {
408             plugSet->setValue(item, set.value(item, "").toString());
409         }
410         bool active = set.value("active",1).toBool();
411
412         if(_default)
413             plugSet->setValue("_default_", "true");
414
415         set.endGroup();
416         addInternalDictionary(plug->getNew(plugSet), active);
417     }
418 }
419
420
421
422 void Backbone::dictUpdated() {
423     if(dryRun)
424         return;
425     _history->setMaxSize(_historyLen);
426     QFileInfo file(QDir::toNativeSeparators(_configPath));
427     QDir confDir(file.dir());
428     if(!confDir.exists())
429         confDir.mkpath(file.dir().path());
430     QSettings set(file.filePath(), QSettings::IniFormat);
431     set.clear();
432
433     QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
434     QDir defConfDir(defFile.dir());
435     if(!defConfDir.exists())
436         defConfDir.mkpath(defFile.dir().path());
437     QSettings defSet(defFile.filePath(), QSettings::IniFormat);
438     defSet.clear();
439     savePrefs(&set);
440     saveDefaultPrefs(&defSet);
441
442     foreach(CommonDictInterface* dict, _dicts.keys()){
443         if(!dict || !dict->settings())
444             continue;
445         if(!dict->settings()->keys().contains("_default_"))
446             saveState(&set, dict->settings(), _dicts[dict], dict->hash());
447         else
448             saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
449     }
450 }
451
452
453
454 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
455                          , uint hash) {
456     if(dryRun)
457         return;
458     if(!set || !plugSet)
459         return;
460     QString section;
461     section.append(QString("dictionary_%1").arg(hash));
462     QList<QString> keys = plugSet->keys();
463     foreach(QString key, keys)
464         set->setValue(section + "/" + key, plugSet->value(key));
465     set->setValue(section + "/active", active);
466 }
467
468
469
470 QStringList Backbone::htmls() {
471     return _htmlResult;
472 }
473
474
475
476 void Backbone::searchHtml(QList<Translation *> translations) {
477     _htmlResult.clear();
478
479     QList<TranslationPtr> dummy;
480     stopped = false;
481     foreach(Translation* tr, translations) {
482         if(containsDict(tr->dict()) || !tr->dict())
483             dummy.append(TranslationPtr(tr));
484   }
485
486    _innerHtmlResult = QtConcurrent::mapped(dummy,
487                                             &TranslationPtr::toHtml);
488    _htmlResultWatcher.setFuture(_innerHtmlResult);
489 }
490
491 void Backbone::htmlTranslationReady() {
492
493     QFutureIterator<QString> it(_innerHtmlResult);
494     while(it.hasNext())
495        _htmlResult.append(it.next());
496
497     if(!stopped)
498         Q_EMIT htmlReady();
499
500 }
501
502
503 QList<CommonDictInterface*> Backbone::activeDicts() {
504     QList<CommonDictInterface*>res;
505     foreach(CommonDictInterface* dict, _dicts.keys())
506         if(_dicts[dict])
507             res.append(dict);
508     return res;
509
510 }
511
512
513
514 void Backbone::bookmarksListReady() {
515    _bookmarksResult = _innerBookmarks.result();
516    Q_EMIT bookmarksReady();
517 }
518
519
520
521
522 void Backbone::setSettings(Settings *settings) {
523     _historyLen = settings->value("history_size").toInt();
524     _searchLimit = settings->value("search_limit").toInt();
525     if(settings->value("search_dictionaries") == "true")
526         _searchDicts = 1;
527     else
528         _searchDicts = 0;
529     if(settings->value("search_bookmarks") == "true")
530         _searchBookmarks = 1;
531     else
532         _searchBookmarks = 0;
533     dictUpdated();
534 }
535
536
537
538
539 Settings* Backbone::settings() {
540     Settings * settings = new Settings();
541     settings->setValue("history_size", QString("%1").arg(_historyLen));
542     settings->setValue("search_limit", QString("%1").arg(_searchLimit));
543     if(_searchBookmarks)
544         settings->setValue("search_bookmarks", "true");
545     else
546         settings->setValue("search_bookmarks", "false");
547
548     if(_searchDicts)
549         settings->setValue("search_dictionaries", "true");
550     else
551         settings->setValue("search_dictionaries", "false");
552     return settings;
553 }
554
555
556 bool Backbone::containsDict(uint hash) const {
557     QHashIterator<CommonDictInterface*, bool> it(_dicts);
558     if (!hash)
559         return false;
560     while(it.hasNext())
561         if(it.next().key()->hash() == hash)
562             return true;
563     return false;
564 }