Proper dictionary result handling with test
[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 Backbone::~Backbone()
33 {
34
35 }
36
37 Backbone::Backbone(const Backbone &b){
38     dicts = QHash<CommonDictInterface*, bool > (b.dicts);
39     plugins = QList<CommonDictInterface* > (b.plugins);
40     _result = QHash<QString, Translation* > (b._result);
41     searchLimitv = b.searchLimit();
42 }
43
44 int Backbone::searchLimit() const {
45     return searchLimitv;
46 }
47
48 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
49     return dicts;
50 }
51
52 QList<CommonDictInterface* > Backbone::getPlugins() {
53     return plugins;
54 }
55
56 QList<QString> Backbone::getHistory() {
57     //TODO code needed
58 }
59
60 QMultiHash<QString, Translation*> Backbone::result() {
61     return _result;
62 }
63
64 void Backbone::stopSearching() {
65     foreach(CommonDictInterface* dict, dicts.keys())
66         dict->stop();
67 }
68
69 void Backbone::search(QString word) {
70     //TODO add running searches in new threads
71     _result.clear();
72     activeSearchNum = 0;
73     foreach(CommonDictInterface* dict, dicts.keys())
74         if(dicts[dict] == 1) {
75             activeSearchNum ++;
76         }
77
78     foreach(CommonDictInterface* dict, dicts.keys())
79         if(dicts[dict] == 1) {
80             dict->search(word, searchLimit());
81         }
82 }
83
84  void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
85      foreach(CommonDictInterface* dict, dicts.keys())
86          if(activeDicts.contains(dict))
87              dicts[dict] = 1;
88          else
89              dicts[dict] = 0;
90  }
91
92  void Backbone::addDictionary(CommonDictInterface* dict) {
93      dicts[dict] = 1;
94      connect(dict, SIGNAL(finalTranslation(QList<Translation*>)),
95              this, SLOT(translation(QList<Translation*>)),
96              Qt::UniqueConnection);
97  }
98
99  void Backbone::quit() {
100     stopSearching();
101     Q_EMIT closeOk();
102 }
103
104
105
106 int Backbone::activeSearches() const {
107     return activeSearchNum;
108 }
109
110
111
112 void Backbone::translation(QList<Translation *> trans) {
113     activeSearchNum--;
114     foreach(Translation* t, trans)
115         _result.insert(t->key(), t);
116
117     if(activeSearchNum < 1)
118         Q_EMIT ready();
119 }
120
121
122