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