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