/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2007 Raphaël Slinckx <raphael@slinckx.net>
- * Copyright (C) 2007 Collabora Ltd.
+ * Copyright (C) 2007-2009 Collabora Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <bonobo/bonobo-ui-component.h>
#include <panel-2.0/panel-applet-gconf.h>
#include <gconf/gconf-client.h>
-#include <libgnomevfs/gnome-vfs-utils.h>
#include <libmissioncontrol/mission-control.h>
#include <libmissioncontrol/mc-account.h>
-#include <libempathy/empathy-contact-factory.h>
+#include <libempathy/empathy-tp-contact-factory.h>
+#include <libempathy/empathy-account-manager.h>
#include <libempathy/empathy-contact.h>
#include <libempathy/empathy-contact-list.h>
#include <libempathy/empathy-contact-manager.h>
-#include <libempathy/empathy-debug.h>
#include <libempathy/empathy-utils.h>
#include <libempathy-gtk/empathy-contact-list-view.h>
#include "megaphone-applet.h"
-#define DEBUG_DOMAIN "MegaphoneApplet"
-
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), MEGAPHONE_TYPE_APPLET, MegaphoneAppletPriv))
-
-typedef struct _MegaphoneAppletPriv MegaphoneAppletPriv;
-
-struct _MegaphoneAppletPriv {
- EmpathyContactFactory *factory;
- GtkWidget *image;
- gint image_size;
- EmpathyContact *contact;
- GConfClient *gconf;
- guint gconf_cnxn;
-};
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include <libempathy/empathy-debug.h>
-static void megaphone_applet_finalize (GObject *object);
-static void megaphone_applet_size_allocate_cb (GtkWidget *widget,
- GtkAllocation *allocation,
- MegaphoneApplet *applet);
-static gboolean megaphone_applet_button_press_event_cb (GtkWidget *widget,
- GdkEventButton *event,
- MegaphoneApplet *applet);
-static void megaphone_applet_information_cb (BonoboUIComponent *uic,
- MegaphoneApplet *applet,
- const gchar *verb_name);
-static void megaphone_applet_preferences_cb (BonoboUIComponent *uic,
- MegaphoneApplet *applet,
- const gchar *verb_name);
-static void megaphone_applet_about_cb (BonoboUIComponent *uic,
- MegaphoneApplet *applet,
- const gchar *verb_name);
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, MegaphoneApplet)
+typedef struct {
+ EmpathyTpContactFactory *factory;
+ EmpathyAccountManager *account_manager;
+ McAccount *account;
+ gchar *id;
+ GtkWidget *image;
+ gint image_size;
+ EmpathyContact *contact;
+ GConfClient *gconf;
+ guint gconf_cnxn;
+} MegaphoneAppletPriv;
G_DEFINE_TYPE(MegaphoneApplet, megaphone_applet, PANEL_TYPE_APPLET)
-static const BonoboUIVerb megaphone_applet_menu_verbs [] = {
- BONOBO_UI_UNSAFE_VERB ("information", megaphone_applet_information_cb),
- BONOBO_UI_UNSAFE_VERB ("preferences", megaphone_applet_preferences_cb),
- BONOBO_UI_UNSAFE_VERB ("about", megaphone_applet_about_cb),
- BONOBO_UI_VERB_END
-};
-
-static const char* authors[] = {
- "Raphaël Slinckx <raphael@slinckx.net>",
- "Xavier Claessens <xclaesse@gmail.com>",
- NULL
-};
-
-static void
-megaphone_applet_class_init (MegaphoneAppletClass *class)
-{
- GObjectClass *object_class;
-
- object_class = G_OBJECT_CLASS (class);
-
- object_class->finalize = megaphone_applet_finalize;
-
- g_type_class_add_private (object_class, sizeof (MegaphoneAppletPriv));
-}
-
static void
-megaphone_applet_init (MegaphoneApplet *applet)
+megaphone_applet_update_icon (MegaphoneApplet *applet)
{
MegaphoneAppletPriv *priv = GET_PRIV (applet);
+ EmpathyAvatar *avatar = NULL;
+ GdkPixbuf *avatar_pixbuf;
- priv->factory = empathy_contact_factory_new ();
- priv->gconf = gconf_client_get_default ();
-
- /* Image holds the contact avatar */
- priv->image = gtk_image_new ();
- gtk_widget_show (priv->image);
- gtk_container_add (GTK_CONTAINER (applet), priv->image);
-
- /* We want transparency */
- panel_applet_set_flags (PANEL_APPLET (applet), PANEL_APPLET_EXPAND_MINOR);
- panel_applet_set_background_widget (PANEL_APPLET (applet), GTK_WIDGET (applet));
-
- /* Listen for clicks on the applet to dispatch a channel */
- g_signal_connect (applet, "button-press-event",
- G_CALLBACK (megaphone_applet_button_press_event_cb),
- applet);
-
- /* Allow to resize our avatar when needed */
- g_signal_connect (applet, "size-allocate",
- G_CALLBACK (megaphone_applet_size_allocate_cb),
- applet);
-}
-
-static void
-megaphone_applet_finalize (GObject *object)
-{
- MegaphoneAppletPriv *priv = GET_PRIV (object);
-
if (priv->contact) {
- g_object_unref (priv->contact);
- }
- g_object_unref (priv->factory);
-
- if (priv->gconf_cnxn != 0) {
- gconf_client_notify_remove (priv->gconf, priv->gconf_cnxn);
+ avatar = empathy_contact_get_avatar (priv->contact);
+ } else {
+ gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
+ GTK_STOCK_PREFERENCES,
+ GTK_ICON_SIZE_MENU);
+ return;
}
- g_object_unref (priv->gconf);
-
- G_OBJECT_CLASS (megaphone_applet_parent_class)->finalize (object);
-}
-
-static gchar *
-megaphone_applet_get_avatar_file (MegaphoneApplet *applet)
-{
- MegaphoneAppletPriv *priv = GET_PRIV (applet);
- const gchar *id;
- gchar *escaped_avatar;
- gchar *avatar_path;
- gchar *avatar_file;
-
- id = empathy_contact_get_id (priv->contact);
- escaped_avatar = gnome_vfs_escape_slashes (id);
-
- avatar_path = g_build_filename (g_get_home_dir (),
- ".gnome2",
- PACKAGE_NAME,
- "megaphone",
- NULL);
- g_mkdir_with_parents (avatar_path, 0700);
-
- avatar_file = g_build_filename (avatar_path, escaped_avatar, NULL);
- g_free (avatar_path);
- g_free (escaped_avatar);
+ if (!avatar) {
+ gchar *avatar_token;
- return avatar_file;
-}
-
-static void
-megaphone_applet_update_avatar (MegaphoneApplet *applet)
-{
- MegaphoneAppletPriv *priv = GET_PRIV (applet);
- GdkPixbuf *avatar;
- gchar *avatar_file;
-
- if (priv->contact == NULL) {
- empathy_debug (DEBUG_DOMAIN, "Update image size: %i",
- priv->image_size - 2);
- gtk_image_set_pixel_size (GTK_IMAGE (priv->image),
- priv->image_size - 2);
- return;
+ /* Try to take avatar from cache */
+ avatar_token = panel_applet_gconf_get_string (PANEL_APPLET (applet),
+ "avatar_token",
+ NULL);
+ if (!EMP_STR_EMPTY (avatar_token)) {
+ empathy_contact_load_avatar_cache (priv->contact, avatar_token);
+ avatar = empathy_contact_get_avatar (priv->contact);
+ }
+ g_free (avatar_token);
}
- empathy_debug (DEBUG_DOMAIN, "Update avatar: %s %i",
- empathy_contact_get_id (priv->contact),
- priv->image_size - 2);
-
- /* Compute the avatar cache file name */
- avatar_file = megaphone_applet_get_avatar_file (applet);
-
- /* Fetch existing avatar */
- avatar = empathy_pixbuf_avatar_from_contact_scaled (priv->contact,
- priv->image_size - 2,
- priv->image_size - 2);
- if (avatar == NULL) {
- /* Try to load a cached avatar */
- avatar = gdk_pixbuf_new_from_file (avatar_file, NULL);
- if (avatar == NULL) {
- GtkIconTheme *icon_theme;
-
- /* Load the default icon when no avatar is found */
- icon_theme = gtk_icon_theme_get_default ();
- avatar = gtk_icon_theme_load_icon (icon_theme,
- "stock_contact",
- priv->image_size - 2,
- 0, NULL);
- }
+ if (avatar) {
+ avatar_pixbuf = empathy_pixbuf_from_avatar_scaled (avatar,
+ priv->image_size - 2,
+ priv->image_size - 2);
} else {
- /* Cache avatar */
- gdk_pixbuf_save (avatar, avatar_file, "png", NULL, NULL);
+ GtkIconTheme *icon_theme;
+
+ /* Load the default icon when no avatar is found */
+ icon_theme = gtk_icon_theme_get_default ();
+ avatar_pixbuf = gtk_icon_theme_load_icon (icon_theme,
+ "stock_contact",
+ priv->image_size - 2,
+ 0, NULL);
}
- g_free (avatar_file);
/* Now some desaturation if the contact is offline */
if (!empathy_contact_is_online (priv->contact)) {
offline_avatar = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
8,
- gdk_pixbuf_get_height (avatar),
- gdk_pixbuf_get_width (avatar));
- gdk_pixbuf_saturate_and_pixelate (avatar,
+ gdk_pixbuf_get_height (avatar_pixbuf),
+ gdk_pixbuf_get_width (avatar_pixbuf));
+ gdk_pixbuf_saturate_and_pixelate (avatar_pixbuf,
offline_avatar,
0.0,
TRUE);
- g_object_unref (avatar);
- avatar = offline_avatar;
+ g_object_unref (avatar_pixbuf);
+ avatar_pixbuf = offline_avatar;
}
- gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), avatar);
- g_object_unref (avatar);
+ gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), avatar_pixbuf);
+ g_object_unref (avatar_pixbuf);
}
static void
const gchar *name;
const gchar *status;
gchar *tip;
+ const gchar *avatar_token = NULL;
- if (priv->contact == NULL) {
- return;
+ if (priv->contact) {
+ EmpathyAvatar *avatar;
+
+ avatar = empathy_contact_get_avatar (priv->contact);
+ if (avatar) {
+ avatar_token = avatar->token;
+ }
}
- empathy_debug (DEBUG_DOMAIN, "Update contact: %s",
- empathy_contact_get_id (priv->contact));
+ if (avatar_token) {
+ panel_applet_gconf_set_string (PANEL_APPLET (applet),
+ "avatar_token", avatar_token,
+ NULL);
+ }
- megaphone_applet_update_avatar (applet);
+ megaphone_applet_update_icon (applet);
- name = empathy_contact_get_name (priv->contact);
- status = empathy_contact_get_status (priv->contact);
- tip = g_strdup_printf ("<b>%s</b>: %s", name, status);
+ if (priv->contact ) {
+ name = empathy_contact_get_name (priv->contact);
+ status = empathy_contact_get_status (priv->contact);
+ tip = g_strdup_printf ("<b>%s</b>: %s", name, status);
+ gtk_widget_set_tooltip_markup (GTK_WIDGET (applet), tip);
+ g_free (tip);
+ } else {
+ gtk_widget_set_tooltip_markup (GTK_WIDGET (applet),
+ _("Please configure a contact."));
+ }
- gtk_widget_set_tooltip_markup (GTK_WIDGET (applet), tip);
- g_free (tip);
}
static void
-megaphone_applet_set_contact (MegaphoneApplet *applet,
- const gchar *str)
+megaphone_applet_got_contact_cb (EmpathyTpContactFactory *factory,
+ GList *contacts,
+ gpointer user_data,
+ GObject *applet)
{
MegaphoneAppletPriv *priv = GET_PRIV (applet);
- McAccount *account = NULL;
- gchar **strv = NULL;
- empathy_debug (DEBUG_DOMAIN, "Setting new contact %s", str);
+ priv->contact = g_object_ref (contacts->data);
+ g_signal_connect_swapped (priv->contact, "notify",
+ G_CALLBACK (megaphone_applet_update_contact),
+ applet);
+ megaphone_applet_update_contact (MEGAPHONE_APPLET (applet));
+}
- /* Release old contact, if any */
- if (priv->contact) {
- g_signal_handlers_disconnect_by_func (priv->contact,
- megaphone_applet_update_contact,
- applet);
- g_object_unref (priv->contact),
- priv->contact = NULL;
- }
+static void
+megaphone_applet_new_connection_cb (EmpathyAccountManager *manager,
+ TpConnection *connection,
+ McAccount *account,
+ MegaphoneApplet *applet)
+{
+ MegaphoneAppletPriv *priv = GET_PRIV (applet);
+ const gchar *id = priv->id;
- /* Lookup the new contact */
- if (str) {
- strv = g_strsplit (str, "/", 2);
- account = mc_account_lookup (strv[0]);
- }
- if (account) {
- priv->contact = empathy_contact_factory_get_from_id (priv->factory,
- account,
- strv[1]);
- g_object_unref (account);
+ if (priv->contact || !empathy_account_equal (account, priv->account)) {
+ return;
}
- g_strfreev (strv);
- /* Take hold of the new contact if any */
- if (priv->contact) {
- /* Listen for updates on the contact, and force a first update */
- g_signal_connect_swapped (priv->contact, "notify",
- G_CALLBACK (megaphone_applet_update_contact),
- applet);
- megaphone_applet_update_contact (applet);
- } else {
- gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
- GTK_STOCK_PREFERENCES,
- GTK_ICON_SIZE_MENU);
- gtk_widget_set_tooltip_markup (GTK_WIDGET (applet),
- "Please configure a contact.");
- }
+ priv->factory = empathy_tp_contact_factory_dup_singleton (connection);
+ empathy_tp_contact_factory_get_from_ids (priv->factory, 1, &id,
+ megaphone_applet_got_contact_cb,
+ NULL, NULL, G_OBJECT (applet));
}
static void
contact_id = empathy_contact_get_id (contact);
str = g_strconcat (account_id, "/", contact_id, NULL);
+ panel_applet_gconf_set_string (PANEL_APPLET (applet),
+ "avatar_token", "",
+ NULL);
panel_applet_gconf_set_string (PANEL_APPLET (applet),
"contact_id", str,
NULL);
EmpathyContactListStore *contact_store;
EmpathyContactManager *contact_manager;
- dialog = gtk_dialog_new_with_buttons ("Select contact...",
+ dialog = gtk_dialog_new_with_buttons (_("Select contact..."),
NULL, 0,
GTK_STOCK_CANCEL,
GTK_RESPONSE_REJECT,
NULL);
/* Show all contacts, even offline and sort alphabetically */
- contact_manager = empathy_contact_manager_new ();
+ contact_manager = empathy_contact_manager_dup_singleton ();
contact_store = empathy_contact_list_store_new (EMPATHY_CONTACT_LIST (contact_manager));
g_object_set (contact_store,
"is-compact", TRUE,
"show-offline", TRUE,
"sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME,
NULL);
- contact_list = empathy_contact_list_view_new (contact_store);
- gtk_tree_view_expand_all (GTK_TREE_VIEW (contact_list));
+ contact_list = empathy_contact_list_view_new (contact_store,
+ EMPATHY_CONTACT_LIST_FEATURE_NONE,
+ EMPATHY_CONTACT_FEATURE_NONE);
g_object_unref (contact_manager);
g_object_unref (contact_store);
gtk_widget_show (GTK_WIDGET (contact_list));
gtk_widget_show (dialog);
}
-static void
-megaphone_applet_information_cb (BonoboUIComponent *uic,
- MegaphoneApplet *applet,
- const gchar *verb_name)
-{
- MegaphoneAppletPriv *priv = GET_PRIV (applet);
-
- /* FIXME: We should grey out the menu item if there are no available contact */
- if (priv->contact) {
- empathy_contact_information_dialog_show (priv->contact, NULL, FALSE);
- }
-}
-
-static void
-megaphone_applet_preferences_cb (BonoboUIComponent *uic,
- MegaphoneApplet *applet,
- const gchar *verb_name)
-{
- megaphone_applet_show_preferences (applet);
-}
-
-static void
-megaphone_applet_about_cb (BonoboUIComponent *uic,
- MegaphoneApplet *applet,
- const gchar *verb_name)
-{
- gtk_show_about_dialog (NULL,
- "name", "Megaphone",
- "version", PACKAGE_VERSION,
- "copyright", "Raphaël Slinckx 2007\nCollabora Ltd 2007",
- "comments", _("Talk!"),
- "authors", authors,
- "logo-icon-name", "stock_people",
- NULL);
-}
-
static gboolean
megaphone_applet_button_press_event_cb (GtkWidget *widget,
GdkEventButton *event,
return TRUE;
}
- empathy_debug (DEBUG_DOMAIN, "Requesting text channel for contact %s (%d)",
- empathy_contact_get_id (priv->contact),
- empathy_contact_get_handle (priv->contact));
+ DEBUG ("Requesting text channel for contact %s (%d)",
+ empathy_contact_get_id (priv->contact),
+ empathy_contact_get_handle (priv->contact));
- mc = empathy_mission_control_new ();
+ mc = empathy_mission_control_dup_singleton ();
mission_control_request_channel (mc,
empathy_contact_get_account (priv->contact),
TP_IFACE_CHANNEL_TYPE_TEXT,
if (size != priv->image_size) {
priv->image_size = size;
- megaphone_applet_update_avatar (applet);
+ megaphone_applet_update_icon (applet);
+ }
+}
+
+static void
+megaphone_applet_finalize (GObject *object)
+{
+ MegaphoneAppletPriv *priv = GET_PRIV (object);
+
+ if (priv->contact) {
+ g_object_unref (priv->contact);
+ }
+ if (priv->factory) {
+ g_object_unref (priv->factory);
+ }
+ if (priv->account_manager) {
+ g_signal_handlers_disconnect_by_func (priv->account_manager,
+ megaphone_applet_new_connection_cb, object);
+ g_object_unref (priv->account_manager);
+ }
+
+ if (priv->gconf_cnxn != 0) {
+ gconf_client_notify_remove (priv->gconf, priv->gconf_cnxn);
+ }
+ g_object_unref (priv->gconf);
+ g_free (priv->id);
+
+ G_OBJECT_CLASS (megaphone_applet_parent_class)->finalize (object);
+}
+
+static void
+megaphone_applet_class_init (MegaphoneAppletClass *class)
+{
+ GObjectClass *object_class;
+
+ object_class = G_OBJECT_CLASS (class);
+
+ object_class->finalize = megaphone_applet_finalize;
+
+ g_type_class_add_private (object_class, sizeof (MegaphoneAppletPriv));
+ empathy_gtk_init ();
+}
+
+static void
+megaphone_applet_init (MegaphoneApplet *applet)
+{
+ MegaphoneAppletPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (applet,
+ MEGAPHONE_TYPE_APPLET, MegaphoneAppletPriv);
+
+ applet->priv = priv;
+ priv->gconf = gconf_client_get_default ();
+ priv->account_manager = empathy_account_manager_dup_singleton ();
+ g_signal_connect (priv->account_manager, "new-connection",
+ G_CALLBACK (megaphone_applet_new_connection_cb), applet);
+
+ /* Image holds the contact avatar */
+ priv->image = gtk_image_new ();
+ gtk_widget_show (priv->image);
+ gtk_container_add (GTK_CONTAINER (applet), priv->image);
+
+ /* We want transparency */
+ panel_applet_set_flags (PANEL_APPLET (applet), PANEL_APPLET_EXPAND_MINOR);
+ panel_applet_set_background_widget (PANEL_APPLET (applet), GTK_WIDGET (applet));
+
+ /* Listen for clicks on the applet to dispatch a channel */
+ g_signal_connect (applet, "button-press-event",
+ G_CALLBACK (megaphone_applet_button_press_event_cb),
+ applet);
+
+ /* Allow to resize our avatar when needed */
+ g_signal_connect (applet, "size-allocate",
+ G_CALLBACK (megaphone_applet_size_allocate_cb),
+ applet);
+}
+
+static void
+megaphone_applet_set_contact (MegaphoneApplet *applet,
+ const gchar *str)
+{
+ MegaphoneAppletPriv *priv = GET_PRIV (applet);
+ TpConnection *connection;
+ gchar **strv = NULL;
+
+ DEBUG ("Setting new contact %s", str);
+
+ /* Release old contact, if any */
+ if (priv->contact) {
+ g_signal_handlers_disconnect_by_func (priv->contact,
+ megaphone_applet_update_contact,
+ applet);
+ g_object_unref (priv->contact),
+ priv->contact = NULL;
+ }
+ if (priv->account) {
+ g_object_unref (priv->account),
+ priv->account = NULL;
+ }
+ if (priv->factory) {
+ g_object_unref (priv->factory),
+ priv->factory = NULL;
+ }
+
+ /* Lookup the new contact */
+ if (str) {
+ strv = g_strsplit (str, "/", 2);
+ priv->account = mc_account_lookup (strv[0]);
+ priv->id = strv[1];
+ g_free (strv[0]);
+ g_free (strv);
+ }
+
+ if (priv->account) {
+ connection = empathy_account_manager_get_connection (
+ priv->account_manager, priv->account);
+ if (connection) {
+ megaphone_applet_new_connection_cb (priv->account_manager,
+ connection, priv->account, applet);
+ }
}
}
+static void
+megaphone_applet_information_cb (BonoboUIComponent *uic,
+ MegaphoneApplet *applet,
+ const gchar *verb_name)
+{
+ MegaphoneAppletPriv *priv = GET_PRIV (applet);
+
+ /* FIXME: We should grey out the menu item if there are no available contact */
+ if (priv->contact) {
+ empathy_contact_information_dialog_show (priv->contact, NULL);
+ }
+}
+
+static void
+megaphone_applet_preferences_cb (BonoboUIComponent *uic,
+ MegaphoneApplet *applet,
+ const gchar *verb_name)
+{
+ megaphone_applet_show_preferences (applet);
+}
+
static void
megaphone_applet_gconf_notify_cb (GConfClient *client,
guint cnxn_id,
key = gconf_entry_get_key (entry);
value = gconf_entry_get_value (entry);
- empathy_debug (DEBUG_DOMAIN, "GConf notification for key '%s'", key);
+ DEBUG ("GConf notification for key '%s'", key);
if (value && g_str_has_suffix (key, "/contact_id")) {
megaphone_applet_set_contact (applet,
}
}
+static void
+megaphone_applet_about_cb (BonoboUIComponent *uic,
+ MegaphoneApplet *applet,
+ const gchar *verb_name)
+{
+ const char* authors[] = {
+ "Raphaël Slinckx <raphael@slinckx.net>",
+ "Xavier Claessens <xclaesse@gmail.com>",
+ NULL
+ };
+
+ gtk_show_about_dialog (NULL,
+ "name", "Megaphone",
+ "version", PACKAGE_VERSION,
+ "copyright", "Raphaël Slinckx 2007\nCollabora Ltd 2007",
+ "comments", _("Talk!"),
+ "authors", authors,
+ "logo-icon-name", "stock_people",
+ NULL);
+}
+
+static const BonoboUIVerb megaphone_applet_menu_verbs [] = {
+ BONOBO_UI_UNSAFE_VERB ("information", megaphone_applet_information_cb),
+ BONOBO_UI_UNSAFE_VERB ("preferences", megaphone_applet_preferences_cb),
+ BONOBO_UI_UNSAFE_VERB ("about", megaphone_applet_about_cb),
+ BONOBO_UI_VERB_END
+};
+
static gboolean
megaphone_applet_factory (PanelApplet *applet,
const gchar *iid,
return FALSE;
}
- empathy_debug (DEBUG_DOMAIN, "Starting up new instance!");
+ DEBUG ("Starting up new instance!");
/* Set up the right-click menu */
panel_applet_setup_menu_from_file (applet,