From: deztructor Date: Tue, 17 Nov 2009 16:07:33 +0000 (+0000) Subject: Correct support for multiply clients X-Git-Url: http://git.maemo.org/git/?p=qtrapids;a=commitdiff_plain;h=fdb78d7880be7bc82544e66efd5acb3da74a617a Correct support for multiply clients 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 --- diff --git a/dbus/com.ixonos.qtrapids.xml b/dbus/com.ixonos.qtrapids.xml index c4fdd90..5c25e77 100644 --- a/dbus/com.ixonos.qtrapids.xml +++ b/dbus/com.ixonos.qtrapids.xml @@ -20,7 +20,7 @@ - + diff --git a/src/client/DownloadView.cpp b/src/client/DownloadView.cpp index 5aecc86..09b1e2e 100644 --- a/src/client/DownloadView.cpp +++ b/src/client/DownloadView.cpp @@ -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 (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() ; diff --git a/src/client/DownloadView.h b/src/client/DownloadView.h index abc7ea6..5ddffe4 100644 --- a/src/client/DownloadView.h +++ b/src/client/DownloadView.h @@ -49,7 +49,7 @@ namespace qtrapids ~DownloadView(); void updateItem(TorrentState const& info, ParamsMap_t other_info); - QString removeSelected(); + QString prepareRemoveSelected(); 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 removeItem_(DownloadViewItem *item, TorrentState const& info); // Maps torrent to downloadview item. // Key: SHA1 info hash of torrent. Data: View item corresponding to torrent. diff --git a/src/client/MainWindow.cpp b/src/client/MainWindow.cpp index e200865..14b5372 100644 --- a/src/client/MainWindow.cpp +++ b/src/client/MainWindow.cpp @@ -140,7 +140,7 @@ namespace qtrapids void MainWindow::on_removeAction_clicked() { - QString hash = dlView_->removeSelected(); + QString hash = dlView_->prepareRemoveSelected(); try { server_.removeTorrent(hash); } catch (...) { diff --git a/src/include/qtrapids/dbus.hpp b/src/include/qtrapids/dbus.hpp index 433aa16..103c950 100644 --- a/src/include/qtrapids/dbus.hpp +++ b/src/include/qtrapids/dbus.hpp @@ -13,6 +13,14 @@ namespace qtrapids struct TorrentState { + + enum torrent_action + { + action_add, + action_remove, + action_update + }; + TorrentState() : hash("") , name("") @@ -24,11 +32,11 @@ namespace qtrapids , leeches(0) , ratio(0) , total_size(0) - { } + { } QString hash; QString name; - bool is_new; + torrent_action action; uint state; uint progress; uint down_rate; @@ -49,7 +57,7 @@ namespace qtrapids { 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(); @@ -61,9 +69,11 @@ namespace qtrapids { 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.action = (TorrentState::torrent_action)action; argument.endStructure(); return argument; } diff --git a/src/include/qtrapids/settings.hpp b/src/include/qtrapids/settings.hpp new file mode 100644 index 0000000..d78625d --- /dev/null +++ b/src/include/qtrapids/settings.hpp @@ -0,0 +1,20 @@ +#include +#include +#include + +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; + } + +} diff --git a/src/server/TorrentSession.cpp b/src/server/TorrentSession.cpp index 6158452..531cd83 100644 --- a/src/server/TorrentSession.cpp +++ b/src/server/TorrentSession.cpp @@ -58,7 +58,7 @@ namespace qtrapids 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(); @@ -83,7 +83,7 @@ namespace qtrapids 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(); @@ -123,7 +123,7 @@ namespace qtrapids 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(); @@ -140,6 +140,7 @@ namespace qtrapids 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; @@ -151,6 +152,12 @@ namespace qtrapids "exception catched" ; } + + TorrentState state; + state.hash = hash; + state.action = TorrentState::action_remove; + emit alert(state, ParamsMap_t()); + torrents_.erase(p); }