~ubuntu-branches/ubuntu/lucid/gnome-power-manager/lucid

2.1.2 by Oliver Grawert
Import upstream version 0.3.4
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
 *
2.1.18 by Oliver Grawert
Import upstream version 2.17.91
3
 * Copyright (C) 2005-2007 Richard Hughes <richard@hughsie.com>
2.1.3 by Daniel Silverstone
Import upstream version 2.13.90
4
 *
5
 * Taken in part from:
6
 *  - lshal   (C) 2003 David Zeuthen, <david@fubar.dk>
7
 *  - notibat (C) 2004 Benjamin Kahn, <xkahn@zoned.net>
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
8
 *
9
 * Licensed under the GNU General Public License Version 2
10
 *
2.1.3 by Daniel Silverstone
Import upstream version 2.13.90
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
2.1.49 by Martin Pitt
Import upstream version 2.29.2
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
24
 */
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
25
26
#include "config.h"
27
28
#include <string.h>
29
#include <unistd.h>
2.1.39 by Martin Pitt
Import upstream version 2.26.1
30
#include <stdlib.h>
31
#include <errno.h>
32
#include <locale.h>
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
33
#include <glib.h>
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
34
#include <glib/gi18n.h>
2.1.39 by Martin Pitt
Import upstream version 2.26.1
35
#include <gtk/gtk.h>
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
36
#include <dbus/dbus-glib.h>
37
#include <dbus/dbus-glib-lowlevel.h>
38
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
39
#include "gpm-stock-icons.h"
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
40
#include "gpm-common.h"
2.1.39 by Martin Pitt
Import upstream version 2.26.1
41
#include "gpm-manager.h"
42
#include "gpm-session.h"
43
2.1.42 by Martin Pitt
Import upstream version 2.27.2+git20090729
44
#include "org.gnome.PowerManager.h"
2.1.39 by Martin Pitt
Import upstream version 2.26.1
45
2.1.37 by Ted Gould
Import upstream version 2.24.0
46
#include "egg-debug.h"
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
47
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
48
/**
49
 * gpm_object_register:
50
 * @connection: What we want to register to
51
 * @object: The GObject we want to register
52
 *
2.1.42 by Martin Pitt
Import upstream version 2.27.2+git20090729
53
 * Register org.gnome.PowerManager on the session bus.
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
54
 * This function MUST be called before DBUS service will work.
55
 *
56
 * Return value: success
57
 **/
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
58
static gboolean
59
gpm_object_register (DBusGConnection *connection,
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
60
		     GObject	     *object)
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
61
{
62
	DBusGProxy *bus_proxy = NULL;
63
	GError *error = NULL;
64
	guint request_name_result;
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
65
	gboolean ret;
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
66
2.1.3 by Daniel Silverstone
Import upstream version 2.13.90
67
	bus_proxy = dbus_g_proxy_new_for_name (connection,
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
68
					       DBUS_SERVICE_DBUS,
69
					       DBUS_PATH_DBUS,
70
					       DBUS_INTERFACE_DBUS);
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
71
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
72
	ret = dbus_g_proxy_call (bus_proxy, "RequestName", &error,
73
				 G_TYPE_STRING, GPM_DBUS_SERVICE,
74
				 G_TYPE_UINT, 0,
75
				 G_TYPE_INVALID,
76
				 G_TYPE_UINT, &request_name_result,
77
				 G_TYPE_INVALID);
78
	if (error) {
2.1.37 by Ted Gould
Import upstream version 2.24.0
79
		egg_debug ("ERROR: %s", error->message);
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
80
		g_error_free (error);
81
	}
2.1.35 by Ted Gould
Import upstream version 2.23.6
82
	if (!ret) {
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
83
		/* abort as the DBUS method failed */
2.1.37 by Ted Gould
Import upstream version 2.24.0
84
		egg_warning ("RequestName failed!");
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
85
		return FALSE;
86
	}
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
87
88
	/* free the bus_proxy */
89
	g_object_unref (G_OBJECT (bus_proxy));
90
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
91
	/* already running */
92
 	if (request_name_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
93
		return FALSE;
94
	}
95
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
96
	dbus_g_object_type_install_info (GPM_TYPE_MANAGER, &dbus_glib_gpm_manager_object_info);
2.1.23 by Oliver Grawert
Import upstream version 2.19.2
97
	dbus_g_error_domain_register (GPM_MANAGER_ERROR, NULL, GPM_MANAGER_TYPE_ERROR);
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
98
	dbus_g_connection_register_g_object (connection, GPM_DBUS_PATH, object);
99
100
	return TRUE;
101
}
102
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
103
/**
2.1.12 by Brandon Hale
Import upstream version 2.15.92
104
 * timed_exit_cb:
105
 * @loop: The main loop
106
 *
107
 * Exits the main loop, which is helpful for valgrinding g-p-m.
108
 *
109
 * Return value: FALSE, as we don't want to repeat this action.
110
 **/
111
static gboolean
112
timed_exit_cb (GMainLoop *loop)
113
{
114
	g_main_loop_quit (loop);
115
	return FALSE;
116
}
117
118
/**
2.1.39 by Martin Pitt
Import upstream version 2.26.1
119
 * gpm_main_stop_cb:
120
 **/
121
static void
122
gpm_main_stop_cb (GpmSession *session, GMainLoop *loop)
123
{
124
	g_main_loop_quit (loop);
125
}
126
127
/**
128
 * gpm_main_query_end_session_cb:
129
 **/
130
static void
131
gpm_main_query_end_session_cb (GpmSession *session, guint flags, GMainLoop *loop)
132
{
133
	/* just send response */
134
	gpm_session_end_session_response (session, TRUE, NULL);
135
}
136
137
/**
138
 * gpm_main_end_session_cb:
139
 **/
140
static void
141
gpm_main_end_session_cb (GpmSession *session, guint flags, GMainLoop *loop)
142
{
143
	/* send response */
144
	gpm_session_end_session_response (session, TRUE, NULL);
145
146
	/* exit loop, will unref manager */
147
	g_main_loop_quit (loop);
148
}
149
150
/**
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
151
 * main:
152
 **/
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
153
int
154
main (int argc, char *argv[])
155
{
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
156
	GMainLoop *loop;
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
157
	DBusGConnection *system_connection;
158
	DBusGConnection *session_connection;
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
159
	gboolean verbose = FALSE;
2.1.25 by Oliver Grawert
Import upstream version 2.19.5
160
	gboolean version = FALSE;
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
161
	gboolean timed_exit = FALSE;
2.1.23 by Oliver Grawert
Import upstream version 2.19.2
162
	gboolean immediate_exit = FALSE;
2.1.39 by Martin Pitt
Import upstream version 2.26.1
163
	GpmSession *session = NULL;
2.1.15 by Riccardo Setti
Import upstream version 2.17.3
164
	GpmManager *manager = NULL;
165
	GError *error = NULL;
166
	GOptionContext *context;
2.1.42 by Martin Pitt
Import upstream version 2.27.2+git20090729
167
	gint ret;
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
168
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
169
	const GOptionEntry options[] = {
170
		{ "verbose", '\0', 0, G_OPTION_ARG_NONE, &verbose,
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
171
		  N_("Show extra debugging information"), NULL },
2.1.25 by Oliver Grawert
Import upstream version 2.19.5
172
		{ "version", '\0', 0, G_OPTION_ARG_NONE, &version,
173
		  N_("Show version of installed program and exit"), NULL },
2.1.12 by Brandon Hale
Import upstream version 2.15.92
174
		{ "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit,
175
		  N_("Exit after a small delay (for debugging)"), NULL },
2.1.23 by Oliver Grawert
Import upstream version 2.19.2
176
		{ "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit,
2.1.27 by Oliver Grawert
Import upstream version 2.19.92
177
		  N_("Exit after the manager has loaded (for debugging)"), NULL },
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
178
		{ NULL}
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
179
	};
180
2.1.39 by Martin Pitt
Import upstream version 2.26.1
181
	setlocale (LC_ALL, "");
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
182
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
183
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
184
	textdomain (GETTEXT_PACKAGE);
185
2.1.39 by Martin Pitt
Import upstream version 2.26.1
186
	if (! g_thread_supported ())
187
		g_thread_init (NULL);
188
	dbus_g_thread_init ();
189
	g_type_init ();
190
191
	context = g_option_context_new (N_("GNOME Power Manager"));
192
	/* TRANSLATORS: program name, a simple app to view pending updates */
2.1.10 by Oliver Grawert
Import upstream version 2.15.4
193
	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
2.1.25 by Oliver Grawert
Import upstream version 2.19.5
194
	g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
2.1.39 by Martin Pitt
Import upstream version 2.26.1
195
	g_option_context_set_summary (context, _("GNOME Power Manager"));
196
	g_option_context_parse (context, &argc, &argv, NULL);
2.1.33 by Ted Gould
Import upstream version 2.22.1
197
2.1.35 by Ted Gould
Import upstream version 2.23.6
198
	if (version) {
2.1.25 by Oliver Grawert
Import upstream version 2.19.5
199
		g_print ("Version %s\n", VERSION);
2.1.33 by Ted Gould
Import upstream version 2.22.1
200
		goto unref_program;
201
	}
202
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
203
	if (!g_thread_supported ())
204
		g_thread_init (NULL);
205
	dbus_g_thread_init ();
206
2.1.39 by Martin Pitt
Import upstream version 2.26.1
207
	gtk_init (&argc, &argv);
2.1.37 by Ted Gould
Import upstream version 2.24.0
208
	egg_debug_init (verbose);
2.1.3 by Daniel Silverstone
Import upstream version 2.13.90
209
2.1.37 by Ted Gould
Import upstream version 2.24.0
210
	egg_debug ("GNOME %s %s", GPM_NAME, VERSION);
2.1.18 by Oliver Grawert
Import upstream version 2.17.91
211
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
212
	/* check dbus connections, exit if not valid */
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
213
	system_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
214
	if (error) {
2.1.37 by Ted Gould
Import upstream version 2.24.0
215
		egg_warning ("%s", error->message);
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
216
		g_error_free (error);
2.1.37 by Ted Gould
Import upstream version 2.24.0
217
		egg_error ("This program cannot start until you start "
2.1.23 by Oliver Grawert
Import upstream version 2.19.2
218
			   "the dbus system service.\n"
219
			   "It is <b>strongly recommended</b> you reboot "
220
			   "your computer after starting this service.");
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
221
	}
222
223
	session_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
224
	if (error) {
2.1.37 by Ted Gould
Import upstream version 2.24.0
225
		egg_warning ("%s", error->message);
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
226
		g_error_free (error);
2.1.37 by Ted Gould
Import upstream version 2.24.0
227
		egg_error ("This program cannot start until you start the "
2.1.23 by Oliver Grawert
Import upstream version 2.19.2
228
			   "dbus session service.\n\n"
229
			   "This is usually started automatically in X "
230
			   "or gnome startup when you start a new session.");
231
	}
232
2.1.39 by Martin Pitt
Import upstream version 2.26.1
233
	/* add application specific icons to search path */
2.1.27 by Oliver Grawert
Import upstream version 2.19.92
234
	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
235
                                           GPM_DATA G_DIR_SEPARATOR_S "icons");
2.1.23 by Oliver Grawert
Import upstream version 2.19.2
236
2.1.39 by Martin Pitt
Import upstream version 2.26.1
237
	loop = g_main_loop_new (NULL, FALSE);
238
239
	/* optionally register with the session */
240
	session = gpm_session_new ();
241
	g_signal_connect (session, "stop", G_CALLBACK (gpm_main_stop_cb), loop);
242
	g_signal_connect (session, "query-end-session", G_CALLBACK (gpm_main_query_end_session_cb), loop);
243
	g_signal_connect (session, "end-session", G_CALLBACK (gpm_main_end_session_cb), loop);
244
	gpm_session_register_client (session, "gnome-power-manager", getenv ("DESKTOP_AUTOSTART_ID"));
2.1.24 by Oliver Grawert
Import upstream version 2.19.3
245
2.1.23 by Oliver Grawert
Import upstream version 2.19.2
246
	/* create a new gui object */
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
247
	manager = gpm_manager_new ();
248
249
	if (!gpm_object_register (session_connection, G_OBJECT (manager))) {
2.1.37 by Ted Gould
Import upstream version 2.24.0
250
		egg_error ("%s is already running in this session.", GPM_NAME);
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
251
		return 0;
252
	}
253
2.1.42 by Martin Pitt
Import upstream version 2.27.2+git20090729
254
	/* register to be a policy agent, just like kpackagekit does */
255
	ret = dbus_bus_request_name(dbus_g_connection_get_connection(system_connection),
256
				    "org.freedesktop.Policy.Power",
257
				    DBUS_NAME_FLAG_REPLACE_EXISTING, NULL);
258
	switch (ret) {
259
	case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
260
		egg_debug ("Successfully acquired interface org.freedesktop.Policy.Power.");
261
		break;
262
	case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
263
		egg_debug ("Queued for interface org.freedesktop.Policy.Power.");
264
		break;
265
	default:
266
		break;
267
	};
268
2.1.12 by Brandon Hale
Import upstream version 2.15.92
269
	/* Only timeout and close the mainloop if we have specified it
270
	 * on the command line */
2.1.35 by Ted Gould
Import upstream version 2.23.6
271
	if (timed_exit) {
2.1.39 by Martin Pitt
Import upstream version 2.26.1
272
		g_timeout_add_seconds (20, (GSourceFunc) timed_exit_cb, loop);
2.1.12 by Brandon Hale
Import upstream version 2.15.92
273
	}
274
2.1.23 by Oliver Grawert
Import upstream version 2.19.2
275
	if (immediate_exit == FALSE) {
276
		g_main_loop_run (loop);
277
	}
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
278
2.1.25 by Oliver Grawert
Import upstream version 2.19.5
279
	g_main_loop_unref (loop);
2.1.2 by Oliver Grawert
Import upstream version 0.3.4
280
2.1.39 by Martin Pitt
Import upstream version 2.26.1
281
	g_object_unref (session);
2.1.12 by Brandon Hale
Import upstream version 2.15.92
282
	g_object_unref (manager);
2.1.33 by Ted Gould
Import upstream version 2.22.1
283
unref_program:
284
	g_option_context_free (context);
1 by Matthew Garrett
suspend by calling gdm-signal directly, rather than by going through
285
	return 0;
286
}