1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
21 /*! /file backbone.cpp
22 \brief Backbone/core main file \see Backbone
25 \author Bartosz Szatkowski <bulislaw@linux.com>
32 QList<Translation*> mapSearch(CommonDictInterface *dict) {
34 return dict->searchWordList(mappedSearch, 15);
35 return QList<Translation*>();
38 class TranslationPtr {
41 TranslationPtr(Translation* tr) :_tr(tr) {}
42 QString toHtml() const {
44 trans = _tr->toHtml();
50 void Backbone::init() {
51 _interval = 250; //msec
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";
62 loadPrefs(_defaultConfigPath);
63 _defaultPluginPath = _pluginPath;
64 _defaultHistoryLen = _historyLen;
65 _defaultSearchLimit = _searchLimit;
66 loadPrefs(_configPath);
70 loadDicts(_defaultConfigPath, true);
71 loadDicts(_configPath);
73 connect(&_timerSearch, SIGNAL(timeout()), this, SLOT(translationReady()));
74 connect(&_timerHtmlSearch, SIGNAL(timeout()), this,
75 SLOT(htmlTranslationReady()));
77 QThreadPool::globalInstance()->setMaxThreadCount(
78 QThreadPool::globalInstance()->maxThreadCount()+1);
80 _history = new History(5, this);
83 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
87 _pluginPath = pluginPath;
88 _configPath = configPath;
89 _defaultConfigPath = configPath;
100 QListIterator<CommonDictInterface*> it(_dicts.keys());
105 it = QListIterator<CommonDictInterface*>(_plugins);
109 QHashIterator<QString, Translation*> it2(_result);
111 delete it2.next().value();
118 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();
129 int Backbone::searchLimit() const {
136 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
143 QList<CommonDictInterface* > Backbone::getPlugins() {
150 History* Backbone::history() {
157 QMultiHash<QString, Translation*> Backbone::result() {
164 void Backbone::stopSearching() {
166 _timerHtmlSearch.stop();
167 foreach(CommonDictInterface* dict, _dicts.keys())
176 void Backbone::search(QString word) {
179 mappedSearch = word.toLower();
181 _innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
182 _timerSearch.start(_interval);
186 //foreach(CommonDictInterface* dict, _dicts.keys())
187 // if(_dicts[dict] == 1) {
188 // QFuture<QList<Translation*> > tr =
189 // QtConcurrent::run(dict,
190 // &CommonDictInterface::searchWordList,word,
192 // _innerResult.append(tr);
200 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
201 foreach(CommonDictInterface* dict, _dicts.keys())
202 if(activeDicts.contains(dict))
211 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
212 addInternalDictionary(dict,active);
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()));
224 void Backbone::removeDictionary(CommonDictInterface *dict) {
232 void Backbone::quit() {
242 void Backbone::translationReady() {
243 if(!_innerResult.isFinished())
245 QFutureIterator<QList<Translation*> > it(_innerResult);
248 while(it.hasNext()) {
249 QList<Translation* > list = it.next();
250 foreach(Translation* trans, list)
251 _result.insert(trans->key().toLower(), trans);
257 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
258 QDir plug(QDir::toNativeSeparators(dir));
260 qDebug() << plug.absolutePath() << " folder dosen't exists";
261 return QStringList();
263 plug.setFilter(QDir::Files);
264 QStringList list = plug.entryList(nameFilter);
266 for(int i = 0; i < list.size(); i++)
267 list[i] = plug.absoluteFilePath(list.at(i));
272 void Backbone::loadPlugins() {
275 QStringList nameFilter;
276 nameFilter << "*.so";
277 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
279 foreach(QString file, files) {
280 QPluginLoader loader(file);
282 qDebug()<< file << " " << loader.errorString();
285 QObject *pl = loader.instance();
287 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
288 _plugins.append(plugin);
294 CommonDictInterface* Backbone::plugin(QString type) {
295 foreach(CommonDictInterface* plugin, _plugins)
296 if(plugin->type() == type)
303 void Backbone::loadPrefs(QString fileName) {
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() << ")";
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();
321 void Backbone::savePrefs(QSettings *set) {
324 set->setValue("general/plugin_path", _pluginPath);
325 set->setValue("general/history_length", _historyLen);
326 set->setValue("general/search_limit", _searchLimit);
331 void Backbone::saveDefaultPrefs(QSettings *set) {
334 set->setValue("general/plugin_path", _defaultPluginPath);
335 set->setValue("general/history_length", _defaultHistoryLen);
336 set->setValue("general/search_limit", _defaultSearchLimit);
341 void Backbone::loadDicts(QString fileName, bool _default) {
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() << ")";
352 QSettings set(file.filePath(), QSettings::IniFormat);
353 QStringList dicts = set.childGroups();
354 foreach(QString dict, dicts) {
355 if(!dict.contains("dictionary_"))
357 CommonDictInterface* plug = plugin
358 (set.value(dict + "/type", "").toString());
360 qDebug() << "Config file error: "
361 << set.value(dict + "/type", "").toString()
362 << " dosen't exists";
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());
371 bool active = set.value("active",1).toBool();
374 plugSet->setValue("_default_", "true");
377 addInternalDictionary(plug->getNew(plugSet), active);
383 void Backbone::dictUpdated() {
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);
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);
400 saveDefaultPrefs(&defSet);
402 foreach(CommonDictInterface* dict, _dicts.keys()){
403 if(!dict || !dict->settings())
405 if(!dict->settings()->keys().contains("_default_"))
406 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
408 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
414 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
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);
430 QStringList Backbone::htmls() {
436 void Backbone::searchHtml(QList<Translation *> translations) {
437 _timerHtmlSearch.stop();
439 QList<TranslationPtr> dummy;
440 foreach(Translation* tr, translations)
441 dummy.append(TranslationPtr(tr));
442 _timerHtmlSearch.start(_interval);
444 _innerHtmlResult = QtConcurrent::mapped(dummy,
445 &TranslationPtr::toHtml);
448 void Backbone::htmlTranslationReady() {
449 if(!_innerHtmlResult.isFinished())
452 _timerHtmlSearch.stop();
453 QFutureIterator<QString> it(_innerHtmlResult);
455 _htmlResult.append(it.next());
462 QList<CommonDictInterface*> Backbone::activeDicts() {
463 QList<CommonDictInterface*>res;
464 foreach(CommonDictInterface* dict, _dicts.keys())