Changed path to plugins folder
[mdictionary] / trunk / src / base / backbone / backbone.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
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.
9
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.
14
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/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 // Created by Bartosz Szatkowski
23
24 #include "backbone.h"
25 #include <QDebug>
26
27 void Backbone::init() {
28    _searchLimit = 10;
29    _interval = 250; //msec
30    _pluginPath = "/usr/lib/mdictionary/";
31    loadPlugins();
32
33    if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
34        qDebug() << "Timer signal not connected";
35 }
36
37 Backbone::Backbone(QObject *parent)
38     : QObject(parent)
39 {
40     init();
41 }
42
43
44
45 Backbone::~Backbone()
46 {
47     QListIterator<CommonDictInterface*> it(_dicts.keys());
48
49     while(it.hasNext())
50         delete it.next();
51
52     it = QListIterator<CommonDictInterface*>(_plugins);
53     while(it.hasNext())
54         delete it.next();
55
56     QHashIterator<QString, Translation*> it2(_result);
57     while(it2.hasNext())
58         delete it2.next().value();
59
60 }
61
62
63
64
65 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
66     init();
67     _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
68     _plugins = QList<CommonDictInterface* > (b._plugins);
69     _result = QHash<QString, Translation* > (b._result);
70     _searchLimit = b.searchLimit();
71 }
72
73
74
75
76 int Backbone::searchLimit() const {
77     return _searchLimit;
78 }
79
80
81
82
83 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
84     return _dicts;
85 }
86
87
88
89
90 QList<CommonDictInterface* > Backbone::getPlugins() {
91     return _plugins;
92 }
93
94
95
96
97 QList<QString> Backbone::getHistory() {
98     //TODO code needed
99 }
100
101
102
103
104 QMultiHash<QString, Translation*> Backbone::result() {
105     return _result;
106 }
107
108
109
110
111 void Backbone::stopSearching() {
112     _timer.stop();
113     _innerResult.clear();
114     foreach(CommonDictInterface* dict, _dicts.keys())
115         dict->stop();
116 }
117
118
119
120
121 void Backbone::search(QString word) {
122     _timer.stop();
123     _result.clear();
124     _innerResult.clear();
125
126     _timer.start(_interval);
127     foreach(CommonDictInterface* dict, _dicts.keys())
128         if(_dicts[dict] == 1) {
129             QFuture<QList<Translation*> > tr =
130                     QtConcurrent::run(dict,
131                                       &CommonDictInterface::searchWordList,word,
132                                                              searchLimit());
133             _innerResult.append(tr);
134         }
135
136 }
137
138
139
140
141  void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
142      foreach(CommonDictInterface* dict, _dicts.keys())
143          if(activeDicts.contains(dict))
144              _dicts[dict] = 1;
145          else
146              _dicts[dict] = 0;
147  }
148
149
150
151
152  void Backbone::addDictionary(CommonDictInterface* dict) {
153      dict->setHash(_dicts.size()+1);
154      _dicts[dict] = 1;
155  }
156
157  void Backbone::removeDictionary(CommonDictInterface *dict) {
158      _dicts.remove(dict);
159
160  }
161
162
163
164  void Backbone::quit() {
165     stopSearching();
166     Q_EMIT closeOk();
167 }
168
169
170
171 int Backbone::activeSearches() const {
172     return _innerResult.size();
173 }
174
175
176
177 void Backbone::translation() {
178     foreach(QFuture<QList<Translation*> > trans, _innerResult) {
179         if(!trans.isFinished())
180             continue;
181         QList<Translation*> tList = trans.result();
182         foreach(Translation* t, tList)
183             _result.insert(t->key(), t);
184         _innerResult.removeOne(trans);
185     }
186     if(!_innerResult.size()) {
187         _timer.stop();
188         Q_EMIT ready();
189     }
190 }
191
192
193
194
195 void Backbone::loadPlugins() {
196     QDir plug(QDir::toNativeSeparators(_pluginPath));
197     if(!plug.exists()) {
198         qDebug() << plug.absolutePath() << " folder dosen't exists";
199         return;
200     }
201     QStringList nameFilter;
202     nameFilter << "*.so";
203     plug.setFilter(QDir::Files);
204     QStringList files = plug.entryList(nameFilter);
205     qDebug() << files;
206
207
208     foreach(QString file, files) {
209         QPluginLoader loader(plug.absoluteFilePath(file));
210         if(!loader.load()) {
211             qDebug()<< file << " " << loader.errorString();
212             continue;
213         }
214         QObject *pl = loader.instance();
215
216         CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
217         _plugins.append(plugin);
218         addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
219     }
220 }
221
222
223