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 if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
43 qDebug() << "Timer signal not connected";
46 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
49 _pluginPath = pluginPath;
50 _configPath = configPath;
58 QListIterator<CommonDictInterface*> it(_dicts.keys());
63 it = QListIterator<CommonDictInterface*>(_plugins);
67 QHashIterator<QString, Translation*> it2(_result);
69 delete it2.next().value();
76 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
78 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
79 _plugins = QList<CommonDictInterface* > (b._plugins);
80 _result = QHash<QString, Translation* > (b._result);
81 _searchLimit = b.searchLimit();
87 int Backbone::searchLimit() const {
94 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
101 QList<CommonDictInterface* > Backbone::getPlugins() {
108 QList<QString> Backbone::getHistory() {
115 QMultiHash<QString, Translation*> Backbone::result() {
122 void Backbone::stopSearching() {
124 _innerResult.clear();
125 foreach(CommonDictInterface* dict, _dicts.keys())
132 void Backbone::search(QStringList words) {
135 _innerResult.clear();
137 _timer.start(_interval);
138 foreach(QString word, words)
139 foreach(CommonDictInterface* dict, _dicts.keys())
140 if(_dicts[dict] == 1) {
141 QFuture<QList<Translation*> > tr =
142 QtConcurrent::run(dict,
143 &CommonDictInterface::searchWordList,word,
145 _innerResult.append(tr);
153 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
154 foreach(CommonDictInterface* dict, _dicts.keys())
155 if(activeDicts.contains(dict))
164 void Backbone::addDictionary(CommonDictInterface* dict, bool active) {
165 dict->setHash(_dicts.size()+1);
166 _dicts[dict] = active;
167 connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
170 void Backbone::removeDictionary(CommonDictInterface *dict) {
177 void Backbone::quit() {
184 int Backbone::activeSearches() const {
185 return _innerResult.size();
190 void Backbone::translation() {
191 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
192 if(!trans.isFinished())
194 QList<Translation*> tList = trans.result();
195 foreach(Translation* t, tList) {
196 _result.insert(t->key().toLower(), t);
198 _innerResult.removeOne(trans);
200 if(!_innerResult.size()) {
206 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
207 QDir plug(QDir::toNativeSeparators(dir));
209 qDebug() << plug.absolutePath() << " folder dosen't exists";
210 return QStringList();
212 plug.setFilter(QDir::Files);
213 QStringList list = plug.entryList(nameFilter);
215 for(int i = 0; i < list.size(); i++)
216 list[i] = plug.absoluteFilePath(list.at(i));
221 void Backbone::loadPlugins() {
222 QStringList nameFilter;
223 nameFilter << "*.so";
224 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
226 foreach(QString file, files) {
227 QPluginLoader loader(file);
229 qDebug()<< file << " " << loader.errorString();
232 QObject *pl = loader.instance();
234 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
235 _plugins.append(plugin);
236 // addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
237 //Settings* set = new Settings();
238 //set->setValue("path", "dict2.xdxf");
239 //addDictionary(plugin->getNew(set));
245 CommonDictInterface* Backbone::plugin(QString type) {
246 foreach(CommonDictInterface* plugin, _plugins)
247 if(plugin->type() == type)
254 void Backbone::loadPrefs() {
255 QDir confDir(_configPath);
256 if(!confDir.exists())
257 qDebug() << "Configuration file dosn't exists (" << _configPath << ")";
258 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
259 _pluginPath = set.value("general/plugin_path", _pluginPath).toString();
260 _historyLen = set.value("general/history_length", 10).toInt();
261 _searchLimit = set.value("general/search_limit", 15).toInt();
266 void Backbone::loadDicts() {
267 QDir confDir(_configPath);
268 if(!confDir.exists())
269 qDebug() << confDir.mkpath(_configPath);
270 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
271 _pluginPath = set.value("general/path", _pluginPath).toString();
272 QStringList dicts = set.childGroups();
273 foreach(QString dict, dicts) {
274 if(!dict.contains("dictionary_"))
276 CommonDictInterface* plug = plugin
277 (set.value(dict + "/type", "").toString());
279 qDebug() << "Config file error: "
280 << set.value(dict + "/type", "").toString()
281 << " dosen't exists";
284 Settings* plugSet = new Settings();
285 set.beginGroup(dict);
286 QStringList items = set.childKeys();
287 foreach(QString item, items)
288 plugSet->setValue(item, set.value(item, "").toString());
289 addDictionary(plug->getNew(plugSet), set.value("active",1).toBool());
294 void Backbone::dictUpdated() {
295 qDebug() << "UPDATE";
296 QDir confDir(_configPath);
297 if(!confDir.exists())
298 qDebug() << confDir.mkpath(_configPath);
299 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
300 foreach(CommonDictInterface* dict, _dicts.keys())
301 saveState(&set, dict->settings(), _dicts[dict], dict->hash());
304 void Backbone::saveState(QSettings* set, Settings* plugSet, bool active
307 section.append("dictionary_%1").arg(hash);
308 QList<QString> keys = plugSet->keys();
309 foreach(QString key, keys)
310 set->setValue(section + "/" + key, plugSet->value(key));
311 set->setValue(section + "/active", active);
315 //saving dictionaries state
316 //saving selection state