|
From: Yoda-JM <yo...@us...> - 2012-09-15 17:11:26
|
Author: Vincent Le Ligeour <yo...@us...>
Date: Sat Sep 15 19:09:28 2012 +0200
new: add interface to pause/resume/remove torrents
Add GUI to remove torrents from session (backspace) or pause/resume
torrent (space).
The default torrent status is paused (seeding/leaching not automatically
started).
---
data/schema.xml | 4 ++++
game/downloader.cc | 31 ++++++++++++++++++++++++++++++-
game/downloader.hh | 3 +++
game/screen_downloads.cc | 11 +++++++++++
4 files changed, 48 insertions(+), 1 deletions(-)
diff --git a/data/schema.xml b/data/schema.xml
index 6c98cfc..72a015b 100644
--- a/data/schema.xml
+++ b/data/schema.xml
@@ -272,4 +272,8 @@ to save the current settings to XML.
<short>DLC save directory</short>
<long>Where to save DLC. DATADIR at the beginning means all Performous data folders.</long>
</entry>
+ <entry name="dlc/autostart" type="bool" value="false">
+ <short>Automatically start DLC</short>
+ <short>Automatically start DLC (seeding and downloading) when performous starts.</short>
+ </entry>
</performous>
diff --git a/game/downloader.cc b/game/downloader.cc
index d3d9bf5..1b5adb4 100644
--- a/game/downloader.cc
+++ b/game/downloader.cc
@@ -69,8 +69,13 @@ class Downloader::Impl {
torrent_status status = h.status();
Torrent t;
t.name = h.name();
- t.state = state_str[status.state];
+ if(status.paused) {
+ t.state = "paused";
+ } else {
+ t.state = state_str[status.state];
+ }
t.progress = status.progress;
+ t.sha1 = h.info_hash().to_string();
std::string percent = boost::lexical_cast<std::string>(int(round(t.progress*100)));
std::clog << "torrent/debug: " << t.name << " (" << t.state << ":" << percent << "%)" << std::endl;
@@ -98,6 +103,21 @@ class Downloader::Impl {
}
}
+ void removeTorrent(std::string sha1) {
+ torrent_handle h = s.find_torrent(sha1_hash(sha1));
+ s.remove_torrent(h);
+ }
+
+ void pauseResume(std::string sha1) {
+ torrent_handle h = s.find_torrent(sha1_hash(sha1));
+ if(h.status().paused) {
+ h.resume();
+ } else {
+ h.auto_managed(false);
+ h.pause();
+ }
+ }
+
void pause(bool state) {
if (state) {
s.pause();
@@ -133,6 +153,11 @@ class Downloader::Impl {
add_torrent_params params;
params.save_path = savePath.string();
params.url = url;
+ if(config["dlc/autostart"].b()) {
+ params.flags |= add_torrent_params::flag_auto_managed;
+ } else {
+ params.flags &= ~add_torrent_params::flag_auto_managed;
+ }
std::clog << "torrent/info: adding " << url << " (saving in " << savePath << ")" << std::endl;
s.add_torrent(params, ec);
if(ec) {
@@ -161,6 +186,10 @@ Downloader::~Downloader() {}
void Downloader::pause(bool state) { self->pause(state); }
+void Downloader::pauseResume(std::string sha1) { self->pauseResume(sha1); }
+
void Downloader::addTorrent(std::string url) { self->addTorrent(url); }
+void Downloader::removeTorrent(std::string sha1) { self->removeTorrent(sha1); }
+
std::vector<Torrent> Downloader::getTorrents() const { return self->getTorrents(); };
diff --git a/game/downloader.hh b/game/downloader.hh
index 79b44a8..1b0a0d0 100644
--- a/game/downloader.hh
+++ b/game/downloader.hh
@@ -8,6 +8,7 @@ struct Torrent {
Torrent() {};
std::string name;
std::string state;
+ std::string sha1;
float progress;
};
@@ -16,7 +17,9 @@ class Downloader : boost::noncopyable {
Downloader();
~Downloader();
void pause(bool state);
+ void pauseResume(std::string sha1);
void addTorrent(std::string url);
+ void removeTorrent(std::string sha1);
std::vector<Torrent> getTorrents() const;
private:
class Impl;
diff --git a/game/screen_downloads.cc b/game/screen_downloads.cc
index 9b82dce..93f5e87 100644
--- a/game/screen_downloads.cc
+++ b/game/screen_downloads.cc
@@ -30,6 +30,17 @@ void ScreenDownloads::manageEvent(SDL_Event event) {
} else if (event.type == SDL_KEYDOWN) {
int key = event.key.keysym.sym;
SDLMod modifier = event.key.keysym.mod;
+ if (key == SDLK_SPACE) {
+ std::vector<Torrent> torrents = m_downloader.getTorrents();
+ if(m_selectedTorrent < torrents.size()) {
+ m_downloader.pauseResume(torrents[m_selectedTorrent].sha1);
+ }
+ } else if(key == SDLK_BACKSPACE) {
+ std::vector<Torrent> torrents = m_downloader.getTorrents();
+ if(m_selectedTorrent < torrents.size()) {
+ m_downloader.removeTorrent(torrents[m_selectedTorrent].sha1);
+ }
+ }
}
}
|