home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XFM / XFM-1.3 / XFM-1 / xfm-1.3 / xfm / FmInfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-30  |  7.1 KB  |  275 lines

  1. /*---------------------------------------------------------------------------
  2.   Module FmInfo
  3.  
  4.   (c) Simon Marlow 1990-92
  5.   (c) Albert Graef 1994
  6.  
  7.   Routines for creating and initialising the info window.
  8. ---------------------------------------------------------------------------*/
  9.  
  10. #include <pwd.h>
  11. #include <time.h>
  12. #include <grp.h>
  13.  
  14. #include <X11/Intrinsic.h>
  15. #include <X11/StringDefs.h>
  16. #include <X11/Shell.h>
  17. #include <X11/Xaw/Label.h>
  18. #include <X11/Xaw/Box.h>
  19. #include <X11/Xaw/Form.h>
  20.  
  21. #include "Am.h"
  22. #include "Fm.h"
  23.  
  24. #define LABEL_WIDTH 200
  25. #define PADDING 30
  26.  
  27. #define I_NAME 0
  28. #define I_LENGTH 1
  29. #define I_OWNER 2
  30. #define I_GROUP 3
  31. #define I_ACCESS 4
  32. #define I_TYPE 5
  33. #define I_LINK 6
  34. #define I_MODIFY_T 7
  35. #define I_STATUS_T 8
  36. #define N_ITEMS 9
  37.  
  38. /*---------------------------------------------------------------------------
  39.   STATIC DATA
  40. ---------------------------------------------------------------------------*/
  41.  
  42. typedef struct {
  43.   FileRec *file;
  44.   Widget shell;
  45.   Widget items[N_ITEMS];
  46. } InfoData;
  47.  
  48. static InfoData info;
  49.  
  50. /*---------------------------------------------------------------------------
  51.   Strings to go in labels
  52. ---------------------------------------------------------------------------*/
  53.  
  54. static String labels[] = {
  55.   "Name",
  56.   "Length",
  57.   "Owner",
  58.   "Group",
  59.   "Access Permissions",
  60.   "Type",
  61.   "Symbolic Link To",
  62.   "Last Modification",
  63.   "Last Status Change"
  64.   };
  65.  
  66. /*---------------------------------------------------------------------------
  67.   Widget argument lists
  68. ---------------------------------------------------------------------------*/
  69.  
  70. static Arg *form_args = NULL;
  71.  
  72. static Arg left_args[] = {
  73.   { XtNfromHoriz, (XtArgVal) NULL },
  74.   { XtNfromVert, (XtArgVal) NULL },
  75.   { XtNlabel, (XtArgVal) NULL },
  76.   { XtNwidth, (XtArgVal) 0 },
  77.   { XtNjustify, XtJustifyRight },
  78.   { XtNfont, (XtArgVal) NULL },
  79.   { XtNresize, False },
  80.   { XtNinternalWidth, (XtArgVal) 0 },
  81.   { XtNhorizDistance, (XtArgVal) PADDING },
  82.   { XtNtop, XtChainTop },
  83.   { XtNbottom, XtChainTop },
  84.   { XtNleft, XtChainLeft },
  85.   { XtNright, XtChainLeft }
  86. };
  87.  
  88. static Arg right_args[] = {
  89.   { XtNfromHoriz, (XtArgVal) NULL },
  90.   { XtNfromVert, (XtArgVal) NULL },
  91.   { XtNlabel, (XtArgVal) NULL },
  92.   { XtNwidth, (XtArgVal) LABEL_WIDTH },
  93.   { XtNjustify, XtJustifyLeft },
  94.   { XtNfont, (XtArgVal) NULL },
  95.   { XtNresize, False },
  96.   { XtNinternalWidth, (XtArgVal) 0 },
  97.   { XtNhorizDistance, (XtArgVal) PADDING },
  98.   { XtNtop, XtChainTop },
  99.   { XtNbottom, XtChainTop },
  100.   { XtNleft, XtChainLeft },
  101.   { XtNright, XtChainRight }
  102. };
  103.  
  104. static Arg button_box_args[] = {
  105.   { XtNfromHoriz, (XtArgVal) NULL },
  106.   { XtNfromVert, (XtArgVal) NULL },
  107.   { XtNtop, XtChainTop },
  108.   { XtNbottom, XtChainTop },
  109.   { XtNleft, XtChainLeft },
  110.   { XtNright, XtChainLeft }
  111. };
  112.  
  113. static Arg shell_args[] = {
  114.   { XtNtitle, (XtArgVal) "File Information" }
  115. };
  116.  
  117. /*---------------------------------------------------------------------------
  118.   PRIVATE FUNCTIONS
  119. ---------------------------------------------------------------------------*/
  120.  
  121. FmCallbackProc infoOkCb;
  122.  
  123. void infoOkCb(Widget w, FileWindowRec *fw, XtPointer call_data)
  124. {
  125.   freeze = False;
  126.   XtPopdown(info.shell);
  127. }
  128.  
  129. /*---------------------------------------------------------------------------
  130.   Button data
  131. ---------------------------------------------------------------------------*/
  132.  
  133. static ButtonRec info_buttons[] = {
  134.   { "ok", "Ok", infoOkCb }
  135. };
  136.  
  137. /*---------------------------------------------------------------------------
  138.   PUBLIC FUNCTIONS
  139. ---------------------------------------------------------------------------*/
  140.  
  141. void createInfoPopup()
  142. {
  143.   int i;
  144.   Widget form, w;
  145.   Dimension left_width = 0;
  146.  
  147.   /* create shell */
  148.   info.shell = XtCreatePopupShell("info", transientShellWidgetClass,
  149.                    aw.shell, shell_args, XtNumber(shell_args));
  150.  
  151.   /* create outer form */
  152.   form = XtCreateManagedWidget("form", formWidgetClass, info.shell,
  153.                    form_args, XtNumber(form_args) );
  154.  
  155.   left_args[5].value = (XtArgVal) resources.bold_font;
  156.   right_args[5].value = (XtArgVal) resources.label_font;
  157.   
  158.   for (i=0; i<N_ITEMS; i++) {
  159.     Dimension l;
  160.     l = XTextWidth(resources.bold_font, labels[i], strlen(labels[i]));
  161.     if (l > left_width)
  162.       left_width = l;
  163.   }
  164.  
  165.   left_args[3].value = (XtArgVal) left_width;
  166.  
  167.   /* create all the smaller labels */
  168.   w = NULL;
  169.   for (i=0; i<N_ITEMS; i++) {
  170.     left_args[1].value = (XtArgVal) w;
  171.     right_args[1].value = (XtArgVal) w;
  172.     left_args[2].value = (XtArgVal) labels[i];
  173.     w = XtCreateManagedWidget("leftlabel", labelWidgetClass, form,
  174.                   left_args, XtNumber(left_args));
  175.  
  176.     right_args[0].value = (XtArgVal) w;
  177.     info.items[i] = XtCreateManagedWidget("rightlabel", labelWidgetClass, form,
  178.                       right_args, XtNumber(right_args));
  179.   }
  180.  
  181.   button_box_args[1].value = (XtArgVal) w;
  182.   w = XtCreateManagedWidget("button box", boxWidgetClass, form,
  183.                 button_box_args, XtNumber(button_box_args));
  184.   createButtons(info_buttons, XtNumber(info_buttons), w, NULL);
  185.  
  186.   XtRealizeWidget(info.shell);
  187. }
  188.  
  189. /*---------------------------------------------------------------------------*/
  190.  
  191. void infoPopup(Widget w, FileWindowRec *fw, XtPointer call_data)
  192. {
  193.   char s[11], link[MAXPATHLEN];
  194.   char *type;
  195.   struct passwd *pw;
  196.   struct group *gp;
  197.   int i;
  198.  
  199.   if (fw == NULL) fw = popup_fw;
  200.  
  201.   if (!fw->n_selections) return;
  202.  
  203.   for (i=0;; i++)
  204.     if (fw->files[i]->selected) {
  205.       info.file = fw->files[i];
  206.       break;
  207.     }
  208.  
  209.   XtVaSetValues(info.items[I_NAME], XtNlabel, (XtArgVal) info.file->name, 
  210.         NULL);
  211.  
  212.   sprintf(s, "%ld", (long) info.file->stats.st_size);
  213.   XtVaSetValues(info.items[I_LENGTH], XtNlabel, (XtArgVal) s, NULL);
  214.  
  215.   if (!(pw = getpwuid(info.file->stats.st_uid)))
  216.     sprintf(s, "%d", info.file->stats.st_uid);
  217.   XtVaSetValues(info.items[I_OWNER], XtNlabel, (XtArgVal) (pw?pw->pw_name:s),
  218.         NULL);
  219.  
  220.   if (!(gp = getgrgid(info.file->stats.st_gid)))
  221.     sprintf(s, "%d", info.file->stats.st_gid);
  222.   XtVaSetValues(info.items[I_GROUP], XtNlabel, (XtArgVal) (gp?gp->gr_name:s),
  223.         NULL);
  224.  
  225.   makePermissionsString(s, info.file->stats.st_mode);
  226.   XtVaSetValues(info.items[I_ACCESS], XtNlabel, (XtArgVal) s, NULL);
  227.  
  228.   if(info.file->sym_link) {
  229.     type = "Symbolic link";
  230.     if (chdir(fw->directory)) {
  231.       sysError("System error:");
  232.       return;
  233.     }
  234.     
  235.     i = readlink(info.file->name, link, MAXPATHLEN);
  236.     if (i == -1) {
  237.       sysError("Error reading link:");
  238.       return;
  239.     }
  240.     link[i] = '\0';
  241.     
  242.     XtVaSetValues(info.items[I_LINK], XtNlabel, (XtArgVal) link, NULL);
  243.   }    
  244.   else {
  245.     mode_t mode = info.file->stats.st_mode;
  246.  
  247.     if (S_ISDIR(mode))
  248.       type = "Directory";
  249.     else if (S_ISCHR(mode))
  250.       type = "Character special file";
  251.     else if(S_ISBLK(mode))
  252.       type = "Block special file";
  253.     else if(S_ISSOCK(mode))
  254.       type = "Socket";
  255.     else if(S_ISFIFO(mode))
  256.       type = "Pipe or FIFO special file";
  257.     else
  258.       type = "Ordinary file";
  259.  
  260.     XtVaSetValues(info.items[I_LINK], XtNlabel, (XtArgVal) "-", NULL);
  261.   }
  262.  
  263.   XtVaSetValues(info.items[I_TYPE], XtNlabel, (XtArgVal) type, NULL);
  264.  
  265.   XtVaSetValues(info.items[I_MODIFY_T], XtNlabel, 
  266.         (XtArgVal) ctime(&info.file->stats.st_mtime), NULL);
  267.  
  268.   XtVaSetValues(info.items[I_STATUS_T], XtNlabel, 
  269.         (XtArgVal) ctime(&info.file->stats.st_ctime), NULL);
  270.  
  271.   freeze = True;
  272.   popupByCursor(info.shell, XtGrabExclusive);
  273. }
  274.  
  275.