gwget r613 - in trunk: . src



Author: davidsf
Date: Fri Feb  6 19:24:58 2009
New Revision: 613
URL: http://svn.gnome.org/viewvc/gwget?rev=613&view=rev

Log:
2009-02-06  David SedeÃo <david alderia com>

        Patch from Michael Fotsch <foetsch yahoo com> with:
        * src/gwget_data.c: Support of mp3 playlist: When gwget finishes
        downloading an .m3u file, it asks the user whether it should download
        the files that it contains. If yes, the files will be added as
        additional downloads.
        * src/wget_data.c: Parser for wget version 1.11.4
        * Moved check_url function to utils.{.c.h}
        * In several places, leading and trailing whitespace is stripped from
        the user-specified URL. (This is a common pitfall when inadvertently
        copying leading spaces or trailing newlines into the clipboard.)



Modified:
   trunk/ChangeLog
   trunk/src/gwget_data.c
   trunk/src/new_window.c
   trunk/src/new_window.h
   trunk/src/utils.c
   trunk/src/utils.h
   trunk/src/wget-log.c

Modified: trunk/src/gwget_data.c
==============================================================================
--- trunk/src/gwget_data.c	(original)
+++ trunk/src/gwget_data.c	Fri Feb  6 19:24:58 2009
@@ -17,6 +17,7 @@
 #define _FILE_OFFSET_BITS  64
 #include <config.h>
 #include <gnome.h>
+#include <glib/gstdio.h>
 #include <libgnomevfs/gnome-vfs.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -43,6 +44,8 @@
 static void convert_time2str(gchar *buffer, guint32 time);
 static void gwget_gnotify_finished(GwgetData *gwgetdata);
 static void gwget_download_finished(GwgetData *gwgetdata);
+static gboolean gwget_ask_download_playlist(GwgetData *gwgetdata);
+static void gwget_download_playlist_items(gchar *filename);
 
 static void
 convert_time2str (gchar *buffer, guint32 time)
@@ -780,9 +783,53 @@
 static void
 gwget_download_finished (GwgetData *gwgetdata)
 {
-   gwget_gnotify_finished(gwgetdata);
-   gwget_tray_notify (_("Download Complete"), gwgetdata->filename, gwgetdata->icon_name);
-   gtk_widget_set_sensitive(glade_xml_get_widget(xml, "clear_button"), TRUE);
+	gwget_gnotify_finished(gwgetdata);
+	gwget_tray_notify (_("Download Complete"), gwgetdata->filename, gwgetdata->icon_name);
+	gtk_widget_set_sensitive(glade_xml_get_widget(xml, "clear_button"), TRUE);
+
+	if (gwget_ask_download_playlist(gwgetdata))
+		gwget_download_playlist_items(gwgetdata->local_filename);
+}
+
+static gboolean
+gwget_ask_download_playlist(GwgetData *gwgetdata)
+{
+	gchar *msg;
+	gint response;
+
+	if (g_str_has_suffix(gwgetdata->filename, ".m3u")
+	    	|| g_str_has_suffix(gwgetdata->filename, ".M3U")) {   
+		msg = g_strdup_printf(
+			_("The file %s is an MP3 playlist.\nDownload the files that it contains?"),
+			gwgetdata->filename);
+		response = run_dialog(_("Download files in MP3 playlist?"),
+							  _(msg), _("No"), _("Yes"));
+		g_free(msg);
+		if (response == GTK_RESPONSE_OK)
+			return TRUE;
+	}
+	
+	return FALSE;
+}
+
+static void
+gwget_download_playlist_items(gchar *filename)
+{
+	FILE *f;
+	gchar line[1024];
+	GwgetData *gwgetdata;
+	
+	f = g_fopen(filename, "r");
+	if (f!=NULL) {
+		while (fgets(line, 1024, f)!=NULL) {
+			if (check_url("http://";, line) || check_url("ftp://";, line)) {
+				gwgetdata = gwget_data_new (g_strstrip(line));
+				gwget_data_add_download(gwgetdata);
+				gwget_data_start_download(gwgetdata);
+			}
+		}
+		fclose(f);
+	}
 }
 
 void

Modified: trunk/src/new_window.c
==============================================================================
--- trunk/src/new_window.c	(original)
+++ trunk/src/new_window.c	Fri Feb  6 19:24:58 2009
@@ -49,7 +49,7 @@
 	combo = glade_xml_get_widget (xml, "save_in_comboboxentry");
 	save_in_entry=GTK_ENTRY(GTK_BIN(combo)->child);
 	
-	url=(gchar *)(gtk_entry_get_text (GTK_ENTRY(glade_xml_get_widget(xml, "url_entry"))));
+	url=g_strstrip((gchar *)(gtk_entry_get_text (GTK_ENTRY(glade_xml_get_widget(xml, "url_entry")))));
 	
 	if (strcmp(url,"")) {
 		url = g_strdup(url);
@@ -85,17 +85,6 @@
 	gtk_widget_hide (window);
 }
 
-/* checks for data in clipboard: URL or not */
-int check_url( char *str1, char *str2 ) 
-{
-	int i;
-	for( i = 0; str1[i] != '\0'; i++ ) 
-	{
-		if( str1[i] != str2[i] ) return(0);
-	}
-	return(1);
-}
-
 void 
 create_new_window(void)
 {
@@ -106,7 +95,7 @@
 
 	clipboard = gtk_clipboard_get (GDK_NONE);
 	if (clipboard!=NULL) {
-		url = gtk_clipboard_wait_for_text (clipboard);
+		url = g_strstrip(gtk_clipboard_wait_for_text (clipboard));
 	}
 		
 	
@@ -114,11 +103,14 @@
 
 	/* if clipboards data is an URL, then leave url value as is, else -- empty string */
 	entry = GTK_ENTRY(glade_xml_get_widget (xml, "url_entry"));
-	if ( (url!=NULL) && !check_url( "http://";, url ) && !check_url( "ftp://";, url))
+	if ( (url!=NULL) && !check_url( "http://";, url ) && !check_url( "ftp://";, url)) {
+		g_free(url);
 		url = NULL;
+	}
 	
 	if (url != NULL) {	
 		gtk_entry_set_text(GTK_ENTRY(entry),url);
+		g_free(url);
 	}
 	
 	gtk_list_store_clear (GTK_LIST_STORE(save_in_model));

Modified: trunk/src/new_window.h
==============================================================================
--- trunk/src/new_window.h	(original)
+++ trunk/src/new_window.h	Fri Feb  6 19:24:58 2009
@@ -20,7 +20,6 @@
 
 
 void on_ok_button_clicked(GtkWidget *widget, gpointer data);
-int check_url(char *str1, char *str2);
 void create_new_window(void);
 void on_cancel_button_clicked(GtkWidget *widget,gpointer data);
 

Modified: trunk/src/utils.c
==============================================================================
--- trunk/src/utils.c	(original)
+++ trunk/src/utils.c	Fri Feb  6 19:24:58 2009
@@ -32,9 +32,12 @@
 			GTK_DIALOG_MODAL,
 			GTK_MESSAGE_QUESTION,
 			GTK_BUTTONS_NONE,
-			g_strdup_printf("<span weight='bold' size='large'>%s</span>", title));
-			
-	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), message);
+			"<span weight='bold' size='large'>%s</span>",
+			title);
+
+	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", message);
+		/* message may contain '%' characters, so we must not use it
+		 * as the format string directly. */
 	
 	gtk_dialog_add_buttons (GTK_DIALOG(dialog), cancel_message, GTK_RESPONSE_CANCEL,
 			action_message, GTK_RESPONSE_OK, NULL);
@@ -80,6 +83,16 @@
     gtk_widget_show (dialog);
 }
 
+int check_url( char *str1, char *str2 ) 
+{
+	int i;
+	for( i = 0; str1[i] != '\0'; i++ ) 
+	{
+		if( str1[i] != str2[i] ) return(0);
+	}
+	return(1);
+}
+
 gboolean check_url_already_exists(gchar *checkurl)
 {
 	GwgetData* gwgetdata;

Modified: trunk/src/utils.h
==============================================================================
--- trunk/src/utils.h	(original)
+++ trunk/src/utils.h	Fri Feb  6 19:24:58 2009
@@ -23,6 +23,7 @@
 gint run_dialog(gchar *title, gchar *message, gchar *cancel_message, gchar *action_message);
 void run_dialog_error(gchar *title, gchar *msg);
 gint run_dialog_information(gchar *title, gchar *msg);
+int check_url(char *str1, char *str2);
 gboolean check_url_already_exists(gchar *checkurl);
 gboolean check_server_already_exists(gchar *checkurl);
 

Modified: trunk/src/wget-log.c
==============================================================================
--- trunk/src/wget-log.c	(original)
+++ trunk/src/wget-log.c	Fri Feb  6 19:24:58 2009
@@ -15,6 +15,12 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
+ 
+/*
+ * For the log parsing to work, the wget process must be started with
+ * the "C" locale, e.g., by setting the environment variable LC_ALL=C
+ * programmatically prior to invoking wget.
+ */
 
 #define _FILE_OFFSET_BITS 64
 
@@ -61,45 +67,65 @@
 	gwgetdata->error_msg=g_strdup(error_msg);
 }
 
+/* Get session start time and session file start size */
+static void
+set_start_size_and_time(GwgetData *gwgetdata)
+{
+	struct stat file_stat;
+	if (stat (gwgetdata->local_filename, &file_stat) != -1) {
+    	gwgetdata->session_start_time = file_stat.st_ctime;
+    	gwgetdata->session_start_size = file_stat.st_size;
+	} else {
+    	gwgetdata->session_start_time = 0;
+    	gwgetdata->session_start_size = 0;
+	}
+}
+
+/* 
+ * If gwgetdata->filename does not match the filesystem filename,
+ * bad things can happen. We intecept the line that prints the 
+ * filesystem filename and set gwgetdata->filename
+ */
+static void
+update_filename(GwgetData *gwgetdata)
+{
+	char *sName = gwgetdata->line;
+	int iL = strlen(sName);
+	sName[iL-1] = 0; // Chop the final ' 
+
+	/*
+	 * Now sName contains the whole pathname. No filename can
+	 * contain '/' so the following search for the last component
+	 * is sane
+	 */
+	sName += iL - 2;
+	while (*sName != '/' && sName != gwgetdata->line)
+		sName--;
+	if (*sName == '/')
+		sName++;
+
+	gwget_data_set_filename(gwgetdata,sName);
+	set_start_size_and_time(gwgetdata);
+	gwget_data_update_statistics(gwgetdata);
+	gwget_remember_downloads();
+}
+
 static int
 wget_log_process_line (GwgetData *gwgetdata)
 {
 	gchar *p;
-	struct stat file_stat;
 	gint dots = 0;
 
 	if ((gwgetdata->line == NULL) || (strlen (gwgetdata->line) == 0))
 		return 0;
-	
+		
 	switch (gwgetdata->state) {
 	case DL_NOT_CONNECTED:
-		/* 
-		 * If gwgetdata->filename does not match the filesystem filename,
-		 * bad things can happen. We intecept the line that prints the 
-		 * filesystem filename and set gwgetdata->filename
-		 */
-		if (strstr(gwgetdata->line,"           => `")) {
-			char *sName = gwgetdata->line;
-			int iL = strlen(sName);
-			sName[iL-1] = 0; // Chop the final ' 
-
-			/*
-			 * Now sName contains the whole pathname. No filename can
-			 * contain '/' so the following search for the last component
-			 * is sane
-			 */
-			sName += iL - 2;
-			while (*sName != '/' && sName != gwgetdata->line)
-				sName--;
-			if (*sName == '/')
-				sName++;
-
-			gwget_data_set_filename(gwgetdata,sName);
-			gwget_data_update_statistics(gwgetdata);
-			gwget_remember_downloads();
-			break;
-		}
-		  
+		if (strstr(gwgetdata->line,"           => `")) {		/* wget <1.11 */
+		    update_filename(gwgetdata);
+		    break;
+    	}
+   
 		/* First check to see if we connected to the host correctly. */
 
 		/* Wget 1.8.1 says "connected." rather than "connected!" */
@@ -249,14 +275,7 @@
 			}
 			gwget_data_set_state (gwgetdata, DL_RETRIEVING);
 			set_icon_newdownload();
-			/* Get session start time and session file start size */
-			if (stat (gwgetdata->local_filename, &file_stat) != -1) {
-		    	gwgetdata->session_start_time = file_stat.st_ctime;
-		    	gwgetdata->session_start_size = file_stat.st_size;
-			} else {
-		    	gwgetdata->session_start_time = 0;
-		    	gwgetdata->session_start_size = 0;
-			}
+			set_start_size_and_time(gwgetdata);
 			gwgetdata->session_elapsed = 0;
 	    } else {
 				/* We didn't get a length so, probably it's unspecified size
@@ -267,19 +286,18 @@
 					/* Unspecified size, so set total_size to 0 */
 					gwget_data_set_total_size (gwgetdata, 0);
 					gwget_data_set_state (gwgetdata, DL_RETRIEVING);
-					if (stat (gwgetdata->local_filename, &file_stat) != -1) {
-						gwgetdata->session_start_time = file_stat.st_ctime;
-						gwgetdata->session_start_size = file_stat.st_size;
-					} else {
-						gwgetdata->session_start_time = 0;
-						gwgetdata->session_start_size = 0;
-					}
+					set_start_size_and_time(gwgetdata);
 				}
 				gwgetdata->session_elapsed = 0;
         }
 	    break;
 
 		case DL_RETRIEVING:
+			if (strstr(gwgetdata->line,"Saving to: `")) {		/* wget >=1.11 */
+			    update_filename(gwgetdata);
+			    break;
+	    	}
+
 			if (strncmp (gwgetdata->line, "Cannot write to ", 15) == 0) {
 			show_error (gwgetdata,
 					    _ ("Can't write to target directory"));
@@ -294,14 +312,7 @@
 				if (p != NULL) {
 					p += 8;
 					gwget_data_set_total_size (gwgetdata,convert_wget_size (p));
-					/* Get session start time and session file start size */
-					if (stat (gwgetdata->local_filename, &file_stat) != -1) {
-						gwgetdata->session_start_time = file_stat.st_ctime;
-						gwgetdata->session_start_size = file_stat.st_size;
-					} else {
-						gwgetdata->session_start_time = 0;
-						gwgetdata->session_start_size = 0;
-					}
+					set_start_size_and_time(gwgetdata);
 					gwgetdata->session_elapsed = 0;
 				} else {
 		                	/* We didn't get a length so, probably it's unspecified size
@@ -311,13 +322,7 @@
 					if (p != NULL) {
 						/* Unspecified size, so set total_size to 0 */
 						gwget_data_set_total_size (gwgetdata, 0);
-						if (stat (gwgetdata->local_filename, &file_stat) != -1) {
-							gwgetdata->session_start_time = file_stat.st_ctime;
-							gwgetdata->session_start_size = file_stat.st_size;
-						} else {
-							gwgetdata->session_start_time = 0;
-							gwgetdata->session_start_size = 0;
-						}
+						set_start_size_and_time(gwgetdata);
 					}
 					gwgetdata->session_elapsed = 0;
 				}
@@ -418,10 +423,7 @@
 void
 wget_drain_remaining_log(GwgetData *gwgetdata) 
 {
-	while (wget_log_read_log_line (gwgetdata))
-		wget_log_process_line (gwgetdata);
-	
-	gwget_data_update_statistics (gwgetdata);
+	wget_log_process (gwgetdata);
 }
 
 void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]