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