home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / SaveFromMail1_03 / Source / Controller.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  5.5 KB  |  204 lines

  1. /*
  2.  * Controller.m
  3.  *
  4.  *    This program is the NeXTStep wrapper for import_export_mailrc.l
  5.  *      It also provides a service for saving (portions of) mail in files.
  6.  * 
  7.  * Copyright (C) 1991 Roberto Di Cosmo
  8.  *
  9.  * Everyone is granted permission to copy, modify and redistribute
  10.  * this file, as far as this notice is preserved on all copies.
  11.  *
  12.  * Written by:
  13.  *      Roberto Di Cosmo <dicosmo@dmi.ens.fr>
  14.  *      Dipartimento di Informatica     LIENS
  15.  *      Universita' di Pisa             Ecole Normale Superieure
  16.  *      Corso Italia, 40                45, Rue d'Ulm
  17.  *      Pisa ITALY                      Paris FRANCE
  18.  *
  19.  * $Header: /users/formel/dicosmo/Projects/SAVEFROMMAIL/RCS/Controller.m,v 1.3 92/05/06 21:17:13 dicosmo Exp Locker: dicosmo $
  20.  *
  21.  * $Log:    Controller.m,v $
  22.  * Revision 1.3  92/05/06  21:17:13  dicosmo
  23.  * Removed SavePanel free,
  24.  * now stays up!
  25.  * 
  26.  * Revision 1.2  92/05/06  16:52:39  dicosmo
  27.  * Rev 1.2
  28.  * 
  29.  * Revision 1.1  92/05/06  14:18:17  dicosmo
  30.  * Initial revision
  31.  * 
  32.  *
  33.  */
  34.  
  35. /* Generated by Interface Builder */
  36.  
  37. #import <stdio.h>
  38. #import "Controller.h"
  39. #import <appkit/Application.h>
  40. #import <appkit/Panel.h>
  41. #import <appkit/Listener.h>
  42. #import <appkit/ScrollView.h>
  43. #import <appkit/SavePanel.h>
  44. #import <appkit/Text.h>
  45. #import <appkit/Pasteboard.h>
  46. #import <appkit/TextField.h>
  47. #import <stdlib.h>
  48. #import <strings.h>
  49. #import <mach_init.h>
  50. /* #import <vm/vm_user.h> */
  51. #import <sys/stat.h>
  52. #import <defaults/defaults.h>
  53. #import <pwd.h>
  54.  
  55.  
  56. @implementation Controller
  57.  
  58. - appDidInit:sender
  59. {
  60.     myListener = [NXApp appListener];
  61.     [myListener setServicesDelegate: self];
  62.     return self;
  63. }
  64.  
  65. /* Brings up the Info panel.   Not done on startup because it's in a separate
  66.  * interface file. Saves startup time for the user if we do this when they ask
  67.  * for it, and not before.
  68.  * "infoPanel" and "helpPanel" are defined in "Info.nib".
  69.  */
  70. - infoPanel:sender
  71. {
  72.     if( ! infoPanel ){
  73.         [NXApp loadNibSection:"Info.nib" owner:self];
  74.     }
  75.     [infoPanel makeKeyAndOrderFront:sender];
  76.     return self;    
  77. }
  78. /*
  79.  * Brings up the Help panel (as above)
  80.  */
  81. - helpPanel:sender
  82. {
  83.     if( ! helpPanel ){
  84.         [NXApp loadNibSection:"Info.nib" owner:self];
  85.     }
  86.     [helpPanel makeKeyAndOrderFront:sender];
  87.     return self;    
  88.  
  89. }
  90.  
  91. extern void import_mailrc();
  92. extern FILE *freopen();
  93.  
  94. - importMailrcFile:sender;
  95. {
  96.     char *Unixmailrcfn;
  97.     char *NeXTmailaliasfn;
  98.     FILE *old_stdin;
  99.     FILE *old_stdout;
  100.     
  101.     Unixmailrcfn  = malloc(550);
  102.     NeXTmailaliasfn = malloc(550);
  103.     (void)strcpy(Unixmailrcfn, strcpy(NeXTmailaliasfn, 
  104.                            NXHomeDirectory()));
  105.     (void)strcat(Unixmailrcfn, "/.mailrc");
  106.     (void)strcat(NeXTmailaliasfn, "/.NeXT/.mailalias");
  107.     /* Save stdin and stdout across freopen call */
  108.     old_stdin  = stdin;
  109.     old_stdout = stdout;
  110.         if ((freopen(Unixmailrcfn,"r",stdin))     != NULL)
  111.     if ((freopen(NeXTmailaliasfn,"a",stdout)) != NULL)
  112.             {import_mailrc();
  113.         /* close open files */
  114.         fclose(stdin);
  115.         fclose(stdout);
  116.         }
  117.     else
  118.           NXRunAlertPanel("Import .mailrc Service Alert","Cannot open mail default files :-(", NULL, NULL, NULL);
  119.     /* Restore stdin and stdout after freopen call */
  120.     /* Is it necessary? And how do you do it? 
  121.         stdin  = old_stdin;
  122.     stdout = old_stdout; */
  123.     return self;
  124. }
  125.  
  126.  
  127. extern void export_mailrc();
  128.  
  129. - exportMailaliasFile:sender /* Ok, just wait a second! :-) */
  130. {
  131.     char *Unixmailrcfn;
  132.     char *NeXTmailaliasfn;
  133.     FILE *old_stdin;
  134.     FILE *old_stdout;
  135.     
  136.     Unixmailrcfn  = malloc(550);
  137.     NeXTmailaliasfn = malloc(550);
  138.     (void)strcpy(Unixmailrcfn, strcpy(NeXTmailaliasfn, 
  139.                            NXHomeDirectory()));
  140.     (void)strcat(Unixmailrcfn, "/.mailrc");
  141.     (void)strcat(NeXTmailaliasfn, "/.NeXT/.mailalias");
  142.     /* Save stdin and stdout across freopen call */
  143.     old_stdin  = stdin;
  144.     old_stdout = stdout;
  145.         if ((freopen(NeXTmailaliasfn,"r",stdin))     != NULL)
  146.     if ((freopen(Unixmailrcfn,"a",stdout)) != NULL)
  147.             {export_mailrc();
  148.         /* close open files */
  149.         fclose(stdin);
  150.         fclose(stdout);
  151.         }
  152.     else
  153.           NXRunAlertPanel("Export .mailrc Service Alert","Cannot open mail default files :-(", NULL, NULL, NULL);
  154.     /* Restore stdin and stdout after freopen call */
  155.     /* Is it necessary? And how do you do it? 
  156.         stdin  = old_stdin;
  157.     stdout = old_stdout; */
  158.     return self;
  159. }
  160.  
  161. - SaveSelection:(id)pbid userData:(const char *)udata error:(char **)errmsg
  162. {
  163.   SavePanel *panel;
  164.   const char *name = NULL, *path = NULL;
  165.   FILE *fpout;
  166.   char *data; 
  167.   int   length;
  168.   int savePanel = 0;
  169.   const char *const *types;
  170.   int   hasAscii, i;
  171.   int rest = 0, fits, divs;
  172.   int bufsize;
  173.   
  174.   bufsize = 1024*64;
  175.   types = [pbid types];        /* get a list of pasteboard types */
  176.   hasAscii = 0;
  177.   for(i=0; !hasAscii && types[i]; i++)
  178.     if(!strcmp(types[i], NXAsciiPboardType)) hasAscii = 1;
  179.   if(hasAscii)
  180.     {
  181.       [pbid readType:NXAsciiPboardType data:&data length:&length];
  182.       if(data && length)
  183.         {
  184.           if (!savePanel) {panel = [SavePanel new];savePanel =1;};
  185.           if ([panel runModalForDirectory:path file:name])
  186.             { 
  187.               if ((fpout = fopen([panel filename],"a")) != NULL)
  188.                 {
  189.                   if (length < bufsize) fwrite(data,length,1,fpout);
  190.                   else {fwrite(data,bufsize,(divs = length/bufsize),fpout);
  191.                         if ((rest = (length - (fits = divs*bufsize))) != 0)
  192.                           {fwrite(data+fits,rest,1,fpout);};
  193.                       };
  194.                fclose(fpout); printf ("Saved file from mail\n");
  195.                 };
  196. /*              [panel free]; */
  197.             };
  198.         };
  199.     };
  200.   return self;
  201. }
  202.  
  203. @end
  204.