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-2010 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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 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>
26 * Travis Reitter <travis.reitter@collabora.co.uk>
28 * Part of this file is copied from GtkSourceView (gtksourceiter.c):
36 #include <X11/Xatom.h>
38 #include <glib/gi18n-lib.h>
42 #include <folks/folks.h>
44 #include "empathy-ui-utils.h"
45 #include "empathy-images.h"
46 #include "empathy-smiley-manager.h"
48 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
49 #include <libempathy/empathy-debug.h>
50 #include <libempathy/empathy-utils.h>
51 #include <libempathy/empathy-dispatcher.h>
52 #include <libempathy/empathy-idle.h>
53 #include <libempathy/empathy-ft-factory.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");
71 builder_get_file_valist (const gchar *filename,
72 const gchar *first_object,
80 DEBUG ("Loading file %s", filename);
82 gui = gtk_builder_new ();
83 gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
84 if (!gtk_builder_add_from_file (gui, filename, &error)) {
85 g_critical ("GtkBuilder Error (%s): %s",
86 filename, error->message);
87 g_clear_error (&error);
90 /* we need to iterate and set all of the pointers to NULL */
91 for (name = first_object; name;
92 name = va_arg (args, const gchar *)) {
93 object_ptr = va_arg (args, GObject**);
101 for (name = first_object; name; name = va_arg (args, const gchar *)) {
102 object_ptr = va_arg (args, GObject**);
104 *object_ptr = gtk_builder_get_object (gui, name);
107 g_warning ("File is missing object '%s'.", name);
116 empathy_builder_get_file (const gchar *filename,
117 const gchar *first_object,
123 va_start (args, first_object);
124 gui = builder_get_file_valist (filename, first_object, args);
131 empathy_builder_connect (GtkBuilder *gui,
142 va_start (args, first_object);
143 for (name = first_object; name; name = va_arg (args, const gchar *)) {
144 sig = va_arg (args, const gchar *);
145 callback = va_arg (args, GCallback);
147 object = gtk_builder_get_object (gui, name);
149 g_warning ("File is missing object '%s'.", name);
153 g_signal_connect (object, sig, callback, user_data);
160 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
163 /* On construction gui sinks the initial reference to widget. When gui
164 * is finalized it will drop its ref to widget. We take our own ref to
165 * prevent widget being finalised. The widget is forced to have a
166 * floating reference, like when it was initially unowned so that it can
167 * be used like any other GtkWidget. */
169 g_object_ref (widget);
170 g_object_force_floating (G_OBJECT (widget));
171 g_object_unref (gui);
177 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
180 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
181 return EMPATHY_IMAGE_AVAILABLE;
182 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
183 return EMPATHY_IMAGE_BUSY;
184 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
185 return EMPATHY_IMAGE_AWAY;
186 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
187 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
188 EMPATHY_IMAGE_EXT_AWAY))
189 return EMPATHY_IMAGE_EXT_AWAY;
191 /* The 'extended-away' icon is not an official one so we fallback to idle if
192 * it's not implemented */
193 return EMPATHY_IMAGE_IDLE;
194 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
195 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
196 EMPATHY_IMAGE_HIDDEN))
197 return EMPATHY_IMAGE_HIDDEN;
199 /* The 'hidden' icon is not an official one so we fallback to offline if
200 * it's not implemented */
201 return EMPATHY_IMAGE_OFFLINE;
202 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
203 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
204 return EMPATHY_IMAGE_OFFLINE;
205 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
206 return EMPATHY_IMAGE_PENDING;
207 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
215 empathy_icon_name_for_contact (EmpathyContact *contact)
217 TpConnectionPresenceType presence;
219 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
220 EMPATHY_IMAGE_OFFLINE);
222 presence = empathy_contact_get_presence (contact);
223 return empathy_icon_name_for_presence (presence);
227 empathy_icon_name_for_individual (FolksIndividual *individual)
229 FolksPresenceType folks_presence;
230 TpConnectionPresenceType presence;
232 folks_presence = folks_individual_get_presence_type (individual);
233 presence = empathy_folks_presence_type_to_tp (folks_presence);
235 return empathy_icon_name_for_presence (presence);
239 empathy_protocol_name_for_contact (EmpathyContact *contact)
243 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
245 account = empathy_contact_get_account (contact);
246 if (account == NULL) {
250 return tp_account_get_icon_name (account);
254 empathy_pixbuf_from_data (gchar *data,
257 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
261 empathy_pixbuf_from_data_and_mime (gchar *data,
265 GdkPixbufLoader *loader;
266 GdkPixbufFormat *format;
267 GdkPixbuf *pixbuf = NULL;
269 GError *error = NULL;
275 loader = gdk_pixbuf_loader_new ();
276 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
277 DEBUG ("Failed to write to pixbuf loader: %s",
278 error ? error->message : "No error given");
281 if (!gdk_pixbuf_loader_close (loader, &error)) {
282 DEBUG ("Failed to close pixbuf loader: %s",
283 error ? error->message : "No error given");
287 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
289 g_object_ref (pixbuf);
291 if (mime_type != NULL) {
292 format = gdk_pixbuf_loader_get_format (loader);
293 mime_types = gdk_pixbuf_format_get_mime_types (format);
295 *mime_type = g_strdup (*mime_types);
296 if (mime_types[1] != NULL) {
297 DEBUG ("Loader supports more than one mime "
298 "type! Picking the first one, %s",
301 g_strfreev (mime_types);
306 g_clear_error (&error);
307 g_object_unref (loader);
315 gboolean preserve_aspect_ratio;
319 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
322 struct SizeData *data)
324 g_return_if_fail (width > 0 && height > 0);
326 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
327 if (width < data->width && height < data->height) {
332 if (data->width < 0) {
333 width = width * (double) data->height / (gdouble) height;
334 height = data->height;
335 } else if (data->height < 0) {
336 height = height * (double) data->width / (double) width;
338 } else if ((double) height * (double) data->width >
339 (double) width * (double) data->height) {
340 width = 0.5 + (double) width * (double) data->height / (double) height;
341 height = data->height;
343 height = 0.5 + (double) height * (double) data->width / (double) width;
347 if (data->width > 0) {
351 if (data->height > 0) {
352 height = data->height;
356 gdk_pixbuf_loader_set_size (loader, width, height);
360 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
362 gint width, height, rowstride;
365 width = gdk_pixbuf_get_width (pixbuf);
366 height = gdk_pixbuf_get_height (pixbuf);
367 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
368 pixels = gdk_pixbuf_get_pixels (pixbuf);
370 if (width < 6 || height < 6) {
378 pixels[rowstride + 3] = 0x80;
379 pixels[rowstride * 2 + 3] = 0xC0;
382 pixels[width * 4 - 1] = 0;
383 pixels[width * 4 - 5] = 0x80;
384 pixels[width * 4 - 9] = 0xC0;
385 pixels[rowstride + (width * 4) - 1] = 0x80;
386 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
389 pixels[(height - 1) * rowstride + 3] = 0;
390 pixels[(height - 1) * rowstride + 7] = 0x80;
391 pixels[(height - 1) * rowstride + 11] = 0xC0;
392 pixels[(height - 2) * rowstride + 3] = 0x80;
393 pixels[(height - 3) * rowstride + 3] = 0xC0;
396 pixels[height * rowstride - 1] = 0;
397 pixels[(height - 1) * rowstride - 1] = 0x80;
398 pixels[(height - 2) * rowstride - 1] = 0xC0;
399 pixels[height * rowstride - 5] = 0x80;
400 pixels[height * rowstride - 9] = 0xC0;
404 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
406 gint width, height, rowstride, i;
410 width = gdk_pixbuf_get_width (pixbuf);
411 height = gdk_pixbuf_get_height (pixbuf);
412 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
413 pixels = gdk_pixbuf_get_pixels (pixbuf);
416 for (i = 3; i < rowstride; i+=4) {
422 for (i = 1; i < height - 1; i++) {
423 row = pixels + (i*rowstride);
424 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
429 row = pixels + ((height-1) * rowstride);
430 for (i = 3; i < rowstride; i+=4) {
440 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
445 GdkPixbufLoader *loader;
446 struct SizeData data;
447 GError *error = NULL;
454 data.height = height;
455 data.preserve_aspect_ratio = TRUE;
457 loader = gdk_pixbuf_loader_new ();
459 g_signal_connect (loader, "size-prepared",
460 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
463 if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error)) {
464 g_warning ("Couldn't write avatar image:%p with "
465 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
466 avatar->data, avatar->len, error->message);
467 g_error_free (error);
471 gdk_pixbuf_loader_close (loader, NULL);
473 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
474 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
475 GdkPixbuf *rounded_pixbuf;
477 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
478 gdk_pixbuf_get_width (pixbuf),
479 gdk_pixbuf_get_height (pixbuf));
480 gdk_pixbuf_copy_area (pixbuf, 0, 0,
481 gdk_pixbuf_get_width (pixbuf),
482 gdk_pixbuf_get_height (pixbuf),
485 pixbuf = rounded_pixbuf;
487 g_object_ref (pixbuf);
490 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
491 empathy_avatar_pixbuf_roundify (pixbuf);
494 g_object_unref (loader);
500 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
504 EmpathyAvatar *avatar;
506 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
508 avatar = empathy_contact_get_avatar (contact);
510 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
514 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
515 gboolean show_protocol)
517 const gchar *icon_name;
519 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
521 icon_name = empathy_icon_name_for_contact (contact);
523 if (icon_name == NULL) {
526 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
532 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
533 const gchar *icon_name,
534 gboolean show_protocol)
536 GdkPixbuf *pix_status;
537 GdkPixbuf *pix_protocol;
538 gchar *icon_filename;
540 gint numerator, denominator;
542 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
543 (show_protocol == FALSE), NULL);
544 g_return_val_if_fail (icon_name != NULL, NULL);
549 icon_filename = empathy_filename_from_icon_name (icon_name,
551 if (icon_filename == NULL) {
552 DEBUG ("icon name: %s could not be found\n", icon_name);
556 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
558 if (pix_status == NULL) {
559 DEBUG ("Could not open icon %s\n", icon_filename);
560 g_free (icon_filename);
564 g_free (icon_filename);
569 height = gdk_pixbuf_get_height (pix_status);
570 width = gdk_pixbuf_get_width (pix_status);
572 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
573 width * numerator / denominator,
574 height * numerator / denominator);
576 if (pix_protocol == NULL) {
579 gdk_pixbuf_composite (pix_protocol, pix_status,
580 0, height - height * numerator / denominator,
581 width * numerator / denominator, height * numerator / denominator,
582 0, height - height * numerator / denominator,
584 GDK_INTERP_BILINEAR, 255);
586 g_object_unref (pix_protocol);
592 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
598 GdkPixbuf *pixbuf = NULL;
600 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
602 account = empathy_contact_get_account (contact);
603 filename = empathy_filename_from_icon_name (tp_account_get_icon_name (account),
605 if (filename != NULL) {
606 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
614 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
619 width = gdk_pixbuf_get_width (pixbuf);
620 height = gdk_pixbuf_get_height (pixbuf);
622 if (width > 0 && (width > max_size || height > max_size)) {
623 factor = (gdouble) max_size / MAX (width, height);
625 width = width * factor;
626 height = height * factor;
628 return gdk_pixbuf_scale_simple (pixbuf,
633 return g_object_ref (pixbuf);
637 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
642 GError *error = NULL;
648 theme = gtk_icon_theme_get_default ();
650 pixbuf = gtk_icon_theme_load_icon (theme,
656 DEBUG ("Error loading icon: %s", error->message);
657 g_clear_error (&error);
664 empathy_pixbuf_from_icon_name (const gchar *icon_name,
665 GtkIconSize icon_size)
674 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
678 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
682 empathy_filename_from_icon_name (const gchar *icon_name,
683 GtkIconSize icon_size)
685 GtkIconTheme *icon_theme;
686 GtkIconInfo *icon_info;
691 icon_theme = gtk_icon_theme_get_default ();
693 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
697 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
698 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
699 gtk_icon_info_free (icon_info);
704 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
705 * that to make it easier to apply changes from the original code.
707 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
709 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
710 * decomposable character it consumes the decomposition length from the given
711 * offset. So it's useful when the offset was calculated for the normalized
712 * version of str, but we need a pointer to str itself. */
714 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
716 gchar *casefold, *normal;
722 q = g_utf8_next_char (p);
723 casefold = g_utf8_casefold (p, q - p);
724 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
725 offset -= g_utf8_strlen (normal, -1);
734 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
738 const gchar *ret = NULL;
741 gchar *caseless_haystack;
744 g_return_val_if_fail (haystack != NULL, NULL);
745 g_return_val_if_fail (needle != NULL, NULL);
747 casefold = g_utf8_casefold (haystack, -1);
748 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
751 needle_len = g_utf8_strlen (needle, -1);
752 haystack_len = g_utf8_strlen (caseless_haystack, -1);
756 ret = (gchar *) haystack;
760 if (haystack_len < needle_len)
766 p = (gchar *) caseless_haystack;
767 needle_len = strlen (needle);
772 if ((strncmp (p, needle, needle_len) == 0))
774 ret = pointer_from_offset_skipping_decomp (haystack, i);
778 p = g_utf8_next_char (p);
783 g_free (caseless_haystack);
789 g_utf8_caselessnmatch (const char *s1, const char *s2,
790 gssize n1, gssize n2)
793 gchar *normalized_s1;
794 gchar *normalized_s2;
797 gboolean ret = FALSE;
799 g_return_val_if_fail (s1 != NULL, FALSE);
800 g_return_val_if_fail (s2 != NULL, FALSE);
801 g_return_val_if_fail (n1 > 0, FALSE);
802 g_return_val_if_fail (n2 > 0, FALSE);
804 casefold = g_utf8_casefold (s1, n1);
805 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
808 casefold = g_utf8_casefold (s2, n2);
809 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
812 len_s1 = strlen (normalized_s1);
813 len_s2 = strlen (normalized_s2);
818 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
821 g_free (normalized_s1);
822 g_free (normalized_s2);
828 forward_chars_with_skipping (GtkTextIter *iter,
830 gboolean skip_invisible,
831 gboolean skip_nontext,
832 gboolean skip_decomp)
836 g_return_if_fail (count >= 0);
842 gboolean ignored = FALSE;
844 /* minimal workaround to avoid the infinite loop of bug #168247.
845 * It doesn't fix the problemjust the symptom...
847 if (gtk_text_iter_is_end (iter))
850 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
853 if (!ignored && skip_invisible &&
854 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
857 if (!ignored && skip_decomp)
859 /* being UTF8 correct sucks; this accounts for extra
860 offsets coming from canonical decompositions of
861 UTF8 characters (e.g. accented characters) which
862 g_utf8_normalize () performs */
867 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
868 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
869 i -= (g_utf8_strlen (normal, -1) - 1);
873 gtk_text_iter_forward_char (iter);
881 lines_match (const GtkTextIter *start,
883 gboolean visible_only,
885 GtkTextIter *match_start,
886 GtkTextIter *match_end)
893 if (*lines == NULL || **lines == '\0')
896 *match_start = *start;
903 gtk_text_iter_forward_line (&next);
905 /* No more text in buffer, but *lines is nonempty */
906 if (gtk_text_iter_equal (start, &next))
912 line_text = gtk_text_iter_get_visible_slice (start, &next);
914 line_text = gtk_text_iter_get_slice (start, &next);
919 line_text = gtk_text_iter_get_visible_text (start, &next);
921 line_text = gtk_text_iter_get_text (start, &next);
924 if (match_start) /* if this is the first line we're matching */
926 found = g_utf8_strcasestr (line_text, *lines);
930 /* If it's not the first line, we have to match from the
933 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
946 /* Get offset to start of search string */
947 offset = g_utf8_strlen (line_text, found - line_text);
951 /* If match start needs to be returned, set it to the
952 * start of the search string.
954 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
960 /* Go to end of search string */
961 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
970 /* pass NULL for match_start, since we don't need to find the
973 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
976 /* strsplit () that retains the delimiter as part of the string. */
978 strbreakup (const char *string,
979 const char *delimiter,
982 GSList *string_list = NULL, *slist;
983 gchar **str_array, *s, *casefold, *new_string;
986 g_return_val_if_fail (string != NULL, NULL);
987 g_return_val_if_fail (delimiter != NULL, NULL);
990 max_tokens = G_MAXINT;
992 s = strstr (string, delimiter);
995 guint delimiter_len = strlen (delimiter);
1001 len = s - string + delimiter_len;
1002 new_string = g_new (gchar, len + 1);
1003 strncpy (new_string, string, len);
1004 new_string[len] = 0;
1005 casefold = g_utf8_casefold (new_string, -1);
1006 g_free (new_string);
1007 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1009 string_list = g_slist_prepend (string_list, new_string);
1011 string = s + delimiter_len;
1012 s = strstr (string, delimiter);
1013 } while (--max_tokens && s);
1019 casefold = g_utf8_casefold (string, -1);
1020 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1022 string_list = g_slist_prepend (string_list, new_string);
1025 str_array = g_new (gchar*, n);
1029 str_array[i--] = NULL;
1030 for (slist = string_list; slist; slist = slist->next)
1031 str_array[i--] = slist->data;
1033 g_slist_free (string_list);
1039 empathy_text_iter_forward_search (const GtkTextIter *iter,
1041 GtkTextIter *match_start,
1042 GtkTextIter *match_end,
1043 const GtkTextIter *limit)
1045 gchar **lines = NULL;
1047 gboolean retval = FALSE;
1049 gboolean visible_only;
1052 g_return_val_if_fail (iter != NULL, FALSE);
1053 g_return_val_if_fail (str != NULL, FALSE);
1055 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
1059 /* If we can move one char, return the empty string there */
1062 if (gtk_text_iter_forward_char (&match)) {
1063 if (limit && gtk_text_iter_equal (&match, limit)) {
1068 *match_start = match;
1079 visible_only = TRUE;
1082 /* locate all lines */
1083 lines = strbreakup (str, "\n", -1);
1088 /* This loop has an inefficient worst-case, where
1089 * gtk_text_iter_get_text () is called repeatedly on
1094 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1098 if (lines_match (&search, (const gchar**)lines,
1099 visible_only, slice, &match, &end)) {
1100 if (limit == NULL ||
1101 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1105 *match_start = match;
1113 } while (gtk_text_iter_forward_line (&search));
1115 g_strfreev ((gchar **) lines);
1120 static const gchar *
1121 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1125 const gchar *ret = NULL;
1128 gchar *caseless_haystack;
1131 g_return_val_if_fail (haystack != NULL, NULL);
1132 g_return_val_if_fail (needle != NULL, NULL);
1134 casefold = g_utf8_casefold (haystack, -1);
1135 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1138 needle_len = g_utf8_strlen (needle, -1);
1139 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1141 if (needle_len == 0)
1143 ret = (gchar *) haystack;
1147 if (haystack_len < needle_len)
1153 i = haystack_len - needle_len;
1154 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1155 needle_len = strlen (needle);
1157 while (p >= caseless_haystack)
1159 if (strncmp (p, needle, needle_len) == 0)
1161 ret = pointer_from_offset_skipping_decomp (haystack, i);
1165 p = g_utf8_prev_char (p);
1170 g_free (caseless_haystack);
1176 backward_lines_match (const GtkTextIter *start,
1177 const gchar **lines,
1178 gboolean visible_only,
1180 GtkTextIter *match_start,
1181 GtkTextIter *match_end)
1183 GtkTextIter line, next;
1188 if (*lines == NULL || **lines == '\0')
1191 *match_start = *start;
1193 *match_end = *start;
1197 line = next = *start;
1198 if (gtk_text_iter_get_line_offset (&next) == 0)
1200 if (!gtk_text_iter_backward_line (&next))
1204 gtk_text_iter_set_line_offset (&next, 0);
1209 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1211 line_text = gtk_text_iter_get_slice (&next, &line);
1216 line_text = gtk_text_iter_get_visible_text (&next, &line);
1218 line_text = gtk_text_iter_get_text (&next, &line);
1221 if (match_start) /* if this is the first line we're matching */
1223 found = g_utf8_strrcasestr (line_text, *lines);
1227 /* If it's not the first line, we have to match from the
1228 * start of the line.
1230 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1243 /* Get offset to start of search string */
1244 offset = g_utf8_strlen (line_text, found - line_text);
1246 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1248 /* If match start needs to be returned, set it to the
1249 * start of the search string.
1253 *match_start = next;
1256 /* Go to end of search string */
1257 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1266 /* try to match the rest of the lines forward, passing NULL
1267 * for match_start so lines_match will try to match the entire
1269 return lines_match (&next, lines, visible_only,
1270 slice, NULL, match_end);
1274 empathy_text_iter_backward_search (const GtkTextIter *iter,
1276 GtkTextIter *match_start,
1277 GtkTextIter *match_end,
1278 const GtkTextIter *limit)
1280 gchar **lines = NULL;
1282 gboolean retval = FALSE;
1284 gboolean visible_only;
1287 g_return_val_if_fail (iter != NULL, FALSE);
1288 g_return_val_if_fail (str != NULL, FALSE);
1290 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1295 /* If we can move one char, return the empty string there */
1298 if (gtk_text_iter_backward_char (&match))
1300 if (limit && gtk_text_iter_equal (&match, limit))
1304 *match_start = match;
1315 visible_only = TRUE;
1318 /* locate all lines */
1319 lines = strbreakup (str, "\n", -1);
1325 /* This loop has an inefficient worst-case, where
1326 * gtk_text_iter_get_text () is called repeatedly on
1331 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1334 if (backward_lines_match (&search, (const gchar**)lines,
1335 visible_only, slice, &match, &end))
1337 if (limit == NULL || (limit &&
1338 gtk_text_iter_compare (&end, limit) > 0))
1343 *match_start = match;
1350 if (gtk_text_iter_get_line_offset (&search) == 0)
1352 if (!gtk_text_iter_backward_line (&search))
1357 gtk_text_iter_set_line_offset (&search, 0);
1361 g_strfreev ((gchar **) lines);
1367 empathy_window_get_is_visible (GtkWindow *window)
1369 GdkWindowState state;
1370 GdkWindow *gdk_window;
1372 g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);
1374 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1379 state = gdk_window_get_state (gdk_window);
1380 if (state & (GDK_WINDOW_STATE_WITHDRAWN | GDK_WINDOW_STATE_ICONIFIED)) {
1388 empathy_window_iconify (GtkWindow *window, GtkStatusIcon *status_icon)
1390 GdkRectangle icon_location;
1393 GdkWindow *gdk_window;
1395 gtk_status_icon_get_geometry (status_icon, NULL, &icon_location, NULL);
1396 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1397 dpy = gdk_x11_drawable_get_xdisplay (gdk_window);
1399 data[0] = icon_location.x;
1400 data[1] = icon_location.y;
1401 data[2] = icon_location.width;
1402 data[3] = icon_location.height;
1404 XChangeProperty (dpy,
1405 GDK_WINDOW_XID (gdk_window),
1406 gdk_x11_get_xatom_by_name_for_display (
1407 gdk_drawable_get_display (gdk_window),
1408 "_NET_WM_ICON_GEOMETRY"),
1409 XA_CARDINAL, 32, PropModeReplace,
1410 (guchar *)&data, 4);
1412 gtk_window_set_skip_taskbar_hint (window, TRUE);
1413 gtk_window_iconify (window);
1416 /* Takes care of moving the window to the current workspace. */
1418 empathy_window_present_with_time (GtkWindow *window,
1421 GdkWindow *gdk_window;
1423 g_return_if_fail (GTK_IS_WINDOW (window));
1425 /* Move the window to the current workspace before trying to show it.
1426 * This is the behaviour people expect when clicking on the statusbar icon. */
1427 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1432 /* Has no effect if the WM has viewports, like compiz */
1433 gdk_x11_window_move_to_current_desktop (gdk_window);
1435 /* If window is still off-screen, hide it to force it to
1436 * reposition on the current workspace. */
1437 gtk_window_get_position (window, &x, &y);
1438 gtk_window_get_size (window, &w, &h);
1439 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1440 gtk_widget_hide (GTK_WIDGET (window));
1443 gtk_window_present_with_time (window, timestamp);
1444 gtk_window_set_skip_taskbar_hint (window, FALSE);
1445 gtk_window_deiconify (window);
1449 empathy_window_present (GtkWindow *window)
1451 empathy_window_present_with_time (window, GDK_CURRENT_TIME);
1455 empathy_get_toplevel_window (GtkWidget *widget)
1457 GtkWidget *toplevel;
1459 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1461 toplevel = gtk_widget_get_toplevel (widget);
1462 if (GTK_IS_WINDOW (toplevel) &&
1463 gtk_widget_is_toplevel (toplevel)) {
1464 return GTK_WINDOW (toplevel);
1470 /** empathy_make_absolute_url_len:
1474 * Same as #empathy_make_absolute_url but for a limited string length
1477 empathy_make_absolute_url_len (const gchar *url,
1480 g_return_val_if_fail (url != NULL, NULL);
1482 if (g_str_has_prefix (url, "ghelp:") ||
1483 g_str_has_prefix (url, "mailto:") ||
1484 strstr (url, ":/")) {
1485 return g_strndup (url, len);
1488 if (strstr (url, "@")) {
1489 return g_strdup_printf ("mailto:%.*s", len, url);
1492 return g_strdup_printf ("http://%.*s", len, url);
1495 /** empathy_make_absolute_url:
1498 * The URL opening code can't handle schemeless strings, so we try to be
1499 * smart and add http if there is no scheme or doesn't look like a mail
1500 * address. This should work in most cases, and let us click on strings
1501 * like "www.gnome.org".
1503 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1504 * g_free when your are done with it
1507 empathy_make_absolute_url (const gchar *url)
1509 return empathy_make_absolute_url_len (url, strlen (url));
1513 empathy_url_show (GtkWidget *parent,
1517 GError *error = NULL;
1519 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1520 g_return_if_fail (url != NULL);
1522 real_url = empathy_make_absolute_url (url);
1524 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1525 gtk_get_current_event_time (), &error);
1530 dialog = gtk_message_dialog_new (NULL, 0,
1531 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1532 _("Unable to open URI"));
1533 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1534 "%s", error->message);
1536 g_signal_connect (dialog, "response",
1537 G_CALLBACK (gtk_widget_destroy),
1539 gtk_window_present (GTK_WINDOW (dialog));
1541 g_clear_error (&error);
1548 empathy_send_file (EmpathyContact *contact, GFile *file)
1550 EmpathyFTFactory *factory;
1551 GtkRecentManager *manager;
1554 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1555 g_return_if_fail (G_IS_FILE (file));
1557 factory = empathy_ft_factory_dup_singleton ();
1559 empathy_ft_factory_new_transfer_outgoing (factory, contact, file);
1561 uri = g_file_get_uri (file);
1562 manager = gtk_recent_manager_get_default ();
1563 gtk_recent_manager_add_item (manager, uri);
1566 g_object_unref (factory);
1570 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1575 /* Only handle a single file for now. It would be wicked cool to be
1576 able to do multiple files, offering to zip them or whatever like
1577 nautilus-sendto does. Note that text/uri-list is defined to have
1578 each line terminated by \r\n, but we can be tolerant of applications
1579 that only use \n or don't terminate single-line entries.
1581 nl = strstr (uri_list, "\r\n");
1583 nl = strchr (uri_list, '\n');
1586 gchar *uri = g_strndup (uri_list, nl - uri_list);
1587 file = g_file_new_for_uri (uri);
1591 file = g_file_new_for_uri (uri_list);
1594 empathy_send_file (contact, file);
1596 g_object_unref (file);
1600 file_manager_send_file_response_cb (GtkDialog *widget,
1602 EmpathyContact *contact)
1606 if (response_id == GTK_RESPONSE_OK) {
1607 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1609 empathy_send_file (contact, file);
1611 g_object_unref (file);
1614 gtk_widget_destroy (GTK_WIDGET (widget));
1618 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1623 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1625 DEBUG ("Creating selection file chooser");
1627 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1629 GTK_FILE_CHOOSER_ACTION_OPEN,
1631 GTK_RESPONSE_CANCEL,
1635 button = gtk_button_new_with_mnemonic (_("_Send"));
1636 gtk_button_set_image (GTK_BUTTON (button),
1637 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1638 GTK_ICON_SIZE_BUTTON));
1639 gtk_widget_show (button);
1640 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1642 gtk_widget_set_can_default (button, TRUE);
1643 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1646 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1648 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1651 g_signal_connect (widget, "response",
1652 G_CALLBACK (file_manager_send_file_response_cb),
1655 gtk_widget_show (widget);
1659 file_manager_receive_file_response_cb (GtkDialog *dialog,
1660 GtkResponseType response,
1661 EmpathyFTHandler *handler)
1663 EmpathyFTFactory *factory;
1666 if (response == GTK_RESPONSE_OK) {
1667 factory = empathy_ft_factory_dup_singleton ();
1668 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1670 empathy_ft_factory_set_destination_for_incoming_handler
1671 (factory, handler, file);
1673 g_object_unref (factory);
1674 g_object_unref (file);
1676 /* unref the handler, as we dismissed the file chooser,
1677 * and refused the transfer.
1679 g_object_unref (handler);
1682 gtk_widget_destroy (GTK_WIDGET (dialog));
1686 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1690 EmpathyContact *contact;
1693 contact = empathy_ft_handler_get_contact (handler);
1694 g_assert (contact != NULL);
1696 title = g_strdup_printf (_("Incoming file from %s"),
1697 empathy_contact_get_name (contact));
1699 widget = gtk_file_chooser_dialog_new (title,
1701 GTK_FILE_CHOOSER_ACTION_SAVE,
1703 GTK_RESPONSE_CANCEL,
1707 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1708 empathy_ft_handler_get_filename (handler));
1709 gtk_file_chooser_set_do_overwrite_confirmation
1710 (GTK_FILE_CHOOSER (widget), TRUE);
1712 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1714 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1715 dir = g_get_home_dir ();
1717 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1719 g_signal_connect (widget, "response",
1720 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1722 gtk_widget_show (widget);