panel-action-button.c 16.9 KB
Newer Older
1
/*
2
 * panel-action-button.c: panel "Action Button" module
3
 *
Mark McLoughlin's avatar
Mark McLoughlin committed
4
 * Copyright (C) 2002 Sun Microsystems, Inc.
5
 *
6 7 8 9
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
10
 *
11 12
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * General Public License for more details.
15
 *
16 17 18 19
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * Authors:
 *	Mark McLoughlin <mark@skynet.ie>
 */

#include <config.h>
#include <string.h>
#include <stdlib.h>

#include "panel-action-button.h"

#include <libgnome/gnome-i18n.h>

#include "applet.h"
#include "egg-screen-exec.h"
#include "menu.h"
#include "panel-config-global.h"
#include "panel-gconf.h"
38
#include "panel-profile.h"
39 40
#include "panel-stock-icons.h"
#include "panel-typebuiltins.h"
Mark McLoughlin's avatar
Mark McLoughlin committed
41
#include "panel-force-quit.h"
42
#include "panel-util.h"
43 44
#include "panel-session.h"
#include "panel-globals.h"
45
#include "panel-run-dialog.h"
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

enum {
	PROP_0,
	PROP_ACTION_TYPE,
};

struct _PanelActionButtonPrivate {
	PanelActionButtonType  type;
	AppletInfo            *info;

	guint                  gconf_notify;
};

static GObjectClass *parent_class;

static GConfEnumStringPair panel_action_type_map [] = {
	{ PANEL_ACTION_NONE,         "none"         },
	{ PANEL_ACTION_LOCK,         "lock"         },
	{ PANEL_ACTION_LOGOUT,       "logout"       },
	{ PANEL_ACTION_RUN,          "run"          },
	{ PANEL_ACTION_SEARCH,       "search"       },
	{ PANEL_ACTION_SCREENSHOT,   "screenshot"   },
Mark McLoughlin's avatar
Mark McLoughlin committed
68
	{ PANEL_ACTION_FORCE_QUIT,   "force-quit"   },
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
};

/* Lock Screen
 */
void
panel_action_lock_screen (GtkWidget *widget)
{
	panel_lock_screen (gtk_widget_get_screen (widget));
}

static void
panel_action_lock_setup_menu (PanelActionButton *button)
{
	panel_applet_add_callback (button->priv->info, "activate", NULL, _("_Activate Screensaver"));
	panel_applet_add_callback (button->priv->info, "lock",     NULL, _("_Lock Screen"));
	panel_applet_add_callback (button->priv->info, "exit",     NULL, _("_Kill Screensaver Daemon"));
	panel_applet_add_callback (button->priv->info, "restart",  NULL, _("Restart _Screensaver Daemon"));

	if (!commie_mode)
		panel_applet_add_callback (button->priv->info, "prefs", NULL, _("_Properties"));
}

static void
panel_action_lock_invoke_menu (PanelActionButton *button,
			       const char *callback_name)
{
	char *command = NULL;

	if (!strcmp (callback_name, "restart"))
		command = g_strdup ("xscreensaver-command -exit ; xscreensaver &");

	else if (!strcmp (callback_name, "prefs"))
		command = g_strdup ("xscreensaver-demo");

	else if (!strcmp (callback_name, "activate") ||
		 !strcmp (callback_name, "lock") ||
		 !strcmp (callback_name, "exit"))
		command = g_strdup_printf ("xscreensaver-command -%s", callback_name);

	if (command)
		egg_screen_execute_shell (
			gtk_widget_get_screen (GTK_WIDGET (button)),
					       g_get_home_dir (), command);

	g_free (command);
}

/* Log Out
 */
void
panel_action_logout (GtkWidget *widget)
{
	static int recursion_guard = 0;

	if (recursion_guard)
		return;

	recursion_guard++;

128
	panel_session_request_logout ();
129 130 131 132

	recursion_guard--;
}

133
/* Run Application
134 135 136 137
 */
void
panel_action_run_program (GtkWidget *widget)
{
138
	panel_run_dialog_present (gtk_widget_get_screen (widget));
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

/* Search For Files
 */
void
panel_action_search (GtkWidget *widget)
{
	GdkScreen *screen;
	char      *argv[2] = {"gnome-search-tool", NULL};

	screen = gtk_widget_get_screen (widget);

	if (egg_screen_execute_async (screen, g_get_home_dir (), 1, argv) < 0)
		panel_error_dialog (screen,
				    "cannot_exec_gnome-search-tool",
				    _("Cannot execute gnome-search-tool"));
}

/* Take Screenshot
 */
void
panel_action_screenshot (GtkWidget *widget)
{
	GdkScreen *screen;
	char      *argv [2] = {"gnome-panel-screenshot", NULL};

	screen = gtk_widget_get_screen (widget);

	if (egg_screen_execute_async (screen, g_get_home_dir (), 1, argv) < 0)
		panel_error_dialog (screen,
				    "cannot_exec_gnome-panel-screenshot",
				    _("Cannot execute gnome-panel-screenshot"));
}

Mark McLoughlin's avatar
Mark McLoughlin committed
173 174 175 176 177 178 179 180 181 182 183 184
/* Force Quit
 */
static void
panel_action_force_quit (GtkWidget *widget)
{
	GdkScreen *screen;

	screen = gtk_widget_get_screen (widget);

	panel_force_quit (screen);
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
typedef struct {
	PanelActionButtonType   type;
	char                   *stock_icon;
	char                   *tooltip;
	char                   *help_index;
	void                  (*invoke)      (GtkWidget         *widget);
	void                  (*setup_menu)  (PanelActionButton *button);
	void                  (*invoke_menu) (PanelActionButton *button,
					      const char        *callback_name);
} PanelAction;

/* Keep order in sync with PanelActionButtonType
 */
static PanelAction actions [] = {
	{ PANEL_ACTION_NONE, NULL, NULL },
	{
		PANEL_ACTION_LOCK, PANEL_STOCK_LOCKSCREEN,
		N_("Lock screen"), "gospanel-21",
		panel_action_lock_screen,
		panel_action_lock_setup_menu,
		panel_action_lock_invoke_menu
	},
	{
		PANEL_ACTION_LOGOUT, PANEL_STOCK_LOGOUT,
		N_("Log out of GNOME"), "gospanel-20",
		panel_action_logout, NULL, NULL
	},
	{
		PANEL_ACTION_RUN, PANEL_STOCK_RUN,
214
		N_("Run Application"), NULL,
215 216 217 218 219 220 221 222 223 224 225
		panel_action_run_program, NULL, NULL
	},
	{
		PANEL_ACTION_SEARCH, PANEL_STOCK_SEARCHTOOL,
		N_("Search for Files"), NULL,
		panel_action_search, NULL, NULL
	},
	{
		PANEL_ACTION_SCREENSHOT, PANEL_STOCK_SCREENSHOT,
		N_("Screenshot"), NULL,
		panel_action_screenshot, NULL, NULL
Mark McLoughlin's avatar
Mark McLoughlin committed
226 227 228 229 230
	},
	{
		PANEL_ACTION_FORCE_QUIT, PANEL_STOCK_FORCE_QUIT,
		N_("Force Quit"), NULL,
		panel_action_force_quit, NULL, NULL
231 232 233 234 235 236 237 238 239 240 241
	}
};

static void
panel_action_button_finalize (GObject *object)
{
	PanelActionButton *button = PANEL_ACTION_BUTTON (object);

	button->priv->info = NULL;
	button->priv->type = PANEL_ACTION_NONE;

242 243
	gconf_client_notify_remove (panel_gconf_get_client (),
				    button->priv->gconf_notify);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	button->priv->gconf_notify = 0;

	g_free (button->priv);
	button->priv = NULL;

	parent_class->finalize (object);
}

static void
panel_action_button_get_property (GObject    *object,
				  guint       prop_id,
				  GValue     *value,
				  GParamSpec *pspec)
{
	PanelActionButton *button;

	g_return_if_fail (PANEL_IS_ACTION_BUTTON (object));

	button = PANEL_ACTION_BUTTON (object);

	switch (prop_id) {
	case PROP_ACTION_TYPE:
		g_value_set_enum (value, button->priv->type);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
panel_action_button_set_property (GObject      *object,
				  guint         prop_id,
				  const GValue *value,
				  GParamSpec   *pspec)
{
	PanelActionButton *button;

	g_return_if_fail (PANEL_IS_ACTION_BUTTON (object));

	button = PANEL_ACTION_BUTTON (object);

	switch (prop_id) {
	case PROP_ACTION_TYPE:
		panel_action_button_set_type (button,
					      g_value_get_enum (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
panel_action_button_drag_data_get (GtkWidget          *widget,
				   GdkDragContext     *context,
				   GtkSelectionData   *selection_data,
				   guint               info,
				   guint               time)
{
	PanelActionButton *button;
	char              *drag_data;

	g_return_if_fail (PANEL_IS_ACTION_BUTTON (widget));

	button = PANEL_ACTION_BUTTON (widget);

	drag_data = g_strdup_printf ("ACTION:%s:%d", 
				     gconf_enum_to_string (panel_action_type_map, button->priv->type),
313
				     panel_find_applet_index (widget));
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

	gtk_selection_data_set (
		selection_data, selection_data->target,
		8, (guchar *) drag_data, strlen (drag_data));

	g_free (drag_data);
}

static void
panel_action_button_clicked (GtkButton *gtk_button)
{
	PanelActionButton *button;

	g_return_if_fail (PANEL_IS_ACTION_BUTTON (gtk_button));

	button = PANEL_ACTION_BUTTON (gtk_button);

	g_return_if_fail (button->priv->type > PANEL_ACTION_NONE);
	g_return_if_fail (button->priv->type < PANEL_ACTION_LAST);

334 335
	if (panel_global_config_get_drawer_auto_close ()) {
		PanelToplevel *toplevel;
336

337
		toplevel = PANEL_WIDGET (GTK_WIDGET (button)->parent)->toplevel;
338

339 340
		if (panel_toplevel_get_is_attached (toplevel))
			panel_toplevel_hide (toplevel, FALSE, -1);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	}

	if (actions [button->priv->type].invoke)
		actions [button->priv->type].invoke (GTK_WIDGET (button));
}

static void
panel_action_button_class_init (PanelActionButtonClass *klass,
				gpointer               dummy)
{
	GObjectClass   *gobject_class = (GObjectClass *) klass;
	GtkWidgetClass *widget_class  = (GtkWidgetClass *) klass;
	GtkButtonClass *button_class  = (GtkButtonClass *) klass;

	parent_class = g_type_class_peek_parent (klass);

	gobject_class->finalize     = panel_action_button_finalize;
	gobject_class->get_property = panel_action_button_get_property;
	gobject_class->set_property = panel_action_button_set_property;

	widget_class->drag_data_get = panel_action_button_drag_data_get;

	button_class->clicked       = panel_action_button_clicked;

	g_object_class_install_property (
			gobject_class,
			PROP_ACTION_TYPE,
			g_param_spec_enum ("action-type",
					   _("Action Type"),
					   _("The type of action this button implements"),
371
					   PANEL_TYPE_ACTION_BUTTON_TYPE,
372
					   PANEL_ORIENTATION_TOP,
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
					   G_PARAM_READWRITE));
}

static void
panel_action_button_instance_init (PanelActionButton      *button,
				   PanelActionButtonClass *klass)
{
	static GtkTargetEntry dnd_targets [] = {
		{ "application/x-panel-applet-internal", 0, 0 }
	};

	button->priv = g_new0 (PanelActionButtonPrivate, 1);

	button->priv->type = PANEL_ACTION_NONE;
	button->priv->info = NULL;

	button->priv->gconf_notify = 0;

	GTK_WIDGET_UNSET_FLAGS (button, GTK_NO_WINDOW);
	gtk_drag_source_set (GTK_WIDGET (button), GDK_BUTTON1_MASK,
			     dnd_targets, 1,
			     GDK_ACTION_COPY | GDK_ACTION_MOVE);
	GTK_WIDGET_SET_FLAGS (button, GTK_NO_WINDOW);

}

GType
panel_action_button_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (PanelActionButtonClass),
			NULL,
			NULL,
			(GClassInitFunc) panel_action_button_class_init,
			NULL,
			NULL,
			sizeof (PanelActionButton),
			0,
			(GInstanceInitFunc) panel_action_button_instance_init,
			NULL
		};

		type = g_type_register_static (
				BUTTON_TYPE_WIDGET, "PanelActionButton", &info, 0);
	}

	return type;
}

void
panel_action_button_set_type (PanelActionButton     *button,
			      PanelActionButtonType  type)
{
	g_return_if_fail (type > PANEL_ACTION_NONE && type < PANEL_ACTION_LAST);

	if (type == button->priv->type)
		return;

	button->priv->type = type;

	button_widget_set_stock_id (BUTTON_WIDGET (button), actions [type].stock_icon);

	gtk_tooltips_set_tip (panel_tooltips, GTK_WIDGET (button),
			      _(actions [type].tooltip), NULL);
}

static void
panel_action_button_type_changed (GConfClient       *client,
				  guint              cnxn_id,
				  GConfEntry        *entry,
				  PanelActionButton *button)
{
448 449
	int         type;
	const char *action_type;
450 451 452 453 454 455 456 457

	g_return_if_fail (PANEL_IS_ACTION_BUTTON (button));

	if (!entry->value || entry->value->type != GCONF_VALUE_STRING)
		return;

	action_type = gconf_value_get_string (entry->value);

458
	if (!gconf_string_to_enum (panel_action_type_map, action_type, &type))
459 460 461 462 463 464 465 466 467 468 469
		return;

	panel_action_button_set_type (button, type);
}

static void
panel_action_button_connect_to_gconf (PanelActionButton *button)
{
	const char  *key;
	const char  *profile;

470
	profile = panel_profile_get_name ();
471 472

	key = panel_gconf_full_key (
473
			PANEL_GCONF_OBJECTS, profile, button->priv->info->id, "action_type");
474 475

	button->priv->gconf_notify =
476
		gconf_client_notify_add (panel_gconf_get_client (), key, 
477 478 479 480
					 (GConfClientNotifyFunc) panel_action_button_type_changed,
					 button, NULL, NULL);
}

481
static void
482
panel_action_button_style_set (PanelActionButton *button)
483 484 485 486
{
	button_widget_set_stock_id (BUTTON_WIDGET (button), actions [button->priv->type].stock_icon);
}

487
static void
488 489 490 491
panel_action_button_load (PanelActionButtonType  type,
			  PanelWidget           *panel,
			  int                    position,
			  gboolean               exactpos,
492
			  const char            *id,
493
			  gboolean               compatibility)
494 495
{
	PanelActionButton *button;
496
	PanelObjectType    object_type;
497

498
	g_return_if_fail (panel != NULL);
499 500 501

	button = g_object_new (PANEL_TYPE_ACTION_BUTTON, "action-type", type, NULL);

502
	object_type = PANEL_OBJECT_ACTION;
503 504
	if (compatibility) { /* Backward compatibility with GNOME 2.0.x */
		if (type == PANEL_ACTION_LOCK)
505
			object_type = PANEL_OBJECT_LOCK;
506
		else if (type == PANEL_ACTION_LOGOUT)
507
			object_type = PANEL_OBJECT_LOGOUT;
508 509
	}

510 511
	button->priv->info = panel_applet_register (
				GTK_WIDGET (button), NULL, NULL, panel,
512
				position, exactpos, object_type, id);
513 514
	if (!button->priv->info) {
		gtk_widget_destroy (GTK_WIDGET (button));
515
		return;
516 517 518 519 520 521 522 523 524 525
	}

	panel_applet_add_callback (
		button->priv->info, "help", GTK_STOCK_HELP, _("_Help"));

	if (actions [button->priv->type].setup_menu)
		actions [button->priv->type].setup_menu (button);

	panel_action_button_connect_to_gconf (button);

526 527
	g_signal_connect (button, "style-set",
			  G_CALLBACK (panel_action_button_style_set), NULL);
528 529
}

530 531 532 533
void
panel_action_button_create (PanelToplevel         *toplevel,
			    int                    position,
			    PanelActionButtonType  type)
534
{
535 536 537 538
	GConfClient *client;
	const char  *profile;
	const char  *key;
	char        *id;
539 540

	client  = panel_gconf_get_client ();
541
	profile = panel_profile_get_name ();
542

543
	id = panel_profile_prepare_object (PANEL_OBJECT_ACTION, toplevel, position, FALSE);
544

545 546 547 548 549
	key = panel_gconf_full_key (PANEL_GCONF_OBJECTS, profile, id, "action_type");
	gconf_client_set_string (client,
				 key,
				 gconf_enum_to_string (panel_action_type_map, type),
				 NULL);
550

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	/* frees id */
	panel_profile_add_to_list (PANEL_GCONF_OBJECTS, id);
}

/* This is only for backwards compatibility with 2.0.x
 * We load an old-style lock/logout button as an action
 * button but make sure to retain the lock/logout configuration
 * so logging back into 2.0.x still works.
 */
void
panel_action_button_load_compatible (PanelObjectType  object_type,
				     PanelWidget     *panel,
				     int              position,
				     gboolean         exactpos,
				     const char      *id)
{
	PanelActionButtonType action_type;

	g_assert (object_type == PANEL_OBJECT_LOGOUT || object_type == PANEL_OBJECT_LOCK);
570

571
	action_type = object_type == PANEL_OBJECT_LOGOUT ? PANEL_ACTION_LOGOUT : PANEL_ACTION_LOCK;
572

573
	panel_action_button_load (action_type, panel, position, exactpos, id, TRUE);
574 575 576
}

void
577 578 579 580
panel_action_button_load_from_gconf (PanelWidget *panel,
				     int          position,
				     gboolean     exactpos,
				     const char  *id)
581
{
582
	int          type;
583 584
	const char  *key;
	const char  *profile;
585
	char        *action_type;
586

587
	profile = panel_profile_get_name ();
588

589 590
	key = panel_gconf_full_key (PANEL_GCONF_OBJECTS, profile, id, "action_type");
	action_type = gconf_client_get_string (panel_gconf_get_client (), key, NULL);
591

592 593 594 595 596
	if (!gconf_string_to_enum (panel_action_type_map, action_type, &type)) {
		g_warning ("Unkown action type '%s' from %s", action_type, key);
		g_free (action_type);
		return;
	}
597

598
	g_free (action_type);
599

600
	panel_action_button_load (type, panel, position, exactpos, id, FALSE);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
}

void
panel_action_button_invoke_menu (PanelActionButton *button,
				 const char        *callback_name)
{
	g_return_if_fail (PANEL_IS_ACTION_BUTTON (button));
	g_return_if_fail (callback_name != NULL);
	g_return_if_fail (button->priv->type > PANEL_ACTION_NONE &&
			  button->priv->type < PANEL_ACTION_LAST);

	if (!strcmp (callback_name, "help")) {
		GdkScreen *screen;

		if (!actions [button->priv->type].help_index)
			return;

		screen = gtk_widget_get_screen (GTK_WIDGET (button));

		panel_show_help (screen, "wgospanel.xml",
				 actions [button->priv->type].help_index);

		return;
	}

	if (actions [button->priv->type].invoke_menu)
		actions [button->priv->type].invoke_menu (button, callback_name);
}

gboolean
631 632 633 634
panel_action_button_load_from_drag (PanelToplevel *toplevel,
				    int            position,
				    const char    *drag_string,
				    int           *old_applet_idx)
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
{
	PanelActionButtonType   type = PANEL_ACTION_NONE;
	gboolean                retval = FALSE;
	char                  **elements;

	if (strncmp (drag_string, "ACTION:", strlen ("ACTION:")))
		return retval;

	elements = g_strsplit (drag_string, ":", 0);

	g_assert (elements != NULL);

	if (!elements [1] || !elements [2]) {
		g_strfreev (elements);
		return retval;
	}

	if (!gconf_string_to_enum (panel_action_type_map, elements [1], (int *) &type)) {
		g_strfreev (elements);
		return retval;
	}

	g_return_val_if_fail (type > PANEL_ACTION_NONE && type < PANEL_ACTION_LAST, FALSE);

	if (strcmp (elements [2], "NEW")) {
660
		*old_applet_idx = strtol (elements [2], NULL, 10);
661 662 663 664 665
		retval = TRUE; /* Remove the old applet */
	}

	g_strfreev (elements);

666
	panel_action_button_create (toplevel, position, type);
667 668 669

	return retval;
}