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