setup.c 12.9 KB
Newer Older
1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Module:          setup.c
 *
 * Description:     This module contains the setup functions for 
 *                  adding/modifying a Data Source in the ODBC.INI portion
 *                  of the registry.
 *
 * Classes:         n/a
 *
 * API functions:   ConfigDSN
 *
 * Comments:        See "notice.txt" for copyright and license information.
 *
 *************************************************************************************/

#include  "psqlodbc.h"
#include  "connection.h"
18 19
#include  <windows.h>
#include  <windowsx.h>
20 21
#include  <odbcinst.h>
#include  <string.h>
22 23
#include  <stdlib.h>
#include  "resource.h"
24 25
#include  "dlg_specific.h"

26 27 28 29 30 31

#define INTFUNC  __stdcall

extern HINSTANCE NEAR s_hModule;               /* Saved module handle. */
extern GLOBAL_VALUES globals;

32
// Constants ---------------------------------------------------------------
33 34
#define MIN(x,y)      ((x) < (y) ? (x) : (y))

35 36 37 38
#ifdef WIN32
#define MAXPGPATH       (255+1)
#endif

39 40 41
#define MAXKEYLEN       (15+1)            // Max keyword length
#define MAXDESC         (255+1)           // Max description length
#define MAXDSNAME       (32+1)            // Max data source name length
42 43


44 45
// Globals -----------------------------------------------------------------
// NOTE:  All these are used by the dialog procedures
46
typedef struct tagSETUPDLG {
47 48
        HWND       hwndParent;                   // Parent window handle
        LPCSTR     lpszDrvr;                     // Driver description
49
		ConnInfo   ci;
50 51 52
        char       szDSN[MAXDSNAME];             // Original data source name
        BOOL       fNewDSN;                      // New data source flag
        BOOL       fDefault;                     // Default data source flag
53 54 55 56 57

} SETUPDLG, FAR *LPSETUPDLG;



58
// Prototypes --------------------------------------------------------------
59 60 61 62
void INTFUNC CenterDialog(HWND hdlg);
int  CALLBACK ConfigDlgProc(HWND hdlg, WORD wMsg, WPARAM wParam, LPARAM lParam);
void INTFUNC ParseAttributes (LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg);
BOOL INTFUNC SetDSNAttributes(HWND hwnd, LPSETUPDLG lpsetupdlg);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80


/* ConfigDSN ---------------------------------------------------------------
  Description:  ODBC Setup entry point
                This entry point is called by the ODBC Installer
                (see file header for more details)
  Input      :  hwnd ----------- Parent window handle
                fRequest ------- Request type (i.e., add, config, or remove)
                lpszDriver ----- Driver name
                lpszAttributes - data source attribute string
  Output     :  TRUE success, FALSE otherwise
--------------------------------------------------------------------------*/

BOOL CALLBACK ConfigDSN (HWND    hwnd,
                         WORD    fRequest,
                         LPCSTR  lpszDriver,
                         LPCSTR  lpszAttributes)
{
81
BOOL  fSuccess;                                            // Success/fail flag
82 83
GLOBALHANDLE hglbAttr;
LPSETUPDLG lpsetupdlg;
84 85
        

86
        // Allocate attribute array
87 88 89 90 91
        hglbAttr = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(SETUPDLG));
        if (!hglbAttr)
                return FALSE;
        lpsetupdlg = (LPSETUPDLG)GlobalLock(hglbAttr);

92
        // Parse attribute string
93 94 95
        if (lpszAttributes)
                ParseAttributes(lpszAttributes, lpsetupdlg);

96
        // Save original data source name
97 98
        if (lpsetupdlg->ci.dsn[0])
                lstrcpy(lpsetupdlg->szDSN, lpsetupdlg->ci.dsn);
99 100 101
        else
                lpsetupdlg->szDSN[0] = '\0';

102
        // Remove data source
103
        if (ODBC_REMOVE_DSN == fRequest) {
104
                // Fail if no data source name was supplied
105
                if (!lpsetupdlg->ci.dsn[0])
106 107
                        fSuccess = FALSE;

108
                // Otherwise remove data source from ODBC.INI
109
                else
110
                        fSuccess = SQLRemoveDSNFromIni(lpsetupdlg->ci.dsn);
111 112
        }

113
        // Add or Configure data source
114
        else {
115
                // Save passed variables for global access (e.g., dialog access)
116 117 118
                lpsetupdlg->hwndParent = hwnd;
                lpsetupdlg->lpszDrvr     = lpszDriver;
                lpsetupdlg->fNewDSN      = (ODBC_ADD_DSN == fRequest);
119
                lpsetupdlg->fDefault     = !lstrcmpi(lpsetupdlg->ci.dsn, INI_DSN);
120

121
                // Display the appropriate dialog (if parent window handle supplied)
122
                if (hwnd) {
123
                        // Display dialog(s)
124
                          fSuccess = (IDOK == DialogBoxParam(s_hModule,
125 126 127 128
                                                                MAKEINTRESOURCE(DLG_CONFIG),
                                                                hwnd,
                                                                ConfigDlgProc,
                                                                (LONG)(LPSTR)lpsetupdlg));
129 130
                }

131
                else if (lpsetupdlg->ci.dsn[0])
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 192 193 194 195 196 197
                        fSuccess = SetDSNAttributes(hwnd, lpsetupdlg);
                else
                        fSuccess = FALSE;
        }

        GlobalUnlock(hglbAttr);
        GlobalFree(hglbAttr);

        return fSuccess;
}


/* CenterDialog ------------------------------------------------------------
        Description:  Center the dialog over the frame window
        Input      :  hdlg -- Dialog window handle
        Output     :  None
--------------------------------------------------------------------------*/
void INTFUNC CenterDialog(HWND hdlg)
{
        HWND    hwndFrame;
        RECT    rcDlg, rcScr, rcFrame;
        int             cx, cy;

        hwndFrame = GetParent(hdlg);

        GetWindowRect(hdlg, &rcDlg);
        cx = rcDlg.right  - rcDlg.left;
        cy = rcDlg.bottom - rcDlg.top;

        GetClientRect(hwndFrame, &rcFrame);
        ClientToScreen(hwndFrame, (LPPOINT)(&rcFrame.left));
        ClientToScreen(hwndFrame, (LPPOINT)(&rcFrame.right));
        rcDlg.top    = rcFrame.top  + (((rcFrame.bottom - rcFrame.top) - cy) >> 1);
        rcDlg.left   = rcFrame.left + (((rcFrame.right - rcFrame.left) - cx) >> 1);
        rcDlg.bottom = rcDlg.top  + cy;
        rcDlg.right  = rcDlg.left + cx;

        GetWindowRect(GetDesktopWindow(), &rcScr);
        if (rcDlg.bottom > rcScr.bottom)
        {
                rcDlg.bottom = rcScr.bottom;
                rcDlg.top    = rcDlg.bottom - cy;
        }
        if (rcDlg.right  > rcScr.right)
        {
                rcDlg.right = rcScr.right;
                rcDlg.left  = rcDlg.right - cx;
        }

        if (rcDlg.left < 0) rcDlg.left = 0;
        if (rcDlg.top  < 0) rcDlg.top  = 0;

        MoveWindow(hdlg, rcDlg.left, rcDlg.top, cx, cy, TRUE);
        return;
}

/* ConfigDlgProc -----------------------------------------------------------
  Description:  Manage add data source name dialog
  Input      :  hdlg --- Dialog window handle
                wMsg --- Message
                wParam - Message parameter
                lParam - Message parameter
  Output     :  TRUE if message processed, FALSE otherwise
--------------------------------------------------------------------------*/


198 199 200 201
int CALLBACK ConfigDlgProc(HWND   hdlg,
                           WORD   wMsg,
                           WPARAM wParam,
                           LPARAM lParam)
202 203
{

204
	switch (wMsg) {
205
	// Initialize the dialog
206 207 208 209 210 211 212 213 214
	case WM_INITDIALOG:
	{
		LPSETUPDLG lpsetupdlg = (LPSETUPDLG) lParam;
		ConnInfo *ci = &lpsetupdlg->ci;

		/*	Hide the driver connect message */
		ShowWindow(GetDlgItem(hdlg, DRV_MSG_LABEL), SW_HIDE);

		SetWindowLong(hdlg, DWL_USER, lParam);
215
		CenterDialog(hdlg);                             // Center dialog
216

217 218
		// NOTE: Values supplied in the attribute string will always
		//       override settings in ODBC.INI
219

220
		//	Get the rest of the common attributes
221 222
		getDSNinfo(ci, CONN_DONT_OVERWRITE);

223
		//	Fill in any defaults
224 225 226
		getDSNdefaults(ci);


227
		// Initialize dialog fields
228 229 230 231 232 233 234 235 236 237 238 239 240
		SetDlgStuff(hdlg, ci);


		if (lpsetupdlg->fDefault) {
			EnableWindow(GetDlgItem(hdlg, IDC_DSNAME), FALSE);
			EnableWindow(GetDlgItem(hdlg, IDC_DSNAMETEXT), FALSE);
		}
		else
			SendDlgItemMessage(hdlg, IDC_DSNAME,
				EM_LIMITTEXT, (WPARAM)(MAXDSNAME-1), 0L);

		SendDlgItemMessage(hdlg, IDC_DESC,
			EM_LIMITTEXT, (WPARAM)(MAXDESC-1), 0L);
241
		return TRUE;                                            // Focus was not set
242 243 244
    }


245
	// Process buttons
246
	case WM_COMMAND:
247

248
		switch (GET_WM_COMMAND_ID(wParam, lParam)) {
249 250
		// Ensure the OK button is enabled only when a data source name
		// is entered
251 252 253
		case IDC_DSNAME:
			if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE)
			{
254
				char    szItem[MAXDSNAME];              // Edit control text
255

256
				// Enable/disable the OK button
257 258 259 260 261 262 263 264
				EnableWindow(GetDlgItem(hdlg, IDOK),
					GetDlgItemText(hdlg, IDC_DSNAME,
					szItem, sizeof(szItem)));

				return TRUE;
			}
			break;

265
		// Accept results
266 267 268 269 270
		case IDOK:
		{
			LPSETUPDLG lpsetupdlg;

			lpsetupdlg = (LPSETUPDLG)GetWindowLong(hdlg, DWL_USER);
271
			// Retrieve dialog values
272 273 274 275 276 277
			if (!lpsetupdlg->fDefault)
					GetDlgItemText(hdlg, IDC_DSNAME,
							lpsetupdlg->ci.dsn,
							sizeof(lpsetupdlg->ci.dsn));


278
			//	Get Dialog Values
279 280
			GetDlgStuff(hdlg, &lpsetupdlg->ci);

281
			// Update ODBC.INI
282
			SetDSNAttributes(hdlg, lpsetupdlg);
283 284
        }

285
		// Return to caller
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		case IDCANCEL:
			EndDialog(hdlg, wParam);
			return TRUE;

		case IDC_DRIVER:

			DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DRV),
									hdlg, driver_optionsProc, (LPARAM) NULL);

			return TRUE;

		case IDC_DATASOURCE:
			{
			LPSETUPDLG lpsetupdlg;

			lpsetupdlg = (LPSETUPDLG)GetWindowLong(hdlg, DWL_USER);

			DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DS),
									hdlg, ds_optionsProc, (LPARAM) &lpsetupdlg->ci);

			return TRUE;
			}
		}

		break;
	}

313
	// Message not processed
314
	return FALSE;
315 316 317 318 319 320 321 322 323 324
}


/* ParseAttributes ---------------------------------------------------------
  Description:  Parse attribute string moving values into the aAttr array
  Input      :  lpszAttributes - Pointer to attribute string
  Output     :  None (global aAttr normally updated)
--------------------------------------------------------------------------*/
void INTFUNC ParseAttributes(LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg)
{
325 326 327 328
LPCSTR  lpsz;
LPCSTR  lpszStart;
char    aszKey[MAXKEYLEN];
int     cbKey;
329
char    value[MAXPGPATH];
330 331

		memset(&lpsetupdlg->ci, 0, sizeof(ConnInfo));
332 333

        for (lpsz=lpszAttributes; *lpsz; lpsz++)
334
        {  //  Extract key name (e.g., DSN), it must be terminated by an equals
335 336 337 338
                lpszStart = lpsz;
                for (;; lpsz++)
                {
                        if (!*lpsz)
339
                                return;         // No key was found
340
                        else if (*lpsz == '=')
341
                                break;          // Valid key found
342
                }
343
                // Determine the key's index in the key table (-1 if not found)
344 345 346 347 348 349 350 351
                cbKey    = lpsz - lpszStart;
                if (cbKey < sizeof(aszKey))
                {

                        _fmemcpy(aszKey, lpszStart, cbKey);
                        aszKey[cbKey] = '\0';
                }

352
                // Locate end of key value
353 354 355
                lpszStart = ++lpsz;
                for (; *lpsz; lpsz++);

356

357
                // lpsetupdlg->aAttr[iElement].fSupplied = TRUE;
358
                _fmemcpy(value, lpszStart, MIN(lpsz-lpszStart+1, MAXPGPATH));
359 360 361

				mylog("aszKey='%s', value='%s'\n", aszKey, value);

362
				//	Copy the appropriate value to the conninfo 
363
				copyAttributes(&lpsetupdlg->ci, aszKey, value);
364 365 366 367 368 369 370 371 372 373 374 375 376
        }
        return;
}


/* SetDSNAttributes --------------------------------------------------------
  Description:  Write data source attributes to ODBC.INI
  Input      :  hwnd - Parent window handle (plus globals)
  Output     :  TRUE if successful, FALSE otherwise
--------------------------------------------------------------------------*/

BOOL INTFUNC SetDSNAttributes(HWND hwndParent, LPSETUPDLG lpsetupdlg)
{
377
LPCSTR  lpszDSN;                                                // Pointer to data source name
378
    
379
        lpszDSN = lpsetupdlg->ci.dsn;
380

381
        // Validate arguments
382
        if (lpsetupdlg->fNewDSN && !*lpsetupdlg->ci.dsn)
383 384
                return FALSE;

385
        // Write the data source name
386 387 388 389
        if (!SQLWriteDSNToIni(lpszDSN, lpsetupdlg->lpszDrvr))
        {
                if (hwndParent)
                {
390 391
                        char  szBuf[MAXPGPATH];
                        char  szMsg[MAXPGPATH];
392 393 394 395 396 397 398 399 400 401

                        LoadString(s_hModule, IDS_BADDSN, szBuf, sizeof(szBuf));
                        wsprintf(szMsg, szBuf, lpszDSN);
                        LoadString(s_hModule, IDS_MSGTITLE, szBuf, sizeof(szBuf));
                        MessageBox(hwndParent, szMsg, szBuf, MB_ICONEXCLAMATION | MB_OK);
                }
                return FALSE;
        }


402
        // Update ODBC.INI
403 404
		writeDSNinfo(&lpsetupdlg->ci);

405

406
        // If the data source name has changed, remove the old name
407
        if (lstrcmpi(lpsetupdlg->szDSN, lpsetupdlg->ci.dsn))
408 409 410 411 412
        {
                SQLRemoveDSNFromIni(lpsetupdlg->szDSN);
        }
        return TRUE;
}