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>
56 #define SCHEMES "(https?|s?ftps?|nntp|news|javascript|about|ghelp|apt|telnet|"\
58 #define BODY "([^\\ \\n]+)"
59 #define END_BODY "([^\\ \\n]*[^,;\?><()\\ \"\\.\\n])"
60 #define URI_REGEX "("SCHEMES"://"END_BODY")" \
61 "|((mailto:)?"BODY"@"BODY"\\."END_BODY")"\
62 "|((www|ftp)\\."END_BODY")"
65 empathy_gtk_init (void)
67 static gboolean initialized = FALSE;
73 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
74 PKGDATADIR G_DIR_SEPARATOR_S "icons");
80 empathy_uri_regex_dup_singleton (void)
82 static GRegex *uri_regex = NULL;
84 /* We intentionally leak the regex so it's not recomputed */
86 uri_regex = g_regex_new (URI_REGEX, 0, 0, NULL);
89 return g_regex_ref (uri_regex);
95 gboolean preserve_aspect_ratio;
99 get_glade_file (const gchar *filename,
102 const gchar *first_required_widget,
107 GtkWidget **widget_ptr;
109 DEBUG ("Loading glade file %s", filename);
111 gui = glade_xml_new (filename, root, domain);
114 g_warning ("Couldn't find necessary glade file '%s'", filename);
117 for (name = first_required_widget; name; name = va_arg (args, char *)) {
118 widget_ptr = va_arg (args, void *);
120 *widget_ptr = glade_xml_get_widget (gui, name);
123 g_warning ("Glade file '%s' is missing widget '%s'.",
133 empathy_glade_get_file_simple (const gchar *filename,
136 const gchar *first_required_widget, ...)
141 va_start (args, first_required_widget);
143 gui = get_glade_file (filename,
146 first_required_widget,
152 g_object_unref (gui);
157 empathy_glade_get_file (const gchar *filename,
160 const gchar *first_required_widget, ...)
165 va_start (args, first_required_widget);
167 gui = get_glade_file (filename,
170 first_required_widget,
183 empathy_glade_connect (GladeXML *gui,
185 gchar *first_widget, ...)
193 va_start (args, first_widget);
195 for (name = first_widget; name; name = va_arg (args, char *)) {
196 signal = va_arg (args, void *);
197 callback = va_arg (args, void *);
199 widget = glade_xml_get_widget (gui, name);
201 g_warning ("Glade file is missing widget '%s', aborting",
206 g_signal_connect (widget,
208 G_CALLBACK (callback),
216 empathy_glade_setup_size_group (GladeXML *gui,
217 GtkSizeGroupMode mode,
218 gchar *first_widget, ...)
222 GtkSizeGroup *size_group;
225 va_start (args, first_widget);
227 size_group = gtk_size_group_new (mode);
229 for (name = first_widget; name; name = va_arg (args, char *)) {
230 widget = glade_xml_get_widget (gui, name);
232 g_warning ("Glade file is missing widget '%s'", name);
236 gtk_size_group_add_widget (size_group, widget);
239 g_object_unref (size_group);
245 empathy_icon_name_from_account (McAccount *account)
249 profile = mc_account_get_profile (account);
251 return mc_profile_get_icon_name (profile);
255 empathy_icon_name_for_presence (McPresence presence)
258 case MC_PRESENCE_AVAILABLE:
259 return EMPATHY_IMAGE_AVAILABLE;
260 case MC_PRESENCE_DO_NOT_DISTURB:
261 return EMPATHY_IMAGE_BUSY;
262 case MC_PRESENCE_AWAY:
263 return EMPATHY_IMAGE_AWAY;
264 case MC_PRESENCE_EXTENDED_AWAY:
265 return EMPATHY_IMAGE_EXT_AWAY;
266 case MC_PRESENCE_HIDDEN:
267 return EMPATHY_IMAGE_HIDDEN;
268 case MC_PRESENCE_OFFLINE:
269 case MC_PRESENCE_UNSET:
270 return EMPATHY_IMAGE_OFFLINE;
272 g_assert_not_reached ();
279 empathy_icon_name_for_contact (EmpathyContact *contact)
283 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
284 EMPATHY_IMAGE_OFFLINE);
286 presence = empathy_contact_get_presence (contact);
287 return empathy_icon_name_for_presence (presence);
291 empathy_pixbuf_from_data (gchar *data,
294 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
298 empathy_pixbuf_from_data_and_mime (gchar *data,
302 GdkPixbufLoader *loader;
303 GdkPixbufFormat *format;
304 GdkPixbuf *pixbuf = NULL;
306 GError *error = NULL;
312 loader = gdk_pixbuf_loader_new ();
313 if (!gdk_pixbuf_loader_write (loader, data, data_size, &error)) {
314 DEBUG ("Failed to write to pixbuf loader: %s",
315 error ? error->message : "No error given");
318 if (!gdk_pixbuf_loader_close (loader, &error)) {
319 DEBUG ("Failed to close pixbuf loader: %s",
320 error ? error->message : "No error given");
324 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
326 g_object_ref (pixbuf);
328 if (mime_type != NULL) {
329 format = gdk_pixbuf_loader_get_format (loader);
330 mime_types = gdk_pixbuf_format_get_mime_types (format);
332 *mime_type = g_strdup (*mime_types);
333 if (mime_types[1] != NULL) {
334 DEBUG ("Loader supports more than one mime "
335 "type! Picking the first one, %s",
338 g_strfreev (mime_types);
343 g_clear_error (&error);
344 g_object_unref (loader);
350 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
353 struct SizeData *data)
355 g_return_if_fail (width > 0 && height > 0);
357 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
358 if (width < data->width && height < data->height) {
363 if (data->width < 0) {
364 width = width * (double) data->height / (gdouble) height;
365 height = data->height;
366 } else if (data->height < 0) {
367 height = height * (double) data->width / (double) width;
369 } else if ((double) height * (double) data->width >
370 (double) width * (double) data->height) {
371 width = 0.5 + (double) width * (double) data->height / (double) height;
372 height = data->height;
374 height = 0.5 + (double) height * (double) data->width / (double) width;
378 if (data->width > 0) {
382 if (data->height > 0) {
383 height = data->height;
387 gdk_pixbuf_loader_set_size (loader, width, height);
391 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
393 gint width, height, rowstride;
396 width = gdk_pixbuf_get_width (pixbuf);
397 height = gdk_pixbuf_get_height (pixbuf);
398 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
399 pixels = gdk_pixbuf_get_pixels (pixbuf);
401 if (width < 6 || height < 6) {
409 pixels[rowstride + 3] = 0x80;
410 pixels[rowstride * 2 + 3] = 0xC0;
413 pixels[width * 4 - 1] = 0;
414 pixels[width * 4 - 5] = 0x80;
415 pixels[width * 4 - 9] = 0xC0;
416 pixels[rowstride + (width * 4) - 1] = 0x80;
417 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
420 pixels[(height - 1) * rowstride + 3] = 0;
421 pixels[(height - 1) * rowstride + 7] = 0x80;
422 pixels[(height - 1) * rowstride + 11] = 0xC0;
423 pixels[(height - 2) * rowstride + 3] = 0x80;
424 pixels[(height - 3) * rowstride + 3] = 0xC0;
427 pixels[height * rowstride - 1] = 0;
428 pixels[(height - 1) * rowstride - 1] = 0x80;
429 pixels[(height - 2) * rowstride - 1] = 0xC0;
430 pixels[height * rowstride - 5] = 0x80;
431 pixels[height * rowstride - 9] = 0xC0;
435 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
437 gint width, height, rowstride, i;
441 width = gdk_pixbuf_get_width (pixbuf);
442 height = gdk_pixbuf_get_height (pixbuf);
443 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
444 pixels = gdk_pixbuf_get_pixels (pixbuf);
447 for (i = 3; i < rowstride; i+=4) {
453 for (i = 1; i < height - 1; i++) {
454 row = pixels + (i*rowstride);
455 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
460 row = pixels + ((height-1) * rowstride);
461 for (i = 3; i < rowstride; i+=4) {
471 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
476 GdkPixbufLoader *loader;
477 struct SizeData data;
478 GError *error = NULL;
485 data.height = height;
486 data.preserve_aspect_ratio = TRUE;
488 loader = gdk_pixbuf_loader_new ();
490 g_signal_connect (loader, "size-prepared",
491 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
494 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
495 g_warning ("Couldn't write avatar image:%p with "
496 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
497 avatar->data, avatar->len, error->message);
498 g_error_free (error);
502 gdk_pixbuf_loader_close (loader, NULL);
504 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
505 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
506 GdkPixbuf *rounded_pixbuf;
508 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
509 gdk_pixbuf_get_width (pixbuf),
510 gdk_pixbuf_get_height (pixbuf));
511 gdk_pixbuf_copy_area (pixbuf, 0, 0,
512 gdk_pixbuf_get_width (pixbuf),
513 gdk_pixbuf_get_height (pixbuf),
516 pixbuf = rounded_pixbuf;
518 g_object_ref (pixbuf);
521 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
522 empathy_avatar_pixbuf_roundify (pixbuf);
525 g_object_unref (loader);
531 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
535 EmpathyAvatar *avatar;
537 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
539 avatar = empathy_contact_get_avatar (contact);
541 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
545 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
550 width = gdk_pixbuf_get_width (pixbuf);
551 height = gdk_pixbuf_get_height (pixbuf);
553 if (width > 0 && (width > max_size || height > max_size)) {
554 factor = (gdouble) max_size / MAX (width, height);
556 width = width * factor;
557 height = height * factor;
559 return gdk_pixbuf_scale_simple (pixbuf,
564 return g_object_ref (pixbuf);
568 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
573 GError *error = NULL;
579 theme = gtk_icon_theme_get_default ();
581 pixbuf = gtk_icon_theme_load_icon (theme,
587 DEBUG ("Error loading icon: %s", error->message);
588 g_clear_error (&error);
595 empathy_pixbuf_from_icon_name (const gchar *icon_name,
596 GtkIconSize icon_size)
605 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
609 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
612 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
613 * that to make it easier to apply changes from the original code.
615 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
617 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
618 * decomposable character it consumes the decomposition length from the given
619 * offset. So it's useful when the offset was calculated for the normalized
620 * version of str, but we need a pointer to str itself. */
622 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
624 gchar *casefold, *normal;
630 q = g_utf8_next_char (p);
631 casefold = g_utf8_casefold (p, q - p);
632 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
633 offset -= g_utf8_strlen (normal, -1);
642 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
646 const gchar *ret = NULL;
649 gchar *caseless_haystack;
652 g_return_val_if_fail (haystack != NULL, NULL);
653 g_return_val_if_fail (needle != NULL, NULL);
655 casefold = g_utf8_casefold (haystack, -1);
656 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
659 needle_len = g_utf8_strlen (needle, -1);
660 haystack_len = g_utf8_strlen (caseless_haystack, -1);
664 ret = (gchar *)haystack;
668 if (haystack_len < needle_len)
674 p = (gchar*)caseless_haystack;
675 needle_len = strlen (needle);
680 if ((strncmp (p, needle, needle_len) == 0))
682 ret = pointer_from_offset_skipping_decomp (haystack, i);
686 p = g_utf8_next_char (p);
691 g_free (caseless_haystack);
697 g_utf8_caselessnmatch (const char *s1, const char *s2,
698 gssize n1, gssize n2)
701 gchar *normalized_s1;
702 gchar *normalized_s2;
705 gboolean ret = FALSE;
707 g_return_val_if_fail (s1 != NULL, FALSE);
708 g_return_val_if_fail (s2 != NULL, FALSE);
709 g_return_val_if_fail (n1 > 0, FALSE);
710 g_return_val_if_fail (n2 > 0, FALSE);
712 casefold = g_utf8_casefold (s1, n1);
713 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
716 casefold = g_utf8_casefold (s2, n2);
717 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
720 len_s1 = strlen (normalized_s1);
721 len_s2 = strlen (normalized_s2);
726 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
729 g_free (normalized_s1);
730 g_free (normalized_s2);
736 forward_chars_with_skipping (GtkTextIter *iter,
738 gboolean skip_invisible,
739 gboolean skip_nontext,
740 gboolean skip_decomp)
744 g_return_if_fail (count >= 0);
750 gboolean ignored = FALSE;
752 /* minimal workaround to avoid the infinite loop of bug #168247.
753 * It doesn't fix the problemjust the symptom...
755 if (gtk_text_iter_is_end (iter))
758 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
761 if (!ignored && skip_invisible &&
762 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
765 if (!ignored && skip_decomp)
767 /* being UTF8 correct sucks; this accounts for extra
768 offsets coming from canonical decompositions of
769 UTF8 characters (e.g. accented characters) which
770 g_utf8_normalize() performs */
775 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
776 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
777 i -= (g_utf8_strlen (normal, -1) - 1);
781 gtk_text_iter_forward_char (iter);
789 lines_match (const GtkTextIter *start,
791 gboolean visible_only,
793 GtkTextIter *match_start,
794 GtkTextIter *match_end)
801 if (*lines == NULL || **lines == '\0')
804 *match_start = *start;
811 gtk_text_iter_forward_line (&next);
813 /* No more text in buffer, but *lines is nonempty */
814 if (gtk_text_iter_equal (start, &next))
820 line_text = gtk_text_iter_get_visible_slice (start, &next);
822 line_text = gtk_text_iter_get_slice (start, &next);
827 line_text = gtk_text_iter_get_visible_text (start, &next);
829 line_text = gtk_text_iter_get_text (start, &next);
832 if (match_start) /* if this is the first line we're matching */
834 found = g_utf8_strcasestr (line_text, *lines);
838 /* If it's not the first line, we have to match from the
841 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
854 /* Get offset to start of search string */
855 offset = g_utf8_strlen (line_text, found - line_text);
859 /* If match start needs to be returned, set it to the
860 * start of the search string.
862 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
868 /* Go to end of search string */
869 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
878 /* pass NULL for match_start, since we don't need to find the
881 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
884 /* strsplit () that retains the delimiter as part of the string. */
886 strbreakup (const char *string,
887 const char *delimiter,
890 GSList *string_list = NULL, *slist;
891 gchar **str_array, *s, *casefold, *new_string;
894 g_return_val_if_fail (string != NULL, NULL);
895 g_return_val_if_fail (delimiter != NULL, NULL);
898 max_tokens = G_MAXINT;
900 s = strstr (string, delimiter);
903 guint delimiter_len = strlen (delimiter);
909 len = s - string + delimiter_len;
910 new_string = g_new (gchar, len + 1);
911 strncpy (new_string, string, len);
913 casefold = g_utf8_casefold (new_string, -1);
915 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
917 string_list = g_slist_prepend (string_list, new_string);
919 string = s + delimiter_len;
920 s = strstr (string, delimiter);
921 } while (--max_tokens && s);
927 casefold = g_utf8_casefold (string, -1);
928 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
930 string_list = g_slist_prepend (string_list, new_string);
933 str_array = g_new (gchar*, n);
937 str_array[i--] = NULL;
938 for (slist = string_list; slist; slist = slist->next)
939 str_array[i--] = slist->data;
941 g_slist_free (string_list);
947 empathy_text_iter_forward_search (const GtkTextIter *iter,
949 GtkTextIter *match_start,
950 GtkTextIter *match_end,
951 const GtkTextIter *limit)
953 gchar **lines = NULL;
955 gboolean retval = FALSE;
957 gboolean visible_only;
960 g_return_val_if_fail (iter != NULL, FALSE);
961 g_return_val_if_fail (str != NULL, FALSE);
963 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
967 /* If we can move one char, return the empty string there */
970 if (gtk_text_iter_forward_char (&match)) {
971 if (limit && gtk_text_iter_equal (&match, limit)) {
976 *match_start = match;
990 /* locate all lines */
991 lines = strbreakup (str, "\n", -1);
996 /* This loop has an inefficient worst-case, where
997 * gtk_text_iter_get_text () is called repeatedly on
1002 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1006 if (lines_match (&search, (const gchar**)lines,
1007 visible_only, slice, &match, &end)) {
1008 if (limit == NULL ||
1009 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1013 *match_start = match;
1021 } while (gtk_text_iter_forward_line (&search));
1023 g_strfreev ((gchar**)lines);
1028 static const gchar *
1029 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1033 const gchar *ret = NULL;
1036 gchar *caseless_haystack;
1039 g_return_val_if_fail (haystack != NULL, NULL);
1040 g_return_val_if_fail (needle != NULL, NULL);
1042 casefold = g_utf8_casefold (haystack, -1);
1043 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1046 needle_len = g_utf8_strlen (needle, -1);
1047 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1049 if (needle_len == 0)
1051 ret = (gchar *)haystack;
1055 if (haystack_len < needle_len)
1061 i = haystack_len - needle_len;
1062 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1063 needle_len = strlen (needle);
1065 while (p >= caseless_haystack)
1067 if (strncmp (p, needle, needle_len) == 0)
1069 ret = pointer_from_offset_skipping_decomp (haystack, i);
1073 p = g_utf8_prev_char (p);
1078 g_free (caseless_haystack);
1084 backward_lines_match (const GtkTextIter *start,
1085 const gchar **lines,
1086 gboolean visible_only,
1088 GtkTextIter *match_start,
1089 GtkTextIter *match_end)
1091 GtkTextIter line, next;
1096 if (*lines == NULL || **lines == '\0')
1099 *match_start = *start;
1101 *match_end = *start;
1105 line = next = *start;
1106 if (gtk_text_iter_get_line_offset (&next) == 0)
1108 if (!gtk_text_iter_backward_line (&next))
1112 gtk_text_iter_set_line_offset (&next, 0);
1117 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1119 line_text = gtk_text_iter_get_slice (&next, &line);
1124 line_text = gtk_text_iter_get_visible_text (&next, &line);
1126 line_text = gtk_text_iter_get_text (&next, &line);
1129 if (match_start) /* if this is the first line we're matching */
1131 found = g_utf8_strrcasestr (line_text, *lines);
1135 /* If it's not the first line, we have to match from the
1136 * start of the line.
1138 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1151 /* Get offset to start of search string */
1152 offset = g_utf8_strlen (line_text, found - line_text);
1154 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1156 /* If match start needs to be returned, set it to the
1157 * start of the search string.
1161 *match_start = next;
1164 /* Go to end of search string */
1165 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1174 /* try to match the rest of the lines forward, passing NULL
1175 * for match_start so lines_match will try to match the entire
1177 return lines_match (&next, lines, visible_only,
1178 slice, NULL, match_end);
1182 empathy_text_iter_backward_search (const GtkTextIter *iter,
1184 GtkTextIter *match_start,
1185 GtkTextIter *match_end,
1186 const GtkTextIter *limit)
1188 gchar **lines = NULL;
1190 gboolean retval = FALSE;
1192 gboolean visible_only;
1195 g_return_val_if_fail (iter != NULL, FALSE);
1196 g_return_val_if_fail (str != NULL, FALSE);
1198 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1203 /* If we can move one char, return the empty string there */
1206 if (gtk_text_iter_backward_char (&match))
1208 if (limit && gtk_text_iter_equal (&match, limit))
1212 *match_start = match;
1223 visible_only = TRUE;
1226 /* locate all lines */
1227 lines = strbreakup (str, "\n", -1);
1233 /* This loop has an inefficient worst-case, where
1234 * gtk_text_iter_get_text () is called repeatedly on
1239 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1242 if (backward_lines_match (&search, (const gchar**)lines,
1243 visible_only, slice, &match, &end))
1245 if (limit == NULL || (limit &&
1246 gtk_text_iter_compare (&end, limit) > 0))
1251 *match_start = match;
1258 if (gtk_text_iter_get_line_offset (&search) == 0)
1260 if (!gtk_text_iter_backward_line (&search))
1265 gtk_text_iter_set_line_offset (&search, 0);
1269 g_strfreev ((gchar**)lines);
1275 empathy_window_get_is_visible (GtkWindow *window)
1277 GdkWindowState state;
1278 GdkWindow *gdk_window;
1280 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1282 gdk_window = GTK_WIDGET (window)->window;
1287 state = gdk_window_get_state (gdk_window);
1288 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1296 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1298 GdkRectangle icon_location;
1301 GdkWindow *gdk_window;
1303 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1304 gdk_window = GTK_WIDGET (window)->window;
1305 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1307 data[0] = icon_location.x;
1308 data[1] = icon_location.y;
1309 data[2] = icon_location.width;
1310 data[3] = icon_location.height;
1312 XChangeProperty (dpy,
1313 GDK_WINDOW_XID (gdk_window),
1314 gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window),
1315 "_NET_WM_ICON_GEOMETRY"),
1316 XA_CARDINAL, 32, PropModeReplace,
1317 (guchar *)&data, 4);
1319 gtk_window_set_skip_taskbar_hint (window, TRUE);
1320 gtk_window_iconify (window);
1323 /* Takes care of moving the window to the current workspace. */
1325 empathy_window_present (GtkWindow *window,
1326 gboolean steal_focus)
1330 g_return_if_fail (GTK_IS_WINDOW (window));
1332 /* There are three cases: hidden, visible, visible on another
1336 if (!empathy_window_get_is_visible (window)) {
1337 /* Hide it so present brings it to the current workspace. */
1338 gtk_widget_hide (GTK_WIDGET (window));
1341 timestamp = gtk_get_current_event_time ();
1342 gtk_window_set_skip_taskbar_hint (window, FALSE);
1343 gtk_window_present_with_time (window, timestamp);
1344 /* FIXME: This shouldn't be required as gtk_window_present's doc says
1345 * it deiconify automatically. */
1346 gtk_window_deiconify (window);
1350 empathy_get_toplevel_window (GtkWidget *widget)
1352 GtkWidget *toplevel;
1354 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1356 toplevel = gtk_widget_get_toplevel (widget);
1357 if (GTK_IS_WINDOW (toplevel) &&
1358 GTK_WIDGET_TOPLEVEL (toplevel)) {
1359 return GTK_WINDOW (toplevel);
1365 /* The URL opening code can't handle schemeless strings, so we try to be
1366 * smart and add http if there is no scheme or doesn't look like a mail
1367 * address. This should work in most cases, and let us click on strings
1368 * like "www.gnome.org".
1371 fixup_url (const gchar *url)
1373 if (g_str_has_prefix (url, "ghelp:") ||
1374 g_str_has_prefix (url, "mailto:") ||
1375 strstr (url, ":/")) {
1379 if (strstr (url, "@")) {
1380 return g_strdup_printf ("mailto:%s", url);
1383 return g_strdup_printf ("http://%s", url);
1387 empathy_url_show (GtkWidget *parent,
1391 GError *error = NULL;
1393 real_url = fixup_url (url);
1398 gtk_show_uri (gtk_widget_get_screen (parent), url,
1399 gtk_get_current_event_time (), &error);
1404 dialog = gtk_message_dialog_new (NULL, 0,
1405 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1406 _("Unable to open URI"));
1407 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1408 "%s", error->message);
1410 g_signal_connect (dialog, "response",
1411 G_CALLBACK (gtk_widget_destroy),
1413 gtk_window_present (GTK_WINDOW (dialog));
1415 g_clear_error (&error);
1422 link_button_hook (GtkLinkButton *button,
1426 empathy_url_show (GTK_WIDGET (button), link);
1430 empathy_link_button_new (const gchar *url,
1433 static gboolean hook = FALSE;
1437 gtk_link_button_set_uri_hook (link_button_hook, NULL, NULL);
1440 return gtk_link_button_new_with_label (url, title);
1444 empathy_toggle_button_set_state_quietly (GtkWidget *widget,
1449 g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget));
1451 g_signal_handlers_block_by_func (widget, callback, user_data);
1452 g_object_set (widget, "active", active, NULL);
1453 g_signal_handlers_unblock_by_func (widget, callback, user_data);
1456 /* Sending files with the file chooser */
1459 file_manager_send_file_request_cb (EmpathyDispatchOperation *operation,
1460 const GError *error, gpointer user_data)
1462 GFile *file = (GFile *)user_data;
1463 EmpathyTpFile *tp_file;
1465 if (error != NULL) {
1466 DEBUG ("Couldn't request channel: %s", error->message);
1467 g_object_unref (file);
1471 DEBUG ("Starting to send file");
1473 tp_file = EMPATHY_TP_FILE (
1474 empathy_dispatch_operation_get_channel_wrapper (operation));
1476 empathy_tp_file_offer (tp_file, file, NULL);
1478 g_object_unref (file);
1482 file_manager_send_file_response_cb (GtkDialog *widget,
1484 EmpathyContact *contact)
1486 if (response_id == GTK_RESPONSE_OK) {
1490 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (widget));
1492 DEBUG ("File chooser selected files:");
1494 for (l = list; l; l = l->next) {
1498 GtkRecentManager *manager;
1501 GError *error = NULL;
1504 gfile = g_file_new_for_uri (uri);
1505 info = g_file_query_info (gfile,
1506 G_FILE_ATTRIBUTE_STANDARD_SIZE ","
1507 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
1508 G_FILE_ATTRIBUTE_TIME_MODIFIED,
1512 DEBUG ("Can't get info about the file: %s", error->message);
1513 g_clear_error (&error);
1514 g_object_unref (gfile);
1518 DEBUG ("\t%s", uri);
1519 filename = g_file_get_basename (gfile);
1520 g_file_info_get_modification_time (info, &mtime);
1522 empathy_dispatcher_send_file_to_contact (contact,
1523 filename, g_file_info_get_size (info), mtime.tv_sec,
1524 g_file_info_get_content_type (info),
1525 file_manager_send_file_request_cb, gfile);
1528 g_object_unref (info);
1530 manager = gtk_recent_manager_get_default ();
1531 gtk_recent_manager_add_item (manager, uri);
1535 g_slist_free (list);
1538 gtk_widget_destroy (GTK_WIDGET (widget));
1542 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1547 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1549 DEBUG ("Creating selection file chooser");
1551 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1553 GTK_FILE_CHOOSER_ACTION_OPEN,
1555 GTK_RESPONSE_CANCEL,
1559 button = gtk_button_new_with_mnemonic (_("_Send"));
1560 gtk_button_set_image (GTK_BUTTON (button),
1561 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1562 GTK_ICON_SIZE_BUTTON));
1563 gtk_widget_show (button);
1564 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1566 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1567 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1570 g_signal_connect (widget, "response",
1571 G_CALLBACK (file_manager_send_file_response_cb),
1574 gtk_widget_show (widget);
1578 EmpathySound sound_id;
1579 const char * event_ca_id;
1580 const char * event_ca_description;
1581 const char * gconf_key;
1582 } EmpathySoundEntry;
1584 /* NOTE: these entries MUST be in the same order than EmpathySound enum */
1585 static EmpathySoundEntry sound_entries[LAST_EMPATHY_SOUND] = {
1586 { EMPATHY_SOUND_MESSAGE_INCOMING, "message-new-instant",
1587 N_("Received an instant message"), EMPATHY_PREFS_SOUNDS_INCOMING_MESSAGE } ,
1588 { EMPATHY_SOUND_MESSAGE_OUTGOING, "message-sent-instant",
1589 N_("Sent an instant message"), EMPATHY_PREFS_SOUNDS_OUTGOING_MESSAGE } ,
1590 { EMPATHY_SOUND_CONVERSATION_NEW, "message-new-instant",
1591 N_("Incoming chat request"), EMPATHY_PREFS_SOUNDS_NEW_CONVERSATION },
1592 { EMPATHY_SOUND_CONTACT_CONNECTED, "service-login",
1593 N_("Contact connected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGIN },
1594 { EMPATHY_SOUND_CONTACT_DISCONNECTED, "service-logout",
1595 N_("Contact disconnected"), EMPATHY_PREFS_SOUNDS_CONTACT_LOGOUT },
1596 { EMPATHY_SOUND_ACCOUNT_CONNECTED, "service-login",
1597 N_("Connected to server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGIN },
1598 { EMPATHY_SOUND_ACCOUNT_DISCONNECTED, "service-logout",
1599 N_("Disconnected from server"), EMPATHY_PREFS_SOUNDS_SERVICE_LOGOUT },
1600 { EMPATHY_SOUND_PHONE_INCOMING, "phone-incoming-call",
1601 N_("Incoming voice call"), NULL },
1602 { EMPATHY_SOUND_PHONE_OUTGOING, "phone-outgoing-calling",
1603 N_("Outgoing voice call"), NULL },
1604 { EMPATHY_SOUND_PHONE_HANGUP, "phone-hangup",
1605 N_("Voice call ended"), NULL },
1610 empathy_sound_pref_is_enabled (const char *key)
1615 conf = empathy_conf_get ();
1618 empathy_conf_get_bool (conf, EMPATHY_PREFS_SOUNDS_ENABLED, &res);
1624 if (!empathy_check_available_state ()) {
1625 empathy_conf_get_bool (conf, EMPATHY_PREFS_SOUNDS_DISABLED_AWAY,
1632 empathy_conf_get_bool (conf, key, &res);
1638 empathy_sound_stop (EmpathySound sound_id)
1640 EmpathySoundEntry *entry;
1642 g_return_if_fail (sound_id < LAST_EMPATHY_SOUND);
1644 entry = &(sound_entries[sound_id]);
1645 g_return_if_fail (entry->sound_id == sound_id);
1647 ca_context_cancel (ca_gtk_context_get (), entry->sound_id);
1652 empathy_sound_play_full (GtkWidget *widget, EmpathySound sound_id,
1653 ca_finish_callback_t callback, gpointer user_data)
1655 EmpathySoundEntry *entry;
1656 gboolean should_play = TRUE;
1657 ca_proplist *p = NULL;
1660 g_return_val_if_fail (sound_id < LAST_EMPATHY_SOUND, FALSE);
1662 entry = &(sound_entries[sound_id]);
1663 g_return_val_if_fail (entry->sound_id == sound_id, FALSE);
1665 if (entry->gconf_key != NULL) {
1666 should_play = empathy_sound_pref_is_enabled (entry->gconf_key);
1672 c = ca_gtk_context_get ();
1673 ca_context_cancel (c, entry->sound_id);
1675 DEBUG ("Play sound \"%s\" (%s)",
1677 entry->event_ca_description);
1679 if (ca_proplist_create(&p) < 0)
1682 if (ca_proplist_sets (p, CA_PROP_EVENT_ID, entry->event_ca_id) < 0)
1685 if (ca_proplist_sets (p, CA_PROP_EVENT_DESCRIPTION,
1686 gettext (entry->event_ca_id)) < 0)
1689 if (ca_gtk_proplist_set_for_widget (p, widget) < 0)
1692 ca_context_play_full (ca_gtk_context_get (), entry->sound_id,
1693 p, callback, user_data);
1695 ca_proplist_destroy (p);
1701 ca_proplist_destroy (p);
1707 empathy_sound_play (GtkWidget *widget, EmpathySound sound_id)
1709 empathy_sound_play_full (widget, sound_id, NULL, NULL);