mirror of
https://git.deluge-torrent.org/deluge
synced 2025-04-20 11:35:49 +00:00
[Console] Refactor quit code and cleanup conn mgr
This commit is contained in:
parent
d474576104
commit
15e0e0f30a
8 changed files with 57 additions and 84 deletions
|
@ -10,9 +10,7 @@
|
|||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from twisted.internet import error, reactor
|
||||
|
||||
from deluge.ui.client import client
|
||||
import deluge.component as component
|
||||
|
||||
from . import BaseCommand
|
||||
|
||||
|
@ -23,12 +21,4 @@ class Command(BaseCommand):
|
|||
interactive_only = True
|
||||
|
||||
def handle(self, options):
|
||||
if client.connected():
|
||||
def on_disconnect(result):
|
||||
reactor.stop()
|
||||
return client.disconnect().addCallback(on_disconnect)
|
||||
else:
|
||||
try:
|
||||
reactor.stop()
|
||||
except error.ReactorNotRunning:
|
||||
pass
|
||||
component.get('ConsoleUI').quit()
|
||||
|
|
|
@ -16,7 +16,7 @@ import os
|
|||
import sys
|
||||
import time
|
||||
|
||||
from twisted.internet import defer, reactor
|
||||
from twisted.internet import defer, error, reactor
|
||||
|
||||
import deluge.common
|
||||
import deluge.component as component
|
||||
|
@ -155,6 +155,17 @@ Please use commands from the command line, e.g.:\n
|
|||
import curses.wrapper
|
||||
curses.wrapper(self.run)
|
||||
|
||||
def quit(self):
|
||||
if client.connected():
|
||||
def on_disconnect(result):
|
||||
reactor.stop()
|
||||
return client.disconnect().addCallback(on_disconnect)
|
||||
else:
|
||||
try:
|
||||
reactor.stop()
|
||||
except error.ReactorNotRunning:
|
||||
pass
|
||||
|
||||
def exec_args(self, options):
|
||||
"""Execute console commands from command line."""
|
||||
from deluge.ui.console.cmdline.command import Commander
|
||||
|
|
|
@ -450,16 +450,9 @@ class AddTorrents(BaseMode):
|
|||
self.refresh()
|
||||
return
|
||||
|
||||
if util.is_printable_char(c):
|
||||
if util.is_printable_chr(c):
|
||||
if chr(c) == 'Q':
|
||||
from twisted.internet import reactor
|
||||
if client.connected():
|
||||
def on_disconnect(result):
|
||||
reactor.stop()
|
||||
client.disconnect().addCallback(on_disconnect)
|
||||
else:
|
||||
reactor.stop()
|
||||
return
|
||||
component.get('ConsoleUI').quit()
|
||||
elif chr(c) == 'q':
|
||||
self.back_to_overview()
|
||||
return
|
||||
|
@ -487,7 +480,7 @@ class AddTorrents(BaseMode):
|
|||
elif c == util.KEY_ESC:
|
||||
self.back_to_overview()
|
||||
else:
|
||||
if util.is_printable_char(c):
|
||||
if util.is_printable_chr(c):
|
||||
if chr(c) == 'h':
|
||||
self.popup = MessagePopup(self, 'Help', HELP_STR, width_req=0.75)
|
||||
elif chr(c) == '>':
|
||||
|
|
|
@ -13,8 +13,8 @@ import logging
|
|||
|
||||
import deluge.component as component
|
||||
from deluge.decorators import overrides
|
||||
from deluge.ui.client import client
|
||||
from deluge.ui.console.modes.basemode import BaseMode
|
||||
from deluge.ui.console.utils.curses_util import is_printable_chr
|
||||
from deluge.ui.console.widgets.popup import InputPopup, PopupsHandler, SelectablePopup
|
||||
from deluge.ui.hostlist import HostList
|
||||
|
||||
|
@ -38,14 +38,13 @@ class ConnectionManager(BaseMode, PopupsHandler):
|
|||
self.update_select_host_popup()
|
||||
|
||||
def update_select_host_popup(self):
|
||||
selected_index = None
|
||||
if self.popup:
|
||||
selected_index = self.popup.current_selection()
|
||||
selected_index = self.popup.current_selection() if self.popup else None
|
||||
|
||||
popup = SelectablePopup(self, _('Select Host'), self._host_selected, border_off_west=1, active_wrap=True)
|
||||
popup.add_header("{!white,black,bold!}'Q'=%s, 'a'=%s, 'D'=%s" %
|
||||
(_('Quit'), _('Add New Host'), _('Delete Host')),
|
||||
space_below=True)
|
||||
popup = SelectablePopup(
|
||||
self, _('Select Host'), self._host_selected, border_off_west=1, active_wrap=True)
|
||||
popup.add_header(
|
||||
"{!white,black,bold!}'Q'=%s, 'a'=%s, 'D'=%s" %
|
||||
(_('Quit'), _('Add Host'), _('Delete Host')), space_below=True)
|
||||
self.push_popup(popup, clear=True)
|
||||
|
||||
for host_entry in self.hostlist.get_hosts_info():
|
||||
|
@ -58,25 +57,22 @@ class ConnectionManager(BaseMode, PopupsHandler):
|
|||
host_str = '%s:%d [%s]' % (hostname, port, state)
|
||||
self.popup.add_line(host_id, host_str, selectable=True, use_underline=True, **args)
|
||||
|
||||
if selected_index is not None:
|
||||
if selected_index:
|
||||
self.popup.set_selection(selected_index)
|
||||
self.inlist = True
|
||||
self.refresh()
|
||||
|
||||
def update_hosts_status(self):
|
||||
|
||||
for host_entry in self.hostlist.get_hosts_info():
|
||||
def on_host_status(status_info):
|
||||
self.statuses[status_info[0]] = status_info
|
||||
self.update_select_host_popup()
|
||||
|
||||
self.hostlist.get_host_status(host_entry[0]).addCallback(on_host_status)
|
||||
|
||||
def _on_connected(self, result):
|
||||
d = component.get('ConsoleUI').start_console()
|
||||
|
||||
def on_console_start(result):
|
||||
component.get('ConsoleUI').set_mode('TorrentList')
|
||||
d = component.get('ConsoleUI').start_console()
|
||||
d.addCallback(on_console_start)
|
||||
|
||||
def _on_connect_fail(self, result):
|
||||
|
@ -100,9 +96,12 @@ class ConnectionManager(BaseMode, PopupsHandler):
|
|||
|
||||
def add_popup(self):
|
||||
self.inlist = False
|
||||
popup = InputPopup(self, _('Add Host (Up & Down arrows to navigate, Esc to cancel)'),
|
||||
border_off_north=1, border_off_east=1,
|
||||
close_cb=self._do_add)
|
||||
popup = InputPopup(
|
||||
self,
|
||||
_('Add Host (Up & Down arrows to navigate, Esc to cancel)'),
|
||||
border_off_north=1,
|
||||
border_off_east=1,
|
||||
close_cb=self._do_add)
|
||||
popup.add_text_input('hostname', _('Hostname:'))
|
||||
popup.add_text_input('port', _('Port:'))
|
||||
popup.add_text_input('username', _('Username:'))
|
||||
|
@ -111,16 +110,16 @@ class ConnectionManager(BaseMode, PopupsHandler):
|
|||
self.refresh()
|
||||
|
||||
def add_host(self, hostname, port, username, password):
|
||||
log.info('Adding host: %s', hostname)
|
||||
try:
|
||||
self.hostlist.add_host(hostname, port, username, password)
|
||||
except ValueError as ex:
|
||||
self.report_message(_('Error adding host'), '%s' % ex)
|
||||
return
|
||||
self.report_message(_('Error adding host'), '%s: %s' % (hostname, ex))
|
||||
else:
|
||||
self.update_select_host_popup()
|
||||
|
||||
def delete_host(self, host_id):
|
||||
log.debug('deleting host: %s', host_id)
|
||||
log.info('Deleting host: %s', host_id)
|
||||
self.hostlist.remove_host(host_id)
|
||||
self.update_select_host_popup()
|
||||
|
||||
|
@ -171,28 +170,21 @@ class ConnectionManager(BaseMode, PopupsHandler):
|
|||
def read_input(self):
|
||||
c = self.stdscr.getch()
|
||||
|
||||
if c > 31 and c < 256:
|
||||
if chr(c) == 'q' and self.inlist:
|
||||
return
|
||||
if is_printable_chr(c):
|
||||
if chr(c) == 'Q':
|
||||
from twisted.internet import reactor
|
||||
if client.connected():
|
||||
def on_disconnect(result):
|
||||
reactor.stop()
|
||||
client.disconnect().addCallback(on_disconnect)
|
||||
else:
|
||||
reactor.stop()
|
||||
return
|
||||
if chr(c) == 'D' and self.inlist:
|
||||
host_id = self.popup.current_selection()[1]
|
||||
self.delete_host(host_id)
|
||||
return
|
||||
if chr(c) == 'a' and self.inlist:
|
||||
self.add_popup()
|
||||
return
|
||||
component.get('ConsoleUI').quit()
|
||||
elif self.inlist:
|
||||
if chr(c) == 'q':
|
||||
return
|
||||
elif chr(c) == 'D':
|
||||
host_id = self.popup.current_selection()[1]
|
||||
self.delete_host(host_id)
|
||||
return
|
||||
elif chr(c) == 'a':
|
||||
self.add_popup()
|
||||
return
|
||||
|
||||
if self.popup:
|
||||
if self.popup.handle_read(c) and self.popup.closed():
|
||||
self.pop_popup()
|
||||
self.refresh()
|
||||
return
|
||||
|
|
|
@ -292,17 +292,11 @@ class Preferences(BaseMode, PopupsHandler):
|
|||
self.refresh()
|
||||
return
|
||||
|
||||
if util.is_printable_char(c):
|
||||
if chr(c) == 'Q':
|
||||
from twisted.internet import reactor
|
||||
if client.connected():
|
||||
def on_disconnect(result):
|
||||
reactor.stop()
|
||||
client.disconnect().addCallback(on_disconnect)
|
||||
else:
|
||||
reactor.stop()
|
||||
return
|
||||
elif chr(c) == 'h':
|
||||
if util.is_printable_chr(c):
|
||||
char = chr(c)
|
||||
if char == 'Q':
|
||||
component.get('ConsoleUI').quit()
|
||||
elif char == 'h':
|
||||
self.push_popup(MessagePopup(self, 'Preferences Help', HELP_STR))
|
||||
|
||||
if self.sidebar.has_focus() and c == util.KEY_ESC:
|
||||
|
|
|
@ -259,16 +259,9 @@ class TorrentList(BaseMode, PopupsHandler):
|
|||
self.pop_popup()
|
||||
self.refresh()
|
||||
return ret
|
||||
if util.is_printable_char(c):
|
||||
if util.is_printable_chr(c):
|
||||
if chr(c) == 'Q':
|
||||
from twisted.internet import reactor
|
||||
if client.connected():
|
||||
def on_disconnect(result):
|
||||
reactor.stop()
|
||||
client.disconnect().addCallback(on_disconnect)
|
||||
else:
|
||||
reactor.stop()
|
||||
return
|
||||
component.get('ConsoleUI').quit()
|
||||
elif chr(c) == 'C':
|
||||
self.consoleui.set_mode('ConnectionManager')
|
||||
return
|
||||
|
@ -312,7 +305,7 @@ class TorrentList(BaseMode, PopupsHandler):
|
|||
self.show_torrent_details(tid)
|
||||
return
|
||||
|
||||
elif util.is_printable_char(c):
|
||||
elif util.is_printable_chr(c):
|
||||
if chr(c) == 'a':
|
||||
show_torrent_add_popup(self)
|
||||
elif chr(c) == 'v':
|
||||
|
|
|
@ -31,7 +31,7 @@ KEY_CTRL_AND_ARROW_UP = 566
|
|||
KEY_CTRL_AND_ARROW_DOWN = 525
|
||||
|
||||
|
||||
def is_printable_char(c):
|
||||
def is_printable_chr(c):
|
||||
return c >= 32 and c <= 126
|
||||
|
||||
|
||||
|
|
|
@ -849,7 +849,7 @@ class ComboInput(InputField):
|
|||
|
||||
def search_handler(key):
|
||||
"""Handle keyboard input to seach the list"""
|
||||
if not util.is_printable_char(key):
|
||||
if not util.is_printable_chr(key):
|
||||
return
|
||||
selected = select_popup.current_selection()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue