summaryrefslogtreecommitdiffstats
path: root/deluge/ui/gtkui/removetorrentdialog.py
blob: 5446eae3986687c137e63149b40059c4a4c1e242 (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
#
# removetorrentdialog.py
#
# Copyright (C) 2007, 2008 Andrew Resch <andrewresch@gmail.com>
#
# Deluge is free software.
#
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with deluge.    If not, write to:
# 	The Free Software Foundation, Inc.,
# 	51 Franklin Street, Fifth Floor
# 	Boston, MA  02110-1301, USA.
#
#    In addition, as a special exception, the copyright holders give
#    permission to link the code of portions of this program with the OpenSSL
#    library.
#    You must obey the GNU General Public License in all respects for all of
#    the code used other than OpenSSL. If you modify file(s) with this
#    exception, you may extend this exception to your version of the file(s),
#    but you are not obligated to do so. If you do not wish to do so, delete
#    this exception statement from your version. If you delete this exception
#    statement from all source files in the program, then also delete it here.
#
#

import os
import gtk
import logging

from deluge.ui.client import client
import deluge.common
import deluge.component as component

log = logging.getLogger(__name__)

class RemoveTorrentDialog(object):
    """
    This class is used to create and show a Remove Torrent Dialog.

    :param torrent_ids: the torrent_ids to remove
    :type torrent_ids: list of torrent_ids

    :raises TypeError: if `torrent_id` is not a sequence type
    :raises ValueError: if `torrent_id` contains no torrent_ids or is None

    """
    def __init__(self, torrent_ids):
        if type(torrent_ids) != list and type(torrent_ids) != tuple:
            raise TypeError("requires a list of torrent_ids")

        if not torrent_ids:
            raise ValueError("requires a list of torrent_ids")

        self.__torrent_ids = torrent_ids

        builder = gtk.Builder()
        builder.add_from_file(deluge.common.resource_filename(
            "deluge.ui.gtkui", os.path.join("glade", "remove_torrent_dialog.ui"))
        )

        self.__dialog = builder.get_object("remove_torrent_dialog")
        self.__dialog.set_transient_for(component.get("MainWindow").window)
        self.__dialog.set_title("")

        if len(self.__torrent_ids) > 1:
            # We need to pluralize the dialog
            label_title = builder.get_object("label_title")
            button_ok = builder.get_object("button_ok_label")
            button_data = builder.get_object("button_data_label")

            def pluralize_torrents(text):
                plural_torrent = _("Torrents")
                return text.replace("torrent", plural_torrent.lower()).replace("Torrent", plural_torrent)

            label_title.set_markup(pluralize_torrents(label_title.get_label()))
            button_ok.set_label(pluralize_torrents(button_ok.get_label()))
            button_data.set_label(pluralize_torrents(button_data.get_label()))

    def __remove_torrents(self, remove_data):
        # Unselect all to avoid issues with the selection changed event
        component.get("TorrentView").treeview.get_selection().unselect_all()

        for torrent_id in self.__torrent_ids:
            client.core.remove_torrent(torrent_id, remove_data)

    def run(self):
        """
        Shows the dialog and awaits for user input.  The user can select to
        remove the torrent(s) from the session with or without their data.
        """
        # Response IDs from the buttons
        RESPONSE_SESSION = 1
        RESPONSE_DATA = 2

        response = self.__dialog.run()
        if response == RESPONSE_SESSION:
            self.__remove_torrents(False)
        elif response == RESPONSE_DATA:
            self.__remove_torrents(True)

        self.__dialog.destroy()