Exemplo n.º 1
0
/*! 
 * \brief   Save Data Source Name.
 *
 *          Call this to save User, System and File DSN's based upon a property list.
 * 
 * \param   pwidgetParent   Input. Widget to use as parent for messages. Null is ok.
 * \param   hFirstProperty  Input. First property in NULL terminated property list.
 * \param   nType           Input. Type of DSN.
 * \param   stringIni       Input. Default is QString::null.
 *
 *                          \li User and System DSN. This can be a QString::null to use 
 *                          default location or it can be a fully qualified path and name.
 *                          \li File DSN. This can be a QString::null to use default 
 *                          location or it can be an abs directory. In either case the file
 *                          name itself will be derived from the Name property.
 * 
 * \return  bool
 * \retval  true    Hey - it worked :)
 * \retval  false   Call failed for some reason. Error message(s) is shown in message dialog.
 */
bool CODBCInst::saveDataSourceName( QWidget *pwidgetParent, HODBCINSTPROPERTY hFirstProperty, CDSNWizardData::Type nType, const QString &stringIni )
{
    // sanity checks...
    if ( !hFirstProperty )
    {
        QMessageBox::critical( pwidgetParent, QObject::tr( "ODBC Administrator" ),  QObject::tr( "Programmer error: Uninitialized property list." ) );
        return false;
    }
    if ( (QString( hFirstProperty->szValue )).simplified().isEmpty() )
    {
        QMessageBox::critical( pwidgetParent, QObject::tr( "ODBC Administrator" ),  QObject::tr( "Missing 'Name' property value." ) );
        return false;
    }

    // init
    UWORD   nConfigMode     = ODBC_BOTH_DSN;
    bool    bReturn         = false;
    QString stringFileName  = stringIni;

    switch ( nType )
    {
        case CDSNWizardData::TypeUser:
            SQLGetConfigMode( &nConfigMode );
            SQLSetConfigMode( ODBC_USER_DSN );
            if ( stringFileName.isEmpty() ) stringFileName = "odbc.ini";
            break;
        case CDSNWizardData::TypeSystem:
            SQLGetConfigMode( &nConfigMode );
            SQLSetConfigMode( ODBC_SYSTEM_DSN );
            if ( stringFileName.isEmpty() ) stringFileName = "odbc.ini";
            break;
        case CDSNWizardData::TypeFile:
            // should we calc the dir and file name (do not allow the special purpose names)...
            if ( stringFileName.isEmpty() || stringFileName.at( 0 ) != QChar( '/' ) )
            {
                char szDirectory[FILENAME_MAX];

                szDirectory[0] = '\0';
                _odbcinst_FileINI( szDirectory );
                stringFileName = QString( "%1/%2.dsn" ).arg( szDirectory ).arg( hFirstProperty->szValue );
            }
            else if ( stringFileName.at( 0 ) == QChar( '/' ) )
                stringFileName = QString( "%1/%2.dsn" ).arg( stringFileName ).arg( hFirstProperty->szValue );
            else
            {
                QMessageBox::critical( pwidgetParent, QObject::tr( "ODBC Administrator" ),  QObject::tr( "File DSN directory expected but seems to be a name or file name." ) );
                return false;
            }
            break;
        default:
            QMessageBox::critical( pwidgetParent, QObject::tr( "ODBC Administrator" ),  QObject::tr( "Unhandled Data Source Name Type" ) );
            return false;
    }

    // save...
    if ( nType == CDSNWizardData::TypeFile )
    {
        // create the file with a section called "ODBC"...
        if ( !SQLWriteFileDSN( stringFileName.toAscii().data(), "ODBC", NULL, NULL ) )
        {
            showErrors( 0, QString( "Could not write to (%1)" ).arg( stringFileName ) );
            goto CODBCInstExit1;
        }
        // add all of the properties...
        for ( HODBCINSTPROPERTY hCurProperty = hFirstProperty->pNext; hCurProperty != NULL; hCurProperty = hCurProperty->pNext )
        {
            if ( !SQLWriteFileDSN( stringFileName.toAscii().data(), "ODBC", hCurProperty->szName, hCurProperty->szValue ) )
            {
                showErrors( 0, QString( "Could not write to (%1)" ).arg( stringFileName ) );
                goto CODBCInstExit1;
            }
        }
    }
    else
    {
        // create a section called using Name property (always 1st property)...
        if ( SQLWritePrivateProfileString( hFirstProperty->szValue, NULL, NULL, stringFileName.toAscii().data() ) == FALSE )
        {
            showErrors( pwidgetParent, QString( QObject::tr( "Could not write to (%1). You may need additional system privileges." ) ).arg( stringFileName ) );
            goto CODBCInstExit1;
        }
        // add all of the properties...
        for ( HODBCINSTPROPERTY hCurProperty = hFirstProperty->pNext; hCurProperty != NULL; hCurProperty = hCurProperty->pNext )
        {
            SQLWritePrivateProfileString( hFirstProperty->szValue, hCurProperty->szName, hCurProperty->szValue, stringFileName.toAscii().data() );
        }
    }

    bReturn = true;

    // fini
CODBCInstExit1:
    if ( nType != CDSNWizardData::TypeFile )
        SQLSetConfigMode( nConfigMode );

    return bReturn;
}
Exemplo n.º 2
0
BOOL SQLWriteFileDSN(			LPCSTR	pszFileName,
								LPCSTR	pszAppName,
								LPCSTR	pszKeyName,
								LPCSTR	pszString )
{
	HINI	hIni;
	char	szFileName[ODBC_FILENAME_MAX+1];

	if ( pszFileName[0] == '/' )
	{
		strcpy( szFileName, pszFileName );
	}
	else
	{	
		char szPath[ODBC_FILENAME_MAX+1];
		*szPath = '\0';
		_odbcinst_FileINI( szPath );
		sprintf( szFileName, "%s/%s", szPath, pszFileName );
	}

    if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" ))
    {
        strcat( szFileName, ".dsn" );
    }

#ifdef __OS2__
	if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 0L ) != INI_SUCCESS )
#else
	if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS )
#endif
	{
       	inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" );
		return FALSE;
	}

	/* delete section */
	if ( pszString == NULL && pszKeyName == NULL )
	{
		if ( iniObjectSeek( hIni, (char *)pszAppName ) == INI_SUCCESS )
        {
			iniObjectDelete( hIni );
        }
	}
	/* delete entry */
	else if	( pszString == NULL )
	{
		if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) == INI_SUCCESS )
        {
			iniPropertyDelete( hIni );
        }
	}
	else
	{
		/* add section */
		if ( iniObjectSeek( hIni, (char *)pszAppName ) != INI_SUCCESS )
        {
			iniObjectInsert( hIni, (char *)pszAppName );
        }
		/* update entry */
		if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) == INI_SUCCESS )
		{
			iniObjectSeek( hIni, (char *)pszAppName );
			iniPropertyUpdate( hIni, (char *)pszKeyName, (char *)pszString );
		}
		/* add entry */
		else
		{
			iniObjectSeek( hIni, (char *)pszAppName );
			iniPropertyInsert( hIni, (char *)pszKeyName, (char *)pszString );
		}
	}

	if ( iniCommit( hIni ) != INI_SUCCESS )
	{
		iniClose( hIni );
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
		return FALSE;
	}

	iniClose( hIni );

	return TRUE;
}
Exemplo n.º 3
0
BOOL SQLReadFileDSN(            LPCSTR  pszFileName,
                                LPCSTR  pszAppName,
                                LPCSTR  pszKeyName,
                                LPSTR   pszString,
                                WORD    nString,
                                WORD    *pnString )
{
    HINI    hIni;
    int     nBufPos         = 0;
    char    szValue[INI_MAX_PROPERTY_VALUE+1];
    char    szFileName[ODBC_FILENAME_MAX+1];

    inst_logClear();

    /* SANITY CHECKS */
    if ( pszString == NULL || nString < 1  )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_BUFF_LEN, "" );
        return FALSE;
    }
    if ( pszFileName == NULL && pszAppName == NULL && pszKeyName == NULL )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
        return FALSE;
    }
    if ( pszAppName == NULL && pszKeyName != NULL )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_REQUEST_TYPE, "" );
        return FALSE;
    }
    if ( pszFileName && strlen( pszFileName ) > ODBC_FILENAME_MAX ) {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" );
        return FALSE;
    }

    *pszString = '\0';

    /*****************************************************
     * GATHER ALL RELEVANT DSN INFORMATION INTO AN hIni
     *****************************************************/
    if ( pszFileName && pszFileName[0] == '/' )
    {
        strcpy( szFileName, pszFileName );
        if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" ))
        {
            strcat( szFileName, ".dsn" );
        }

/* on OS/2 the file DSN is a text file */
#ifdef __OS2__
        if ( iniOpen( &hIni, (char*)szFileName, "#;", '[', ']', '=', TRUE, 0L )
             != INI_SUCCESS )
#else
        if ( iniOpen( &hIni, (char*)szFileName, "#;", '[', ']', '=', TRUE )
             != INI_SUCCESS )
#endif
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_INVALID_PATH, "" );

            return FALSE;
        }
    }
    else if ( pszFileName )
    {
        char szPath[ODBC_FILENAME_MAX+1];
        *szPath = '\0';
        _odbcinst_FileINI( szPath );
        sprintf( szFileName, "%s/%s", szPath, pszFileName );

        if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" ))
        {
            strcat( szFileName, ".dsn" );
        }

/* on OS/2 the file DSN is a text file */
#ifdef __OS2__
        if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE, 0L )
             != INI_SUCCESS )
#else
        if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE )
             != INI_SUCCESS )
#endif
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_INVALID_PATH, "" );

            return FALSE;
        }
    }

    if ( pszAppName == NULL && pszKeyName == NULL )
    {
        GetSections( hIni, pszString, nString );
    }
    else if ( pszAppName != NULL && pszKeyName == NULL )
    {
        GetEntries( hIni, pszAppName, pszString, nString );
    }
    else
    {
        /* TRY TO GET THE ONE ITEM MATCHING Section & Entry */
        if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) != INI_SUCCESS )
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_REQUEST_FAILED, "" );

            return FALSE;
        }
        else
        {
            iniValue( hIni, szValue );
            strncpy( pszString, szValue, nString );
            pszString[ nString - 1 ] = '\0';
            nBufPos = strlen( szValue );
        }
    }

    if ( pszFileName )
    {
        iniClose( hIni );
    }

    if ( pnString )
    {
        *pnString = strlen( pszString );
    }

    return TRUE;
}