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