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()));
56 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
61 _pluginPath = pluginPath;
62 _configPath = configPath;
63 _defaultConfigPath = configPath;
70 QListIterator<CommonDictInterface*> it(_dicts.keys());
75 it = QListIterator<CommonDictInterface*>(_plugins);
79 QHashIterator<QString, Translation*> it2(_result);
81 delete it2.next().value();
88 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
90 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
91 _plugins = QList<CommonDictInterface* > (b._plugins);
92 _result = QHash<QString, Translation* > (b._result);
93 _searchLimit = b.searchLimit();
99 int Backbone::searchLimit() const {
106 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
113 QList<CommonDictInterface* > Backbone::getPlugins() {
120 QList<QString> Backbone::getHistory() {
127 QMultiHash<QString, Translation*> Backbone::result() {
134 void Backbone::stopSearching() {
136 _timerHtmlSearch.stop();
137 foreach(CommonDictInterface* dict, _dicts.keys())
144 void Backbone::search(QStringList words) {
147 _innerResult.clear();
149 _timerSearch.start(_interval);
150 foreach(QString word, words)
151 foreach(CommonDictInterface* dict, _dicts.keys())
152 if(_dicts[dict] == 1) {
153 QFuture<QList<Translation*> > tr =
154 QtConcurrent::run(dict,
155 &CommonDictInterface::searchWordList,word,
157 _innerResult.append(tr);
165 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
166 foreach(CommonDictInterface* dict, _dicts.keys())
167 if(activeDicts.contains(dict))
176 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
177 addInternalDictionary(dict,active);
183 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
184 dict->setHash(_dicts.size()+1);
185 _dicts[dict] = active;
186 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
189 void Backbone::removeDictionary(CommonDictInterface *dict) {
197 void Backbone::quit() {
204 int Backbone::activeSearches() const {
205 return _innerResult.size();
210 void Backbone::translationReady() {
211 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
212 if(!trans.isFinished())
214 QList<Translation*> tList = trans.result();
215 foreach(Translation* t, tList) {
216 _result.insert(t->key().toLower(), t);
218 _innerResult.removeOne(trans);
220 if(!_innerResult.size()) {
226 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
227 QDir plug(QDir::toNativeSeparators(dir));
229 qDebug() << plug.absolutePath() << " folder dosen't exists";
230 return QStringList();
232 plug.setFilter(QDir::Files);
233 QStringList list = plug.entryList(nameFilter);
235 for(int i = 0; i < list.size(); i++)
236 list[i] = plug.absoluteFilePath(list.at(i));
241 void Backbone::loadPlugins() {
244 QStringList nameFilter;
245 nameFilter << "*.so";
246 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
249 foreach(QString file, files) {
250 QPluginLoader loader(file);
252 qDebug()<< file << " " << loader.errorString();
255 QObject *pl = loader.instance();
257 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
258 _plugins.append(plugin);
264 CommonDictInterface* Backbone::plugin(QString type) {
265 foreach(CommonDictInterface* plugin, _plugins)
266 if(plugin->type() == type)
273 void Backbone::loadPrefs(QString fileName) {
276 QFileInfo file(QDir::toNativeSeparators(fileName));
277 QDir confDir(file.dir());
278 if(!confDir.exists()){
279 qDebug() << "Configuration file dosn't exists ("
280 << file.filePath() << ")";
283 QSettings set(file.filePath(), QSettings::IniFormat);
284 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
285 _historyLen = set.value("general/history_length", 10).toInt();
286 _searchLimit = set.value("general/search_limit", 15).toInt();
291 void Backbone::savePrefs(QSettings *set) {
294 set->setValue("general/plugin_path", _pluginPath);
295 set->setValue("general/history_length", _historyLen);
296 set->setValue("general/search_limit", _searchLimit);
301 void Backbone::saveDefaultPrefs(QSettings *set) {
304 set->setValue("general/plugin_path", _defaultPluginPath);
305 set->setValue("general/history_length", _defaultHistoryLen);
306 set->setValue("general/search_limit", _defaultSearchLimit);
311 void Backbone::loadDicts(QString fileName, bool _default) {
314 QFileInfo file(QDir::toNativeSeparators(fileName));
315 qDebug() << file.filePath();
316 QDir confDir(file.dir());
317 if(!confDir.exists()){
318 qDebug() << "Configuration file dosn't exists ("
319 << file.filePath() << ")";
323 QSettings set(file.filePath(), QSettings::IniFormat);
324 QStringList dicts = set.childGroups();
325 foreach(QString dict, dicts) {
326 if(!dict.contains("dictionary_"))
328 CommonDictInterface* plug = plugin
329 (set.value(dict + "/type", "").toString());
331 qDebug() << "Config file error: "
332 << set.value(dict + "/type", "").toString()
333 << " dosen't exists";
336 Settings* plugSet = new Settings();
337 set.beginGroup(dict);
338 QStringList items = set.childKeys();
339 foreach(QString item, items) {
340 plugSet->setValue(item, set.value(item, "").toString());
342 bool active = set.value("active",1).toBool();
345 plugSet->setValue("_default_", "true");
348 addInternalDictionary(plug->getNew(plugSet), active);
354 void Backbone::dictUpdated() {
357 QFileInfo file(QDir::toNativeSeparators(_configPath));
358 QDir confDir(file.dir());
359 if(!confDir.exists())
360 confDir.mkpath(file.dir().path());
361 QSettings set(file.filePath(), QSettings::IniFormat);
364 QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
365 QDir defConfDir(defFile.dir());
366 if(!defConfDir.exists())
367 defConfDir.mkpath(defFile.dir().path());
368 QSettings defSet(defFile.filePath(), QSettings::IniFormat);
371 saveDefaultPrefs(&defSet);
373 foreach(CommonDictInterface* dict, _dicts.keys()){
374 if(!dict || !dict->settings())
376 if(!dict->settings()->keys().contains("_default_"))
377 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
379 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
385 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
392 section.append(QString("dictionary_%1").arg(hash));
393 QList<QString> keys = plugSet->keys();
394 foreach(QString key, keys)
395 set->setValue(section + "/" + key, plugSet->value(key));
396 set->setValue(section + "/active", active);
401 QStringList Backbone::htmls() {
407 void Backbone::searchHtml(QList<Translation *> translations) {
408 _timerHtmlSearch.stop();
410 _innerHtmlResult.clear();
411 _timerHtmlSearch.start();
413 foreach(Translation* trans, translations)
415 _innerHtmlResult.append(
416 QtConcurrent::run(trans, &Translation::toHtml));
419 void Backbone::htmlTranslationReady() {
420 foreach(QFuture<QString> res, _innerHtmlResult) {
421 if(!res.isFinished())
423 _htmlResult.append(res.result());
424 _innerHtmlResult.removeOne(res);
426 if(!_innerHtmlResult.size()) {
427 _timerHtmlSearch.stop();