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