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() {
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";
61 loadPrefs(_defaultConfigPath);
62 _defaultPluginPath = _pluginPath;
63 _defaultHistoryLen = _historyLen;
64 _defaultSearchLimit = _searchLimit;
65 loadPrefs(_configPath);
69 loadDicts(_defaultConfigPath, true);
70 loadDicts(_configPath);
72 connect(&_resultWatcher, SIGNAL(finished()), this, SLOT(translationReady()));
73 connect(&_htmlResultWatcher, SIGNAL(finished()), this,
74 SLOT(htmlTranslationReady()));
76 QThreadPool::globalInstance()->setMaxThreadCount(
77 QThreadPool::globalInstance()->maxThreadCount()+1);
79 _history = new History(5, this);
84 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
88 _pluginPath = pluginPath;
89 _configPath = configPath;
90 _defaultConfigPath = configPath;
101 QListIterator<CommonDictInterface*> it(_dicts.keys());
106 it = QListIterator<CommonDictInterface*>(_plugins);
110 QHashIterator<QString, Translation*> it2(_result);
112 delete it2.next().value();
119 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 {
135 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
141 QList<CommonDictInterface* > Backbone::getPlugins() {
147 History* Backbone::history() {
153 QMultiHash<QString, Translation*> Backbone::result() {
159 void Backbone::stopSearching() {
163 foreach(CommonDictInterface* dict, _dicts.keys())
166 _innerHtmlResult.cancel();
167 _innerResult.cancel();
168 Q_EMIT searchCanceled();
173 void Backbone::search(QString word) {
175 mappedSearch = word.toLower();
179 _innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
180 _resultWatcher.setFuture(_innerResult);
185 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
186 foreach(CommonDictInterface* dict, _dicts.keys())
187 if(activeDicts.contains(dict))
196 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
197 addInternalDictionary(dict,active);
203 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
204 dict->setHash(_dicts.size()+1);
205 _dicts[dict] = active;
206 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
209 void Backbone::removeDictionary(CommonDictInterface *dict) {
217 void Backbone::quit() {
224 void Backbone::translationReady() {
225 //if(!_innerResult.isFinished())
227 QFutureIterator<QList<Translation*> > it(_innerResult);
229 while(it.hasNext()) {
230 QList<Translation* > list = it.next();
231 foreach(Translation* trans, list)
232 _result.insert(trans->key().toLower(), trans);
235 //qDebug () << "time " << _time.elapsed();
240 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
241 QDir plug(QDir::toNativeSeparators(dir));
243 qDebug() << plug.absolutePath() << " folder dosen't exists";
244 return QStringList();
246 plug.setFilter(QDir::Files);
247 QStringList list = plug.entryList(nameFilter);
249 for(int i = 0; i < list.size(); i++)
250 list[i] = plug.absoluteFilePath(list.at(i));
255 void Backbone::loadPlugins() {
258 QStringList nameFilter;
259 nameFilter << "*.so";
260 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
262 foreach(QString file, files) {
263 QPluginLoader loader(file);
265 qDebug()<< file << " " << loader.errorString();
268 QObject *pl = loader.instance();
270 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
271 _plugins.append(plugin);
277 CommonDictInterface* Backbone::plugin(QString type) {
278 foreach(CommonDictInterface* plugin, _plugins)
279 if(plugin->type() == type)
286 void Backbone::loadPrefs(QString fileName) {
289 QFileInfo file(QDir::toNativeSeparators(fileName));
290 QDir confDir(file.dir());
291 if(!confDir.exists()){
292 qDebug() << "Configuration file dosn't exists ("
293 << file.filePath() << ")";
296 QSettings set(file.filePath(), QSettings::IniFormat);
297 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
298 _historyLen = set.value("general/history_length", 10).toInt();
299 _searchLimit = set.value("general/search_limit", 15).toInt();
304 void Backbone::savePrefs(QSettings *set) {
307 set->setValue("general/plugin_path", _pluginPath);
308 set->setValue("general/history_length", _historyLen);
309 set->setValue("general/search_limit", _searchLimit);
314 void Backbone::saveDefaultPrefs(QSettings *set) {
317 set->setValue("general/plugin_path", _defaultPluginPath);
318 set->setValue("general/history_length", _defaultHistoryLen);
319 set->setValue("general/search_limit", _defaultSearchLimit);
324 void Backbone::loadDicts(QString fileName, bool _default) {
327 QFileInfo file(QDir::toNativeSeparators(fileName));
328 QDir confDir(file.dir());
329 if(!confDir.exists()){
330 qDebug() << "Configuration file dosn't exists ("
331 << file.filePath() << ")";
335 QSettings set(file.filePath(), QSettings::IniFormat);
336 QStringList dicts = set.childGroups();
337 foreach(QString dict, dicts) {
338 if(!dict.contains("dictionary_"))
340 CommonDictInterface* plug = plugin
341 (set.value(dict + "/type", "").toString());
343 qDebug() << "Config file error: "
344 << set.value(dict + "/type", "").toString()
345 << " dosen't exists";
348 Settings* plugSet = new Settings();
349 set.beginGroup(dict);
350 QStringList items = set.childKeys();
351 foreach(QString item, items) {
352 plugSet->setValue(item, set.value(item, "").toString());
354 bool active = set.value("active",1).toBool();
357 plugSet->setValue("_default_", "true");
360 addInternalDictionary(plug->getNew(plugSet), active);
366 void Backbone::dictUpdated() {
369 QFileInfo file(QDir::toNativeSeparators(_configPath));
370 QDir confDir(file.dir());
371 if(!confDir.exists())
372 confDir.mkpath(file.dir().path());
373 QSettings set(file.filePath(), QSettings::IniFormat);
376 QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
377 QDir defConfDir(defFile.dir());
378 if(!defConfDir.exists())
379 defConfDir.mkpath(defFile.dir().path());
380 QSettings defSet(defFile.filePath(), QSettings::IniFormat);
383 saveDefaultPrefs(&defSet);
385 foreach(CommonDictInterface* dict, _dicts.keys()){
386 if(!dict || !dict->settings())
388 if(!dict->settings()->keys().contains("_default_"))
389 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
391 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
397 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
404 section.append(QString("dictionary_%1").arg(hash));
405 QList<QString> keys = plugSet->keys();
406 foreach(QString key, keys)
407 set->setValue(section + "/" + key, plugSet->value(key));
408 set->setValue(section + "/active", active);
413 QStringList Backbone::htmls() {
419 void Backbone::searchHtml(QList<Translation *> translations) {
421 QList<TranslationPtr> dummy;
424 foreach(Translation* tr, translations)
425 dummy.append(TranslationPtr(tr));
427 _innerHtmlResult = QtConcurrent::mapped(dummy,
428 &TranslationPtr::toHtml);
429 _htmlResultWatcher.setFuture(_innerHtmlResult);
432 void Backbone::htmlTranslationReady() {
433 //if(!_innerHtmlResult.isFinished())
436 QFutureIterator<QString> it(_innerHtmlResult);
438 _htmlResult.append(it.next());
440 //qDebug() << "time " << _time.elapsed();
447 QList<CommonDictInterface*> Backbone::activeDicts() {
448 QList<CommonDictInterface*>res;
449 foreach(CommonDictInterface* dict, _dicts.keys())