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
30 if(!_pluginPath.size())
31 _pluginPath = "/usr/lib/mdictionary/";
33 if(!_configPath.size())
34 _configPath = "~/mdictrc";
37 if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
38 qDebug() << "Timer signal not connected";
41 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
44 _pluginPath = pluginPath;
45 _configPath = configPath;
53 QListIterator<CommonDictInterface*> it(_dicts.keys());
58 it = QListIterator<CommonDictInterface*>(_plugins);
62 QHashIterator<QString, Translation*> it2(_result);
64 delete it2.next().value();
71 Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
73 _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
74 _plugins = QList<CommonDictInterface* > (b._plugins);
75 _result = QHash<QString, Translation* > (b._result);
76 _searchLimit = b.searchLimit();
82 int Backbone::searchLimit() const {
89 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
96 QList<CommonDictInterface* > Backbone::getPlugins() {
103 QList<QString> Backbone::getHistory() {
110 QMultiHash<QString, Translation*> Backbone::result() {
117 void Backbone::stopSearching() {
119 _innerResult.clear();
120 foreach(CommonDictInterface* dict, _dicts.keys())
127 void Backbone::search(QString word) {
130 _innerResult.clear();
132 _timer.start(_interval);
133 foreach(CommonDictInterface* dict, _dicts.keys())
134 if(_dicts[dict] == 1) {
135 QFuture<QList<Translation*> > tr =
136 QtConcurrent::run(dict,
137 &CommonDictInterface::searchWordList,word,
139 _innerResult.append(tr);
147 void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
148 foreach(CommonDictInterface* dict, _dicts.keys())
149 if(activeDicts.contains(dict))
158 void Backbone::addDictionary(CommonDictInterface* dict) {
159 dict->setHash(_dicts.size()+1);
163 void Backbone::removeDictionary(CommonDictInterface *dict) {
170 void Backbone::quit() {
177 int Backbone::activeSearches() const {
178 return _innerResult.size();
183 void Backbone::translation() {
184 foreach(QFuture<QList<Translation*> > trans, _innerResult) {
185 if(!trans.isFinished())
187 QList<Translation*> tList = trans.result();
188 foreach(Translation* t, tList) {
189 _result.insert(t->key().toLower(), t);
191 _innerResult.removeOne(trans);
193 if(!_innerResult.size()) {
199 QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
200 QDir plug(QDir::toNativeSeparators(dir));
202 qDebug() << plug.absolutePath() << " folder dosen't exists";
203 return QStringList();
205 plug.setFilter(QDir::Files);
206 QStringList list = plug.entryList(nameFilter);
208 for(int i = 0; i < list.size(); i++)
209 list[i] = plug.absoluteFilePath(list.at(i));
214 void Backbone::loadPlugins() {
215 QStringList nameFilter;
216 nameFilter << "*.so";
217 QStringList files = getFilesFromDir(_pluginPath, nameFilter);
219 foreach(QString file, files) {
220 QPluginLoader loader(file);
222 qDebug()<< file << " " << loader.errorString();
225 QObject *pl = loader.instance();
227 CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
228 _plugins.append(plugin);
229 addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
230 //Settings* set = new Settings();
231 //set->setValue("path", "dict2.xdxf");
232 //addDictionary(plugin->getNew(set));