Search in sources :

Example 16 with DialogAction

use of com.mucommander.ui.dialog.DialogAction in project mucommander by mucommander.

the class CalculateChecksumJob method jobStarted.

// //////////////////////
// Overridden methods //
// //////////////////////
@Override
protected void jobStarted() {
    super.jobStarted();
    // Check for file collisions, i.e. if the file already exists in the destination
    int collision = FileCollisionChecker.checkForCollision(null, checksumFile);
    if (collision != FileCollisionChecker.NO_COLLISION) {
        // File already exists in destination, ask the user what to do (cancel, overwrite,...) but
        // do not offer the multiple files mode options such as 'skip' and 'apply to all'.
        DialogAction choice = waitForUserResponse(new FileCollisionDialog(getProgressDialog(), getMainFrame(), collision, null, checksumFile, false, false));
        // Overwrite file
        if (choice == FileCollisionDialog.FileCollisionAction.OVERWRITE) {
        // Do nothing, simply continue and file will be overwritten
        } else // 'Cancel' or close dialog interrupts the job
        {
            interrupt();
            return;
        }
    }
    // Loop for retry
    do {
        try {
            // Tries to get an OutputStream on the destination file
            this.checksumFileOut = checksumFile.getOutputStream();
            break;
        } catch (Exception e) {
            DialogAction choice = showErrorDialog(Translator.get("error"), Translator.get("cannot_write_file", checksumFile.getName()), Arrays.asList(FileJobAction.CANCEL, FileJobAction.RETRY));
            // Retry loops
            if (choice == FileJobAction.RETRY)
                continue;
            // 'Cancel' or close dialog interrupts the job
            interrupt();
            return;
        }
    } while (true);
}
Also used : FileCollisionDialog(com.mucommander.ui.dialog.file.FileCollisionDialog) DialogAction(com.mucommander.ui.dialog.DialogAction) IOException(java.io.IOException)

Example 17 with DialogAction

use of com.mucommander.ui.dialog.DialogAction in project mucommander by mucommander.

the class CalculateChecksumJob method processFile.

// //////////////////////////////////
// TransferFileJob implementation //
// //////////////////////////////////
@Override
protected boolean processFile(AbstractFile file, Object recurseParams) {
    // Skip directories
    if (file.isDirectory()) {
        do {
            // Loop for retry
            try {
                // for each file in folder...
                AbstractFile[] children = file.ls();
                for (int i = 0; i < children.length && getState() != FileJobState.INTERRUPTED; i++) {
                    // Notify job that we're starting to process this file (needed for recursive calls to processFile)
                    nextFile(children[i]);
                    processFile(children[i], null);
                }
                return true;
            } catch (IOException e) {
                // file.ls() failed
                DialogAction ret = showErrorDialog(Translator.get("error"), Translator.get("cannot_read_folder", file.getName()));
                // Retry loops
                if (ret == FileJobAction.RETRY)
                    continue;
                // Cancel, skip or close dialog returns false
                return false;
            }
        } while (true);
    }
    // Calculate the file's checksum
    do {
        // Loop for retry
        InputStream in = null;
        String line;
        String checksum;
        try {
            // Resets the digest before use
            digest.reset();
            in = null;
            in = setCurrentInputStream(file.getInputStream());
            // Determine the path relative to the base source folder
            String relativePath = file.getAbsolutePath();
            relativePath = relativePath.substring(baseSourcePath.length(), relativePath.length());
            // Write a new line in the checksum file, in the appropriate format
            checksum = AbstractFile.calculateChecksum(in, digest);
            if (useSfvFormat) {
                // SFV format for CRC32 checksums
                // 1 space character
                line = relativePath + " " + checksum;
            } else {
                // 'SUMS' format for other checksum algorithms
                // 2 space characters, that's how the format is
                line = checksum + "  " + relativePath;
            }
            line += '\n';
            // Close the InputStream, we're done with it
            in.close();
            checksumFileOut.write(line.getBytes("utf-8"));
            return true;
        } catch (IOException e) {
            // Close the InputStream, a new one will be created when retrying
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e2) {
                }
            }
            // Same goes if the current file was skipped.
            if (getState() == FileJobState.INTERRUPTED || wasCurrentFileSkipped())
                return false;
            LOGGER.debug("Caught IOException", e);
            DialogAction ret = showErrorDialog(Translator.get("error"), Translator.get("error_while_transferring", file.getAbsolutePath()));
            // Retry loops
            if (ret == FileJobAction.RETRY) {
                // Reset processed bytes currentFileByteCounter
                resetCurrentFileByteCounter();
                continue;
            }
            // Cancel, skip or close dialog return false
            return false;
        }
    } while (true);
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile) DialogAction(com.mucommander.ui.dialog.DialogAction) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 18 with DialogAction

use of com.mucommander.ui.dialog.DialogAction in project mucommander by mucommander.

the class CopyJob method processFile.

// //////////////////////////////////
// TransferFileJob implementation //
// //////////////////////////////////
/**
 * Copies recursively the given file or folder.
 *
 * @param file the file or folder to move
 * @param recurseParams destination folder where the given file will be copied (null for top level files)
 *
 * @return <code>true</code> if the file has been copied.
 */
@Override
protected boolean processFile(AbstractFile file, Object recurseParams) {
    // Stop if interrupted
    if (getState() == FileJobState.INTERRUPTED)
        return false;
    // Destination folder
    AbstractFile destFolder = recurseParams == null ? baseDestFolder : (AbstractFile) recurseParams;
    // Is current file in base folder ?
    boolean isFileInBaseFolder = files.indexOf(file) != -1;
    // Determine filename in destination
    String destFileName = isFileInBaseFolder && newName != null ? newName : file.getName();
    // Create destination AbstractFile instance
    AbstractFile destFile = createDestinationFile(file, destFolder, destFileName);
    if (destFile == null)
        return false;
    currentDestFile = destFile;
    // is non-local (skip file and return)
    if (file.isSymlink() && (!file.hasAncestor(LocalFile.class) || !destFile.hasAncestor(LocalFile.class))) {
        return true;
    }
    destFile = checkForCollision(file, destFolder, destFile, false);
    if (destFile == null)
        return false;
    if (file.isSymlink()) {
        return tryCopySymlinkFile(file, destFile);
    }
    // Copy directory recursively
    if (file.isDirectory()) {
        // Create the folder in the destination folder if it doesn't exist
        if (!destFile.exists() || !destFile.isDirectory()) {
            // Loop for retry
            do {
                try {
                    destFile.mkdir();
                } catch (IOException e) {
                    // Unable to create folder
                    DialogAction ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_create_folder", destFileName));
                    // Retry loops
                    if (ret == FileJobAction.RETRY)
                        continue;
                    // Cancel or close dialog return false
                    return false;
                // Skip continues
                }
                break;
            } while (true);
        }
        // and copy each file in this folder recursively
        do {
            // Loop for retry
            try {
                // for each file in folder...
                for (AbstractFile subFile : file.ls()) {
                    if (getState() == FileJobState.INTERRUPTED)
                        break;
                    // Notify job that we're starting to process this file (needed for recursive calls to processFile)
                    nextFile(subFile);
                    processFile(subFile, destFile);
                }
                // Set currentDestFile back to the enclosing folder in case an overridden processFile method
                // needs to work with the folder after calling super.processFile.
                currentDestFile = destFile;
                // Only when finished with folder, set destination folder's date to match the original folder one
                if (destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) {
                    try {
                        destFile.changeDate(file.getDate());
                    } catch (IOException e) {
                        LOGGER.debug("failed to change the date of " + destFile, e);
                    // Fail silently
                    }
                }
                return true;
            } catch (IOException e) {
                // file.ls() failed
                DialogAction ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_read_folder", file.getName()));
                // Retry loops
                if (ret == FileJobAction.RETRY)
                    continue;
                // Cancel, skip or close dialog returns false
                return false;
            }
        } while (true);
    } else // File is a regular file, copy it
    {
        // Copy the file
        return tryCopyFile(file, destFile, append, errorDialogTitle);
    }
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile) DialogAction(com.mucommander.ui.dialog.DialogAction) IOException(java.io.IOException)

Example 19 with DialogAction

use of com.mucommander.ui.dialog.DialogAction in project mucommander by mucommander.

the class BinaryEditor method saveAsFile.

public void saveAsFile() {
    JFileChooser fileChooser = new JFileChooser();
    // Sets selected file in JFileChooser to current file
    if (currentFile.getURL().getScheme().equals(LocalFile.SCHEMA)) {
        fileChooser.setSelectedFile(new java.io.File(currentFile.getAbsolutePath()));
    }
    fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
    int ret = fileChooser.showSaveDialog(windowFrame);
    if (ret == JFileChooser.APPROVE_OPTION) {
        AbstractFile destFile;
        try {
            destFile = FileFactory.getFile(fileChooser.getSelectedFile().getAbsolutePath(), true);
        } catch (IOException e) {
            InformationDialog.showErrorDialog(windowFrame, Translator.get("write_error"), Translator.get("file_editor.cannot_write"));
            return;
        }
        // Check for file collisions, i.e. if the file already exists in the destination
        int collision = FileCollisionChecker.checkForCollision(null, destFile);
        if (collision != FileCollisionChecker.NO_COLLISION) {
            // File already exists in destination, ask the user what to do (cancel, overwrite,...) but
            // do not offer the multiple file mode options such as 'skip' and 'apply to all'.
            DialogAction action = new FileCollisionDialog(windowFrame, windowFrame, /* mainFrame */
            collision, null, destFile, false, false).getActionValue();
            if (action != FileCollisionDialog.FileCollisionAction.OVERWRITE) {
                return;
            }
        }
        trySave(destFile);
    }
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile) FileCollisionDialog(com.mucommander.ui.dialog.file.FileCollisionDialog) JFileChooser(javax.swing.JFileChooser) DialogAction(com.mucommander.ui.dialog.DialogAction) IOException(java.io.IOException)

Example 20 with DialogAction

use of com.mucommander.ui.dialog.DialogAction in project mucommander by mucommander.

the class BinaryEditor method canClose.

/**
 * Checks whether file can be closed and asks for confirmation if necessary.
 *
 * @return true if the file does not have any unsaved change or if the user opted to save the changes, false if the
 *         user canceled the dialog or the save failed.
 */
public boolean canClose() {
    if (!saveNeeded) {
        return true;
    }
    QuestionDialog dialog = new QuestionDialog(windowFrame, null, Translator.get("file_editor.save_warning"), binaryComponent, Arrays.asList(BinaryEditorAction.YES, BinaryEditorAction.NO, BinaryEditorAction.CANCEL), 0);
    DialogAction ret = dialog.getActionValue();
    if (ret == BinaryEditorAction.YES && trySave(currentFile) || ret == BinaryEditorAction.NO) {
        setSaveNeeded(false);
        return true;
    }
    // User canceled or the file couldn't be properly saved
    return false;
}
Also used : QuestionDialog(com.mucommander.ui.dialog.QuestionDialog) DialogAction(com.mucommander.ui.dialog.DialogAction)

Aggregations

DialogAction (com.mucommander.ui.dialog.DialogAction)22 IOException (java.io.IOException)13 AbstractFile (com.mucommander.commons.file.AbstractFile)10 FileCollisionDialog (com.mucommander.ui.dialog.file.FileCollisionDialog)7 QuestionDialog (com.mucommander.ui.dialog.QuestionDialog)6 JFileChooser (javax.swing.JFileChooser)3 InformationPane (com.mucommander.ui.layout.InformationPane)2 MainFrame (com.mucommander.ui.main.MainFrame)2 WarnUserException (com.mucommander.viewer.WarnUserException)2 Cursor (java.awt.Cursor)2 Frame (java.awt.Frame)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 JCheckBox (javax.swing.JCheckBox)2 VersionChecker (com.mucommander.VersionChecker)1 CredentialsMapping (com.mucommander.auth.CredentialsMapping)1 AuthException (com.mucommander.commons.file.AuthException)1 AuthenticationType (com.mucommander.commons.file.AuthenticationType)1 FileURL (com.mucommander.commons.file.FileURL)1 UnsupportedFileOperationException (com.mucommander.commons.file.UnsupportedFileOperationException)1