Fixed minor issues.

This commit is contained in:
rakshasa
2019-06-09 17:09:35 +09:00
parent 35d98c91e0
commit a3a384b049
9 changed files with 283 additions and 30 deletions
+234 -2
View File
@@ -36,11 +36,14 @@
#include "config.h"
#include <fstream>
#include <stdexcept>
#include <string.h>
#include <rak/string_manip.h>
#include <torrent/throttle.h>
#include <torrent/torrent.h>
#include <torrent/download/resource_manager.h>
#include <torrent/utils/log.h>
#include "core/manager.h"
#include "display/frame.h"
@@ -54,6 +57,7 @@
#include "control.h"
#include "download_list.h"
#include "core/download_store.h"
#include "root.h"
@@ -65,7 +69,17 @@ Root::Root() :
m_windowTitle(NULL),
m_windowHttpQueue(NULL),
m_windowInput(NULL),
m_windowStatusbar(NULL) {
m_windowStatusbar(NULL),
m_input_history_length(99),
m_input_history_pointer_get(0),
m_input_history_last_input("") {
// Initialise prefilled m_input_history and m_input_history_pointers objects.
for (int type = ui::DownloadList::INPUT_LOAD_DEFAULT; type != ui::DownloadList::INPUT_EOI; type++) {
m_input_history.insert( std::make_pair(type, InputHistoryCategory(m_input_history_length)) );
m_input_history_pointers.insert( std::make_pair(type, 0) );
}
}
void
@@ -230,7 +244,7 @@ Root::adjust_up_throttle(int throttle) {
}
void
Root::enable_input(const std::string& title, input::TextInput* input) {
Root::enable_input(const std::string& title, input::TextInput* input, ui::DownloadList::Input type) {
if (m_windowInput->input() != NULL)
throw torrent::internal_error("Root::enable_input(...) m_windowInput->input() != NULL.");
@@ -243,8 +257,14 @@ Root::enable_input(const std::string& title, input::TextInput* input) {
m_windowInput->set_title(title);
m_windowInput->set_focus(true);
reset_input_history_attributes(type);
input->bindings()['\x0C'] = std::bind(&display::Manager::force_redraw, m_control->display()); // ^L
input->bindings()['\x11'] = std::bind(&Control::receive_normal_shutdown, m_control); // ^Q
input->bindings()[KEY_UP] = std::bind(&Root::prev_in_input_history, this, type); // UP arrow
input->bindings()['\x10'] = std::bind(&Root::prev_in_input_history, this, type); // ^P
input->bindings()[KEY_DOWN] = std::bind(&Root::next_in_input_history, this, type); // DOWN arrow
input->bindings()['\x0E'] = std::bind(&Root::next_in_input_history, this, type); // ^N
control->input()->set_text_input(input);
control->display()->adjust_layout();
@@ -272,4 +292,216 @@ Root::current_input() {
return m_windowInput->input();
}
void
Root::add_to_input_history(ui::DownloadList::Input type, std::string item) {
InputHistory::iterator itr = m_input_history.find(type);
InputHistoryPointers::iterator pitr = m_input_history_pointers.find(type);
int prev_item_pointer = (pitr->second - 1) < 0 ? (m_input_history_length - 1) : (pitr->second - 1);
// Don't store item if it's empty or the same as the last one in the category.
if (!item.empty() && item != itr->second.at(prev_item_pointer)) {
itr->second.at(pitr->second) = rak::trim(item);
m_input_history_pointers[type] = (pitr->second + 1) % m_input_history_length;
}
}
void
Root::prev_in_input_history(ui::DownloadList::Input type) {
if (m_windowInput->input() == NULL)
throw torrent::internal_error("Root::prev_in_input_history() m_windowInput->input() == NULL.");
InputHistory::iterator itr = m_input_history.find(type);
InputHistoryPointers::const_iterator pitr = m_input_history_pointers.find(type);
if (m_input_history_pointer_get == pitr->second)
m_input_history_last_input = m_windowInput->input()->str();
else
itr->second.at(m_input_history_pointer_get) = m_windowInput->input()->str();
std::string tmp_input = m_input_history_last_input;
int prev_pointer_get = (m_input_history_pointer_get - 1) < 0 ? (m_input_history_length - 1) : (m_input_history_pointer_get - 1);
if (prev_pointer_get != pitr->second && itr->second.at(prev_pointer_get) != "")
m_input_history_pointer_get = prev_pointer_get;
if (m_input_history_pointer_get != pitr->second)
tmp_input = itr->second.at(m_input_history_pointer_get);
m_windowInput->input()->str() = tmp_input;
m_windowInput->input()->set_pos(tmp_input.length());
m_windowInput->input()->mark_dirty();
}
void
Root::next_in_input_history(ui::DownloadList::Input type) {
if (m_windowInput->input() == NULL)
throw torrent::internal_error("Root::next_in_input_history() m_windowInput->input() == NULL.");
InputHistory::iterator itr = m_input_history.find(type);
InputHistoryPointers::const_iterator pitr = m_input_history_pointers.find(type);
if (m_input_history_pointer_get == pitr->second)
m_input_history_last_input = m_windowInput->input()->str();
else
itr->second.at(m_input_history_pointer_get) = m_windowInput->input()->str();
std::string tmp_input = m_input_history_last_input;
if (m_input_history_pointer_get != pitr->second) {
m_input_history_pointer_get = (m_input_history_pointer_get + 1) % m_input_history_length;
tmp_input = (m_input_history_pointer_get == pitr->second) ? m_input_history_last_input : itr->second.at(m_input_history_pointer_get);
}
m_windowInput->input()->str() = tmp_input;
m_windowInput->input()->set_pos(tmp_input.length());
m_windowInput->input()->mark_dirty();
}
void
Root::reset_input_history_attributes(ui::DownloadList::Input type) {
InputHistoryPointers::const_iterator itr = m_input_history_pointers.find(type);
// Clear last_input and set pointer_get to the same as pointer_insert.
m_input_history_last_input = "";
m_input_history_pointer_get = itr->second;
}
void
Root::set_input_history_size(int size) {
if (size < 1)
throw torrent::input_error("Invalid input history size.");
for (InputHistory::iterator itr = m_input_history.begin(), last = m_input_history.end(); itr != last; itr++) {
// Reserve the latest input history entries if new size is smaller than original.
if (size < m_input_history_length) {
int pointer_offset = m_input_history_length - size;
InputHistoryPointers::iterator pitr = m_input_history_pointers.find(itr->first);
InputHistoryCategory input_history_category_tmp = itr->second;
for (int i=0; i != size; i++)
itr->second.at(i) = input_history_category_tmp.at((pitr->second + pointer_offset + i) % m_input_history_length);
m_input_history_pointers[pitr->first] = 0;
}
itr->second.resize(size);
}
m_input_history_length = size;
}
void
Root::load_input_history() {
if (!m_control->core()->download_store()->is_enabled()) {
lt_log_print(torrent::LOG_DEBUG, "ignoring input history file");
return;
}
std::string history_filename = m_control->core()->download_store()->path() + "rtorrent.input_history";
std::fstream history_file(history_filename.c_str(), std::ios::in);
if (history_file.is_open()) {
// Create a temp object of the content since size of history categories can be smaller than this.
InputHistory input_history_tmp;
for (int type = ui::DownloadList::INPUT_LOAD_DEFAULT; type != ui::DownloadList::INPUT_EOI; type++)
input_history_tmp.insert( std::make_pair(type, InputHistoryCategory()) );
std::string line;
while (std::getline(history_file, line)) {
if (!line.empty()) {
int delim_pos = line.find("|");
if (delim_pos != std::string::npos) {
int type = std::atoi(line.substr(0, delim_pos).c_str());
InputHistory::iterator itr = input_history_tmp.find(type);
if (itr != input_history_tmp.end()) {
std::string input_str = rak::trim(line.substr(delim_pos + 1));
if (!input_str.empty())
itr->second.push_back(input_str);
}
}
}
}
if (history_file.bad()) {
lt_log_print(torrent::LOG_DEBUG, "input history file corrupted, discarding (path:%s)", history_filename.c_str());
return;
} else {
lt_log_print(torrent::LOG_DEBUG, "input history file read (path:%s)", history_filename.c_str());
}
for (InputHistory::const_iterator itr = input_history_tmp.begin(), last = input_history_tmp.end(); itr != last; itr++) {
int input_history_tmp_category_length = itr->second.size();
InputHistory::iterator hitr = m_input_history.find(itr->first);
InputHistoryPointers::iterator pitr = m_input_history_pointers.find(itr->first);
if (m_input_history_length < input_history_tmp_category_length) {
int pointer_offset = input_history_tmp_category_length - m_input_history_length;
for (int i=0; i != m_input_history_length; i++)
hitr->second.at(i) = itr->second.at((pointer_offset + i) % input_history_tmp_category_length);
pitr->second = 0;
} else {
hitr->second = itr->second;
hitr->second.resize(m_input_history_length);
pitr->second = input_history_tmp_category_length % m_input_history_length;
}
}
} else {
lt_log_print(torrent::LOG_DEBUG, "could not open input history file (path:%s)", history_filename.c_str());
}
}
void
Root::save_input_history() {
if (!m_control->core()->download_store()->is_enabled())
return;
std::string history_filename = m_control->core()->download_store()->path() + "rtorrent.input_history";
std::string history_filename_tmp = history_filename + ".new";
std::fstream history_file(history_filename_tmp.c_str(), std::ios::out | std::ios::trunc);
if (!history_file.is_open()) {
lt_log_print(torrent::LOG_DEBUG, "could not open input history file for writing (path:%s)", history_filename.c_str());
return;
}
for (InputHistory::const_iterator itr = m_input_history.begin(), last = m_input_history.end(); itr != last; itr++) {
InputHistoryPointers::const_iterator pitr = m_input_history_pointers.find(itr->first);
for (int i=0; i != m_input_history_length; i++)
if (!itr->second.at((pitr->second + i) % m_input_history_length).empty())
history_file << itr->first << "|" + itr->second.at((pitr->second + i) % m_input_history_length) + "\n";
}
if (!history_file.good()) {
lt_log_print(torrent::LOG_DEBUG, "input history file corrupted during writing, discarding (path:%s)", history_filename.c_str());
return;
} else {
lt_log_print(torrent::LOG_DEBUG, "input history file written (path:%s)", history_filename.c_str());
}
history_file.close();
std::rename(history_filename_tmp.c_str(), history_filename.c_str());
}
void
Root::clear_input_history() {
for (int type = ui::DownloadList::INPUT_LOAD_DEFAULT; type != ui::DownloadList::INPUT_EOI; type++) {
InputHistory::iterator itr = m_input_history.find(type);
for (int i=0; i != m_input_history_length; i++)
itr->second.at(i) = "";
m_input_history_pointers[type] = 0;
}
}
}