~ubuntu-branches/ubuntu/precise/xfce4-power-manager/precise

« back to all changes in this revision

Viewing changes to src/xfpm-disks.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-09 18:28:34 UTC
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: james.westby@ubuntu.com-20101209182834-tjz13qcewqlq19eu
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * * Copyright (C) 2009 Ali <aliov@xfce.org>
 
3
 *
 
4
 * Licensed under the GNU General Public License Version 2
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
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
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
 
 
29
#include <dbus/dbus-glib.h>
 
30
 
 
31
#include "xfpm-disks.h"
 
32
#include "xfpm-polkit.h"
 
33
#include "xfpm-xfconf.h"
 
34
#include "xfpm-power.h"
 
35
#include "xfpm-config.h"
 
36
#include "xfpm-debug.h"
 
37
 
 
38
static void xfpm_disks_finalize   (GObject *object);
 
39
 
 
40
#define XFPM_DISKS_GET_PRIVATE(o) \
 
41
(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_DISKS, XfpmDisksPrivate))
 
42
 
 
43
struct XfpmDisksPrivate
 
44
{
 
45
    XfpmXfconf      *conf;
 
46
    XfpmPower       *power;
 
47
    XfpmPolkit      *polkit;
 
48
    
 
49
    DBusGConnection *bus;
 
50
    DBusGProxy      *proxy;
 
51
    gchar           *cookie;
 
52
    gboolean         set;
 
53
    gboolean         can_spin;
 
54
};
 
55
 
 
56
G_DEFINE_TYPE (XfpmDisks, xfpm_disks, G_TYPE_OBJECT)
 
57
 
 
58
static void
 
59
xfpm_disks_class_init (XfpmDisksClass *klass)
 
60
{
 
61
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
62
 
 
63
    object_class->finalize = xfpm_disks_finalize;
 
64
 
 
65
    g_type_class_add_private (klass, sizeof (XfpmDisksPrivate));
 
66
}
 
67
 
 
68
static void
 
69
xfpm_disks_disable_spin_down_timeouts (XfpmDisks *disks)
 
70
{
 
71
    GError *error = NULL;
 
72
    
 
73
    disks->priv->set = FALSE;
 
74
    
 
75
    XFPM_DEBUG ("Clearing spin down timeout");
 
76
    
 
77
    dbus_g_proxy_call (disks->priv->proxy, "DriveUnsetAllSpindownTimeouts", &error,
 
78
                       G_TYPE_STRING, disks->priv->cookie,
 
79
                       G_TYPE_INVALID,
 
80
                       G_TYPE_INVALID);
 
81
                       
 
82
    if ( error )
 
83
    {
 
84
        g_warning ("Failed to unset spindown timeouts : %s", error->message);
 
85
        g_error_free (error);
 
86
        disks->priv->set = TRUE;
 
87
    }
 
88
    
 
89
    g_free (disks->priv->cookie);
 
90
    disks->priv->cookie = NULL;
 
91
}
 
92
 
 
93
static void
 
94
xfpm_disks_enable_spin_down_timeouts (XfpmDisks *disks, gint timeout)
 
95
{
 
96
    GError *error = NULL;
 
97
    const gchar **options = { NULL };
 
98
    
 
99
    disks->priv->set = TRUE;
 
100
    
 
101
    XFPM_DEBUG ("Setting spin down timeout %d", timeout);
 
102
    
 
103
    dbus_g_proxy_call (disks->priv->proxy, "DriveSetAllSpindownTimeouts", &error,
 
104
                       G_TYPE_INT, timeout,
 
105
                       G_TYPE_STRV, options,
 
106
                       G_TYPE_INVALID,
 
107
                       G_TYPE_STRING, &disks->priv->cookie,
 
108
                       G_TYPE_INVALID);
 
109
                       
 
110
    if ( error )
 
111
    {
 
112
        g_warning ("Failed to set spindown timeouts : %s", error->message);
 
113
        g_error_free (error);
 
114
        disks->priv->set = FALSE;
 
115
    }
 
116
}
 
117
 
 
118
static void
 
119
xfpm_disks_set_spin_timeouts (XfpmDisks *disks)
 
120
{
 
121
    gboolean enabled;
 
122
    gboolean on_battery;
 
123
    gint timeout = 0;
 
124
    
 
125
    if (!disks->priv->can_spin )
 
126
        return;
 
127
    
 
128
    g_object_get (G_OBJECT (disks->priv->power),
 
129
                  "on-battery", &on_battery,
 
130
                  NULL);
 
131
 
 
132
    if ( !on_battery )
 
133
    {
 
134
        g_object_get (G_OBJECT (disks->priv->conf),
 
135
                      SPIN_DOWN_ON_AC, &enabled,
 
136
                      SPIN_DOWN_ON_AC_TIMEOUT, &timeout,
 
137
                      NULL);
 
138
    }
 
139
    else
 
140
    {
 
141
        g_object_get (G_OBJECT (disks->priv->conf),
 
142
                      SPIN_DOWN_ON_BATTERY, &enabled,
 
143
                      SPIN_DOWN_ON_BATTERY_TIMEOUT, &timeout,
 
144
                      NULL);
 
145
    }
 
146
    
 
147
    XFPM_DEBUG ("On Battery=%d spin_down_enabled=%d timeout=%d\n", on_battery, enabled, timeout);
 
148
    
 
149
    if ( !enabled )
 
150
    {
 
151
        if ( disks->priv->set && disks->priv->cookie )
 
152
            xfpm_disks_disable_spin_down_timeouts (disks);
 
153
    }
 
154
    else if ( timeout != 0 && timeout > 120 && !disks->priv->set)
 
155
    {
 
156
        xfpm_disks_enable_spin_down_timeouts (disks, timeout);
 
157
    }
 
158
}
 
159
 
 
160
static void
 
161
xfpm_disks_get_is_auth_to_spin (XfpmDisks *disks)
 
162
{
 
163
    const gchar *action_id;
 
164
 
 
165
    action_id = "org.freedesktop.udisks.drive-set-spindown";
 
166
 
 
167
    disks->priv->can_spin = xfpm_polkit_check_auth (disks->priv->polkit, 
 
168
                                                    action_id);
 
169
                                                    
 
170
    XFPM_DEBUG ("Is auth to spin down disks : %d", disks->priv->can_spin);
 
171
}
 
172
 
 
173
static void
 
174
xfpm_disks_init (XfpmDisks *disks)
 
175
{
 
176
    GError *error = NULL;
 
177
    
 
178
    disks->priv = XFPM_DISKS_GET_PRIVATE (disks);
 
179
    
 
180
    disks->priv->can_spin = FALSE;
 
181
    disks->priv->bus    = NULL;
 
182
    disks->priv->proxy  = NULL;
 
183
    disks->priv->conf   = NULL;
 
184
    disks->priv->power  = NULL;
 
185
    disks->priv->cookie = NULL;
 
186
    disks->priv->polkit = NULL;
 
187
 
 
188
    
 
189
    disks->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
 
190
    
 
191
    if ( error )
 
192
    {
 
193
        g_critical ("Unable to get system bus connection : %s", error->message);
 
194
        g_error_free (error);
 
195
        goto out;
 
196
    }
 
197
 
 
198
    disks->priv->proxy = dbus_g_proxy_new_for_name (disks->priv->bus,
 
199
                                                    "org.freedesktop.UDisks",
 
200
                                                    "/org/freedesktop/UDisks",
 
201
                                                    "org.freedesktop.UDisks");
 
202
    
 
203
 
 
204
    disks->priv->conf = xfpm_xfconf_new ();
 
205
    disks->priv->power  = xfpm_power_get ();
 
206
    disks->priv->polkit = xfpm_polkit_get ();
 
207
 
 
208
    xfpm_disks_get_is_auth_to_spin (disks);
 
209
    
 
210
    g_signal_connect_swapped (disks->priv->polkit, "auth-changed",
 
211
                              G_CALLBACK (xfpm_disks_get_is_auth_to_spin), disks);
 
212
 
 
213
    g_signal_connect_swapped (disks->priv->power, "on-battery-changed",
 
214
                              G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
 
215
 
 
216
    g_signal_connect_swapped (disks->priv->conf, "notify::" SPIN_DOWN_ON_AC,
 
217
                              G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
 
218
                        
 
219
    g_signal_connect_swapped (disks->priv->conf, "notify::" SPIN_DOWN_ON_AC_TIMEOUT,
 
220
                              G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
 
221
                              
 
222
    g_signal_connect_swapped (disks->priv->conf, "notify::" SPIN_DOWN_ON_BATTERY,
 
223
                              G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
 
224
    
 
225
    g_signal_connect_swapped (disks->priv->conf, "notify::" SPIN_DOWN_ON_BATTERY_TIMEOUT,
 
226
                              G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
 
227
                              
 
228
    xfpm_disks_set_spin_timeouts (disks);
 
229
    
 
230
out:
 
231
    ;
 
232
 
 
233
}
 
234
 
 
235
static void
 
236
xfpm_disks_finalize (GObject *object)
 
237
{
 
238
    XfpmDisks *disks;
 
239
 
 
240
    disks = XFPM_DISKS (object);
 
241
 
 
242
    if (disks->priv->can_spin && disks->priv->set )
 
243
        xfpm_disks_disable_spin_down_timeouts (disks);
 
244
    
 
245
    if ( disks->priv->bus )
 
246
        dbus_g_connection_unref (disks->priv->bus);
 
247
        
 
248
    if ( disks->priv->proxy )
 
249
        g_object_unref (disks->priv->proxy);
 
250
        
 
251
    if ( disks->priv->polkit )
 
252
        g_object_unref (disks->priv->polkit);
 
253
        
 
254
    if ( disks->priv->conf )
 
255
        g_object_unref (disks->priv->conf);
 
256
        
 
257
    if ( disks->priv->power )
 
258
        g_object_unref (disks->priv->power );
 
259
 
 
260
    G_OBJECT_CLASS (xfpm_disks_parent_class)->finalize (object);
 
261
}
 
262
 
 
263
XfpmDisks *
 
264
xfpm_disks_new (void)
 
265
{
 
266
    XfpmDisks *disks = NULL;
 
267
    disks = g_object_new (XFPM_TYPE_DISKS, NULL);
 
268
    return disks;
 
269
}
 
270
 
 
271
gboolean xfpm_disks_get_can_spin (XfpmDisks *disks)
 
272
{
 
273
    return disks->priv->can_spin;
 
274
}
 
275
 
 
276
gboolean xfpm_disks_kit_is_running (XfpmDisks *disks)
 
277
{
 
278
    return disks->priv->proxy != NULL ? TRUE : FALSE;
 
279
}