Merge branch '2372_mcedit_utf8_fix'
[midnight-commander.git] / lib / mcconfig / paths.c
blob2feb3cba35f7a16d8dba67fc1880166a41091963
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 <errno.h>
31 #include "lib/global.h"
32 #include "lib/fileloc.h"
33 #include "lib/vfs/vfs.h"
34 #include "lib/util.h" /* unix_error_string() */
36 #include "lib/mcconfig.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 static gboolean xdg_vars_initialized = FALSE;
47 static char *xdg_config = NULL;
48 static char *xdg_cache = NULL;
49 static char *xdg_data = NULL;
51 static const char *homedir = NULL;
53 static gboolean config_dir_present = FALSE;
55 static const struct
57 const char *old_filename;
59 char **new_basedir;
60 const char *new_filename;
61 } mc_config_migrate_rules[] =
63 /* *INDENT-OFF* */
64 /* config */
65 { "ini", &xdg_config, MC_CONFIG_FILE},
66 { "filehighlight.ini", &xdg_config, MC_FHL_INI_FILE},
67 { "hotlist", &xdg_config, MC_HOTLIST_FILE},
68 { "mc.keymap", &xdg_config, GLOBAL_KEYMAP_FILE},
70 /* data */
71 { "skins", &xdg_data, MC_SKINS_SUBDIR},
72 { "fish", &xdg_data, FISH_PREFIX},
73 { "bindings", &xdg_data, MC_FILEBIND_FILE},
74 { "menu", &xdg_data, MC_USERMENU_FILE},
75 { "bashrc", &xdg_data, "bashrc"},
76 { "inputrc", &xdg_data, "inputrc"},
77 { "extfs.d", &xdg_data, MC_EXTFS_DIR},
78 { "cedit" PATH_SEP_STR "Syntax", &xdg_data, EDIT_SYNTAX_FILE},
79 { "cedit" PATH_SEP_STR "menu", &xdg_data, EDIT_HOME_MENU},
80 { "cedit" PATH_SEP_STR "edit.indent.rc", &xdg_data, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
81 { "cedit" PATH_SEP_STR "edit.spell.rc", &xdg_data, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
83 /* cache */
84 { "history", &xdg_cache, MC_HISTORY_FILE},
85 { "panels.ini", &xdg_cache, MC_PANELS_FILE},
86 { "log", &xdg_cache, "mc.log"},
87 { "filepos", &xdg_cache, MC_FILEPOS_FILE},
88 { "Tree", &xdg_cache, MC_TREESTORE_FILE},
89 { "cedit" PATH_SEP_STR "cooledit.clip", &xdg_cache, EDIT_CLIP_FILE},
90 { "cedit" PATH_SEP_STR "cooledit.temp", &xdg_cache, EDIT_TEMP_FILE},
91 { "cedit" PATH_SEP_STR "cooledit.block", &xdg_cache, EDIT_BLOCK_FILE},
93 {NULL, NULL, NULL}
94 /* *INDENT-ON* */
97 /*** file scope functions *********************************************************************** */
98 /* --------------------------------------------------------------------------------------------- */
100 static void
101 mc_config_mkdir (const char *directory_name, GError ** error)
103 if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
104 (g_mkdir_with_parents (directory_name, 0700) != 0))
106 g_propagate_error (error,
107 g_error_new (MC_ERROR, 0, _("Cannot create %s directory"),
108 directory_name));
112 /* --------------------------------------------------------------------------------------------- */
114 static char *
115 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** error)
117 char *full_path;
119 full_path = g_build_filename (path_base, subdir, NULL);
121 if (g_file_test (full_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
122 config_dir_present = TRUE;
124 mc_config_mkdir (full_path, error);
125 if (error != NULL && *error != NULL)
127 g_free (full_path);
128 full_path = NULL;
130 return full_path;
133 /* --------------------------------------------------------------------------------------------- */
135 static char *
136 mc_config_get_deprecated_path (void)
138 return g_build_filename (mc_config_get_home_dir (), "." MC_USERCONF_DIR, NULL);
141 /* --------------------------------------------------------------------------------------------- */
143 static void
144 mc_config_copy (const char *old_name, const char *new_name, GError ** error)
146 if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
148 char *contents = NULL;
149 size_t length;
151 if (g_file_get_contents (old_name, &contents, &length, error))
152 g_file_set_contents (new_name, contents, length, error);
154 g_free (contents);
155 return;
158 if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
161 GDir *dir;
162 const char *dir_name;
164 dir = g_dir_open (old_name, 0, error);
165 if (dir == NULL)
166 return;
168 if (g_mkdir_with_parents (new_name, 0700) == -1)
170 g_dir_close (dir);
171 g_propagate_error (error,
172 g_error_new (MC_ERROR, 0,
174 ("An error occured while migrating user settings: %s"),
175 unix_error_string (errno)));
176 return;
179 while ((dir_name = g_dir_read_name (dir)) != NULL)
181 char *old_name2, *new_name2;
183 old_name2 = g_build_filename (old_name, dir_name, NULL);
184 new_name2 = g_build_filename (new_name, dir_name, NULL);
185 mc_config_copy (old_name2, new_name2, error);
186 g_free (new_name2);
187 g_free (old_name2);
192 /* --------------------------------------------------------------------------------------------- */
193 /*** public functions ****************************************************************************/
194 /* --------------------------------------------------------------------------------------------- */
196 void
197 mc_config_init_config_paths (GError ** error)
199 const char *mc_datadir;
201 char *u_config_dir = (char *) g_get_user_config_dir ();
202 char *u_data_dir = (char *) g_get_user_data_dir ();
203 char *u_cache_dir = (char *) g_get_user_cache_dir ();
205 if (xdg_vars_initialized)
206 return;
208 u_config_dir = (u_config_dir == NULL)
209 ? g_build_filename (mc_config_get_home_dir (), ".config", NULL) : g_strdup (u_config_dir);
211 u_cache_dir = (u_cache_dir == NULL)
212 ? g_build_filename (mc_config_get_home_dir (), ".cache", NULL) : g_strdup (u_cache_dir);
214 u_data_dir = (u_data_dir == NULL)
215 ? g_build_filename (mc_config_get_home_dir (), ".local", "share", NULL)
216 : g_strdup (u_data_dir);
218 xdg_config = mc_config_init_one_config_path (u_config_dir, MC_USERCONF_DIR, error);
219 xdg_cache = mc_config_init_one_config_path (u_cache_dir, MC_USERCONF_DIR, error);
220 xdg_data = mc_config_init_one_config_path (u_data_dir, MC_USERCONF_DIR, error);
222 g_free (u_data_dir);
223 g_free (u_cache_dir);
224 g_free (u_config_dir);
226 /* This is the directory, where MC was installed, on Unix this is DATADIR */
227 /* and can be overriden by the MC_DATADIR environment variable */
228 mc_datadir = g_getenv ("MC_DATADIR");
229 if (mc_datadir != NULL)
230 mc_global.sysconfig_dir = g_strdup (mc_datadir);
231 else
232 mc_global.sysconfig_dir = g_strdup (SYSCONFDIR);
234 mc_global.share_data_dir = g_strdup (DATADIR);
236 xdg_vars_initialized = TRUE;
239 /* --------------------------------------------------------------------------------------------- */
241 void
242 mc_config_deinit_config_paths (void)
244 if (!xdg_vars_initialized)
245 return;
247 g_free (xdg_config);
248 g_free (xdg_cache);
249 g_free (xdg_data);
251 g_free (mc_global.share_data_dir);
252 g_free (mc_global.sysconfig_dir);
254 xdg_vars_initialized = FALSE;
257 /* --------------------------------------------------------------------------------------------- */
259 const char *
260 mc_config_get_data_path (void)
262 if (!xdg_vars_initialized)
263 mc_config_init_config_paths (NULL);
265 return (const char *) xdg_data;
268 /* --------------------------------------------------------------------------------------------- */
270 const char *
271 mc_config_get_cache_path (void)
273 if (!xdg_vars_initialized)
274 mc_config_init_config_paths (NULL);
276 return (const char *) xdg_cache;
279 /* --------------------------------------------------------------------------------------------- */
281 const char *
282 mc_config_get_home_dir (void)
284 if (homedir == NULL)
286 homedir = g_getenv ("HOME");
287 if (homedir == NULL)
288 homedir = g_get_home_dir ();
290 return homedir;
293 /* --------------------------------------------------------------------------------------------- */
295 const char *
296 mc_config_get_path (void)
298 if (!xdg_vars_initialized)
299 mc_config_init_config_paths (NULL);
301 return (const char *) xdg_config;
304 /* --------------------------------------------------------------------------------------------- */
306 void
307 mc_config_migrate_from_old_place (GError ** error)
309 char *old_dir;
310 size_t rule_index;
312 old_dir = mc_config_get_deprecated_path ();
314 g_free (mc_config_init_one_config_path (xdg_config, EDIT_DIR, error));
315 g_free (mc_config_init_one_config_path (xdg_cache, EDIT_DIR, error));
316 g_free (mc_config_init_one_config_path (xdg_data, EDIT_DIR, error));
318 for (rule_index = 0; mc_config_migrate_rules[rule_index].old_filename != NULL; rule_index++)
320 char *old_name;
322 old_name =
323 g_build_filename (old_dir, mc_config_migrate_rules[rule_index].old_filename, NULL);
325 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
327 char *new_name;
329 new_name = g_build_filename (*mc_config_migrate_rules[rule_index].new_basedir,
330 mc_config_migrate_rules[rule_index].new_filename, NULL);
332 mc_config_copy (old_name, new_name, error);
334 g_free (new_name);
336 g_free (old_name);
339 g_propagate_error (error,
340 g_error_new (MC_ERROR, 0,
342 ("Your old settings were migrated from %s\n"
343 "to Freedesktop recommended dirs.\n"
344 "To get more info, please visit\n"
345 "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
346 old_dir));
348 g_free (old_dir);
351 /* --------------------------------------------------------------------------------------------- */
353 gboolean
354 mc_config_deprecated_dir_present (void)
356 char *old_dir;
357 gboolean is_present;
359 old_dir = mc_config_get_deprecated_path ();
360 is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
361 g_free (old_dir);
363 return is_present && !config_dir_present;
366 /* --------------------------------------------------------------------------------------------- */