Fix compatibility with LIBFM 1.0.1 and 1.1.0.
[lxde/pcmanfm.git] / src / app-config.c
1 /*
2 * app-config.c
3 *
4 * Copyright 2010 PCMan <pcman.tw@gmail.com>
5 * Copyright 2012-2014 Andriy Grytsenko (LStranger) <andrej@rep.kiev.ua>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "app-config.h"
28
29 #include <libfm/fm-gtk.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "tab-page.h"
35
36 #if !FM_CHECK_VERSION(1, 2, 0)
37 typedef struct
38 {
39 GKeyFile *kf;
40 char *group; /* allocated if not in cache */
41 char *filepath; /* NULL if in cache */
42 gboolean changed;
43 } FmFolderConfig;
44
45 static GKeyFile *fc_cache = NULL;
46
47 static gboolean dir_cache_changed = FALSE;
48
49 static FmFolderConfig *fm_folder_config_open(FmPath *path)
50 {
51 FmFolderConfig *fc = g_slice_new(FmFolderConfig);
52 FmPath *sub_path;
53
54 fc->changed = FALSE;
55 /* clear .directory file first */
56 sub_path = fm_path_new_child(path, ".directory");
57 fc->filepath = fm_path_to_str(sub_path);
58 fm_path_unref(sub_path);
59 if (g_file_test(fc->filepath, G_FILE_TEST_EXISTS))
60 {
61 fc->kf = g_key_file_new();
62 if (g_key_file_load_from_file(fc->kf, fc->filepath,
63 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
64 NULL) &&
65 g_key_file_has_group(fc->kf, "File Manager"))
66 {
67 fc->group = "File Manager";
68 return fc;
69 }
70 g_key_file_free(fc->kf);
71 }
72 g_free(fc->filepath);
73 fc->filepath = NULL;
74 fc->group = fm_path_to_str(path);
75 fc->kf = fc_cache;
76 return fc;
77 }
78
79 static gboolean fm_folder_config_close(FmFolderConfig *fc, GError **error)
80 {
81 gboolean ret = TRUE;
82
83 if (fc->filepath)
84 {
85 if (fc->changed)
86 {
87 char *out;
88 gsize len;
89
90 out = g_key_file_to_data(fc->kf, &len, error);
91 if (!out || !g_file_set_contents(fc->filepath, out, len, error))
92 ret = FALSE;
93 g_free(out);
94 }
95 g_free(fc->filepath);
96 g_key_file_free(fc->kf);
97 }
98 else
99 {
100 if (fc->changed)
101 {
102 /* raise 'changed' flag and schedule config save */
103 dir_cache_changed = TRUE;
104 pcmanfm_save_config(FALSE);
105 }
106 g_free(fc->group);
107 }
108
109 g_slice_free(FmFolderConfig, fc);
110 return ret;
111 }
112
113 static gboolean fm_folder_config_is_empty(FmFolderConfig *fc)
114 {
115 return !g_key_file_has_group(fc->kf, fc->group);
116 }
117
118 static gboolean fm_folder_config_get_integer(FmFolderConfig *fc, const char *key,
119 gint *val)
120 {
121 return fm_key_file_get_int(fc->kf, fc->group, key, val);
122 }
123
124 static gboolean fm_folder_config_get_boolean(FmFolderConfig *fc, const char *key,
125 gboolean *val)
126 {
127 return fm_key_file_get_bool(fc->kf, fc->group, key, val);
128 }
129
130 #if FM_CHECK_VERSION(1, 0, 2)
131 static char *fm_folder_config_get_string(FmFolderConfig *fc, const char *key)
132 {
133 return g_key_file_get_string(fc->kf, fc->group, key, NULL);
134 }
135
136 static char **fm_folder_config_get_string_list(FmFolderConfig *fc,
137 const char *key, gsize *length)
138 {
139 return g_key_file_get_string_list(fc->kf, fc->group, key, length, NULL);
140 }
141
142 #else
143 static void fm_folder_config_set_integer(FmFolderConfig *fc, const char *key,
144 gint val)
145 {
146 fc->changed = TRUE;
147 g_key_file_set_integer(fc->kf, fc->group, key, val);
148 }
149 #endif
150
151 static void fm_folder_config_set_boolean(FmFolderConfig *fc, const char *key,
152 gboolean val)
153 {
154 fc->changed = TRUE;
155 g_key_file_set_boolean(fc->kf, fc->group, key, val);
156 }
157
158 #if FM_CHECK_VERSION(1, 0, 2)
159 static void fm_folder_config_set_string(FmFolderConfig *fc, const char *key,
160 const char *string)
161 {
162 fc->changed = TRUE;
163 g_key_file_set_string(fc->kf, fc->group, key, string);
164 }
165
166 static void fm_folder_config_set_string_list(FmFolderConfig *fc, const char *key,
167 const gchar * const list[],
168 gsize length)
169 {
170 fc->changed = TRUE;
171 g_key_file_set_string_list(fc->kf, fc->group, key, list, length);
172 }
173
174 static void fm_folder_config_remove_key(FmFolderConfig *fc, const char *key)
175 {
176 fc->changed = TRUE;
177 g_key_file_remove_key(fc->kf, fc->group, key, NULL);
178 }
179 #endif
180
181 static void fm_folder_config_purge(FmFolderConfig *fc)
182 {
183 fc->changed = TRUE;
184 g_key_file_remove_group(fc->kf, fc->group, NULL);
185 }
186
187 static void fm_folder_config_save_cache(const char *dir_path)
188 {
189 char *path, *path2, *path3;
190 char *out;
191 gsize len;
192
193 /* if per-directory cache was changed since last invocation then save it */
194 if (dir_cache_changed)
195 {
196 out = g_key_file_to_data(fc_cache, &len, NULL);
197 if (out)
198 {
199 /* create temp file with settings */
200 path = g_build_filename(dir_path, "dir-settings.conf", NULL);
201 path2 = g_build_filename(dir_path, "dir-settings.tmp", NULL);
202 path3 = g_build_filename(dir_path, "dir-settings.backup", NULL);
203 /* do safe replace now, the file is important enough to be lost */
204 if (g_file_set_contents(path2, out, len, NULL))
205 {
206 /* backup old cache file */
207 g_unlink(path3);
208 if (!g_file_test(path, G_FILE_TEST_EXISTS) ||
209 g_rename(path, path3) == 0)
210 {
211 /* rename temp file */
212 if (g_rename(path2, path) == 0)
213 {
214 /* success! remove the old cache file */
215 g_unlink(path3);
216 /* reset the 'changed' flag */
217 dir_cache_changed = FALSE;
218 }
219 else
220 g_warning("cannot rename %s to %s", path2, path);
221 }
222 else
223 g_warning("cannot rename %s to %s", path, path3);
224 }
225 else
226 g_warning("cannot save %s", path2);
227 g_free(path);
228 g_free(path2);
229 g_free(path3);
230 g_free(out);
231 }
232 }
233 }
234 #endif /* LibFM < 1.2.0 */
235
236 #if FM_CHECK_VERSION(1, 0, 2)
237 static void _parse_sort(GKeyFile *kf, const char *group, FmSortMode *mode,
238 FmFolderModelCol *col)
239 {
240 int tmp_int;
241 char **sort;
242
243 /* parse "sort" strings list first */
244 sort = g_key_file_get_string_list(kf, group, "sort", NULL, NULL);
245 if (sort)
246 {
247 FmSortMode tmp_mode = 0;
248 FmFolderModelCol tmp_col = FM_FOLDER_MODEL_COL_DEFAULT;
249
250 for (tmp_int = 0; sort[tmp_int]; tmp_int++)
251 {
252 if (tmp_int == 0) /* column should be first! */
253 tmp_col = fm_folder_model_get_col_by_name(sort[tmp_int]);
254 else if (strcmp(sort[tmp_int], "ascending") == 0)
255 tmp_mode = (tmp_mode & ~FM_SORT_ORDER_MASK) | FM_SORT_ASCENDING;
256 else if (strcmp(sort[tmp_int], "descending") == 0)
257 tmp_mode = (tmp_mode & ~FM_SORT_ORDER_MASK) | FM_SORT_DESCENDING;
258 else if (strcmp(sort[tmp_int], "case") == 0)
259 tmp_mode |= FM_SORT_CASE_SENSITIVE;
260 #if FM_CHECK_VERSION(1, 2, 0)
261 else if (strcmp(sort[tmp_int], "mingle") == 0)
262 tmp_mode |= FM_SORT_NO_FOLDER_FIRST;
263 #endif
264 }
265 *mode = tmp_mode;
266 if (tmp_col != FM_FOLDER_MODEL_COL_DEFAULT)
267 *col = tmp_col;
268 g_strfreev(sort);
269 return;
270 }
271 /* parse fallback old style sort config */
272 if(fm_key_file_get_int(kf, group, "sort_type", &tmp_int) &&
273 tmp_int == GTK_SORT_DESCENDING)
274 *mode = FM_SORT_DESCENDING;
275 else
276 *mode = FM_SORT_ASCENDING;
277 if(fm_key_file_get_int(kf, group, "sort_by", &tmp_int) &&
278 #if FM_CHECK_VERSION(1, 2, 0)
279 fm_folder_model_col_is_valid((guint)tmp_int))
280 #else
281 FM_FOLDER_MODEL_COL_IS_VALID((guint)tmp_int))
282 #endif
283 *col = tmp_int;
284 }
285 #else /* < 1.0.2 */
286 static void _parse_sort(GKeyFile *kf, const char *group, GtkSortType *mode,
287 int *col)
288 {
289 int tmp_int;
290
291 if(fm_key_file_get_int(kf, group, "sort_type", &tmp_int) &&
292 tmp_int == GTK_SORT_DESCENDING)
293 *mode = GTK_SORT_DESCENDING;
294 else
295 *mode = GTK_SORT_ASCENDING;
296 if(fm_key_file_get_int(kf, group, "sort_by", &tmp_int) &&
297 FM_FOLDER_MODEL_COL_IS_VALID((guint)tmp_int))
298 *col = tmp_int;
299 }
300 #endif
301
302 #if FM_CHECK_VERSION(1, 0, 2)
303 static void _save_sort(GString *buf, FmSortMode mode, FmFolderModelCol col)
304 {
305 const char *name = fm_folder_model_col_get_name(col);
306
307 if (name == NULL) /* FM_FOLDER_MODEL_COL_NAME is always valid */
308 name = fm_folder_model_col_get_name(FM_FOLDER_MODEL_COL_NAME);
309 g_string_append_printf(buf, "sort=%s;%s;", name,
310 FM_SORT_IS_ASCENDING(mode) ? "ascending" : "descending");
311 if (mode & FM_SORT_CASE_SENSITIVE)
312 g_string_append(buf, "case;");
313 #if FM_CHECK_VERSION(1, 2, 0)
314 if (mode & FM_SORT_NO_FOLDER_FIRST)
315 g_string_append(buf, "mingle;");
316 #endif
317 g_string_append_c(buf, '\n');
318 }
319 #else
320 static void _save_sort(GString *buf, GtkSortType type, int col)
321 {
322 g_string_append_printf(buf, "sort_type=%d\n", type);
323 g_string_append_printf(buf, "sort_by=%d\n", col);
324 }
325 #endif
326
327 /* we use capitalized keys here because it is de-facto standard for
328 desktop entry files and we use those keys in '.directory' as well */
329 #if FM_CHECK_VERSION(1, 0, 2)
330 static gboolean _parse_config_for_path(FmFolderConfig *fc,
331 FmSortMode *mode, FmFolderModelCol *by,
332 #else
333 static gboolean _parse_config_for_path(FmFolderConfig *fc,
334 GtkSortType *mode, gint *by,
335 #endif
336 FmStandardViewMode *view_mode,
337 gboolean *show_hidden, char ***columns)
338 {
339 int tmp_int;
340 /* we cannot use _parse_sort() here because we have no access to GKeyFile */
341 #if FM_CHECK_VERSION(1, 0, 2)
342 char *tmp;
343 char **sort;
344
345 /* parse "sort" strings list first */
346 sort = fm_folder_config_get_string_list(fc, "Sort", NULL);
347 if (sort)
348 {
349 FmSortMode tmp_mode = 0;
350 FmFolderModelCol tmp_col = FM_FOLDER_MODEL_COL_DEFAULT;
351
352 for (tmp_int = 0; sort[tmp_int]; tmp_int++)
353 {
354 if (tmp_int == 0) /* column should be first! */
355 tmp_col = fm_folder_model_get_col_by_name(sort[tmp_int]);
356 else if (strcmp(sort[tmp_int], "ascending") == 0)
357 tmp_mode = (tmp_mode & ~FM_SORT_ORDER_MASK) | FM_SORT_ASCENDING;
358 else if (strcmp(sort[tmp_int], "descending") == 0)
359 tmp_mode = (tmp_mode & ~FM_SORT_ORDER_MASK) | FM_SORT_DESCENDING;
360 else if (strcmp(sort[tmp_int], "case") == 0)
361 tmp_mode |= FM_SORT_CASE_SENSITIVE;
362 #if FM_CHECK_VERSION(1, 2, 0)
363 else if (strcmp(sort[tmp_int], "mingle") == 0)
364 tmp_mode |= FM_SORT_NO_FOLDER_FIRST;
365 #endif
366 }
367 *mode = tmp_mode;
368 if (tmp_col != FM_FOLDER_MODEL_COL_DEFAULT)
369 *by = tmp_col;
370 g_strfreev(sort);
371 }
372 else
373 {
374 /* parse fallback old style sort config */
375 if(fm_folder_config_get_integer(fc, "sort_type", &tmp_int) &&
376 tmp_int == GTK_SORT_DESCENDING)
377 *mode = FM_SORT_DESCENDING;
378 else
379 *mode = FM_SORT_ASCENDING;
380 if(fm_folder_config_get_integer(fc, "sort_by", &tmp_int) &&
381 #if FM_CHECK_VERSION(1, 2, 0)
382 fm_folder_model_col_is_valid((guint)tmp_int))
383 #else
384 FM_FOLDER_MODEL_COL_IS_VALID((guint)tmp_int))
385 #endif
386 *by = tmp_int;
387 }
388 #else /* < 1.0.2 */
389 if(fm_folder_config_get_integer(fc, "sort_type", &tmp_int) &&
390 tmp_int == GTK_SORT_DESCENDING)
391 *mode = GTK_SORT_DESCENDING;
392 else
393 *mode = GTK_SORT_ASCENDING;
394 if(fm_folder_config_get_integer(fc, "sort_by", &tmp_int) &&
395 FM_FOLDER_MODEL_COL_IS_VALID((guint)tmp_int))
396 *by = tmp_int;
397 #endif
398 #if FM_CHECK_VERSION(1, 0, 2)
399 if (view_mode && (tmp = fm_folder_config_get_string(fc, "ViewMode")))
400 {
401 *view_mode = fm_standard_view_mode_from_str(tmp);
402 g_free(tmp);
403 }
404 #else
405 if (view_mode && fm_folder_config_get_integer(fc, "ViewMode", &tmp_int) &&
406 FM_STANDARD_VIEW_MODE_IS_VALID(tmp_int))
407 *view_mode = tmp_int;
408 #endif
409 if (show_hidden)
410 fm_folder_config_get_boolean(fc, "ShowHidden", show_hidden);
411 #if FM_CHECK_VERSION(1, 0, 2)
412 if (columns)
413 *columns = fm_folder_config_get_string_list(fc, "Columns", NULL);
414 #endif
415 return TRUE;
416 }
417
418
419 static void fm_app_config_finalize (GObject *object);
420
421 G_DEFINE_TYPE(FmAppConfig, fm_app_config, FM_CONFIG_TYPE);
422
423
424 static void fm_app_config_class_init(FmAppConfigClass *klass)
425 {
426 GObjectClass *g_object_class;
427 g_object_class = G_OBJECT_CLASS(klass);
428 g_object_class->finalize = fm_app_config_finalize;
429 }
430
431
432 static void fm_app_config_finalize(GObject *object)
433 {
434 FmAppConfig *cfg;
435
436 g_return_if_fail(object != NULL);
437 g_return_if_fail(IS_FM_APP_CONFIG(object));
438
439 cfg = (FmAppConfig*)object;
440 #if FM_CHECK_VERSION(1, 0, 2)
441 g_strfreev(cfg->columns);
442 #endif
443 if (cfg->desktop_section.configured)
444 {
445 if(cfg->desktop_section.wallpapers_configured > 0)
446 {
447 int i;
448
449 for(i = 0; i < cfg->desktop_section.wallpapers_configured; i++)
450 g_free(cfg->desktop_section.wallpapers[i]);
451 g_free(cfg->desktop_section.wallpapers);
452 }
453 g_free(cfg->desktop_section.wallpaper);
454 g_free(cfg->desktop_section.desktop_font);
455 g_free(cfg->desktop_section.folder);
456 }
457 /*g_free(cfg->su_cmd);*/
458 g_hash_table_unref(cfg->autorun_choices);
459
460 #if !FM_CHECK_VERSION(1, 2, 0)
461 g_key_file_free(fc_cache);
462 fc_cache = NULL;
463 #endif
464
465 #if FM_CHECK_VERSION(1, 2, 0)
466 g_free(cfg->home_path);
467 #endif
468
469 G_OBJECT_CLASS(fm_app_config_parent_class)->finalize(object);
470 }
471
472 static void _free_archoice(gpointer data)
473 {
474 FmAutorunChoice *choice = data;
475
476 g_free(choice->last_used);
477 g_slice_free(FmAutorunChoice, choice);
478 }
479
480 static void fm_app_config_init(FmAppConfig *cfg)
481 {
482 /* load libfm config file */
483 fm_config_load_from_file((FmConfig*)cfg, NULL);
484
485 cfg->bm_open_method = FM_OPEN_IN_CURRENT_TAB;
486
487 cfg->mount_on_startup = TRUE;
488 cfg->mount_removable = TRUE;
489 cfg->autorun = TRUE;
490
491 cfg->desktop_section.desktop_fg.red = cfg->desktop_section.desktop_fg.green = cfg->desktop_section.desktop_fg.blue = 65535;
492 cfg->win_width = 640;
493 cfg->win_height = 480;
494 cfg->splitter_pos = 150;
495 cfg->max_tab_chars = 32;
496 cfg->media_in_new_tab = FALSE;
497 cfg->desktop_folder_new_win = FALSE;
498
499 cfg->side_pane_mode = FM_SP_PLACES;
500
501 cfg->view_mode = FM_FV_ICON_VIEW;
502 cfg->show_hidden = FALSE;
503 #if FM_CHECK_VERSION(1, 0, 2)
504 cfg->sort_type = FM_SORT_ASCENDING;
505 cfg->sort_by = FM_FOLDER_MODEL_COL_NAME;
506 #if FM_CHECK_VERSION(1, 2, 0)
507 cfg->desktop_section.desktop_sort_type = FM_SORT_ASCENDING | FM_SORT_NO_FOLDER_FIRST;
508 #else
509 cfg->desktop_section.desktop_sort_type = FM_SORT_ASCENDING;
510 #endif
511 cfg->desktop_section.desktop_sort_by = FM_FOLDER_MODEL_COL_MTIME;
512 #else
513 cfg->sort_type = GTK_SORT_ASCENDING;
514 cfg->sort_by = COL_FILE_NAME;
515 cfg->desktop_section.desktop_sort_type = GTK_SORT_ASCENDING;
516 cfg->desktop_section.desktop_sort_by = COL_FILE_MTIME;
517 #endif
518 cfg->desktop_section.wallpaper_common = TRUE;
519 #if FM_CHECK_VERSION(1, 2, 0)
520 cfg->desktop_section.show_documents = FALSE;
521 cfg->desktop_section.show_trash = TRUE;
522 cfg->desktop_section.show_mounts = FALSE;
523 #endif
524 cfg->desktop_section.folder = NULL;
525 cfg->tb.visible = cfg->tb.new_tab = cfg->tb.nav = cfg->tb.home = TRUE;
526 cfg->tb.new_win = FALSE;
527 cfg->autorun_choices = g_hash_table_new_full(g_str_hash, g_str_equal,
528 g_free, _free_archoice);
529 cfg->show_statusbar = TRUE;
530 #if FM_CHECK_VERSION(1, 2, 0)
531 cfg->home_path = NULL;
532 cfg->focus_previous = FALSE;
533 #endif
534 cfg->change_tab_on_drop = TRUE;
535 cfg->close_on_unmount = TRUE;
536 cfg->maximized = FALSE;
537 cfg->pathbar_mode_buttons = FALSE;
538 }
539
540
541 FmConfig *fm_app_config_new(void)
542 {
543 return (FmConfig*)g_object_new(FM_APP_CONFIG_TYPE, NULL);
544 }
545
546 void fm_app_config_load_desktop_config(GKeyFile *kf, const char *group, FmDesktopConfig *cfg)
547 {
548 char* tmp;
549
550 if (!g_key_file_has_group(kf, group))
551 return;
552
553 /* set some defaults, assuming config is zeroed now */
554 cfg->desktop_fg.red = cfg->desktop_fg.green = cfg->desktop_fg.blue = 65535;
555 #if FM_CHECK_VERSION(1, 0, 2)
556 #if FM_CHECK_VERSION(1, 2, 0)
557 cfg->desktop_sort_type = FM_SORT_ASCENDING | FM_SORT_NO_FOLDER_FIRST;
558 #else
559 cfg->desktop_sort_type = FM_SORT_ASCENDING;
560 #endif
561 cfg->desktop_sort_by = FM_FOLDER_MODEL_COL_MTIME;
562 #else
563 cfg->desktop_sort_type = GTK_SORT_ASCENDING;
564 cfg->desktop_sort_by = COL_FILE_MTIME;
565 #endif
566 cfg->wallpaper_common = TRUE;
567 #if FM_CHECK_VERSION(1, 2, 0)
568 cfg->show_trash = TRUE;
569 #endif
570 cfg->configured = TRUE;
571 tmp = g_key_file_get_string(kf, group, "wallpaper_mode", NULL);
572 if (tmp)
573 {
574 if (tmp[0] >= '0' && tmp[0] <= '4') /* backward compatibility */
575 cfg->wallpaper_mode = tmp[0] - '0';
576 else
577 cfg->wallpaper_mode = fm_app_wallpaper_get_mode_by_name(tmp);
578 if (cfg->wallpaper_mode == (FmWallpaperMode)-1)
579 cfg->wallpaper_mode = FM_WP_COLOR; /* fallback */
580 g_free(tmp);
581 }
582
583 if(cfg->wallpapers_configured > 0)
584 {
585 int i;
586
587 for(i = 0; i < cfg->wallpapers_configured; i++)
588 g_free(cfg->wallpapers[i]);
589 g_free(cfg->wallpapers);
590 }
591 g_free(cfg->wallpaper);
592 cfg->wallpaper = NULL;
593 fm_key_file_get_int(kf, group, "wallpapers_configured", &cfg->wallpapers_configured);
594 if(cfg->wallpapers_configured > 0)
595 {
596 char wpn_buf[32];
597 int i;
598
599 cfg->wallpapers = g_malloc(cfg->wallpapers_configured * sizeof(char *));
600 for(i = 0; i < cfg->wallpapers_configured; i++)
601 {
602 snprintf(wpn_buf, sizeof(wpn_buf), "wallpaper%d", i);
603 tmp = g_key_file_get_string(kf, group, wpn_buf, NULL);
604 cfg->wallpapers[i] = tmp;
605 }
606 }
607 fm_key_file_get_bool(kf, group, "wallpaper_common", &cfg->wallpaper_common);
608 if (cfg->wallpaper_common)
609 {
610 tmp = g_key_file_get_string(kf, group, "wallpaper", NULL);
611 if (tmp && tmp[0] == '\0') /* ignore empty string */
612 {
613 g_free(tmp);
614 tmp = NULL;
615 }
616 g_free(cfg->wallpaper);
617 cfg->wallpaper = tmp;
618 }
619
620 tmp = g_key_file_get_string(kf, group, "desktop_bg", NULL);
621 if(tmp)
622 {
623 gdk_color_parse(tmp, &cfg->desktop_bg);
624 g_free(tmp);
625 }
626 tmp = g_key_file_get_string(kf, group, "desktop_fg", NULL);
627 if(tmp)
628 {
629 gdk_color_parse(tmp, &cfg->desktop_fg);
630 g_free(tmp);
631 }
632 tmp = g_key_file_get_string(kf, group, "desktop_shadow", NULL);
633 if(tmp)
634 {
635 gdk_color_parse(tmp, &cfg->desktop_shadow);
636 g_free(tmp);
637 }
638
639 tmp = g_key_file_get_string(kf, group, "desktop_font", NULL);
640 g_free(cfg->desktop_font);
641 cfg->desktop_font = tmp;
642
643 g_free(cfg->folder);
644 cfg->folder = g_key_file_get_string(kf, group, "folder", NULL);
645
646 fm_key_file_get_bool(kf, group, "show_wm_menu", &cfg->show_wm_menu);
647 _parse_sort(kf, group, &cfg->desktop_sort_type, &cfg->desktop_sort_by);
648 #if FM_CHECK_VERSION(1, 2, 0)
649 fm_key_file_get_bool(kf, group, "show_documents", &cfg->show_documents);
650 fm_key_file_get_bool(kf, group, "show_trash", &cfg->show_trash);
651 fm_key_file_get_bool(kf, group, "show_mounts", &cfg->show_mounts);
652 #endif
653 }
654
655 void fm_app_config_load_from_key_file(FmAppConfig* cfg, GKeyFile* kf)
656 {
657 char *tmp;
658 char **tmpv;
659 int tmp_int, i;
660
661 /* behavior */
662 fm_key_file_get_int(kf, "config", "bm_open_method", &cfg->bm_open_method);
663 /*tmp = g_key_file_get_string(kf, "config", "su_cmd", NULL);
664 g_free(cfg->su_cmd);
665 cfg->su_cmd = tmp;*/
666 #if FM_CHECK_VERSION(1, 2, 0)
667 g_free(cfg->home_path);
668 cfg->home_path = g_key_file_get_string(kf, "config", "home_path", NULL);
669 #endif
670
671 /* volume management */
672 fm_key_file_get_bool(kf, "volume", "mount_on_startup", &cfg->mount_on_startup);
673 fm_key_file_get_bool(kf, "volume", "mount_removable", &cfg->mount_removable);
674 fm_key_file_get_bool(kf, "volume", "autorun", &cfg->autorun);
675
676 /* [desktop] section */
677 fm_app_config_load_desktop_config(kf, "desktop", &cfg->desktop_section);
678
679 /* ui */
680 fm_key_file_get_bool(kf, "ui", "always_show_tabs", &cfg->always_show_tabs);
681 fm_key_file_get_int(kf, "ui", "hide_close_btn", &cfg->hide_close_btn);
682 fm_key_file_get_int(kf, "ui", "max_tab_chars", &cfg->max_tab_chars);
683
684 fm_key_file_get_int(kf, "ui", "win_width", &cfg->win_width);
685 fm_key_file_get_int(kf, "ui", "win_height", &cfg->win_height);
686 fm_key_file_get_bool(kf, "ui", "maximized", &cfg->maximized);
687
688 fm_key_file_get_int(kf, "ui", "splitter_pos", &cfg->splitter_pos);
689
690 fm_key_file_get_bool(kf, "ui", "media_in_new_tab", &cfg->media_in_new_tab);
691 fm_key_file_get_bool(kf, "ui", "desktop_folder_new_win", &cfg->desktop_folder_new_win);
692 fm_key_file_get_bool(kf, "ui", "change_tab_on_drop", &cfg->change_tab_on_drop);
693 fm_key_file_get_bool(kf, "ui", "close_on_unmount", &cfg->close_on_unmount);
694
695 #if FM_CHECK_VERSION(1, 2, 0)
696 fm_key_file_get_bool(kf, "ui", "focus_previous", &cfg->focus_previous);
697 tmp_int = FM_SP_NONE;
698 tmpv = g_key_file_get_string_list(kf, "ui", "side_pane_mode", NULL, NULL);
699 if (tmpv)
700 {
701 for (i = 0; tmpv[i]; i++)
702 {
703 tmp = tmpv[i];
704 if (strcmp(tmp, "hidden") == 0)
705 tmp_int |= FM_SP_HIDE;
706 else
707 {
708 tmp_int &= ~FM_SP_MODE_MASK;
709 if (tmp[0] >= '0' && tmp[0] <= '9') /* backward compatibility */
710 tmp_int |= atoi(tmp);
711 else /* portable way */
712 tmp_int |= fm_side_pane_get_mode_by_name(tmp);
713 }
714 }
715 g_strfreev(tmpv);
716 }
717 if ((tmp_int & FM_SP_MODE_MASK) != FM_SP_NONE)
718 #else
719 if(fm_key_file_get_int(kf, "ui", "side_pane_mode", &tmp_int))
720 #endif
721 cfg->side_pane_mode = (FmSidePaneMode)tmp_int;
722
723 /* default values for folder views */
724 #if FM_CHECK_VERSION(1, 0, 2)
725 tmp = g_key_file_get_string(kf, "ui", "view_mode", NULL);
726 if (tmp)
727 {
728 if (tmp[0] >= '0' && tmp[0] <= '9') /* backward compatibility */
729 tmp_int = atoi(tmp);
730 else /* portable way */
731 tmp_int = fm_standard_view_mode_from_str(tmp);
732 g_free(tmp);
733 }
734 if (tmp &&
735 #else
736 if(fm_key_file_get_int(kf, "ui", "view_mode", &tmp_int) &&
737 #endif
738 FM_STANDARD_VIEW_MODE_IS_VALID(tmp_int))
739 cfg->view_mode = tmp_int;
740 fm_key_file_get_bool(kf, "ui", "show_hidden", &cfg->show_hidden);
741 _parse_sort(kf, "ui", &cfg->sort_type, &cfg->sort_by);
742 #if FM_CHECK_VERSION(1, 0, 2)
743 tmpv = g_key_file_get_string_list(kf, "ui", "columns", NULL, NULL);
744 if (tmpv)
745 {
746 g_strfreev(cfg->columns);
747 cfg->columns = tmpv;
748 }
749 #endif
750 tmpv = g_key_file_get_string_list(kf, "ui", "toolbar", NULL, NULL);
751 if (tmpv)
752 {
753 /* reset defaults */
754 cfg->tb.visible = TRUE;
755 cfg->tb.new_win = cfg->tb.new_tab = cfg->tb.nav = cfg->tb.home = FALSE;
756 /* parse the array */
757 for (i = 0; tmpv[i]; i++)
758 {
759 tmp = tmpv[i];
760 if (cfg->tb.visible && strcmp(tmp, "hidden") == 0)
761 cfg->tb.visible = FALSE;
762 else if (!cfg->tb.new_win && strcmp(tmp, "newwin") == 0)
763 cfg->tb.new_win = TRUE;
764 else if (!cfg->tb.new_tab && strcmp(tmp, "newtab") == 0)
765 cfg->tb.new_tab = TRUE;
766 else if (!cfg->tb.nav && strcmp(tmp, "navigation") == 0)
767 cfg->tb.nav = TRUE;
768 else if (!cfg->tb.home && strcmp(tmp, "home") == 0)
769 cfg->tb.home = TRUE;
770 }
771 g_strfreev(tmpv);
772 }
773 fm_key_file_get_bool(kf, "ui", "show_statusbar", &cfg->show_statusbar);
774 if (g_key_file_has_group(kf, "autorun"))
775 {
776 tmpv = g_key_file_get_keys(kf, "autorun", NULL, NULL);
777 for (i = 0; tmpv[i]; i++)
778 {
779 tmp = g_key_file_get_string(kf, "autorun", tmpv[i], NULL);
780 if (tmp && tmp[0])
781 {
782 if (tmp[0] == '*')
783 fm_app_config_set_autorun_choice(cfg, tmpv[i],
784 tmp[1] ? &tmp[1] : NULL,
785 TRUE);
786 else
787 fm_app_config_set_autorun_choice(cfg, tmpv[i], tmp, FALSE);
788 }
789 g_free(tmp);
790 }
791 g_strfreev(tmpv);
792 }
793 fm_key_file_get_bool(kf, "ui", "pathbar_mode_buttons", &cfg->pathbar_mode_buttons);
794 }
795
796 void fm_app_config_load_from_profile(FmAppConfig* cfg, const char* name)
797 {
798 const gchar * const *dirs, * const *dir;
799 char *path;
800 GKeyFile* kf = g_key_file_new();
801 const char* old_name = name;
802
803 if(!name || !*name) /* if profile name is not provided, use 'default' */
804 {
805 name = "default";
806 old_name = "pcmanfm"; /* for compatibility with old versions. */
807 }
808
809 /* load system-wide settings */
810 dirs = g_get_system_config_dirs();
811 for(dir=dirs;*dir;++dir)
812 {
813 path = g_build_filename(*dir, "pcmanfm", name, "pcmanfm.conf", NULL);
814 if(g_key_file_load_from_file(kf, path, 0, NULL))
815 fm_app_config_load_from_key_file(cfg, kf);
816 g_free(path);
817 }
818
819 /* override system-wide settings with user-specific configuration */
820
821 /* For backward compatibility, try to load old config file and
822 * then migrate to new location */
823 path = g_strconcat(g_get_user_config_dir(), "/pcmanfm/", old_name, ".conf", NULL);
824 if(G_UNLIKELY(g_key_file_load_from_file(kf, path, 0, NULL)))
825 {
826 char* new_dir;
827 /* old config file is found, migrate to new profile format */
828 fm_app_config_load_from_key_file(cfg, kf);
829
830 /* create the profile dir */
831 new_dir = g_build_filename(g_get_user_config_dir(), "pcmanfm", name, NULL);
832 if(g_mkdir_with_parents(new_dir, 0700) == 0)
833 {
834 /* move the old config file to new location */
835 char* new_path = g_build_filename(new_dir, "pcmanfm.conf", NULL);
836 rename(path, new_path);
837 g_free(new_path);
838 }
839 g_free(new_dir);
840 }
841 else
842 {
843 g_free(path);
844 path = g_build_filename(g_get_user_config_dir(), "pcmanfm", name, "pcmanfm.conf", NULL);
845 if(g_key_file_load_from_file(kf, path, 0, NULL))
846 fm_app_config_load_from_key_file(cfg, kf);
847 }
848 g_free(path);
849 g_key_file_free(kf);
850
851 #if !FM_CHECK_VERSION(1, 2, 0)
852 fc_cache = g_key_file_new();
853 path = g_build_filename(g_get_user_config_dir(), "pcmanfm", name,
854 "dir-settings.conf", NULL);
855 g_key_file_load_from_file(fc_cache, path, 0, NULL);
856 g_free(path);
857 #endif
858 }
859
860 /**
861 * fm_app_config_get_config_for_path
862 * @path: path to get config
863 * @mode: (allow-none) (out): location to save sort mode
864 * @by: (allow-none) (out): location to save sort columns
865 * @view_mode: (allow-none) (out): location to save view mode
866 * @show_hidden: (allow-none) (out): location to save show hidden flag
867 * @columns: (allow-none) (out) (transfer none): location to save columns list
868 *
869 * Returns: %TRUE if @path has individual configuration.
870 */
871 #if FM_CHECK_VERSION(1, 0, 2)
872 gboolean fm_app_config_get_config_for_path(FmPath *path, FmSortMode *mode,
873 FmFolderModelCol *by,
874 #else
875 gboolean fm_app_config_get_config_for_path(FmPath *path, GtkSortType *mode,
876 gint *by,
877 #endif
878 FmStandardViewMode *view_mode,
879 gboolean *show_hidden,
880 char ***columns)
881 {
882 FmPath *sub_path;
883 FmFolderConfig *fc;
884 gboolean ret = TRUE;
885
886 /* preload defaults */
887 if (mode)
888 *mode = app_config->sort_type;
889 if (by)
890 *by = app_config->sort_by;
891 if (view_mode)
892 *view_mode = app_config->view_mode;
893 if (show_hidden)
894 *show_hidden = app_config->show_hidden;
895 #if FM_CHECK_VERSION(1, 0, 2)
896 if (columns)
897 *columns = app_config->columns;
898 #endif
899 fc = fm_folder_config_open(path);
900 if (!fm_folder_config_is_empty(fc))
901 _parse_config_for_path(fc, mode, by, view_mode, show_hidden, columns);
902 else if (!fm_path_is_native(path))
903 {
904 /* if path is non-native then try the scheme */
905 #if FM_CHECK_VERSION(1, 2, 0)
906 sub_path = fm_path_get_scheme_path(path);
907 #else
908 for (sub_path = path; fm_path_get_parent(sub_path) != NULL; )
909 sub_path = fm_path_get_parent(sub_path);
910 #endif
911 fm_folder_config_close(fc, NULL);
912 fc = fm_folder_config_open(sub_path);
913 if (!fm_folder_config_is_empty(fc))
914 _parse_config_for_path(fc, mode, by, view_mode, show_hidden, columns);
915 /* if path is search://... then use predefined values */
916 else if (strncmp(fm_path_get_basename(sub_path), "search:", 7) == 0)
917 {
918 if (view_mode)
919 *view_mode = FM_FV_LIST_VIEW;
920 if (show_hidden)
921 *show_hidden = TRUE;
922 #if FM_CHECK_VERSION(1, 0, 2)
923 if (columns)
924 {
925 static char *def[] = {"name", "desc", "dirname", "size", "mtime", NULL};
926 *columns = def;
927 }
928 #endif
929 }
930 else
931 ret = FALSE;
932 }
933 else
934 ret = FALSE;
935 fm_folder_config_close(fc, NULL);
936 return ret;
937 }
938
939 /**
940 * @path, @mode, @by, @show_hidden are mandatory
941 * @view_mode may be -1 to not change
942 * @columns may be %NULL to not change
943 */
944 #if FM_CHECK_VERSION(1, 0, 2)
945 void fm_app_config_save_config_for_path(FmPath *path, FmSortMode mode,
946 FmFolderModelCol by,
947 #else
948 void fm_app_config_save_config_for_path(FmPath *path, GtkSortType mode, gint by,
949 #endif
950 FmStandardViewMode view_mode,
951 gboolean show_hidden, char **columns)
952 {
953 FmPath *sub_path;
954 FmFolderConfig *fc;
955 #if FM_CHECK_VERSION(1, 0, 2)
956 char const *list[5];
957 int n = 2;
958 #endif
959
960 /* if path is search://... then use search: instead */
961 #if FM_CHECK_VERSION(1, 2, 0)
962 sub_path = fm_path_get_scheme_path(path);
963 #else
964 for (sub_path = path; fm_path_get_parent(sub_path) != NULL; )
965 sub_path = fm_path_get_parent(sub_path);
966 #endif
967 if (strncmp(fm_path_get_basename(sub_path), "search:", 7) == 0)
968 {
969 path = fm_path_new_for_uri("search:///");
970 fc = fm_folder_config_open(path);
971 fm_path_unref(path);
972 /* allow to create new entry only if we got valid columns */
973 if (fm_folder_config_is_empty(fc) && (columns != NULL && columns[0] != NULL))
974 {
975 fm_folder_config_close(fc, NULL);
976 /* save columns is mandatory for search view */
977 return;
978 }
979 view_mode = FM_FV_LIST_VIEW; /* search view mode should be immutable */
980 }
981 else
982 fc = fm_folder_config_open(path);
983 #if FM_CHECK_VERSION(1, 0, 2)
984 list[0] = fm_folder_model_col_get_name(by);
985 if (list[0] == NULL) /* FM_FOLDER_MODEL_COL_NAME is always valid */
986 list[0] = fm_folder_model_col_get_name(FM_FOLDER_MODEL_COL_NAME);
987 list[1] = FM_SORT_IS_ASCENDING(mode) ? "ascending" : "descending";
988 if (mode & FM_SORT_CASE_SENSITIVE)
989 list[n++] = "case";
990 #if FM_CHECK_VERSION(1, 2, 0)
991 if (mode & FM_SORT_NO_FOLDER_FIRST)
992 list[n++] = "mingle";
993 #endif
994 list[n] = NULL;
995 fm_folder_config_set_string_list(fc, "Sort", list, n);
996 if (FM_STANDARD_VIEW_MODE_IS_VALID(view_mode))
997 fm_folder_config_set_string(fc, "ViewMode",
998 fm_standard_view_mode_to_str(view_mode));
999 if (columns && columns[0])
1000 fm_folder_config_set_string_list(fc, "Columns",
1001 (const char *const *)columns,
1002 g_strv_length(columns));
1003 else if (columns) /* empty list means we should reset columns */
1004 fm_folder_config_remove_key(fc, "Columns");
1005 #else /* pre-1.0.2 */
1006 if (FM_FOLDER_VIEW_MODE_IS_VALID(view_mode))
1007 fm_folder_config_set_integer(fc, "ViewMode", view_mode);
1008 fm_folder_config_set_integer(fc, "sort_type", mode);
1009 fm_folder_config_set_integer(fc, "sort_by", by);
1010 #endif
1011 fm_folder_config_set_boolean(fc, "ShowHidden", show_hidden);
1012 fm_folder_config_close(fc, NULL);
1013 #if FM_CHECK_VERSION(1, 2, 0)
1014 /* raise 'changed' flag and schedule config save */
1015 pcmanfm_save_config(FALSE);
1016 #endif
1017 }
1018
1019 void fm_app_config_clear_config_for_path(FmPath *path)
1020 {
1021 FmFolderConfig *fc = fm_folder_config_open(path);
1022
1023 fm_folder_config_purge(fc);
1024 fm_folder_config_close(fc, NULL);
1025 #if FM_CHECK_VERSION(1, 2, 0)
1026 /* raise 'changed' flag and schedule config save */
1027 pcmanfm_save_config(FALSE);
1028 #endif
1029 }
1030
1031 void fm_app_config_save_desktop_config(GString *buf, const char *group, FmDesktopConfig *cfg)
1032 {
1033 g_string_append_printf(buf, "[%s]\n"
1034 "wallpaper_mode=%s\n", group,
1035 fm_app_wallpaper_get_mode_name(cfg->wallpaper_mode));
1036 g_string_append_printf(buf, "wallpaper_common=%d\n", cfg->wallpaper_common);
1037 if (cfg->wallpapers && cfg->wallpapers_configured > 0)
1038 {
1039 int i;
1040
1041 g_string_append_printf(buf, "wallpapers_configured=%d\n", cfg->wallpapers_configured);
1042 for (i = 0; i < cfg->wallpapers_configured; i++)
1043 if (cfg->wallpapers[i])
1044 g_string_append_printf(buf, "wallpaper%d=%s\n", i, cfg->wallpapers[i]);
1045 }
1046 if (cfg->wallpaper_common && cfg->wallpaper)
1047 g_string_append_printf(buf, "wallpaper=%s\n", cfg->wallpaper);
1048 g_string_append_printf(buf, "desktop_bg=#%02x%02x%02x\n",
1049 cfg->desktop_bg.red/257,
1050 cfg->desktop_bg.green/257,
1051 cfg->desktop_bg.blue/257);
1052 g_string_append_printf(buf, "desktop_fg=#%02x%02x%02x\n",
1053 cfg->desktop_fg.red/257,
1054 cfg->desktop_fg.green/257,
1055 cfg->desktop_fg.blue/257);
1056 g_string_append_printf(buf, "desktop_shadow=#%02x%02x%02x\n",
1057 cfg->desktop_shadow.red/257,
1058 cfg->desktop_shadow.green/257,
1059 cfg->desktop_shadow.blue/257);
1060 if(cfg->desktop_font && *cfg->desktop_font)
1061 g_string_append_printf(buf, "desktop_font=%s\n", cfg->desktop_font);
1062 if(cfg->folder)
1063 g_string_append_printf(buf, "folder=%s\n", cfg->folder);
1064 g_string_append_printf(buf, "show_wm_menu=%d\n", cfg->show_wm_menu);
1065 _save_sort(buf, cfg->desktop_sort_type, cfg->desktop_sort_by);
1066 #if FM_CHECK_VERSION(1, 2, 0)
1067 g_string_append_printf(buf, "show_documents=%d\n", cfg->show_documents);
1068 g_string_append_printf(buf, "show_trash=%d\n", cfg->show_trash);
1069 g_string_append_printf(buf, "show_mounts=%d\n", cfg->show_mounts);
1070 #endif
1071 }
1072
1073 static void _save_choice(gpointer key, gpointer val, gpointer buf)
1074 {
1075 FmAutorunChoice *choice = val;
1076
1077 if (!choice->dont_ask && !choice->last_used)
1078 return;
1079 g_string_append_printf(buf, "%s=%s%s\n", (char*)key,
1080 choice->dont_ask ? "*" : "",
1081 choice->last_used ? choice->last_used : "");
1082 }
1083
1084 void fm_app_config_save_profile(FmAppConfig* cfg, const char* name)
1085 {
1086 char* path = NULL;;
1087 char* dir_path;
1088
1089 if(!name || !*name)
1090 name = "default";
1091
1092 dir_path = g_build_filename(g_get_user_config_dir(), "pcmanfm", name, NULL);
1093 if(g_mkdir_with_parents(dir_path, 0700) != -1)
1094 {
1095 GString* buf = g_string_sized_new(1024);
1096
1097 g_string_append(buf, "[config]\n");
1098 g_string_append_printf(buf, "bm_open_method=%d\n", cfg->bm_open_method);
1099 /*if(cfg->su_cmd && *cfg->su_cmd)
1100 g_string_append_printf(buf, "su_cmd=%s\n", cfg->su_cmd);*/
1101 #if FM_CHECK_VERSION(1, 2, 0)
1102 if (cfg->home_path && cfg->home_path[0]
1103 && strcmp(cfg->home_path, fm_get_home_dir()) != 0)
1104 g_string_append_printf(buf, "home_path=%s\n", cfg->home_path);
1105 #endif
1106
1107 g_string_append(buf, "\n[volume]\n");
1108 g_string_append_printf(buf, "mount_on_startup=%d\n", cfg->mount_on_startup);
1109 g_string_append_printf(buf, "mount_removable=%d\n", cfg->mount_removable);
1110 g_string_append_printf(buf, "autorun=%d\n", cfg->autorun);
1111
1112 if (g_hash_table_size(cfg->autorun_choices) > 0)
1113 {
1114 g_string_append(buf, "\n[autorun]\n");
1115 g_hash_table_foreach(cfg->autorun_choices, _save_choice, buf);
1116 }
1117
1118 g_string_append(buf, "\n[ui]\n");
1119 g_string_append_printf(buf, "always_show_tabs=%d\n", cfg->always_show_tabs);
1120 g_string_append_printf(buf, "max_tab_chars=%d\n", cfg->max_tab_chars);
1121 /* g_string_append_printf(buf, "hide_close_btn=%d\n", cfg->hide_close_btn); */
1122 g_string_append_printf(buf, "win_width=%d\n", cfg->win_width);
1123 g_string_append_printf(buf, "win_height=%d\n", cfg->win_height);
1124 if (cfg->maximized)
1125 g_string_append(buf, "maximized=1\n");
1126 g_string_append_printf(buf, "splitter_pos=%d\n", cfg->splitter_pos);
1127 g_string_append_printf(buf, "media_in_new_tab=%d\n", cfg->media_in_new_tab);
1128 g_string_append_printf(buf, "desktop_folder_new_win=%d\n", cfg->desktop_folder_new_win);
1129 g_string_append_printf(buf, "change_tab_on_drop=%d\n", cfg->change_tab_on_drop);
1130 g_string_append_printf(buf, "close_on_unmount=%d\n", cfg->close_on_unmount);
1131 #if FM_CHECK_VERSION(1, 2, 0)
1132 g_string_append_printf(buf, "focus_previous=%d\n", cfg->focus_previous);
1133 g_string_append(buf, "side_pane_mode=");
1134 if (cfg->side_pane_mode & FM_SP_HIDE)
1135 g_string_append(buf, "hidden;");
1136 g_string_append_printf(buf, "%s\n",
1137 fm_side_pane_get_mode_name(cfg->side_pane_mode & FM_SP_MODE_MASK));
1138 #else
1139 g_string_append_printf(buf, "side_pane_mode=%d\n", cfg->side_pane_mode);
1140 #endif
1141 #if FM_CHECK_VERSION(1, 0, 2)
1142 g_string_append_printf(buf, "view_mode=%s\n", fm_standard_view_mode_to_str(cfg->view_mode));
1143 #else
1144 g_string_append_printf(buf, "view_mode=%d\n", cfg->view_mode);
1145 #endif
1146 g_string_append_printf(buf, "show_hidden=%d\n", cfg->show_hidden);
1147 _save_sort(buf, cfg->sort_type, cfg->sort_by);
1148 #if FM_CHECK_VERSION(1, 0, 2)
1149 if (cfg->columns && cfg->columns[0])
1150 {
1151 char **colptr;
1152
1153 g_string_append(buf, "columns=");
1154 for (colptr = cfg->columns; *colptr; colptr++)
1155 g_string_append_printf(buf, "%s;", *colptr);
1156 g_string_append_c(buf, '\n');
1157 }
1158 #endif
1159 g_string_append(buf, "toolbar=");
1160 if (!cfg->tb.visible)
1161 g_string_append(buf, "hidden;");
1162 if (cfg->tb.new_win)
1163 g_string_append(buf, "newwin;");
1164 if (cfg->tb.new_tab)
1165 g_string_append(buf, "newtab;");
1166 if (cfg->tb.nav)
1167 g_string_append(buf, "navigation;");
1168 if (cfg->tb.home)
1169 g_string_append(buf, "home;");
1170 g_string_append_c(buf, '\n');
1171 g_string_append_printf(buf, "show_statusbar=%d\n", cfg->show_statusbar);
1172 g_string_append_printf(buf, "pathbar_mode_buttons=%d\n", cfg->pathbar_mode_buttons);
1173
1174 path = g_build_filename(dir_path, "pcmanfm.conf", NULL);
1175 g_file_set_contents(path, buf->str, buf->len, NULL);
1176 g_free(path);
1177 g_string_free(buf, TRUE);
1178
1179 #if FM_CHECK_VERSION(1, 2, 0)
1180 /* libfm does not have any profile things */
1181 fm_folder_config_save_cache();
1182 #else
1183 fm_folder_config_save_cache(dir_path);
1184 #endif
1185 }
1186 g_free(dir_path);
1187 }
1188
1189 void fm_app_config_set_autorun_choice(FmAppConfig *cfg,
1190 const char *content_type,
1191 const char *app, gboolean dont_ask)
1192 {
1193 FmAutorunChoice *choice;
1194
1195 if (content_type == NULL)
1196 return;
1197 choice = g_hash_table_lookup(cfg->autorun_choices, content_type);
1198 if (choice)
1199 g_free(choice->last_used);
1200 else
1201 {
1202 choice = g_slice_new(FmAutorunChoice);
1203 g_hash_table_insert(cfg->autorun_choices, g_strdup(content_type), choice);
1204 }
1205 choice->last_used = g_strdup(app);
1206 choice->dont_ask = dont_ask;
1207 }
1208
1209 typedef struct
1210 {
1211 const char *name;
1212 FmWallpaperMode mode;
1213 } _WPModeDesc;
1214
1215 static const _WPModeDesc _wp_modes[] = {
1216 { "color", FM_WP_COLOR },
1217 { "stretch", FM_WP_STRETCH },
1218 { "fit", FM_WP_FIT },
1219 { "center", FM_WP_CENTER },
1220 { "tile", FM_WP_TILE },
1221 { "crop", FM_WP_CROP },
1222 { "screen", FM_WP_SCREEN }
1223 };
1224
1225 FmWallpaperMode fm_app_wallpaper_get_mode_by_name(const char *name)
1226 {
1227 guint i;
1228 if (name) for (i = 0; i < G_N_ELEMENTS(_wp_modes); i++)
1229 if (strcmp(_wp_modes[i].name, name) == 0)
1230 return _wp_modes[i].mode;
1231 return (FmWallpaperMode)-1;
1232 }
1233
1234 const char *fm_app_wallpaper_get_mode_name(FmWallpaperMode mode)
1235 {
1236 guint i;
1237 for (i = 0; i < G_N_ELEMENTS(_wp_modes); i++)
1238 if (_wp_modes[i].mode == mode)
1239 return _wp_modes[i].name;
1240 return NULL;
1241 }