"Fossies" - the Fresh Open Source Software Archive

Member "xzgv-0.9.2/src/rename.c" (3 Sep 2017, 5453 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 "rename.c" see the Fossies "Dox" file reference documentation.

    1 /* xzgv v0.3 - picture viewer for X, with file selector.
    2  * Copyright (C) 1999,2000 Russell Marks. See main.c for license details.
    3  *
    4  * rename.c - file rename dialog.
    5  */
    6 
    7 #include <stdio.h>
    8 #include <string.h>
    9 #include <stdlib.h>
   10 #include <unistd.h>
   11 #include <sys/types.h>
   12 #include <sys/stat.h>
   13 #include <gtk/gtk.h>
   14 #include <gdk/gdkkeysyms.h>
   15 #include "backend.h"
   16 #include "main.h"
   17 
   18 #include "rename.h"
   19 
   20 
   21 static GtkWidget *rename_win;
   22 static int current_row;
   23 static char *oldname;
   24 
   25 
   26 static void cb_ok_button(GtkWidget *button,GtkWidget *entry)
   27 {
   28 struct stat sbuf;
   29 char *tn_src,*tn_dst;
   30 char *dest=g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
   31 
   32 gtk_widget_destroy(rename_win);
   33 
   34 if(!dest || *dest==0)
   35   {
   36   free(dest);
   37   return;
   38   }
   39 
   40 /* refuse anything with path elements in */
   41 if(strchr(dest,'/'))
   42   {
   43   free(dest);
   44   error_dialog("xzgv error","File must remain in current directory");
   45   return;
   46   }
   47 
   48 /* refuse the renaming if it would blast an existing file */
   49 if(stat(dest,&sbuf)!=-1)
   50   {
   51   free(dest);
   52   error_dialog("xzgv error","File already exists");
   53   return;
   54   }
   55 
   56 if(rename(oldname,dest)==-1)
   57   {
   58   free(dest);
   59   error_dialog("xzgv error","Couldn't rename file!");
   60   return;
   61   }
   62 
   63 /* try renaming any thumbnail, and rename entry when it won't break oldname */
   64 tn_src=tn_dst=NULL;
   65 
   66 /* ".xvpics/" is 8 chars */
   67 if((tn_src=malloc(8+strlen(oldname)+1))==NULL ||
   68    (tn_dst=malloc(8+strlen(dest)+1))==NULL)
   69   {
   70   /* rename entry */
   71   gtk_clist_set_text(GTK_CLIST(clist),current_row,SELECTOR_NAME_COL,dest);
   72   if(tn_src) free(tn_src);
   73   resort_finish();
   74   return;
   75   }
   76 
   77 strcpy(tn_src,".xvpics/");
   78 strcat(tn_src,oldname);
   79 strcpy(tn_dst,".xvpics/");
   80 strcat(tn_dst,dest);
   81 
   82 rename(tn_src,tn_dst);      /* don't much care if it works or not */
   83 
   84 /* rename entry */
   85 gtk_clist_set_text(GTK_CLIST(clist),current_row,SELECTOR_NAME_COL,dest);
   86 
   87 free(tn_dst);
   88 free(tn_src);
   89 
   90 free(dest);
   91 
   92 resort_finish();
   93 }
   94 
   95 
   96 void cb_rename_file(void)
   97 {
   98 GtkWidget *vbox;
   99 GtkWidget *action_tbl,*ok_button,*cancel_button;
  100 GtkWidget *table;
  101 GtkWidget *label,*entry;
  102 static char buf[1024];
  103 int tbl_row;
  104 
  105 current_row=GTK_CLIST(clist)->focus_row;
  106 if(current_row<0 || current_row>=numrows) return;
  107 
  108 oldname=NULL;
  109 gtk_clist_get_text(GTK_CLIST(clist),current_row,SELECTOR_NAME_COL,&oldname);
  110 
  111 rename_win=gtk_dialog_new();
  112 
  113 gtk_window_set_title(GTK_WINDOW(rename_win),"Rename file");
  114 gtk_window_set_policy(GTK_WINDOW(rename_win),FALSE,TRUE,FALSE);
  115 gtk_window_set_position(GTK_WINDOW(rename_win),GTK_WIN_POS_CENTER);
  116 /* must be modal */
  117 gtk_window_set_modal(GTK_WINDOW(rename_win),TRUE);
  118 
  119 /* make a new vbox for the top part so we can get spacing more sane */
  120 vbox=gtk_vbox_new(FALSE,10);
  121 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(rename_win)->vbox),
  122                    vbox,TRUE,TRUE,0);
  123 gtk_widget_show(vbox);
  124 
  125 gtk_container_set_border_width(GTK_CONTAINER(vbox),5);
  126 gtk_container_set_border_width(
  127   GTK_CONTAINER(GTK_DIALOG(rename_win)->action_area),5);
  128 
  129 /* add ok/cancel buttons */
  130 action_tbl=gtk_table_new(1,5,TRUE);
  131 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(rename_win)->action_area),
  132                    action_tbl,TRUE,TRUE,0);
  133 gtk_widget_show(action_tbl);
  134 
  135 ok_button=gtk_button_new_with_label("Ok");
  136 gtk_table_attach_defaults(GTK_TABLE(action_tbl),ok_button, 1,2, 0,1);
  137 /* we connect the signal later, so we can refer to the text-entry */
  138 gtk_widget_show(ok_button);
  139 
  140 cancel_button=gtk_button_new_with_label("Cancel");
  141 gtk_table_attach_defaults(GTK_TABLE(action_tbl),cancel_button, 3,4, 0,1);
  142 gtk_signal_connect_object(GTK_OBJECT(cancel_button),"clicked",
  143                           GTK_SIGNAL_FUNC(gtk_widget_destroy),
  144                           GTK_OBJECT(rename_win));
  145 gtk_widget_show(cancel_button);
  146 
  147 
  148 table=gtk_table_new(2,3,TRUE);
  149 gtk_box_pack_start(GTK_BOX(vbox),table,TRUE,TRUE,0);
  150 gtk_table_set_col_spacings(GTK_TABLE(table),20);
  151 gtk_table_set_row_spacings(GTK_TABLE(table),8);
  152 gtk_container_set_border_width(GTK_CONTAINER(table),8);
  153 gtk_widget_show(table);
  154 
  155 #define DO_TBL_LEFT(table,row,name) \
  156   label=gtk_label_new(name);                \
  157   gtk_misc_set_alignment(GTK_MISC(label),1.,0.5);   \
  158   gtk_table_attach_defaults(GTK_TABLE(table),label, 0,1, (row),(row)+1); \
  159   gtk_widget_show(label)
  160 
  161 #define DO_TBL_RIGHT(table,row,name) \
  162   label=gtk_label_new(name);                \
  163   gtk_misc_set_alignment(GTK_MISC(label),0.,0.5);   \
  164   gtk_table_attach_defaults(GTK_TABLE(table),label, 1,3, (row),(row)+1); \
  165   gtk_widget_show(label)
  166 
  167 tbl_row=0;
  168 
  169 DO_TBL_LEFT(table,tbl_row,"Old filename:");
  170 DO_TBL_RIGHT(table,tbl_row,oldname);
  171 tbl_row++;
  172 
  173 DO_TBL_LEFT(table,tbl_row,"New filename:");
  174 
  175 entry=gtk_entry_new_with_max_length(sizeof(buf)-1);
  176 gtk_entry_set_text(GTK_ENTRY(entry), oldname);
  177 gtk_table_attach_defaults(GTK_TABLE(table),entry, 1,3, tbl_row,tbl_row+1);
  178 gtk_widget_grab_focus(entry);
  179 gtk_widget_show(entry);
  180 gtk_signal_connect(GTK_OBJECT(entry),"activate",
  181                    GTK_SIGNAL_FUNC(cb_ok_button),GTK_OBJECT(entry));
  182 
  183 /* finally connect up the ok button */
  184 gtk_signal_connect(GTK_OBJECT(ok_button),"clicked",
  185                    GTK_SIGNAL_FUNC(cb_ok_button),GTK_OBJECT(entry));
  186 
  187 
  188 /* esc = cancel */
  189 gtk_widget_add_accelerator(cancel_button,"clicked",
  190                            mainwin_accel_group,
  191                            GDK_Escape,0,0);
  192 
  193 gtk_widget_add_accelerator(ok_button,"clicked",
  194                            mainwin_accel_group,
  195                            GDK_Return,0,0);
  196 
  197 
  198 gtk_widget_show(rename_win);
  199 
  200 /* that's it then; it's modal so we can just leave GTK+ to deal with things. */
  201 }