Beispiel #1
0
bool
QofXmlBackendProvider::type_check (const char *uri)
{
    GStatBuf sbuf;
    int rc;
    FILE* t;
    gchar* filename;
    QofBookFileType xml_type;
    gboolean result;

    if (!uri)
    {
        return FALSE;
    }

    filename = gnc_uri_get_path (uri);
    if (0 == g_strcmp0 (filename, QOF_STDOUT))
    {
        result = FALSE;
        goto det_exit;
    }
    t = g_fopen (filename, "r");
    if (!t)
    {
        PINFO (" new file");
        result = TRUE;
        goto det_exit;
    }
    fclose (t);
    rc = g_stat (filename, &sbuf);
    if (rc < 0)
    {
        result = FALSE;
        goto det_exit;
    }
    if (sbuf.st_size == 0)
    {
        PINFO (" empty file");
        result = TRUE;
        goto det_exit;
    }
    xml_type = gnc_is_xml_data_file_v2 (filename, NULL);
    if ((xml_type == GNC_BOOK_XML2_FILE) ||
        (xml_type == GNC_BOOK_XML1_FILE) ||
        (xml_type == GNC_BOOK_POST_XML2_0_0_FILE))
    {
        result = TRUE;
        goto det_exit;
    }
    PINFO (" %s is not a gnc XML file", filename);
    result = FALSE;

det_exit:
    g_free (filename);
    return result;
}
Beispiel #2
0
/**************************************************
 * csv_export_file_chooser_confirm_cb
 *
 * call back for ok button in file chooser widget
 **************************************************/
void
csv_export_file_chooser_confirm_cb (GtkWidget *button, CsvExportInfo *info)
{
    GtkAssistant *assistant = GTK_ASSISTANT(info->window);
    gint num = gtk_assistant_get_current_page (assistant);
    GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);

    gchar *file_name;

    gtk_assistant_set_page_complete (assistant, page, FALSE);

    file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(info->file_chooser));

    if (file_name)
    {
        if (g_file_test (file_name, G_FILE_TEST_EXISTS))
        {
            const char *format = _("The file %s already exists. "
                                   "Are you sure you want to overwrite it?");

            /* if user says cancel, we should break out */
            if (!gnc_verify_dialog (NULL, FALSE, format, file_name))
                return;
        }

        info->file_name = g_strdup (file_name);
        gtk_assistant_set_page_complete (assistant, page, TRUE);
    }

    if (file_name)
    {
        gchar *filepath = gnc_uri_get_path (file_name);
        gchar *filedir = g_path_get_dirname (filepath);
        info->starting_dir = g_strdup (filedir);
        g_free (filedir);
        g_free (filepath);
    }
    g_free (file_name);

    DEBUG("file_name selected is %s", info->file_name);
    DEBUG("starting directory is %s", info->starting_dir);

    /* Step to next page if page is complete */
    if(gtk_assistant_get_page_complete (assistant, page))
        gtk_assistant_set_current_page (assistant, num + 1);
}
/**************************************************
 * csv_file_chooser_confirm_cb
 *
 * call back for ok button in file chooser widget
 **************************************************/
void
csv_import_file_chooser_confirm_cb (GtkWidget *button, CsvImportInfo *info)
{
    GtkAssistant *assistant = GTK_ASSISTANT(info->window);
    gint num = gtk_assistant_get_current_page (assistant);
    GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);

    gchar *file_name;
    csv_import_result res;

    gtk_assistant_set_page_complete (assistant, page, FALSE);

    file_name = gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(info->file_chooser ));

    if (file_name)
    {
        gchar *filepath = gnc_uri_get_path ( file_name );
        gchar *filedir = g_path_get_dirname( filepath );
        info->starting_dir = g_strdup(filedir);
        g_free ( filedir );
        g_free ( filepath );

        info->file_name = g_strdup(file_name);

        // generate preview
        gtk_list_store_clear (info->store);
        res = csv_import_read_file (info->file_name, info->regexp->str, info->store, 1 );
        if (res == RESULT_OPEN_FAILED)
            gnc_error_dialog (info->window, _("The input file can not be opened."));
        else if (res == RESULT_OK)
            gtk_assistant_set_page_complete (assistant, page, TRUE);
        else if (res == MATCH_FOUND)
            gtk_assistant_set_page_complete (assistant, page, TRUE);
    }
    g_free(file_name);

    DEBUG("file_name selected is %s", info->file_name);
    DEBUG("starting directory is %s", info->starting_dir);

    /* Step to next page if page is complete */
    if(gtk_assistant_get_page_complete(assistant, page))
        gtk_assistant_set_current_page (assistant, num + 1);

}
Beispiel #4
0
/*
 * Checks to see whether the file is an sqlite file or not
 *1980
 */
template<> bool
QofDbiBackendProvider<DbType::DBI_SQLITE>::type_check(const char *uri)
{
    FILE* f;
    gchar buf[50];
    G_GNUC_UNUSED size_t chars_read;
    gint status;
    gchar* filename;

    // BAD if the path is null
    g_return_val_if_fail (uri != nullptr, FALSE);

    filename = gnc_uri_get_path (uri);
    f = g_fopen (filename, "r");
    g_free (filename);

    // OK if the file doesn't exist - new file
    if (f == nullptr)
    {
        PINFO ("doesn't exist (errno=%d) -> DBI", errno);
        return TRUE;
    }

    // OK if file has the correct header
    chars_read = fread (buf, sizeof (buf), 1, f);
    status = fclose (f);
    if (status < 0)
    {
        PERR ("Error in fclose(): %d\n", errno);
    }
    if (g_str_has_prefix (buf, "SQLite format 3"))
    {
        PINFO ("has SQLite format string -> DBI");
        return TRUE;
    }
    PINFO ("exists, does not have SQLite format string -> not DBI");

    // Otherwise, BAD
    return FALSE;
}
Beispiel #5
0
static void
gnc_state_set_base (const QofSession *session)
{
    gchar *basename, *original = NULL, *filename, *file_guid;
    gchar *sf_extension = NULL, *newstyle_filename = NULL;
    const gchar *uri;
    gchar guid_string[GUID_ENCODING_LENGTH+1];
    QofBook *book;
    const GncGUID *guid;
    GKeyFile *key_file = NULL;
    gint i;

    /* Reset filenames possibly found in a previous run */
    g_free (state_file_name);
    g_free (state_file_name_pre_241);
    state_file_name = NULL;
    state_file_name_pre_241 = NULL;

    uri = qof_session_get_url(session);
    ENTER("session %p (%s)", session, uri ? uri : "(null)");
    if (!uri)
    {
        LEAVE("no uri, nothing to do");
        return;
    }

    /* Get the book GncGUID */
    book = qof_session_get_book(session);
    guid = qof_entity_get_guid(QOF_INSTANCE(book));
    guid_to_string_buff(guid, guid_string);

    if (gnc_uri_is_file_uri (uri))
    {
        /* The book_uri is a true file, use its basename. */
        gchar *path = gnc_uri_get_path (uri);
        basename = g_path_get_basename (path);
        g_free (path);
    }
    else
    {
        /* The book_uri is composed of database connection parameters. */
        gchar* protocol = NULL;
        gchar* host = NULL;
        gchar* dbname = NULL;
        gchar* username = NULL;
        gchar* password = NULL;
        gint portnum = 0;
        gnc_uri_get_components (uri, &protocol, &host, &portnum,
                                &username, &password, &dbname);

        basename = g_strjoin ("_", protocol, host, username, dbname, NULL);
        g_free (protocol);
        g_free (host);
        g_free (username);
        g_free (password);
        g_free (dbname);
    }

    DEBUG ("Basename %s", basename);
    original = gnc_build_book_path (basename);
    g_free (basename);
    DEBUG ("Original %s", original);

    sf_extension = g_strdup (STATE_FILE_EXT);
    i = 1;
    while (1)
    {
        if (i == 1)
            filename = g_strconcat (original, sf_extension, NULL);
        else
            filename = g_strdup_printf ("%s_%d%s", original, i, sf_extension);
        DEBUG ("Trying %s", filename);
        key_file = gnc_key_file_load_from_file (filename, TRUE, FALSE, NULL);
        DEBUG ("Result %p", key_file);

        if (!key_file)
        {
            DEBUG ("No key file by that name");
            if (g_strcmp0 (sf_extension, STATE_FILE_EXT) == 0)
            {
                DEBUG ("Trying old state file names for compatibility");
                i = 1;
                g_free (sf_extension);
                sf_extension = g_strdup ("");

                /* Regardless of whether or not an old state file is found,
                 * the currently tested name should be used for the future
                 * state file.
                 */
                state_file_name = filename;
                continue;
            }

            /* No old style file found. We'll return with the new file name
             * we set earlier, and no existing key file. */
            g_free (filename);
            break;
        }

        file_guid = g_key_file_get_string (key_file,
                                           STATE_FILE_TOP, STATE_FILE_BOOK_GUID,
                                           NULL);
        DEBUG ("File GncGUID is %s", file_guid ? file_guid : "<not found>");
        if (g_strcmp0 (guid_string, file_guid) == 0)
        {
            DEBUG ("Matched !!!");
            /* Save the found file for later use. Which name to save to
             * depends on whether it was an old or new style file name
             */
            if (g_strcmp0 (sf_extension, STATE_FILE_EXT) == 0)
                state_file_name = filename;
            else
                state_file_name_pre_241 = filename;

            g_free (file_guid);
            break;
        }
        DEBUG ("Clean up this pass");
        g_free (file_guid);
        g_key_file_free (key_file);
        g_free (filename);
        i++;
    }

    DEBUG("Clean up");
    g_free(sf_extension);
    g_free(original);
    g_key_file_free (key_file);

    LEAVE ();
}
gboolean
gnc_xml_convert_single_file (const gchar *filename)
{
    GncXmlImportData *data;
    GtkWidget *widget;
    GtkBuilder *builder;
    gboolean success;

    data = g_new0 (GncXmlImportData, 1);
    data->filename = gnc_uri_get_path (filename);
    data->canceled = FALSE;

    /* gather ambiguous info */
    gxi_check_file (data);
    if (data->n_impossible == -1)
        return FALSE;

    if (!g_hash_table_size (data->ambiguous_ht))
    {
        /* no ambiguous strings */
        success = gxi_parse_file (data) &&
                  gxi_save_file (data);

        gxi_data_destroy (data);
    }
    else
    {
        /* common assistant initialization */
        builder = gtk_builder_new();
        gnc_builder_add_from_file  (builder , "assistant-xml-encoding.glade", "assistant_xml_encoding");
        data->assistant = GTK_WIDGET(gtk_builder_get_object (builder, "assistant_xml_encoding"));

        /* Enable buttons on all pages. */
        gtk_assistant_set_page_complete (GTK_ASSISTANT (data->assistant),
                                         GTK_WIDGET(gtk_builder_get_object(builder, "start_page")),
                                         TRUE);
        gtk_assistant_set_page_complete (GTK_ASSISTANT (data->assistant),
                                         GTK_WIDGET(gtk_builder_get_object(builder, "conversion_page")),
                                         TRUE);
        gtk_assistant_set_page_complete (GTK_ASSISTANT (data->assistant),
                                         GTK_WIDGET(gtk_builder_get_object(builder, "end_page")),
                                         TRUE);

        /* start page, explanations */
        gtk_assistant_set_page_title (GTK_ASSISTANT(data->assistant),
                                      gtk_assistant_get_nth_page (GTK_ASSISTANT(data->assistant), 0),
                                      gettext(encodings_doc_page_title));

        widget = GTK_WIDGET(gtk_builder_get_object (builder, "start_page"));
        gtk_label_set_text (GTK_LABEL(widget), gettext (encodings_doc_string));

        /* conversion page */
        data->default_encoding_hbox = GTK_WIDGET(gtk_builder_get_object (builder, "default_enc_box"));
        data->string_box_container = GTK_WIDGET(gtk_builder_get_object (builder, "string_box_container"));
        data->impossible_label = GTK_WIDGET(gtk_builder_get_object (builder, "impossible_label"));

        /* finish page */
        widget = GTK_WIDGET(gtk_builder_get_object(builder, "end_page"));
        gtk_label_set_text (GTK_LABEL(widget), gettext (finish_convert_string));

        gtk_builder_connect_signals(builder, data);

        gtk_widget_show_all (data->assistant);

        gxi_update_default_enc_combo (data);
        gxi_update_string_box (data);

        g_object_unref(G_OBJECT(builder));

        /* This won't return until the assistant is finished */
        gtk_main();

        if (data->canceled)
            success = FALSE;
        else
            success = gxi_save_file (data);
    }

    /* destroy all the data variables */
    gxi_data_destroy (data);
    g_free (data);

    return success;
}
Beispiel #7
0
int
main(int argc, char **argv)
{
    int i;

    qof_init();

    /* TEST: gnc_uri_get_components */
    for (i = 0; strs[i].uri != NULL; i++)
    {
        gchar *tprotocol = NULL;
        gchar *thostname = NULL;
        gchar *tusername = NULL;
        gchar *tpassword = NULL;
        gchar *tpath     = NULL;
        gint32 tport     = 0;
        gboolean testresult;

        gnc_uri_get_components( strs[i].uri, &tprotocol, &thostname,
                                &tport, &tusername, &tpassword, &tpath );
        testresult = ( safe_strcmp ( tprotocol, strs[i].protocol ) == 0 ) &
                     ( safe_strcmp ( thostname, strs[i].hostname ) == 0 ) &
                     ( safe_strcmp ( tusername, strs[i].username ) == 0 ) &
                     ( safe_strcmp ( tpassword, strs[i].password ) == 0 ) &
                     ( safe_strcmp ( tpath, strs[i].path ) == 0 ) &
                     ( tport == strs[i].port );
        do_test_args(testresult,
                     "gnc_uri_get_components",
                     __FILE__, __LINE__,
                     "\n  %s:\n"
                     "    Expected: %s, %s, %s, %s, %s, %d\n"
                     "    Got     : %s, %s, %s, %s, %s, %d\n",
                     strs[i].uri, strs[i].protocol, strs[i].hostname,
                     strs[i].username, strs[i].password, strs[i].path, strs[i].port,
                     tprotocol, thostname, tusername, tpassword, tpath, tport);
        g_free(tprotocol);
        g_free(thostname);
        g_free(tusername);
        g_free(tpassword);
        g_free(tpath);
    }

    /* TEST: gnc_uri_get_protocol */
    for (i = 0; strs[i].uri != NULL; i++)
    {
        gchar *tprotocol = NULL;
        gboolean testresult;

        tprotocol = gnc_uri_get_protocol( strs[i].uri );
        testresult = ( safe_strcmp ( tprotocol, strs[i].protocol ) == 0 );
        do_test_args(testresult,
                     "gnc_uri_get_protocol",
                     __FILE__, __LINE__,
                     "\n  %s:\n"
                     "    Expected: %s\n"
                     "    Got     : %s\n",
                     strs[i].uri, strs[i].protocol, tprotocol );
        g_free(tprotocol);
    }

    /* TEST: gnc_uri_get_path */
    for (i = 0; strs[i].uri != NULL; i++)
    {
        gchar *tpath = NULL;
        gboolean testresult;

        tpath = gnc_uri_get_path( strs[i].uri );
        testresult = ( safe_strcmp ( tpath, strs[i].path ) == 0 );
        do_test_args(testresult,
                     "gnc_uri_get_path",
                     __FILE__, __LINE__,
                     "\n  %s:\n"
                     "    Expected: %s\n"
                     "    Got     : %s\n",
                     strs[i].uri, strs[i].path, tpath );
        g_free(tpath);
    }

    /* TEST: gnc_uri_create_uri */
    for (i = 0; strs[i].uri != NULL; i++)
    {
        gchar *turi = NULL;
        gboolean testresult;

        turi = gnc_uri_create_uri( strs[i].protocol, strs[i].hostname, strs[i].port,
                                   strs[i].username, strs[i].password, strs[i].path );
        testresult = ( safe_strcmp ( turi, strs[i].created_uri ) == 0 );
        do_test_args(testresult,
                     "gnc_uri_create_uri",
                     __FILE__, __LINE__,
                     "\n  %s, %s, %s, %s, %s, %d:\n"
                     "    Expected: %s\n"
                     "    Got     : %s\n",
                     strs[i].protocol, strs[i].hostname,
                     strs[i].username, strs[i].password, strs[i].path, strs[i].port,
                     strs[i].created_uri, turi);
        g_free(turi);
    }

    /* TEST: gnc_uri_normalize_uri */
    for (i = 0; strs[i].uri != NULL; i++)
    {
        gchar *turi = NULL;
        gboolean testresult;

        turi = gnc_uri_normalize_uri( strs[i].uri, strs[i].want_password );
        testresult = ( safe_strcmp ( turi, strs[i].normalized_uri ) == 0 );
        do_test_args(testresult,
                     "gnc_uri_normalize_uri",
                     __FILE__, __LINE__,
                     "\n  %s:\n"
                     "    Expected: %s\n"
                     "    Got     : %s\n",
                     strs[i].uri, strs[i].normalized_uri, turi );
        g_free(turi);
    }

    /* TEST: gnc_uri_is_file_protocol */
    for (i = 0; strs[i].uri != NULL; i++)
    {
        gboolean tis_file_protocol;
        gboolean testresult;

        tis_file_protocol = gnc_uri_is_file_protocol( strs[i].protocol );
        testresult = ( tis_file_protocol == strs[i].is_file_protocol );
        do_test_args(testresult,
                     "gnc_uri_is_file_protocol",
                     __FILE__, __LINE__,
                     "\n  %s:\n"
                     "    Expected: %s\n"
                     "    Got     : %s\n",
                     strs[i].uri, strs[i].is_file_protocol, tis_file_protocol );
    }

    /* TEST: gnc_uri_is_file_uri */
    for (i = 0; strs[i].uri != NULL; i++)
    {
        gboolean tis_file_uri;
        gboolean testresult;

        tis_file_uri = gnc_uri_is_file_uri( strs[i].uri );
        testresult = ( tis_file_uri == strs[i].is_file_protocol );
        do_test_args(testresult,
                     "gnc_uri_is_file_uri",
                     __FILE__, __LINE__,
                     "\n  %s:\n"
                     "    Expected: %s\n"
                     "    Got     : %s\n",
                     strs[i].uri, strs[i].is_file_protocol, tis_file_uri );
    }

    print_test_results();
    return get_rv();
}
Beispiel #8
0
template <> void
gnc_dbi_session_begin<DbType::DBI_SQLITE>(QofBackend* qbe, QofSession* session,
                               const char* book_id, gboolean ignore_lock,
                               gboolean create, gboolean force)
{
    GncDbiBackend* be = (GncDbiBackend*)qbe;
    const char* msg = nullptr;
    gboolean file_exists;
    PairVec options;

    g_return_if_fail (qbe != nullptr);
    g_return_if_fail (session != nullptr);
    g_return_if_fail (book_id != nullptr);

    ENTER (" ");

    /* Remove uri type if present */
    auto path = gnc_uri_get_path (book_id);
    std::string filepath{path};
    g_free(path);
    GFileTest ftest = static_cast<decltype (ftest)> (
        G_FILE_TEST_IS_REGULAR | G_FILE_TEST_EXISTS) ;
    file_exists = g_file_test (filepath.c_str(), ftest);
    if (!create && !file_exists)
    {
        qof_backend_set_error (qbe, ERR_FILEIO_FILE_NOT_FOUND);
        qof_backend_set_message (qbe, "Sqlite3 file %s not found",
                                 filepath.c_str());
        PWARN ("Sqlite3 file %s not found", filepath.c_str());
        LEAVE("Error");
	return;
    }

    if (create && !force && file_exists)
    {
        qof_backend_set_error (qbe, ERR_BACKEND_STORE_EXISTS);
        msg = "Might clobber, no force";
        PWARN ("%s", msg);
        LEAVE("Error");
	return;
    }

    be->connect(nullptr);
    /* dbi-sqlite3 documentation says that sqlite3 doesn't take a "host" option */
    options.push_back(std::make_pair("host", "localhost"));
    auto dirname = g_path_get_dirname (filepath.c_str());
    auto basename = g_path_get_basename (filepath.c_str());
    options.push_back(std::make_pair("dbname", basename));
    options.push_back(std::make_pair("sqlite3_dbdir", dirname));
    if (basename != nullptr) g_free (basename);
    if (dirname != nullptr) g_free (dirname);
    UriStrings uri;
    auto conn = conn_setup<DbType::DBI_SQLITE>(qbe, options, uri);
    if (conn == nullptr)
    {
        LEAVE("Error");
        return;
    }

    auto result = dbi_conn_connect (conn);

    if (result < 0)
    {
        dbi_conn_close(conn);
        PERR ("Unable to connect to %s: %d\n", book_id, result);
        qof_backend_set_error (qbe, ERR_BACKEND_BAD_URL);
        LEAVE("Error");
	return;
    }

    if (!conn_test_dbi_library(conn, qbe))
    {
        if (create && !file_exists)
        {
         /* File didn't exist before, but it does now, and we don't want to
          * leave it lying around.
          */
            dbi_conn_close (conn);
            conn = nullptr;
            g_unlink (filepath.c_str());
        }
        dbi_conn_close(conn);
        LEAVE("Bad DBI Library");
        return;
    }

    try
    {
        be->connect(new GncDbiSqlConnection(DbType::DBI_SQLITE,
                                            qbe, conn, ignore_lock));
    }
    catch (std::runtime_error& err)
    {
        return;
    }

    /* We should now have a proper session set up.
     * Let's start logging */
    xaccLogSetBaseName (filepath.c_str());
    PINFO ("logpath=%s", filepath.c_str() ? filepath.c_str() : "(null)");

    LEAVE ("");
}