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