]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-ui-utils.c
Remove per-state "Custom message..." item and add one on the bottom of the
[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         case MC_PRESENCE_OFFLINE:
355         case MC_PRESENCE_UNSET:
356                 return EMPATHY_IMAGE_OFFLINE;
357         default:
358                 g_assert_not_reached ();
359         }
360
361         return NULL;
362 }
363
364 const gchar *
365 empathy_icon_name_for_presence (EmpathyPresence *presence)
366 {
367         McPresence state;
368
369         g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
370                               EMPATHY_IMAGE_OFFLINE);
371
372         state = empathy_presence_get_state (presence);
373
374         return empathy_icon_name_for_presence_state (state);
375 }
376
377 const gchar *
378 empathy_icon_name_for_contact (EmpathyContact *contact)
379 {
380         EmpathyPresence     *presence;
381         EmpathySubscription  subscription;
382
383         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
384                               EMPATHY_IMAGE_OFFLINE);
385
386         presence = empathy_contact_get_presence (contact);
387         if (presence) {
388                 return empathy_icon_name_for_presence (presence);
389         }
390
391         subscription = empathy_contact_get_subscription (contact);
392         if (!(subscription & EMPATHY_SUBSCRIPTION_FROM)) {
393                 return EMPATHY_IMAGE_PENDING;
394         }
395
396         return EMPATHY_IMAGE_OFFLINE;
397 }
398
399 static void
400 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
401                                      int              width,
402                                      int              height,
403                                      struct SizeData *data)
404 {
405         g_return_if_fail (width > 0 && height > 0);
406
407         if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
408                 if (width < data->width && height < data->height) {
409                         width = width;
410                         height = height;
411                 }
412
413                 if (data->width < 0) {
414                         width = width * (double) data->height / (gdouble) height;
415                         height = data->height;
416                 } else if (data->height < 0) {
417                         height = height * (double) data->width / (double) width;
418                         width = data->width;
419                 } else if ((double) height * (double) data->width >
420                            (double) width * (double) data->height) {
421                         width = 0.5 + (double) width * (double) data->height / (double) height;
422                         height = data->height;
423                 } else {
424                         height = 0.5 + (double) height * (double) data->width / (double) width;
425                         width = data->width;
426                 }
427         } else {
428                 if (data->width > 0) {
429                         width = data->width;
430                 }
431
432                 if (data->height > 0) {
433                         height = data->height;
434                 }
435         }
436
437         gdk_pixbuf_loader_set_size (loader, width, height);
438 }
439
440 static void
441 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
442 {
443         gint width, height, rowstride;
444         guchar *pixels;
445
446         width = gdk_pixbuf_get_width (pixbuf);
447         height = gdk_pixbuf_get_height (pixbuf);
448         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
449         pixels = gdk_pixbuf_get_pixels (pixbuf);
450
451         if (width < 6 || height < 6) {
452                 return;
453         }
454
455         /* Top left */
456         pixels[3] = 0;
457         pixels[7] = 0x80;
458         pixels[11] = 0xC0;
459         pixels[rowstride + 3] = 0x80;
460         pixels[rowstride * 2 + 3] = 0xC0;
461
462         /* Top right */
463         pixels[width * 4 - 1] = 0;
464         pixels[width * 4 - 5] = 0x80;
465         pixels[width * 4 - 9] = 0xC0;
466         pixels[rowstride + (width * 4) - 1] = 0x80;
467         pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
468
469         /* Bottom left */
470         pixels[(height - 1) * rowstride + 3] = 0;
471         pixels[(height - 1) * rowstride + 7] = 0x80;
472         pixels[(height - 1) * rowstride + 11] = 0xC0;
473         pixels[(height - 2) * rowstride + 3] = 0x80;
474         pixels[(height - 3) * rowstride + 3] = 0xC0;
475
476         /* Bottom right */
477         pixels[height * rowstride - 1] = 0;
478         pixels[(height - 1) * rowstride - 1] = 0x80;
479         pixels[(height - 2) * rowstride - 1] = 0xC0;
480         pixels[height * rowstride - 5] = 0x80;
481         pixels[height * rowstride - 9] = 0xC0;
482 }
483
484 static gboolean
485 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
486 {
487         gint width, height, rowstride, i;
488         guchar *pixels;
489         guchar *row;
490
491         width = gdk_pixbuf_get_width (pixbuf);
492         height = gdk_pixbuf_get_height (pixbuf);
493         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
494         pixels = gdk_pixbuf_get_pixels (pixbuf);
495
496         row = pixels;
497         for (i = 3; i < rowstride; i+=4) {
498                 if (row[i] < 0xfe) {
499                         return FALSE;
500                 }
501         }
502
503         for (i = 1; i < height - 1; i++) {
504                 row = pixels + (i*rowstride);
505                 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
506                         return FALSE;
507                 }
508         }
509
510         row = pixels + ((height-1) * rowstride);
511         for (i = 3; i < rowstride; i+=4) {
512                 if (row[i] < 0xfe) {
513                         return FALSE;
514                 }
515         }
516
517         return TRUE;
518 }
519
520 GdkPixbuf *
521 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
522                                   gint          width,
523                                   gint          height)
524 {
525         GdkPixbuf        *pixbuf;
526         GdkPixbufLoader  *loader;
527         struct SizeData   data;
528         GError           *error = NULL;
529
530         if (!avatar) {
531                 return NULL;
532         }
533
534         data.width = width;
535         data.height = height;
536         data.preserve_aspect_ratio = TRUE;
537
538         loader = gdk_pixbuf_loader_new ();
539
540         g_signal_connect (loader, "size-prepared",
541                           G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
542                           &data);
543
544         if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
545                 g_warning ("Couldn't write avatar image:%p with "
546                            "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
547                            avatar->data, avatar->len, error->message);
548                 g_error_free (error);
549                 return NULL;
550         }
551
552         gdk_pixbuf_loader_close (loader, NULL);
553
554         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
555         if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
556                 GdkPixbuf *rounded_pixbuf;
557
558                 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
559                                                  gdk_pixbuf_get_width (pixbuf),
560                                                  gdk_pixbuf_get_height (pixbuf));
561                 gdk_pixbuf_copy_area (pixbuf, 0, 0,
562                                       gdk_pixbuf_get_width (pixbuf),
563                                       gdk_pixbuf_get_height (pixbuf),
564                                       rounded_pixbuf,
565                                       0, 0);
566                 pixbuf = rounded_pixbuf;
567         } else {
568                 g_object_ref (pixbuf);
569         }
570
571         if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
572                 empathy_avatar_pixbuf_roundify (pixbuf);
573         }
574
575         g_object_unref (loader);
576
577         return pixbuf;
578 }
579
580 GdkPixbuf *
581 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
582                                           gint           width,
583                                           gint           height)
584 {
585         EmpathyAvatar *avatar;
586
587         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
588
589         avatar = empathy_contact_get_avatar (contact);
590
591         return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
592 }
593
594 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
595  * that to make it easier to apply changes from the original code.
596  */
597 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
598
599 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
600  * decomposable character it consumes the decomposition length from the given
601  * offset.  So it's useful when the offset was calculated for the normalized
602  * version of str, but we need a pointer to str itself. */
603 static const gchar *
604 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
605 {
606         gchar *casefold, *normal;
607         const gchar *p, *q;
608
609         p = str;
610         while (offset > 0)
611         {
612                 q = g_utf8_next_char (p);
613                 casefold = g_utf8_casefold (p, q - p);
614                 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
615                 offset -= g_utf8_strlen (normal, -1);
616                 g_free (casefold);
617                 g_free (normal);
618                 p = q;
619         }
620         return p;
621 }
622
623 static const gchar *
624 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
625 {
626         gsize needle_len;
627         gsize haystack_len;
628         const gchar *ret = NULL;
629         gchar *p;
630         gchar *casefold;
631         gchar *caseless_haystack;
632         gint i;
633
634         g_return_val_if_fail (haystack != NULL, NULL);
635         g_return_val_if_fail (needle != NULL, NULL);
636
637         casefold = g_utf8_casefold (haystack, -1);
638         caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
639         g_free (casefold);
640
641         needle_len = g_utf8_strlen (needle, -1);
642         haystack_len = g_utf8_strlen (caseless_haystack, -1);
643
644         if (needle_len == 0)
645         {
646                 ret = (gchar *)haystack;
647                 goto finally_1;
648         }
649
650         if (haystack_len < needle_len)
651         {
652                 ret = NULL;
653                 goto finally_1;
654         }
655
656         p = (gchar*)caseless_haystack;
657         needle_len = strlen (needle);
658         i = 0;
659
660         while (*p)
661         {
662                 if ((strncmp (p, needle, needle_len) == 0))
663                 {
664                         ret = pointer_from_offset_skipping_decomp (haystack, i);
665                         goto finally_1;
666                 }
667
668                 p = g_utf8_next_char (p);
669                 i++;
670         }
671
672 finally_1:
673         g_free (caseless_haystack);
674
675         return ret;
676 }
677
678 static gboolean
679 g_utf8_caselessnmatch (const char *s1, const char *s2,
680                        gssize n1, gssize n2)
681 {
682         gchar *casefold;
683         gchar *normalized_s1;
684         gchar *normalized_s2;
685         gint len_s1;
686         gint len_s2;
687         gboolean ret = FALSE;
688
689         g_return_val_if_fail (s1 != NULL, FALSE);
690         g_return_val_if_fail (s2 != NULL, FALSE);
691         g_return_val_if_fail (n1 > 0, FALSE);
692         g_return_val_if_fail (n2 > 0, FALSE);
693
694         casefold = g_utf8_casefold (s1, n1);
695         normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
696         g_free (casefold);
697
698         casefold = g_utf8_casefold (s2, n2);
699         normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
700         g_free (casefold);
701
702         len_s1 = strlen (normalized_s1);
703         len_s2 = strlen (normalized_s2);
704
705         if (len_s1 < len_s2)
706                 goto finally_2;
707
708         ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
709
710 finally_2:
711         g_free (normalized_s1);
712         g_free (normalized_s2);
713
714         return ret;
715 }
716
717 static void
718 forward_chars_with_skipping (GtkTextIter *iter,
719                              gint         count,
720                              gboolean     skip_invisible,
721                              gboolean     skip_nontext,
722                              gboolean     skip_decomp)
723 {
724         gint i;
725
726         g_return_if_fail (count >= 0);
727
728         i = count;
729
730         while (i > 0)
731         {
732                 gboolean ignored = FALSE;
733
734                 /* minimal workaround to avoid the infinite loop of bug #168247.
735                  * It doesn't fix the problemjust the symptom...
736                  */
737                 if (gtk_text_iter_is_end (iter))
738                         return;
739
740                 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
741                         ignored = TRUE;
742
743                 if (!ignored && skip_invisible &&
744                     /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
745                         ignored = TRUE;
746
747                 if (!ignored && skip_decomp)
748                 {
749                         /* being UTF8 correct sucks; this accounts for extra
750                            offsets coming from canonical decompositions of
751                            UTF8 characters (e.g. accented characters) which
752                            g_utf8_normalize() performs */
753                         gchar *normal;
754                         gchar buffer[6];
755                         gint buffer_len;
756
757                         buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
758                         normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
759                         i -= (g_utf8_strlen (normal, -1) - 1);
760                         g_free (normal);
761                 }
762
763                 gtk_text_iter_forward_char (iter);
764
765                 if (!ignored)
766                         --i;
767         }
768 }
769
770 static gboolean
771 lines_match (const GtkTextIter *start,
772              const gchar      **lines,
773              gboolean           visible_only,
774              gboolean           slice,
775              GtkTextIter       *match_start,
776              GtkTextIter       *match_end)
777 {
778         GtkTextIter next;
779         gchar *line_text;
780         const gchar *found;
781         gint offset;
782
783         if (*lines == NULL || **lines == '\0')
784         {
785                 if (match_start)
786                         *match_start = *start;
787                 if (match_end)
788                         *match_end = *start;
789                 return TRUE;
790         }
791
792         next = *start;
793         gtk_text_iter_forward_line (&next);
794
795         /* No more text in buffer, but *lines is nonempty */
796         if (gtk_text_iter_equal (start, &next))
797                 return FALSE;
798
799         if (slice)
800         {
801                 if (visible_only)
802                         line_text = gtk_text_iter_get_visible_slice (start, &next);
803                 else
804                         line_text = gtk_text_iter_get_slice (start, &next);
805         }
806         else
807         {
808                 if (visible_only)
809                         line_text = gtk_text_iter_get_visible_text (start, &next);
810                 else
811                         line_text = gtk_text_iter_get_text (start, &next);
812         }
813
814         if (match_start) /* if this is the first line we're matching */
815         {
816                 found = g_utf8_strcasestr (line_text, *lines);
817         }
818         else
819         {
820                 /* If it's not the first line, we have to match from the
821                  * start of the line.
822                  */
823                 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
824                                            strlen (*lines)))
825                         found = line_text;
826                 else
827                         found = NULL;
828         }
829
830         if (found == NULL)
831         {
832                 g_free (line_text);
833                 return FALSE;
834         }
835
836         /* Get offset to start of search string */
837         offset = g_utf8_strlen (line_text, found - line_text);
838
839         next = *start;
840
841         /* If match start needs to be returned, set it to the
842          * start of the search string.
843          */
844         forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
845         if (match_start)
846         {
847                 *match_start = next;
848         }
849
850         /* Go to end of search string */
851         forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
852
853         g_free (line_text);
854
855         ++lines;
856
857         if (match_end)
858                 *match_end = next;
859
860         /* pass NULL for match_start, since we don't need to find the
861          * start again.
862          */
863         return lines_match (&next, lines, visible_only, slice, NULL, match_end);
864 }
865
866 /* strsplit () that retains the delimiter as part of the string. */
867 static gchar **
868 strbreakup (const char *string,
869             const char *delimiter,
870             gint        max_tokens)
871 {
872         GSList *string_list = NULL, *slist;
873         gchar **str_array, *s, *casefold, *new_string;
874         guint i, n = 1;
875
876         g_return_val_if_fail (string != NULL, NULL);
877         g_return_val_if_fail (delimiter != NULL, NULL);
878
879         if (max_tokens < 1)
880                 max_tokens = G_MAXINT;
881
882         s = strstr (string, delimiter);
883         if (s)
884         {
885                 guint delimiter_len = strlen (delimiter);
886
887                 do
888                 {
889                         guint len;
890
891                         len = s - string + delimiter_len;
892                         new_string = g_new (gchar, len + 1);
893                         strncpy (new_string, string, len);
894                         new_string[len] = 0;
895                         casefold = g_utf8_casefold (new_string, -1);
896                         g_free (new_string);
897                         new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
898                         g_free (casefold);
899                         string_list = g_slist_prepend (string_list, new_string);
900                         n++;
901                         string = s + delimiter_len;
902                         s = strstr (string, delimiter);
903                 } while (--max_tokens && s);
904         }
905
906         if (*string)
907         {
908                 n++;
909                 casefold = g_utf8_casefold (string, -1);
910                 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
911                 g_free (casefold);
912                 string_list = g_slist_prepend (string_list, new_string);
913         }
914
915         str_array = g_new (gchar*, n);
916
917         i = n - 1;
918
919         str_array[i--] = NULL;
920         for (slist = string_list; slist; slist = slist->next)
921                 str_array[i--] = slist->data;
922
923         g_slist_free (string_list);
924
925         return str_array;
926 }
927
928 gboolean
929 empathy_text_iter_forward_search (const GtkTextIter   *iter,
930                                  const gchar         *str,
931                                  GtkTextIter         *match_start,
932                                  GtkTextIter         *match_end,
933                                  const GtkTextIter   *limit)
934 {
935         gchar **lines = NULL;
936         GtkTextIter match;
937         gboolean retval = FALSE;
938         GtkTextIter search;
939         gboolean visible_only;
940         gboolean slice;
941
942         g_return_val_if_fail (iter != NULL, FALSE);
943         g_return_val_if_fail (str != NULL, FALSE);
944
945         if (limit && gtk_text_iter_compare (iter, limit) >= 0)
946                 return FALSE;
947
948         if (*str == '\0') {
949                 /* If we can move one char, return the empty string there */
950                 match = *iter;
951
952                 if (gtk_text_iter_forward_char (&match)) {
953                         if (limit && gtk_text_iter_equal (&match, limit)) {
954                                 return FALSE;
955                         }
956
957                         if (match_start) {
958                                 *match_start = match;
959                         }
960                         if (match_end) {
961                                 *match_end = match;
962                         }
963                         return TRUE;
964                 } else {
965                         return FALSE;
966                 }
967         }
968
969         visible_only = TRUE;
970         slice = FALSE;
971
972         /* locate all lines */
973         lines = strbreakup (str, "\n", -1);
974
975         search = *iter;
976
977         do {
978                 /* This loop has an inefficient worst-case, where
979                  * gtk_text_iter_get_text () is called repeatedly on
980                  * a single line.
981                  */
982                 GtkTextIter end;
983
984                 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
985                         break;
986                 }
987
988                 if (lines_match (&search, (const gchar**)lines,
989                                  visible_only, slice, &match, &end)) {
990                         if (limit == NULL ||
991                             (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
992                                 retval = TRUE;
993
994                                 if (match_start) {
995                                         *match_start = match;
996                                 }
997                                 if (match_end) {
998                                         *match_end = end;
999                                 }
1000                         }
1001                         break;
1002                 }
1003         } while (gtk_text_iter_forward_line (&search));
1004
1005         g_strfreev ((gchar**)lines);
1006
1007         return retval;
1008 }
1009
1010 static const gchar *
1011 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1012 {
1013         gsize needle_len;
1014         gsize haystack_len;
1015         const gchar *ret = NULL;
1016         gchar *p;
1017         gchar *casefold;
1018         gchar *caseless_haystack;
1019         gint i;
1020
1021         g_return_val_if_fail (haystack != NULL, NULL);
1022         g_return_val_if_fail (needle != NULL, NULL);
1023
1024         casefold = g_utf8_casefold (haystack, -1);
1025         caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1026         g_free (casefold);
1027
1028         needle_len = g_utf8_strlen (needle, -1);
1029         haystack_len = g_utf8_strlen (caseless_haystack, -1);
1030
1031         if (needle_len == 0)
1032         {
1033                 ret = (gchar *)haystack;
1034                 goto finally_1;
1035         }
1036
1037         if (haystack_len < needle_len)
1038         {
1039                 ret = NULL;
1040                 goto finally_1;
1041         }
1042
1043         i = haystack_len - needle_len;
1044         p = g_utf8_offset_to_pointer (caseless_haystack, i);
1045         needle_len = strlen (needle);
1046
1047         while (p >= caseless_haystack)
1048         {
1049                 if (strncmp (p, needle, needle_len) == 0)
1050                 {
1051                         ret = pointer_from_offset_skipping_decomp (haystack, i);
1052                         goto finally_1;
1053                 }
1054
1055                 p = g_utf8_prev_char (p);
1056                 i--;
1057         }
1058
1059 finally_1:
1060         g_free (caseless_haystack);
1061
1062         return ret;
1063 }
1064
1065 static gboolean
1066 backward_lines_match (const GtkTextIter *start,
1067                       const gchar      **lines,
1068                       gboolean           visible_only,
1069                       gboolean           slice,
1070                       GtkTextIter       *match_start,
1071                       GtkTextIter       *match_end)
1072 {
1073         GtkTextIter line, next;
1074         gchar *line_text;
1075         const gchar *found;
1076         gint offset;
1077
1078         if (*lines == NULL || **lines == '\0')
1079         {
1080                 if (match_start)
1081                         *match_start = *start;
1082                 if (match_end)
1083                         *match_end = *start;
1084                 return TRUE;
1085         }
1086
1087         line = next = *start;
1088         if (gtk_text_iter_get_line_offset (&next) == 0)
1089         {
1090                 if (!gtk_text_iter_backward_line (&next))
1091                         return FALSE;
1092         }
1093         else
1094                 gtk_text_iter_set_line_offset (&next, 0);
1095
1096         if (slice)
1097         {
1098                 if (visible_only)
1099                         line_text = gtk_text_iter_get_visible_slice (&next, &line);
1100                 else
1101                         line_text = gtk_text_iter_get_slice (&next, &line);
1102         }
1103         else
1104         {
1105                 if (visible_only)
1106                         line_text = gtk_text_iter_get_visible_text (&next, &line);
1107                 else
1108                         line_text = gtk_text_iter_get_text (&next, &line);
1109         }
1110
1111         if (match_start) /* if this is the first line we're matching */
1112         {
1113                 found = g_utf8_strrcasestr (line_text, *lines);
1114         }
1115         else
1116         {
1117                 /* If it's not the first line, we have to match from the
1118                  * start of the line.
1119                  */
1120                 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1121                                            strlen (*lines)))
1122                         found = line_text;
1123                 else
1124                         found = NULL;
1125         }
1126
1127         if (found == NULL)
1128         {
1129                 g_free (line_text);
1130                 return FALSE;
1131         }
1132
1133         /* Get offset to start of search string */
1134         offset = g_utf8_strlen (line_text, found - line_text);
1135
1136         forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1137
1138         /* If match start needs to be returned, set it to the
1139          * start of the search string.
1140          */
1141         if (match_start)
1142         {
1143                 *match_start = next;
1144         }
1145
1146         /* Go to end of search string */
1147         forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1148
1149         g_free (line_text);
1150
1151         ++lines;
1152
1153         if (match_end)
1154                 *match_end = next;
1155
1156         /* try to match the rest of the lines forward, passing NULL
1157          * for match_start so lines_match will try to match the entire
1158          * line */
1159         return lines_match (&next, lines, visible_only,
1160                             slice, NULL, match_end);
1161 }
1162
1163 gboolean
1164 empathy_text_iter_backward_search (const GtkTextIter   *iter,
1165                                   const gchar         *str,
1166                                   GtkTextIter         *match_start,
1167                                   GtkTextIter         *match_end,
1168                                   const GtkTextIter   *limit)
1169 {
1170         gchar **lines = NULL;
1171         GtkTextIter match;
1172         gboolean retval = FALSE;
1173         GtkTextIter search;
1174         gboolean visible_only;
1175         gboolean slice;
1176
1177         g_return_val_if_fail (iter != NULL, FALSE);
1178         g_return_val_if_fail (str != NULL, FALSE);
1179
1180         if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1181                 return FALSE;
1182
1183         if (*str == '\0')
1184         {
1185                 /* If we can move one char, return the empty string there */
1186                 match = *iter;
1187
1188                 if (gtk_text_iter_backward_char (&match))
1189                 {
1190                         if (limit && gtk_text_iter_equal (&match, limit))
1191                                 return FALSE;
1192
1193                         if (match_start)
1194                                 *match_start = match;
1195                         if (match_end)
1196                                 *match_end = match;
1197                         return TRUE;
1198                 }
1199                 else
1200                 {
1201                         return FALSE;
1202                 }
1203         }
1204
1205         visible_only = TRUE;
1206         slice = TRUE;
1207
1208         /* locate all lines */
1209         lines = strbreakup (str, "\n", -1);
1210
1211         search = *iter;
1212
1213         while (TRUE)
1214         {
1215                 /* This loop has an inefficient worst-case, where
1216                  * gtk_text_iter_get_text () is called repeatedly on
1217                  * a single line.
1218                  */
1219                 GtkTextIter end;
1220
1221                 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1222                         break;
1223
1224                 if (backward_lines_match (&search, (const gchar**)lines,
1225                                           visible_only, slice, &match, &end))
1226                 {
1227                         if (limit == NULL || (limit &&
1228                                               gtk_text_iter_compare (&end, limit) > 0))
1229                         {
1230                                 retval = TRUE;
1231
1232                                 if (match_start)
1233                                         *match_start = match;
1234                                 if (match_end)
1235                                         *match_end = end;
1236                         }
1237                         break;
1238                 }
1239
1240                 if (gtk_text_iter_get_line_offset (&search) == 0)
1241                 {
1242                         if (!gtk_text_iter_backward_line (&search))
1243                                 break;
1244                 }
1245                 else
1246                 {
1247                         gtk_text_iter_set_line_offset (&search, 0);
1248                 }
1249         }
1250
1251         g_strfreev ((gchar**)lines);
1252
1253         return retval;
1254 }
1255
1256 static gboolean
1257 window_get_is_on_current_workspace (GtkWindow *window)
1258 {
1259         GdkWindow *gdk_window;
1260
1261         gdk_window = GTK_WIDGET (window)->window;
1262         if (gdk_window) {
1263                 return !(gdk_window_get_state (gdk_window) &
1264                          GDK_WINDOW_STATE_ICONIFIED);
1265         } else {
1266                 return FALSE;
1267         }
1268 }
1269
1270 /* Checks if the window is visible as in visible on the current workspace. */
1271 gboolean
1272 empathy_window_get_is_visible (GtkWindow *window)
1273 {
1274         g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1275
1276         return GTK_WIDGET_VISIBLE (GTK_WIDGET (window)) &&
1277                window_get_is_on_current_workspace (window);
1278 }
1279
1280 void 
1281 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1282 {
1283         GdkRectangle  icon_location;
1284         gulong        data[4];
1285         Display      *dpy;
1286         GdkWindow    *gdk_window;
1287
1288         gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1289         gdk_window = GTK_WIDGET (window)->window;
1290         dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1291
1292         data[0] = icon_location.x;
1293         data[1] = icon_location.y;
1294         data[2] = icon_location.width;
1295         data[3] = icon_location.height;
1296
1297         XChangeProperty (dpy,
1298                          GDK_WINDOW_XID (gdk_window),
1299                          gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1300                                                                 "_NET_WM_ICON_GEOMETRY"),
1301                          XA_CARDINAL, 32, PropModeReplace,
1302                          (guchar *)&data, 4);
1303
1304         gtk_window_set_skip_taskbar_hint (window, TRUE);
1305         gtk_window_iconify (window);
1306
1307 }
1308
1309 /* Takes care of moving the window to the current workspace. */
1310 void
1311 empathy_window_present (GtkWindow *window,
1312                        gboolean   steal_focus)
1313 {
1314         gboolean on_current;
1315         guint32  timestamp;
1316
1317         g_return_if_fail (window != NULL);
1318
1319         /* There are three cases: hidden, visible, visible on another
1320          * workspace.
1321          */
1322
1323         on_current = window_get_is_on_current_workspace (window);
1324
1325         if ( GTK_WIDGET_VISIBLE (GTK_WIDGET (window)) && !on_current) {
1326                 /* Hide it so present brings it to the current workspace. */
1327                 gtk_widget_hide (GTK_WIDGET (window));
1328         }
1329
1330         gtk_window_set_skip_taskbar_hint (window, FALSE);
1331
1332         timestamp = gtk_get_current_event_time ();
1333         if (steal_focus && timestamp != GDK_CURRENT_TIME) {
1334                 gtk_window_present_with_time (window, timestamp);
1335         } else {
1336                 gtk_window_present (window);
1337         }
1338 }
1339
1340 GtkWindow *
1341 empathy_get_toplevel_window (GtkWidget *widget)
1342 {
1343         GtkWidget *toplevel;
1344
1345         g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1346
1347         toplevel = gtk_widget_get_toplevel (widget);
1348         if (GTK_IS_WINDOW (toplevel) &&
1349             GTK_WIDGET_TOPLEVEL (toplevel)) {
1350                 return GTK_WINDOW (toplevel);
1351         }
1352
1353         return NULL;
1354 }
1355
1356 /* The URL opening code can't handle schemeless strings, so we try to be
1357  * smart and add http if there is no scheme or doesn't look like a mail
1358  * address. This should work in most cases, and let us click on strings
1359  * like "www.gnome.org".
1360  */
1361 static gchar *
1362 fixup_url (const gchar *url)
1363 {
1364         gchar *real_url;
1365
1366         if (!g_str_has_prefix (url, "http://") &&
1367             !strstr (url, ":/") &&
1368             !strstr (url, "@")) {
1369                 real_url = g_strdup_printf ("http://%s", url);
1370         } else {
1371                 real_url = g_strdup (url);
1372         }
1373
1374         return real_url;
1375 }
1376
1377 void
1378 empathy_url_show (const char *url)
1379 {
1380         gchar          *real_url;
1381         GnomeVFSResult  res;
1382
1383         real_url = fixup_url (url);
1384         res = gnome_vfs_url_show (real_url);
1385         if (res != GNOME_VFS_OK) {
1386                 empathy_debug (DEBUG_DOMAIN, "Couldn't show URL %s: %s",
1387                               real_url,
1388                               gnome_vfs_result_to_string (res));
1389         }
1390
1391         g_free (real_url);
1392 }
1393
1394 static void
1395 link_button_hook (GtkLinkButton *button,
1396                   const gchar *link,
1397                   gpointer user_data)
1398 {
1399         empathy_url_show (link);
1400 }
1401
1402 GtkWidget *
1403 empathy_link_button_new (const gchar *url,
1404                         const gchar *title)
1405 {
1406         static gboolean hook = FALSE;
1407
1408         if (!hook) {
1409                 hook = TRUE;
1410                 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1411         }
1412
1413         return gtk_link_button_new_with_label (url, title);
1414 }
1415
1416 void
1417 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1418                                         GCallback  callback,
1419                                         gpointer   user_data,
1420                                         gboolean   active)
1421 {
1422         g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1423
1424         g_signal_handlers_block_by_func (widget, callback, user_data);
1425         g_object_set (widget, "active", active, NULL);
1426         g_signal_handlers_unblock_by_func (widget, callback, user_data);
1427 }
1428