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 <fstream>
40 #include <stdexcept>
41 #include <string.h>
42 #include <rak/string_manip.h>
43 #include <torrent/throttle.h>
44 #include <torrent/torrent.h>
45 #include <torrent/download/resource_manager.h>
46 #include <torrent/utils/log.h>
47
48 #include "core/manager.h"
49 #include "display/frame.h"
50 #include "display/window_http_queue.h"
51 #include "display/window_title.h"
52 #include "display/window_input.h"
53 #include "display/window_statusbar.h"
54 #include "input/manager.h"
55 #include "input/text_input.h"
56 #include "rpc/parse_commands.h"
57
58 #include "control.h"
59 #include "download_list.h"
60 #include "core/download_store.h"
61
62 #include "root.h"
63
64 namespace ui {
65
Root()66 Root::Root() :
67 m_control(NULL),
68 m_downloadList(NULL),
69 m_windowTitle(NULL),
70 m_windowHttpQueue(NULL),
71 m_windowInput(NULL),
72 m_windowStatusbar(NULL),
73 m_input_history_length(99),
74 m_input_history_last_input(""),
75 m_input_history_pointer_get(0) {
76
77 // Initialise prefilled m_input_history and m_input_history_pointers objects.
78 for (int type = ui::DownloadList::INPUT_LOAD_DEFAULT; type != ui::DownloadList::INPUT_EOI; type++) {
79 m_input_history.insert( std::make_pair(type, InputHistoryCategory(m_input_history_length)) );
80 m_input_history_pointers.insert( std::make_pair(type, 0) );
81 }
82
83 }
84
85 void
init(Control * c)86 Root::init(Control* c) {
87 if (m_control != NULL)
88 throw std::logic_error("Root::init() called twice on the same object");
89
90 m_control = c;
91
92 m_windowTitle = new WTitle();
93 m_windowHttpQueue = new WHttpQueue(control->core()->http_queue());
94 m_windowInput = new WInput();
95 m_windowStatusbar = new WStatusbar();
96
97 m_downloadList = new DownloadList();
98
99 display::Frame* rootFrame = m_control->display()->root_frame();
100
101 rootFrame->initialize_row(5);
102 rootFrame->frame(0)->initialize_window(m_windowTitle);
103 rootFrame->frame(2)->initialize_window(m_windowHttpQueue);
104 rootFrame->frame(3)->initialize_window(m_windowInput);
105 rootFrame->frame(4)->initialize_window(m_windowStatusbar);
106
107 m_windowTitle->set_active(true);
108 m_windowStatusbar->set_active(true);
109 m_windowStatusbar->set_bottom(true);
110
111 setup_keys();
112
113 m_downloadList->activate(rootFrame->frame(1));
114 }
115
116 void
cleanup()117 Root::cleanup() {
118 if (m_control == NULL)
119 throw std::logic_error("Root::cleanup() called twice on the same object");
120
121 if (m_downloadList->is_active())
122 m_downloadList->disable();
123
124 m_control->display()->root_frame()->clear();
125
126 delete m_downloadList;
127
128 delete m_windowTitle;
129 delete m_windowHttpQueue;
130 delete m_windowInput;
131 delete m_windowStatusbar;
132
133 m_control->input()->erase(&m_bindings);
134 m_control = NULL;
135 }
136
137 const char*
get_throttle_keys()138 Root::get_throttle_keys() {
139 const std::string& keyLayout = rpc::call_command_string("keys.layout");
140
141 if (strcasecmp(keyLayout.c_str(), "azerty") == 0)
142 return "qwQWsxSXdcDC";
143 else if (strcasecmp(keyLayout.c_str(), "qwertz") == 0)
144 return "ayAYsxSXdcDC";
145 else if (strcasecmp(keyLayout.c_str(), "dvorak") == 0)
146 return "a;A:oqOQejEJ";
147 else
148 return "azAZsxSXdcDC";
149 }
150
151 void
setup_keys()152 Root::setup_keys() {
153 m_control->input()->push_back(&m_bindings);
154
155 const char* keys = get_throttle_keys();
156
157 m_bindings[keys[ 0]] = std::bind(&Root::adjust_up_throttle, this, (int) rpc::call_command_value("ui.throttle.global.step.small"));
158 m_bindings[keys[ 1]] = std::bind(&Root::adjust_up_throttle, this, (int) -rpc::call_command_value("ui.throttle.global.step.small"));
159 m_bindings[keys[ 2]] = std::bind(&Root::adjust_down_throttle, this, (int) rpc::call_command_value("ui.throttle.global.step.small"));
160 m_bindings[keys[ 3]] = std::bind(&Root::adjust_down_throttle, this, (int) -rpc::call_command_value("ui.throttle.global.step.small"));
161
162 m_bindings[keys[ 4]] = std::bind(&Root::adjust_up_throttle, this, (int) rpc::call_command_value("ui.throttle.global.step.medium"));
163 m_bindings[keys[ 5]] = std::bind(&Root::adjust_up_throttle, this, (int) -rpc::call_command_value("ui.throttle.global.step.medium"));
164 m_bindings[keys[ 6]] = std::bind(&Root::adjust_down_throttle, this, (int) rpc::call_command_value("ui.throttle.global.step.medium"));
165 m_bindings[keys[ 7]] = std::bind(&Root::adjust_down_throttle, this, (int) -rpc::call_command_value("ui.throttle.global.step.medium"));
166
167 m_bindings[keys[ 8]] = std::bind(&Root::adjust_up_throttle, this, (int) rpc::call_command_value("ui.throttle.global.step.large"));
168 m_bindings[keys[ 9]] = std::bind(&Root::adjust_up_throttle, this, (int) -rpc::call_command_value("ui.throttle.global.step.large"));
169 m_bindings[keys[10]] = std::bind(&Root::adjust_down_throttle, this, (int) rpc::call_command_value("ui.throttle.global.step.large"));
170 m_bindings[keys[11]] = std::bind(&Root::adjust_down_throttle, this, (int) -rpc::call_command_value("ui.throttle.global.step.large"));
171
172 m_bindings['\x0C'] = std::bind(&display::Manager::force_redraw, m_control->display()); // ^L
173 m_bindings['\x11'] = std::bind(&Control::receive_normal_shutdown, m_control); // ^Q
174 }
175
176 void
set_down_throttle(unsigned int throttle)177 Root::set_down_throttle(unsigned int throttle) {
178 if (m_windowStatusbar != NULL)
179 m_windowStatusbar->mark_dirty();
180
181 torrent::down_throttle_global()->set_max_rate(throttle * 1024);
182
183 unsigned int div = std::max<int>(rpc::call_command_value("throttle.max_downloads.div"), 0);
184 unsigned int global = std::max<int>(rpc::call_command_value("throttle.max_downloads.global"), 0);
185
186 if (throttle == 0 || div == 0) {
187 torrent::resource_manager()->set_max_download_unchoked(global);
188 return;
189 }
190
191 throttle /= div;
192
193 unsigned int maxUnchoked;
194
195 if (throttle <= 10)
196 maxUnchoked = 1 + throttle / 1;
197 else
198 maxUnchoked = 10 + throttle / 5;
199
200 if (global != 0)
201 torrent::resource_manager()->set_max_download_unchoked(std::min(maxUnchoked, global));
202 else
203 torrent::resource_manager()->set_max_download_unchoked(maxUnchoked);
204 }
205
206 void
set_up_throttle(unsigned int throttle)207 Root::set_up_throttle(unsigned int throttle) {
208 if (m_windowStatusbar != NULL)
209 m_windowStatusbar->mark_dirty();
210
211 torrent::up_throttle_global()->set_max_rate(throttle * 1024);
212
213 unsigned int div = std::max<int>(rpc::call_command_value("throttle.max_uploads.div"), 0);
214 unsigned int global = std::max<int>(rpc::call_command_value("throttle.max_uploads.global"), 0);
215
216 if (throttle == 0 || div == 0) {
217 torrent::resource_manager()->set_max_upload_unchoked(global);
218 return;
219 }
220
221 throttle /= div;
222
223 unsigned int maxUnchoked;
224
225 if (throttle <= 10)
226 maxUnchoked = 1 + throttle / 1;
227 else
228 maxUnchoked = 10 + throttle / 5;
229
230 if (global != 0)
231 torrent::resource_manager()->set_max_upload_unchoked(std::min(maxUnchoked, global));
232 else
233 torrent::resource_manager()->set_max_upload_unchoked(maxUnchoked);
234 }
235
236 void
adjust_down_throttle(int throttle)237 Root::adjust_down_throttle(int throttle) {
238 set_down_throttle(std::max<int>(torrent::down_throttle_global()->max_rate() / 1024 + throttle, 0));
239 }
240
241 void
adjust_up_throttle(int throttle)242 Root::adjust_up_throttle(int throttle) {
243 set_up_throttle(std::max<int>(torrent::up_throttle_global()->max_rate() / 1024 + throttle, 0));
244 }
245
246 void
enable_input(const std::string & title,input::TextInput * input,ui::DownloadList::Input type)247 Root::enable_input(const std::string& title, input::TextInput* input, ui::DownloadList::Input type) {
248 if (m_windowInput->input() != NULL)
249 throw torrent::internal_error("Root::enable_input(...) m_windowInput->input() != NULL.");
250
251 input->slot_dirty(std::bind(&WInput::mark_dirty, m_windowInput));
252
253 m_windowStatusbar->set_active(false);
254
255 m_windowInput->set_active(true);
256 m_windowInput->set_input(input);
257 m_windowInput->set_title(title);
258 m_windowInput->set_focus(true);
259
260 reset_input_history_attributes(type);
261
262 input->bindings()['\x0C'] = std::bind(&display::Manager::force_redraw, m_control->display()); // ^L
263 input->bindings()['\x11'] = std::bind(&Control::receive_normal_shutdown, m_control); // ^Q
264 input->bindings()[KEY_UP] = std::bind(&Root::prev_in_input_history, this, type); // UP arrow
265 input->bindings()['\x10'] = std::bind(&Root::prev_in_input_history, this, type); // ^P
266 input->bindings()[KEY_DOWN] = std::bind(&Root::next_in_input_history, this, type); // DOWN arrow
267 input->bindings()['\x0E'] = std::bind(&Root::next_in_input_history, this, type); // ^N
268
269 control->input()->set_text_input(input);
270 control->display()->adjust_layout();
271 }
272
273 void
disable_input()274 Root::disable_input() {
275 if (m_windowInput->input() == NULL)
276 throw torrent::internal_error("Root::disable_input() m_windowInput->input() == NULL.");
277
278 m_windowInput->input()->slot_dirty(ElementBase::slot_type());
279
280 m_windowStatusbar->set_active(true);
281
282 m_windowInput->set_active(false);
283 m_windowInput->set_focus(false);
284 m_windowInput->set_input(NULL);
285
286 control->input()->set_text_input(NULL);
287 control->display()->adjust_layout();
288 }
289
290 input::TextInput*
current_input()291 Root::current_input() {
292 return m_windowInput->input();
293 }
294
295 void
add_to_input_history(ui::DownloadList::Input type,std::string item)296 Root::add_to_input_history(ui::DownloadList::Input type, std::string item) {
297 InputHistory::iterator itr = m_input_history.find(type);
298 InputHistoryPointers::iterator pitr = m_input_history_pointers.find(type);
299 int prev_item_pointer = (pitr->second - 1) < 0 ? (m_input_history_length - 1) : (pitr->second - 1);
300
301 // Don't store item if it's empty or the same as the last one in the category.
302 if (!item.empty() && item != itr->second.at(prev_item_pointer)) {
303 itr->second.at(pitr->second) = rak::trim(item);
304 m_input_history_pointers[type] = (pitr->second + 1) % m_input_history_length;
305 }
306 }
307
308 void
prev_in_input_history(ui::DownloadList::Input type)309 Root::prev_in_input_history(ui::DownloadList::Input type) {
310 if (m_windowInput->input() == NULL)
311 throw torrent::internal_error("Root::prev_in_input_history() m_windowInput->input() == NULL.");
312
313 InputHistory::iterator itr = m_input_history.find(type);
314 InputHistoryPointers::const_iterator pitr = m_input_history_pointers.find(type);
315
316 if (m_input_history_pointer_get == pitr->second)
317 m_input_history_last_input = m_windowInput->input()->str();
318 else
319 itr->second.at(m_input_history_pointer_get) = m_windowInput->input()->str();
320
321 std::string tmp_input = m_input_history_last_input;
322 int prev_pointer_get = (m_input_history_pointer_get - 1) < 0 ? (m_input_history_length - 1) : (m_input_history_pointer_get - 1);
323
324 if (prev_pointer_get != pitr->second && itr->second.at(prev_pointer_get) != "")
325 m_input_history_pointer_get = prev_pointer_get;
326
327 if (m_input_history_pointer_get != pitr->second)
328 tmp_input = itr->second.at(m_input_history_pointer_get);
329
330 m_windowInput->input()->str() = tmp_input;
331 m_windowInput->input()->set_pos(tmp_input.length());
332 m_windowInput->input()->mark_dirty();
333 }
334
335 void
next_in_input_history(ui::DownloadList::Input type)336 Root::next_in_input_history(ui::DownloadList::Input type) {
337 if (m_windowInput->input() == NULL)
338 throw torrent::internal_error("Root::next_in_input_history() m_windowInput->input() == NULL.");
339
340 InputHistory::iterator itr = m_input_history.find(type);
341 InputHistoryPointers::const_iterator pitr = m_input_history_pointers.find(type);
342
343 if (m_input_history_pointer_get == pitr->second)
344 m_input_history_last_input = m_windowInput->input()->str();
345 else
346 itr->second.at(m_input_history_pointer_get) = m_windowInput->input()->str();
347
348 std::string tmp_input = m_input_history_last_input;
349
350 if (m_input_history_pointer_get != pitr->second) {
351 m_input_history_pointer_get = (m_input_history_pointer_get + 1) % m_input_history_length;
352 tmp_input = (m_input_history_pointer_get == pitr->second) ? m_input_history_last_input : itr->second.at(m_input_history_pointer_get);
353 }
354
355 m_windowInput->input()->str() = tmp_input;
356 m_windowInput->input()->set_pos(tmp_input.length());
357 m_windowInput->input()->mark_dirty();
358 }
359
360 void
reset_input_history_attributes(ui::DownloadList::Input type)361 Root::reset_input_history_attributes(ui::DownloadList::Input type) {
362 InputHistoryPointers::const_iterator itr = m_input_history_pointers.find(type);
363
364 // Clear last_input and set pointer_get to the same as pointer_insert.
365 m_input_history_last_input = "";
366 m_input_history_pointer_get = itr->second;
367 }
368
369 void
set_input_history_size(int size)370 Root::set_input_history_size(int size) {
371 if (size < 1)
372 throw torrent::input_error("Invalid input history size.");
373
374 for (InputHistory::iterator itr = m_input_history.begin(), last = m_input_history.end(); itr != last; itr++) {
375 // Reserve the latest input history entries if new size is smaller than original.
376 if (size < m_input_history_length) {
377 int pointer_offset = m_input_history_length - size;
378 InputHistoryPointers::iterator pitr = m_input_history_pointers.find(itr->first);
379 InputHistoryCategory input_history_category_tmp = itr->second;
380
381 for (int i=0; i != size; i++)
382 itr->second.at(i) = input_history_category_tmp.at((pitr->second + pointer_offset + i) % m_input_history_length);
383
384 m_input_history_pointers[pitr->first] = 0;
385 }
386
387 itr->second.resize(size);
388 }
389
390 m_input_history_length = size;
391 }
392
393 void
load_input_history()394 Root::load_input_history() {
395 if (!m_control->core()->download_store()->is_enabled()) {
396 lt_log_print(torrent::LOG_DEBUG, "ignoring input history file");
397 return;
398 }
399
400 std::string history_filename = m_control->core()->download_store()->path() + "rtorrent.input_history";
401 std::fstream history_file(history_filename.c_str(), std::ios::in);
402
403 if (history_file.is_open()) {
404 // Create a temp object of the content since size of history categories can be smaller than this.
405 InputHistory input_history_tmp;
406
407 for (int type = ui::DownloadList::INPUT_LOAD_DEFAULT; type != ui::DownloadList::INPUT_EOI; type++)
408 input_history_tmp.insert( std::make_pair(type, InputHistoryCategory()) );
409
410 std::string line;
411
412 while (std::getline(history_file, line)) {
413 if (!line.empty()) {
414 int delim_pos = line.find("|");
415
416 if (delim_pos != std::string::npos) {
417 int type = std::atoi(line.substr(0, delim_pos).c_str());
418 InputHistory::iterator itr = input_history_tmp.find(type);
419
420 if (itr != input_history_tmp.end()) {
421 std::string input_str = rak::trim(line.substr(delim_pos + 1));
422
423 if (!input_str.empty())
424 itr->second.push_back(input_str);
425 }
426 }
427 }
428 }
429
430 if (history_file.bad()) {
431 lt_log_print(torrent::LOG_DEBUG, "input history file corrupted, discarding (path:%s)", history_filename.c_str());
432 return;
433 } else {
434 lt_log_print(torrent::LOG_DEBUG, "input history file read (path:%s)", history_filename.c_str());
435 }
436
437 for (InputHistory::const_iterator itr = input_history_tmp.begin(), last = input_history_tmp.end(); itr != last; itr++) {
438 int input_history_tmp_category_length = itr->second.size();
439 InputHistory::iterator hitr = m_input_history.find(itr->first);
440 InputHistoryPointers::iterator pitr = m_input_history_pointers.find(itr->first);
441
442 if (m_input_history_length < input_history_tmp_category_length) {
443 int pointer_offset = input_history_tmp_category_length - m_input_history_length;
444
445 for (int i=0; i != m_input_history_length; i++)
446 hitr->second.at(i) = itr->second.at((pointer_offset + i) % input_history_tmp_category_length);
447
448 pitr->second = 0;
449 } else {
450 hitr->second = itr->second;
451 hitr->second.resize(m_input_history_length);
452
453 pitr->second = input_history_tmp_category_length % m_input_history_length;
454 }
455 }
456 } else {
457 lt_log_print(torrent::LOG_DEBUG, "could not open input history file (path:%s)", history_filename.c_str());
458 }
459 }
460
461 void
save_input_history()462 Root::save_input_history() {
463 if (!m_control->core()->download_store()->is_enabled())
464 return;
465
466 std::string history_filename = m_control->core()->download_store()->path() + "rtorrent.input_history";
467 std::string history_filename_tmp = history_filename + ".new";
468 std::fstream history_file(history_filename_tmp.c_str(), std::ios::out | std::ios::trunc);
469
470 if (!history_file.is_open()) {
471 lt_log_print(torrent::LOG_DEBUG, "could not open input history file for writing (path:%s)", history_filename.c_str());
472 return;
473 }
474
475 for (InputHistory::const_iterator itr = m_input_history.begin(), last = m_input_history.end(); itr != last; itr++) {
476 InputHistoryPointers::const_iterator pitr = m_input_history_pointers.find(itr->first);
477
478 for (int i=0; i != m_input_history_length; i++)
479 if (!itr->second.at((pitr->second + i) % m_input_history_length).empty())
480 history_file << itr->first << "|" + itr->second.at((pitr->second + i) % m_input_history_length) + "\n";
481 }
482
483 if (!history_file.good()) {
484 lt_log_print(torrent::LOG_DEBUG, "input history file corrupted during writing, discarding (path:%s)", history_filename.c_str());
485 return;
486 } else {
487 lt_log_print(torrent::LOG_DEBUG, "input history file written (path:%s)", history_filename.c_str());
488 }
489
490 history_file.close();
491
492 std::rename(history_filename_tmp.c_str(), history_filename.c_str());
493 }
494
495 void
clear_input_history()496 Root::clear_input_history() {
497 for (int type = ui::DownloadList::INPUT_LOAD_DEFAULT; type != ui::DownloadList::INPUT_EOI; type++) {
498 InputHistory::iterator itr = m_input_history.find(type);
499
500 for (int i=0; i != m_input_history_length; i++)
501 itr->second.at(i) = "";
502
503 m_input_history_pointers[type] = 0;
504 }
505 }
506
507 }
508