home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / ExportAll.bproj / ExportAll.m < prev    next >
Encoding:
Text File  |  1995-02-28  |  5.1 KB  |  189 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    FILENAME:    ExportAll.m
  3. //    SUMMARY:    Implementation of a multi-format SaveAll
  4. //    SUPERCLASS:    Object
  5. //    INTERFACE:    None
  6. //    PROTOCOLS:    <Tool>
  7. //    AUTHOR:        Rohit Khare
  8. //    COPYRIGHT:    (c) 1994 California Institure of Technology, eText Project
  9. ///////////////////////////////////////////////////////////////////////////////
  10. //    DESCRIPTION: 
  11. //    Designed to save all open documents in the HTMLD format.
  12. ///////////////////////////////////////////////////////////////////////////////
  13. //    HISTORY
  14. //    10/31/94:    Created. Implemented in just minutes, from scratch.
  15. ///////////////////////////////////////////////////////////////////////////////
  16.  
  17. #import "ExportAll.h"
  18.  
  19. @implementation ExportAll
  20.  
  21. + new 
  22. {
  23.     static ExportAll *ea = nil;
  24.     
  25.     if (!ea) {
  26.         ea = [[ExportAll alloc] init];
  27.     }
  28.     return ea;
  29. }
  30.  
  31. + toolAwake:theApp
  32. {
  33.     [theApp registerAccessory:NXUniqueString("Export ALL Documents")
  34.                 key:'\0'
  35.                name:NXUniqueString("ExportAll")
  36.              target:[ExportAll new]
  37.              action:@selector(exportAll:)];
  38.     [theApp registerAccessory:NXUniqueString("Export Open Documents")
  39.                 key:'\0'
  40.                name:NXUniqueString("ExportOpen")
  41.              target:[ExportAll new]
  42.              action:@selector(exportOpen:)];
  43.     [theApp registerAccessory:NXUniqueString("Print Open Documents")
  44.                 key:'\0'
  45.                name:NXUniqueString("PrintAll")
  46.              target:[ExportAll new]
  47.              action:@selector(printAll:)];
  48.     return self;
  49. }
  50.  
  51. - init {return [super init];}
  52.  
  53. - free {return self;}
  54.  
  55. - exportOpen:sender
  56. {
  57.     char    path[MAXPATHLEN];
  58.     char    newPath[MAXPATHLEN];
  59.     const char     *docdir;
  60.     long    key; 
  61.     id        doc; 
  62.     NXHashState  state;
  63.     id docTable;
  64.     
  65.     int choice, fmt;
  66.     
  67.     choice = NXRunAlertPanel("Export All","What format would you like to export all open, untouched (current .etfd on disk) documents to?", "HTML","LaTeX","ASCII");
  68.     switch (choice) {
  69.         case NX_ALERTDEFAULT: fmt = HTMD_FMT; break;
  70.         case NX_ALERTALTERNATE: fmt = TeXD_FMT; break;
  71.         case NX_ALERTOTHER: fmt = ASCII_FMT; break;
  72.     }
  73.  
  74.     
  75.     docTable= [[NXApp etApp] docTable];
  76.     state = [docTable initState];
  77.     
  78.     while ([docTable nextState: &state key:(void **)&key value:(void **)&doc] &&
  79.             !NXUserAborted())  {
  80.         if (![doc needsSaving]) {
  81.             strcpy(path, [[doc docInfo] docPath]);
  82.             // CRITICALLY IMPORTANT: come up with a virgin path for each format
  83.             switch (fmt) {
  84.                 case HTMD_FMT:  *rindex(path, '.') = 0;
  85.                                 docdir = [[NXApp userModel] stringQuery:HTMDIRECTORY];
  86.                                 if (docdir && *docdir) {
  87.                                     sprintf(newPath,"%s/%s."HTMD_EXT, 
  88.                                             docdir, rindex(path, '/')+1);
  89.                                 } else {
  90.                                     sprintf(newPath,"%s."HTMD_EXT, path);
  91.                                 }
  92.                                 break;
  93.                 case TeXD_FMT:  *rindex(path, '.') = 0;
  94.                                 sprintf(newPath,"%s."TeXD_EXT, path); 
  95.                                 break;
  96.                 case ASCII_FMT: *rindex(path, '.') = 0;
  97.                                 sprintf(newPath,"%s."ASCII_EXT, path); 
  98.                                 break;
  99.                 default:         return nil;
  100.             }
  101.             if (*newPath && strcmp(newPath, path)) {
  102.                 [doc saveTo:newPath inFormat:fmt changePath:NO];
  103.             }
  104.         }
  105.     }
  106.     return self;
  107. }
  108.  
  109. - exportAll:sender
  110. {
  111.     char    path[MAXPATHLEN];
  112.     char    newPath[MAXPATHLEN];
  113.     const char     *docdir;
  114.     id        docInfo; 
  115.     int  i,N;
  116.     id docList;
  117.     
  118.     int choice, fmt;
  119.     
  120.     choice = NXRunAlertPanel("Export All","What format would you like to export EVERY DOCUMENT to?", "HTML","LaTeX","ASCII");
  121.     switch (choice) {
  122.         case NX_ALERTDEFAULT: fmt = HTMD_FMT; break;
  123.         case NX_ALERTALTERNATE: fmt = TeXD_FMT; break;
  124.         case NX_ALERTOTHER: fmt = ASCII_FMT; break;
  125.     }
  126.  
  127.     
  128.     docList= [[navigator query:"*" field: "*"] copy];
  129.     N = [docList count];
  130.     
  131.     for (i=0; i<N; i++)  {
  132.         if (NXUserAborted()) break;
  133.         docInfo = [docList objectAt:(i)];
  134.         [etApp openID:[docInfo docID]];
  135.         NXPing(); // Paranoia
  136.         if ([docInfo etDoc] && ![[docInfo etDoc] needsSaving]) {
  137.             [[[[docInfo etDoc] docUI] window] display];
  138.             strcpy(path, [docInfo docPath]);
  139.             // CRITICALLY IMPORTANT: come up with a virgin path for each format
  140.             switch (fmt) {
  141.                 case HTMD_FMT:  *rindex(path, '.') = 0;
  142.                                 docdir = [[NXApp userModel] stringQuery:HTMDIRECTORY];
  143.                                 if (docdir && *docdir) {
  144.                                     sprintf(newPath,"%s/%s."HTMD_EXT, 
  145.                                             docdir, rindex(path, '/')+1);
  146.                                 } else {
  147.                                     sprintf(newPath,"%s."HTMD_EXT, path);
  148.                                 }
  149.                                 break;
  150.                 case TeXD_FMT:  *rindex(path, '.') = 0;
  151.                                 sprintf(newPath,"%s."TeXD_EXT, path); 
  152.                                 break;
  153.                 case ASCII_FMT: *rindex(path, '.') = 0;
  154.                                 sprintf(newPath,"%s."ASCII_EXT, path); 
  155.                                 break;
  156.                 default:         return nil;
  157.             }
  158.             if (*newPath && strcmp(newPath, path)) {
  159.                 [[docInfo etDoc] saveTo:newPath inFormat:fmt changePath:NO];
  160.             }
  161.         }
  162.         [etApp closeID:[docInfo docID]];
  163.     }
  164.     [docList free];
  165.     return self;
  166. }
  167.  
  168. static BOOL runPrintPanel=YES;
  169.  
  170. -(BOOL)shouldRunPrintPanel:aView {return runPrintPanel;} 
  171. // batch-mode printing requests... you were warned!
  172. - printAll:sender {
  173.     long    key; 
  174.     id         doc; 
  175.     NXHashState  state;
  176.     id docTable = [[NXApp etApp] docTable];
  177.     state = [docTable initState];
  178.     
  179.     runPrintPanel= YES;
  180.     while ([docTable nextState: &state key:(void **)&key value:(void **)&doc] &&
  181.             !NXUserAborted())  {
  182.         [[doc docUI] print:self];
  183.         // run the panel only once
  184.         if (runPrintPanel) runPrintPanel = NO;
  185.     }
  186.     runPrintPanel = YES;
  187.     return self;
  188. }
  189. @end