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,
252 GdkPixbufLoader *loader;
253 GdkPixbufFormat *format;
254 GdkPixbuf *pixbuf = NULL;
256 GError *error = NULL;
262 loader = gdk_pixbuf_loader_new ();
263 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
264 DEBUG ("Failed to write to pixbuf loader: %s",
265 error ? error->message : "No error given");
268 if (!gdk_pixbuf_loader_close (loader, &error)) {
269 DEBUG ("Failed to close pixbuf loader: %s",
270 error ? error->message : "No error given");
274 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
276 g_object_ref (pixbuf);
278 if (mime_type != NULL) {
279 format = gdk_pixbuf_loader_get_format (loader);
280 mime_types = gdk_pixbuf_format_get_mime_types (format);
282 *mime_type = g_strdup (*mime_types);
283 if (mime_types[1] != NULL) {
284 DEBUG ("Loader supports more than one mime "
285 "type! Picking the first one, %s",
288 g_strfreev (mime_types);
293 g_clear_error (&error);
294 g_object_unref (loader);
300 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
303 struct SizeData *data)
305 g_return_if_fail (width > 0 && height > 0);
307 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
308 if (width < data->width && height < data->height) {
313 if (data->width < 0) {
314 width = width * (double) data->height / (gdouble) height;
315 height = data->height;
316 } else if (data->height < 0) {
317 height = height * (double) data->width / (double) width;
319 } else if ((double) height * (double) data->width >
320 (double) width * (double) data->height) {
321 width = 0.5 + (double) width * (double) data->height / (double) height;
322 height = data->height;
324 height = 0.5 + (double) height * (double) data->width / (double) width;
328 if (data->width > 0) {
332 if (data->height > 0) {
333 height = data->height;
337 gdk_pixbuf_loader_set_size (loader, width, height);
341 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
343 gint width, height, rowstride;
346 width = gdk_pixbuf_get_width (pixbuf);
347 height = gdk_pixbuf_get_height (pixbuf);
348 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
349 pixels = gdk_pixbuf_get_pixels (pixbuf);
351 if (width < 6 || height < 6) {
359 pixels[rowstride + 3] = 0x80;
360 pixels[rowstride * 2 + 3] = 0xC0;
363 pixels[width * 4 - 1] = 0;
364 pixels[width * 4 - 5] = 0x80;
365 pixels[width * 4 - 9] = 0xC0;
366 pixels[rowstride + (width * 4) - 1] = 0x80;
367 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
370 pixels[(height - 1) * rowstride + 3] = 0;
371 pixels[(height - 1) * rowstride + 7] = 0x80;
372 pixels[(height - 1) * rowstride + 11] = 0xC0;
373 pixels[(height - 2) * rowstride + 3] = 0x80;
374 pixels[(height - 3) * rowstride + 3] = 0xC0;
377 pixels[height * rowstride - 1] = 0;
378 pixels[(height - 1) * rowstride - 1] = 0x80;
379 pixels[(height - 2) * rowstride - 1] = 0xC0;
380 pixels[height * rowstride - 5] = 0x80;
381 pixels[height * rowstride - 9] = 0xC0;
385 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
387 gint width, height, rowstride, i;
391 width = gdk_pixbuf_get_width (pixbuf);
392 height = gdk_pixbuf_get_height (pixbuf);
393 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
394 pixels = gdk_pixbuf_get_pixels (pixbuf);
397 for (i = 3; i < rowstride; i+=4) {
403 for (i = 1; i < height - 1; i++) {
404 row = pixels + (i*rowstride);
405 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
410 row = pixels + ((height-1) * rowstride);
411 for (i = 3; i < rowstride; i+=4) {
421 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
426 GdkPixbufLoader *loader;
427 struct SizeData data;
428 GError *error = NULL;
435 data.height = height;
436 data.preserve_aspect_ratio = TRUE;
438 loader = gdk_pixbuf_loader_new ();
440 g_signal_connect (loader, "size-prepared",
441 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
444 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
445 g_warning ("Couldn't write avatar image:%p with "
446 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
447 avatar->data, avatar->len, error->message);
448 g_error_free (error);
452 gdk_pixbuf_loader_close (loader, NULL);
454 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
455 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
456 GdkPixbuf *rounded_pixbuf;
458 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
459 gdk_pixbuf_get_width (pixbuf),
460 gdk_pixbuf_get_height (pixbuf));
461 gdk_pixbuf_copy_area (pixbuf, 0, 0,
462 gdk_pixbuf_get_width (pixbuf),
463 gdk_pixbuf_get_height (pixbuf),
466 pixbuf = rounded_pixbuf;
468 g_object_ref (pixbuf);
471 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
472 empathy_avatar_pixbuf_roundify (pixbuf);
475 g_object_unref (loader);
481 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
485 EmpathyAvatar *avatar;
487 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
489 avatar = empathy_contact_get_avatar (contact);
491 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
495 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
500 width = gdk_pixbuf_get_width (pixbuf);
501 height = gdk_pixbuf_get_height (pixbuf);
503 if (width > 0 && (width > max_size || height > max_size)) {
504 factor = (gdouble) max_size / MAX (width, height);
506 width = width * factor;
507 height = height * factor;
509 return gdk_pixbuf_scale_simple (pixbuf,
514 return g_object_ref (pixbuf);
518 empathy_pixbuf_from_icon_name (const gchar *icon_name,
519 GtkIconSize icon_size)
522 GdkPixbuf *pixbuf = NULL;
523 GError *error = NULL;
531 theme = gtk_icon_theme_get_default ();
533 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
537 pixbuf = gtk_icon_theme_load_icon (theme,
543 DEBUG ("Error loading icon: %s", error->message);
544 g_clear_error (&error);
550 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
551 * that to make it easier to apply changes from the original code.
553 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
555 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
556 * decomposable character it consumes the decomposition length from the given
557 * offset. So it's useful when the offset was calculated for the normalized
558 * version of str, but we need a pointer to str itself. */
560 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
562 gchar *casefold, *normal;
568 q = g_utf8_next_char (p);
569 casefold = g_utf8_casefold (p, q - p);
570 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
571 offset -= g_utf8_strlen (normal, -1);
580 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
584 const gchar *ret = NULL;
587 gchar *caseless_haystack;
590 g_return_val_if_fail (haystack != NULL, NULL);
591 g_return_val_if_fail (needle != NULL, NULL);
593 casefold = g_utf8_casefold (haystack, -1);
594 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
597 needle_len = g_utf8_strlen (needle, -1);
598 haystack_len = g_utf8_strlen (caseless_haystack, -1);
602 ret = (gchar *)haystack;
606 if (haystack_len < needle_len)
612 p = (gchar*)caseless_haystack;
613 needle_len = strlen (needle);
618 if ((strncmp (p, needle, needle_len) == 0))
620 ret = pointer_from_offset_skipping_decomp (haystack, i);
624 p = g_utf8_next_char (p);
629 g_free (caseless_haystack);
635 g_utf8_caselessnmatch (const char *s1, const char *s2,
636 gssize n1, gssize n2)
639 gchar *normalized_s1;
640 gchar *normalized_s2;
643 gboolean ret = FALSE;
645 g_return_val_if_fail (s1 != NULL, FALSE);
646 g_return_val_if_fail (s2 != NULL, FALSE);
647 g_return_val_if_fail (n1 > 0, FALSE);
648 g_return_val_if_fail (n2 > 0, FALSE);
650 casefold = g_utf8_casefold (s1, n1);
651 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
654 casefold = g_utf8_casefold (s2, n2);
655 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
658 len_s1 = strlen (normalized_s1);
659 len_s2 = strlen (normalized_s2);
664 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
667 g_free (normalized_s1);
668 g_free (normalized_s2);
674 forward_chars_with_skipping (GtkTextIter *iter,
676 gboolean skip_invisible,
677 gboolean skip_nontext,
678 gboolean skip_decomp)
682 g_return_if_fail (count >= 0);
688 gboolean ignored = FALSE;
690 /* minimal workaround to avoid the infinite loop of bug #168247.
691 * It doesn't fix the problemjust the symptom...
693 if (gtk_text_iter_is_end (iter))
696 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
699 if (!ignored && skip_invisible &&
700 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
703 if (!ignored && skip_decomp)
705 /* being UTF8 correct sucks; this accounts for extra
706 offsets coming from canonical decompositions of
707 UTF8 characters (e.g. accented characters) which
708 g_utf8_normalize() performs */
713 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
714 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
715 i -= (g_utf8_strlen (normal, -1) - 1);
719 gtk_text_iter_forward_char (iter);
727 lines_match (const GtkTextIter *start,
729 gboolean visible_only,
731 GtkTextIter *match_start,
732 GtkTextIter *match_end)
739 if (*lines == NULL || **lines == '\0')
742 *match_start = *start;
749 gtk_text_iter_forward_line (&next);
751 /* No more text in buffer, but *lines is nonempty */
752 if (gtk_text_iter_equal (start, &next))
758 line_text = gtk_text_iter_get_visible_slice (start, &next);
760 line_text = gtk_text_iter_get_slice (start, &next);
765 line_text = gtk_text_iter_get_visible_text (start, &next);
767 line_text = gtk_text_iter_get_text (start, &next);
770 if (match_start) /* if this is the first line we're matching */
772 found = g_utf8_strcasestr (line_text, *lines);
776 /* If it's not the first line, we have to match from the
779 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
792 /* Get offset to start of search string */
793 offset = g_utf8_strlen (line_text, found - line_text);
797 /* If match start needs to be returned, set it to the
798 * start of the search string.
800 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
806 /* Go to end of search string */
807 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
816 /* pass NULL for match_start, since we don't need to find the
819 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
822 /* strsplit () that retains the delimiter as part of the string. */
824 strbreakup (const char *string,
825 const char *delimiter,
828 GSList *string_list = NULL, *slist;
829 gchar **str_array, *s, *casefold, *new_string;
832 g_return_val_if_fail (string != NULL, NULL);
833 g_return_val_if_fail (delimiter != NULL, NULL);
836 max_tokens = G_MAXINT;
838 s = strstr (string, delimiter);
841 guint delimiter_len = strlen (delimiter);
847 len = s - string + delimiter_len;
848 new_string = g_new (gchar, len + 1);
849 strncpy (new_string, string, len);
851 casefold = g_utf8_casefold (new_string, -1);
853 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
855 string_list = g_slist_prepend (string_list, new_string);
857 string = s + delimiter_len;
858 s = strstr (string, delimiter);
859 } while (--max_tokens && s);
865 casefold = g_utf8_casefold (string, -1);
866 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
868 string_list = g_slist_prepend (string_list, new_string);
871 str_array = g_new (gchar*, n);
875 str_array[i--] = NULL;
876 for (slist = string_list; slist; slist = slist->next)
877 str_array[i--] = slist->data;
879 g_slist_free (string_list);
885 empathy_text_iter_forward_search (const GtkTextIter *iter,
887 GtkTextIter *match_start,
888 GtkTextIter *match_end,
889 const GtkTextIter *limit)
891 gchar **lines = NULL;
893 gboolean retval = FALSE;
895 gboolean visible_only;
898 g_return_val_if_fail (iter != NULL, FALSE);
899 g_return_val_if_fail (str != NULL, FALSE);
901 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
905 /* If we can move one char, return the empty string there */
908 if (gtk_text_iter_forward_char (&match)) {
909 if (limit && gtk_text_iter_equal (&match, limit)) {
914 *match_start = match;
928 /* locate all lines */
929 lines = strbreakup (str, "\n", -1);
934 /* This loop has an inefficient worst-case, where
935 * gtk_text_iter_get_text () is called repeatedly on
940 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
944 if (lines_match (&search, (const gchar**)lines,
945 visible_only, slice, &match, &end)) {
947 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
951 *match_start = match;
959 } while (gtk_text_iter_forward_line (&search));
961 g_strfreev ((gchar**)lines);
967 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
971 const gchar *ret = NULL;
974 gchar *caseless_haystack;
977 g_return_val_if_fail (haystack != NULL, NULL);
978 g_return_val_if_fail (needle != NULL, NULL);
980 casefold = g_utf8_casefold (haystack, -1);
981 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
984 needle_len = g_utf8_strlen (needle, -1);
985 haystack_len = g_utf8_strlen (caseless_haystack, -1);
989 ret = (gchar *)haystack;
993 if (haystack_len < needle_len)
999 i = haystack_len - needle_len;
1000 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1001 needle_len = strlen (needle);
1003 while (p >= caseless_haystack)
1005 if (strncmp (p, needle, needle_len) == 0)
1007 ret = pointer_from_offset_skipping_decomp (haystack, i);
1011 p = g_utf8_prev_char (p);
1016 g_free (caseless_haystack);
1022 backward_lines_match (const GtkTextIter *start,
1023 const gchar **lines,
1024 gboolean visible_only,
1026 GtkTextIter *match_start,
1027 GtkTextIter *match_end)
1029 GtkTextIter line, next;
1034 if (*lines == NULL || **lines == '\0')
1037 *match_start = *start;
1039 *match_end = *start;
1043 line = next = *start;
1044 if (gtk_text_iter_get_line_offset (&next) == 0)
1046 if (!gtk_text_iter_backward_line (&next))
1050 gtk_text_iter_set_line_offset (&next, 0);
1055 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1057 line_text = gtk_text_iter_get_slice (&next, &line);
1062 line_text = gtk_text_iter_get_visible_text (&next, &line);
1064 line_text = gtk_text_iter_get_text (&next, &line);
1067 if (match_start) /* if this is the first line we're matching */
1069 found = g_utf8_strrcasestr (line_text, *lines);
1073 /* If it's not the first line, we have to match from the
1074 * start of the line.
1076 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1089 /* Get offset to start of search string */
1090 offset = g_utf8_strlen (line_text, found - line_text);
1092 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1094 /* If match start needs to be returned, set it to the
1095 * start of the search string.
1099 *match_start = next;
1102 /* Go to end of search string */
1103 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1112 /* try to match the rest of the lines forward, passing NULL
1113 * for match_start so lines_match will try to match the entire
1115 return lines_match (&next, lines, visible_only,
1116 slice, NULL, match_end);
1120 empathy_text_iter_backward_search (const GtkTextIter *iter,
1122 GtkTextIter *match_start,
1123 GtkTextIter *match_end,
1124 const GtkTextIter *limit)
1126 gchar **lines = NULL;
1128 gboolean retval = FALSE;
1130 gboolean visible_only;
1133 g_return_val_if_fail (iter != NULL, FALSE);
1134 g_return_val_if_fail (str != NULL, FALSE);
1136 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1141 /* If we can move one char, return the empty string there */
1144 if (gtk_text_iter_backward_char (&match))
1146 if (limit && gtk_text_iter_equal (&match, limit))
1150 *match_start = match;
1161 visible_only = TRUE;
1164 /* locate all lines */
1165 lines = strbreakup (str, "\n", -1);
1171 /* This loop has an inefficient worst-case, where
1172 * gtk_text_iter_get_text () is called repeatedly on
1177 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1180 if (backward_lines_match (&search, (const gchar**)lines,
1181 visible_only, slice, &match, &end))
1183 if (limit == NULL || (limit &&
1184 gtk_text_iter_compare (&end, limit) > 0))
1189 *match_start = match;
1196 if (gtk_text_iter_get_line_offset (&search) == 0)
1198 if (!gtk_text_iter_backward_line (&search))
1203 gtk_text_iter_set_line_offset (&search, 0);
1207 g_strfreev ((gchar**)lines);
1213 empathy_window_get_is_visible (GtkWindow *window)
1215 GdkWindowState state;
1216 GdkWindow *gdk_window;
1218 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1220 gdk_window = GTK_WIDGET (window)->window;
1225 state = gdk_window_get_state (gdk_window);
1226 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1234 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1236 GdkRectangle icon_location;
1239 GdkWindow *gdk_window;
1241 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1242 gdk_window = GTK_WIDGET (window)->window;
1243 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1245 data[0] = icon_location.x;
1246 data[1] = icon_location.y;
1247 data[2] = icon_location.width;
1248 data[3] = icon_location.height;
1250 XChangeProperty (dpy,
1251 GDK_WINDOW_XID (gdk_window),
1252 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1253 "_NET_WM_ICON_GEOMETRY"),
1254 XA_CARDINAL, 32, PropModeReplace,
1255 (guchar *)&data, 4);
1257 gtk_window_set_skip_taskbar_hint (window, TRUE);
1258 gtk_window_iconify (window);
1261 /* Takes care of moving the window to the current workspace. */
1263 empathy_window_present (GtkWindow *window,
1264 gboolean steal_focus)
1268 g_return_if_fail (GTK_IS_WINDOW (window));
1270 /* There are three cases: hidden, visible, visible on another
1274 if (!empathy_window_get_is_visible (window)) {
1275 /* Hide it so present brings it to the current workspace. */
1276 gtk_widget_hide (GTK_WIDGET (window));
1279 timestamp = gtk_get_current_event_time ();
1280 gtk_window_set_skip_taskbar_hint (window, FALSE);
1281 gtk_window_present_with_time (window, timestamp);
1282 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1283 * it deiconify automatically. */
1284 gtk_window_deiconify (window);
1288 empathy_get_toplevel_window (GtkWidget *widget)
1290 GtkWidget *toplevel;
1292 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1294 toplevel = gtk_widget_get_toplevel (widget);
1295 if (GTK_IS_WINDOW (toplevel) &&
1296 GTK_WIDGET_TOPLEVEL (toplevel)) {
1297 return GTK_WINDOW (toplevel);
1303 /* The URL opening code can't handle schemeless strings, so we try to be
1304 * smart and add http if there is no scheme or doesn't look like a mail
1305 * address. This should work in most cases, and let us click on strings
1306 * like "www.gnome.org".
1309 fixup_url (const gchar *url)
1311 if (g_str_has_prefix (url, "ghelp:") ||
1312 g_str_has_prefix (url, "mailto:") ||
1313 strstr (url, ":/")) {
1317 if (strstr (url, "@")) {
1318 return g_strdup_printf ("mailto:%s", url);
1321 return g_strdup_printf ("http://%s", url);
1325 empathy_url_show (const char *url)
1328 GError *error = NULL;
1330 real_url = fixup_url (url);
1335 /* FIXME: this does not work for multihead, we should use
1336 * GdkAppLaunchContext or gtk_show_url, see bug #514396.
1338 g_app_info_launch_default_for_uri (url, NULL, &error);
1342 dialog = gtk_message_dialog_new (NULL, 0,
1343 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1344 _("Unable to open URI"));
1345 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1346 "%s", error->message);
1348 g_signal_connect (dialog, "response",
1349 G_CALLBACK (gtk_widget_destroy),
1351 gtk_window_present (GTK_WINDOW (dialog));
1353 g_clear_error (&error);
1360 link_button_hook (GtkLinkButton *button,
1364 empathy_url_show (link);
1368 empathy_link_button_new (const gchar *url,
1371 static gboolean hook = FALSE;
1375 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1378 return gtk_link_button_new_with_label (url, title);
1382 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1387 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1389 g_signal_handlers_block_by_func (widget, callback, user_data);
1390 g_object_set (widget, "active", active, NULL);
1391 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1395 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1396 const gchar *tag_name,
1397 const gchar *first_property_name,
1400 GtkTextTagTable *table;
1403 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1404 g_return_val_if_fail (tag_name != NULL, NULL);
1406 table = gtk_text_buffer_get_tag_table (buffer);
1407 tag = gtk_text_tag_table_lookup (table, tag_name);
1410 tag = gtk_text_tag_new (tag_name);
1411 gtk_text_tag_table_add (table, tag);
1412 g_object_unref (tag);
1414 /* Clear the old values so that we don't affect the new theme. */
1416 "background-set", FALSE,
1417 "foreground-set", FALSE,
1418 "invisible-set", FALSE,
1419 "justification-set", FALSE,
1420 "paragraph-background-set", FALSE,
1421 "pixels-above-lines-set", FALSE,
1422 "pixels-below-lines-set", FALSE,
1427 "weight-set", FALSE,
1431 if (first_property_name) {
1434 va_start (list, first_property_name);
1435 g_object_set_valist (G_OBJECT (tag), first_property_name, list);