2 paths to configuration files
4 Copyright (C) 2010, 2011
5 The Free Software Foundation, Inc.
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/>.
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
;
57 const char *old_filename
;
60 const char *new_filename
;
61 } mc_config_migrate_rules
[] =
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
},
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"},
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
},
97 /*** file scope functions *********************************************************************** */
98 /* --------------------------------------------------------------------------------------------- */
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"),
112 /* --------------------------------------------------------------------------------------------- */
115 mc_config_init_one_config_path (const char *path_base
, const char *subdir
, GError
** error
)
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
)
133 /* --------------------------------------------------------------------------------------------- */
136 mc_config_get_deprecated_path (void)
138 return g_build_filename (mc_config_get_home_dir (), "." MC_USERCONF_DIR
, NULL
);
141 /* --------------------------------------------------------------------------------------------- */
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
;
151 if (g_file_get_contents (old_name
, &contents
, &length
, error
))
152 g_file_set_contents (new_name
, contents
, length
, error
);
158 if (g_file_test (old_name
, G_FILE_TEST_IS_DIR
))
162 const char *dir_name
;
164 dir
= g_dir_open (old_name
, 0, error
);
168 if (g_mkdir_with_parents (new_name
, 0700) == -1)
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
)));
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
);
192 /* --------------------------------------------------------------------------------------------- */
193 /*** public functions ****************************************************************************/
194 /* --------------------------------------------------------------------------------------------- */
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
)
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
);
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
);
232 mc_global
.sysconfig_dir
= g_strdup (SYSCONFDIR
);
234 mc_global
.share_data_dir
= g_strdup (DATADIR
);
236 xdg_vars_initialized
= TRUE
;
239 /* --------------------------------------------------------------------------------------------- */
242 mc_config_deinit_config_paths (void)
244 if (!xdg_vars_initialized
)
251 g_free (mc_global
.share_data_dir
);
252 g_free (mc_global
.sysconfig_dir
);
254 xdg_vars_initialized
= FALSE
;
257 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
282 mc_config_get_home_dir (void)
286 homedir
= g_getenv ("HOME");
288 homedir
= g_get_home_dir ();
293 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
307 mc_config_migrate_from_old_place (GError
** error
)
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
++)
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
))
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
);
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"),
351 /* --------------------------------------------------------------------------------------------- */
354 mc_config_deprecated_dir_present (void)
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
);
363 return is_present
&& !config_dir_present
;
366 /* --------------------------------------------------------------------------------------------- */