A
download capplet-util.c
Language: C
License: GPL
Copyright: (C) 2001 Ximian, Inc.
LOC: 245
Project Info
gnome-control-center
Server: Gnome
Type: cvs
...rol‑center\capplets\common\
   .cvsignore
   activate-settings-daemon.c
   activate-settings-daemon.h
   ...property-editor-range.c
   ...property-editor-range.h
   ..._Capplet_generic.oaf.in
   capplet-stock-icons.c
   capplet-stock-icons.h
   capplet-util.c
   capplet-util.h
   file-transfer-dialog.c
   file-transfer-dialog.h
   ...operty-editor-marshal.c
   ...operty-editor-marshal.h
   gconf-property-editor.c
   gconf-property-editor.h
   gnome-theme-apply.c
   gnome-theme-apply.h
   gnome-theme-info.c
   gnome-theme-info.h
   gnome-theme-test.c
   Makefile.am
   theme-thumbnail.c
   theme-thumbnail.h
   wm-common.c
   wm-common.h
   wrapper-script.in

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
128
129
130
131
132
133
134
135
136
137
138
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
173
174
175
176
177
178
179
180
181
182
183
184
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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
371
372
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
/* -*- mode: c; style: linux -*- */

/* capplet-util.c
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Written by Bradford Hovinen <hovinen@ximian.com>
 *
 * 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, or (at your option)
 * any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <ctype.h>

/* For stat */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "capplet-util.h"

#if 0

/* apply_cb
 *
 * Callback issued when the user clicks "Apply" or "Ok". This function is
 * responsible for making sure the current settings are properly saved.
 */

static void
apply_cb (BonoboPropertyControl *pc, Bonobo_PropertyControl_Action action) 
{
	if (action == Bonobo_PropertyControl_APPLY)
		gconf_engine_commit_change_set (gconf_engine_get_default (),
						changeset, TRUE, NULL);
}

/* properties_changed_cb
 *
 * Callback issued when some setting has changed
 */

static void
properties_changed_cb (GConfEngine *engine, guint cnxn_id, GConfEntry *entry, gpointer user_data) 
{
	if (apply_settings_cb != NULL)
		apply_settings_cb ();
}

/* get_control_cb
 *
 * Callback to construct the main dialog box for this capplet; invoked by Bonobo
 * whenever capplet activation is requested. Returns a BonoboObject representing
 * the control that encapsulates the object.
 */

static BonoboObject *
get_control_cb (BonoboPropertyControl *property_control, gint page_number) 
{
	BonoboControl        *control;
	GtkWidget            *widget;

	widget = create_dialog_cb ();

	if (widget == NULL)
		return NULL;

	control = bonobo_control_new (widget);
	setup_property_editors_cb (widget, changeset);

	bonobo_control_set_automerge (control, TRUE);

	return BONOBO_OBJECT (control);
}

/* create_control_cb
 *
 * Small function to create the PropertyControl and return it.
 */

static BonoboObject *
create_control_cb (BonoboGenericFactory *factory, const gchar *component_id) 
{
	BonoboObject                  *obj;
	BonoboPropertyControl         *property_control;

	static const gchar            *prefix1 = "OAFIID:Bonobo_Control_Capplet_";

	g_message ("%s: Enter", G_GNUC_FUNCTION);

	if (!strncmp (component_id, prefix1, strlen (prefix1))) {
		property_control = bonobo_property_control_new
			((BonoboPropertyControlGetControlFn) get_control_cb, 1, NULL);
		g_signal_connect (G_OBJECT (property_control), "action",
				  G_CALLBACK (apply_cb), NULL);
		obj = BONOBO_OBJECT (property_control);
	} else {
		g_critical ("Not creating %s", component_id);
		obj = NULL;
	}

	return obj;
}

/* get_factory_name
 *
 * Construct the OAF IID of the factory from the binary name
 */

static gchar *
get_factory_name (const gchar *binary) 
{
	gchar *s, *tmp, *tmp1, *res;

	s = g_strdup (binary);
	tmp = strrchr (s, '/');
	if (tmp == NULL) tmp = s;
	else tmp++;
	if ((tmp1 = strstr (tmp, "-control")) != NULL) *tmp1 = '\0';
	if ((tmp1 = strstr (tmp, "-capplet")) != NULL) *tmp1 = '\0';
	while ((tmp1 = strchr (tmp, '-')) != NULL) *tmp1 = '_';

	res = g_strconcat ("OAFIID:Bonobo_", tmp, "_Factory", NULL);
	g_free (s);
	return res;
}

/* get_property_name
 *
 * Get the property name associated with this capplet
 */

static gchar *
get_property_name (const gchar *binary) 
{
	gchar *s, *tmp, *tmp1, *res;

	s = g_strdup (binary);
	tmp = strrchr (s, '/');
	if (tmp == NULL) tmp = s;
	else tmp++;
	if ((tmp1 = strstr (tmp, "-control")) != NULL) *tmp1 = '\0';
	if ((tmp1 = strstr (tmp, "-capplet")) != NULL) *tmp1 = '\0';

	for (tmp1 = tmp; *tmp1 != '\0'; tmp1++) {
		*tmp1 = toupper (*tmp1);
		if (*tmp1 == '-') *tmp1 = '_';
	}

	res = g_strconcat ("GNOME_", tmp, NULL);
	g_free (s);
	return res;
}

#endif

/* setup_session_mgmt
 *
 * Make sure the capplet launches and applies its settings next time the user
 * logs in
 */

void
setup_session_mgmt (const gchar *binary_name) 
{
/* Disabled. I never really understood this code anyway, and I am absolutely
 * unclear about how to port it to GNOME 2.0 */
#if 0
	GnomeClient *client;
	GnomeClientFlags flags;
	gint token;
	gchar *restart_args[3];
	gchar *prop_name;

	g_return_if_fail (binary_name != NULL);

	client = gnome_master_client ();
	flags = gnome_client_get_flags (client);

	if (flags & GNOME_CLIENT_IS_CONNECTED) {
		prop_name = get_property_name (binary_name);
		token = gnome_startup_acquire_token
			(prop_name, gnome_client_get_id (client));
		g_free (prop_name);

		if (token) {
			gnome_client_set_priority (client, 20);
			gnome_client_set_restart_style
				(client, GNOME_RESTART_ANYWAY);
			restart_args[0] = g_strdup (binary_name);
			restart_args[1] = "--init-session-settings";
			restart_args[2] = NULL;
			gnome_client_set_restart_command
				(client, 2, restart_args);
			g_free (restart_args[0]);
		} else {
			gnome_client_set_restart_style
				(client, GNOME_RESTART_NEVER);
		}
	}
#endif
}

#if 0

/* capplet_init -- see documentation in capplet-util.h
 */

void
capplet_init (int                      argc,
	      char                   **argv,
	      ApplySettingsFn          apply_fn,
	      CreateDialogFn           create_dialog_fn,
	      SetupPropertyEditorsFn   setup_fn,
	      GetLegacySettingsFn      get_legacy_fn) 
{
	gchar                         *factory_iid;
	BonoboGenericFactory          *factory;

	static gboolean apply_only;
	static gboolean get_legacy;
	static struct poptOption cap_options[] = {
		{ "apply", '\0', POPT_ARG_NONE, &apply_only, 0,
		  N_("Just apply settings and quit"), NULL },
		{ "init-session-settings", '\0', POPT_ARG_NONE, &apply_only, 0,
		  N_("Just apply settings and quit"), NULL },
		{ "get-legacy", '\0', POPT_ARG_NONE, &get_legacy, 0,
		  N_("Retrieve and store legacy settings"), NULL },
		{ NULL, '\0', 0, NULL, 0, NULL, NULL }
	};

	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	gnome_program_init (argv[0], VERSION, LIBGNOMEUI_MODULE, argc, argv,
			    GNOME_PARAM_POPT_TABLE, cap_options,
			    NULL);

	if (!bonobo_init (&argc, argv))
		g_error ("Cannot initialize bonobo");

	if (apply_only && apply_fn != NULL) {
		setup_session_mgmt (argv[0]);
		apply_fn ();
	}
	else if (get_legacy && get_legacy_fn != NULL) {
		setup_session_mgmt (argv[0]);
		get_legacy_fn ();
	} else {
		setup_session_mgmt (argv[0]);

		create_dialog_cb = create_dialog_fn;
		apply_settings_cb = apply_fn;
		setup_property_editors_cb = setup_fn;

		factory_iid = get_factory_name (argv[0]);
		factory = bonobo_generic_factory_new
			(factory_iid, (BonoboFactoryCallback) create_control_cb, NULL);
		g_free (factory_iid);
		bonobo_running_context_auto_exit_unref (BONOBO_OBJECT (factory));

		changeset = gconf_change_set_new ();

		bonobo_main ();

		gconf_change_set_unref (changeset);
	}
}

#endif


/**
 * capplet_error_dialog :
 *
 * @parent :
 * @msg : already translated.
 * @err :
 *
 */
void
capplet_error_dialog (GtkWindow *parent, char const *msg, GError *err)
{
	if (err != NULL) {
		GtkWidget *dialog;

		dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
			GTK_DIALOG_DESTROY_WITH_PARENT,
			GTK_MESSAGE_ERROR,
			GTK_BUTTONS_CLOSE,
			msg, err->message);

		g_signal_connect (G_OBJECT (dialog),
			"response",
			G_CALLBACK (gtk_widget_destroy), NULL);
		gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
		gtk_widget_show (dialog);
		g_error_free (err);
	}
}

/**
 * capplet_help :
 * @parent :
 * @helpfile :
 * @section  :
 *
 * A quick utility routine to display help for capplets, and handle errors in a
 * Havoc happy way.
 **/
void
capplet_help (GtkWindow *parent, char const *helpfile, char const *section)
{
	GError *error = NULL;

	g_return_if_fail (helpfile != NULL);
	g_return_if_fail (section != NULL);

	gnome_help_display_desktop (NULL,
		"user-guide",
		helpfile, section, &error);
	if (error != NULL)
		capplet_error_dialog (parent, 
			_("There was an error displaying help: %s"),
			error);
}

/**
 * capplet_set_icon :
 * @window :
 * @file_name  :
 *
 * A quick utility routine to avoid the cut-n-paste of bogus code
 * that caused several bugs.
 **/
void
capplet_set_icon (GtkWidget *window, char const *icon_file_name)
{
	char *path;
	char *tmp;
	char *p;
	GdkPixbuf *icon_pixbuf = NULL;
	GtkIconTheme *icon_theme;
	GtkIconInfo  *icon_info;

	/* First look up from the icon theme */
	icon_theme = gtk_icon_theme_get_default ();

	tmp = g_strdup (icon_file_name);
	p = strrchr (tmp, '.');
	if (p)
		p[0] = '\0';

	icon_info = gtk_icon_theme_lookup_icon (icon_theme, tmp, 48, 0);

	if (icon_info != NULL) {
		icon_pixbuf = gtk_icon_info_load_icon (icon_info, NULL);
		gtk_icon_info_free (icon_info);
	}

	g_free (tmp);

	if (icon_pixbuf == NULL) {
		/* Then we fallback to the control center icon location */
		path = g_strconcat (GNOMECC_DATA_DIR "/icons/", icon_file_name, NULL);

		icon_pixbuf = gdk_pixbuf_new_from_file (path, NULL);
		g_free (path);
	}

	if (icon_pixbuf == NULL) {
		/* Then we fallback to the gnome program discovery stuff */
		path = gnome_pixmap_file (icon_file_name);
		if (path != NULL) {
			icon_pixbuf = gdk_pixbuf_new_from_file (path, NULL);
			g_free (path);
		}
	}

	if (icon_pixbuf != NULL) {
		/* Make sure that every window gets an icon */
		gtk_window_set_default_icon (icon_pixbuf);

		gtk_window_set_icon (GTK_WINDOW (window), icon_pixbuf);

		g_object_unref (icon_pixbuf);
	}
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us