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