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 {return _tr->toHtml();}
45 void Backbone::init() {
46 _interval = 250; //msec
49 if(!_configPath.size())
50 _configPath = QDir::homePath() + "/.mdictionary/mdictionary.config";
51 if(!_defaultConfigPath.size())
52 _defaultConfigPath = QDir::homePath() + "/.mdictionary/mdictionary.defaults";
53 if(!_pluginPath.size())
54 _pluginPath = "/usr/lib/mdictionary";
58 loadPrefs(_defaultConfigPath);
59 _defaultPluginPath = _pluginPath;
60 _defaultHistoryLen = _historyLen;
61 _defaultSearchLimit = _searchLimit;
62 loadPrefs(_configPath);
66 loadDicts(_defaultConfigPath, true);
67 loadDicts(_configPath);
69 connect(&_timerSearch, SIGNAL(timeout()), this, SLOT(translationReady()));
70 connect(&_timerHtmlSearch, SIGNAL(timeout()), this,
71 SLOT(htmlTranslationReady()));
73 _history = new History(5, this);
76 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
80 _pluginPath = pluginPath;
81 _configPath = configPath;
82 _defaultConfigPath = configPath;
92 QListIterator<CommonDictInterface*> it(_dicts.keys());
97 it = QListIterator<CommonDictInterface*>(_plugins);
101 QHashIterator<QString, Translation*> it2(_result);
103 delete it2.next().value();
110 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
112 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
113 _plugins = QList<CommonDictInterface* > (b._plugins);
114 _result = QHash<QString, Translation* > (b._result);
115 _searchLimit = b.searchLimit();
121 int Backbone::searchLimit() const {
128 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
135 QList<CommonDictInterface* > Backbone::getPlugins() {
142 History* Backbone::history() {
149 QMultiHash<QString, Translation*> Backbone::result() {
156 void Backbone::stopSearching() {
158 _timerHtmlSearch.stop();
159 foreach(CommonDictInterface* dict, _dicts.keys())
168 void Backbone::search(QString word) {
171 mappedSearch = word.toLower();
173 _innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
174 _timerSearch.start(_interval);
178 //foreach(CommonDictInterface* dict, _dicts.keys())
179 // if(_dicts[dict] == 1) {
180 // QFuture<QList<Translation*> > tr =
181 // QtConcurrent::run(dict,
182 // &CommonDictInterface::searchWordList,word,
184 // _innerResult.append(tr);
192 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
193 foreach(CommonDictInterface* dict, _dicts.keys())
194 if(activeDicts.contains(dict))
203 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
204 addInternalDictionary(dict,active);
210 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
211 dict->setHash(_dicts.size()+1);
212 _dicts[dict] = active;
213 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
216 void Backbone::removeDictionary(CommonDictInterface *dict) {
224 void Backbone::quit() {
231 int Backbone::activeSearches() const {
237 void Backbone::translationReady() {
238 if(!_innerResult.isFinished())
240 QFutureIterator<QList<Translation*> > it(_innerResult);
242 while(it.hasNext()) {
243 QList<Translation* > list = it.next();
244 foreach(Translation* trans, list)
245 _result.insert(trans->key().toLower(), trans);
252 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
253 QDir plug(QDir::toNativeSeparators(dir));
255 qDebug() << plug.absolutePath() << " folder dosen't exists";
256 return QStringList();
258 plug.setFilter(QDir::Files);
259 QStringList list = plug.entryList(nameFilter);
261 for(int i = 0; i < list.size(); i++)
262 list[i] = plug.absoluteFilePath(list.at(i));
267 void Backbone::loadPlugins() {
270 QStringList nameFilter;
271 nameFilter << "*.so";
272 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
274 foreach(QString file, files) {
275 QPluginLoader loader(file);
277 qDebug()<< file << " " << loader.errorString();
280 QObject *pl = loader.instance();
282 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
283 _plugins.append(plugin);
289 CommonDictInterface* Backbone::plugin(QString type) {
290 foreach(CommonDictInterface* plugin, _plugins)
291 if(plugin->type() == type)
298 void Backbone::loadPrefs(QString fileName) {
301 QFileInfo file(QDir::toNativeSeparators(fileName));
302 QDir confDir(file.dir());
303 if(!confDir.exists()){
304 qDebug() << "Configuration file dosn't exists ("
305 << file.filePath() << ")";
308 QSettings set(file.filePath(), QSettings::IniFormat);
309 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
310 _historyLen = set.value("general/history_length", 10).toInt();
311 _searchLimit = set.value("general/search_limit", 15).toInt();
316 void Backbone::savePrefs(QSettings *set) {
319 set->setValue("general/plugin_path", _pluginPath);
320 set->setValue("general/history_length", _historyLen);
321 set->setValue("general/search_limit", _searchLimit);
326 void Backbone::saveDefaultPrefs(QSettings *set) {
329 set->setValue("general/plugin_path", _defaultPluginPath);
330 set->setValue("general/history_length", _defaultHistoryLen);
331 set->setValue("general/search_limit", _defaultSearchLimit);
336 void Backbone::loadDicts(QString fileName, bool _default) {
339 QFileInfo file(QDir::toNativeSeparators(fileName));
340 QDir confDir(file.dir());
341 if(!confDir.exists()){
342 qDebug() << "Configuration file dosn't exists ("
343 << file.filePath() << ")";
347 QSettings set(file.filePath(), QSettings::IniFormat);
348 QStringList dicts = set.childGroups();
349 foreach(QString dict, dicts) {
350 if(!dict.contains("dictionary_"))
352 CommonDictInterface* plug = plugin
353 (set.value(dict + "/type", "").toString());
355 qDebug() << "Config file error: "
356 << set.value(dict + "/type", "").toString()
357 << " dosen't exists";
360 Settings* plugSet = new Settings();
361 set.beginGroup(dict);
362 QStringList items = set.childKeys();
363 foreach(QString item, items) {
364 plugSet->setValue(item, set.value(item, "").toString());
366 bool active = set.value("active",1).toBool();
369 plugSet->setValue("_default_", "true");
372 addInternalDictionary(plug->getNew(plugSet), active);
378 void Backbone::dictUpdated() {
381 QFileInfo file(QDir::toNativeSeparators(_configPath));
382 QDir confDir(file.dir());
383 if(!confDir.exists())
384 confDir.mkpath(file.dir().path());
385 QSettings set(file.filePath(), QSettings::IniFormat);
388 QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
389 QDir defConfDir(defFile.dir());
390 if(!defConfDir.exists())
391 defConfDir.mkpath(defFile.dir().path());
392 QSettings defSet(defFile.filePath(), QSettings::IniFormat);
395 saveDefaultPrefs(&defSet);
397 foreach(CommonDictInterface* dict, _dicts.keys()){
398 if(!dict || !dict->settings())
400 if(!dict->settings()->keys().contains("_default_"))
401 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
403 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
409 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
416 section.append(QString("dictionary_%1").arg(hash));
417 QList<QString> keys = plugSet->keys();
418 foreach(QString key, keys)
419 set->setValue(section + "/" + key, plugSet->value(key));
420 set->setValue(section + "/active", active);
425 QStringList Backbone::htmls() {
431 void Backbone::searchHtml(QList<Translation *> translations) {
432 _timerHtmlSearch.stop();
434 QList<TranslationPtr> dummy;
435 foreach(Translation* tr, translations)
436 dummy.append(TranslationPtr(tr));
437 _timerHtmlSearch.start(_interval);
439 _innerHtmlResult = QtConcurrent::mapped(dummy,
440 &TranslationPtr::toHtml);
443 void Backbone::htmlTranslationReady() {
444 if(!_innerHtmlResult.isFinished())
447 _timerHtmlSearch.stop();
448 QFutureIterator<QString> it(_innerHtmlResult);
450 _htmlResult.append(it.next());
457 QList<CommonDictInterface*> Backbone::activeDicts() {
458 QList<CommonDictInterface*>res;
459 foreach(CommonDictInterface* dict, _dicts.keys())