udapted vi.po
[rhythmbox.git] / plugins / power-manager / rb-power-manager-plugin.c
blob61d4f37d068b50ad7194020a99d1951d090b826e
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 #include <dbus/dbus-glib.h>
31 #include "rb-plugin.h"
32 #include "rb-debug.h"
34 #define RB_TYPE_GPM_PLUGIN (rb_gpm_plugin_get_type ())
35 #define RB_GPM_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_GPM_PLUGIN, RBGPMPlugin))
36 #define RB_GPM_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_GPM_PLUGIN, RBGPMPluginClass))
37 #define RB_IS_GPM_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_GPM_PLUGIN))
38 #define RB_IS_GPM_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_GPM_PLUGIN))
39 #define RB_GPM_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_GPM_PLUGIN, RBGPMPluginClass))
41 typedef struct
43 RBPlugin parent;
45 DBusGProxy *proxy;
46 guint32 cookie;
47 gint handler_id;
48 gint timeout_id;
49 } RBGPMPlugin;
51 typedef struct
53 RBPluginClass parent_class;
54 } RBGPMPluginClass;
56 G_MODULE_EXPORT GType register_rb_plugin (GTypeModule *module);
57 GType rb_gpm_plugin_get_type (void) G_GNUC_CONST;
59 static void rb_gpm_plugin_init (RBGPMPlugin *plugin);
60 static void impl_activate (RBPlugin *plugin, RBShell *shell);
61 static void impl_deactivate (RBPlugin *plugin, RBShell *shell);
63 RB_PLUGIN_REGISTER(RBGPMPlugin, rb_gpm_plugin)
65 static void
66 rb_gpm_plugin_class_init (RBGPMPluginClass *klass)
68 RBPluginClass *plugin_class = RB_PLUGIN_CLASS (klass);
69 plugin_class->activate = impl_activate;
70 plugin_class->deactivate = impl_deactivate;
73 static void
74 rb_gpm_plugin_init (RBGPMPlugin *plugin)
76 rb_debug ("RBGPMPlugin initialising");
79 static gboolean
80 ignore_error (GError *error)
82 if (error == NULL)
83 return TRUE;
85 /* ignore 'no such service' type errors */
86 if (error->domain == DBUS_GERROR) {
87 if (error->code == DBUS_GERROR_NAME_HAS_NO_OWNER ||
88 error->code == DBUS_GERROR_SERVICE_UNKNOWN)
89 return TRUE;
92 return FALSE;
95 static void
96 inhibit_cb (DBusGProxy *proxy,
97 DBusGProxyCall *call_id,
98 RBGPMPlugin *plugin)
100 GError *error = NULL;
102 dbus_g_proxy_end_call (proxy,
103 call_id,
104 &error,
105 G_TYPE_UINT, &plugin->cookie,
106 G_TYPE_INVALID);
107 if (error != NULL) {
108 if (!ignore_error (error)) {
109 g_warning ("Failed to invoke org.gnome.PowerManager.Inhibit: %s",
110 error->message);
111 } else {
112 rb_debug ("inhibit failed: %s", error->message);
114 g_error_free (error);
115 } else {
116 rb_debug ("got cookie %u", plugin->cookie);
119 g_object_unref (plugin);
122 static gboolean
123 inhibit (RBGPMPlugin *plugin)
125 plugin->timeout_id = 0;
126 if (plugin->proxy == NULL)
127 return FALSE;
129 if (plugin->cookie != 0) {
130 rb_debug ("Was going to inhibit gnome-power-manager, but we already have done");
131 return FALSE;
134 rb_debug ("inhibiting");
135 g_object_ref (plugin);
136 dbus_g_proxy_begin_call (plugin->proxy, "Inhibit",
137 (DBusGProxyCallNotify) inhibit_cb,
138 plugin,
139 NULL,
140 G_TYPE_STRING, _("Music Player"),
141 G_TYPE_STRING, _("Playing"),
142 G_TYPE_INVALID);
144 return FALSE;
147 static void
148 uninhibit_cb (DBusGProxy *proxy,
149 DBusGProxyCall *call_id,
150 RBGPMPlugin *plugin)
152 GError *error = NULL;
154 dbus_g_proxy_end_call (proxy,
155 call_id,
156 &error,
157 G_TYPE_INVALID);
158 if (error != NULL) {
159 if (!ignore_error (error)) {
160 g_warning ("Failed to invoke org.gnome.PowerManager.Inhibit: %s",
161 error->message);
162 } else {
163 rb_debug ("uninhibit failed: %s", error->message);
165 g_error_free (error);
166 } else {
167 rb_debug ("uninhibited");
168 plugin->cookie = 0;
171 g_object_unref (plugin);
174 static gboolean
175 uninhibit (RBGPMPlugin *plugin)
177 plugin->timeout_id = 0;
178 if (plugin->proxy == NULL)
179 return FALSE;
181 if (plugin->cookie == 0) {
182 rb_debug ("Was going to uninhibit power manager, but we haven't inhibited it");
183 return FALSE;
186 rb_debug ("uninhibiting; cookie = %u", plugin->cookie);
187 g_object_ref (plugin);
188 dbus_g_proxy_begin_call (plugin->proxy, "UnInhibit",
189 (DBusGProxyCallNotify) uninhibit_cb,
190 plugin,
191 NULL,
192 G_TYPE_UINT, plugin->cookie,
193 G_TYPE_INVALID);
194 return FALSE;
197 static void
198 playing_changed_cb (GObject *player, gboolean playing, RBGPMPlugin *plugin)
200 if (plugin->timeout_id != 0) {
201 g_source_remove (plugin->timeout_id);
202 plugin->timeout_id = 0;
205 /* small delay to avoid uninhibit/inhibit
206 * cycles when changing sources etc.
208 plugin->timeout_id = g_timeout_add (1000,
209 (GSourceFunc) (playing ? inhibit : uninhibit),
210 plugin);
213 static void
214 impl_activate (RBPlugin *rbplugin,
215 RBShell *shell)
217 RBGPMPlugin *plugin;
218 GError *error = NULL;
219 DBusGConnection *bus;
220 GObject *shell_player;
221 gboolean playing;
223 plugin = RB_GPM_PLUGIN (rbplugin);
225 bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
226 if (bus == NULL) {
227 g_warning ("Couldn't connect to system bus: %s", (error) ? error->message : "(null)");
228 return;
231 plugin->proxy = dbus_g_proxy_new_for_name (bus,
232 "org.gnome.PowerManager",
233 "/org/gnome/PowerManager",
234 "org.gnome.PowerManager");
236 g_object_get (shell, "shell-player", &shell_player, NULL);
238 plugin->handler_id = g_signal_connect (shell_player,
239 "playing-changed",
240 (GCallback) playing_changed_cb,
241 plugin);
243 g_object_get (shell_player, "playing", &playing, NULL);
244 if (playing) {
245 inhibit (plugin);
248 g_object_unref (shell_player);
251 static void
252 impl_deactivate (RBPlugin *rbplugin,
253 RBShell *shell)
255 RBGPMPlugin *plugin;
256 GObject *shell_player;
258 plugin = RB_GPM_PLUGIN (rbplugin);
260 if (plugin->timeout_id != 0) {
261 g_source_remove (plugin->timeout_id);
262 plugin->timeout_id = 0;
265 if (plugin->cookie != 0) {
266 uninhibit (plugin);
267 plugin->cookie = 0;
270 g_object_get (shell, "shell-player", &shell_player, NULL);
272 g_signal_handler_disconnect (shell_player, plugin->handler_id);
273 plugin->handler_id = 0;
275 g_object_unref (shell_player);
277 g_object_unref (plugin->proxy);
278 plugin->proxy = NULL;