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) {
33 qDebug() << "ooooooooooooooooooooooo" << unsigned(dict);
35 return dict->searchWordList(mappedSearch, 15);
36 return QList<Translation*>();
39 class TranslationPtr {
42 TranslationPtr(Translation* tr) :_tr(tr) {}
43 QString toHtml() const {
45 trans = _tr->toHtml();
51 void Backbone::init() {
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(&_resultWatcher, SIGNAL(finished()), this, SLOT(translationReady()));
74 connect(&_htmlResultWatcher, SIGNAL(finished()), this,
75 SLOT(htmlTranslationReady()));
76 connect(&_bookmarkWatcher, SIGNAL(finished()), this,
77 SLOT(bookmarksListReady()));
78 connect(&_bookmarkSearchWatcher, SIGNAL(finished()), this,
79 SLOT(translationReady()));
81 QThreadPool::globalInstance()->setMaxThreadCount(
82 QThreadPool::globalInstance()->maxThreadCount()+1);
84 _history = new History(5, this);
89 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
93 _pluginPath = pluginPath;
94 _configPath = configPath;
95 _defaultConfigPath = configPath;
104 Backbone::~Backbone()
106 QListIterator<CommonDictInterface*> it(_dicts.keys());
111 it = QListIterator<CommonDictInterface*>(_plugins);
115 QHashIterator<QString, Translation*> it2(_result);
117 delete it2.next().value();
124 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
125 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
126 _plugins = QList<CommonDictInterface* > (b._plugins);
127 _result = QHash<QString, Translation* > (b._result);
128 _searchLimit = b.searchLimit();
134 int Backbone::searchLimit() const {
140 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
146 QList<CommonDictInterface* > Backbone::getPlugins() {
152 History* Backbone::history() {
158 QMultiHash<QString, Translation*> Backbone::result() {
164 void Backbone::stopSearching() {
168 foreach(CommonDictInterface* dict, _dicts.keys())
171 _innerHtmlResult.cancel();
172 _innerResult.cancel();
173 Q_EMIT searchCanceled();
178 void Backbone::search(QString word){
180 mappedSearch = word.toLower();
183 dictFin = !_searchDicts;
184 bookmarkFin = !_searchBookmarks;
187 _innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
188 _resultWatcher.setFuture(_innerResult);
191 if(_searchBookmarks) {
192 _innerBookmarks = QtConcurrent::run(_bookmarks,
193 &Bookmarks::searchWordList, word);
194 _bookmarkSearchWatcher.setFuture(_innerBookmarks);
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()));
222 connect(dict, SIGNAL(notify(Notify::NotifyType,QString)), this,
223 SIGNAL(notify(Notify::NotifyType,QString)));
226 void Backbone::removeDictionary(CommonDictInterface *dict) {
234 void Backbone::quit() {
241 void Backbone::translationReady() {
242 if(!dictFin && _innerResult.isFinished()) {
244 QFutureIterator<QList<Translation*> > it(_innerResult);
246 while(it.hasNext()) {
247 QList<Translation* > list = it.next();
248 foreach(Translation* trans, list)
249 _result.insert(trans->key().toLower(), trans);
253 if(!bookmarkFin && _innerBookmarks.isFinished()) {
255 QList<Translation*> list = _innerBookmarks.result();
257 foreach(Translation* trans, list)
258 _result.insert(trans->key().toLower(), trans);
261 if(!stopped && bookmarkFin && dictFin)
265 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
266 QDir plug(QDir::toNativeSeparators(dir));
268 qDebug() << plug.absolutePath() << " folder dosen't exists";
269 Q_EMIT notify(Notify::Warning,
270 QString("%1 folder dosen't exists.").arg(plug.path()));
271 return QStringList();
273 plug.setFilter(QDir::Files);
274 QStringList list = plug.entryList(nameFilter);
276 for(int i = 0; i < list.size(); i++)
277 list[i] = plug.absoluteFilePath(list.at(i));
282 void Backbone::loadPlugins() {
285 QStringList nameFilter;
286 nameFilter << "*.so";
287 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
289 foreach(QString file, files) {
290 QPluginLoader loader(file);
292 Q_EMIT notify(Notify::Error,
293 QString("%1 plugin cannot be loaded: %2.")
294 .arg(file).arg(loader.errorString()));
295 qDebug()<< file << " " << loader.errorString();
298 QObject *pl = loader.instance();
300 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
301 _plugins.append(plugin);
307 CommonDictInterface* Backbone::plugin(QString type) {
308 foreach(CommonDictInterface* plugin, _plugins)
309 if(plugin->type() == type)
316 void Backbone::loadPrefs(QString fileName) {
319 QFileInfo file(QDir::toNativeSeparators(fileName));
320 QDir confDir(file.dir());
321 if(!confDir.exists()){
322 qDebug() << "Configuration file dosn't exists ("
323 << file.filePath() << ")";
324 Q_EMIT notify(Notify::Warning,
325 QString("%1 configurationfile dosen't exists.")
326 .arg(file.filePath()));
329 QSettings set(file.filePath(), QSettings::IniFormat);
330 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
331 _historyLen = set.value("general/history_size", 10).toInt();
332 _searchLimit = set.value("general/search_limit", 15).toInt();
333 _searchBookmarks = set.value("general/search_bookmarks",1).toBool();
334 _searchDicts = set.value("general/search_dictionaries",1).toBool();
339 void Backbone::savePrefs(QSettings *set) {
342 set->setValue("general/plugin_path", _pluginPath);
343 set->setValue("general/history_size", _historyLen);
344 set->setValue("general/search_limit", _searchLimit);
345 set->setValue("general/search_bookmarks", _searchBookmarks);
346 set->setValue("general/search_dictionaries", _searchDicts);
351 void Backbone::saveDefaultPrefs(QSettings *set) {
354 set->setValue("general/plugin_path", _defaultPluginPath);
355 set->setValue("general/history_size", _defaultHistoryLen);
356 set->setValue("general/search_limit", _defaultSearchLimit);
361 void Backbone::loadDicts(QString fileName, bool _default) {
364 QFileInfo file(QDir::toNativeSeparators(fileName));
365 QDir confDir(file.dir());
366 if(!confDir.exists()){
367 qDebug() << "Configuration file dosn't exists ("
368 << file.filePath() << ")";
369 Q_EMIT notify(Notify::Warning,
370 QString("%1 configurationfile dosen't exists.")
371 .arg(file.filePath()));
375 QSettings set(file.filePath(), QSettings::IniFormat);
376 QStringList dicts = set.childGroups();
377 foreach(QString dict, dicts) {
378 if(!dict.contains("dictionary_"))
380 CommonDictInterface* plug = plugin
381 (set.value(dict + "/type", "").toString());
383 qDebug() << "Config file error: "
384 << set.value(dict + "/type", "").toString()
385 << " dosen't exists";
386 Q_EMIT notify(Notify::Warning,
387 QString("Configuration file error. %2 plugin dosen't exists.")
388 .arg(set.value(dict + "/type", "").toString()));
391 Settings* plugSet = new Settings();
392 set.beginGroup(dict);
393 QStringList items = set.childKeys();
394 foreach(QString item, items) {
395 plugSet->setValue(item, set.value(item, "").toString());
397 bool active = set.value("active",1).toBool();
400 plugSet->setValue("_default_", "true");
403 addInternalDictionary(plug->getNew(plugSet), active);
409 void Backbone::dictUpdated() {
412 _history->setMaxSize(_historyLen);
413 QFileInfo file(QDir::toNativeSeparators(_configPath));
414 QDir confDir(file.dir());
415 if(!confDir.exists())
416 confDir.mkpath(file.dir().path());
417 QSettings set(file.filePath(), QSettings::IniFormat);
420 QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
421 QDir defConfDir(defFile.dir());
422 if(!defConfDir.exists())
423 defConfDir.mkpath(defFile.dir().path());
424 QSettings defSet(defFile.filePath(), QSettings::IniFormat);
427 saveDefaultPrefs(&defSet);
429 foreach(CommonDictInterface* dict, _dicts.keys()){
430 if(!dict || !dict->settings())
432 if(!dict->settings()->keys().contains("_default_"))
433 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
435 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
441 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
448 section.append(QString("dictionary_%1").arg(hash));
449 QList<QString> keys = plugSet->keys();
450 foreach(QString key, keys)
451 set->setValue(section + "/" + key, plugSet->value(key));
452 set->setValue(section + "/active", active);
457 QStringList Backbone::htmls() {
463 void Backbone::searchHtml(QList<Translation *> translations) {
466 QList<TranslationPtr> dummy;
468 foreach(Translation* tr, translations)
469 dummy.append(TranslationPtr(tr));
471 _innerHtmlResult = QtConcurrent::mapped(dummy,
472 &TranslationPtr::toHtml);
473 _htmlResultWatcher.setFuture(_innerHtmlResult);
476 void Backbone::htmlTranslationReady() {
478 QFutureIterator<QString> it(_innerHtmlResult);
480 _htmlResult.append(it.next());
488 QList<CommonDictInterface*> Backbone::activeDicts() {
489 QList<CommonDictInterface*>res;
490 foreach(CommonDictInterface* dict, _dicts.keys())
499 void Backbone::bookmarksListReady() {
500 _bookmarksResult = _innerBookmarks.result();
501 Q_EMIT bookmarksReady();
507 void Backbone::setSettings(Settings *settings) {
508 _historyLen = settings->value("history_size").toInt();
509 _searchLimit = settings->value("search_limit").toInt();
510 if(settings->value("search_dictionaries") == "true")
514 if(settings->value("search_bookmarks") == "true")
515 _searchBookmarks = 1;
517 _searchBookmarks = 0;
524 Settings* Backbone::settings() {
525 Settings * settings = new Settings();
526 settings->setValue("history_size", QString("%1").arg(_historyLen));
527 settings->setValue("search_limit", QString("%1").arg(_searchLimit));
529 settings->setValue("search_bookmarks", "true");
531 settings->setValue("search_bookmarks", "false");
534 settings->setValue("search_dictionaries", "true");
536 settings->setValue("search_dictionaries", "false");