Example #1
0
/** @fn const gchar * gnc_dotgnucash_dir ()
 *  @brief Ensure that the user's configuration directory exists and is minimally populated.
 *
 *  Note that the default path is $HOME/.gnucash; This can be changed
 *  by the environment variable $GNC_DOT_DIR.
 *
 *  @return An absolute path to the configuration directory
 */
const gchar *
gnc_dotgnucash_dir (void)
{
    static gchar *dotgnucash = NULL;
    gchar *tmp_dir;

    if (dotgnucash)
        return dotgnucash;

    dotgnucash = g_strdup(g_getenv("GNC_DOT_DIR"));

    if (!dotgnucash)
    {
        const gchar *home = g_get_home_dir();
        if (!home)
        {
            g_warning("Cannot find home directory. Using tmp directory instead.");
            home = g_get_tmp_dir();
        }
        g_assert(home);

        dotgnucash = g_build_filename(home, ".gnucash", (gchar *)NULL);
    }
    gnc_validate_directory(dotgnucash);

    /* Since we're in code that is only executed once.... */
    tmp_dir = g_build_filename(dotgnucash, "books", (gchar *)NULL);
    gnc_validate_directory(tmp_dir);
    g_free(tmp_dir);
    tmp_dir = g_build_filename(dotgnucash, "checks", (gchar *)NULL);
    gnc_validate_directory(tmp_dir);
    g_free(tmp_dir);
    tmp_dir = g_build_filename(dotgnucash, "translog", (gchar *)NULL);
    gnc_validate_directory(tmp_dir);
    g_free(tmp_dir);

    return dotgnucash;
}
/** @fn const gchar * gnc_dotgnucash_dir ()
 *  @brief Ensure that the user's configuration directory exists and is minimally populated.
 *
 *  Note that the default path is $HOME/.gnucash; This can be changed
 *  by the environment variable $GNC_DOT_DIR.
 *
 *  @return An absolute path to the configuration directory
 */
const gchar *
gnc_dotgnucash_dir (void)
{
    static gchar *dotgnucash = NULL;
    gchar *tmp_dir;
    gchar *errmsg = NULL;

    if (dotgnucash)
        return dotgnucash;

    dotgnucash = g_strdup(g_getenv("GNC_DOT_DIR"));

    if (!dotgnucash)
    {
        const gchar *home = g_get_home_dir();
        if (!home || !gnc_validate_directory(home, FALSE, &errmsg))
        {
            g_free(errmsg);
            g_warning("Cannot find suitable home directory. Using tmp directory instead.");
            home = g_get_tmp_dir();
        }
        g_assert(home);

        dotgnucash = g_build_filename(home, ".gnucash", (gchar *)NULL);
    }
    if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
    {
        const gchar *tmp = g_get_tmp_dir();
        g_free(errmsg);
        g_free(dotgnucash);
        g_warning("Cannot find suitable .gnucash directory in home directory. Using tmp directory instead.");
        g_assert(tmp);

        dotgnucash = g_build_filename(tmp, ".gnucash", (gchar *)NULL);
        
        if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
            exit(1);
    }

    /* Since we're in code that is only executed once.... */
    tmp_dir = g_build_filename(dotgnucash, "books", (gchar *)NULL);
    if (!gnc_validate_directory(tmp_dir, TRUE, &errmsg))
        exit(1);
    g_free(tmp_dir);
    tmp_dir = g_build_filename(dotgnucash, "checks", (gchar *)NULL);
    if (!gnc_validate_directory(tmp_dir, TRUE, &errmsg))
        exit(1);
    g_free(tmp_dir);
    tmp_dir = g_build_filename(tmp_dir, "translog", (gchar *)NULL);
    if (!gnc_validate_directory(dotgnucash, TRUE, &errmsg))
        exit(1);
    g_free(tmp_dir);

    return dotgnucash;
}