use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class MkdirDialog method startJob.
/**
* Starts an {@link com.mucommander.job.impl.MkdirJob}. This method is trigged by the 'OK' button or the return key.
*/
public void startJob() {
String enteredPath = pathField.getText();
AbstractFile currentFolder = mainFrame.getActivePanel().getCurrentFolder();
// Resolves destination folder
PathUtils.ResolvedDestination resolvedDest = PathUtils.resolveDestination(enteredPath, currentFolder, currentFolder);
// The path entered doesn't correspond to any existing folder
if (resolvedDest == null) {
InformationDialog.showErrorDialog(mainFrame, Translator.get("invalid_path", enteredPath));
return;
}
// Checks if the directory already exists and reports the error if that's the case
DestinationType destinationType = resolvedDest.getDestinationType();
if (destinationType == DestinationType.EXISTING_FOLDER) {
InformationDialog.showErrorDialog(mainFrame, Translator.get("directory_already_exists", enteredPath));
return;
}
// Don't check for existing regular files, MkdirJob will take of it and popup a FileCollisionDialog
AbstractFile destFile = resolvedDest.getDestinationFile();
FileSet fileSet = new FileSet(destFile.getParent());
// Job's FileSet needs to contain at least one file
fileSet.add(destFile);
ProgressDialog progressDialog = new ProgressDialog(mainFrame, getTitle());
MkdirJob job;
if (mkfileMode)
job = new MkdirJob(progressDialog, mainFrame, fileSet, allocateSpaceCheckBox.isSelected() ? allocateSpaceChooser.getValue() : -1);
else
job = new MkdirJob(progressDialog, mainFrame, fileSet);
progressDialog.start(job);
}
use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class BatchRenameDialog method checkForDuplicates.
/**
* Checks if there are duplicates in new file names.
*/
private void checkForDuplicates() {
boolean duplicates = false;
boolean oldNamesConflict = false;
Set<String> names = new HashSet<String>();
for (int i = 0; i < newNames.size(); i++) {
String newName = newNames.get(i);
AbstractFile file = files.get(i);
AbstractFile parent = file.getParent();
if (parent != null) {
newName = parent.getAbsolutePath(true) + newName;
}
if (names.contains(newName)) {
duplicates = true;
break;
}
AbstractFile oldFile = oldNames.get(newName);
if (oldFile != null && oldFile != file) {
oldNamesConflict = true;
break;
}
names.add(newName);
}
if (duplicates) {
lblDuplicates.setText(Translator.get("batch_rename_dialog.duplicate_names"));
}
if (oldNamesConflict) {
lblDuplicates.setText(Translator.get("batch_rename_dialog.names_conflict"));
}
lblDuplicates.setVisible(duplicates || oldNamesConflict);
btnRename.setEnabled(!duplicates && !oldNamesConflict);
}
use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class BatchRenameDialog method removeUnchangedFiles.
/**
* Counts or removes unchanged files from change set.
* @param countOnly if true only counts files that are changing.
* @return number of changed files
*/
private int removeUnchangedFiles(boolean countOnly) {
// remove non changed files
Iterator<AbstractFile> fi = files.iterator();
Iterator<String> ni = newNames.iterator();
int changed = 0;
while (fi.hasNext()) {
AbstractFile file = fi.next();
String nn = ni.next();
if (file.getName().equals(nn)) {
if (!countOnly) {
fi.remove();
ni.remove();
}
} else {
changed++;
}
}
return changed;
}
use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class CopyDialog method createTransferFileJob.
// ////////////////////////////////////////////
// TransferDestinationDialog implementation //
// ////////////////////////////////////////////
@Override
protected TransferFileJob createTransferFileJob(ProgressDialog progressDialog, PathUtils.ResolvedDestination resolvedDest, FileCollisionDialog.FileCollisionAction defaultFileExistsAction) {
AbstractFile baseFolder = files.getBaseFolder();
AbstractArchiveFile parentArchiveFile = baseFolder.getParentArchive();
TransferFileJob job;
String newName = resolvedDest.getDestinationType() == DestinationType.EXISTING_FOLDER ? null : resolvedDest.getDestinationFile().getName();
// their natural order (more efficient)
if (parentArchiveFile != null) {
// Add all selected archive entries to a vector
int nbFiles = files.size();
List<ArchiveEntry> selectedEntries = new Vector<ArchiveEntry>();
for (int i = 0; i < nbFiles; i++) {
selectedEntries.add((ArchiveEntry) files.elementAt(i).getAncestor(AbstractArchiveEntryFile.class).getUnderlyingFileObject());
}
job = new UnpackJob(progressDialog, mainFrame, parentArchiveFile, PathUtils.getDepth(baseFolder.getAbsolutePath(), baseFolder.getSeparator()) - PathUtils.getDepth(parentArchiveFile.getAbsolutePath(), parentArchiveFile.getSeparator()), resolvedDest.getDestinationFolder(), newName, defaultFileExistsAction, selectedEntries);
} else {
job = new CopyJob(progressDialog, mainFrame, files, resolvedDest.getDestinationFolder(), newName, TransferMode.COPY, defaultFileExistsAction);
}
return job;
}
use of com.mucommander.commons.file.AbstractFile in project mucommander by mucommander.
the class MoveJob method processFile.
// //////////////////////////////////
// TransferFileJob implementation //
// //////////////////////////////////
/**
* Moves recursively the given file or folder.
*
* @param file the file or folder to move
* @param recurseParams destination folder where the given file will be moved (null for top level files)
*
* @return <code>true</code> if the file has been moved completely (copied + deleted).
*/
@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;
// the renamed file (the search results may reside in different sub-folders)
if (renameMode && SearchFile.SCHEMA.equals(destFolder.getURL().getScheme()))
destFolder = file.getParent();
// Is current file at the base folder level ?
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;
/*
// Do not follow symlink, simply delete it and return
if(file.isSymlink()) {
do { // Loop for retry
try {
file.delete();
return true;
}
catch(IOException e) {
LOGGER.debug("IOException caught", e);
int ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_delete_file", file.getAbsolutePath()));
// Retry loops
if(ret==FileJobAction.RETRY)
continue;
// Cancel, skip or close dialog returns false
return false;
}
} while(true);
}
*/
destFile = checkForCollision(file, destFolder, destFile, renameMode);
if (destFile == null)
return false;
// I/O bound checks and ends up throwing an exception which also comes at a cost.
if (!append && file.getURL().schemeEquals(destFile.getURL()) && file.isFileOperationSupported(FileOperation.RENAME)) {
try {
file.renameTo(destFile);
return true;
} catch (IOException e) {
// Fail silently: renameTo might fail under normal conditions, for instance for local files which are
// not located on the same volume.
LOGGER.debug("Failed to rename " + file + " into " + destFile + " (not necessarily an error)", e);
}
}
// Move the directory and all its children recursively, by copying files to the destination and then deleting them.
if (file.isDirectory()) {
// create the destination folder if it doesn't exist
if (!(destFile.exists() && destFile.isDirectory())) {
do {
// Loop for retry
try {
destFile.mkdir();
} catch (IOException e) {
DialogAction ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_create_folder", destFile.getAbsolutePath()));
// Retry loops
if (ret == FileJobAction.RETRY)
continue;
// Cancel, skip or close dialog returns false
return false;
}
break;
} while (true);
}
// move each file in this folder recursively
do {
// Loop for retry
try {
AbstractFile[] subFiles = file.ls();
boolean isFolderEmpty = true;
for (AbstractFile subFile : subFiles) {
// Return now if the job was interrupted, so that we do not attempt to delete this folder
if (getState() == FileJobState.INTERRUPTED)
return false;
// Notify job that we're starting to process this file (needed for recursive calls to processFile)
nextFile(subFile);
if (!processFile(subFile, destFile))
isFolderEmpty = false;
}
// 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
}
}
// If one file failed to be moved, return false (failure) since this folder could not be moved totally
if (!isFolderEmpty)
return false;
} 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;
}
break;
} while (true);
// Return now if the job was interrupted, so that we do not attempt to delete this folder
if (getState() == FileJobState.INTERRUPTED)
return false;
// finally, delete the empty folder
do {
// Loop for retry
try {
file.delete();
return true;
} catch (IOException e) {
DialogAction ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_delete_folder", file.getAbsolutePath()));
// Retry loops
if (ret == FileJobAction.RETRY)
continue;
// Cancel, skip or close dialog returns false
return false;
}
} while (true);
} else // File is a regular file, move it by copying it to the destination and then deleting it
{
// try the hard way by copying the file first, and then deleting the source file.
if (tryCopyFile(file, destFile, append, errorDialogTitle) && getState() != FileJobState.INTERRUPTED) {
// Delete the source file
do {
// Loop for retry
try {
file.delete();
// All OK
return true;
} catch (IOException e) {
LOGGER.debug("IOException caught", e);
DialogAction ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_delete_file", file.getAbsolutePath()));
// Retry loops
if (ret == FileJobAction.RETRY)
continue;
// Cancel, skip or close dialog returns false
return false;
}
} while (true);
}
return false;
}
}
Aggregations