-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathnemo-global-preferences.c
More file actions
519 lines (414 loc) · 17.1 KB
/
nemo-global-preferences.c
File metadata and controls
519 lines (414 loc) · 17.1 KB
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
/* -*- Mode: C; indent-tabs-mode: f; c-basic-offset: 4; tab-width: 4 -*- */
/* nemo-global-preferences.c - Nemo specific preference keys and
functions.
Copyright (C) 1999, 2000, 2001 Eazel, Inc.
The Gnome 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 Gnome 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 Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 51 Franklin Street - Suite 500,
Boston, MA 02110-1335, USA.
Authors: Ramiro Estrugo <ramiro@eazel.com>
*/
#include <config.h>
#include "nemo-global-preferences.h"
#include "nemo-file-utilities.h"
#include "nemo-file.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 <eel/eel-debug.h>
#include <glib/gi18n.h>
#include <gio/gdesktopappinfo.h>
#define DEBUG_FLAG NEMO_DEBUG_PREFERENCES
#include <libnemo-private/nemo-debug.h>
GSettings *nemo_preferences;
GSettings *nemo_icon_view_preferences;
GSettings *nemo_list_view_preferences;
GSettings *nemo_compact_view_preferences;
GSettings *nemo_desktop_preferences;
GSettings *nemo_tree_sidebar_preferences;
GSettings *nemo_window_state;
GSettings *gtk_filechooser_preferences;
GSettings *nemo_plugin_preferences;
GSettings *nemo_menu_config_preferences;
GSettings *nemo_search_preferences;
GSettings *nemo_preview_pane_preferences;
GSettings *gnome_lockdown_preferences;
GSettings *gnome_background_preferences;
GSettings *gnome_media_handling_preferences;
GSettings *gnome_terminal_preferences;
GSettings *cinnamon_privacy_preferences;
GSettings *cinnamon_interface_preferences;
GSettings *gnome_interface_preferences;
GTimeZone *prefs_current_timezone;
gboolean prefs_current_24h_time_format;
NemoDateFormat prefs_current_date_format;
GTimer *nemo_startup_timer;
static gboolean ignore_view_metadata = FALSE;
static gboolean inherit_folder_view_preference = FALSE;
static gboolean inherit_show_thumbnails_preference = FALSE;
static int size_prefixes_preference = 0;
static gchar **file_roller_mimetypes = NULL;
GFileMonitor *tz_mon;
/*
* Public functions
*/
char *
nemo_global_preferences_get_default_folder_viewer_preference_as_iid (void)
{
int preference_value;
const char *viewer_iid;
preference_value =
g_settings_get_enum (nemo_preferences, NEMO_PREFERENCES_DEFAULT_FOLDER_VIEWER);
if (preference_value == NEMO_DEFAULT_FOLDER_VIEWER_LIST_VIEW) {
viewer_iid = NEMO_LIST_VIEW_IID;
} else if (preference_value == NEMO_DEFAULT_FOLDER_VIEWER_COMPACT_VIEW) {
viewer_iid = NEMO_COMPACT_VIEW_IID;
} else {
viewer_iid = NEMO_ICON_VIEW_IID;
}
return g_strdup (viewer_iid);
}
gboolean
nemo_global_preferences_get_inherit_folder_viewer_preference (void)
{
return inherit_folder_view_preference;
}
gboolean
nemo_global_preferences_get_ignore_view_metadata (void)
{
return ignore_view_metadata;
}
gboolean
nemo_global_preferences_get_inherit_show_thumbnails_preference (void)
{
return inherit_show_thumbnails_preference;
}
int
nemo_global_preferences_get_size_prefix_preference (void)
{
switch (size_prefixes_preference) {
case 0: // base-10
return G_FORMAT_SIZE_DEFAULT;
case 1: // base-10 full
return G_FORMAT_SIZE_DEFAULT | G_FORMAT_SIZE_LONG_FORMAT;
case 2: // base-2
return G_FORMAT_SIZE_DEFAULT | G_FORMAT_SIZE_IEC_UNITS;
case 3: // base-2 full
return G_FORMAT_SIZE_DEFAULT | G_FORMAT_SIZE_IEC_UNITS | G_FORMAT_SIZE_LONG_FORMAT;
}
return 0;
}
char *
nemo_global_preferences_get_desktop_iid (void)
{
gboolean use_grid;
const char *viewer_iid;
use_grid = g_settings_get_boolean (nemo_desktop_preferences, NEMO_PREFERENCES_USE_DESKTOP_GRID);
if (use_grid) {
viewer_iid = NEMO_DESKTOP_ICON_GRID_VIEW_IID;
} else {
viewer_iid = NEMO_DESKTOP_ICON_VIEW_IID;
}
return g_strdup (viewer_iid);
}
gint
nemo_global_preferences_get_tooltip_flags (void)
{
NemoFileTooltipFlags flags = NEMO_FILE_TOOLTIP_FLAGS_NONE;
if (g_settings_get_boolean (nemo_preferences, NEMO_PREFERENCES_TOOLTIP_FILE_TYPE))
flags |= NEMO_FILE_TOOLTIP_FLAGS_FILE_TYPE;
if (g_settings_get_boolean (nemo_preferences, NEMO_PREFERENCES_TOOLTIP_MOD_DATE))
flags |= NEMO_FILE_TOOLTIP_FLAGS_MOD_DATE;
if (g_settings_get_boolean (nemo_preferences, NEMO_PREFERENCES_TOOLTIP_ACCESS_DATE))
flags |= NEMO_FILE_TOOLTIP_FLAGS_ACCESS_DATE;
if (g_settings_get_boolean (nemo_preferences, NEMO_PREFERENCES_TOOLTIP_FULL_PATH))
flags |= NEMO_FILE_TOOLTIP_FLAGS_PATH;
if (g_settings_get_boolean (nemo_preferences, NEMO_PREFERENCES_TOOLTIP_CREATED_DATE))
flags |= NEMO_FILE_TOOLTIP_FLAGS_CREATED_DATE;
return flags;
}
gboolean
nemo_global_preferences_should_load_plugin (const gchar *name, const gchar *key)
{
gchar **disabled_list = g_settings_get_strv (nemo_plugin_preferences, key);
gboolean ret = TRUE;
guint i = 0;
for (i = 0; i < g_strv_length (disabled_list); i++) {
if (g_strcmp0 (disabled_list[i], name) == 0)
ret = FALSE;
}
g_strfreev (disabled_list);
return ret;
}
static void
boolean_changed_cb (GSettings *settings,
gchar *key,
gboolean *user_data)
{
*user_data = g_settings_get_boolean (settings, key);
}
static void
enum_changed_cb (GSettings *settings,
gchar *key,
int *user_data)
{
*user_data = g_settings_get_enum (settings, key);
}
static void
setup_cached_pref_keys (void)
{
g_signal_connect (nemo_preferences,
"changed::" NEMO_PREFERENCES_IGNORE_VIEW_METADATA,
G_CALLBACK (boolean_changed_cb), &ignore_view_metadata);
boolean_changed_cb (nemo_preferences, NEMO_PREFERENCES_IGNORE_VIEW_METADATA, &ignore_view_metadata);
g_signal_connect (nemo_preferences,
"changed::" NEMO_PREFERENCES_INHERIT_FOLDER_VIEWER,
G_CALLBACK (boolean_changed_cb), &inherit_folder_view_preference);
boolean_changed_cb (nemo_preferences, NEMO_PREFERENCES_INHERIT_FOLDER_VIEWER, &inherit_folder_view_preference);
g_signal_connect (nemo_preferences,
"changed::" NEMO_PREFERENCES_INHERIT_SHOW_THUMBNAILS,
G_CALLBACK (boolean_changed_cb), &inherit_show_thumbnails_preference);
boolean_changed_cb (nemo_preferences, NEMO_PREFERENCES_INHERIT_SHOW_THUMBNAILS, &inherit_show_thumbnails_preference);
g_signal_connect (nemo_preferences,
"changed::" NEMO_PREFERENCES_SIZE_PREFIXES,
G_CALLBACK (enum_changed_cb), &size_prefixes_preference);
enum_changed_cb (nemo_preferences, NEMO_PREFERENCES_SIZE_PREFIXES, &size_prefixes_preference);
}
gchar **
nemo_global_preferences_get_fileroller_mimetypes (void)
{
static gsize once_init = 0;
if (g_once_init_enter (&once_init)) {
if (nemo_is_file_roller_installed ()) {
GAppInfo *app_info;
gchar ***results;
gchar **result;
gint i;
results = g_desktop_app_info_search ("file-roller");
if (results != NULL && results[0] != NULL) {
const gchar *best;
best = results[0][0];
app_info = G_APP_INFO (g_desktop_app_info_new (best));
if (app_info) {
file_roller_mimetypes = g_strdupv ((gchar **) g_app_info_get_supported_types (app_info));
g_object_unref (app_info);
}
if (app_info == NULL) {
g_warning ("Unable to retrieve list of file-roller mimetypes");
}
i = 0;
result = results[i];
while (result != NULL) {
g_strfreev (result);
result = results[++i];
}
g_free (results);
}
}
g_once_init_leave (&once_init, 1);
}
return file_roller_mimetypes;
}
gchar *
nemo_global_preferences_get_mono_system_font (void)
{
return g_settings_get_string (gnome_interface_preferences, NEMO_PREFERENCES_MONO_FONT_NAME);
}
static gint
sort_shortest_first (gconstpointer a, gconstpointer b)
{
return strlen ((const gchar *) a) > strlen((const gchar *) b);
}
static GList *mono_families = NULL;
gchar *
nemo_global_preferences_get_mono_font_family_match (const gchar *in_family_name)
{
static gsize once_init = 0;
DEBUG ("Looking up monospaced version of '%s'", in_family_name);
// Create a static list of available monospace family names -
//
// FreeMono
// Noto Mono
// Tlwg Mono
// ...
// ...
if (g_once_init_enter (&once_init)) {
DEBUG ("First run: initialize");
PangoFontMap *map = pango_cairo_font_map_get_default ();
PangoFontFamily **families;
gint n_families, i;
pango_font_map_list_families (map, &families, &n_families);
for (i = 0; i < n_families; i++) {
PangoFontFamily *family = families[i];
// should be pango_font_family_is_monospace (family)) but Fonts lie.
if (g_strstr_len (pango_font_family_get_name (family), -1, "Mono")) {
mono_families = g_list_prepend (mono_families, g_strdup (pango_font_family_get_name (family)));
}
}
g_free (families);
mono_families = g_list_sort (mono_families, (GCompareFunc) sort_shortest_first);
GList *l;
if (DEBUGGING) {
for (l = mono_families; l != NULL; l = l->next) {
DEBUG ("Added '%s'", (gchar *) l->data);
}
}
g_once_init_leave (&once_init, 1);
}
// Split the passed-in font string into an array ( like ["Liberation", "Sans" "Regular"] )
gchar **split_in_family_name = g_strsplit (in_family_name, " ", -1);
GList *tmp = g_list_copy (mono_families);
GList *l;
const gchar *best = NULL;
gint best_count = 0;
l = tmp;
// Work thru the list of mono font strings
do {
gint current_count = 0, i;
const gchar *iter_family_name = (const gchar *) l->data;
// Split them the same way ( ["Noto", "Sans", "Mono"] )
gchar **split_iter_family_name = g_strsplit (iter_family_name, " ", -1);
DEBUG (" Comparing %s with %s", in_family_name, iter_family_name);
// Now see how many parts split_iter_family_name and split_n_family have in common.
for (i = 0; i < g_strv_length (split_iter_family_name); i++) {
if (g_strv_contains ((const gchar * const*) split_in_family_name, split_iter_family_name[i])) {
current_count++;
DEBUG (" Matched %s to %s (%d segments)", split_iter_family_name[i], in_family_name, current_count);
} else {
if (i == 0) { // If you can't get the first segment correct, get removed. This avoids
// Liberation Sans Regular getting Noto Sans Mono instead of Liberation Sans Mono.
// In that case, the scores would both be 1, but it's obvious the Noto should be discarded.
l = tmp = g_list_remove (tmp, iter_family_name);
DEBUG (" Discarded %s\n", iter_family_name);
break;
}
}
}
// Is this score better than the last? (more segments matched) - save it.
if (current_count > best_count) {
best_count = current_count;
best = iter_family_name;
DEBUG (" New winner: %s with %d matches", best, best_count);
}
g_strfreev(split_iter_family_name);
l = l->next;
} while (l != NULL);
g_list_free (tmp);
g_strfreev (split_in_family_name);
// We return the best match, or Monospace if there were none - this is the default monospace font Pango uses
// if you use <tt></tt> markup.
if (!best) {
DEBUG ("Match not found, falling back to 'Monospace'");
if (g_strstr_len (in_family_name, -1, "Bold")) {
best = "Monospace Bold";
}
else if (g_strstr_len (in_family_name, -1, "Italic")) {
best = "Monospace Italic";
}
else if (g_strstr_len (in_family_name, -1, "Bold Italic")) {
best = "Monospace Bold Italic";
}
else {
best = "Monospace";
}
}
DEBUG ("Finished: '%s' ----> '%s'", in_family_name, best);
return g_strdup (best);
}
static void
on_time_data_changed (gpointer user_data)
{
prefs_current_date_format = g_settings_get_enum (nemo_preferences, NEMO_PREFERENCES_DATE_FORMAT);
prefs_current_24h_time_format = g_settings_get_boolean (cinnamon_interface_preferences, "clock-use-24h");
if (prefs_current_timezone != NULL) {
g_time_zone_unref (prefs_current_timezone);
}
prefs_current_timezone = g_time_zone_new_local ();
}
static void
setup_cached_time_data (void)
{
GFile *tz;
prefs_current_timezone = NULL;
g_signal_connect_swapped (nemo_preferences,
"changed::" NEMO_PREFERENCES_DATE_FORMAT,
G_CALLBACK (on_time_data_changed), NULL);
g_signal_connect_swapped (cinnamon_interface_preferences,
"changed::clock-use-24h",
G_CALLBACK (on_time_data_changed), NULL);
tz = g_file_new_for_path ("/etc/localtime");
tz_mon = g_file_monitor_file (tz, 0, NULL, NULL);
g_object_unref (tz);
g_signal_connect_swapped (tz_mon,
"changed",
G_CALLBACK (on_time_data_changed), NULL);
on_time_data_changed (NULL);
}
void
nemo_global_preferences_init (void)
{
static gboolean initialized = FALSE;
if (initialized) {
return;
}
initialized = TRUE;
nemo_preferences = g_settings_new("org.nemo.preferences");
nemo_window_state = g_settings_new("org.nemo.window-state");
nemo_icon_view_preferences = g_settings_new("org.nemo.icon-view");
nemo_list_view_preferences = g_settings_new("org.nemo.list-view");
nemo_compact_view_preferences = g_settings_new("org.nemo.compact-view");
nemo_desktop_preferences = g_settings_new("org.nemo.desktop");
/* Some settings such as show hidden files are shared between Nautilus and GTK file chooser */
gtk_filechooser_preferences = g_settings_new_with_path ("org.gtk.Settings.FileChooser",
"/org/gtk/settings/file-chooser/");
nemo_tree_sidebar_preferences = g_settings_new("org.nemo.sidebar-panels.tree");
nemo_plugin_preferences = g_settings_new("org.nemo.plugins");
nemo_menu_config_preferences = g_settings_new("org.nemo.preferences.menu-config");
nemo_search_preferences = g_settings_new("org.nemo.search");
nemo_preview_pane_preferences = g_settings_new("org.nemo.preview-pane");
gnome_lockdown_preferences = g_settings_new("org.cinnamon.desktop.lockdown");
gnome_background_preferences = g_settings_new("org.cinnamon.desktop.background");
gnome_media_handling_preferences = g_settings_new("org.cinnamon.desktop.media-handling");
gnome_terminal_preferences = g_settings_new("org.cinnamon.desktop.default-applications.terminal");
cinnamon_privacy_preferences = g_settings_new("org.cinnamon.desktop.privacy");
cinnamon_interface_preferences = g_settings_new ("org.cinnamon.desktop.interface");
// System mono font
gnome_interface_preferences = g_settings_new ("org.gnome.desktop.interface");
setup_cached_pref_keys ();
setup_cached_time_data ();
eel_debug_call_at_shutdown (nemo_global_preferences_finalize);
}
void
nemo_global_preferences_finalize (void)
{
g_strfreev (file_roller_mimetypes);
g_list_free_full (mono_families, g_free);
g_object_unref (tz_mon);
g_object_unref (nemo_preferences);
g_object_unref (nemo_window_state);
g_object_unref (nemo_icon_view_preferences);
g_object_unref (nemo_list_view_preferences);
g_object_unref (nemo_compact_view_preferences);
g_object_unref (nemo_desktop_preferences);
g_object_unref (nemo_tree_sidebar_preferences);
g_object_unref (nemo_plugin_preferences);
g_object_unref (nemo_menu_config_preferences);
g_object_unref (nemo_search_preferences);
g_object_unref (nemo_preview_pane_preferences);
g_object_unref (gnome_lockdown_preferences);
g_object_unref (gnome_background_preferences);
g_object_unref (gnome_media_handling_preferences);
g_object_unref (gnome_terminal_preferences);
g_object_unref (cinnamon_privacy_preferences);
g_object_unref (cinnamon_interface_preferences);
g_object_unref (gnome_interface_preferences);
}