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/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 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 static gboolean xdg_vars_initialized
= FALSE
;
48 static char *xdg_config
= NULL
;
49 static char *xdg_cache
= NULL
;
50 static char *xdg_data
= NULL
;
52 static const char *homedir
= NULL
;
54 static gboolean config_dir_present
= FALSE
;
58 const char *old_filename
;
61 const char *new_filename
;
62 } mc_config_migrate_rules
[] =
66 { "ini", &xdg_config
, MC_CONFIG_FILE
},
67 { "filehighlight.ini", &xdg_config
, MC_FHL_INI_FILE
},
68 { "hotlist", &xdg_config
, MC_HOTLIST_FILE
},
69 { "mc.keymap", &xdg_config
, GLOBAL_KEYMAP_FILE
},
72 { "skins", &xdg_data
, MC_SKINS_SUBDIR
},
73 { "fish", &xdg_data
, FISH_PREFIX
},
74 { "bindings", &xdg_data
, MC_FILEBIND_FILE
},
75 { "menu", &xdg_data
, MC_USERMENU_FILE
},
76 { "bashrc", &xdg_data
, "bashrc"},
77 { "inputrc", &xdg_data
, "inputrc"},
78 { "extfs.d", &xdg_data
, MC_EXTFS_DIR
},
79 { "cedit" PATH_SEP_STR
"Syntax", &xdg_data
, EDIT_SYNTAX_FILE
},
80 { "cedit" PATH_SEP_STR
"menu", &xdg_data
, EDIT_HOME_MENU
},
81 { "cedit" PATH_SEP_STR
"edit.indent.rc", &xdg_data
, EDIT_DIR PATH_SEP_STR
"edit.indent.rc"},
82 { "cedit" PATH_SEP_STR
"edit.spell.rc", &xdg_data
, EDIT_DIR PATH_SEP_STR
"edit.spell.rc"},
85 { "history", &xdg_cache
, MC_HISTORY_FILE
},
86 { "panels.ini", &xdg_cache
, MC_PANELS_FILE
},
87 { "log", &xdg_cache
, "mc.log"},
88 { "filepos", &xdg_cache
, MC_FILEPOS_FILE
},
89 { "Tree", &xdg_cache
, MC_TREESTORE_FILE
},
90 { "cedit" PATH_SEP_STR
"cooledit.clip", &xdg_cache
, EDIT_CLIP_FILE
},
91 { "cedit" PATH_SEP_STR
"cooledit.temp", &xdg_cache
, EDIT_TEMP_FILE
},
92 { "cedit" PATH_SEP_STR
"cooledit.block", &xdg_cache
, EDIT_BLOCK_FILE
},
98 /*** file scope functions *********************************************************************** */
99 /* --------------------------------------------------------------------------------------------- */
102 mc_config_mkdir (const char *directory_name
, GError
** error
)
104 if ((!g_file_test (directory_name
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) &&
105 (g_mkdir_with_parents (directory_name
, 0700) != 0))
107 g_propagate_error (error
,
108 g_error_new (MC_ERROR
, 0, _("Cannot create %s directory"),
113 /* --------------------------------------------------------------------------------------------- */
116 mc_config_init_one_config_path (const char *path_base
, const char *subdir
, GError
** error
)
120 full_path
= g_build_filename (path_base
, subdir
, NULL
);
122 if (g_file_test (full_path
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
))
123 config_dir_present
= TRUE
;
125 mc_config_mkdir (full_path
, error
);
126 if (error
!= NULL
&& *error
!= NULL
)
134 /* --------------------------------------------------------------------------------------------- */
137 mc_config_get_deprecated_path (void)
139 return g_build_filename (mc_config_get_home_dir (), "." MC_USERCONF_DIR
, NULL
);
142 /* --------------------------------------------------------------------------------------------- */
145 mc_config_copy (const char *old_name
, const char *new_name
, GError
** error
)
147 if (g_file_test (old_name
, G_FILE_TEST_IS_REGULAR
))
149 char *contents
= NULL
;
152 if (g_file_get_contents (old_name
, &contents
, &length
, error
))
153 g_file_set_contents (new_name
, contents
, length
, error
);
159 if (g_file_test (old_name
, G_FILE_TEST_IS_DIR
))
163 const char *dir_name
;
165 dir
= g_dir_open (old_name
, 0, error
);
169 if (g_mkdir_with_parents (new_name
, 0700) == -1)
172 g_propagate_error (error
,
173 g_error_new (MC_ERROR
, 0,
175 ("An error occured while migrating user settings: %s"),
176 unix_error_string (errno
)));
180 while ((dir_name
= g_dir_read_name (dir
)) != NULL
)
182 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
);
193 /* --------------------------------------------------------------------------------------------- */
194 /*** public functions ****************************************************************************/
195 /* --------------------------------------------------------------------------------------------- */
198 mc_config_init_config_paths (GError
** error
)
200 const char *mc_datadir
;
202 char *u_config_dir
= (char *) g_get_user_config_dir ();
203 char *u_data_dir
= (char *) g_get_user_data_dir ();
204 char *u_cache_dir
= (char *) g_get_user_cache_dir ();
206 if (xdg_vars_initialized
)
209 u_config_dir
= (u_config_dir
== NULL
)
210 ? g_build_filename (mc_config_get_home_dir (), ".config", NULL
) : g_strdup (u_config_dir
);
212 u_cache_dir
= (u_cache_dir
== NULL
)
213 ? g_build_filename (mc_config_get_home_dir (), ".cache", NULL
) : g_strdup (u_cache_dir
);
215 u_data_dir
= (u_data_dir
== NULL
)
216 ? g_build_filename (mc_config_get_home_dir (), ".local", "share", NULL
)
217 : g_strdup (u_data_dir
);
219 xdg_config
= mc_config_init_one_config_path (u_config_dir
, MC_USERCONF_DIR
, error
);
220 xdg_cache
= mc_config_init_one_config_path (u_cache_dir
, MC_USERCONF_DIR
, error
);
221 xdg_data
= mc_config_init_one_config_path (u_data_dir
, MC_USERCONF_DIR
, error
);
224 g_free (u_cache_dir
);
225 g_free (u_config_dir
);
227 /* This is the directory, where MC was installed, on Unix this is DATADIR */
228 /* and can be overriden by the MC_DATADIR environment variable */
229 mc_datadir
= g_getenv ("MC_DATADIR");
230 if (mc_datadir
!= NULL
)
231 mc_global
.sysconfig_dir
= g_strdup (mc_datadir
);
233 mc_global
.sysconfig_dir
= g_strdup (SYSCONFDIR
);
235 mc_global
.share_data_dir
= g_strdup (DATADIR
);
237 xdg_vars_initialized
= TRUE
;
240 /* --------------------------------------------------------------------------------------------- */
243 mc_config_deinit_config_paths (void)
245 if (!xdg_vars_initialized
)
252 g_free (mc_global
.share_data_dir
);
253 g_free (mc_global
.sysconfig_dir
);
255 xdg_vars_initialized
= FALSE
;
258 /* --------------------------------------------------------------------------------------------- */
261 mc_config_get_data_path (void)
263 if (!xdg_vars_initialized
)
264 mc_config_init_config_paths (NULL
);
266 return (const char *) xdg_data
;
269 /* --------------------------------------------------------------------------------------------- */
272 mc_config_get_cache_path (void)
274 if (!xdg_vars_initialized
)
275 mc_config_init_config_paths (NULL
);
277 return (const char *) xdg_cache
;
280 /* --------------------------------------------------------------------------------------------- */
283 mc_config_get_home_dir (void)
287 homedir
= g_getenv ("HOME");
289 homedir
= g_get_home_dir ();
294 /* --------------------------------------------------------------------------------------------- */
297 mc_config_get_path (void)
299 if (!xdg_vars_initialized
)
300 mc_config_init_config_paths (NULL
);
302 return (const char *) xdg_config
;
305 /* --------------------------------------------------------------------------------------------- */
308 mc_config_migrate_from_old_place (GError
** error
)
313 old_dir
= mc_config_get_deprecated_path ();
315 g_free (mc_config_init_one_config_path (xdg_config
, EDIT_DIR
, error
));
316 g_free (mc_config_init_one_config_path (xdg_cache
, EDIT_DIR
, error
));
317 g_free (mc_config_init_one_config_path (xdg_data
, EDIT_DIR
, error
));
319 for (rule_index
= 0; mc_config_migrate_rules
[rule_index
].old_filename
!= NULL
; rule_index
++)
324 g_build_filename (old_dir
, mc_config_migrate_rules
[rule_index
].old_filename
, NULL
);
326 if (g_file_test (old_name
, G_FILE_TEST_EXISTS
))
330 new_name
= g_build_filename (*mc_config_migrate_rules
[rule_index
].new_basedir
,
331 mc_config_migrate_rules
[rule_index
].new_filename
, NULL
);
333 mc_config_copy (old_name
, new_name
, error
);
340 g_propagate_error (error
,
341 g_error_new (MC_ERROR
, 0,
343 ("Your old settings were migrated from %s\n"
344 "to Freedesktop recommended dirs.\n"
345 "To get more info, please visit\n"
346 "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
352 /* --------------------------------------------------------------------------------------------- */
355 mc_config_deprecated_dir_present (void)
360 old_dir
= mc_config_get_deprecated_path ();
361 is_present
= g_file_test (old_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
);
364 return is_present
&& !config_dir_present
;
367 /* --------------------------------------------------------------------------------------------- */