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