system-tile.c 8.36 KB
Newer Older
Jim Krehl's avatar
Jim Krehl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * This file is part of the Main Menu.
 *
 * Copyright (c) 2006 Novell, Inc.
 *
 * The Main Menu 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.
 *
 * The Main Menu is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * the Main Menu; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "system-tile.h"

#include <string.h>
#include <glib/gi18n.h>
#include <gconf/gconf-client.h>
#include <libgnomeui/gnome-client.h>

#include "slab-gnome-util.h"
#include "gnome-utils.h"

G_DEFINE_TYPE (SystemTile, system_tile, NAMEPLATE_TILE_TYPE)

33 34
static void system_tile_finalize (GObject *);
static void system_tile_style_set (GtkWidget *, GtkStyle *);
Jim Krehl's avatar
Jim Krehl committed
35

36 37
static void load_image (SystemTile *);
static GtkWidget *create_header (const gchar *);
Jim Krehl's avatar
Jim Krehl committed
38

39
static void system_tile_activated (Tile *, TileEvent *);
Jim Krehl's avatar
Jim Krehl committed
40

41 42 43
static void system_tile_open (Tile *, TileEvent *, TileAction *);
static void system_tile_logout (Tile *, TileEvent *, TileAction *);
static void system_tile_lock_screen (Tile *, TileEvent *, TileAction *);
Jim Krehl's avatar
Jim Krehl committed
44

45 46 47 48 49 50 51 52 53 54 55
typedef struct
{
	SystemTileType type;
	
	GnomeDesktopItem *desktop_item;
	
	gchar *image_id;
	gboolean image_is_broken;
	
	MainMenuConf *conf;
} SystemTilePrivate;
Jim Krehl's avatar
Jim Krehl committed
56 57 58

#define SYSTEM_TILE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SYSTEM_TILE_TYPE, SystemTilePrivate))

59
static void system_tile_class_init (SystemTileClass * sys_tile_class)
Jim Krehl's avatar
Jim Krehl committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
	GObjectClass *g_obj_class = G_OBJECT_CLASS (sys_tile_class);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (sys_tile_class);
	TileClass *tile_class = TILE_CLASS (sys_tile_class);

	g_obj_class->finalize = system_tile_finalize;

	widget_class->style_set = system_tile_style_set;

	tile_class->tile_activated = system_tile_activated;

	g_type_class_add_private (sys_tile_class, sizeof (SystemTilePrivate));
}

GtkWidget *
system_tile_new_with_type (SystemTileType type, MainMenuConf * conf)
{
	SystemTile *tile;
	SystemTilePrivate *priv;

	gchar *uri = NULL;
	GtkWidget *header = NULL;

	GnomeDesktopItem *desktop_item = NULL;
	gchar *image_id = NULL;
85 86 87
	gchar *header_txt = NULL;

	AtkObject *accessible = NULL;
Jim Krehl's avatar
Jim Krehl committed
88 89 90 91

	if (!(0 <= type && type < SYSTEM_TILE_TYPE_SENTINEL))
		return NULL;

92 93
	switch (type)
	{
Jim Krehl's avatar
Jim Krehl committed
94
	case SYSTEM_TILE_TYPE_HELP:
95 96 97 98 99 100 101 102
		desktop_item = load_desktop_item_by_unknown_id (conf->system_area_conf->help_item);

		if (desktop_item)
		{
			image_id =
				g_strdup (gnome_desktop_item_get_localestring (desktop_item,
					"Icon"));
			uri = g_strdup (gnome_desktop_item_get_location (desktop_item));
103
			header_txt = g_strdup (_("_Help"));
104 105
		}

Jim Krehl's avatar
Jim Krehl committed
106 107 108
		break;

	case SYSTEM_TILE_TYPE_CONTROL_CENTER:
109 110 111 112 113 114 115 116
		desktop_item = load_desktop_item_by_unknown_id (
			conf->system_area_conf->control_center_item);

		if (desktop_item)
		{
			image_id = g_strdup (
				gnome_desktop_item_get_localestring (desktop_item, "Icon"));
			uri = g_strdup (gnome_desktop_item_get_location (desktop_item));
117
			header_txt = g_strdup (_("_Control Center"));
118 119
		}

Jim Krehl's avatar
Jim Krehl committed
120 121 122
		break;

	case SYSTEM_TILE_TYPE_PACKAGE_MANAGER:
123 124 125 126 127 128 129 130
		desktop_item = load_desktop_item_by_unknown_id (
			conf->system_area_conf->package_manager_item);

		if (desktop_item)
		{
			image_id = g_strdup (
				gnome_desktop_item_get_localestring (desktop_item, "Icon"));
			uri = g_strdup (gnome_desktop_item_get_location (desktop_item));
131
			header_txt = g_strdup (_("I_nstall Software"));
132 133
		}

Jim Krehl's avatar
Jim Krehl committed
134 135 136
		break;

	case SYSTEM_TILE_TYPE_LOG_OUT:
137 138 139
	        image_id = g_strdup ("gnome-logout");
		header_txt = g_strdup (_("Log _Out ..."));
		uri = g_strdup ("system-tile://logout");
140

Jim Krehl's avatar
Jim Krehl committed
141 142 143
		break;

	case SYSTEM_TILE_TYPE_LOCK_SCREEN:
144 145 146
	        image_id = g_strdup ("gnome-lockscreen");
		header_txt = g_strdup (_("_Lock Screen ..."));
		uri = g_strdup ("system-tile://lockscreen");
147

Jim Krehl's avatar
Jim Krehl committed
148 149 150 151 152 153
		break;

	default:
		break;
	}

154 155 156
	if (!uri)
		return NULL;

157
	if (!header_txt)
158 159
	{
		g_warning ("Unable to make header for SystemTileType:%d\n", type);
Jim Krehl's avatar
Jim Krehl committed
160

161
		header_txt = g_strdup (_("_Unknown"));
Jim Krehl's avatar
Jim Krehl committed
162 163
	}

164 165
	header = create_header (header_txt);

166 167
	tile = g_object_new (SYSTEM_TILE_TYPE, "tile-uri", uri, "nameplate-image", gtk_image_new (),
		"nameplate-header", header, "nameplate-subheader", NULL, NULL);
Jim Krehl's avatar
Jim Krehl committed
168 169 170 171

	TILE (tile)->actions = g_new0 (TileAction *, 1);
	TILE (tile)->n_actions = 1;

172 173
	TILE (tile)->actions[SYSTEM_TILE_ACTION_OPEN] =
		tile_action_new (TILE (tile), system_tile_open, NULL, TILE_ACTION_OPENS_NEW_WINDOW);
Jim Krehl's avatar
Jim Krehl committed
174

175
	TILE (tile)->default_action = TILE (tile)->actions[SYSTEM_TILE_ACTION_OPEN];
Jim Krehl's avatar
Jim Krehl committed
176 177 178 179

	priv = SYSTEM_TILE_GET_PRIVATE (tile);
	priv->type = type;
	priv->desktop_item = desktop_item;
180
	priv->image_id = g_strdup (image_id);
Jim Krehl's avatar
Jim Krehl committed
181 182 183 184
	priv->conf = conf;

	load_image (tile);

185 186 187 188 189 190 191 192 193 194 195 196
	/* Set up the mnemonic for the tile */
	gtk_label_set_mnemonic_widget (GTK_LABEL (header), GTK_WIDGET (tile));

	/* Set up the accessible name for the tile */
	accessible = gtk_widget_get_accessible (GTK_WIDGET (tile));
	if (header_txt)
	  atk_object_set_name (accessible, header_txt);

	g_free (header_txt);
	g_free (image_id);
	g_free (uri);

Jim Krehl's avatar
Jim Krehl committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	return GTK_WIDGET (tile);
}

static void
system_tile_init (SystemTile * tile)
{
	SystemTilePrivate *priv = SYSTEM_TILE_GET_PRIVATE (tile);

	priv->desktop_item = NULL;
	priv->image_id = NULL;
	priv->image_is_broken = TRUE;
}

static void
system_tile_finalize (GObject * g_object)
{
213 214 215 216 217 218
        SystemTile *tile = SYSTEM_TILE (g_object);
        SystemTilePrivate *priv = SYSTEM_TILE_GET_PRIVATE (tile);

	g_free (priv->image_id);
	gnome_desktop_item_unref (priv->desktop_item);

219 220
	/* FIXME - more to free ? */
	(*G_OBJECT_CLASS (system_tile_parent_class)->finalize) (g_object);
Jim Krehl's avatar
Jim Krehl committed
221 222 223 224 225 226 227 228 229 230 231 232 233
}

static void
system_tile_style_set (GtkWidget * widget, GtkStyle * prev_style)
{
	load_image (SYSTEM_TILE (widget));
}

static void
load_image (SystemTile * tile)
{
	SystemTilePrivate *priv = SYSTEM_TILE_GET_PRIVATE (tile);

234 235
	priv->image_is_broken = load_image_by_id (
		GTK_IMAGE (NAMEPLATE_TILE (tile)->image), GTK_ICON_SIZE_MENU, priv->image_id);
Jim Krehl's avatar
Jim Krehl committed
236 237 238 239 240 241 242 243
}

static GtkWidget *
create_header (const gchar * name)
{
	GtkWidget *header;

	header = gtk_label_new (name);
244
	gtk_label_set_use_underline (GTK_LABEL (header), TRUE);
Jim Krehl's avatar
Jim Krehl committed
245 246 247 248 249 250 251 252
	gtk_misc_set_alignment (GTK_MISC (header), 0.0, 0.5);

	return header;
}

static void
system_tile_activated (Tile * tile, TileEvent * event)
{
253
	tile_trigger_action_with_time (tile, tile->default_action, event->time);
Jim Krehl's avatar
Jim Krehl committed
254 255 256 257 258 259 260
}

static void
system_tile_open (Tile * tile, TileEvent * event, TileAction * action)
{
	SystemTilePrivate *priv = SYSTEM_TILE_GET_PRIVATE (tile);

261 262
	switch (priv->type)
	{
Jim Krehl's avatar
Jim Krehl committed
263 264 265
	case SYSTEM_TILE_TYPE_HELP:
	case SYSTEM_TILE_TYPE_CONTROL_CENTER:
	case SYSTEM_TILE_TYPE_PACKAGE_MANAGER:
266
		open_desktop_item_exec (SYSTEM_TILE_GET_PRIVATE (tile)->desktop_item);
Jim Krehl's avatar
Jim Krehl committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
		break;

	case SYSTEM_TILE_TYPE_LOG_OUT:
		system_tile_logout (tile, event, action);
		break;

	case SYSTEM_TILE_TYPE_LOCK_SCREEN:
		system_tile_lock_screen (tile, event, action);
		break;

	default:
		break;
	}
}

static void
system_tile_logout (Tile * tile, TileEvent * event, TileAction * action)
{
	GnomeClient *client = gnome_master_client ();

	if (!client)
		return;

290 291
	gnome_client_request_save (client, GNOME_SAVE_GLOBAL, TRUE, GNOME_INTERACT_ANY, FALSE,
		TRUE);
Jim Krehl's avatar
Jim Krehl committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

static void
system_tile_lock_screen (Tile * tile, TileEvent * event, TileAction * action)
{
	GSList *command_priority;

	gchar *exec_string;
	gchar *cmd_string;
	gchar *arg_string;

	GSList *node;
	gint i;

306
	command_priority = get_slab_gconf_slist (SLAB_LOCK_SCREEN_PRIORITY_KEY);
Jim Krehl's avatar
Jim Krehl committed
307

308 309
	for (node = command_priority; node; node = node->next)
	{
Jim Krehl's avatar
Jim Krehl committed
310 311 312 313 314
		exec_string = (gchar *) node->data;

		cmd_string = g_strdup (exec_string);
		arg_string = NULL;

315 316 317 318
		for (i = 0; i < strlen (exec_string); ++i)
		{
			if (g_ascii_isspace (exec_string[i]))
			{
Jim Krehl's avatar
Jim Krehl committed
319
				cmd_string = g_strndup (exec_string, i);
320
				arg_string = g_strdup (&exec_string[i + 1]);
Jim Krehl's avatar
Jim Krehl committed
321 322 323 324 325
			}
		}

		cmd_string = g_find_program_in_path (cmd_string);

326 327 328
		if (cmd_string)
		{
			exec_string = g_strdup_printf ("%s %s", cmd_string, arg_string);
Jim Krehl's avatar
Jim Krehl committed
329

330 331
			g_spawn_async (NULL, g_strsplit (exec_string, " ", 0), NULL, 0, NULL, NULL,
				NULL, NULL);
Jim Krehl's avatar
Jim Krehl committed
332 333 334 335 336 337 338 339 340 341 342

			g_free (cmd_string);
			g_free (arg_string);
			g_free (exec_string);

			return;
		}
	}

	g_warning ("could not find a command to lock screen\n");
}