Move data directory creation/query code to ag_get_user_data_dir()

This commit is contained in:
Gergely Polonkai 2014-09-02 21:37:07 +02:00
parent 5f95ffd16e
commit 0cf6f171a6
3 changed files with 38 additions and 15 deletions

View File

@ -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,

View File

@ -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[])
{

View File

@ -1,6 +1,7 @@
#ifndef __ASTROGNOME_H__
#define __ASTROGNOME_H__
#include <gtk/gtk.h>
#include <swe-glib.h>
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