2
* * Copyright (C) 2009 Ali <aliov@xfce.org>
4
* Licensed under the GNU General Public License Version 2
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.
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.
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
29
#include <dbus/dbus-glib.h>
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"
38
static void xfpm_disks_finalize (GObject *object);
40
#define XFPM_DISKS_GET_PRIVATE(o) \
41
(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_DISKS, XfpmDisksPrivate))
43
struct XfpmDisksPrivate
56
G_DEFINE_TYPE (XfpmDisks, xfpm_disks, G_TYPE_OBJECT)
59
xfpm_disks_class_init (XfpmDisksClass *klass)
61
GObjectClass *object_class = G_OBJECT_CLASS (klass);
63
object_class->finalize = xfpm_disks_finalize;
65
g_type_class_add_private (klass, sizeof (XfpmDisksPrivate));
69
xfpm_disks_disable_spin_down_timeouts (XfpmDisks *disks)
73
disks->priv->set = FALSE;
75
XFPM_DEBUG ("Clearing spin down timeout");
77
dbus_g_proxy_call (disks->priv->proxy, "DriveUnsetAllSpindownTimeouts", &error,
78
G_TYPE_STRING, disks->priv->cookie,
84
g_warning ("Failed to unset spindown timeouts : %s", error->message);
86
disks->priv->set = TRUE;
89
g_free (disks->priv->cookie);
90
disks->priv->cookie = NULL;
94
xfpm_disks_enable_spin_down_timeouts (XfpmDisks *disks, gint timeout)
97
const gchar **options = { NULL };
99
disks->priv->set = TRUE;
101
XFPM_DEBUG ("Setting spin down timeout %d", timeout);
103
dbus_g_proxy_call (disks->priv->proxy, "DriveSetAllSpindownTimeouts", &error,
105
G_TYPE_STRV, options,
107
G_TYPE_STRING, &disks->priv->cookie,
112
g_warning ("Failed to set spindown timeouts : %s", error->message);
113
g_error_free (error);
114
disks->priv->set = FALSE;
119
xfpm_disks_set_spin_timeouts (XfpmDisks *disks)
125
if (!disks->priv->can_spin )
128
g_object_get (G_OBJECT (disks->priv->power),
129
"on-battery", &on_battery,
134
g_object_get (G_OBJECT (disks->priv->conf),
135
SPIN_DOWN_ON_AC, &enabled,
136
SPIN_DOWN_ON_AC_TIMEOUT, &timeout,
141
g_object_get (G_OBJECT (disks->priv->conf),
142
SPIN_DOWN_ON_BATTERY, &enabled,
143
SPIN_DOWN_ON_BATTERY_TIMEOUT, &timeout,
147
XFPM_DEBUG ("On Battery=%d spin_down_enabled=%d timeout=%d\n", on_battery, enabled, timeout);
151
if ( disks->priv->set && disks->priv->cookie )
152
xfpm_disks_disable_spin_down_timeouts (disks);
154
else if ( timeout != 0 && timeout > 120 && !disks->priv->set)
156
xfpm_disks_enable_spin_down_timeouts (disks, timeout);
161
xfpm_disks_get_is_auth_to_spin (XfpmDisks *disks)
163
const gchar *action_id;
165
action_id = "org.freedesktop.udisks.drive-set-spindown";
167
disks->priv->can_spin = xfpm_polkit_check_auth (disks->priv->polkit,
170
XFPM_DEBUG ("Is auth to spin down disks : %d", disks->priv->can_spin);
174
xfpm_disks_init (XfpmDisks *disks)
176
GError *error = NULL;
178
disks->priv = XFPM_DISKS_GET_PRIVATE (disks);
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;
189
disks->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
193
g_critical ("Unable to get system bus connection : %s", error->message);
194
g_error_free (error);
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");
204
disks->priv->conf = xfpm_xfconf_new ();
205
disks->priv->power = xfpm_power_get ();
206
disks->priv->polkit = xfpm_polkit_get ();
208
xfpm_disks_get_is_auth_to_spin (disks);
210
g_signal_connect_swapped (disks->priv->polkit, "auth-changed",
211
G_CALLBACK (xfpm_disks_get_is_auth_to_spin), disks);
213
g_signal_connect_swapped (disks->priv->power, "on-battery-changed",
214
G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
216
g_signal_connect_swapped (disks->priv->conf, "notify::" SPIN_DOWN_ON_AC,
217
G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
219
g_signal_connect_swapped (disks->priv->conf, "notify::" SPIN_DOWN_ON_AC_TIMEOUT,
220
G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
222
g_signal_connect_swapped (disks->priv->conf, "notify::" SPIN_DOWN_ON_BATTERY,
223
G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
225
g_signal_connect_swapped (disks->priv->conf, "notify::" SPIN_DOWN_ON_BATTERY_TIMEOUT,
226
G_CALLBACK (xfpm_disks_set_spin_timeouts), disks);
228
xfpm_disks_set_spin_timeouts (disks);
236
xfpm_disks_finalize (GObject *object)
240
disks = XFPM_DISKS (object);
242
if (disks->priv->can_spin && disks->priv->set )
243
xfpm_disks_disable_spin_down_timeouts (disks);
245
if ( disks->priv->bus )
246
dbus_g_connection_unref (disks->priv->bus);
248
if ( disks->priv->proxy )
249
g_object_unref (disks->priv->proxy);
251
if ( disks->priv->polkit )
252
g_object_unref (disks->priv->polkit);
254
if ( disks->priv->conf )
255
g_object_unref (disks->priv->conf);
257
if ( disks->priv->power )
258
g_object_unref (disks->priv->power );
260
G_OBJECT_CLASS (xfpm_disks_parent_class)->finalize (object);
264
xfpm_disks_new (void)
266
XfpmDisks *disks = NULL;
267
disks = g_object_new (XFPM_TYPE_DISKS, NULL);
271
gboolean xfpm_disks_get_can_spin (XfpmDisks *disks)
273
return disks->priv->can_spin;
276
gboolean xfpm_disks_kit_is_running (XfpmDisks *disks)
278
return disks->priv->proxy != NULL ? TRUE : FALSE;