30
32
#ifdef HAVE_STRING_H
31
33
#include <string.h>
40
#include <dbus/dbus-glib.h>
38
41
#include <libxfce4util/libxfce4util.h>
40
#include "panel-app.h"
41
#include "panel-app-messages.h"
48
static gboolean opt_version = FALSE;
49
static gboolean opt_customize = FALSE;
50
static gboolean opt_save = FALSE;
51
static gboolean opt_restart = FALSE;
52
static gboolean opt_quit = FALSE;
53
static gboolean opt_exit = FALSE;
54
static gboolean opt_add = FALSE;
55
static gchar *opt_client_id = NULL;
42
#include <libxfce4ui/libxfce4ui.h>
44
#include <common/panel-private.h>
45
#include <common/panel-debug.h>
46
#include <libxfce4panel/libxfce4panel.h>
47
#include <panel/panel-application.h>
48
#include <panel/panel-dbus-service.h>
49
#include <panel/panel-dbus-client.h>
50
#include <panel/panel-preferences-dialog.h>
54
static gint opt_preferences = -1;
55
static gint opt_add_items = -1;
56
static gboolean opt_save = FALSE;
57
static gchar *opt_add = NULL;
58
static gboolean opt_restart = FALSE;
59
static gboolean opt_quit = FALSE;
60
static gboolean opt_version = FALSE;
61
static gchar *opt_plugin_event = NULL;
62
static gchar **opt_arguments = NULL;
63
static gboolean sm_client_saved_state = FALSE;
67
static gboolean panel_callback_handler (const gchar *name,
57
74
/* command line options */
75
#define PANEL_CALLBACK_OPTION G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, panel_callback_handler
58
76
static GOptionEntry option_entries[] =
60
{ "version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, N_ ("Print version information and exit"), NULL },
61
{ "customize", 'c', 0, G_OPTION_ARG_NONE, &opt_customize, N_ ("Show 'Customize Panel' dialog"), NULL },
62
{ "save", 's', 0, G_OPTION_ARG_NONE, &opt_save, N_ ("Save the panel configuration"), NULL },
63
{ "restart", 'r', 0, G_OPTION_ARG_NONE, &opt_restart, N_ ("Restart the running instance of xfce4-panel"), NULL },
64
{ "quit", 'q', 0, G_OPTION_ARG_NONE, &opt_quit, N_ ("Log out the active session"), NULL },
65
{ "exit", 'x', 0, G_OPTION_ARG_NONE, &opt_exit, N_ ("Close all panels and end the program"), NULL },
66
{ "add", 'a', 0, G_OPTION_ARG_NONE, &opt_add, N_ ("Show 'Add New Items' dialog"), NULL },
67
{ "sm-client-id", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &opt_client_id, NULL, NULL },
78
{ "preferences", 'p', PANEL_CALLBACK_OPTION, N_("Show the 'Panel Preferences' dialog"), N_("PANEL-NUMBER") },
79
{ "add-items", 'a', PANEL_CALLBACK_OPTION, N_("Show the 'Add New Items' dialog"), N_("PANEL-NUMBER") },
80
{ "save", 's', 0, G_OPTION_ARG_NONE, &opt_save, N_("Save the panel configuration"), NULL },
81
{ "add", '\0', 0, G_OPTION_ARG_STRING, &opt_add, N_("Add a new plugin to the panel"), N_("PLUGIN-NAME") },
82
{ "restart", 'r', 0, G_OPTION_ARG_NONE, &opt_restart, N_("Restart the running panel instance"), NULL },
83
{ "quit", 'q', 0, G_OPTION_ARG_NONE, &opt_quit, N_("Quit the running panel instance"), NULL },
84
{ "version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, N_("Print version information and exit"), NULL },
85
{ "plugin-event", '\0', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &opt_plugin_event, NULL, NULL },
86
{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &opt_arguments, NULL, NULL },
93
panel_callback_handler (const gchar *name,
98
panel_return_val_if_fail (name != NULL, FALSE);
100
if (strcmp (name, "--preferences") == 0
101
|| strcmp (name, "-p") == 0)
103
opt_preferences = value != NULL ? MAX (0, atoi (value)) : 0;
105
else if (strcmp (name, "--add-items") == 0
106
|| strcmp (name, "-a") == 0)
108
opt_add_items = value != NULL ? MAX (0, atoi (value)) : 0;
112
panel_assert_not_reached ();
122
panel_signal_handler (gint signum)
124
panel_debug (PANEL_DEBUG_DOMAIN_MAIN,
125
"received signal %s <%d>, %s panel",
126
g_strsignal (signum), signum,
127
signum == SIGUSR1 ? "restarting" : "quiting");
129
panel_dbus_service_exit_panel (signum == SIGUSR1);
135
panel_sm_client_quit (XfceSMClient *sm_client)
137
panel_return_if_fail (XFCE_IS_SM_CLIENT (sm_client));
138
panel_return_if_fail (!panel_dbus_service_get_restart ());
140
panel_debug (PANEL_DEBUG_DOMAIN_MAIN,
141
"terminate panel for session manager");
149
panel_sm_client_save_state (XfceSMClient *sm_client,
150
PanelApplication *application)
152
panel_return_if_fail (XFCE_IS_SM_CLIENT (sm_client));
153
panel_return_if_fail (PANEL_IS_APPLICATION (application));
155
panel_debug (PANEL_DEBUG_DOMAIN_MAIN,
156
"save configuration for session manager");
158
panel_application_save (application, TRUE);
160
sm_client_saved_state = TRUE;
73
166
main (gint argc, gchar **argv)
78
/* translation domain */
79
xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
81
/* application name */
82
g_set_application_name (PACKAGE_NAME);
84
MARK ("start gtk_init_with_args ()");
87
if (!gtk_init_with_args (&argc, &argv, (gchar *) "", option_entries, (gchar *) GETTEXT_PACKAGE, &error))
89
/* TRANSLATORS: Errors when gtk_init failed, probably the command
90
executed without xserver running */
91
g_print ("%s: %s\n", PACKAGE_NAME, error ? error->message : _("Failed to open display"));
99
/* handle the options */
100
if (G_UNLIKELY (opt_version))
102
g_print ("%s %s (Xfce %s)\n\n", PACKAGE_NAME, PACKAGE_VERSION, xfce_version_string ());
103
g_print ("%s\n", "Copyright (c) 2004-2009");
104
g_print ("\t%s\n\n", _("The Xfce development team. All rights reserved."));
106
/* TRANSLATORS: Bug report website, %s points to bugzilla.xfce.org */
107
g_print (_("Please report bugs to <%s>."), PACKAGE_BUGREPORT);
112
else if (G_UNLIKELY (opt_customize))
113
msg = PANEL_APP_CUSTOMIZE;
114
else if (G_UNLIKELY (opt_save))
115
msg = PANEL_APP_SAVE;
116
else if (G_UNLIKELY (opt_restart))
117
msg = PANEL_APP_RESTART;
118
else if (G_UNLIKELY (opt_quit))
119
msg = PANEL_APP_QUIT;
120
else if (G_UNLIKELY (opt_exit))
121
msg = PANEL_APP_EXIT;
122
else if (G_UNLIKELY (opt_add))
125
/* handle the message, if there is any */
126
if (G_UNLIKELY (msg >= 0))
128
if (!panel_app_send (msg))
130
if (msg != PANEL_APP_RESTART )
134
/* else: continue and start new panel */
142
MARK ("start panel_init()");
143
msg = panel_app_init ();
145
if (G_UNLIKELY (msg == INIT_FAILURE))
149
else if (G_UNLIKELY (msg == INIT_RUNNING))
151
g_message (_("Xfce4-panel already running"));
156
MARK ("start panel_app_run()");
157
msg = panel_app_run (opt_client_id);
158
MARK ("end panel_app_run()");
160
if (G_UNLIKELY (msg == RUN_RESTART))
162
g_message (_("Restarting xfce4-panel..."));
165
execvp (argv[0], argv);
168
return G_UNLIKELY (msg == RUN_FAILURE) ? EXIT_FAILURE : EXIT_SUCCESS;
168
GOptionContext *context;
169
PanelApplication *application;
170
GError *error = NULL;
171
PanelDBusService *dbus_service;
172
gboolean succeed = FALSE;
174
const gint signums[] = { SIGINT, SIGQUIT, SIGTERM, SIGABRT, SIGUSR1 };
175
const gchar *error_msg;
176
XfceSMClient *sm_client;
178
panel_debug (PANEL_DEBUG_DOMAIN_MAIN,
179
"version %s on gtk+ %d.%d.%d (%d.%d.%d), glib %d.%d.%d (%d.%d.%d)",
180
LIBXFCE4PANEL_VERSION,
181
gtk_major_version, gtk_minor_version, gtk_micro_version,
182
GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
183
glib_major_version, glib_minor_version, glib_micro_version,
184
GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
186
/* set translation domain */
187
xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
189
#ifdef G_ENABLE_DEBUG
190
/* do NOT remove this line for now, If something doesn't work,
191
* fix your code instead! */
192
g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING);
195
/* parse context options */
196
context = g_option_context_new (_("[ARGUMENTS...]"));
197
g_option_context_add_main_entries (context, option_entries, GETTEXT_PACKAGE);
198
g_option_context_add_group (context, gtk_get_option_group (TRUE));
199
g_option_context_add_group (context, xfce_sm_client_get_option_group (argc, argv));
200
if (!g_option_context_parse (context, &argc, &argv, &error))
202
g_print ("%s: %s.\n", PACKAGE_NAME, error->message);
203
g_print (_("Type \"%s --help\" for usage."), G_LOG_DOMAIN);
205
g_error_free (error);
209
g_option_context_free (context);
211
gtk_init (&argc, &argv);
215
/* print version information */
216
if (opt_arguments != NULL && *opt_arguments != NULL)
217
g_print ("%s (%s)", *opt_arguments, PACKAGE_NAME);
219
g_print ("%s", PACKAGE_NAME);
220
g_print (" %s (Xfce %s)\n\n", PACKAGE_VERSION, xfce_version_string ());
221
g_print ("%s\n", "Copyright (c) 2004-2010");
222
g_print ("\t%s\n\n", _("The Xfce development team. All rights reserved."));
223
g_print (_("Please report bugs to <%s>."), PACKAGE_BUGREPORT);
228
else if (opt_preferences >= 0)
230
/* send a signal to the running instance to show the preferences dialog */
231
succeed = panel_dbus_client_display_preferences_dialog (opt_preferences, &error);
234
else if (opt_add_items >= 0)
236
/* send a signal to the running instance to show the add items dialog */
237
succeed = panel_dbus_client_display_items_dialog (opt_add_items, &error);
242
/* send a save signal to the running instance */
243
succeed = panel_dbus_client_save (&error);
246
else if (opt_add != NULL)
248
/* send a add-new-item signal to the running instance */
249
succeed = panel_dbus_client_add_new_item (opt_add, opt_arguments, &error);
252
else if (opt_restart || opt_quit)
254
/* send a terminate signal to the running instance */
255
succeed = panel_dbus_client_terminate (opt_restart, &error);
258
else if (opt_plugin_event != NULL)
260
/* send the plugin event to the running instance */
261
succeed = panel_dbus_client_plugin_event (opt_plugin_event, &error);
267
/* start dbus service */
268
dbus_service = panel_dbus_service_get ();
269
if (!panel_dbus_service_is_owner (dbus_service))
271
/* quit without error if an instance is running */
274
g_print ("%s: %s\n\n", G_LOG_DOMAIN, _("There is already a running instance"));
278
/* start session management */
279
sm_client = xfce_sm_client_get ();
280
xfce_sm_client_set_restart_style (sm_client, XFCE_SM_CLIENT_RESTART_IMMEDIATELY);
281
xfce_sm_client_set_priority (sm_client, XFCE_SM_CLIENT_PRIORITY_CORE);
282
g_signal_connect (G_OBJECT (sm_client), "quit",
283
G_CALLBACK (panel_sm_client_quit), NULL);
284
if (!xfce_sm_client_connect (sm_client, &error))
286
g_printerr ("%s: Failed to connect to session manager: %s\n",
287
G_LOG_DOMAIN, error->message);
288
g_clear_error (&error);
291
/* setup signal handlers to properly quit the main loop */
292
for (i = 0; i < G_N_ELEMENTS (signums); i++)
293
signal (signums[i], panel_signal_handler);
295
application = panel_application_get ();
297
/* save the state before the quit signal if we can, this is a bit safer */
298
g_signal_connect (G_OBJECT (sm_client), "save-state",
299
G_CALLBACK (panel_sm_client_save_state), application);
301
/* open dialog if we started from launch_panel */
302
if (opt_preferences >= 0)
303
panel_preferences_dialog_show (panel_application_get_nth_window (application, opt_preferences));
307
/* make sure there are no incomming events when we close */
308
g_object_unref (G_OBJECT (dbus_service));
310
/* destroy all the opened dialogs */
311
panel_application_destroy_dialogs (application);
313
/* only save if we do not quit by the sm client */
314
if (!sm_client_saved_state)
315
panel_application_save (application, TRUE);
317
g_object_unref (G_OBJECT (application));
318
g_object_unref (G_OBJECT (sm_client));
320
if (panel_dbus_service_get_restart ())
322
/* spawn ourselfs again */
323
g_print ("%s: %s\n\n", G_LOG_DOMAIN, _("Restarting..."));
324
g_spawn_command_line_async (argv[0], NULL);
331
/* stop any running startup notification */
332
gdk_notify_startup_complete ();
334
if (G_UNLIKELY (error != NULL))
336
/* get suitable error message */
337
if (opt_preferences >= 0)
338
error_msg = _("Failed to show the preferences dialog");
339
else if (opt_add_items >= 0)
340
error_msg = _("Failed to show the add new items dialog");
342
error_msg = _("Failed to save the panel configuration");
344
error_msg = _("Failed to add a plugin to the panel");
345
else if (opt_restart)
346
error_msg = _("Failed to restart the panel");
348
error_msg = _("Failed to quit the panel");
350
error_msg = _("Failed to send D-Bus message");
352
/* show understandable message for this common error */
353
if (error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
355
/* normally start the panel */
356
if (opt_preferences >= 0)
358
g_clear_error (&error);
360
if (xfce_dialog_confirm (NULL, GTK_STOCK_EXECUTE, NULL,
361
_("Do you want to start the panel? If you do, make sure "
362
"you save the session on logout, so the panel is "
363
"automatically started the next time you login."),
364
_("No running instance of %s was found"), G_LOG_DOMAIN))
366
panel_debug (PANEL_DEBUG_DOMAIN_MAIN, "user confirmed to start the panel");
376
/* I18N: %s is replaced with xfce4-panel */
377
g_clear_error (&error);
378
g_set_error (&error, 0, 0, _("No running instance of %s was found"), G_LOG_DOMAIN);
382
/* show error dialog */
383
xfce_dialog_show_error (NULL, error, "%s", error_msg);
384
g_error_free (error);
387
return succeed ? EXIT_SUCCESS : EXIT_FAILURE;