Correct support for multiply clients
authordeztructor <denis.zalewsky@gmail.com>
Tue, 17 Nov 2009 16:07:33 +0000 (16:07 +0000)
committerdeztructor <denis.zalewsky@gmail.com>
Tue, 17 Nov 2009 16:07:33 +0000 (16:07 +0000)
Attribute is new replaced with action attribute. All activities on the
server side are reflected by server in the alert event including torrent
removal so client removes torrent only after notification about removal
is received from server

git-svn-id: file:///svnroot/qtrapids/trunk@29 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda

dbus/com.ixonos.qtrapids.xml
src/client/DownloadView.cpp
src/client/DownloadView.h
src/client/MainWindow.cpp
src/include/qtrapids/dbus.hpp
src/include/qtrapids/settings.hpp [new file with mode: 0644]
src/server/TorrentSession.cpp

index c4fdd90..5c25e77 100644 (file)
@@ -20,7 +20,7 @@
     </method>
 
     <signal name="alert">
     </method>
 
     <signal name="alert">
-      <arg type="(ssbuuuuuuut)" name="info" direction="out"/>
+      <arg type="(ssuuuuuuuut)" name="info" direction="out"/>
       <annotation name="com.trolltech.QtDBus.QtTypeName.In0" 
                   value="qtrapids::TorrentState"/>
       <arg type="a{ss}" name="other_info" direction="out"/>
       <annotation name="com.trolltech.QtDBus.QtTypeName.In0" 
                   value="qtrapids::TorrentState"/>
       <arg type="a{ss}" name="other_info" direction="out"/>
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);
     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();
 
     {
         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() ;
 
 
         qDebug() << "DownloadView::removeSelected() " <<  topLevelItemCount() ;
 
index abc7ea6..5ddffe4 100644 (file)
@@ -49,7 +49,7 @@ namespace qtrapids
         ~DownloadView();
 
                void updateItem(TorrentState const& info, ParamsMap_t other_info);
         ~DownloadView();
 
                void updateItem(TorrentState const& info, ParamsMap_t other_info);
-               QString removeSelected();
+               QString prepareRemoveSelected();
                
     private slots:
                void on_itemClicked(QTreeWidgetItem * item, int column);
                
     private slots:
                void on_itemClicked(QTreeWidgetItem * item, int column);
@@ -58,6 +58,7 @@ namespace qtrapids
                void addItem_(TorrentState const& info, ParamsMap_t other_info);
                void updateItem_(DownloadViewItem *item
                          , TorrentState const& info, ParamsMap_t other_info);
                void addItem_(TorrentState const& info, ParamsMap_t other_info);
                void updateItem_(DownloadViewItem *item
                          , TorrentState const& info, ParamsMap_t other_info);
+        void removeItem_(DownloadViewItem *item, TorrentState const& info);
 
                // Maps torrent to downloadview item.
                // Key: SHA1 info hash of torrent. Data: View item corresponding to torrent.
 
                // Maps torrent to downloadview item.
                // Key: SHA1 info hash of torrent. Data: View item corresponding to torrent.
index e200865..14b5372 100644 (file)
@@ -140,7 +140,7 @@ namespace qtrapids
 
     void MainWindow::on_removeAction_clicked()
     {
 
     void MainWindow::on_removeAction_clicked()
     {
-        QString hash = dlView_->removeSelected();
+        QString hash = dlView_->prepareRemoveSelected();
         try {
             server_.removeTorrent(hash);
         } catch (...) {
         try {
             server_.removeTorrent(hash);
         } catch (...) {
index 433aa16..103c950 100644 (file)
@@ -13,6 +13,14 @@ namespace qtrapids
 
     struct TorrentState
     {
 
     struct TorrentState
     {
+
+        enum torrent_action
+        {
+            action_add,
+            action_remove,
+            action_update
+        };
+        
         TorrentState() 
             : hash("")
             , name("")
         TorrentState() 
             : hash("")
             , name("")
@@ -24,11 +32,11 @@ namespace qtrapids
             , leeches(0)
             , ratio(0)
             , total_size(0)
             , leeches(0)
             , ratio(0)
             , total_size(0)
-        { }
+            { }
 
         QString hash;
         QString name;
 
         QString hash;
         QString name;
-        bool is_new;
+        torrent_action action;
         uint state;
         uint progress;
         uint down_rate;
         uint state;
         uint progress;
         uint down_rate;
@@ -49,7 +57,7 @@ namespace qtrapids
     {
         std::cout << "serialize" << std::endl;
         argument.beginStructure();
     {
         std::cout << "serialize" << std::endl;
         argument.beginStructure();
-        argument << state.hash << state.name << state.is_new << state.state << state.progress 
+        argument << state.hash << state.name << (uint)(state.action) << state.state << state.progress 
                  << state.down_rate << state.up_rate << state.seeds
                  << state.leeches << state.ratio << state.total_size;
         argument.endStructure();
                  << state.down_rate << state.up_rate << state.seeds
                  << state.leeches << state.ratio << state.total_size;
         argument.endStructure();
@@ -61,9 +69,11 @@ namespace qtrapids
     {
         std::cout << "deserialize" << std::endl;
         argument.beginStructure();
     {
         std::cout << "deserialize" << std::endl;
         argument.beginStructure();
-        argument >> state.hash >> state.name >> state.is_new >> state.state >> state.progress 
+        uint action;
+        argument >> state.hash >> state.name >> action >> state.state >> state.progress 
                  >> state.down_rate >> state.up_rate >> state.seeds
                  >> state.leeches >> state.ratio >> state.total_size;
                  >> state.down_rate >> state.up_rate >> state.seeds
                  >> state.leeches >> state.ratio >> state.total_size;
+        state.action = (TorrentState::torrent_action)action;
         argument.endStructure();
         return argument;
     }
         argument.endStructure();
         return argument;
     }
diff --git a/src/include/qtrapids/settings.hpp b/src/include/qtrapids/settings.hpp
new file mode 100644 (file)
index 0000000..d78625d
--- /dev/null
@@ -0,0 +1,20 @@
+#include <QSettings>
+#include <QString>
+#include <QVariant>
+
+namespace qtrapids
+{
+    static inline QVariant GetSettingsStoreDefault(QSettings &settings
+                                                   , QString const& name
+                                                   , QVariant const& default_value)
+    {
+        QVariant v(settings.value(name));
+        if (!v.isNull()) {
+            return v;
+        }
+
+        settings.setValue(name, default_value);
+        return default_value;
+    }
+
+}
index 6158452..531cd83 100644 (file)
@@ -58,7 +58,7 @@ namespace qtrapids
                 TorrentState state;
 
                 state.hash = Hash2QStr(handle.hash());
                 TorrentState state;
 
                 state.hash = Hash2QStr(handle.hash());
-                state.is_new = false;
+                state.action = TorrentState::action_update;
                 state.state = handle.state();
                 state.progress = handle.progress() * torrent_progress_max;
                 state.down_rate = handle.downloadRate();
                 state.state = handle.state();
                 state.progress = handle.progress() * torrent_progress_max;
                 state.down_rate = handle.downloadRate();
@@ -83,7 +83,7 @@ namespace qtrapids
             
             state.hash = hash;
             state.name = handle->name();
             
             state.hash = hash;
             state.name = handle->name();
-            state.is_new = true;
+            state.action = TorrentState::action_add;
             state.state = handle->state();
             state.progress = handle->progress() * torrent_progress_max;
             state.down_rate = handle->downloadRate();
             state.state = handle->state();
             state.progress = handle->progress() * torrent_progress_max;
             state.down_rate = handle->downloadRate();
@@ -123,7 +123,7 @@ namespace qtrapids
 
         state.hash = hash;
         state.name = handle->name();
 
         state.hash = hash;
         state.name = handle->name();
-        state.is_new = true;
+        state.action = TorrentState::action_add;
         state.state = handle->state();
         state.progress = handle->progress() * torrent_progress_max;
         state.down_rate = handle->downloadRate();
         state.state = handle->state();
         state.progress = handle->progress() * torrent_progress_max;
         state.down_rate = handle->downloadRate();
@@ -140,6 +140,7 @@ namespace qtrapids
     void TorrentSession::removeTorrent(const QString &hash)
     {
         torrents_t::iterator p = torrents_.find(hash);
     void TorrentSession::removeTorrent(const QString &hash)
     {
         torrents_t::iterator p = torrents_.find(hash);
+           
         if (p == torrents_.end()) {
             qDebug() << "Invalid request to remove torrent with hash " << hash;
             return;
         if (p == torrents_.end()) {
             qDebug() << "Invalid request to remove torrent with hash " << hash;
             return;
@@ -151,6 +152,12 @@ namespace qtrapids
                 "exception catched"
                 ;
         }
                 "exception catched"
                 ;
         }
+
+        TorrentState state;
+        state.hash = hash;
+        state.action = TorrentState::action_remove;
+        emit alert(state, ParamsMap_t());
+        torrents_.erase(p);
     }
 
 
     }