Stuffed another memory leak
[mdictionary] / trunk / src / base / backbone / backbone.cpp
index b353e2c..360448d 100644 (file)
 #include "backbone.h"
 #include <QDebug>
 
+int Backbone::_searchLimit;
+
+// Sadly QtConcurent mapped dont let me use something like calling method of
+// some class with supplied argument
 QString mappedSearch;
 QList<Translation*> mapSearch(CommonDictInterface *dict) {
     if(dict)
-        return dict->searchWordList(mappedSearch, 15);
+        return dict->searchWordList(mappedSearch, Backbone::_searchLimit);
     return QList<Translation*>();
 }
 
@@ -81,6 +85,7 @@ void Backbone::init() {
            QThreadPool::globalInstance()->maxThreadCount()+1);
 
    _history = new History(5, this);
+   _dictNum = 0;
 }
 
 
@@ -215,13 +220,16 @@ void Backbone::addDictionary(CommonDictInterface *dict, bool active) {
 
 
  void Backbone::addInternalDictionary(CommonDictInterface* dict, bool active) {
-     dict->setHash(_dicts.size()+1);
+     dict->setHash(++_dictNum);
      _dicts[dict] = active;
      connect(dict, SIGNAL(settingsChanged()), this, SLOT(dictUpdated()));
+     connect(dict, SIGNAL(notify(Notify::NotifyType,QString)), this,
+             SIGNAL(notify(Notify::NotifyType,QString)));
  }
 
  void Backbone::removeDictionary(CommonDictInterface *dict) {
      _dicts.remove(dict);
+     delete dict;
      dictUpdated();
 
  }
@@ -263,6 +271,8 @@ QStringList Backbone::getFilesFromDir(QString dir, QStringList nameFilter) {
     QDir plug(QDir::toNativeSeparators(dir));
     if(!plug.exists()) {
         qDebug() << plug.absolutePath() << " folder dosen't exists";
+        Q_EMIT notify(Notify::Warning,
+                QString("%1 folder dosen't exists.").arg(plug.path()));
         return QStringList();
     }
     plug.setFilter(QDir::Files);
@@ -284,6 +294,9 @@ void Backbone::loadPlugins() {
     foreach(QString file, files) {
         QPluginLoader loader(file);
         if(!loader.load()) {
+            Q_EMIT notify(Notify::Error,
+                    QString("%1 plugin cannot be loaded: %2.")
+                    .arg(file).arg(loader.errorString()));
             qDebug()<< file << " " << loader.errorString();
             continue;
         }
@@ -313,6 +326,9 @@ void Backbone::loadPrefs(QString fileName) {
     if(!confDir.exists()){
         qDebug() << "Configuration file dosn't exists ("
                 << file.filePath() << ")";
+        Q_EMIT notify(Notify::Warning,
+                QString("%1 configurationfile dosen't exists.")
+                .arg(file.filePath()));
         return;
     }
     QSettings set(file.filePath(), QSettings::IniFormat);
@@ -355,6 +371,9 @@ void Backbone::loadDicts(QString fileName, bool _default) {
     if(!confDir.exists()){
         qDebug() << "Configuration file dosn't exists ("
                 << file.filePath() << ")";
+        Q_EMIT notify(Notify::Warning,
+                QString("%1 configurationfile dosen't exists.")
+                .arg(file.filePath()));
         return;
     }
 
@@ -369,6 +388,9 @@ void Backbone::loadDicts(QString fileName, bool _default) {
             qDebug() << "Config file error: "
                     << set.value(dict + "/type", "").toString()
                     << " dosen't exists";
+            Q_EMIT notify(Notify::Warning,
+                    QString("Configuration file error. %2 plugin dosen't exists.")
+                    .arg(set.value(dict + "/type", "").toString()));
             continue;
         }
         Settings* plugSet = new Settings();
@@ -448,8 +470,10 @@ void Backbone::searchHtml(QList<Translation *> translations) {
 
     QList<TranslationPtr> dummy;
     stopped = false;
-    foreach(Translation* tr, translations)
-        dummy.append(TranslationPtr(tr));
+    foreach(Translation* tr, translations) {
+        if(containsDict(tr->dict()) || !tr->dict())
+            dummy.append(TranslationPtr(tr));
+  }
 
    _innerHtmlResult = QtConcurrent::mapped(dummy,
                                             &TranslationPtr::toHtml);
@@ -519,3 +543,14 @@ Settings* Backbone::settings() {
         settings->setValue("search_dictionaries", "false");
     return settings;
 }
+
+
+bool Backbone::containsDict(uint hash) const {
+    QHashIterator<CommonDictInterface*, bool> it(_dicts);
+    if (!hash)
+        return false;
+    while(it.hasNext())
+        if(it.next().key()->hash() == hash)
+            return true;
+    return false;
+}