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 *******************************************************************************/
22 // Created by Bartosz Szatkowski
27 void Backbone::init() {
28 _interval = 250; //msec
31 if(!_configPath.size())
32 _configPath = QDir::homePath() + "/.mdictionary/mdictionary.config";
33 if(!_defaultConfigPath.size())
34 _defaultConfigPath = QDir::homePath() + "/.mdictionary/mdictionary.defaults";
35 if(!_pluginPath.size())
36 _pluginPath = "/usr/lib/mdictionary";
40 loadPrefs(_defaultConfigPath);
41 _defaultPluginPath = _pluginPath;
42 _defaultHistoryLen = _historyLen;
43 _defaultSearchLimit = _searchLimit;
44 loadPrefs(_configPath);
48 loadDicts(_defaultConfigPath, true);
49 loadDicts(_configPath);
51 connect(&_timerSearch, SIGNAL(timeout()), this, SLOT(translationReady()));
52 connect(&_timerHtmlSearch, SIGNAL(timeout()), this,
53 SLOT(htmlTranslationReady()));
55 _history = new History(5, this);
58 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
63 _pluginPath = pluginPath;
64 _configPath = configPath;
65 _defaultConfigPath = configPath;
72 QListIterator<CommonDictInterface*> it(_dicts.keys());
77 it = QListIterator<CommonDictInterface*>(_plugins);
81 QHashIterator<QString, Translation*> it2(_result);
83 delete it2.next().value();
90 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
92 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
93 _plugins = QList<CommonDictInterface* > (b._plugins);
94 _result = QHash<QString, Translation* > (b._result);
95 _searchLimit = b.searchLimit();
101 int Backbone::searchLimit() const {
108 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
115 QList<CommonDictInterface* > Backbone::getPlugins() {
122 History* Backbone::history() {
129 QMultiHash<QString, Translation*> Backbone::result() {
136 void Backbone::stopSearching() {
138 _timerHtmlSearch.stop();
139 foreach(CommonDictInterface* dict, _dicts.keys())
146 void Backbone::search(QString word) {
149 _innerResult.clear();
151 _timerSearch.start(_interval);
153 foreach(CommonDictInterface* dict, _dicts.keys())
154 if(_dicts[dict] == 1) {
155 QFuture<QList<Translation*> > tr =
156 QtConcurrent::run(dict,
157 &CommonDictInterface::searchWordList,word,
159 _innerResult.append(tr);
167 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
168 foreach(CommonDictInterface* dict, _dicts.keys())
169 if(activeDicts.contains(dict))
178 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
179 addInternalDictionary(dict,active);
185 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
186 dict->setHash(_dicts.size()+1);
187 _dicts[dict] = active;
188 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
191 void Backbone::removeDictionary(CommonDictInterface *dict) {
199 void Backbone::quit() {
206 int Backbone::activeSearches() const {
207 return _innerResult.size();
212 void Backbone::translationReady() {
213 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
214 if(!trans.isFinished())
216 QList<Translation*> tList = trans.result();
217 foreach(Translation* t, tList) {
218 _result.insert(t->key().toLower(), t);
220 _innerResult.removeOne(trans);
222 if(!_innerResult.size()) {
228 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
229 QDir plug(QDir::toNativeSeparators(dir));
231 qDebug() << plug.absolutePath() << " folder dosen't exists";
232 return QStringList();
234 plug.setFilter(QDir::Files);
235 QStringList list = plug.entryList(nameFilter);
237 for(int i = 0; i < list.size(); i++)
238 list[i] = plug.absoluteFilePath(list.at(i));
243 void Backbone::loadPlugins() {
246 QStringList nameFilter;
247 nameFilter << "*.so";
248 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
251 foreach(QString file, files) {
252 QPluginLoader loader(file);
254 qDebug()<< file << " " << loader.errorString();
257 QObject *pl = loader.instance();
259 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
260 _plugins.append(plugin);
266 CommonDictInterface* Backbone::plugin(QString type) {
267 foreach(CommonDictInterface* plugin, _plugins)
268 if(plugin->type() == type)
275 void Backbone::loadPrefs(QString fileName) {
278 QFileInfo file(QDir::toNativeSeparators(fileName));
279 QDir confDir(file.dir());
280 if(!confDir.exists()){
281 qDebug() << "Configuration file dosn't exists ("
282 << file.filePath() << ")";
285 QSettings set(file.filePath(), QSettings::IniFormat);
286 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
287 _historyLen = set.value("general/history_length", 10).toInt();
288 _searchLimit = set.value("general/search_limit", 15).toInt();
293 void Backbone::savePrefs(QSettings *set) {
296 set->setValue("general/plugin_path", _pluginPath);
297 set->setValue("general/history_length", _historyLen);
298 set->setValue("general/search_limit", _searchLimit);
303 void Backbone::saveDefaultPrefs(QSettings *set) {
306 set->setValue("general/plugin_path", _defaultPluginPath);
307 set->setValue("general/history_length", _defaultHistoryLen);
308 set->setValue("general/search_limit", _defaultSearchLimit);
313 void Backbone::loadDicts(QString fileName, bool _default) {
316 QFileInfo file(QDir::toNativeSeparators(fileName));
317 qDebug() << file.filePath();
318 QDir confDir(file.dir());
319 if(!confDir.exists()){
320 qDebug() << "Configuration file dosn't exists ("
321 << file.filePath() << ")";
325 QSettings set(file.filePath(), QSettings::IniFormat);
326 QStringList dicts = set.childGroups();
327 foreach(QString dict, dicts) {
328 if(!dict.contains("dictionary_"))
330 CommonDictInterface* plug = plugin
331 (set.value(dict + "/type", "").toString());
333 qDebug() << "Config file error: "
334 << set.value(dict + "/type", "").toString()
335 << " dosen't exists";
338 Settings* plugSet = new Settings();
339 set.beginGroup(dict);
340 QStringList items = set.childKeys();
341 foreach(QString item, items) {
342 plugSet->setValue(item, set.value(item, "").toString());
344 bool active = set.value("active",1).toBool();
347 plugSet->setValue("_default_", "true");
350 addInternalDictionary(plug->getNew(plugSet), active);
356 void Backbone::dictUpdated() {
359 QFileInfo file(QDir::toNativeSeparators(_configPath));
360 QDir confDir(file.dir());
361 if(!confDir.exists())
362 confDir.mkpath(file.dir().path());
363 QSettings set(file.filePath(), QSettings::IniFormat);
366 QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
367 QDir defConfDir(defFile.dir());
368 if(!defConfDir.exists())
369 defConfDir.mkpath(defFile.dir().path());
370 QSettings defSet(defFile.filePath(), QSettings::IniFormat);
373 saveDefaultPrefs(&defSet);
375 foreach(CommonDictInterface* dict, _dicts.keys()){
376 if(!dict || !dict->settings())
378 if(!dict->settings()->keys().contains("_default_"))
379 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
381 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
387 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
394 section.append(QString("dictionary_%1").arg(hash));
395 QList<QString> keys = plugSet->keys();
396 foreach(QString key, keys)
397 set->setValue(section + "/" + key, plugSet->value(key));
398 set->setValue(section + "/active", active);
403 QStringList Backbone::htmls() {
409 void Backbone::searchHtml(QList<Translation *> translations) {
410 _timerHtmlSearch.stop();
412 _innerHtmlResult.clear();
413 _timerHtmlSearch.start();
415 foreach(Translation* trans, translations)
417 _innerHtmlResult.append(
418 QtConcurrent::run(trans, &Translation::toHtml));
421 void Backbone::htmlTranslationReady() {
422 foreach(QFuture<QString> res, _innerHtmlResult) {
423 if(!res.isFinished())
425 _htmlResult.append(res.result());
426 _innerHtmlResult.removeOne(res);
428 if(!_innerHtmlResult.size()) {
429 _timerHtmlSearch.stop();