Beispiel #1
0
static gchar*
geturl( FileAccessWindow* faw )
{
    gchar* url = NULL;
    const gchar* host;
    const gchar* database;
    const gchar* username;
    const gchar* password;
    const gchar* type;
    const gchar* file;
    const gchar* path;

    host = gtk_entry_get_text( faw->tf_host );
    database = gtk_entry_get_text( faw->tf_database );
    username = gtk_entry_get_text( faw->tf_username );
    password = gtk_entry_get_text( faw->tf_password );
    file = gtk_file_chooser_get_filename( faw->fileChooser );

    type = gtk_combo_box_get_active_text( faw->cb_uri_type );
    if ( gnc_uri_is_file_protocol( type ) )
    {
        if ( file == NULL ) /* file protocol was chosen but no filename was set */
            return NULL;
        else                /* file protocol was chosen with filename set */
            path = file;
    }
    else                    /* db protocol was chosen */
        path = database;

    url = gnc_uri_create_uri (type, host, 0, username, password, path);

    return url;
}
Beispiel #2
0
/* Checks if the given uri defines a file
 * (as opposed to a network service)
 */
gboolean gnc_uri_is_file_uri (const gchar *uri)
{
    gchar *protocol = gnc_uri_get_protocol ( uri );
    gboolean result = gnc_uri_is_file_protocol ( protocol );

    g_free ( protocol );

    return result;
}
Beispiel #3
0
/* Splits a uri into its separate components */
void gnc_uri_get_components (const gchar *uri,
                             gchar **protocol,
                             gchar **hostname,
                             gint32 *port,
                             gchar **username,
                             gchar **password,
                             gchar **path)
{
    gchar **splituri, **spliturl;
    gchar *url = NULL, *tmpusername = NULL, *tmphostname = NULL;
    gchar *delimiter = NULL;

    *protocol = NULL;
    *hostname = NULL;
    *port      = 0;
    *username = NULL;
    *password = NULL;
    *path     = NULL;

    g_return_if_fail( uri != NULL );

    splituri = g_strsplit ( uri, "://", 2 );
    if ( splituri[1] == NULL )
    {
        /* No protocol means simple file uri */
        *protocol = g_strdup ( "file" );
        *path     = g_strdup ( splituri[0] );
        g_strfreev ( splituri );
        return;
    }

    /* At least a protocol was found, set it here */
    *protocol = g_strdup ( splituri[0] );

    if ( gnc_uri_is_file_protocol ( *protocol ) )
    {
        /* Protocol indicates file based uri.
         * Note that unknown protocols are treated as if they are
         * file-based protocols. This is done to prevent password
         * lookups on unknown protocols.
         * On the other hand, since we don't know the specifics of
         * unknown protocols, we don't attempt to return an absolute
         * pathname for them, just whatever was there.
         */
        if ( gnc_uri_is_known_protocol ( *protocol ) )
            *path     = gnc_resolve_file_path ( splituri[1] );
        else
            *path     = g_strdup ( splituri[1] );
        g_strfreev ( splituri );
        return;
    }

    /* Protocol indicates full network style uri, let's see if it
     * has a username and/or password
     */
    url = g_strdup (splituri[1]);
    g_strfreev ( splituri );

    /* Check for "@" sign, but start from the end - the password may contain
     * this sign as well
     */
    delimiter = g_strrstr ( url, "@" );
    if ( delimiter != NULL )
    {
        /* There is at least a username in the url */
        delimiter[0] = '\0';
        tmpusername = url;
        tmphostname = delimiter + 1;

        /* Check if there's a password too by looking for a :
         * Start from the beginning this time to avoid possible :
         * in the password */
        delimiter = g_strstr_len ( tmpusername, -1, ":" );
        if ( delimiter != NULL )
        {
            /* There is password in the url */
            delimiter[0] = '\0';
            *password = g_strdup ( (const gchar*)(delimiter + 1) );
        }
        *username = g_strdup ( (const gchar*)tmpusername );
    }
    else
    {
        /* No username and password were given */
        tmphostname = url;
    }

    /* Find the path part */
    delimiter = g_strstr_len ( tmphostname, -1, "/" );
    if ( delimiter != NULL )
    {
        delimiter[0] = '\0';
        if ( gnc_uri_is_file_protocol ( *protocol ) ) /* always return absolute file paths */
            *path = gnc_resolve_file_path ( (const gchar*)(delimiter + 1) );
        else /* path is no file path, so copy it as is */
            *path = g_strdup ( (const gchar*)(delimiter + 1) );
    }

    /* Check for a port specifier */
    delimiter = g_strstr_len ( tmphostname, -1, ":" );
    if ( delimiter != NULL )
    {
        delimiter[0] = '\0';
        *port = g_ascii_strtoll ( delimiter + 1, NULL, 0 );
    }

    *hostname = g_strdup ( (const gchar*)tmphostname );

    g_free ( url );

    return;

}
Beispiel #4
0
/* Generates a normalized uri from the separate components */
gchar *gnc_uri_create_uri (const gchar *protocol,
                           const gchar *hostname,
                           gint32 port,
                           const gchar *username,
                           const gchar *password,
                           const gchar *path)
{
    gchar *userpass = NULL, *portstr = NULL, *uri = NULL;

    g_return_val_if_fail( path != 0, NULL );

    if ( (protocol == NULL) || gnc_uri_is_file_protocol ( protocol ) )
    {
        /* Compose a file based uri, which means ignore everything but
         * the protocol and the path
         * We return an absolute pathname if the protocol is known or
         * no protocol was given. For an unknown protocol, we return the
         * path info as is.
         */
        gchar *abs_path;
        if ( protocol && (!gnc_uri_is_known_protocol (protocol)) )
            abs_path = g_strdup ( path );
        else
            abs_path = gnc_resolve_file_path ( path );
        if ( protocol == NULL )
            uri = g_strdup_printf ( "file://%s", abs_path );
        else
            uri = g_strdup_printf ( "%s://%s", protocol, abs_path );
        g_free (abs_path);
        return uri;
    }

    /* Not a file based uri, we need to setup all components that are not NULL
     * For this scenario, hostname is mandatory.
     */
    g_return_val_if_fail( hostname != 0, NULL );

    if ( username != NULL && *username )
    {
        if ( password != NULL && *password )
            userpass = g_strdup_printf ( "%s:%s@", username, password );
        else
            userpass = g_strdup_printf ( "%s@", username );
    }
    else
        userpass = g_strdup ( "" );

    if ( port != 0 )
        portstr = g_strdup_printf ( ":%d", port );
    else
        portstr = g_strdup ( "" );

    // XXX Do I have to add the slash always or are there situations
    //     it is in the path already ?
    uri = g_strconcat ( protocol, "://", userpass, hostname, portstr, "/", path, NULL );

    g_free ( userpass );
    g_free ( portstr );

    return uri;

}
Beispiel #5
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();
}