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>
31 void Backbone::init() {
32 _interval = 250; //msec
35 if(!_configPath.size())
36 _configPath = QDir::homePath() + "/.mdictionary/mdictionary.config";
37 if(!_defaultConfigPath.size())
38 _defaultConfigPath = QDir::homePath() + "/.mdictionary/mdictionary.defaults";
39 if(!_pluginPath.size())
40 _pluginPath = "/usr/lib/mdictionary";
44 loadPrefs(_defaultConfigPath);
45 _defaultPluginPath = _pluginPath;
46 _defaultHistoryLen = _historyLen;
47 _defaultSearchLimit = _searchLimit;
48 loadPrefs(_configPath);
52 loadDicts(_defaultConfigPath, true);
53 loadDicts(_configPath);
55 connect(&_timerSearch, SIGNAL(timeout()), this, SLOT(translationReady()));
56 connect(&_timerHtmlSearch, SIGNAL(timeout()), this,
57 SLOT(htmlTranslationReady()));
59 _history = new History(5, this);
62 Backbone::Backbone(QString pluginPath, QString configPath, bool dry,
66 _pluginPath = pluginPath;
67 _configPath = configPath;
68 _defaultConfigPath = configPath;
78 QListIterator<CommonDictInterface*> it(_dicts.keys());
83 it = QListIterator<CommonDictInterface*>(_plugins);
87 QHashIterator<QString, Translation*> it2(_result);
89 delete it2.next().value();
96 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
98 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
99 _plugins = QList<CommonDictInterface* > (b._plugins);
100 _result = QHash<QString, Translation* > (b._result);
101 _searchLimit = b.searchLimit();
107 int Backbone::searchLimit() const {
114 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
121 QList<CommonDictInterface* > Backbone::getPlugins() {
128 History* Backbone::history() {
135 QMultiHash<QString, Translation*> Backbone::result() {
142 void Backbone::stopSearching() {
144 _timerHtmlSearch.stop();
145 foreach(CommonDictInterface* dict, _dicts.keys())
152 void Backbone::search(QString word) {
155 _innerResult.clear();
157 _timerSearch.start(_interval);
159 foreach(CommonDictInterface* dict, _dicts.keys())
160 if(_dicts[dict] == 1) {
161 QFuture<QList<Translation*> > tr =
162 QtConcurrent::run(dict,
163 &CommonDictInterface::searchWordList,word,
165 _innerResult.append(tr);
173 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
174 foreach(CommonDictInterface* dict, _dicts.keys())
175 if(activeDicts.contains(dict))
184 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
185 addInternalDictionary(dict,active);
191 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
192 dict->setHash(_dicts.size()+1);
193 _dicts[dict] = active;
194 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
197 void Backbone::removeDictionary(CommonDictInterface *dict) {
205 void Backbone::quit() {
212 int Backbone::activeSearches() const {
213 return _innerResult.size();
218 void Backbone::translationReady() {
219 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
220 if(!trans.isFinished())
222 QList<Translation*> tList = trans.result();
223 foreach(Translation* t, tList) {
224 _result.insert(t->key().toLower(), t);
226 _innerResult.removeOne(trans);
228 if(!_innerResult.size()) {
234 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
235 QDir plug(QDir::toNativeSeparators(dir));
237 qDebug() << plug.absolutePath() << " folder dosen't exists";
238 return QStringList();
240 plug.setFilter(QDir::Files);
241 QStringList list = plug.entryList(nameFilter);
243 for(int i = 0; i < list.size(); i++)
244 list[i] = plug.absoluteFilePath(list.at(i));
249 void Backbone::loadPlugins() {
252 QStringList nameFilter;
253 nameFilter << "*.so";
254 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
256 foreach(QString file, files) {
257 QPluginLoader loader(file);
259 qDebug()<< file << " " << loader.errorString();
262 QObject *pl = loader.instance();
264 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
265 _plugins.append(plugin);
271 CommonDictInterface* Backbone::plugin(QString type) {
272 foreach(CommonDictInterface* plugin, _plugins)
273 if(plugin->type() == type)
280 void Backbone::loadPrefs(QString fileName) {
283 QFileInfo file(QDir::toNativeSeparators(fileName));
284 QDir confDir(file.dir());
285 if(!confDir.exists()){
286 qDebug() << "Configuration file dosn't exists ("
287 << file.filePath() << ")";
290 QSettings set(file.filePath(), QSettings::IniFormat);
291 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
292 _historyLen = set.value("general/history_length", 10).toInt();
293 _searchLimit = set.value("general/search_limit", 15).toInt();
298 void Backbone::savePrefs(QSettings *set) {
301 set->setValue("general/plugin_path", _pluginPath);
302 set->setValue("general/history_length", _historyLen);
303 set->setValue("general/search_limit", _searchLimit);
308 void Backbone::saveDefaultPrefs(QSettings *set) {
311 set->setValue("general/plugin_path", _defaultPluginPath);
312 set->setValue("general/history_length", _defaultHistoryLen);
313 set->setValue("general/search_limit", _defaultSearchLimit);
318 void Backbone::loadDicts(QString fileName, bool _default) {
321 QFileInfo file(QDir::toNativeSeparators(fileName));
322 QDir confDir(file.dir());
323 if(!confDir.exists()){
324 qDebug() << "Configuration file dosn't exists ("
325 << file.filePath() << ")";
329 QSettings set(file.filePath(), QSettings::IniFormat);
330 QStringList dicts = set.childGroups();
331 foreach(QString dict, dicts) {
332 if(!dict.contains("dictionary_"))
334 CommonDictInterface* plug = plugin
335 (set.value(dict + "/type", "").toString());
337 qDebug() << "Config file error: "
338 << set.value(dict + "/type", "").toString()
339 << " dosen't exists";
342 Settings* plugSet = new Settings();
343 set.beginGroup(dict);
344 QStringList items = set.childKeys();
345 foreach(QString item, items) {
346 plugSet->setValue(item, set.value(item, "").toString());
348 bool active = set.value("active",1).toBool();
351 plugSet->setValue("_default_", "true");
354 addInternalDictionary(plug->getNew(plugSet), active);
360 void Backbone::dictUpdated() {
363 QFileInfo file(QDir::toNativeSeparators(_configPath));
364 QDir confDir(file.dir());
365 if(!confDir.exists())
366 confDir.mkpath(file.dir().path());
367 QSettings set(file.filePath(), QSettings::IniFormat);
370 QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
371 QDir defConfDir(defFile.dir());
372 if(!defConfDir.exists())
373 defConfDir.mkpath(defFile.dir().path());
374 QSettings defSet(defFile.filePath(), QSettings::IniFormat);
377 saveDefaultPrefs(&defSet);
379 foreach(CommonDictInterface* dict, _dicts.keys()){
380 if(!dict || !dict->settings())
382 if(!dict->settings()->keys().contains("_default_"))
383 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
385 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
391 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
398 section.append(QString("dictionary_%1").arg(hash));
399 QList<QString> keys = plugSet->keys();
400 foreach(QString key, keys)
401 set->setValue(section + "/" + key, plugSet->value(key));
402 set->setValue(section + "/active", active);
407 QStringList Backbone::htmls() {
413 void Backbone::searchHtml(QList<Translation *> translations) {
414 _timerHtmlSearch.stop();
416 _innerHtmlResult.clear();
417 _timerHtmlSearch.start();
419 foreach(Translation* trans, translations)
421 _innerHtmlResult.append(
422 QtConcurrent::run(trans, &Translation::toHtml));
425 void Backbone::htmlTranslationReady() {
426 foreach(QFuture<QString> res, _innerHtmlResult) {
427 if(!res.isFinished())
429 _htmlResult.append(res.result());
430 _innerHtmlResult.removeOne(res);
432 if(!_innerHtmlResult.size()) {
433 _timerHtmlSearch.stop();