Updated Finnish translation
[rhythmbox.git] / plugins / power-manager / rb-power-manager-plugin.c
blob964f7ea2dce770ad76cebc7e2f2144b9d45466d7
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Copyright (C) 2006 Jonathan Matthew <jonathan@kaolin.wh9.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 * gnome-power-manager integration.
23 * currently consists of inhibiting suspend while playing.
26 #include <config.h>
28 #include <glib/gi18n.h>
29 #if WITH_DBUS
30 #include <dbus/dbus-glib.h>
31 #endif
33 #include "rb-plugin.h"
34 #include "rb-debug.h"
36 #define RB_TYPE_GPM_PLUGIN (rb_gpm_plugin_get_type ())
37 #define RB_GPM_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_GPM_PLUGIN, RBGPMPlugin))
38 #define RB_GPM_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_GPM_PLUGIN, RBGPMPluginClass))
39 #define RB_IS_GPM_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_GPM_PLUGIN))
40 #define RB_IS_GPM_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_GPM_PLUGIN))
41 #define RB_GPM_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_GPM_PLUGIN, RBGPMPluginClass))
43 typedef struct
45 RBPlugin parent;
47 DBusGProxy *proxy;
48 guint32 cookie;
49 gint handler_id;
50 gint timeout_id;
51 } RBGPMPlugin;
53 typedef struct
55 RBPluginClass parent_class;
56 } RBGPMPluginClass;
58 G_MODULE_EXPORT GType register_rb_plugin (GTypeModule *module);
59 GType rb_gpm_plugin_get_type (void) G_GNUC_CONST;
61 static void rb_gpm_plugin_init (RBGPMPlugin *plugin);
62 static void impl_activate (RBPlugin *plugin, RBShell *shell);
63 static void impl_deactivate (RBPlugin *plugin, RBShell *shell);
65 RB_PLUGIN_REGISTER(RBGPMPlugin, rb_gpm_plugin)
67 static void
68 rb_gpm_plugin_class_init (RBGPMPluginClass *klass)
70 RBPluginClass *plugin_class = RB_PLUGIN_CLASS (klass);
71 plugin_class->activate = impl_activate;
72 plugin_class->deactivate = impl_deactivate;
75 static void
76 rb_gpm_plugin_init (RBGPMPlugin *plugin)
78 rb_debug ("RBGPMPlugin initialising");
81 static gboolean
82 ignore_error (GError *error)
84 if (error == NULL)
85 return TRUE;
87 /* ignore 'no such service' type errors */
88 if (error->domain == DBUS_GERROR) {
89 if (error->code == DBUS_GERROR_NAME_HAS_NO_OWNER ||
90 error->code == DBUS_GERROR_SERVICE_UNKNOWN)
91 return TRUE;
94 return FALSE;
97 static void
98 inhibit_cb (DBusGProxy *proxy,
99 DBusGProxyCall *call_id,
100 RBGPMPlugin *plugin)
102 GError *error = NULL;
104 dbus_g_proxy_end_call (proxy,
105 call_id,
106 &error,
107 G_TYPE_UINT, &plugin->cookie,
108 G_TYPE_INVALID);
109 if (error != NULL) {
110 if (!ignore_error (error)) {
111 g_warning ("Failed to invoke org.gnome.PowerManager.Inhibit: %s",
112 error->message);
113 } else {
114 rb_debug ("inhibit failed: %s", error->message);
116 g_error_free (error);
117 } else {
118 rb_debug ("got cookie %u", plugin->cookie);
121 g_object_unref (plugin);
124 static gboolean
125 inhibit (RBGPMPlugin *plugin)
127 plugin->timeout_id = 0;
128 if (plugin->proxy == NULL)
129 return FALSE;
131 if (plugin->cookie != 0) {
132 rb_debug ("Was going to inhibit gnome-power-manager, but we already have done");
133 return FALSE;
136 rb_debug ("inhibiting");
137 g_object_ref (plugin);
138 dbus_g_proxy_begin_call (plugin->proxy, "Inhibit",
139 (DBusGProxyCallNotify) inhibit_cb,
140 plugin,
141 NULL,
142 G_TYPE_STRING, _("Music Player"),
143 G_TYPE_STRING, _("Playing"),
144 G_TYPE_INVALID);
146 return FALSE;
149 static void
150 uninhibit_cb (DBusGProxy *proxy,
151 DBusGProxyCall *call_id,
152 RBGPMPlugin *plugin)
154 GError *error = NULL;
156 dbus_g_proxy_end_call (proxy,
157 call_id,
158 &error,
159 G_TYPE_INVALID);
160 if (error != NULL) {
161 if (!ignore_error (error)) {
162 g_warning ("Failed to invoke org.gnome.PowerManager.Inhibit: %s",
163 error->message);
164 } else {
165 rb_debug ("uninhibit failed: %s", error->message);
167 g_error_free (error);
168 } else {
169 rb_debug ("uninhibited");
170 plugin->cookie = 0;
173 g_object_unref (plugin);
176 static gboolean
177 uninhibit (RBGPMPlugin *plugin)
179 plugin->timeout_id = 0;
180 if (plugin->proxy == NULL)
181 return FALSE;
183 if (plugin->cookie == 0) {
184 rb_debug ("Was going to uninhibit power manager, but we haven't inhibited it");
185 return FALSE;
188 rb_debug ("uninhibiting; cookie = %u", plugin->cookie);
189 g_object_ref (plugin);
190 dbus_g_proxy_begin_call (plugin->proxy, "UnInhibit",
191 (DBusGProxyCallNotify) uninhibit_cb,
192 plugin,
193 NULL,
194 G_TYPE_UINT, plugin->cookie,
195 G_TYPE_INVALID);
196 return FALSE;
199 static void
200 playing_changed_cb (GObject *player, gboolean playing, RBGPMPlugin *plugin)
202 if (plugin->timeout_id != 0) {
203 g_source_remove (plugin->timeout_id);
204 plugin->timeout_id = 0;
207 /* small delay to avoid uninhibit/inhibit
208 * cycles when changing sources etc.
210 plugin->timeout_id = g_timeout_add (1000,
211 (GSourceFunc) (playing ? inhibit : uninhibit),
212 plugin);
215 static void
216 impl_activate (RBPlugin *rbplugin,
217 RBShell *shell)
219 RBGPMPlugin *plugin;
220 GError *error = NULL;
221 DBusGConnection *bus;
222 GObject *shell_player;
223 gboolean playing;
225 plugin = RB_GPM_PLUGIN (rbplugin);
227 bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
228 if (bus == NULL) {
229 g_warning ("Couldn't connect to system bus: %s", (error) ? error->message : "(null)");
230 return;
233 plugin->proxy = dbus_g_proxy_new_for_name (bus,
234 "org.gnome.PowerManager",
235 "/org/gnome/PowerManager",
236 "org.gnome.PowerManager");
238 g_object_get (shell, "shell-player", &shell_player, NULL);
240 plugin->handler_id = g_signal_connect (shell_player,
241 "playing-changed",
242 (GCallback) playing_changed_cb,
243 plugin);
245 g_object_get (shell_player, "playing", &playing, NULL);
246 if (playing) {
247 inhibit (plugin);
250 g_object_unref (shell_player);
253 static void
254 impl_deactivate (RBPlugin *rbplugin,
255 RBShell *shell)
257 RBGPMPlugin *plugin;
258 GObject *shell_player;
260 plugin = RB_GPM_PLUGIN (rbplugin);
262 if (plugin->timeout_id != 0) {
263 g_source_remove (plugin->timeout_id);
264 plugin->timeout_id = 0;
267 if (plugin->cookie != 0) {
268 uninhibit (plugin);
269 plugin->cookie = 0;
272 g_object_get (shell, "shell-player", &shell_player, NULL);
274 g_signal_handler_disconnect (shell_player, plugin->handler_id);
275 plugin->handler_id = 0;
277 g_object_unref (shell_player);
279 g_object_unref (plugin->proxy);
280 plugin->proxy = NULL;