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>
26 * Part of this file is copied from GtkSourceView (gtksourceiter.c):
34 #include <X11/Xatom.h>
36 #include <glib/gi18n.h>
39 #include <glade/glade.h>
41 #include <libmissioncontrol/mc-profile.h>
43 #include "empathy-ui-utils.h"
44 #include "empathy-images.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include <libempathy/empathy-debug.h>
52 gboolean preserve_aspect_ratio;
56 get_glade_file (const gchar *filename,
59 const gchar *first_required_widget,
64 GtkWidget **widget_ptr;
66 DEBUG ("Loading glade file %s", filename);
68 gui = glade_xml_new (filename, root, domain);
71 g_warning ("Couldn't find necessary glade file '%s'", filename);
74 for (name = first_required_widget; name; name = va_arg (args, char *)) {
75 widget_ptr = va_arg (args, void *);
77 *widget_ptr = glade_xml_get_widget (gui, name);
80 g_warning ("Glade file '%s' is missing widget '%s'.",
90 empathy_glade_get_file_simple (const gchar *filename,
93 const gchar *first_required_widget, ...)
98 va_start (args, first_required_widget);
100 gui = get_glade_file (filename,
103 first_required_widget,
109 g_object_unref (gui);
114 empathy_glade_get_file (const gchar *filename,
117 const gchar *first_required_widget, ...)
122 va_start (args, first_required_widget);
124 gui = get_glade_file (filename,
127 first_required_widget,
140 empathy_glade_connect (GladeXML *gui,
142 gchar *first_widget, ...)
150 va_start (args, first_widget);
152 for (name = first_widget; name; name = va_arg (args, char *)) {
153 signal = va_arg (args, void *);
154 callback = va_arg (args, void *);
156 widget = glade_xml_get_widget (gui, name);
158 g_warning ("Glade file is missing widget '%s', aborting",
163 g_signal_connect (widget,
165 G_CALLBACK (callback),
173 empathy_glade_setup_size_group (GladeXML *gui,
174 GtkSizeGroupMode mode,
175 gchar *first_widget, ...)
179 GtkSizeGroup *size_group;
182 va_start (args, first_widget);
184 size_group = gtk_size_group_new (mode);
186 for (name = first_widget; name; name = va_arg (args, char *)) {
187 widget = glade_xml_get_widget (gui, name);
189 g_warning ("Glade file is missing widget '%s'", name);
193 gtk_size_group_add_widget (size_group, widget);
196 g_object_unref (size_group);
202 empathy_icon_name_from_account (McAccount *account)
206 profile = mc_account_get_profile (account);
208 return mc_profile_get_icon_name (profile);
212 empathy_icon_name_for_presence (McPresence presence)
215 case MC_PRESENCE_AVAILABLE:
216 return EMPATHY_IMAGE_AVAILABLE;
217 case MC_PRESENCE_DO_NOT_DISTURB:
218 return EMPATHY_IMAGE_BUSY;
219 case MC_PRESENCE_AWAY:
220 return EMPATHY_IMAGE_AWAY;
221 case MC_PRESENCE_EXTENDED_AWAY:
222 return EMPATHY_IMAGE_EXT_AWAY;
223 case MC_PRESENCE_HIDDEN:
224 return EMPATHY_IMAGE_HIDDEN;
225 case MC_PRESENCE_OFFLINE:
226 case MC_PRESENCE_UNSET:
227 return EMPATHY_IMAGE_OFFLINE;
229 g_assert_not_reached ();
236 empathy_icon_name_for_contact (EmpathyContact *contact)
240 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
241 EMPATHY_IMAGE_OFFLINE);
243 presence = empathy_contact_get_presence (contact);
244 return empathy_icon_name_for_presence (presence);
248 empathy_pixbuf_from_data (gchar *data,
251 GdkPixbufLoader *loader;
252 GdkPixbuf *pixbuf = NULL;
253 GError *error = NULL;
259 loader = gdk_pixbuf_loader_new ();
260 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
261 DEBUG ("Failed to write to pixbuf loader: %s",
262 error ? error->message : "No error given");
263 g_clear_error (&error);
264 g_object_unref (loader);
267 if (!gdk_pixbuf_loader_close (loader, &error)) {
268 DEBUG ("Failed to close pixbuf loader: %s",
269 error ? error->message : "No error given");
270 g_clear_error (&error);
271 g_object_unref (loader);
275 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
277 g_object_ref (pixbuf);
280 g_object_unref (loader);
286 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
289 struct SizeData *data)
291 g_return_if_fail (width > 0 && height > 0);
293 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
294 if (width < data->width && height < data->height) {
299 if (data->width < 0) {
300 width = width * (double) data->height / (gdouble) height;
301 height = data->height;
302 } else if (data->height < 0) {
303 height = height * (double) data->width / (double) width;
305 } else if ((double) height * (double) data->width >
306 (double) width * (double) data->height) {
307 width = 0.5 + (double) width * (double) data->height / (double) height;
308 height = data->height;
310 height = 0.5 + (double) height * (double) data->width / (double) width;
314 if (data->width > 0) {
318 if (data->height > 0) {
319 height = data->height;
323 gdk_pixbuf_loader_set_size (loader, width, height);
327 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
329 gint width, height, rowstride;
332 width = gdk_pixbuf_get_width (pixbuf);
333 height = gdk_pixbuf_get_height (pixbuf);
334 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
335 pixels = gdk_pixbuf_get_pixels (pixbuf);
337 if (width < 6 || height < 6) {
345 pixels[rowstride + 3] = 0x80;
346 pixels[rowstride * 2 + 3] = 0xC0;
349 pixels[width * 4 - 1] = 0;
350 pixels[width * 4 - 5] = 0x80;
351 pixels[width * 4 - 9] = 0xC0;
352 pixels[rowstride + (width * 4) - 1] = 0x80;
353 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
356 pixels[(height - 1) * rowstride + 3] = 0;
357 pixels[(height - 1) * rowstride + 7] = 0x80;
358 pixels[(height - 1) * rowstride + 11] = 0xC0;
359 pixels[(height - 2) * rowstride + 3] = 0x80;
360 pixels[(height - 3) * rowstride + 3] = 0xC0;
363 pixels[height * rowstride - 1] = 0;
364 pixels[(height - 1) * rowstride - 1] = 0x80;
365 pixels[(height - 2) * rowstride - 1] = 0xC0;
366 pixels[height * rowstride - 5] = 0x80;
367 pixels[height * rowstride - 9] = 0xC0;
371 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
373 gint width, height, rowstride, i;
377 width = gdk_pixbuf_get_width (pixbuf);
378 height = gdk_pixbuf_get_height (pixbuf);
379 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
380 pixels = gdk_pixbuf_get_pixels (pixbuf);
383 for (i = 3; i < rowstride; i+=4) {
389 for (i = 1; i < height - 1; i++) {
390 row = pixels + (i*rowstride);
391 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
396 row = pixels + ((height-1) * rowstride);
397 for (i = 3; i < rowstride; i+=4) {
407 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
412 GdkPixbufLoader *loader;
413 struct SizeData data;
414 GError *error = NULL;
421 data.height = height;
422 data.preserve_aspect_ratio = TRUE;
424 loader = gdk_pixbuf_loader_new ();
426 g_signal_connect (loader, "size-prepared",
427 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
430 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
431 g_warning ("Couldn't write avatar image:%p with "
432 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
433 avatar->data, avatar->len, error->message);
434 g_error_free (error);
438 gdk_pixbuf_loader_close (loader, NULL);
440 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
441 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
442 GdkPixbuf *rounded_pixbuf;
444 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
445 gdk_pixbuf_get_width (pixbuf),
446 gdk_pixbuf_get_height (pixbuf));
447 gdk_pixbuf_copy_area (pixbuf, 0, 0,
448 gdk_pixbuf_get_width (pixbuf),
449 gdk_pixbuf_get_height (pixbuf),
452 pixbuf = rounded_pixbuf;
454 g_object_ref (pixbuf);
457 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
458 empathy_avatar_pixbuf_roundify (pixbuf);
461 g_object_unref (loader);
467 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
471 EmpathyAvatar *avatar;
473 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
475 avatar = empathy_contact_get_avatar (contact);
477 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
481 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
486 width = gdk_pixbuf_get_width (pixbuf);
487 height = gdk_pixbuf_get_height (pixbuf);
489 if (width > max_size || height > max_size) {
490 factor = (gdouble) max_size / MAX (width, height);
492 width = width * factor;
493 height = height * factor;
495 return gdk_pixbuf_scale_simple (pixbuf,
500 return g_object_ref (pixbuf);
504 empathy_pixbuf_from_icon_name (const gchar *icon_name,
505 GtkIconSize icon_size)
508 GdkPixbuf *pixbuf = NULL;
509 GError *error = NULL;
517 theme = gtk_icon_theme_get_default ();
519 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
523 pixbuf = gtk_icon_theme_load_icon (theme,
529 DEBUG ("Error loading icon: %s", error->message);
530 g_clear_error (&error);
536 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
537 * that to make it easier to apply changes from the original code.
539 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
541 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
542 * decomposable character it consumes the decomposition length from the given
543 * offset. So it's useful when the offset was calculated for the normalized
544 * version of str, but we need a pointer to str itself. */
546 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
548 gchar *casefold, *normal;
554 q = g_utf8_next_char (p);
555 casefold = g_utf8_casefold (p, q - p);
556 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
557 offset -= g_utf8_strlen (normal, -1);
566 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
570 const gchar *ret = NULL;
573 gchar *caseless_haystack;
576 g_return_val_if_fail (haystack != NULL, NULL);
577 g_return_val_if_fail (needle != NULL, NULL);
579 casefold = g_utf8_casefold (haystack, -1);
580 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
583 needle_len = g_utf8_strlen (needle, -1);
584 haystack_len = g_utf8_strlen (caseless_haystack, -1);
588 ret = (gchar *)haystack;
592 if (haystack_len < needle_len)
598 p = (gchar*)caseless_haystack;
599 needle_len = strlen (needle);
604 if ((strncmp (p, needle, needle_len) == 0))
606 ret = pointer_from_offset_skipping_decomp (haystack, i);
610 p = g_utf8_next_char (p);
615 g_free (caseless_haystack);
621 g_utf8_caselessnmatch (const char *s1, const char *s2,
622 gssize n1, gssize n2)
625 gchar *normalized_s1;
626 gchar *normalized_s2;
629 gboolean ret = FALSE;
631 g_return_val_if_fail (s1 != NULL, FALSE);
632 g_return_val_if_fail (s2 != NULL, FALSE);
633 g_return_val_if_fail (n1 > 0, FALSE);
634 g_return_val_if_fail (n2 > 0, FALSE);
636 casefold = g_utf8_casefold (s1, n1);
637 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
640 casefold = g_utf8_casefold (s2, n2);
641 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
644 len_s1 = strlen (normalized_s1);
645 len_s2 = strlen (normalized_s2);
650 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
653 g_free (normalized_s1);
654 g_free (normalized_s2);
660 forward_chars_with_skipping (GtkTextIter *iter,
662 gboolean skip_invisible,
663 gboolean skip_nontext,
664 gboolean skip_decomp)
668 g_return_if_fail (count >= 0);
674 gboolean ignored = FALSE;
676 /* minimal workaround to avoid the infinite loop of bug #168247.
677 * It doesn't fix the problemjust the symptom...
679 if (gtk_text_iter_is_end (iter))
682 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
685 if (!ignored && skip_invisible &&
686 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
689 if (!ignored && skip_decomp)
691 /* being UTF8 correct sucks; this accounts for extra
692 offsets coming from canonical decompositions of
693 UTF8 characters (e.g. accented characters) which
694 g_utf8_normalize() performs */
699 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
700 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
701 i -= (g_utf8_strlen (normal, -1) - 1);
705 gtk_text_iter_forward_char (iter);
713 lines_match (const GtkTextIter *start,
715 gboolean visible_only,
717 GtkTextIter *match_start,
718 GtkTextIter *match_end)
725 if (*lines == NULL || **lines == '\0')
728 *match_start = *start;
735 gtk_text_iter_forward_line (&next);
737 /* No more text in buffer, but *lines is nonempty */
738 if (gtk_text_iter_equal (start, &next))
744 line_text = gtk_text_iter_get_visible_slice (start, &next);
746 line_text = gtk_text_iter_get_slice (start, &next);
751 line_text = gtk_text_iter_get_visible_text (start, &next);
753 line_text = gtk_text_iter_get_text (start, &next);
756 if (match_start) /* if this is the first line we're matching */
758 found = g_utf8_strcasestr (line_text, *lines);
762 /* If it's not the first line, we have to match from the
765 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
778 /* Get offset to start of search string */
779 offset = g_utf8_strlen (line_text, found - line_text);
783 /* If match start needs to be returned, set it to the
784 * start of the search string.
786 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
792 /* Go to end of search string */
793 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
802 /* pass NULL for match_start, since we don't need to find the
805 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
808 /* strsplit () that retains the delimiter as part of the string. */
810 strbreakup (const char *string,
811 const char *delimiter,
814 GSList *string_list = NULL, *slist;
815 gchar **str_array, *s, *casefold, *new_string;
818 g_return_val_if_fail (string != NULL, NULL);
819 g_return_val_if_fail (delimiter != NULL, NULL);
822 max_tokens = G_MAXINT;
824 s = strstr (string, delimiter);
827 guint delimiter_len = strlen (delimiter);
833 len = s - string + delimiter_len;
834 new_string = g_new (gchar, len + 1);
835 strncpy (new_string, string, len);
837 casefold = g_utf8_casefold (new_string, -1);
839 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
841 string_list = g_slist_prepend (string_list, new_string);
843 string = s + delimiter_len;
844 s = strstr (string, delimiter);
845 } while (--max_tokens && s);
851 casefold = g_utf8_casefold (string, -1);
852 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
854 string_list = g_slist_prepend (string_list, new_string);
857 str_array = g_new (gchar*, n);
861 str_array[i--] = NULL;
862 for (slist = string_list; slist; slist = slist->next)
863 str_array[i--] = slist->data;
865 g_slist_free (string_list);
871 empathy_text_iter_forward_search (const GtkTextIter *iter,
873 GtkTextIter *match_start,
874 GtkTextIter *match_end,
875 const GtkTextIter *limit)
877 gchar **lines = NULL;
879 gboolean retval = FALSE;
881 gboolean visible_only;
884 g_return_val_if_fail (iter != NULL, FALSE);
885 g_return_val_if_fail (str != NULL, FALSE);
887 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
891 /* If we can move one char, return the empty string there */
894 if (gtk_text_iter_forward_char (&match)) {
895 if (limit && gtk_text_iter_equal (&match, limit)) {
900 *match_start = match;
914 /* locate all lines */
915 lines = strbreakup (str, "\n", -1);
920 /* This loop has an inefficient worst-case, where
921 * gtk_text_iter_get_text () is called repeatedly on
926 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
930 if (lines_match (&search, (const gchar**)lines,
931 visible_only, slice, &match, &end)) {
933 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
937 *match_start = match;
945 } while (gtk_text_iter_forward_line (&search));
947 g_strfreev ((gchar**)lines);
953 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
957 const gchar *ret = NULL;
960 gchar *caseless_haystack;
963 g_return_val_if_fail (haystack != NULL, NULL);
964 g_return_val_if_fail (needle != NULL, NULL);
966 casefold = g_utf8_casefold (haystack, -1);
967 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
970 needle_len = g_utf8_strlen (needle, -1);
971 haystack_len = g_utf8_strlen (caseless_haystack, -1);
975 ret = (gchar *)haystack;
979 if (haystack_len < needle_len)
985 i = haystack_len - needle_len;
986 p = g_utf8_offset_to_pointer (caseless_haystack, i);
987 needle_len = strlen (needle);
989 while (p >= caseless_haystack)
991 if (strncmp (p, needle, needle_len) == 0)
993 ret = pointer_from_offset_skipping_decomp (haystack, i);
997 p = g_utf8_prev_char (p);
1002 g_free (caseless_haystack);
1008 backward_lines_match (const GtkTextIter *start,
1009 const gchar **lines,
1010 gboolean visible_only,
1012 GtkTextIter *match_start,
1013 GtkTextIter *match_end)
1015 GtkTextIter line, next;
1020 if (*lines == NULL || **lines == '\0')
1023 *match_start = *start;
1025 *match_end = *start;
1029 line = next = *start;
1030 if (gtk_text_iter_get_line_offset (&next) == 0)
1032 if (!gtk_text_iter_backward_line (&next))
1036 gtk_text_iter_set_line_offset (&next, 0);
1041 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1043 line_text = gtk_text_iter_get_slice (&next, &line);
1048 line_text = gtk_text_iter_get_visible_text (&next, &line);
1050 line_text = gtk_text_iter_get_text (&next, &line);
1053 if (match_start) /* if this is the first line we're matching */
1055 found = g_utf8_strrcasestr (line_text, *lines);
1059 /* If it's not the first line, we have to match from the
1060 * start of the line.
1062 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1075 /* Get offset to start of search string */
1076 offset = g_utf8_strlen (line_text, found - line_text);
1078 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1080 /* If match start needs to be returned, set it to the
1081 * start of the search string.
1085 *match_start = next;
1088 /* Go to end of search string */
1089 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1098 /* try to match the rest of the lines forward, passing NULL
1099 * for match_start so lines_match will try to match the entire
1101 return lines_match (&next, lines, visible_only,
1102 slice, NULL, match_end);
1106 empathy_text_iter_backward_search (const GtkTextIter *iter,
1108 GtkTextIter *match_start,
1109 GtkTextIter *match_end,
1110 const GtkTextIter *limit)
1112 gchar **lines = NULL;
1114 gboolean retval = FALSE;
1116 gboolean visible_only;
1119 g_return_val_if_fail (iter != NULL, FALSE);
1120 g_return_val_if_fail (str != NULL, FALSE);
1122 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1127 /* If we can move one char, return the empty string there */
1130 if (gtk_text_iter_backward_char (&match))
1132 if (limit && gtk_text_iter_equal (&match, limit))
1136 *match_start = match;
1147 visible_only = TRUE;
1150 /* locate all lines */
1151 lines = strbreakup (str, "\n", -1);
1157 /* This loop has an inefficient worst-case, where
1158 * gtk_text_iter_get_text () is called repeatedly on
1163 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1166 if (backward_lines_match (&search, (const gchar**)lines,
1167 visible_only, slice, &match, &end))
1169 if (limit == NULL || (limit &&
1170 gtk_text_iter_compare (&end, limit) > 0))
1175 *match_start = match;
1182 if (gtk_text_iter_get_line_offset (&search) == 0)
1184 if (!gtk_text_iter_backward_line (&search))
1189 gtk_text_iter_set_line_offset (&search, 0);
1193 g_strfreev ((gchar**)lines);
1199 empathy_window_get_is_visible (GtkWindow *window)
1201 GdkWindowState state;
1202 GdkWindow *gdk_window;
1204 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1206 gdk_window = GTK_WIDGET (window)->window;
1211 state = gdk_window_get_state (gdk_window);
1212 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1220 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1222 GdkRectangle icon_location;
1225 GdkWindow *gdk_window;
1227 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1228 gdk_window = GTK_WIDGET (window)->window;
1229 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1231 data[0] = icon_location.x;
1232 data[1] = icon_location.y;
1233 data[2] = icon_location.width;
1234 data[3] = icon_location.height;
1236 XChangeProperty (dpy,
1237 GDK_WINDOW_XID (gdk_window),
1238 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1239 "_NET_WM_ICON_GEOMETRY"),
1240 XA_CARDINAL, 32, PropModeReplace,
1241 (guchar *)&data, 4);
1243 gtk_window_set_skip_taskbar_hint (window, TRUE);
1244 gtk_window_iconify (window);
1247 /* Takes care of moving the window to the current workspace. */
1249 empathy_window_present (GtkWindow *window,
1250 gboolean steal_focus)
1254 g_return_if_fail (GTK_IS_WINDOW (window));
1256 /* There are three cases: hidden, visible, visible on another
1260 if (!empathy_window_get_is_visible (window)) {
1261 /* Hide it so present brings it to the current workspace. */
1262 gtk_widget_hide (GTK_WIDGET (window));
1265 timestamp = gtk_get_current_event_time ();
1266 gtk_window_set_skip_taskbar_hint (window, FALSE);
1267 gtk_window_present_with_time (window, timestamp);
1268 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1269 * it deiconify automatically. */
1270 gtk_window_deiconify (window);
1274 empathy_get_toplevel_window (GtkWidget *widget)
1276 GtkWidget *toplevel;
1278 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1280 toplevel = gtk_widget_get_toplevel (widget);
1281 if (GTK_IS_WINDOW (toplevel) &&
1282 GTK_WIDGET_TOPLEVEL (toplevel)) {
1283 return GTK_WINDOW (toplevel);
1289 /* The URL opening code can't handle schemeless strings, so we try to be
1290 * smart and add http if there is no scheme or doesn't look like a mail
1291 * address. This should work in most cases, and let us click on strings
1292 * like "www.gnome.org".
1295 fixup_url (const gchar *url)
1297 if (!g_str_has_prefix (url, "ghelp:") &&
1298 !strstr (url, ":/") &&
1299 !strstr (url, "@")) {
1300 return g_strdup_printf ("http://%s", url);
1307 empathy_url_show (const char *url)
1310 GError *error = NULL;
1312 real_url = fixup_url (url);
1317 /* FIXME: this does not work for multihead, we should use
1318 * GdkAppLaunchContext or gtk_show_url, see bug #514396.
1320 g_app_info_launch_default_for_uri (url, NULL, &error);
1324 dialog = gtk_message_dialog_new (NULL, 0,
1325 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1326 _("Unable to open URI"));
1327 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1328 "%s", error->message);
1330 g_signal_connect (dialog, "response",
1331 G_CALLBACK (gtk_widget_destroy),
1333 gtk_window_present (GTK_WINDOW (dialog));
1335 g_clear_error (&error);
1342 link_button_hook (GtkLinkButton *button,
1346 empathy_url_show (link);
1350 empathy_link_button_new (const gchar *url,
1353 static gboolean hook = FALSE;
1357 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1360 return gtk_link_button_new_with_label (url, title);
1364 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1369 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1371 g_signal_handlers_block_by_func (widget, callback, user_data);
1372 g_object_set (widget, "active", active, NULL);
1373 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1377 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1378 const gchar *tag_name,
1379 const gchar *first_property_name,
1382 GtkTextTagTable *table;
1385 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1386 g_return_val_if_fail (tag_name != NULL, NULL);
1388 table = gtk_text_buffer_get_tag_table (buffer);
1389 tag = gtk_text_tag_table_lookup (table, tag_name);
1392 tag = gtk_text_tag_new (tag_name);
1393 gtk_text_tag_table_add (table, tag);
1394 g_object_unref (tag);
1396 /* Clear the old values so that we don't affect the new theme. */
1398 "background-set", FALSE,
1399 "foreground-set", FALSE,
1400 "invisible-set", FALSE,
1401 "justification-set", FALSE,
1402 "paragraph-background-set", FALSE,
1403 "pixels-above-lines-set", FALSE,
1404 "pixels-below-lines-set", FALSE,
1409 "weight-set", FALSE,
1413 if (first_property_name) {
1416 va_start (list, first_property_name);
1417 g_object_set_valist (G_OBJECT (tag), first_property_name, list);