001    package net.sourceforge.mp3dings.utils;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: FileCopyRunnable.java,v 1.8 2006/09/04 12:32:27 ringler Exp $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020     */
021    
022    import java.awt.Frame;
023    import java.io.File;
024    import java.io.FileInputStream;
025    import java.io.FileOutputStream;
026    import java.io.IOException;
027    import java.lang.reflect.InvocationTargetException;
028    import java.nio.channels.FileChannel;
029    import java.nio.channels.FileLock;
030    import java.util.logging.Logger;
031    import java.util.Arrays;
032    import javax.swing.JOptionPane;
033    import javax.swing.SwingUtilities;
034    import de.mospace.swing.StatusBar;
035    import helliker.id3.APICImage;
036    import net.sourceforge.mp3dings.Globals;
037    import net.sourceforge.mp3dings.editors.APICImageEditor;
038    
039    /** This class copies files in a background thread. Progress
040    * is indicated in a Status Bar. In case of an exception
041    * file copying is suspended and the user is asked what to do in a JDialog.
042    * If the Status Bar is part of a container hierarchy
043    * the error dialog will be owned by the window ancestor of the Status bar.
044    **/
045    public class ImageSaveRunnable implements Runnable{
046        private static final Logger logger = Logger.getLogger(ImageSaveRunnable.class.getPackage().getName());
047        private int j = 0;
048        private int nimg = 1;
049        private final File dest;
050        private final StatusBar cpstatus;
051        private Integer errorAction;
052        private final File[] src;
053        private final APICImage[] images;
054        APICImageEditor saver = new APICImageEditor(); //does not need any renderers
055        int imagecount;
056    
057        private static String getExtension(APICImage pic){
058            String ext = ".apic";
059            if(pic.getURL() == null){
060                String mime = pic.getMimeType();
061                if(mime != null){
062                    String[] extensions = MimeTypeManager.getDefault().getExtensions(mime);
063                    if(extensions.length != 0){
064                        ext = extensions[0];
065                    }
066                }
067            } else {
068                String urlext = MimeTypeManager.getExtension(pic.getURL().toString());
069                if(urlext != null){
070                    ext = urlext;
071                }
072            }
073            return ext;
074        }
075    
076        public void run(){
077            int errorcount = 0;
078            boolean skipAll = false;
079            for(int i = 0; i<images.length; i++){
080                APICImage pic = images[i];
081                if(pic != null){
082                    nimg++;
083                    cpstatus.setStatus(Globals.getMessage("SAVE_IMAGES_ST",
084                    new Object[]{
085                        new Integer(nimg),
086                        new Integer(imagecount)}));
087                    String desc = pic.getDescription();
088                    if(desc != null){
089                        desc = desc.trim();
090                        if(desc.length() != 0){
091                            desc = desc.replaceAll("[^ \\p{L}\\d-_]+","");
092                            if(desc.length() != 0){
093                                desc = desc + "__";
094                            }
095                        }
096                    } else {
097                        desc = "";
098                    }
099                    File target = new File(dest, desc + src[i].getName() + getExtension(pic));
100                    if(target.exists()){
101                        String msg = Globals.getMessage("FILE_EXISTS_ERR",target);
102                        cpstatus.setStatus(msg);
103                        logger.warning(msg);
104                        errorcount++;
105                        continue;
106                    } else {
107                        saver.setImage(pic);
108                        try{
109                            saver.saveImage(target);
110                            j++;
111                        } catch(Exception ex){
112                            errorcount++;
113                            final String msg = ex.toString();
114                            cpstatus.setStatus(msg);
115                            logger.warning(msg);
116                        }
117                    }
118                }
119            }
120            SwingUtilities.invokeLater(
121                new Runnable(){
122                    public void run(){
123                        finished();
124                    }
125                }
126            );
127        }
128    
129        /** Returns the StatusBar in which this FileCopyRunnable displays its
130        * status messages. **/
131        public StatusBar getStatusBar(){
132            return cpstatus;
133        }
134    
135        /** Returns the number of files copied so far. */
136        public int getImagesSaved(){
137            return j;
138        }
139    
140        public int getTotalImages(){
141            return imagecount;
142        }
143    
144        public ImageSaveRunnable(File dest, File[] src, APICImage[] img){
145            cpstatus = new StatusBar(){
146                public void clear(){
147                }
148            };
149            images = img;
150            imagecount = img.length;
151            for(int i=0; i<img.length; i++){
152                if(img[i] == null){
153                    imagecount--;
154                }
155            }
156            cpstatus.setStatus(Globals.getMessage("SAVE_IMAGES_WAITING",imagecount));
157            this.src = src;
158            this.dest = dest;
159        }
160    
161        /** Should be overwritten. */
162        public void finished(){
163        }
164    };
165