~ubuntu-branches/ubuntu/precise/gnome-control-center/precise-updates

1 by Sebastien Bacher
Import upstream version 2.19.1
1
/*
2
 * This file is part of the Main Menu.
3
 *
4
 * Copyright (c) 2006, 2007 Novell, Inc.
5
 *
6
 * The Main Menu is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License as published by the Free
8
 * Software Foundation; either version 2 of the License, or (at your option)
9
 * any later version.
10
 *
11
 * The Main Menu is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14
 * more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * the Main Menu; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
21
#include "system-tile.h"
1.1.30 by Chris Coulson
Import upstream version 2.27.5
22
#include "config.h"
1 by Sebastien Bacher
Import upstream version 2.19.1
23
24
#include <string.h>
1.1.30 by Chris Coulson
Import upstream version 2.27.5
25
#include <glib/gi18n-lib.h>
1 by Sebastien Bacher
Import upstream version 2.19.1
26
#include <gconf/gconf-client.h>
27
28
#include "bookmark-agent.h"
29
#include "slab-gnome-util.h"
30
#include "libslab-utils.h"
31
32
G_DEFINE_TYPE (SystemTile, system_tile, NAMEPLATE_TILE_TYPE)
33
34
static void system_tile_finalize (GObject *);
35
static void system_tile_style_set (GtkWidget *, GtkStyle *);
36
37
static void load_image (SystemTile *);
38
static GtkWidget *create_header (const gchar *);
39
40
static void open_trigger   (Tile *, TileEvent *, TileAction *);
41
static void remove_trigger (Tile *, TileEvent *, TileAction *);
42
43
static void update_user_list_menu_item (SystemTile *);
44
static void agent_notify_cb (GObject *, GParamSpec *, gpointer);
45
46
typedef struct {
47
	GnomeDesktopItem *desktop_item;
48
49
	BookmarkAgent       *agent;
50
	BookmarkStoreStatus  agent_status;
51
	gulong               notify_signal_id;
52
	
53
	gchar    *image_id;
54
	gboolean  image_is_broken;
55
} SystemTilePrivate;
56
57
#define PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SYSTEM_TILE_TYPE, SystemTilePrivate))
58
59
GtkWidget *
60
system_tile_new (const gchar *desktop_item_id, const gchar *title)
61
{
62
	SystemTile        *this;
63
	SystemTilePrivate *priv;
64
65
	gchar     *uri    = NULL;
66
	GtkWidget *header = NULL;
67
68
	GtkMenu *context_menu;
69
70
	TileAction  **actions;
71
	TileAction   *action;
72
	GtkWidget    *menu_item;
73
	GtkContainer *menu_ctnr;
74
75
	GnomeDesktopItem *desktop_item = NULL;
76
	gchar            *image_id     = NULL;
77
	gchar            *header_txt   = NULL;
78
79
	gchar *markup;
80
81
	AtkObject *accessible = NULL;
82
83
84
	desktop_item = libslab_gnome_desktop_item_new_from_unknown_id (desktop_item_id);
85
86
	if (desktop_item) {
87
		image_id = g_strdup (gnome_desktop_item_get_localestring (desktop_item, "Icon"));
88
		uri      = g_strdup (gnome_desktop_item_get_location (desktop_item));
89
90
		if (title)
91
			header_txt = g_strdup (title);
92
		else
93
			header_txt = g_strdup (
94
				gnome_desktop_item_get_localestring (desktop_item, "Name"));
95
	}
96
97
	if (! uri)
98
		return NULL;
99
100
	header = create_header (header_txt);
101
102
	context_menu = GTK_MENU (gtk_menu_new ());
103
104
	this = g_object_new (
105
		SYSTEM_TILE_TYPE,
106
		"tile-uri",            uri,
107
		"context-menu",        context_menu,
108
		"nameplate-image",     gtk_image_new (),
109
		"nameplate-header",    header,
110
		"nameplate-subheader", NULL,
111
		NULL);
112
	priv = PRIVATE (this);
113
114
	priv->agent = bookmark_agent_get_instance (BOOKMARK_STORE_SYSTEM);
115
	g_object_get (G_OBJECT (priv->agent), BOOKMARK_AGENT_STORE_STATUS_PROP, & priv->agent_status, NULL);
116
117
	priv->notify_signal_id = g_signal_connect (
118
		G_OBJECT (priv->agent), "notify", G_CALLBACK (agent_notify_cb), this);
119
120
	actions = g_new0 (TileAction *, 2);
121
122
	TILE (this)->actions   = actions;
123
	TILE (this)->n_actions = 2;
124
125
	menu_ctnr = GTK_CONTAINER (TILE (this)->context_menu);
126
127
	markup = g_markup_printf_escaped (_("<b>Open %s</b>"), header_txt);
128
	action = tile_action_new (TILE (this), open_trigger, markup, TILE_ACTION_OPENS_NEW_WINDOW);
129
	actions [SYSTEM_TILE_ACTION_OPEN] = action;
130
	g_free (markup);
131
132
	menu_item = GTK_WIDGET (tile_action_get_menu_item (action));
133
134
	gtk_container_add (menu_ctnr, menu_item);
135
136
	TILE (this)->default_action = action;
137
138
	gtk_container_add (menu_ctnr, gtk_separator_menu_item_new ());
139
140
	markup = g_markup_printf_escaped (_("Remove from System Items"));
141
	action = tile_action_new (TILE (this), remove_trigger, markup, 0);
142
	actions [SYSTEM_TILE_ACTION_REMOVE] = action;
143
	g_free (markup);
144
145
	menu_item = GTK_WIDGET (tile_action_get_menu_item (action));
146
147
	gtk_container_add (menu_ctnr, menu_item);
148
149
	gtk_widget_show_all (GTK_WIDGET (TILE (this)->context_menu));
150
151
	update_user_list_menu_item (this);
152
153
	priv->desktop_item = desktop_item;
154
	priv->image_id = g_strdup (image_id);
155
156
	load_image (this);
157
158
	/* Set up the mnemonic for the tile */
159
	gtk_label_set_mnemonic_widget (GTK_LABEL (header), GTK_WIDGET (this));
160
161
	/* Set up the accessible name for the tile */
162
	accessible = gtk_widget_get_accessible (GTK_WIDGET (this));
163
	if (header_txt)
164
		atk_object_set_name (accessible, header_txt);
165
166
	g_free (header_txt);
167
	g_free (image_id);
168
	g_free (uri);
169
170
	return GTK_WIDGET (this);
171
}
172
173
static void
174
system_tile_class_init (SystemTileClass *this_class)
175
{
176
	GObjectClass   *g_obj_class  = G_OBJECT_CLASS   (this_class);
177
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (this_class);
178
179
	g_obj_class->finalize = system_tile_finalize;
180
181
	widget_class->style_set = system_tile_style_set;
182
183
	g_type_class_add_private (this_class, sizeof (SystemTilePrivate));
184
}
185
186
static void
187
system_tile_init (SystemTile *this)
188
{
189
	SystemTilePrivate *priv = PRIVATE (this);
190
191
	priv->desktop_item    = NULL;
192
	priv->image_id        = NULL;
193
	priv->image_is_broken = TRUE;
194
195
	priv->agent            = NULL;
196
	priv->agent_status     = BOOKMARK_STORE_ABSENT;
197
	priv->notify_signal_id = 0;
198
}
199
200
static void
201
system_tile_finalize (GObject *g_obj)
202
{
203
        SystemTilePrivate *priv = PRIVATE (g_obj);
204
205
	g_free (priv->image_id);
206
	gnome_desktop_item_unref (priv->desktop_item);
207
208
	if (priv->notify_signal_id)
209
		g_signal_handler_disconnect (priv->agent, priv->notify_signal_id);
210
211
	G_OBJECT_CLASS (system_tile_parent_class)->finalize (g_obj);
212
}
213
214
static void
215
system_tile_style_set (GtkWidget *widget, GtkStyle *prev_style)
216
{
217
	load_image (SYSTEM_TILE (widget));
218
}
219
220
static void
221
load_image (SystemTile *this)
222
{
223
	SystemTilePrivate *priv = PRIVATE (this);
224
225
	GtkImage *image = GTK_IMAGE (NAMEPLATE_TILE (this)->image);
226
227
228
	g_object_set (G_OBJECT (image), "icon-size", GTK_ICON_SIZE_MENU, NULL);
229
230
	priv->image_is_broken = libslab_gtk_image_set_by_id (image, priv->image_id);
231
}
232
233
static GtkWidget *
234
create_header (const gchar *name)
235
{
236
	GtkWidget *header;
237
238
	header = gtk_label_new (name);
239
	gtk_label_set_use_underline (GTK_LABEL (header), TRUE);
240
	gtk_misc_set_alignment (GTK_MISC (header), 0.0, 0.5);
241
242
	return header;
243
}
244
245
static void
246
open_trigger (Tile *this, TileEvent *event, TileAction *action)
247
{
248
	open_desktop_item_exec (PRIVATE (this)->desktop_item);
249
}
250
251
static void
252
remove_trigger (Tile *this, TileEvent *event, TileAction *action)
253
{
254
	bookmark_agent_remove_item (PRIVATE (this)->agent, this->uri);
255
}
256
257
static void
258
update_user_list_menu_item (SystemTile *this)
259
{
260
	SystemTilePrivate *priv = PRIVATE (this);
261
262
	TileAction *action;
263
	GtkWidget  *item;
264
265
266
	action = TILE (this)->actions [SYSTEM_TILE_ACTION_REMOVE];
267
268
	if (! action)
269
		return;
270
271
	item = GTK_WIDGET (tile_action_get_menu_item (action));
272
273
	if (! GTK_IS_MENU_ITEM (item))
274
		return;
275
276
	g_object_get (G_OBJECT (priv->agent), BOOKMARK_AGENT_STORE_STATUS_PROP, & priv->agent_status, NULL);
277
278
	gtk_widget_set_sensitive (item, (priv->agent_status != BOOKMARK_STORE_DEFAULT_ONLY));
279
}
280
281
static void
282
agent_notify_cb (GObject *g_obj, GParamSpec *pspec, gpointer user_data)
283
{
284
	update_user_list_menu_item (SYSTEM_TILE (user_data));
285
}