home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE-.1 / VIEW-FIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-22  |  7.7 KB  |  283 lines

  1. /*
  2.  * view-file.c : Routines for the windows when files are "opened" via ftp
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 23 Apr 1993.
  5.  */
  6. #include <stdio.h>
  7. #include <X11/Intrinsic.h>
  8. #include <X11/Shell.h>
  9. #include <X11/StringDefs.h>
  10. #include <X11/Xaw/Form.h>
  11. #include <X11/Xaw/Label.h>
  12. #include <X11/Xaw/Command.h>
  13. #include <X11/Xaw/AsciiText.h>
  14. #include "config.h"
  15. #ifdef HAVE_SYS_PARAM_H
  16. #include <sys/param.h>
  17. #endif
  18. #include "xarchie.h"
  19. #include "fchooser.h"
  20. #include "stringdefs.h"
  21. #include "xutil.h"
  22. #include "syserr.h"
  23. #include "debug.h"
  24.  
  25. /*
  26.  * Functions defined here:
  27.  */
  28. void viewFile();
  29.  
  30. static void nonmaskableEventHandler();
  31. static void viewDone(),viewDown(),viewUp(),viewSave();
  32. static void viewSaveOk(),viewSaveCancel();
  33. static int fileCopy();
  34.  
  35. /*
  36.  * Data defined here:
  37.  */
  38. typedef struct _ViewFileInfo {
  39.     Widget shell;
  40.     Widget text;
  41.     String filename;
  42. } ViewFileInfo;
  43.  
  44. /*    -    -    -    -    -    -    -    -    */
  45.  
  46. void
  47. viewFile(filename)
  48. char *filename;
  49. {
  50.     ViewFileInfo *vfinfo;
  51.     Widget form,button;
  52.     Arg args[2];
  53.  
  54.     vfinfo = XtNew(ViewFileInfo);
  55.     vfinfo->filename = XtNewString(filename);
  56.     XtSetArg(args[0],XtNtitle,filename);
  57.     vfinfo->shell = XtCreatePopupShell("viewShell",topLevelShellWidgetClass,
  58.                        toplevel,args,1);
  59.     form = XtCreateManagedWidget("viewForm",formWidgetClass,
  60.                  vfinfo->shell,NULL,0);
  61.     button = XtCreateManagedWidget("viewDoneButton",commandWidgetClass,
  62.                    form,NULL,0);
  63.     XtAddCallback(button,XtNcallback,viewDone,(XtPointer)vfinfo);
  64.     button = XtCreateManagedWidget("viewDownButton",commandWidgetClass,
  65.                    form,NULL,0);
  66.     XtAddCallback(button,XtNcallback,viewDown,(XtPointer)vfinfo);
  67.     button = XtCreateManagedWidget("viewUpButton",commandWidgetClass,
  68.                    form,NULL,0);
  69.     XtAddCallback(button,XtNcallback,viewUp,(XtPointer)vfinfo);
  70.     button = XtCreateManagedWidget("viewSaveButton",commandWidgetClass,
  71.                    form,NULL,0);
  72.     XtAddCallback(button,XtNcallback,viewSave,(XtPointer)vfinfo);
  73.     XtSetArg(args[0],XtNtype,XawAsciiFile);
  74.     XtSetArg(args[1],XtNstring,filename);
  75.     vfinfo->text = XtCreateManagedWidget("viewText",asciiTextWidgetClass,
  76.                      form,args,2);
  77.     XtRealizeWidget(vfinfo->shell);
  78.     /* Allow WM_DELETE_WINDOW to the Shell to be Done */
  79.     (void)XSetWMProtocols(XtDisplay(vfinfo->shell),XtWindow(vfinfo->shell),
  80.               &WM_DELETE_WINDOW,1);
  81.     XtAddEventHandler(vfinfo->shell,NoEventMask,True,
  82.               nonmaskableEventHandler,(XtPointer)vfinfo);
  83.     XtPopup(vfinfo->shell,XtGrabNone);
  84. }
  85.  
  86. /*
  87.  * Nonmaskable event handler for Shell: If the event is a ClientMessage
  88.  * of WM_PROTOCOLS then act as if Done had been clicked.
  89.  */
  90. static void
  91. nonmaskableEventHandler(w,client_data,event,continue_to_dispatch)
  92. Widget w;
  93. XtPointer client_data;
  94. XEvent *event;
  95. Boolean *continue_to_dispatch;
  96. {
  97.     DEBUG1("nonmaskableHandler: w=0x%x\n",w);
  98.     if (event->type == ClientMessage &&
  99.         event->xclient.data.l[0] == WM_DELETE_WINDOW) {
  100.     DEBUG0("nonmaskableHandler: calling cancelButtonCallback\n");
  101.     viewDone(NULL,client_data,NULL);
  102.     }
  103.     DEBUG0("nonmaskableHandler: done\n");
  104. }
  105.  
  106. /*    -    -    -    -    -    -    -    -    */
  107. /* Callbacks for the view window */
  108.  
  109. /*ARGSUSED*/
  110. static void
  111. viewDone(w,client_data,call_data)
  112. Widget w;
  113. XtPointer client_data;        /* ViewFileInfo */
  114. XtPointer call_data;        /* not used */
  115. {
  116.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  117.     char *name;
  118.  
  119.     DEBUG0("viewDone...\n");
  120.     name = getWidgetString(vfinfo->text);
  121.     if (name != NULL && *name != '\0') {
  122.     DEBUG1("viewDone: unlinking \"%s\"\n",name);
  123.     if (unlink(name) < 0)
  124.         sysError(name);
  125.     }
  126.     XtPopdown(vfinfo->shell);
  127.     XtUnrealizeWidget(vfinfo->shell);
  128.     XtDestroyWidget(vfinfo->shell);
  129.     XtFree(vfinfo->filename);
  130.     XtFree((char*)vfinfo);
  131.     DEBUG0("viewDone: done\n");
  132. }
  133.  
  134. /*ARGSUSED*/
  135. static void
  136. viewDown(w,client_data,call_data)
  137. Widget w;
  138. XtPointer client_data;        /* ViewFileInfo */
  139. XtPointer call_data;        /* not used */
  140. {
  141.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  142.  
  143.     XtCallActionProc(vfinfo->text,"next-page",NULL,NULL,0);
  144. }
  145.  
  146. /*ARGSUSED*/
  147. static void
  148. viewUp(w,client_data,call_data)
  149. Widget w;
  150. XtPointer client_data;        /* ViewFileInfo */
  151. XtPointer call_data;        /* not used */
  152. {
  153.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  154.  
  155.     XtCallActionProc(vfinfo->text,"previous-page",NULL,NULL,0);
  156. }
  157.  
  158. /*ARGSUSED*/
  159. static void
  160. viewSave(w,client_data,call_data)
  161. Widget w;
  162. XtPointer client_data;        /* ViewFileInfo */
  163. XtPointer call_data;        /* not used */
  164. {
  165.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  166.     FileChooserInfo *fcinfo;
  167.     Widget shell,form,text;
  168.     char *name,*basename;
  169.     Arg args[1];
  170.  
  171.     DEBUG0("viewSave...\n");
  172.     setBusyStatus(True);
  173.     shell = XtCreatePopupShell("viewSaveShell",topLevelShellWidgetClass,
  174.                    vfinfo->shell,NULL,0);
  175.     form = XtCreateManagedWidget("viewSaveForm",formWidgetClass,
  176.                  shell,NULL,0);
  177.     (void)XtCreateManagedWidget("viewSaveLabel",labelWidgetClass,
  178.                 form,NULL,0);
  179.     text = XtCreateManagedWidget("viewSaveLabelText",asciiTextWidgetClass,
  180.                  form,NULL,0);
  181.     name = vfinfo->filename;
  182.     setWidgetString(text,name);
  183.     fcinfo = createFileChooser(shell,form,"viewSave",viewSaveOk,
  184.                    viewSaveCancel,(XtPointer)vfinfo);
  185.     /* Adjust vertical layout */
  186.     XtSetArg(args[0],XtNfromVert,text);
  187. #ifdef FILECHOOSER
  188.     XtSetValues(fcinfo->fcw,args,1);
  189. #else
  190.     XtSetValues(fcinfo->text,args,1);
  191. #endif
  192.     /* Realize them all */
  193.     XtRealizeWidget(shell);
  194.     /* Set initial filename (has to be after realize for some reason) */
  195.     if ((basename=rindex(name,'/')) != NULL)
  196.     name = basename+1;
  197.     setWidgetString(fcinfo->text,name);
  198.     /* Register window for WM */
  199.     (void)XSetWMProtocols(XtDisplay(shell),XtWindow(shell),
  200.               &WM_DELETE_WINDOW,1);
  201.     /* Here we go */
  202.     XtPopup(shell,XtGrabNone);
  203.     setBusyStatus(False);
  204.     DEBUG0("viewSave: done\n");
  205. }
  206.  
  207. /*    -    -    -    -    -    -    -    -    */
  208. /* Callbacks from the view-save FileChooser */
  209.  
  210. /*ARGSUSED*/
  211. static void
  212. viewSaveOk(fcinfo,filename,client_data)
  213. FileChooserInfo *fcinfo;
  214. char *filename;
  215. XtPointer client_data;        /* ViewFileInfo */
  216. {
  217.     ViewFileInfo *vfinfo = (ViewFileInfo *)client_data;
  218.  
  219.     DEBUG1("viewSaveOk: fcinfo=0x%x\n",fcinfo);
  220.     DEBUG2("viewSaveOk: copying \"%s\" to \"%s\"\n",vfinfo->filename,filename);
  221.     if (fileCopy(vfinfo->filename,filename) >= 0) {
  222.     /* Remove the File Selector if successful */
  223.     XtPopdown(fcinfo->shell);
  224.     XtUnrealizeWidget(fcinfo->shell);
  225.     XtDestroyWidget(fcinfo->shell);
  226.     XtFree((char*)fcinfo);
  227.     }
  228.     DEBUG0("viewSaveOk: done\n");
  229. }
  230.  
  231. /*ARGSUSED*/
  232. static void
  233. viewSaveCancel(fcinfo,client_data)
  234. FileChooserInfo *fcinfo;
  235. XtPointer client_data;        /* ViewFileInfo */
  236. {
  237.     DEBUG1("viewSaveCancel: fcinfo=0x%x\n",fcinfo);
  238.     XtPopdown(fcinfo->shell);
  239.     XtUnrealizeWidget(fcinfo->shell);
  240.     XtDestroyWidget(fcinfo->shell);
  241.     XtFree((char*)fcinfo);
  242.     DEBUG0("viewSaveCancel: done\n");
  243. }
  244.  
  245. /*    -    -    -    -    -    -    -    -    */
  246. /* Misc. functions */
  247.  
  248. static int
  249. fileCopy(path1,path2)
  250. char *path1,*path2;
  251. {
  252.     FILE *infp,*outfp;
  253.     char buf[BUFSIZ];
  254.     int n,retcode;
  255.  
  256.     DEBUG2("fileCopy: \"%s\" \"%s\"\n",path1,path2);
  257.     if ((infp=fopen(path1,"r")) == NULL) {
  258.     sysError(path1);
  259.     return(-1);
  260.     }
  261.     if ((outfp=fopen(path2,"w")) == NULL) {
  262.     sysError(path2);
  263.     return(-1);
  264.     }
  265.     retcode = 0;
  266.     while (!feof(infp)) {
  267.     if ((n=fread(buf,1,sizeof(buf),infp)) <= 0) {
  268.         sysError("read");
  269.         retcode = -1;
  270.         break;
  271.     }
  272.     if (fwrite(buf,1,n,outfp) != n) {
  273.         sysError("write");
  274.         retcode = -1;
  275.         break;
  276.     }
  277.     }
  278.     fclose(infp);
  279.     fclose(outfp);
  280.     DEBUG0("fileCopy: done\n");
  281.     return(retcode);
  282. }
  283.