Correct support for multiply clients
[qtrapids] / src / client / DownloadView.cpp
index 5aecc86..09b1e2e 100644 (file)
@@ -48,17 +48,46 @@ namespace qtrapids
     void DownloadView::updateItem(TorrentState const& info, ParamsMap_t other_info)
     {
         DownloadItems_t::iterator p = items_.find(info.hash);
-        if (p != items_.end()) {
-            if (info.is_new) {
-                qWarning() << "item with similar info hash marked as new";
-            }
-            updateItem_(p.value(), info, other_info);
-        } else {
-            if (!info.is_new) {
-                qDebug() << "torrent info arrived but there is no torrent for it";
-                return;
-            }
-            addItem_(info, other_info);
+        switch (info.action) {
+            case TorrentState::action_add :
+                if (p == items_.end()) {
+                    addItem_(info, other_info);
+                } else {
+                    qWarning() << "item with similar info hash marked as added";
+                    updateItem_(p.value(), info, other_info);
+                }
+                break;
+            case TorrentState::action_update :
+                if (p != items_.end()) {
+                    updateItem_(p.value(), info, other_info);
+                } else {
+                    qWarning() << "item does not exist in list but information update arrived";
+                }
+                break;
+            case TorrentState::action_remove :
+                if (p != items_.end()) {
+                    removeItem_(p.value(), info);
+                } else {
+                    qWarning() << "item removal request arrived but there is no such item";
+                }
+                break;
+            default:
+                qWarning() << "unknown action requested: " << info.action;
+                break;
+        }
+    }
+
+    void DownloadView::removeItem_(DownloadViewItem *item, TorrentState const& info)
+    {
+        QString hash = item->getHash();
+
+        int removed = items_.remove(hash);
+        if (!removed)
+            qDebug() << "Inconsistent download view state on item removal";
+
+        int index = indexOfTopLevelItem(item);
+        if (index >= 0) {
+            takeTopLevelItem(index);
         }
     }
 
@@ -104,21 +133,14 @@ namespace qtrapids
     }
 
 
-    QString DownloadView::removeSelected()
+    QString DownloadView::prepareRemoveSelected()
     {
         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
 
         DownloadViewItem *item = dynamic_cast<DownloadViewItem*> (currentItem());
         QString hash = item->getHash();
 
-        int removed = items_.remove(hash);
-        if (!removed)
-            qDebug() << "Inconsistent download view state on item removal";
-
-        int index = indexOfTopLevelItem(currentItem());
-        if (index >= 0) {
-            takeTopLevelItem(index);
-        }
+        item->setDisabled(true);
 
         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;