File: dialog-new.c

package info (click to toggle)
gtm 0.4.4-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,732 kB
  • ctags: 680
  • sloc: ansic: 6,929; sh: 6,837; makefile: 354; sed: 93
file content (203 lines) | stat: -rw-r--r-- 7,577 bytes parent folder | download
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
/*
 *  Copyright (C) 1999 Bruno Pires Marinho
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <gnome.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "gtm_applet.h"
#include "dialogs.h"

/* Structure that keeps the widgets with the information of the new download
 * file */
typedef struct 
{
    GtkWidget *dlg;              /* GNOME dialog */
    GtkWidget *url;              /* Entry */
    GtkWidget *download_dir;     /* Entry */
    GtkWidget *disable_proxy;    /* Toggle button */
    GtkWidget *disable_auto_dl;  /* Check button */
} NewDlData;


/* Call back function used when the OK and CANCEL buttons get pressed */
static void
ok_new_file (GtkWidget *widget, NewDlData *new_dl_data)
{
    gchar *url, *dir, *filename;
    gint length;
    gboolean disable_proxy, disable_auto_dl;
    GtkWidget *error;

    /* Get the URL data and make sure it's right */
    url = g_strdup (gtk_entry_get_text (GTK_ENTRY (new_dl_data->url)));
    if (strncmp (url, "http://", 7) && strncmp (url, "ftp://", 6)) {
	error = gnome_message_box_new (
	    _ ("Invalid URL"), GNOME_MESSAGE_BOX_ERROR, 
	    GNOME_STOCK_BUTTON_OK, NULL);
	gnome_dialog_set_parent (GNOME_DIALOG (error), 
                                 GTK_WINDOW (new_dl_data->dlg));
	gnome_dialog_run_and_close (GNOME_DIALOG (error));
        g_free (url);
	return;
    }

    /* Get download filename data */
    filename = &url[strlen (url)];
    while (*filename != '/' && filename != url)
	filename--;
    filename++;

    /* TODO: If filename == NULL instead of reminding the user that it is not
     * possible yet to do multiple downloads, when the group stuff is 
     * implemented this should enter a new dialog like dialog-multiple and 
     * ask there for download depth and get the available files to download. 
     * Then the user can remove what he doesn't want. */
    if (strlen (filename) == 0) {
	error = gnome_message_box_new (
	    _ ("Multiple download not implemented yet"), 
            GNOME_MESSAGE_BOX_ERROR, 
	    GNOME_STOCK_BUTTON_OK, NULL);
	gnome_dialog_set_parent (GNOME_DIALOG (error), 
                                 GTK_WINDOW (new_dl_data->dlg));
	gnome_dialog_run_and_close (GNOME_DIALOG (error));
        g_free (url);
	return;
    }

    /* Get download directory data */
    dir = gtk_entry_get_text (GTK_ENTRY (new_dl_data->download_dir));

    /* Get the disable proxy option data */
    disable_proxy =
        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (
            new_dl_data->disable_proxy));

    /* Get the disable auto download option data */
    disable_auto_dl = 
        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (
	    new_dl_data->disable_auto_dl));

    /* Add the new download file to the file list (if not a duplicate) */
    if (gtm_add_url (url, dir, disable_proxy, disable_auto_dl) == FALSE) {
        /* URL on the file list */
        gchar *error_msg;
        
        error_msg = g_strconcat (_ ("URL:\n"), "\t", url, "\n",
                                 _ ("already in the list"), NULL);
        error = gnome_message_box_new (error_msg, GNOME_MESSAGE_BOX_ERROR,
                                       GNOME_STOCK_BUTTON_OK, NULL);
        gnome_dialog_run_and_close (GNOME_DIALOG (error));
        g_free (error_msg);
    }
    
    /* If dialog new or add changes default download dir update the def dl 
     * dir */
    if (gtm_applet_prop.addnew_changes_def_dl_dir) {
        g_free (gtm_applet_prop.def_download_dir);
        length = strlen (dir);
        if (length != 0 && dir[length - 1] != '/')
            gtm_applet_prop.def_download_dir = g_strconcat (dir, "/", NULL);
        else
            gtm_applet_prop.def_download_dir = g_strdup (dir);
    }

    /* Destroy the dialog box and free unused that */
    gtk_widget_destroy (new_dl_data->dlg);
    g_free (url);
    g_free (new_dl_data);
}

static void
cancel_new_file (GtkWidget *widget, NewDlData *new_dl_data)
{
    /* Destroy the dialog box and free unused that */
    gtk_widget_destroy (new_dl_data->dlg);
    g_free (new_dl_data);
}


/* Dialog used to add a new file to download */
void
dialog_new (void)
{
    GtkWidget *label, *file_entry, *hbox1;
    NewDlData *new_dl_data;

    new_dl_data = g_new (NewDlData, 1);

    /* Create dialog box */
    new_dl_data->dlg = 
	gnome_dialog_new (_ ("GTM Applet - New file to download"),
			  GNOME_STOCK_BUTTON_OK, GNOME_STOCK_BUTTON_CANCEL,
			  NULL);

    /* Set it's contents */
    label = gtk_label_new (_ ("Please enter URL of the file to download:"));
    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (new_dl_data->dlg)->vbox), 
			label, FALSE, FALSE, 0);
    new_dl_data->url = gtk_entry_new ();
    gtk_widget_set_usize (new_dl_data->url, 300, -2);
    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (new_dl_data->dlg)->vbox), 
			new_dl_data->url, FALSE, FALSE, 0);
    gtk_widget_grab_focus (new_dl_data->url);

    label = gtk_label_new (_ ("Download dir:"));
    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (new_dl_data->dlg)->vbox), 
			label, FALSE, FALSE, 0);
    file_entry = gnome_file_entry_new ("NewDownloadDir", 
                                       _("GTM - New Download Dir"));
    new_dl_data->download_dir = 
	gnome_file_entry_gtk_entry (GNOME_FILE_ENTRY (file_entry));
    gnome_file_entry_set_directory (GNOME_FILE_ENTRY (file_entry), 1);
    gtk_entry_set_text (GTK_ENTRY (new_dl_data->download_dir), 
			gtm_applet_prop.def_download_dir);
    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (new_dl_data->dlg)->vbox),
			file_entry, FALSE, FALSE, 0);

    hbox1 = gtk_hbox_new(FALSE, FALSE);
    new_dl_data->disable_proxy = 
	gtk_check_button_new_with_label (_ ("Disable proxy"));
    gtk_box_pack_start (GTK_BOX (hbox1), new_dl_data->disable_proxy,
                        FALSE, FALSE, 0);
    new_dl_data->disable_auto_dl =
        gtk_check_button_new_with_label (_ ("Disable auto download"));
    gtk_box_pack_start (GTK_BOX (hbox1), new_dl_data->disable_auto_dl,
                        FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (new_dl_data->dlg)->vbox),
                        hbox1, FALSE, FALSE, 0);

    /* Configure the dialog box */
    gnome_dialog_set_close (GNOME_DIALOG (new_dl_data->dlg), FALSE);
    gnome_dialog_set_default (GNOME_DIALOG (new_dl_data->dlg), GNOME_OK);

    /* Set the signal handlers */
    gnome_dialog_button_connect (GNOME_DIALOG (new_dl_data->dlg), GNOME_OK,
				 GTK_SIGNAL_FUNC (ok_new_file),
				 (gpointer) new_dl_data);
    gnome_dialog_button_connect (GNOME_DIALOG (new_dl_data->dlg), GNOME_CANCEL,
				 GTK_SIGNAL_FUNC (cancel_new_file),
				 (gpointer) new_dl_data);
    gtk_signal_connect (GTK_OBJECT (new_dl_data->url), "activate",
			GTK_SIGNAL_FUNC (ok_new_file), 
			(gpointer) new_dl_data);

    /* Show the dialog box and every thing it contains */
    gtk_widget_show_all (new_dl_data->dlg);
}