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