2 * Copyright (C) 2002-2007 Imendio AB
3 * Copyright (C) 2007-2010 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Mikael Hallendal <micke@imendio.com>
21 * Richard Hult <richard@imendio.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Xavier Claessens <xclaesse@gmail.com>
24 * Jonny Lamb <jonny.lamb@collabora.co.uk>
25 * Travis Reitter <travis.reitter@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>
41 #include <telepathy-glib/util.h>
42 #include <folks/folks.h>
44 #include "empathy-ui-utils.h"
45 #include "empathy-images.h"
46 #include "empathy-live-search.h"
47 #include "empathy-smiley-manager.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
50 #include <libempathy/empathy-debug.h>
51 #include <libempathy/empathy-utils.h>
52 #include <libempathy/empathy-ft-factory.h>
55 empathy_gtk_init (void)
57 static gboolean initialized = FALSE;
64 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
65 PKGDATADIR G_DIR_SEPARATOR_S "icons");
67 /* Add icons from source dir if available */
68 if (g_getenv ("EMPATHY_SRCDIR") != NULL)
72 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "data",
73 "icons", "local-copy", NULL);
75 if (g_file_test (path, G_FILE_TEST_EXISTS))
76 gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), path);
85 builder_get_file_valist (const gchar *filename,
86 const gchar *first_object,
94 DEBUG ("Loading file %s", filename);
96 gui = gtk_builder_new ();
97 gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
99 if (!gtk_builder_add_from_file (gui, filename, &error))
101 g_critical ("GtkBuilder Error (%s): %s",
102 filename, error->message);
104 g_clear_error (&error);
105 g_object_unref (gui);
107 /* we need to iterate and set all of the pointers to NULL */
108 for (name = first_object; name; name = va_arg (args, const gchar *))
110 object_ptr = va_arg (args, GObject**);
118 for (name = first_object; name; name = va_arg (args, const gchar *))
120 object_ptr = va_arg (args, GObject**);
122 *object_ptr = gtk_builder_get_object (gui, name);
126 g_warning ("File is missing object '%s'.", name);
135 empathy_builder_get_file (const gchar *filename,
136 const gchar *first_object,
142 va_start (args, first_object);
143 gui = builder_get_file_valist (filename, first_object, args);
150 empathy_builder_connect (GtkBuilder *gui,
152 const gchar *first_object,
161 va_start (args, first_object);
162 for (name = first_object; name; name = va_arg (args, const gchar *))
164 sig = va_arg (args, const gchar *);
165 callback = va_arg (args, GCallback);
167 object = gtk_builder_get_object (gui, name);
170 g_warning ("File is missing object '%s'.", name);
174 g_signal_connect (object, sig, callback, user_data);
181 empathy_builder_unref_and_keep_widget (GtkBuilder *gui,
184 /* On construction gui sinks the initial reference to widget. When gui
185 * is finalized it will drop its ref to widget. We take our own ref to
186 * prevent widget being finalised. The widget is forced to have a
187 * floating reference, like when it was initially unowned so that it can
188 * be used like any other GtkWidget. */
190 g_object_ref (widget);
191 g_object_force_floating (G_OBJECT (widget));
192 g_object_unref (gui);
198 empathy_icon_name_for_presence (TpConnectionPresenceType presence)
202 case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
203 return EMPATHY_IMAGE_AVAILABLE;
204 case TP_CONNECTION_PRESENCE_TYPE_BUSY:
205 return EMPATHY_IMAGE_BUSY;
206 case TP_CONNECTION_PRESENCE_TYPE_AWAY:
207 return EMPATHY_IMAGE_AWAY;
208 case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
209 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
210 EMPATHY_IMAGE_EXT_AWAY))
211 return EMPATHY_IMAGE_EXT_AWAY;
213 /* The 'extended-away' icon is not an official one so we fallback to
214 * idle if it's not implemented */
215 return EMPATHY_IMAGE_IDLE;
216 case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
217 if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (),
218 EMPATHY_IMAGE_HIDDEN))
219 return EMPATHY_IMAGE_HIDDEN;
221 /* The 'hidden' icon is not an official one so we fallback to offline if
222 * it's not implemented */
223 return EMPATHY_IMAGE_OFFLINE;
224 case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
225 case TP_CONNECTION_PRESENCE_TYPE_ERROR:
226 return EMPATHY_IMAGE_OFFLINE;
227 case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
228 return EMPATHY_IMAGE_PENDING;
229 case TP_CONNECTION_PRESENCE_TYPE_UNSET:
238 empathy_icon_name_for_contact (EmpathyContact *contact)
240 TpConnectionPresenceType presence;
242 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact),
243 EMPATHY_IMAGE_OFFLINE);
245 presence = empathy_contact_get_presence (contact);
246 return empathy_icon_name_for_presence (presence);
250 empathy_icon_name_for_individual (FolksIndividual *individual)
252 FolksPresenceType folks_presence;
253 TpConnectionPresenceType presence;
255 folks_presence = folks_presence_details_get_presence_type (
256 FOLKS_PRESENCE_DETAILS (individual));
257 presence = empathy_folks_presence_type_to_tp (folks_presence);
259 return empathy_icon_name_for_presence (presence);
263 empathy_protocol_name_for_contact (EmpathyContact *contact)
267 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
269 account = empathy_contact_get_account (contact);
273 return tp_account_get_icon_name (account);
277 empathy_pixbuf_from_data (gchar *data,
280 return empathy_pixbuf_from_data_and_mime (data, data_size, NULL);
284 empathy_pixbuf_from_data_and_mime (gchar *data,
288 GdkPixbufLoader *loader;
289 GdkPixbufFormat *format;
290 GdkPixbuf *pixbuf = NULL;
292 GError *error = NULL;
297 loader = gdk_pixbuf_loader_new ();
298 if (!gdk_pixbuf_loader_write (loader, (guchar *) data, data_size, &error))
300 DEBUG ("Failed to write to pixbuf loader: %s",
301 error ? error->message : "No error given");
305 if (!gdk_pixbuf_loader_close (loader, &error))
307 DEBUG ("Failed to close pixbuf loader: %s",
308 error ? error->message : "No error given");
312 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
315 g_object_ref (pixbuf);
317 if (mime_type != NULL)
319 format = gdk_pixbuf_loader_get_format (loader);
320 mime_types = gdk_pixbuf_format_get_mime_types (format);
322 *mime_type = g_strdup (*mime_types);
323 if (mime_types[1] != NULL)
324 DEBUG ("Loader supports more than one mime "
325 "type! Picking the first one, %s",
328 g_strfreev (mime_types);
333 g_clear_error (&error);
334 g_object_unref (loader);
343 gboolean preserve_aspect_ratio;
347 pixbuf_from_avatar_size_prepared_cb (GdkPixbufLoader *loader,
350 struct SizeData *data)
352 g_return_if_fail (width > 0 && height > 0);
354 if (data->preserve_aspect_ratio && (data->width > 0 || data->height > 0))
356 if (width < data->width && height < data->height)
364 width = width * (double) data->height / (gdouble) height;
365 height = data->height;
367 else if (data->height < 0)
369 height = height * (double) data->width / (double) width;
372 else if ((double) height * (double) data->width >
373 (double) width * (double) data->height)
375 width = 0.5 + (double) width * (double) data->height / (double) height;
376 height = data->height;
380 height = 0.5 + (double) height * (double) data->width / (double) width;
389 if (data->height > 0)
390 height = data->height;
393 gdk_pixbuf_loader_set_size (loader, width, height);
397 empathy_avatar_pixbuf_roundify (GdkPixbuf *pixbuf)
399 gint width, height, rowstride;
402 width = gdk_pixbuf_get_width (pixbuf);
403 height = gdk_pixbuf_get_height (pixbuf);
404 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
405 pixels = gdk_pixbuf_get_pixels (pixbuf);
407 if (width < 6 || height < 6)
414 pixels[rowstride + 3] = 0x80;
415 pixels[rowstride * 2 + 3] = 0xC0;
418 pixels[width * 4 - 1] = 0;
419 pixels[width * 4 - 5] = 0x80;
420 pixels[width * 4 - 9] = 0xC0;
421 pixels[rowstride + (width * 4) - 1] = 0x80;
422 pixels[(2 * rowstride) + (width * 4) - 1] = 0xC0;
425 pixels[(height - 1) * rowstride + 3] = 0;
426 pixels[(height - 1) * rowstride + 7] = 0x80;
427 pixels[(height - 1) * rowstride + 11] = 0xC0;
428 pixels[(height - 2) * rowstride + 3] = 0x80;
429 pixels[(height - 3) * rowstride + 3] = 0xC0;
432 pixels[height * rowstride - 1] = 0;
433 pixels[(height - 1) * rowstride - 1] = 0x80;
434 pixels[(height - 2) * rowstride - 1] = 0xC0;
435 pixels[height * rowstride - 5] = 0x80;
436 pixels[height * rowstride - 9] = 0xC0;
440 empathy_gdk_pixbuf_is_opaque (GdkPixbuf *pixbuf)
442 gint height, rowstride, i;
446 height = gdk_pixbuf_get_height (pixbuf);
447 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
448 pixels = gdk_pixbuf_get_pixels (pixbuf);
451 for (i = 3; i < rowstride; i+=4)
455 for (i = 1; i < height - 1; i++)
457 row = pixels + (i*rowstride);
458 if (row[3] < 0xfe || row[rowstride-1] < 0xfe)
462 row = pixels + ((height-1) * rowstride);
463 for (i = 3; i < rowstride; i+=4)
471 avatar_pixbuf_from_loader (GdkPixbufLoader *loader)
475 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
477 if (!gdk_pixbuf_get_has_alpha (pixbuf))
479 GdkPixbuf *rounded_pixbuf;
481 rounded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
482 gdk_pixbuf_get_width (pixbuf),
483 gdk_pixbuf_get_height (pixbuf));
485 gdk_pixbuf_copy_area (pixbuf, 0, 0,
486 gdk_pixbuf_get_width (pixbuf),
487 gdk_pixbuf_get_height (pixbuf),
491 pixbuf = rounded_pixbuf;
495 g_object_ref (pixbuf);
498 if (empathy_gdk_pixbuf_is_opaque (pixbuf))
499 empathy_avatar_pixbuf_roundify (pixbuf);
505 empathy_pixbuf_from_avatar_scaled (EmpathyAvatar *avatar,
510 GdkPixbufLoader *loader;
511 struct SizeData data;
512 GError *error = NULL;
518 data.height = height;
519 data.preserve_aspect_ratio = TRUE;
521 loader = gdk_pixbuf_loader_new ();
523 g_signal_connect (loader, "size-prepared",
524 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb), &data);
526 if (avatar->len == 0)
528 g_warning ("Avatar has 0 length");
531 else if (!gdk_pixbuf_loader_write (loader, avatar->data, avatar->len, &error))
533 g_warning ("Couldn't write avatar image:%p with "
534 "length:%" G_GSIZE_FORMAT " to pixbuf loader: %s",
535 avatar->data, avatar->len, error->message);
537 g_error_free (error);
541 gdk_pixbuf_loader_close (loader, NULL);
542 pixbuf = avatar_pixbuf_from_loader (loader);
544 g_object_unref (loader);
550 empathy_pixbuf_avatar_from_contact_scaled (EmpathyContact *contact,
554 EmpathyAvatar *avatar;
556 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
558 avatar = empathy_contact_get_avatar (contact);
560 return empathy_pixbuf_from_avatar_scaled (avatar, width, height);
565 FolksIndividual *individual;
566 GSimpleAsyncResult *result;
569 struct SizeData size_data;
570 GdkPixbufLoader *loader;
571 GCancellable *cancellable;
573 } PixbufAvatarFromIndividualClosure;
575 static PixbufAvatarFromIndividualClosure *
576 pixbuf_avatar_from_individual_closure_new (FolksIndividual *individual,
577 GSimpleAsyncResult *result,
580 GCancellable *cancellable)
582 PixbufAvatarFromIndividualClosure *closure;
584 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
585 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
587 closure = g_new0 (PixbufAvatarFromIndividualClosure, 1);
588 closure->individual = g_object_ref (individual);
589 closure->result = g_object_ref (result);
590 closure->width = width;
591 closure->height = height;
593 if (cancellable != NULL)
594 closure->cancellable = g_object_ref (cancellable);
600 pixbuf_avatar_from_individual_closure_free (
601 PixbufAvatarFromIndividualClosure *closure)
603 g_clear_object (&closure->cancellable);
604 tp_clear_object (&closure->loader);
605 g_object_unref (closure->individual);
606 g_object_unref (closure->result);
611 avatar_icon_load_close_cb (GObject *object,
612 GAsyncResult *result,
615 GError *error = NULL;
617 g_input_stream_close_finish (G_INPUT_STREAM (object), result, &error);
621 DEBUG ("Failed to close pixbuf stream: %s", error->message);
622 g_error_free (error);
627 avatar_icon_load_read_cb (GObject *object,
628 GAsyncResult *result,
631 GInputStream *stream = G_INPUT_STREAM (object);
632 PixbufAvatarFromIndividualClosure *closure = user_data;
634 GError *error = NULL;
636 /* Finish reading this chunk from the stream */
637 n_read = g_input_stream_read_finish (stream, result, &error);
640 DEBUG ("Failed to finish read from pixbuf stream: %s",
643 g_simple_async_result_set_from_error (closure->result, error);
647 /* Write the chunk to the pixbuf loader */
648 if (!gdk_pixbuf_loader_write (closure->loader, (guchar *) closure->data,
651 DEBUG ("Failed to write to pixbuf loader: %s",
652 error ? error->message : "No error given");
654 g_simple_async_result_set_from_error (closure->result, error);
661 if (!gdk_pixbuf_loader_close (closure->loader, &error))
663 DEBUG ("Failed to close pixbuf loader: %s",
664 error ? error->message : "No error given");
666 g_simple_async_result_set_from_error (closure->result, error);
671 g_simple_async_result_set_op_res_gpointer (closure->result,
672 avatar_pixbuf_from_loader (closure->loader),
679 /* Loop round and read another chunk. */
680 g_input_stream_read_async (stream, closure->data,
681 G_N_ELEMENTS (closure->data),
682 G_PRIORITY_DEFAULT, closure->cancellable,
683 avatar_icon_load_read_cb, closure);
689 /* We must close the pixbuf loader before unreffing it. */
690 gdk_pixbuf_loader_close (closure->loader, NULL);
693 /* Close the file for safety (even though it should be
694 * automatically closed when the stream is finalised). */
695 g_input_stream_close_async (stream, G_PRIORITY_DEFAULT, NULL,
696 (GAsyncReadyCallback) avatar_icon_load_close_cb, NULL);
698 g_simple_async_result_complete (closure->result);
700 g_clear_error (&error);
701 pixbuf_avatar_from_individual_closure_free (closure);
705 avatar_icon_load_cb (GObject *object,
706 GAsyncResult *result,
709 GLoadableIcon *icon = G_LOADABLE_ICON (object);
710 PixbufAvatarFromIndividualClosure *closure = user_data;
711 GInputStream *stream;
712 GError *error = NULL;
714 stream = g_loadable_icon_load_finish (icon, result, NULL, &error);
717 DEBUG ("Failed to open avatar stream: %s", error->message);
718 g_simple_async_result_set_from_error (closure->result, error);
722 closure->size_data.width = closure->width;
723 closure->size_data.height = closure->height;
724 closure->size_data.preserve_aspect_ratio = TRUE;
726 /* Load the data into a pixbuf loader in chunks. */
727 closure->loader = gdk_pixbuf_loader_new ();
729 g_signal_connect (closure->loader, "size-prepared",
730 G_CALLBACK (pixbuf_from_avatar_size_prepared_cb),
731 &(closure->size_data));
733 /* Begin to read the first chunk. */
734 g_input_stream_read_async (stream, closure->data,
735 G_N_ELEMENTS (closure->data),
736 G_PRIORITY_DEFAULT, closure->cancellable,
737 avatar_icon_load_read_cb, closure);
739 g_object_unref (stream);
744 g_simple_async_result_complete (closure->result);
746 g_clear_error (&error);
747 tp_clear_object (&stream);
748 pixbuf_avatar_from_individual_closure_free (closure);
752 empathy_pixbuf_avatar_from_individual_scaled_async (
753 FolksIndividual *individual,
756 GCancellable *cancellable,
757 GAsyncReadyCallback callback,
760 GLoadableIcon *avatar_icon;
761 GSimpleAsyncResult *result;
762 PixbufAvatarFromIndividualClosure *closure;
764 result = g_simple_async_result_new (G_OBJECT (individual),
765 callback, user_data, empathy_pixbuf_avatar_from_individual_scaled_async);
767 avatar_icon = folks_avatar_details_get_avatar (
768 FOLKS_AVATAR_DETAILS (individual));
770 if (avatar_icon == NULL)
772 g_simple_async_result_set_error (result, G_IO_ERROR,
773 G_IO_ERROR_NOT_FOUND, "no avatar found");
775 g_simple_async_result_complete (result);
776 g_object_unref (result);
780 closure = pixbuf_avatar_from_individual_closure_new (individual, result,
781 width, height, cancellable);
783 g_return_if_fail (closure != NULL);
785 g_loadable_icon_load_async (avatar_icon, width, cancellable,
786 avatar_icon_load_cb, closure);
788 g_object_unref (result);
791 /* Return a ref on the GdkPixbuf */
793 empathy_pixbuf_avatar_from_individual_scaled_finish (
794 FolksIndividual *individual,
795 GAsyncResult *result,
798 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
799 gboolean result_valid;
802 g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
803 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
805 if (g_simple_async_result_propagate_error (simple, error))
808 result_valid = g_simple_async_result_is_valid (result,
809 G_OBJECT (individual),
810 empathy_pixbuf_avatar_from_individual_scaled_async);
812 g_return_val_if_fail (result_valid, NULL);
814 pixbuf = g_simple_async_result_get_op_res_gpointer (simple);
815 return pixbuf != NULL ? g_object_ref (pixbuf) : NULL;
819 empathy_pixbuf_contact_status_icon (EmpathyContact *contact,
820 gboolean show_protocol)
822 const gchar *icon_name;
824 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
826 icon_name = empathy_icon_name_for_contact (contact);
828 if (icon_name == NULL)
831 return empathy_pixbuf_contact_status_icon_with_icon_name (contact,
832 icon_name, show_protocol);
835 static GdkPixbuf * empathy_pixbuf_protocol_from_contact_scaled (
836 EmpathyContact *contact,
841 empathy_pixbuf_contact_status_icon_with_icon_name (EmpathyContact *contact,
842 const gchar *icon_name,
843 gboolean show_protocol)
845 GdkPixbuf *pix_status;
846 GdkPixbuf *pix_protocol;
847 gchar *icon_filename;
849 gint numerator, denominator;
851 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact) ||
852 (show_protocol == FALSE), NULL);
853 g_return_val_if_fail (icon_name != NULL, NULL);
858 icon_filename = empathy_filename_from_icon_name (icon_name,
861 if (icon_filename == NULL)
863 DEBUG ("icon name: %s could not be found\n", icon_name);
867 pix_status = gdk_pixbuf_new_from_file (icon_filename, NULL);
869 if (pix_status == NULL)
871 DEBUG ("Could not open icon %s\n", icon_filename);
872 g_free (icon_filename);
876 g_free (icon_filename);
881 height = gdk_pixbuf_get_height (pix_status);
882 width = gdk_pixbuf_get_width (pix_status);
884 pix_protocol = empathy_pixbuf_protocol_from_contact_scaled (contact,
885 width * numerator / denominator,
886 height * numerator / denominator);
888 if (pix_protocol == NULL)
891 gdk_pixbuf_composite (pix_protocol, pix_status,
892 0, height - height * numerator / denominator,
893 width * numerator / denominator, height * numerator / denominator,
894 0, height - height * numerator / denominator,
896 GDK_INTERP_BILINEAR, 255);
898 g_object_unref (pix_protocol);
904 empathy_pixbuf_protocol_from_contact_scaled (EmpathyContact *contact,
910 GdkPixbuf *pixbuf = NULL;
912 g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
914 account = empathy_contact_get_account (contact);
915 filename = empathy_filename_from_icon_name (
916 tp_account_get_icon_name (account), GTK_ICON_SIZE_MENU);
918 if (filename != NULL)
920 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, width, height, NULL);
928 empathy_pixbuf_scale_down_if_necessary (GdkPixbuf *pixbuf,
934 width = gdk_pixbuf_get_width (pixbuf);
935 height = gdk_pixbuf_get_height (pixbuf);
937 if (width > 0 && (width > max_size || height > max_size))
939 factor = (gdouble) max_size / MAX (width, height);
941 width = width * factor;
942 height = height * factor;
944 return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_HYPER);
947 return g_object_ref (pixbuf);
951 empathy_pixbuf_from_icon_name_sized (const gchar *icon_name,
956 GError *error = NULL;
961 theme = gtk_icon_theme_get_default ();
963 pixbuf = gtk_icon_theme_load_icon (theme, icon_name, size, 0, &error);
967 DEBUG ("Error loading icon: %s", error->message);
968 g_clear_error (&error);
975 empathy_pixbuf_from_icon_name (const gchar *icon_name,
976 GtkIconSize icon_size)
984 if (gtk_icon_size_lookup (icon_size, &w, &h))
987 return empathy_pixbuf_from_icon_name_sized (icon_name, size);
991 empathy_filename_from_icon_name (const gchar *icon_name,
992 GtkIconSize icon_size)
994 GtkIconTheme *icon_theme;
995 GtkIconInfo *icon_info;
1000 icon_theme = gtk_icon_theme_get_default ();
1002 if (gtk_icon_size_lookup (icon_size, &w, &h))
1005 icon_info = gtk_icon_theme_lookup_icon (icon_theme, icon_name, size, 0);
1006 if (icon_info == NULL)
1009 ret = g_strdup (gtk_icon_info_get_filename (icon_info));
1010 gtk_icon_info_free (icon_info);
1015 /* Takes care of moving the window to the current workspace. */
1017 empathy_window_present_with_time (GtkWindow *window,
1020 GdkWindow *gdk_window;
1022 g_return_if_fail (GTK_IS_WINDOW (window));
1024 /* Move the window to the current workspace before trying to show it.
1025 * This is the behaviour people expect when clicking on the statusbar icon. */
1026 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1033 /* Has no effect if the WM has viewports, like compiz */
1034 gdk_x11_window_move_to_current_desktop (gdk_window);
1036 /* If window is still off-screen, hide it to force it to
1037 * reposition on the current workspace. */
1038 gtk_window_get_position (window, &x, &y);
1039 gtk_window_get_size (window, &w, &h);
1040 if (!EMPATHY_RECT_IS_ON_SCREEN (x, y, w, h))
1041 gtk_widget_hide (GTK_WIDGET (window));
1044 if (timestamp == GDK_CURRENT_TIME)
1045 gtk_window_present (window);
1047 gtk_window_present_with_time (window, timestamp);
1051 empathy_window_present (GtkWindow *window)
1053 empathy_window_present_with_time (window, gtk_get_current_event_time ());
1057 empathy_get_toplevel_window (GtkWidget *widget)
1059 GtkWidget *toplevel;
1061 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1063 toplevel = gtk_widget_get_toplevel (widget);
1064 if (GTK_IS_WINDOW (toplevel) &&
1065 gtk_widget_is_toplevel (toplevel))
1066 return GTK_WINDOW (toplevel);
1071 /** empathy_make_absolute_url_len:
1075 * Same as #empathy_make_absolute_url but for a limited string length
1078 empathy_make_absolute_url_len (const gchar *url,
1081 g_return_val_if_fail (url != NULL, NULL);
1083 if (g_str_has_prefix (url, "help:") ||
1084 g_str_has_prefix (url, "mailto:") ||
1086 return g_strndup (url, len);
1088 if (strstr (url, "@"))
1089 return g_strdup_printf ("mailto:%.*s", len, url);
1091 return g_strdup_printf ("http://%.*s", len, url);
1094 /** empathy_make_absolute_url:
1097 * The URL opening code can't handle schemeless strings, so we try to be
1098 * smart and add http if there is no scheme or doesn't look like a mail
1099 * address. This should work in most cases, and let us click on strings
1100 * like "www.gnome.org".
1102 * Returns: a newly allocated url with proper mailto: or http:// prefix, use
1103 * g_free when your are done with it
1106 empathy_make_absolute_url (const gchar *url)
1108 return empathy_make_absolute_url_len (url, strlen (url));
1112 empathy_url_show (GtkWidget *parent,
1116 GError *error = NULL;
1118 g_return_if_fail (parent == NULL || GTK_IS_WIDGET (parent));
1119 g_return_if_fail (url != NULL);
1121 real_url = empathy_make_absolute_url (url);
1123 gtk_show_uri (parent ? gtk_widget_get_screen (parent) : NULL, real_url,
1124 gtk_get_current_event_time (), &error);
1130 dialog = gtk_message_dialog_new (NULL, 0,
1131 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
1132 _("Unable to open URI"));
1134 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1135 "%s", error->message);
1137 g_signal_connect (dialog, "response",
1138 G_CALLBACK (gtk_widget_destroy), NULL);
1140 gtk_window_present (GTK_WINDOW (dialog));
1142 g_clear_error (&error);
1149 empathy_send_file (EmpathyContact *contact,
1152 EmpathyFTFactory *factory;
1153 GtkRecentManager *manager;
1156 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1157 g_return_if_fail (G_IS_FILE (file));
1159 factory = empathy_ft_factory_dup_singleton ();
1161 empathy_ft_factory_new_transfer_outgoing (factory, contact, file,
1162 empathy_get_current_action_time ());
1164 uri = g_file_get_uri (file);
1165 manager = gtk_recent_manager_get_default ();
1166 gtk_recent_manager_add_item (manager, uri);
1169 g_object_unref (factory);
1173 empathy_send_file_from_uri_list (EmpathyContact *contact,
1174 const gchar *uri_list)
1179 /* Only handle a single file for now. It would be wicked cool to be
1180 able to do multiple files, offering to zip them or whatever like
1181 nautilus-sendto does. Note that text/uri-list is defined to have
1182 each line terminated by \r\n, but we can be tolerant of applications
1183 that only use \n or don't terminate single-line entries.
1185 nl = strstr (uri_list, "\r\n");
1187 nl = strchr (uri_list, '\n');
1191 gchar *uri = g_strndup (uri_list, nl - uri_list);
1192 file = g_file_new_for_uri (uri);
1197 file = g_file_new_for_uri (uri_list);
1200 empathy_send_file (contact, file);
1202 g_object_unref (file);
1206 file_manager_send_file_response_cb (GtkDialog *widget,
1208 EmpathyContact *contact)
1212 if (response_id == GTK_RESPONSE_OK)
1214 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (widget));
1216 empathy_send_file (contact, file);
1218 g_object_unref (file);
1221 g_object_unref (contact);
1222 gtk_widget_destroy (GTK_WIDGET (widget));
1226 filter_cb (const GtkFileFilterInfo *filter_info,
1229 /* filter out socket files */
1230 return tp_strdiff (filter_info->mime_type, "inode/socket");
1233 static GtkFileFilter *
1234 create_file_filter (void)
1236 GtkFileFilter *filter;
1238 filter = gtk_file_filter_new ();
1240 gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_MIME_TYPE, filter_cb,
1247 empathy_send_file_with_file_chooser (EmpathyContact *contact)
1252 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1254 DEBUG ("Creating selection file chooser");
1256 widget = gtk_file_chooser_dialog_new (_("Select a file"), NULL,
1257 GTK_FILE_CHOOSER_ACTION_OPEN,
1258 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1262 button = gtk_button_new_with_mnemonic (_("_Send"));
1263 gtk_button_set_image (GTK_BUTTON (button),
1264 gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
1265 GTK_ICON_SIZE_BUTTON));
1266 gtk_widget_show (button);
1268 gtk_dialog_add_action_widget (GTK_DIALOG (widget), button,
1271 gtk_widget_set_can_default (button, TRUE);
1272 gtk_dialog_set_default_response (GTK_DIALOG (widget),
1275 gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (widget), FALSE);
1277 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget),
1280 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (widget),
1281 create_file_filter ());
1283 g_signal_connect (widget, "response",
1284 G_CALLBACK (file_manager_send_file_response_cb), g_object_ref (contact));
1286 gtk_widget_show (widget);
1290 file_manager_receive_file_response_cb (GtkDialog *dialog,
1291 GtkResponseType response,
1292 EmpathyFTHandler *handler)
1294 EmpathyFTFactory *factory;
1297 if (response == GTK_RESPONSE_OK)
1301 guint64 free_space, file_size;
1302 GError *error = NULL;
1304 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
1305 parent = g_file_get_parent (file);
1306 info = g_file_query_filesystem_info (parent,
1307 G_FILE_ATTRIBUTE_FILESYSTEM_FREE, NULL, &error);
1309 g_object_unref (parent);
1313 g_warning ("Error: %s", error->message);
1315 g_object_unref (file);
1319 free_space = g_file_info_get_attribute_uint64 (info,
1320 G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
1321 file_size = empathy_ft_handler_get_total_bytes (handler);
1323 g_object_unref (info);
1325 if (file_size > free_space)
1327 GtkWidget *message = gtk_message_dialog_new (GTK_WINDOW (dialog),
1328 GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
1330 _("Insufficient free space to save file"));
1331 char *file_size_str, *free_space_str;
1333 file_size_str = g_format_size (file_size);
1334 free_space_str = g_format_size (free_space);
1336 gtk_message_dialog_format_secondary_text (
1337 GTK_MESSAGE_DIALOG (message),
1338 _("%s of free space are required to save this "
1339 "file, but only %s is available. Please "
1340 "choose another location."),
1341 file_size_str, free_space_str);
1343 gtk_dialog_run (GTK_DIALOG (message));
1345 g_free (file_size_str);
1346 g_free (free_space_str);
1347 gtk_widget_destroy (message);
1349 g_object_unref (file);
1354 factory = empathy_ft_factory_dup_singleton ();
1356 empathy_ft_factory_set_destination_for_incoming_handler (
1357 factory, handler, file);
1359 g_object_unref (factory);
1360 g_object_unref (file);
1364 /* unref the handler, as we dismissed the file chooser,
1365 * and refused the transfer.
1367 g_object_unref (handler);
1370 gtk_widget_destroy (GTK_WIDGET (dialog));
1374 empathy_receive_file_with_file_chooser (EmpathyFTHandler *handler)
1378 EmpathyContact *contact;
1381 contact = empathy_ft_handler_get_contact (handler);
1382 g_assert (contact != NULL);
1384 title = g_strdup_printf (_("Incoming file from %s"),
1385 empathy_contact_get_alias (contact));
1387 widget = gtk_file_chooser_dialog_new (title,
1388 NULL, GTK_FILE_CHOOSER_ACTION_SAVE,
1389 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1390 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
1393 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (widget),
1394 empathy_ft_handler_get_filename (handler));
1396 gtk_file_chooser_set_do_overwrite_confirmation
1397 (GTK_FILE_CHOOSER (widget), TRUE);
1399 dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
1401 /* Fallback to $HOME if $XDG_DOWNLOAD_DIR is not set */
1402 dir = g_get_home_dir ();
1404 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (widget), dir);
1406 g_signal_connect (widget, "response",
1407 G_CALLBACK (file_manager_receive_file_response_cb), handler);
1409 gtk_widget_show (widget);
1414 empathy_make_color_whiter (GdkRGBA *color)
1416 const GdkRGBA white = { 1.0, 1.0, 1.0, 1.0 };
1418 color->red = (color->red + white.red) / 2;
1419 color->green = (color->green + white.green) / 2;
1420 color->blue = (color->blue + white.blue) / 2;
1424 menu_deactivate_cb (GtkMenu *menu,
1427 /* FIXME: we shouldn't have to disconnect the signal (bgo #641327) */
1428 g_signal_handlers_disconnect_by_func (menu,
1429 menu_deactivate_cb, user_data);
1431 gtk_menu_detach (menu);
1434 /* Convenient function to create a GtkMenu attached to @attach_to and detach
1435 * it when the menu is not displayed any more. This is useful when creating a
1436 * context menu that we want to get rid as soon as it as been displayed. */
1438 empathy_context_menu_new (GtkWidget *attach_to)
1442 menu = gtk_menu_new ();
1444 gtk_menu_attach_to_widget (GTK_MENU (menu), attach_to, NULL);
1446 /* menu is initially unowned but gtk_menu_attach_to_widget () taked its
1447 * floating ref. We can either wait that @attach_to releases its ref when
1448 * it will be destroyed (when leaving Empathy most of the time) or explicitely
1449 * detach the menu when it's not displayed any more.
1450 * We go for the latter as we don't want to keep useless menus in memory
1451 * during the whole lifetime of Empathy. */
1452 g_signal_connect (menu, "deactivate", G_CALLBACK (menu_deactivate_cb), NULL);
1458 empathy_get_current_action_time (void)
1460 return (tp_user_action_time_from_x11 (gtk_get_current_event_time ()));
1463 /* @words = empathy_live_search_strip_utf8_string (@text);
1465 * User has to pass both so we don't have to compute @words ourself each time
1466 * this function is called. */
1468 empathy_individual_match_string (FolksIndividual *individual,
1475 gboolean retval = FALSE;
1477 /* check alias name */
1478 str = folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual));
1480 if (empathy_live_search_match_words (str, words))
1483 personas = folks_individual_get_personas (individual);
1485 /* check contact id, remove the @server.com part */
1486 iter = gee_iterable_iterator (GEE_ITERABLE (personas));
1487 while (retval == FALSE && gee_iterator_next (iter))
1489 FolksPersona *persona = gee_iterator_get (iter);
1492 if (empathy_folks_persona_is_interesting (persona))
1494 str = folks_persona_get_display_id (persona);
1496 /* Accept the persona if @text is a full prefix of his ID; that allows
1497 * user to find, say, a jabber contact by typing his JID. */
1498 if (g_str_has_prefix (str, text))
1504 gchar *dup_str = NULL;
1507 p = strstr (str, "@");
1509 str = dup_str = g_strndup (str, p - str);
1511 visible = empathy_live_search_match_words (str, words);
1517 g_clear_object (&persona);
1519 g_clear_object (&iter);
1521 /* FIXME: Add more rules here, we could check phone numbers in
1522 * contact's vCard for example. */
1527 empathy_launch_program (const gchar *dir,
1531 GdkDisplay *display;
1532 GError *error = NULL;
1535 GdkAppLaunchContext *context = NULL;
1537 /* Try to run from source directory if possible */
1538 path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
1541 if (!g_file_test (path, G_FILE_TEST_EXISTS))
1544 path = g_build_filename (dir, name, NULL);
1548 cmd = g_strconcat (path, " ", args, NULL);
1550 cmd = g_strdup (path);
1552 app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
1553 if (app_info == NULL)
1555 DEBUG ("Failed to create app info: %s", error->message);
1556 g_error_free (error);
1560 display = gdk_display_get_default ();
1561 context = gdk_display_get_app_launch_context (display);
1563 if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
1566 g_warning ("Failed to launch %s: %s", name, error->message);
1567 g_error_free (error);
1572 tp_clear_object (&app_info);
1573 tp_clear_object (&context);
1578 /* Most of the workspace manipulation code has been copied from libwnck
1579 * Copyright (C) 2001 Havoc Pennington
1580 * Copyright (C) 2005-2007 Vincent Untz
1583 _wnck_activate_workspace (Screen *screen,
1584 int new_active_space,
1591 display = DisplayOfScreen (screen);
1592 root = RootWindowOfScreen (screen);
1594 xev.xclient.type = ClientMessage;
1595 xev.xclient.serial = 0;
1596 xev.xclient.send_event = True;
1597 xev.xclient.display = display;
1598 xev.xclient.window = root;
1599 xev.xclient.message_type = gdk_x11_get_xatom_by_name ("_NET_CURRENT_DESKTOP");
1600 xev.xclient.format = 32;
1601 xev.xclient.data.l[0] = new_active_space;
1602 xev.xclient.data.l[1] = timestamp;
1603 xev.xclient.data.l[2] = 0;
1604 xev.xclient.data.l[3] = 0;
1605 xev.xclient.data.l[4] = 0;
1607 gdk_error_trap_push ();
1608 XSendEvent (display, root, False,
1609 SubstructureRedirectMask | SubstructureNotifyMask,
1611 XSync (display, False);
1612 gdk_error_trap_pop_ignored ();
1616 _wnck_get_cardinal (Screen *screen,
1629 display = DisplayOfScreen (screen);
1633 gdk_error_trap_push ();
1635 result = XGetWindowProperty (display, xwindow, atom,
1636 0, G_MAXLONG, False, XA_CARDINAL, &type, &format, &nitems,
1637 &bytes_after, (void *) &num);
1638 err = gdk_error_trap_pop ();
1639 if (err != Success ||
1643 if (type != XA_CARDINAL)
1657 window_get_workspace (Screen *xscreen,
1662 if (!_wnck_get_cardinal (xscreen, win,
1663 gdk_x11_get_xatom_by_name ("_NET_WM_DESKTOP"), &number))
1669 /* Ask X to move to the desktop on which @window currently is
1670 * and the present @window. */
1672 empathy_move_to_window_desktop (GtkWindow *window,
1677 GdkWindow *gdk_window;
1680 screen = gtk_window_get_screen (window);
1681 xscreen = gdk_x11_screen_get_xscreen (screen);
1682 gdk_window = gtk_widget_get_window (GTK_WIDGET (window));
1684 workspace = window_get_workspace (xscreen,
1685 gdk_x11_window_get_xid (gdk_window));
1686 if (workspace == -1)
1689 _wnck_activate_workspace (xscreen, workspace, timestamp);
1692 gtk_window_present_with_time (window, timestamp);
1696 empathy_set_css_provider (GtkWidget *widget)
1698 GtkCssProvider *provider;
1700 GError *error = NULL;
1703 filename = empathy_file_lookup ("empathy.css", "data");
1705 provider = gtk_css_provider_new ();
1707 if (!gtk_css_provider_load_from_path (provider, filename, &error))
1709 g_warning ("Failed to load css file '%s': %s", filename, error->message);
1710 g_error_free (error);
1715 screen = gtk_widget_get_screen (widget);
1717 screen = gdk_screen_get_default ();
1719 gtk_style_context_add_provider_for_screen (screen,
1720 GTK_STYLE_PROVIDER (provider),
1721 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
1725 g_object_unref (provider);