~noskcaj/ubuntu/saucy/xfce4-power-manager/1.2.0-2ubuntu1

« back to all changes in this revision

Viewing changes to src/xfpm-main.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-09 18:28:34 UTC
  • mfrom: (2.3.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20101209182834-efb7dinmf9ssp3es
Tags: 1.0.1-0ubuntu1
Upload to natty (pkg-xfce svn r4665), no Ubuntu changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <stdlib.h>
27
27
#include <string.h>
28
28
 
 
29
#ifdef HAVE_UNISTD_H
29
30
#include <unistd.h>
 
31
#endif
30
32
#include <signal.h>
31
33
 
32
34
#include <gtk/gtk.h>
33
35
#include <glib.h>
34
36
 
35
37
#include <libxfce4util/libxfce4util.h>
 
38
#include <libxfce4ui/libxfce4ui.h>
36
39
 
37
40
#include <dbus/dbus-glib.h>
38
41
#include <dbus/dbus-glib-lowlevel.h>
39
42
 
40
 
#include "libxfpm/xfpm-dbus.h"
41
 
#include "libxfpm/xfpm-popups.h"
 
43
#include "xfpm-dbus.h"
 
44
#include "xfpm-debug.h"
 
45
#include "xfpm-common.h"
42
46
 
43
47
#include "xfce-power-manager-dbus-client.h"
44
48
#include "xfpm-manager.h"
45
 
#include "xfpm-session.h"
46
 
 
47
 
static gchar    *client_id = NULL;
48
 
static gboolean no_daemon  = FALSE;
49
49
 
50
50
static void G_GNUC_NORETURN
51
51
show_version (void)
64
64
{
65
65
    XfpmManager *manager = (XfpmManager *) data;
66
66
    
67
 
    TRACE ("sig %d", sig);
 
67
    XFPM_DEBUG ("sig %d", sig);
68
68
    
69
69
    if ( sig != SIGHUP )
70
70
        xfpm_manager_stop (manager);
71
71
}
72
72
 
 
73
static const gchar *
 
74
xfpm_bool_to_local_string (gboolean value)
 
75
{
 
76
    return value == TRUE ? _("True") : _("False");
 
77
}
 
78
 
 
79
static void
 
80
xfpm_dump (GHashTable *hash)
 
81
{
 
82
    gboolean has_battery;
 
83
    gboolean auth_suspend;
 
84
    gboolean auth_hibernate;
 
85
    gboolean can_suspend;
 
86
    gboolean can_hibernate;
 
87
    gboolean can_shutdown;
 
88
    gboolean has_lcd_brightness;
 
89
    gboolean has_sleep_button;
 
90
    gboolean has_hibernate_button;
 
91
    gboolean has_power_button;
 
92
    gboolean has_lid;
 
93
    gboolean can_spin_down;
 
94
    gboolean devkit_disk;
 
95
    
 
96
    has_battery = xfpm_string_to_bool (g_hash_table_lookup (hash, "has-battery"));
 
97
    has_lid = xfpm_string_to_bool (g_hash_table_lookup (hash, "has-lid"));
 
98
    can_suspend = xfpm_string_to_bool (g_hash_table_lookup (hash, "can-suspend"));
 
99
    can_hibernate = xfpm_string_to_bool (g_hash_table_lookup (hash, "can-hibernate"));
 
100
    auth_suspend = xfpm_string_to_bool (g_hash_table_lookup (hash, "auth-suspend"));
 
101
    auth_hibernate = xfpm_string_to_bool (g_hash_table_lookup (hash, "auth-hibernate"));
 
102
    has_lcd_brightness = xfpm_string_to_bool (g_hash_table_lookup (hash, "has-brightness"));
 
103
    has_sleep_button = xfpm_string_to_bool (g_hash_table_lookup (hash, "sleep-button"));
 
104
    has_power_button = xfpm_string_to_bool (g_hash_table_lookup (hash, "power-button"));
 
105
    has_hibernate_button = xfpm_string_to_bool (g_hash_table_lookup (hash, "hibernate-button"));
 
106
    can_shutdown = xfpm_string_to_bool (g_hash_table_lookup (hash, "can-shutdown"));
 
107
    can_spin_down = xfpm_string_to_bool (g_hash_table_lookup (hash, "can-spin"));
 
108
    devkit_disk = xfpm_string_to_bool (g_hash_table_lookup (hash, "devkit-disk"));
 
109
        
 
110
    g_print ("---------------------------------------------------\n");
 
111
    g_print ("       Xfce power manager version %s\n", VERSION);
 
112
#ifdef WITH_HAL
 
113
    g_print (_("With HAL support\n"));
 
114
#else
 
115
    g_print (_("Without HAL support\n"));
 
116
#endif
 
117
#ifdef ENABLE_POLKIT
 
118
    g_print (_("With policykit support\n"));
 
119
#else
 
120
    g_print (_("Without policykit support\n"));
 
121
#endif
 
122
#ifdef WITH_NETWORK_MANAGER
 
123
    g_print (_("With network manager support\n"));
 
124
#else
 
125
    g_print (_("Without network manager support\n"));
 
126
#endif
 
127
#ifdef HAVE_DPMS
 
128
    g_print (_("With DPMS support\n"));
 
129
#else
 
130
    g_print (_("Without DPMS support\n"));
 
131
#endif
 
132
    g_print ("---------------------------------------------------\n");
 
133
    g_print ( "%s: %s\n"
 
134
              "%s: %s\n"
 
135
              "%s: %s\n"
 
136
              "%s: %s\n"
 
137
              "%s: %s\n"
 
138
              "%s: %s\n"
 
139
              "%s: %s\n"
 
140
              "%s: %s\n"
 
141
              "%s: %s\n"
 
142
              "%s: %s\n"
 
143
              "%s: %s\n"
 
144
              "%s: %s\n",
 
145
             _("Can suspend"),
 
146
             xfpm_bool_to_local_string (can_suspend),
 
147
             _("Can hibernate"),
 
148
             xfpm_bool_to_local_string (can_hibernate),
 
149
             _("Can spin down hard disks"),
 
150
             xfpm_bool_to_local_string (devkit_disk),
 
151
             _("Authorized to suspend"),
 
152
             xfpm_bool_to_local_string (auth_suspend),
 
153
             _("Authorized to hibernate"),
 
154
             xfpm_bool_to_local_string (auth_hibernate),
 
155
             _("Authorized to shutdown"),
 
156
             xfpm_bool_to_local_string (can_shutdown),
 
157
             _("Authorized to spin down hard disks"),
 
158
             xfpm_bool_to_local_string (can_spin_down),
 
159
             _("Has brightness panel"),
 
160
             xfpm_bool_to_local_string (has_lcd_brightness),
 
161
             _("Has power button"),
 
162
             xfpm_bool_to_local_string (has_power_button),
 
163
             _("Has hibernate button"),
 
164
             xfpm_bool_to_local_string (has_hibernate_button),
 
165
             _("Has sleep button"),
 
166
              xfpm_bool_to_local_string (has_sleep_button),
 
167
             _("Has LID"),
 
168
              xfpm_bool_to_local_string (has_lid));
 
169
}
 
170
 
 
171
static void
 
172
xfpm_dump_remote (DBusGConnection *bus)
 
173
{
 
174
    DBusGProxy *proxy;
 
175
    GError *error = NULL;
 
176
    GHashTable *hash;
 
177
    
 
178
    proxy = dbus_g_proxy_new_for_name (bus,
 
179
                                       "org.xfce.PowerManager",
 
180
                                       "/org/xfce/PowerManager",
 
181
                                       "org.xfce.Power.Manager");
 
182
        
 
183
    xfpm_manager_dbus_client_get_config (proxy, 
 
184
                                         &hash,
 
185
                                         &error);
 
186
                                             
 
187
    g_object_unref (proxy);
 
188
    
 
189
    if ( error )
 
190
    {
 
191
        g_error ("%s", error->message);
 
192
        exit (EXIT_FAILURE);
 
193
    }
 
194
    
 
195
    xfpm_dump (hash);
 
196
    g_hash_table_destroy (hash);
 
197
}
 
198
 
73
199
static void G_GNUC_NORETURN
74
 
xfpm_start (DBusGConnection *bus)
 
200
xfpm_start (DBusGConnection *bus, const gchar *client_id, gboolean dump)
75
201
{
76
202
    XfpmManager *manager;
77
 
    XfpmSession *session;
78
203
    GError *error = NULL;
79
204
    
80
 
    TRACE ("Starting the power manager");
81
 
    session = xfpm_session_new ();
82
 
    
83
 
    if ( client_id != NULL )
84
 
        xfpm_session_set_client_id (session, client_id);
85
 
        
86
 
    xfpm_session_real_init (session);
87
 
    
88
 
    manager = xfpm_manager_new (bus);
 
205
    XFPM_DEBUG ("Starting the power manager");
 
206
    
 
207
    manager = xfpm_manager_new (bus, client_id);
89
208
    
90
209
    if ( xfce_posix_signal_handler_init (&error)) 
91
210
    {
92
 
        xfce_posix_signal_handler_set_handler(SIGHUP,
93
 
                                              xfpm_quit_signal,
94
 
                                              manager, NULL);
95
 
 
96
 
        xfce_posix_signal_handler_set_handler(SIGINT,
97
 
                                              xfpm_quit_signal,
98
 
                                              manager, NULL);
99
 
 
100
 
        xfce_posix_signal_handler_set_handler(SIGTERM,
101
 
                                              xfpm_quit_signal,
102
 
                                              manager, NULL);
 
211
        xfce_posix_signal_handler_set_handler (SIGHUP,
 
212
                                               xfpm_quit_signal,
 
213
                                               manager, NULL);
 
214
 
 
215
        xfce_posix_signal_handler_set_handler (SIGINT,
 
216
                                               xfpm_quit_signal,
 
217
                                               manager, NULL);
 
218
 
 
219
        xfce_posix_signal_handler_set_handler (SIGTERM,
 
220
                                               xfpm_quit_signal,
 
221
                                               manager, NULL);
103
222
    } 
104
223
    else 
105
224
    {
106
225
        g_warning ("Unable to set up POSIX signal handlers: %s", error->message);
107
 
        g_error_free(error);
 
226
        g_error_free (error);
108
227
    }
109
228
 
110
229
    xfpm_manager_start (manager);
 
230
    
 
231
    if ( dump )
 
232
    {
 
233
        GHashTable *hash;
 
234
        hash = xfpm_manager_get_config (manager);
 
235
        xfpm_dump (hash);
 
236
        g_hash_table_destroy (hash);
 
237
    }
 
238
 
 
239
    
111
240
    gtk_main ();
112
241
    
113
 
    g_object_unref (session);
 
242
    g_object_unref (manager);
114
243
    
115
244
    exit (EXIT_SUCCESS);
116
245
}
117
246
 
118
 
int main(int argc, char **argv)
 
247
int main (int argc, char **argv)
119
248
{
120
249
    DBusGConnection *bus;
121
250
    GError *error = NULL;
126
255
    gboolean config     = FALSE;
127
256
    gboolean version    = FALSE;
128
257
    gboolean reload     = FALSE;
 
258
    gboolean no_daemon  = FALSE;
 
259
    gboolean debug      = FALSE;
 
260
    gboolean dump       = FALSE;
 
261
    gchar   *client_id  = NULL;
129
262
    
130
263
    GOptionEntry option_entries[] = 
131
264
    {
132
265
        { "run",'r', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &run, NULL, NULL },
133
266
        { "no-daemon",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &no_daemon, N_("Do not daemonize"), NULL },
 
267
        { "debug",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &debug, N_("Enable debugging"), NULL },
 
268
        { "dump",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &dump, N_("Dump all information"), NULL },
134
269
        { "restart", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &reload, N_("Restart the running instance of Xfce power manager"), NULL},
135
270
        { "customize", 'c', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &config, N_("Show the configuration dialog"), NULL },
136
271
        { "quit", 'q', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &quit, N_("Quit any running xfce power manager"), NULL },
145
280
    dbus_g_thread_init ();
146
281
 
147
282
    xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
148
 
 
 
283
    
 
284
    g_set_application_name (PACKAGE_NAME);
 
285
    
149
286
    if (!gtk_init_with_args (&argc, &argv, (gchar *)"", option_entries, (gchar *)PACKAGE, &error)) 
150
287
    {
151
 
        if(G_LIKELY(error)) 
 
288
        if (G_LIKELY (error)) 
152
289
        {
153
 
            g_printerr("%s: %s.\n", G_LOG_DOMAIN, error->message);
154
 
            g_printerr(_("Type '%s --help' for usage."), G_LOG_DOMAIN);
155
 
            g_printerr("\n");
156
 
            g_error_free(error);
 
290
            g_printerr ("%s: %s.\n", G_LOG_DOMAIN, error->message);
 
291
            g_printerr (_("Type '%s --help' for usage."), G_LOG_DOMAIN);
 
292
            g_printerr ("\n");
 
293
            g_error_free (error);
157
294
        }
158
295
        else
159
296
        {
160
 
            g_error("Unable to open display.");
 
297
            g_error ("Unable to open display.");
161
298
        }
162
299
 
163
300
        return EXIT_FAILURE;
165
302
    
166
303
    if ( version )    
167
304
    {
168
 
        show_version();
 
305
        show_version ();
169
306
    }
170
307
    
171
 
    if ( run + quit + config + version > 1 )
172
 
    {
173
 
        g_printerr(_("Too many arguments"));
174
 
        g_printerr("\n");
175
 
        g_printerr(_("Type '%s --help' for usage."), G_LOG_DOMAIN);
176
 
        g_printerr("\n");
177
 
        return EXIT_FAILURE;
178
 
    }
 
308
    xfpm_debug_init (debug);
179
309
 
180
 
    if ( no_daemon == FALSE && daemon(0,0) )
 
310
    if ( dump == FALSE && debug == FALSE && no_daemon == FALSE && daemon(0,0) )
181
311
    {
182
312
        g_critical ("Could not daemonize");
183
313
    }
184
314
    
185
 
    bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
 
315
    bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
186
316
            
187
317
    if ( error )
188
318
    {
189
 
        gchar *message = g_strdup(_("Unable to get connection to the message bus session"));
190
 
        message = g_strdup_printf("%s: ",error->message);
191
 
        
192
 
        xfpm_error (_("Xfce Power Manager"),
193
 
                    message );
194
 
                           
195
 
        g_error ("%s: \n", message);
 
319
        xfce_dialog_show_error (NULL, 
 
320
                                error, 
 
321
                                "%s",
 
322
                                _("Unable to get connection to the message bus session"));
 
323
        g_error ("%s: \n", error->message);
196
324
    }
197
325
    
198
326
    if ( quit )
199
327
    {
200
 
        if (!xfpm_dbus_name_has_owner(dbus_g_connection_get_connection(bus), 
201
 
                                      "org.xfce.PowerManager") )
 
328
        if (!xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), 
 
329
                                       "org.xfce.PowerManager") )
202
330
        {
203
 
            g_print(_("Xfce power manager is not running"));
204
 
            g_print("\n");
 
331
            g_print (_("Xfce power manager is not running"));
 
332
            g_print ("\n");
205
333
            return EXIT_SUCCESS;
206
334
        }
207
335
        else
208
336
        {
209
 
            proxy = dbus_g_proxy_new_for_name(bus, 
210
 
                                              "org.xfce.PowerManager",
211
 
                                              "/org/xfce/PowerManager",
212
 
                                              "org.xfce.Power.Manager");
 
337
            proxy = dbus_g_proxy_new_for_name (bus, 
 
338
                                               "org.xfce.PowerManager",
 
339
                                               "/org/xfce/PowerManager",
 
340
                                               "org.xfce.Power.Manager");
213
341
            if ( !proxy ) 
214
342
            {
215
343
                g_critical ("Failed to get proxy");
216
344
                dbus_g_connection_unref(bus);
217
345
                return EXIT_FAILURE;
218
346
            }
219
 
            xfpm_manager_dbus_client_quit(proxy , &error);
 
347
            xfpm_manager_dbus_client_quit (proxy , &error);
220
348
            g_object_unref (proxy);
221
349
            
222
350
            if ( error)
243
371
    
244
372
    if ( reload )
245
373
    {
246
 
        if (!xfpm_dbus_name_has_owner(dbus_g_connection_get_connection (bus), "org.xfce.PowerManager") &&
247
 
            !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), "org.freedesktop.PowerManagement"))
 
374
        if (!xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.xfce.PowerManager") &&
 
375
            !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.freedesktop.PowerManagement"))
248
376
        {
249
377
            g_print ("Xfce power manager is not running\n");
250
 
            xfpm_start (bus);
 
378
            xfpm_start (bus, client_id, dump);
251
379
        }
252
 
        proxy = dbus_g_proxy_new_for_name(bus, 
253
 
                                              "org.xfce.PowerManager",
254
 
                                              "/org/xfce/PowerManager",
255
 
                                              "org.xfce.Power.Manager");
 
380
        
 
381
        proxy = dbus_g_proxy_new_for_name (bus, 
 
382
                                           "org.xfce.PowerManager",
 
383
                                           "/org/xfce/PowerManager",
 
384
                                           "org.xfce.Power.Manager");
256
385
        if ( !proxy ) 
257
386
        {
258
387
            g_critical ("Failed to get proxy");
259
 
            dbus_g_connection_unref(bus);
 
388
            dbus_g_connection_unref (bus);
260
389
            return EXIT_FAILURE;
261
390
        }
262
391
            
270
399
        return EXIT_SUCCESS;
271
400
    }
272
401
    
273
 
    if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), "org.freedesktop.PowerManagement") )
 
402
    if (dump)
 
403
    {
 
404
        if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), 
 
405
                                      "org.xfce.PowerManager"))
 
406
        {
 
407
            xfpm_dump_remote (bus);
 
408
            return EXIT_SUCCESS;
 
409
        }
 
410
    }
 
411
    
 
412
    if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.freedesktop.PowerManagement") )
274
413
    {
275
414
        g_print ("%s: %s\n", 
276
415
                 _("Xfce Power Manager"),
277
416
                 _("Another power manager is already running"));
278
417
                  
279
418
    }
280
 
    else if (xfpm_dbus_name_has_owner(dbus_g_connection_get_connection(bus), 
281
 
                                      "org.xfce.PowerManager"))
 
419
    else if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), 
 
420
                                       "org.xfce.PowerManager"))
282
421
    {
283
422
        g_print (_("Xfce power manager is already running"));
284
423
        g_print ("\n");
286
425
    }
287
426
    else
288
427
    {   
289
 
        xfpm_start (bus);
 
428
        xfpm_start (bus, client_id, dump);
290
429
    }
291
430
    
292
431
    return EXIT_SUCCESS;