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