summaryrefslogtreecommitdiffstats
path: root/deluge/ui/console/modes/torrentlist/queue_mode.py
blob: f101cea6994ca7cbe935956201ba80b465f1494e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011 Nick Lanham <nick@afternight.org>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#

from deluge.ui.client import client
from deluge.ui.console.utils import curses_util as util
from deluge.ui.console.widgets.popup import MessagePopup, SelectablePopup

from . import ACTION

try:
    import curses
except ImportError:
    pass

key_to_action = {
    curses.KEY_HOME: ACTION.QUEUE_TOP,
    curses.KEY_UP: ACTION.QUEUE_UP,
    curses.KEY_DOWN: ACTION.QUEUE_DOWN,
    curses.KEY_END: ACTION.QUEUE_BOTTOM,
}
QUEUE_MODE_HELP_STR = """
Change queue position of selected torrents

{!info!}'+'{!normal!} - {|indent_pos:|}Move up
{!info!}'-'{!normal!} - {|indent_pos:|}Move down

{!info!}'Home'{!normal!} - {|indent_pos:|}Move to top
{!info!}'End'{!normal!} - {|indent_pos:|}Move to bottom

"""


class QueueMode(object):
    def __init__(self, torrentslist, torrent_ids):
        self.torrentslist = torrentslist
        self.torrentview = torrentslist.torrentview
        self.torrent_ids = torrent_ids

    def set_statusbar_args(self, statusbar_args):
        statusbar_args[
            'bottombar'
        ] = '{!black,white!}Queue mode: change queue position of selected torrents.'
        statusbar_args['bottombar_help'] = ' Press [h] for help'

    def update_cursor(self):
        pass

    def update_colors(self, tidx, colors):
        pass

    def handle_read(self, c):
        if c in [util.KEY_ESC, util.KEY_BELL]:  # If Escape key or CTRL-g, we abort
            self.torrentslist.set_minor_mode(None)
        elif c == ord('h'):
            popup = MessagePopup(
                self.torrentslist,
                'Help',
                QUEUE_MODE_HELP_STR,
                width_req=0.65,
                border_off_west=1,
            )
            self.torrentslist.push_popup(popup, clear=True)
        elif c in [
            curses.KEY_UP,
            curses.KEY_DOWN,
            curses.KEY_HOME,
            curses.KEY_END,
            curses.KEY_NPAGE,
            curses.KEY_PPAGE,
        ]:
            action = key_to_action[c]
            self.do_queue(action)

    def move_selection(self, cb_arg, qact):
        if self.torrentslist.config['torrentview']['move_selection'] is False:
            return
        queue_length = 0
        selected_num = 0
        for tid in self.torrentview.curstate:
            tq = self.torrentview.curstate[tid]['queue']
            if tq != -1:
                queue_length += 1
                if tq in self.torrentview.marked:
                    selected_num += 1
        if qact == ACTION.QUEUE_TOP:
            if self.torrentview.marked:
                self.torrentview.cursel = 1 + sorted(self.torrentview.marked).index(
                    self.torrentview.cursel
                )
            else:
                self.torrentview.cursel = 1
            self.torrentview.marked = list(range(1, selected_num + 1))
        elif qact == ACTION.QUEUE_UP:
            self.torrentview.cursel = max(1, self.torrentview.cursel - 1)
            self.torrentview.marked = [marked - 1 for marked in self.torrentview.marked]
            self.torrentview.marked = [
                marked for marked in self.torrentview.marked if marked > 0
            ]
        elif qact == ACTION.QUEUE_DOWN:
            self.torrentview.cursel = min(queue_length, self.torrentview.cursel + 1)
            self.torrentview.marked = [marked + 1 for marked in self.torrentview.marked]
            self.torrentview.marked = [
                marked for marked in self.torrentview.marked if marked <= queue_length
            ]
        elif qact == ACTION.QUEUE_BOTTOM:
            if self.torrentview.marked:
                self.torrentview.cursel = (
                    queue_length
                    - selected_num
                    + 1
                    + sorted(self.torrentview.marked).index(self.torrentview.cursel)
                )
            else:
                self.torrentview.cursel = queue_length
            self.torrentview.marked = list(
                range(queue_length - selected_num + 1, queue_length + 1)
            )

    def do_queue(self, qact, *args, **kwargs):
        if qact == ACTION.QUEUE_TOP:
            client.core.queue_top(self.torrent_ids).addCallback(
                self.move_selection, qact
            )
        elif qact == ACTION.QUEUE_BOTTOM:
            client.core.queue_bottom(self.torrent_ids).addCallback(
                self.move_selection, qact
            )
        elif qact == ACTION.QUEUE_UP:
            client.core.queue_up(self.torrent_ids).addCallback(
                self.move_selection, qact
            )
        elif qact == ACTION.QUEUE_DOWN:
            client.core.queue_down(self.torrent_ids).addCallback(
                self.move_selection, qact
            )

    def popup(self, **kwargs):
        popup = SelectablePopup(
            self.torrentslist,
            'Queue Action',
            self.do_queue,
            cb_args=kwargs,
            border_off_west=1,
        )
        popup.add_line(ACTION.QUEUE_TOP, '_Top')
        popup.add_line(ACTION.QUEUE_UP, '_Up')
        popup.add_line(ACTION.QUEUE_DOWN, '_Down')
        popup.add_line(ACTION.QUEUE_BOTTOM, '_Bottom')
        self.torrentslist.push_popup(popup)