1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2008 Andrew Resch <andrewresch@gmail.com>
4#
5# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
6# the additional special exception to link portions of this program with the OpenSSL library.
7# See LICENSE for more details.
8#
9
10from __future__ import unicode_literals
11
12import logging
13
14import deluge.component as component
15from deluge.common import ftime
16
17from .tab_data_funcs import fcount, ftranslate, fyes_no
18from .torrentdetails import Tab
19
20log = logging.getLogger(__name__)
21
22
23class TrackersTab(Tab):
24    def __init__(self):
25        super(TrackersTab, self).__init__(
26            'Trackers', 'trackers_tab', 'trackers_tab_label'
27        )
28
29        self.add_tab_widget('summary_next_announce', ftime, ('next_announce',))
30        self.add_tab_widget('summary_tracker', None, ('tracker_host',))
31        self.add_tab_widget('summary_tracker_status', ftranslate, ('tracker_status',))
32        self.add_tab_widget('summary_tracker_total', fcount, ('trackers',))
33        self.add_tab_widget('summary_private', fyes_no, ('private',))
34
35        component.get('MainWindow').connect_signals(self)
36
37    def update(self):
38        # Get the first selected torrent
39        selected = component.get('TorrentView').get_selected_torrents()
40
41        # Only use the first torrent in the list or return if None selected
42        if selected:
43            selected = selected[0]
44        else:
45            self.clear()
46            return
47
48        session = component.get('SessionProxy')
49        session.get_torrent_status(selected, self.status_keys).addCallback(
50            self._on_get_torrent_status
51        )
52
53    def _on_get_torrent_status(self, status):
54        # Check to see if we got valid data from the core
55        if not status:
56            return
57
58        # Update all the tab label widgets
59        for widget in self.tab_widgets.values():
60            txt = self.widget_status_as_fstr(widget, status)
61            if widget.obj.get_text() != txt:
62                widget.obj.set_text(txt)
63
64    def clear(self):
65        for widget in self.tab_widgets.values():
66            widget.obj.set_text('')
67
68    def on_button_edit_trackers_clicked(self, button):
69        torrent_id = component.get('TorrentView').get_selected_torrent()
70        if torrent_id:
71            from .edittrackersdialog import EditTrackersDialog
72
73            dialog = EditTrackersDialog(torrent_id, component.get('MainWindow').window)
74            dialog.run()
75