Added macros for Layout and Misc sections of main config file.
[midnight-commander.git] / lib / mcconfig / paths.c
blob918c11bc062655ca2a4e7482b7169a478b0781e5
1 /*
2 paths to configuration files
4 Copyright (C) 2010, 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2010.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
32 #include "lib/global.h"
33 #include "lib/fileloc.h"
34 #include "lib/vfs/vfs.h"
35 #include "lib/util.h" /* unix_error_string() */
37 #include "lib/mcconfig.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 #define MC_OLD_USERCONF_DIR ".mc"
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 static gboolean xdg_vars_initialized = FALSE;
50 static char *mc_config_str = NULL;
51 static char *mc_cache_str = NULL;
52 static char *mc_data_str = NULL;
54 static const char *homedir = NULL;
56 static gboolean config_dir_present = FALSE;
58 static const struct
60 const char *old_filename;
62 char **new_basedir;
63 const char *new_filename;
64 } mc_config_files_reference[] =
66 /* *INDENT-OFF* */
67 /* config */
68 { "ini", &mc_config_str, MC_CONFIG_FILE},
69 { "filehighlight.ini", &mc_config_str, MC_FHL_INI_FILE},
70 { "hotlist", &mc_config_str, MC_HOTLIST_FILE},
71 { "mc.keymap", &mc_config_str, GLOBAL_KEYMAP_FILE},
72 { "menu", &mc_config_str, MC_USERMENU_FILE},
73 { "cedit" PATH_SEP_STR "Syntax", &mc_config_str, EDIT_SYNTAX_FILE},
74 { "cedit" PATH_SEP_STR "menu", &mc_config_str, EDIT_HOME_MENU},
75 { "cedit" PATH_SEP_STR "edit.indent.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
76 { "cedit" PATH_SEP_STR "edit.spell.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
77 { "panels.ini", &mc_config_str, MC_PANELS_FILE},
79 /* User should move this file with applying some changes in file */
80 { "", &mc_config_str, MC_FILEBIND_FILE},
82 /* data */
83 { "skins", &mc_data_str, MC_SKINS_SUBDIR},
84 { "fish", &mc_data_str, FISH_PREFIX},
85 { "bashrc", &mc_data_str, "bashrc"},
86 { "inputrc", &mc_data_str, "inputrc"},
87 { "extfs.d", &mc_data_str, MC_EXTFS_DIR},
88 { "history", &mc_data_str, MC_HISTORY_FILE},
89 { "filepos", &mc_data_str, MC_FILEPOS_FILE},
90 { "cedit" PATH_SEP_STR "cooledit.clip", &mc_data_str, EDIT_CLIP_FILE},
91 { "", &mc_data_str, MC_MACRO_FILE},
93 /* cache */
94 { "log", &mc_cache_str, "mc.log"},
95 { "Tree", &mc_cache_str, MC_TREESTORE_FILE},
96 { "cedit" PATH_SEP_STR "cooledit.temp", &mc_cache_str, EDIT_TEMP_FILE},
97 { "cedit" PATH_SEP_STR "cooledit.block", &mc_cache_str, EDIT_BLOCK_FILE},
99 {NULL, NULL, NULL}
100 /* *INDENT-ON* */
103 #ifdef MC_HOMEDIR_XDG
104 static const struct
106 char **old_basedir;
107 const char *filename;
109 char **new_basedir;
110 } mc_config_migrate_rules_fix[] =
112 /* *INDENT-OFF* */
113 { &mc_data_str, MC_USERMENU_FILE, &mc_config_str},
114 { &mc_data_str, EDIT_SYNTAX_FILE, &mc_config_str},
115 { &mc_data_str, EDIT_HOME_MENU, &mc_config_str},
116 { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc", &mc_config_str},
117 { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc", &mc_config_str},
118 { &mc_data_str, MC_FILEBIND_FILE, &mc_config_str},
120 { &mc_cache_str, MC_HISTORY_FILE, &mc_data_str},
121 { &mc_cache_str, MC_FILEPOS_FILE, &mc_data_str},
122 { &mc_cache_str, EDIT_CLIP_FILE, &mc_data_str},
124 { &mc_cache_str, MC_PANELS_FILE, &mc_config_str},
126 {NULL, NULL, NULL}
127 /* *INDENT-ON* */
129 #endif /* MC_HOMEDIR_XDG */
131 /*** file scope functions *********************************************************************** */
132 /* --------------------------------------------------------------------------------------------- */
134 static void
135 mc_config_mkdir (const char *directory_name, GError ** error)
137 if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
138 (g_mkdir_with_parents (directory_name, 0700) != 0))
140 g_propagate_error (error,
141 g_error_new (MC_ERROR, 0, _("Cannot create %s directory"),
142 directory_name));
146 /* --------------------------------------------------------------------------------------------- */
148 static char *
149 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** error)
151 char *full_path;
153 full_path = g_build_filename (path_base, subdir, NULL);
155 if (g_file_test (full_path, G_FILE_TEST_EXISTS))
157 if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
159 config_dir_present = TRUE;
161 else
163 fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
164 exit (EXIT_FAILURE);
168 mc_config_mkdir (full_path, error);
169 if (error != NULL && *error != NULL)
171 g_free (full_path);
172 full_path = NULL;
174 return full_path;
177 /* --------------------------------------------------------------------------------------------- */
179 static char *
180 mc_config_get_deprecated_path (void)
182 return g_build_filename (mc_config_get_home_dir (), MC_OLD_USERCONF_DIR, NULL);
185 /* --------------------------------------------------------------------------------------------- */
187 static void
188 mc_config_copy (const char *old_name, const char *new_name, GError ** error)
190 if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
192 char *contents = NULL;
193 size_t length;
195 if (g_file_get_contents (old_name, &contents, &length, error))
196 g_file_set_contents (new_name, contents, length, error);
198 g_free (contents);
199 return;
202 if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
205 GDir *dir;
206 const char *dir_name;
208 dir = g_dir_open (old_name, 0, error);
209 if (dir == NULL)
210 return;
212 if (g_mkdir_with_parents (new_name, 0700) == -1)
214 g_dir_close (dir);
215 g_propagate_error (error,
216 g_error_new (MC_ERROR, 0,
218 ("An error occured while migrating user settings: %s"),
219 unix_error_string (errno)));
220 return;
223 while ((dir_name = g_dir_read_name (dir)) != NULL)
225 char *old_name2, *new_name2;
227 old_name2 = g_build_filename (old_name, dir_name, NULL);
228 new_name2 = g_build_filename (new_name, dir_name, NULL);
229 mc_config_copy (old_name2, new_name2, error);
230 g_free (new_name2);
231 g_free (old_name2);
236 /* --------------------------------------------------------------------------------------------- */
238 #if MC_HOMEDIR_XDG
239 static void
240 mc_config_fix_migrated_rules (void)
242 size_t rule_index;
244 for (rule_index = 0; mc_config_migrate_rules_fix[rule_index].old_basedir != NULL; rule_index++)
246 char *old_name;
248 old_name =
249 g_build_filename (*mc_config_migrate_rules_fix[rule_index].old_basedir,
250 mc_config_migrate_rules_fix[rule_index].filename, NULL);
252 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
254 char *new_name;
255 const char *basedir = *mc_config_migrate_rules_fix[rule_index].new_basedir;
256 const char *filename = mc_config_migrate_rules_fix[rule_index].filename;
258 new_name = g_build_filename (basedir, filename, NULL);
259 rename (old_name, new_name);
260 g_free (new_name);
262 g_free (old_name);
265 #endif /* MC_HOMEDIR_XDG */
267 /* --------------------------------------------------------------------------------------------- */
268 /*** public functions ****************************************************************************/
269 /* --------------------------------------------------------------------------------------------- */
271 void
272 mc_config_init_config_paths (GError ** error)
274 char *dir;
276 if (xdg_vars_initialized)
277 return;
279 #ifdef MC_HOMEDIR_XDG
280 dir = g_build_filename (mc_config_get_home_dir (), ".config", (char *) NULL);
281 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
282 g_free (dir);
284 dir = g_build_filename (mc_config_get_home_dir (), ".cache", (char *) NULL);
285 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
286 g_free (dir);
288 dir = g_build_filename (mc_config_get_home_dir (), ".local", "share", (char *) NULL);
289 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, error);
290 g_free (dir);
292 mc_config_fix_migrated_rules ();
293 #else /* MC_HOMEDIR_XDG */
294 char *defined_userconf_dir;
296 defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
297 if (g_path_is_absolute (defined_userconf_dir))
298 dir = defined_userconf_dir;
299 else
301 g_free (defined_userconf_dir);
302 dir = g_build_filename (mc_config_get_home_dir (), MC_USERCONF_DIR, (char *) NULL);
305 mc_data_str = mc_cache_str = mc_config_str =
306 mc_config_init_one_config_path (dir, "", error);
308 g_free (dir);
309 #endif /* MC_HOMEDIR_XDG */
311 xdg_vars_initialized = TRUE;
314 /* --------------------------------------------------------------------------------------------- */
316 void
317 mc_config_deinit_config_paths (void)
319 if (!xdg_vars_initialized)
320 return;
322 g_free (mc_config_str);
323 #ifdef MC_HOMEDIR_XDG
324 g_free (mc_cache_str);
325 g_free (mc_data_str);
326 #endif /* MC_HOMEDIR_XDG */
328 g_free (mc_global.share_data_dir);
329 g_free (mc_global.sysconfig_dir);
331 xdg_vars_initialized = FALSE;
334 /* --------------------------------------------------------------------------------------------- */
336 const char *
337 mc_config_get_data_path (void)
339 if (!xdg_vars_initialized)
340 mc_config_init_config_paths (NULL);
342 return (const char *) mc_data_str;
345 /* --------------------------------------------------------------------------------------------- */
347 const char *
348 mc_config_get_cache_path (void)
350 if (!xdg_vars_initialized)
351 mc_config_init_config_paths (NULL);
353 return (const char *) mc_cache_str;
356 /* --------------------------------------------------------------------------------------------- */
358 const char *
359 mc_config_get_home_dir (void)
361 if (homedir == NULL)
363 homedir = g_getenv ("MC_HOME");
364 if (homedir == NULL || *homedir == '\0')
365 homedir = g_getenv ("HOME");
366 if (homedir == NULL || *homedir == '\0')
367 homedir = g_get_home_dir ();
369 return homedir;
372 /* --------------------------------------------------------------------------------------------- */
374 const char *
375 mc_config_get_path (void)
377 if (!xdg_vars_initialized)
378 mc_config_init_config_paths (NULL);
380 return (const char *) mc_config_str;
383 /* --------------------------------------------------------------------------------------------- */
385 void
386 mc_config_migrate_from_old_place (GError ** error)
388 char *old_dir;
389 size_t rule_index;
391 old_dir = mc_config_get_deprecated_path ();
393 g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, error));
394 #ifdef MC_HOMEDIR_XDG
395 g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, error));
396 g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, error));
397 #endif /* MC_HOMEDIR_XDG */
399 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
401 char *old_name;
402 if (*mc_config_files_reference[rule_index].old_filename == '\0')
403 continue;
405 old_name =
406 g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename, NULL);
408 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
410 char *new_name;
411 const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
412 const char *filename = mc_config_files_reference[rule_index].new_filename;
414 new_name = g_build_filename (basedir, filename, NULL);
415 mc_config_copy (old_name, new_name, error);
416 g_free (new_name);
418 g_free (old_name);
421 #ifdef MC_HOMEDIR_XDG
422 g_propagate_error (error,
423 g_error_new (MC_ERROR, 0,
425 ("Your old settings were migrated from %s\n"
426 "to Freedesktop recommended dirs.\n"
427 "To get more info, please visit\n"
428 "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
429 old_dir));
430 #else /* MC_HOMEDIR_XDG */
431 g_propagate_error (error,
432 g_error_new (MC_ERROR, 0,
434 ("Your old settings were migrated from %s\n"
435 "to %s\n"), old_dir, mc_config_str));
436 #endif /* MC_HOMEDIR_XDG */
438 g_free (old_dir);
441 /* --------------------------------------------------------------------------------------------- */
443 gboolean
444 mc_config_deprecated_dir_present (void)
446 char *old_dir;
447 gboolean is_present;
449 old_dir = mc_config_get_deprecated_path ();
450 is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
451 g_free (old_dir);
453 return is_present && !config_dir_present;
456 /* --------------------------------------------------------------------------------------------- */
458 * Get full path to config file by short name.
460 * @param config_name short name
461 * @return full path to config file
464 char *
465 mc_config_get_full_path (const char *config_name)
467 size_t rule_index;
469 if (config_name == NULL)
470 return NULL;
472 if (!xdg_vars_initialized)
473 mc_config_init_config_paths (NULL);
475 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
477 if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
479 return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
480 mc_config_files_reference[rule_index].new_filename, NULL);
483 return NULL;
486 /* --------------------------------------------------------------------------------------------- */
488 * Get full path to config file by short name.
490 * @param config_name short name
491 * @return object with full path to config file
494 vfs_path_t *
495 mc_config_get_full_vpath (const char *config_name)
497 vfs_path_t *ret_vpath;
498 char *str_path;
500 str_path = mc_config_get_full_path (config_name);
502 ret_vpath = vfs_path_from_str(str_path);
503 g_free (str_path);
504 return ret_vpath;
507 /* --------------------------------------------------------------------------------------------- */