I just noticed I didn't differentiate bn_IN from bn when I added this translation.
[pidgin-git.git] / pidgin / gtksmiley.c
blobe59c082fd7b23c17c295816197d2dd6df8cca184
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 (strcmp(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 = gdk_pixbuf_new_from_file_at_scale(filename, 64, 64, FALSE, NULL);
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 GError *err = NULL;
694 PidginSmiley *ps;
695 GdkPixbuf *image;
697 if ((error_message != NULL) || (len == 0)) {
698 return;
701 f = purple_mkstemp(&path, TRUE);
702 wc = fwrite(smileydata, len, 1, f);
703 if (wc != 1) {
704 purple_debug_warning("smiley_got_url", "Unable to write smiley data.\n");
705 fclose(f);
706 g_unlink(path);
707 g_free(path);
708 return;
710 fclose(f);
712 image = gdk_pixbuf_new_from_file(path, &err);
713 g_unlink(path);
714 g_free(path);
715 if (err) {
716 g_error_free(err);
717 return;
720 ps = pidgin_smiley_edit(dialog->window, NULL);
721 pidgin_smiley_editor_set_image(ps, image);
722 pidgin_smiley_editor_set_data(ps, g_memdup(smileydata, len), len);
725 static void
726 smiley_dnd_recv(GtkWidget *widget, GdkDragContext *dc, guint x, guint y,
727 GtkSelectionData *sd, guint info, guint t, gpointer user_data)
729 SmileyManager *dialog = user_data;
730 gchar *name = g_strchomp((gchar *)sd->data);
732 if ((sd->length >= 0) && (sd->format == 8)) {
733 /* Well, it looks like the drag event was cool.
734 * Let's do something with it */
736 if (!g_ascii_strncasecmp(name, "file://", 7)) {
737 GError *converr = NULL;
738 gchar *tmp;
739 PidginSmiley *ps;
740 /* It looks like we're dealing with a local file. Let's
741 * just try and read it */
742 if(!(tmp = g_filename_from_uri(name, NULL, &converr))) {
743 purple_debug_error("smiley dnd", "%s\n",
744 (converr ? converr->message :
745 "g_filename_from_uri error"));
746 return;
748 ps = pidgin_smiley_edit(dialog->window, NULL);
749 do_add_file_cb(tmp, ps);
750 if (gtk_image_get_pixbuf(GTK_IMAGE(ps->smiley_image)) == NULL)
751 gtk_dialog_response(GTK_DIALOG(ps->parent), GTK_RESPONSE_CANCEL);
752 g_free(tmp);
753 } else if (!g_ascii_strncasecmp(name, "http://", 7)) {
754 /* Oo, a web drag and drop. This is where things
755 * will start to get interesting */
756 purple_util_fetch_url(name, TRUE, NULL, FALSE, smiley_got_url, dialog);
757 } else if (!g_ascii_strncasecmp(name, "https://", 8)) {
758 /* purple_util_fetch_url() doesn't support HTTPS */
759 char *tmp = g_strdup(name + 1);
760 tmp[0] = 'h';
761 tmp[1] = 't';
762 tmp[2] = 't';
763 tmp[3] = 'p';
765 purple_util_fetch_url(tmp, TRUE, NULL, FALSE, smiley_got_url, dialog);
766 g_free(tmp);
769 gtk_drag_finish(dc, TRUE, FALSE, t);
772 gtk_drag_finish(dc, FALSE, FALSE, t);
775 static GtkWidget *smiley_list_create(SmileyManager *dialog)
777 GtkWidget *sw;
778 GtkWidget *treeview;
779 GtkTreeSelection *sel;
780 GtkTargetEntry te[3] = {
781 {"text/plain", 0, 0},
782 {"text/uri-list", 0, 1},
783 {"STRING", 0, 2}
786 sw = gtk_scrolled_window_new(NULL, NULL);
787 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
788 GTK_POLICY_AUTOMATIC,
789 GTK_POLICY_AUTOMATIC);
790 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
791 GTK_SHADOW_IN);
792 gtk_widget_show(sw);
794 /* Create the list model */
795 dialog->model = gtk_list_store_new(N_COL,
796 GDK_TYPE_PIXBUF, /* ICON */
797 G_TYPE_STRING, /* SHORTCUT */
798 G_TYPE_OBJECT /* SMILEY */
801 /* the actual treeview */
802 treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dialog->model));
803 dialog->treeview = treeview;
804 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);
805 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dialog->model), SHORTCUT, GTK_SORT_ASCENDING);
806 g_object_unref(G_OBJECT(dialog->model));
808 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
809 gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE);
810 gtk_container_add(GTK_CONTAINER(sw), treeview);
812 g_signal_connect(G_OBJECT(sel), "changed", G_CALLBACK(smile_selected_cb), dialog);
813 g_signal_connect(G_OBJECT(treeview), "row_activated", G_CALLBACK(smiley_edit_cb), dialog);
815 gtk_drag_dest_set(treeview,
816 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
817 te, G_N_ELEMENTS(te), GDK_ACTION_COPY | GDK_ACTION_MOVE);
818 g_signal_connect(G_OBJECT(treeview), "drag_data_received", G_CALLBACK(smiley_dnd_recv), dialog);
820 gtk_widget_show(treeview);
822 add_columns(treeview, dialog);
823 populate_smiley_list(dialog);
825 return sw;
828 static void refresh_list()
830 populate_smiley_list(smiley_manager);
833 static void smiley_manager_select_cb(GtkWidget *widget, gint resp, SmileyManager *dialog)
835 GtkTreeSelection *selection = NULL;
837 switch (resp) {
838 case GTK_RESPONSE_YES:
839 pidgin_smiley_edit(dialog->window, NULL);
840 break;
841 case GTK_RESPONSE_NO:
842 smiley_delete(dialog);
843 break;
844 case GTK_RESPONSE_DELETE_EVENT:
845 case GTK_RESPONSE_CLOSE:
846 gtk_widget_destroy(dialog->window);
847 g_free(smiley_manager);
848 smiley_manager = NULL;
849 break;
850 case PIDGIN_RESPONSE_MODIFY:
851 /* Find smiley of selection... */
852 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->treeview));
853 gtk_tree_selection_selected_foreach(selection, edit_selected_cb, dialog);
854 break;
855 default:
856 purple_debug_info("gtksmiley", "No valid selection\n");
857 break;
861 void pidgin_smiley_manager_show(void)
863 SmileyManager *dialog;
864 GtkWidget *win;
865 GtkWidget *sw;
866 GtkWidget *vbox;
868 if (smiley_manager) {
869 gtk_window_present(GTK_WINDOW(smiley_manager->window));
870 return;
873 dialog = g_new0(SmileyManager, 1);
874 smiley_manager = dialog;
876 dialog->window = win = gtk_dialog_new_with_buttons(
877 _("Custom Smiley Manager"),
878 NULL,
879 GTK_DIALOG_DESTROY_WITH_PARENT,
880 PIDGIN_STOCK_ADD, GTK_RESPONSE_YES,
881 PIDGIN_STOCK_MODIFY, PIDGIN_RESPONSE_MODIFY,
882 GTK_STOCK_DELETE, GTK_RESPONSE_NO,
883 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
884 NULL);
886 gtk_window_set_default_size(GTK_WINDOW(win), 50, 400);
887 gtk_window_set_role(GTK_WINDOW(win), "custom_smiley_manager");
888 gtk_container_set_border_width(GTK_CONTAINER(win),PIDGIN_HIG_BORDER);
889 gtk_dialog_set_response_sensitive(GTK_DIALOG(win), GTK_RESPONSE_NO, FALSE);
890 gtk_dialog_set_response_sensitive(GTK_DIALOG(win),
891 PIDGIN_RESPONSE_MODIFY, FALSE);
893 g_signal_connect(win, "response", G_CALLBACK(smiley_manager_select_cb),
894 dialog);
896 /* The vbox */
897 vbox = gtk_vbox_new(FALSE, PIDGIN_HIG_BORDER);
898 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(win)->vbox), vbox);
899 gtk_widget_show(vbox);
901 /* get the scrolled window with all stuff */
902 sw = smiley_list_create(dialog);
903 gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0);
904 gtk_widget_show(sw);
906 gtk_widget_show(win);