"Fossies" - the Fresh Open Source Software Archive

Member "xzgv-0.9.2/src/filedetails.c" (3 Sep 2017, 7173 Bytes) of package /linux/misc/old/xzgv-0.9.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "filedetails.c" see the Fossies "Dox" file reference documentation.

    1 /* xzgv v0.2 - picture viewer for X, with file selector.
    2  * Copyright (C) 1999 Russell Marks. See main.c for license details.
    3  *
    4  * filedetails.c - code for file details dialog.
    5  */
    6 
    7 #include <stdio.h>
    8 #include <string.h>
    9 #include <time.h>
   10 #include <pwd.h>
   11 #include <grp.h>
   12 #include <sys/types.h>
   13 #include <sys/stat.h>
   14 #include <gtk/gtk.h>
   15 #include <gdk/gdkkeysyms.h>
   16 #include "backend.h"
   17 #include "main.h"
   18 
   19 #include "filedetails.h"
   20 
   21 
   22 /* make `rw-r--r--'-style permission string from mode. */
   23 char *make_perms_string(int mode)
   24 {
   25 static char buf[10];
   26 int f,shift,submode;
   27 char *execptr;
   28 
   29 strcpy(buf,"---------");
   30 
   31 for(f=0,shift=6;f<3;f++,shift-=3)
   32   {
   33   /* first do basic `rwx' bit. */
   34   submode=((mode>>shift)&7);
   35   if(submode&4) buf[f*3+0]='r';
   36   if(submode&2) buf[f*3+1]='w';
   37   if(submode&1) buf[f*3+2]='x';
   38   
   39   execptr=buf+f*3+2;
   40   
   41   /* apply any setuid/setgid/sticky bits */
   42   switch(f)
   43     {
   44     case 0: if(mode&04000) *execptr=((*execptr=='x')?'s':'S'); break;
   45     case 1: if(mode&02000) *execptr=((*execptr=='x')?'s':'S'); break;
   46     case 2: if(mode&01000) *execptr=((*execptr=='x')?'t':'T'); break;
   47     }
   48   }
   49 
   50 return(buf);
   51 }
   52 
   53 
   54 /* returns window widget */
   55 GtkWidget *make_details_win(char *filename)
   56 {
   57 GtkWidget *details_win,*vbox;
   58 GtkWidget *action_tbl,*button;
   59 GtkWidget *os_frame,*os_tbl;
   60 GtkWidget *tn_frame,*tn_tbl;
   61 GtkWidget *label;
   62 static char buf[1024];
   63 struct stat sbuf;
   64 struct tm *ctime;
   65 struct passwd *pwptr;
   66 struct group *grptr;
   67 FILE *tn;
   68 int tbl_row;
   69 int got_stat_info=(stat(filename,&sbuf)!=-1);
   70 int got_dimensions=0;
   71 int w,h;
   72 char *ptr;
   73 
   74 if((ptr=strrchr(filename,'/'))==NULL)
   75   {
   76   strcpy(buf,".xvpics/");
   77   strcat(buf,filename);
   78   }
   79 else
   80   {
   81   strcpy(buf,filename);
   82   strcpy(strrchr(buf,'/')+1,".xvpics/");
   83   strcat(buf,ptr+1);
   84   }
   85 
   86 if((tn=fopen(buf,"rb"))!=NULL)
   87   {
   88   fgets(buf,sizeof(buf),tn);    /* lose first line */
   89   fgets(buf,sizeof(buf),tn);    /* this may be "#IMGINFO:123x456 <type>" */
   90   while(buf[0]=='#')
   91     {
   92     if(sscanf(buf,"#IMGINFO:%dx%d",&w,&h)==2)
   93       {
   94       got_dimensions=1;
   95       break;
   96       }
   97     /* otherwise try another comment line */
   98     fgets(buf,sizeof(buf),tn);
   99     }
  100   fclose(tn);
  101   }
  102 
  103 
  104 details_win=gtk_dialog_new();
  105 
  106 gtk_window_set_title(GTK_WINDOW(details_win),"File Details");
  107 gtk_window_set_policy(GTK_WINDOW(details_win),FALSE,TRUE,FALSE);
  108 gtk_window_set_position(GTK_WINDOW(details_win),GTK_WIN_POS_CENTER);
  109 /* it's kludgey making it modal, but it does simplify things... */
  110 gtk_window_set_modal(GTK_WINDOW(details_win),TRUE);
  111 
  112 /* make a new vbox for the top part so we can get spacing more sane */
  113 vbox=gtk_vbox_new(FALSE,10);
  114 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(details_win)->vbox),
  115                    vbox,TRUE,TRUE,0);
  116 gtk_widget_show(vbox);
  117 
  118 gtk_container_set_border_width(GTK_CONTAINER(vbox),5);
  119 gtk_container_set_border_width(
  120   GTK_CONTAINER(GTK_DIALOG(details_win)->action_area),5);
  121 
  122 /* add ok button */
  123 action_tbl=gtk_table_new(1,5,TRUE);
  124 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(details_win)->action_area),
  125                    action_tbl,TRUE,TRUE,0);
  126 gtk_widget_show(action_tbl);
  127 
  128 button=gtk_button_new_with_label("Ok");
  129 gtk_table_attach_defaults(GTK_TABLE(action_tbl),button,2,3,0,1);
  130 gtk_signal_connect_object(GTK_OBJECT(button),"clicked",
  131                           GTK_SIGNAL_FUNC(gtk_widget_destroy),
  132                           GTK_OBJECT(details_win));
  133 gtk_widget_grab_focus(button);
  134 gtk_widget_show(button);
  135 
  136 /* add file details */
  137 os_frame=gtk_frame_new("Details from OS");
  138 gtk_box_pack_start(GTK_BOX(vbox),os_frame,TRUE,TRUE,0);
  139 gtk_widget_show(os_frame);
  140 
  141 /* 3 columns used to get the proportions nice - left one is for
  142  * description (e.g. "Filename:"), right two for value (e.g. "foo.jpg").
  143  */
  144 os_tbl=gtk_table_new(6,3,TRUE);
  145 gtk_container_add(GTK_CONTAINER(os_frame),os_tbl);
  146 gtk_table_set_col_spacings(GTK_TABLE(os_tbl),20);
  147 gtk_table_set_row_spacings(GTK_TABLE(os_tbl),8);
  148 gtk_container_set_border_width(GTK_CONTAINER(os_tbl),8);
  149 gtk_widget_show(os_tbl);
  150 
  151 /* couldn't get gtk_label_set_justify() to work (perhaps this is
  152  * only relevant to multi-line labels?), but gtk_misc_set_alignment() is ok.
  153  */
  154 #define DO_TBL_LEFT(table,row,name) \
  155   label=gtk_label_new(name);                \
  156   gtk_misc_set_alignment(GTK_MISC(label),1.,0.5);   \
  157   gtk_table_attach_defaults(GTK_TABLE(table),label, 0,1, (row),(row)+1); \
  158   gtk_widget_show(label)
  159 
  160 #define DO_TBL_RIGHT(table,row,name) \
  161   label=gtk_label_new(name);                \
  162   gtk_misc_set_alignment(GTK_MISC(label),0.,0.5);   \
  163   gtk_table_attach_defaults(GTK_TABLE(table),label, 1,3, (row),(row)+1); \
  164   gtk_widget_show(label)
  165 
  166 /* some of the info below (size, mtime) we have in the data ptr too,
  167  * but we need the stat() result for permissions, so we might as well
  168  * use it for the lot.
  169  */
  170 
  171 tbl_row=0;
  172 
  173 DO_TBL_LEFT(os_tbl,tbl_row,"Filename:");
  174 DO_TBL_RIGHT(os_tbl,tbl_row,filename);
  175 tbl_row++;
  176 
  177 strcpy(buf,"unknown");
  178 
  179 DO_TBL_LEFT(os_tbl,tbl_row,"Size:");
  180 if(got_stat_info)
  181   sprintf(buf,"%d  (%dk)",(int)sbuf.st_size,((int)sbuf.st_size+1023)/1024);
  182 DO_TBL_RIGHT(os_tbl,tbl_row,buf);
  183 tbl_row++;
  184 
  185 DO_TBL_LEFT(os_tbl,tbl_row,"Last modified:");
  186 if(got_stat_info)
  187   {
  188   if((ctime=localtime(&sbuf.st_mtime))==NULL)   /* can't happen? */
  189     strcpy(buf,"unknown");
  190   else
  191     sprintf(buf,"%d-%02d-%02d  %02d:%02d",
  192             1900+ctime->tm_year,ctime->tm_mon+1,ctime->tm_mday,
  193             ctime->tm_hour,ctime->tm_min);
  194   }
  195 DO_TBL_RIGHT(os_tbl,tbl_row,buf);
  196 tbl_row++;
  197 
  198 DO_TBL_LEFT(os_tbl,tbl_row,"Permissions:");
  199 if(got_stat_info)
  200   sprintf(buf,"%s  (%o)",
  201           make_perms_string(sbuf.st_mode&07777),sbuf.st_mode&07777);
  202 DO_TBL_RIGHT(os_tbl,tbl_row,buf);
  203 tbl_row++;
  204 
  205 DO_TBL_LEFT(os_tbl,tbl_row,"Owner:");
  206 pwptr=NULL;
  207 if(got_stat_info)
  208   pwptr=getpwuid(sbuf.st_uid);
  209 DO_TBL_RIGHT(os_tbl,tbl_row,pwptr?pwptr->pw_name:"unknown");
  210 tbl_row++;
  211 
  212 DO_TBL_LEFT(os_tbl,tbl_row,"Group:");
  213 grptr=NULL;
  214 if(got_stat_info)
  215   grptr=getgrgid(sbuf.st_gid);
  216 DO_TBL_RIGHT(os_tbl,tbl_row,grptr?grptr->gr_name:"unknown");
  217 tbl_row++;
  218 
  219 
  220 tn_frame=gtk_frame_new("Details from thumbnail");
  221 gtk_box_pack_start(GTK_BOX(vbox),tn_frame,TRUE,TRUE,0);
  222 gtk_widget_show(tn_frame);
  223 
  224 tn_tbl=gtk_table_new(1,3,TRUE);
  225 gtk_container_add(GTK_CONTAINER(tn_frame),tn_tbl);
  226 gtk_table_set_col_spacings(GTK_TABLE(tn_tbl),20);
  227 gtk_table_set_row_spacings(GTK_TABLE(tn_tbl),8);
  228 gtk_container_set_border_width(GTK_CONTAINER(tn_tbl),8);
  229 gtk_widget_show(tn_tbl);
  230 
  231 tbl_row=0;
  232 
  233 DO_TBL_LEFT(tn_tbl,tbl_row,"Dimensions:");
  234 if(got_dimensions)
  235   sprintf(buf,"%d x %d",w,h);
  236 else
  237   strcpy(buf,"unknown");
  238 DO_TBL_RIGHT(tn_tbl,tbl_row,buf);
  239 tbl_row++;
  240 
  241 if(!got_dimensions)
  242   gtk_widget_set_sensitive(tn_frame,FALSE);
  243 
  244 
  245 /* esc also acks it (even from main window!) */
  246 gtk_widget_add_accelerator(button,"clicked",mainwin_accel_group,
  247                            GDK_Escape,0,0);
  248 
  249 
  250 gtk_widget_show(details_win);
  251 
  252 return(details_win);
  253 }
  254 
  255 
  256 void cb_file_details(void)
  257 {
  258 char *ptr;
  259 int row;
  260 
  261 /* if focus row is somehow bogus, don't do it */
  262 row=GTK_CLIST(clist)->focus_row;
  263 if(row<0 || row>=numrows)
  264   return;
  265 
  266 /* make the window and fill with details */
  267 gtk_clist_get_text(GTK_CLIST(clist),row,SELECTOR_NAME_COL,&ptr);
  268 make_details_win(ptr);
  269 
  270 /* that's it then; it's modal so we can just leave GTK+ to deal with things. */
  271 }