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