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