home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Connectivity / GateKeeper-2.1 / GateDocEditor.m < prev    next >
Encoding:
Text File  |  1997-06-24  |  6.4 KB  |  197 lines

  1. //*****************************************************************************
  2. //
  3. //    GateDocEditor.m.  
  4. //        
  5. //        Create, edit and link using a Gate doc 
  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 "GateDocEditor.h"
  23. #import "options.h"
  24. #import "Coordinator.h"
  25. #import "OptionsEditor.h"
  26. #import <dbkit/DBImageView.h>
  27.  
  28.                 // filter for Gate doc path's
  29.     const char *filter[2] = {"Gate", NULL};
  30.  
  31.  
  32. @implementation GateDocEditor
  33.  
  34. //*****************************************************************************
  35. //
  36. //        create new Gate Document with default files inside.
  37. //
  38. //*****************************************************************************
  39.  
  40. - newDoc:sender
  41. {
  42.     theSavePanel = [SavePanel new];
  43.     [theSavePanel setTitle:[[NXApp delegate] localString:"New"]];
  44.     [theSavePanel setRequiredFileType:filter[0]]; // append ext if not supplied
  45.     if(NX_OKTAG == [theSavePanel runModalForDirectory:
  46.                         NXGetDefaultValue([NXApp appName], "path") file:NULL])
  47.         {                                        // create .Gate doc wrapper
  48.         mkdir([theSavePanel filename], (_S_IRUSR | _S_IWUSR | _S_IXUSR));        
  49.         chown([theSavePanel filename], getuid(), getgid());
  50.         [self addDefaultFiles:[theSavePanel filename]];
  51.         [self editGateDoc:[theSavePanel filename]];
  52.         }
  53.  
  54.     return self;
  55. }
  56. //*****************************************************************************
  57. //
  58. //        execute pppd with info from .Gate open document
  59. //
  60. //*****************************************************************************
  61.  
  62. - linkDoc:sender
  63. {
  64.     if((strcmp([[[NXApp mainMenu] findCellWithTag:2] title], 
  65.                 [[NXApp delegate] localString:"Connect"]) == 0) && 
  66.                         [[[NXApp mainMenu] findCellWithTag:2] isEnabled])
  67.         {                                        // is link menu item enabled?
  68.         [[sender window] close];
  69.           [[NXApp delegate] 
  70.                     linkWithFile:NXGetDefaultValue([NXApp appName], "path")];
  71.         }
  72.     else
  73.         [[NXApp delegate] showAlert:"A ppp session is already active."];
  74.  
  75.     return self;
  76. }
  77. //*****************************************************************************
  78. //
  79. //        open an existing .Gate document
  80. //
  81. //*****************************************************************************
  82.  
  83. - openDoc:sender
  84. {
  85.     theOpenPanel = [OpenPanel new];
  86.     [theOpenPanel setTitle:[[NXApp delegate] localString:"Open"]];
  87.     if(NX_OKTAG == [theOpenPanel runModalForDirectory:
  88.                         NXGetDefaultValue([NXApp appName], "path") file:NULL     
  89.                             types:filter])
  90.         [self editGateDoc:[theOpenPanel filename]];
  91.  
  92.     return self;
  93. }
  94. //*****************************************************************************
  95. //
  96. //        edit an existing .Gate document using built in editor
  97. //
  98. //*****************************************************************************
  99.  
  100. - editGateDoc:(const char *)aGateDoc
  101. {
  102. id image;
  103.  
  104.     if(!NXWriteDefault([NXApp appName],"path", aGateDoc))
  105.         [[NXApp delegate] showAlert:"ddbWriteError"];
  106.     strcpy(Path, aGateDoc);
  107.     strcat(Path, PPPUP);                        // pppup file inside of dir
  108.     if([[NXApp delegate] readable:Path])
  109.         {
  110.         if(!theOptionsEditor)                    // open edit doc window
  111.             [NXApp loadNibSection:"EditDoc.nib" owner:self withNames:NO];
  112.         [docPath setStringValue:aGateDoc];        // display name
  113.         [docName setStringValue:[[NXApp delegate] extractName:aGateDoc]];    
  114.         strcpy(Path, aGateDoc);
  115.         strcat(Path, OPTION);                    // file name inside of dir
  116.         [theOptionsEditor parseOptionsFile:Path];
  117.  
  118.         strcpy(Path, aGateDoc);
  119.         strcat(Path, "/Icon.tiff");
  120.         [[GDEImageView setEditable:NO] setStyle:DB_ImageNoFrame];
  121.         if(!(image = [[NXImage alloc] initFromFile:Path]))        // if imge
  122.             image = [NXImage findImageNamed: ".dir"];        // else use blank
  123.         [GDEImageView setImage:image];
  124.  
  125.         [[theOptionsEditor editorWindow] center];
  126.         [[theOptionsEditor editorWindow] makeKeyAndOrderFront:self];
  127.         }
  128.  
  129.     return self;
  130. }
  131. //*****************************************************************************
  132. //
  133. //        add our default options and pppup files to our new Gate document
  134. //
  135. //        paths for pppup file set from pref editor path
  136. //
  137. //*****************************************************************************
  138.  
  139. - addDefaultFiles:(const char *)nameOfFile
  140. {
  141. NXStream *Stream;
  142.                             // add example options
  143.     if(![[NXBundle mainBundle]getPath:Path forResource:"Examples" ofType:NULL])
  144.         [[NXApp delegate] showAlert:"Error getting examples path"];
  145.     strncat(Path, XOPTION,  MAXPATHLEN - strlen(XOPTION) - strlen(Path) + 1);
  146.     if((Stream = NXMapFile(Path, NX_READONLY)) == NULL)
  147.         [[NXApp delegate] showAlert:"Unable to open example options File"];
  148.     else
  149.         {
  150.         strncpy(Path, nameOfFile, MAXPATHLEN - strlen(OPTION));
  151.         strncat(Path, OPTION, (strlen(OPTION) + 1));
  152.         if((NXSaveToFile(Stream, Path)) == -1)
  153.             [[NXApp delegate] showAlert:"Error saving options to Gate doc"];
  154.         else
  155.             chown(Path, getuid(), getgid());
  156.         NXCloseMemory(Stream, NX_FREEBUFFER);
  157.         }
  158.                             // add example pppup
  159.     if(![[NXBundle mainBundle]getPath:Path forResource:"Examples" ofType:NULL])
  160.         [[NXApp delegate] showAlert:"Error getting examples path"];
  161.     strncat(Path, PPPUP,  MAXPATHLEN - strlen(PPPUP) - strlen(Path) + 1);
  162.     if((Stream = NXMapFile(Path, NX_READONLY)) == NULL)
  163.         [[NXApp delegate] showAlert:"Unable to open pppup example File"];
  164.     else
  165.         {
  166.         strncpy(Path, nameOfFile, MAXPATHLEN - strlen(PPPUP));
  167.         strncat(Path, PPPUP, (strlen(PPPUP) + 1));
  168.         if((NXSaveToFile(Stream, Path)) == -1)
  169.             [[NXApp delegate] showAlert:"Error saving pppup to .Gate doc"];
  170.         else
  171.             {
  172.             chmod(Path, (_S_IRUSR | _S_IWUSR));        // set to owner read/write
  173.             chown(Path, getuid(), getgid());
  174.             }
  175.         NXCloseMemory(Stream, NX_FREEBUFFER);
  176.         }
  177.                         // add default Icon.tiff
  178.     if(![[NXBundle mainBundle] getPath:Path 
  179.                                         forResource:"Icon.tiff" ofType:NULL])
  180.         [[NXApp delegate] showAlert:"Error getting Icon.tiff path"];
  181.     if((Stream = NXMapFile(Path, NX_READONLY)) == NULL)
  182.         [[NXApp delegate] showAlert:"Unable to open Icon.tiff File"];
  183.     strncpy(Path, nameOfFile, MAXPATHLEN - strlen(IMAGE));
  184.     strncat(Path, IMAGE, (strlen(IMAGE) + 1));
  185.     if((NXSaveToFile(Stream, Path)) == -1)
  186.            [[NXApp delegate] showAlert:"Unable to save Icon.tiff to Gate doc"];
  187.     else
  188.         chown(Path, getuid(), getgid());
  189.     NXCloseMemory(Stream, NX_FREEBUFFER);
  190.  
  191.     return self;
  192. }
  193.  
  194.  
  195. @end
  196.  
  197.