1/*
2 *  IODBCProxy_SetupController.m
3 *
4 *  $Id$
5 *
6 *  The iODBC driver manager.
7 *
8 *  Copyright (C) 1996-2021 OpenLink Software <iodbc@openlinksw.com>
9 *  All Rights Reserved.
10 *
11 *  This software is released under the terms of either of the following
12 *  licenses:
13 *
14 *      - GNU Library General Public License (see LICENSE.LGPL)
15 *      - The BSD License (see LICENSE.BSD).
16 *
17 *  Note that the only valid version of the LGPL license as far as this
18 *  project is concerned is the original GNU Library General Public License
19 *  Version 2, dated June 1991.
20 *
21 *  While not mandated by the BSD license, any patches you make to the
22 *  iODBC source code may be contributed back into the iODBC project
23 *  at your discretion. Contributions will benefit the Open Source and
24 *  Data Access community as a whole. Submissions may be made at:
25 *
26 *      http://www.iodbc.org
27 *
28 *
29 *  GNU Library Generic Public License Version 2
30 *  ============================================
31 *  This library is free software; you can redistribute it and/or
32 *  modify it under the terms of the GNU Library General Public
33 *  License as published by the Free Software Foundation; only
34 *  Version 2 of the License dated June 1991.
35 *
36 *  This library is distributed in the hope that it will be useful,
37 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
38 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39 *  Library General Public License for more details.
40 *
41 *  You should have received a copy of the GNU Library General Public
42 *  License along with this library; if not, write to the Free
43 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
44 *
45 *
46 *  The BSD License
47 *  ===============
48 *  Redistribution and use in source and binary forms, with or without
49 *  modification, are permitted provided that the following conditions
50 *  are met:
51 *
52 *  1. Redistributions of source code must retain the above copyright
53 *     notice, this list of conditions and the following disclaimer.
54 *  2. Redistributions in binary form must reproduce the above copyright
55 *     notice, this list of conditions and the following disclaimer in
56 *     the documentation and/or other materials provided with the
57 *     distribution.
58 *  3. Neither the name of OpenLink Software Inc. nor the names of its
59 *     contributors may be used to endorse or promote products derived
60 *     from this software without specific prior written permission.
61 *
62 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
63 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
64 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
66 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
69 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
70 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 */
74
75#import "IODBCProxy_SetupController.h"
76#import "utils.h"
77
78static char *STRCONN = "DSN=%s\0Description=%s\0\0";
79static int STRCONN_NB_TOKENS = 2;
80
81
82char* showSetup(char* dsn, char* attrs, BOOL addEnable)
83{
84    char *connstr = (char*) - 1L;
85	@autoreleasepool {
86        NSApplication *app = [NSApplication sharedApplication];
87
88        IODBCProxy_SetupController *dlg = [[IODBCProxy_SetupController alloc] initWithAttrs:attrs];
89        if (dsn)
90            dlg.d_dsn = conv_char_to_NSString(dsn);
91        dlg._addEnabled = addEnable;
92
93        NSInteger rc = [app runModalForWindow:dlg.window];
94        [dlg.window orderOut:dlg.window];
95        if (rc == 1){
96            char *cour, *curr;
97            int i = 0, size = 0;
98            char *val,*key;
99            char *dsn,*comment;
100            Size len;
101
102            dsn = (char*)conv_NSString_to_char(dlg.d_dsn);
103            comment = (char*)conv_NSString_to_char(dlg.d_comment);
104            len = (dsn!=NULL)?strlen(dsn):0;
105            size += len + strlen("DSN=") + 1;
106            len = (comment!=NULL)?strlen(comment):0;
107            size += len + strlen("Description=") + 1;
108
109            // Malloc it
110            if ((connstr = (char *) malloc (++size)))
111            {
112                for (curr = STRCONN, cour = connstr;
113                     i < STRCONN_NB_TOKENS; i++, curr += (strlen(curr) + 1))
114                {
115                    switch (i)
116                    {
117                        case 0:
118                            sprintf (cour, curr, dsn?dsn:"");
119                            cour += (strlen(cour) + 1);
120                            break;
121                        case 1:
122                            sprintf (cour, curr, comment?comment:"");
123                            cour += (strlen(cour) + 1);
124                            break;
125                    }
126                }
127
128                for (i = 0; i < dlg.Attrs_list.count; i++)
129                {
130                    NSDictionary *row = [dlg.Attrs_list objectAtIndex:i];
131                    NSString *nskey = (NSString*)[row valueForKey:@"key"];
132                    if ([nskey isEqualToString:@"..."] || nskey.length==0)
133                        continue;
134                    key = (char*)conv_NSString_to_char(nskey);
135                    val = (char*)conv_NSString_to_char((NSString*)[row valueForKey:@"val"]);
136                    cour = connstr;
137                    connstr = (char*) malloc (size + strlen(key) + strlen(val) + 2);
138                    if (connstr)
139                    {
140                        memcpy (connstr, cour, size);
141                        sprintf (connstr + size - 1, "%s=%s", key, val);
142                        free (cour);
143                        size += strlen(key) + strlen(val) + 2;
144                    }
145                    else
146                        connstr = cour;
147
148                    if (key!=NULL) free(key);
149                    if (val!=NULL) free(val);
150                }
151
152                connstr[size - 1] = '\0';
153
154                if (dsn!=NULL) free(dsn);
155                if (comment!=NULL) free(comment);
156            }
157        }
158        [dlg release];
159        return connstr;
160    }
161}
162
163
164
165
166#define MODE_ADD 1
167#define MODE_VIEW 0
168@interface IODBCProxy_SetupController ()
169
170@end
171
172@implementation IODBCProxy_SetupController
173@synthesize Attrs_ArrController;
174@synthesize fld_DSN;
175@synthesize fld_Comment;
176@synthesize btn_Add;
177@synthesize btn_Remove;
178@synthesize d_dsn, d_comment, _addEnabled;
179@synthesize Attrs_list=_Attrs_list;
180
181- (id)initWithAttrs:(const char*)attrs
182{
183    self = [super initWithWindowNibName:@"IODBCProxy_SetupController"];
184    if (self) {
185        self.Attrs_list = [NSMutableArray arrayWithCapacity:16];
186        [self parse_attrs:attrs];
187    }
188    return self;
189}
190
191- (void)dealloc
192{
193    [d_dsn release];
194    [d_comment release];
195    [_Attrs_list release];
196	[super dealloc];
197}
198
199- (id)initWithWindow:(NSWindow *)window
200{
201    self = [super initWithWindow:window];
202    if (self) {
203        // Initialization code here.
204    }
205    return self;
206}
207
208- (void)parse_attrs:(const char *)attrs
209{
210    char *curr, *cour;
211
212    for (curr = (char*) attrs; *curr; curr += (strlen(curr) + 1))
213    {
214        if (!strncasecmp (curr, "Description=", strlen("Description=")))
215            self.d_comment = conv_char_to_NSString(curr + strlen("Description="));
216
217        if (!strncasecmp (curr, "DSN=", strlen("DSN=")) ||
218            !strncasecmp (curr, "Driver=", strlen("Driver=")) ||
219            !strncasecmp (curr, "Description=", strlen("Description=")))
220            continue;
221
222        if ((cour = strchr (curr, '=')))
223        {
224            NSString *key, *val;
225            *cour = '\0';
226            key = conv_char_to_NSString(curr);
227            *cour = '=';
228            val = conv_char_to_NSString(cour+1);
229            [_Attrs_list addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:key!=nil?key:@"", @"key",
230                                    val!=nil?val:@"", @"val", nil]];
231
232        }
233    }
234}
235
236- (void)windowDidLoad
237{
238    [super windowDidLoad];
239
240    _dialogCode = 0;
241    _curMode = MODE_VIEW;
242
243    [[self window] center];  // Center the window.
244    if (d_dsn!=nil){
245        self.window.title = [NSString stringWithFormat:@"Setup of %@", d_dsn];
246        self.fld_DSN.stringValue = d_dsn;
247    }
248    if (d_comment!=nil)
249        self.fld_Comment.stringValue = d_comment;
250}
251
252
253- (void)windowWillClose:(NSNotification*)notification
254{
255    [NSApp stopModalWithCode:_dialogCode];
256}
257
258
259- (IBAction)call_Ok:(id)sender {
260    _dialogCode = 1;
261    self.d_dsn = fld_DSN.stringValue;
262    self.d_comment = fld_Comment.stringValue;
263    [self.window close];
264}
265
266- (IBAction)call_Cancel:(id)sender {
267    _dialogCode = 0;
268    [self.window close];
269}
270
271@end
272