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-lib.h>
40 #include <glade/glade.h>
41 #include <canberra-gtk.h>
43 #include <libmissioncontrol/mc-profile.h>
45 #include "empathy-ui-utils.h"
46 #include "empathy-images.h"
47 #include "empathy-conf.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
50 #include <libempathy/empathy-debug.h>
51 #include <libempathy/empathy-utils.h>
52 #include <libempathy/empathy-dispatcher.h>
53 #include <libempathy/empathy-idle.h>
54 #include <libempathy/empathy-tp-file.h>
57 empathy_gtk_init (void)
59 static gboolean initialized = FALSE;
65 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
66 PKGDATADIR G_DIR_SEPARATOR_S "icons");
74 gboolean preserve_aspect_ratio;
78 get_glade_file (const gchar *filename,
81 const gchar *first_required_widget,
86 GtkWidget **widget_ptr;
88 DEBUG ("Loading glade file %s", filename);
90 gui = glade_xml_new (filename, root, domain);
93 g_warning ("Couldn't find necessary glade file '%s'", filename);
96 for (name = first_required_widget; name; name = va_arg (args, char *)) {
97 widget_ptr = va_arg (args, void *);
99 *widget_ptr = glade_xml_get_widget (gui, name);
102 g_warning ("Glade file '%s' is missing widget '%s'.",
112 empathy_glade_get_file_simple (const gchar *filename,
115 const gchar *first_required_widget, ...)
120 va_start (args, first_required_widget);
122 gui = get_glade_file (filename,
125 first_required_widget,
131 g_object_unref (gui);
136 empathy_glade_get_file (const gchar *filename,
139 const gchar *first_required_widget, ...)
144 va_start (args, first_required_widget);
146 gui = get_glade_file (filename,
149 first_required_widget,
162 empathy_glade_connect (GladeXML *gui,
164 gchar *first_widget, ...)
172 va_start (args, first_widget);
174 for (name = first_widget; name; name = va_arg (args, char *)) {
175 signal = va_arg (args, void *);
176 callback = va_arg (args, void *);
178 widget = glade_xml_get_widget (gui, name);
180 g_warning ("Glade file is missing widget '%s', aborting",
185 g_signal_connect (widget,
187 G_CALLBACK (callback),
195 empathy_glade_setup_size_group (GladeXML *gui,
196 GtkSizeGroupMode mode,
197 gchar *first_widget, ...)
201 GtkSizeGroup *size_group;
204 va_start (args, first_widget);
206 size_group = gtk_size_group_new (mode);
208 for (name = first_widget; name; name = va_arg (args, char *)) {
209 widget = glade_xml_get_widget (gui, name);
211 g_warning ("Glade file is missing widget '%s'", name);
215 gtk_size_group_add_widget (size_group, widget);
218 g_object_unref (size_group);
224 empathy_icon_name_from_account (McAccount *account)
228 profile = mc_account_get_profile (account);
230 return mc_profile_get_icon_name (profile);
234 empathy_icon_name_for_presence (McPresence presence)
237 case MC_PRESENCE_AVAILABLE:
238 return EMPATHY_IMAGE_AVAILABLE;
239 case MC_PRESENCE_DO_NOT_DISTURB:
240 return EMPATHY_IMAGE_BUSY;
241 case MC_PRESENCE_AWAY:
242 return EMPATHY_IMAGE_AWAY;
243 case MC_PRESENCE_EXTENDED_AWAY:
244 return EMPATHY_IMAGE_EXT_AWAY;
245 case MC_PRESENCE_HIDDEN:
246 return EMPATHY_IMAGE_HIDDEN;
247 case MC_PRESENCE_OFFLINE:
248 case MC_PRESENCE_UNSET:
249 return EMPATHY_IMAGE_OFFLINE;
251 g_assert_not_reached ();
258 empathy_icon_name_for_contact (EmpathyContact *contact)
262 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
263 EMPATHY_IMAGE_OFFLINE);
265 presence = empathy_contact_get_presence (contact);
266 return empathy_icon_name_for_presence (presence);
270 empathy_pixbuf_from_data (gchar *data,
273 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
277 empathy_pixbuf_from_data_and_mime (gchar *data,
281 GdkPixbufLoader *loader;
282 GdkPixbufFormat *format;
283 GdkPixbuf *pixbuf = NULL;
285 GError *error = NULL;
291 loader = gdk_pixbuf_loader_new ();
292 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
293 DEBUG ("Failed to write to pixbuf loader: %s",
294 error ? error->message : "No error given");
297 if (!gdk_pixbuf_loader_close (loader, &error)) {
298 DEBUG ("Failed to close pixbuf loader: %s",
299 error ? error->message : "No error given");
303 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
305 g_object_ref (pixbuf);
307 if (mime_type != NULL) {
308 format = gdk_pixbuf_loader_get_format (loader);
309 mime_types = gdk_pixbuf_format_get_mime_types (format);
311 *mime_type = g_strdup (*mime_types);
312 if (mime_types[1] != NULL) {
313 DEBUG ("Loader supports more than one mime "
314 "type! Picking the first one, %s",
317 g_strfreev (mime_types);
322 g_clear_error (&error);
323 g_object_unref (loader);
329 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
332 struct SizeData *data)
334 g_return_if_fail (width > 0 && height > 0);
336 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
337 if (width < data->width && height < data->height) {
342 if (data->width < 0) {
343 width = width * (double) data->height / (gdouble) height;
344 height = data->height;
345 } else if (data->height < 0) {
346 height = height * (double) data->width / (double) width;
348 } else if ((double) height * (double) data->width >
349 (double) width * (double) data->height) {
350 width = 0.5 + (double) width * (double) data->height / (double) height;
351 height = data->height;
353 height = 0.5 + (double) height * (double) data->width / (double) width;
357 if (data->width > 0) {
361 if (data->height > 0) {
362 height = data->height;
366 gdk_pixbuf_loader_set_size (loader, width, height);
370 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
372 gint width, height, rowstride;
375 width = gdk_pixbuf_get_width (pixbuf);
376 height = gdk_pixbuf_get_height (pixbuf);
377 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
378 pixels = gdk_pixbuf_get_pixels (pixbuf);
380 if (width < 6 || height < 6) {
388 pixels[rowstride + 3] = 0x80;
389 pixels[rowstride * 2 + 3] = 0xC0;
392 pixels[width * 4 - 1] = 0;
393 pixels[width * 4 - 5] = 0x80;
394 pixels[width * 4 - 9] = 0xC0;
395 pixels[rowstride + (width * 4) - 1] = 0x80;
396 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
399 pixels[(height - 1) * rowstride + 3] = 0;
400 pixels[(height - 1) * rowstride + 7] = 0x80;
401 pixels[(height - 1) * rowstride + 11] = 0xC0;
402 pixels[(height - 2) * rowstride + 3] = 0x80;
403 pixels[(height - 3) * rowstride + 3] = 0xC0;
406 pixels[height * rowstride - 1] = 0;
407 pixels[(height - 1) * rowstride - 1] = 0x80;
408 pixels[(height - 2) * rowstride - 1] = 0xC0;
409 pixels[height * rowstride - 5] = 0x80;
410 pixels[height * rowstride - 9] = 0xC0;
414 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
416 gint width, height, rowstride, i;
420 width = gdk_pixbuf_get_width (pixbuf);
421 height = gdk_pixbuf_get_height (pixbuf);
422 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
423 pixels = gdk_pixbuf_get_pixels (pixbuf);
426 for (i = 3; i < rowstride; i+=4) {
432 for (i = 1; i < height - 1; i++) {
433 row = pixels + (i*rowstride);
434 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
439 row = pixels + ((height-1) * rowstride);
440 for (i = 3; i < rowstride; i+=4) {
450 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
455 GdkPixbufLoader *loader;
456 struct SizeData data;
457 GError *error = NULL;
464 data.height = height;
465 data.preserve_aspect_ratio = TRUE;
467 loader = gdk_pixbuf_loader_new ();
469 g_signal_connect (loader, "size-prepared",
470 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
473 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
474 g_warning ("Couldn't write avatar image:%p with "
475 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
476 avatar->data, avatar->len, error->message);
477 g_error_free (error);
481 gdk_pixbuf_loader_close (loader, NULL);
483 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
484 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
485 GdkPixbuf *rounded_pixbuf;
487 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
488 gdk_pixbuf_get_width (pixbuf),
489 gdk_pixbuf_get_height (pixbuf));
490 gdk_pixbuf_copy_area (pixbuf, 0, 0,
491 gdk_pixbuf_get_width (pixbuf),
492 gdk_pixbuf_get_height (pixbuf),
495 pixbuf = rounded_pixbuf;
497 g_object_ref (pixbuf);
500 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
501 empathy_avatar_pixbuf_roundify (pixbuf);
504 g_object_unref (loader);
510 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
514 EmpathyAvatar *avatar;
516 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
518 avatar = empathy_contact_get_avatar (contact);
520 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
524 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
529 width = gdk_pixbuf_get_width (pixbuf);
530 height = gdk_pixbuf_get_height (pixbuf);
532 if (width > 0 && (width > max_size || height > max_size)) {
533 factor = (gdouble) max_size / MAX (width, height);
535 width = width * factor;
536 height = height * factor;
538 return gdk_pixbuf_scale_simple (pixbuf,
543 return g_object_ref (pixbuf);
547 empathy_pixbuf_from_icon_name (const gchar *icon_name,
548 GtkIconSize icon_size)
551 GdkPixbuf *pixbuf = NULL;
552 GError *error = NULL;
560 theme = gtk_icon_theme_get_default ();
562 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
566 pixbuf = gtk_icon_theme_load_icon (theme,
572 DEBUG ("Error loading icon: %s", error->message);
573 g_clear_error (&error);
579 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
580 * that to make it easier to apply changes from the original code.
582 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
584 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
585 * decomposable character it consumes the decomposition length from the given
586 * offset. So it's useful when the offset was calculated for the normalized
587 * version of str, but we need a pointer to str itself. */
589 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
591 gchar *casefold, *normal;
597 q = g_utf8_next_char (p);
598 casefold = g_utf8_casefold (p, q - p);
599 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
600 offset -= g_utf8_strlen (normal, -1);
609 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
613 const gchar *ret = NULL;
616 gchar *caseless_haystack;
619 g_return_val_if_fail (haystack != NULL, NULL);
620 g_return_val_if_fail (needle != NULL, NULL);
622 casefold = g_utf8_casefold (haystack, -1);
623 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
626 needle_len = g_utf8_strlen (needle, -1);
627 haystack_len = g_utf8_strlen (caseless_haystack, -1);
631 ret = (gchar *)haystack;
635 if (haystack_len < needle_len)
641 p = (gchar*)caseless_haystack;
642 needle_len = strlen (needle);
647 if ((strncmp (p, needle, needle_len) == 0))
649 ret = pointer_from_offset_skipping_decomp (haystack, i);
653 p = g_utf8_next_char (p);
658 g_free (caseless_haystack);
664 g_utf8_caselessnmatch (const char *s1, const char *s2,
665 gssize n1, gssize n2)
668 gchar *normalized_s1;
669 gchar *normalized_s2;
672 gboolean ret = FALSE;
674 g_return_val_if_fail (s1 != NULL, FALSE);
675 g_return_val_if_fail (s2 != NULL, FALSE);
676 g_return_val_if_fail (n1 > 0, FALSE);
677 g_return_val_if_fail (n2 > 0, FALSE);
679 casefold = g_utf8_casefold (s1, n1);
680 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
683 casefold = g_utf8_casefold (s2, n2);
684 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
687 len_s1 = strlen (normalized_s1);
688 len_s2 = strlen (normalized_s2);
693 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
696 g_free (normalized_s1);
697 g_free (normalized_s2);
703 forward_chars_with_skipping (GtkTextIter *iter,
705 gboolean skip_invisible,
706 gboolean skip_nontext,
707 gboolean skip_decomp)
711 g_return_if_fail (count >= 0);
717 gboolean ignored = FALSE;
719 /* minimal workaround to avoid the infinite loop of bug #168247.
720 * It doesn't fix the problemjust the symptom...
722 if (gtk_text_iter_is_end (iter))
725 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
728 if (!ignored && skip_invisible &&
729 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
732 if (!ignored && skip_decomp)
734 /* being UTF8 correct sucks; this accounts for extra
735 offsets coming from canonical decompositions of
736 UTF8 characters (e.g. accented characters) which
737 g_utf8_normalize() performs */
742 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
743 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
744 i -= (g_utf8_strlen (normal, -1) - 1);
748 gtk_text_iter_forward_char (iter);
756 lines_match (const GtkTextIter *start,
758 gboolean visible_only,
760 GtkTextIter *match_start,
761 GtkTextIter *match_end)
768 if (*lines == NULL || **lines == '\0')
771 *match_start = *start;
778 gtk_text_iter_forward_line (&next);
780 /* No more text in buffer, but *lines is nonempty */
781 if (gtk_text_iter_equal (start, &next))
787 line_text = gtk_text_iter_get_visible_slice (start, &next);
789 line_text = gtk_text_iter_get_slice (start, &next);
794 line_text = gtk_text_iter_get_visible_text (start, &next);
796 line_text = gtk_text_iter_get_text (start, &next);
799 if (match_start) /* if this is the first line we're matching */
801 found = g_utf8_strcasestr (line_text, *lines);
805 /* If it's not the first line, we have to match from the
808 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
821 /* Get offset to start of search string */
822 offset = g_utf8_strlen (line_text, found - line_text);
826 /* If match start needs to be returned, set it to the
827 * start of the search string.
829 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
835 /* Go to end of search string */
836 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
845 /* pass NULL for match_start, since we don't need to find the
848 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
851 /* strsplit () that retains the delimiter as part of the string. */
853 strbreakup (const char *string,
854 const char *delimiter,
857 GSList *string_list = NULL, *slist;
858 gchar **str_array, *s, *casefold, *new_string;
861 g_return_val_if_fail (string != NULL, NULL);
862 g_return_val_if_fail (delimiter != NULL, NULL);
865 max_tokens = G_MAXINT;
867 s = strstr (string, delimiter);
870 guint delimiter_len = strlen (delimiter);
876 len = s - string + delimiter_len;
877 new_string = g_new (gchar, len + 1);
878 strncpy (new_string, string, len);
880 casefold = g_utf8_casefold (new_string, -1);
882 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
884 string_list = g_slist_prepend (string_list, new_string);
886 string = s + delimiter_len;
887 s = strstr (string, delimiter);
888 } while (--max_tokens && s);
894 casefold = g_utf8_casefold (string, -1);
895 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
897 string_list = g_slist_prepend (string_list, new_string);
900 str_array = g_new (gchar*, n);
904 str_array[i--] = NULL;
905 for (slist = string_list; slist; slist = slist->next)
906 str_array[i--] = slist->data;
908 g_slist_free (string_list);
914 empathy_text_iter_forward_search (const GtkTextIter *iter,
916 GtkTextIter *match_start,
917 GtkTextIter *match_end,
918 const GtkTextIter *limit)
920 gchar **lines = NULL;
922 gboolean retval = FALSE;
924 gboolean visible_only;
927 g_return_val_if_fail (iter != NULL, FALSE);
928 g_return_val_if_fail (str != NULL, FALSE);
930 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
934 /* If we can move one char, return the empty string there */
937 if (gtk_text_iter_forward_char (&match)) {
938 if (limit && gtk_text_iter_equal (&match, limit)) {
943 *match_start = match;
957 /* locate all lines */
958 lines = strbreakup (str, "\n", -1);
963 /* This loop has an inefficient worst-case, where
964 * gtk_text_iter_get_text () is called repeatedly on
969 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
973 if (lines_match (&search, (const gchar**)lines,
974 visible_only, slice, &match, &end)) {
976 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
980 *match_start = match;
988 } while (gtk_text_iter_forward_line (&search));
990 g_strfreev ((gchar**)lines);
996 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1000 const gchar *ret = NULL;
1003 gchar *caseless_haystack;
1006 g_return_val_if_fail (haystack != NULL, NULL);
1007 g_return_val_if_fail (needle != NULL, NULL);
1009 casefold = g_utf8_casefold (haystack, -1);
1010 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1013 needle_len = g_utf8_strlen (needle, -1);
1014 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1016 if (needle_len == 0)
1018 ret = (gchar *)haystack;
1022 if (haystack_len < needle_len)
1028 i = haystack_len - needle_len;
1029 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1030 needle_len = strlen (needle);
1032 while (p >= caseless_haystack)
1034 if (strncmp (p, needle, needle_len) == 0)
1036 ret = pointer_from_offset_skipping_decomp (haystack, i);
1040 p = g_utf8_prev_char (p);
1045 g_free (caseless_haystack);
1051 backward_lines_match (const GtkTextIter *start,
1052 const gchar **lines,
1053 gboolean visible_only,
1055 GtkTextIter *match_start,
1056 GtkTextIter *match_end)
1058 GtkTextIter line, next;
1063 if (*lines == NULL || **lines == '\0')
1066 *match_start = *start;
1068 *match_end = *start;
1072 line = next = *start;
1073 if (gtk_text_iter_get_line_offset (&next) == 0)
1075 if (!gtk_text_iter_backward_line (&next))
1079 gtk_text_iter_set_line_offset (&next, 0);
1084 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1086 line_text = gtk_text_iter_get_slice (&next, &line);
1091 line_text = gtk_text_iter_get_visible_text (&next, &line);
1093 line_text = gtk_text_iter_get_text (&next, &line);
1096 if (match_start) /* if this is the first line we're matching */
1098 found = g_utf8_strrcasestr (line_text, *lines);
1102 /* If it's not the first line, we have to match from the
1103 * start of the line.
1105 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1118 /* Get offset to start of search string */
1119 offset = g_utf8_strlen (line_text, found - line_text);
1121 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1123 /* If match start needs to be returned, set it to the
1124 * start of the search string.
1128 *match_start = next;
1131 /* Go to end of search string */
1132 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1141 /* try to match the rest of the lines forward, passing NULL
1142 * for match_start so lines_match will try to match the entire
1144 return lines_match (&next, lines, visible_only,
1145 slice, NULL, match_end);
1149 empathy_text_iter_backward_search (const GtkTextIter *iter,
1151 GtkTextIter *match_start,
1152 GtkTextIter *match_end,
1153 const GtkTextIter *limit)
1155 gchar **lines = NULL;
1157 gboolean retval = FALSE;
1159 gboolean visible_only;
1162 g_return_val_if_fail (iter != NULL, FALSE);
1163 g_return_val_if_fail (str != NULL, FALSE);
1165 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1170 /* If we can move one char, return the empty string there */
1173 if (gtk_text_iter_backward_char (&match))
1175 if (limit && gtk_text_iter_equal (&match, limit))
1179 *match_start = match;
1190 visible_only = TRUE;
1193 /* locate all lines */
1194 lines = strbreakup (str, "\n", -1);
1200 /* This loop has an inefficient worst-case, where
1201 * gtk_text_iter_get_text () is called repeatedly on
1206 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1209 if (backward_lines_match (&search, (const gchar**)lines,
1210 visible_only, slice, &match, &end))
1212 if (limit == NULL || (limit &&
1213 gtk_text_iter_compare (&end, limit) > 0))
1218 *match_start = match;
1225 if (gtk_text_iter_get_line_offset (&search) == 0)
1227 if (!gtk_text_iter_backward_line (&search))
1232 gtk_text_iter_set_line_offset (&search, 0);
1236 g_strfreev ((gchar**)lines);
1242 empathy_window_get_is_visible (GtkWindow *window)
1244 GdkWindowState state;
1245 GdkWindow *gdk_window;
1247 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1249 gdk_window = GTK_WIDGET (window)->window;
1254 state = gdk_window_get_state (gdk_window);
1255 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1263 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1265 GdkRectangle icon_location;
1268 GdkWindow *gdk_window;
1270 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1271 gdk_window = GTK_WIDGET (window)->window;
1272 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1274 data[0] = icon_location.x;
1275 data[1] = icon_location.y;
1276 data[2] = icon_location.width;
1277 data[3] = icon_location.height;
1279 XChangeProperty (dpy,
1280 GDK_WINDOW_XID (gdk_window),
1281 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1282 "_NET_WM_ICON_GEOMETRY"),
1283 XA_CARDINAL, 32, PropModeReplace,
1284 (guchar *)&data, 4);
1286 gtk_window_set_skip_taskbar_hint (window, TRUE);
1287 gtk_window_iconify (window);
1290 /* Takes care of moving the window to the current workspace. */
1292 empathy_window_present (GtkWindow *window,
1293 gboolean steal_focus)
1297 g_return_if_fail (GTK_IS_WINDOW (window));
1299 /* There are three cases: hidden, visible, visible on another
1303 if (!empathy_window_get_is_visible (window)) {
1304 /* Hide it so present brings it to the current workspace. */
1305 gtk_widget_hide (GTK_WIDGET (window));
1308 timestamp = gtk_get_current_event_time ();
1309 gtk_window_set_skip_taskbar_hint (window, FALSE);
1310 gtk_window_present_with_time (window, timestamp);
1311 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1312 * it deiconify automatically. */
1313 gtk_window_deiconify (window);
1317 empathy_get_toplevel_window (GtkWidget *widget)
1319 GtkWidget *toplevel;
1321 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1323 toplevel = gtk_widget_get_toplevel (widget);
1324 if (GTK_IS_WINDOW (toplevel) &&
1325 GTK_WIDGET_TOPLEVEL (toplevel)) {
1326 return GTK_WINDOW (toplevel);
1332 /* The URL opening code can't handle schemeless strings, so we try to be
1333 * smart and add http if there is no scheme or doesn't look like a mail
1334 * address. This should work in most cases, and let us click on strings
1335 * like "www.gnome.org".
1338 fixup_url (const gchar *url)
1340 if (g_str_has_prefix (url, "ghelp:") ||
1341 g_str_has_prefix (url, "mailto:") ||
1342 strstr (url, ":/")) {
1346 if (strstr (url, "@")) {
1347 return g_strdup_printf ("mailto:%s", url);
1350 return g_strdup_printf ("http://%s", url);
1354 empathy_url_show (GtkWidget *parent,
1358 GError *error = NULL;
1360 real_url = fixup_url (url);
1365 gtk_show_uri (gtk_widget_get_screen (parent), url,
1366 gtk_get_current_event_time (), &error);
1371 dialog = gtk_message_dialog_new (NULL, 0,
1372 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1373 _("Unable to open URI"));
1374 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1375 "%s", error->message);
1377 g_signal_connect (dialog, "response",
1378 G_CALLBACK (gtk_widget_destroy),
1380 gtk_window_present (GTK_WINDOW (dialog));
1382 g_clear_error (&error);
1389 link_button_hook (GtkLinkButton *button,
1393 empathy_url_show (GTK_WIDGET (button), link);
1397 empathy_link_button_new (const gchar *url,
1400 static gboolean hook = FALSE;
1404 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1407 return gtk_link_button_new_with_label (url, title);
1411 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1416 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1418 g_signal_handlers_block_by_func (widget, callback, user_data);
1419 g_object_set (widget, "active", active, NULL);
1420 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1423 /* Sending files with the file chooser */
1426 file_manager_send_file_request_cb (EmpathyDispatchOperation *operation,
1427 const GError *error, gpointer user_data)
1429 GFile *file = (GFile *)user_data;
1430 EmpathyTpFile *tp_file;
1432 if (error != NULL) {
1433 DEBUG ("Couldn't request channel: %s", error->message);
1434 g_object_unref (file);
1438 DEBUG ("Starting to send file");
1440 tp_file = EMPATHY_TP_FILE (
1441 empathy_dispatch_operation_get_channel_wrapper (operation));
1443 empathy_tp_file_offer (tp_file, file, NULL);
1445 g_object_unref (file);
1449 file_manager_send_file_response_cb (GtkDialog *widget,
1451 EmpathyContact *contact)
1453 if (response_id == GTK_RESPONSE_OK) {
1457 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget));
1459 DEBUG ("File chooser selected files:");
1461 for (l = list; l; l = l->next) {
1465 GtkRecentManager *manager;
1468 GError *error = NULL;
1471 gfile = g_file_new_for_uri (uri);
1472 info = g_file_query_info (gfile,
1473 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1474 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1475 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1479 DEBUG ("Can't get info about the file: %s", error->message);
1480 g_clear_error (&error);
1481 g_object_unref (gfile);
1485 DEBUG ("\t%s", uri);
1486 filename = g_file_get_basename (gfile);
1487 g_file_info_get_modification_time (info, &mtime);
1489 empathy_dispatcher_send_file_to_contact (contact,
1490 filename, g_file_info_get_size (info), mtime.tv_sec,
1491 g_file_info_get_content_type (info),
1492 file_manager_send_file_request_cb, gfile);
1495 g_object_unref (info);
1497 manager = gtk_recent_manager_get_default ();
1498 gtk_recent_manager_add_item (manager, uri);
1502 g_slist_free (list);
1505 gtk_widget_destroy (GTK_WIDGET (widget));
1509 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1514 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1516 DEBUG ("Creating selection file chooser");
1518 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1520 GTK_FILE_CHOOSER_ACTION_OPEN,
1522 GTK_RESPONSE_CANCEL,
1526 button = gtk_button_new_with_mnemonic (_("_Send"));
1527 gtk_button_set_image (GTK_BUTTON (button),
1528 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1529 GTK_ICON_SIZE_BUTTON));
1530 gtk_widget_show (button);
1531 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1533 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1534 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1537 g_signal_connect (widget, "response",
1538 G_CALLBACK (file_manager_send_file_response_cb),
1541 gtk_widget_show (widget);
1545 EmpathySound sound_id;
1546 const char * event_ca_id;
1547 const char * event_ca_description;
1548 const char * gconf_key;
1549 } EmpathySoundEntry;
1551 /* NOTE: these entries MUST be in the same order than EmpathySound enum */
1552 static EmpathySoundEntry sound_entries[LAST_EMPATHY_SOUND] = {
1553 { EMPATHY_SOUND_MESSAGE_INCOMING, "message-new-instant",
1554 N_("Received an instant message"), EMPATHY_PREFS_SOUNDS_INCOMING_MESSAGE } ,
1555 { EMPATHY_SOUND_MESSAGE_OUTGOING, "message-sent-instant",
1556 N_("Sent an instant message"), EMPATHY_PREFS_SOUNDS_OUTGOING_MESSAGE } ,
1557 { EMPATHY_SOUND_CONVERSATION_NEW, "message-new-instant",
1558 N_("Incoming chat request"), EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION },
1559 { EMPATHY_SOUND_CONTACT_CONNECTED, "service-login",
1560 N_("Contact connected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN },
1561 { EMPATHY_SOUND_CONTACT_DISCONNECTED, "service-logout",
1562 N_("Contact disconnected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT },
1563 { EMPATHY_SOUND_ACCOUNT_CONNECTED, "service-login",
1564 N_("Connected to server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN },
1565 { EMPATHY_SOUND_ACCOUNT_DISCONNECTED, "service-logout",
1566 N_("Disconnected from server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT },
1567 { EMPATHY_SOUND_PHONE_INCOMING, "phone-incoming-call",
1568 N_("Incoming voice call"), NULL },
1569 { EMPATHY_SOUND_PHONE_OUTGOING, "phone-outgoing-calling",
1570 N_("Outgoing voice call"), NULL },
1571 { EMPATHY_SOUND_PHONE_HANGUP, "phone-hangup",
1572 N_("Voice call ended"), NULL },
1576 empathy_sound_pref_is_enabled (const char *key)
1579 McPresence presence;
1583 conf = empathy_conf_get ();
1586 empathy_conf_get_bool (conf, EMPATHY_PREFS_SOUNDS_ENABLED, &res);
1592 idle = empathy_idle_dup_singleton ();
1593 presence = empathy_idle_get_state (idle);
1594 g_object_unref (idle);
1596 if (presence != MC_PRESENCE_AVAILABLE &&
1597 presence != MC_PRESENCE_UNSET) {
1598 empathy_conf_get_bool (conf, EMPATHY_PREFS_SOUNDS_DISABLED_AWAY,
1605 empathy_conf_get_bool (conf, key, &res);
1611 empathy_sound_play (GtkWidget *widget,
1612 EmpathySound sound_id)
1614 EmpathySoundEntry *entry;
1615 gboolean should_play = TRUE;
1617 g_return_if_fail (sound_id < LAST_EMPATHY_SOUND);
1619 entry = &(sound_entries[sound_id]);
1620 g_return_if_fail (entry->sound_id == sound_id);
1622 if (entry->gconf_key != NULL) {
1623 should_play = empathy_sound_pref_is_enabled (entry->gconf_key);
1627 DEBUG ("Play sound \"%s\" (%s)",
1629 entry->event_ca_description);
1631 ca_gtk_play_for_widget (widget, 0,
1632 CA_PROP_EVENT_ID, entry->event_ca_id,
1633 CA_PROP_EVENT_DESCRIPTION, gettext (entry->event_ca_description),