changeset 9573:c8e0426c63ed

[gaim-migrate @ 10416] Make the right-click->save image for IM images use the 2.4 file chooser. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Thu, 22 Jul 2004 03:23:12 +0000
parents d066c21dcbef
children fd32ae5ee373
files src/gtkimhtml.c src/gtkimhtml.h src/gtkrequest.c
diffstat 3 files changed, 111 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/gtkimhtml.c	Thu Jul 22 03:21:21 2004 +0000
+++ b/src/gtkimhtml.c	Thu Jul 22 03:23:12 2004 +0000
@@ -2693,6 +2693,7 @@
 	im_image->mark = NULL;
 	im_image->filename = filename ? g_strdup(filename) : NULL;
 	im_image->id = id;
+	im_image->filesel = NULL;
 
 	g_object_ref(img);
 	return GTK_IMHTML_SCALABLE(im_image);
@@ -2724,34 +2725,24 @@
 	}
 }
 
-static void write_img_to_file(GtkWidget *w, GtkFileSelection *sel)
+static void
+image_save_yes_cb(GtkIMHtmlImage *image, const char *filename)
 {
-	const gchar *filename = gtk_file_selection_get_filename(sel);
-	gchar *dirname;
-	GtkIMHtmlImage *image = g_object_get_data(G_OBJECT(sel), "GtkIMHtmlImage");
 	gchar *type = NULL;
 	GError *error = NULL;
 #if GTK_CHECK_VERSION(2,2,0)
 	GSList *formats = gdk_pixbuf_get_formats();
 #else
+	GtkIMHtmlImage *image = g_object_get_data(G_OBJECT(sel), "GtkIMHtmlImage");
 	char *basename = g_path_get_basename(filename);
 	char *ext = strrchr(basename, '.');
 #endif
 
-	if (g_file_test(filename, G_FILE_TEST_IS_DIR)) {
-		/* append a / if needed */
-		if (filename[strlen(filename) - 1] != '/') {
-			dirname = g_strconcat(filename, "/", NULL);
-		} else {
-			dirname = g_strdup(filename);
-		}
-		gtk_file_selection_set_filename(sel, dirname);
-		g_free(dirname);
-		return;
-	}
+	gtk_widget_destroy(image->filesel);
+	image->filesel = NULL;
 
 #if GTK_CHECK_VERSION(2,2,0)
-	while(formats){
+	while (formats) {
 		GdkPixbufFormat *format = formats->data;
 		gchar **extensions = gdk_pixbuf_format_get_extensions(format);
 		gpointer p = extensions;
@@ -2770,7 +2761,7 @@
 
 		g_strfreev(p);
 
-		if(type)
+		if (type)
 			break;
 
 		formats = formats->next;
@@ -2779,11 +2770,11 @@
 	g_slist_free(formats);
 #else
 	/* this is really ugly code, but I think it will work */
-	if(ext) {
+	if (ext) {
 		ext++;
-		if(!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg"))
+		if (!g_ascii_strcasecmp(ext, "jpeg") || !g_ascii_strcasecmp(ext, "jpg"))
 			type = g_strdup("jpeg");
-		else if(!g_ascii_strcasecmp(ext, "png"))
+		else if (!g_ascii_strcasecmp(ext, "png"))
 			type = g_strdup("png");
 	}
 
@@ -2792,7 +2783,7 @@
 
 	/* If I can't find a valid type, I will just tell the user about it and then assume
 	   it's a png */
-	if(!type){
+	if (!type){
 		gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
 						_("Unable to guess the image type based on the file extension supplied.  Defaulting to PNG."));
 		type = g_strdup("png");
@@ -2800,7 +2791,7 @@
 
 	gdk_pixbuf_save(image->pixbuf, filename, type, &error, NULL);
 
-	if(error){
+	if (error){
 		gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
 				_("Error saving image: %s"), error->message);
 		g_error_free(error);
@@ -2809,22 +2800,100 @@
 	g_free(type);
 }
 
-static void gtk_imhtml_image_save(GtkWidget *w, GtkIMHtmlImage *image)
+#if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
+static void
+image_save_check_if_exists_cb(GtkWidget *widget, gint response, GtkIMHtmlImage *image)
+{
+	gchar *filename;
+
+	if (response != GTK_RESPONSE_ACCEPT) {
+		gtk_widget_destroy(widget);
+		image->filesel = NULL;
+		return;
+	}
+
+	filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
+#else /* FILECHOOSER */
+static void
+image_save_check_if_exists_cb(GtkWidget *button, GtkIMHtmlImage *image)
 {
-	GtkWidget *sel = gtk_file_selection_new(_("Save Image"));
-
-	if (image->filename)
-		gtk_file_selection_set_filename(GTK_FILE_SELECTION(sel), image->filename);
-	g_object_set_data(G_OBJECT(sel), "GtkIMHtmlImage", image);
-	g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked",
-					 G_CALLBACK(write_img_to_file), sel);
-
-	g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->ok_button), "clicked",
-							 G_CALLBACK(gtk_widget_destroy), sel);
-	g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(sel)->cancel_button), "clicked",
-							 G_CALLBACK(gtk_widget_destroy), sel);
-
-	gtk_widget_show(sel);
+	gchar *filename;
+
+	filename = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(image->filesel)));
+
+	if (g_file_test(filename, G_FILE_TEST_IS_DIR)) {
+		gchar *dirname;
+		/* append a / is needed */
+		if (filename[strlen(filename) - 1] != G_DIR_SEPARATOR) {
+			dirname = g_strconcat(filename, G_DIR_SEPARATOR_S, NULL);
+		} else {
+			dirname = g_strdup(filename);
+		}
+		gtk_file_selection_set_filename(image->filesel, dirname);
+		g_free(dirname);
+		g_free(filename);
+		return;
+	}
+#endif /* FILECHOOSER */
+
+	/*
+	 * XXX - We should probably prompt the user to determine if they really
+	 * want to overwrite the file or not.  However, I don't feel like doing
+	 * that, so we're just always going to overwrite if the file exists.
+	 */
+	/*
+	if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
+	} else
+		image_save_yes_cb(image, filename);
+	*/
+
+	image_save_yes_cb(image, filename);
+
+	g_free(filename);
+}
+
+#if !GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
+static void
+image_save_cancel_cb(GtkIMHtmlImage *image)
+{
+	gtk_widget_destroy(image->filesel);
+	image->filesel = NULL;
+}
+#endif /* FILECHOOSER */
+
+static void
+gtk_imhtml_image_save(GtkWidget *w, GtkIMHtmlImage *image)
+{
+	if (image->filesel != NULL) {
+		gtk_window_present(GTK_WINDOW(image->filesel));
+		return;
+	}
+
+#if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
+	image->filesel = gtk_file_chooser_dialog_new(_("Save Image"),
+						NULL,
+						GTK_FILE_CHOOSER_ACTION_SAVE,
+						GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+						GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+						NULL);
+	gtk_dialog_set_default_response(GTK_DIALOG(image->filesel), GTK_RESPONSE_ACCEPT);
+	if (image->filename != NULL)
+		gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(image->filesel), image->filename);
+	g_signal_connect(G_OBJECT(GTK_FILE_CHOOSER(image->filesel)), "response",
+					 G_CALLBACK(image_save_check_if_exists_cb), image);
+#else /* FILECHOOSER */
+	image->filesel = gtk_file_selection_new(_("Save Image"));
+	if (image->filename != NULL)
+		gtk_file_selection_set_filename(GTK_FILE_SELECTION(image->filesel), image->filename);
+	g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(image->filesel)->ok_button), "clicked",
+					 G_CALLBACK(image_save_check_if_exists_cb), image);
+	g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(image->filesel)->ok_button), "clicked",
+							 G_CALLBACK(image_save_cancel_cb), image);
+	g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(image->filesel)->cancel_button), "clicked",
+							 G_CALLBACK(image_save_cancel_cb), image);
+#endif /* FILECHOOSER */
+
+	gtk_widget_show(image->filesel);
 }
 
 static gboolean gtk_imhtml_image_clicked(GtkWidget *w, GdkEvent *event, GtkIMHtmlImage *image)
@@ -2866,6 +2935,8 @@
 	g_object_unref(image->pixbuf);
 	if (image->filename)
 		g_free(image->filename);
+	if (image->filesel)
+		gtk_widget_destroy(image->filesel);
 	g_free(scale);
 }
 
--- a/src/gtkimhtml.h	Thu Jul 22 03:21:21 2004 +0000
+++ b/src/gtkimhtml.h	Thu Jul 22 03:23:12 2004 +0000
@@ -170,6 +170,7 @@
 	int width;
 	int height;
 	int id;
+	GtkWidget *filesel;
 };
 
 struct _GtkIMHtmlHr {
--- a/src/gtkrequest.c	Thu Jul 22 03:21:21 2004 +0000
+++ b/src/gtkrequest.c	Thu Jul 22 03:23:12 2004 +0000
@@ -1360,8 +1360,8 @@
 		gaim_request_close(GAIM_REQUEST_FILE, data);
 }
 
+#if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
 static void
-#if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
 file_ok_check_if_exists_cb(GtkWidget *widget, gint response, GaimGtkRequestData *data)
 {
 	if (response != GTK_RESPONSE_ACCEPT) {
@@ -1373,6 +1373,7 @@
 
 	data->u.file.name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(data->dialog));
 #else /* FILECHOOSER */
+static void
 file_ok_check_if_exists_cb(GtkWidget *button, GaimGtkRequestData *data)
 {
 	const gchar *name;