1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 // Created by Bartosz Szatkowski
27 void Backbone::init() {
29 _interval = 250; //msec
31 if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
32 qDebug() << "Timer signal not connected";
35 Backbone::Backbone(QObject *parent)
45 QListIterator<CommonDictInterface*> it(_dicts.keys());
50 it = QListIterator<CommonDictInterface*>(_plugins);
54 QHashIterator<QString, Translation*> it2(_result);
56 delete it2.next().value();
63 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
65 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
66 _plugins = QList<CommonDictInterface* > (b._plugins);
67 _result = QHash<QString, Translation* > (b._result);
68 _searchLimit = b.searchLimit();
74 int Backbone::searchLimit() const {
81 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
88 QList<CommonDictInterface* > Backbone::getPlugins() {
95 QList<QString> Backbone::getHistory() {
102 QMultiHash<QString, Translation*> Backbone::result() {
109 void Backbone::stopSearching() {
111 _innerResult.clear();
112 foreach(CommonDictInterface* dict, _dicts.keys())
119 void Backbone::search(QString word) {
120 //TODO add running searches in new threads
123 _innerResult.clear();
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,
132 _innerResult.append(tr);
140 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
141 foreach(CommonDictInterface* dict, _dicts.keys())
142 if(activeDicts.contains(dict))
151 void Backbone::addDictionary(CommonDictInterface* dict) {
152 dict->setHash(_dicts.size()+1);
158 void Backbone::quit() {
165 int Backbone::activeSearches() const {
166 return _innerResult.size();
171 void Backbone::translation() {
172 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
173 if(!trans.isFinished())
175 QList<Translation*> tList = trans.result();
176 foreach(Translation* t, tList)
177 _result.insert(t->key(), t);
178 _innerResult.removeOne(trans);
180 if(!_innerResult.size()) {
189 void Backbone::loadPlugins() {
190 QPluginLoader loader("xdxf.so");
192 qDebug()<<loader.errorString();
195 QObject *pl = loader.instance();
198 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
199 _plugins.append(plugin);
200 addDictionary(plugin->getNew(0)); //TODO change 0 to real settings