Added -g, --oldmouse option to support of NORMAL/BUTTON_EVENT mouse type.
[midnight-commander.git] / lib / mcconfig / paths.c
blob98d95a90fa6003cf8f9d4869df40730bf5d073e8
1 /*
2 paths to configuration files
4 Copyright (C) 2010 The Free Software Foundation, Inc.
6 Written by:
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,
24 MA 02110-1301, USA.
27 #include <config.h>
29 #include <stdio.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 /*** 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;
56 static const struct
58 const char *old_filename;
60 char **new_basedir;
61 const char *new_filename;
62 } mc_config_migrate_rules[] =
64 /* *INDENT-OFF* */
65 /* config */
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},
71 /* data */
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"},
84 /* cache */
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},
94 {NULL, NULL, NULL}
95 /* *INDENT-ON* */
98 /*** file scope functions *********************************************************************** */
99 /* --------------------------------------------------------------------------------------------- */
101 static void
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"),
109 directory_name));
113 /* --------------------------------------------------------------------------------------------- */
115 static char *
116 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** error)
118 char *full_path;
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)
128 g_free (full_path);
129 full_path = NULL;
131 return full_path;
134 /* --------------------------------------------------------------------------------------------- */
136 static char *
137 mc_config_get_deprecated_path (void)
139 return g_build_filename (mc_config_get_home_dir (), "." MC_USERCONF_DIR, NULL);
142 /* --------------------------------------------------------------------------------------------- */
144 static void
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;
150 size_t length;
152 if (g_file_get_contents (old_name, &contents, &length, error))
153 g_file_set_contents (new_name, contents, length, error);
155 g_free (contents);
156 return;
159 if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
162 GDir *dir;
163 const char *dir_name;
165 dir = g_dir_open (old_name, 0, error);
166 if (dir == NULL)
167 return;
169 if (g_mkdir_with_parents (new_name, 0700) == -1)
171 g_dir_close (dir);
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)));
177 return;
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);
187 g_free (new_name2);
188 g_free (old_name2);
193 /* --------------------------------------------------------------------------------------------- */
194 /*** public functions ****************************************************************************/
195 /* --------------------------------------------------------------------------------------------- */
197 void
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)
207 return;
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);
223 g_free (u_data_dir);
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);
232 else
233 mc_global.sysconfig_dir = g_strdup (SYSCONFDIR);
235 mc_global.share_data_dir = g_strdup (DATADIR);
237 xdg_vars_initialized = TRUE;
240 /* --------------------------------------------------------------------------------------------- */
242 void
243 mc_config_deinit_config_paths (void)
245 if (!xdg_vars_initialized)
246 return;
248 g_free (xdg_config);
249 g_free (xdg_cache);
250 g_free (xdg_data);
252 g_free (mc_global.share_data_dir);
253 g_free (mc_global.sysconfig_dir);
255 xdg_vars_initialized = FALSE;
258 /* --------------------------------------------------------------------------------------------- */
260 const char *
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 /* --------------------------------------------------------------------------------------------- */
271 const char *
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 /* --------------------------------------------------------------------------------------------- */
282 const char *
283 mc_config_get_home_dir (void)
285 if (homedir == NULL)
287 homedir = g_getenv ("HOME");
288 if (homedir == NULL)
289 homedir = g_get_home_dir ();
291 return homedir;
294 /* --------------------------------------------------------------------------------------------- */
296 const char *
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 /* --------------------------------------------------------------------------------------------- */
307 void
308 mc_config_migrate_from_old_place (GError ** error)
310 char *old_dir;
311 size_t rule_index;
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++)
321 char *old_name;
323 old_name =
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))
328 char *new_name;
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);
335 g_free (new_name);
337 g_free (old_name);
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"),
347 old_dir));
349 g_free (old_dir);
352 /* --------------------------------------------------------------------------------------------- */
354 gboolean
355 mc_config_deprecated_dir_present (void)
357 char *old_dir;
358 gboolean is_present;
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);
362 g_free (old_dir);
364 return is_present && !config_dir_present;
367 /* --------------------------------------------------------------------------------------------- */