- Implement rename, the same way it was done before (Fl_Input that hovers above item)

- Implement delete
- Separate all file operations (cut/copy/paste etc) into fileops.cpp/.h
This commit is contained in:
Vedran Ljubovic 2007-07-23 19:59:48 +00:00
parent 3f239d2d9b
commit e9e37d33cc
4 changed files with 673 additions and 391 deletions

View File

@ -15,6 +15,7 @@
#define EDE_FileView_H
#include <Fl/Fl_Shared_Image.H>
#include <Fl/Fl_Input.H>
#include <edelib/String.h>
#include <edelib/IconTheme.h>
@ -39,6 +40,89 @@ struct FileItem {
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/*// Event handler for EditBox
int EditBox::handle(int event) {
if (!this->visible()) return parent()->handle(event);
bool above=false;
//fprintf(stderr,"Editbox event: %d (%s)\n",event,event_name(event));
// Change filename
if (event==KEY && (event_key()==ReturnKey || event_key()==KeypadEnter)) {
// split old filename to path and file
char path[PATH_MAX], file[PATH_MAX];
strcpy(path, (char*)editing_->user_data());
if (path[strlen(path)-1] == '/') path[strlen(path)-1]='\0';
char *p = strrchr(path,'/');
if (p==0 || *p=='\0') {
strcpy(file,path);
path[0]='\0';
} else { // usual case
p++;
strcpy(file,p);
*p='\0';
}
if (strlen(file)!=strlen(text()) || strcmp(file,text())!=0) {
// Create new filename
strncat(path, text(), PATH_MAX-strlen(path));
char oldname[PATH_MAX];
strcpy(oldname, (char*)editing_->user_data());
if (rename(oldname, path) == -1) {
alert(tsprintf(_("Could not rename file! Error was:\n\t%s"), strerror(errno)));
} else {
// Update browser
free(editing_->user_data());
editing_->user_data(strdup(path));
const char* l = editing_->label();
editing_->label(tasprintf("%s%s",text(),strchr(l, '\t')));
}
}
above=true;
}
// Hide editbox
if (above || ( event==KEY && event_key()==EscapeKey ) ) {
this->hide();
return 1;
}
Input::handle(event);
}
// We override hide method to ensure certain things done
void EditBox::hide() {
Input::hide();
// Remove box so it doesn't get in the way
this->x(0);
this->y(0);
this->w(0);
this->h(0);
// Return the browser item into "visible" state
if (editing_) {
editing_->textcolor(textcolor());
editing_->redraw();
parent()->take_focus();
}
}
*/
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
// Type for rename_callback
// I don't know how to do this without creating a new type :(
typedef void (my_callback)(const char*);
class FileDetailsView : public EDE_Browser {
private:
// EDE_Browser* browser; - yada
@ -56,14 +140,97 @@ private:
}
return 0;
}
/* struct myfileitem* finditem(edelib::String realpath) {
if (!firstitem) return;
struct myfileitem* work = firstitem;
do {
if (work->item->realpath == realpath) return work;
} while (work=work->next != 0);
return 0;
}*/
// Subclass Fl_Input so we can handle keyboard
class EditBox : public Fl_Input {
friend class FileDetailsView;
public:
EditBox(int x, int y, int w, int h, const char* label = 0) : Fl_Input(x,y,w,h,label) {}
int handle (int e) {
FileDetailsView* view = (FileDetailsView*)parent();
if (e==FL_KEYBOARD && visible()) {
int k = Fl::event_key();
if (Fl::event_key()==FL_Enter && visible()) {
view->hide_editbox();
view->do_rename();
} else if (k==FL_Up || k==FL_Down || k==FL_Page_Up || k==FL_Page_Down) {
view->hide_editbox();
return 0; // let the view scroll
} else if (k==FL_F+2 || k==FL_Escape)
view->hide_editbox();
else
Fl_Input::handle(e);
return 1; // don't send keys to view
}
return Fl_Input::handle(e);
}
}* editbox_;
int editbox_row;
my_callback* rename_callback_;
// show editbox at specified row and make the row "invisible" (bgcolor same as fgcolor)
void show_editbox(int row) {
if (row<1 || row>size()) return; // nothing selected
if (strcmp(text(row), "..")==0) return; // can't rename "go up" button
// unselect the row with focus - it's prettier that way
select(row,0);
// Copy filename to editbox
char* filename = strdup(text(row));
char* tmp = strchr(filename, column_char());
if (tmp) *tmp='\0';
editbox_->value(filename);
// make the row "invisible"
editbox_row=row;
char* ntext = (char*)malloc(sizeof(char)*strlen(text(row))+5); // add 4 places for format chars
strncpy(ntext+5, text(row), strlen(text(row)));
ntext[0]='@'; ntext[1]='C'; ntext[2]='2'; ntext[3]='5'; ntext[4]='5';
text(row,ntext);
free(ntext);
// calculate location for editbox
// "Magic constants" here are just nice spacing
int X=x()+get_icon(row)->w()+8;
int W=150; // reasonable
int H=get_icon(row)->h()+2;
int Y=y()+2 - position();
void* item = item_first();
for (int i=1; i<row; i++) {
Y+=item_height(item);
item=item_next(item);
}
editbox_->resize(X,Y,W,H);
editbox_->show();
editbox_->take_focus();
editbox_->redraw();
}
void hide_editbox() {
fprintf(stderr, "hide_editbox()\n");
editbox_->hide();
// Make the edited row visible again
char* ntext = (char*)malloc(sizeof(char)*strlen(text(editbox_row))-5); // add 4 places for format chars
strncpy(ntext, text(editbox_row)+5, strlen(text(editbox_row))-5);
text(editbox_row,ntext);
free(ntext);
}
// Do the rename
void do_rename() {
fprintf(stderr, "editbox_cb()\n");
rename_callback_(editbox_->value());
//hide_editbox();
}
public:
FileDetailsView(int X, int Y, int W, int H, char*label=0) : EDE_Browser(X,Y,W,H,label) {
// browser = new EDE_Browser(X,Y,W,H,label);
@ -82,6 +249,12 @@ public:
when(FL_WHEN_ENTER_KEY_ALWAYS);
//when(FL_WHEN_ENTER_KEY_CHANGED);
//first=0;
editbox_ = new EditBox(0, 0, 0, 0);
editbox_->box(FL_BORDER_BOX);
editbox_->parent(this);
editbox_->textsize(12); // FIXME: hack for font size
editbox_->hide();
}
// ~FileDetailsView() { delete browser; }
@ -100,79 +273,69 @@ fprintf (stderr, "value: %s\n", value.c_str());
void add(FileItem *item) { insert(size()+1, item); }
// browser->redraw();
/* struct myfileitem* i;
if (!first)
i = first = new myfileitem;
else {
i=first;
while (i->next!=0) i=i->next;
i->next = new myfileitem;
i->next->previous = i;
i=i->next;
}
i->item = item;
i->next = 0;*/
// }
void remove(FileItem *item) {
int row = findrow(item->realpath);
if (row) EDE_Browser::remove(row);
/* struct myfileitem* i = finditem(item->realpath);
if (i) {
i->previous->next = i->next;
i->next->previous = i->previous;
delete i;
}*/
}
void update(FileItem *item) {
int row=findrow(item->realpath);
if (row==0) return;
EDE_Browser::remove(row);
insert(row, item);
/*
//
edelib::String value = item->name + "\t" + item->description + "\t" + item->size + "\t" + item->date + "\t" + item->permissions;
browser->insert(row,value.c_str(),strdup(item->realpath.c_str()));
edelib::String icon = edelib::IconTheme::get(item->icon.c_str(),edelib::ICON_SIZE_TINY);
browser->set_icon(row, Fl_Shared_Image::get(icon.c_str()));
browser->redraw(); // OPTIMIZE
// browser->redraw_line(browser->find_item(row));*/
// FIXME: this will lose focus, making it impossible to click on something while
// directory is loading
}
// Change color of row to gray
void gray(int row) {
char *ntext = strdup(text(row));
if (ntext[0] == '@' && ntext[1] == 'C') { free(ntext); return; } //already greyed
ntext = (char*)realloc(ntext, strlen(ntext)+4);
for(int i=strlen(ntext); i>=0; i--) ntext[i+4]=ntext[i];
if (text(row)[0] == '@' && text(row)[1] == 'C') return; // already greyed
char *ntext = (char*)malloc(sizeof(char)*strlen(text(row))+4); // add 4 places for format chars
strncpy(ntext+4, text(row), strlen(text(row)));
ntext[0]='@'; ntext[1]='C'; ntext[2]='2'; ntext[3]='5';
text(row,ntext);
fprintf (stderr, "row(%d): '%s'\n",row,ntext);
// free(ntext);
free(ntext);
// grey icon - it will work automagically since we work with a pointer
// grey icon - but how to ungray?
Fl_Image* im = get_icon(row)->copy();
im->inactive();
set_icon(row,im);
//redraw(); // OPTIMIZE
}
void ungray(int row) {
char *ntext = strdup(text(row));
if (ntext[0] != '@' || ntext[1] != 'C') { free(ntext); return; } //not greyed
snprintf(ntext, strlen(ntext), "%s", ntext+4);
if (text(row)[0] != '@' || text(row)[1] != 'C') return; // not greyed
char *ntext = (char*)malloc(sizeof(char)*strlen(text(row))-4); // 4 places for format chars
strncpy(ntext, text(row)+4, strlen(text(row))-4);
text(row,ntext);
free(ntext);
// grey icon - it will work automagically since we work with a pointer
Fl_Image* im = get_icon(row);
im->uncache(); // doesn't work
// don't work
//Fl_Image* im = get_icon(row);
//im->uncache(); // doesn't work
//redraw(); // OPTIMIZE
}
// Renaming support
int handle(int e) {
if (e==FL_KEYBOARD) {
if (Fl::event_key()==FL_F+2) {
if (editbox_->visible())
hide_editbox();
else
show_editbox(get_focus());
}
}
if (e==FL_PUSH && editbox_->visible() && !Fl::event_inside(editbox_))
hide_editbox(); // hide editbox when click outside it
return Fl_Icon_Browser::handle(e);
}
// Setup callback that will be used when renaming
void rename_callback(my_callback* cb) { rename_callback_ = cb; }
};

View File

@ -15,7 +15,6 @@
#include <dirent.h>
#include <sys/stat.h>
#include <sys/time.h> // timer
#include <unistd.h> // edelib/Run needs it :(
#include <sys/resource.h> // for core
#include <sys/vfs.h> // used for statfs()
@ -24,8 +23,6 @@
#include <Fl/Fl_Double_Window.H>
#include <Fl/Fl_Menu_Bar.H>
#include <Fl/Fl_File_Chooser.H> // for fl_dir_chooser, used in "Open location"
#include <Fl/Fl_Progress.H>
#include <Fl/Fl_Button.H>
#include <Fl/filename.H>
#include <Fl/fl_ask.H>
@ -34,10 +31,11 @@
#include <edelib/String.h>
#include <edelib/StrUtil.h>
#include <edelib/Run.h>
#include <edelib/File.h>
#include "EDE_FileView.h"
#include "Util.h"
#include "EDE_FileView.h" // our file view widget
#include "Util.h" // ex-edelib
#include "fileops.h" // file operations
#define DEFAULT_ICON "misc-vedran"
@ -279,7 +277,7 @@ fprintf (stderr, "loaddir(%s) = (%s)\n",path,current_dir);
char fullpath[FL_PATH_MAX];
snprintf (fullpath,FL_PATH_MAX-1,"%s%s",current_dir,n);
if (stat(fullpath,&stat_buffer)) continue; // error
if (stat(fullpath,&stat_buffer)) continue; // happens when user has traverse but not read privilege
FileItem *item = new FileItem;
item->name = n;
@ -306,17 +304,20 @@ fprintf (stderr, "loaddir(%s) = (%s)\n",path,current_dir);
// Populate view
for (int i=0; i<fsize; i++)
if (item_list[i]->description == "Go up")
view->add(item_list[i]);
if (dirsfirst) {
for (int i=0; i<fsize; i++) {
if (item_list[i]->description == "Directory" || item_list[i]->description == "Go up")
for (int i=0; i<fsize; i++)
if (item_list[i]->description == "Directory")
view->add(item_list[i]);
}
for (int i=0; i<fsize; i++)
if (item_list[i]->description != "Directory" && item_list[i]->description != "Go up")
view->add(item_list[i]);
} else {
for (int i=0; i<fsize; i++)
view->add(item_list[i]);
if (item_list[i]->description != "Go up")
view->add(item_list[i]);
}
view->redraw();
Fl::check();
@ -328,7 +329,7 @@ fprintf (stderr, "loaddir(%s) = (%s)\n",path,current_dir);
edelib::String desc,icon;
desc = mime_resolver.comment();
// First letter of desc should be upper case:
if (desc[0]>='a' && desc[0]<='z') desc[0] = desc[0]-'a'+'A';
if (desc.length()>0 && desc[0]>='a' && desc[0]<='z') desc[0] = desc[0]-'a'+'A';
icon = mime_resolver.icon_name();
if (desc!="" || icon!="") {
if (desc != "") item_list[i]->description = desc;
@ -345,6 +346,7 @@ fprintf (stderr, "ICON: %s !!!!!\n", icon.c_str());
delete[] item_list;
semaphore=false;
// Get partition size and free space
// TODO Attempt to cache the results in a meaningful way
static struct statfs statfs_buffer;
if (statfs(current_dir, &statfs_buffer)==0) {
@ -365,330 +367,6 @@ fprintf (stderr, "ICON: %s !!!!!\n", icon.c_str());
File moving and copying operations
-------------------------------------------------------------------*/
Fl_Progress* cut_copy_progress;
bool stop_now;
bool overwrite_all, skip_all;
char **cut_copy_buffer = 0;
bool operation_is_copy = false;
// Execute cut or copy operation when List View is active
void do_cut_copy(bool m_copy) {
// Count selected icons, for malloc
int num = view->size();
int nselected = 0;
for (int i=1; i<=num; i++)
if (view->selected(i)) {
nselected++;
// Can not cut/copy the up directory button (..)
const char* tmp = view->text(i);
if (tmp[0]=='.' && tmp[1]=='.' && (tmp[2]=='\0' || tmp[2]==view->column_char())) return;
}
// Clear cut/copy buffer and optionally ungray the previously cutted icons
if (cut_copy_buffer) {
for (int i=0; cut_copy_buffer[i]; i++)
free(cut_copy_buffer[i]);
free(cut_copy_buffer);
if (!operation_is_copy) {
for (int i=1; i<=num; i++)
view->ungray(i);
}
}
// Allocate buffer
cut_copy_buffer = (char**)malloc(sizeof(char*) * (nselected+2));
// Add selected files to buffer and optionally grey icons (for cut effect)
int buf=0;
for (int i=1; i<=num; i++) {
if (view->selected(i)==1) {
cut_copy_buffer[buf] = strdup((char*)view->data(i));
if (!m_copy) view->gray(i);
buf++;
}
}
if (buf==0) { //nothing selected, use the focused item
int i=view->get_focus();
cut_copy_buffer[buf] = strdup((char*)view->data(i));
if (!m_copy) view->gray(i);
buf++;
nselected=1;
}
cut_copy_buffer[buf] = 0;
operation_is_copy = m_copy;
// Deselect all
int focused = view->get_focus();
for (int i=1; i<=num; i++)
if (view->selected(i)) view->select(i,0);
view->set_focus(focused);
// Update statusbar
if (m_copy)
statusbar->label(tasprintf(_("Selected %d items for copying"), nselected));
else
statusbar->label(tasprintf(_("Selected %d items for moving"), nselected));
}
// Helper functions for paste:
// Copy single file. Returns true if operation should continue
bool my_copy(const char* src, const char* dest) {
FILE *fold, *fnew;
int c;
if (strcmp(src,dest)==0)
// this shouldn't happen
return true;
if (edelib::file_exists(dest)) {
// if both src and dest are directories, do nothing
if (fl_filename_isdir(src) && fl_filename_isdir(dest))
return true;
int c = -1;
if (!overwrite_all && !skip_all) {
// here was choice_alert
c = fl_choice(tsprintf(_("File already exists: %s. What to do?"), dest), _("&Overwrite"), _("Over&write all"), _("*&Skip"), _("Skip &all"), 0); // asterisk (*) means default
}
if (c==1) overwrite_all=true;
if (c==3) skip_all=true;
if (c==2 || skip_all) {
return true;
}
// At this point either c==0 (overwrite) or overwrite_all == true
// copy directory over file
if (fl_filename_isdir(src))
unlink(dest);
// copy file over directory
// TODO: we will just skip this case, but ideally there should be
// another warning
if (fl_filename_isdir(dest))
return true;
}
if (fl_filename_isdir(src)) {
if (mkdir (dest, umask(0))==0)
return true; // success
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot create directory %s"),dest), _("*&Continue"), _("&Stop"), 0);
if (q == 0) return true; else return false;
}
if ( ( fold = fopen( src, "rb" ) ) == NULL ) {
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot read file %s"),src), _("*&Continue"), _("&Stop"), 0);
if (q == 0) return true; else return false;
}
if ( ( fnew = fopen( dest, "wb" ) ) == NULL )
{
fclose ( fold );
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot create file %s"),dest), _("*&Continue"), _("&Stop"), 0);
if (q == 0) return true; else return false;
}
while (!feof(fold)) {
c = fgetc(fold);
fputc(c, fnew);
}
// TODO: Add more error handling using ferror()
fclose(fold);
fclose(fnew);
return true;
}
// Recursive function that creates a list of all files to be
// copied, expanding directories etc. The total number of elements
// will be stored in listsize. Returns false if user decided to
// interrupt copying.
bool create_list(const char* src, char** &list, int &list_size, int &list_capacity) {
// Grow list if neccessary
if (list_size >= list_capacity-1) {
list_capacity += 1000;
list = (char**)realloc(list, sizeof(char**)*list_capacity);
}
// We add both files and diretories to list
list[list_size++] = strdup(src);
if (fl_filename_isdir(src)) {
char new_src[PATH_MAX];
dirent **files;
// FIXME: use same sort as currently used
// FIXME: detect errors on accessing folder
int num_files = fl_filename_list(src, &files, fl_casenumericsort);
for (int i=0; i<num_files; i++) {
if (strcmp(files[i]->d_name,"./")==0 || strcmp(files[i]->d_name,"../")==0) continue;
snprintf(new_src, PATH_MAX, "%s%s", src, files[i]->d_name);
Fl::check(); // update gui
if (stop_now || !create_list(new_src, list, list_size, list_capacity))
return false;
}
}
return true;
}
// Callback for Stop button on progress window
void stop_copying_cb(Fl_Widget*,void* v) {
stop_now=true;
// Let's inform user that we're stopping...
Fl_Box* caption = (Fl_Box*)v;
caption->label(_("Stopping..."));
caption->redraw();
caption->parent()->redraw();
}
// Execute paste operation - this will copy or move files based on last
// operation
void do_paste() {
if (!cut_copy_buffer || !cut_copy_buffer[0]) return;
overwrite_all=false; skip_all=false;
// Moving files on same filesystem is trivial, just like rename
// We don't even need a progress bar
if (!operation_is_copy && is_on_same_fs(current_dir, cut_copy_buffer[0])) {
for (int i=0; cut_copy_buffer[i]; i++) {
char *newname;
asprintf(&newname, "%s%s", current_dir, fl_filename_name(cut_copy_buffer[i]));
if (edelib::file_exists(newname)) {
int c = -1;
if (!overwrite_all && !skip_all) {
// here was choice_alert
c = fl_choice(tsprintf(_("File already exists: %s. What to do?"), newname), _("&Overwrite"), _("Over&write all"), _("*&Skip"), _("Skip &all")); // * means default
}
if (c==1) overwrite_all=true;
if (c==3) skip_all=true;
if (c==2 || skip_all) {
free(cut_copy_buffer[i]);
continue; // go to next entry
}
// At this point c==0 (Overwrite) or overwrite_all == true
unlink(newname);
}
rename(cut_copy_buffer[i],newname);
free(cut_copy_buffer[i]);
}
free(cut_copy_buffer);
cut_copy_buffer=0;
loaddir(current_dir); // Update display
return;
//
// Real file moving / copying using recursive algorithm
//
} else {
stop_now = false;
// Create srcdir string
char *srcdir = strdup(cut_copy_buffer[0]);
char *p = strrchr(srcdir,'/');
if (*(p+1) == '\0') { // slash is last - find one before
*p = '\0';
p = strrchr(srcdir,'/');
}
*(p+1) = '\0';
if (strcmp(srcdir,current_dir)==0) {
fl_alert(_("You cannot copy a file onto itself!"));
return;
}
// Draw progress dialog
// NOTE: sometimes when copying/moving just one file, the window
// can get "stuck"
// See: fltk STR 1255, http://www.fltk.org/str.php?L1255
Fl_Window* progress_window = new Fl_Window(350,150);
if (operation_is_copy)
progress_window->label(_("Copying files"));
else
progress_window->label(_("Moving files"));
progress_window->set_modal();
progress_window->begin();
Fl_Box* caption = new Fl_Box(20,20,310,25, _("Counting files in directories"));
caption->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
cut_copy_progress = new Fl_Progress(20,60,310,20);
Fl_Button* stop_button = new Fl_Button(145,100,60,40, _("&Stop"));
stop_button->callback(stop_copying_cb,caption);
progress_window->end();
progress_window->show();
// How many items do we copy?
int copy_items=0;
for (; cut_copy_buffer[copy_items]; copy_items++);
// Set ProgressBar range accordingly
cut_copy_progress->minimum(0);
cut_copy_progress->maximum(copy_items);
cut_copy_progress->value(0);
// Count files in directories
int list_size = 0, list_capacity = 1000;
char** files_list = (char**)malloc(sizeof(char**)*list_capacity);
for (int i=0; i<copy_items; i++) {
// We must ensure that cut_copy_buffer is deallocated
// even if user clicked on Stop
if (!stop_now) create_list(cut_copy_buffer[i], files_list, list_size, list_capacity);
free(cut_copy_buffer[i]);
cut_copy_progress->value(i+1);
Fl::check(); // check to see if user pressed Stop
}
free (cut_copy_buffer);
cut_copy_buffer=0;
// Now copying those files
if (!stop_now) {
char label[150];
char dest[PATH_MAX];
cut_copy_progress->minimum(0);
cut_copy_progress->maximum(list_size);
cut_copy_progress->value(0);
for (int i=0; i<list_size; i++) {
// Prepare dest filename
char *srcfile = files_list[i] + strlen(srcdir);
snprintf (dest, PATH_MAX, "%s%s", current_dir, srcfile);
snprintf(label, 150, _("Copying %d of %d files to %s"), i, list_size, current_dir);
caption->label(label);
caption->redraw();
if (stop_now || !my_copy(files_list[i], dest))
break;
// Delete file after moving
if (!operation_is_copy) unlink(files_list[i]);
cut_copy_progress->value(cut_copy_progress->value()+1);
Fl::check(); // check to see if user pressed Stop
}
}
progress_window->hide();
// Deallocate files_list[][]
for (int i=0; i<list_size; i++)
free(files_list[i]);
free(files_list);
// Reload current dir
loaddir(current_dir);
}
}
/*-----------------------------------------------------------------
Main menu callbacks
@ -706,6 +384,7 @@ fprintf (stderr,"enter\n");
gettimeofday(&newtm,0);
if (newtm.tv_sec - tm.tv_sec < 1 || (newtm.tv_sec-tm.tv_sec==1 && newtm.tv_usec<tm.tv_usec)) return; // no calling within 1 second
tm=newtm;
if (view->value()==0) return; // This can happen while efiler is loading
char* filename = strdup(view->text(view->value()));
if (char*k = strchr(filename, view->column_char())) *k='\0';
@ -750,11 +429,14 @@ void location_cb(Fl_Widget*, void*) {
if (dir) loaddir(dir);
}
void new_cb(Fl_Widget*, void*) { edelib::run_program(tsprintf("efiler %s",current_dir),false); }
void quit_cb(Fl_Widget*, void*) {exit(0);}
void cut_cb(Fl_Widget*, void*) { do_cut_copy(false); }
void copy_cb(Fl_Widget*, void*) { do_cut_copy(true); }
void paste_cb(Fl_Widget*, void*) { do_paste(); }
void delete_cb(Fl_Widget*, void*) { do_delete(); }
void showhidden_cb(Fl_Widget*, void*) { showhidden=!showhidden; loaddir(current_dir); }
@ -770,7 +452,6 @@ void dirsfirst_cb(Fl_Widget*, void*) { dirsfirst=!dirsfirst; loaddir(current_dir
// dummy callbacks
void ow_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); } // make a list of openers
void delete_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); } // popup a warning
void pref_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
void iconsview_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
void listview_cb(Fl_Widget*, void*) { fprintf(stderr, "callback\n"); }
@ -791,6 +472,7 @@ Fl_Menu_Item main_menu_definition[] = {
{_("&Open"), FL_CTRL+'o', open_cb},
{_("Open &with..."), 0, ow_cb, 0,FL_MENU_DIVIDER},
{_("Open &location"), 0, location_cb, 0,FL_MENU_DIVIDER},
{_("&New window"), FL_CTRL+'n', new_cb, 0,FL_MENU_DIVIDER},
{_("&Quit"), FL_CTRL+'q', quit_cb},
{0},
@ -805,13 +487,13 @@ Fl_Menu_Item main_menu_definition[] = {
{_("&View"), 0, 0, 0, FL_SUBMENU},
{_("&Icons"), FL_F+8, iconsview_cb, 0, FL_MENU_RADIO}, // coming soon
{_("&Detailed list"), FL_F+8, listview_cb, 0, FL_MENU_RADIO|FL_MENU_DIVIDER},
{_("&Detailed list"), FL_F+8, listview_cb, 0, FL_MENU_RADIO|FL_MENU_VALUE|FL_MENU_DIVIDER},
{_("&Show hidden"), 0, showhidden_cb, 0, FL_MENU_TOGGLE},
{_("Directory &tree"), FL_F+9, showtree_cb, 0, FL_MENU_TOGGLE|FL_MENU_DIVIDER}, // coming soon
{_("&Refresh"), FL_F+5, refresh_cb},
{_("S&ort"), 0, 0, 0, FL_SUBMENU},
{_("&Case sensitive"), 0, case_cb, 0, FL_MENU_TOGGLE},
{_("D&irectories first"), 0, dirsfirst_cb, 0, FL_MENU_TOGGLE},
{_("D&irectories first"), 0, dirsfirst_cb, 0, FL_MENU_TOGGLE|FL_MENU_VALUE},
{0},
{0},
@ -841,6 +523,8 @@ edelib::IconTheme::init("crystalsvg");
view = new FileDetailsView(0, menubar_height, default_window_width, default_window_height-menubar_height-statusbar_height, 0);
view->callback(open_cb);
// callback for renaming
view->rename_callback(do_rename);
Fl_Group *sbgroup = new Fl_Group(0, default_window_height-statusbar_height, default_window_width, statusbar_height);
statusbar = new Fl_Box(2, default_window_height-statusbar_height+2, statusbar_width, statusbar_height-4);

407
efiler/fileops.cpp Normal file
View File

@ -0,0 +1,407 @@
// This file implements copy / move / delete operation with files
// NOT TO BE CONFUSED WITH edelib::File.h !!!
// Functions here usually call stuff from File.h, but also update
// efiler interface, display warnings to user etc.
#include "fileops.h"
#include <sys/stat.h>
#include <Fl/Fl_Window.H>
#include <Fl/Fl_Progress.H>
#include <Fl/Fl_Button.H>
#include <Fl/filename.H>
#include <Fl/fl_ask.H>
#include <edelib/File.h>
#include <edelib/Nls.h>
#include "EDE_FileView.h"
#include "Util.h"
Fl_Progress* cut_copy_progress;
bool stop_now;
bool overwrite_all, skip_all;
char **cut_copy_buffer = 0;
bool operation_is_copy = false;
// Execute cut or copy operation when List View is active
void do_cut_copy(bool m_copy) {
// Count selected icons, for malloc
int num = view->size();
int nselected = 0;
for (int i=1; i<=num; i++)
if (view->selected(i)) {
nselected++;
// Can not cut/copy the up directory button (..)
const char* tmp = view->text(i);
if (tmp[0]=='.' && tmp[1]=='.' && (tmp[2]=='\0' || tmp[2]==view->column_char())) return;
}
// Clear cut/copy buffer and optionally ungray the previously cutted icons
if (cut_copy_buffer) {
for (int i=0; cut_copy_buffer[i]; i++)
free(cut_copy_buffer[i]);
free(cut_copy_buffer);
if (!operation_is_copy) {
for (int i=1; i<=num; i++)
view->ungray(i);
}
}
// Allocate buffer
cut_copy_buffer = (char**)malloc(sizeof(char*) * (nselected+2));
// Add selected files to buffer and optionally grey icons (for cut effect)
int buf=0;
for (int i=1; i<=num; i++) {
if (view->selected(i)==1) {
cut_copy_buffer[buf] = strdup((char*)view->data(i));
if (!m_copy) view->gray(i);
buf++;
}
}
if (buf==0) { //nothing selected, use the focused item
int i=view->get_focus();
cut_copy_buffer[buf] = strdup((char*)view->data(i));
if (!m_copy) view->gray(i);
buf++;
nselected=1;
}
cut_copy_buffer[buf] = 0;
operation_is_copy = m_copy;
// Deselect all
int focused = view->get_focus();
for (int i=1; i<=num; i++)
if (view->selected(i)) view->select(i,0);
view->set_focus(focused);
// Update statusbar
if (m_copy)
statusbar->label(tasprintf(_("Selected %d items for copying"), nselected));
else
statusbar->label(tasprintf(_("Selected %d items for moving"), nselected));
}
// Helper functions for paste:
// Copy single file. Returns true if operation should continue
// Note that at this point directories should be expanded into subdirectories etc.
// so when "copying" a directory we actually mean creating a new directory with same info
bool my_copy(const char* src, const char* dest) {
FILE *fold, *fnew;
int c;
if (strcmp(src,dest)==0)
// this shouldn't happen
return true;
if (edelib::file_exists(dest)) {
// if both src and dest are directories, do nothing
if (fl_filename_isdir(src) && fl_filename_isdir(dest))
return true;
int c = -1;
if (!overwrite_all && !skip_all) {
// here was choice_alert
c = fl_choice(tsprintf(_("File already exists: %s. What to do?"), dest), _("&Overwrite"), _("Over&write all"), _("&Skip"), _("Skip &all"), 0); // asterisk (*) means default
}
if (c==1) overwrite_all=true;
if (c==3) skip_all=true;
if (c==2 || skip_all) {
return true;
}
// At this point either c==0 (overwrite) or overwrite_all == true
// copy directory over file
if (fl_filename_isdir(src))
unlink(dest);
// copy file over directory
// TODO: we will just skip this case, but ideally there should be
// another warning
if (fl_filename_isdir(dest))
return true;
}
if (fl_filename_isdir(src)) {
if (mkdir (dest, umask(0))==0)
return true; // success
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot create directory %s"),dest), _("&Stop"), _("&Continue"), 0);
if (q == 0) return true; else return false;
}
if ( !edelib::file_readable(src) ) {
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot read file %s"),src), _("&Stop"), _("&Continue"), 0);
if (q == 0) return true; else return false;
}
if ( !edelib::file_writeable(dest) )
{
// here was choice_alert
int q = fl_choice(tsprintf(_("Cannot create file %s"),dest), _("&Stop"), _("&Continue"), 0);
if (q == 0) return true; else return false;
}
// we will try to preserve permissions etc. cause that's usually what people want
if (!edelib::file_copy(src,dest,true))
fl_alert(tsprintf(_("Error copying %s to %s"),src,dest));
fclose(fold);
fclose(fnew);
return true;
}
// Recursive function that creates a list of all files to be copied, such that
// directories are opened and all files and subdirectories etc. are added
// separately to the list. The total number of elements will be stored in
// listsize. Returns false if user decided to interrupt copying.
bool expand_dirs(const char* src, char** &list, int &list_size, int &list_capacity) {
// Grow list if neccessary
if (list_size >= list_capacity-1) {
list_capacity += 1000;
list = (char**)realloc(list, sizeof(char**)*list_capacity);
}
// We add both files and directories to list
list[list_size++] = strdup(src);
if (fl_filename_isdir(src)) {
char new_src[PATH_MAX];
dirent **files;
// FIXME: use same sort as used in view
// FIXME: detect errors on accessing folder
int num_files = fl_filename_list(src, &files, fl_casenumericsort);
for (int i=0; i<num_files; i++) {
if (strcmp(files[i]->d_name,"./")==0 || strcmp(files[i]->d_name,"../")==0) continue;
snprintf(new_src, PATH_MAX, "%s%s", src, files[i]->d_name);
Fl::check(); // update gui
if (stop_now || !expand_dirs(new_src, list, list_size, list_capacity))
return false;
}
}
return true;
}
// Callback for Stop button on progress window
void stop_copying_cb(Fl_Widget*,void* v) {
stop_now=true;
// Let's inform user that we're stopping...
Fl_Box* caption = (Fl_Box*)v;
caption->label(_("Stopping..."));
caption->redraw();
caption->parent()->redraw();
}
// Execute paste operation - this will copy or move files based on chosen
// operation
void do_paste() {
if (!cut_copy_buffer || !cut_copy_buffer[0]) return;
overwrite_all=false; skip_all=false;
// Moving files on same filesystem is trivial, just like rename
// We don't even need a progress bar
if (!operation_is_copy && is_on_same_fs(current_dir, cut_copy_buffer[0])) {
for (int i=0; cut_copy_buffer[i]; i++) {
char *newname;
asprintf(&newname, "%s%s", current_dir, fl_filename_name(cut_copy_buffer[i]));
if (edelib::file_exists(newname)) {
int c = -1;
if (!overwrite_all && !skip_all) {
// here was choice_alert
c = fl_choice(tsprintf(_("File already exists: %s. What to do?"), newname), _("&Overwrite"), _("Over&write all"), _("&Skip"), _("Skip &all")); // * means default
}
if (c==1) overwrite_all=true;
if (c==3) skip_all=true;
if (c==2 || skip_all) {
free(cut_copy_buffer[i]);
continue; // go to next entry
}
// At this point c==0 (Overwrite) or overwrite_all == true
unlink(newname);
}
rename(cut_copy_buffer[i],newname);
free(cut_copy_buffer[i]);
}
free(cut_copy_buffer);
cut_copy_buffer=0;
loaddir(current_dir); // Update display
return;
//
// Real file moving / copying using recursive algorithm
//
} else {
stop_now = false;
// Create srcdir string
char *srcdir = strdup(cut_copy_buffer[0]);
char *p = strrchr(srcdir,'/');
if (*(p+1) == '\0') { // slash is last - find one before
*p = '\0';
p = strrchr(srcdir,'/');
}
*(p+1) = '\0';
if (strcmp(srcdir,current_dir)==0) {
fl_alert(_("You cannot copy a file onto itself!"));
return;
}
// Draw progress dialog
Fl_Window* progress_window = new Fl_Window(350,150);
if (operation_is_copy)
progress_window->label(_("Copying files"));
else
progress_window->label(_("Moving files"));
progress_window->set_modal();
progress_window->begin();
Fl_Box* caption = new Fl_Box(20,20,310,25, _("Counting files in directories"));
caption->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
cut_copy_progress = new Fl_Progress(20,60,310,20);
Fl_Button* stop_button = new Fl_Button(145,100,60,40, _("&Stop"));
stop_button->callback(stop_copying_cb,caption);
progress_window->end();
progress_window->show();
// How many items do we copy?
int copy_items=0;
for (; cut_copy_buffer[copy_items]; copy_items++);
// Set ProgressBar range accordingly
cut_copy_progress->minimum(0);
cut_copy_progress->maximum(copy_items);
cut_copy_progress->value(0);
// Count files in directories
int list_size = 0, list_capacity = 1000;
char** files_list = (char**)malloc(sizeof(char**)*list_capacity);
for (int i=0; i<copy_items; i++) {
// We must ensure that cut_copy_buffer is deallocated
// even if user clicked on Stop
if (!stop_now) expand_dirs(cut_copy_buffer[i], files_list, list_size, list_capacity);
free(cut_copy_buffer[i]);
cut_copy_progress->value(i+1);
Fl::check(); // check to see if user pressed Stop
}
free (cut_copy_buffer);
cut_copy_buffer=0;
// Now copying those files
if (!stop_now) {
char label[150];
char dest[PATH_MAX];
cut_copy_progress->minimum(0);
cut_copy_progress->maximum(list_size);
cut_copy_progress->value(0);
for (int i=0; i<list_size; i++) {
// Prepare dest filename
char *srcfile = files_list[i] + strlen(srcdir);
snprintf (dest, PATH_MAX, "%s%s", current_dir, srcfile);
if (operation_is_copy)
snprintf(label, 150, _("Copying %d of %d files to %s"), i, list_size, current_dir);
else
snprintf(label, 150, _("Moving %d of %d files to %s"), i, list_size, current_dir);
caption->label(label);
caption->redraw();
if (stop_now || !my_copy(files_list[i], dest))
break;
// Delete file after moving
if (!operation_is_copy) unlink(files_list[i]);
cut_copy_progress->value(cut_copy_progress->value()+1);
Fl::check(); // check to see if user pressed Stop
}
}
progress_window->hide();
// Deallocate files_list[][]
for (int i=0; i<list_size; i++)
free(files_list[i]);
free(files_list);
// Reload current dir
loaddir(current_dir);
}
}
// Delete currently selected file(s) or directory(es)
void do_delete() {
// Count all selected item, expanding directories as neccessary
int list_size = 0, list_capacity = 1000;
char** files_list = (char**)malloc(sizeof(char**)*list_capacity);
for (int i=1; i<=view->size(); i++) {
if (view->selected(i)==1) {
expand_dirs((char*)view->data(i), files_list, list_size, list_capacity);
}
}
if (list_size==0) { //nothing selected, use the focused item
int i=view->get_focus();
expand_dirs((char*)view->data(i), files_list, list_size, list_capacity);
}
// Issue a warning
// here was choice_alert
int c;
if (list_size==1) {
char* k=strrchr(files_list[0],'/');
c = fl_choice(tsprintf(_("Are you sure that you want to delete file %s ?"), k+1), _("Do&n't delete"), _("&Delete"), 0);
} else
c = fl_choice(tsprintf(_("Are you sure that you want to delete %d files?"), list_size), _("Do&n't delete"), _("&Delete"), 0);
if (c!=1) return;
// delete
for (int i=0; i<list_size; i++)
edelib::file_remove(files_list[i]);
loaddir(current_dir);
}
// Rename the file with focus to given name
void do_rename(const char* c) {
edelib::String oldname(current_dir);
edelib::String newname(current_dir);
int focus = view->get_focus();
edelib::String vline(view->text(focus)); // get filename
oldname += vline.substr(0,vline.find(view->column_char(),0));
newname += c;
if (edelib::file_exists(newname.c_str()))
fl_alert(tsprintf(_("Filename already in use: %s"), newname.c_str()));
// For some reason edelib::file_rename() always fails
// else if (!edelib::file_rename(oldname.c_str(),newname.c_str()))
// fl_alert(tsprintf(_("Rename %s to %s failed!"), oldname.c_str(), newname.c_str()));
else {
rename(oldname.c_str(),newname.c_str());
// Insert new name into vline
int i=0,j=0;
while (i != edelib::String::npos) { j=i; i=newname.find('/',j+1); }
vline = newname.substr(j+1) + vline.substr(vline.find(view->column_char(),0));
view->text(focus,vline.c_str());
}
}

28
efiler/fileops.h Normal file
View File

@ -0,0 +1,28 @@
// This file implements copy / move / delete operation with files
// NOT TO BE CONFUSED WITH edelib::File.h !!!
// Functions here usually call stuff from File.h, but also update
// efiler interface, display warnings to user etc.
#include "EDE_FileView.h"
#include <FL/Fl_Box.H>
// Execute cut or copy operation when List View is active
void do_cut_copy(bool m_copy);
// Execute paste operation - this will copy or move files based on chosen
// operation
void do_paste();
// Delete currently selected file(s) or directory(es)
void do_delete();
// Rename the file with focus to given name
void do_rename(const char*);
extern FileDetailsView* view;
extern Fl_Box* statusbar;
extern char current_dir[];
extern void loaddir(const char*);
extern bool is_on_same_fs(const char*, const char*); // should be moved to another library