4
* Description: This module contains the setup functions for
5
* adding/modifying a Data Source in the ODBC.INI portion
10
* API functions: ConfigDSN
12
* Comments: See "notice.txt" for copyright and license information.
18
#include "connection.h"
23
#include "dlg_specific.h"
24
#include "win_setup.h"
27
#define INTFUNC __stdcall
29
extern HINSTANCE NEAR s_hModule; /* Saved module handle. */
30
extern GLOBAL_VALUES globals;
33
#define MIN(x,y) ((x) < (y) ? (x) : (y))
35
#define MAXKEYLEN (15+1) /* Max keyword length */
36
#define MAXDESC (255+1) /* Max description length */
37
#define MAXDSNAME (32+1) /* Max data source name length */
43
* Description: ODBC Setup entry point
44
* This entry point is called by the ODBC Installer
45
* (see file header for more details)
46
* Input : hwnd ----------- Parent window handle
47
* fRequest ------- Request type (i.e., add, config, or remove)
48
* lpszDriver ----- Driver name
49
* lpszAttributes - data source attribute string
50
* Output : TRUE success, FALSE otherwise
57
LPCSTR lpszAttributes)
59
BOOL fSuccess; /* Success/fail flag */
60
GLOBALHANDLE hglbAttr;
61
LPSETUPDLG lpsetupdlg;
64
/* Allocate attribute array */
65
hglbAttr = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(SETUPDLG));
68
lpsetupdlg = (LPSETUPDLG) GlobalLock(hglbAttr);
69
/* Parse attribute string */
71
ParseAttributes(lpszAttributes, lpsetupdlg);
73
/* Save original data source name */
74
if (lpsetupdlg->ci.dsn[0])
75
lstrcpy(lpsetupdlg->szDSN, lpsetupdlg->ci.dsn);
77
lpsetupdlg->szDSN[0] = '\0';
79
/* Remove data source */
80
if (ODBC_REMOVE_DSN == fRequest)
82
/* Fail if no data source name was supplied */
83
if (!lpsetupdlg->ci.dsn[0])
86
/* Otherwise remove data source from ODBC.INI */
88
fSuccess = SQLRemoveDSNFromIni(lpsetupdlg->ci.dsn);
90
/* Add or Configure data source */
93
/* Save passed variables for global access (e.g., dialog access) */
94
lpsetupdlg->hwndParent = hwnd;
95
lpsetupdlg->lpszDrvr = lpszDriver;
96
lpsetupdlg->fNewDSN = (ODBC_ADD_DSN == fRequest);
97
lpsetupdlg->fDefault = !lstrcmpi(lpsetupdlg->ci.dsn, INI_DSN);
100
* Display the appropriate dialog (if parent window handle
105
/* Display dialog(s) */
106
fSuccess = (IDOK == DialogBoxParam(s_hModule,
107
MAKEINTRESOURCE(DLG_CONFIG),
110
(LONG) (LPSTR) lpsetupdlg));
112
else if (lpsetupdlg->ci.dsn[0])
113
fSuccess = SetDSNAttributes(hwnd, lpsetupdlg, NULL);
118
GlobalUnlock(hglbAttr);
119
GlobalFree(hglbAttr);
128
* Description: Center the dialog over the frame window
129
* Input : hdlg -- Dialog window handle
134
CenterDialog(HWND hdlg)
143
hwndFrame = GetParent(hdlg);
145
GetWindowRect(hdlg, &rcDlg);
146
cx = rcDlg.right - rcDlg.left;
147
cy = rcDlg.bottom - rcDlg.top;
149
GetClientRect(hwndFrame, &rcFrame);
150
ClientToScreen(hwndFrame, (LPPOINT) (&rcFrame.left));
151
ClientToScreen(hwndFrame, (LPPOINT) (&rcFrame.right));
152
rcDlg.top = rcFrame.top + (((rcFrame.bottom - rcFrame.top) - cy) >> 1);
153
rcDlg.left = rcFrame.left + (((rcFrame.right - rcFrame.left) - cx) >> 1);
154
rcDlg.bottom = rcDlg.top + cy;
155
rcDlg.right = rcDlg.left + cx;
157
GetWindowRect(GetDesktopWindow(), &rcScr);
158
if (rcDlg.bottom > rcScr.bottom)
160
rcDlg.bottom = rcScr.bottom;
161
rcDlg.top = rcDlg.bottom - cy;
163
if (rcDlg.right > rcScr.right)
165
rcDlg.right = rcScr.right;
166
rcDlg.left = rcDlg.right - cx;
174
MoveWindow(hdlg, rcDlg.left, rcDlg.top, cx, cy, TRUE);
180
* Description: Manage add data source name dialog
181
* Input : hdlg --- Dialog window handle
183
* wParam - Message parameter
184
* lParam - Message parameter
185
* Output : TRUE if message processed, FALSE otherwise
189
ConfigDlgProc(HWND hdlg,
194
LPSETUPDLG lpsetupdlg;
201
/* Initialize the dialog */
203
lpsetupdlg = (LPSETUPDLG) lParam;
204
ci = &lpsetupdlg->ci;
206
/* Hide the driver connect message */
207
ShowWindow(GetDlgItem(hdlg, DRV_MSG_LABEL), SW_HIDE);
208
LoadString(s_hModule, IDS_ADVANCE_SAVE, strbuf, sizeof(strbuf));
209
SetWindowText(GetDlgItem(hdlg, IDOK), strbuf);
211
SetWindowLong(hdlg, DWL_USER, lParam);
212
CenterDialog(hdlg); /* Center dialog */
215
* NOTE: Values supplied in the attribute string will always
217
/* override settings in ODBC.INI */
219
memcpy(&ci->drivers, &globals, sizeof(globals));
220
/* Get the rest of the common attributes */
221
getDSNinfo(ci, CONN_DONT_OVERWRITE);
223
/* Fill in any defaults */
226
/* Initialize dialog fields */
227
SetDlgStuff(hdlg, ci);
229
if (lpsetupdlg->fNewDSN || !ci->dsn[0])
230
ShowWindow(GetDlgItem(hdlg, IDC_MANAGEDSN), SW_HIDE);
231
if (lpsetupdlg->fDefault)
233
EnableWindow(GetDlgItem(hdlg, IDC_DSNAME), FALSE);
234
EnableWindow(GetDlgItem(hdlg, IDC_DSNAMETEXT), FALSE);
237
SendDlgItemMessage(hdlg, IDC_DSNAME,
238
EM_LIMITTEXT, (WPARAM) (MAXDSNAME - 1), 0L);
240
SendDlgItemMessage(hdlg, IDC_DESC,
241
EM_LIMITTEXT, (WPARAM) (MAXDESC - 1), 0L);
242
return TRUE; /* Focus was not set */
244
/* Process buttons */
246
switch (cmd = GET_WM_COMMAND_ID(wParam, lParam))
249
* Ensure the OK button is enabled only when a data
254
if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE)
256
char szItem[MAXDSNAME]; /* Edit control text */
258
/* Enable/disable the OK button */
259
EnableWindow(GetDlgItem(hdlg, IDOK),
260
GetDlgItemText(hdlg, IDC_DSNAME,
261
szItem, sizeof(szItem)));
269
lpsetupdlg = (LPSETUPDLG) GetWindowLong(hdlg, DWL_USER);
270
/* Retrieve dialog values */
271
if (!lpsetupdlg->fDefault)
272
GetDlgItemText(hdlg, IDC_DSNAME,
274
sizeof(lpsetupdlg->ci.dsn));
275
/* Get Dialog Values */
276
GetDlgStuff(hdlg, &lpsetupdlg->ci);
278
/* Update ODBC.INI */
279
SetDSNAttributes(hdlg, lpsetupdlg, NULL);
282
/* Return to caller */
284
EndDialog(hdlg, wParam);
288
lpsetupdlg = (LPSETUPDLG) GetWindowLong(hdlg, DWL_USER);
289
DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DRV),
290
hdlg, ds_options1Proc, (LPARAM) &lpsetupdlg->ci);
294
lpsetupdlg = (LPSETUPDLG) GetWindowLong(hdlg, DWL_USER);
295
DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_GLOBAL),
296
hdlg, global_optionsProc, (LPARAM) &lpsetupdlg->ci);
300
lpsetupdlg = (LPSETUPDLG) GetWindowLong(hdlg, DWL_USER);
301
if (DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_DRIVER_CHANGE),
302
hdlg, manage_dsnProc,
303
(LPARAM) lpsetupdlg) > 0)
311
/* Message not processed */
319
* Description: Parse attribute string moving values into the aAttr array
320
* Input : lpszAttributes - Pointer to attribute string
321
* Output : None (global aAttr normally updated)
325
ParseAttributes(LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg)
329
char aszKey[MAXKEYLEN];
331
char value[MAXPGPATH];
333
CC_conninfo_init(&(lpsetupdlg->ci));
335
for (lpsz = lpszAttributes; *lpsz; lpsz++)
338
* Extract key name (e.g., DSN), it must be terminated by an
345
return; /* No key was found */
346
else if (*lpsz == '=')
347
break; /* Valid key found */
349
/* Determine the key's index in the key table (-1 if not found) */
350
cbKey = lpsz - lpszStart;
351
if (cbKey < sizeof(aszKey))
353
_fmemcpy(aszKey, lpszStart, cbKey);
354
aszKey[cbKey] = '\0';
357
/* Locate end of key value */
359
for (; *lpsz; lpsz++)
362
/* lpsetupdlg->aAttr[iElement].fSupplied = TRUE; */
363
_fmemcpy(value, lpszStart, MIN(lpsz - lpszStart + 1, MAXPGPATH));
365
mylog("aszKey='%s', value='%s'\n", aszKey, value);
367
/* Copy the appropriate value to the conninfo */
368
copyAttributes(&lpsetupdlg->ci, aszKey, value);
377
* Description: Write data source attributes to ODBC.INI
378
* Input : hwnd - Parent window handle (plus globals)
379
* Output : TRUE if successful, FALSE otherwise
383
SetDSNAttributes(HWND hwndParent, LPSETUPDLG lpsetupdlg, DWORD *errcode)
385
LPCSTR lpszDSN; /* Pointer to data source name */
387
lpszDSN = lpsetupdlg->ci.dsn;
391
/* Validate arguments */
392
if (lpsetupdlg->fNewDSN && !*lpsetupdlg->ci.dsn)
395
/* Write the data source name */
396
if (!SQLWriteDSNToIni(lpszDSN, lpsetupdlg->lpszDrvr))
398
RETCODE ret = SQL_ERROR;
399
DWORD err = SQL_ERROR;
400
char szMsg[SQL_MAX_MESSAGE_LENGTH];
402
#if (ODBCVER >= 0x0300)
403
ret = SQLInstallerError(1, &err, szMsg, sizeof(szMsg), NULL);
407
char szBuf[MAXPGPATH];
409
if (SQL_SUCCESS != ret)
411
LoadString(s_hModule, IDS_BADDSN, szBuf, sizeof(szBuf));
412
wsprintf(szMsg, szBuf, lpszDSN);
414
LoadString(s_hModule, IDS_MSGTITLE, szBuf, sizeof(szBuf));
415
MessageBox(hwndParent, szMsg, szBuf, MB_ICONEXCLAMATION | MB_OK);
422
/* Update ODBC.INI */
423
writeDriverCommoninfo(ODBC_INI, lpsetupdlg->ci.dsn, &(lpsetupdlg->ci.drivers));
424
writeDSNinfo(&lpsetupdlg->ci);
426
/* If the data source name has changed, remove the old name */
427
if (lstrcmpi(lpsetupdlg->szDSN, lpsetupdlg->ci.dsn))
428
SQLRemoveDSNFromIni(lpsetupdlg->szDSN);
436
ChangeDriverName(HWND hwndParent, LPSETUPDLG lpsetupdlg, LPCSTR driver_name)
439
ConnInfo *ci = &lpsetupdlg->ci;
445
else if (!driver_name || strnicmp(driver_name, "postgresql", 10))
451
LPCSTR lpszDrvr = lpsetupdlg->lpszDrvr;
453
lpsetupdlg->lpszDrvr = driver_name;
454
if (!SetDSNAttributes(hwndParent, lpsetupdlg, &err))
458
lpsetupdlg->lpszDrvr = lpszDrvr;