~ubuntu-branches/ubuntu/lucid/psqlodbc/lucid

« back to all changes in this revision

Viewing changes to setup.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-05-13 10:47:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040513104736-a530gmn0p3knep89
Tags: upstream-07.03.0200
ImportĀ upstreamĀ versionĀ 07.03.0200

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------
 
2
 * Module:                      setup.c
 
3
 *
 
4
 * Description:         This module contains the setup functions for
 
5
 *                                      adding/modifying a Data Source in the ODBC.INI portion
 
6
 *                                      of the registry.
 
7
 *
 
8
 * Classes:                     n/a
 
9
 *
 
10
 * API functions:       ConfigDSN
 
11
 *
 
12
 * Comments:            See "notice.txt" for copyright and license information.
 
13
 *-------
 
14
 */
 
15
 
 
16
#include  "psqlodbc.h"
 
17
 
 
18
#include  "connection.h"
 
19
#include  <windowsx.h>
 
20
#include  <string.h>
 
21
#include  <stdlib.h>
 
22
#include  "resource.h"
 
23
#include  "dlg_specific.h"
 
24
#include  "win_setup.h"
 
25
 
 
26
 
 
27
#define INTFUNC  __stdcall
 
28
 
 
29
extern HINSTANCE NEAR s_hModule;        /* Saved module handle. */
 
30
extern GLOBAL_VALUES    globals;
 
31
 
 
32
/* Constants */
 
33
#define MIN(x,y)          ((x) < (y) ? (x) : (y))
 
34
 
 
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 */
 
38
 
 
39
 
 
40
/*--------
 
41
 *      ConfigDSN
 
42
 *
 
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
 
51
 *--------
 
52
 */
 
53
BOOL            CALLBACK
 
54
ConfigDSN(HWND hwnd,
 
55
                  WORD fRequest,
 
56
                  LPCSTR lpszDriver,
 
57
                  LPCSTR lpszAttributes)
 
58
{
 
59
        BOOL            fSuccess;               /* Success/fail flag */
 
60
        GLOBALHANDLE hglbAttr;
 
61
        LPSETUPDLG      lpsetupdlg;
 
62
 
 
63
 
 
64
        /* Allocate attribute array */
 
65
        hglbAttr = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(SETUPDLG));
 
66
        if (!hglbAttr)
 
67
                return FALSE;
 
68
        lpsetupdlg = (LPSETUPDLG) GlobalLock(hglbAttr);
 
69
        /* Parse attribute string */
 
70
        if (lpszAttributes)
 
71
                ParseAttributes(lpszAttributes, lpsetupdlg);
 
72
 
 
73
        /* Save original data source name */
 
74
        if (lpsetupdlg->ci.dsn[0])
 
75
                lstrcpy(lpsetupdlg->szDSN, lpsetupdlg->ci.dsn);
 
76
        else
 
77
                lpsetupdlg->szDSN[0] = '\0';
 
78
 
 
79
        /* Remove data source */
 
80
        if (ODBC_REMOVE_DSN == fRequest)
 
81
        {
 
82
                /* Fail if no data source name was supplied */
 
83
                if (!lpsetupdlg->ci.dsn[0])
 
84
                        fSuccess = FALSE;
 
85
 
 
86
                /* Otherwise remove data source from ODBC.INI */
 
87
                else
 
88
                        fSuccess = SQLRemoveDSNFromIni(lpsetupdlg->ci.dsn);
 
89
        }
 
90
        /* Add or Configure data source */
 
91
        else
 
92
        {
 
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);
 
98
 
 
99
                /*
 
100
                 * Display the appropriate dialog (if parent window handle
 
101
                 * supplied)
 
102
                 */
 
103
                if (hwnd)
 
104
                {
 
105
                        /* Display dialog(s) */
 
106
                        fSuccess = (IDOK == DialogBoxParam(s_hModule,
 
107
                                                                                         MAKEINTRESOURCE(DLG_CONFIG),
 
108
                                                                                           hwnd,
 
109
                                                                                           ConfigDlgProc,
 
110
                                                                                         (LONG) (LPSTR) lpsetupdlg));
 
111
                }
 
112
                else if (lpsetupdlg->ci.dsn[0])
 
113
                        fSuccess = SetDSNAttributes(hwnd, lpsetupdlg, NULL);
 
114
                else
 
115
                        fSuccess = FALSE;
 
116
        }
 
117
 
 
118
        GlobalUnlock(hglbAttr);
 
119
        GlobalFree(hglbAttr);
 
120
 
 
121
        return fSuccess;
 
122
}
 
123
 
 
124
 
 
125
/*-------
 
126
 * CenterDialog
 
127
 *
 
128
 *              Description:  Center the dialog over the frame window
 
129
 *              Input      :  hdlg -- Dialog window handle
 
130
 *              Output     :  None
 
131
 *-------
 
132
 */
 
133
void            INTFUNC
 
134
CenterDialog(HWND hdlg)
 
135
{
 
136
        HWND            hwndFrame;
 
137
        RECT            rcDlg,
 
138
                                rcScr,
 
139
                                rcFrame;
 
140
        int                     cx,
 
141
                                cy;
 
142
 
 
143
        hwndFrame = GetParent(hdlg);
 
144
 
 
145
        GetWindowRect(hdlg, &rcDlg);
 
146
        cx = rcDlg.right - rcDlg.left;
 
147
        cy = rcDlg.bottom - rcDlg.top;
 
148
 
 
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;
 
156
 
 
157
        GetWindowRect(GetDesktopWindow(), &rcScr);
 
158
        if (rcDlg.bottom > rcScr.bottom)
 
159
        {
 
160
                rcDlg.bottom = rcScr.bottom;
 
161
                rcDlg.top = rcDlg.bottom - cy;
 
162
        }
 
163
        if (rcDlg.right > rcScr.right)
 
164
        {
 
165
                rcDlg.right = rcScr.right;
 
166
                rcDlg.left = rcDlg.right - cx;
 
167
        }
 
168
 
 
169
        if (rcDlg.left < 0)
 
170
                rcDlg.left = 0;
 
171
        if (rcDlg.top < 0)
 
172
                rcDlg.top = 0;
 
173
 
 
174
        MoveWindow(hdlg, rcDlg.left, rcDlg.top, cx, cy, TRUE);
 
175
        return;
 
176
}
 
177
 
 
178
/*-------
 
179
 * ConfigDlgProc
 
180
 *      Description:    Manage add data source name dialog
 
181
 *      Input    :      hdlg --- Dialog window handle
 
182
 *                              wMsg --- Message
 
183
 *                              wParam - Message parameter
 
184
 *                              lParam - Message parameter
 
185
 *      Output   :      TRUE if message processed, FALSE otherwise
 
186
 *-------
 
187
 */
 
188
int                     CALLBACK
 
189
ConfigDlgProc(HWND hdlg,
 
190
                          UINT wMsg,
 
191
                          WPARAM wParam,
 
192
                          LPARAM lParam)
 
193
{
 
194
        LPSETUPDLG      lpsetupdlg;
 
195
        ConnInfo   *ci;
 
196
        DWORD           cmd;
 
197
        char            strbuf[64];
 
198
 
 
199
        switch (wMsg)
 
200
        {
 
201
                        /* Initialize the dialog */
 
202
                case WM_INITDIALOG:
 
203
                        lpsetupdlg = (LPSETUPDLG) lParam;
 
204
                        ci = &lpsetupdlg->ci;
 
205
 
 
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);
 
210
 
 
211
                        SetWindowLong(hdlg, DWL_USER, lParam);
 
212
                        CenterDialog(hdlg); /* Center dialog */
 
213
 
 
214
                        /*
 
215
                         * NOTE: Values supplied in the attribute string will always
 
216
                         */
 
217
                        /* override settings in ODBC.INI */
 
218
 
 
219
                        memcpy(&ci->drivers, &globals, sizeof(globals));
 
220
                        /* Get the rest of the common attributes */
 
221
                        getDSNinfo(ci, CONN_DONT_OVERWRITE);
 
222
 
 
223
                        /* Fill in any defaults */
 
224
                        getDSNdefaults(ci);
 
225
 
 
226
                        /* Initialize dialog fields */
 
227
                        SetDlgStuff(hdlg, ci);
 
228
 
 
229
                        if (lpsetupdlg->fNewDSN || !ci->dsn[0])
 
230
                                ShowWindow(GetDlgItem(hdlg, IDC_MANAGEDSN), SW_HIDE);
 
231
                        if (lpsetupdlg->fDefault)
 
232
                        {
 
233
                                EnableWindow(GetDlgItem(hdlg, IDC_DSNAME), FALSE);
 
234
                                EnableWindow(GetDlgItem(hdlg, IDC_DSNAMETEXT), FALSE);
 
235
                        }
 
236
                        else
 
237
                                SendDlgItemMessage(hdlg, IDC_DSNAME,
 
238
                                                         EM_LIMITTEXT, (WPARAM) (MAXDSNAME - 1), 0L);
 
239
 
 
240
                        SendDlgItemMessage(hdlg, IDC_DESC,
 
241
                                                           EM_LIMITTEXT, (WPARAM) (MAXDESC - 1), 0L);
 
242
                        return TRUE;            /* Focus was not set */
 
243
 
 
244
                        /* Process buttons */
 
245
                case WM_COMMAND:
 
246
                        switch (cmd = GET_WM_COMMAND_ID(wParam, lParam))
 
247
                        {
 
248
                                        /*
 
249
                                         * Ensure the OK button is enabled only when a data
 
250
                                         * source name
 
251
                                         */
 
252
                                        /* is entered */
 
253
                                case IDC_DSNAME:
 
254
                                        if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE)
 
255
                                        {
 
256
                                                char            szItem[MAXDSNAME];      /* Edit control text */
 
257
 
 
258
                                                /* Enable/disable the OK button */
 
259
                                                EnableWindow(GetDlgItem(hdlg, IDOK),
 
260
                                                                         GetDlgItemText(hdlg, IDC_DSNAME,
 
261
                                                                                                szItem, sizeof(szItem)));
 
262
                                                return TRUE;
 
263
                                        }
 
264
                                        break;
 
265
 
 
266
                                        /* Accept results */
 
267
                                case IDOK:
 
268
                                case IDAPPLY:
 
269
                                        lpsetupdlg = (LPSETUPDLG) GetWindowLong(hdlg, DWL_USER);
 
270
                                        /* Retrieve dialog values */
 
271
                                        if (!lpsetupdlg->fDefault)
 
272
                                                GetDlgItemText(hdlg, IDC_DSNAME,
 
273
                                                                           lpsetupdlg->ci.dsn,
 
274
                                                                           sizeof(lpsetupdlg->ci.dsn));
 
275
                                        /* Get Dialog Values */
 
276
                                        GetDlgStuff(hdlg, &lpsetupdlg->ci);
 
277
 
 
278
                                        /* Update ODBC.INI */
 
279
                                        SetDSNAttributes(hdlg, lpsetupdlg, NULL);
 
280
                                        if (IDAPPLY == cmd)
 
281
                                                break;
 
282
                                        /* Return to caller */
 
283
                                case IDCANCEL:
 
284
                                        EndDialog(hdlg, wParam);
 
285
                                        return TRUE;
 
286
 
 
287
                                case IDC_DATASOURCE:
 
288
                                        lpsetupdlg = (LPSETUPDLG) GetWindowLong(hdlg, DWL_USER);
 
289
                                        DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DRV),
 
290
                                         hdlg, ds_options1Proc, (LPARAM) &lpsetupdlg->ci);
 
291
                                        return TRUE;
 
292
 
 
293
                                case IDC_DRIVER:
 
294
                                        lpsetupdlg = (LPSETUPDLG) GetWindowLong(hdlg, DWL_USER);
 
295
                                        DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_GLOBAL),
 
296
                                                 hdlg, global_optionsProc, (LPARAM) &lpsetupdlg->ci);
 
297
 
 
298
                                        return TRUE;
 
299
                                case IDC_MANAGEDSN:
 
300
                                        lpsetupdlg = (LPSETUPDLG) GetWindowLong(hdlg, DWL_USER);
 
301
                                        if (DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_DRIVER_CHANGE),
 
302
                                                hdlg, manage_dsnProc,
 
303
                                                (LPARAM) lpsetupdlg) > 0)
 
304
                                                EndDialog(hdlg, 0);
 
305
 
 
306
                                        return TRUE;
 
307
                        }
 
308
                        break;
 
309
        }
 
310
 
 
311
        /* Message not processed */
 
312
        return FALSE;
 
313
}
 
314
 
 
315
 
 
316
/*-------
 
317
 * ParseAttributes
 
318
 *
 
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)
 
322
 *-------
 
323
 */
 
324
void            INTFUNC
 
325
ParseAttributes(LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg)
 
326
{
 
327
        LPCSTR          lpsz;
 
328
        LPCSTR          lpszStart;
 
329
        char            aszKey[MAXKEYLEN];
 
330
        int                     cbKey;
 
331
        char            value[MAXPGPATH];
 
332
 
 
333
        CC_conninfo_init(&(lpsetupdlg->ci));
 
334
 
 
335
        for (lpsz = lpszAttributes; *lpsz; lpsz++)
 
336
        {
 
337
                /*
 
338
                 * Extract key name (e.g., DSN), it must be terminated by an
 
339
                 * equals
 
340
                 */
 
341
                lpszStart = lpsz;
 
342
                for (;; lpsz++)
 
343
                {
 
344
                        if (!*lpsz)
 
345
                                return;                 /* No key was found */
 
346
                        else if (*lpsz == '=')
 
347
                                break;                  /* Valid key found */
 
348
                }
 
349
                /* Determine the key's index in the key table (-1 if not found) */
 
350
                cbKey = lpsz - lpszStart;
 
351
                if (cbKey < sizeof(aszKey))
 
352
                {
 
353
                        _fmemcpy(aszKey, lpszStart, cbKey);
 
354
                        aszKey[cbKey] = '\0';
 
355
                }
 
356
 
 
357
                /* Locate end of key value */
 
358
                lpszStart = ++lpsz;
 
359
                for (; *lpsz; lpsz++)
 
360
                        ;
 
361
 
 
362
                /* lpsetupdlg->aAttr[iElement].fSupplied = TRUE; */
 
363
                _fmemcpy(value, lpszStart, MIN(lpsz - lpszStart + 1, MAXPGPATH));
 
364
 
 
365
                mylog("aszKey='%s', value='%s'\n", aszKey, value);
 
366
 
 
367
                /* Copy the appropriate value to the conninfo  */
 
368
                copyAttributes(&lpsetupdlg->ci, aszKey, value);
 
369
        }
 
370
        return;
 
371
}
 
372
 
 
373
 
 
374
/*--------
 
375
 * SetDSNAttributes
 
376
 *
 
377
 *      Description:    Write data source attributes to ODBC.INI
 
378
 *      Input    :      hwnd - Parent window handle (plus globals)
 
379
 *      Output   :      TRUE if successful, FALSE otherwise
 
380
 *--------
 
381
 */
 
382
BOOL            INTFUNC
 
383
SetDSNAttributes(HWND hwndParent, LPSETUPDLG lpsetupdlg, DWORD *errcode)
 
384
{
 
385
        LPCSTR          lpszDSN;                /* Pointer to data source name */
 
386
 
 
387
        lpszDSN = lpsetupdlg->ci.dsn;
 
388
 
 
389
        if (errcode)
 
390
                *errcode = 0;
 
391
        /* Validate arguments */
 
392
        if (lpsetupdlg->fNewDSN && !*lpsetupdlg->ci.dsn)
 
393
                return FALSE;
 
394
 
 
395
        /* Write the data source name */
 
396
        if (!SQLWriteDSNToIni(lpszDSN, lpsetupdlg->lpszDrvr))
 
397
        {
 
398
                RETCODE ret = SQL_ERROR;
 
399
                DWORD   err = SQL_ERROR;
 
400
                char    szMsg[SQL_MAX_MESSAGE_LENGTH];
 
401
 
 
402
#if (ODBCVER >= 0x0300)
 
403
                ret = SQLInstallerError(1, &err, szMsg, sizeof(szMsg), NULL);
 
404
#endif /* ODBCVER */
 
405
                if (hwndParent)
 
406
                {
 
407
                        char            szBuf[MAXPGPATH];
 
408
 
 
409
                        if (SQL_SUCCESS != ret)
 
410
                        {
 
411
                                LoadString(s_hModule, IDS_BADDSN, szBuf, sizeof(szBuf));
 
412
                                wsprintf(szMsg, szBuf, lpszDSN);
 
413
                        }
 
414
                        LoadString(s_hModule, IDS_MSGTITLE, szBuf, sizeof(szBuf));
 
415
                        MessageBox(hwndParent, szMsg, szBuf, MB_ICONEXCLAMATION | MB_OK);
 
416
                }
 
417
                if (errcode)
 
418
                        *errcode = err;
 
419
                return FALSE;
 
420
        }
 
421
 
 
422
        /* Update ODBC.INI */
 
423
        writeDriverCommoninfo(ODBC_INI, lpsetupdlg->ci.dsn, &(lpsetupdlg->ci.drivers));
 
424
        writeDSNinfo(&lpsetupdlg->ci);
 
425
 
 
426
        /* If the data source name has changed, remove the old name */
 
427
        if (lstrcmpi(lpsetupdlg->szDSN, lpsetupdlg->ci.dsn))
 
428
                SQLRemoveDSNFromIni(lpsetupdlg->szDSN);
 
429
        return TRUE;
 
430
}
 
431
 
 
432
 
 
433
#ifdef  WIN32
 
434
 
 
435
BOOL    INTFUNC
 
436
ChangeDriverName(HWND hwndParent, LPSETUPDLG lpsetupdlg, LPCSTR driver_name)
 
437
{
 
438
        DWORD   err = 0;
 
439
        ConnInfo        *ci = &lpsetupdlg->ci;
 
440
 
 
441
        if (!ci->dsn[0])
 
442
        {
 
443
                err = IDS_BADDSN;
 
444
        }
 
445
        else if (!driver_name || strnicmp(driver_name, "postgresql", 10))
 
446
        {
 
447
                err = IDS_BADDSN;
 
448
        }
 
449
        else
 
450
        {
 
451
                LPCSTR  lpszDrvr = lpsetupdlg->lpszDrvr;
 
452
 
 
453
                lpsetupdlg->lpszDrvr = driver_name;
 
454
                if (!SetDSNAttributes(hwndParent, lpsetupdlg, &err))
 
455
                {
 
456
                        if (!err)
 
457
                                err = IDS_BADDSN;
 
458
                        lpsetupdlg->lpszDrvr = lpszDrvr;
 
459
                }
 
460
        }
 
461
        return (err == 0);
 
462
}
 
463
 
 
464
#endif /* WIN32 */