File: classDataSource.cpp

package info (click to toggle)
unixodbc 2.1.1-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,668 kB
  • ctags: 12,486
  • sloc: ansic: 107,685; cpp: 33,663; sh: 13,300; makefile: 2,926; yacc: 499; lex: 241; sed: 93; sql: 1
file content (163 lines) | stat: -rw-r--r-- 4,483 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**************************************************
 *
 *
 **************************************************
 * This code was created by Peter Harvey @ CodeByDesign.
 * Released under GPL 18.FEB.99
 *
 * Contributions from...
 * -----------------------------------------------
 * Peter Harvey		- pharvey@codebydesign.com
 **************************************************/
#include "classDataSource.h"

#include "computergreen.xpm"
#include "computerred.xpm"

classDataSource::classDataSource( QListView *pParent, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
    : classNode( pParent, pCanvas )
{
	Init( nDataSourceType,	pszDataSourceName, hEnv );
}

classDataSource::classDataSource( QListViewItem *pParent, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
    : classNode( pParent, pCanvas )
{
	Init( nDataSourceType,	pszDataSourceName, hEnv );
}

classDataSource::classDataSource( QListViewItem *pParent,QListViewItem *pAfter, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
    : classNode( pParent, pAfter, pCanvas )
{
	Init( nDataSourceType,	pszDataSourceName, hEnv );
}

classDataSource::~classDataSource()
{
	if ( pConnectionFrame )
		delete pConnectionFrame;

	if ((void*) hDbc != 0 )
	{
		if ( bLoggedIn )
			SQLDisconnect( hDbc );
		SQLFreeConnect( hDbc );
	}
}

void classDataSource::Init( int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
{
	char	szResults[9600];
	char	szDescription[INI_MAX_PROPERTY_VALUE+1];
	char	szDriver[INI_MAX_PROPERTY_VALUE+1];
	QString	qsDescription;

	if ( nDataSourceType == classDataSource::System )
		this->nDataSourceType = nDataSourceType;
	else
		this->nDataSourceType = classDataSource::User;

	qsDataSourceName = "";
	if ( pszDataSourceName != 0 )
		qsDataSourceName = pszDataSourceName;

	this->pCanvas 	= pCanvas;
	bLoggedIn		= FALSE;
	this->hEnv		= hEnv;
	hDbc 			= (SQLHDBC)0;
	setPixmap( 0, QPixmap( computerred_xpm ) );

	// CREATE AN ODBC CONNECTION
	if ( SQLAllocConnect( hEnv, &hDbc ) != SQL_SUCCESS )
	{
		QMessageBox::critical( (QWidget *)this, "Data Manager", "Call to the ODBC Driver Manager failed" );
		return;
	}

	szResults[0] 		= '\0';
    szDescription[0]	= '\0';
    szDriver[0]			= '\0';
	if ( nDataSourceType == User )
		SQLSetConfigMode( ODBC_USER_DSN );
	else
		SQLSetConfigMode( ODBC_SYSTEM_DSN );
	if ( SQLGetPrivateProfileString((char*) qsDataSourceName.data(), "Description", "", szResults, 9600, 0 ) > 0 )
		iniElement( szResults, '\0', '\0', 0, szDescription, INI_MAX_PROPERTY_VALUE );
	if ( SQLGetPrivateProfileString((char*) qsDataSourceName.data(), "Driver", "", szResults, 9600, 0 ) > 0 )
		iniElement( szResults, '\0', '\0', 0, szDriver, INI_MAX_PROPERTY_VALUE );
	SQLSetConfigMode( ODBC_BOTH_DSN );

	qsDescription.sprintf( "%s [%s]", szDescription, szDriver );
	setText( 0, qsDataSourceName );
    setText( 1, "" );
	setText( 2, qsDescription );
	pTables = 0;
	pConnectionFrame	= 0;
}

void classDataSource::setOpen( bool bOpen )
{
    listView()->setSelected( listView()->selectedItem(), false );
    if ( bOpen && !bLoggedIn )
    {
        // try to connect
		if ( hDbc )
		{
            classLogin	*pLogin = new classLogin( pCanvas, hDbc, (char*)qsDataSourceName.data(), nDataSourceType );
			if ( pLogin->exec() )
			{
				bLoggedIn = true;
				setPixmap( 0, QPixmap( computergreen_xpm ) );
				pTables = new classTables( this, pCanvas, hDbc );
                QListViewItem::setOpen( bOpen );
			}
            else
            {
                delete pLogin;
                return;
            }
            delete pLogin;
		}
    }
    else if ( !bOpen && bLoggedIn )
    {
        // disconnect
        delete pTables;
        pTables = 0;
        delete pConnectionFrame;
        pConnectionFrame = 0;
        SQLDisconnect( hDbc );
        setPixmap( 0, QPixmap( computerred_xpm ) );
        bLoggedIn = false;
        QListViewItem::setOpen( bOpen );
    }
}

void classDataSource::setup()
{
    setExpandable( TRUE );
    QListViewItem::setup();
}

void classDataSource::selectionChanged( QListViewItem *p )
{
    if ( pTables ) pTables->selectionChanged( p );
	if ( p == this )
	{
		if ( bLoggedIn )
		{
			if ( pConnectionFrame )
				pConnectionFrame->show();
			else
			{
				pConnectionFrame = new classConnectionFrame( hDbc, qsDataSourceName, pCanvas );
				pConnectionFrame->show();
			}
		}
	}
	else if ( pConnectionFrame )
		pConnectionFrame->hide();
}