Replace strcmp() with purple_strequal()
[pidgin-git.git] / pidgin / gtksmiley.c
blobf0093b86574968f6bf3bd80188f87c82cc5190b7
1 /**
2 * @file gtksmiley.c GTK+ Smiley Manager API
3 * @ingroup pidgin
4 */
6 /*
7 * pidgin
9 * Pidgin is the legal property of its developers, whose names are too numerous
10 * to list here. Please refer to the COPYRIGHT file distributed with this
11 * source distribution.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
28 #include "internal.h"
29 #include "pidgin.h"
31 #include "debug.h"
32 #include "notify.h"
33 #include "smiley.h"
35 #include "gtkimhtml.h"
36 #include "gtksmiley.h"
37 #include "gtkutils.h"
38 #include "pidginstock.h"
40 #define PIDGIN_RESPONSE_MODIFY 1000
42 struct _PidginSmiley
44 PurpleSmiley *smiley;
45 GtkWidget *parent;
46 GtkWidget *smile;
47 GtkWidget *smiley_image;
48 gchar *filename;
49 GdkPixbuf *custom_pixbuf;
50 gpointer data; /** @since 2.6.0 */
51 gsize datasize; /** @since 2.6.0 */
52 gint entry_len; /** @since 2.6.0 */
55 typedef struct
57 GtkWidget *window;
59 GtkWidget *treeview;
60 GtkListStore *model;
61 } SmileyManager;
63 enum
65 ICON,
66 SHORTCUT,
67 SMILEY,
68 N_COL
71 static SmileyManager *smiley_manager = NULL;
72 static GSList *gtk_smileys = NULL;
74 static void
75 pidgin_smiley_destroy(PidginSmiley *smiley)
77 if (smiley->smiley)
78 g_object_set_data(G_OBJECT(smiley->smiley), "edit-dialog", NULL);
79 gtk_widget_destroy(smiley->parent);
80 g_free(smiley->filename);
81 if (smiley->custom_pixbuf)
82 g_object_unref(G_OBJECT(smiley->custom_pixbuf));
83 g_free(smiley);
86 /******************************************************************************
87 * GtkIMHtmlSmileys stuff
88 *****************************************************************************/
89 /* Perhaps these should be in gtkimhtml.c instead. -- sadrul */
90 static void add_gtkimhtml_to_list(GtkIMHtmlSmiley *gtksmiley)
92 gtk_smileys = g_slist_prepend(gtk_smileys, gtksmiley);
94 purple_debug_info("gtksmiley", "adding %s to gtk_smileys\n", gtksmiley->smile);
97 static void
98 shortcut_changed_cb(PurpleSmiley *smiley, gpointer dontcare, GtkIMHtmlSmiley *gtksmiley)
100 g_free(gtksmiley->smile);
101 gtksmiley->smile = g_strdup(purple_smiley_get_shortcut(smiley));
104 static void
105 image_changed_cb(PurpleSmiley *smiley, gpointer dontcare, GtkIMHtmlSmiley *gtksmiley)
107 const char *file;
109 g_free(gtksmiley->file);
111 file = purple_imgstore_get_filename(purple_smiley_get_stored_image(smiley));
112 gtksmiley->file = g_build_filename(purple_smileys_get_storing_dir(), file, NULL);
113 gtk_imhtml_smiley_reload(gtksmiley);
116 static GtkIMHtmlSmiley *smiley_purple_to_gtkimhtml(PurpleSmiley *smiley)
118 GtkIMHtmlSmiley *gtksmiley;
119 gchar *filename;
120 const gchar *file;
122 file = purple_imgstore_get_filename(purple_smiley_get_stored_image(smiley));
124 filename = g_build_filename(purple_smileys_get_storing_dir(), file, NULL);
126 gtksmiley = gtk_imhtml_smiley_create(filename, purple_smiley_get_shortcut(smiley),
127 FALSE, GTK_IMHTML_SMILEY_CUSTOM);
128 g_free(filename);
130 /* Make sure the shortcut for the GtkIMHtmlSmiley is updated with the PurpleSmiley */
131 g_signal_connect(G_OBJECT(smiley), "notify::shortcut",
132 G_CALLBACK(shortcut_changed_cb), gtksmiley);
134 /* And update the pixbuf too when the image is changed */
135 g_signal_connect(G_OBJECT(smiley), "notify::image",
136 G_CALLBACK(image_changed_cb), gtksmiley);
138 return gtksmiley;
141 void pidgin_smiley_del_from_list(PurpleSmiley *smiley)
143 GSList *list = NULL;
144 GtkIMHtmlSmiley *gtksmiley;
146 if (gtk_smileys == NULL)
147 return;
149 list = gtk_smileys;
151 for (; list; list = list->next) {
152 gtksmiley = (GtkIMHtmlSmiley*)list->data;
154 if (!purple_strequal(gtksmiley->smile, purple_smiley_get_shortcut(smiley)))
155 continue;
157 gtk_imhtml_smiley_destroy(gtksmiley);
158 g_signal_handlers_disconnect_matched(G_OBJECT(smiley), G_SIGNAL_MATCH_DATA,
159 0, 0, NULL, NULL, gtksmiley);
160 break;
163 if (list)
164 gtk_smileys = g_slist_delete_link(gtk_smileys, list);
167 void pidgin_smiley_add_to_list(PurpleSmiley *smiley)
169 GtkIMHtmlSmiley *gtksmiley;
171 gtksmiley = smiley_purple_to_gtkimhtml(smiley);
172 add_gtkimhtml_to_list(gtksmiley);
173 g_signal_connect(G_OBJECT(smiley), "destroy", G_CALLBACK(pidgin_smiley_del_from_list), NULL);
176 void pidgin_smileys_init(void)
178 GList *smileys;
179 PurpleSmiley *smiley;
181 if (gtk_smileys != NULL)
182 return;
184 smileys = purple_smileys_get_all();
186 for (; smileys; smileys = g_list_delete_link(smileys, smileys)) {
187 smiley = (PurpleSmiley*)smileys->data;
189 pidgin_smiley_add_to_list(smiley);
193 void pidgin_smileys_uninit(void)
195 GSList *list;
196 GtkIMHtmlSmiley *gtksmiley;
198 list = gtk_smileys;
200 if (list == NULL)
201 return;
203 for (; list; list = g_slist_delete_link(list, list)) {
204 gtksmiley = (GtkIMHtmlSmiley*)list->data;
205 gtk_imhtml_smiley_destroy(gtksmiley);
208 gtk_smileys = NULL;
211 GSList *pidgin_smileys_get_all(void)
213 return gtk_smileys;
216 /******************************************************************************
217 * Manager stuff
218 *****************************************************************************/
220 static void refresh_list(void);
222 /******************************************************************************
223 * The Add dialog
224 ******************************************************************************/
226 static void do_add(GtkWidget *widget, PidginSmiley *s)
228 const gchar *entry;
229 PurpleSmiley *emoticon;
231 entry = gtk_entry_get_text(GTK_ENTRY(s->smile));
233 emoticon = purple_smileys_find_by_shortcut(entry);
234 if (emoticon && emoticon != s->smiley) {
235 gchar *msg;
236 msg = g_strdup_printf(_("A custom smiley for '%s' already exists. "
237 "Please use a different shortcut."), entry);
238 purple_notify_error(s->parent, _("Custom Smiley"),
239 _("Duplicate Shortcut"), msg);
240 g_free(msg);
241 return;
244 if (s->smiley) {
245 if (s->filename) {
246 gchar *data = NULL;
247 size_t len;
248 GError *err = NULL;
250 if (!g_file_get_contents(s->filename, &data, &len, &err)) {
251 purple_debug_error("gtksmiley", "Error reading %s: %s\n",
252 s->filename, err->message);
253 g_error_free(err);
255 return;
257 purple_smiley_set_data(s->smiley, (guchar*)data, len);
259 purple_smiley_set_shortcut(s->smiley, entry);
260 } else {
261 purple_debug_info("gtksmiley", "adding a new smiley\n");
263 if (s->filename == NULL) {
264 gchar *buffer = NULL;
265 gsize size = 0;
266 gchar *filename;
267 const gchar *dirname = purple_smileys_get_storing_dir();
269 /* since this may be called before purple_smiley_new_* has ever been
270 called, we create the storing dir, if it doesn't exist yet, to be
271 able to save the pixbuf before adding the smiley */
272 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
273 purple_debug_info("gtksmiley", "Creating smileys directory.\n");
275 if (g_mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
276 purple_debug_error("gtksmiley",
277 "Unable to create directory %s: %s\n",
278 dirname, g_strerror(errno));
282 if (s->data && s->datasize) {
283 /* Cached data & size in memory */
284 buffer = s->data;
285 size = s->datasize;
287 else {
288 /* Get the smiley from the custom pixbuf */
289 gdk_pixbuf_save_to_buffer(s->custom_pixbuf, &buffer, &size,
290 "png", NULL, "compression", "9", NULL, NULL);
292 filename = purple_util_get_image_filename(buffer, size);
293 s->filename = g_build_filename(dirname, filename, NULL);
294 purple_util_write_data_to_file_absolute(s->filename, buffer, size);
295 g_free(filename);
296 g_free(buffer);
298 emoticon = purple_smiley_new_from_file(entry, s->filename);
299 if (emoticon)
300 pidgin_smiley_add_to_list(emoticon);
303 if (smiley_manager != NULL)
304 refresh_list();
306 gtk_widget_destroy(s->parent);
309 static void do_add_select_cb(GtkWidget *widget, gint resp, PidginSmiley *s)
311 switch (resp) {
312 case GTK_RESPONSE_ACCEPT:
313 do_add(widget, s);
314 break;
315 case GTK_RESPONSE_DELETE_EVENT:
316 case GTK_RESPONSE_CANCEL:
317 gtk_widget_destroy(s->parent);
318 break;
319 default:
320 purple_debug_error("gtksmiley", "no valid response\n");
321 break;
325 static void do_add_file_cb(const char *filename, gpointer data)
327 PidginSmiley *s = data;
328 GdkPixbuf *pixbuf;
330 if (!filename)
331 return;
333 g_free(s->filename);
334 s->filename = g_strdup(filename);
335 pixbuf = pidgin_pixbuf_new_from_file_at_scale(filename, 64, 64, FALSE);
336 gtk_image_set_from_pixbuf(GTK_IMAGE(s->smiley_image), pixbuf);
337 if (pixbuf)
338 g_object_unref(G_OBJECT(pixbuf));
339 gtk_widget_grab_focus(s->smile);
341 if (s->entry_len > 0)
342 gtk_dialog_set_response_sensitive(GTK_DIALOG(s->parent), GTK_RESPONSE_ACCEPT, TRUE);
345 static void
346 open_image_selector(GtkWidget *widget, PidginSmiley *psmiley)
348 GtkWidget *file_chooser;
349 file_chooser = pidgin_buddy_icon_chooser_new(GTK_WINDOW(gtk_widget_get_toplevel(widget)),
350 do_add_file_cb, psmiley);
351 gtk_window_set_title(GTK_WINDOW(file_chooser), _("Custom Smiley"));
352 gtk_window_set_role(GTK_WINDOW(file_chooser), "file-selector-custom-smiley");
353 gtk_widget_show_all(file_chooser);
356 static void
357 smiley_name_insert_cb(GtkEditable *editable,
358 gchar *new_text,
359 gint new_text_length,
360 gint *position,
361 gpointer user_data)
363 PidginSmiley *s = user_data;
364 if (new_text_length != -1)
365 s->entry_len += new_text_length;
366 else
367 s->entry_len += strlen(new_text);
369 if (s->filename != NULL || s->custom_pixbuf != NULL || s->smiley != NULL)
370 gtk_dialog_set_response_sensitive(GTK_DIALOG(s->parent), GTK_RESPONSE_ACCEPT, TRUE);
373 static void
374 smiley_name_delete_cb(GtkEditable *editable,
375 gint start_pos,
376 gint end_pos,
377 gpointer user_data)
379 PidginSmiley *s = user_data;
380 s->entry_len -= end_pos - start_pos;
382 if (s->entry_len <= 0)
383 gtk_dialog_set_response_sensitive(GTK_DIALOG(s->parent), GTK_RESPONSE_ACCEPT, FALSE);
386 PidginSmiley *
387 pidgin_smiley_edit(GtkWidget *widget, PurpleSmiley *smiley)
389 GtkWidget *vbox;
390 GtkWidget *hbox;
391 GtkWidget *label;
392 GtkWidget *filech;
393 GtkWidget *window;
394 GdkPixbuf *pixbuf = NULL;
395 PurpleStoredImage *stored_img;
397 PidginSmiley *s = g_new0(PidginSmiley, 1);
398 s->smiley = smiley;
400 window = gtk_dialog_new_with_buttons(smiley ? _("Edit Smiley") : _("Add Smiley"),
401 widget ? GTK_WINDOW(widget) : NULL,
402 GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
403 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
404 smiley ? GTK_STOCK_SAVE : GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT,
405 NULL);
406 s->parent = window;
407 if (smiley)
408 g_object_set_data(G_OBJECT(smiley), "edit-dialog", window);
410 gtk_container_set_border_width(GTK_CONTAINER(window), PIDGIN_HIG_BORDER);
412 gtk_dialog_set_default_response(GTK_DIALOG(window), GTK_RESPONSE_ACCEPT);
413 g_signal_connect(window, "response", G_CALLBACK(do_add_select_cb), s);
415 /* The vbox */
416 vbox = gtk_vbox_new(FALSE, PIDGIN_HIG_BORDER);
417 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(window)->vbox), vbox);
418 gtk_widget_show(vbox);
420 /* The hbox */
421 hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_BORDER);
422 gtk_container_add(GTK_CONTAINER(GTK_VBOX(vbox)), hbox);
424 label = gtk_label_new_with_mnemonic(_("_Image:"));
425 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
426 gtk_widget_show(label);
428 filech = gtk_button_new();
429 gtk_box_pack_end(GTK_BOX(hbox), filech, FALSE, FALSE, 0);
430 pidgin_set_accessible_label(filech, label);
432 s->smiley_image = gtk_image_new();
433 gtk_container_add(GTK_CONTAINER(filech), s->smiley_image);
434 if (smiley && (stored_img = purple_smiley_get_stored_image(smiley))) {
435 pixbuf = pidgin_pixbuf_from_imgstore(stored_img);
436 purple_imgstore_unref(stored_img);
437 } else {
438 GtkIconSize icon_size = gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_SMALL);
439 pixbuf = gtk_widget_render_icon(window, PIDGIN_STOCK_TOOLBAR_SELECT_AVATAR,
440 icon_size, "PidginSmiley");
443 gtk_image_set_from_pixbuf(GTK_IMAGE(s->smiley_image), pixbuf);
444 if (pixbuf != NULL)
445 g_object_unref(G_OBJECT(pixbuf));
446 g_signal_connect(G_OBJECT(filech), "clicked", G_CALLBACK(open_image_selector), s);
448 gtk_widget_show_all(hbox);
450 /* info */
451 hbox = gtk_hbox_new(FALSE, PIDGIN_HIG_BORDER);
452 gtk_container_add(GTK_CONTAINER(GTK_VBOX(vbox)),hbox);
454 /* Shortcut text */
455 label = gtk_label_new_with_mnemonic(_("S_hortcut text:"));
456 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
457 gtk_widget_show(label);
459 s->smile = gtk_entry_new();
460 gtk_entry_set_activates_default(GTK_ENTRY(s->smile), TRUE);
461 pidgin_set_accessible_label(s->smile, label);
462 if (smiley) {
463 const char *shortcut = purple_smiley_get_shortcut(smiley);
464 gtk_entry_set_text(GTK_ENTRY(s->smile), shortcut);
465 s->entry_len = strlen(shortcut);
467 else
468 gtk_dialog_set_response_sensitive(GTK_DIALOG(window), GTK_RESPONSE_ACCEPT, FALSE);
470 /* gtk_entry_get_text_length is 2.14+, so we'll just keep track ourselves */
471 g_signal_connect(G_OBJECT(s->smile), "insert-text", G_CALLBACK(smiley_name_insert_cb), s);
472 g_signal_connect(G_OBJECT(s->smile), "delete-text", G_CALLBACK(smiley_name_delete_cb), s);
474 gtk_box_pack_end(GTK_BOX(hbox), s->smile, FALSE, FALSE, 0);
475 gtk_widget_show(s->smile);
477 gtk_widget_show(hbox);
479 gtk_widget_show(GTK_WIDGET(window));
480 g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(pidgin_smiley_destroy), s);
481 g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(purple_notify_close_with_handle), s);
483 return s;
486 void
487 pidgin_smiley_editor_set_shortcut(PidginSmiley *editor, const gchar *shortcut)
489 gtk_entry_set_text(GTK_ENTRY(editor->smile), shortcut ? shortcut : "");
492 void
493 pidgin_smiley_editor_set_image(PidginSmiley *editor, GdkPixbuf *image)
495 if (editor->custom_pixbuf)
496 g_object_unref(G_OBJECT(editor->custom_pixbuf));
497 editor->custom_pixbuf = image ? g_object_ref(G_OBJECT(image)) : NULL;
498 if (image) {
499 gtk_image_set_from_pixbuf(GTK_IMAGE(editor->smiley_image), image);
500 if (editor->entry_len > 0)
501 gtk_dialog_set_response_sensitive(GTK_DIALOG(editor->parent),
502 GTK_RESPONSE_ACCEPT, TRUE);
504 else
505 gtk_dialog_set_response_sensitive(GTK_DIALOG(editor->parent),
506 GTK_RESPONSE_ACCEPT, FALSE);
509 void
510 pidgin_smiley_editor_set_data(PidginSmiley *editor, gpointer data, gsize datasize)
512 editor->data = data;
513 editor->datasize = datasize;
516 /******************************************************************************
517 * Delete smiley
518 *****************************************************************************/
519 static void delete_foreach(GtkTreeModel *model, GtkTreePath *path,
520 GtkTreeIter *iter, gpointer data)
522 PurpleSmiley *smiley = NULL;
524 gtk_tree_model_get(model, iter,
525 SMILEY, &smiley,
526 -1);
528 if(smiley != NULL) {
529 g_object_unref(G_OBJECT(smiley));
530 pidgin_smiley_del_from_list(smiley);
531 purple_smiley_delete(smiley);
535 static void append_to_list(GtkTreeModel *model, GtkTreePath *path,
536 GtkTreeIter *iter, gpointer data)
538 GList **list = data;
539 *list = g_list_prepend(*list, gtk_tree_path_copy(path));
542 static void smiley_delete(SmileyManager *dialog)
544 GtkTreeSelection *selection;
545 GList *list = NULL;
547 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->treeview));
548 gtk_tree_selection_selected_foreach(selection, delete_foreach, dialog);
549 gtk_tree_selection_selected_foreach(selection, append_to_list, &list);
551 while (list) {
552 GtkTreeIter iter;
553 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(dialog->model), &iter, list->data))
554 gtk_list_store_remove(GTK_LIST_STORE(dialog->model), &iter);
555 gtk_tree_path_free(list->data);
556 list = g_list_delete_link(list, list);
559 /******************************************************************************
560 * The Smiley Manager
561 *****************************************************************************/
562 static void add_columns(GtkWidget *treeview, SmileyManager *dialog)
564 GtkCellRenderer *rend;
565 GtkTreeViewColumn *column;
567 /* Icon */
568 column = gtk_tree_view_column_new();
569 gtk_tree_view_column_set_title(column, _("Smiley"));
570 gtk_tree_view_column_set_resizable(column, TRUE);
571 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
573 rend = gtk_cell_renderer_pixbuf_new();
574 gtk_tree_view_column_pack_start(column, rend, FALSE);
575 gtk_tree_view_column_add_attribute(column, rend, "pixbuf", ICON);
577 /* Shortcut Text */
578 column = gtk_tree_view_column_new();
579 gtk_tree_view_column_set_title(column, _("Shortcut Text"));
580 gtk_tree_view_column_set_resizable(column, TRUE);
581 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
583 rend = gtk_cell_renderer_text_new();
584 gtk_tree_view_column_pack_start(column, rend, TRUE);
585 gtk_tree_view_column_add_attribute(column, rend, "text", SHORTCUT);
588 static void store_smiley_add(PurpleSmiley *smiley)
590 GtkTreeIter iter;
591 PurpleStoredImage *img;
592 GdkPixbuf *sized_smiley = NULL;
594 if (smiley_manager == NULL)
595 return;
597 img = purple_smiley_get_stored_image(smiley);
599 if (img != NULL) {
600 GdkPixbuf *smiley_image = pidgin_pixbuf_from_imgstore(img);
601 purple_imgstore_unref(img);
603 if (smiley_image != NULL) {
604 if (gdk_pixbuf_get_width(smiley_image) > 22 ||
605 gdk_pixbuf_get_height(smiley_image) > 22) {
606 sized_smiley = gdk_pixbuf_scale_simple(smiley_image,
607 22, 22, GDK_INTERP_HYPER);
608 g_object_unref(G_OBJECT(smiley_image));
609 } else {
610 /* don't scale up smaller smileys, avoid blurryness */
611 sized_smiley = smiley_image;
617 gtk_list_store_append(smiley_manager->model, &iter);
619 gtk_list_store_set(smiley_manager->model, &iter,
620 ICON, sized_smiley,
621 SHORTCUT, purple_smiley_get_shortcut(smiley),
622 SMILEY, smiley,
623 -1);
625 if (sized_smiley != NULL)
626 g_object_unref(G_OBJECT(sized_smiley));
629 static void populate_smiley_list(SmileyManager *dialog)
631 GList *list;
632 PurpleSmiley *emoticon;
634 gtk_list_store_clear(dialog->model);
636 for(list = purple_smileys_get_all(); list != NULL;
637 list = g_list_delete_link(list, list)) {
638 emoticon = (PurpleSmiley*)list->data;
640 store_smiley_add(emoticon);
644 static void smile_selected_cb(GtkTreeSelection *sel, SmileyManager *dialog)
646 gint selected;
648 selected = gtk_tree_selection_count_selected_rows(sel);
650 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog->window),
651 GTK_RESPONSE_NO, selected > 0);
653 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog->window),
654 PIDGIN_RESPONSE_MODIFY, selected > 0);
657 static void
658 smiley_edit_iter(SmileyManager *dialog, GtkTreeIter *iter)
660 PurpleSmiley *smiley = NULL;
661 GtkWidget *window = NULL;
662 gtk_tree_model_get(GTK_TREE_MODEL(dialog->model), iter, SMILEY, &smiley, -1);
663 if ((window = g_object_get_data(G_OBJECT(smiley), "edit-dialog")) != NULL)
664 gtk_window_present(GTK_WINDOW(window));
665 else
666 pidgin_smiley_edit(gtk_widget_get_toplevel(GTK_WIDGET(dialog->treeview)), smiley);
667 g_object_unref(G_OBJECT(smiley));
670 static void smiley_edit_cb(GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
672 GtkTreeIter iter;
673 SmileyManager *dialog = data;
675 gtk_tree_model_get_iter(GTK_TREE_MODEL(dialog->model), &iter, path);
676 smiley_edit_iter(dialog, &iter);
679 static void
680 edit_selected_cb(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
682 smiley_edit_iter(data, iter);
685 static void
686 smiley_got_url(PurpleUtilFetchUrlData *url_data, gpointer user_data,
687 const gchar *smileydata, size_t len, const gchar *error_message)
689 SmileyManager *dialog = user_data;
690 FILE *f;
691 gchar *path;
692 size_t wc;
693 PidginSmiley *ps;
694 GdkPixbuf *image;
696 if ((error_message != NULL) || (len == 0)) {
697 return;
700 f = purple_mkstemp(&path, TRUE);
701 wc = fwrite(smileydata, len, 1, f);
702 if (wc != 1) {
703 purple_debug_warning("smiley_got_url", "Unable to write smiley data.\n");
704 fclose(f);
705 g_unlink(path);
706 g_free(path);
707 return;
709 fclose(f);
711 image = pidgin_pixbuf_new_from_file(path);
712 g_unlink(path);
713 g_free(path);
714 if (!image)
715 return;
717 ps = pidgin_smiley_edit(dialog->window, NULL);
718 pidgin_smiley_editor_set_image(ps, image);
719 pidgin_smiley_editor_set_data(ps, g_memdup(smileydata, len), len);
722 static void
723 smiley_dnd_recv(GtkWidget *widget, GdkDragContext *dc, guint x, guint y,
724 GtkSelectionData *sd, guint info, guint t, gpointer user_data)
726 SmileyManager *dialog = user_data;
727 gchar *name = g_strchomp((gchar *)sd->data);
729 if ((sd->length >= 0) && (sd->format == 8)) {
730 /* Well, it looks like the drag event was cool.
731 * Let's do something with it */
733 if (!g_ascii_strncasecmp(name, "file://", 7)) {
734 GError *converr = NULL;
735 gchar *tmp;
736 PidginSmiley *ps;
737 /* It looks like we're dealing with a local file. Let's
738 * just try and read it */
739 if(!(tmp = g_filename_from_uri(name, NULL, &converr))) {
740 purple_debug_error("smiley dnd", "%s\n",
741 (converr ? converr->message :
742 "g_filename_from_uri error"));
743 return;
745 ps = pidgin_smiley_edit(dialog->window, NULL);
746 do_add_file_cb(tmp, ps);
747 if (gtk_image_get_pixbuf(GTK_IMAGE(ps->smiley_image)) == NULL)
748 gtk_dialog_response(GTK_DIALOG(ps->parent), GTK_RESPONSE_CANCEL);
749 g_free(tmp);
750 } else if (!g_ascii_strncasecmp(name, "http://", 7)) {
751 /* Oo, a web drag and drop. This is where things
752 * will start to get interesting */
753 purple_util_fetch_url(name, TRUE, NULL, FALSE, smiley_got_url, dialog);
754 } else if (!g_ascii_strncasecmp(name, "https://", 8)) {
755 /* purple_util_fetch_url() doesn't support HTTPS */
756 char *tmp = g_strdup(name + 1);
757 tmp[0] = 'h';
758 tmp[1] = 't';
759 tmp[2] = 't';
760 tmp[3] = 'p';
762 purple_util_fetch_url(tmp, TRUE, NULL, FALSE, smiley_got_url, dialog);
763 g_free(tmp);
766 gtk_drag_finish(dc, TRUE, FALSE, t);
769 gtk_drag_finish(dc, FALSE, FALSE, t);
772 static GtkWidget *smiley_list_create(SmileyManager *dialog)
774 GtkWidget *treeview;
775 GtkTreeSelection *sel;
776 GtkTargetEntry te[3] = {
777 {"text/plain", 0, 0},
778 {"text/uri-list", 0, 1},
779 {"STRING", 0, 2}
782 /* Create the list model */
783 dialog->model = gtk_list_store_new(N_COL,
784 GDK_TYPE_PIXBUF, /* ICON */
785 G_TYPE_STRING, /* SHORTCUT */
786 G_TYPE_OBJECT /* SMILEY */
789 /* the actual treeview */
790 treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dialog->model));
791 dialog->treeview = treeview;
792 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);
793 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dialog->model), SHORTCUT, GTK_SORT_ASCENDING);
794 g_object_unref(G_OBJECT(dialog->model));
796 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
797 gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE);
799 g_signal_connect(G_OBJECT(sel), "changed", G_CALLBACK(smile_selected_cb), dialog);
800 g_signal_connect(G_OBJECT(treeview), "row_activated", G_CALLBACK(smiley_edit_cb), dialog);
802 gtk_drag_dest_set(treeview,
803 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
804 te, G_N_ELEMENTS(te), GDK_ACTION_COPY | GDK_ACTION_MOVE);
805 g_signal_connect(G_OBJECT(treeview), "drag_data_received", G_CALLBACK(smiley_dnd_recv), dialog);
807 gtk_widget_show(treeview);
809 add_columns(treeview, dialog);
810 populate_smiley_list(dialog);
812 return pidgin_make_scrollable(treeview, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC, GTK_SHADOW_IN, -1, -1);
815 static void refresh_list()
817 populate_smiley_list(smiley_manager);
820 static void smiley_manager_select_cb(GtkWidget *widget, gint resp, SmileyManager *dialog)
822 GtkTreeSelection *selection = NULL;
824 switch (resp) {
825 case GTK_RESPONSE_YES:
826 pidgin_smiley_edit(dialog->window, NULL);
827 break;
828 case GTK_RESPONSE_NO:
829 smiley_delete(dialog);
830 break;
831 case GTK_RESPONSE_DELETE_EVENT:
832 case GTK_RESPONSE_CLOSE:
833 gtk_widget_destroy(dialog->window);
834 g_free(smiley_manager);
835 smiley_manager = NULL;
836 break;
837 case PIDGIN_RESPONSE_MODIFY:
838 /* Find smiley of selection... */
839 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->treeview));
840 gtk_tree_selection_selected_foreach(selection, edit_selected_cb, dialog);
841 break;
842 default:
843 purple_debug_info("gtksmiley", "No valid selection\n");
844 break;
848 void pidgin_smiley_manager_show(void)
850 SmileyManager *dialog;
851 GtkWidget *win;
852 GtkWidget *sw;
853 GtkWidget *vbox;
855 if (smiley_manager) {
856 gtk_window_present(GTK_WINDOW(smiley_manager->window));
857 return;
860 dialog = g_new0(SmileyManager, 1);
861 smiley_manager = dialog;
863 dialog->window = win = gtk_dialog_new_with_buttons(
864 _("Custom Smiley Manager"),
865 NULL,
866 GTK_DIALOG_DESTROY_WITH_PARENT,
867 PIDGIN_STOCK_ADD, GTK_RESPONSE_YES,
868 PIDGIN_STOCK_MODIFY, PIDGIN_RESPONSE_MODIFY,
869 GTK_STOCK_DELETE, GTK_RESPONSE_NO,
870 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
871 NULL);
873 gtk_window_set_default_size(GTK_WINDOW(win), 50, 400);
874 gtk_window_set_role(GTK_WINDOW(win), "custom_smiley_manager");
875 gtk_container_set_border_width(GTK_CONTAINER(win),PIDGIN_HIG_BORDER);
876 gtk_dialog_set_response_sensitive(GTK_DIALOG(win), GTK_RESPONSE_NO, FALSE);
877 gtk_dialog_set_response_sensitive(GTK_DIALOG(win),
878 PIDGIN_RESPONSE_MODIFY, FALSE);
880 g_signal_connect(win, "response", G_CALLBACK(smiley_manager_select_cb),
881 dialog);
883 /* The vbox */
884 vbox = gtk_vbox_new(FALSE, PIDGIN_HIG_BORDER);
885 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(win)->vbox), vbox);
886 gtk_widget_show(vbox);
888 /* get the scrolled window with all stuff */
889 sw = smiley_list_create(dialog);
890 gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0);
891 gtk_widget_show(sw);
893 gtk_widget_show(win);