DSNchooserController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * $Id$
  3. *
  4. * The iODBC driver manager.
  5. *
  6. * Copyright (C) 1996-2021 OpenLink Software <[email protected]>
  7. * All Rights Reserved.
  8. *
  9. * This software is released under the terms of either of the following
  10. * licenses:
  11. *
  12. * - GNU Library General Public License (see LICENSE.LGPL)
  13. * - The BSD License (see LICENSE.BSD).
  14. *
  15. * Note that the only valid version of the LGPL license as far as this
  16. * project is concerned is the original GNU Library General Public License
  17. * Version 2, dated June 1991.
  18. *
  19. * While not mandated by the BSD license, any patches you make to the
  20. * iODBC source code may be contributed back into the iODBC project
  21. * at your discretion. Contributions will benefit the Open Source and
  22. * Data Access community as a whole. Submissions may be made at:
  23. *
  24. * http://www.iodbc.org
  25. *
  26. *
  27. * GNU Library Generic Public License Version 2
  28. * ============================================
  29. * This library is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU Library General Public
  31. * License as published by the Free Software Foundation; only
  32. * Version 2 of the License dated June 1991.
  33. *
  34. * This library is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  37. * Library General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU Library General Public
  40. * License along with this library; if not, write to the Free
  41. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  42. *
  43. *
  44. * The BSD License
  45. * ===============
  46. * Redistribution and use in source and binary forms, with or without
  47. * modification, are permitted provided that the following conditions
  48. * are met:
  49. *
  50. * 1. Redistributions of source code must retain the above copyright
  51. * notice, this list of conditions and the following disclaimer.
  52. * 2. Redistributions in binary form must reproduce the above copyright
  53. * notice, this list of conditions and the following disclaimer in
  54. * the documentation and/or other materials provided with the
  55. * distribution.
  56. * 3. Neither the name of OpenLink Software Inc. nor the names of its
  57. * contributors may be used to endorse or promote products derived
  58. * from this software without specific prior written permission.
  59. *
  60. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  61. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  62. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  63. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
  64. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  65. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  66. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  67. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  68. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  69. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  70. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  71. */
  72. #import "DSNchooserController.h"
  73. #import "Helpers.h"
  74. #include <sys/types.h>
  75. #include <sys/stat.h>
  76. #include <sys/param.h>
  77. #include <dirent.h>
  78. #include <errno.h>
  79. #include <unistd.h>
  80. #include <pwd.h>
  81. @interface DSNchooserController ()
  82. @end
  83. @implementation DSNchooserController
  84. @synthesize popup_dir_btn = _popup_dir_btn;
  85. @synthesize fdsn_tableView = _fdsn_tableView;
  86. @synthesize tab_view = _tab_view;
  87. @synthesize FileDSN_ArrController = _FileDSN_ArrController;
  88. @synthesize SysDSN_ArrController = _SysDSN_ArrController;
  89. @synthesize UserDSN_ArrController = _UserDSN_ArrController;
  90. @synthesize UserDSN_list=_UserDSN_list;
  91. @synthesize SysDSN_list=_SysDSN_list;
  92. @synthesize FileDSN_list=_FileDSN_list;
  93. @synthesize cur_dir=_cur_dir;
  94. @synthesize type_dsn = _type_dsn;
  95. @synthesize selected_dsn = _selected_dsn;
  96. @synthesize uid = _uid;
  97. @synthesize pwd = _pwd;
  98. - (id)init
  99. {
  100. SQLTCHAR tmp[1024] = {""};
  101. self = [super initWithWindowNibName:@"DSNchooserController"];
  102. if (self) {
  103. _UserDSN_list = [[NSMutableArray alloc] initWithCapacity:16];
  104. _SysDSN_list = [[NSMutableArray alloc] initWithCapacity:16];
  105. _FileDSN_list = [[NSMutableArray alloc] initWithCapacity:16];
  106. SQLSetConfigMode (ODBC_BOTH_DSN);
  107. if (!SQLGetPrivateProfileString(TEXT("ODBC"), TEXT("FileDSNPath"), TEXT(""), tmp, sizeof(tmp)/sizeof(SQLTCHAR), TEXT("odbcinst.ini")))
  108. _cur_dir = [NSString stringWithUTF8String:DEFAULT_FILEDSNPATH];
  109. else
  110. _cur_dir = (NSString*)TEXTtoNS(tmp);
  111. }
  112. return self;
  113. }
  114. - (void)dealloc
  115. {
  116. [_UserDSN_list release];
  117. [_SysDSN_list release];
  118. [_FileDSN_list release];
  119. [_type_dsn release];
  120. [_selected_dsn release];
  121. [super dealloc];
  122. }
  123. - (id)initWithWindow:(NSWindow *)window
  124. {
  125. self = [super initWithWindow:window];
  126. if (self) {
  127. // Initialization code here.
  128. }
  129. return self;
  130. }
  131. - (void)windowDidLoad
  132. {
  133. [super windowDidLoad];
  134. _dialogCode = 0;
  135. [[self window] center]; // Center the window.
  136. addDSNs_to_list(FALSE, _UserDSN_ArrController);
  137. addDSNs_to_list(TRUE, _SysDSN_ArrController);
  138. [_fdsn_tableView setDoubleAction:@selector(call_FDSN_DoubleClick)];
  139. self.type_dsn = @"userdsn";
  140. }
  141. - (void)windowWillClose:(NSNotification*)notification
  142. {
  143. [NSApp stopModalWithCode:_dialogCode];
  144. }
  145. - (void) call_FDSN_DoubleClick
  146. {
  147. NSInteger row = _fdsn_tableView.clickedRow;
  148. NSDictionary *dict = [_FileDSN_list objectAtIndex:row];
  149. NSNumber *isdir = [dict valueForKey:@"isdir"];
  150. if (isdir.boolValue==TRUE){
  151. NSString *cliked_dir = [dict valueForKey:@"name"];
  152. NSString *new_path = [NSString stringWithFormat:@"%@/%@", _cur_dir, cliked_dir];
  153. self.cur_dir = new_path;
  154. const char *path = [_cur_dir UTF8String];
  155. addFDSNs_to_list(path, FALSE, _FileDSN_ArrController);
  156. fill_dir_menu(path, _popup_dir_btn);
  157. }
  158. }
  159. /** NSTabViewDelegate **/
  160. - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
  161. {
  162. NSString *identifier = [tabViewItem identifier];
  163. self.type_dsn = identifier;
  164. if ([identifier isEqualToString:@"userdsn"]){
  165. addDSNs_to_list(FALSE, _UserDSN_ArrController);
  166. }
  167. else if ([identifier isEqualToString:@"sysdsn"]){
  168. addDSNs_to_list(TRUE, _SysDSN_ArrController);
  169. }
  170. else if ([identifier isEqualToString:@"filedsn"]){
  171. const char *cur_path = [_cur_dir UTF8String];
  172. addFDSNs_to_list(cur_path, FALSE, _FileDSN_ArrController);
  173. fill_dir_menu(cur_path, _popup_dir_btn);
  174. }
  175. }
  176. /** NSTableViewDelegate **/
  177. - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
  178. // Group our "model" object, which is a dictionary
  179. if (tableView == _fdsn_tableView) {
  180. NSDictionary *dict = [_FileDSN_list objectAtIndex:row];
  181. NSString *identifier = [tableColumn identifier];
  182. NSTableCellView *cellView = [tableView makeViewWithIdentifier:identifier owner:self];
  183. // Then setup properties on the cellView based on the column
  184. cellView.textField.stringValue = [dict objectForKey:@"name"];
  185. cellView.imageView.objectValue = [dict objectForKey:@"icon"];
  186. return cellView;
  187. }
  188. return nil;
  189. }
  190. - (IBAction)call_Ok:(id)sender {
  191. _dialogCode = 1;
  192. if ([_type_dsn isEqualToString:@"userdsn"]) {
  193. NSArray *item = [_UserDSN_ArrController selectedObjects];
  194. if (item!=nil && item.count>0){
  195. NSDictionary *dict = [item objectAtIndex:0];
  196. self.selected_dsn = [dict valueForKey:@"name"];
  197. }
  198. }
  199. else if ([_type_dsn isEqualToString:@"sysdsn"]) {
  200. NSArray *item = [_SysDSN_ArrController selectedObjects];
  201. if (item!=nil && item.count>0){
  202. NSDictionary *dict = [item objectAtIndex:0];
  203. self.selected_dsn = [dict valueForKey:@"name"];
  204. }
  205. }
  206. else if ([_type_dsn isEqualToString:@"filedsn"]) {
  207. NSArray *item = [_FileDSN_ArrController selectedObjects];
  208. if (item!=nil && item.count>0){
  209. NSDictionary *dict = [item objectAtIndex:0];
  210. self.selected_dsn = [NSString stringWithFormat:@"%@/%@", _cur_dir, [dict valueForKey:@"name"]];
  211. }
  212. }
  213. [self.window close];
  214. }
  215. - (IBAction)call_Cancel:(id)sender {
  216. _dialogCode = 0;
  217. [self.window close];
  218. }
  219. - (IBAction)call_Dir_PopupBtn:(id)sender {
  220. self.cur_dir = _popup_dir_btn.titleOfSelectedItem;
  221. const char *path = [_cur_dir UTF8String];
  222. addFDSNs_to_list(path, FALSE, _FileDSN_ArrController);
  223. fill_dir_menu(path, _popup_dir_btn);
  224. }
  225. void addDSNs_to_list(BOOL systemDSN, NSArrayController* list)
  226. {
  227. SQLTCHAR dsnname[1024], dsndesc[1024];
  228. SQLSMALLINT len;
  229. SQLRETURN ret;
  230. HENV henv;
  231. [list removeObjects:[list arrangedObjects]];
  232. /* Create a HENV to get the list of data sources then */
  233. ret = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  234. if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
  235. {
  236. _nativeerrorbox (henv, SQL_NULL_HANDLE, SQL_NULL_HANDLE);
  237. goto end;
  238. }
  239. /* Set the version ODBC API to use */
  240. SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3,
  241. SQL_IS_UINTEGER);
  242. # ifdef UNICODE
  243. SQLSetEnvAttr (henv, SQL_ATTR_APP_UNICODE_TYPE,
  244. (SQLPOINTER) SQL_DM_CP_DEF, SQL_IS_UINTEGER);
  245. #endif
  246. /* Get the list of datasources */
  247. ret = SQLDataSourcesW (henv,
  248. systemDSN ? SQL_FETCH_FIRST_SYSTEM : SQL_FETCH_FIRST_USER,
  249. dsnname, sizeof (dsnname)/sizeof(SQLTCHAR), &len,
  250. dsndesc, sizeof (dsndesc)/sizeof(SQLTCHAR), NULL);
  251. if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO && ret != SQL_NO_DATA)
  252. {
  253. _nativeerrorbox (henv, SQL_NULL_HANDLE, SQL_NULL_HANDLE);
  254. goto error;
  255. }
  256. while (ret != SQL_NO_DATA)
  257. {
  258. NSString *name = TEXTtoNS(dsnname);
  259. if (dsndesc[0] == 0)
  260. {
  261. SQLSetConfigMode (ODBC_BOTH_DSN);
  262. SQLGetPrivateProfileString(TEXT("Default"), TEXT("Driver"), TEXT(""), dsndesc,
  263. sizeof (dsndesc)/sizeof(wchar_t), TEXT("odbc.ini"));
  264. }
  265. NSString *drv = dsndesc[0]!=0? TEXTtoNS(dsndesc): @"-";
  266. /* Get the description */
  267. SQLSetConfigMode (systemDSN ? ODBC_SYSTEM_DSN : ODBC_USER_DSN);
  268. SQLGetPrivateProfileString (dsnname, TEXT("Description"), TEXT(""), dsndesc,
  269. sizeof (dsndesc)/sizeof(SQLTCHAR), TEXT("odbc.ini"));
  270. NSString *desc = dsndesc[0]!=0? TEXTtoNS(dsndesc): @"-";
  271. [list addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name",
  272. desc, @"desc", drv, @"drv", nil]];
  273. /* Process next one */
  274. ret = SQLDataSources (henv, SQL_FETCH_NEXT, dsnname,
  275. sizeof (dsnname)/sizeof(SQLTCHAR), &len, dsndesc,
  276. sizeof (dsndesc)/sizeof(SQLTCHAR), NULL);
  277. if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO
  278. && ret != SQL_NO_DATA)
  279. {
  280. _nativeerrorbox (henv, SQL_NULL_HANDLE, SQL_NULL_HANDLE);
  281. goto error;
  282. }
  283. }
  284. error:
  285. /* Clean all that */
  286. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  287. end:
  288. return;
  289. }
  290. #define MAX_ROWS 1024
  291. void addFDSNs_to_list(const char* path, BOOL b_reset, NSArrayController* list)
  292. {
  293. int nrows;
  294. DIR *dir;
  295. char *path_buf;
  296. struct dirent *dir_entry;
  297. struct stat fstat;
  298. [list removeObjects:[list arrangedObjects]];
  299. nrows = 0;
  300. if ((dir = opendir (path)))
  301. {
  302. while ((dir_entry = readdir (dir)) && nrows < MAX_ROWS)
  303. {
  304. asprintf (&path_buf, "%s/%s", path, dir_entry->d_name);
  305. if (stat ((const char*) path_buf, &fstat) >= 0 && S_ISDIR (fstat.st_mode))
  306. {
  307. if (dir_entry->d_name && dir_entry->d_name[0] != '.')
  308. {
  309. NSString *name = [NSString stringWithUTF8String:dir_entry->d_name];
  310. NSImage *icon = [NSImage imageNamed:NSImageNameFolder];
  311. [list addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name",
  312. icon, @"icon", [NSNumber numberWithBool:TRUE], @"isdir", nil]];
  313. nrows++;
  314. }
  315. }
  316. else if (stat ((const char*) path_buf, &fstat) >= 0 && !S_ISDIR (fstat.st_mode)
  317. && strstr (dir_entry->d_name, ".dsn"))
  318. {
  319. NSString *name = [NSString stringWithUTF8String:dir_entry->d_name];
  320. NSImage *icon = [[NSWorkspace sharedWorkspace]
  321. iconForFileType:NSFileTypeForHFSTypeCode(kGenericDocumentIcon)];
  322. [list addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name",
  323. icon, @"icon", [NSNumber numberWithBool:FALSE], @"isdir", nil]];
  324. nrows++;
  325. }
  326. if (path_buf)
  327. free (path_buf);
  328. }
  329. /* Close the directory entry */
  330. closedir (dir);
  331. }
  332. else
  333. create_error ("Error during accessing directory information", strerror (errno));
  334. //?? if (b_reset)
  335. //?? SetDataBrowserScrollPosition(widget, 0, 0);
  336. }
  337. void fill_dir_menu(const char* path, NSPopUpButton* list)
  338. {
  339. char *curr_dir, *prov, *dir;
  340. if (!path || !(prov = strdup (path)))
  341. return;
  342. if (prov[strlen(prov) - 1] == '/' && strlen(prov) > 1)
  343. prov[strlen(prov) - 1] = 0;
  344. [list removeAllItems];
  345. /* Add the root directory */
  346. [list addItemWithTitle:@"/"];
  347. if (strlen(prov) > 1)
  348. for (curr_dir = prov, dir = NULL; curr_dir;
  349. curr_dir = strchr (curr_dir + 1, '/'))
  350. {
  351. if (strchr (curr_dir + 1, '/'))
  352. {
  353. dir = strchr (curr_dir + 1, '/');
  354. *dir = 0;
  355. }
  356. [list addItemWithTitle:[NSString stringWithUTF8String:prov]];
  357. if (dir)
  358. *dir = '/';
  359. }
  360. free(prov);
  361. [list selectItemAtIndex:list.numberOfItems-1];
  362. }
  363. @end