Mercurial > pidgin.yaz
changeset 9497:44a5bfa4730b
[gaim-migrate @ 10323]
Done with the file chooser for right clicking and saving icons.
There is a lot of code duplication here... I'm going to look
at the file chooser in the request API now.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sat, 10 Jul 2004 15:18:02 +0000 |
parents | 27a595d413ff |
children | 6f9bedacac3b |
files | src/gtkconv.c src/gtkconv.h src/gtkutils.c src/gtkutils.h |
diffstat | 4 files changed, 154 insertions(+), 106 deletions(-) [+] |
line wrap: on
line diff
--- a/src/gtkconv.c Fri Jul 09 22:37:13 2004 +0000 +++ b/src/gtkconv.c Sat Jul 10 15:18:02 2004 +0000 @@ -2582,6 +2582,153 @@ gtkconv->u.im->iter = NULL; } +static void +saveicon_writefile_cb(GaimConversation *conv, gint id) +{ + GaimGtkConversation *gtkconv = GAIM_GTK_CONVERSATION(conv); + const char *filename; + FILE *fp; + GaimBuddyIcon *icon; + const void *data; + size_t len; + +#if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */ + filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(gtkconv->u.im->saveicon)); +#else /* FILECHOOSER */ + filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(gtkconv->u.im->saveicon)); +#endif /* FILECHOOSER */ + + gaim_notify_close_with_handle(gtkconv->u.im->saveicon); + + if (filename == NULL) { + gaim_notify_error(gtkconv->u.im->saveicon, NULL, _("Invalid file name."), NULL); + return; + } + + if ((fp = fopen(filename, "wb")) == NULL) { + gaim_notify_error(gtkconv->u.im->saveicon, NULL, _("Unable to open file."), NULL); + return; + } + + icon = gaim_conv_im_get_icon(GAIM_CONV_IM(conv)); + data = gaim_buddy_icon_get_data(icon, &len); + + if ((len <= 0) || (data == NULL)) { + gaim_notify_error(gtkconv->u.im->saveicon, NULL, _("Unable to save icon file to disk."), NULL); + return; + } + + fwrite(data, 1, len, fp); + fclose(fp); + + gtk_widget_destroy(gtkconv->u.im->saveicon); + gtkconv->u.im->saveicon = NULL; +} + +#if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */ +static void +saveicon_checkfile_cb(GtkWidget *widget, gint response, GaimConversation *conv) +{ + GaimGtkConversation *gtkconv = GAIM_GTK_CONVERSATION(conv); + const char *filename; + + if (response != GTK_RESPONSE_ACCEPT) { + gaim_notify_close_with_handle(gtkconv->u.im->saveicon); + gaim_request_close_with_handle(gtkconv->u.im->saveicon); + if (response == GTK_RESPONSE_CANCEL) + gtk_widget_destroy(gtkconv->u.im->saveicon); + gtkconv->u.im->saveicon = NULL; + return; + } + + filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)); +#else /* FILECHOOSER */ +static void +saveicon_checkfile_cb(GtkWidget *widget, GaimConversation *conv) +{ + GaimGtkConversation *gtkconv = GAIM_GTK_CONVERSATION(conv); + const char *filename; + + filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(gtkconv->u.im->saveicon)); + if (gaim_gtk_check_if_dir(filename, GTK_FILE_SELECTION(gtkconv->u.im->saveicon))) { + return; + } +#endif /* FILECHOOSER */ + + gaim_request_close_with_handle(gtkconv->u.im->saveicon); + + if (g_file_test(filename, G_FILE_TEST_EXISTS)) + { + gaim_request_yes_no(gtkconv->u.im->saveicon, NULL, _("That file already exists"), + _("Would you like to overwrite it?"), 1, + conv, G_CALLBACK(saveicon_writefile_cb), NULL); + } + else + saveicon_writefile_cb(conv, 1); +} + +#if !GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */ +static void +saveicon_destroy_cb(GtkWidget *widget, GaimConversation *conv) +{ + GaimGtkConversation *gtkconv = GAIM_GTK_CONVERSATION(conv); + if (gtkconv->u.im->saveicon != NULL) { + gaim_notify_close_with_handle(gtkconv->u.im->saveicon); + gaim_request_close_with_handle(gtkconv->u.im->saveicon); + gtk_widget_destroy(gtkconv->u.im->saveicon); + gtkconv->u.im->saveicon = NULL; + } +} +#endif + +static void +icon_menu_save_cb(GtkWidget *widget, GaimConversation *conv) +{ + GaimConvWindow *win = (GaimConvWindow *)conv->window; + GaimGtkConversation *gtkconv = GAIM_GTK_CONVERSATION(conv); + gchar *buf; + + g_return_if_fail(conv != NULL); + g_return_if_fail(gaim_conversation_get_type(conv) == GAIM_CONV_IM); + g_return_if_fail(GAIM_IS_GTK_CONVERSATION(conv)); + + if (gtkconv->u.im->saveicon != NULL) { + gtk_window_present(GTK_WINDOW(gtkconv->u.im->saveicon)); + return; + } + +#if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */ + buf = g_strdup_printf("%s.icon", gaim_normalize(conv->account, conv->name)); + gtkconv->u.im->saveicon = gtk_file_chooser_dialog_new(_("Save Icon"), + GTK_WINDOW(GAIM_GTK_WINDOW(win)->window), + 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(gtkconv->u.im->saveicon), + GTK_RESPONSE_ACCEPT); + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(gtkconv->u.im->saveicon), + buf); + g_signal_connect(G_OBJECT(gtkconv->u.im->saveicon), "response", + G_CALLBACK(saveicon_checkfile_cb), conv); +#else /* FILECHOOSER */ + buf = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s.icon", gaim_home_dir(), + gaim_normalize(conv->account, conv->name)); + gtkconv->u.im->saveicon = gtk_file_selection_new(_("Save Icon")); + gtk_file_selection_set_filename(GTK_FILE_SELECTION(gtkconv->u.im->saveicon), buf); + g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(gtkconv->u.im->saveicon)->ok_button), + "clicked", G_CALLBACK(saveicon_checkfile_cb), conv); + g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(gtkconv->u.im->saveicon)->cancel_button), + "clicked", G_CALLBACK(saveicon_destroy_cb), conv); + g_signal_connect(G_OBJECT(gtkconv->u.im->saveicon), + "destroy", G_CALLBACK(saveicon_destroy_cb), conv); +#endif /* FILECHOOSER */ + + g_free(buf); + + gtk_widget_show_all(GTK_WIDGET(gtkconv->u.im->saveicon)); +} + static gboolean icon_menu(GtkObject *obj, GdkEventButton *e, GaimConversation *conv) { @@ -2618,7 +2765,7 @@ gtk_widget_show(button); gaim_new_item_from_stock(menu, _("Save Icon As..."), GTK_STOCK_SAVE_AS, - G_CALLBACK(gaim_gtk_save_icon_dialog), conv, + G_CALLBACK(icon_menu_save_cb), conv, 0, 0, NULL); gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, e->button, e->time); @@ -4864,8 +5011,11 @@ if (gtkconv->u.im->icon_timer != 0) g_source_remove(gtkconv->u.im->icon_timer); - if (gtkconv->u.im->save_icon != NULL) - gtk_widget_destroy(gtkconv->u.im->save_icon); + if (gtkconv->u.im->saveicon != NULL) { + gaim_notify_close_with_handle(gtkconv->u.im->saveicon); + gaim_request_close_with_handle(gtkconv->u.im->saveicon); + gtk_widget_destroy(gtkconv->u.im->saveicon); + } if (gtkconv->u.im->anim != NULL) g_object_unref(G_OBJECT(gtkconv->u.im->anim));
--- a/src/gtkconv.h Fri Jul 09 22:37:13 2004 +0000 +++ b/src/gtkconv.h Sat Jul 10 15:18:02 2004 +0000 @@ -111,7 +111,7 @@ GdkPixbufAnimation *anim; GdkPixbufAnimationIter *iter; guint32 icon_timer; - GtkWidget *save_icon; + GtkWidget *saveicon; }; /**
--- a/src/gtkutils.c Fri Jul 09 22:37:13 2004 +0000 +++ b/src/gtkutils.c Sat Jul 10 15:18:02 2004 +0000 @@ -109,100 +109,6 @@ (*text != '\0')); } -static int -des_save_icon(GtkObject *obj, GdkEvent *e, - GaimGtkConversation *gtkconv) -{ - gtk_widget_destroy(gtkconv->u.im->save_icon); - gtkconv->u.im->save_icon = NULL; - - return TRUE; -} - -static void -do_save_icon(GtkObject *obj, GaimConversation *c) -{ - GaimGtkConversation *gtkconv; - FILE *file; - const char *f; - GaimBuddyIcon *icon; - size_t len; - const void *data; - - - gtkconv = GAIM_GTK_CONVERSATION(c); - - f = gtk_file_selection_get_filename( - GTK_FILE_SELECTION(gtkconv->u.im->save_icon)); - - if (gaim_gtk_check_if_dir(f, GTK_FILE_SELECTION(gtkconv->u.im->save_icon))) - return; - - icon = gaim_conv_im_get_icon(GAIM_CONV_IM(c)); - data = gaim_buddy_icon_get_data(icon, &len); - - if ((len > 0) && (data != NULL) && (file = fopen(f, "wb")) != NULL) { - fwrite(data, 1, len, file); - fclose(file); - } else { - gaim_notify_error(NULL, NULL, - _("Can't save icon file to disk."), NULL); - } - - gtk_widget_destroy(gtkconv->u.im->save_icon); - gtkconv->u.im->save_icon = NULL; -} - -static void -cancel_save_icon(GtkObject *obj, GaimGtkConversation *gtkconv) -{ - gtk_widget_destroy(gtkconv->u.im->save_icon); - gtkconv->u.im->save_icon = NULL; -} - - -void -gaim_gtk_save_icon_dialog(GtkObject *obj, GaimConversation *conv) -{ - GaimGtkConversation *gtkconv; - char buf[BUF_LEN]; - - if (conv == NULL || gaim_conversation_get_type(conv) != GAIM_CONV_IM) - return; - - if (!GAIM_IS_GTK_CONVERSATION(conv)) - return; - - gtkconv = GAIM_GTK_CONVERSATION(conv); - - if (gtkconv->u.im->save_icon != NULL) - { - gdk_window_raise(gtkconv->u.im->save_icon->window); - return; - } - - gtkconv->u.im->save_icon = gtk_file_selection_new(_("Save Icon")); - - gtk_file_selection_hide_fileop_buttons( - GTK_FILE_SELECTION(gtkconv->u.im->save_icon)); - - g_snprintf(buf, BUF_LEN - 1, - "%s" G_DIR_SEPARATOR_S "%s.icon", - gaim_home_dir(), gaim_conversation_get_name(conv)); - - gtk_file_selection_set_filename( - GTK_FILE_SELECTION(gtkconv->u.im->save_icon), buf); - - g_signal_connect(G_OBJECT(gtkconv->u.im->save_icon), "delete_event", - G_CALLBACK(des_save_icon), gtkconv); - g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(gtkconv->u.im->save_icon)->ok_button), "clicked", - G_CALLBACK(do_save_icon), conv); - g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(gtkconv->u.im->save_icon)->cancel_button), "clicked", - G_CALLBACK(cancel_save_icon), gtkconv); - - gtk_widget_show(gtkconv->u.im->save_icon); -} - int gaim_gtk_get_dispstyle(GaimConversationType type) {
--- a/src/gtkutils.h Fri Jul 09 22:37:13 2004 +0000 +++ b/src/gtkutils.h Sat Jul 10 15:18:02 2004 +0000 @@ -67,14 +67,6 @@ void gaim_setup_imhtml(GtkWidget *imhtml); /** - * Displays a dialog for saving the buddy icon in a conversation. - * - * @param obj @c NULL - * @param conv The conversation. - */ -void gaim_gtk_save_icon_dialog(GtkObject *obj, GaimConversation *conv); - -/** * Returns the display style for buttons for the specified conversation * type. *