Changed path to plugins folder
[mdictionary] / trunk / src / base / backbone / backbone.cpp
index 7500884..dde87a2 100644 (file)
 // Created by Bartosz Szatkowski
 
 #include "backbone.h"
+#include <QDebug>
+
+void Backbone::init() {
+   _searchLimit = 10;
+   _interval = 250; //msec
+   _pluginPath = "/usr/lib/mdictionary/";
+   loadPlugins();
+
+   if(!connect(&_timer, SIGNAL(timeout()), this, SLOT(translation())))
+       qDebug() << "Timer signal not connected";
+}
 
 Backbone::Backbone(QObject *parent)
     : QObject(parent)
 {
-   searchLimitv = 10;
+    init();
 }
 
+
+
 Backbone::~Backbone()
 {
+    QListIterator<CommonDictInterface*> it(_dicts.keys());
+
+    while(it.hasNext())
+        delete it.next();
+
+    it = QListIterator<CommonDictInterface*>(_plugins);
+    while(it.hasNext())
+        delete it.next();
+
+    QHashIterator<QString, Translation*> it2(_result);
+    while(it2.hasNext())
+        delete it2.next().value();
 
 }
 
-Backbone::Backbone(const Backbone &b){
-    dicts = QHash<CommonDictInterface*, bool > (b.dicts);
-    plugins = QList<CommonDictInterface* > (b.plugins);
-    resultv = QHash<QString, Translation* > (b.resultv);
-    searchLimitv = b.searchLimit();
+
+
+
+Backbone::Backbone(const Backbone &b) :QObject(b.parent()) {
+    init();
+    _dicts = QHash<CommonDictInterface*, bool > (b._dicts);
+    _plugins = QList<CommonDictInterface* > (b._plugins);
+    _result = QHash<QString, Translation* > (b._result);
+    _searchLimit = b.searchLimit();
 }
 
+
+
+
 int Backbone::searchLimit() const {
-    return searchLimitv;
+    return _searchLimit;
 }
 
+
+
+
 QHash<CommonDictInterface*, bool > Backbone::getDictionaries() {
-    return dicts;
+    return _dicts;
 }
 
+
+
+
 QList<CommonDictInterface* > Backbone::getPlugins() {
-    return plugins;
+    return _plugins;
 }
 
+
+
+
 QList<QString> Backbone::getHistory() {
     //TODO code needed
 }
 
-QHash<QString, Translation*> Backbone::result() {
-    return resultv;
+
+
+
+QMultiHash<QString, Translation*> Backbone::result() {
+    return _result;
 }
 
+
+
+
 void Backbone::stopSearching() {
-    foreach(CommonDictInterface* dict, dicts.keys())
+    _timer.stop();
+    _innerResult.clear();
+    foreach(CommonDictInterface* dict, _dicts.keys())
         dict->stop();
 }
 
+
+
+
 void Backbone::search(QString word) {
-    //TODO add running searches in new threads
-    foreach(CommonDictInterface* dict, dicts.keys())
-        if(dicts[dict] == 1)
-            dict->search(word, searchLimit());
+    _timer.stop();
+    _result.clear();
+    _innerResult.clear();
+
+    _timer.start(_interval);
+    foreach(CommonDictInterface* dict, _dicts.keys())
+        if(_dicts[dict] == 1) {
+            QFuture<QList<Translation*> > tr =
+                    QtConcurrent::run(dict,
+                                      &CommonDictInterface::searchWordList,word,
+                                                             searchLimit());
+            _innerResult.append(tr);
+        }
+
 }
 
+
+
+
  void Backbone::selectedDictionaries(QList<CommonDictInterface* > activeDicts) {
-     foreach(CommonDictInterface* dict, dicts.keys())
+     foreach(CommonDictInterface* dict, _dicts.keys())
          if(activeDicts.contains(dict))
-             dicts[dict] = 1;
+             _dicts[dict] = 1;
          else
-             dicts[dict] = 0;
+             _dicts[dict] = 0;
  }
 
+
+
+
  void Backbone::addDictionary(CommonDictInterface* dict) {
-     dicts[dict] = 1;
+     dict->setHash(_dicts.size()+1);
+     _dicts[dict] = 1;
+ }
+
+ void Backbone::removeDictionary(CommonDictInterface *dict) {
+     _dicts.remove(dict);
+
  }
 
+
+
  void Backbone::quit() {
     stopSearching();
     Q_EMIT closeOk();
@@ -92,4 +168,56 @@ void Backbone::search(QString word) {
 
 
 
+int Backbone::activeSearches() const {
+    return _innerResult.size();
+}
+
+
+
+void Backbone::translation() {
+    foreach(QFuture<QList<Translation*> > trans, _innerResult) {
+        if(!trans.isFinished())
+            continue;
+        QList<Translation*> tList = trans.result();
+        foreach(Translation* t, tList)
+            _result.insert(t->key(), t);
+        _innerResult.removeOne(trans);
+    }
+    if(!_innerResult.size()) {
+        _timer.stop();
+        Q_EMIT ready();
+    }
+}
+
+
+
+
+void Backbone::loadPlugins() {
+    QDir plug(QDir::toNativeSeparators(_pluginPath));
+    if(!plug.exists()) {
+        qDebug() << plug.absolutePath() << " folder dosen't exists";
+        return;
+    }
+    QStringList nameFilter;
+    nameFilter << "*.so";
+    plug.setFilter(QDir::Files);
+    QStringList files = plug.entryList(nameFilter);
+    qDebug() << files;
+
+
+    foreach(QString file, files) {
+        QPluginLoader loader(plug.absoluteFilePath(file));
+        if(!loader.load()) {
+            qDebug()<< file << " " << loader.errorString();
+            continue;
+        }
+        QObject *pl = loader.instance();
+
+        CommonDictInterface *plugin = qobject_cast<CommonDictInterface*>(pl);
+        _plugins.append(plugin);
+        addDictionary(plugin->getNew(0)); //TODO change 0 to real settings
+    }
+}
+
+