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()));
46 _history = new History(5, this);
49 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
52 _pluginPath = pluginPath;
53 _configPath = configPath;
61 QListIterator<CommonDictInterface*> it(_dicts.keys());
66 it = QListIterator<CommonDictInterface*>(_plugins);
70 QHashIterator<QString, Translation*> it2(_result);
72 delete it2.next().value();
79 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
81 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
82 _plugins = QList<CommonDictInterface* > (b._plugins);
83 _result = QHash<QString, Translation* > (b._result);
84 _searchLimit = b.searchLimit();
90 int Backbone::searchLimit() const {
97 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
104 QList<CommonDictInterface* > Backbone::getPlugins() {
111 History* Backbone::history() {
118 QMultiHash<QString, Translation*> Backbone::result() {
125 void Backbone::stopSearching() {
127 _innerResult.clear();
128 foreach(CommonDictInterface* dict, _dicts.keys())
135 void Backbone::search(QString word) {
138 _innerResult.clear();
140 _timerSearch.start(_interval);
142 foreach(CommonDictInterface* dict, _dicts.keys())
143 if(_dicts[dict] == 1) {
144 QFuture<QList<Translation*> > tr =
145 QtConcurrent::run(dict,
146 &CommonDictInterface::searchWordList,word,
148 _innerResult.append(tr);
156 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
157 foreach(CommonDictInterface* dict, _dicts.keys())
158 if(activeDicts.contains(dict))
167 void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
168 addInternalDictionary(dict,active);
174 void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
175 dict->setHash(_dicts.size()+1);
176 _dicts[dict] = active;
177 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
180 void Backbone::removeDictionary(CommonDictInterface *dict) {
188 void Backbone::quit() {
195 int Backbone::activeSearches() const {
196 return _innerResult.size();
201 void Backbone::translationReady() {
202 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
203 if(!trans.isFinished())
205 QList<Translation*> tList = trans.result();
206 foreach(Translation* t, tList) {
207 _result.insert(t->key().toLower(), t);
209 _innerResult.removeOne(trans);
211 if(!_innerResult.size()) {
217 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
218 QDir plug(QDir::toNativeSeparators(dir));
220 qDebug() << plug.absolutePath() << " folder dosen't exists";
221 return QStringList();
223 plug.setFilter(QDir::Files);
224 QStringList list = plug.entryList(nameFilter);
226 for(int i = 0; i < list.size(); i++)
227 list[i] = plug.absoluteFilePath(list.at(i));
232 void Backbone::loadPlugins() {
233 QStringList nameFilter;
234 nameFilter << "*.so";
235 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
237 foreach(QString file, files) {
238 QPluginLoader loader(file);
240 qDebug()<< file << " " << loader.errorString();
243 QObject *pl = loader.instance();
245 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
246 _plugins.append(plugin);
247 // addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
248 //Settings* set = new Settings();
249 //set->setValue("path", "dict2.xdxf");
250 //addDictionary(plugin->getNew(set));
256 CommonDictInterface* Backbone::plugin(QString type) {
257 foreach(CommonDictInterface* plugin, _plugins)
258 if(plugin->type() == type)
265 void Backbone::loadPrefs() {
266 QDir confDir(_configPath);
267 if(!confDir.exists())
268 qDebug() << "Configuration file dosn't exists (" << _configPath << ")";
270 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
271 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
272 _historyLen = set.value("general/history_length", 10).toInt();
273 _searchLimit = set.value("general/search_limit", 15).toInt();
278 void Backbone::loadDicts() {
279 QDir confDir(_configPath);
280 if(!confDir.exists())
281 qDebug() << confDir.mkpath(_configPath);
283 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
284 QStringList dicts = set.childGroups();
285 foreach(QString dict, dicts) {
286 if(!dict.contains("dictionary_"))
288 CommonDictInterface* plug = plugin
289 (set.value(dict + "/type", "").toString());
291 qDebug() << "Config file error: "
292 << set.value(dict + "/type", "").toString()
293 << " dosen't exists";
296 Settings* plugSet = new Settings();
297 set.beginGroup(dict);
298 QStringList items = set.childKeys();
299 foreach(QString item, items)
300 plugSet->setValue(item, set.value(item, "").toString());
301 bool active = set.value("active",1).toBool();
303 addInternalDictionary(plug->getNew(plugSet), active);
309 void Backbone::dictUpdated() {
310 QDir confDir(_configPath);
311 if(!confDir.exists())
312 qDebug() << confDir.mkpath(_configPath);
313 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
314 foreach(CommonDictInterface* dict, _dicts.keys())
315 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
320 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
325 section.append(QString("dictionary_%1").arg(hash));
326 QList<QString> keys = plugSet->keys();
327 foreach(QString key, keys)
328 set->setValue(section + "/" + key, plugSet->value(key));
329 set->setValue(section + "/active", active);
334 QStringList Backbone::htmls() {
340 void Backbone::searchHtml(QList<Translation *> translations) {
341 _timerHtmlSearch.stop();
343 _innerHtmlResult.clear();
344 _timerHtmlSearch.start();
346 foreach(Translation* trans, translations)
348 _innerHtmlResult.append(
349 QtConcurrent::run(trans, &Translation::toHtml));
352 void Backbone::htmlTranslationReady() {
353 foreach(QFuture<QString> res, _innerHtmlResult) {
354 if(!res.isFinished())
356 _htmlResult.append(res.result());
357 _innerHtmlResult.removeOne(res);
359 if(!_innerHtmlResult.size()) {
360 _timerHtmlSearch.stop();