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