Search in sources :

Example 96 with AbstractFile

use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.

the class SearchDialog method actionPerformed.

// /////////////////////////
// ActionListener method //
// /////////////////////////
@Override
public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == cancelButton) {
        dispose();
        return;
    }
    // otherwise, searchButton was pressed
    if (!validateAndUpdateValues())
        return;
    String searchIn = searchInField.getText();
    AbstractFile file = FileFactory.getFile(searchIn);
    if (file == null || !file.exists()) {
        searchInField.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
        searchInField.setToolTipText(Translator.get("folder_does_not_exist"));
        return;
    }
    FileURL fileURL = SearchUtils.toSearchURL(file);
    fileURL.setPath(searchFilesField.getText());
    String searchQuery = getSearchQuery();
    if (!searchQuery.isEmpty())
        fileURL.setQuery(searchQuery);
    dispose();
    mainFrame.getActivePanel().tryChangeCurrentFolder(fileURL);
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AbstractFile(com.mucommander.commons.file.AbstractFile)

Example 97 with AbstractFile

use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.

the class ArchiveJob method processFile.

// //////////////////////////////////
// TransferFileJob implementation //
// //////////////////////////////////
@Override
protected boolean processFile(AbstractFile file, Object recurseParams) {
    if (getState() == FileJobState.INTERRUPTED)
        return false;
    String filePath = file.getAbsolutePath(false);
    String entryRelativePath = filePath.substring(baseFolderPath.length() + 1, filePath.length());
    // Process current file
    do {
        // Loop for retry
        try {
            if (file.isDirectory() && !file.isSymlink()) {
                // Create new directory entry in archive file
                archiver.createEntry(entryRelativePath, file);
                // Recurse on files
                AbstractFile[] subFiles = file.ls();
                boolean folderComplete = true;
                for (int i = 0; i < subFiles.length && getState() != FileJobState.INTERRUPTED; i++) {
                    // Notify job that we're starting to process this file (needed for recursive calls to processFile)
                    nextFile(subFiles[i]);
                    if (!processFile(subFiles[i], null))
                        folderComplete = false;
                }
                return folderComplete;
            } else {
                InputStream in = setCurrentInputStream(file.getInputStream());
                // written to the archive OutputStream, this would cause ZipOutputStream to deadlock.
                synchronized (ioLock) {
                    // Create a new file entry in archive and copy the current file
                    StreamUtils.copyStream(in, archiver.createEntry(entryRelativePath, file));
                    in.close();
                }
                return true;
            }
        }// Catch Exception rather than IOException as ZipOutputStream has been seen throwing NullPointerException
         catch (Exception e) {
            // In this case, the exception should not be interpreted as an error.
            if (getState() == FileJobState.INTERRUPTED)
                return false;
            LOGGER.debug("Caught IOException", e);
            DialogAction ret = showErrorDialog(Translator.get("pack_dialog.error_title"), 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) InputStream(java.io.InputStream) DialogAction(com.mucommander.ui.dialog.DialogAction) IOException(java.io.IOException)

Example 98 with AbstractFile

use of com.mucommander.commons.file.AbstractFile 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 99 with AbstractFile

use of com.mucommander.commons.file.AbstractFile 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 100 with AbstractFile

use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.

the class AbstractViewerAction method performAction.

// - AbstractAction implementation ---------------------------------------------------
// -----------------------------------------------------------------------------------
/**
 * Edits the currently selected file.
 */
@Override
public synchronized void performAction() {
    AbstractFile file;
    Command customCommand;
    file = mainFrame.getActiveTable().getSelectedFile(false, true);
    // viewer/editor implementations will decide whether they allow a particular file or not.
    if (file != null) {
        customCommand = getCustomCommand();
        // If we're using a custom command...
        if (customCommand != null) {
            // If it's local, run the custom editor on it.
            if (file.hasAncestor(LocalFile.class)) {
                try {
                    InformationDialog.showErrorDialogIfNeeded(getMainFrame(), ProcessRunner.executeAsync(customCommand.getTokens(file), file));
                } catch (Exception e) {
                    InformationDialog.showErrorDialog(mainFrame);
                }
            } else // If it's distant, copies it locally before running the custom editor on it.
            {
                ProgressDialog progressDialog = new ProgressDialog(mainFrame, Translator.get("copy_dialog.copying"));
                TempOpenWithJob job = new TempOpenWithJob(progressDialog, mainFrame, file, customCommand);
                progressDialog.start(job);
            }
        } else
            // If we're not using a custom editor, this action behaves exactly like its parent.
            performInternalAction(file);
    }
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile) Command(com.mucommander.command.Command) ProgressDialog(com.mucommander.ui.dialog.file.ProgressDialog) TempOpenWithJob(com.mucommander.job.impl.TempOpenWithJob)

Aggregations

AbstractFile (com.mucommander.commons.file.AbstractFile)150 IOException (java.io.IOException)49 FileURL (com.mucommander.commons.file.FileURL)19 FileSet (com.mucommander.commons.file.util.FileSet)11 FileTable (com.mucommander.ui.main.table.FileTable)11 DialogAction (com.mucommander.ui.dialog.DialogAction)10 File (java.io.File)9 List (java.util.List)8 MainFrame (com.mucommander.ui.main.MainFrame)6 InputStream (java.io.InputStream)6 Vector (java.util.Vector)6 AbstractArchiveEntryFile (com.mucommander.commons.file.archive.AbstractArchiveEntryFile)5 ProtocolFile (com.mucommander.commons.file.protocol.ProtocolFile)5 LocalFile (com.mucommander.commons.file.protocol.local.LocalFile)5 ProgressDialog (com.mucommander.ui.dialog.file.ProgressDialog)5 FolderPanel (com.mucommander.ui.main.FolderPanel)5 FileTableModel (com.mucommander.ui.main.table.FileTableModel)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 UnsupportedFileOperationException (com.mucommander.commons.file.UnsupportedFileOperationException)4