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.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>
54 gboolean preserve_aspect_ratio;
58 get_glade_file (const gchar *filename,
61 const gchar *first_required_widget,
66 GtkWidget **widget_ptr;
68 DEBUG ("Loading glade file %s", filename);
70 gui = glade_xml_new (filename, root, domain);
73 g_warning ("Couldn't find necessary glade file '%s'", filename);
76 for (name = first_required_widget; name; name = va_arg (args, char *)) {
77 widget_ptr = va_arg (args, void *);
79 *widget_ptr = glade_xml_get_widget (gui, name);
82 g_warning ("Glade file '%s' is missing widget '%s'.",
92 empathy_glade_get_file_simple (const gchar *filename,
95 const gchar *first_required_widget, ...)
100 va_start (args, first_required_widget);
102 gui = get_glade_file (filename,
105 first_required_widget,
111 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_icon_name_from_account (McAccount *account)
208 profile = mc_account_get_profile (account);
210 return mc_profile_get_icon_name (profile);
214 empathy_icon_name_for_presence (McPresence presence)
217 case MC_PRESENCE_AVAILABLE:
218 return EMPATHY_IMAGE_AVAILABLE;
219 case MC_PRESENCE_DO_NOT_DISTURB:
220 return EMPATHY_IMAGE_BUSY;
221 case MC_PRESENCE_AWAY:
222 return EMPATHY_IMAGE_AWAY;
223 case MC_PRESENCE_EXTENDED_AWAY:
224 return EMPATHY_IMAGE_EXT_AWAY;
225 case MC_PRESENCE_HIDDEN:
226 return EMPATHY_IMAGE_HIDDEN;
227 case MC_PRESENCE_OFFLINE:
228 case MC_PRESENCE_UNSET:
229 return EMPATHY_IMAGE_OFFLINE;
231 g_assert_not_reached ();
238 empathy_icon_name_for_contact (EmpathyContact *contact)
242 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
243 EMPATHY_IMAGE_OFFLINE);
245 presence = empathy_contact_get_presence (contact);
246 return empathy_icon_name_for_presence (presence);
250 empathy_pixbuf_from_data (gchar *data,
253 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
257 empathy_pixbuf_from_data_and_mime (gchar *data,
261 GdkPixbufLoader *loader;
262 GdkPixbufFormat *format;
263 GdkPixbuf *pixbuf = NULL;
265 GError *error = NULL;
271 loader = gdk_pixbuf_loader_new ();
272 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
273 DEBUG ("Failed to write to pixbuf loader: %s",
274 error ? error->message : "No error given");
277 if (!gdk_pixbuf_loader_close (loader, &error)) {
278 DEBUG ("Failed to close pixbuf loader: %s",
279 error ? error->message : "No error given");
283 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
285 g_object_ref (pixbuf);
287 if (mime_type != NULL) {
288 format = gdk_pixbuf_loader_get_format (loader);
289 mime_types = gdk_pixbuf_format_get_mime_types (format);
291 *mime_type = g_strdup (*mime_types);
292 if (mime_types[1] != NULL) {
293 DEBUG ("Loader supports more than one mime "
294 "type! Picking the first one, %s",
297 g_strfreev (mime_types);
302 g_clear_error (&error);
303 g_object_unref (loader);
309 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
312 struct SizeData *data)
314 g_return_if_fail (width > 0 && height > 0);
316 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
317 if (width < data->width && height < data->height) {
322 if (data->width < 0) {
323 width = width * (double) data->height / (gdouble) height;
324 height = data->height;
325 } else if (data->height < 0) {
326 height = height * (double) data->width / (double) width;
328 } else if ((double) height * (double) data->width >
329 (double) width * (double) data->height) {
330 width = 0.5 + (double) width * (double) data->height / (double) height;
331 height = data->height;
333 height = 0.5 + (double) height * (double) data->width / (double) width;
337 if (data->width > 0) {
341 if (data->height > 0) {
342 height = data->height;
346 gdk_pixbuf_loader_set_size (loader, width, height);
350 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
352 gint width, height, rowstride;
355 width = gdk_pixbuf_get_width (pixbuf);
356 height = gdk_pixbuf_get_height (pixbuf);
357 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
358 pixels = gdk_pixbuf_get_pixels (pixbuf);
360 if (width < 6 || height < 6) {
368 pixels[rowstride + 3] = 0x80;
369 pixels[rowstride * 2 + 3] = 0xC0;
372 pixels[width * 4 - 1] = 0;
373 pixels[width * 4 - 5] = 0x80;
374 pixels[width * 4 - 9] = 0xC0;
375 pixels[rowstride + (width * 4) - 1] = 0x80;
376 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
379 pixels[(height - 1) * rowstride + 3] = 0;
380 pixels[(height - 1) * rowstride + 7] = 0x80;
381 pixels[(height - 1) * rowstride + 11] = 0xC0;
382 pixels[(height - 2) * rowstride + 3] = 0x80;
383 pixels[(height - 3) * rowstride + 3] = 0xC0;
386 pixels[height * rowstride - 1] = 0;
387 pixels[(height - 1) * rowstride - 1] = 0x80;
388 pixels[(height - 2) * rowstride - 1] = 0xC0;
389 pixels[height * rowstride - 5] = 0x80;
390 pixels[height * rowstride - 9] = 0xC0;
394 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
396 gint width, height, rowstride, i;
400 width = gdk_pixbuf_get_width (pixbuf);
401 height = gdk_pixbuf_get_height (pixbuf);
402 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
403 pixels = gdk_pixbuf_get_pixels (pixbuf);
406 for (i = 3; i < rowstride; i+=4) {
412 for (i = 1; i < height - 1; i++) {
413 row = pixels + (i*rowstride);
414 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
419 row = pixels + ((height-1) * rowstride);
420 for (i = 3; i < rowstride; i+=4) {
430 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
435 GdkPixbufLoader *loader;
436 struct SizeData data;
437 GError *error = NULL;
444 data.height = height;
445 data.preserve_aspect_ratio = TRUE;
447 loader = gdk_pixbuf_loader_new ();
449 g_signal_connect (loader, "size-prepared",
450 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
453 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
454 g_warning ("Couldn't write avatar image:%p with "
455 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
456 avatar->data, avatar->len, error->message);
457 g_error_free (error);
461 gdk_pixbuf_loader_close (loader, NULL);
463 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
464 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
465 GdkPixbuf *rounded_pixbuf;
467 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
468 gdk_pixbuf_get_width (pixbuf),
469 gdk_pixbuf_get_height (pixbuf));
470 gdk_pixbuf_copy_area (pixbuf, 0, 0,
471 gdk_pixbuf_get_width (pixbuf),
472 gdk_pixbuf_get_height (pixbuf),
475 pixbuf = rounded_pixbuf;
477 g_object_ref (pixbuf);
480 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
481 empathy_avatar_pixbuf_roundify (pixbuf);
484 g_object_unref (loader);
490 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
494 EmpathyAvatar *avatar;
496 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
498 avatar = empathy_contact_get_avatar (contact);
500 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
504 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
509 width = gdk_pixbuf_get_width (pixbuf);
510 height = gdk_pixbuf_get_height (pixbuf);
512 if (width > 0 && (width > max_size || height > max_size)) {
513 factor = (gdouble) max_size / MAX (width, height);
515 width = width * factor;
516 height = height * factor;
518 return gdk_pixbuf_scale_simple (pixbuf,
523 return g_object_ref (pixbuf);
527 empathy_pixbuf_from_icon_name (const gchar *icon_name,
528 GtkIconSize icon_size)
531 GdkPixbuf *pixbuf = NULL;
532 GError *error = NULL;
540 theme = gtk_icon_theme_get_default ();
542 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
546 pixbuf = gtk_icon_theme_load_icon (theme,
552 DEBUG ("Error loading icon: %s", error->message);
553 g_clear_error (&error);
559 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
560 * that to make it easier to apply changes from the original code.
562 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
564 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
565 * decomposable character it consumes the decomposition length from the given
566 * offset. So it's useful when the offset was calculated for the normalized
567 * version of str, but we need a pointer to str itself. */
569 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
571 gchar *casefold, *normal;
577 q = g_utf8_next_char (p);
578 casefold = g_utf8_casefold (p, q - p);
579 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
580 offset -= g_utf8_strlen (normal, -1);
589 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
593 const gchar *ret = NULL;
596 gchar *caseless_haystack;
599 g_return_val_if_fail (haystack != NULL, NULL);
600 g_return_val_if_fail (needle != NULL, NULL);
602 casefold = g_utf8_casefold (haystack, -1);
603 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
606 needle_len = g_utf8_strlen (needle, -1);
607 haystack_len = g_utf8_strlen (caseless_haystack, -1);
611 ret = (gchar *)haystack;
615 if (haystack_len < needle_len)
621 p = (gchar*)caseless_haystack;
622 needle_len = strlen (needle);
627 if ((strncmp (p, needle, needle_len) == 0))
629 ret = pointer_from_offset_skipping_decomp (haystack, i);
633 p = g_utf8_next_char (p);
638 g_free (caseless_haystack);
644 g_utf8_caselessnmatch (const char *s1, const char *s2,
645 gssize n1, gssize n2)
648 gchar *normalized_s1;
649 gchar *normalized_s2;
652 gboolean ret = FALSE;
654 g_return_val_if_fail (s1 != NULL, FALSE);
655 g_return_val_if_fail (s2 != NULL, FALSE);
656 g_return_val_if_fail (n1 > 0, FALSE);
657 g_return_val_if_fail (n2 > 0, FALSE);
659 casefold = g_utf8_casefold (s1, n1);
660 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
663 casefold = g_utf8_casefold (s2, n2);
664 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
667 len_s1 = strlen (normalized_s1);
668 len_s2 = strlen (normalized_s2);
673 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
676 g_free (normalized_s1);
677 g_free (normalized_s2);
683 forward_chars_with_skipping (GtkTextIter *iter,
685 gboolean skip_invisible,
686 gboolean skip_nontext,
687 gboolean skip_decomp)
691 g_return_if_fail (count >= 0);
697 gboolean ignored = FALSE;
699 /* minimal workaround to avoid the infinite loop of bug #168247.
700 * It doesn't fix the problemjust the symptom...
702 if (gtk_text_iter_is_end (iter))
705 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
708 if (!ignored && skip_invisible &&
709 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
712 if (!ignored && skip_decomp)
714 /* being UTF8 correct sucks; this accounts for extra
715 offsets coming from canonical decompositions of
716 UTF8 characters (e.g. accented characters) which
717 g_utf8_normalize() performs */
722 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
723 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
724 i -= (g_utf8_strlen (normal, -1) - 1);
728 gtk_text_iter_forward_char (iter);
736 lines_match (const GtkTextIter *start,
738 gboolean visible_only,
740 GtkTextIter *match_start,
741 GtkTextIter *match_end)
748 if (*lines == NULL || **lines == '\0')
751 *match_start = *start;
758 gtk_text_iter_forward_line (&next);
760 /* No more text in buffer, but *lines is nonempty */
761 if (gtk_text_iter_equal (start, &next))
767 line_text = gtk_text_iter_get_visible_slice (start, &next);
769 line_text = gtk_text_iter_get_slice (start, &next);
774 line_text = gtk_text_iter_get_visible_text (start, &next);
776 line_text = gtk_text_iter_get_text (start, &next);
779 if (match_start) /* if this is the first line we're matching */
781 found = g_utf8_strcasestr (line_text, *lines);
785 /* If it's not the first line, we have to match from the
788 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
801 /* Get offset to start of search string */
802 offset = g_utf8_strlen (line_text, found - line_text);
806 /* If match start needs to be returned, set it to the
807 * start of the search string.
809 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
815 /* Go to end of search string */
816 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
825 /* pass NULL for match_start, since we don't need to find the
828 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
831 /* strsplit () that retains the delimiter as part of the string. */
833 strbreakup (const char *string,
834 const char *delimiter,
837 GSList *string_list = NULL, *slist;
838 gchar **str_array, *s, *casefold, *new_string;
841 g_return_val_if_fail (string != NULL, NULL);
842 g_return_val_if_fail (delimiter != NULL, NULL);
845 max_tokens = G_MAXINT;
847 s = strstr (string, delimiter);
850 guint delimiter_len = strlen (delimiter);
856 len = s - string + delimiter_len;
857 new_string = g_new (gchar, len + 1);
858 strncpy (new_string, string, len);
860 casefold = g_utf8_casefold (new_string, -1);
862 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
864 string_list = g_slist_prepend (string_list, new_string);
866 string = s + delimiter_len;
867 s = strstr (string, delimiter);
868 } while (--max_tokens && s);
874 casefold = g_utf8_casefold (string, -1);
875 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
877 string_list = g_slist_prepend (string_list, new_string);
880 str_array = g_new (gchar*, n);
884 str_array[i--] = NULL;
885 for (slist = string_list; slist; slist = slist->next)
886 str_array[i--] = slist->data;
888 g_slist_free (string_list);
894 empathy_text_iter_forward_search (const GtkTextIter *iter,
896 GtkTextIter *match_start,
897 GtkTextIter *match_end,
898 const GtkTextIter *limit)
900 gchar **lines = NULL;
902 gboolean retval = FALSE;
904 gboolean visible_only;
907 g_return_val_if_fail (iter != NULL, FALSE);
908 g_return_val_if_fail (str != NULL, FALSE);
910 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
914 /* If we can move one char, return the empty string there */
917 if (gtk_text_iter_forward_char (&match)) {
918 if (limit && gtk_text_iter_equal (&match, limit)) {
923 *match_start = match;
937 /* locate all lines */
938 lines = strbreakup (str, "\n", -1);
943 /* This loop has an inefficient worst-case, where
944 * gtk_text_iter_get_text () is called repeatedly on
949 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
953 if (lines_match (&search, (const gchar**)lines,
954 visible_only, slice, &match, &end)) {
956 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
960 *match_start = match;
968 } while (gtk_text_iter_forward_line (&search));
970 g_strfreev ((gchar**)lines);
976 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
980 const gchar *ret = NULL;
983 gchar *caseless_haystack;
986 g_return_val_if_fail (haystack != NULL, NULL);
987 g_return_val_if_fail (needle != NULL, NULL);
989 casefold = g_utf8_casefold (haystack, -1);
990 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
993 needle_len = g_utf8_strlen (needle, -1);
994 haystack_len = g_utf8_strlen (caseless_haystack, -1);
998 ret = (gchar *)haystack;
1002 if (haystack_len < needle_len)
1008 i = haystack_len - needle_len;
1009 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1010 needle_len = strlen (needle);
1012 while (p >= caseless_haystack)
1014 if (strncmp (p, needle, needle_len) == 0)
1016 ret = pointer_from_offset_skipping_decomp (haystack, i);
1020 p = g_utf8_prev_char (p);
1025 g_free (caseless_haystack);
1031 backward_lines_match (const GtkTextIter *start,
1032 const gchar **lines,
1033 gboolean visible_only,
1035 GtkTextIter *match_start,
1036 GtkTextIter *match_end)
1038 GtkTextIter line, next;
1043 if (*lines == NULL || **lines == '\0')
1046 *match_start = *start;
1048 *match_end = *start;
1052 line = next = *start;
1053 if (gtk_text_iter_get_line_offset (&next) == 0)
1055 if (!gtk_text_iter_backward_line (&next))
1059 gtk_text_iter_set_line_offset (&next, 0);
1064 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1066 line_text = gtk_text_iter_get_slice (&next, &line);
1071 line_text = gtk_text_iter_get_visible_text (&next, &line);
1073 line_text = gtk_text_iter_get_text (&next, &line);
1076 if (match_start) /* if this is the first line we're matching */
1078 found = g_utf8_strrcasestr (line_text, *lines);
1082 /* If it's not the first line, we have to match from the
1083 * start of the line.
1085 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1098 /* Get offset to start of search string */
1099 offset = g_utf8_strlen (line_text, found - line_text);
1101 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1103 /* If match start needs to be returned, set it to the
1104 * start of the search string.
1108 *match_start = next;
1111 /* Go to end of search string */
1112 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1121 /* try to match the rest of the lines forward, passing NULL
1122 * for match_start so lines_match will try to match the entire
1124 return lines_match (&next, lines, visible_only,
1125 slice, NULL, match_end);
1129 empathy_text_iter_backward_search (const GtkTextIter *iter,
1131 GtkTextIter *match_start,
1132 GtkTextIter *match_end,
1133 const GtkTextIter *limit)
1135 gchar **lines = NULL;
1137 gboolean retval = FALSE;
1139 gboolean visible_only;
1142 g_return_val_if_fail (iter != NULL, FALSE);
1143 g_return_val_if_fail (str != NULL, FALSE);
1145 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1150 /* If we can move one char, return the empty string there */
1153 if (gtk_text_iter_backward_char (&match))
1155 if (limit && gtk_text_iter_equal (&match, limit))
1159 *match_start = match;
1170 visible_only = TRUE;
1173 /* locate all lines */
1174 lines = strbreakup (str, "\n", -1);
1180 /* This loop has an inefficient worst-case, where
1181 * gtk_text_iter_get_text () is called repeatedly on
1186 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1189 if (backward_lines_match (&search, (const gchar**)lines,
1190 visible_only, slice, &match, &end))
1192 if (limit == NULL || (limit &&
1193 gtk_text_iter_compare (&end, limit) > 0))
1198 *match_start = match;
1205 if (gtk_text_iter_get_line_offset (&search) == 0)
1207 if (!gtk_text_iter_backward_line (&search))
1212 gtk_text_iter_set_line_offset (&search, 0);
1216 g_strfreev ((gchar**)lines);
1222 empathy_window_get_is_visible (GtkWindow *window)
1224 GdkWindowState state;
1225 GdkWindow *gdk_window;
1227 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1229 gdk_window = GTK_WIDGET (window)->window;
1234 state = gdk_window_get_state (gdk_window);
1235 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1243 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1245 GdkRectangle icon_location;
1248 GdkWindow *gdk_window;
1250 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1251 gdk_window = GTK_WIDGET (window)->window;
1252 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1254 data[0] = icon_location.x;
1255 data[1] = icon_location.y;
1256 data[2] = icon_location.width;
1257 data[3] = icon_location.height;
1259 XChangeProperty (dpy,
1260 GDK_WINDOW_XID (gdk_window),
1261 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1262 "_NET_WM_ICON_GEOMETRY"),
1263 XA_CARDINAL, 32, PropModeReplace,
1264 (guchar *)&data, 4);
1266 gtk_window_set_skip_taskbar_hint (window, TRUE);
1267 gtk_window_iconify (window);
1270 /* Takes care of moving the window to the current workspace. */
1272 empathy_window_present (GtkWindow *window,
1273 gboolean steal_focus)
1277 g_return_if_fail (GTK_IS_WINDOW (window));
1279 /* There are three cases: hidden, visible, visible on another
1283 if (!empathy_window_get_is_visible (window)) {
1284 /* Hide it so present brings it to the current workspace. */
1285 gtk_widget_hide (GTK_WIDGET (window));
1288 timestamp = gtk_get_current_event_time ();
1289 gtk_window_set_skip_taskbar_hint (window, FALSE);
1290 gtk_window_present_with_time (window, timestamp);
1291 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1292 * it deiconify automatically. */
1293 gtk_window_deiconify (window);
1297 empathy_get_toplevel_window (GtkWidget *widget)
1299 GtkWidget *toplevel;
1301 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1303 toplevel = gtk_widget_get_toplevel (widget);
1304 if (GTK_IS_WINDOW (toplevel) &&
1305 GTK_WIDGET_TOPLEVEL (toplevel)) {
1306 return GTK_WINDOW (toplevel);
1312 /* The URL opening code can't handle schemeless strings, so we try to be
1313 * smart and add http if there is no scheme or doesn't look like a mail
1314 * address. This should work in most cases, and let us click on strings
1315 * like "www.gnome.org".
1318 fixup_url (const gchar *url)
1320 if (g_str_has_prefix (url, "ghelp:") ||
1321 g_str_has_prefix (url, "mailto:") ||
1322 strstr (url, ":/")) {
1326 if (strstr (url, "@")) {
1327 return g_strdup_printf ("mailto:%s", url);
1330 return g_strdup_printf ("http://%s", url);
1334 empathy_url_show (const char *url)
1337 GError *error = NULL;
1339 real_url = fixup_url (url);
1344 /* FIXME: this does not work for multihead, we should use
1345 * GdkAppLaunchContext or gtk_show_url, see bug #514396.
1347 g_app_info_launch_default_for_uri (url, NULL, &error);
1351 dialog = gtk_message_dialog_new (NULL, 0,
1352 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1353 _("Unable to open URI"));
1354 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1355 "%s", error->message);
1357 g_signal_connect (dialog, "response",
1358 G_CALLBACK (gtk_widget_destroy),
1360 gtk_window_present (GTK_WINDOW (dialog));
1362 g_clear_error (&error);
1369 link_button_hook (GtkLinkButton *button,
1373 empathy_url_show (link);
1377 empathy_link_button_new (const gchar *url,
1380 static gboolean hook = FALSE;
1384 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1387 return gtk_link_button_new_with_label (url, title);
1391 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1396 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1398 g_signal_handlers_block_by_func (widget, callback, user_data);
1399 g_object_set (widget, "active", active, NULL);
1400 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1404 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1405 const gchar *tag_name,
1406 const gchar *first_property_name,
1409 GtkTextTagTable *table;
1412 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1413 g_return_val_if_fail (tag_name != NULL, NULL);
1415 table = gtk_text_buffer_get_tag_table (buffer);
1416 tag = gtk_text_tag_table_lookup (table, tag_name);
1419 tag = gtk_text_tag_new (tag_name);
1420 gtk_text_tag_table_add (table, tag);
1421 g_object_unref (tag);
1423 /* Clear the old values so that we don't affect the new theme. */
1425 "background-set", FALSE,
1426 "foreground-set", FALSE,
1427 "invisible-set", FALSE,
1428 "justification-set", FALSE,
1429 "paragraph-background-set", FALSE,
1430 "pixels-above-lines-set", FALSE,
1431 "pixels-below-lines-set", FALSE,
1436 "weight-set", FALSE,
1440 if (first_property_name) {
1443 va_start (list, first_property_name);
1444 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1451 /* Sending files with the file chooser */
1454 EmpathyContact *contact;
1455 EmpathyFileChooserCallback callback;
1457 } FileChooserResponseData;
1460 file_manager_send_file_response_cb (GtkDialog *widget,
1462 FileChooserResponseData *response_data)
1464 if (response_id == GTK_RESPONSE_OK) {
1467 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget));
1471 DEBUG ("File chooser selected files:");
1473 for (l = list; l; l = l->next) {
1476 EmpathyTpFile *tp_file;
1477 GtkRecentManager *manager;
1480 gfile = g_file_new_for_uri (uri);
1482 DEBUG ("\t%s", uri);
1483 tp_file = empathy_send_file (response_data->contact,
1485 if (response_data->callback)
1486 response_data->callback (tp_file,
1487 response_data->user_data);
1489 manager = gtk_recent_manager_get_default ();
1490 gtk_recent_manager_add_item (manager, uri);
1493 /* FIXME: This should be unrefed, but
1494 * it's not referenced anywhere else,
1495 * so the transfer just ends. Uncomment
1496 * this out when there is a file
1497 * transfer "manager".
1498 g_object_unref (file);
1500 g_object_unref (gfile);
1504 g_slist_free (list);
1507 g_object_unref (response_data->contact);
1508 g_free (response_data);
1509 gtk_widget_destroy (GTK_WIDGET (widget));
1513 empathy_send_file_with_file_chooser (EmpathyContact *contact,
1514 EmpathyFileChooserCallback callback,
1519 FileChooserResponseData *response_data;
1521 /* FIXME we cannot return the ft as the response is async, maybe we
1522 * should call a callback with the file when available */
1524 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1526 DEBUG ("Creating selection file chooser");
1528 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1530 GTK_FILE_CHOOSER_ACTION_OPEN,
1532 GTK_RESPONSE_CANCEL,
1536 button = gtk_button_new_with_mnemonic (_("_Send"));
1537 gtk_button_set_image (GTK_BUTTON (button),
1538 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1539 GTK_ICON_SIZE_BUTTON));
1540 gtk_widget_show (button);
1541 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1543 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1544 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1547 response_data = g_new0 (FileChooserResponseData, 1);
1548 response_data->contact = g_object_ref (contact);
1549 response_data->callback = callback;
1550 response_data->user_data = user_data;
1551 g_signal_connect (widget, "response",
1552 G_CALLBACK (file_manager_send_file_response_cb),
1555 gtk_widget_show (widget);
1559 add_file_to_manager (EmpathyTpFile *tp_file,
1560 EmpathyFTManager *ft_manager)
1562 empathy_ft_manager_add_tp_file (ft_manager, tp_file);
1566 empathy_send_file_with_file_chooser_and_manager (EmpathyContact *contact)
1568 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1570 empathy_send_file_with_file_chooser (contact,
1571 (EmpathyFileChooserCallback) add_file_to_manager,
1572 empathy_ft_manager_get_default ());