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 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):
33 #include <glib/gi18n.h>
35 #include <glade/glade.h>
36 #include <libgnomevfs/gnome-vfs-utils.h>
38 #include <libmissioncontrol/mc-profile.h>
40 #include <libempathy/empathy-debug.h>
42 #include "empathy-ui-utils.h"
43 #include "empathy-images.h"
45 #define DEBUG_DOMAIN "UiUtils"
50 gboolean preserve_aspect_ratio;
54 get_glade_file (const gchar *filename,
57 const gchar *first_required_widget,
63 GtkWidget **widget_ptr;
65 path = g_build_filename (DATADIR, "empathy", filename, NULL);
66 gui = glade_xml_new (path, root, domain);
70 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,
112 g_object_unref (gui);
116 empathy_glade_get_file (const gchar *filename,
119 const gchar *first_required_widget, ...)
124 va_start (args, first_required_widget);
126 gui = get_glade_file (filename,
129 first_required_widget,
142 empathy_glade_connect (GladeXML *gui,
144 gchar *first_widget, ...)
152 va_start (args, first_widget);
154 for (name = first_widget; name; name = va_arg (args, char *)) {
155 signal = va_arg (args, void *);
156 callback = va_arg (args, void *);
158 widget = glade_xml_get_widget (gui, name);
160 g_warning ("Glade file is missing widget '%s', aborting",
165 g_signal_connect (widget,
167 G_CALLBACK (callback),
175 empathy_glade_setup_size_group (GladeXML *gui,
176 GtkSizeGroupMode mode,
177 gchar *first_widget, ...)
181 GtkSizeGroup *size_group;
184 va_start (args, first_widget);
186 size_group = gtk_size_group_new (mode);
188 for (name = first_widget; name; name = va_arg (args, char *)) {
189 widget = glade_xml_get_widget (gui, name);
191 g_warning ("Glade file is missing widget '%s'", name);
195 gtk_size_group_add_widget (size_group, widget);
198 g_object_unref (size_group);
204 empathy_pixbuf_from_icon_name (const gchar *icon_name,
205 GtkIconSize icon_size)
208 GdkPixbuf *pixbuf = NULL;
209 GError *error = NULL;
213 theme = gtk_icon_theme_get_default ();
215 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
219 pixbuf = gtk_icon_theme_load_icon (theme,
225 empathy_debug (DEBUG_DOMAIN, "Error loading icon: %s", error->message);
226 g_clear_error (&error);
233 empathy_pixbuf_from_smiley (EmpathySmiley type,
234 GtkIconSize icon_size)
236 const gchar *icon_id;
239 case EMPATHY_SMILEY_NORMAL: /* :) */
240 icon_id = "stock_smiley-1";
242 case EMPATHY_SMILEY_WINK: /* ;) */
243 icon_id = "stock_smiley-3";
245 case EMPATHY_SMILEY_BIGEYE: /* =) */
246 icon_id = "stock_smiley-2";
248 case EMPATHY_SMILEY_NOSE: /* :-) */
249 icon_id = "stock_smiley-7";
251 case EMPATHY_SMILEY_CRY: /* :'( */
252 icon_id = "stock_smiley-11";
254 case EMPATHY_SMILEY_SAD: /* :( */
255 icon_id = "stock_smiley-4";
257 case EMPATHY_SMILEY_SCEPTICAL: /* :/ */
258 icon_id = "stock_smiley-9";
260 case EMPATHY_SMILEY_BIGSMILE: /* :D */
261 icon_id = "stock_smiley-6";
263 case EMPATHY_SMILEY_INDIFFERENT: /* :| */
264 icon_id = "stock_smiley-8";
266 case EMPATHY_SMILEY_TOUNGE: /* :p */
267 icon_id = "stock_smiley-10";
269 case EMPATHY_SMILEY_SHOCKED: /* :o */
270 icon_id = "stock_smiley-5";
272 case EMPATHY_SMILEY_COOL: /* 8) */
273 icon_id = "stock_smiley-15";
275 case EMPATHY_SMILEY_SORRY: /* *| */
276 icon_id = "stock_smiley-12";
278 case EMPATHY_SMILEY_KISS: /* :* */
279 icon_id = "stock_smiley-13";
281 case EMPATHY_SMILEY_SHUTUP: /* :# */
282 icon_id = "stock_smiley-14";
284 case EMPATHY_SMILEY_YAWN: /* |O */
287 case EMPATHY_SMILEY_CONFUSED: /* :$ */
288 icon_id = "stock_smiley-17";
290 case EMPATHY_SMILEY_ANGEL: /* O) */
291 icon_id = "stock_smiley-18";
293 case EMPATHY_SMILEY_OOOH: /* :x */
294 icon_id = "stock_smiley-19";
296 case EMPATHY_SMILEY_LOOKAWAY: /* *) */
297 icon_id = "stock_smiley-20";
299 case EMPATHY_SMILEY_BLUSH: /* *S */
300 icon_id = "stock_smiley-23";
302 case EMPATHY_SMILEY_COOLBIGSMILE: /* 8D */
303 icon_id = "stock_smiley-25";
305 case EMPATHY_SMILEY_ANGRY: /* :@ */
306 icon_id = "stock_smiley-16";
308 case EMPATHY_SMILEY_BOSS: /* @) */
309 icon_id = "stock_smiley-21";
311 case EMPATHY_SMILEY_MONKEY: /* #) */
312 icon_id = "stock_smiley-22";
314 case EMPATHY_SMILEY_SILLY: /* O) */
315 icon_id = "stock_smiley-24";
317 case EMPATHY_SMILEY_SICK: /* +o( */
318 icon_id = "stock_smiley-26";
322 g_assert_not_reached ();
327 return empathy_pixbuf_from_icon_name (icon_id, icon_size);
331 empathy_icon_name_from_account (McAccount *account)
335 profile = mc_account_get_profile (account);
337 return mc_profile_get_icon_name (profile);
341 empathy_icon_name_for_presence_state (McPresence 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;
357 g_assert_not_reached ();
364 empathy_icon_name_for_presence (EmpathyPresence *presence)
368 g_return_val_if_fail (EMPATHY_IS_PRESENCE (presence),
369 EMPATHY_IMAGE_OFFLINE);
371 state = empathy_presence_get_state (presence);
373 return empathy_icon_name_for_presence_state (state);
377 empathy_icon_name_for_contact (EmpathyContact *contact)
379 EmpathyPresence *presence;
380 EmpathySubscription subscription;
382 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
383 EMPATHY_IMAGE_OFFLINE);
385 presence = empathy_contact_get_presence (contact);
387 return empathy_icon_name_for_presence (presence);
390 subscription = empathy_contact_get_subscription (contact);
391 if (!(subscription & EMPATHY_SUBSCRIPTION_FROM)) {
392 return EMPATHY_IMAGE_PENDING;
395 return EMPATHY_IMAGE_OFFLINE;
399 empathy_pixbuf_avatar_from_contact (EmpathyContact *contact)
402 GdkPixbufLoader *loader;
403 EmpathyAvatar *avatar;
404 GError *error = NULL;
406 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
408 avatar = empathy_contact_get_avatar (contact);
413 loader = gdk_pixbuf_loader_new ();
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);
423 gdk_pixbuf_loader_close (loader, NULL);
425 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
427 g_object_ref (pixbuf);
428 g_object_unref (loader);
434 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
437 struct SizeData *data)
439 g_return_if_fail (width > 0 && height > 0);
441 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
442 if (width < data->width && height < data->height) {
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;
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;
458 height = 0.5 + (double) height * (double) data->width / (double) width;
462 if (data->width > 0) {
466 if (data->height > 0) {
467 height = data->height;
471 gdk_pixbuf_loader_set_size (loader, width, height);
475 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
480 GdkPixbufLoader *loader;
481 struct SizeData data;
482 GError *error = NULL;
489 data.height = height;
490 data.preserve_aspect_ratio = TRUE;
492 loader = gdk_pixbuf_loader_new ();
494 g_signal_connect (loader, "size-prepared",
495 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
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);
506 gdk_pixbuf_loader_close (loader, NULL);
508 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
510 g_object_ref (pixbuf);
511 g_object_unref (loader);
517 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
521 EmpathyAvatar *avatar;
523 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
525 avatar = empathy_contact_get_avatar (contact);
527 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
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.
532 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
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. */
539 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
541 gchar *casefold, *normal;
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);
559 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
563 const gchar *ret = NULL;
566 gchar *caseless_haystack;
569 g_return_val_if_fail (haystack != NULL, NULL);
570 g_return_val_if_fail (needle != NULL, NULL);
572 casefold = g_utf8_casefold (haystack, -1);
573 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
576 needle_len = g_utf8_strlen (needle, -1);
577 haystack_len = g_utf8_strlen (caseless_haystack, -1);
581 ret = (gchar *)haystack;
585 if (haystack_len < needle_len)
591 p = (gchar*)caseless_haystack;
592 needle_len = strlen (needle);
597 if ((strncmp (p, needle, needle_len) == 0))
599 ret = pointer_from_offset_skipping_decomp (haystack, i);
603 p = g_utf8_next_char (p);
608 g_free (caseless_haystack);
614 g_utf8_caselessnmatch (const char *s1, const char *s2,
615 gssize n1, gssize n2)
618 gchar *normalized_s1;
619 gchar *normalized_s2;
622 gboolean ret = FALSE;
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);
629 casefold = g_utf8_casefold (s1, n1);
630 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
633 casefold = g_utf8_casefold (s2, n2);
634 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
637 len_s1 = strlen (normalized_s1);
638 len_s2 = strlen (normalized_s2);
643 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
646 g_free (normalized_s1);
647 g_free (normalized_s2);
653 forward_chars_with_skipping (GtkTextIter *iter,
655 gboolean skip_invisible,
656 gboolean skip_nontext,
657 gboolean skip_decomp)
661 g_return_if_fail (count >= 0);
667 gboolean ignored = FALSE;
669 /* minimal workaround to avoid the infinite loop of bug #168247.
670 * It doesn't fix the problemjust the symptom...
672 if (gtk_text_iter_is_end (iter))
675 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
678 if (!ignored && skip_invisible &&
679 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
682 if (!ignored && skip_decomp)
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 */
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);
698 gtk_text_iter_forward_char (iter);
706 lines_match (const GtkTextIter *start,
708 gboolean visible_only,
710 GtkTextIter *match_start,
711 GtkTextIter *match_end)
718 if (*lines == NULL || **lines == '\0')
721 *match_start = *start;
728 gtk_text_iter_forward_line (&next);
730 /* No more text in buffer, but *lines is nonempty */
731 if (gtk_text_iter_equal (start, &next))
737 line_text = gtk_text_iter_get_visible_slice (start, &next);
739 line_text = gtk_text_iter_get_slice (start, &next);
744 line_text = gtk_text_iter_get_visible_text (start, &next);
746 line_text = gtk_text_iter_get_text (start, &next);
749 if (match_start) /* if this is the first line we're matching */
751 found = g_utf8_strcasestr (line_text, *lines);
755 /* If it's not the first line, we have to match from the
758 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
771 /* Get offset to start of search string */
772 offset = g_utf8_strlen (line_text, found - line_text);
776 /* If match start needs to be returned, set it to the
777 * start of the search string.
779 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
785 /* Go to end of search string */
786 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
795 /* pass NULL for match_start, since we don't need to find the
798 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
801 /* strsplit () that retains the delimiter as part of the string. */
803 strbreakup (const char *string,
804 const char *delimiter,
807 GSList *string_list = NULL, *slist;
808 gchar **str_array, *s, *casefold, *new_string;
811 g_return_val_if_fail (string != NULL, NULL);
812 g_return_val_if_fail (delimiter != NULL, NULL);
815 max_tokens = G_MAXINT;
817 s = strstr (string, delimiter);
820 guint delimiter_len = strlen (delimiter);
826 len = s - string + delimiter_len;
827 new_string = g_new (gchar, len + 1);
828 strncpy (new_string, string, len);
830 casefold = g_utf8_casefold (new_string, -1);
832 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
834 string_list = g_slist_prepend (string_list, new_string);
836 string = s + delimiter_len;
837 s = strstr (string, delimiter);
838 } while (--max_tokens && s);
844 casefold = g_utf8_casefold (string, -1);
845 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
847 string_list = g_slist_prepend (string_list, new_string);
850 str_array = g_new (gchar*, n);
854 str_array[i--] = NULL;
855 for (slist = string_list; slist; slist = slist->next)
856 str_array[i--] = slist->data;
858 g_slist_free (string_list);
864 empathy_text_iter_forward_search (const GtkTextIter *iter,
866 GtkTextIter *match_start,
867 GtkTextIter *match_end,
868 const GtkTextIter *limit)
870 gchar **lines = NULL;
872 gboolean retval = FALSE;
874 gboolean visible_only;
877 g_return_val_if_fail (iter != NULL, FALSE);
878 g_return_val_if_fail (str != NULL, FALSE);
880 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
884 /* If we can move one char, return the empty string there */
887 if (gtk_text_iter_forward_char (&match)) {
888 if (limit && gtk_text_iter_equal (&match, limit)) {
893 *match_start = match;
907 /* locate all lines */
908 lines = strbreakup (str, "\n", -1);
913 /* This loop has an inefficient worst-case, where
914 * gtk_text_iter_get_text () is called repeatedly on
919 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
923 if (lines_match (&search, (const gchar**)lines,
924 visible_only, slice, &match, &end)) {
926 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
930 *match_start = match;
938 } while (gtk_text_iter_forward_line (&search));
940 g_strfreev ((gchar**)lines);
946 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
950 const gchar *ret = NULL;
953 gchar *caseless_haystack;
956 g_return_val_if_fail (haystack != NULL, NULL);
957 g_return_val_if_fail (needle != NULL, NULL);
959 casefold = g_utf8_casefold (haystack, -1);
960 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
963 needle_len = g_utf8_strlen (needle, -1);
964 haystack_len = g_utf8_strlen (caseless_haystack, -1);
968 ret = (gchar *)haystack;
972 if (haystack_len < needle_len)
978 i = haystack_len - needle_len;
979 p = g_utf8_offset_to_pointer (caseless_haystack, i);
980 needle_len = strlen (needle);
982 while (p >= caseless_haystack)
984 if (strncmp (p, needle, needle_len) == 0)
986 ret = pointer_from_offset_skipping_decomp (haystack, i);
990 p = g_utf8_prev_char (p);
995 g_free (caseless_haystack);
1001 backward_lines_match (const GtkTextIter *start,
1002 const gchar **lines,
1003 gboolean visible_only,
1005 GtkTextIter *match_start,
1006 GtkTextIter *match_end)
1008 GtkTextIter line, next;
1013 if (*lines == NULL || **lines == '\0')
1016 *match_start = *start;
1018 *match_end = *start;
1022 line = next = *start;
1023 if (gtk_text_iter_get_line_offset (&next) == 0)
1025 if (!gtk_text_iter_backward_line (&next))
1029 gtk_text_iter_set_line_offset (&next, 0);
1034 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1036 line_text = gtk_text_iter_get_slice (&next, &line);
1041 line_text = gtk_text_iter_get_visible_text (&next, &line);
1043 line_text = gtk_text_iter_get_text (&next, &line);
1046 if (match_start) /* if this is the first line we're matching */
1048 found = g_utf8_strrcasestr (line_text, *lines);
1052 /* If it's not the first line, we have to match from the
1053 * start of the line.
1055 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1068 /* Get offset to start of search string */
1069 offset = g_utf8_strlen (line_text, found - line_text);
1071 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1073 /* If match start needs to be returned, set it to the
1074 * start of the search string.
1078 *match_start = next;
1081 /* Go to end of search string */
1082 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
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
1094 return lines_match (&next, lines, visible_only,
1095 slice, NULL, match_end);
1099 empathy_text_iter_backward_search (const GtkTextIter *iter,
1101 GtkTextIter *match_start,
1102 GtkTextIter *match_end,
1103 const GtkTextIter *limit)
1105 gchar **lines = NULL;
1107 gboolean retval = FALSE;
1109 gboolean visible_only;
1112 g_return_val_if_fail (iter != NULL, FALSE);
1113 g_return_val_if_fail (str != NULL, FALSE);
1115 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1120 /* If we can move one char, return the empty string there */
1123 if (gtk_text_iter_backward_char (&match))
1125 if (limit && gtk_text_iter_equal (&match, limit))
1129 *match_start = match;
1140 visible_only = TRUE;
1143 /* locate all lines */
1144 lines = strbreakup (str, "\n", -1);
1150 /* This loop has an inefficient worst-case, where
1151 * gtk_text_iter_get_text () is called repeatedly on
1156 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1159 if (backward_lines_match (&search, (const gchar**)lines,
1160 visible_only, slice, &match, &end))
1162 if (limit == NULL || (limit &&
1163 gtk_text_iter_compare (&end, limit) > 0))
1168 *match_start = match;
1175 if (gtk_text_iter_get_line_offset (&search) == 0)
1177 if (!gtk_text_iter_backward_line (&search))
1182 gtk_text_iter_set_line_offset (&search, 0);
1186 g_strfreev ((gchar**)lines);
1192 window_get_is_on_current_workspace (GtkWindow *window)
1194 GdkWindow *gdk_window;
1196 gdk_window = GTK_WIDGET (window)->window;
1198 return !(gdk_window_get_state (gdk_window) &
1199 GDK_WINDOW_STATE_ICONIFIED);
1205 /* Checks if the window is visible as in visible on the current workspace. */
1207 empathy_window_get_is_visible (GtkWindow *window)
1211 g_return_val_if_fail (window != NULL, FALSE);
1213 g_object_get (window,
1214 "visible", &visible,
1217 return visible && window_get_is_on_current_workspace (window);
1220 /* Takes care of moving the window to the current workspace. */
1222 empathy_window_present (GtkWindow *window,
1223 gboolean steal_focus)
1226 gboolean on_current;
1229 g_return_if_fail (window != NULL);
1231 /* There are three cases: hidden, visible, visible on another
1235 g_object_get (window,
1236 "visible", &visible,
1239 on_current = window_get_is_on_current_workspace (window);
1241 if (visible && !on_current) {
1242 /* Hide it so present brings it to the current workspace. */
1243 gtk_widget_hide (GTK_WIDGET (window));
1246 timestamp = gtk_get_current_event_time ();
1247 if (steal_focus && timestamp != GDK_CURRENT_TIME) {
1248 gtk_window_present_with_time (window, timestamp);
1250 gtk_window_present (window);
1255 empathy_get_toplevel_window (GtkWidget *widget)
1257 GtkWidget *toplevel;
1259 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1261 toplevel = gtk_widget_get_toplevel (widget);
1262 if (GTK_IS_WINDOW (toplevel)) {
1263 return GTK_WINDOW (toplevel);
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".
1275 fixup_url (const gchar *url)
1279 if (!g_str_has_prefix (url, "http://") &&
1280 !strstr (url, ":/") &&
1281 !strstr (url, "@")) {
1282 real_url = g_strdup_printf ("http://%s", url);
1284 real_url = g_strdup (url);
1291 empathy_url_show (const char *url)
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",
1301 gnome_vfs_result_to_string (res));
1308 link_button_hook (GtkLinkButton *button,
1312 empathy_url_show (link);
1316 empathy_link_button_new (const gchar *url,
1319 static gboolean hook = FALSE;
1323 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1326 return gtk_link_button_new_with_label (url, title);
1330 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1335 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
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);