diff --git a/src/ag-db.c b/src/ag-db.c index 60aee74..fb32e92 100644 --- a/src/ag-db.c +++ b/src/ag-db.c @@ -456,30 +456,17 @@ static void ag_db_init(AgDb *db) { GdaSqlParser *parser; - GFile *user_data_dir = g_file_new_for_path(g_get_user_data_dir()), - *ag_data_dir = g_file_get_child(user_data_dir, "astrognome"); + GFile *ag_data_dir = ag_get_user_data_dir(); AgDbPrivate *priv = ag_db_get_instance_private(db); gchar *path = g_file_get_path(ag_data_dir); GError *err = NULL; gda_init(); - if (!g_file_query_exists(ag_data_dir, NULL)) { - gchar *path = g_file_get_path(ag_data_dir); - - if (g_mkdir_with_parents(path, 0700) != 0) { - g_error( - "Data directory %s does not exist and can not be created.", - path - ); - } - } - priv->dsn = g_strdup_printf("SQLite://DB_DIR=%s;DB_NAME=charts", path); g_free(path); - g_object_unref(user_data_dir); - g_object_unref(ag_data_dir); + g_clear_object(&ag_data_dir); priv->conn = gda_connection_open_from_string( NULL, diff --git a/src/astrognome.c b/src/astrognome.c index b7bb512..bd2fe06 100644 --- a/src/astrognome.c +++ b/src/astrognome.c @@ -188,6 +188,40 @@ ag_house_system_nick_to_id(const gchar *nick) return GSWE_HOUSE_SYSTEM_NONE; } +/** + * ag_get_user_data_dir: + * + * Creates the astrognome data directory (~/.local/share/astrognome on XDG + * compatible systems) if necessary, and returns a GFile handle to it. + * + * Returns: (transfer full): a #GFile handle to the application data directory + * that must be freed with g_object_unref(). + */ +GFile * +ag_get_user_data_dir(void) +{ + GFile *user_data_dir = g_file_new_for_path(g_get_user_data_dir()), + *ag_data_dir = g_file_get_child(user_data_dir, "astrognome"); + + g_clear_object(&user_data_dir); + g_assert(ag_data_dir); + + if (!g_file_query_exists(ag_data_dir, NULL)) { + gchar *path = g_file_get_path(ag_data_dir); + + if (g_mkdir_with_parents(path, 0700) != 0) { + g_error( + "Data directory ā€˜%sā€™ does not exist and cannot be created.", + path + ); + } + + g_free(path); + } + + return ag_data_dir; +} + int main(int argc, char *argv[]) { diff --git a/src/astrognome.h b/src/astrognome.h index 80a1eeb..efcc1c4 100644 --- a/src/astrognome.h +++ b/src/astrognome.h @@ -1,6 +1,7 @@ #ifndef __ASTROGNOME_H__ #define __ASTROGNOME_H__ +#include #include typedef struct { @@ -34,6 +35,7 @@ enum { const gchar *ag_house_system_id_to_nick(GsweHouseSystem house_system); GsweHouseSystem ag_house_system_nick_to_id(const gchar *nick); +GFile *ag_get_user_data_dir(void); #ifndef GDOUBLE_FROM_LE inline static gdouble