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>
50 #include <libempathy/empathy-dispatcher.h>
55 gboolean preserve_aspect_ratio;
59 get_glade_file (const gchar *filename,
62 const gchar *first_required_widget,
67 GtkWidget **widget_ptr;
69 DEBUG ("Loading glade file %s", filename);
71 gui = glade_xml_new (filename, root, domain);
74 g_warning ("Couldn't find necessary glade file '%s'", filename);
77 for (name = first_required_widget; name; name = va_arg (args, char *)) {
78 widget_ptr = va_arg (args, void *);
80 *widget_ptr = glade_xml_get_widget (gui, name);
83 g_warning ("Glade file '%s' is missing widget '%s'.",
93 empathy_glade_get_file_simple (const gchar *filename,
96 const gchar *first_required_widget, ...)
101 va_start (args, first_required_widget);
103 gui = get_glade_file (filename,
106 first_required_widget,
112 g_object_unref (gui);
117 empathy_glade_get_file (const gchar *filename,
120 const gchar *first_required_widget, ...)
125 va_start (args, first_required_widget);
127 gui = get_glade_file (filename,
130 first_required_widget,
143 empathy_glade_connect (GladeXML *gui,
145 gchar *first_widget, ...)
153 va_start (args, first_widget);
155 for (name = first_widget; name; name = va_arg (args, char *)) {
156 signal = va_arg (args, void *);
157 callback = va_arg (args, void *);
159 widget = glade_xml_get_widget (gui, name);
161 g_warning ("Glade file is missing widget '%s', aborting",
166 g_signal_connect (widget,
168 G_CALLBACK (callback),
176 empathy_glade_setup_size_group (GladeXML *gui,
177 GtkSizeGroupMode mode,
178 gchar *first_widget, ...)
182 GtkSizeGroup *size_group;
185 va_start (args, first_widget);
187 size_group = gtk_size_group_new (mode);
189 for (name = first_widget; name; name = va_arg (args, char *)) {
190 widget = glade_xml_get_widget (gui, name);
192 g_warning ("Glade file is missing widget '%s'", name);
196 gtk_size_group_add_widget (size_group, widget);
199 g_object_unref (size_group);
205 empathy_icon_name_from_account (McAccount *account)
209 profile = mc_account_get_profile (account);
211 return mc_profile_get_icon_name (profile);
215 empathy_icon_name_for_presence (McPresence presence)
218 case MC_PRESENCE_AVAILABLE:
219 return EMPATHY_IMAGE_AVAILABLE;
220 case MC_PRESENCE_DO_NOT_DISTURB:
221 return EMPATHY_IMAGE_BUSY;
222 case MC_PRESENCE_AWAY:
223 return EMPATHY_IMAGE_AWAY;
224 case MC_PRESENCE_EXTENDED_AWAY:
225 return EMPATHY_IMAGE_EXT_AWAY;
226 case MC_PRESENCE_HIDDEN:
227 return EMPATHY_IMAGE_HIDDEN;
228 case MC_PRESENCE_OFFLINE:
229 case MC_PRESENCE_UNSET:
230 return EMPATHY_IMAGE_OFFLINE;
232 g_assert_not_reached ();
239 empathy_icon_name_for_contact (EmpathyContact *contact)
243 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
244 EMPATHY_IMAGE_OFFLINE);
246 presence = empathy_contact_get_presence (contact);
247 return empathy_icon_name_for_presence (presence);
251 empathy_pixbuf_from_data (gchar *data,
254 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
258 empathy_pixbuf_from_data_and_mime (gchar *data,
262 GdkPixbufLoader *loader;
263 GdkPixbufFormat *format;
264 GdkPixbuf *pixbuf = NULL;
266 GError *error = NULL;
272 loader = gdk_pixbuf_loader_new ();
273 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
274 DEBUG ("Failed to write to pixbuf loader: %s",
275 error ? error->message : "No error given");
278 if (!gdk_pixbuf_loader_close (loader, &error)) {
279 DEBUG ("Failed to close pixbuf loader: %s",
280 error ? error->message : "No error given");
284 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
286 g_object_ref (pixbuf);
288 if (mime_type != NULL) {
289 format = gdk_pixbuf_loader_get_format (loader);
290 mime_types = gdk_pixbuf_format_get_mime_types (format);
292 *mime_type = g_strdup (*mime_types);
293 if (mime_types[1] != NULL) {
294 DEBUG ("Loader supports more than one mime "
295 "type! Picking the first one, %s",
298 g_strfreev (mime_types);
303 g_clear_error (&error);
304 g_object_unref (loader);
310 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
313 struct SizeData *data)
315 g_return_if_fail (width > 0 && height > 0);
317 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
318 if (width < data->width && height < data->height) {
323 if (data->width < 0) {
324 width = width * (double) data->height / (gdouble) height;
325 height = data->height;
326 } else if (data->height < 0) {
327 height = height * (double) data->width / (double) width;
329 } else if ((double) height * (double) data->width >
330 (double) width * (double) data->height) {
331 width = 0.5 + (double) width * (double) data->height / (double) height;
332 height = data->height;
334 height = 0.5 + (double) height * (double) data->width / (double) width;
338 if (data->width > 0) {
342 if (data->height > 0) {
343 height = data->height;
347 gdk_pixbuf_loader_set_size (loader, width, height);
351 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
353 gint width, height, rowstride;
356 width = gdk_pixbuf_get_width (pixbuf);
357 height = gdk_pixbuf_get_height (pixbuf);
358 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
359 pixels = gdk_pixbuf_get_pixels (pixbuf);
361 if (width < 6 || height < 6) {
369 pixels[rowstride + 3] = 0x80;
370 pixels[rowstride * 2 + 3] = 0xC0;
373 pixels[width * 4 - 1] = 0;
374 pixels[width * 4 - 5] = 0x80;
375 pixels[width * 4 - 9] = 0xC0;
376 pixels[rowstride + (width * 4) - 1] = 0x80;
377 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
380 pixels[(height - 1) * rowstride + 3] = 0;
381 pixels[(height - 1) * rowstride + 7] = 0x80;
382 pixels[(height - 1) * rowstride + 11] = 0xC0;
383 pixels[(height - 2) * rowstride + 3] = 0x80;
384 pixels[(height - 3) * rowstride + 3] = 0xC0;
387 pixels[height * rowstride - 1] = 0;
388 pixels[(height - 1) * rowstride - 1] = 0x80;
389 pixels[(height - 2) * rowstride - 1] = 0xC0;
390 pixels[height * rowstride - 5] = 0x80;
391 pixels[height * rowstride - 9] = 0xC0;
395 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
397 gint width, height, rowstride, i;
401 width = gdk_pixbuf_get_width (pixbuf);
402 height = gdk_pixbuf_get_height (pixbuf);
403 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
404 pixels = gdk_pixbuf_get_pixels (pixbuf);
407 for (i = 3; i < rowstride; i+=4) {
413 for (i = 1; i < height - 1; i++) {
414 row = pixels + (i*rowstride);
415 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
420 row = pixels + ((height-1) * rowstride);
421 for (i = 3; i < rowstride; i+=4) {
431 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
436 GdkPixbufLoader *loader;
437 struct SizeData data;
438 GError *error = NULL;
445 data.height = height;
446 data.preserve_aspect_ratio = TRUE;
448 loader = gdk_pixbuf_loader_new ();
450 g_signal_connect (loader, "size-prepared",
451 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
454 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
455 g_warning ("Couldn't write avatar image:%p with "
456 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
457 avatar->data, avatar->len, error->message);
458 g_error_free (error);
462 gdk_pixbuf_loader_close (loader, NULL);
464 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
465 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
466 GdkPixbuf *rounded_pixbuf;
468 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
469 gdk_pixbuf_get_width (pixbuf),
470 gdk_pixbuf_get_height (pixbuf));
471 gdk_pixbuf_copy_area (pixbuf, 0, 0,
472 gdk_pixbuf_get_width (pixbuf),
473 gdk_pixbuf_get_height (pixbuf),
476 pixbuf = rounded_pixbuf;
478 g_object_ref (pixbuf);
481 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
482 empathy_avatar_pixbuf_roundify (pixbuf);
485 g_object_unref (loader);
491 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
495 EmpathyAvatar *avatar;
497 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
499 avatar = empathy_contact_get_avatar (contact);
501 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
505 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
510 width = gdk_pixbuf_get_width (pixbuf);
511 height = gdk_pixbuf_get_height (pixbuf);
513 if (width > 0 && (width > max_size || height > max_size)) {
514 factor = (gdouble) max_size / MAX (width, height);
516 width = width * factor;
517 height = height * factor;
519 return gdk_pixbuf_scale_simple (pixbuf,
524 return g_object_ref (pixbuf);
528 empathy_pixbuf_from_icon_name (const gchar *icon_name,
529 GtkIconSize icon_size)
532 GdkPixbuf *pixbuf = NULL;
533 GError *error = NULL;
541 theme = gtk_icon_theme_get_default ();
543 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
547 pixbuf = gtk_icon_theme_load_icon (theme,
553 DEBUG ("Error loading icon: %s", error->message);
554 g_clear_error (&error);
560 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
561 * that to make it easier to apply changes from the original code.
563 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
565 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
566 * decomposable character it consumes the decomposition length from the given
567 * offset. So it's useful when the offset was calculated for the normalized
568 * version of str, but we need a pointer to str itself. */
570 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
572 gchar *casefold, *normal;
578 q = g_utf8_next_char (p);
579 casefold = g_utf8_casefold (p, q - p);
580 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
581 offset -= g_utf8_strlen (normal, -1);
590 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
594 const gchar *ret = NULL;
597 gchar *caseless_haystack;
600 g_return_val_if_fail (haystack != NULL, NULL);
601 g_return_val_if_fail (needle != NULL, NULL);
603 casefold = g_utf8_casefold (haystack, -1);
604 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
607 needle_len = g_utf8_strlen (needle, -1);
608 haystack_len = g_utf8_strlen (caseless_haystack, -1);
612 ret = (gchar *)haystack;
616 if (haystack_len < needle_len)
622 p = (gchar*)caseless_haystack;
623 needle_len = strlen (needle);
628 if ((strncmp (p, needle, needle_len) == 0))
630 ret = pointer_from_offset_skipping_decomp (haystack, i);
634 p = g_utf8_next_char (p);
639 g_free (caseless_haystack);
645 g_utf8_caselessnmatch (const char *s1, const char *s2,
646 gssize n1, gssize n2)
649 gchar *normalized_s1;
650 gchar *normalized_s2;
653 gboolean ret = FALSE;
655 g_return_val_if_fail (s1 != NULL, FALSE);
656 g_return_val_if_fail (s2 != NULL, FALSE);
657 g_return_val_if_fail (n1 > 0, FALSE);
658 g_return_val_if_fail (n2 > 0, FALSE);
660 casefold = g_utf8_casefold (s1, n1);
661 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
664 casefold = g_utf8_casefold (s2, n2);
665 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
668 len_s1 = strlen (normalized_s1);
669 len_s2 = strlen (normalized_s2);
674 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
677 g_free (normalized_s1);
678 g_free (normalized_s2);
684 forward_chars_with_skipping (GtkTextIter *iter,
686 gboolean skip_invisible,
687 gboolean skip_nontext,
688 gboolean skip_decomp)
692 g_return_if_fail (count >= 0);
698 gboolean ignored = FALSE;
700 /* minimal workaround to avoid the infinite loop of bug #168247.
701 * It doesn't fix the problemjust the symptom...
703 if (gtk_text_iter_is_end (iter))
706 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
709 if (!ignored && skip_invisible &&
710 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
713 if (!ignored && skip_decomp)
715 /* being UTF8 correct sucks; this accounts for extra
716 offsets coming from canonical decompositions of
717 UTF8 characters (e.g. accented characters) which
718 g_utf8_normalize() performs */
723 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
724 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
725 i -= (g_utf8_strlen (normal, -1) - 1);
729 gtk_text_iter_forward_char (iter);
737 lines_match (const GtkTextIter *start,
739 gboolean visible_only,
741 GtkTextIter *match_start,
742 GtkTextIter *match_end)
749 if (*lines == NULL || **lines == '\0')
752 *match_start = *start;
759 gtk_text_iter_forward_line (&next);
761 /* No more text in buffer, but *lines is nonempty */
762 if (gtk_text_iter_equal (start, &next))
768 line_text = gtk_text_iter_get_visible_slice (start, &next);
770 line_text = gtk_text_iter_get_slice (start, &next);
775 line_text = gtk_text_iter_get_visible_text (start, &next);
777 line_text = gtk_text_iter_get_text (start, &next);
780 if (match_start) /* if this is the first line we're matching */
782 found = g_utf8_strcasestr (line_text, *lines);
786 /* If it's not the first line, we have to match from the
789 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
802 /* Get offset to start of search string */
803 offset = g_utf8_strlen (line_text, found - line_text);
807 /* If match start needs to be returned, set it to the
808 * start of the search string.
810 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
816 /* Go to end of search string */
817 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
826 /* pass NULL for match_start, since we don't need to find the
829 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
832 /* strsplit () that retains the delimiter as part of the string. */
834 strbreakup (const char *string,
835 const char *delimiter,
838 GSList *string_list = NULL, *slist;
839 gchar **str_array, *s, *casefold, *new_string;
842 g_return_val_if_fail (string != NULL, NULL);
843 g_return_val_if_fail (delimiter != NULL, NULL);
846 max_tokens = G_MAXINT;
848 s = strstr (string, delimiter);
851 guint delimiter_len = strlen (delimiter);
857 len = s - string + delimiter_len;
858 new_string = g_new (gchar, len + 1);
859 strncpy (new_string, string, len);
861 casefold = g_utf8_casefold (new_string, -1);
863 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
865 string_list = g_slist_prepend (string_list, new_string);
867 string = s + delimiter_len;
868 s = strstr (string, delimiter);
869 } while (--max_tokens && s);
875 casefold = g_utf8_casefold (string, -1);
876 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
878 string_list = g_slist_prepend (string_list, new_string);
881 str_array = g_new (gchar*, n);
885 str_array[i--] = NULL;
886 for (slist = string_list; slist; slist = slist->next)
887 str_array[i--] = slist->data;
889 g_slist_free (string_list);
895 empathy_text_iter_forward_search (const GtkTextIter *iter,
897 GtkTextIter *match_start,
898 GtkTextIter *match_end,
899 const GtkTextIter *limit)
901 gchar **lines = NULL;
903 gboolean retval = FALSE;
905 gboolean visible_only;
908 g_return_val_if_fail (iter != NULL, FALSE);
909 g_return_val_if_fail (str != NULL, FALSE);
911 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
915 /* If we can move one char, return the empty string there */
918 if (gtk_text_iter_forward_char (&match)) {
919 if (limit && gtk_text_iter_equal (&match, limit)) {
924 *match_start = match;
938 /* locate all lines */
939 lines = strbreakup (str, "\n", -1);
944 /* This loop has an inefficient worst-case, where
945 * gtk_text_iter_get_text () is called repeatedly on
950 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
954 if (lines_match (&search, (const gchar**)lines,
955 visible_only, slice, &match, &end)) {
957 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
961 *match_start = match;
969 } while (gtk_text_iter_forward_line (&search));
971 g_strfreev ((gchar**)lines);
977 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
981 const gchar *ret = NULL;
984 gchar *caseless_haystack;
987 g_return_val_if_fail (haystack != NULL, NULL);
988 g_return_val_if_fail (needle != NULL, NULL);
990 casefold = g_utf8_casefold (haystack, -1);
991 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
994 needle_len = g_utf8_strlen (needle, -1);
995 haystack_len = g_utf8_strlen (caseless_haystack, -1);
999 ret = (gchar *)haystack;
1003 if (haystack_len < needle_len)
1009 i = haystack_len - needle_len;
1010 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1011 needle_len = strlen (needle);
1013 while (p >= caseless_haystack)
1015 if (strncmp (p, needle, needle_len) == 0)
1017 ret = pointer_from_offset_skipping_decomp (haystack, i);
1021 p = g_utf8_prev_char (p);
1026 g_free (caseless_haystack);
1032 backward_lines_match (const GtkTextIter *start,
1033 const gchar **lines,
1034 gboolean visible_only,
1036 GtkTextIter *match_start,
1037 GtkTextIter *match_end)
1039 GtkTextIter line, next;
1044 if (*lines == NULL || **lines == '\0')
1047 *match_start = *start;
1049 *match_end = *start;
1053 line = next = *start;
1054 if (gtk_text_iter_get_line_offset (&next) == 0)
1056 if (!gtk_text_iter_backward_line (&next))
1060 gtk_text_iter_set_line_offset (&next, 0);
1065 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1067 line_text = gtk_text_iter_get_slice (&next, &line);
1072 line_text = gtk_text_iter_get_visible_text (&next, &line);
1074 line_text = gtk_text_iter_get_text (&next, &line);
1077 if (match_start) /* if this is the first line we're matching */
1079 found = g_utf8_strrcasestr (line_text, *lines);
1083 /* If it's not the first line, we have to match from the
1084 * start of the line.
1086 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1099 /* Get offset to start of search string */
1100 offset = g_utf8_strlen (line_text, found - line_text);
1102 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1104 /* If match start needs to be returned, set it to the
1105 * start of the search string.
1109 *match_start = next;
1112 /* Go to end of search string */
1113 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1122 /* try to match the rest of the lines forward, passing NULL
1123 * for match_start so lines_match will try to match the entire
1125 return lines_match (&next, lines, visible_only,
1126 slice, NULL, match_end);
1130 empathy_text_iter_backward_search (const GtkTextIter *iter,
1132 GtkTextIter *match_start,
1133 GtkTextIter *match_end,
1134 const GtkTextIter *limit)
1136 gchar **lines = NULL;
1138 gboolean retval = FALSE;
1140 gboolean visible_only;
1143 g_return_val_if_fail (iter != NULL, FALSE);
1144 g_return_val_if_fail (str != NULL, FALSE);
1146 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1151 /* If we can move one char, return the empty string there */
1154 if (gtk_text_iter_backward_char (&match))
1156 if (limit && gtk_text_iter_equal (&match, limit))
1160 *match_start = match;
1171 visible_only = TRUE;
1174 /* locate all lines */
1175 lines = strbreakup (str, "\n", -1);
1181 /* This loop has an inefficient worst-case, where
1182 * gtk_text_iter_get_text () is called repeatedly on
1187 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1190 if (backward_lines_match (&search, (const gchar**)lines,
1191 visible_only, slice, &match, &end))
1193 if (limit == NULL || (limit &&
1194 gtk_text_iter_compare (&end, limit) > 0))
1199 *match_start = match;
1206 if (gtk_text_iter_get_line_offset (&search) == 0)
1208 if (!gtk_text_iter_backward_line (&search))
1213 gtk_text_iter_set_line_offset (&search, 0);
1217 g_strfreev ((gchar**)lines);
1223 empathy_window_get_is_visible (GtkWindow *window)
1225 GdkWindowState state;
1226 GdkWindow *gdk_window;
1228 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1230 gdk_window = GTK_WIDGET (window)->window;
1235 state = gdk_window_get_state (gdk_window);
1236 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1244 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1246 GdkRectangle icon_location;
1249 GdkWindow *gdk_window;
1251 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1252 gdk_window = GTK_WIDGET (window)->window;
1253 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1255 data[0] = icon_location.x;
1256 data[1] = icon_location.y;
1257 data[2] = icon_location.width;
1258 data[3] = icon_location.height;
1260 XChangeProperty (dpy,
1261 GDK_WINDOW_XID (gdk_window),
1262 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1263 "_NET_WM_ICON_GEOMETRY"),
1264 XA_CARDINAL, 32, PropModeReplace,
1265 (guchar *)&data, 4);
1267 gtk_window_set_skip_taskbar_hint (window, TRUE);
1268 gtk_window_iconify (window);
1271 /* Takes care of moving the window to the current workspace. */
1273 empathy_window_present (GtkWindow *window,
1274 gboolean steal_focus)
1278 g_return_if_fail (GTK_IS_WINDOW (window));
1280 /* There are three cases: hidden, visible, visible on another
1284 if (!empathy_window_get_is_visible (window)) {
1285 /* Hide it so present brings it to the current workspace. */
1286 gtk_widget_hide (GTK_WIDGET (window));
1289 timestamp = gtk_get_current_event_time ();
1290 gtk_window_set_skip_taskbar_hint (window, FALSE);
1291 gtk_window_present_with_time (window, timestamp);
1292 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1293 * it deiconify automatically. */
1294 gtk_window_deiconify (window);
1298 empathy_get_toplevel_window (GtkWidget *widget)
1300 GtkWidget *toplevel;
1302 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1304 toplevel = gtk_widget_get_toplevel (widget);
1305 if (GTK_IS_WINDOW (toplevel) &&
1306 GTK_WIDGET_TOPLEVEL (toplevel)) {
1307 return GTK_WINDOW (toplevel);
1313 /* The URL opening code can't handle schemeless strings, so we try to be
1314 * smart and add http if there is no scheme or doesn't look like a mail
1315 * address. This should work in most cases, and let us click on strings
1316 * like "www.gnome.org".
1319 fixup_url (const gchar *url)
1321 if (g_str_has_prefix (url, "ghelp:") ||
1322 g_str_has_prefix (url, "mailto:") ||
1323 strstr (url, ":/")) {
1327 if (strstr (url, "@")) {
1328 return g_strdup_printf ("mailto:%s", url);
1331 return g_strdup_printf ("http://%s", url);
1335 empathy_url_show (const char *url)
1338 GError *error = NULL;
1340 real_url = fixup_url (url);
1345 /* FIXME: this does not work for multihead, we should use
1346 * GdkAppLaunchContext or gtk_show_url, see bug #514396.
1348 g_app_info_launch_default_for_uri (url, NULL, &error);
1352 dialog = gtk_message_dialog_new (NULL, 0,
1353 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1354 _("Unable to open URI"));
1355 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1356 "%s", error->message);
1358 g_signal_connect (dialog, "response",
1359 G_CALLBACK (gtk_widget_destroy),
1361 gtk_window_present (GTK_WINDOW (dialog));
1363 g_clear_error (&error);
1370 link_button_hook (GtkLinkButton *button,
1374 empathy_url_show (link);
1378 empathy_link_button_new (const gchar *url,
1381 static gboolean hook = FALSE;
1385 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1388 return gtk_link_button_new_with_label (url, title);
1392 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1397 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1399 g_signal_handlers_block_by_func (widget, callback, user_data);
1400 g_object_set (widget, "active", active, NULL);
1401 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1405 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1406 const gchar *tag_name,
1407 const gchar *first_property_name,
1410 GtkTextTagTable *table;
1413 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1414 g_return_val_if_fail (tag_name != NULL, NULL);
1416 table = gtk_text_buffer_get_tag_table (buffer);
1417 tag = gtk_text_tag_table_lookup (table, tag_name);
1420 tag = gtk_text_tag_new (tag_name);
1421 gtk_text_tag_table_add (table, tag);
1422 g_object_unref (tag);
1424 /* Clear the old values so that we don't affect the new theme. */
1426 "background-set", FALSE,
1427 "foreground-set", FALSE,
1428 "invisible-set", FALSE,
1429 "justification-set", FALSE,
1430 "paragraph-background-set", FALSE,
1431 "pixels-above-lines-set", FALSE,
1432 "pixels-below-lines-set", FALSE,
1437 "weight-set", FALSE,
1441 if (first_property_name) {
1444 va_start (list, first_property_name);
1445 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1452 /* Sending files with the file chooser */
1455 file_manager_send_file_response_cb (GtkDialog *widget,
1457 EmpathyContact *contact)
1459 if (response_id == GTK_RESPONSE_OK) {
1463 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget));
1465 DEBUG ("File chooser selected files:");
1467 for (l = list; l; l = l->next) {
1470 GtkRecentManager *manager;
1473 gfile = g_file_new_for_uri (uri);
1475 DEBUG ("\t%s", uri);
1476 empathy_dispatcher_send_file (contact, gfile);
1478 manager = gtk_recent_manager_get_default ();
1479 gtk_recent_manager_add_item (manager, uri);
1481 g_object_unref (gfile);
1485 g_slist_free (list);
1488 gtk_widget_destroy (GTK_WIDGET (widget));
1492 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1497 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1499 DEBUG ("Creating selection file chooser");
1501 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1503 GTK_FILE_CHOOSER_ACTION_OPEN,
1505 GTK_RESPONSE_CANCEL,
1509 button = gtk_button_new_with_mnemonic (_("_Send"));
1510 gtk_button_set_image (GTK_BUTTON (button),
1511 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1512 GTK_ICON_SIZE_BUTTON));
1513 gtk_widget_show (button);
1514 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1516 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1517 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1520 g_signal_connect (widget, "response",
1521 G_CALLBACK (file_manager_send_file_response_cb),
1524 gtk_widget_show (widget);