~bcurtiswx/ubuntu/precise/empathy/3.4.2.1-0ubuntu1

« back to all changes in this revision

Viewing changes to libempathy/empathy-idle.c

  • Committer: Bazaar Package Importer
  • Author(s): Sjoerd Simons
  • Date: 2007-05-20 15:31:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070520153142-r3auwguxdgxhktqb
Tags: upstream-0.4
ImportĀ upstreamĀ versionĀ 0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <glib/gi18n.h>
 
26
#include <dbus/dbus-glib.h>
 
27
 
 
28
#include <libtelepathy/tp-helpers.h>
 
29
 
 
30
#include <libmissioncontrol/mission-control.h>
 
31
 
 
32
#include "empathy-idle.h"
 
33
#include "gossip-utils.h" 
 
34
#include "gossip-debug.h"
 
35
 
 
36
#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
 
37
                       EMPATHY_TYPE_IDLE, EmpathyIdlePriv))
 
38
 
 
39
#define DEBUG_DOMAIN "Idle"
 
40
 
 
41
/* Number of seconds before entering extended autoaway. */
 
42
#define EXT_AWAY_TIME (30*60)
 
43
 
 
44
enum {
 
45
        LAST_SIGNAL
 
46
};
 
47
 
 
48
struct _EmpathyIdlePriv {
 
49
        MissionControl *mc;
 
50
        DBusGProxy     *gs_proxy;
 
51
        gboolean        is_idle;
 
52
        McPresence      last_state;
 
53
        gchar          *last_status;
 
54
        guint           ext_away_timeout;
 
55
};
 
56
 
 
57
static void     empathy_idle_class_init      (EmpathyIdleClass *klass);
 
58
static void     empathy_idle_init            (EmpathyIdle      *idle);
 
59
static void     idle_finalize                (GObject          *object);
 
60
static void     idle_session_idle_changed_cb (DBusGProxy       *gs_proxy,
 
61
                                              gboolean          is_idle,
 
62
                                              EmpathyIdle      *idle);
 
63
static void     idle_ext_away_start          (EmpathyIdle      *idle);
 
64
static void     idle_ext_away_stop           (EmpathyIdle      *idle);
 
65
static gboolean idle_ext_away_cb             (EmpathyIdle      *idle);
 
66
 
 
67
//static guint signals[LAST_SIGNAL];
 
68
 
 
69
G_DEFINE_TYPE (EmpathyIdle, empathy_idle, G_TYPE_OBJECT)
 
70
 
 
71
static void
 
72
empathy_idle_class_init (EmpathyIdleClass *klass)
 
73
{
 
74
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
75
 
 
76
        object_class->finalize = idle_finalize;
 
77
 
 
78
        g_type_class_add_private (object_class, sizeof (EmpathyIdlePriv));
 
79
}
 
80
 
 
81
static void
 
82
empathy_idle_init (EmpathyIdle *idle)
 
83
{
 
84
        EmpathyIdlePriv *priv;
 
85
 
 
86
        priv = GET_PRIV (idle);
 
87
 
 
88
        priv->is_idle = FALSE;
 
89
        priv->mc = gossip_mission_control_new ();
 
90
        priv->gs_proxy = dbus_g_proxy_new_for_name (tp_get_bus (),
 
91
                                                    "org.gnome.ScreenSaver",
 
92
                                                    "/org/gnome/ScreenSaver",
 
93
                                                    "org.gnome.ScreenSaver");
 
94
        if (!priv->gs_proxy) {
 
95
                gossip_debug (DEBUG_DOMAIN, "Failed to get gs proxy");
 
96
                return;
 
97
        }
 
98
 
 
99
        dbus_g_proxy_add_signal (priv->gs_proxy, "SessionIdleChanged",
 
100
                                 G_TYPE_BOOLEAN,
 
101
                                 G_TYPE_INVALID);
 
102
        dbus_g_proxy_connect_signal (priv->gs_proxy, "SessionIdleChanged",
 
103
                                     G_CALLBACK (idle_session_idle_changed_cb),
 
104
                                     idle, NULL);
 
105
}
 
106
 
 
107
static void
 
108
idle_finalize (GObject *object)
 
109
{
 
110
        EmpathyIdlePriv *priv;
 
111
 
 
112
        priv = GET_PRIV (object);
 
113
 
 
114
        g_free (priv->last_status);
 
115
        g_object_unref (priv->mc);
 
116
 
 
117
        if (priv->gs_proxy) {
 
118
                g_object_unref (priv->gs_proxy);
 
119
        }
 
120
 
 
121
        idle_ext_away_stop (EMPATHY_IDLE (object));
 
122
}
 
123
 
 
124
EmpathyIdle *
 
125
empathy_idle_new (void)
 
126
{
 
127
        static EmpathyIdle *idle = NULL;
 
128
 
 
129
        if (!idle) {
 
130
                idle = g_object_new (EMPATHY_TYPE_IDLE, NULL);
 
131
                g_object_add_weak_pointer (G_OBJECT (idle), (gpointer) &idle);
 
132
        } else {
 
133
                g_object_ref (idle);
 
134
        }
 
135
 
 
136
        return idle;
 
137
}
 
138
 
 
139
static void
 
140
idle_session_idle_changed_cb (DBusGProxy  *gs_proxy,
 
141
                              gboolean     is_idle,
 
142
                              EmpathyIdle *idle)
 
143
{
 
144
        EmpathyIdlePriv *priv;
 
145
 
 
146
        priv = GET_PRIV (idle);
 
147
 
 
148
        gossip_debug (DEBUG_DOMAIN, "Session idle state changed, %s -> %s",
 
149
                      priv->is_idle ? "yes" : "no",
 
150
                      is_idle ? "yes" : "no");
 
151
 
 
152
        if (is_idle && !priv->is_idle) {
 
153
                /* We are now idle, set state to away */
 
154
                g_free (priv->last_status);
 
155
                idle_ext_away_stop (idle);
 
156
 
 
157
                priv->last_state = mission_control_get_presence_actual (priv->mc, NULL);
 
158
                priv->last_status = mission_control_get_presence_message_actual (priv->mc, NULL);
 
159
 
 
160
                gossip_debug (DEBUG_DOMAIN, "Going to autoaway");
 
161
                mission_control_set_presence (priv->mc,
 
162
                                              MC_PRESENCE_AWAY,
 
163
                                              _("Autoaway"),
 
164
                                              NULL, NULL);
 
165
                idle_ext_away_start (idle);
 
166
        } else if (!is_idle && priv->is_idle) {
 
167
                /* We are no more idle, restore state */
 
168
                idle_ext_away_stop (idle);
 
169
 
 
170
                gossip_debug (DEBUG_DOMAIN, "Restoring state to %d %s",
 
171
                              priv->last_state,
 
172
                              priv->last_status);
 
173
 
 
174
                mission_control_set_presence (priv->mc,
 
175
                                              priv->last_state,
 
176
                                              priv->last_status,
 
177
                                              NULL, NULL);
 
178
        }
 
179
 
 
180
        priv->is_idle = is_idle;
 
181
}
 
182
 
 
183
static void
 
184
idle_ext_away_start (EmpathyIdle *idle)
 
185
{
 
186
        EmpathyIdlePriv *priv;
 
187
 
 
188
        priv = GET_PRIV (idle);
 
189
 
 
190
        idle_ext_away_stop (idle);
 
191
        priv->ext_away_timeout = g_timeout_add (EXT_AWAY_TIME * 1000,
 
192
                                                (GSourceFunc) idle_ext_away_cb,
 
193
                                                idle);
 
194
}
 
195
 
 
196
static void
 
197
idle_ext_away_stop (EmpathyIdle *idle)
 
198
{
 
199
        EmpathyIdlePriv *priv;
 
200
 
 
201
        priv = GET_PRIV (idle);
 
202
 
 
203
        if (priv->ext_away_timeout) {
 
204
                g_source_remove (priv->ext_away_timeout);
 
205
                priv->ext_away_timeout = 0;
 
206
        }
 
207
}
 
208
 
 
209
static gboolean
 
210
idle_ext_away_cb (EmpathyIdle *idle)
 
211
{
 
212
        EmpathyIdlePriv *priv;
 
213
 
 
214
        priv = GET_PRIV (idle);
 
215
 
 
216
        gossip_debug (DEBUG_DOMAIN, "Going to extended autoaway");
 
217
        mission_control_set_presence (priv->mc,
 
218
                                      MC_PRESENCE_EXTENDED_AWAY,
 
219
                                      _("Extended autoaway"),
 
220
                                      NULL, NULL);
 
221
 
 
222
        priv->ext_away_timeout = 0;
 
223
 
 
224
        return FALSE;
 
225
}
 
226