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 <telepathy-glib/util.h>
43 #include <folks/folks.h>
45 #include "empathy-ui-utils.h"
46 #include "empathy-images.h"
47 #include "empathy-live-search.h"
48 #include "empathy-smiley-manager.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
51 #include <libempathy/empathy-debug.h>
52 #include <libempathy/empathy-utils.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,
133 const gchar *first_object,
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:
216 empathy_icon_name_for_contact (EmpathyContact *contact)
218 TpConnectionPresenceType presence;
220 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
221 EMPATHY_IMAGE_OFFLINE);
223 presence = empathy_contact_get_presence (contact);
224 return empathy_icon_name_for_presence (presence);
228 empathy_icon_name_for_individual (FolksIndividual *individual)
230 FolksPresenceType folks_presence;
231 TpConnectionPresenceType presence;
234 folks_presence_details_get_presence_type (
235 FOLKS_PRESENCE_DETAILS (individual));
236 presence = empathy_folks_presence_type_to_tp (folks_presence);
238 return empathy_icon_name_for_presence (presence);
242 empathy_protocol_name_for_contact (EmpathyContact *contact)
246 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
248 account = empathy_contact_get_account (contact);
249 if (account == NULL) {
253 return tp_account_get_icon_name (account);
257 empathy_pixbuf_from_data (gchar *data,
260 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
264 empathy_pixbuf_from_data_and_mime (gchar *data,
268 GdkPixbufLoader *loader;
269 GdkPixbufFormat *format;
270 GdkPixbuf *pixbuf = NULL;
272 GError *error = NULL;
278 loader = gdk_pixbuf_loader_new ();
279 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error)) {
280 DEBUG ("Failed to write to pixbuf loader: %s",
281 error ? error->message : "No error given");
284 if (!gdk_pixbuf_loader_close (loader, &error)) {
285 DEBUG ("Failed to close pixbuf loader: %s",
286 error ? error->message : "No error given");
290 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
292 g_object_ref (pixbuf);
294 if (mime_type != NULL) {
295 format = gdk_pixbuf_loader_get_format (loader);
296 mime_types = gdk_pixbuf_format_get_mime_types (format);
298 *mime_type = g_strdup (*mime_types);
299 if (mime_types[1] != NULL) {
300 DEBUG ("Loader supports more than one mime "
301 "type! Picking the first one, %s",
304 g_strfreev (mime_types);
309 g_clear_error (&error);
310 g_object_unref (loader);
318 gboolean preserve_aspect_ratio;
322 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
325 struct SizeData *data)
327 g_return_if_fail (width > 0 && height > 0);
329 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0)) {
330 if (width < data->width && height < data->height) {
335 if (data->width < 0) {
336 width = width * (double) data->height / (gdouble) height;
337 height = data->height;
338 } else if (data->height < 0) {
339 height = height * (double) data->width / (double) width;
341 } else if ((double) height * (double) data->width >
342 (double) width * (double) data->height) {
343 width = 0.5 + (double) width * (double) data->height / (double) height;
344 height = data->height;
346 height = 0.5 + (double) height * (double) data->width / (double) width;
350 if (data->width > 0) {
354 if (data->height > 0) {
355 height = data->height;
359 gdk_pixbuf_loader_set_size (loader, width, height);
363 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
365 gint width, height, rowstride;
368 width = gdk_pixbuf_get_width (pixbuf);
369 height = gdk_pixbuf_get_height (pixbuf);
370 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
371 pixels = gdk_pixbuf_get_pixels (pixbuf);
373 if (width < 6 || height < 6) {
381 pixels[rowstride + 3] = 0x80;
382 pixels[rowstride * 2 + 3] = 0xC0;
385 pixels[width * 4 - 1] = 0;
386 pixels[width * 4 - 5] = 0x80;
387 pixels[width * 4 - 9] = 0xC0;
388 pixels[rowstride + (width * 4) - 1] = 0x80;
389 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
392 pixels[(height - 1) * rowstride + 3] = 0;
393 pixels[(height - 1) * rowstride + 7] = 0x80;
394 pixels[(height - 1) * rowstride + 11] = 0xC0;
395 pixels[(height - 2) * rowstride + 3] = 0x80;
396 pixels[(height - 3) * rowstride + 3] = 0xC0;
399 pixels[height * rowstride - 1] = 0;
400 pixels[(height - 1) * rowstride - 1] = 0x80;
401 pixels[(height - 2) * rowstride - 1] = 0xC0;
402 pixels[height * rowstride - 5] = 0x80;
403 pixels[height * rowstride - 9] = 0xC0;
407 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
409 gint height, rowstride, i;
413 height = gdk_pixbuf_get_height (pixbuf);
414 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
415 pixels = gdk_pixbuf_get_pixels (pixbuf);
418 for (i = 3; i < rowstride; i+=4) {
424 for (i = 1; i < height - 1; i++) {
425 row = pixels + (i*rowstride);
426 if (row[3] < 0xfe || row[rowstride-1] < 0xfe) {
431 row = pixels + ((height-1) * rowstride);
432 for (i = 3; i < rowstride; i+=4) {
442 avatar_pixbuf_from_loader (GdkPixbufLoader *loader)
446 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
447 if (!gdk_pixbuf_get_has_alpha (pixbuf)) {
448 GdkPixbuf *rounded_pixbuf;
450 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
451 gdk_pixbuf_get_width (pixbuf),
452 gdk_pixbuf_get_height (pixbuf));
453 gdk_pixbuf_copy_area (pixbuf, 0, 0,
454 gdk_pixbuf_get_width (pixbuf),
455 gdk_pixbuf_get_height (pixbuf),
458 pixbuf = rounded_pixbuf;
460 g_object_ref (pixbuf);
463 if (empathy_gdk_pixbuf_is_opaque (pixbuf)) {
464 empathy_avatar_pixbuf_roundify (pixbuf);
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);
503 pixbuf = avatar_pixbuf_from_loader (loader);
505 g_object_unref (loader);
511 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
515 EmpathyAvatar *avatar;
517 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
519 avatar = empathy_contact_get_avatar (contact);
521 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
525 FolksIndividual *individual;
526 GSimpleAsyncResult *result;
529 struct SizeData size_data;
530 GdkPixbufLoader *loader;
531 GCancellable *cancellable;
533 } PixbufAvatarFromIndividualClosure;
535 static PixbufAvatarFromIndividualClosure *
536 pixbuf_avatar_from_individual_closure_new (FolksIndividual *individual,
537 GSimpleAsyncResult *result,
540 GCancellable *cancellable)
542 PixbufAvatarFromIndividualClosure *closure;
544 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
545 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
547 closure = g_new0 (PixbufAvatarFromIndividualClosure, 1);
548 closure->individual = g_object_ref (individual);
549 closure->result = g_object_ref (result);
550 closure->width = width;
551 closure->height = height;
552 closure->cancellable = g_object_ref (cancellable);
558 pixbuf_avatar_from_individual_closure_free (
559 PixbufAvatarFromIndividualClosure *closure)
561 g_object_unref (closure->cancellable);
562 tp_clear_object (&closure->loader);
563 g_object_unref (closure->individual);
564 g_object_unref (closure->result);
569 avatar_icon_load_close_cb (GObject *object,
570 GAsyncResult *result,
573 GError *error = NULL;
575 g_input_stream_close_finish (G_INPUT_STREAM (object), result, &error);
578 DEBUG ("Failed to close pixbuf stream: %s", error->message);
579 g_error_free (error);
584 avatar_icon_load_read_cb (GObject *object,
585 GAsyncResult *result,
588 GInputStream *stream = G_INPUT_STREAM (object);
589 PixbufAvatarFromIndividualClosure *closure = user_data;
591 GError *error = NULL;
593 /* Finish reading this chunk from the stream */
594 n_read = g_input_stream_read_finish (stream, result, &error);
596 DEBUG ("Failed to finish read from pixbuf stream: %s",
598 g_simple_async_result_set_from_error (closure->result, error);
602 /* Write the chunk to the pixbuf loader */
603 if (!gdk_pixbuf_loader_write (closure->loader, (guchar *) closure->data,
605 DEBUG ("Failed to write to pixbuf loader: %s",
606 error ? error->message : "No error given");
607 g_simple_async_result_set_from_error (closure->result, error);
613 if (!gdk_pixbuf_loader_close (closure->loader, &error)) {
614 DEBUG ("Failed to close pixbuf loader: %s",
615 error ? error->message : "No error given");
616 g_simple_async_result_set_from_error (closure->result, error);
621 g_simple_async_result_set_op_res_gpointer (closure->result,
622 avatar_pixbuf_from_loader (closure->loader),
627 /* Loop round and read another chunk. */
628 g_input_stream_read_async (stream, closure->data,
629 G_N_ELEMENTS (closure->data),
630 G_PRIORITY_DEFAULT, closure->cancellable,
631 avatar_icon_load_read_cb, closure);
637 /* We must close the pixbuf loader before unreffing it. */
638 gdk_pixbuf_loader_close (closure->loader, NULL);
641 /* Close the file for safety (even though it should be
642 * automatically closed when the stream is finalised). */
643 g_input_stream_close_async (stream, G_PRIORITY_DEFAULT, NULL,
644 (GAsyncReadyCallback) avatar_icon_load_close_cb, NULL);
646 g_simple_async_result_complete (closure->result);
648 g_clear_error (&error);
649 pixbuf_avatar_from_individual_closure_free (closure);
653 avatar_icon_load_cb (GObject *object,
654 GAsyncResult *result,
657 GLoadableIcon *icon = G_LOADABLE_ICON (object);
658 PixbufAvatarFromIndividualClosure *closure = user_data;
659 GInputStream *stream;
660 GError *error = NULL;
662 stream = g_loadable_icon_load_finish (icon, result, NULL, &error);
664 DEBUG ("Failed to open avatar stream: %s", error->message);
665 g_simple_async_result_set_from_error (closure->result, error);
669 closure->size_data.width = closure->width;
670 closure->size_data.height = closure->height;
671 closure->size_data.preserve_aspect_ratio = TRUE;
673 /* Load the data into a pixbuf loader in chunks. */
674 closure->loader = gdk_pixbuf_loader_new ();
676 g_signal_connect (closure->loader, "size-prepared",
677 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
678 &(closure->size_data));
680 /* Begin to read the first chunk. */
681 g_input_stream_read_async (stream, closure->data,
682 G_N_ELEMENTS (closure->data),
683 G_PRIORITY_DEFAULT, closure->cancellable,
684 avatar_icon_load_read_cb, closure);
686 g_object_unref (stream);
691 g_simple_async_result_complete (closure->result);
693 g_clear_error (&error);
694 tp_clear_object (&stream);
695 pixbuf_avatar_from_individual_closure_free (closure);
699 empathy_pixbuf_avatar_from_individual_scaled_async (
700 FolksIndividual *individual,
703 GCancellable *cancellable,
704 GAsyncReadyCallback callback,
707 GLoadableIcon *avatar_icon;
708 GSimpleAsyncResult *result;
709 PixbufAvatarFromIndividualClosure *closure;
711 result = g_simple_async_result_new (G_OBJECT (individual),
713 empathy_pixbuf_avatar_from_individual_scaled_async);
716 folks_avatar_details_get_avatar (FOLKS_AVATAR_DETAILS (individual));
717 if (avatar_icon == NULL)
720 closure = pixbuf_avatar_from_individual_closure_new (individual, result,
726 g_loadable_icon_load_async (avatar_icon, width, cancellable,
727 avatar_icon_load_cb, closure);
729 g_object_unref (result);
734 g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
735 g_simple_async_result_complete (result);
736 g_object_unref (result);
739 /* Return a ref on the GdkPixbuf */
741 empathy_pixbuf_avatar_from_individual_scaled_finish (
742 FolksIndividual *individual,
743 GAsyncResult *result,
746 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
747 gboolean result_valid;
750 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
751 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
753 if (g_simple_async_result_propagate_error (simple, error))
756 result_valid = g_simple_async_result_is_valid (result,
757 G_OBJECT (individual),
758 empathy_pixbuf_avatar_from_individual_scaled_async);
759 g_return_val_if_fail (result_valid, NULL);
761 pixbuf = g_simple_async_result_get_op_res_gpointer (simple);
762 return pixbuf != NULL ? g_object_ref (pixbuf) : NULL;
766 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
767 gboolean show_protocol)
769 const gchar *icon_name;
771 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
773 icon_name = empathy_icon_name_for_contact (contact);
775 if (icon_name == NULL) {
778 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
784 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
785 const gchar *icon_name,
786 gboolean show_protocol)
788 GdkPixbuf *pix_status;
789 GdkPixbuf *pix_protocol;
790 gchar *icon_filename;
792 gint numerator, denominator;
794 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
795 (show_protocol == FALSE), NULL);
796 g_return_val_if_fail (icon_name != NULL, NULL);
801 icon_filename = empathy_filename_from_icon_name (icon_name,
803 if (icon_filename == NULL) {
804 DEBUG ("icon name: %s could not be found\n", icon_name);
808 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
810 if (pix_status == NULL) {
811 DEBUG ("Could not open icon %s\n", icon_filename);
812 g_free (icon_filename);
816 g_free (icon_filename);
821 height = gdk_pixbuf_get_height (pix_status);
822 width = gdk_pixbuf_get_width (pix_status);
824 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
825 width * numerator / denominator,
826 height * numerator / denominator);
828 if (pix_protocol == NULL) {
831 gdk_pixbuf_composite (pix_protocol, pix_status,
832 0, height - height * numerator / denominator,
833 width * numerator / denominator, height * numerator / denominator,
834 0, height - height * numerator / denominator,
836 GDK_INTERP_BILINEAR, 255);
838 g_object_unref (pix_protocol);
844 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
850 GdkPixbuf *pixbuf = NULL;
852 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
854 account = empathy_contact_get_account (contact);
855 filename = empathy_filename_from_icon_name (tp_account_get_icon_name (account),
857 if (filename != NULL) {
858 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
866 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf, gint max_size)
871 width = gdk_pixbuf_get_width (pixbuf);
872 height = gdk_pixbuf_get_height (pixbuf);
874 if (width > 0 && (width > max_size || height > max_size)) {
875 factor = (gdouble) max_size / MAX (width, height);
877 width = width * factor;
878 height = height * factor;
880 return gdk_pixbuf_scale_simple (pixbuf,
885 return g_object_ref (pixbuf);
889 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
894 GError *error = NULL;
900 theme = gtk_icon_theme_get_default ();
902 pixbuf = gtk_icon_theme_load_icon (theme,
908 DEBUG ("Error loading icon: %s", error->message);
909 g_clear_error (&error);
916 empathy_pixbuf_from_icon_name (const gchar *icon_name,
917 GtkIconSize icon_size)
926 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
930 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
934 empathy_filename_from_icon_name (const gchar *icon_name,
935 GtkIconSize icon_size)
937 GtkIconTheme *icon_theme;
938 GtkIconInfo *icon_info;
943 icon_theme = gtk_icon_theme_get_default ();
945 if (gtk_icon_size_lookup (icon_size, &w, &h)) {
949 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
950 if (icon_info == NULL)
953 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
954 gtk_icon_info_free (icon_info);
959 /* Stolen from GtkSourceView, hence the weird intendation. Please keep it like
960 * that to make it easier to apply changes from the original code.
962 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
964 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
965 * decomposable character it consumes the decomposition length from the given
966 * offset. So it's useful when the offset was calculated for the normalized
967 * version of str, but we need a pointer to str itself. */
969 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
971 gchar *casefold, *normal;
977 q = g_utf8_next_char (p);
978 casefold = g_utf8_casefold (p, q - p);
979 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
980 offset -= g_utf8_strlen (normal, -1);
989 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
993 const gchar *ret = NULL;
996 gchar *caseless_haystack;
999 g_return_val_if_fail (haystack != NULL, NULL);
1000 g_return_val_if_fail (needle != NULL, NULL);
1002 casefold = g_utf8_casefold (haystack, -1);
1003 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1006 needle_len = g_utf8_strlen (needle, -1);
1007 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1009 if (needle_len == 0)
1011 ret = (gchar *) haystack;
1015 if (haystack_len < needle_len)
1021 p = (gchar *) caseless_haystack;
1022 needle_len = strlen (needle);
1027 if ((strncmp (p, needle, needle_len) == 0))
1029 ret = pointer_from_offset_skipping_decomp (haystack, i);
1033 p = g_utf8_next_char (p);
1038 g_free (caseless_haystack);
1044 g_utf8_caselessnmatch (const char *s1, const char *s2,
1045 gssize n1, gssize n2)
1048 gchar *normalized_s1;
1049 gchar *normalized_s2;
1052 gboolean ret = FALSE;
1054 g_return_val_if_fail (s1 != NULL, FALSE);
1055 g_return_val_if_fail (s2 != NULL, FALSE);
1056 g_return_val_if_fail (n1 > 0, FALSE);
1057 g_return_val_if_fail (n2 > 0, FALSE);
1059 casefold = g_utf8_casefold (s1, n1);
1060 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1063 casefold = g_utf8_casefold (s2, n2);
1064 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1067 len_s1 = strlen (normalized_s1);
1068 len_s2 = strlen (normalized_s2);
1070 if (len_s1 < len_s2)
1073 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
1076 g_free (normalized_s1);
1077 g_free (normalized_s2);
1083 forward_chars_with_skipping (GtkTextIter *iter,
1085 gboolean skip_invisible,
1086 gboolean skip_nontext,
1087 gboolean skip_decomp)
1091 g_return_if_fail (count >= 0);
1097 gboolean ignored = FALSE;
1099 /* minimal workaround to avoid the infinite loop of bug #168247.
1100 * It doesn't fix the problemjust the symptom...
1102 if (gtk_text_iter_is_end (iter))
1105 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
1108 if (!ignored && skip_invisible &&
1109 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
1112 if (!ignored && skip_decomp)
1114 /* being UTF8 correct sucks; this accounts for extra
1115 offsets coming from canonical decompositions of
1116 UTF8 characters (e.g. accented characters) which
1117 g_utf8_normalize () performs */
1122 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
1123 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
1124 i -= (g_utf8_strlen (normal, -1) - 1);
1128 gtk_text_iter_forward_char (iter);
1136 lines_match (const GtkTextIter *start,
1137 const gchar **lines,
1138 gboolean visible_only,
1140 GtkTextIter *match_start,
1141 GtkTextIter *match_end)
1148 if (*lines == NULL || **lines == '\0')
1151 *match_start = *start;
1153 *match_end = *start;
1158 gtk_text_iter_forward_line (&next);
1160 /* No more text in buffer, but *lines is nonempty */
1161 if (gtk_text_iter_equal (start, &next))
1167 line_text = gtk_text_iter_get_visible_slice (start, &next);
1169 line_text = gtk_text_iter_get_slice (start, &next);
1174 line_text = gtk_text_iter_get_visible_text (start, &next);
1176 line_text = gtk_text_iter_get_text (start, &next);
1179 if (match_start) /* if this is the first line we're matching */
1181 found = g_utf8_strcasestr (line_text, *lines);
1185 /* If it's not the first line, we have to match from the
1186 * start of the line.
1188 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1201 /* Get offset to start of search string */
1202 offset = g_utf8_strlen (line_text, found - line_text);
1206 /* If match start needs to be returned, set it to the
1207 * start of the search string.
1209 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1212 *match_start = next;
1215 /* Go to end of search string */
1216 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1225 /* pass NULL for match_start, since we don't need to find the
1228 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
1231 /* strsplit () that retains the delimiter as part of the string. */
1233 strbreakup (const char *string,
1234 const char *delimiter,
1237 GSList *string_list = NULL, *slist;
1238 gchar **str_array, *s, *casefold, *new_string;
1241 g_return_val_if_fail (string != NULL, NULL);
1242 g_return_val_if_fail (delimiter != NULL, NULL);
1245 max_tokens = G_MAXINT;
1247 s = strstr (string, delimiter);
1250 guint delimiter_len = strlen (delimiter);
1256 len = s - string + delimiter_len;
1257 new_string = g_new (gchar, len + 1);
1258 strncpy (new_string, string, len);
1259 new_string[len] = 0;
1260 casefold = g_utf8_casefold (new_string, -1);
1261 g_free (new_string);
1262 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1264 string_list = g_slist_prepend (string_list, new_string);
1266 string = s + delimiter_len;
1267 s = strstr (string, delimiter);
1268 } while (--max_tokens && s);
1274 casefold = g_utf8_casefold (string, -1);
1275 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1277 string_list = g_slist_prepend (string_list, new_string);
1280 str_array = g_new (gchar*, n);
1284 str_array[i--] = NULL;
1285 for (slist = string_list; slist; slist = slist->next)
1286 str_array[i--] = slist->data;
1288 g_slist_free (string_list);
1294 empathy_text_iter_forward_search (const GtkTextIter *iter,
1296 GtkTextIter *match_start,
1297 GtkTextIter *match_end,
1298 const GtkTextIter *limit)
1300 gchar **lines = NULL;
1302 gboolean retval = FALSE;
1304 gboolean visible_only;
1307 g_return_val_if_fail (iter != NULL, FALSE);
1308 g_return_val_if_fail (str != NULL, FALSE);
1310 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
1314 /* If we can move one char, return the empty string there */
1317 if (gtk_text_iter_forward_char (&match)) {
1318 if (limit && gtk_text_iter_equal (&match, limit)) {
1323 *match_start = match;
1334 visible_only = TRUE;
1337 /* locate all lines */
1338 lines = strbreakup (str, "\n", -1);
1343 /* This loop has an inefficient worst-case, where
1344 * gtk_text_iter_get_text () is called repeatedly on
1349 if (limit && gtk_text_iter_compare (&search, limit) >= 0) {
1353 if (lines_match (&search, (const gchar**)lines,
1354 visible_only, slice, &match, &end)) {
1355 if (limit == NULL ||
1356 (limit && gtk_text_iter_compare (&end, limit) <= 0)) {
1360 *match_start = match;
1368 } while (gtk_text_iter_forward_line (&search));
1370 g_strfreev ((gchar **) lines);
1375 static const gchar *
1376 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
1380 const gchar *ret = NULL;
1383 gchar *caseless_haystack;
1386 g_return_val_if_fail (haystack != NULL, NULL);
1387 g_return_val_if_fail (needle != NULL, NULL);
1389 casefold = g_utf8_casefold (haystack, -1);
1390 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
1393 needle_len = g_utf8_strlen (needle, -1);
1394 haystack_len = g_utf8_strlen (caseless_haystack, -1);
1396 if (needle_len == 0)
1398 ret = (gchar *) haystack;
1402 if (haystack_len < needle_len)
1408 i = haystack_len - needle_len;
1409 p = g_utf8_offset_to_pointer (caseless_haystack, i);
1410 needle_len = strlen (needle);
1412 while (p >= caseless_haystack)
1414 if (strncmp (p, needle, needle_len) == 0)
1416 ret = pointer_from_offset_skipping_decomp (haystack, i);
1420 p = g_utf8_prev_char (p);
1425 g_free (caseless_haystack);
1431 backward_lines_match (const GtkTextIter *start,
1432 const gchar **lines,
1433 gboolean visible_only,
1435 GtkTextIter *match_start,
1436 GtkTextIter *match_end)
1438 GtkTextIter line, next;
1443 if (*lines == NULL || **lines == '\0')
1446 *match_start = *start;
1448 *match_end = *start;
1452 line = next = *start;
1453 if (gtk_text_iter_get_line_offset (&next) == 0)
1455 if (!gtk_text_iter_backward_line (&next))
1459 gtk_text_iter_set_line_offset (&next, 0);
1464 line_text = gtk_text_iter_get_visible_slice (&next, &line);
1466 line_text = gtk_text_iter_get_slice (&next, &line);
1471 line_text = gtk_text_iter_get_visible_text (&next, &line);
1473 line_text = gtk_text_iter_get_text (&next, &line);
1476 if (match_start) /* if this is the first line we're matching */
1478 found = g_utf8_strrcasestr (line_text, *lines);
1482 /* If it's not the first line, we have to match from the
1483 * start of the line.
1485 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
1498 /* Get offset to start of search string */
1499 offset = g_utf8_strlen (line_text, found - line_text);
1501 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
1503 /* If match start needs to be returned, set it to the
1504 * start of the search string.
1508 *match_start = next;
1511 /* Go to end of search string */
1512 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
1521 /* try to match the rest of the lines forward, passing NULL
1522 * for match_start so lines_match will try to match the entire
1524 return lines_match (&next, lines, visible_only,
1525 slice, NULL, match_end);
1529 empathy_text_iter_backward_search (const GtkTextIter *iter,
1531 GtkTextIter *match_start,
1532 GtkTextIter *match_end,
1533 const GtkTextIter *limit)
1535 gchar **lines = NULL;
1537 gboolean retval = FALSE;
1539 gboolean visible_only;
1542 g_return_val_if_fail (iter != NULL, FALSE);
1543 g_return_val_if_fail (str != NULL, FALSE);
1545 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
1550 /* If we can move one char, return the empty string there */
1553 if (gtk_text_iter_backward_char (&match))
1555 if (limit && gtk_text_iter_equal (&match, limit))
1559 *match_start = match;
1570 visible_only = TRUE;
1573 /* locate all lines */
1574 lines = strbreakup (str, "\n", -1);
1580 /* This loop has an inefficient worst-case, where
1581 * gtk_text_iter_get_text () is called repeatedly on
1586 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
1589 if (backward_lines_match (&search, (const gchar**)lines,
1590 visible_only, slice, &match, &end))
1592 if (limit == NULL || (limit &&
1593 gtk_text_iter_compare (&end, limit) > 0))
1598 *match_start = match;
1605 if (gtk_text_iter_get_line_offset (&search) == 0)
1607 if (!gtk_text_iter_backward_line (&search))
1612 gtk_text_iter_set_line_offset (&search, 0);
1616 g_strfreev ((gchar **) lines);
1621 /* Takes care of moving the window to the current workspace. */
1623 empathy_window_present_with_time (GtkWindow *window,
1626 GdkWindow *gdk_window;
1628 g_return_if_fail (GTK_IS_WINDOW (window));
1630 /* Move the window to the current workspace before trying to show it.
1631 * This is the behaviour people expect when clicking on the statusbar icon. */
1632 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1637 /* Has no effect if the WM has viewports, like compiz */
1638 gdk_x11_window_move_to_current_desktop (gdk_window);
1640 /* If window is still off-screen, hide it to force it to
1641 * reposition on the current workspace. */
1642 gtk_window_get_position (window, &x, &y);
1643 gtk_window_get_size (window, &w, &h);
1644 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1645 gtk_widget_hide (GTK_WIDGET (window));
1648 if (timestamp == GDK_CURRENT_TIME)
1649 gtk_window_present (window);
1651 gtk_window_present_with_time (window, timestamp);
1655 empathy_window_present (GtkWindow *window)
1657 empathy_window_present_with_time (window, gtk_get_current_event_time ());
1661 empathy_get_toplevel_window (GtkWidget *widget)
1663 GtkWidget *toplevel;
1665 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1667 toplevel = gtk_widget_get_toplevel (widget);
1668 if (GTK_IS_WINDOW (toplevel) &&
1669 gtk_widget_is_toplevel (toplevel)) {
1670 return GTK_WINDOW (toplevel);
1676 /** empathy_make_absolute_url_len:
1680 * Same as #empathy_make_absolute_url but for a limited string length
1683 empathy_make_absolute_url_len (const gchar *url,
1686 g_return_val_if_fail (url != NULL, NULL);
1688 if (g_str_has_prefix (url, "ghelp:") ||
1689 g_str_has_prefix (url, "mailto:") ||
1690 strstr (url, ":/")) {
1691 return g_strndup (url, len);
1694 if (strstr (url, "@")) {
1695 return g_strdup_printf ("mailto:%.*s", len, url);
1698 return g_strdup_printf ("http://%.*s", len, url);
1701 /** empathy_make_absolute_url:
1704 * The URL opening code can't handle schemeless strings, so we try to be
1705 * smart and add http if there is no scheme or doesn't look like a mail
1706 * address. This should work in most cases, and let us click on strings
1707 * like "www.gnome.org".
1709 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1710 * g_free when your are done with it
1713 empathy_make_absolute_url (const gchar *url)
1715 return empathy_make_absolute_url_len (url, strlen (url));
1719 empathy_url_show (GtkWidget *parent,
1723 GError *error = NULL;
1725 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1726 g_return_if_fail (url != NULL);
1728 real_url = empathy_make_absolute_url (url);
1730 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1731 gtk_get_current_event_time (), &error);
1736 dialog = gtk_message_dialog_new (NULL, 0,
1737 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1738 _("Unable to open URI"));
1739 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1740 "%s", error->message);
1742 g_signal_connect (dialog, "response",
1743 G_CALLBACK (gtk_widget_destroy),
1745 gtk_window_present (GTK_WINDOW (dialog));
1747 g_clear_error (&error);
1754 empathy_send_file (EmpathyContact *contact, GFile *file)
1756 EmpathyFTFactory *factory;
1757 GtkRecentManager *manager;
1760 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1761 g_return_if_fail (G_IS_FILE (file));
1763 factory = empathy_ft_factory_dup_singleton ();
1765 empathy_ft_factory_new_transfer_outgoing (factory, contact, file,
1766 empathy_get_current_action_time ());
1768 uri = g_file_get_uri (file);
1769 manager = gtk_recent_manager_get_default ();
1770 gtk_recent_manager_add_item (manager, uri);
1773 g_object_unref (factory);
1777 empathy_send_file_from_uri_list (EmpathyContact *contact, const gchar *uri_list)
1782 /* Only handle a single file for now. It would be wicked cool to be
1783 able to do multiple files, offering to zip them or whatever like
1784 nautilus-sendto does. Note that text/uri-list is defined to have
1785 each line terminated by \r\n, but we can be tolerant of applications
1786 that only use \n or don't terminate single-line entries.
1788 nl = strstr (uri_list, "\r\n");
1790 nl = strchr (uri_list, '\n');
1793 gchar *uri = g_strndup (uri_list, nl - uri_list);
1794 file = g_file_new_for_uri (uri);
1798 file = g_file_new_for_uri (uri_list);
1801 empathy_send_file (contact, file);
1803 g_object_unref (file);
1807 file_manager_send_file_response_cb (GtkDialog *widget,
1809 EmpathyContact *contact)
1813 if (response_id == GTK_RESPONSE_OK) {
1814 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1816 empathy_send_file (contact, file);
1818 g_object_unref (file);
1821 g_object_unref (contact);
1822 gtk_widget_destroy (GTK_WIDGET (widget));
1826 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1831 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1833 DEBUG ("Creating selection file chooser");
1835 widget = gtk_file_chooser_dialog_new (_("Select a file"),
1837 GTK_FILE_CHOOSER_ACTION_OPEN,
1839 GTK_RESPONSE_CANCEL,
1843 button = gtk_button_new_with_mnemonic (_("_Send"));
1844 gtk_button_set_image (GTK_BUTTON (button),
1845 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1846 GTK_ICON_SIZE_BUTTON));
1847 gtk_widget_show (button);
1848 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1850 gtk_widget_set_can_default (button, TRUE);
1851 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1854 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1856 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1859 g_signal_connect (widget, "response",
1860 G_CALLBACK (file_manager_send_file_response_cb),
1861 g_object_ref (contact));
1863 gtk_widget_show (widget);
1867 file_manager_receive_file_response_cb (GtkDialog *dialog,
1868 GtkResponseType response,
1869 EmpathyFTHandler *handler)
1871 EmpathyFTFactory *factory;
1874 if (response == GTK_RESPONSE_OK) {
1877 guint64 free_space, file_size;
1878 GError *error = NULL;
1880 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1881 parent = g_file_get_parent (file);
1882 info = g_file_query_filesystem_info (parent,
1883 G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
1886 g_object_unref (parent);
1888 if (error != NULL) {
1889 g_warning ("Error: %s", error->message);
1891 g_object_unref (file);
1895 free_space = g_file_info_get_attribute_uint64 (info,
1896 G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
1897 file_size = empathy_ft_handler_get_total_bytes (handler);
1899 g_object_unref (info);
1901 if (file_size > free_space) {
1902 GtkWidget *message = gtk_message_dialog_new (
1903 GTK_WINDOW (dialog),
1907 _("Insufficient free space to save file"));
1908 char *file_size_str, *free_space_str;
1910 file_size_str = g_format_size_for_display (file_size);
1911 free_space_str = g_format_size_for_display (free_space);
1913 gtk_message_dialog_format_secondary_text (
1914 GTK_MESSAGE_DIALOG (message),
1915 _("%s of free space are required to save this "
1916 "file, but only %s is available. Please "
1917 "choose another location."),
1918 file_size_str, free_space_str);
1920 gtk_dialog_run (GTK_DIALOG (message));
1922 g_free (file_size_str);
1923 g_free (free_space_str);
1924 gtk_widget_destroy (message);
1926 g_object_unref (file);
1931 factory = empathy_ft_factory_dup_singleton ();
1933 empathy_ft_factory_set_destination_for_incoming_handler (
1934 factory, handler, file);
1936 g_object_unref (factory);
1937 g_object_unref (file);
1939 /* unref the handler, as we dismissed the file chooser,
1940 * and refused the transfer.
1942 g_object_unref (handler);
1945 gtk_widget_destroy (GTK_WIDGET (dialog));
1949 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1953 EmpathyContact *contact;
1956 contact = empathy_ft_handler_get_contact (handler);
1957 g_assert (contact != NULL);
1959 title = g_strdup_printf (_("Incoming file from %s"),
1960 empathy_contact_get_alias (contact));
1962 widget = gtk_file_chooser_dialog_new (title,
1964 GTK_FILE_CHOOSER_ACTION_SAVE,
1966 GTK_RESPONSE_CANCEL,
1970 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1971 empathy_ft_handler_get_filename (handler));
1972 gtk_file_chooser_set_do_overwrite_confirmation
1973 (GTK_FILE_CHOOSER (widget), TRUE);
1975 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1977 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1978 dir = g_get_home_dir ();
1980 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1982 g_signal_connect (widget, "response",
1983 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1985 gtk_widget_show (widget);
1990 empathy_make_color_whiter (GdkRGBA *color)
1992 const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
1994 color->red = (color->red + white.red) / 2;
1995 color->green = (color->green + white.green) / 2;
1996 color->blue = (color->blue + white.blue) / 2;
2000 menu_deactivate_cb (GtkMenu *menu,
2003 /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
2004 g_signal_handlers_disconnect_by_func (menu,
2005 menu_deactivate_cb, user_data);
2007 gtk_menu_detach (menu);
2010 /* Convenient function to create a GtkMenu attached to @attach_to and detach
2011 * it when the menu is not displayed any more. This is useful when creating a
2012 * context menu that we want to get rid as soon as it as been displayed. */
2014 empathy_context_menu_new (GtkWidget *attach_to)
2018 menu = gtk_menu_new ();
2020 gtk_menu_attach_to_widget (GTK_MENU (menu), attach_to, NULL);
2022 /* menu is initially unowned but gtk_menu_attach_to_widget () taked its
2023 * floating ref. We can either wait that @attach_to releases its ref when
2024 * it will be destroyed (when leaving Empathy most of the time) or explicitely
2025 * detach the menu when it's not displayed any more.
2026 * We go for the latter as we don't want to keep useless menus in memory
2027 * during the whole lifetime of Empathy. */
2028 g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb), NULL);
2034 empathy_get_current_action_time (void)
2036 return (tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
2039 /* @words = empathy_live_search_strip_utf8_string (@text);
2041 * User has to pass both so we don't have to compute @words ourself each time
2042 * this function is called. */
2044 empathy_individual_match_string (FolksIndividual *individual,
2051 gboolean retval = FALSE;
2053 /* check alias name */
2054 str = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual));
2056 if (empathy_live_search_match_words (str, words))
2059 personas = folks_individual_get_personas (individual);
2061 /* check contact id, remove the @server.com part */
2062 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
2063 while (retval == FALSE && gee_iterator_next (iter))
2065 FolksPersona *persona = gee_iterator_get (iter);
2068 if (empathy_folks_persona_is_interesting (persona))
2070 str = folks_persona_get_display_id (persona);
2072 /* Accept the persona if @text is a full prefix of his ID; that allows
2073 * user to find, say, a jabber contact by typing his JID. */
2074 if (g_str_has_prefix (str, text))
2080 gchar *dup_str = NULL;
2083 p = strstr (str, "@");
2085 str = dup_str = g_strndup (str, p - str);
2087 visible = empathy_live_search_match_words (str, words);
2093 g_clear_object (&persona);
2095 g_clear_object (&iter);
2097 /* FIXME: Add more rules here, we could check phone numbers in
2098 * contact's vCard for example. */
2103 dtmf_dialpad_button_pressed_cb (GObject *button,
2106 GtkEntryBuffer *buffer = gtk_entry_get_buffer (entry);
2109 label = g_object_get_data (button, "label");
2110 gtk_entry_buffer_insert_text (buffer, -1, label, -1);
2116 empathy_create_dtmf_dialpad (GObject *self,
2117 GCallback dtmf_button_pressed_cb,
2118 GCallback dtmf_button_released_cb)
2120 GtkWidget *box, *entry, *table;
2122 GQuark button_quark;
2125 const gchar *sublabel;
2127 } dtmfbuttons[] = { { "1", "", TP_DTMF_EVENT_DIGIT_1 },
2128 { "2", "abc", TP_DTMF_EVENT_DIGIT_2 },
2129 { "3", "def", TP_DTMF_EVENT_DIGIT_3 },
2130 { "4", "ghi", TP_DTMF_EVENT_DIGIT_4 },
2131 { "5", "jkl", TP_DTMF_EVENT_DIGIT_5 },
2132 { "6", "mno", TP_DTMF_EVENT_DIGIT_6 },
2133 { "7", "pqrs", TP_DTMF_EVENT_DIGIT_7 },
2134 { "8", "tuv", TP_DTMF_EVENT_DIGIT_8 },
2135 { "9", "wxyz", TP_DTMF_EVENT_DIGIT_9 },
2136 { "#", "", TP_DTMF_EVENT_HASH },
2137 { "0", "", TP_DTMF_EVENT_DIGIT_0 },
2138 { "*", "", TP_DTMF_EVENT_ASTERISK },
2141 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 3);
2143 entry = gtk_entry_new ();
2144 gtk_editable_set_editable (GTK_EDITABLE (entry), FALSE);
2146 gtk_box_pack_start (GTK_BOX (box), entry, FALSE, FALSE, 3);
2148 button_quark = g_quark_from_static_string (EMPATHY_DTMF_BUTTON_ID);
2150 table = gtk_table_new (4, 3, TRUE);
2152 for (i = 0; dtmfbuttons[i].label != NULL; i++)
2154 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
2155 GtkWidget *button = gtk_button_new ();
2159 gtk_container_add (GTK_CONTAINER (button), vbox);
2162 label = gtk_label_new ("");
2163 str = g_strdup_printf ("<span size='x-large'>%s</span>",
2164 dtmfbuttons[i].label);
2165 gtk_label_set_markup (GTK_LABEL (label), str);
2168 g_object_set_data (G_OBJECT (button), "label",
2169 (gpointer) dtmfbuttons[i].label);
2171 gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 3);
2174 label = gtk_label_new ("");
2175 str = g_strdup_printf (
2176 "<span foreground='#555555'>%s</span>",
2177 dtmfbuttons[i].sublabel);
2178 gtk_label_set_markup (GTK_LABEL (label), str);
2181 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
2183 gtk_table_attach (GTK_TABLE (table), button, i % 3, i % 3 + 1,
2184 i/3, i/3 + 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 1, 1);
2186 g_object_set_qdata (G_OBJECT (button), button_quark,
2187 GUINT_TO_POINTER (dtmfbuttons[i].event));
2189 /* To update the GtkEntry */
2190 g_signal_connect (G_OBJECT (button), "pressed",
2191 G_CALLBACK (dtmf_dialpad_button_pressed_cb), entry);
2193 g_signal_connect (G_OBJECT (button), "pressed",
2194 dtmf_button_pressed_cb, self);
2195 g_signal_connect (G_OBJECT (button), "released",
2196 dtmf_button_released_cb, self);
2199 gtk_box_pack_start (GTK_BOX (box), table, FALSE, FALSE, 3);
2205 empathy_launch_program (const gchar *dir,
2209 GdkDisplay *display;
2210 GError *error = NULL;
2213 GdkAppLaunchContext *context = NULL;
2215 /* Try to run from source directory if possible */
2216 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
2219 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2222 path = g_build_filename (dir, name, NULL);
2226 cmd = g_strconcat (path, " ", args, NULL);
2228 cmd = g_strdup (path);
2230 app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
2231 if (app_info == NULL)
2233 DEBUG ("Failed to create app info: %s", error->message);
2234 g_error_free (error);
2238 display = gdk_display_get_default ();
2239 context = gdk_display_get_app_launch_context (display);
2241 if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
2244 g_warning ("Failed to launch %s: %s", name, error->message);
2245 g_error_free (error);
2250 tp_clear_object (&app_info);
2251 tp_clear_object (&context);