fix bug
[mdictionary] / src / mdictionary / 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 "ConfigGenerator.h"
30 class ConfigGenerator;
31 #include <QDebug>
32
33 int Backbone::_searchLimit;
34
35 // Sadly QtConcurrent::mapped doesn't let me use something like calling method of
36 // some class with supplied argument; so I have to sin against art and put
37 // global function and variable so I could supply function with some parameter
38 QString mappedSearch;
39 QList<Translation*> mapSearch(CommonDictInterface *dict) {
40     if(dict)
41         return dict->searchWordList(mappedSearch, Backbone::_searchLimit);
42     return QList<Translation*>();
43 }
44
45
46
47 /*! Smart pointer (kind of) for translation object
48
49     QtConcurrent::mapped uses collection of data and one function, what I need is
50     to map single data object to method calls for multiple objects. TranslationPtr
51     is an attempt to store method call as a data -> moreover QtConcurrent allows only for
52     methods without any parameters so TranslationPtr is created with Translation
53     object -> ready to call toHtml() for supplied Translation.
54
55     Another thing is that QtConcurrent doesn't like pointers in data collection
56     so TranslationPtr is a way to hide real translation object (pointer to object)
57     */
58 class TranslationPtr {
59     Translation* _tr;
60 public:
61     TranslationPtr(Translation* tr) :_tr(tr) {}
62
63     /*! \return translation text for corresponding Translation object */
64     QString toHtml() const {
65         QString trans;
66         trans = _tr->toXml();
67         return trans;
68     }
69 };
70
71 void Backbone::init() {
72
73    _dictNum = 0;
74    _dir = QDir::homePath() + "/.mdictionary/";
75    if(!QDir(_dir).exists())
76        QDir().mkdir(_dir);
77
78   _configPath = _dir + "mdictionary.config";
79   _pluginPath = "/usr/lib/mdictionary/plugins/";
80
81    //Install default config files
82    ConfigGenerator confGen;
83    ///confGen.generateCss(_dir + "style.css");
84    confGen.generateDefaultConfig(_configPath);
85
86    loadPrefs(_configPath);
87
88    loadPlugins();
89
90    loadDicts(_configPath);
91
92    connect(&_resultWatcher, SIGNAL(finished()), this, SLOT(translationReady()));
93    connect(&_xmlResultWatcher, SIGNAL(finished()), this,
94            SLOT(xmlTranslationReady()));
95    connect(&_bookmarkWatcher, SIGNAL(finished()), this,
96            SLOT(bookmarksListReady()));
97    connect(&_bookmarkSearchWatcher, SIGNAL(finished()), this,
98            SLOT(translationReady()));
99
100    // In common opinion perfect thread count is cores_number+1 (in qt perfect
101    // thread count is set to cores number) so iam changin it
102    QThreadPool::globalInstance()->setMaxThreadCount(
103            QThreadPool::globalInstance()->maxThreadCount()+1);
104
105    _history = new History(_historyLen, this);
106 }
107
108
109
110 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
111                    QObject *parent)
112     : QObject(parent)
113 {
114     _pluginPath = pluginPath;
115     _configPath = configPath;
116
117     dryRun = false;
118     if(dry)
119         dryRun = true;
120     init();
121 }
122
123
124
125 Backbone::~Backbone()
126 {
127     QListIterator<CommonDictInterface*> it(_dicts.keys());
128
129     while(it.hasNext())
130         delete it.next();
131
132     it = QListIterator<CommonDictInterface*>(_plugins);
133     while(it.hasNext())
134         delete it.next();
135
136     QHashIterator<QString, Translation*> it2(_result);
137     while(it2.hasNext())
138         delete it2.next().value();
139
140     delete _history;
141 }
142
143
144
145
146 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
147     init();
148     _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
149     _plugins = QList<CommonDictInterface* > (b._plugins);
150     _result = QHash<QString, Translation* > (b._result);
151     _searchLimit = b.searchLimit();
152 }
153
154
155
156
157 int Backbone::searchLimit() const {
158     return _searchLimit;
159 }
160
161
162
163 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
164     return _dicts;
165 }
166
167
168
169 QList<CommonDictInterface* > Backbone::getPlugins() {
170     return _plugins;
171 }
172
173
174
175 History* Backbone::history() {
176     return _history;
177 }
178
179
180
181 QMultiHash<QString, Translation*> Backbone::result() {
182     return _result;
183 }
184
185
186
187 void Backbone::stopSearching() {
188     if(stopped)
189         return;
190
191     foreach(CommonDictInterface* dict, _dicts.keys())
192         dict->stop();
193     stopped = true;
194     _innerXmlResult.cancel();
195     _innerResult.cancel();
196     Q_EMIT searchCanceled();
197 }
198
199
200
201 void Backbone::search(QString word){
202     _result.clear();
203     mappedSearch = word.toLower();
204
205     stopped = false;
206
207     // When dictFin and bookmarkFin is set to true then translationReady()
208     // signal is emitted see translationReady(),
209     // so when searching only in one of them, corresponding *Fin is set to false
210     // and other to true so program is waiting only for one translation
211     dictFin = !_searchDicts;
212     bookmarkFin = !_searchBookmarks;
213
214     if(!_searchDicts && !_searchBookmarks) {
215         Q_EMIT ready();
216         Q_EMIT notify(Notify::Warning, tr("You have to specify where You want "
217                 "to look for translations"));
218     }
219
220     if (_searchDicts) {
221         _innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
222         _resultWatcher.setFuture(_innerResult);
223     }
224
225     if(_searchBookmarks) {
226         _innerBookmarks = QtConcurrent::run(_bookmarks,
227                 &Bookmarks::searchWordList, word);
228         _bookmarkSearchWatcher.setFuture(_innerBookmarks);
229     }
230 }
231
232
233
234 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
235     foreach(CommonDictInterface* dict, _dicts.keys())
236         if(activeDicts.contains(dict))
237             _dicts[dict] = 1;
238         else
239             _dicts[dict] = 0;
240     dictUpdated();
241  }
242
243
244
245 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
246     addInternalDictionary(dict,active);
247     dictUpdated();
248 }
249
250
251
252 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
253     if(dict) {
254         dict->setHash(++_dictNum); // Hash must be uniqe in every session but not between
255         _dicts[dict] = active;
256         connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
257         connect(dict, SIGNAL(notify(Notify::NotifyType,QString)), this,
258             SIGNAL(notify(Notify::NotifyType,QString)));
259     }
260 }
261
262
263
264  void Backbone::removeDictionary(CommonDictInterface *dict) {
265      _dicts.remove(dict);
266      if(dict) {
267         dict->clean();
268         delete dict;
269      }
270      dictUpdated();
271  }
272
273
274
275  void Backbone::quit() {
276     stopSearching();
277     usleep(100);
278     Q_EMIT closeOk();
279 }
280
281
282
283 void Backbone::translationReady() {
284     bool changed = 0; // prevents from doubling ready() signal, when both if's are
285                       //  executed in one translationReady() call then second
286                       // translationReady() call doubles ready*() emit
287
288     if(!dictFin && _innerResult.isFinished()) {
289         changed = 1;
290         dictFin = 1;
291         QFutureIterator<QList<Translation*> > it(_innerResult);
292
293         while(it.hasNext()) {
294             QList<Translation* > list = it.next();
295             foreach(Translation* trans, list) {
296                 if(!trans)
297                     continue;
298                 if(!_searchBookmarks)
299                     trans->setBookmark(_bookmarks.
300                             inBookmarks(trans->key()));
301                 _result.insert(trans->key().toLower(), trans);
302            }
303         }
304     }
305
306     if(!bookmarkFin && _innerBookmarks.isFinished()) {
307         changed = 1;
308         bookmarkFin = 1;
309         QList<Translation*> list = _innerBookmarks.result();
310
311         foreach(Translation* trans, list)
312                 _result.insert(trans->key().toLower(), trans);
313     }
314
315     if(!stopped && bookmarkFin && dictFin && changed) {
316         Q_EMIT ready();
317     }
318 }
319
320
321
322
323 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
324     QDir plug(QDir::toNativeSeparators(dir));
325     if(!plug.exists()) {
326         qDebug() << plug.absolutePath() << " folder doesn't exist";
327         Q_EMIT notify(Notify::Warning,
328                 tr("%1 folder doesn't exist.").arg(plug.path()));
329         return QStringList();
330     }
331     plug.setFilter(QDir::Files);
332     QStringList list = plug.entryList(nameFilter);
333
334     for(int i = 0; i < list.size(); i++)
335         list[i] = plug.absoluteFilePath(list.at(i));
336     return list;
337 }
338
339
340 void Backbone::loadPlugins() {
341     if(dryRun)
342         return;
343     QStringList nameFilter;
344     nameFilter << "*.so" << "*.so.*";
345     QStringList files = getFilesFromDir(_pluginPath, nameFilter);
346
347     foreach(QString file, files) {
348         QPluginLoader loader(file);
349         if(!loader.load()) {
350             Q_EMIT notify(Notify::Error,
351                     tr("%1 plugin cannot be loaded: %2.")
352                     .arg(file).arg(loader.errorString()));
353             continue;
354         }
355         QObject *pl = loader.instance();
356
357         bool exists = 0;
358         CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
359         foreach(CommonDictInterface* pl, _plugins)
360             if(pl->type() == plugin->type()) {
361                 exists = 1;
362                 break;
363            }
364         if(!exists) {
365             _plugins.append(plugin);
366             plugin->retranslate();
367             connect(plugin, SIGNAL(notify(Notify::NotifyType,QString)),
368                     this, SIGNAL(notify(Notify::NotifyType,QString)));
369         }
370     }
371 }
372
373
374
375 CommonDictInterface* Backbone::plugin(QString type) {
376     foreach(CommonDictInterface* plugin, _plugins)
377         if(plugin->type() == type)
378             return plugin;
379     return 0;
380 }
381
382
383
384 void Backbone::loadPrefs(QString fileName) {
385     if(dryRun)
386         return;
387     QFileInfo file(QDir::toNativeSeparators(fileName));
388     QDir confDir(file.dir());
389     if(!confDir.exists()){
390         qDebug() << "Configuration file doesn't exist ("
391                 << file.filePath() << ")";
392         Q_EMIT notify(Notify::Warning,
393                 tr("%1 configuration file doesn't exist.")
394                 .arg(file.filePath()));
395         return;
396     }
397     QSettings set(file.filePath(), QSettings::IniFormat);
398     _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
399     _historyLen = set.value("general/history_size", 10).toInt();
400     _searchLimit = set.value("general/search_limit", 15).toInt();
401     _searchBookmarks = set.value("general/search_bookmarks",1).toBool();
402     _searchDicts = set.value("general/search_dictionaries",1).toBool();
403     _zoom = set.value("general/zoom", 1.0).toReal();
404 }
405
406
407
408 void Backbone::savePrefs(QSettings *set) {
409     if(dryRun)
410         return;
411     set->setValue("general/plugin_path", _pluginPath);
412     set->setValue("general/history_size", _historyLen);
413     set->setValue("general/search_limit", _searchLimit);
414     set->setValue("general/search_bookmarks", _searchBookmarks);
415     set->setValue("general/search_dictionaries", _searchDicts);
416     set->setValue("general/zoom", _zoom);
417 }
418
419
420
421 void Backbone::loadDicts(QString fileName) {
422     if(dryRun)
423         return;
424
425     QFileInfo file(QDir::toNativeSeparators(fileName));
426     QDir confDir(file.dir());
427     if(!confDir.exists()){
428         qDebug() << "Configuration file doesn't exist ("
429                 << file.filePath() << ")";
430         Q_EMIT notify(Notify::Warning,
431                 tr("%1 configuration file doesn't exist.")
432                 .arg(file.filePath()));
433         return;
434     }
435
436     QSettings set(file.filePath(), QSettings::IniFormat);
437     QStringList dicts = set.childGroups();
438     foreach(QString dict, dicts) {
439         if(!dict.contains("dictionary_"))
440             continue;
441         CommonDictInterface* plug = plugin
442                                     (set.value(dict + "/type", "").toString());
443         if(!plug) {
444             qDebug() << "Config file error: "
445                     << set.value(dict + "/type", "").toString()
446                     << " doesn't exist";
447             Q_EMIT notify(Notify::Warning,
448                     tr("Configuration file error. %2 plugin doesn't exist.")
449                     .arg(set.value(dict + "/type", "").toString()));
450             continue;
451         }
452         Settings* plugSet = new Settings();
453         set.beginGroup(dict);
454         QStringList items = set.childKeys();
455         foreach(QString item, items) {
456             plugSet->setValue(item, set.value(item, "").toString());
457         }
458         bool active = set.value("active",1).toBool();
459
460         set.endGroup();
461         addInternalDictionary(plug->getNew(plugSet), active);
462         delete plugSet;
463     }
464 }
465
466
467
468 void Backbone::dictUpdated() {
469     if(dryRun)
470         return;
471
472     // For convienence this function is called for each change in dictionaries
473     // and each call dumps configuration for all dictionaries into file.
474     // Maybe a better way would be to store new/changed configuration but
475     // parsing settings file and figuring out what was changed, in my opinion,
476     // would take more time
477     _history->setMaxSize(_historyLen);
478     QFileInfo file(QDir::toNativeSeparators(_configPath));
479     QDir confDir(file.dir());
480     if(!confDir.exists())
481         confDir.mkpath(file.dir().path());
482     QSettings set(file.filePath(), QSettings::IniFormat);
483     set.clear();
484
485     savePrefs(&set);
486
487     foreach(CommonDictInterface* dict, _dicts.keys()){
488         if(!dict || !dict->settings())
489             continue;
490         saveState(&set, dict->settings(), _dicts[dict], dict->hash());
491     }
492 }
493
494
495
496 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
497                          , uint hash) {
498     if(dryRun)
499         return;
500     if(!set || !plugSet)
501         return;
502
503     QString section;
504     section.append(QString("dictionary_%1").arg(hash));
505     QList<QString> keys = plugSet->keys();
506     foreach(QString key, keys)
507         set->setValue(section + "/" + key, plugSet->value(key));
508     set->setValue(section + "/active", active);
509 }
510
511
512
513 QStringList Backbone::xmls() {
514     return _xmlResult;
515 }
516
517 void Backbone::addBookmark(QList<Translation*> translations) {
518     if(translations.size()>0) {
519         Translation *tr = translations.at(0);
520         foreach(CommonDictInterface* dict, activeDicts()) {
521             Translation* trans = dict->getTranslationFor(tr->key());
522             if(trans) {
523                 translations.append(trans);
524             }
525         }
526     }
527
528     Translation* translation;
529     QHash<QString,QString> trans;
530     foreach(translation,translations){
531         translation->setBookmark(true);
532         trans.insert(translation->toXml(),translation->key());
533
534     }
535
536     foreach(QString value,trans.keys()){
537         QString key= trans.value(value);
538         _bookmarks.add(key,key,value);
539     }
540
541     // Translation* translation;
542     // foreach(translation, translations)
543     // _bookmarks.add(translation);
544     // emit bookmarkReady();
545 }
546
547
548 void Backbone::searchXml(QList<Translation *> translations) {
549     _xmlResult.clear();
550
551     QList<TranslationPtr> dummy;
552     stopped = false;
553     foreach(Translation* tr, translations) {
554          if(containsDict(tr->dict()) || !tr->dict())
555             dummy.append(TranslationPtr(tr));
556     }
557     if(translations.size()>0) {
558         Translation *tr = translations.at(0);
559         foreach(CommonDictInterface* dict, activeDicts()) {
560             Translation* trans = dict->getTranslationFor(tr->key());
561             if(trans)
562                 dummy.append(TranslationPtr(trans));
563         }
564     }
565
566    _innerXmlResult = QtConcurrent::mapped(dummy,
567                                             &TranslationPtr::toHtml);
568    _xmlResultWatcher.setFuture(_innerXmlResult);
569 }
570
571
572
573 void Backbone::xmlTranslationReady() {
574
575     QFutureIterator<QString> it(_innerXmlResult);
576     QSet<QString> uniqe;
577     while(it.hasNext())
578         uniqe.insert(it.next());
579     _xmlResult.clear();
580     _xmlResult = uniqe.toList();
581
582     if(!stopped)
583         Q_EMIT xmlReady();
584
585 }
586
587
588 QList<CommonDictInterface*> Backbone::activeDicts() {
589     QList<CommonDictInterface*>res;
590     foreach(CommonDictInterface* dict, _dicts.keys())
591         if(_dicts[dict]){
592             res.append(dict);
593         }
594     return res;
595
596 }
597
598
599
600 void Backbone::bookmarksListReady() {
601    _bookmarksResult = _innerBookmarks.result();
602    Q_EMIT bookmarksReady();
603 }
604
605
606
607
608 void Backbone::setSettings(Settings *settings) {
609     _historyLen = settings->value("history_size").toInt();
610     _searchLimit = settings->value("search_limit").toInt();
611     if(settings->value("search_dictionaries") == "true")
612         _searchDicts = 1;
613     else
614         _searchDicts = 0;
615     if(settings->value("search_bookmarks") == "true")
616         _searchBookmarks = 1;
617     else
618         _searchBookmarks = 0;
619     _zoom = settings->value("zoom").toFloat();
620     if(!_zoom)
621         _zoom ++;
622
623     dictUpdated();
624     if(settings)
625         delete settings;
626 }
627
628
629
630
631 Settings* Backbone::settings() {
632     Settings * settings = new Settings();
633     settings->setValue("history_size", QString("%1").arg(_historyLen));
634     settings->setValue("search_limit", QString("%1").arg(_searchLimit));
635     settings->setValue("zoom", QString("%1").arg(_zoom));
636     if(_searchBookmarks)
637         settings->setValue("search_bookmarks", "true");
638     else
639         settings->setValue("search_bookmarks", "false");
640
641     if(_searchDicts)
642         settings->setValue("search_dictionaries", "true");
643     else
644         settings->setValue("search_dictionaries", "false");
645     return settings;
646 }
647
648
649 bool Backbone::containsDict(uint hash) const {
650     QHashIterator<CommonDictInterface*, bool> it(_dicts);
651     if (!hash)
652         return false;
653     while(it.hasNext())
654         if(it.next().key()->hash() == hash)
655             return true;
656     return false;
657 }