int main (int argc, char **argv)
{
    GDBusConnection *bus;
    GError *error = NULL;
    XfpmPowerManager *proxy;
    GOptionContext *octx;
     
    gboolean run        = FALSE;
    gboolean quit       = FALSE;
    gboolean config     = FALSE;
    gboolean version    = FALSE;
    gboolean reload     = FALSE;
    gboolean no_daemon  = FALSE;
    gboolean debug      = FALSE;
    gboolean dump       = FALSE;
    gchar   *client_id  = NULL;
    
    GOptionEntry option_entries[] = 
    {
	{ "run",'r', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &run, NULL, NULL },
	{ "no-daemon",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &no_daemon, N_("Do not daemonize"), NULL },
	{ "debug",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &debug, N_("Enable debugging"), NULL },
	{ "dump",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &dump, N_("Dump all information"), NULL },
	{ "restart", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &reload, N_("Restart the running instance of Xfce power manager"), NULL},
	{ "customize", 'c', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &config, N_("Show the configuration dialog"), NULL },
	{ "quit", 'q', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &quit, N_("Quit any running xfce power manager"), NULL },
	{ "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &version, N_("Version information"), NULL },
	{ "sm-client-id", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &client_id, NULL, NULL },
	{ NULL, },
    };

    /* Parse the options */
    octx = g_option_context_new("");
    g_option_context_set_ignore_unknown_options(octx, TRUE);
    g_option_context_add_main_entries(octx, option_entries, NULL);
    g_option_context_add_group(octx, xfce_sm_client_get_option_group(argc, argv));
    /* We can't add the following command because it will invoke gtk_init
       before we have a chance to fork.
       g_option_context_add_group(octx, gtk_get_option_group(TRUE));
     */

    if(!g_option_context_parse(octx, &argc, &argv, &error)) {
        g_printerr(_("Failed to parse arguments: %s\n"), error->message);
        g_option_context_free(octx);
        g_error_free(error);

        return EXIT_FAILURE;
    }

    g_option_context_free(octx);

    if ( version )    
        show_version ();

    /* Fork if needed */
    if ( dump == FALSE && debug == FALSE && no_daemon == FALSE && daemon(0,0) )
    {
        g_critical ("Could not daemonize");
    }

    /* Initialize */
    xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");

    g_set_application_name (PACKAGE_NAME);

    if (!gtk_init_check (&argc, &argv))
    {
        if (G_LIKELY (error))
        {
            g_printerr ("%s: %s.\n", G_LOG_DOMAIN, error->message);
            g_printerr (_("Type '%s --help' for usage."), G_LOG_DOMAIN);
            g_printerr ("\n");
            g_error_free (error);
        }
        else
        {
            g_error ("Unable to open display.");
        }

        return EXIT_FAILURE;
    }
    
    xfpm_debug_init (debug);
    
    bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
            
    if ( error )
    {
	xfce_dialog_show_error (NULL, 
				error, 
				"%s",
				_("Unable to get connection to the message bus session"));
	g_error ("%s: \n", error->message);
    }
    
    if ( quit )
    {
	if (!xfpm_dbus_name_has_owner (bus, "org.xfce.PowerManager") )
        {
            g_print (_("Xfce power manager is not running"));
	    g_print ("\n");
            return EXIT_SUCCESS;
        }
	else
	{
	    proxy = xfpm_power_manager_proxy_new_sync (bus,
						       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
						       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
						       "org.xfce.PowerManager",
						       "/org/xfce/PowerManager",
						       NULL,
						       NULL);
	    if ( !proxy ) 
	    {
		g_critical ("Failed to get proxy");
		g_object_unref(bus);
            	return EXIT_FAILURE;
	    }
	    xfpm_power_manager_call_quit_sync (proxy, NULL, &error);
	    g_object_unref (proxy);
	    
	    if ( error)
	    {
		g_critical ("Failed to send quit message %s:\n", error->message);
		g_error_free (error);
	    }
	}
	return EXIT_SUCCESS;
    }
    
    if ( config )
    {
	g_spawn_command_line_async ("xfce4-power-manager-settings", &error);
	
	if ( error )
	{
	    g_critical ("Failed to execute xfce4-power-manager-settings: %s", error->message);
	    g_error_free (error);
	    return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
    }
    
    if ( reload )
    {
	if (!xfpm_dbus_name_has_owner (bus, "org.xfce.PowerManager") &&
	    !xfpm_dbus_name_has_owner (bus, "org.freedesktop.PowerManagement"))
	{
	    g_print ("Xfce power manager is not running\n");
	    xfpm_start (bus, client_id, dump);
	}
	
	proxy = xfpm_power_manager_proxy_new_sync (bus,
						   G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
						   G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
						   "org.xfce.PowerManager",
						   "/org/xfce/PowerManager",
						   NULL,
						   NULL);
	if ( !proxy ) 
	{
	    g_critical ("Failed to get proxy");
	    g_object_unref (bus);
	    return EXIT_FAILURE;
	}
	    
	if ( !xfpm_power_manager_call_restart_sync (proxy, NULL, NULL) )
	{
	    g_critical ("Unable to send reload message");
	    g_object_unref (proxy);
	    g_object_unref (bus);
	    return EXIT_SUCCESS;
	}
	return EXIT_SUCCESS;
    }
    
    if (dump)
    {
	if (xfpm_dbus_name_has_owner (bus, "org.xfce.PowerManager"))
	{
	    xfpm_dump_remote (bus);
	    return EXIT_SUCCESS;
	}
    }
    
    if (xfpm_dbus_name_has_owner (bus, "org.freedesktop.PowerManagement") )
    {
	g_print ("%s: %s\n", 
		 _("Xfce Power Manager"),
		 _("Another power manager is already running"));
		  
    }
    else if (xfpm_dbus_name_has_owner (bus, "org.xfce.PowerManager"))
    {
	g_print (_("Xfce power manager is already running"));
	g_print ("\n");
    	return EXIT_SUCCESS;
    }
    else
    {	
	xfpm_start (bus, client_id, dump);
    }
    
    return EXIT_SUCCESS;
}
示例#2
0
int main (int argc, char **argv)
{
    DBusGConnection *bus;
    GError *error = NULL;
    DBusGProxy *proxy;
     
    gboolean run        = FALSE;
    gboolean quit       = FALSE;
    gboolean config     = FALSE;
    gboolean version    = FALSE;
    gboolean reload     = FALSE;
    gboolean no_daemon  = FALSE;
    gboolean debug      = FALSE;
    gboolean dump       = FALSE;
    gchar   *client_id  = NULL;
    
    GOptionEntry option_entries[] = 
    {
	{ "run",'r', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &run, NULL, NULL },
	{ "no-daemon",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &no_daemon, N_("Do not daemonize"), NULL },
	{ "debug",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &debug, N_("Enable debugging"), NULL },
	{ "dump",'\0' , G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &dump, N_("Dump all information"), NULL },
	{ "restart", '\0', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &reload, N_("Restart the running instance of Xfce power manager"), NULL},
	{ "customize", 'c', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &config, N_("Show the configuration dialog"), NULL },
	{ "quit", 'q', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &quit, N_("Quit any running xfce power manager"), NULL },
	{ "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &version, N_("Version information"), NULL },
	{ "sm-client-id", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &client_id, NULL, NULL },
	{ NULL, },
    };

#if !GLIB_CHECK_VERSION (2, 32, 0)
    if ( !g_thread_supported () )
	g_thread_init (NULL);
#endif

    dbus_g_thread_init ();

    xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8");
    
    g_set_application_name (PACKAGE_NAME);
    
    if (!gtk_init_with_args (&argc, &argv, (gchar *)"", option_entries, (gchar *)PACKAGE, &error)) 
    {
        if (G_LIKELY (error)) 
        {
            g_printerr ("%s: %s.\n", G_LOG_DOMAIN, error->message);
            g_printerr (_("Type '%s --help' for usage."), G_LOG_DOMAIN);
            g_printerr ("\n");
            g_error_free (error);
        }
        else
        {
            g_error ("Unable to open display.");
	}

        return EXIT_FAILURE;
    }
    
    if ( version )    
    {
	show_version ();
    }
    
    xfpm_debug_init (debug);

    if ( dump == FALSE && debug == FALSE && no_daemon == FALSE && daemon(0,0) )
    {
	g_critical ("Could not daemonize");
    }
    
    bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
            
    if ( error )
    {
	xfce_dialog_show_error (NULL, 
				error, 
				"%s",
				_("Unable to get connection to the message bus session"));
	g_error ("%s: \n", error->message);
    }
    
    if ( quit )
    {
	if (!xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), 
				       "org.xfce.PowerManager") )
        {
            g_print (_("Xfce power manager is not running"));
	    g_print ("\n");
            return EXIT_SUCCESS;
        }
	else
	{
	    proxy = dbus_g_proxy_new_for_name (bus, 
			                       "org.xfce.PowerManager",
					       "/org/xfce/PowerManager",
					       "org.xfce.Power.Manager");
	    if ( !proxy ) 
	    {
		g_critical ("Failed to get proxy");
		dbus_g_connection_unref(bus);
            	return EXIT_FAILURE;
	    }
	    xfpm_manager_dbus_client_quit (proxy , &error);
	    g_object_unref (proxy);
	    
	    if ( error)
	    {
		g_critical ("Failed to send quit message %s:\n", error->message);
		g_error_free (error);
	    }
	}
	return EXIT_SUCCESS;
    }
    
    if ( config )
    {
	g_spawn_command_line_async ("xfce4-power-manager-settings", &error);
	
	if ( error )
	{
	    g_critical ("Failed to execute xfce4-power-manager-settings: %s", error->message);
	    g_error_free (error);
	    return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
    }
    
    if ( reload )
    {
	if (!xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.xfce.PowerManager") &&
	    !xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.freedesktop.PowerManagement"))
	{
	    g_print ("Xfce power manager is not running\n");
	    xfpm_start (bus, client_id, dump);
	}
	
	proxy = dbus_g_proxy_new_for_name (bus, 
			                   "org.xfce.PowerManager",
					   "/org/xfce/PowerManager",
					   "org.xfce.Power.Manager");
	if ( !proxy ) 
	{
	    g_critical ("Failed to get proxy");
	    dbus_g_connection_unref (bus);
	    return EXIT_FAILURE;
	}
	    
	if ( !xfpm_manager_dbus_client_restart (proxy, NULL) )
	{
	    g_critical ("Unable to send reload message");
	    g_object_unref (proxy);
	    dbus_g_connection_unref (bus);
	    return EXIT_SUCCESS;
	}
	return EXIT_SUCCESS;
    }
    
    if (dump)
    {
	if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), 
				      "org.xfce.PowerManager"))
	{
	    xfpm_dump_remote (bus);
	    return EXIT_SUCCESS;
	}
    }
    
    if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection (bus), "org.freedesktop.PowerManagement") )
    {
	g_print ("%s: %s\n", 
		 _("Xfce Power Manager"),
		 _("Another power manager is already running"));
		  
    }
    else if (xfpm_dbus_name_has_owner (dbus_g_connection_get_connection(bus), 
				       "org.xfce.PowerManager"))
    {
	g_print (_("Xfce power manager is already running"));
	g_print ("\n");
    	return EXIT_SUCCESS;
    }
    else
    {	
	xfpm_start (bus, client_id, dump);
    }
    
    return EXIT_SUCCESS;
}