IODBCProxy_SetupController.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * IODBCProxy_SetupController.m
  3. *
  4. * $Id$
  5. *
  6. * The iODBC driver manager.
  7. *
  8. * Copyright (C) 1996-2021 OpenLink Software <[email protected]>
  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. #import "IODBCProxy_SetupController.h"
  75. #import "utils.h"
  76. static char *STRCONN = "DSN=%s\0Description=%s\0\0";
  77. static int STRCONN_NB_TOKENS = 2;
  78. char* showSetup(char* dsn, char* attrs, BOOL addEnable)
  79. {
  80. char *connstr = (char*) - 1L;
  81. @autoreleasepool {
  82. NSApplication *app = [NSApplication sharedApplication];
  83. IODBCProxy_SetupController *dlg = [[IODBCProxy_SetupController alloc] initWithAttrs:attrs];
  84. if (dsn)
  85. dlg.d_dsn = conv_char_to_NSString(dsn);
  86. dlg._addEnabled = addEnable;
  87. NSInteger rc = [app runModalForWindow:dlg.window];
  88. [dlg.window orderOut:dlg.window];
  89. if (rc == 1){
  90. char *cour, *curr;
  91. int i = 0, size = 0;
  92. char *val,*key;
  93. char *dsn,*comment;
  94. Size len;
  95. dsn = (char*)conv_NSString_to_char(dlg.d_dsn);
  96. comment = (char*)conv_NSString_to_char(dlg.d_comment);
  97. len = (dsn!=NULL)?strlen(dsn):0;
  98. size += len + strlen("DSN=") + 1;
  99. len = (comment!=NULL)?strlen(comment):0;
  100. size += len + strlen("Description=") + 1;
  101. // Malloc it
  102. if ((connstr = (char *) malloc (++size)))
  103. {
  104. for (curr = STRCONN, cour = connstr;
  105. i < STRCONN_NB_TOKENS; i++, curr += (strlen(curr) + 1))
  106. {
  107. switch (i)
  108. {
  109. case 0:
  110. sprintf (cour, curr, dsn?dsn:"");
  111. cour += (strlen(cour) + 1);
  112. break;
  113. case 1:
  114. sprintf (cour, curr, comment?comment:"");
  115. cour += (strlen(cour) + 1);
  116. break;
  117. }
  118. }
  119. for (i = 0; i < dlg.Attrs_list.count; i++)
  120. {
  121. NSDictionary *row = [dlg.Attrs_list objectAtIndex:i];
  122. NSString *nskey = (NSString*)[row valueForKey:@"key"];
  123. int alen = 0;
  124. if ([nskey isEqualToString:@"..."] || nskey.length==0)
  125. continue;
  126. key = (char*)conv_NSString_to_char(nskey);
  127. alen += key ? strlen(key) : 0;
  128. val = (char*)conv_NSString_to_char((NSString*)[row valueForKey:@"val"]);
  129. alen += val ? strlen(val) : 0;
  130. cour = connstr;
  131. connstr = (char*) malloc (size + alen + 2);
  132. if (connstr)
  133. {
  134. memcpy (connstr, cour, size);
  135. sprintf (connstr + size - 1, "%s=%s", key?key:"", val?val:"");
  136. free (cour);
  137. size += alen + 2;
  138. }
  139. else
  140. connstr = cour;
  141. if (key!=NULL) free(key);
  142. if (val!=NULL) free(val);
  143. }
  144. connstr[size - 1] = '\0';
  145. if (dsn!=NULL) free(dsn);
  146. if (comment!=NULL) free(comment);
  147. }
  148. }
  149. [dlg release];
  150. return connstr;
  151. }
  152. }
  153. #define MODE_ADD 1
  154. #define MODE_VIEW 0
  155. @interface IODBCProxy_SetupController ()
  156. @end
  157. @implementation IODBCProxy_SetupController
  158. @synthesize Attrs_ArrController;
  159. @synthesize fld_DSN;
  160. @synthesize fld_Comment;
  161. @synthesize btn_Add;
  162. @synthesize btn_Remove;
  163. @synthesize d_dsn, d_comment, _addEnabled;
  164. @synthesize Attrs_list=_Attrs_list;
  165. - (id)initWithAttrs:(const char*)attrs
  166. {
  167. self = [super initWithWindowNibName:@"IODBCProxy_SetupController"];
  168. if (self) {
  169. self.Attrs_list = [NSMutableArray arrayWithCapacity:16];
  170. [self parse_attrs:attrs];
  171. }
  172. return self;
  173. }
  174. - (void)dealloc
  175. {
  176. [d_dsn release];
  177. [d_comment release];
  178. [_Attrs_list release];
  179. [super dealloc];
  180. }
  181. - (id)initWithWindow:(NSWindow *)window
  182. {
  183. self = [super initWithWindow:window];
  184. if (self) {
  185. // Initialization code here.
  186. }
  187. return self;
  188. }
  189. - (void)parse_attrs:(const char *)attrs
  190. {
  191. char *curr, *cour;
  192. for (curr = (char*) attrs; *curr; curr += (strlen(curr) + 1))
  193. {
  194. if (!strncasecmp (curr, "Description=", strlen("Description=")))
  195. self.d_comment = conv_char_to_NSString(curr + strlen("Description="));
  196. if (!strncasecmp (curr, "DSN=", strlen("DSN=")) ||
  197. !strncasecmp (curr, "Driver=", strlen("Driver=")) ||
  198. !strncasecmp (curr, "Description=", strlen("Description=")))
  199. continue;
  200. if ((cour = strchr (curr, '=')))
  201. {
  202. NSString *key, *val;
  203. *cour = '\0';
  204. key = conv_char_to_NSString(curr);
  205. *cour = '=';
  206. val = conv_char_to_NSString(cour+1);
  207. [_Attrs_list addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:key!=nil?key:@"", @"key",
  208. val!=nil?val:@"", @"val", nil]];
  209. }
  210. }
  211. }
  212. - (void)windowDidLoad
  213. {
  214. [super windowDidLoad];
  215. _dialogCode = 0;
  216. _curMode = MODE_VIEW;
  217. [[self window] center]; // Center the window.
  218. if (d_dsn!=nil){
  219. self.window.title = [NSString stringWithFormat:@"Setup of %@", d_dsn];
  220. self.fld_DSN.stringValue = d_dsn;
  221. }
  222. if (d_comment!=nil)
  223. self.fld_Comment.stringValue = d_comment;
  224. }
  225. - (void)windowWillClose:(NSNotification*)notification
  226. {
  227. [NSApp stopModalWithCode:_dialogCode];
  228. }
  229. - (IBAction)call_Ok:(id)sender {
  230. _dialogCode = 1;
  231. self.d_dsn = fld_DSN.stringValue;
  232. self.d_comment = fld_Comment.stringValue;
  233. [self.window close];
  234. }
  235. - (IBAction)call_Cancel:(id)sender {
  236. _dialogCode = 0;
  237. [self.window close];
  238. }
  239. @end