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>
48 #include <libempathy/empathy-utils.h>
53 gboolean preserve_aspect_ratio;
57 get_glade_file (const gchar *filename,
60 const gchar *first_required_widget,
65 GtkWidget **widget_ptr;
67 DEBUG ("Loading glade file %s", filename);
69 gui = glade_xml_new (filename, root, domain);
72 g_warning ("Couldn't find necessary glade file '%s'", filename);
75 for (name = first_required_widget; name; name = va_arg (args, char *)) {
76 widget_ptr = va_arg (args, void *);
78 *widget_ptr = glade_xml_get_widget (gui, name);
81 g_warning ("Glade file '%s' is missing widget '%s'.",
91 empathy_glade_get_file_simple (const gchar *filename,
94 const gchar *first_required_widget, ...)
99 va_start (args, first_required_widget);
101 gui = get_glade_file (filename,
104 first_required_widget,
110 g_object_unref (gui);
115 empathy_glade_get_file (const gchar *filename,
118 const gchar *first_required_widget, ...)
123 va_start (args, first_required_widget);
125 gui = get_glade_file (filename,
128 first_required_widget,
141 empathy_glade_connect (GladeXML *gui,
143 gchar *first_widget, ...)
151 va_start (args, first_widget);
153 for (name = first_widget; name; name = va_arg (args, char *)) {
154 signal = va_arg (args, void *);
155 callback = va_arg (args, void *);
157 widget = glade_xml_get_widget (gui, name);
159 g_warning ("Glade file is missing widget '%s', aborting",
164 g_signal_connect (widget,
166 G_CALLBACK (callback),
174 empathy_glade_setup_size_group (GladeXML *gui,
175 GtkSizeGroupMode mode,
176 gchar *first_widget, ...)
180 GtkSizeGroup *size_group;
183 va_start (args, first_widget);
185 size_group = gtk_size_group_new (mode);
187 for (name = first_widget; name; name = va_arg (args, char *)) {
188 widget = glade_xml_get_widget (gui, name);
190 g_warning ("Glade file is missing widget '%s'", name);
194 gtk_size_group_add_widget (size_group, widget);
197 g_object_unref (size_group);
203 empathy_icon_name_from_account (McAccount *account)
207 profile = mc_account_get_profile (account);
209 return mc_profile_get_icon_name (profile);
213 empathy_icon_name_for_presence (McPresence presence)
216 case MC_PRESENCE_AVAILABLE:
217 return EMPATHY_IMAGE_AVAILABLE;
218 case MC_PRESENCE_DO_NOT_DISTURB:
219 return EMPATHY_IMAGE_BUSY;
220 case MC_PRESENCE_AWAY:
221 return EMPATHY_IMAGE_AWAY;
222 case MC_PRESENCE_EXTENDED_AWAY:
223 return EMPATHY_IMAGE_EXT_AWAY;
224 case MC_PRESENCE_HIDDEN:
225 return EMPATHY_IMAGE_HIDDEN;
226 case MC_PRESENCE_OFFLINE:
227 case MC_PRESENCE_UNSET:
228 return EMPATHY_IMAGE_OFFLINE;
230 g_assert_not_reached ();
237 empathy_icon_name_for_contact (EmpathyContact *contact)
241 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
242 EMPATHY_IMAGE_OFFLINE);
244 presence = empathy_contact_get_presence (contact);
245 return empathy_icon_name_for_presence (presence);
249 empathy_pixbuf_from_data (gchar *data,
252 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
256 empathy_pixbuf_from_data_and_mime (gchar *data,
260 GdkPixbufLoader *loader;
261 GdkPixbufFormat *format;
262 GdkPixbuf *pixbuf = NULL;
264 GError *error = NULL;
270 loader = gdk_pixbuf_loader_new ();
271 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
272 DEBUG ("Failed to write to pixbuf loader: %s",
273 error ? error->message : "No error given");
276 if (!gdk_pixbuf_loader_close (loader, &error)) {
277 DEBUG ("Failed to close pixbuf loader: %s",
278 error ? error->message : "No error given");
282 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
284 g_object_ref (pixbuf);
286 if (mime_type != NULL) {
287 format = gdk_pixbuf_loader_get_format (loader);
288 mime_types = gdk_pixbuf_format_get_mime_types (format);
290 *mime_type = g_strdup (*mime_types);
291 if (mime_types[1] != NULL) {
292 DEBUG ("Loader supports more than one mime "
293 "type! Picking the first one, %s",
296 g_strfreev (mime_types);
301 g_clear_error (&error);
302 g_object_unref (loader);
308 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
311 struct SizeData *data)
313 g_return_if_fail (width > 0 && height > 0);
315 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
316 if (width < data->width && height < data->height) {
321 if (data->width < 0) {
322 width = width * (double) data->height / (gdouble) height;
323 height = data->height;
324 } else if (data->height < 0) {
325 height = height * (double) data->width / (double) width;
327 } else if ((double) height * (double) data->width >
328 (double) width * (double) data->height) {
329 width = 0.5 + (double) width * (double) data->height / (double) height;
330 height = data->height;
332 height = 0.5 + (double) height * (double) data->width / (double) width;
336 if (data->width > 0) {
340 if (data->height > 0) {
341 height = data->height;
345 gdk_pixbuf_loader_set_size (loader, width, height);
349 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
351 gint width, height, rowstride;
354 width = gdk_pixbuf_get_width (pixbuf);
355 height = gdk_pixbuf_get_height (pixbuf);
356 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
357 pixels = gdk_pixbuf_get_pixels (pixbuf);
359 if (width < 6 || height < 6) {
367 pixels[rowstride + 3] = 0x80;
368 pixels[rowstride * 2 + 3] = 0xC0;
371 pixels[width * 4 - 1] = 0;
372 pixels[width * 4 - 5] = 0x80;
373 pixels[width * 4 - 9] = 0xC0;
374 pixels[rowstride + (width * 4) - 1] = 0x80;
375 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
378 pixels[(height - 1) * rowstride + 3] = 0;
379 pixels[(height - 1) * rowstride + 7] = 0x80;
380 pixels[(height - 1) * rowstride + 11] = 0xC0;
381 pixels[(height - 2) * rowstride + 3] = 0x80;
382 pixels[(height - 3) * rowstride + 3] = 0xC0;
385 pixels[height * rowstride - 1] = 0;
386 pixels[(height - 1) * rowstride - 1] = 0x80;
387 pixels[(height - 2) * rowstride - 1] = 0xC0;
388 pixels[height * rowstride - 5] = 0x80;
389 pixels[height * rowstride - 9] = 0xC0;
393 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
395 gint width, height, rowstride, i;
399 width = gdk_pixbuf_get_width (pixbuf);
400 height = gdk_pixbuf_get_height (pixbuf);
401 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
402 pixels = gdk_pixbuf_get_pixels (pixbuf);
405 for (i = 3; i < rowstride; i+=4) {
411 for (i = 1; i < height - 1; i++) {
412 row = pixels + (i*rowstride);
413 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
418 row = pixels + ((height-1) * rowstride);
419 for (i = 3; i < rowstride; i+=4) {
429 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
434 GdkPixbufLoader *loader;
435 struct SizeData data;
436 GError *error = NULL;
443 data.height = height;
444 data.preserve_aspect_ratio = TRUE;
446 loader = gdk_pixbuf_loader_new ();
448 g_signal_connect (loader, "size-prepared",
449 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
452 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
453 g_warning ("Couldn't write avatar image:%p with "
454 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
455 avatar->data, avatar->len, error->message);
456 g_error_free (error);
460 gdk_pixbuf_loader_close (loader, NULL);
462 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
463 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
464 GdkPixbuf *rounded_pixbuf;
466 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
467 gdk_pixbuf_get_width (pixbuf),
468 gdk_pixbuf_get_height (pixbuf));
469 gdk_pixbuf_copy_area (pixbuf, 0, 0,
470 gdk_pixbuf_get_width (pixbuf),
471 gdk_pixbuf_get_height (pixbuf),
474 pixbuf = rounded_pixbuf;
476 g_object_ref (pixbuf);
479 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
480 empathy_avatar_pixbuf_roundify (pixbuf);
483 g_object_unref (loader);
489 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
493 EmpathyAvatar *avatar;
495 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
497 avatar = empathy_contact_get_avatar (contact);
499 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
503 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
508 width = gdk_pixbuf_get_width (pixbuf);
509 height = gdk_pixbuf_get_height (pixbuf);
511 if (width > 0 && (width > max_size || height > max_size)) {
512 factor = (gdouble) max_size / MAX (width, height);
514 width = width * factor;
515 height = height * factor;
517 return gdk_pixbuf_scale_simple (pixbuf,
522 return g_object_ref (pixbuf);
526 empathy_pixbuf_from_icon_name (const gchar *icon_name,
527 GtkIconSize icon_size)
530 GdkPixbuf *pixbuf = NULL;
531 GError *error = NULL;
539 theme = gtk_icon_theme_get_default ();
541 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
545 pixbuf = gtk_icon_theme_load_icon (theme,
551 DEBUG ("Error loading icon: %s", error->message);
552 g_clear_error (&error);
558 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
559 * that to make it easier to apply changes from the original code.
561 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
563 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
564 * decomposable character it consumes the decomposition length from the given
565 * offset. So it's useful when the offset was calculated for the normalized
566 * version of str, but we need a pointer to str itself. */
568 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
570 gchar *casefold, *normal;
576 q = g_utf8_next_char (p);
577 casefold = g_utf8_casefold (p, q - p);
578 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
579 offset -= g_utf8_strlen (normal, -1);
588 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
592 const gchar *ret = NULL;
595 gchar *caseless_haystack;
598 g_return_val_if_fail (haystack != NULL, NULL);
599 g_return_val_if_fail (needle != NULL, NULL);
601 casefold = g_utf8_casefold (haystack, -1);
602 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
605 needle_len = g_utf8_strlen (needle, -1);
606 haystack_len = g_utf8_strlen (caseless_haystack, -1);
610 ret = (gchar *)haystack;
614 if (haystack_len < needle_len)
620 p = (gchar*)caseless_haystack;
621 needle_len = strlen (needle);
626 if ((strncmp (p, needle, needle_len) == 0))
628 ret = pointer_from_offset_skipping_decomp (haystack, i);
632 p = g_utf8_next_char (p);
637 g_free (caseless_haystack);
643 g_utf8_caselessnmatch (const char *s1, const char *s2,
644 gssize n1, gssize n2)
647 gchar *normalized_s1;
648 gchar *normalized_s2;
651 gboolean ret = FALSE;
653 g_return_val_if_fail (s1 != NULL, FALSE);
654 g_return_val_if_fail (s2 != NULL, FALSE);
655 g_return_val_if_fail (n1 > 0, FALSE);
656 g_return_val_if_fail (n2 > 0, FALSE);
658 casefold = g_utf8_casefold (s1, n1);
659 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
662 casefold = g_utf8_casefold (s2, n2);
663 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
666 len_s1 = strlen (normalized_s1);
667 len_s2 = strlen (normalized_s2);
672 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
675 g_free (normalized_s1);
676 g_free (normalized_s2);
682 forward_chars_with_skipping (GtkTextIter *iter,
684 gboolean skip_invisible,
685 gboolean skip_nontext,
686 gboolean skip_decomp)
690 g_return_if_fail (count >= 0);
696 gboolean ignored = FALSE;
698 /* minimal workaround to avoid the infinite loop of bug #168247.
699 * It doesn't fix the problemjust the symptom...
701 if (gtk_text_iter_is_end (iter))
704 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
707 if (!ignored && skip_invisible &&
708 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
711 if (!ignored && skip_decomp)
713 /* being UTF8 correct sucks; this accounts for extra
714 offsets coming from canonical decompositions of
715 UTF8 characters (e.g. accented characters) which
716 g_utf8_normalize() performs */
721 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
722 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
723 i -= (g_utf8_strlen (normal, -1) - 1);
727 gtk_text_iter_forward_char (iter);
735 lines_match (const GtkTextIter *start,
737 gboolean visible_only,
739 GtkTextIter *match_start,
740 GtkTextIter *match_end)
747 if (*lines == NULL || **lines == '\0')
750 *match_start = *start;
757 gtk_text_iter_forward_line (&next);
759 /* No more text in buffer, but *lines is nonempty */
760 if (gtk_text_iter_equal (start, &next))
766 line_text = gtk_text_iter_get_visible_slice (start, &next);
768 line_text = gtk_text_iter_get_slice (start, &next);
773 line_text = gtk_text_iter_get_visible_text (start, &next);
775 line_text = gtk_text_iter_get_text (start, &next);
778 if (match_start) /* if this is the first line we're matching */
780 found = g_utf8_strcasestr (line_text, *lines);
784 /* If it's not the first line, we have to match from the
787 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
800 /* Get offset to start of search string */
801 offset = g_utf8_strlen (line_text, found - line_text);
805 /* If match start needs to be returned, set it to the
806 * start of the search string.
808 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
814 /* Go to end of search string */
815 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
824 /* pass NULL for match_start, since we don't need to find the
827 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
830 /* strsplit () that retains the delimiter as part of the string. */
832 strbreakup (const char *string,
833 const char *delimiter,
836 GSList *string_list = NULL, *slist;
837 gchar **str_array, *s, *casefold, *new_string;
840 g_return_val_if_fail (string != NULL, NULL);
841 g_return_val_if_fail (delimiter != NULL, NULL);
844 max_tokens = G_MAXINT;
846 s = strstr (string, delimiter);
849 guint delimiter_len = strlen (delimiter);
855 len = s - string + delimiter_len;
856 new_string = g_new (gchar, len + 1);
857 strncpy (new_string, string, len);
859 casefold = g_utf8_casefold (new_string, -1);
861 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
863 string_list = g_slist_prepend (string_list, new_string);
865 string = s + delimiter_len;
866 s = strstr (string, delimiter);
867 } while (--max_tokens && s);
873 casefold = g_utf8_casefold (string, -1);
874 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
876 string_list = g_slist_prepend (string_list, new_string);
879 str_array = g_new (gchar*, n);
883 str_array[i--] = NULL;
884 for (slist = string_list; slist; slist = slist->next)
885 str_array[i--] = slist->data;
887 g_slist_free (string_list);
893 empathy_text_iter_forward_search (const GtkTextIter *iter,
895 GtkTextIter *match_start,
896 GtkTextIter *match_end,
897 const GtkTextIter *limit)
899 gchar **lines = NULL;
901 gboolean retval = FALSE;
903 gboolean visible_only;
906 g_return_val_if_fail (iter != NULL, FALSE);
907 g_return_val_if_fail (str != NULL, FALSE);
909 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
913 /* If we can move one char, return the empty string there */
916 if (gtk_text_iter_forward_char (&match)) {
917 if (limit && gtk_text_iter_equal (&match, limit)) {
922 *match_start = match;
936 /* locate all lines */
937 lines = strbreakup (str, "\n", -1);
942 /* This loop has an inefficient worst-case, where
943 * gtk_text_iter_get_text () is called repeatedly on
948 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
952 if (lines_match (&search, (const gchar**)lines,
953 visible_only, slice, &match, &end)) {
955 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
959 *match_start = match;
967 } while (gtk_text_iter_forward_line (&search));
969 g_strfreev ((gchar**)lines);
975 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
979 const gchar *ret = NULL;
982 gchar *caseless_haystack;
985 g_return_val_if_fail (haystack != NULL, NULL);
986 g_return_val_if_fail (needle != NULL, NULL);
988 casefold = g_utf8_casefold (haystack, -1);
989 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
992 needle_len = g_utf8_strlen (needle, -1);
993 haystack_len = g_utf8_strlen (caseless_haystack, -1);
997 ret = (gchar *)haystack;
1001 if (haystack_len < needle_len)
1007 i = haystack_len - needle_len;
1008 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1009 needle_len = strlen (needle);
1011 while (p >= caseless_haystack)
1013 if (strncmp (p, needle, needle_len) == 0)
1015 ret = pointer_from_offset_skipping_decomp (haystack, i);
1019 p = g_utf8_prev_char (p);
1024 g_free (caseless_haystack);
1030 backward_lines_match (const GtkTextIter *start,
1031 const gchar **lines,
1032 gboolean visible_only,
1034 GtkTextIter *match_start,
1035 GtkTextIter *match_end)
1037 GtkTextIter line, next;
1042 if (*lines == NULL || **lines == '\0')
1045 *match_start = *start;
1047 *match_end = *start;
1051 line = next = *start;
1052 if (gtk_text_iter_get_line_offset (&next) == 0)
1054 if (!gtk_text_iter_backward_line (&next))
1058 gtk_text_iter_set_line_offset (&next, 0);
1063 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1065 line_text = gtk_text_iter_get_slice (&next, &line);
1070 line_text = gtk_text_iter_get_visible_text (&next, &line);
1072 line_text = gtk_text_iter_get_text (&next, &line);
1075 if (match_start) /* if this is the first line we're matching */
1077 found = g_utf8_strrcasestr (line_text, *lines);
1081 /* If it's not the first line, we have to match from the
1082 * start of the line.
1084 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1097 /* Get offset to start of search string */
1098 offset = g_utf8_strlen (line_text, found - line_text);
1100 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1102 /* If match start needs to be returned, set it to the
1103 * start of the search string.
1107 *match_start = next;
1110 /* Go to end of search string */
1111 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1120 /* try to match the rest of the lines forward, passing NULL
1121 * for match_start so lines_match will try to match the entire
1123 return lines_match (&next, lines, visible_only,
1124 slice, NULL, match_end);
1128 empathy_text_iter_backward_search (const GtkTextIter *iter,
1130 GtkTextIter *match_start,
1131 GtkTextIter *match_end,
1132 const GtkTextIter *limit)
1134 gchar **lines = NULL;
1136 gboolean retval = FALSE;
1138 gboolean visible_only;
1141 g_return_val_if_fail (iter != NULL, FALSE);
1142 g_return_val_if_fail (str != NULL, FALSE);
1144 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1149 /* If we can move one char, return the empty string there */
1152 if (gtk_text_iter_backward_char (&match))
1154 if (limit && gtk_text_iter_equal (&match, limit))
1158 *match_start = match;
1169 visible_only = TRUE;
1172 /* locate all lines */
1173 lines = strbreakup (str, "\n", -1);
1179 /* This loop has an inefficient worst-case, where
1180 * gtk_text_iter_get_text () is called repeatedly on
1185 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1188 if (backward_lines_match (&search, (const gchar**)lines,
1189 visible_only, slice, &match, &end))
1191 if (limit == NULL || (limit &&
1192 gtk_text_iter_compare (&end, limit) > 0))
1197 *match_start = match;
1204 if (gtk_text_iter_get_line_offset (&search) == 0)
1206 if (!gtk_text_iter_backward_line (&search))
1211 gtk_text_iter_set_line_offset (&search, 0);
1215 g_strfreev ((gchar**)lines);
1221 empathy_window_get_is_visible (GtkWindow *window)
1223 GdkWindowState state;
1224 GdkWindow *gdk_window;
1226 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1228 gdk_window = GTK_WIDGET (window)->window;
1233 state = gdk_window_get_state (gdk_window);
1234 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1242 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1244 GdkRectangle icon_location;
1247 GdkWindow *gdk_window;
1249 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1250 gdk_window = GTK_WIDGET (window)->window;
1251 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1253 data[0] = icon_location.x;
1254 data[1] = icon_location.y;
1255 data[2] = icon_location.width;
1256 data[3] = icon_location.height;
1258 XChangeProperty (dpy,
1259 GDK_WINDOW_XID (gdk_window),
1260 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1261 "_NET_WM_ICON_GEOMETRY"),
1262 XA_CARDINAL, 32, PropModeReplace,
1263 (guchar *)&data, 4);
1265 gtk_window_set_skip_taskbar_hint (window, TRUE);
1266 gtk_window_iconify (window);
1269 /* Takes care of moving the window to the current workspace. */
1271 empathy_window_present (GtkWindow *window,
1272 gboolean steal_focus)
1276 g_return_if_fail (GTK_IS_WINDOW (window));
1278 /* There are three cases: hidden, visible, visible on another
1282 if (!empathy_window_get_is_visible (window)) {
1283 /* Hide it so present brings it to the current workspace. */
1284 gtk_widget_hide (GTK_WIDGET (window));
1287 timestamp = gtk_get_current_event_time ();
1288 gtk_window_set_skip_taskbar_hint (window, FALSE);
1289 gtk_window_present_with_time (window, timestamp);
1290 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1291 * it deiconify automatically. */
1292 gtk_window_deiconify (window);
1296 empathy_get_toplevel_window (GtkWidget *widget)
1298 GtkWidget *toplevel;
1300 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1302 toplevel = gtk_widget_get_toplevel (widget);
1303 if (GTK_IS_WINDOW (toplevel) &&
1304 GTK_WIDGET_TOPLEVEL (toplevel)) {
1305 return GTK_WINDOW (toplevel);
1311 /* The URL opening code can't handle schemeless strings, so we try to be
1312 * smart and add http if there is no scheme or doesn't look like a mail
1313 * address. This should work in most cases, and let us click on strings
1314 * like "www.gnome.org".
1317 fixup_url (const gchar *url)
1319 if (g_str_has_prefix (url, "ghelp:") ||
1320 g_str_has_prefix (url, "mailto:") ||
1321 strstr (url, ":/")) {
1325 if (strstr (url, "@")) {
1326 return g_strdup_printf ("mailto:%s", url);
1329 return g_strdup_printf ("http://%s", url);
1333 empathy_url_show (const char *url)
1336 GError *error = NULL;
1338 real_url = fixup_url (url);
1343 /* FIXME: this does not work for multihead, we should use
1344 * GdkAppLaunchContext or gtk_show_url, see bug #514396.
1346 g_app_info_launch_default_for_uri (url, NULL, &error);
1350 dialog = gtk_message_dialog_new (NULL, 0,
1351 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1352 _("Unable to open URI"));
1353 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1354 "%s", error->message);
1356 g_signal_connect (dialog, "response",
1357 G_CALLBACK (gtk_widget_destroy),
1359 gtk_window_present (GTK_WINDOW (dialog));
1361 g_clear_error (&error);
1368 link_button_hook (GtkLinkButton *button,
1372 empathy_url_show (link);
1376 empathy_link_button_new (const gchar *url,
1379 static gboolean hook = FALSE;
1383 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1386 return gtk_link_button_new_with_label (url, title);
1390 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1395 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1397 g_signal_handlers_block_by_func (widget, callback, user_data);
1398 g_object_set (widget, "active", active, NULL);
1399 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1403 empathy_text_buffer_tag_set (GtkTextBuffer *buffer,
1404 const gchar *tag_name,
1405 const gchar *first_property_name,
1408 GtkTextTagTable *table;
1411 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1412 g_return_val_if_fail (tag_name != NULL, NULL);
1414 table = gtk_text_buffer_get_tag_table (buffer);
1415 tag = gtk_text_tag_table_lookup (table, tag_name);
1418 tag = gtk_text_tag_new (tag_name);
1419 gtk_text_tag_table_add (table, tag);
1420 g_object_unref (tag);
1422 /* Clear the old values so that we don't affect the new theme. */
1424 "background-set", FALSE,
1425 "foreground-set", FALSE,
1426 "invisible-set", FALSE,
1427 "justification-set", FALSE,
1428 "paragraph-background-set", FALSE,
1429 "pixels-above-lines-set", FALSE,
1430 "pixels-below-lines-set", FALSE,
1435 "weight-set", FALSE,
1439 if (first_property_name) {
1442 va_start (list, first_property_name);
1443 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1450 /* Sending files with the file chooser */
1453 EmpathyContact *contact;
1454 EmpathyFileChooserCallback callback;
1456 } FileChooserResponseData;
1459 file_manager_send_file_response_cb (GtkDialog *widget,
1461 FileChooserResponseData *response_data)
1463 if (response_id == GTK_RESPONSE_OK) {
1466 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget));
1471 DEBUG ("File chooser selected files:");
1473 for (l = list; l; l = l->next) {
1477 GtkRecentManager *manager;
1480 gfile = g_file_new_for_uri (uri);
1482 DEBUG ("\t%s", uri);
1483 file = empathy_send_file (response_data->contact,
1485 if (response_data->callback)
1486 response_data->callback (file,
1487 response_data->user_data);
1489 manager = gtk_recent_manager_get_default ();
1490 gtk_recent_manager_add_item (manager, uri);
1493 /* TODO: 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);
1506 DEBUG ("File chooser had no files selected");
1510 g_object_unref (response_data->contact);
1511 g_free (response_data);
1512 gtk_widget_destroy (GTK_WIDGET (widget));
1516 empathy_send_file_with_file_chooser (EmpathyContact *contact,
1517 EmpathyFileChooserCallback callback,
1522 GtkFileFilter *filter;
1523 FileChooserResponseData *response_data;
1525 /* FIXME we cannot return the ft as the response is async, maybe we
1526 * should call a callback with the file when available */
1528 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1530 DEBUG ("Creating selection file chooser");
1532 widget = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
1533 "action", GTK_FILE_CHOOSER_ACTION_OPEN,
1534 "select-multiple", FALSE,
1537 gtk_window_set_title (GTK_WINDOW (widget), _("Select a file"));
1540 gtk_dialog_add_button (GTK_DIALOG (widget),
1541 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
1544 button = gtk_button_new_with_mnemonic (_("_Send"));
1545 /* FIXME maybe we should use another icon */
1546 gtk_button_set_image (GTK_BUTTON (button),
1547 gtk_image_new_from_stock (GTK_STOCK_OPEN,
1548 GTK_ICON_SIZE_BUTTON));
1549 gtk_widget_show (button);
1550 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1552 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1553 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1556 response_data = g_new0 (FileChooserResponseData, 1);
1557 response_data->contact = g_object_ref (contact);
1558 response_data->callback = callback;
1559 response_data->user_data = user_data;
1560 g_signal_connect (widget, "response",
1561 G_CALLBACK (file_manager_send_file_response_cb),
1565 filter = gtk_file_filter_new ();
1566 gtk_file_filter_set_name (filter, "All Files");
1567 gtk_file_filter_add_pattern (filter, "*");
1568 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (widget), filter);
1570 gtk_widget_show (widget);
1574 add_file_to_manager (EmpathyFile *file,
1575 EmpathyFTManager *ft_manager)
1577 empathy_ft_manager_add_file (ft_manager, file);
1581 empathy_send_file_with_file_chooser_and_manager (EmpathyContact *contact)
1583 empathy_send_file_with_file_chooser (contact,
1584 (EmpathyFileChooserCallback) add_file_to_manager,
1585 empathy_ft_manager_get_default ());