2 paths to configuration files
4 Copyright (C) 2010 The Free Software Foundation, Inc.
7 Slava Zanko <slavazanko@gmail.com>, 2010.
9 This file is part of the Midnight Commander.
11 The Midnight Commander is free software; you can redistribute it
12 and/or modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 The Midnight Commander is distributed in the hope that it will be
17 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
18 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 #include "lib/global.h"
33 #include "lib/mcconfig.h"
34 #include "lib/fileloc.h"
35 #include "lib/vfs/mc-vfs/vfs.h"
37 /*** global variables ****************************************************************************/
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 /*** file scope variables ************************************************************************/
45 static gboolean xdg_vars_initialized
= FALSE
;
46 static char *xdg_config
= NULL
;
47 static char *xdg_cache
= NULL
;
48 static char *xdg_data
= NULL
;
50 static const char *homedir
= NULL
;
52 static gboolean config_dir_present
= FALSE
;
56 const char *old_filename
;
59 const char *new_filename
;
60 } mc_config_migrate_rules
[] =
64 { "ini", &xdg_config
, MC_CONFIG_FILE
},
65 { "filehighlight.ini", &xdg_config
, MC_FHL_INI_FILE
},
66 { "hotlist", &xdg_config
, MC_HOTLIST_FILE
},
67 { "mc.keymap", &xdg_config
, GLOBAL_KEYMAP_FILE
},
70 { "skins", &xdg_data
, MC_SKINS_SUBDIR
},
71 { "fish", &xdg_data
, FISH_PREFIX
},
72 { "bindings", &xdg_data
, MC_FILEBIND_FILE
},
73 { "menu", &xdg_data
, MC_USERMENU_FILE
},
74 { "bashrc", &xdg_data
, "bashrc"},
75 { "inputrc", &xdg_data
, "inputrc"},
76 { "extfs.d", &xdg_data
, MC_EXTFS_DIR
},
77 { "cedit" PATH_SEP_STR
"Syntax", &xdg_data
, EDIT_SYNTAX_FILE
},
78 { "cedit" PATH_SEP_STR
"menu", &xdg_data
, EDIT_HOME_MENU
},
79 { "cedit" PATH_SEP_STR
"edit.indent.rc", &xdg_data
, EDIT_DIR PATH_SEP_STR
"edit.indent.rc"},
80 { "cedit" PATH_SEP_STR
"edit.spell.rc", &xdg_data
, EDIT_DIR PATH_SEP_STR
"edit.spell.rc"},
83 { "history", &xdg_cache
, MC_HISTORY_FILE
},
84 { "panels.ini", &xdg_cache
, MC_PANELS_FILE
},
85 { "log", &xdg_cache
, "mc.log"},
86 { "filepos", &xdg_cache
, MC_FILEPOS_FILE
},
87 { "Tree", &xdg_cache
, MC_TREESTORE_FILE
},
88 { "cedit" PATH_SEP_STR
"cooledit.clip", &xdg_cache
, EDIT_CLIP_FILE
},
89 { "cedit" PATH_SEP_STR
"cooledit.temp", &xdg_cache
, EDIT_TEMP_FILE
},
90 { "cedit" PATH_SEP_STR
"cooledit.block", &xdg_cache
, EDIT_BLOCK_FILE
},
96 /*** file scope functions *********************************************************************** */
97 /* --------------------------------------------------------------------------------------------- */
100 mc_config_mkdir (const char *directory_name
, GError
** error
)
102 if ((!g_file_test (directory_name
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) &&
103 (g_mkdir_with_parents (directory_name
, 0700) != 0))
105 g_propagate_error (error
,
106 g_error_new (MC_ERROR
, 0, _("Cannot create %s directory"),
111 /* --------------------------------------------------------------------------------------------- */
114 mc_config_init_one_config_path (const char *path_base
, const char *subdir
, GError
** error
)
118 full_path
= g_build_filename (path_base
, subdir
, NULL
);
120 if (g_file_test (full_path
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
))
121 config_dir_present
= TRUE
;
123 mc_config_mkdir (full_path
, error
);
124 if (error
!= NULL
&& *error
!= NULL
)
132 /* --------------------------------------------------------------------------------------------- */
135 mc_config_get_deprecated_path (void)
137 return g_build_filename (mc_config_get_home_dir (), "." MC_USERCONF_DIR
, NULL
);
140 /* --------------------------------------------------------------------------------------------- */
143 mc_config_copy (const char *old_name
, const char *new_name
, GError
** error
)
145 if (error
!= NULL
&& *error
!= NULL
)
148 if (g_file_test (old_name
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_REGULAR
))
150 char *contents
= NULL
;
153 if (g_file_get_contents (old_name
, &contents
, &length
, error
))
154 g_file_set_contents (new_name
, contents
, length
, error
);
160 if (g_file_test (old_name
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
))
164 const char *dir_name
;
166 dir
= g_dir_open (old_name
, 0, error
);
170 if (!g_mkdir_with_parents (new_name
, 0700))
173 g_propagate_error (error
,
174 g_error_new (MC_ERROR
, 0,
176 ("An error occured while migrating user settings: %s"),
177 g_strerror (errno
)));
181 while ((dir_name
= g_dir_read_name (dir
)) != NULL
)
183 char *old_name2
, *new_name2
;
184 old_name2
= g_build_filename (old_name
, dir_name
, NULL
);
185 new_name2
= g_build_filename (new_name
, dir_name
, NULL
);
186 mc_config_copy (old_name2
, new_name2
, error
);
192 if (rename (old_name
, new_name
) != 0)
194 g_propagate_error (error
,
195 g_error_new (MC_ERROR
, 0,
197 ("An error occured while migrating user settings: %s"),
198 g_strerror (errno
)));
202 /* --------------------------------------------------------------------------------------------- */
203 /*** public functions ****************************************************************************/
204 /* --------------------------------------------------------------------------------------------- */
207 mc_config_init_config_paths (GError
** error
)
209 char *u_config_dir
= (char *) g_get_user_config_dir ();
210 char *u_data_dir
= (char *) g_get_user_data_dir ();
211 char *u_cache_dir
= (char *) g_get_user_cache_dir ();
213 if (xdg_vars_initialized
)
216 u_config_dir
= (u_config_dir
== NULL
)
217 ? g_build_filename (mc_config_get_home_dir (), ".config", NULL
) : g_strdup (u_config_dir
);
219 u_cache_dir
= (u_cache_dir
== NULL
)
220 ? g_build_filename (mc_config_get_home_dir (), ".cache", NULL
) : g_strdup (u_cache_dir
);
222 u_data_dir
= (u_data_dir
== NULL
)
223 ? g_build_filename (mc_config_get_home_dir (), ".local", "share", NULL
)
224 : g_strdup (u_data_dir
);
226 xdg_config
= mc_config_init_one_config_path (u_config_dir
, MC_USERCONF_DIR
, error
);
227 xdg_cache
= mc_config_init_one_config_path (u_cache_dir
, MC_USERCONF_DIR
, error
);
228 xdg_data
= mc_config_init_one_config_path (u_data_dir
, MC_USERCONF_DIR
, error
);
231 g_free (u_cache_dir
);
232 g_free (u_config_dir
);
233 xdg_vars_initialized
= TRUE
;
236 /* --------------------------------------------------------------------------------------------- */
239 mc_config_deinit_config_paths (void)
241 if (!xdg_vars_initialized
)
248 xdg_vars_initialized
= FALSE
;
251 /* --------------------------------------------------------------------------------------------- */
254 mc_config_get_data_path (void)
256 if (!xdg_vars_initialized
)
257 mc_config_init_config_paths (NULL
);
259 return (const char *) xdg_data
;
262 /* --------------------------------------------------------------------------------------------- */
265 mc_config_get_cache_path (void)
267 if (!xdg_vars_initialized
)
268 mc_config_init_config_paths (NULL
);
270 return (const char *) xdg_cache
;
273 /* --------------------------------------------------------------------------------------------- */
276 mc_config_get_home_dir (void)
280 homedir
= g_getenv ("HOME");
282 homedir
= g_get_home_dir ();
287 /* --------------------------------------------------------------------------------------------- */
290 mc_config_get_path (void)
292 if (!xdg_vars_initialized
)
293 mc_config_init_config_paths (NULL
);
295 return (const char *) xdg_config
;
298 /* --------------------------------------------------------------------------------------------- */
301 mc_config_migrate_from_old_place (GError
** error
)
303 char *old_dir
, *tmp_dir_name
;
306 old_dir
= mc_config_get_deprecated_path ();
308 tmp_dir_name
= mc_config_init_one_config_path (xdg_config
, EDIT_DIR
, error
);
309 g_free (tmp_dir_name
);
310 tmp_dir_name
= mc_config_init_one_config_path (xdg_cache
, EDIT_DIR
, error
);
311 g_free (tmp_dir_name
);
312 tmp_dir_name
= mc_config_init_one_config_path (xdg_data
, EDIT_DIR
, error
);
313 g_free (tmp_dir_name
);
315 for (rule_index
= 0; mc_config_migrate_rules
[rule_index
].old_filename
!= NULL
; rule_index
++)
317 char *old_name
, *new_name
;
320 g_build_filename (old_dir
, mc_config_migrate_rules
[rule_index
].old_filename
, NULL
);
322 if (!g_file_test (old_name
, G_FILE_TEST_EXISTS
))
328 new_name
= g_build_filename (*mc_config_migrate_rules
[rule_index
].new_basedir
,
329 mc_config_migrate_rules
[rule_index
].new_filename
, NULL
);
331 mc_config_copy (old_name
, new_name
, error
);
339 old_dir2 = g_strconcat (old_dir, "~", NULL);
340 rename (old_dir, old_dir2);
344 g_propagate_error (error
,
345 g_error_new (MC_ERROR
, 0,
347 ("Your old settings were migrated from %s\n"
348 "to Freedesktop recommended dirs.\n"
349 "To get more info, please visit\n"
350 "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
356 /* --------------------------------------------------------------------------------------------- */
359 mc_config_deprecated_dir_present (void)
361 char *old_dir
= mc_config_get_deprecated_path ();
362 gboolean is_present
= g_file_test (old_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
);
364 return is_present
&& !config_dir_present
;
367 /* --------------------------------------------------------------------------------------------- */