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