1 // rTorrent - BitTorrent client
2 // Copyright (C) 2005-2011, Jari Sundell
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 //
18 // In addition, as a special exception, the copyright holders give
19 // permission to link the code of portions of this program with the
20 // OpenSSL library under certain conditions as described in each
21 // individual source file, and distribute linked combinations
22 // including the two.
23 //
24 // You must obey the GNU General Public License in all respects for
25 // all of the code used other than OpenSSL.  If you modify file(s)
26 // with this exception, you may extend this exception to your version
27 // of the file(s), but you are not obligated to do so.  If you do not
28 // wish to do so, delete this exception statement from your version.
29 // If you delete this exception statement from all source files in the
30 // program, then also delete it here.
31 //
32 // Contact:  Jari Sundell <jaris@ifi.uio.no>
33 //
34 //           Skomakerveien 33
35 //           3185 Skoppum, NORWAY
36 
37 #include "config.h"
38 
39 #include <torrent/exceptions.h>
40 
41 #include "display/frame.h"
42 #include "display/window_download_chunks_seen.h"
43 #include "input/manager.h"
44 
45 #include "control.h"
46 #include "element_chunks_seen.h"
47 
48 namespace ui {
49 
ElementChunksSeen(core::Download * d)50 ElementChunksSeen::ElementChunksSeen(core::Download* d) :
51   m_download(d),
52   m_window(NULL),
53   m_focus(0) {
54 
55   m_bindings[KEY_LEFT] = m_bindings['B' - '@'] = std::bind(&slot_type::operator(), &m_slot_exit);
56 
57   m_bindings[KEY_DOWN]  = m_bindings['N' - '@'] = std::bind(&ElementChunksSeen::receive_next, this);
58   m_bindings[KEY_UP]    = m_bindings['P' - '@'] = std::bind(&ElementChunksSeen::receive_prev, this);
59   m_bindings[KEY_NPAGE] = std::bind(&ElementChunksSeen::receive_pagenext, this);
60   m_bindings[KEY_PPAGE] = std::bind(&ElementChunksSeen::receive_pageprev, this);
61 }
62 
63 void
activate(display::Frame * frame,bool focus)64 ElementChunksSeen::activate(display::Frame* frame, bool focus) {
65   if (is_active())
66     throw torrent::internal_error("ui::ElementChunksSeen::activate(...) is_active().");
67 
68   if (focus)
69     control->input()->push_back(&m_bindings);
70 
71   m_window = new WChunksSeen(m_download, &m_focus);
72   m_window->set_active(true);
73 
74   m_frame = frame;
75   m_frame->initialize_window(m_window);
76 }
77 
78 void
disable()79 ElementChunksSeen::disable() {
80   if (!is_active())
81     throw torrent::internal_error("ui::ElementChunksSeen::disable(...) !is_active().");
82 
83   control->input()->erase(&m_bindings);
84 
85   m_frame->clear();
86   m_frame = NULL;
87 
88   delete m_window;
89   m_window = NULL;
90 }
91 
92 display::Window*
window()93 ElementChunksSeen::window() {
94   return m_window;
95 }
96 
97 // void
98 // ElementChunksSeen::receive_disable() {
99 //   if (m_window == NULL)
100 //     throw std::logic_error("ui::ElementChunksSeen::receive_disable(...) called on a disabled object");
101 
102 //   if (m_download->download()->tracker(m_focus).is_enabled())
103 //     m_download->download()->tracker(m_focus).disable();
104 //   else
105 //     m_download->download()->tracker(m_focus).enable();
106 
107 //   m_window->mark_dirty();
108 // }
109 
110 void
receive_next()111 ElementChunksSeen::receive_next() {
112   if (m_window == NULL)
113     throw torrent::internal_error("ui::ElementChunksSeen::receive_next(...) called on a disabled object");
114 
115   if (++m_focus > m_window->max_focus())
116     m_focus = 0;
117 
118   m_window->mark_dirty();
119 }
120 
121 void
receive_prev()122 ElementChunksSeen::receive_prev() {
123   if (m_window == NULL)
124     throw torrent::internal_error("ui::ElementChunksSeen::receive_prev(...) called on a disabled object");
125 
126   if (m_focus > 0)
127     --m_focus;
128   else
129     m_focus = m_window->max_focus();
130 
131   m_window->mark_dirty();
132 }
133 
134 void
receive_pagenext()135 ElementChunksSeen::receive_pagenext() {
136   if (m_window == NULL)
137     throw torrent::internal_error("ui::ElementChunksSeen::receive_pagenext(...) called on a disabled object");
138 
139   unsigned int visible = m_window->height() - 1;
140   unsigned int maxFocus = m_window->max_focus();
141 
142   if (maxFocus == 0 || m_focus == maxFocus)
143     m_focus = 0;
144   else if (m_focus + visible / 2 < maxFocus)
145     m_focus += visible / 2;
146   else
147     m_focus = maxFocus;
148 
149   m_window->mark_dirty();
150 }
151 
152 void
receive_pageprev()153 ElementChunksSeen::receive_pageprev() {
154   if (m_window == NULL)
155     throw torrent::internal_error("ui::ElementChunksSeen::receive_pageprev(...) called on a disabled object");
156 
157   unsigned int visible = m_window->height() - 1;
158   unsigned int maxFocus = m_window->max_focus();
159 
160   if (m_focus > visible / 2)
161     m_focus -= visible / 2;
162   else if (maxFocus > 0 && m_focus == 0)
163     m_focus = maxFocus;
164   else
165     m_focus = 0;
166 
167   m_window->mark_dirty();
168 }
169 
170 }
171