001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.job;
020    
021    import com.mucommander.file.AbstractFile;
022    import com.mucommander.file.util.FileSet;
023    import com.mucommander.text.Translator;
024    import com.mucommander.ui.dialog.file.ProgressDialog;
025    import com.mucommander.ui.main.MainFrame;
026    
027    import java.io.IOException;
028    
029    /**
030     * @author Maxence Bernard
031     */
032    public class ChangeFileAttributesJob extends FileJob {
033    
034        private boolean recurseOnDirectories;
035    
036        private int permissions = -1;
037        private long date = -1;
038    
039    
040        public ChangeFileAttributesJob(ProgressDialog progressDialog, MainFrame mainFrame, FileSet files, int permissions, boolean recurseOnDirectories) {
041            super(progressDialog, mainFrame, files);
042    
043            this.permissions = permissions;
044            this.recurseOnDirectories = recurseOnDirectories;
045        }
046    
047    
048        public ChangeFileAttributesJob(ProgressDialog progressDialog, MainFrame mainFrame, FileSet files, long date, boolean recurseOnDirectories) {
049            super(progressDialog, mainFrame, files);
050    
051            this.date = date;
052            this.recurseOnDirectories = recurseOnDirectories;
053        }
054    
055    
056        ////////////////////////////
057        // FileJob implementation //
058        ////////////////////////////
059    
060        protected boolean processFile(AbstractFile file, Object recurseParams) {
061            // Stop if interrupted
062            if(getState()==INTERRUPTED)
063                return false;
064    
065            if(recurseOnDirectories && file.isDirectory()) {
066                do {                // Loop for retries
067                    try {
068                        AbstractFile children[] = file.ls();
069                        int nbChildren = children.length;
070    
071                        for(int i=0; i<nbChildren && getState()!=INTERRUPTED; i++) {
072                            // Notify job that we're starting to process this file (needed for recursive calls to processFile)
073                            nextFile(children[i]);
074                            processFile(children[i], null);
075                        }
076    
077                        break;
078                    }
079                    catch(IOException e) {
080                        // Unable to open source file
081                        int ret = showErrorDialog("", Translator.get("cannot_read_folder", file.getName()));
082                        // Retry loops
083                        if(ret==RETRY_ACTION)
084                            continue;
085                        // Cancel, skip or close dialog return false
086                        return false;
087                    }
088                }
089                while(true);
090            }
091    
092            if(permissions!=-1)
093                return file.changePermissions(permissions);
094    
095    //        if(date!=-1)
096            return file.changeDate(date);
097        }
098    
099        // This job modifies the FileSet's base folder and potentially its subfolders
100        protected boolean hasFolderChanged(AbstractFile folder) {
101            return files.getBaseFolder().isParentOf(folder);
102        }
103    }