1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Richard Hult <richard@imendio.com>
23 * Martyn Russell <martyn@imendio.com>
24 * Xavier Claessens <xclaesse@gmail.com>
25 * Jonny Lamb <jonny.lamb@collabora.co.uk>
27 * Part of this file is copied from GtkSourceView (gtksourceiter.c):
35 #include <X11/Xatom.h>
37 #include <glib/gi18n-lib.h>
40 #include <glade/glade.h>
42 #include <libmissioncontrol/mc-profile.h>
44 #include "empathy-ui-utils.h"
45 #include "empathy-images.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
48 #include <libempathy/empathy-debug.h>
49 #include <libempathy/empathy-utils.h>
50 #include <libempathy/empathy-dispatcher.h>
53 init (gboolean abort_if_fail,
56 char *parameter_string,
57 GOptionEntry *entries,
58 char *translation_domain,
61 static gboolean initialized = FALSE;
69 gtk_init (argc, argv);
71 ret = gtk_init_with_args (argc, argv, parameter_string, entries,
72 translation_domain, error);
74 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
75 PKGDATADIR G_DIR_SEPARATOR_S "icons");
83 empathy_gtk_init (int *argc, char ***argv)
85 init (FALSE, argc, argv, NULL, NULL, NULL, NULL);
89 empathy_gtk_init_with_args (int *argc,
91 char *parameter_string,
92 GOptionEntry *entries,
93 char *translation_domain,
96 return init (TRUE, argc, argv, parameter_string, entries,
97 translation_domain, error);
103 gboolean preserve_aspect_ratio;
107 get_glade_file (const gchar *filename,
110 const gchar *first_required_widget,
115 GtkWidget **widget_ptr;
117 DEBUG ("Loading glade file %s", filename);
119 gui = glade_xml_new (filename, root, domain);
122 g_warning ("Couldn't find necessary glade file '%s'", filename);
125 for (name = first_required_widget; name; name = va_arg (args, char *)) {
126 widget_ptr = va_arg (args, void *);
128 *widget_ptr = glade_xml_get_widget (gui, name);
131 g_warning ("Glade file '%s' is missing widget '%s'.",
141 empathy_glade_get_file_simple (const gchar *filename,
144 const gchar *first_required_widget, ...)
149 va_start (args, first_required_widget);
151 gui = get_glade_file (filename,
154 first_required_widget,
160 g_object_unref (gui);
165 empathy_glade_get_file (const gchar *filename,
168 const gchar *first_required_widget, ...)
173 va_start (args, first_required_widget);
175 gui = get_glade_file (filename,
178 first_required_widget,
191 empathy_glade_connect (GladeXML *gui,
193 gchar *first_widget, ...)
201 va_start (args, first_widget);
203 for (name = first_widget; name; name = va_arg (args, char *)) {
204 signal = va_arg (args, void *);
205 callback = va_arg (args, void *);
207 widget = glade_xml_get_widget (gui, name);
209 g_warning ("Glade file is missing widget '%s', aborting",
214 g_signal_connect (widget,
216 G_CALLBACK (callback),
224 empathy_glade_setup_size_group (GladeXML *gui,
225 GtkSizeGroupMode mode,
226 gchar *first_widget, ...)
230 GtkSizeGroup *size_group;
233 va_start (args, first_widget);
235 size_group = gtk_size_group_new (mode);
237 for (name = first_widget; name; name = va_arg (args, char *)) {
238 widget = glade_xml_get_widget (gui, name);
240 g_warning ("Glade file is missing widget '%s'", name);
244 gtk_size_group_add_widget (size_group, widget);
247 g_object_unref (size_group);
253 empathy_icon_name_from_account (McAccount *account)
257 profile = mc_account_get_profile (account);
259 return mc_profile_get_icon_name (profile);
263 empathy_icon_name_for_presence (McPresence presence)
266 case MC_PRESENCE_AVAILABLE:
267 return EMPATHY_IMAGE_AVAILABLE;
268 case MC_PRESENCE_DO_NOT_DISTURB:
269 return EMPATHY_IMAGE_BUSY;
270 case MC_PRESENCE_AWAY:
271 return EMPATHY_IMAGE_AWAY;
272 case MC_PRESENCE_EXTENDED_AWAY:
273 return EMPATHY_IMAGE_EXT_AWAY;
274 case MC_PRESENCE_HIDDEN:
275 return EMPATHY_IMAGE_HIDDEN;
276 case MC_PRESENCE_OFFLINE:
277 case MC_PRESENCE_UNSET:
278 return EMPATHY_IMAGE_OFFLINE;
280 g_assert_not_reached ();
287 empathy_icon_name_for_contact (EmpathyContact *contact)
291 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
292 EMPATHY_IMAGE_OFFLINE);
294 presence = empathy_contact_get_presence (contact);
295 return empathy_icon_name_for_presence (presence);
299 empathy_pixbuf_from_data (gchar *data,
302 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
306 empathy_pixbuf_from_data_and_mime (gchar *data,
310 GdkPixbufLoader *loader;
311 GdkPixbufFormat *format;
312 GdkPixbuf *pixbuf = NULL;
314 GError *error = NULL;
320 loader = gdk_pixbuf_loader_new ();
321 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
322 DEBUG ("Failed to write to pixbuf loader: %s",
323 error ? error->message : "No error given");
326 if (!gdk_pixbuf_loader_close (loader, &error)) {
327 DEBUG ("Failed to close pixbuf loader: %s",
328 error ? error->message : "No error given");
332 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
334 g_object_ref (pixbuf);
336 if (mime_type != NULL) {
337 format = gdk_pixbuf_loader_get_format (loader);
338 mime_types = gdk_pixbuf_format_get_mime_types (format);
340 *mime_type = g_strdup (*mime_types);
341 if (mime_types[1] != NULL) {
342 DEBUG ("Loader supports more than one mime "
343 "type! Picking the first one, %s",
346 g_strfreev (mime_types);
351 g_clear_error (&error);
352 g_object_unref (loader);
358 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
361 struct SizeData *data)
363 g_return_if_fail (width > 0 && height > 0);
365 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
366 if (width < data->width && height < data->height) {
371 if (data->width < 0) {
372 width = width * (double) data->height / (gdouble) height;
373 height = data->height;
374 } else if (data->height < 0) {
375 height = height * (double) data->width / (double) width;
377 } else if ((double) height * (double) data->width >
378 (double) width * (double) data->height) {
379 width = 0.5 + (double) width * (double) data->height / (double) height;
380 height = data->height;
382 height = 0.5 + (double) height * (double) data->width / (double) width;
386 if (data->width > 0) {
390 if (data->height > 0) {
391 height = data->height;
395 gdk_pixbuf_loader_set_size (loader, width, height);
399 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
401 gint width, height, rowstride;
404 width = gdk_pixbuf_get_width (pixbuf);
405 height = gdk_pixbuf_get_height (pixbuf);
406 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
407 pixels = gdk_pixbuf_get_pixels (pixbuf);
409 if (width < 6 || height < 6) {
417 pixels[rowstride + 3] = 0x80;
418 pixels[rowstride * 2 + 3] = 0xC0;
421 pixels[width * 4 - 1] = 0;
422 pixels[width * 4 - 5] = 0x80;
423 pixels[width * 4 - 9] = 0xC0;
424 pixels[rowstride + (width * 4) - 1] = 0x80;
425 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
428 pixels[(height - 1) * rowstride + 3] = 0;
429 pixels[(height - 1) * rowstride + 7] = 0x80;
430 pixels[(height - 1) * rowstride + 11] = 0xC0;
431 pixels[(height - 2) * rowstride + 3] = 0x80;
432 pixels[(height - 3) * rowstride + 3] = 0xC0;
435 pixels[height * rowstride - 1] = 0;
436 pixels[(height - 1) * rowstride - 1] = 0x80;
437 pixels[(height - 2) * rowstride - 1] = 0xC0;
438 pixels[height * rowstride - 5] = 0x80;
439 pixels[height * rowstride - 9] = 0xC0;
443 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
445 gint width, height, rowstride, i;
449 width = gdk_pixbuf_get_width (pixbuf);
450 height = gdk_pixbuf_get_height (pixbuf);
451 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
452 pixels = gdk_pixbuf_get_pixels (pixbuf);
455 for (i = 3; i < rowstride; i+=4) {
461 for (i = 1; i < height - 1; i++) {
462 row = pixels + (i*rowstride);
463 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
468 row = pixels + ((height-1) * rowstride);
469 for (i = 3; i < rowstride; i+=4) {
479 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
484 GdkPixbufLoader *loader;
485 struct SizeData data;
486 GError *error = NULL;
493 data.height = height;
494 data.preserve_aspect_ratio = TRUE;
496 loader = gdk_pixbuf_loader_new ();
498 g_signal_connect (loader, "size-prepared",
499 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
502 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
503 g_warning ("Couldn't write avatar image:%p with "
504 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
505 avatar->data, avatar->len, error->message);
506 g_error_free (error);
510 gdk_pixbuf_loader_close (loader, NULL);
512 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
513 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
514 GdkPixbuf *rounded_pixbuf;
516 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
517 gdk_pixbuf_get_width (pixbuf),
518 gdk_pixbuf_get_height (pixbuf));
519 gdk_pixbuf_copy_area (pixbuf, 0, 0,
520 gdk_pixbuf_get_width (pixbuf),
521 gdk_pixbuf_get_height (pixbuf),
524 pixbuf = rounded_pixbuf;
526 g_object_ref (pixbuf);
529 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
530 empathy_avatar_pixbuf_roundify (pixbuf);
533 g_object_unref (loader);
539 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
543 EmpathyAvatar *avatar;
545 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
547 avatar = empathy_contact_get_avatar (contact);
549 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
553 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
558 width = gdk_pixbuf_get_width (pixbuf);
559 height = gdk_pixbuf_get_height (pixbuf);
561 if (width > 0 && (width > max_size || height > max_size)) {
562 factor = (gdouble) max_size / MAX (width, height);
564 width = width * factor;
565 height = height * factor;
567 return gdk_pixbuf_scale_simple (pixbuf,
572 return g_object_ref (pixbuf);
576 empathy_pixbuf_from_icon_name (const gchar *icon_name,
577 GtkIconSize icon_size)
580 GdkPixbuf *pixbuf = NULL;
581 GError *error = NULL;
589 theme = gtk_icon_theme_get_default ();
591 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
595 pixbuf = gtk_icon_theme_load_icon (theme,
601 DEBUG ("Error loading icon: %s", error->message);
602 g_clear_error (&error);
608 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
609 * that to make it easier to apply changes from the original code.
611 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
613 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
614 * decomposable character it consumes the decomposition length from the given
615 * offset. So it's useful when the offset was calculated for the normalized
616 * version of str, but we need a pointer to str itself. */
618 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
620 gchar *casefold, *normal;
626 q = g_utf8_next_char (p);
627 casefold = g_utf8_casefold (p, q - p);
628 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
629 offset -= g_utf8_strlen (normal, -1);
638 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
642 const gchar *ret = NULL;
645 gchar *caseless_haystack;
648 g_return_val_if_fail (haystack != NULL, NULL);
649 g_return_val_if_fail (needle != NULL, NULL);
651 casefold = g_utf8_casefold (haystack, -1);
652 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
655 needle_len = g_utf8_strlen (needle, -1);
656 haystack_len = g_utf8_strlen (caseless_haystack, -1);
660 ret = (gchar *)haystack;
664 if (haystack_len < needle_len)
670 p = (gchar*)caseless_haystack;
671 needle_len = strlen (needle);
676 if ((strncmp (p, needle, needle_len) == 0))
678 ret = pointer_from_offset_skipping_decomp (haystack, i);
682 p = g_utf8_next_char (p);
687 g_free (caseless_haystack);
693 g_utf8_caselessnmatch (const char *s1, const char *s2,
694 gssize n1, gssize n2)
697 gchar *normalized_s1;
698 gchar *normalized_s2;
701 gboolean ret = FALSE;
703 g_return_val_if_fail (s1 != NULL, FALSE);
704 g_return_val_if_fail (s2 != NULL, FALSE);
705 g_return_val_if_fail (n1 > 0, FALSE);
706 g_return_val_if_fail (n2 > 0, FALSE);
708 casefold = g_utf8_casefold (s1, n1);
709 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
712 casefold = g_utf8_casefold (s2, n2);
713 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
716 len_s1 = strlen (normalized_s1);
717 len_s2 = strlen (normalized_s2);
722 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
725 g_free (normalized_s1);
726 g_free (normalized_s2);
732 forward_chars_with_skipping (GtkTextIter *iter,
734 gboolean skip_invisible,
735 gboolean skip_nontext,
736 gboolean skip_decomp)
740 g_return_if_fail (count >= 0);
746 gboolean ignored = FALSE;
748 /* minimal workaround to avoid the infinite loop of bug #168247.
749 * It doesn't fix the problemjust the symptom...
751 if (gtk_text_iter_is_end (iter))
754 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
757 if (!ignored && skip_invisible &&
758 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
761 if (!ignored && skip_decomp)
763 /* being UTF8 correct sucks; this accounts for extra
764 offsets coming from canonical decompositions of
765 UTF8 characters (e.g. accented characters) which
766 g_utf8_normalize() performs */
771 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
772 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
773 i -= (g_utf8_strlen (normal, -1) - 1);
777 gtk_text_iter_forward_char (iter);
785 lines_match (const GtkTextIter *start,
787 gboolean visible_only,
789 GtkTextIter *match_start,
790 GtkTextIter *match_end)
797 if (*lines == NULL || **lines == '\0')
800 *match_start = *start;
807 gtk_text_iter_forward_line (&next);
809 /* No more text in buffer, but *lines is nonempty */
810 if (gtk_text_iter_equal (start, &next))
816 line_text = gtk_text_iter_get_visible_slice (start, &next);
818 line_text = gtk_text_iter_get_slice (start, &next);
823 line_text = gtk_text_iter_get_visible_text (start, &next);
825 line_text = gtk_text_iter_get_text (start, &next);
828 if (match_start) /* if this is the first line we're matching */
830 found = g_utf8_strcasestr (line_text, *lines);
834 /* If it's not the first line, we have to match from the
837 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
850 /* Get offset to start of search string */
851 offset = g_utf8_strlen (line_text, found - line_text);
855 /* If match start needs to be returned, set it to the
856 * start of the search string.
858 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
864 /* Go to end of search string */
865 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
874 /* pass NULL for match_start, since we don't need to find the
877 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
880 /* strsplit () that retains the delimiter as part of the string. */
882 strbreakup (const char *string,
883 const char *delimiter,
886 GSList *string_list = NULL, *slist;
887 gchar **str_array, *s, *casefold, *new_string;
890 g_return_val_if_fail (string != NULL, NULL);
891 g_return_val_if_fail (delimiter != NULL, NULL);
894 max_tokens = G_MAXINT;
896 s = strstr (string, delimiter);
899 guint delimiter_len = strlen (delimiter);
905 len = s - string + delimiter_len;
906 new_string = g_new (gchar, len + 1);
907 strncpy (new_string, string, len);
909 casefold = g_utf8_casefold (new_string, -1);
911 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
913 string_list = g_slist_prepend (string_list, new_string);
915 string = s + delimiter_len;
916 s = strstr (string, delimiter);
917 } while (--max_tokens && s);
923 casefold = g_utf8_casefold (string, -1);
924 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
926 string_list = g_slist_prepend (string_list, new_string);
929 str_array = g_new (gchar*, n);
933 str_array[i--] = NULL;
934 for (slist = string_list; slist; slist = slist->next)
935 str_array[i--] = slist->data;
937 g_slist_free (string_list);
943 empathy_text_iter_forward_search (const GtkTextIter *iter,
945 GtkTextIter *match_start,
946 GtkTextIter *match_end,
947 const GtkTextIter *limit)
949 gchar **lines = NULL;
951 gboolean retval = FALSE;
953 gboolean visible_only;
956 g_return_val_if_fail (iter != NULL, FALSE);
957 g_return_val_if_fail (str != NULL, FALSE);
959 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
963 /* If we can move one char, return the empty string there */
966 if (gtk_text_iter_forward_char (&match)) {
967 if (limit && gtk_text_iter_equal (&match, limit)) {
972 *match_start = match;
986 /* locate all lines */
987 lines = strbreakup (str, "\n", -1);
992 /* This loop has an inefficient worst-case, where
993 * gtk_text_iter_get_text () is called repeatedly on
998 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1002 if (lines_match (&search, (const gchar**)lines,
1003 visible_only, slice, &match, &end)) {
1004 if (limit == NULL ||
1005 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1009 *match_start = match;
1017 } while (gtk_text_iter_forward_line (&search));
1019 g_strfreev ((gchar**)lines);
1024 static const gchar *
1025 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1029 const gchar *ret = NULL;
1032 gchar *caseless_haystack;
1035 g_return_val_if_fail (haystack != NULL, NULL);
1036 g_return_val_if_fail (needle != NULL, NULL);
1038 casefold = g_utf8_casefold (haystack, -1);
1039 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1042 needle_len = g_utf8_strlen (needle, -1);
1043 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1045 if (needle_len == 0)
1047 ret = (gchar *)haystack;
1051 if (haystack_len < needle_len)
1057 i = haystack_len - needle_len;
1058 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1059 needle_len = strlen (needle);
1061 while (p >= caseless_haystack)
1063 if (strncmp (p, needle, needle_len) == 0)
1065 ret = pointer_from_offset_skipping_decomp (haystack, i);
1069 p = g_utf8_prev_char (p);
1074 g_free (caseless_haystack);
1080 backward_lines_match (const GtkTextIter *start,
1081 const gchar **lines,
1082 gboolean visible_only,
1084 GtkTextIter *match_start,
1085 GtkTextIter *match_end)
1087 GtkTextIter line, next;
1092 if (*lines == NULL || **lines == '\0')
1095 *match_start = *start;
1097 *match_end = *start;
1101 line = next = *start;
1102 if (gtk_text_iter_get_line_offset (&next) == 0)
1104 if (!gtk_text_iter_backward_line (&next))
1108 gtk_text_iter_set_line_offset (&next, 0);
1113 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1115 line_text = gtk_text_iter_get_slice (&next, &line);
1120 line_text = gtk_text_iter_get_visible_text (&next, &line);
1122 line_text = gtk_text_iter_get_text (&next, &line);
1125 if (match_start) /* if this is the first line we're matching */
1127 found = g_utf8_strrcasestr (line_text, *lines);
1131 /* If it's not the first line, we have to match from the
1132 * start of the line.
1134 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1147 /* Get offset to start of search string */
1148 offset = g_utf8_strlen (line_text, found - line_text);
1150 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1152 /* If match start needs to be returned, set it to the
1153 * start of the search string.
1157 *match_start = next;
1160 /* Go to end of search string */
1161 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1170 /* try to match the rest of the lines forward, passing NULL
1171 * for match_start so lines_match will try to match the entire
1173 return lines_match (&next, lines, visible_only,
1174 slice, NULL, match_end);
1178 empathy_text_iter_backward_search (const GtkTextIter *iter,
1180 GtkTextIter *match_start,
1181 GtkTextIter *match_end,
1182 const GtkTextIter *limit)
1184 gchar **lines = NULL;
1186 gboolean retval = FALSE;
1188 gboolean visible_only;
1191 g_return_val_if_fail (iter != NULL, FALSE);
1192 g_return_val_if_fail (str != NULL, FALSE);
1194 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1199 /* If we can move one char, return the empty string there */
1202 if (gtk_text_iter_backward_char (&match))
1204 if (limit && gtk_text_iter_equal (&match, limit))
1208 *match_start = match;
1219 visible_only = TRUE;
1222 /* locate all lines */
1223 lines = strbreakup (str, "\n", -1);
1229 /* This loop has an inefficient worst-case, where
1230 * gtk_text_iter_get_text () is called repeatedly on
1235 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1238 if (backward_lines_match (&search, (const gchar**)lines,
1239 visible_only, slice, &match, &end))
1241 if (limit == NULL || (limit &&
1242 gtk_text_iter_compare (&end, limit) > 0))
1247 *match_start = match;
1254 if (gtk_text_iter_get_line_offset (&search) == 0)
1256 if (!gtk_text_iter_backward_line (&search))
1261 gtk_text_iter_set_line_offset (&search, 0);
1265 g_strfreev ((gchar**)lines);
1271 empathy_window_get_is_visible (GtkWindow *window)
1273 GdkWindowState state;
1274 GdkWindow *gdk_window;
1276 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1278 gdk_window = GTK_WIDGET (window)->window;
1283 state = gdk_window_get_state (gdk_window);
1284 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1292 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1294 GdkRectangle icon_location;
1297 GdkWindow *gdk_window;
1299 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1300 gdk_window = GTK_WIDGET (window)->window;
1301 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1303 data[0] = icon_location.x;
1304 data[1] = icon_location.y;
1305 data[2] = icon_location.width;
1306 data[3] = icon_location.height;
1308 XChangeProperty (dpy,
1309 GDK_WINDOW_XID (gdk_window),
1310 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1311 "_NET_WM_ICON_GEOMETRY"),
1312 XA_CARDINAL, 32, PropModeReplace,
1313 (guchar *)&data, 4);
1315 gtk_window_set_skip_taskbar_hint (window, TRUE);
1316 gtk_window_iconify (window);
1319 /* Takes care of moving the window to the current workspace. */
1321 empathy_window_present (GtkWindow *window,
1322 gboolean steal_focus)
1326 g_return_if_fail (GTK_IS_WINDOW (window));
1328 /* There are three cases: hidden, visible, visible on another
1332 if (!empathy_window_get_is_visible (window)) {
1333 /* Hide it so present brings it to the current workspace. */
1334 gtk_widget_hide (GTK_WIDGET (window));
1337 timestamp = gtk_get_current_event_time ();
1338 gtk_window_set_skip_taskbar_hint (window, FALSE);
1339 gtk_window_present_with_time (window, timestamp);
1340 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1341 * it deiconify automatically. */
1342 gtk_window_deiconify (window);
1346 empathy_get_toplevel_window (GtkWidget *widget)
1348 GtkWidget *toplevel;
1350 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1352 toplevel = gtk_widget_get_toplevel (widget);
1353 if (GTK_IS_WINDOW (toplevel) &&
1354 GTK_WIDGET_TOPLEVEL (toplevel)) {
1355 return GTK_WINDOW (toplevel);
1361 /* The URL opening code can't handle schemeless strings, so we try to be
1362 * smart and add http if there is no scheme or doesn't look like a mail
1363 * address. This should work in most cases, and let us click on strings
1364 * like "www.gnome.org".
1367 fixup_url (const gchar *url)
1369 if (g_str_has_prefix (url, "ghelp:") ||
1370 g_str_has_prefix (url, "mailto:") ||
1371 strstr (url, ":/")) {
1375 if (strstr (url, "@")) {
1376 return g_strdup_printf ("mailto:%s", url);
1379 return g_strdup_printf ("http://%s", url);
1383 empathy_url_show (GtkWidget *parent,
1387 GError *error = NULL;
1389 real_url = fixup_url (url);
1394 gtk_show_uri (gtk_widget_get_screen (parent), url,
1395 gtk_get_current_event_time (), &error);
1400 dialog = gtk_message_dialog_new (NULL, 0,
1401 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1402 _("Unable to open URI"));
1403 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1404 "%s", error->message);
1406 g_signal_connect (dialog, "response",
1407 G_CALLBACK (gtk_widget_destroy),
1409 gtk_window_present (GTK_WINDOW (dialog));
1411 g_clear_error (&error);
1418 link_button_hook (GtkLinkButton *button,
1422 empathy_url_show (GTK_WIDGET (button), link);
1426 empathy_link_button_new (const gchar *url,
1429 static gboolean hook = FALSE;
1433 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1436 return gtk_link_button_new_with_label (url, title);
1440 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1445 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1447 g_signal_handlers_block_by_func (widget, callback, user_data);
1448 g_object_set (widget, "active", active, NULL);
1449 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1453 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1454 const gchar *tag_name,
1455 const gchar *first_property_name,
1458 GtkTextTagTable *table;
1461 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1462 g_return_val_if_fail (tag_name != NULL, NULL);
1464 table = gtk_text_buffer_get_tag_table (buffer);
1465 tag = gtk_text_tag_table_lookup (table, tag_name);
1468 tag = gtk_text_tag_new (tag_name);
1469 gtk_text_tag_table_add (table, tag);
1470 g_object_unref (tag);
1472 /* Clear the old values so that we don't affect the new theme. */
1474 "background-set", FALSE,
1475 "foreground-set", FALSE,
1476 "invisible-set", FALSE,
1477 "justification-set", FALSE,
1478 "paragraph-background-set", FALSE,
1479 "pixels-above-lines-set", FALSE,
1480 "pixels-below-lines-set", FALSE,
1485 "weight-set", FALSE,
1489 if (first_property_name) {
1492 va_start (list, first_property_name);
1493 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1500 /* Sending files with the file chooser */
1503 file_manager_send_file_response_cb (GtkDialog *widget,
1505 EmpathyContact *contact)
1507 if (response_id == GTK_RESPONSE_OK) {
1511 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget));
1513 DEBUG ("File chooser selected files:");
1515 for (l = list; l; l = l->next) {
1518 GtkRecentManager *manager;
1521 gfile = g_file_new_for_uri (uri);
1523 DEBUG ("\t%s", uri);
1524 empathy_dispatcher_send_file (contact, gfile);
1526 manager = gtk_recent_manager_get_default ();
1527 gtk_recent_manager_add_item (manager, uri);
1529 g_object_unref (gfile);
1532 g_slist_free (list);
1535 gtk_widget_destroy (GTK_WIDGET (widget));
1539 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1544 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1546 DEBUG ("Creating selection file chooser");
1548 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1550 GTK_FILE_CHOOSER_ACTION_OPEN,
1552 GTK_RESPONSE_CANCEL,
1556 button = gtk_button_new_with_mnemonic (_("_Send"));
1557 gtk_button_set_image (GTK_BUTTON (button),
1558 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1559 GTK_ICON_SIZE_BUTTON));
1560 gtk_widget_show (button);
1561 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1563 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1564 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1567 g_signal_connect (widget, "response",
1568 G_CALLBACK (file_manager_send_file_response_cb),
1571 gtk_widget_show (widget);