]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-ui-utils.c
Use accessor functions instead direct access (Fixes #586476)
[empathy.git] / libempathy-gtk / empathy-ui-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2002-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Richard Hult <richard@imendio.com>
23  *          Martyn Russell <martyn@imendio.com>
24  *          Xavier Claessens <xclaesse@gmail.com>
25  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
26  *
27  *          Part of this file is copied from GtkSourceView (gtksourceiter.c):
28  *          Paolo Maggi
29  *          Jeroen Zwartepoorte
30  */
31
32 #include <config.h>
33
34 #include <string.h>
35 #include <X11/Xatom.h>
36 #include <gdk/gdkx.h>
37 #include <glib/gi18n-lib.h>
38 #include <gtk/gtk.h>
39 #include <gio/gio.h>
40
41 #include <libmissioncontrol/mc-profile.h>
42
43 #include "empathy-ui-utils.h"
44 #include "empathy-images.h"
45 #include "empathy-conf.h"
46
47 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
48 #include <libempathy/empathy-debug.h>
49 #include <libempathy/empathy-utils.h>
50 #include <libempathy/empathy-dispatcher.h>
51 #include <libempathy/empathy-idle.h>
52 #include <libempathy/empathy-ft-factory.h>
53
54 #define SCHEMES "(https?|s?ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\
55                 "file|webcal|mailto)"
56 #define BODY "([^\\ \\n]+)"
57 #define END_BODY "([^\\ \\n]*[^,;\?><()\\ \"\\.\\n])"
58 #define URI_REGEX "("SCHEMES"://"END_BODY")" \
59                   "|((mailto:)?"BODY"@"BODY"\\."END_BODY")"\
60                   "|((www|ftp)\\."END_BODY")"
61
62 void
63 empathy_gtk_init (void)
64 {
65         static gboolean initialized = FALSE;
66
67         if (initialized)
68                 return;
69
70         empathy_init ();
71         gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
72                                            PKGDATADIR G_DIR_SEPARATOR_S "icons");
73
74         initialized = TRUE;
75 }
76
77 GRegex *
78 empathy_uri_regex_dup_singleton (void)
79 {
80         static GRegex *uri_regex = NULL;
81
82         /* We intentionally leak the regex so it's not recomputed */
83         if (!uri_regex) {
84                 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
85         }
86
87         return g_regex_ref (uri_regex);
88 }
89
90 static GtkBuilder *
91 builder_get_file_valist (const gchar *filename,
92                          const gchar *first_object,
93                          va_list      args)
94 {
95         GtkBuilder  *gui;
96         const gchar *name;
97         GObject    **object_ptr;
98         GError      *error = NULL;
99
100         DEBUG ("Loading file %s", filename);
101
102         gui = gtk_builder_new ();
103         if (!gtk_builder_add_from_file (gui, filename, &error)) {
104                 g_critical ("GtkBuilder Error: %s", error->message);
105                 g_clear_error (&error);
106                 g_object_unref (gui);
107                 return NULL;
108         }
109
110         for (name = first_object; name; name = va_arg (args, const gchar *)) {
111                 object_ptr = va_arg (args, GObject**);
112
113                 *object_ptr = gtk_builder_get_object (gui, name);
114
115                 if (!*object_ptr) {
116                         g_warning ("File is missing object '%s'.", name);
117                         continue;
118                 }
119         }
120
121         return gui;
122 }
123
124 GtkBuilder *
125 empathy_builder_get_file (const gchar *filename,
126                           const gchar *first_object,
127                           ...)
128 {
129         GtkBuilder *gui;
130         va_list     args;
131
132         va_start (args, first_object);
133         gui = builder_get_file_valist (filename, first_object, args);
134         va_end (args);
135
136         return gui;
137 }
138
139 void
140 empathy_builder_connect (GtkBuilder *gui,
141                          gpointer    user_data,
142                          gchar      *first_object,
143                          ...)
144 {
145         va_list      args;
146         const gchar *name;
147         const gchar *signal;
148         GObject     *object;
149         GCallback    callback;
150
151         va_start (args, first_object);
152         for (name = first_object; name; name = va_arg (args, const gchar *)) {
153                 signal = va_arg (args, const gchar *);
154                 callback = va_arg (args, GCallback);
155
156                 object = gtk_builder_get_object (gui, name);
157                 if (!object) {
158                         g_warning ("File is missing object '%s'.", name);
159                         continue;
160                 }
161
162                 g_signal_connect (object, signal, callback, user_data);
163         }
164
165         va_end (args);
166 }
167
168 GtkWidget *
169 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
170                                        GtkWidget  *widget)
171 {
172         /* On construction gui sinks the initial reference to widget. When gui
173          * is finalized it will drop its ref to widget. We take our own ref to
174          * prevent widget being finalised. The widget is forced to have a
175          * floating reference, like when it was initially unowned so that it can
176          * be used like any other GtkWidget. */
177
178         g_object_ref (widget);
179         g_object_force_floating (G_OBJECT (widget));
180         g_object_unref (gui);
181
182         return widget;
183 }
184
185 const gchar *
186 empathy_icon_name_from_account (EmpathyAccount *account)
187 {
188         McProfile *profile;
189
190         profile = empathy_account_get_profile (account);
191
192         return mc_profile_get_icon_name (profile);
193 }
194
195 const gchar *
196 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
197 {
198         switch (presence) {
199         case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
200                 return EMPATHY_IMAGE_AVAILABLE;
201         case TP_CONNECTION_PRESENCE_TYPE_BUSY:
202                 return EMPATHY_IMAGE_BUSY;
203         case TP_CONNECTION_PRESENCE_TYPE_AWAY:
204                 return EMPATHY_IMAGE_AWAY;
205         case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
206                 return EMPATHY_IMAGE_EXT_AWAY;
207         case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
208                 return EMPATHY_IMAGE_HIDDEN;
209         case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
210         case TP_CONNECTION_PRESENCE_TYPE_ERROR:
211         case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
212                 return EMPATHY_IMAGE_OFFLINE;
213         case TP_CONNECTION_PRESENCE_TYPE_UNSET:
214                 return NULL;
215         }
216
217         return NULL;
218 }
219
220 const gchar *
221 empathy_icon_name_for_contact (EmpathyContact *contact)
222 {
223         TpConnectionPresenceType presence;
224
225         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
226                               EMPATHY_IMAGE_OFFLINE);
227
228         presence = empathy_contact_get_presence (contact);
229         return empathy_icon_name_for_presence (presence);
230 }
231
232 GdkPixbuf *
233 empathy_pixbuf_from_data (gchar *data,
234                           gsize  data_size)
235 {
236         return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
237 }
238
239 GdkPixbuf *
240 empathy_pixbuf_from_data_and_mime (gchar  *data,
241                                    gsize   data_size,
242                                    gchar **mime_type)
243 {
244         GdkPixbufLoader *loader;
245         GdkPixbufFormat *format;
246         GdkPixbuf       *pixbuf = NULL;
247         gchar          **mime_types;
248         GError          *error = NULL;
249
250         if (!data) {
251                 return NULL;
252         }
253
254         loader = gdk_pixbuf_loader_new ();
255         if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
256                 DEBUG ("Failed to write to pixbuf loader: %s",
257                         error ? error->message : "No error given");
258                 goto out;
259         }
260         if (!gdk_pixbuf_loader_close (loader, &error)) {
261                 DEBUG ("Failed to close pixbuf loader: %s",
262                         error ? error->message : "No error given");
263                 goto out;
264         }
265
266         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
267         if (pixbuf) {
268                 g_object_ref (pixbuf);
269
270                 if (mime_type != NULL) {
271                         format = gdk_pixbuf_loader_get_format (loader);
272                         mime_types = gdk_pixbuf_format_get_mime_types (format);
273
274                         *mime_type = g_strdup (*mime_types);
275                         if (mime_types[1] != NULL) {
276                                 DEBUG ("Loader supports more than one mime "
277                                         "type! Picking the first one, %s",
278                                         *mime_type);
279                         }
280                         g_strfreev (mime_types);
281                 }
282         }
283
284 out:
285         g_clear_error (&error);
286         g_object_unref (loader);
287
288         return pixbuf;
289 }
290
291 struct SizeData {
292         gint     width;
293         gint     height;
294         gboolean preserve_aspect_ratio;
295 };
296
297 static void
298 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
299                                      int              width,
300                                      int              height,
301                                      struct SizeData *data)
302 {
303         g_return_if_fail (width > 0 && height > 0);
304
305         if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
306                 if (width < data->width && height < data->height) {
307                         width = width;
308                         height = height;
309                 }
310
311                 if (data->width < 0) {
312                         width = width * (double) data->height / (gdouble) height;
313                         height = data->height;
314                 } else if (data->height < 0) {
315                         height = height * (double) data->width / (double) width;
316                         width = data->width;
317                 } else if ((double) height * (double) data->width >
318                            (double) width * (double) data->height) {
319                         width = 0.5 + (double) width * (double) data->height / (double) height;
320                         height = data->height;
321                 } else {
322                         height = 0.5 + (double) height * (double) data->width / (double) width;
323                         width = data->width;
324                 }
325         } else {
326                 if (data->width > 0) {
327                         width = data->width;
328                 }
329
330                 if (data->height > 0) {
331                         height = data->height;
332                 }
333         }
334
335         gdk_pixbuf_loader_set_size (loader, width, height);
336 }
337
338 static void
339 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
340 {
341         gint width, height, rowstride;
342         guchar *pixels;
343
344         width = gdk_pixbuf_get_width (pixbuf);
345         height = gdk_pixbuf_get_height (pixbuf);
346         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
347         pixels = gdk_pixbuf_get_pixels (pixbuf);
348
349         if (width < 6 || height < 6) {
350                 return;
351         }
352
353         /* Top left */
354         pixels[3] = 0;
355         pixels[7] = 0x80;
356         pixels[11] = 0xC0;
357         pixels[rowstride + 3] = 0x80;
358         pixels[rowstride * 2 + 3] = 0xC0;
359
360         /* Top right */
361         pixels[width * 4 - 1] = 0;
362         pixels[width * 4 - 5] = 0x80;
363         pixels[width * 4 - 9] = 0xC0;
364         pixels[rowstride + (width * 4) - 1] = 0x80;
365         pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
366
367         /* Bottom left */
368         pixels[(height - 1) * rowstride + 3] = 0;
369         pixels[(height - 1) * rowstride + 7] = 0x80;
370         pixels[(height - 1) * rowstride + 11] = 0xC0;
371         pixels[(height - 2) * rowstride + 3] = 0x80;
372         pixels[(height - 3) * rowstride + 3] = 0xC0;
373
374         /* Bottom right */
375         pixels[height * rowstride - 1] = 0;
376         pixels[(height - 1) * rowstride - 1] = 0x80;
377         pixels[(height - 2) * rowstride - 1] = 0xC0;
378         pixels[height * rowstride - 5] = 0x80;
379         pixels[height * rowstride - 9] = 0xC0;
380 }
381
382 static gboolean
383 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
384 {
385         gint width, height, rowstride, i;
386         guchar *pixels;
387         guchar *row;
388
389         width = gdk_pixbuf_get_width (pixbuf);
390         height = gdk_pixbuf_get_height (pixbuf);
391         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
392         pixels = gdk_pixbuf_get_pixels (pixbuf);
393
394         row = pixels;
395         for (i = 3; i < rowstride; i+=4) {
396                 if (row[i] < 0xfe) {
397                         return FALSE;
398                 }
399         }
400
401         for (i = 1; i < height - 1; i++) {
402                 row = pixels + (i*rowstride);
403                 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
404                         return FALSE;
405                 }
406         }
407
408         row = pixels + ((height-1) * rowstride);
409         for (i = 3; i < rowstride; i+=4) {
410                 if (row[i] < 0xfe) {
411                         return FALSE;
412                 }
413         }
414
415         return TRUE;
416 }
417
418 GdkPixbuf *
419 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
420                                   gint          width,
421                                   gint          height)
422 {
423         GdkPixbuf        *pixbuf;
424         GdkPixbufLoader  *loader;
425         struct SizeData   data;
426         GError           *error = NULL;
427
428         if (!avatar) {
429                 return NULL;
430         }
431
432         data.width = width;
433         data.height = height;
434         data.preserve_aspect_ratio = TRUE;
435
436         loader = gdk_pixbuf_loader_new ();
437
438         g_signal_connect (loader, "size-prepared",
439                           G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
440                           &data);
441
442         if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
443                 g_warning ("Couldn't write avatar image:%p with "
444                            "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
445                            avatar->data, avatar->len, error->message);
446                 g_error_free (error);
447                 return NULL;
448         }
449
450         gdk_pixbuf_loader_close (loader, NULL);
451
452         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
453         if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
454                 GdkPixbuf *rounded_pixbuf;
455
456                 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
457                                                  gdk_pixbuf_get_width (pixbuf),
458                                                  gdk_pixbuf_get_height (pixbuf));
459                 gdk_pixbuf_copy_area (pixbuf, 0, 0,
460                                       gdk_pixbuf_get_width (pixbuf),
461                                       gdk_pixbuf_get_height (pixbuf),
462                                       rounded_pixbuf,
463                                       0, 0);
464                 pixbuf = rounded_pixbuf;
465         } else {
466                 g_object_ref (pixbuf);
467         }
468
469         if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
470                 empathy_avatar_pixbuf_roundify (pixbuf);
471         }
472
473         g_object_unref (loader);
474
475         return pixbuf;
476 }
477
478 GdkPixbuf *
479 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
480                                           gint           width,
481                                           gint           height)
482 {
483         EmpathyAvatar *avatar;
484
485         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
486
487         avatar = empathy_contact_get_avatar (contact);
488
489         return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
490 }
491
492 GdkPixbuf *
493 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
494 {
495         gint      width, height;
496         gdouble   factor;
497
498         width = gdk_pixbuf_get_width (pixbuf);
499         height = gdk_pixbuf_get_height (pixbuf);
500
501         if (width > 0 && (width > max_size || height > max_size)) {
502                 factor = (gdouble) max_size / MAX (width, height);
503
504                 width = width * factor;
505                 height = height * factor;
506
507                 return gdk_pixbuf_scale_simple (pixbuf,
508                                                 width, height,
509                                                 GDK_INTERP_HYPER);
510         }
511
512         return g_object_ref (pixbuf);
513 }
514
515 GdkPixbuf *
516 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
517                                      gint size)
518 {
519         GtkIconTheme *theme;
520         GdkPixbuf *pixbuf;
521         GError *error = NULL;
522
523         if (!icon_name) {
524                 return NULL;
525         }
526
527         theme = gtk_icon_theme_get_default ();
528
529         pixbuf = gtk_icon_theme_load_icon (theme,
530                                            icon_name,
531                                            size,
532                                            0,
533                                            &error);
534         if (error) {
535                 DEBUG ("Error loading icon: %s", error->message);
536                 g_clear_error (&error);
537         }
538
539         return pixbuf;
540 }
541
542 GdkPixbuf *
543 empathy_pixbuf_from_icon_name (const gchar *icon_name,
544                                GtkIconSize  icon_size)
545 {
546         gint  w, h;
547         gint  size = 48;
548
549         if (!icon_name) {
550                 return NULL;
551         }
552
553         if (gtk_icon_size_lookup (icon_size, &w, &h)) {
554                 size = (w + h) / 2;
555         }
556
557         return empathy_pixbuf_from_icon_name_sized (icon_name, size);
558 }
559
560 gchar *
561 empathy_filename_from_icon_name (const gchar *icon_name,
562                                  GtkIconSize  icon_size)
563 {
564         GtkIconTheme *icon_theme;
565         GtkIconInfo  *icon_info;
566         gint          w, h;
567         gint          size = 48;
568         gchar        *ret;
569
570         icon_theme = gtk_icon_theme_get_default ();
571
572         if (gtk_icon_size_lookup (icon_size, &w, &h)) {
573                 size = (w + h) / 2;
574         }
575
576         icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
577         ret = g_strdup (gtk_icon_info_get_filename (icon_info));
578         gtk_icon_info_free (icon_info);
579
580         return ret;
581 }
582
583 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
584  * that to make it easier to apply changes from the original code.
585  */
586 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
587
588 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
589  * decomposable character it consumes the decomposition length from the given
590  * offset.  So it's useful when the offset was calculated for the normalized
591  * version of str, but we need a pointer to str itself. */
592 static const gchar *
593 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
594 {
595         gchar *casefold, *normal;
596         const gchar *p, *q;
597
598         p = str;
599         while (offset > 0)
600         {
601                 q = g_utf8_next_char (p);
602                 casefold = g_utf8_casefold (p, q - p);
603                 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
604                 offset -= g_utf8_strlen (normal, -1);
605                 g_free (casefold);
606                 g_free (normal);
607                 p = q;
608         }
609         return p;
610 }
611
612 static const gchar *
613 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
614 {
615         gsize needle_len;
616         gsize haystack_len;
617         const gchar *ret = NULL;
618         gchar *p;
619         gchar *casefold;
620         gchar *caseless_haystack;
621         gint i;
622
623         g_return_val_if_fail (haystack != NULL, NULL);
624         g_return_val_if_fail (needle != NULL, NULL);
625
626         casefold = g_utf8_casefold (haystack, -1);
627         caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
628         g_free (casefold);
629
630         needle_len = g_utf8_strlen (needle, -1);
631         haystack_len = g_utf8_strlen (caseless_haystack, -1);
632
633         if (needle_len == 0)
634         {
635                 ret = (gchar *) haystack;
636                 goto finally_1;
637         }
638
639         if (haystack_len < needle_len)
640         {
641                 ret = NULL;
642                 goto finally_1;
643         }
644
645         p = (gchar *) caseless_haystack;
646         needle_len = strlen (needle);
647         i = 0;
648
649         while (*p)
650         {
651                 if ((strncmp (p, needle, needle_len) == 0))
652                 {
653                         ret = pointer_from_offset_skipping_decomp (haystack, i);
654                         goto finally_1;
655                 }
656
657                 p = g_utf8_next_char (p);
658                 i++;
659         }
660
661 finally_1:
662         g_free (caseless_haystack);
663
664         return ret;
665 }
666
667 static gboolean
668 g_utf8_caselessnmatch (const char *s1, const char *s2,
669                        gssize n1, gssize n2)
670 {
671         gchar *casefold;
672         gchar *normalized_s1;
673         gchar *normalized_s2;
674         gint len_s1;
675         gint len_s2;
676         gboolean ret = FALSE;
677
678         g_return_val_if_fail (s1 != NULL, FALSE);
679         g_return_val_if_fail (s2 != NULL, FALSE);
680         g_return_val_if_fail (n1 > 0, FALSE);
681         g_return_val_if_fail (n2 > 0, FALSE);
682
683         casefold = g_utf8_casefold (s1, n1);
684         normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
685         g_free (casefold);
686
687         casefold = g_utf8_casefold (s2, n2);
688         normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
689         g_free (casefold);
690
691         len_s1 = strlen (normalized_s1);
692         len_s2 = strlen (normalized_s2);
693
694         if (len_s1 < len_s2)
695                 goto finally_2;
696
697         ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
698
699 finally_2:
700         g_free (normalized_s1);
701         g_free (normalized_s2);
702
703         return ret;
704 }
705
706 static void
707 forward_chars_with_skipping (GtkTextIter *iter,
708                              gint         count,
709                              gboolean     skip_invisible,
710                              gboolean     skip_nontext,
711                              gboolean     skip_decomp)
712 {
713         gint i;
714
715         g_return_if_fail (count >= 0);
716
717         i = count;
718
719         while (i > 0)
720         {
721                 gboolean ignored = FALSE;
722
723                 /* minimal workaround to avoid the infinite loop of bug #168247.
724                  * It doesn't fix the problemjust the symptom...
725                  */
726                 if (gtk_text_iter_is_end (iter))
727                         return;
728
729                 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
730                         ignored = TRUE;
731
732                 if (!ignored && skip_invisible &&
733                     /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
734                         ignored = TRUE;
735
736                 if (!ignored && skip_decomp)
737                 {
738                         /* being UTF8 correct sucks; this accounts for extra
739                            offsets coming from canonical decompositions of
740                            UTF8 characters (e.g. accented characters) which
741                            g_utf8_normalize () performs */
742                         gchar *normal;
743                         gchar buffer[6];
744                         gint buffer_len;
745
746                         buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
747                         normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
748                         i -= (g_utf8_strlen (normal, -1) - 1);
749                         g_free (normal);
750                 }
751
752                 gtk_text_iter_forward_char (iter);
753
754                 if (!ignored)
755                         --i;
756         }
757 }
758
759 static gboolean
760 lines_match (const GtkTextIter *start,
761              const gchar      **lines,
762              gboolean           visible_only,
763              gboolean           slice,
764              GtkTextIter       *match_start,
765              GtkTextIter       *match_end)
766 {
767         GtkTextIter next;
768         gchar *line_text;
769         const gchar *found;
770         gint offset;
771
772         if (*lines == NULL || **lines == '\0')
773         {
774                 if (match_start)
775                         *match_start = *start;
776                 if (match_end)
777                         *match_end = *start;
778                 return TRUE;
779         }
780
781         next = *start;
782         gtk_text_iter_forward_line (&next);
783
784         /* No more text in buffer, but *lines is nonempty */
785         if (gtk_text_iter_equal (start, &next))
786                 return FALSE;
787
788         if (slice)
789         {
790                 if (visible_only)
791                         line_text = gtk_text_iter_get_visible_slice (start, &next);
792                 else
793                         line_text = gtk_text_iter_get_slice (start, &next);
794         }
795         else
796         {
797                 if (visible_only)
798                         line_text = gtk_text_iter_get_visible_text (start, &next);
799                 else
800                         line_text = gtk_text_iter_get_text (start, &next);
801         }
802
803         if (match_start) /* if this is the first line we're matching */
804         {
805                 found = g_utf8_strcasestr (line_text, *lines);
806         }
807         else
808         {
809                 /* If it's not the first line, we have to match from the
810                  * start of the line.
811                  */
812                 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
813                                            strlen (*lines)))
814                         found = line_text;
815                 else
816                         found = NULL;
817         }
818
819         if (found == NULL)
820         {
821                 g_free (line_text);
822                 return FALSE;
823         }
824
825         /* Get offset to start of search string */
826         offset = g_utf8_strlen (line_text, found - line_text);
827
828         next = *start;
829
830         /* If match start needs to be returned, set it to the
831          * start of the search string.
832          */
833         forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
834         if (match_start)
835         {
836                 *match_start = next;
837         }
838
839         /* Go to end of search string */
840         forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
841
842         g_free (line_text);
843
844         ++lines;
845
846         if (match_end)
847                 *match_end = next;
848
849         /* pass NULL for match_start, since we don't need to find the
850          * start again.
851          */
852         return lines_match (&next, lines, visible_only, slice, NULL, match_end);
853 }
854
855 /* strsplit () that retains the delimiter as part of the string. */
856 static gchar **
857 strbreakup (const char *string,
858             const char *delimiter,
859             gint        max_tokens)
860 {
861         GSList *string_list = NULL, *slist;
862         gchar **str_array, *s, *casefold, *new_string;
863         guint i, n = 1;
864
865         g_return_val_if_fail (string != NULL, NULL);
866         g_return_val_if_fail (delimiter != NULL, NULL);
867
868         if (max_tokens < 1)
869                 max_tokens = G_MAXINT;
870
871         s = strstr (string, delimiter);
872         if (s)
873         {
874                 guint delimiter_len = strlen (delimiter);
875
876                 do
877                 {
878                         guint len;
879
880                         len = s - string + delimiter_len;
881                         new_string = g_new (gchar, len + 1);
882                         strncpy (new_string, string, len);
883                         new_string[len] = 0;
884                         casefold = g_utf8_casefold (new_string, -1);
885                         g_free (new_string);
886                         new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
887                         g_free (casefold);
888                         string_list = g_slist_prepend (string_list, new_string);
889                         n++;
890                         string = s + delimiter_len;
891                         s = strstr (string, delimiter);
892                 } while (--max_tokens && s);
893         }
894
895         if (*string)
896         {
897                 n++;
898                 casefold = g_utf8_casefold (string, -1);
899                 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
900                 g_free (casefold);
901                 string_list = g_slist_prepend (string_list, new_string);
902         }
903
904         str_array = g_new (gchar*, n);
905
906         i = n - 1;
907
908         str_array[i--] = NULL;
909         for (slist = string_list; slist; slist = slist->next)
910                 str_array[i--] = slist->data;
911
912         g_slist_free (string_list);
913
914         return str_array;
915 }
916
917 gboolean
918 empathy_text_iter_forward_search (const GtkTextIter   *iter,
919                                  const gchar         *str,
920                                  GtkTextIter         *match_start,
921                                  GtkTextIter         *match_end,
922                                  const GtkTextIter   *limit)
923 {
924         gchar **lines = NULL;
925         GtkTextIter match;
926         gboolean retval = FALSE;
927         GtkTextIter search;
928         gboolean visible_only;
929         gboolean slice;
930
931         g_return_val_if_fail (iter != NULL, FALSE);
932         g_return_val_if_fail (str != NULL, FALSE);
933
934         if (limit && gtk_text_iter_compare (iter, limit) >= 0)
935                 return FALSE;
936
937         if (*str == '\0') {
938                 /* If we can move one char, return the empty string there */
939                 match = *iter;
940
941                 if (gtk_text_iter_forward_char (&match)) {
942                         if (limit && gtk_text_iter_equal (&match, limit)) {
943                                 return FALSE;
944                         }
945
946                         if (match_start) {
947                                 *match_start = match;
948                         }
949                         if (match_end) {
950                                 *match_end = match;
951                         }
952                         return TRUE;
953                 } else {
954                         return FALSE;
955                 }
956         }
957
958         visible_only = TRUE;
959         slice = FALSE;
960
961         /* locate all lines */
962         lines = strbreakup (str, "\n", -1);
963
964         search = *iter;
965
966         do {
967                 /* This loop has an inefficient worst-case, where
968                  * gtk_text_iter_get_text () is called repeatedly on
969                  * a single line.
970                  */
971                 GtkTextIter end;
972
973                 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
974                         break;
975                 }
976
977                 if (lines_match (&search, (const gchar**)lines,
978                                  visible_only, slice, &match, &end)) {
979                         if (limit == NULL ||
980                             (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
981                                 retval = TRUE;
982
983                                 if (match_start) {
984                                         *match_start = match;
985                                 }
986                                 if (match_end) {
987                                         *match_end = end;
988                                 }
989                         }
990                         break;
991                 }
992         } while (gtk_text_iter_forward_line (&search));
993
994         g_strfreev ((gchar **) lines);
995
996         return retval;
997 }
998
999 static const gchar *
1000 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1001 {
1002         gsize needle_len;
1003         gsize haystack_len;
1004         const gchar *ret = NULL;
1005         gchar *p;
1006         gchar *casefold;
1007         gchar *caseless_haystack;
1008         gint i;
1009
1010         g_return_val_if_fail (haystack != NULL, NULL);
1011         g_return_val_if_fail (needle != NULL, NULL);
1012
1013         casefold = g_utf8_casefold (haystack, -1);
1014         caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1015         g_free (casefold);
1016
1017         needle_len = g_utf8_strlen (needle, -1);
1018         haystack_len = g_utf8_strlen (caseless_haystack, -1);
1019
1020         if (needle_len == 0)
1021         {
1022                 ret = (gchar *) haystack;
1023                 goto finally_1;
1024         }
1025
1026         if (haystack_len < needle_len)
1027         {
1028                 ret = NULL;
1029                 goto finally_1;
1030         }
1031
1032         i = haystack_len - needle_len;
1033         p = g_utf8_offset_to_pointer (caseless_haystack, i);
1034         needle_len = strlen (needle);
1035
1036         while (p >= caseless_haystack)
1037         {
1038                 if (strncmp (p, needle, needle_len) == 0)
1039                 {
1040                         ret = pointer_from_offset_skipping_decomp (haystack, i);
1041                         goto finally_1;
1042                 }
1043
1044                 p = g_utf8_prev_char (p);
1045                 i--;
1046         }
1047
1048 finally_1:
1049         g_free (caseless_haystack);
1050
1051         return ret;
1052 }
1053
1054 static gboolean
1055 backward_lines_match (const GtkTextIter *start,
1056                       const gchar      **lines,
1057                       gboolean           visible_only,
1058                       gboolean           slice,
1059                       GtkTextIter       *match_start,
1060                       GtkTextIter       *match_end)
1061 {
1062         GtkTextIter line, next;
1063         gchar *line_text;
1064         const gchar *found;
1065         gint offset;
1066
1067         if (*lines == NULL || **lines == '\0')
1068         {
1069                 if (match_start)
1070                         *match_start = *start;
1071                 if (match_end)
1072                         *match_end = *start;
1073                 return TRUE;
1074         }
1075
1076         line = next = *start;
1077         if (gtk_text_iter_get_line_offset (&next) == 0)
1078         {
1079                 if (!gtk_text_iter_backward_line (&next))
1080                         return FALSE;
1081         }
1082         else
1083                 gtk_text_iter_set_line_offset (&next, 0);
1084
1085         if (slice)
1086         {
1087                 if (visible_only)
1088                         line_text = gtk_text_iter_get_visible_slice (&next, &line);
1089                 else
1090                         line_text = gtk_text_iter_get_slice (&next, &line);
1091         }
1092         else
1093         {
1094                 if (visible_only)
1095                         line_text = gtk_text_iter_get_visible_text (&next, &line);
1096                 else
1097                         line_text = gtk_text_iter_get_text (&next, &line);
1098         }
1099
1100         if (match_start) /* if this is the first line we're matching */
1101         {
1102                 found = g_utf8_strrcasestr (line_text, *lines);
1103         }
1104         else
1105         {
1106                 /* If it's not the first line, we have to match from the
1107                  * start of the line.
1108                  */
1109                 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1110                                            strlen (*lines)))
1111                         found = line_text;
1112                 else
1113                         found = NULL;
1114         }
1115
1116         if (found == NULL)
1117         {
1118                 g_free (line_text);
1119                 return FALSE;
1120         }
1121
1122         /* Get offset to start of search string */
1123         offset = g_utf8_strlen (line_text, found - line_text);
1124
1125         forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1126
1127         /* If match start needs to be returned, set it to the
1128          * start of the search string.
1129          */
1130         if (match_start)
1131         {
1132                 *match_start = next;
1133         }
1134
1135         /* Go to end of search string */
1136         forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1137
1138         g_free (line_text);
1139
1140         ++lines;
1141
1142         if (match_end)
1143                 *match_end = next;
1144
1145         /* try to match the rest of the lines forward, passing NULL
1146          * for match_start so lines_match will try to match the entire
1147          * line */
1148         return lines_match (&next, lines, visible_only,
1149                             slice, NULL, match_end);
1150 }
1151
1152 gboolean
1153 empathy_text_iter_backward_search (const GtkTextIter   *iter,
1154                                   const gchar         *str,
1155                                   GtkTextIter         *match_start,
1156                                   GtkTextIter         *match_end,
1157                                   const GtkTextIter   *limit)
1158 {
1159         gchar **lines = NULL;
1160         GtkTextIter match;
1161         gboolean retval = FALSE;
1162         GtkTextIter search;
1163         gboolean visible_only;
1164         gboolean slice;
1165
1166         g_return_val_if_fail (iter != NULL, FALSE);
1167         g_return_val_if_fail (str != NULL, FALSE);
1168
1169         if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1170                 return FALSE;
1171
1172         if (*str == '\0')
1173         {
1174                 /* If we can move one char, return the empty string there */
1175                 match = *iter;
1176
1177                 if (gtk_text_iter_backward_char (&match))
1178                 {
1179                         if (limit && gtk_text_iter_equal (&match, limit))
1180                                 return FALSE;
1181
1182                         if (match_start)
1183                                 *match_start = match;
1184                         if (match_end)
1185                                 *match_end = match;
1186                         return TRUE;
1187                 }
1188                 else
1189                 {
1190                         return FALSE;
1191                 }
1192         }
1193
1194         visible_only = TRUE;
1195         slice = TRUE;
1196
1197         /* locate all lines */
1198         lines = strbreakup (str, "\n", -1);
1199
1200         search = *iter;
1201
1202         while (TRUE)
1203         {
1204                 /* This loop has an inefficient worst-case, where
1205                  * gtk_text_iter_get_text () is called repeatedly on
1206                  * a single line.
1207                  */
1208                 GtkTextIter end;
1209
1210                 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1211                         break;
1212
1213                 if (backward_lines_match (&search, (const gchar**)lines,
1214                                           visible_only, slice, &match, &end))
1215                 {
1216                         if (limit == NULL || (limit &&
1217                                               gtk_text_iter_compare (&end, limit) > 0))
1218                         {
1219                                 retval = TRUE;
1220
1221                                 if (match_start)
1222                                         *match_start = match;
1223                                 if (match_end)
1224                                         *match_end = end;
1225                         }
1226                         break;
1227                 }
1228
1229                 if (gtk_text_iter_get_line_offset (&search) == 0)
1230                 {
1231                         if (!gtk_text_iter_backward_line (&search))
1232                                 break;
1233                 }
1234                 else
1235                 {
1236                         gtk_text_iter_set_line_offset (&search, 0);
1237                 }
1238         }
1239
1240         g_strfreev ((gchar **) lines);
1241
1242         return retval;
1243 }
1244
1245 gboolean
1246 empathy_window_get_is_visible (GtkWindow *window)
1247 {
1248         GdkWindowState  state;
1249         GdkWindow      *gdk_window;
1250
1251         g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1252
1253         gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1254         if (!gdk_window) {
1255                 return FALSE;
1256         }
1257
1258         state = gdk_window_get_state (gdk_window);
1259         if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1260                 return FALSE;
1261         }
1262
1263         return TRUE;
1264 }
1265
1266 void
1267 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1268 {
1269         GdkRectangle  icon_location;
1270         gulong        data[4];
1271         Display      *dpy;
1272         GdkWindow    *gdk_window;
1273
1274         gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1275         gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1276         dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1277
1278         data[0] = icon_location.x;
1279         data[1] = icon_location.y;
1280         data[2] = icon_location.width;
1281         data[3] = icon_location.height;
1282
1283         XChangeProperty (dpy,
1284                          GDK_WINDOW_XID (gdk_window),
1285                          gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1286                          "_NET_WM_ICON_GEOMETRY"),
1287                          XA_CARDINAL, 32, PropModeReplace,
1288                          (guchar *)&data, 4);
1289
1290         gtk_window_set_skip_taskbar_hint (window, TRUE);
1291         gtk_window_iconify (window);
1292 }
1293
1294 /* Takes care of moving the window to the current workspace. */
1295 void
1296 empathy_window_present (GtkWindow *window,
1297                         gboolean   steal_focus)
1298 {
1299         guint32 timestamp;
1300
1301         g_return_if_fail (GTK_IS_WINDOW (window));
1302
1303         /* There are three cases: hidden, visible, visible on another
1304          * workspace.
1305          */
1306
1307         if (!empathy_window_get_is_visible (window)) {
1308                 /* Hide it so present brings it to the current workspace. */
1309                 gtk_widget_hide (GTK_WIDGET (window));
1310         }
1311
1312         timestamp = gtk_get_current_event_time ();
1313         gtk_window_present_with_time (window, timestamp);
1314         gtk_window_set_skip_taskbar_hint (window, FALSE);
1315         /* FIXME: This shouldn't be required as gtk_window_present's doc says
1316          *        it deiconify automatically. */
1317         gtk_window_deiconify (window);
1318 }
1319
1320 GtkWindow *
1321 empathy_get_toplevel_window (GtkWidget *widget)
1322 {
1323         GtkWidget *toplevel;
1324
1325         g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1326
1327         toplevel = gtk_widget_get_toplevel (widget);
1328         if (GTK_IS_WINDOW (toplevel) &&
1329             GTK_WIDGET_TOPLEVEL (toplevel)) {
1330                 return GTK_WINDOW (toplevel);
1331         }
1332
1333         return NULL;
1334 }
1335
1336 /* The URL opening code can't handle schemeless strings, so we try to be
1337  * smart and add http if there is no scheme or doesn't look like a mail
1338  * address. This should work in most cases, and let us click on strings
1339  * like "www.gnome.org".
1340  */
1341 static gchar *
1342 fixup_url (const gchar *url)
1343 {
1344         if (g_str_has_prefix (url, "ghelp:") ||
1345             g_str_has_prefix (url, "mailto:") ||
1346             strstr (url, ":/")) {
1347                 return NULL;
1348         }
1349
1350         if (strstr (url, "@")) {
1351                 return g_strdup_printf ("mailto:%s", url);
1352         }
1353
1354         return g_strdup_printf ("http://%s", url);
1355 }
1356
1357 void
1358 empathy_url_show (GtkWidget *parent,
1359                   const char *url)
1360 {
1361         gchar  *real_url;
1362         GError *error = NULL;
1363
1364         real_url = fixup_url (url);
1365         if (real_url) {
1366                 url = real_url;
1367         }
1368
1369         gtk_show_uri (gtk_widget_get_screen (parent), url,
1370                       gtk_get_current_event_time (), &error);
1371
1372         if (error) {
1373                 GtkWidget *dialog;
1374
1375                 dialog = gtk_message_dialog_new (NULL, 0,
1376                                                  GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1377                                                  _("Unable to open URI"));
1378                 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1379                                                           "%s", error->message);
1380
1381                 g_signal_connect (dialog, "response",
1382                                   G_CALLBACK (gtk_widget_destroy),
1383                                   NULL);
1384                 gtk_window_present (GTK_WINDOW (dialog));
1385
1386                 g_clear_error (&error);
1387         }
1388
1389         g_free (real_url);
1390 }
1391
1392 static void
1393 link_button_hook (GtkLinkButton *button,
1394                   const gchar *link,
1395                   gpointer user_data)
1396 {
1397         empathy_url_show (GTK_WIDGET (button), link);
1398 }
1399
1400 GtkWidget *
1401 empathy_link_button_new (const gchar *url,
1402                         const gchar *title)
1403 {
1404         static gboolean hook = FALSE;
1405
1406         if (!hook) {
1407                 hook = TRUE;
1408                 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1409         }
1410
1411         return gtk_link_button_new_with_label (url, title);
1412 }
1413
1414 void
1415 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1416                                         GCallback  callback,
1417                                         gpointer   user_data,
1418                                         gboolean   active)
1419 {
1420         g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1421
1422         g_signal_handlers_block_by_func (widget, callback, user_data);
1423         g_object_set (widget, "active", active, NULL);
1424         g_signal_handlers_unblock_by_func (widget, callback, user_data);
1425 }
1426
1427 static void
1428 file_manager_send_file_response_cb (GtkDialog      *widget,
1429                                     gint            response_id,
1430                                     EmpathyContact *contact)
1431 {
1432         EmpathyFTFactory *factory;
1433         GFile *file;
1434         gchar *uri;
1435         GtkRecentManager *manager;
1436
1437         if (response_id == GTK_RESPONSE_OK) {
1438                 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1439                 uri = g_file_get_uri (file);
1440
1441                 factory = empathy_ft_factory_dup_singleton ();
1442
1443                 empathy_ft_factory_new_transfer_outgoing (factory, contact,
1444                                                           file);
1445
1446                 manager = gtk_recent_manager_get_default ();
1447                 gtk_recent_manager_add_item (manager, uri);
1448
1449                 g_free (uri);
1450                 g_object_unref (factory);
1451                 g_object_unref (file);
1452         }
1453
1454         gtk_widget_destroy (GTK_WIDGET (widget));
1455 }
1456
1457 void
1458 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1459 {
1460         GtkWidget               *widget;
1461         GtkWidget               *button;
1462
1463         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1464
1465         DEBUG ("Creating selection file chooser");
1466
1467         widget = gtk_file_chooser_dialog_new (_("Select a file"),
1468                                               NULL,
1469                                               GTK_FILE_CHOOSER_ACTION_OPEN,
1470                                               GTK_STOCK_CANCEL,
1471                                               GTK_RESPONSE_CANCEL,
1472                                               NULL);
1473
1474         /* send button */
1475         button = gtk_button_new_with_mnemonic (_("_Send"));
1476         gtk_button_set_image (GTK_BUTTON (button),
1477                 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1478                                               GTK_ICON_SIZE_BUTTON));
1479         gtk_widget_show (button);
1480         gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1481                                       GTK_RESPONSE_OK);
1482         GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1483         gtk_dialog_set_default_response (GTK_DIALOG (widget),
1484                                          GTK_RESPONSE_OK);
1485
1486         g_signal_connect (widget, "response",
1487                           G_CALLBACK (file_manager_send_file_response_cb),
1488                           contact);
1489
1490         gtk_widget_show (widget);
1491 }
1492
1493 static void
1494 file_manager_receive_file_response_cb (GtkDialog *dialog,
1495                                        GtkResponseType response,
1496                                        EmpathyFTHandler *handler)
1497 {
1498         EmpathyFTFactory *factory;
1499         GFile *file;
1500
1501         if (response == GTK_RESPONSE_OK) {
1502                 factory = empathy_ft_factory_dup_singleton ();
1503                 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1504
1505                 empathy_ft_factory_set_destination_for_incoming_handler
1506                         (factory, handler, file);
1507
1508                 g_object_unref (factory);
1509                 g_object_unref (file);
1510         } else {
1511                 /* unref the handler, as we dismissed the file chooser,
1512                  * and refused the transfer.
1513                  */
1514                 g_object_unref (handler);
1515         }
1516
1517         gtk_widget_destroy (GTK_WIDGET (dialog));
1518 }
1519
1520 void
1521 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1522 {
1523         GtkWidget *widget;
1524
1525         widget = gtk_file_chooser_dialog_new (_("Select a destination"),
1526                                               NULL,
1527                                               GTK_FILE_CHOOSER_ACTION_SAVE,
1528                                               GTK_STOCK_CANCEL,
1529                                               GTK_RESPONSE_CANCEL,
1530                                               GTK_STOCK_SAVE,
1531                                               GTK_RESPONSE_OK,
1532                                               NULL);
1533         gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1534                 empathy_ft_handler_get_filename (handler));
1535         gtk_file_chooser_set_do_overwrite_confirmation
1536                 (GTK_FILE_CHOOSER (widget), TRUE);
1537
1538         g_signal_connect (widget, "response",
1539                 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1540
1541         gtk_widget_show (widget);
1542 }