]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-status-icon.c
[darcs-to-svn @ New object: EmpathyStatusIcon]
[empathy.git] / libempathy-gtk / empathy-status-icon.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <glib.h>
28 #include <gtk/gtk.h>
29
30 #include <libtelepathy/tp-helpers.h>
31
32 #include <libmissioncontrol/mission-control.h>
33
34 #include <libempathy/gossip-debug.h>
35 #include <libempathy/gossip-utils.h>
36
37 #include "empathy-status-icon.h"
38 #include "gossip-ui-utils.h"
39
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41                        EMPATHY_TYPE_STATUS_ICON, EmpathyStatusIconPriv))
42
43 #define DEBUG_DOMAIN "StatusIcon"
44
45 struct _EmpathyStatusIconPriv {
46         MissionControl *mc;
47         GtkStatusIcon  *icon;
48 };
49
50 static void empathy_status_icon_class_init  (EmpathyStatusIconClass          *klass);
51 static void empathy_status_icon_init        (EmpathyStatusIcon               *icon);
52 static void status_icon_finalize            (GObject                         *object);
53 static void status_icon_presence_changed_cb (MissionControl                  *mc,
54                                              McPresence                       state,
55                                              EmpathyStatusIcon               *icon);
56 static void status_icon_activate_cb         (GtkStatusIcon                   *status_icon,
57                                              EmpathyStatusIcon               *icon);
58
59 enum {
60         ACTIVATE,
61         LAST_SIGNAL
62 };
63
64 static guint signals[LAST_SIGNAL];
65
66 G_DEFINE_TYPE (EmpathyStatusIcon, empathy_status_icon, G_TYPE_OBJECT);
67
68 static void
69 empathy_status_icon_class_init (EmpathyStatusIconClass *klass)
70 {
71         GObjectClass *object_class = G_OBJECT_CLASS (klass);
72
73         object_class->finalize = status_icon_finalize;
74
75         signals[ACTIVATE] =
76                 g_signal_new ("activate",
77                               G_TYPE_FROM_CLASS (klass),
78                               G_SIGNAL_RUN_LAST,
79                               0,
80                               NULL, NULL,
81                               g_cclosure_marshal_VOID__VOID,
82                               G_TYPE_NONE,
83                               0);
84
85         g_type_class_add_private (object_class, sizeof (EmpathyStatusIconPriv));
86 }
87
88 static void
89 empathy_status_icon_init (EmpathyStatusIcon *icon)
90 {
91         EmpathyStatusIconPriv *priv;
92         McPresence             state;
93
94         priv = GET_PRIV (icon);
95
96         priv->mc = mission_control_new (tp_get_bus ());
97         priv->icon = gtk_status_icon_new ();
98
99         dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
100                                      "PresenceStatusActual",
101                                      G_CALLBACK (status_icon_presence_changed_cb),
102                                      icon, NULL);
103         g_signal_connect (priv->icon, "activate",
104                           G_CALLBACK (status_icon_activate_cb),
105                           icon);
106
107         state = mission_control_get_presence_actual (priv->mc, NULL);
108         status_icon_presence_changed_cb (priv->mc, state, icon);
109 }
110
111 static void
112 status_icon_finalize (GObject *object)
113 {
114         EmpathyStatusIconPriv *priv;
115
116         priv = GET_PRIV (object);
117
118         dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->mc),
119                                         "PresenceStatusActual",
120                                         G_CALLBACK (status_icon_presence_changed_cb),
121                                         object);
122         g_signal_handlers_disconnect_by_func (priv->icon,
123                                               status_icon_activate_cb,
124                                               object);
125
126         g_object_unref (priv->mc);
127         g_object_unref (priv->icon);
128 }
129
130 EmpathyStatusIcon *
131 empathy_status_icon_new (void)
132 {
133         return g_object_new (EMPATHY_TYPE_STATUS_ICON, NULL);
134 }
135
136 static void
137 status_icon_presence_changed_cb (MissionControl    *mc,
138                                  McPresence         state,
139                                  EmpathyStatusIcon *icon)
140 {
141         EmpathyStatusIconPriv *priv;
142         const gchar           *icon_name;
143         gchar                 *status;
144
145         priv = GET_PRIV (icon);
146
147         icon_name = gossip_icon_name_for_presence_state (state);
148         status = mission_control_get_presence_message_actual (priv->mc, NULL);
149         if (G_STR_EMPTY (status)) {
150                 g_free (status);
151                 status = g_strdup (gossip_presence_state_get_default_status (state));
152         }
153
154         gtk_status_icon_set_from_icon_name (priv->icon, icon_name);
155         gtk_status_icon_set_tooltip (priv->icon, status);
156
157         g_free (status);
158 }
159
160 static void
161 status_icon_activate_cb (GtkStatusIcon     *status_icon,
162                          EmpathyStatusIcon *icon)
163 {
164         g_signal_emit (icon, signals[ACTIVATE], 0);
165 }
166