Mercurial > pidgin
diff src/gtkconv.c @ 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 | 960e3fd1da94 |
| children | 6f9bedacac3b |
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));
