summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-user-level-manager.c
blob: c8eaed62b3d305a29f7b16b6d3818fd998dc5c1c (plain)
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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-user-level-manager.c - User level manager class.
 
   Copyright (C) 2000 Eazel, Inc.
  
   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.
  
   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.
  
   Author: Ramiro Estrugo <ramiro@eazel.com>
*/

#include <config.h>
#include "nautilus-user-level-manager.h"
#include "nautilus-gtk-macros.h"
#include "nautilus-glib-extensions.h"

#include "nautilus-preferences.h"
#include "nautilus-preferences-private.h"

#include <libgnome/gnome-i18n.h>
#include <gtk/gtksignal.h>

#include <gconf/gconf.h>
#include <gconf/gconf-client.h>

#define NAUTILUS_TYPE_USER_LEVEL_MANAGER            (nautilus_user_level_manager_get_type ())
#define NAUTILUS_USER_LEVEL_MANAGER(obj)            (GTK_CHECK_CAST ((obj), NAUTILUS_TYPE_USER_LEVEL_MANAGER, NautilusUserLevelManager))
#define NAUTILUS_USER_LEVEL_MANAGER_CLASS(klass)    (GTK_CHECK_CLASS_CAST ((klass), NAUTILUS_TYPE_USER_LEVEL_MANAGER, NautilusUserLevelManagerClass))
#define NAUTILUS_IS_USER_LEVEL_MANAGER(obj)         (GTK_CHECK_TYPE ((obj), NAUTILUS_TYPE_USER_LEVEL_MANAGER))
#define NAUTILUS_IS_USER_LEVEL_MANAGER_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), NAUTILUS_TYPE_USER_LEVEL_MANAGER))

/* FIXME bugzilla.eazel.com 2806: 
 * These names are used both by gconf paths and for user display.
 * We need to maintain both separately, so the user-display ones can
 * be capitalized and localized.
 */
static const char *DEFAULT_USER_LEVEL_NAMES_FOR_STORAGE[] =
{
	"novice",
	"intermediate",
	"hacker"
};

static const char *DEFAULT_USER_LEVEL_NAMES_FOR_DISPLAY[] =
{
	N_("Beginner"),
	N_("Intermediate"),
	N_("Advanced")
};

static const guint   DEFAULT_NUM_USER_LEVELS = NAUTILUS_N_ELEMENTS (DEFAULT_USER_LEVEL_NAMES_FOR_STORAGE);
static const guint   DEFAULT_USER_LEVEL = NAUTILUS_USER_LEVEL_HACKER;

static const char    USER_LEVEL_KEY[] = "/apps/nautilus/user_level";
static const char    USER_LEVEL_PATH[] = "/apps/nautilus";

static NautilusUserLevelManager *global_manager = NULL;

struct _NautilusUserLevelManager 
{
	GtkObject		object;

        guint			num_user_levels;
	NautilusStringList	*user_level_names_for_storage;
	NautilusStringList	*user_level_names_for_display;

	GConfClient		*gconf_client;
	int			user_level_changed_connection;
};

typedef struct 
{
	GtkObjectClass	parent_class;
} NautilusUserLevelManagerClass;

enum
{
	USER_LEVEL_CHANGED,
	LAST_SIGNAL
};

static guint user_level_manager_signals[LAST_SIGNAL];

static GtkType                   nautilus_user_level_manager_get_type         (void);
static void                      nautilus_user_level_manager_initialize_class (NautilusUserLevelManagerClass *user_level_manager_class);
static void                      nautilus_user_level_manager_initialize       (NautilusUserLevelManager      *user_level_manager);
static void                      user_level_manager_destroy                   (GtkObject                     *object);
static char			*user_level_manager_get_user_level_as_string  (void);
static NautilusUserLevelManager *user_level_manager_new                       (void);
static void                      user_level_manager_ensure_global_manager     (void);
static void                      user_level_set_default_if_needed             (NautilusUserLevelManager      *manager);


/* Gconf callbacks */
static void                      gconf_user_level_changed_callback            (GConfClient                   *client,
									       guint                          cnxn_id,
									       const gchar                   *key,
									       GConfValue                    *value,
									       gboolean                       is_default,
									       gpointer                       user_data);


NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusUserLevelManager, nautilus_user_level_manager, GTK_TYPE_OBJECT)

/* Create the icon factory. */
static NautilusUserLevelManager *
user_level_manager_new (void)
{
	GConfError	  	 *error = NULL;
        NautilusUserLevelManager *manager;
        guint			 i;

	if (!gconf_is_initialized ()) {
		char		  *argv[] = { "nautilus", NULL };
		
		if (!gconf_init (1, argv, &error)) {
			g_assert (error != NULL);

			nautilus_preferences_handle_error (&error);
			
			return NULL;
		}
	}

        manager = NAUTILUS_USER_LEVEL_MANAGER (gtk_object_new (nautilus_user_level_manager_get_type (), NULL));
	gtk_object_ref (GTK_OBJECT (manager));
	gtk_object_sink (GTK_OBJECT (manager));

	manager->gconf_client = gconf_client_get_default ();

	/* Let gconf know about ~/.gconf/apps/nautilus */
	gconf_client_add_dir (manager->gconf_client,
			      USER_LEVEL_PATH,
			      GCONF_CLIENT_PRELOAD_RECURSIVE,
			      &error);
	nautilus_preferences_handle_error (&error);
	
	manager->num_user_levels = DEFAULT_NUM_USER_LEVELS;
	manager->user_level_names_for_storage = nautilus_string_list_new (TRUE);
	manager->user_level_names_for_display = nautilus_string_list_new (TRUE);

	for (i = 0; i < DEFAULT_NUM_USER_LEVELS; i++) {
		nautilus_string_list_insert (manager->user_level_names_for_storage, DEFAULT_USER_LEVEL_NAMES_FOR_STORAGE[i]);
		nautilus_string_list_insert (manager->user_level_names_for_display, _(DEFAULT_USER_LEVEL_NAMES_FOR_DISPLAY[i]));
	}
	
	g_assert (manager->gconf_client != NULL);

	user_level_set_default_if_needed (manager);

	/* Add a gconf notification for user_level changes.  */
	manager->user_level_changed_connection = gconf_client_notify_add (manager->gconf_client,
									  USER_LEVEL_KEY,
									  gconf_user_level_changed_callback,
									  NULL,
									  NULL,
									  &error);
	nautilus_preferences_handle_error (&error);
	
        return manager;
}

static void
nautilus_user_level_manager_initialize (NautilusUserLevelManager *manager)
{
	manager->num_user_levels = 0;

	manager->gconf_client = NULL;
	manager->user_level_changed_connection = 0;
}

static void
nautilus_user_level_manager_initialize_class (NautilusUserLevelManagerClass *user_level_manager_class)
{
	GtkObjectClass *object_class = GTK_OBJECT_CLASS (user_level_manager_class);
	
	user_level_manager_signals[USER_LEVEL_CHANGED] = gtk_signal_new ("user_level_changed",
									 GTK_RUN_LAST,
									 object_class->type,
									 0,
									 gtk_marshal_NONE__NONE,
									 GTK_TYPE_NONE,
									 0);
	
	gtk_object_class_add_signals (object_class, user_level_manager_signals, LAST_SIGNAL);

	/* GtkObjectClass */
	object_class->destroy = user_level_manager_destroy;
}

static void
user_level_manager_destroy (GtkObject *object)
{
	NautilusUserLevelManager *manager;
	
	manager = NAUTILUS_USER_LEVEL_MANAGER (object);

	/* Remove the gconf notification if its still lingering */
	if (manager->user_level_changed_connection != 0) {
		gconf_client_notify_remove (manager->gconf_client,
					    manager->user_level_changed_connection);
		
		manager->user_level_changed_connection = 0;
	}

	if (manager->gconf_client != NULL) {
		gtk_object_unref (GTK_OBJECT (manager->gconf_client));
	}

	nautilus_string_list_free (manager->user_level_names_for_storage);
	nautilus_string_list_free (manager->user_level_names_for_display);
}

static void
unref_user_level_manager (void)
{
	gtk_object_unref (GTK_OBJECT (global_manager));
}

static void
user_level_manager_ensure_global_manager (void)
{
        if (global_manager == NULL) {
                global_manager = user_level_manager_new ();
		g_atexit (unref_user_level_manager);
        }

	g_assert (global_manager != NULL);
}

static void
user_level_set_default_if_needed (NautilusUserLevelManager *manager)
{
	GConfError *error = NULL;
	GConfValue *value;

	g_assert (manager != NULL);
	g_assert (manager->gconf_client != NULL);
	
	value = gconf_client_get_without_default (manager->gconf_client, USER_LEVEL_KEY, &error);
	if (nautilus_preferences_handle_error (&error)) {
		if (value != NULL) {
			gconf_value_destroy (value);
			value = NULL;
		}
	}
	
	if (!value) {
		value = gconf_value_new (GCONF_VALUE_STRING);
		
		gconf_value_set_string (value, DEFAULT_USER_LEVEL_NAMES_FOR_STORAGE[DEFAULT_USER_LEVEL]);
		
		gconf_client_set (manager->gconf_client, USER_LEVEL_KEY, value, &error);
		nautilus_preferences_handle_error (&error);

		gconf_client_suggest_sync (manager->gconf_client, &error);
		nautilus_preferences_handle_error (&error);
	}

	g_assert (value != NULL);

	gconf_value_destroy (value);
}

static void
gconf_user_level_changed_callback (GConfClient	*client, 
				   guint	connection_id, 
				   const gchar	*key, 
				   GConfValue	*value, 
				   gboolean	is_default, 
				   gpointer	user_data)
{
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();

	gtk_signal_emit (GTK_OBJECT (manager), user_level_manager_signals[USER_LEVEL_CHANGED]);
}

/* Public NautilusUserLevelManager functions */
NautilusUserLevelManager *
nautilus_user_level_manager_get (void)
{
	user_level_manager_ensure_global_manager ();

	g_assert (global_manager != NULL);
	g_assert (NAUTILUS_IS_USER_LEVEL_MANAGER (global_manager));
	
        return global_manager;
}

void
nautilus_user_level_manager_set_user_level (guint user_level)
{
	GConfError		 *error = NULL;
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();
	char			 *user_level_string;
	guint			 old_user_level;

	g_return_if_fail (user_level < manager->num_user_levels);
	g_return_if_fail (user_level < nautilus_string_list_get_length (manager->user_level_names_for_storage));

	old_user_level = nautilus_user_level_manager_get_user_level ();

	if (old_user_level == user_level) {
		return;
	}

	user_level_string = nautilus_string_list_nth (manager->user_level_names_for_storage, user_level);
	
	g_assert (user_level_string != NULL);

	gconf_client_set_string (manager->gconf_client,
				 USER_LEVEL_KEY,
				 user_level_string,
				 &error);
	nautilus_preferences_handle_error (&error);

	gconf_client_suggest_sync (manager->gconf_client, &error);
	nautilus_preferences_handle_error (&error);
}

guint
nautilus_user_level_manager_get_user_level (void)
{
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();
	char			 *user_level_string;
	gint			 index;

	user_level_string = user_level_manager_get_user_level_as_string ();

	/* The user_level_string is guranteed to be ok now.  The above function, will
	 * make sure of that and deal with gconf problems too.
	 */
	g_assert (user_level_string != NULL);

	index = nautilus_string_list_get_index_for_string (manager->user_level_names_for_storage,
							   user_level_string);

	g_free (user_level_string);

	g_assert (index != NAUTILUS_STRING_LIST_NOT_FOUND);

	return (guint) index;
}

guint
nautilus_user_level_manager_get_num_user_levels (void)
{
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();

	return manager->num_user_levels;
}

char *
nautilus_user_level_manager_get_user_level_name_for_display (guint user_level)
{
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();

	return nautilus_string_list_nth (manager->user_level_names_for_display, user_level);
}

char *
nautilus_user_level_manager_make_gconf_key (const char *preference_name,
					    guint user_level)
{
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();

	char *key;
	char *user_level_string;

	g_return_val_if_fail (preference_name != NULL, NULL);
	g_return_val_if_fail (user_level < manager->num_user_levels, NULL);
	g_return_val_if_fail (user_level < nautilus_string_list_get_length (manager->user_level_names_for_storage), NULL);
	
	user_level_string = nautilus_string_list_nth (manager->user_level_names_for_storage, user_level);
	g_assert (user_level_string != NULL);

	key = g_strdup_printf ("%s/%s/%s",
			       USER_LEVEL_PATH,
			       user_level_string,
			       preference_name);

	g_free (user_level_string);

	return key;
}

char *
nautilus_user_level_manager_make_current_gconf_key (const char *preference_name)
{
	return nautilus_user_level_manager_make_gconf_key (preference_name,
							   nautilus_user_level_manager_get_user_level ());
}

static char *
user_level_manager_get_user_level_as_string (void)
{
	GConfError		 *error = NULL;
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();
	char			 *user_level_string = NULL;

	g_assert (manager->gconf_client != NULL);
	
	user_level_string = gconf_client_get_string (manager->gconf_client, USER_LEVEL_KEY, &error);
	if (nautilus_preferences_handle_error (&error)) {
		g_free (user_level_string);
		user_level_string = NULL;
	}

	if (!user_level_string)
		user_level_string = g_strdup (DEFAULT_USER_LEVEL_NAMES_FOR_STORAGE[0]);

	return user_level_string;
}


/**
 * nautilus_user_level_manager_set_default_value_if_needed
 *
 * This function will ask gconf for a value. If
 * 
 * The value is not found in the user's database:
 *   It will be added to the database using the given default value.  
 *
 * The value is found in the user's database:
 *   Nothing.
 *
 * @name: The name of the preference.
 * @type: The type of preference.
 * @default_value: The default_value to use.
 **/
void
nautilus_user_level_manager_set_default_value_if_needed (const char		*preference_name,
							 NautilusPreferenceType       type,
							 guint			user_level,
							 gconstpointer		default_value)
{
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();
	GConfError      *error = NULL;
	GConfValue	*value = NULL;
	char		*key;

	g_return_if_fail (preference_name != NULL);

	key = nautilus_user_level_manager_make_gconf_key (preference_name, user_level);
	g_assert (key != NULL);

	/* Find out if the preference exists at all */
	value = gconf_client_get_without_default (manager->gconf_client, key, &error);
	if (nautilus_preferences_handle_error (&error)) {
		if (value != NULL) {
			gconf_value_destroy (value);
			value = NULL;
		}
	}
	
	/* The value does not exist, so create one */
	if (!value) {
		switch (type)
		{
		case NAUTILUS_PREFERENCE_STRING: 
			/* Gconf will not grok NULL strings, so for this case dont do it. */
			if (default_value) {
				value = gconf_value_new (GCONF_VALUE_STRING);
				gconf_value_set_string (value, (const char *) default_value);
			}
			break;
		case NAUTILUS_PREFERENCE_BOOLEAN:
			value = gconf_value_new (GCONF_VALUE_BOOL);
			gconf_value_set_bool (value, GPOINTER_TO_INT (default_value));
			break;
		case NAUTILUS_PREFERENCE_ENUM:
			value = gconf_value_new (GCONF_VALUE_INT);
			gconf_value_set_int (value, GPOINTER_TO_INT (default_value));
			break;
		}
		
		if (value) {
			gconf_client_set (manager->gconf_client, key, value, &error);
			nautilus_preferences_handle_error (&error);
		}
	}
	
	if (value) {
		gconf_value_destroy (value);
	}

	g_free (key);
}

gboolean
nautilus_user_level_manager_compare_preference_between_user_levels (const char *preference_name,
								    guint	 user_level_a,
								    guint	 user_level_b)
{
	NautilusUserLevelManager *manager = nautilus_user_level_manager_get ();
	gboolean	result = FALSE;
	char		*key_a;
	char		*key_b;
	GConfValue	*value_a;
	GConfValue	*value_b;
	GConfError	*error = NULL;

	g_return_val_if_fail (preference_name != NULL, FALSE);

	key_a = nautilus_user_level_manager_make_gconf_key (preference_name, user_level_a);
	g_assert (key_a != NULL);

	key_b = nautilus_user_level_manager_make_gconf_key (preference_name, user_level_b);
	g_assert (key_b != NULL);

	value_a = gconf_client_get (manager->gconf_client, key_a, &error);
	if (nautilus_preferences_handle_error (&error)) {
		if (value_a != NULL) {
			gconf_value_destroy (value_a);
			value_a = NULL;
		}
	}
	value_b = gconf_client_get (manager->gconf_client, key_b, &error);
	if (nautilus_preferences_handle_error (&error)) {
		if (value_b != NULL) {
			gconf_value_destroy (value_b);
			value_b = NULL;
		}
	}

	g_free (key_a);
	g_free (key_b);

	if (value_a && value_b)
	{
		g_assert (value_a->type == value_b->type);
		
		switch (value_a->type)
		{
		case GCONF_VALUE_STRING:
			result = (gconf_value_string (value_a)
				  && gconf_value_string (value_b)
				  && (strcmp (gconf_value_string (value_a), gconf_value_string (value_b)) == 0));
			break;
			
		case GCONF_VALUE_INT:
			result = (gconf_value_int (value_a) == gconf_value_int (value_b));
			break;
			
		case GCONF_VALUE_BOOL:
			result = (gconf_value_bool (value_a) == gconf_value_bool (value_b));
			break;

		default:
			g_assert_not_reached ();
		}
	}
	else
	{
		result = TRUE;
	}

	if (value_a) {
		gconf_value_destroy (value_a);
	}

	if (value_b) {
		gconf_value_destroy (value_b);
	}

	return result;
}