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