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 if(!_configPath.size())
31 _configPath = QDir::homePath() + "/.mdictionary";
36 if(!_pluginPath.size())
37 _pluginPath = "/usr/lib/mdictionary/";
42 connect(&_timerSearch, SIGNAL(timeout()), this, SLOT(translationReady()));
43 connect(&_timerHtmlSearch, SIGNAL(timeout()), this,
44 SLOT(htmlTranslationReady()));
47 _history = new History(this);
50 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
53 _pluginPath = pluginPath;
54 _configPath = configPath;
62 QListIterator<CommonDictInterface*> it(_dicts.keys());
67 it = QListIterator<CommonDictInterface*>(_plugins);
71 QHashIterator<QString, Translation*> it2(_result);
73 delete it2.next().value();
80 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
82 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
83 _plugins = QList<CommonDictInterface* > (b._plugins);
84 _result = QHash<QString, Translation* > (b._result);
85 _searchLimit = b.searchLimit();
91 int Backbone::searchLimit() const {
98 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
105 QList<CommonDictInterface* > Backbone::getPlugins() {
112 History* Backbone::history() {
119 QMultiHash<QString, Translation*> Backbone::result() {
126 void Backbone::stopSearching() {
128 _innerResult.clear();
129 foreach(CommonDictInterface* dict, _dicts.keys())
136 void Backbone::search(QString word) {
139 _innerResult.clear();
141 _timerSearch.start(_interval);
143 foreach(CommonDictInterface* dict, _dicts.keys())
144 if(_dicts[dict] == 1) {
145 QFuture<QList<Translation*> > tr =
146 QtConcurrent::run(dict,
147 &CommonDictInterface::searchWordList,word,
149 _innerResult.append(tr);
157 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
158 foreach(CommonDictInterface* dict, _dicts.keys())
159 if(activeDicts.contains(dict))
168 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
169 addInternalDictionary(dict,active);
175 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
176 dict->setHash(_dicts.size()+1);
177 _dicts[dict] = active;
178 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
181 void Backbone::removeDictionary(CommonDictInterface *dict) {
189 void Backbone::quit() {
196 int Backbone::activeSearches() const {
197 return _innerResult.size();
202 void Backbone::translationReady() {
203 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
204 if(!trans.isFinished())
206 QList<Translation*> tList = trans.result();
207 foreach(Translation* t, tList) {
208 _result.insert(t->key().toLower(), t);
210 _innerResult.removeOne(trans);
212 if(!_innerResult.size()) {
218 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
219 QDir plug(QDir::toNativeSeparators(dir));
221 qDebug() << plug.absolutePath() << " folder dosen't exists";
222 return QStringList();
224 plug.setFilter(QDir::Files);
225 QStringList list = plug.entryList(nameFilter);
227 for(int i = 0; i < list.size(); i++)
228 list[i] = plug.absoluteFilePath(list.at(i));
233 void Backbone::loadPlugins() {
234 QStringList nameFilter;
235 nameFilter << "*.so";
236 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
238 foreach(QString file, files) {
239 QPluginLoader loader(file);
241 qDebug()<< file << " " << loader.errorString();
244 QObject *pl = loader.instance();
246 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
247 _plugins.append(plugin);
248 // addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
249 //Settings* set = new Settings();
250 //set->setValue("path", "dict2.xdxf");
251 //addDictionary(plugin->getNew(set));
257 CommonDictInterface* Backbone::plugin(QString type) {
258 foreach(CommonDictInterface* plugin, _plugins)
259 if(plugin->type() == type)
266 void Backbone::loadPrefs() {
267 QDir confDir(_configPath);
268 if(!confDir.exists())
269 qDebug() << "Configuration file dosn't exists (" << _configPath << ")";
271 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
272 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
273 _historyLen = set.value("general/history_length", 10).toInt();
274 _searchLimit = set.value("general/search_limit", 15).toInt();
279 void Backbone::loadDicts() {
280 QDir confDir(_configPath);
281 if(!confDir.exists())
282 qDebug() << confDir.mkpath(_configPath);
284 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
285 QStringList dicts = set.childGroups();
286 foreach(QString dict, dicts) {
287 if(!dict.contains("dictionary_"))
289 CommonDictInterface* plug = plugin
290 (set.value(dict + "/type", "").toString());
292 qDebug() << "Config file error: "
293 << set.value(dict + "/type", "").toString()
294 << " dosen't exists";
297 Settings* plugSet = new Settings();
298 set.beginGroup(dict);
299 QStringList items = set.childKeys();
300 foreach(QString item, items)
301 plugSet->setValue(item, set.value(item, "").toString());
302 bool active = set.value("active",1).toBool();
304 addInternalDictionary(plug->getNew(plugSet), active);
310 void Backbone::dictUpdated() {
311 QDir confDir(_configPath);
312 if(!confDir.exists())
313 qDebug() << confDir.mkpath(_configPath);
314 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
315 foreach(CommonDictInterface* dict, _dicts.keys())
316 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
321 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
326 section.append(QString("dictionary_%1").arg(hash));
327 QList<QString> keys = plugSet->keys();
328 foreach(QString key, keys)
329 set->setValue(section + "/" + key, plugSet->value(key));
330 set->setValue(section + "/active", active);
335 QStringList Backbone::htmls() {
341 void Backbone::searchHtml(QList<Translation *> translations) {
342 _timerHtmlSearch.stop();
344 _innerHtmlResult.clear();
345 _timerHtmlSearch.start();
347 foreach(Translation* trans, translations)
349 _innerHtmlResult.append(
350 QtConcurrent::run(trans, &Translation::toHtml));
353 void Backbone::htmlTranslationReady() {
354 foreach(QFuture<QString> res, _innerHtmlResult) {
355 if(!res.isFinished())
357 _htmlResult.append(res.result());
358 _innerHtmlResult.removeOne(res);
360 if(!_innerHtmlResult.size()) {
361 _timerHtmlSearch.stop();