File: classDataSource.cpp

package info (click to toggle)
unixodbc 2.2.14p2-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 14,620 kB
  • sloc: ansic: 104,255; cpp: 38,573; sh: 15,958; makefile: 2,728; sql: 1
file content (191 lines) | stat: -rw-r--r-- 5,336 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**************************************************
 *
 *
 **************************************************
 * 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"

#ifdef QT_V4LAYOUT
classDataSource::classDataSource( Q3ListView *pParent, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
#else
classDataSource::classDataSource( QListView *pParent, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
#endif
    : classNode( pParent, pCanvas )
{
	Init( nDataSourceType,	pszDataSourceName, hEnv );
}

#ifdef QT_V4LAYOUT
classDataSource::classDataSource( Q3ListViewItem *pParent, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
#else
classDataSource::classDataSource( QListViewItem *pParent, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
#endif
    : classNode( pParent, pCanvas )
{
	Init( nDataSourceType,	pszDataSourceName, hEnv );
}

#ifdef QT_V4LAYOUT
classDataSource::classDataSource( Q3ListViewItem *pParent,Q3ListViewItem *pAfter, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
#else
classDataSource::classDataSource( QListViewItem *pParent,QListViewItem *pAfter, classCanvas *pCanvas, int nDataSourceType, char *pszDataSourceName, SQLHENV hEnv )
#endif
    : 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.ascii(), "Description", "", szResults, 9600, 0 ) > 0 )
		iniElement( szResults, '\0', '\0', 0, szDescription, INI_MAX_PROPERTY_VALUE );
	if ( SQLGetPrivateProfileString((char*) qsDataSourceName.ascii(), "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.ascii(), nDataSourceType );
			if ( pLogin->exec() )
			{
				bLoggedIn = true;
				setPixmap( 0, QPixmap( computergreen_xpm ) );
				pTables = new classTables( this, pCanvas, hDbc );
#ifdef QT_V4LAYOUT
                Q3ListViewItem::setOpen( bOpen );
#else
                QListViewItem::setOpen( bOpen );
#endif
			}
            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;
#ifdef QT_V4LAYOUT
        Q3ListViewItem::setOpen( bOpen );
#else
        QListViewItem::setOpen( bOpen );
#endif
    }
}

void classDataSource::setup()
{
    setExpandable( TRUE );
#ifdef QT_V4LAYOUT
    Q3ListViewItem::setup();
#else
    QListViewItem::setup();
#endif
}

#ifdef QT_V4LAYOUT
void classDataSource::selectionChanged( Q3ListViewItem *p )
#else
void classDataSource::selectionChanged( QListViewItem *p )
#endif
{
    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();
}