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