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