From b657554236bfb0bcb0da8e7ba3d1262659ff92f0 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Mon, 7 Feb 2005 23:40:48 +0000 Subject: [PATCH] Work on peer info display. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@260 e378c898-3ddf-0310-93e7-cc216c733640 --- src/display/Makefile.am | 2 + src/display/window_peer_info.cc | 32 +++++++++++ src/display/window_peer_info.h | 32 +++++++++++ src/display/window_statusbar.cc | 4 +- src/input/bindings.h | 2 + src/main.cc | 10 ++-- src/ui/control.h | 4 ++ src/ui/download.cc | 96 ++++++++++++++++++++++++++++++--- src/ui/download.h | 29 ++++++++-- src/ui/download_list.cc | 9 ++-- 10 files changed, 199 insertions(+), 21 deletions(-) create mode 100644 src/display/window_peer_info.cc create mode 100644 src/display/window_peer_info.h diff --git a/src/display/Makefile.am b/src/display/Makefile.am index 217518e8..bdd3ba03 100644 --- a/src/display/Makefile.am +++ b/src/display/Makefile.am @@ -9,6 +9,8 @@ libsub_display_a_SOURCES = \ window.h \ window_download_list.cc \ window_download_list.h \ + window_peer_info.cc \ + window_peer_info.h \ window_peer_list.cc \ window_peer_list.h \ window_statusbar.cc \ diff --git a/src/display/window_peer_info.cc b/src/display/window_peer_info.cc new file mode 100644 index 00000000..ee133e0b --- /dev/null +++ b/src/display/window_peer_info.cc @@ -0,0 +1,32 @@ +#include "config.h" + +#include + +#include "canvas.h" +#include "window_peer_info.h" + +namespace display { + +WindowPeerInfo::WindowPeerInfo(PList* l, PList::iterator* f) : + Window(new Canvas, true), + m_list(l), + m_focus(f) { +} + +void +WindowPeerInfo::redraw() { + if (Timer::cache() - m_lastDraw < 1000000) + return; + + m_lastDraw = Timer::cache(); + m_canvas->erase(); + + if (*m_focus == m_list->end()) + mvprintw(0, 0, "No focus"); + else + mvprintw(0, 0, "Focus on: %s:%i", + (*m_focus)->get_dns().c_str(), + (*m_focus)->get_port()); +} + +} diff --git a/src/display/window_peer_info.h b/src/display/window_peer_info.h new file mode 100644 index 00000000..e90f5c98 --- /dev/null +++ b/src/display/window_peer_info.h @@ -0,0 +1,32 @@ +#ifndef RTORRENT_DISPLAY_PEER_INFO_H +#define RTORRENT_DISPLAY_PEER_INFO_H + +#include +#include +#include + +#include "window.h" + +namespace display { + +class WindowPeerInfo : public Window { +public: + typedef std::list PList; + typedef sigc::slot0 SlotChunksTotal; + + WindowPeerInfo(PList* l, PList::iterator* f); + + void slot_chunks_total(SlotChunksTotal s) { m_slotChunksTotal = s; } + + virtual void redraw(); + +private: + PList* m_list; + PList::iterator* m_focus; + + SlotChunksTotal m_slotChunksTotal; +}; + +} + +#endif diff --git a/src/display/window_statusbar.cc b/src/display/window_statusbar.cc index ed38d3b7..dbdd5230 100644 --- a/src/display/window_statusbar.cc +++ b/src/display/window_statusbar.cc @@ -14,6 +14,8 @@ WindowStatusbar::WindowStatusbar() : void WindowStatusbar::redraw() { + m_counter++; + if (Timer::cache() - m_lastDraw < 10000000) return; @@ -25,7 +27,7 @@ WindowStatusbar::redraw() { "", (int)torrent::get(torrent::LISTEN_PORT), (int)torrent::get(torrent::HANDSHAKES_TOTAL), - ++m_counter); + m_counter); } } diff --git a/src/input/bindings.h b/src/input/bindings.h index 650f1d13..2645c691 100644 --- a/src/input/bindings.h +++ b/src/input/bindings.h @@ -22,6 +22,8 @@ public: using Base::rend; using Base::find; + using Base::erase; + using Base::operator[]; Bindings() : m_active(true) {} diff --git a/src/main.cc b/src/main.cc index 093d811f..3c450882 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,3 +1,5 @@ +#include "config.h" + #include #include #include @@ -92,6 +94,10 @@ int main(int argc, char** argv) { try { + SignalHandler::set_handler(SIGINT, sigc::ptr_fun(&set_shutdown)); + SignalHandler::set_handler(SIGSEGV, sigc::bind(sigc::ptr_fun(&do_panic), SIGSEGV)); + SignalHandler::set_handler(SIGBUS, sigc::bind(sigc::ptr_fun(&do_panic), SIGBUS)); + display::Canvas::init(); core::CurlStack::init(); @@ -123,10 +129,6 @@ main(int argc, char** argv) { itr->hash_check(); } - SignalHandler::set_handler(SIGINT, sigc::ptr_fun(&set_shutdown)); - SignalHandler::set_handler(SIGSEGV, sigc::bind(sigc::ptr_fun(&do_panic), SIGSEGV)); - SignalHandler::set_handler(SIGBUS, sigc::bind(sigc::ptr_fun(&do_panic), SIGBUS)); - uiControl.get_display().adjust_layout(); while (!is_shutting_down || !torrent::get(torrent::SHUTDOWN_DONE)) { diff --git a/src/ui/control.h b/src/ui/control.h index 87170d98..d959b07a 100644 --- a/src/ui/control.h +++ b/src/ui/control.h @@ -8,11 +8,15 @@ namespace ui { class Control { public: + Control() {} display::Manager& get_display() { return m_display; } input::Manager& get_input() { return m_input; } private: + Control(const Control&); + void operator = (const Control&); + display::Manager m_display; input::Manager m_input; }; diff --git a/src/ui/download.cc b/src/ui/download.cc index 93c9762d..24b57152 100644 --- a/src/ui/download.cc +++ b/src/ui/download.cc @@ -4,9 +4,11 @@ #include #include +#include "core/download.h" #include "input/bindings.h" #include "display/window_title.h" #include "display/window_peer_list.h" +#include "display/window_peer_info.h" #include "display/window_statusbar.h" #include "control.h" @@ -16,6 +18,8 @@ namespace ui { Download::Download(DPtr d, Control* c) : m_download(d), + m_state(DISPLAY_NONE), + m_window(c->get_display().end()), m_title(new WTitle(d->get_download().get_name())), m_status(new WStatus), m_control(c), @@ -23,9 +27,6 @@ Download::Download(DPtr d, Control* c) : m_focus = m_peers.end(); - m_window = new WPeerList(&m_peers, &m_focus); - m_window->slot_chunks_total(sigc::mem_fun(m_download->get_download(), &torrent::Download::get_chunks_total)); - bind_keys(m_bindings); m_download->get_download().peer_list(m_peers); @@ -38,7 +39,6 @@ Download::~Download() { m_connPeerConnected.disconnect(); m_connPeerDisconnected.disconnect(); - delete m_window; delete m_title; delete m_status; delete m_bindings; @@ -46,18 +46,90 @@ Download::~Download() { void Download::activate() { - m_control->get_display().push_back(m_status); - m_control->get_display().push_front(m_window); + if (m_window != m_control->get_display().end()) + throw std::logic_error("ui::Download::activate() called on an already activated object"); + + m_window = m_control->get_display().insert(m_control->get_display().begin(), NULL); + m_control->get_display().push_front(m_title); + m_control->get_display().push_back(m_status); + m_control->get_input().push_front(m_bindings); + + activate_display(DISPLAY_MAIN); } void Download::disable() { + if (m_window == m_control->get_display().end()) + throw std::logic_error("ui::Download::disable() called on an already disabled object"); + + disable_display(); + m_control->get_display().erase(m_window); m_control->get_display().erase(m_title); m_control->get_display().erase(m_status); + m_control->get_input().erase(m_bindings); + + m_window = m_control->get_display().end(); +} + +void +Download::activate_display(Display d) { + WPeerList* wpl; + WPeerInfo* wpi; + + if (m_window == m_control->get_display().end()) + throw std::logic_error("ui::Download::activate_display(...) could not find previous display iterator"); + + switch (d) { + case DISPLAY_MAIN: + *m_window = wpl = new WPeerList(&m_peers, &m_focus); + + wpl->slot_chunks_total(sigc::mem_fun(m_download->get_download(), &torrent::Download::get_chunks_total)); + + (*m_bindings)[KEY_RIGHT] = sigc::bind(sigc::mem_fun(*this, &Download::receive_change), DISPLAY_PEER); + break; + + case DISPLAY_PEER: + *m_window = wpi = new WPeerInfo(&m_peers, &m_focus); + + wpi->slot_chunks_total(sigc::mem_fun(m_download->get_download(), &torrent::Download::get_chunks_total)); + + (*m_bindings)[' '] = sigc::bind(sigc::mem_fun(*this, &Download::receive_change), DISPLAY_MAIN); + break; + + default: + throw std::logic_error("ui::Download::activate_display(...) got wrong state"); + } + + m_state = d; + + m_control->get_display().adjust_layout(); +} + +// Does not delete disabled window. +void +Download::disable_display() { + switch (m_state) { + case DISPLAY_MAIN: + m_bindings->erase(KEY_RIGHT); + + break; + + case DISPLAY_PEER: + m_bindings->erase(' '); + + break; + + default: + break; + } + + delete *m_window; + + *m_window = NULL; } void @@ -105,6 +177,16 @@ Download::receive_throttle(int t) { torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) + t * 1024); } +void +Download::receive_change(Display d) { + if (d == m_state) + return; + + disable_display(); + + activate_display(d); +} + void Download::bind_keys(input::Bindings* b) { (*b)['a'] = sigc::bind(sigc::mem_fun(*this, &Download::receive_throttle), 1); @@ -120,7 +202,7 @@ Download::bind_keys(input::Bindings* b) { void Download::mark_dirty() { - m_window->mark_dirty(); + (*m_window)->mark_dirty(); } } diff --git a/src/ui/download.h b/src/ui/download.h index b8ec6fa6..3b3e982e 100644 --- a/src/ui/download.h +++ b/src/ui/download.h @@ -5,36 +5,52 @@ #include #include -#include "core/download_list.h" - namespace display { class WindowTitle; + class WindowPeerInfo; class WindowPeerList; class WindowStatusbar; } +namespace core { + class Download; +} + namespace ui { class Control; class Download { public: + typedef enum { + DISPLAY_NONE, + DISPLAY_MAIN, + DISPLAY_PEER + } Display; + + typedef display::Window Window; + typedef display::WindowPeerInfo WPeerInfo; typedef display::WindowPeerList WPeerList; typedef display::WindowTitle WTitle; typedef display::WindowStatusbar WStatus; - typedef core::DownloadList::iterator DPtr; + + typedef core::Download* DPtr; typedef std::list PList; + typedef display::Manager::iterator MItr; // We own 'window'. Download(DPtr d, Control* c); ~Download(); - WPeerList& get_window() { return *m_window; } + //WPeerList& get_window() { return *m_window; } input::Bindings& get_bindings() { return *m_bindings; } void activate(); void disable(); + void activate_display(Display d); + void disable_display(); + private: Download(const Download&); void operator = (const Download&); @@ -46,6 +62,7 @@ private: void receive_peer_disconnected(torrent::Peer p); void receive_throttle(int t); + void receive_change(Display d); void bind_keys(input::Bindings* b); @@ -55,7 +72,9 @@ private: PList m_peers; PList::iterator m_focus; - WPeerList* m_window; + Display m_state; + MItr m_window; + WTitle* m_title; WStatus* m_status; diff --git a/src/ui/download_list.cc b/src/ui/download_list.cc index 56cfb935..dc054030 100644 --- a/src/ui/download_list.cc +++ b/src/ui/download_list.cc @@ -77,14 +77,15 @@ DownloadList::receive_view_download() { if (m_focus == m_list->end()) return; + if (m_download != NULL) + throw std::logic_error("DownloadList::receive_view_download() called but m_download != NULL"); + disable(); - m_download = new Download(m_focus, m_control); + m_download = new Download(&*m_focus, m_control); - m_download->get_bindings()[KEY_LEFT] = sigc::mem_fun(*this, &DownloadList::receive_exit_download); m_download->activate(); - - m_control->get_display().adjust_layout(); + m_download->get_bindings()[KEY_LEFT] = sigc::mem_fun(*this, &DownloadList::receive_exit_download); } void