summaryrefslogtreecommitdiff
path: root/libcaja-private/caja-global-preferences.c
blob: 1111e9d3e48ddcbf01ce8dbb3b9d74fedf3b8a27 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* caja-global-preferences.c - Caja specific preference keys and
                                   functions.

   Copyright (C) 1999, 2000, 2001 Eazel, Inc.

   The Mate Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Mate Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Mate Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
   Boston, MA 02110-1301, USA.

   Authors: Ramiro Estrugo <ramiro@eazel.com>
*/

#include <config.h>
#include "caja-global-preferences.h"

#include "caja-file-utilities.h"
#include "caja-file.h"
#include <eel/eel-enumeration.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gtk-extensions.h>
#include <eel/eel-stock-dialogs.h>
#include <eel/eel-string.h>
#include <glib/gi18n.h>

/* Constants */
#define STRING_ARRAY_DEFAULT_TOKENS_DELIMETER ","
#define PREFERENCES_SORT_ORDER_MANUALLY 100

/* Path for mate-vfs preferences */
static const char *EXTRA_MONITOR_PATHS[] = { "/desktop/mate/file_views",
        "/desktop/mate/background",
        "/desktop/mate/lockdown",
        NULL
                                           };

/* Forward declarations */
static void     global_preferences_install_defaults      (void);
static void     global_preferences_register_enumerations (void);
static gpointer default_home_link_name                   (void);
static gpointer default_computer_link_name               (void);
static gpointer default_trash_link_name                  (void);
static gpointer default_network_link_name                (void);


/* An enumeration used for installing type specific preferences defaults. */
typedef enum
{
    PREFERENCE_BOOLEAN = 1,
    PREFERENCE_INTEGER,
    PREFERENCE_STRING,
    PREFERENCE_STRING_ARRAY
} PreferenceType;

static EelEnumerationEntry default_zoom_level_enum_entries[] =
{
    /* xgettext:no-c-format */
    { "smallest",	    N_("25%"),		CAJA_ZOOM_LEVEL_SMALLEST },
    /* xgettext:no-c-format */
    { "smaller",	    N_("50%"),		CAJA_ZOOM_LEVEL_SMALLER },
    /* xgettext:no-c-format */
    { "small",	    N_("75%"),		CAJA_ZOOM_LEVEL_SMALL },
    /* xgettext:no-c-format */
    { "standard",	    N_("100%"),		CAJA_ZOOM_LEVEL_STANDARD },
    /* xgettext:no-c-format */
    { "large",	    N_("150%"),		CAJA_ZOOM_LEVEL_LARGE },
    /* xgettext:no-c-format */
    { "larger",	    N_("200%"),		CAJA_ZOOM_LEVEL_LARGER },
    /* xgettext:no-c-format */
    { "largest",	    N_("400%"),		CAJA_ZOOM_LEVEL_LARGEST }
};

static EelEnumerationEntry file_size_enum_entries[] =
{
    { "102400",	    N_("100 K"),	102400 },
    { "512000",	    N_("500 K"),	512000 },
    { "1048576",	    N_("1 MB"),		1048576 },
    { "3145728",	    N_("3 MB"),		3145728 },
    { "5242880",	    N_("5 MB"),		5242880 },
    { "10485760",	    N_("10 MB"),	10485760 },
    { "104857600",	    N_("100 MB"),	104857600 },
    { "1073741824",     N_("1 GB"),         1073741824 },
    { "2147483648",     N_("2 GB"),         2147483648U },
    { "4294967295",     N_("4 GB"),         4294967295U }
};

static EelEnumerationEntry default_icon_view_sort_order_enum_entries[] =
{
    { "manually",	       N_("Manually"),		    PREFERENCES_SORT_ORDER_MANUALLY },
    { "--------",             "--------" },
    { "name",	       N_("By Name"),		    CAJA_FILE_SORT_BY_DISPLAY_NAME },
    { "size",	       N_("By Size"),		    CAJA_FILE_SORT_BY_SIZE },
    { "type",	       N_("By Type"),		    CAJA_FILE_SORT_BY_TYPE },
    { "modification_date", N_("By Modification Date"),  CAJA_FILE_SORT_BY_MTIME },
    { "emblems",	       N_("By Emblems"),	    CAJA_FILE_SORT_BY_EMBLEMS }
};

static EelEnumerationEntry standard_font_size_entries[] =
{
    { "8",		   N_("8"),	8 },
    { "10",		   N_("10"),	10 },
    { "12",		   N_("12"),	12 },
    { "14",		   N_("14"),	14 },
    { "16",		   N_("16"),	16 },
    { "18",		   N_("18"),	18 },
    { "20",		   N_("20"),	20 },
    { "22",		   N_("22"),	22 },
    { "24",		   N_("24"),	24 }
};

/*
 * A callback which can be used to fetch dynamic fallback values.
 * For example, values that are dependent on the environment (such as user name)
 * cannot be specified as constants.
 */
typedef gpointer (*PreferencesDefaultValueCallback) (void);

/* A structure that describes a single preference including defaults and visibility. */
typedef struct
{
    const char *name;
    PreferenceType type;
    const gpointer fallback_value;
    PreferencesDefaultValueCallback fallback_callback;
    GFreeFunc fallback_callback_result_free_function;
    const char *enumeration_id;
} PreferenceDefault;

/* The following table defines the default values and user level visibilities of
 * Caja preferences.  Each of these preferences does not necessarily need to
 * have a UI item in the preferences dialog.  To add an item to the preferences
 * dialog, see the CajaPreferencesItemDescription tables later in this file.
 *
 * Field definitions:
 *
 * 1. name
 *
 *    The name of the preference.  Usually defined in
 *    caja-global-preferences.h
 *
 * 2. type
 *    The preference type.  One of:
 *
 *	PREFERENCE_BOOLEAN
 *	PREFERENCE_INTEGER
 *	PREFERENCE_STRING
 *	PREFERENCE_STRING_ARRAY
 *
 * 3. fallback_value
 *    Emergency fallback value if our mateconf schemas are hosed somehow.
 *
 * 4. fallback_callback
 *    callback to get dynamic fallback
 *
 * 5. fallback_callback_result_free_function
 *    free result of fallback_callback
 *
 * 6. enumeration_id
 *    An an enumeration id is a unique string that identifies an enumeration.
 *    If given, an enumeration id can be used to qualify a INTEGER preference.
 *    The preferences dialog widgetry will use this enumeration id to find out
 *    what choices and descriptions of choices to present to the user.
 */

/* NOTE THAT THE FALLBACKS HERE ARE NOT SUPPOSED TO BE USED -
 * YOU SHOULD EDIT THE SCHEMAS FILE TO CHANGE DEFAULTS.
 */
static const PreferenceDefault preference_defaults[] =
{
    {
        CAJA_PREFERENCES_TREE_SHOW_ONLY_DIRECTORIES,
        PREFERENCE_BOOLEAN,
        GINT_TO_POINTER (TRUE)
    },

    /* Compact Icon View Default Preferences */
    {
        CAJA_PREFERENCES_COMPACT_VIEW_DEFAULT_ZOOM_LEVEL,
        PREFERENCE_STRING,
        "standard",
        NULL, NULL,
        "default_zoom_level"
    },

    /* List View Default Preferences */
    {
        CAJA_PREFERENCES_LIST_VIEW_DEFAULT_SORT_ORDER,
        PREFERENCE_STRING,
        "name",
        NULL, NULL,
        NULL,
    },
    {
        CAJA_PREFERENCES_LIST_VIEW_DEFAULT_SORT_IN_REVERSE_ORDER,
        PREFERENCE_BOOLEAN,
        GINT_TO_POINTER (FALSE)
    },
    {
        CAJA_PREFERENCES_LIST_VIEW_DEFAULT_ZOOM_LEVEL,
        PREFERENCE_STRING,
        "smaller",
        NULL, NULL,
        "default_zoom_level"
    },

    /* Desktop Preferences */
    {
        CAJA_PREFERENCES_DESKTOP_HOME_VISIBLE,
        PREFERENCE_BOOLEAN,
        GINT_TO_POINTER (FALSE)
    },

    {
        CAJA_PREFERENCES_DESKTOP_HOME_NAME,
        PREFERENCE_STRING,
        NULL,
        default_home_link_name, g_free,
    },

    {
        CAJA_PREFERENCES_DESKTOP_COMPUTER_VISIBLE,
        PREFERENCE_BOOLEAN,
        GINT_TO_POINTER (TRUE)
    },

    {
        CAJA_PREFERENCES_DESKTOP_COMPUTER_NAME,
        PREFERENCE_STRING,
        NULL,
        default_computer_link_name, g_free,
    },

    {
        CAJA_PREFERENCES_DESKTOP_TRASH_VISIBLE,
        PREFERENCE_BOOLEAN,
        GINT_TO_POINTER (TRUE)
    },

    {
        CAJA_PREFERENCES_DESKTOP_TRASH_NAME,
        PREFERENCE_STRING,
        NULL,
        default_trash_link_name, g_free,
    },

    {
        CAJA_PREFERENCES_DESKTOP_VOLUMES_VISIBLE,
        PREFERENCE_BOOLEAN,
        GINT_TO_POINTER (TRUE)
    },

    {
        CAJA_PREFERENCES_DESKTOP_NETWORK_VISIBLE,
        PREFERENCE_BOOLEAN,
        GINT_TO_POINTER (FALSE)
    },

    {
        CAJA_PREFERENCES_DESKTOP_NETWORK_NAME,
        PREFERENCE_STRING,
        NULL,
        default_network_link_name, g_free,
    },

    {
        CAJA_PREFERENCES_DESKTOP_TEXT_ELLIPSIS_LIMIT,
        PREFERENCE_INTEGER,
        GINT_TO_POINTER (3)
    },
    {
        CAJA_PREFERENCES_LOCKDOWN_COMMAND_LINE,
        PREFERENCE_BOOLEAN,
        GINT_TO_POINTER (FALSE)
    },
    { NULL }
};

static gpointer
default_home_link_name (void)
{
    /* Note to translators: If it's hard to compose a good home
     * icon name from the user name, you can use a string without
     * an "%s" here, in which case the home icon name will not
     * include the user's name, which should be fine. To avoid a
     * warning, put "%.0s" somewhere in the string, which will
     * match the user name string passed by the C code, but not
     * put the user name in the final string.
     */
    return g_strdup_printf (_("%s's Home"), g_get_user_name ());
}

static gpointer
default_computer_link_name (void)
{
    return g_strdup (_("Computer"));
}

static gpointer
default_trash_link_name (void)
{
    return g_strdup (_("Trash"));
}

static gpointer
default_network_link_name (void)
{
    return g_strdup (_("Network Servers"));
}



/**
 * global_preferences_register_enumerations
 *
 * Register enumerations for INTEGER preferences that need them.
 *
 * This function needs to be called before the preferences dialog
 * panes are populated, as they use the registered information to
 * create enumeration item widgets.
 */
static void
global_preferences_register_enumerations (void)
{
    guint i;

    /* Register the enumerations.
     * These enumerations are used in the preferences dialog to
     * populate widgets and route preferences changes between the
     * storage (MateConf) and the displayed values.
     */
    eel_enumeration_register ("default_icon_view_sort_order",
                              default_icon_view_sort_order_enum_entries,
                              G_N_ELEMENTS (default_icon_view_sort_order_enum_entries));
    eel_enumeration_register ("default_zoom_level",
                              default_zoom_level_enum_entries,
                              G_N_ELEMENTS (default_zoom_level_enum_entries));
    eel_enumeration_register ("file_size",
                              file_size_enum_entries,
                              G_N_ELEMENTS (file_size_enum_entries));
    eel_enumeration_register ("standard_font_size",
                              standard_font_size_entries,
                              G_N_ELEMENTS (standard_font_size_entries));

    /* Set the enumeration ids for preferences that need them */
    for (i = 0; preference_defaults[i].name != NULL; i++)
    {
        if (eel_strlen (preference_defaults[i].enumeration_id) > 0)
        {
            g_assert (preference_defaults[i].type == PREFERENCE_STRING
                      || preference_defaults[i].type == PREFERENCE_STRING_ARRAY
                      || preference_defaults[i].type == PREFERENCE_INTEGER);
            eel_preferences_set_enumeration_id (preference_defaults[i].name,
                                                preference_defaults[i].enumeration_id);
        }
    }
}

static void
global_preferences_install_one_default (const char *preference_name,
                                        PreferenceType preference_type,
                                        const PreferenceDefault *preference_default)
{
    gpointer value = NULL;
    char **string_array_value;

    g_return_if_fail (preference_name != NULL);
    g_return_if_fail (preference_type >= PREFERENCE_BOOLEAN);
    g_return_if_fail (preference_type <= PREFERENCE_STRING_ARRAY);
    g_return_if_fail (preference_default != NULL);

    /* If a callback is given, use that to fetch the default value */
    if (preference_default->fallback_callback != NULL)
    {
        value = (* preference_default->fallback_callback) ();
    }
    else
    {
        value = preference_default->fallback_value;
    }

    switch (preference_type)
    {
    case PREFERENCE_BOOLEAN:
        eel_preferences_set_emergency_fallback_boolean (preference_name,
                GPOINTER_TO_INT (value));
        break;

    case PREFERENCE_INTEGER:
        eel_preferences_set_emergency_fallback_integer (preference_name,

                GPOINTER_TO_INT (value));
        break;

    case PREFERENCE_STRING:
        eel_preferences_set_emergency_fallback_string (preference_name,
                value);
        break;

    case PREFERENCE_STRING_ARRAY:
        string_array_value = g_strsplit (value,
                                         STRING_ARRAY_DEFAULT_TOKENS_DELIMETER,
                                         -1);
        eel_preferences_set_emergency_fallback_string_array (preference_name,
                string_array_value);
        g_strfreev (string_array_value);
        break;

    default:
        g_assert_not_reached ();
    }

    /* Free the dynamic default value if needed */
    if (preference_default->fallback_callback != NULL
            && preference_default->fallback_callback_result_free_function != NULL)
    {
        (* preference_default->fallback_callback_result_free_function) (value);
    }
}

/**
 * global_preferences_install_defaults
 *
 * Install defaults and visibilities.
 *
 * Most of the defaults come from the preference_defaults table above.
 *
 * Many preferences require their defaults to be computed, and so there
 * are special functions to install those.
 */
static void
global_preferences_install_defaults (void)
{
    guint i;

    for (i = 0; preference_defaults[i].name != NULL; i++)
    {
        global_preferences_install_one_default (preference_defaults[i].name,
                                                preference_defaults[i].type,
                                                &preference_defaults[i]);
    }
}

/*
 * Public functions
 */
char *
caja_global_preferences_get_default_folder_viewer_preference_as_iid (void)
{
    int preference_value;
    const char *viewer_iid;

    preference_value =
        g_settings_get_enum (caja_preferences, CAJA_PREFERENCES_DEFAULT_FOLDER_VIEWER);

    if (preference_value == CAJA_DEFAULT_FOLDER_VIEWER_LIST_VIEW)
    {
        viewer_iid = CAJA_LIST_VIEW_IID;
    }
    else if (preference_value == CAJA_DEFAULT_FOLDER_VIEWER_COMPACT_VIEW)
    {
        viewer_iid = CAJA_COMPACT_VIEW_IID;
    }
    else
    {
        viewer_iid = CAJA_ICON_VIEW_IID;
    }

    return g_strdup (viewer_iid);
}

/* The icon view uses 2 variables to store the sort order and
 * whether to use manual layout.  However, the UI for these
 * preferences presensts them as single option menu.  So we
 * use the following preference as a proxy for the other two.
 * In caja-global-preferences.c we install callbacks for
 * the proxy preference and update the other 2 when it changes
 */
static void
default_icon_view_sort_order_or_manual_layout_changed_callback (gpointer callback_data)
{
    int default_sort_order_or_manual_layout;
    int default_sort_order;

    default_sort_order_or_manual_layout =
        g_settings_get_enum (caja_icon_view_preferences,
                             CAJA_PREFERENCES_ICON_VIEW_DEFAULT_SORT_ORDER_OR_MANUAL_LAYOUT);

    eel_preferences_set_boolean (CAJA_PREFERENCES_ICON_VIEW_DEFAULT_USE_MANUAL_LAYOUT,
                                 default_sort_order_or_manual_layout == PREFERENCES_SORT_ORDER_MANUALLY);

    if (default_sort_order_or_manual_layout != PREFERENCES_SORT_ORDER_MANUALLY)
    {
        default_sort_order = default_sort_order_or_manual_layout;

        g_return_if_fail (default_sort_order >= CAJA_FILE_SORT_BY_DISPLAY_NAME);
        g_return_if_fail (default_sort_order <= CAJA_FILE_SORT_BY_EMBLEMS);

        g_settings_set_enum (caja_icon_view_preferences,
                             CAJA_PREFERENCES_ICON_VIEW_DEFAULT_SORT_ORDER,
                             default_sort_order);
    }
}

void
caja_global_preferences_init (void)
{
    static gboolean initialized = FALSE;
    int i;

    if (initialized)
    {
        return;
    }

    initialized = TRUE;

    eel_preferences_init ("/apps/caja");

    /* Install defaults */
    global_preferences_install_defaults ();

    global_preferences_register_enumerations ();

    /* Add monitors for any other MateConf paths we have keys in */
    for (i=0; EXTRA_MONITOR_PATHS[i] != NULL; i++)
    {
        eel_preferences_monitor_directory (EXTRA_MONITOR_PATHS[i]);
    }
    
    caja_preferences = g_settings_new("org.mate.caja.preferences");
    caja_media_preferences = g_settings_new("org.mate.media-handling");
    caja_window_state = g_settings_new("org.mate.caja.window-state");
    caja_icon_view_preferences = g_settings_new("org.mate.caja.icon-view");

    /* Set up storage for values accessed in this file */
    g_signal_connect_swapped (caja_icon_view_preferences,
                              "changed::" CAJA_PREFERENCES_ICON_VIEW_DEFAULT_SORT_ORDER_OR_MANUAL_LAYOUT,
                              G_CALLBACK (default_icon_view_sort_order_or_manual_layout_changed_callback), 
                              NULL);

    /* Preload everything in a big batch */
    eel_mateconf_preload_cache ("/apps/caja/preferences",
                                MATECONF_CLIENT_PRELOAD_ONELEVEL);
    eel_mateconf_preload_cache ("/desktop/mate/file_views",
                                MATECONF_CLIENT_PRELOAD_ONELEVEL);
    eel_mateconf_preload_cache ("/desktop/mate/background",
                                MATECONF_CLIENT_PRELOAD_ONELEVEL);
    eel_mateconf_preload_cache ("/desktop/mate/lockdown",
                                MATECONF_CLIENT_PRELOAD_ONELEVEL);

    /* These are always needed for the desktop */
    eel_mateconf_preload_cache ("/apps/caja/desktop",
                                MATECONF_CLIENT_PRELOAD_ONELEVEL);
    eel_mateconf_preload_cache ("/apps/caja/icon_view",
                                MATECONF_CLIENT_PRELOAD_ONELEVEL);
    eel_mateconf_preload_cache ("/apps/caja/desktop-metadata",
                                MATECONF_CLIENT_PRELOAD_RECURSIVE);
}