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
30 _configPath = QDir::homePath() + "/.mdictionary/mdictionary.config";
31 _defaultConfigPath = QDir::homePath() + "/.mdictionary/mdictionary.defaults";
32 _pluginPath = "/usr/lib/mdictionary";
36 loadPrefs(_defaultConfigPath);
37 _defaultPluginPath = _pluginPath;
38 _defaultHistoryLen = _historyLen;
39 _defaultSearchLimit = _searchLimit;
40 loadPrefs(_configPath);
44 loadDicts(_defaultConfigPath, true);
45 loadDicts(_configPath);
47 connect(&_timerSearch, SIGNAL(timeout()), this, SLOT(translationReady()));
48 connect(&_timerHtmlSearch, SIGNAL(timeout()), this,
49 SLOT(htmlTranslationReady()));
52 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
55 _pluginPath = pluginPath;
56 _configPath = configPath;
64 QListIterator<CommonDictInterface*> it(_dicts.keys());
69 it = QListIterator<CommonDictInterface*>(_plugins);
73 QHashIterator<QString, Translation*> it2(_result);
75 delete it2.next().value();
82 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
84 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
85 _plugins = QList<CommonDictInterface* > (b._plugins);
86 _result = QHash<QString, Translation* > (b._result);
87 _searchLimit = b.searchLimit();
93 int Backbone::searchLimit() const {
100 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
107 QList<CommonDictInterface* > Backbone::getPlugins() {
114 QList<QString> Backbone::getHistory() {
121 QMultiHash<QString, Translation*> Backbone::result() {
128 void Backbone::stopSearching() {
130 _timerHtmlSearch.stop();
131 foreach(CommonDictInterface* dict, _dicts.keys())
138 void Backbone::search(QStringList words) {
141 _innerResult.clear();
143 _timerSearch.start(_interval);
144 foreach(QString word, words)
145 foreach(CommonDictInterface* dict, _dicts.keys())
146 if(_dicts[dict] == 1) {
147 QFuture<QList<Translation*> > tr =
148 QtConcurrent::run(dict,
149 &CommonDictInterface::searchWordList,word,
151 _innerResult.append(tr);
159 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
160 foreach(CommonDictInterface* dict, _dicts.keys())
161 if(activeDicts.contains(dict))
170 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
171 addInternalDictionary(dict,active);
177 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
178 dict->setHash(_dicts.size()+1);
179 _dicts[dict] = active;
180 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
183 void Backbone::removeDictionary(CommonDictInterface *dict) {
191 void Backbone::quit() {
198 int Backbone::activeSearches() const {
199 return _innerResult.size();
204 void Backbone::translationReady() {
205 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
206 if(!trans.isFinished())
208 QList<Translation*> tList = trans.result();
209 foreach(Translation* t, tList) {
210 _result.insert(t->key().toLower(), t);
212 _innerResult.removeOne(trans);
214 if(!_innerResult.size()) {
220 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
221 QDir plug(QDir::toNativeSeparators(dir));
223 qDebug() << plug.absolutePath() << " folder dosen't exists";
224 return QStringList();
226 plug.setFilter(QDir::Files);
227 QStringList list = plug.entryList(nameFilter);
229 for(int i = 0; i < list.size(); i++)
230 list[i] = plug.absoluteFilePath(list.at(i));
235 void Backbone::loadPlugins() {
236 QStringList nameFilter;
237 nameFilter << "*.so";
238 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
241 foreach(QString file, files) {
242 QPluginLoader loader(file);
244 qDebug()<< file << " " << loader.errorString();
247 QObject *pl = loader.instance();
249 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
250 _plugins.append(plugin);
256 CommonDictInterface* Backbone::plugin(QString type) {
257 foreach(CommonDictInterface* plugin, _plugins)
258 if(plugin->type() == type)
265 void Backbone::loadPrefs(QString fileName) {
266 QFileInfo file(QDir::toNativeSeparators(fileName));
267 QDir confDir(file.dir());
268 if(!confDir.exists()){
269 qDebug() << "Configuration file dosn't exists ("
270 << file.filePath() << ")";
273 QSettings set(file.filePath(), QSettings::IniFormat);
274 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
275 _historyLen = set.value("general/history_length", 10).toInt();
276 _searchLimit = set.value("general/search_limit", 15).toInt();
281 void Backbone::savePrefs(QSettings *set) {
282 set->setValue("general/plugin_path", _pluginPath);
283 set->setValue("general/history_length", _historyLen);
284 set->setValue("general/search_limit", _searchLimit);
289 void Backbone::saveDefaultPrefs(QSettings *set) {
290 set->setValue("general/plugin_path", _defaultPluginPath);
291 set->setValue("general/history_length", _defaultHistoryLen);
292 set->setValue("general/search_limit", _defaultSearchLimit);
297 void Backbone::loadDicts(QString fileName, bool _default) {
298 QFileInfo file(QDir::toNativeSeparators(fileName));
299 qDebug() << file.filePath();
300 QDir confDir(file.dir());
301 if(!confDir.exists()){
302 qDebug() << "Configuration file dosn't exists ("
303 << file.filePath() << ")";
307 QSettings set(file.filePath(), QSettings::IniFormat);
308 QStringList dicts = set.childGroups();
309 foreach(QString dict, dicts) {
310 if(!dict.contains("dictionary_"))
312 CommonDictInterface* plug = plugin
313 (set.value(dict + "/type", "").toString());
315 qDebug() << "Config file error: "
316 << set.value(dict + "/type", "").toString()
317 << " dosen't exists";
320 Settings* plugSet = new Settings();
321 set.beginGroup(dict);
322 QStringList items = set.childKeys();
323 foreach(QString item, items) {
324 plugSet->setValue(item, set.value(item, "").toString());
326 bool active = set.value("active",1).toBool();
329 plugSet->setValue("_default_", "true");
332 addInternalDictionary(plug->getNew(plugSet), active);
338 void Backbone::dictUpdated() {
339 QFileInfo file(QDir::toNativeSeparators(_configPath));
340 QDir confDir(file.dir());
341 if(!confDir.exists())
342 confDir.mkpath(file.dir().path());
343 QSettings set(file.filePath(), QSettings::IniFormat);
346 QFileInfo defFile(QDir::toNativeSeparators(_defaultConfigPath));
347 QDir defConfDir(defFile.dir());
348 if(!defConfDir.exists())
349 defConfDir.mkpath(defFile.dir().path());
350 QSettings defSet(defFile.filePath(), QSettings::IniFormat);
353 saveDefaultPrefs(&defSet);
355 foreach(CommonDictInterface* dict, _dicts.keys())
356 if(!dict->settings()->keys().contains("_default_"))
357 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
359 saveState(&defSet, dict->settings(), _dicts[dict], dict->hash());
364 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
369 section.append(QString("dictionary_%1").arg(hash));
370 QList<QString> keys = plugSet->keys();
371 foreach(QString key, keys)
372 set->setValue(section + "/" + key, plugSet->value(key));
373 set->setValue(section + "/active", active);
378 QStringList Backbone::htmls() {
384 void Backbone::searchHtml(QList<Translation *> translations) {
385 _timerHtmlSearch.stop();
387 _innerHtmlResult.clear();
388 _timerHtmlSearch.start();
390 foreach(Translation* trans, translations)
392 _innerHtmlResult.append(
393 QtConcurrent::run(trans, &Translation::toHtml));
396 void Backbone::htmlTranslationReady() {
397 foreach(QFuture<QString> res, _innerHtmlResult) {
398 if(!res.isFinished())
400 _htmlResult.append(res.result());
401 _innerHtmlResult.removeOne(res);
403 if(!_innerHtmlResult.size()) {
404 _timerHtmlSearch.stop();