1 /******************************************************************************
2  * Copyright (c) Transmission authors and contributors
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *****************************************************************************/
22 
23 #include <gtk/gtk.h>
24 #include <glib/gi18n.h>
25 
26 #include <libtransmission/transmission.h>
27 
28 #include "dialogs.h"
29 #include "tr-core.h"
30 
31 /***
32 ****
33 ***/
34 
35 struct delete_data
36 {
37     gboolean delete_files;
38     GSList* torrent_ids;
39     TrCore* core;
40 };
41 
on_remove_dialog_response(GtkDialog * dialog,gint response,gpointer gdd)42 static void on_remove_dialog_response(GtkDialog* dialog, gint response, gpointer gdd)
43 {
44     struct delete_data* dd = gdd;
45 
46     if (response == GTK_RESPONSE_ACCEPT)
47     {
48         for (GSList* l = dd->torrent_ids; l != NULL; l = l->next)
49         {
50             gtr_core_remove_torrent(dd->core, GPOINTER_TO_INT(l->data), dd->delete_files);
51         }
52     }
53 
54     gtk_widget_destroy(GTK_WIDGET(dialog));
55     g_slist_free(dd->torrent_ids);
56     g_free(dd);
57 }
58 
gtr_confirm_remove(GtkWindow * parent,TrCore * core,GSList * torrent_ids,gboolean delete_files)59 void gtr_confirm_remove(GtkWindow* parent, TrCore* core, GSList* torrent_ids, gboolean delete_files)
60 {
61     GtkWidget* d;
62     GString* primary_text;
63     GString* secondary_text;
64     struct delete_data* dd;
65     int connected = 0;
66     int incomplete = 0;
67     int const count = g_slist_length(torrent_ids);
68 
69     if (count == 0)
70     {
71         return;
72     }
73 
74     dd = g_new0(struct delete_data, 1);
75     dd->core = core;
76     dd->torrent_ids = torrent_ids;
77     dd->delete_files = delete_files;
78 
79     for (GSList* l = torrent_ids; l != NULL; l = l->next)
80     {
81         int const id = GPOINTER_TO_INT(l->data);
82         tr_torrent* tor = gtr_core_find_torrent(core, id);
83         tr_stat const* stat = tr_torrentStat(tor);
84 
85         if (stat->leftUntilDone != 0)
86         {
87             ++incomplete;
88         }
89 
90         if (stat->peersConnected != 0)
91         {
92             ++connected;
93         }
94     }
95 
96     primary_text = g_string_new(NULL);
97 
98     if (!delete_files)
99     {
100         g_string_printf(primary_text, ngettext("Remove torrent?", "Remove %d torrents?", count), count);
101     }
102     else
103     {
104         g_string_printf(primary_text, ngettext("Delete this torrent's downloaded files?",
105             "Delete these %d torrents' downloaded files?", count), count);
106     }
107 
108     secondary_text = g_string_new(NULL);
109 
110     if (incomplete == 0 && connected == 0)
111     {
112         g_string_assign(secondary_text,
113             ngettext("Once removed, continuing the transfer will require the torrent file or magnet link.",
114             "Once removed, continuing the transfers will require the torrent files or magnet links.", count));
115     }
116     else if (count == incomplete)
117     {
118         g_string_assign(secondary_text, ngettext("This torrent has not finished downloading.",
119             "These torrents have not finished downloading.", count));
120     }
121     else if (count == connected)
122     {
123         g_string_assign(secondary_text, ngettext("This torrent is connected to peers.",
124             "These torrents are connected to peers.", count));
125     }
126     else
127     {
128         if (connected != 0)
129         {
130             g_string_append(secondary_text, ngettext("One of these torrents is connected to peers.",
131                 "Some of these torrents are connected to peers.", connected));
132         }
133 
134         if (connected != 0 && incomplete != 0)
135         {
136             g_string_append(secondary_text, "\n");
137         }
138 
139         if (incomplete != 0)
140         {
141             g_string_assign(secondary_text, ngettext("One of these torrents has not finished downloading.",
142                 "Some of these torrents have not finished downloading.", incomplete));
143         }
144     }
145 
146     d = gtk_message_dialog_new_with_markup(parent, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
147         "<big><b>%s</b></big>", primary_text->str);
148 
149     if (secondary_text->len != 0)
150     {
151         gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(d), "%s", secondary_text->str);
152     }
153 
154     gtk_dialog_add_buttons(GTK_DIALOG(d), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
155         delete_files ? GTK_STOCK_DELETE : GTK_STOCK_REMOVE, GTK_RESPONSE_ACCEPT, NULL);
156     gtk_dialog_set_default_response(GTK_DIALOG(d), GTK_RESPONSE_CANCEL);
157     g_signal_connect(d, "response", G_CALLBACK(on_remove_dialog_response), dd);
158     gtk_widget_show_all(d);
159 
160     g_string_free(primary_text, TRUE);
161     g_string_free(secondary_text, TRUE);
162 }
163