summaryrefslogtreecommitdiffstats
path: root/deluge/ui/gtk3/torrentview_data_funcs.py
blob: 8bd1f9c517f8fcc378c03ece36b1f2904892d8ac (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, 2008 Andrew Resch <andrewresch@gmail.com>
#
# 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 __future__ import print_function, unicode_literals

import warnings
from functools import partial

import deluge.common as common
import deluge.component as component

from .common import (
    create_blank_pixbuf,
    get_pixbuf_at_size,
    icon_alert,
    icon_checking,
    icon_downloading,
    icon_inactive,
    icon_queued,
    icon_seeding,
)

# Holds the info for which status icon to display based on TORRENT_STATE
ICON_STATE = {
    'Allocating': icon_checking,
    'Checking': icon_checking,
    'Downloading': icon_downloading,
    'Seeding': icon_seeding,
    'Paused': icon_inactive,
    'Error': icon_alert,
    'Queued': icon_queued,
    'Moving': icon_checking,
}

# Cache the key used to calculate the current value set for the specific cell
# renderer. This is much cheaper than fetch the current value and test if
# it's equal.
func_last_value = {
    'cell_data_time': None,
    'cell_data_ratio_seeds_peers': None,
    'cell_data_ratio_ratio': None,
    'cell_data_ratio_avail': None,
    'cell_data_date_added': None,
    'cell_data_date_completed': None,
    'cell_data_date_or_never': None,
    'cell_data_speed_limit_down': None,
    'cell_data_speed_limit_up': None,
    'cell_data_trackericon': None,
    'cell_data_statusicon': None,
    'cell_data_queue': None,
    'cell_data_progress': [None, None],
    'cell_data_peer_progress': None,
}


def cell_data_statusicon(column, cell, model, row, data):
    """Display text with an icon"""
    try:
        state = model.get_value(row, data)

        if func_last_value['cell_data_statusicon'] == state:
            return
        func_last_value['cell_data_statusicon'] = state

        icon = ICON_STATE[state]

        # Supress Warning: g_object_set_qdata: assertion `G_IS_OBJECT (object)' failed
        original_filters = warnings.filters[:]
        warnings.simplefilter('ignore')
        try:
            cell.set_property('pixbuf', icon)
        finally:
            warnings.filters = original_filters

    except KeyError:
        pass


def set_tracker_icon(tracker_icon, cell):
    if tracker_icon:
        pixbuf = tracker_icon.get_cached_icon()
        if pixbuf is None:
            pixbuf = get_pixbuf_at_size(tracker_icon.get_filename(), 16)
            tracker_icon.set_cached_icon(pixbuf)
    else:
        pixbuf = create_blank_pixbuf()

    # Suppress Warning: g_object_set_qdata: assertion `G_IS_OBJECT (object)' failed
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        cell.set_property('pixbuf', pixbuf)


def cell_data_trackericon(column, cell, model, row, data):
    host = model[row][data]

    if func_last_value['cell_data_trackericon'] == host:
        return
    if host:
        if not component.get('TrackerIcons').has(host):
            # Set blank icon while waiting for the icon to be loaded
            set_tracker_icon(None, cell)
            component.get('TrackerIcons').fetch(host)
            func_last_value['cell_data_trackericon'] = None
        else:
            set_tracker_icon(component.get('TrackerIcons').get(host), cell)
            # Only set the last value when we have found the icon
            func_last_value['cell_data_trackericon'] = host
    else:
        set_tracker_icon(None, cell)
        func_last_value['cell_data_trackericon'] = None


def cell_data_progress(column, cell, model, row, data):
    """Display progress bar with text"""
    (value, state_str) = model.get(row, *data)
    if func_last_value['cell_data_progress'][0] != value:
        func_last_value['cell_data_progress'][0] = value
        cell.set_property('value', value)

    # Marked for translate states text are in filtertreeview
    textstr = _(state_str)
    if state_str not in ('Error', 'Seeding') and value < 100:
        textstr = '%s %i%%' % (textstr, value)

    if func_last_value['cell_data_progress'][1] != textstr:
        func_last_value['cell_data_progress'][1] = textstr
        cell.set_property('text', textstr)


def cell_data_peer_progress(column, cell, model, row, data):
    value = model.get_value(row, data) * 100
    if func_last_value['cell_data_peer_progress'] != value:
        func_last_value['cell_data_peer_progress'] = value
        cell.set_property('value', value)
        cell.set_property('text', '%i%%' % value)


def cell_data_queue(column, cell, model, row, data):
    value = model.get_value(row, data)

    if func_last_value['cell_data_queue'] == value:
        return
    func_last_value['cell_data_queue'] = value

    if value < 0:
        cell.set_property('text', '')
    else:
        cell.set_property('text', str(value + 1))


def cell_data_speed(cell, model, row, data):
    """Display value as a speed, eg. 2 KiB/s"""
    speed = model.get_value(row, data)

    if speed > 0:
        speed_str = common.fspeed(speed, shortform=True)
        cell.set_property(
            'markup', '{0} <small>{1}</small>'.format(*tuple(speed_str.split()))
        )
    else:
        cell.set_property('text', '')


def cell_data_speed_down(column, cell, model, row, data):
    """Display value as a speed, eg. 2 KiB/s"""
    cell_data_speed(cell, model, row, data)


def cell_data_speed_up(column, cell, model, row, data):
    """Display value as a speed, eg. 2 KiB/s"""
    cell_data_speed(cell, model, row, data)


def cell_data_speed_limit(cell, model, row, data, cache_key):
    """Display value as a speed, eg. 2 KiB/s"""
    speed = model.get_value(row, data)

    if func_last_value[cache_key] == speed:
        return
    func_last_value[cache_key] = speed

    if speed > 0:
        speed_str = common.fspeed(speed * 1024, shortform=True)
        cell.set_property(
            'markup', '{0} <small>{1}</small>'.format(*tuple(speed_str.split()))
        )
    else:
        cell.set_property('text', '')


def cell_data_speed_limit_down(column, cell, model, row, data):
    cell_data_speed_limit(cell, model, row, data, 'cell_data_speed_limit_down')


def cell_data_speed_limit_up(column, cell, model, row, data):
    cell_data_speed_limit(cell, model, row, data, 'cell_data_speed_limit_up')


def cell_data_size(column, cell, model, row, data):
    """Display value in terms of size, eg. 2 MB"""
    size = model.get_value(row, data)
    cell.set_property('text', common.fsize(size, shortform=True))


def cell_data_peer(column, cell, model, row, data):
    """Display values as 'value1 (value2)'"""
    (first, second) = model.get(row, *data)
    # Only display a (total) if second is greater than -1
    if second > -1:
        cell.set_property('text', '%d (%d)' % (first, second))
    else:
        cell.set_property('text', '%d' % first)


def cell_data_time(column, cell, model, row, data):
    """Display value as time, eg 1m10s"""
    time = model.get_value(row, data)
    if func_last_value['cell_data_time'] == time:
        return
    func_last_value['cell_data_time'] = time

    if time <= 0:
        time_str = ''
    else:
        time_str = common.ftime(time)
    cell.set_property('text', time_str)


def cell_data_ratio(cell, model, row, data, cache_key):
    """Display value as a ratio with a precision of 2."""
    ratio = model.get_value(row, data)
    # Previous value in cell is the same as for this value, so ignore
    if func_last_value[cache_key] == ratio:
        return
    func_last_value[cache_key] = ratio
    cell.set_property(
        'text', '∞' if ratio < 0 else ('%.1f' % ratio).rstrip('0').rstrip('.')
    )


def cell_data_ratio_seeds_peers(column, cell, model, row, data):
    cell_data_ratio(cell, model, row, data, 'cell_data_ratio_seeds_peers')


def cell_data_ratio_ratio(column, cell, model, row, data):
    cell_data_ratio(cell, model, row, data, 'cell_data_ratio_ratio')


def cell_data_ratio_avail(column, cell, model, row, data):
    cell_data_ratio(cell, model, row, data, 'cell_data_ratio_avail')


def cell_data_date(column, cell, model, row, data, key):
    """Display value as date, eg 05/05/08"""
    date = model.get_value(row, data)

    if func_last_value[key] == date:
        return
    func_last_value[key] = date

    date_str = common.fdate(date, date_only=True) if date > 0 else ''
    cell.set_property('text', date_str)


cell_data_date_added = partial(cell_data_date, key='cell_data_date_added')
cell_data_date_completed = partial(cell_data_date, key='cell_data_date_completed')


def cell_data_date_or_never(column, cell, model, row, data):
    """Display value as date, eg 05/05/08 or Never"""
    value = model.get_value(row, data)

    if func_last_value['cell_data_date_or_never'] == value:
        return
    func_last_value['cell_data_date_or_never'] = value

    date_str = common.fdate(value, date_only=True) if value > 0 else _('Never')
    cell.set_property('text', date_str)