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()));
75 connect(&_bookmarkWatcher, SIGNAL(finished()), this,
76 SLOT(bookmarksListReady()));
78 QThreadPool::globalInstance()->setMaxThreadCount(
79 QThreadPool::globalInstance()->maxThreadCount()+1);
81 _history = new History(5, this);
86 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
90 _pluginPath = pluginPath;
91 _configPath = configPath;
92 _defaultConfigPath = configPath;
101 Backbone::~Backbone()
103 QListIterator<CommonDictInterface*> it(_dicts.keys());
108 it = QListIterator<CommonDictInterface*>(_plugins);
112 QHashIterator<QString, Translation*> it2(_result);
114 delete it2.next().value();
121 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
122 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
123 _plugins = QList<CommonDictInterface* > (b._plugins);
124 _result = QHash<QString, Translation* > (b._result);
125 _searchLimit = b.searchLimit();
131 int Backbone::searchLimit() const {
137 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
143 QList<CommonDictInterface* > Backbone::getPlugins() {
149 History* Backbone::history() {
155 QMultiHash<QString, Translation*> Backbone::result() {
161 void Backbone::stopSearching() {
165 foreach(CommonDictInterface* dict, _dicts.keys())
168 _innerHtmlResult.cancel();
169 _innerResult.cancel();
170 Q_EMIT searchCanceled();
175 void Backbone::search(QString word) {
177 mappedSearch = word.toLower();
181 _innerResult = QtConcurrent::mapped(activeDicts(), mapSearch);
182 _resultWatcher.setFuture(_innerResult);
187 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
188 foreach(CommonDictInterface* dict, _dicts.keys())
189 if(activeDicts.contains(dict))
198 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
199 addInternalDictionary(dict,active);
205 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
206 dict->setHash(_dicts.size()+1);
207 _dicts[dict] = active;
208 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
211 void Backbone::removeDictionary(CommonDictInterface *dict) {
219 void Backbone::quit() {
226 void Backbone::translationReady() {
227 //if(!_innerResult.isFinished())
229 QFutureIterator<QList<Translation*> > it(_innerResult);
231 while(it.hasNext()) {
232 QList<Translation* > list = it.next();
233 foreach(Translation* trans, list)
234 _result.insert(trans->key().toLower(), trans);
237 //qDebug () << "time " << _time.elapsed();
242 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
243 QDir plug(QDir::toNativeSeparators(dir));
245 qDebug() << plug.absolutePath() << " folder dosen't exists";
246 return QStringList();
248 plug.setFilter(QDir::Files);
249 QStringList list = plug.entryList(nameFilter);
251 for(int i = 0; i < list.size(); i++)
252 list[i] = plug.absoluteFilePath(list.at(i));
257 void Backbone::loadPlugins() {
260 QStringList nameFilter;
261 nameFilter << "*.so";
262 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
264 foreach(QString file, files) {
265 QPluginLoader loader(file);
267 qDebug()<< file << " " << loader.errorString();
270 QObject *pl = loader.instance();
272 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
273 _plugins.append(plugin);
279 CommonDictInterface* Backbone::plugin(QString type) {
280 foreach(CommonDictInterface* plugin, _plugins)
281 if(plugin->type() == type)
288 void Backbone::loadPrefs(QString fileName) {
291 QFileInfo file(QDir::toNativeSeparators(fileName));
292 QDir confDir(file.dir());
293 if(!confDir.exists()){
294 qDebug() << "Configuration file dosn't exists ("
295 << file.filePath() << ")";
298 QSettings set(file.filePath(), QSettings::IniFormat);
299 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
300 _historyLen = set.value("general/history_length", 10).toInt();
301 _searchLimit = set.value("general/search_limit", 15).toInt();
306 void Backbone::savePrefs(QSettings *set) {
309 set->setValue("general/plugin_path", _pluginPath);
310 set->setValue("general/history_length", _historyLen);
311 set->setValue("general/search_limit", _searchLimit);
316 void Backbone::saveDefaultPrefs(QSettings *set) {
319 set->setValue("general/plugin_path", _defaultPluginPath);
320 set->setValue("general/history_length", _defaultHistoryLen);
321 set->setValue("general/search_limit", _defaultSearchLimit);
326 void Backbone::loadDicts(QString fileName, bool _default) {
329 QFileInfo file(QDir::toNativeSeparators(fileName));
330 QDir confDir(file.dir());
331 if(!confDir.exists()){
332 qDebug() << "Configuration file dosn't exists ("
333 << file.filePath() << ")";
337 QSettings set(file.filePath(), QSettings::IniFormat);
338 QStringList dicts = set.childGroups();
339 foreach(QString dict, dicts) {
340 if(!dict.contains("dictionary_"))
342 CommonDictInterface* plug = plugin
343 (set.value(dict + "/type", "").toString());
345 qDebug() << "Config file error: "
346 << set.value(dict + "/type", "").toString()
347 << " dosen't exists";
350 Settings* plugSet = new Settings();
351 set.beginGroup(dict);
352 QStringList items = set.childKeys();
353 foreach(QString item, items) {
354 plugSet->setValue(item, set.value(item, "").toString());
356 bool active = set.value("active",1).toBool();
359 plugSet->setValue("_default_", "true");
362 addInternalDictionary(plug->getNew(plugSet), active);
368 void Backbone::dictUpdated() {
371 QFileInfo file(QDir::toNativeSeparators(_configPath));
372 QDir confDir(file.dir());
373 if(!confDir.exists())
374 confDir.mkpath(file.dir().path());
375 QSettings set(file.filePath(), QSettings::IniFormat);
378 QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
379 QDir defConfDir(defFile.dir());
380 if(!defConfDir.exists())
381 defConfDir.mkpath(defFile.dir().path());
382 QSettings defSet(defFile.filePath(), QSettings::IniFormat);
385 saveDefaultPrefs(&defSet);
387 foreach(CommonDictInterface* dict, _dicts.keys()){
388 if(!dict || !dict->settings())
390 if(!dict->settings()->keys().contains("_default_"))
391 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
393 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
399 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
406 section.append(QString("dictionary_%1").arg(hash));
407 QList<QString> keys = plugSet->keys();
408 foreach(QString key, keys)
409 set->setValue(section + "/" + key, plugSet->value(key));
410 set->setValue(section + "/active", active);
415 QStringList Backbone::htmls() {
421 void Backbone::searchHtml(QList<Translation *> translations) {
423 QList<TranslationPtr> dummy;
426 foreach(Translation* tr, translations)
427 dummy.append(TranslationPtr(tr));
429 _innerHtmlResult = QtConcurrent::mapped(dummy,
430 &TranslationPtr::toHtml);
431 _htmlResultWatcher.setFuture(_innerHtmlResult);
434 void Backbone::htmlTranslationReady() {
435 //if(!_innerHtmlResult.isFinished())
438 QFutureIterator<QString> it(_innerHtmlResult);
440 _htmlResult.append(it.next());
442 //qDebug() << "time " << _time.elapsed();
449 QList<CommonDictInterface*> Backbone::activeDicts() {
450 QList<CommonDictInterface*>res;
451 foreach(CommonDictInterface* dict, _dicts.keys())
460 void Backbone::bookmarksListReady() {
461 _bookmarksResult = _innerBookmarks.result();
462 Q_EMIT bookmarksReady();