Use xdg directories for data and config files

master
Yevgen Muntyan 2007-06-10 03:39:26 -05:00
parent 674e3e5b38
commit 92fbd94ee9
8 changed files with 234 additions and 97 deletions

View File

@ -34,7 +34,7 @@
</configure>
</optimized>
</configurations>
<file_selector_dir>/home/muntyan/projects/moo/moo/mooutils/</file_selector_dir>
<file_selector_dir>/home/muntyan/projects/moo/moo/mooapp/</file_selector_dir>
<run>
<args>--new-app --mode=project</args>
<exe>medit/medit</exe>

View File

@ -122,6 +122,9 @@ static void moo_app_exec_cmd_real (MooApp *app,
guint len);
static GtkWidget *moo_app_create_prefs_dialog (MooApp *app);
static void moo_app_load_prefs (MooApp *app);
static void moo_app_save_prefs (MooApp *app);
static void moo_app_set_name (MooApp *app,
const char *short_name,
const char *full_name);
@ -614,40 +617,6 @@ moo_app_get_input_pipe_name (G_GNUC_UNUSED MooApp *app)
}
const char *
moo_app_get_rc_file_name (MooApp *app,
MooPrefsType prefs_type)
{
g_return_val_if_fail (MOO_IS_APP (app), NULL);
g_return_val_if_fail (prefs_type < 2, NULL);
if (!app->priv->rc_files[prefs_type])
{
#ifdef __WIN32__
const char *templates[] = {"%s.ini", "%s.state"};
char *basename = g_strdup_printf (templates[prefs_type],
app->priv->info->short_name);
app->priv->rc_files[prefs_type] =
g_build_filename (g_get_user_config_dir (),
basename,
NULL);
g_free (basename);
#else
const char *templates[] = {".%src", ".%s.state"};
char *basename = g_strdup_printf (templates[prefs_type],
app->priv->info->short_name);
app->priv->rc_files[prefs_type] =
g_build_filename (g_get_home_dir (),
basename,
NULL);
g_free (basename);
#endif
}
return app->priv->rc_files[prefs_type];
}
char *
moo_app_create_user_data_dir (MooApp *app)
{
@ -819,24 +788,10 @@ moo_app_init_ui (MooApp *app)
static gboolean
moo_app_init_real (MooApp *app)
{
GError *error = NULL;
gdk_set_program_class (app->priv->info->full_name);
gtk_window_set_default_icon_name (app->priv->info->short_name);
if (!moo_prefs_load (moo_app_get_rc_file_name (app, MOO_PREFS_RC),
moo_app_get_rc_file_name (app, MOO_PREFS_STATE),
&error))
{
g_warning ("%s: could not read config file", G_STRLOC);
if (error)
{
g_warning ("%s: %s", G_STRLOC, error->message);
g_error_free (error);
}
}
moo_app_load_prefs (app);
moo_app_init_ui (app);
#ifdef MOO_BUILD_EDIT
@ -1043,26 +998,6 @@ moo_app_try_quit_real (MooApp *app)
}
static void
moo_app_save_prefs (MooApp *app)
{
GError *error = NULL;
if (!moo_prefs_save (moo_app_get_rc_file_name (app, MOO_PREFS_RC),
moo_app_get_rc_file_name (app, MOO_PREFS_STATE),
&error))
{
g_warning ("%s: could not save config file", G_STRLOC);
if (error)
{
g_warning ("%s: %s", G_STRLOC, error->message);
g_error_free (error);
}
}
}
static void
moo_app_quit_real (MooApp *app)
{
@ -1327,7 +1262,6 @@ moo_app_info_copy (const MooAppInfo *info)
copy->version = g_strdup (info->version);
copy->website = g_strdup (info->website);
copy->website_label = g_strdup (info->website_label);
copy->rc_file = g_strdup (info->rc_file);
copy->logo = g_strdup (info->logo);
copy->credits = g_strdup (info->credits);
@ -1346,7 +1280,6 @@ moo_app_info_free (MooAppInfo *info)
g_free (info->version);
g_free (info->website);
g_free (info->website_label);
g_free (info->rc_file);
g_free (info->logo);
g_free (info->credits);
g_free (info);
@ -1687,3 +1620,127 @@ moo_app_create_prefs_dialog (MooApp *app)
return GTK_WIDGET (dialog);
}
#ifndef __WIN32__
static void
move_rc_files (MooApp *app)
{
char *old_dir;
char *new_dir;
char *cache_dir;
old_dir = g_strdup_printf ("%s/.%s", g_get_home_dir (), g_get_prgname ());
new_dir = g_strdup_printf ("%s/%s", g_get_user_data_dir (), g_get_prgname ());
cache_dir = g_strdup_printf ("%s/%s", g_get_user_cache_dir (), g_get_prgname ());
/* do not be too clever here, there are way too many possible errors */
if (!g_file_test (new_dir, G_FILE_TEST_EXISTS) &&
g_file_test (old_dir, G_FILE_TEST_EXISTS) &&
_moo_rename (old_dir, new_dir) != 0)
{
_moo_set_user_data_dir (old_dir);
}
{
char *new_file;
char *old_file;
new_file = g_strdup_printf ("%s/%src", g_get_user_config_dir (), g_get_prgname ());
old_file = g_strdup_printf ("%s/.%src", g_get_home_dir (), g_get_prgname ());
if (!g_file_test (new_file, G_FILE_TEST_EXISTS) &&
g_file_test (old_file, G_FILE_TEST_EXISTS) &&
_moo_rename (old_file, new_file) != 0)
{
app->priv->rc_files[MOO_PREFS_RC] = old_file;
old_file = NULL;
}
else
{
app->priv->rc_files[MOO_PREFS_RC] = new_file;
new_file = NULL;
if (!g_file_test (g_get_user_config_dir (), G_FILE_TEST_EXISTS))
_moo_mkdir_with_parents (g_get_user_config_dir ());
}
g_free (old_file);
g_free (new_file);
}
if (!g_file_test (cache_dir, G_FILE_TEST_EXISTS))
_moo_mkdir_with_parents (cache_dir);
{
const char *new_file;
char *old_file = g_strdup_printf ("%s/.%s.state", g_get_home_dir (), g_get_prgname ());
app->priv->rc_files[MOO_PREFS_STATE] =
g_strdup_printf ("%s/%s.state", cache_dir, g_get_prgname ());
new_file = app->priv->rc_files[MOO_PREFS_STATE];
if (!g_file_test (new_file, G_FILE_TEST_EXISTS) &&
g_file_test (old_file, G_FILE_TEST_EXISTS))
{
_moo_rename (old_file, new_file);
}
g_free (old_file);
}
g_free (cache_dir);
g_free (new_dir);
g_free (old_dir);
}
#endif
static void
moo_app_load_prefs (MooApp *app)
{
GError *error = NULL;
#ifndef __WIN32__
move_rc_files (app);
#else
app->priv->rc_files[MOO_PREFS_RC] =
g_strdup_printf ("%s/%s.ini", g_get_user_config_dir (), g_get_prgname ());
app->priv->rc_files[MOO_PREFS_STATE] =
g_strdup_printf ("%s/%s.state", g_get_user_config_dir (), g_get_prgname ());
#endif
if (!moo_prefs_load (app->priv->rc_files[MOO_PREFS_RC],
app->priv->rc_files[MOO_PREFS_STATE],
&error))
{
g_warning ("%s: could not read config file", G_STRLOC);
if (error)
{
g_warning ("%s: %s", G_STRLOC, error->message);
g_error_free (error);
}
}
}
static void
moo_app_save_prefs (MooApp *app)
{
GError *error = NULL;
if (!moo_prefs_save (app->priv->rc_files[MOO_PREFS_RC],
app->priv->rc_files[MOO_PREFS_STATE],
&error))
{
g_warning ("%s: could not save config file", G_STRLOC);
if (error)
{
g_warning ("%s: %s", G_STRLOC, error->message);
g_error_free (error);
}
}
}

View File

@ -41,7 +41,6 @@ struct _MooAppInfo
char *version;
char *website;
char *website_label;
char *rc_file;
char *logo;
char *credits;
};
@ -86,8 +85,6 @@ void moo_app_set_exit_code (MooApp *app,
const MooAppInfo*moo_app_get_info (MooApp *app);
char *moo_app_create_user_data_dir (MooApp *app);
const char *moo_app_get_rc_file_name (MooApp *app,
MooPrefsType prefs_type);
const char *moo_app_get_input_pipe_name (MooApp *app);
MooEditor *moo_app_get_editor (MooApp *app);

View File

@ -64,15 +64,6 @@
;; (return-type "const-MooAppInfo*")
;; )
(define-method get_rc_file_name
(of-object "MooApp")
(c-name "moo_app_get_rc_file_name")
(return-type "const-char*")
(parameters
'("MooPrefsType" "prefs_type" (default "MOO_PREFS_RC"))
)
)
(define-method get_input_pipe_name
(of-object "MooApp")
(c-name "moo_app_get_input_pipe_name")

View File

@ -237,6 +237,13 @@ _moo_remove_dir (const char *path,
}
int
_moo_mkdir_with_parents (const char *path)
{
return g_mkdir_with_parents (path, S_IRWXU);
}
gboolean
_moo_create_dir (const char *path,
GError **error)

View File

@ -59,6 +59,7 @@ gboolean _moo_remove_dir (const char *path,
GError **error);
gboolean _moo_create_dir (const char *path,
GError **error);
int _moo_mkdir_with_parents (const char *path); /* S_IRWXU on unix */
gboolean _moo_rename_file (const char *path,
const char *new_path,
GError **error);

View File

@ -43,6 +43,10 @@
#endif
G_LOCK_DEFINE_STATIC (moo_user_data_dir);
static char *moo_user_data_dir;
#ifdef __WIN32__
static gboolean
@ -1154,14 +1158,43 @@ moo_get_prgname (void)
}
static char *
moo_get_user_cache_dir (void)
{
return g_strdup_printf ("%s/%s", g_get_user_cache_dir (), moo_get_prgname ());
}
char *
moo_get_user_data_dir (void)
{
G_LOCK (moo_user_data_dir);
if (!moo_user_data_dir)
{
#ifdef __WIN32__
return g_build_filename (g_get_home_dir (), moo_get_prgname (), NULL);
moo_user_data_dir = g_build_filename (g_get_home_dir (), moo_get_prgname (), NULL);
#else
return g_strdup_printf ("%s/.%s", g_get_home_dir (), moo_get_prgname ());
moo_user_data_dir = g_strdup_printf ("%s/%s", g_get_user_data_dir (), moo_get_prgname ());
#endif
}
G_UNLOCK (moo_user_data_dir);
return moo_user_data_dir;
}
void
_moo_set_user_data_dir (const char *path)
{
G_LOCK (moo_user_data_dir);
if (moo_user_data_dir)
g_critical ("%s: user data dir already set", G_STRLOC);
g_free (moo_user_data_dir);
moo_user_data_dir = g_strdup (path);
G_UNLOCK (moo_user_data_dir);
}
@ -1176,7 +1209,7 @@ moo_make_user_data_dir (const char *path)
g_return_val_if_fail (user_dir != NULL, FALSE);
full_path = g_build_filename (user_dir, path, NULL);
result = g_mkdir_with_parents (full_path, S_IRWXU);
result = _moo_mkdir_with_parents (full_path);
if (result != 0)
{
@ -1408,14 +1441,19 @@ moo_get_data_subdirs (const char *subdir,
}
char *
moo_get_user_data_file (const char *basename)
static char *
get_user_data_file (const char *basename,
gboolean cache)
{
char *dir, *file;
g_return_val_if_fail (basename && basename[0], NULL);
dir = moo_get_user_data_dir ();
if (cache)
dir = moo_get_user_cache_dir ();
else
dir = moo_get_user_data_dir ();
g_return_val_if_fail (dir != NULL, NULL);
file = g_build_filename (dir, basename, NULL);
@ -1424,6 +1462,18 @@ moo_get_user_data_file (const char *basename)
return file;
}
char *
moo_get_user_data_file (const char *basename)
{
return get_user_data_file (basename, FALSE);
}
char *
moo_get_user_cache_file (const char *basename)
{
return get_user_data_file (basename, TRUE);
}
static gboolean
save_with_backup (const char *filename,
@ -1495,11 +1545,12 @@ out:
return equal;
}
gboolean
moo_save_user_data_file (const char *basename,
const char *content,
gssize len,
GError **error)
static gboolean
save_user_data_file (const char *basename,
gboolean cache,
const char *content,
gssize len,
GError **error)
{
char *dir, *file;
gboolean result = FALSE;
@ -1510,8 +1561,17 @@ moo_save_user_data_file (const char *basename,
if (len < 0)
len = strlen (content);
dir = moo_get_user_data_dir ();
file = moo_get_user_data_file (basename);
if (cache)
{
dir = moo_get_user_cache_dir ();
file = moo_get_user_cache_file (basename);
}
else
{
dir = moo_get_user_data_dir ();
file = moo_get_user_data_file (basename);
}
g_return_val_if_fail (dir && file, FALSE);
if (!_moo_create_dir (dir, error))
@ -1534,6 +1594,24 @@ out:
return result;
}
gboolean
moo_save_user_data_file (const char *basename,
const char *content,
gssize len,
GError **error)
{
return save_user_data_file (basename, FALSE, content, len, error);
}
gboolean
moo_save_user_cache_file (const char *basename,
const char *content,
gssize len,
GError **error)
{
return save_user_data_file (basename, TRUE, content, len, error);
}
GType
moo_data_dir_type_get_type (void)

View File

@ -59,12 +59,18 @@ GType moo_data_dir_type_get_type (void) G_GNUC_CONST;
/* ~/.appname */
gboolean moo_make_user_data_dir (const char *path);
void _moo_set_user_data_dir (const char *path);
char *moo_get_user_data_dir (void);
char *moo_get_user_data_file (const char *basename);
gboolean moo_save_user_data_file (const char *basename,
const char *content,
gssize len,
GError **error);
char *moo_get_user_cache_file (const char *basename);
gboolean moo_save_user_cache_file (const char *basename,
const char *content,
gssize len,
GError **error);
/* user data comes first; MOO_DATA_DIR comes last */
/* $MOO_APP_DIR:$MOO_DATA_DIRS:$prefix/share/appname or