home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Connectivity / GateKeeper-2.1 / Preferences.m < prev    next >
Encoding:
Text File  |  1997-03-13  |  15.5 KB  |  501 lines

  1. //************************************************************************
  2. //
  3. //    Preferences.m.  
  4. //
  5. //        Controller for the preferences panel.
  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. #import "Preferences.h"
  21. #import "SwapView.h"
  22. #import "OptionsEditor.h"
  23. #import "ToolBar.h"
  24. #import "HLDelegate.h"
  25.  
  26.             // used in creating a FIFO
  27.     typedef unsigned short mode_t;
  28. int mkfifo (const char *path, mode_t mode);
  29. #define    FMOD    (S_IREAD | S_IWRITE)                    // read, write by owner
  30.  
  31.  
  32.  
  33. @implementation Preferences
  34.  
  35. //************************************************************************
  36. //
  37. //        sent to us after nib objects are unarchived and init'd 
  38. //
  39. //************************************************************************
  40.  
  41. - awakeFromNib
  42. {
  43. int i = 0;
  44.  
  45.     [swapView initPopUp];
  46.     [swapView inspectName:"Switches"];
  47.                         // remem loc/size
  48.     [[swapView window] setFrameUsingName:[[swapView window] title]];    
  49.     [[swapView window] setFrameAutosaveName:[[swapView window] title]]; 
  50.                                                         // default switches
  51.     do  {                                // if set in ddb turn check switch on
  52.         if(strcmp(NXGetDefaultValue([NXApp appName], sList[i]), "YES") == 0)        
  53.             [[swMatrix findCellWithTag:i] setIntValue:1];
  54.         }
  55.     while(sList[++i]);
  56.     for(i = 0; rsList[i] != NULL; i++)                     // redial switches
  57.         {                                // if set in ddb turn check switch on
  58.         if(strcmp(NXGetDefaultValue([NXApp appName], rsList[i]), "YES") == 0)        
  59.             [[redialSwMatrix findCellWithTag:i] setIntValue:1];
  60.         }
  61.     [preTimeoutSw setIntValue:1];
  62.     for(i = 0; cList[i]; i++)                // display default trigger str's
  63.         [defaultsForm setStringValue:NXGetDefaultValue([NXApp appName],
  64.                                                             cList[i]) at:i];    
  65.     for(i = 0; rfList[i]; i++)                // display redial trigger str's
  66.         [redialForm setStringValue:NXGetDefaultValue([NXApp appName],
  67.                                                             rfList[i]) at:i];    
  68.  
  69.     [self setPPPForm:radioButtons];                // set ppp form strings
  70.     [self timeout:self];                // set timeout circ slide/text field
  71.     [self timeoutThreshold:self];        // set timeout circ slide/text field
  72.     [[[[[[[[pHLBrowser setTitled:NO]     // create and configure nxbrowser
  73.                     setHorizontalScrollButtonsEnabled:NO]
  74.                     setDelegate:[[NXApp delegate] hotListDelegate]]
  75.                     setTarget:[[NXApp delegate] hotListDelegate]]
  76.                     setAction:@selector(pBrowserHit:)]
  77.                     setDoubleAction:@selector(pBrowserDoubleClick:)]
  78.                     setMinColumnWidth:200]
  79.                     setMaxVisibleColumns:1];
  80.     [self resetPHLBrowser];
  81.     [hlPathTextField setStringValue:
  82.                                 NXGetDefaultValue([NXApp appName], HLLIST)];
  83.  
  84.     return self;
  85. }
  86. //************************************************************************
  87. //
  88. //        show pppEditor 
  89. //
  90. //************************************************************************
  91.  
  92. - show_pppEditor:sender 
  93. {
  94.     [self setPPPForm:radioButtons];                // set ppp form strings
  95.  
  96.     return self;
  97. }
  98. //***************************** Editor subview ***************************
  99. //************************************************************************
  100. //
  101. //         Edit ppp file button was pressed, open radio sel'd file in edit
  102. //
  103. //************************************************************************
  104.  
  105. - pppFileEdit:sender 
  106. {                        // if readable
  107.     if([[NXApp delegate] 
  108.                     readable:[self pathOf:(int)[radioButtons selectedTag]]])
  109.                         // call edit to open radioButtons selected file
  110.         [[Application workspace] openFile:[self pathOf:
  111.                     (int)[radioButtons selectedTag]] withApplication:"Edit"];
  112.     
  113.     return self;
  114. }
  115. //************************************************************************
  116. //
  117. //         Edit example ppp file button was pressed, in Editor subview.
  118. //
  119. //************************************************************************
  120.  
  121. - pppExampleEdit:sender 
  122. {                        // appWrapper is mainBundle, return path to resources
  123.                         // bundled within                 
  124.     if(![[NXBundle mainBundle]getPath:Path forResource:"Examples" ofType:NULL])
  125.         [[NXApp delegate] showAlert:"Error getting mainbundle path"];
  126.     strcat(Path, exlist[(int)[radioButtons selectedTag]]);                
  127.                     // call edit to open file
  128.     [[Application workspace] openFile:Path withApplication:"Edit"];
  129.     
  130.     return self;
  131. }
  132. //************************************************************************
  133. //
  134. //        send formcell a path based on the tag of the selected radio button.
  135. //
  136. //************************************************************************
  137.  
  138. - setPPPForm:sender
  139. {
  140.     [pppForm setStringValue:[self pathOf:(int)[sender selectedTag]] at:0];    
  141.     if((2 < (int)[sender selectedTag]))
  142.         [pppForm setEnabled:NO];      
  143.     else 
  144.         [pppForm setEnabled:YES];
  145.  
  146.     return self;
  147. }
  148. //************************************************************************
  149. //
  150. //         return the path of the item selected in flist[]
  151. //
  152. //************************************************************************
  153.  
  154. - (char *)pathOf:(int)i 
  155. {
  156.     strcpy(Path, NXGetDefaultValue([NXApp appName], flist[i]));
  157.     
  158.     return Path;
  159. }
  160. //***************************** defaults subview *************************
  161. //************************************************************************
  162. //
  163. //        user has changed the path of one of the ppp files.
  164. //
  165. //************************************************************************
  166.  
  167. - pppFilesPathChanged:sender
  168. {                                    // write an entry to defaults database
  169.     if(!NXWriteDefault([NXApp appName], flist[(int)[radioButtons selectedTag]], 
  170.                                 [sender stringValueAt:[sender selectedIndex]]))
  171.         [[NXApp delegate] showAlert:"ddbWriteError"];
  172.  
  173.     return self;
  174. }
  175. //************************************************************************
  176. //
  177. //        user has changed a control string, store change in ddb
  178. //
  179. //************************************************************************
  180.  
  181. - defaultsForm:sender
  182. {
  183. int i = (int)[sender selectedIndex];
  184.  
  185.                 // write an entry to defaults database
  186.     if(!NXWriteDefault([NXApp appName], cList[i], [sender stringValueAt:i]))
  187.         [[NXApp delegate] showAlert:"ddbWriteError"];
  188.  
  189.     return self;
  190. }
  191. //***************************** Link subview *****************************
  192. //************************************************************************
  193. //
  194. //        hotList path changed
  195. //
  196. //************************************************************************
  197.  
  198. - setHotListPath:sender
  199. {                // write an entry to defaults database
  200.     if(!NXWriteDefault([NXApp appName], HLLIST, [sender stringValue]))
  201.         [[NXApp delegate] showAlert:"ddbWriteError"];
  202.     [[[NXApp delegate] hotListDelegate] initHotListPath];
  203.     [self resetPHLBrowser];
  204.  
  205.     return self; 
  206. }
  207. //************************************************************************
  208. //
  209. //        select a hotList path
  210. //
  211. //************************************************************************
  212.  
  213. - selectHotListPath:sender
  214. {                
  215. id    theOpenPanel;   
  216. const char *filter[2] = {"Gate", NULL};        // filter for Gate doc path's
  217.  
  218.     theOpenPanel = [OpenPanel new];
  219.     [theOpenPanel setTitle:"HotList Path"];
  220.     if(NX_OKTAG == [theOpenPanel runModalForDirectory:
  221.                         NXGetDefaultValue([NXApp appName], "path") file:NULL     
  222.                             types:filter])
  223.         {
  224.         if(!NXWriteDefault([NXApp appName], HLLIST, [theOpenPanel directory]))
  225.             [[NXApp delegate] showAlert:"ddbWriteError"];
  226.         [hlPathTextField setStringValue:[theOpenPanel directory]];
  227.         [[[NXApp delegate] hotListDelegate] initHotListPath];
  228.         [self resetPHLBrowser];
  229.         }
  230.  
  231.     return self;
  232. }
  233. //************************************************************************
  234. //
  235. //        reset preferences hot list browswer
  236. //
  237. //************************************************************************
  238.  
  239. - resetPHLBrowser
  240. {                        // NXBrowser s/load cols from its delegate
  241.     [pHLBrowser loadColumnZero];
  242.     mat = [pHLBrowser matrixInColumn:0];
  243.     [[[NXApp delegate] hotListDelegate] setPreferencesBMat:mat];        
  244.     [mat selectCellAt:[[[NXApp delegate]hotListDelegate]hLSelectedCell]:0];
  245.  
  246.     return self;
  247. }
  248. //************************************************************************
  249. //
  250. //        user has changed a redial trigger string, store change in ddb
  251. //
  252. //************************************************************************
  253.  
  254. - redialForm:sender
  255. {
  256. int i = (int)[sender selectedIndex];
  257.  
  258.                 // write an entry to defaults database
  259.     if(!NXWriteDefault([NXApp appName], rfList[i], [sender stringValueAt:i]))
  260.         [[NXApp delegate] showAlert:"ddbWriteError"];
  261.  
  262.     return self;
  263. }
  264. //************************************************************************
  265. //
  266. //        Target method of defaults db entries for our preferences
  267. //        check switches based on the tag assigned in IB to each switch.
  268. //
  269. //************************************************************************
  270.  
  271. - switch:sender
  272. {
  273. int tag = [sender selectedTag];
  274.                                         // toggles entries YES/NO
  275.     if(strcmp(NXGetDefaultValue([NXApp appName], sList[tag]),"NO") == 0)
  276.         {                                                        // sw set ON
  277.          if(!NXWriteDefault([NXApp appName], sList[tag], "YES"))
  278.             [[NXApp delegate] showAlert:"ddbWriteError"];
  279.         else 
  280.             {            // use fifo was enabled,
  281.             if(tag == 0) 
  282.                 [self makeFIFO];
  283.             if(tag == 4)                        // Dial on Demand was enabled
  284.                 [[NXApp delegate] DialOnDemand];        // prepare for dial on 
  285.             if(tag == 6)                        // app icon time enabled
  286.                 [[NXApp delegate] setAppIconTimer:YES];        
  287.             }
  288.         }
  289.     else                                                         // sw set OFF
  290.         {
  291.         if(tag == 0) 
  292.             [self makeFile];    // if previously using FIFO create a log file
  293.         if(tag == 4)            // Dial on Demand was disabled, revert to
  294.             [[NXApp delegate] namedReset:"/usr/etc/named"];        // system named
  295.         if(tag == 6)            // app icon time disabled
  296.             [[NXApp delegate] setAppIconTimer:NO];        
  297.         if(!NXWriteDefault([NXApp appName], sList[tag], "NO"))
  298.             [[NXApp delegate] showAlert:"ddbWriteError"];
  299.         }
  300.  
  301.     return self;
  302. }
  303. //*****************************************************************************
  304. //
  305. //         target of redial panel's switch matrix 
  306. //
  307. //*****************************************************************************
  308.  
  309. - redialSwitch:sender;
  310. {
  311.     if((BOOL)[sender intValue])    
  312.         {
  313.         if(!NXWriteDefault([NXApp appName],rsList[[sender selectedTag]],"YES"))
  314.             [[NXApp delegate] showAlert:"ddbWriteError"];
  315.         }
  316.     else
  317.         {
  318.         if(!NXWriteDefault([NXApp appName], rsList[[sender selectedTag]],"NO"))
  319.             [[NXApp delegate] showAlert:"ddbWriteError"];
  320.         }
  321.     if([sender selectedTag] == 4)                // Redial Backoff state change
  322.         [[NXApp delegate] resetDelay:sender];         
  323.  
  324.     return self;
  325. }
  326. //*****************************************************************************
  327. //
  328. //         make the FIFO 
  329. //
  330. //*****************************************************************************
  331.  
  332. - makeFIFO
  333. {                                // find FIFO path per /etc/syslog.conf
  334.     [[NXApp delegate] fifo];    
  335.                                 // remove FIFO and then remake it
  336.     if((strlen(NXGetDefaultValue([NXApp appName],"locFIFO")) + 20)< MAXPATHLEN)
  337.         {
  338.         strcpy(Path, [[[NXApp delegate] stringTable]
  339.                                              valueForStringKey:"createFIFO"]);                
  340.         strcat(Path, NXGetDefaultValue([NXApp appName],"locFIFO"));
  341.         strcat(Path, " ?");
  342.         if(NXRunAlertPanel(0, Path,
  343.                     [[[NXApp delegate] stringTable] valueForStringKey:"Yes"],
  344.                     [[[NXApp delegate] stringTable] valueForStringKey:"No"],
  345.                     NULL) == NX_OKTAG)
  346.             {
  347.             unlink(NXGetDefaultValue([NXApp appName],"locFIFO"));        
  348.             mkfifo(NXGetDefaultValue([NXApp appName],"locFIFO"),(mode_t) FMOD);
  349.             [[NXApp delegate] syslogdRun];            // restart syslogd
  350.             }
  351.         }
  352.     else
  353.         [[NXApp delegate] showAlert:"Error FIFO path is too long"];
  354.  
  355.     return self;
  356. }
  357. //*****************************************************************************
  358. //
  359. //         make the Log File
  360. //
  361. //*****************************************************************************
  362.  
  363. - makeFile
  364. {
  365. int fd;
  366.                                 // find path per /etc/syslog.conf
  367.     [[NXApp delegate] fifo];    
  368.                                 // remove FIFO and then remake it
  369.     if((strlen(NXGetDefaultValue([NXApp appName],"locFIFO")) + 27)< MAXPATHLEN)
  370.         {
  371.         strcpy(Path, [[[NXApp delegate] stringTable]
  372.                                              valueForStringKey:"createFile"]);                
  373.         strcat(Path, NXGetDefaultValue([NXApp appName],"locFIFO"));
  374.         strcat(Path, " ?");
  375.         if(NXRunAlertPanel(0, Path,
  376.                     [[[NXApp delegate] stringTable] valueForStringKey:"Yes"],
  377.                     [[[NXApp delegate] stringTable] valueForStringKey:"No"],
  378.                     NULL) == NX_OKTAG)
  379.             {
  380.             unlink(NXGetDefaultValue([NXApp appName],"locFIFO"));
  381.             if((fd = open(NXGetDefaultValue([NXApp appName],"locFIFO"), 
  382.                                 O_RDWR|O_CREAT|O_TRUNC,(mode_t) 644)) != -1)
  383.                 close(fd);        
  384.             [[NXApp delegate] syslogdRun];            // restart syslogd
  385.             }
  386.         }
  387.     else
  388.         [[NXApp delegate] showAlert:"log file path is too long"];
  389.  
  390.     return self;
  391. }
  392. //*****************************************************************************
  393. //
  394. //         target of timeout circular slider 
  395. //
  396. //*****************************************************************************
  397.  
  398. - timeout:sender
  399. {
  400. int i;
  401.  
  402.     if(sender == self)                             // if initial display of pref
  403.         i = atoi(NXGetDefaultValue([NXApp appName], "iTimeout"));
  404.     else                                        // else user changed control
  405.         {
  406.         i = [sender intValue];
  407.         [[NXApp delegate] setTimeout:i];        // set timeout in Coordinator
  408.         }
  409.     if(i == 0)
  410.         [timeoutField setStringValue:"Off"];
  411.     else
  412.         [timeoutField setIntValue:i];
  413.     [timeoutCircSlider setIntValue:i];
  414.  
  415.     return self;
  416. }
  417. //*****************************************************************************
  418. //
  419. //         target of pre timeout switch 
  420. //
  421. //*****************************************************************************
  422.  
  423. - setPreTimeout:sender
  424. {
  425.     if((BOOL)[sender intValue])
  426.         {
  427.          if(!NXWriteDefault([NXApp appName], "preTimeout", "YES"))
  428.             [[NXApp delegate] showAlert:"ddbWriteError"];
  429.         }
  430.     else 
  431.         {
  432.         if(!NXWriteDefault([NXApp appName], "preTimeout", "NO"))
  433.             [[NXApp delegate] showAlert:"ddbWriteError"];
  434.         }
  435.  
  436.     return self;
  437. }
  438. //*****************************************************************************
  439. //
  440. //         target of timeout threshold circular slider 
  441. //
  442. //*****************************************************************************
  443.  
  444. - timeoutThreshold:sender
  445. {
  446. int i;
  447.  
  448.     if(sender == self)        // if initial display of pref
  449.         i = atoi(NXGetDefaultValue([NXApp appName], "iTimeThreshold"));
  450.     else                                        // else user changed control
  451.         {
  452.         i = [sender intValue];
  453.         sprintf(Path,"%d",(int)i);
  454.         if(!NXWriteDefault([NXApp appName], "iTimeThreshold", Path))
  455.             [[NXApp delegate] showAlert:"ddbWriteError"];
  456.         }                                            
  457.     [timeoutThreshField setIntValue:i];
  458.     [timeoutThreshCircSlider setIntValue:i];
  459.  
  460.     return self;
  461. }
  462. //*********************** text field matrix delegation ************************
  463. //*****************************************************************************
  464. //
  465. //         as text delegate we recieve this when textfield cell is being edited 
  466. //
  467. //*****************************************************************************
  468.  
  469. - (BOOL)textWillChange:textObject
  470. {
  471.     [[swapView window] setDocEdited:YES];    // indicate change is not saved
  472.  
  473.     return NO;
  474. }
  475. //*****************************************************************************
  476. //
  477. //         text delegate receives when editing of textfield cell is ended 
  478. //
  479. //*****************************************************************************
  480.  
  481. - textDidEnd:textObject endChar:(unsigned short)whyEnd
  482. {
  483.     if ((whyEnd == 16) || (whyEnd == 17)) 
  484.         [[swapView window] setDocEdited:NO];    // indicate change is saved
  485.  
  486.     return self;
  487. }
  488.  
  489. @end
  490.  
  491. //*****************************************************************************
  492. //
  493. //         create FIFO at designated path with mode set to 
  494. //
  495. //*****************************************************************************
  496.  
  497. int mkfifo (const char *path, mode_t mode)
  498. {
  499.     return mknod (path, mode | S_IFIFO, 0);
  500. }
  501.