home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Connectivity / GateKeeper-2.1 / HLDelegate.m < prev    next >
Encoding:
Text File  |  1997-04-14  |  16.6 KB  |  564 lines

  1. //************************************************************************
  2. //
  3. //    HLDelegate.m.  
  4. //
  5. //        NXBrowser delegate of the Hot list and HotList controller
  6. //
  7. //            by    Felipe A. Rodriguez        
  8. //
  9. //    This code is supplied "as is" the author makes no warranty as to its 
  10. //    suitability for any purpose.  This code is free and may be distributed 
  11. //    in accordance with the terms of the:
  12. //        
  13. //            GNU GENERAL PUBLIC LICENSE
  14. //            Version 2, June 1991
  15. //            copyright (C) 1989, 1991 Free Software Foundation, Inc.
  16. //             675 Mass Ave, Cambridge, MA 02139, USA
  17. //
  18. //************************************************************************
  19.  
  20.  
  21. #import "GKdefs.h"
  22. #import "HLDelegate.h"
  23. #import "HLRecord.h"
  24. #import "Coordinator.h"
  25. #import <appkit/appkit.h>
  26. #import <dbkit/DBImageView.h>
  27.  
  28. #import <sys/types.h>
  29. #import <sys/dir.h>  /* POSIX applications #include <dirent.h> */
  30.  
  31.  
  32. @implementation HLDelegate
  33.  
  34. //************************************************************************
  35. //
  36. //        sent to us after nib objects are unarchived and init'd 
  37. //
  38. //************************************************************************
  39.  
  40. - awakeFromNib
  41. {
  42. char *sC;
  43. NXRect spVRect;                            // sizeof splitview
  44. NXSize maxSize = {300.0, 600.0};        // max hotlist win size
  45.  
  46.     [[HLImageView setEditable:NO] setStyle:DB_ImageNoFrame];
  47.  
  48.                 // theSplitView -- IB connected to be top view
  49.     [theSplitView getFrame: &spVRect];
  50.     spVRect.size.width -= 20;    
  51.     sC = NXCopyStringBuffer(NXGetDefaultValue([NXApp appName],BROWSERHT));
  52.                 // if not def and val is less than splitview's height
  53.     if(atof(sC) > 600.0 || atof(sC) < 0.0)    
  54.         spVRect.size.height *= 2.0/3.0;    // def, make top view 2/3 of splitview
  55.     else                                 
  56.         spVRect.size.height = atof(sC);                    // per saved 
  57.     free(sC);
  58.  
  59.     HLBrowser = [[NXBrowser alloc] initFrame:&spVRect];
  60.     [[[[[[[[HLBrowser setTitled:NO]         // create and configure nxbrowser
  61.                     setHorizontalScrollButtonsEnabled:NO]
  62.                     setDelegate:self]
  63.                     setTarget:self]
  64.                     setAction:@selector(browserHit:)]
  65.                     setDoubleAction:@selector(browserDoubleClick:)]
  66.                     setMinColumnWidth:200]
  67.                     setMaxVisibleColumns:1];
  68.  
  69.     [theSplitView addSubview:HLBrowser];
  70.     sC = NXCopyStringBuffer(NXGetDefaultValue([NXApp appName], ADDRESSHT));
  71.     if(atof(sC) > 600.0 || atof(sC) < 0.0)    
  72.         spVRect.size.height /= 2.0;        // make bottom view 1/3 of splitview
  73.     else                                 
  74.         spVRect.size.height = atof(sC);                    // per saved 
  75.     free(sC);
  76.     [addressView setFrame:&spVRect];
  77.     [theSplitView addSubview:addressView];
  78.     [theSplitView display];
  79.  
  80.     if(![[NXBundle mainBundle]getPath:oPath forResource:"options" ofType:NULL])
  81.         oPath[0] = '\0';
  82.     [self initHotListPath];        
  83.                 // set each view in the heirarchy to resize its subviews
  84.     [hotList setMaxSize:&maxSize];             
  85.     [hotList setFrameUsingName:[hotList title]];            // remem 
  86.     [hotList setFrameAutosaveName:[hotList title]];
  87.  
  88.     return self;
  89. }
  90. //*****************************************************************************
  91. //
  92. //         initialize the hotlist and reinit if path to it is changed 
  93. //
  94. //*****************************************************************************
  95.  
  96. - initHotListPath 
  97. {
  98. char *sC;
  99.                 //    cell selection in hotList should persist between sessions
  100.                 //    so recall previous selection
  101.     sC = NXCopyStringBuffer(NXGetDefaultValue([NXApp appName],SELCELL));        
  102.     selCell = atoi(sC);
  103.     free(sC);
  104.  
  105.     [HLBrowser loadColumnZero];        // NXBrowser s/load cols from its delegate
  106.     if(hotListDirValid)                // if hotList path is valid
  107.         {
  108.         mat = [HLBrowser matrixInColumn:0];        
  109.         [HLBrowser useScrollButtons:NO];        
  110.         if(!(selCell < [dirList count]))
  111.             selCell = 0;
  112.         if(selCell < [dirList count])
  113.             {
  114.             [mat selectCellAt:selCell :0];
  115.             if(!(image = [[dirList objectAt:selCell] gateIcon]))    
  116.                 image = [NXImage findImageNamed: "Blank"];    // else use blank
  117.             [mat scrollCellToVisible:selCell :0];
  118.             }
  119.         else
  120.             image = [NXImage findImageNamed: "Blank"];    // else use blank
  121.         [HLImageView setImage:image];
  122.         [HLProviderField setStringValue:
  123.                     [[[HLBrowser matrixInColumn:0] selectedCell] stringValue]];
  124.         }                                                    
  125.  
  126.     return self;
  127. }
  128. //*****************************************************************************
  129. //
  130. //         provide to listBrowser(NXBrowser) it's data to be displayed
  131. //            do this because we are its delegate  
  132. //
  133. //*****************************************************************************
  134.  
  135. - (int)browser:b fillMatrix:m inColumn:(int)c
  136. {
  137. int    i = 0, strSize;
  138. id t, obj = nil;
  139. char *ptr;
  140. NXHashState state;
  141. const void  *key; 
  142. void  *value; 
  143. struct direct *dirp;
  144. static DIR *dp = NULL, *dpOLD = NULL;
  145.  
  146.     if(dp)
  147.         dpOLD = dp;
  148.     if ((dp = opendir(NXGetDefaultValue([NXApp appName], "HotList"))) != NULL)
  149.         {
  150.         if(!namHashTable)                // alloc hashtable w/str keys, obj vals
  151.             namHashTable = [[HashTable alloc] initKeyDesc:"*" valueDesc:"@"];
  152.         else
  153.             {
  154.             tmpHashTable = [namHashTable copyFromZone:[self zone]];
  155.             [namHashTable empty];
  156.             }
  157.         if(dirList)
  158.             [dirList empty];                    // empty list
  159.         else
  160.             dirList = [[List alloc] init];        // produce an array of objects
  161.         if ([tmpHashTable isKey:"AppDefault"] == NO)     
  162.             {                        // create a HotList record for app default
  163.             if (oPath[0] != '\0')     // if options file exists in app wrapper
  164.                 {                    
  165.                 obj = [[HLRecord alloc] init];    
  166.                 [obj setRecordPath:oPath];
  167.                 [namHashTable insertKey:"AppDefault" value:obj];
  168.                 }                            
  169.             }                            // if record exists in hash
  170.         else                            // use existing record
  171.             [namHashTable insertKey:"AppDefault"     
  172.                                 value:[tmpHashTable valueForKey:"AppDefault"]];
  173.         while (dirp = readdir(dp))                 // read dir and find all
  174.             {                                    // entries with ext .Gate
  175.             if((strSize = strlen(dirp->d_name)) > 5)
  176.                 {
  177.                 ptr = dirp->d_name + strSize;        // point to end of name str
  178.                 ptr -= 5;                        
  179.                 if(strcmp(ptr, ".Gate") == 0)        // if extension is .Gate
  180.                     {                                // if str not in hash table
  181.                     if ([tmpHashTable isKey:dirp->d_name] == NO)     
  182.                         {                            // create a HotList record
  183.                         obj = [[HLRecord alloc] init];    
  184.                         [obj setGateDocName:dirp->d_name];
  185.                         [namHashTable insertKey:dirp->d_name value:obj];
  186.                         }                            // if record exists in hash
  187.                     else                            // use existing record
  188.                         [namHashTable insertKey:dirp->d_name     
  189.                                 value:[tmpHashTable valueForKey:dirp->d_name]];
  190.                     }
  191.                 }
  192.             };
  193.         [tmpHashTable free];
  194.         if(dpOLD)
  195.             closedir(dpOLD);
  196.                                     // sort and display dir in NXBrowser
  197.         for(i = 0; i < [namHashTable count]; i++) 
  198.             {                        // Return NXHashState structure required 
  199.                                     // when iterating through the HashTable
  200.             state = [namHashTable initState]; 
  201.             while ([namHashTable nextState:&state key:&key value:&value]) 
  202.                 {                    // iterate thru the hashtable and perform
  203.                 if ([dirList indexOf:value] == NX_NOT_IN_LIST)
  204.                     {
  205.                     if(!obj)
  206.                         obj = value;                    // an ascending sort
  207.                     if(strcasecmp(key, [obj gateDocName]) < 0)        
  208.                         obj = [namHashTable valueForKey:key];
  209.                     }            
  210.                 };
  211.             [m addRow];                                // add row to HotList matrx
  212.             t = [m cellAt:i :0];                    // ret cell in NXBrowser
  213.             [dirList addObject:obj];
  214.             [t setStringValueNoCopy:[obj gateDocName]];
  215.             [t setLoaded:YES];
  216.             [t setLeaf:YES];
  217.             obj = nil;
  218.             }
  219.         hotListDirValid = YES;                        // hotList dir was opened
  220.         }
  221.     else
  222.         {
  223.         [[NXApp delegate] showAlert:"Error opening HotList directory."];
  224.         hotListDirValid = NO;                        // hotList dir is invalid
  225.         }
  226.  
  227.     return i; 
  228. }
  229. //*****************************************************************************
  230. //
  231. //  target/action for a single click in the browser. 
  232. //
  233. //*****************************************************************************
  234.  
  235. - browserHit:sender
  236. {   
  237.       selCell = [[sender matrixInColumn:0] selectedRow];
  238.     [self setDispPerCellAt:selCell];            // display traits of sel'd cell
  239.  
  240.     return self;
  241. }
  242. //*****************************************************************************
  243. //
  244. //  target/action method for a double click in the browser.  
  245. //
  246. //*****************************************************************************
  247.  
  248. - browserDoubleClick:sender
  249. {   
  250.       selCell = [[sender matrixInColumn:0] selectedRow];
  251.     [self setDispPerCellAt:selCell];            // display traits of sel'd cell
  252.     if([dirList count] >= 1)                    // open sel'td HotList file
  253.         [[NXApp delegate] app:self openFile:
  254.                         [[dirList objectAt:selCell] gateDocPath] type:NULL];
  255.     else                        
  256.         [[NXApp delegate] showAlert:"noSelectedDoc"];
  257.  
  258.     return self;
  259. }
  260. //*****************************************************************************
  261. //
  262. //     target/action for a single click in the preferences browser. 
  263. //
  264. //*****************************************************************************
  265.  
  266. - pBrowserHit:sender
  267. {   
  268.       selCell = [[sender matrixInColumn:0] selectedRow];
  269.     [mat selectCellAt:selCell :0];
  270.     [self setDispPerCellAt:selCell];            // display traits of sel'd cell
  271.     [mat scrollCellToVisible:selCell :0];
  272.  
  273.     return self;
  274. }
  275. //*****************************************************************************
  276. //
  277. //     target/action method for a double click in the preferences browser.  
  278. //
  279. //*****************************************************************************
  280.  
  281. - pBrowserDoubleClick:sender
  282. {   
  283.       selCell = [[sender matrixInColumn:0] selectedRow];
  284.     [mat selectCellAt:selCell :0];
  285.     [self setDispPerCellAt:selCell];            // display traits of sel'd cell
  286.     [mat scrollCellToVisible:selCell :0];
  287.     if([dirList count] >= 1)                    // open sel'td HotList file
  288.         [[NXApp delegate] app:self openFile:
  289.                         [[dirList objectAt:selCell] gateDocPath] type:NULL];
  290.     else                        
  291.         [[NXApp delegate] showAlert:"noSelectedDoc"];
  292.  
  293.     return self;
  294. }
  295. //*****************************************************************************
  296. //
  297. //         go to next gate doc in hotList 
  298. //
  299. //*****************************************************************************
  300.  
  301. - nextItem:sender 
  302. {
  303.     [sender setState:0];
  304.     if(selCell < ([dirList count] - 1))
  305.         selCell++;
  306.     else
  307.         selCell = 0;
  308.     [mat selectCellAt:selCell :0];
  309.     [self setDispPerCellAt:selCell];            // display traits of sel'd cell
  310.     [mat scrollCellToVisible:selCell :0];
  311.  
  312.     return self;
  313. }
  314. //*****************************************************************************
  315. //
  316. //         go to previous gate doc in hotList 
  317. //
  318. //*****************************************************************************
  319.  
  320. - prevItem:sender 
  321. {
  322.     [sender setState:0];
  323.     if(selCell > 0)
  324.         selCell--;
  325.     else
  326.         selCell = [dirList count] - 1;
  327.     [mat selectCellAt:selCell :0];
  328.     [self setDispPerCellAt:selCell];            // display traits of sel'd cell
  329.     [mat scrollCellToVisible:selCell :0];
  330.  
  331.     return self;
  332. }
  333. //*****************************************************************************
  334. //
  335. //         set views per selected cell in hotlist 
  336. //
  337. //*****************************************************************************
  338.  
  339. - setDispPerCellAt:(int)selectedCell 
  340. {
  341.     if((strcmp([[[NXApp mainMenu] findCellWithTag:2] title], 
  342.                     [[NXApp delegate] localString:"Connect"]) == 0) && 
  343.                             [[[NXApp mainMenu] findCellWithTag:2] isEnabled])
  344.         {                                                    // item enabled
  345.         [HLProviderField setStringValue:
  346.                     [[[HLBrowser matrixInColumn:0] selectedCell] stringValue]];
  347.         if(!(image = [[dirList objectAt:selectedCell] gateIcon]))    // if imge
  348.             image = [NXImage findImageNamed: "Blank"];        // else use blank
  349.         [HLImageView setImage:image];
  350.         if(toolBarView)
  351.             [toolBarView setImage:image];
  352.         }
  353.     if(prefBMat)
  354.         {
  355.         [prefBMat selectCellAt:selCell :0];
  356.         [prefBMat scrollCellToVisible:selCell :0];
  357.         }
  358.  
  359.     return self;
  360. }
  361. //*****************************************************************************
  362. //
  363. //         link based on selected file  
  364. //
  365. //*****************************************************************************
  366.  
  367. - playLink 
  368. {
  369.     if([dirList count] >= 1)                    // open sel'td HotList file
  370.         [[NXApp delegate]linkWithFile:[[dirList objectAt:selCell]gateDocPath]];
  371.     else                        
  372.         [[NXApp delegate] showAlert:"noSelectedDoc"];
  373.  
  374.     return self;
  375. }
  376. //*****************************************************************************
  377. //
  378. //         forward anything in Greek to someone who might understand 
  379. //
  380. //*****************************************************************************
  381.  
  382. - forward:(SEL)aSelector :(marg_list)argFrame
  383. {
  384. //    if ( [toolWin respondsTo:aSelector] )
  385. //        return [toolWin performv:aSelector :argFrame];
  386.  
  387.     [self doesNotRecognize:aSelector];                    // error abort process
  388.  
  389.     return self;
  390. }
  391. //************************************************************************
  392. //
  393. //     app will terminate so save selected cell, browser size
  394. //
  395. //************************************************************************
  396.  
  397. - appWillTerminate
  398. {
  399. char tmpStr[16];
  400. NXRect hlVRect;                            
  401.  
  402.     sprintf(tmpStr, "%d", selCell);
  403.                                 // write an entry to defaults database
  404.     if(!NXWriteDefault([NXApp appName], SELCELL, tmpStr))
  405.         [[NXApp delegate] showAlert:"ddbWriteError"];
  406.  
  407.     [HLBrowser getFrame: &hlVRect];
  408.     sprintf(tmpStr,"%f", hlVRect.size.height);
  409.                                 // write an entry to defaults database
  410.     if(!NXWriteDefault([NXApp appName], BROWSERHT, tmpStr))
  411.         [[NXApp delegate] showAlert:"ddbWriteError"];
  412.     [addressView getFrame: &hlVRect];
  413.     sprintf(tmpStr,"%f", hlVRect.size.height);
  414.                                 // write an entry to defaults database
  415.     if(!NXWriteDefault([NXApp appName], ADDRESSHT, tmpStr))
  416.         [[NXApp delegate] showAlert:"ddbWriteError"];
  417.  
  418.     return self;
  419. }
  420. //************************************************************************
  421. //
  422. //     free simply gets rid of everything we created
  423. //     This is how nice objects clean up.
  424. //
  425. //************************************************************************
  426.  
  427. - free
  428. {
  429.     if(dirList)
  430.         {
  431.         [dirList freeObjects];                            // free list's objects
  432.         [dirList free];                                    // free list object
  433.         }        
  434.     if(namHashTable)
  435.         [namHashTable free];                                    
  436.  
  437.     return [super free];
  438. }
  439. //*****************************************************************************
  440. //
  441. //         splitview delegate method, invoked after divider is moved by user 
  442. //
  443. //*****************************************************************************
  444.  
  445. - splitViewDidResizeSubviews:sender
  446. {
  447. NXRect graphRect;                            // sizeof scroll's content view
  448.  
  449.     [HLBrowser getFrame: &graphRect];
  450.  
  451.     return self;
  452. }
  453. //*****************************************************************************
  454. //
  455. //         tell the Browser to load col 0 
  456. //
  457. //*****************************************************************************
  458.  
  459. - loadBrowser 
  460. {
  461.     [HLBrowser loadColumnZero];        // NXBrowser s/load cols from its delegate
  462.     if(hotListDirValid)                // if hotList path is valid
  463.         {
  464.         mat = [HLBrowser matrixInColumn:0];        
  465.         [mat selectCellAt:selCell :0];
  466.         [mat scrollCellToVisible:selCell :0];
  467.         [HLBrowser display];
  468.         [hotList makeKeyAndOrderFront:self];
  469.         }                                                    
  470.  
  471.     return self;
  472. }
  473. //*****************************************************************************
  474. //
  475. //         send images back to this object 
  476. //
  477. //*****************************************************************************
  478.  
  479. - setToolBar:aView 
  480. {
  481.     toolBarView = aView;
  482.     [toolBarView setImage:image];
  483.  
  484.     return self;
  485. }
  486. //*****************************************************************************
  487. //
  488. //         send selected cell back to this object 
  489. //
  490. //*****************************************************************************
  491.  
  492. - setPreferencesBMat:aMatrix 
  493. {
  494.     prefBMat = aMatrix;
  495.  
  496.     return self;
  497. }
  498. //*****************************************************************************
  499. //
  500. //         return the selectedCell 
  501. //
  502. //*****************************************************************************
  503.  
  504. - (int)hLSelectedCell 
  505. {
  506.     return selCell;
  507. }
  508. //*****************************************************************************
  509. //
  510. //         return the statusWindow 
  511. //
  512. //*****************************************************************************
  513.  
  514. - statusWindow 
  515. {
  516.     return hotList;
  517. }
  518. //*****************************************************************************
  519. //
  520. //         return the path to the selected gate docs options file 
  521. //
  522. //*****************************************************************************
  523.  
  524. - (const char *)selGateDocOptionsPath 
  525. {
  526.     if([dirList count] >= 1)                            // path of selected doc
  527.         {
  528.         strncpy(oPath,[[dirList objectAt:selCell] gateDocPath], MAXPATHLEN);            
  529.         strncat(oPath, OPTION, MAXPATHLEN - strlen(oPath));
  530.  
  531.         return oPath;
  532.         }
  533.     else                        
  534.         [[NXApp delegate] showAlert:"noSelectedDoc"];
  535.  
  536.     return NULL;
  537. }
  538. //******************* status Window delegate methods **************************
  539. //*****************************************************************************
  540. //
  541. //         called whenever the user minituriazes our status window.
  542. //
  543. //*****************************************************************************
  544.  
  545. - windowWillMiniaturize:sender toMiniwindow:miniwindow 
  546. {
  547.     return [sender setMiniwindowIcon:"miniWinIcon"];
  548. }
  549. //*****************************************************************************
  550. //
  551. //         called whenever the user closes our status window.
  552. //
  553. //*****************************************************************************
  554.  
  555. - windowWillClose:sender  
  556. {
  557.     if(!NXWriteDefault([NXApp appName], DISPLAYS, "NO"))
  558.         [[NXApp delegate] showAlert:"ddbWriteError"];
  559.  
  560.     return self;
  561. }
  562.  
  563. @end
  564.