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() {
29 _interval = 250; //msec
30 if(!_pluginPath.size())
31 _pluginPath = "/usr/lib/mdictionary/";
33 if(!_configPath.size())
34 _configPath = QDir::homePath() + "/.mdictionary";
38 if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
39 qDebug() << "Timer signal not connected";
42 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
45 _pluginPath = pluginPath;
46 _configPath = configPath;
54 QListIterator<CommonDictInterface*> it(_dicts.keys());
59 it = QListIterator<CommonDictInterface*>(_plugins);
63 QHashIterator<QString, Translation*> it2(_result);
65 delete it2.next().value();
72 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
74 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
75 _plugins = QList<CommonDictInterface* > (b._plugins);
76 _result = QHash<QString, Translation* > (b._result);
77 _searchLimit = b.searchLimit();
83 int Backbone::searchLimit() const {
90 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
97 QList<CommonDictInterface* > Backbone::getPlugins() {
104 QList<QString> Backbone::getHistory() {
111 QMultiHash<QString, Translation*> Backbone::result() {
118 void Backbone::stopSearching() {
120 _innerResult.clear();
121 foreach(CommonDictInterface* dict, _dicts.keys())
128 void Backbone::search(QString word) {
131 _innerResult.clear();
133 _timer.start(_interval);
134 foreach(CommonDictInterface* dict, _dicts.keys())
135 if(_dicts[dict] == 1) {
136 QFuture<QList<Translation*> > tr =
137 QtConcurrent::run(dict,
138 &CommonDictInterface::searchWordList,word,
140 _innerResult.append(tr);
148 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
149 foreach(CommonDictInterface* dict, _dicts.keys())
150 if(activeDicts.contains(dict))
159 void Backbone::addDictionary(CommonDictInterface* dict) {
160 dict->setHash(_dicts.size()+1);
164 void Backbone::removeDictionary(CommonDictInterface *dict) {
171 void Backbone::quit() {
178 int Backbone::activeSearches() const {
179 return _innerResult.size();
184 void Backbone::translation() {
185 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
186 if(!trans.isFinished())
188 QList<Translation*> tList = trans.result();
189 foreach(Translation* t, tList) {
190 _result.insert(t->key().toLower(), t);
192 _innerResult.removeOne(trans);
194 if(!_innerResult.size()) {
200 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
201 QDir plug(QDir::toNativeSeparators(dir));
203 qDebug() << plug.absolutePath() << " folder dosen't exists";
204 return QStringList();
206 plug.setFilter(QDir::Files);
207 QStringList list = plug.entryList(nameFilter);
209 for(int i = 0; i < list.size(); i++)
210 list[i] = plug.absoluteFilePath(list.at(i));
215 void Backbone::loadPlugins() {
216 QStringList nameFilter;
217 nameFilter << "*.so";
218 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
220 foreach(QString file, files) {
221 QPluginLoader loader(file);
223 qDebug()<< file << " " << loader.errorString();
226 QObject *pl = loader.instance();
228 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
229 _plugins.append(plugin);
230 // addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
231 //Settings* set = new Settings();
232 //set->setValue("path", "dict2.xdxf");
233 //addDictionary(plugin->getNew(set));
237 CommonDictInterface* Backbone::plugin(QString type) {
238 foreach(CommonDictInterface* plugin, _plugins)
239 if(plugin->type() == type)
244 void Backbone::loadPreferences() {
245 QDir confDir(_configPath);
246 if(!confDir.exists())
247 qDebug() << confDir.mkpath(_configPath);
248 QSettings set(_configPath + "/mdictionary.config", QSettings::IniFormat);
249 _pluginPath = set.value("general/path", _pluginPath).toString();
250 QStringList dicts = set.childGroups();
251 foreach(QString dict, dicts) {
252 if(!dict.contains("dictionary_"))
254 CommonDictInterface* plug = plugin
255 (set.value(dict + "/type", "").toString());
257 qDebug() << "Config file error: "
258 << set.value(dict + "/type", "").toString()
259 << " dosen't exists";
262 Settings* plugSet = new Settings();
263 set.beginGroup(dict);
264 QStringList items = set.childKeys();
265 foreach(QString item, items)
266 plugSet->setValue(item, set.value(item, "").toString());
268 addDictionary(plug->getNew(plugSet));
272 //saving dictionaries state
273 //saving selection state