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>
56 empathy_gtk_init (void)
58 static gboolean initialized = FALSE;
64 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
65 PKGDATADIR G_DIR_SEPARATOR_S "icons");
73 gboolean preserve_aspect_ratio;
77 get_glade_file (const gchar *filename,
80 const gchar *first_required_widget,
85 GtkWidget **widget_ptr;
87 DEBUG ("Loading glade file %s", filename);
89 gui = glade_xml_new (filename, root, domain);
92 g_warning ("Couldn't find necessary glade file '%s'", filename);
95 for (name = first_required_widget; name; name = va_arg (args, char *)) {
96 widget_ptr = va_arg (args, void *);
98 *widget_ptr = glade_xml_get_widget (gui, name);
101 g_warning ("Glade file '%s' is missing widget '%s'.",
111 empathy_glade_get_file_simple (const gchar *filename,
114 const gchar *first_required_widget, ...)
119 va_start (args, first_required_widget);
121 gui = get_glade_file (filename,
124 first_required_widget,
130 g_object_unref (gui);
135 empathy_glade_get_file (const gchar *filename,
138 const gchar *first_required_widget, ...)
143 va_start (args, first_required_widget);
145 gui = get_glade_file (filename,
148 first_required_widget,
161 empathy_glade_connect (GladeXML *gui,
163 gchar *first_widget, ...)
171 va_start (args, first_widget);
173 for (name = first_widget; name; name = va_arg (args, char *)) {
174 signal = va_arg (args, void *);
175 callback = va_arg (args, void *);
177 widget = glade_xml_get_widget (gui, name);
179 g_warning ("Glade file is missing widget '%s', aborting",
184 g_signal_connect (widget,
186 G_CALLBACK (callback),
194 empathy_glade_setup_size_group (GladeXML *gui,
195 GtkSizeGroupMode mode,
196 gchar *first_widget, ...)
200 GtkSizeGroup *size_group;
203 va_start (args, first_widget);
205 size_group = gtk_size_group_new (mode);
207 for (name = first_widget; name; name = va_arg (args, char *)) {
208 widget = glade_xml_get_widget (gui, name);
210 g_warning ("Glade file is missing widget '%s'", name);
214 gtk_size_group_add_widget (size_group, widget);
217 g_object_unref (size_group);
223 empathy_icon_name_from_account (McAccount *account)
227 profile = mc_account_get_profile (account);
229 return mc_profile_get_icon_name (profile);
233 empathy_icon_name_for_presence (McPresence presence)
236 case MC_PRESENCE_AVAILABLE:
237 return EMPATHY_IMAGE_AVAILABLE;
238 case MC_PRESENCE_DO_NOT_DISTURB:
239 return EMPATHY_IMAGE_BUSY;
240 case MC_PRESENCE_AWAY:
241 return EMPATHY_IMAGE_AWAY;
242 case MC_PRESENCE_EXTENDED_AWAY:
243 return EMPATHY_IMAGE_EXT_AWAY;
244 case MC_PRESENCE_HIDDEN:
245 return EMPATHY_IMAGE_HIDDEN;
246 case MC_PRESENCE_OFFLINE:
247 case MC_PRESENCE_UNSET:
248 return EMPATHY_IMAGE_OFFLINE;
250 g_assert_not_reached ();
257 empathy_icon_name_for_contact (EmpathyContact *contact)
261 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
262 EMPATHY_IMAGE_OFFLINE);
264 presence = empathy_contact_get_presence (contact);
265 return empathy_icon_name_for_presence (presence);
269 empathy_pixbuf_from_data (gchar *data,
272 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
276 empathy_pixbuf_from_data_and_mime (gchar *data,
280 GdkPixbufLoader *loader;
281 GdkPixbufFormat *format;
282 GdkPixbuf *pixbuf = NULL;
284 GError *error = NULL;
290 loader = gdk_pixbuf_loader_new ();
291 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
292 DEBUG ("Failed to write to pixbuf loader: %s",
293 error ? error->message : "No error given");
296 if (!gdk_pixbuf_loader_close (loader, &error)) {
297 DEBUG ("Failed to close pixbuf loader: %s",
298 error ? error->message : "No error given");
302 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
304 g_object_ref (pixbuf);
306 if (mime_type != NULL) {
307 format = gdk_pixbuf_loader_get_format (loader);
308 mime_types = gdk_pixbuf_format_get_mime_types (format);
310 *mime_type = g_strdup (*mime_types);
311 if (mime_types[1] != NULL) {
312 DEBUG ("Loader supports more than one mime "
313 "type! Picking the first one, %s",
316 g_strfreev (mime_types);
321 g_clear_error (&error);
322 g_object_unref (loader);
328 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
331 struct SizeData *data)
333 g_return_if_fail (width > 0 && height > 0);
335 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
336 if (width < data->width && height < data->height) {
341 if (data->width < 0) {
342 width = width * (double) data->height / (gdouble) height;
343 height = data->height;
344 } else if (data->height < 0) {
345 height = height * (double) data->width / (double) width;
347 } else if ((double) height * (double) data->width >
348 (double) width * (double) data->height) {
349 width = 0.5 + (double) width * (double) data->height / (double) height;
350 height = data->height;
352 height = 0.5 + (double) height * (double) data->width / (double) width;
356 if (data->width > 0) {
360 if (data->height > 0) {
361 height = data->height;
365 gdk_pixbuf_loader_set_size (loader, width, height);
369 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
371 gint width, height, rowstride;
374 width = gdk_pixbuf_get_width (pixbuf);
375 height = gdk_pixbuf_get_height (pixbuf);
376 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
377 pixels = gdk_pixbuf_get_pixels (pixbuf);
379 if (width < 6 || height < 6) {
387 pixels[rowstride + 3] = 0x80;
388 pixels[rowstride * 2 + 3] = 0xC0;
391 pixels[width * 4 - 1] = 0;
392 pixels[width * 4 - 5] = 0x80;
393 pixels[width * 4 - 9] = 0xC0;
394 pixels[rowstride + (width * 4) - 1] = 0x80;
395 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
398 pixels[(height - 1) * rowstride + 3] = 0;
399 pixels[(height - 1) * rowstride + 7] = 0x80;
400 pixels[(height - 1) * rowstride + 11] = 0xC0;
401 pixels[(height - 2) * rowstride + 3] = 0x80;
402 pixels[(height - 3) * rowstride + 3] = 0xC0;
405 pixels[height * rowstride - 1] = 0;
406 pixels[(height - 1) * rowstride - 1] = 0x80;
407 pixels[(height - 2) * rowstride - 1] = 0xC0;
408 pixels[height * rowstride - 5] = 0x80;
409 pixels[height * rowstride - 9] = 0xC0;
413 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
415 gint width, height, rowstride, i;
419 width = gdk_pixbuf_get_width (pixbuf);
420 height = gdk_pixbuf_get_height (pixbuf);
421 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
422 pixels = gdk_pixbuf_get_pixels (pixbuf);
425 for (i = 3; i < rowstride; i+=4) {
431 for (i = 1; i < height - 1; i++) {
432 row = pixels + (i*rowstride);
433 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
438 row = pixels + ((height-1) * rowstride);
439 for (i = 3; i < rowstride; i+=4) {
449 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
454 GdkPixbufLoader *loader;
455 struct SizeData data;
456 GError *error = NULL;
463 data.height = height;
464 data.preserve_aspect_ratio = TRUE;
466 loader = gdk_pixbuf_loader_new ();
468 g_signal_connect (loader, "size-prepared",
469 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
472 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
473 g_warning ("Couldn't write avatar image:%p with "
474 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
475 avatar->data, avatar->len, error->message);
476 g_error_free (error);
480 gdk_pixbuf_loader_close (loader, NULL);
482 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
483 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
484 GdkPixbuf *rounded_pixbuf;
486 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
487 gdk_pixbuf_get_width (pixbuf),
488 gdk_pixbuf_get_height (pixbuf));
489 gdk_pixbuf_copy_area (pixbuf, 0, 0,
490 gdk_pixbuf_get_width (pixbuf),
491 gdk_pixbuf_get_height (pixbuf),
494 pixbuf = rounded_pixbuf;
496 g_object_ref (pixbuf);
499 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
500 empathy_avatar_pixbuf_roundify (pixbuf);
503 g_object_unref (loader);
509 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
513 EmpathyAvatar *avatar;
515 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
517 avatar = empathy_contact_get_avatar (contact);
519 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
523 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
528 width = gdk_pixbuf_get_width (pixbuf);
529 height = gdk_pixbuf_get_height (pixbuf);
531 if (width > 0 && (width > max_size || height > max_size)) {
532 factor = (gdouble) max_size / MAX (width, height);
534 width = width * factor;
535 height = height * factor;
537 return gdk_pixbuf_scale_simple (pixbuf,
542 return g_object_ref (pixbuf);
546 empathy_pixbuf_from_icon_name (const gchar *icon_name,
547 GtkIconSize icon_size)
550 GdkPixbuf *pixbuf = NULL;
551 GError *error = NULL;
559 theme = gtk_icon_theme_get_default ();
561 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
565 pixbuf = gtk_icon_theme_load_icon (theme,
571 DEBUG ("Error loading icon: %s", error->message);
572 g_clear_error (&error);
578 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
579 * that to make it easier to apply changes from the original code.
581 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
583 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
584 * decomposable character it consumes the decomposition length from the given
585 * offset. So it's useful when the offset was calculated for the normalized
586 * version of str, but we need a pointer to str itself. */
588 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
590 gchar *casefold, *normal;
596 q = g_utf8_next_char (p);
597 casefold = g_utf8_casefold (p, q - p);
598 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
599 offset -= g_utf8_strlen (normal, -1);
608 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
612 const gchar *ret = NULL;
615 gchar *caseless_haystack;
618 g_return_val_if_fail (haystack != NULL, NULL);
619 g_return_val_if_fail (needle != NULL, NULL);
621 casefold = g_utf8_casefold (haystack, -1);
622 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
625 needle_len = g_utf8_strlen (needle, -1);
626 haystack_len = g_utf8_strlen (caseless_haystack, -1);
630 ret = (gchar *)haystack;
634 if (haystack_len < needle_len)
640 p = (gchar*)caseless_haystack;
641 needle_len = strlen (needle);
646 if ((strncmp (p, needle, needle_len) == 0))
648 ret = pointer_from_offset_skipping_decomp (haystack, i);
652 p = g_utf8_next_char (p);
657 g_free (caseless_haystack);
663 g_utf8_caselessnmatch (const char *s1, const char *s2,
664 gssize n1, gssize n2)
667 gchar *normalized_s1;
668 gchar *normalized_s2;
671 gboolean ret = FALSE;
673 g_return_val_if_fail (s1 != NULL, FALSE);
674 g_return_val_if_fail (s2 != NULL, FALSE);
675 g_return_val_if_fail (n1 > 0, FALSE);
676 g_return_val_if_fail (n2 > 0, FALSE);
678 casefold = g_utf8_casefold (s1, n1);
679 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
682 casefold = g_utf8_casefold (s2, n2);
683 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
686 len_s1 = strlen (normalized_s1);
687 len_s2 = strlen (normalized_s2);
692 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
695 g_free (normalized_s1);
696 g_free (normalized_s2);
702 forward_chars_with_skipping (GtkTextIter *iter,
704 gboolean skip_invisible,
705 gboolean skip_nontext,
706 gboolean skip_decomp)
710 g_return_if_fail (count >= 0);
716 gboolean ignored = FALSE;
718 /* minimal workaround to avoid the infinite loop of bug #168247.
719 * It doesn't fix the problemjust the symptom...
721 if (gtk_text_iter_is_end (iter))
724 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
727 if (!ignored && skip_invisible &&
728 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
731 if (!ignored && skip_decomp)
733 /* being UTF8 correct sucks; this accounts for extra
734 offsets coming from canonical decompositions of
735 UTF8 characters (e.g. accented characters) which
736 g_utf8_normalize() performs */
741 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
742 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
743 i -= (g_utf8_strlen (normal, -1) - 1);
747 gtk_text_iter_forward_char (iter);
755 lines_match (const GtkTextIter *start,
757 gboolean visible_only,
759 GtkTextIter *match_start,
760 GtkTextIter *match_end)
767 if (*lines == NULL || **lines == '\0')
770 *match_start = *start;
777 gtk_text_iter_forward_line (&next);
779 /* No more text in buffer, but *lines is nonempty */
780 if (gtk_text_iter_equal (start, &next))
786 line_text = gtk_text_iter_get_visible_slice (start, &next);
788 line_text = gtk_text_iter_get_slice (start, &next);
793 line_text = gtk_text_iter_get_visible_text (start, &next);
795 line_text = gtk_text_iter_get_text (start, &next);
798 if (match_start) /* if this is the first line we're matching */
800 found = g_utf8_strcasestr (line_text, *lines);
804 /* If it's not the first line, we have to match from the
807 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
820 /* Get offset to start of search string */
821 offset = g_utf8_strlen (line_text, found - line_text);
825 /* If match start needs to be returned, set it to the
826 * start of the search string.
828 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
834 /* Go to end of search string */
835 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
844 /* pass NULL for match_start, since we don't need to find the
847 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
850 /* strsplit () that retains the delimiter as part of the string. */
852 strbreakup (const char *string,
853 const char *delimiter,
856 GSList *string_list = NULL, *slist;
857 gchar **str_array, *s, *casefold, *new_string;
860 g_return_val_if_fail (string != NULL, NULL);
861 g_return_val_if_fail (delimiter != NULL, NULL);
864 max_tokens = G_MAXINT;
866 s = strstr (string, delimiter);
869 guint delimiter_len = strlen (delimiter);
875 len = s - string + delimiter_len;
876 new_string = g_new (gchar, len + 1);
877 strncpy (new_string, string, len);
879 casefold = g_utf8_casefold (new_string, -1);
881 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
883 string_list = g_slist_prepend (string_list, new_string);
885 string = s + delimiter_len;
886 s = strstr (string, delimiter);
887 } while (--max_tokens && s);
893 casefold = g_utf8_casefold (string, -1);
894 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
896 string_list = g_slist_prepend (string_list, new_string);
899 str_array = g_new (gchar*, n);
903 str_array[i--] = NULL;
904 for (slist = string_list; slist; slist = slist->next)
905 str_array[i--] = slist->data;
907 g_slist_free (string_list);
913 empathy_text_iter_forward_search (const GtkTextIter *iter,
915 GtkTextIter *match_start,
916 GtkTextIter *match_end,
917 const GtkTextIter *limit)
919 gchar **lines = NULL;
921 gboolean retval = FALSE;
923 gboolean visible_only;
926 g_return_val_if_fail (iter != NULL, FALSE);
927 g_return_val_if_fail (str != NULL, FALSE);
929 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
933 /* If we can move one char, return the empty string there */
936 if (gtk_text_iter_forward_char (&match)) {
937 if (limit && gtk_text_iter_equal (&match, limit)) {
942 *match_start = match;
956 /* locate all lines */
957 lines = strbreakup (str, "\n", -1);
962 /* This loop has an inefficient worst-case, where
963 * gtk_text_iter_get_text () is called repeatedly on
968 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
972 if (lines_match (&search, (const gchar**)lines,
973 visible_only, slice, &match, &end)) {
975 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
979 *match_start = match;
987 } while (gtk_text_iter_forward_line (&search));
989 g_strfreev ((gchar**)lines);
995 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
999 const gchar *ret = NULL;
1002 gchar *caseless_haystack;
1005 g_return_val_if_fail (haystack != NULL, NULL);
1006 g_return_val_if_fail (needle != NULL, NULL);
1008 casefold = g_utf8_casefold (haystack, -1);
1009 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1012 needle_len = g_utf8_strlen (needle, -1);
1013 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1015 if (needle_len == 0)
1017 ret = (gchar *)haystack;
1021 if (haystack_len < needle_len)
1027 i = haystack_len - needle_len;
1028 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1029 needle_len = strlen (needle);
1031 while (p >= caseless_haystack)
1033 if (strncmp (p, needle, needle_len) == 0)
1035 ret = pointer_from_offset_skipping_decomp (haystack, i);
1039 p = g_utf8_prev_char (p);
1044 g_free (caseless_haystack);
1050 backward_lines_match (const GtkTextIter *start,
1051 const gchar **lines,
1052 gboolean visible_only,
1054 GtkTextIter *match_start,
1055 GtkTextIter *match_end)
1057 GtkTextIter line, next;
1062 if (*lines == NULL || **lines == '\0')
1065 *match_start = *start;
1067 *match_end = *start;
1071 line = next = *start;
1072 if (gtk_text_iter_get_line_offset (&next) == 0)
1074 if (!gtk_text_iter_backward_line (&next))
1078 gtk_text_iter_set_line_offset (&next, 0);
1083 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1085 line_text = gtk_text_iter_get_slice (&next, &line);
1090 line_text = gtk_text_iter_get_visible_text (&next, &line);
1092 line_text = gtk_text_iter_get_text (&next, &line);
1095 if (match_start) /* if this is the first line we're matching */
1097 found = g_utf8_strrcasestr (line_text, *lines);
1101 /* If it's not the first line, we have to match from the
1102 * start of the line.
1104 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1117 /* Get offset to start of search string */
1118 offset = g_utf8_strlen (line_text, found - line_text);
1120 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1122 /* If match start needs to be returned, set it to the
1123 * start of the search string.
1127 *match_start = next;
1130 /* Go to end of search string */
1131 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1140 /* try to match the rest of the lines forward, passing NULL
1141 * for match_start so lines_match will try to match the entire
1143 return lines_match (&next, lines, visible_only,
1144 slice, NULL, match_end);
1148 empathy_text_iter_backward_search (const GtkTextIter *iter,
1150 GtkTextIter *match_start,
1151 GtkTextIter *match_end,
1152 const GtkTextIter *limit)
1154 gchar **lines = NULL;
1156 gboolean retval = FALSE;
1158 gboolean visible_only;
1161 g_return_val_if_fail (iter != NULL, FALSE);
1162 g_return_val_if_fail (str != NULL, FALSE);
1164 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1169 /* If we can move one char, return the empty string there */
1172 if (gtk_text_iter_backward_char (&match))
1174 if (limit && gtk_text_iter_equal (&match, limit))
1178 *match_start = match;
1189 visible_only = TRUE;
1192 /* locate all lines */
1193 lines = strbreakup (str, "\n", -1);
1199 /* This loop has an inefficient worst-case, where
1200 * gtk_text_iter_get_text () is called repeatedly on
1205 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1208 if (backward_lines_match (&search, (const gchar**)lines,
1209 visible_only, slice, &match, &end))
1211 if (limit == NULL || (limit &&
1212 gtk_text_iter_compare (&end, limit) > 0))
1217 *match_start = match;
1224 if (gtk_text_iter_get_line_offset (&search) == 0)
1226 if (!gtk_text_iter_backward_line (&search))
1231 gtk_text_iter_set_line_offset (&search, 0);
1235 g_strfreev ((gchar**)lines);
1241 empathy_window_get_is_visible (GtkWindow *window)
1243 GdkWindowState state;
1244 GdkWindow *gdk_window;
1246 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1248 gdk_window = GTK_WIDGET (window)->window;
1253 state = gdk_window_get_state (gdk_window);
1254 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1262 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1264 GdkRectangle icon_location;
1267 GdkWindow *gdk_window;
1269 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1270 gdk_window = GTK_WIDGET (window)->window;
1271 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1273 data[0] = icon_location.x;
1274 data[1] = icon_location.y;
1275 data[2] = icon_location.width;
1276 data[3] = icon_location.height;
1278 XChangeProperty (dpy,
1279 GDK_WINDOW_XID (gdk_window),
1280 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1281 "_NET_WM_ICON_GEOMETRY"),
1282 XA_CARDINAL, 32, PropModeReplace,
1283 (guchar *)&data, 4);
1285 gtk_window_set_skip_taskbar_hint (window, TRUE);
1286 gtk_window_iconify (window);
1289 /* Takes care of moving the window to the current workspace. */
1291 empathy_window_present (GtkWindow *window,
1292 gboolean steal_focus)
1296 g_return_if_fail (GTK_IS_WINDOW (window));
1298 /* There are three cases: hidden, visible, visible on another
1302 if (!empathy_window_get_is_visible (window)) {
1303 /* Hide it so present brings it to the current workspace. */
1304 gtk_widget_hide (GTK_WIDGET (window));
1307 timestamp = gtk_get_current_event_time ();
1308 gtk_window_set_skip_taskbar_hint (window, FALSE);
1309 gtk_window_present_with_time (window, timestamp);
1310 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1311 * it deiconify automatically. */
1312 gtk_window_deiconify (window);
1316 empathy_get_toplevel_window (GtkWidget *widget)
1318 GtkWidget *toplevel;
1320 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1322 toplevel = gtk_widget_get_toplevel (widget);
1323 if (GTK_IS_WINDOW (toplevel) &&
1324 GTK_WIDGET_TOPLEVEL (toplevel)) {
1325 return GTK_WINDOW (toplevel);
1331 /* The URL opening code can't handle schemeless strings, so we try to be
1332 * smart and add http if there is no scheme or doesn't look like a mail
1333 * address. This should work in most cases, and let us click on strings
1334 * like "www.gnome.org".
1337 fixup_url (const gchar *url)
1339 if (g_str_has_prefix (url, "ghelp:") ||
1340 g_str_has_prefix (url, "mailto:") ||
1341 strstr (url, ":/")) {
1345 if (strstr (url, "@")) {
1346 return g_strdup_printf ("mailto:%s", url);
1349 return g_strdup_printf ("http://%s", url);
1353 empathy_url_show (GtkWidget *parent,
1357 GError *error = NULL;
1359 real_url = fixup_url (url);
1364 gtk_show_uri (gtk_widget_get_screen (parent), url,
1365 gtk_get_current_event_time (), &error);
1370 dialog = gtk_message_dialog_new (NULL, 0,
1371 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1372 _("Unable to open URI"));
1373 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1374 "%s", error->message);
1376 g_signal_connect (dialog, "response",
1377 G_CALLBACK (gtk_widget_destroy),
1379 gtk_window_present (GTK_WINDOW (dialog));
1381 g_clear_error (&error);
1388 link_button_hook (GtkLinkButton *button,
1392 empathy_url_show (GTK_WIDGET (button), link);
1396 empathy_link_button_new (const gchar *url,
1399 static gboolean hook = FALSE;
1403 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1406 return gtk_link_button_new_with_label (url, title);
1410 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1415 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1417 g_signal_handlers_block_by_func (widget, callback, user_data);
1418 g_object_set (widget, "active", active, NULL);
1419 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1422 /* Sending files with the file chooser */
1425 file_manager_send_file_response_cb (GtkDialog *widget,
1427 EmpathyContact *contact)
1429 if (response_id == GTK_RESPONSE_OK) {
1433 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget));
1435 DEBUG ("File chooser selected files:");
1437 for (l = list; l; l = l->next) {
1440 GtkRecentManager *manager;
1443 gfile = g_file_new_for_uri (uri);
1445 DEBUG ("\t%s", uri);
1446 empathy_dispatcher_send_file (contact, gfile);
1448 manager = gtk_recent_manager_get_default ();
1449 gtk_recent_manager_add_item (manager, uri);
1451 g_object_unref (gfile);
1454 g_slist_free (list);
1457 gtk_widget_destroy (GTK_WIDGET (widget));
1461 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1466 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1468 DEBUG ("Creating selection file chooser");
1470 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1472 GTK_FILE_CHOOSER_ACTION_OPEN,
1474 GTK_RESPONSE_CANCEL,
1478 button = gtk_button_new_with_mnemonic (_("_Send"));
1479 gtk_button_set_image (GTK_BUTTON (button),
1480 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1481 GTK_ICON_SIZE_BUTTON));
1482 gtk_widget_show (button);
1483 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1485 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1486 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1489 g_signal_connect (widget, "response",
1490 G_CALLBACK (file_manager_send_file_response_cb),
1493 gtk_widget_show (widget);
1497 EmpathySound sound_id;
1498 const char * event_ca_id;
1499 const char * event_ca_description;
1500 const char * gconf_key;
1501 } EmpathySoundEntry;
1503 /* NOTE: these entries MUST be in the same order than EmpathySound enum */
1504 static EmpathySoundEntry sound_entries[LAST_EMPATHY_SOUND] = {
1505 { EMPATHY_SOUND_MESSAGE_INCOMING, "message-new-instant",
1506 N_("Received an instant message"), EMPATHY_PREFS_SOUNDS_INCOMING_MESSAGE } ,
1507 { EMPATHY_SOUND_MESSAGE_OUTGOING, "message-sent-instant",
1508 N_("Sent an instant message"), EMPATHY_PREFS_SOUNDS_OUTGOING_MESSAGE } ,
1509 { EMPATHY_SOUND_CONVERSATION_NEW, "message-new-instant",
1510 N_("Incoming chat request"), EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION },
1511 { EMPATHY_SOUND_CONTACT_CONNECTED, "service-login",
1512 N_("Contact connected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN },
1513 { EMPATHY_SOUND_CONTACT_DISCONNECTED, "service-logout",
1514 N_("Contact disconnected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT },
1515 { EMPATHY_SOUND_ACCOUNT_CONNECTED, "service-login",
1516 N_("Connected to server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN },
1517 { EMPATHY_SOUND_ACCOUNT_DISCONNECTED, "service-logout",
1518 N_("Disconnected from server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT },
1519 { EMPATHY_SOUND_PHONE_INCOMING, "phone-incoming-call",
1520 N_("Incoming voice call"), NULL },
1521 { EMPATHY_SOUND_PHONE_OUTGOING, "phone-outgoing-calling",
1522 N_("Outgoing voice call"), NULL },
1523 { EMPATHY_SOUND_PHONE_HANGUP, "phone-hangup",
1524 N_("Voice call ended"), NULL },
1528 empathy_sound_pref_is_enabled (const char *key)
1531 McPresence presence;
1535 conf = empathy_conf_get ();
1538 empathy_conf_get_bool (conf, EMPATHY_PREFS_SOUNDS_ENABLED, &res);
1544 idle = empathy_idle_new ();
1545 presence = empathy_idle_get_state (idle);
1546 g_object_unref (idle);
1548 if (presence != MC_PRESENCE_AVAILABLE &&
1549 presence != MC_PRESENCE_UNSET) {
1550 empathy_conf_get_bool (conf, EMPATHY_PREFS_SOUNDS_DISABLED_AWAY,
1557 empathy_conf_get_bool (conf, key, &res);
1563 empathy_sound_play (GtkWidget *widget,
1564 EmpathySound sound_id)
1566 EmpathySoundEntry *entry;
1567 gboolean should_play = TRUE;
1569 g_return_if_fail (sound_id < LAST_EMPATHY_SOUND);
1571 entry = &(sound_entries[sound_id]);
1572 g_return_if_fail (entry->sound_id == sound_id);
1574 if (entry->gconf_key != NULL) {
1575 should_play = empathy_sound_pref_is_enabled (entry->gconf_key);
1579 DEBUG ("Play sound \"%s\" (%s)",
1581 entry->event_ca_description);
1583 ca_gtk_play_for_widget (widget, 0,
1584 CA_PROP_EVENT_ID, entry->event_ca_id,
1585 CA_PROP_EVENT_DESCRIPTION, gettext (entry->event_ca_description),