public FileMoveOperation QueueMoveOperation(string name, string source, string destination, PlexSection plexSection) { var fmo = new FileMoveOperation { CurrentState = FileMoveState.Queued, Destination = destination, ErrorMessage = null, Finished = null, Name = name, PlexSection = plexSection, Source = source }; // todo: test from here _allOperationsRWLock.EnterWriteLock(); try { fmo.ID = _ID; ++_ID; _allOperations.Add(fmo); } finally { _allOperationsRWLock.ExitWriteLock(); } _operationsPending.Add(fmo); _logger.LogDebug("Queued new FileMoveOperation named {0}", name); return(fmo.Clone()); }
private FileOperation NewFileMoveOperation() { var fileMove = new FileMoveOperation( new DirectoryInfo(Source), new DirectoryInfo(Destination), GetFileFilters(), GetDirectoryFilters(), GetOptions()) { BufferSize = BufferSize, Credentials = GetCredentials(), WhatToCopy = GetWhatToCopy(), RetryCount = RetryCount, RetryInterval = TimeSpan.FromSeconds(RetryIntervalSeconds) }; return(ThreadCount.HasValue ? (FileOperation)fileMove.MakeMultiThreaded(ThreadCount.Value ?? 8) : fileMove); }
public FileMoveState QueryState(FileMoveOperation fmo) { _allOperationsRWLock.EnterReadLock(); try { var op = _allOperations.FirstOrDefault(o => o.ID == fmo.ID); if (op == null) { return(op.CurrentState); } throw new KeyNotFoundException("The given ID does not exist in the operations table"); } finally { _allOperationsRWLock.ExitReadLock(); } }
public static void Main() { //reference to the active project and the main window Form primaryMainForm = Program.ActiveProjectShell.PrimaryMainForm; if (primaryMainForm == null) { return; } SwissAcademic.Citavi.Project activeProject = Program.ActiveProjectShell.Project; if (activeProject == null) { return; } if (IsBackupAvailable() == false) { return; //user wants to backup his/her project first } //if no reference filters applied, macro cannot operate if (Program.ActiveProjectShell.PrimaryMainForm.ReferenceEditorFilterSet.Filters.Count == 0) { string message = "This macro requires a selection of references to operate on.\r\n"; message += "Please select some references with PDF files attached."; MessageBox.Show(message, "Citavi"); return; } //path to the current project's CitaviFiles folder string citaviFilesPath = activeProject.GetFolderPath(CitaviFolder.CitaviFiles); List <Reference> references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences(); //List<Reference> references = CollectionUtility.ToList(activeProject.References); #region Generate list of file locations to move //first we create a list of files attached to the currently selected references, //having a PDF extension, //and existing inside the CitaviFiles folder List <FileMoveOperation> fileMoveOperations = new List <FileMoveOperation>(); foreach (Reference reference in references) { foreach (Location location in reference.Locations) { if (location.LocationType != LocationType.ElectronicAddress) { continue; } if (location.AddressUri.AddressInfo == ElectronicAddressInfo.CitaviFiles) { if (Path.GetExtension(location.Address).Equals(".pdf", StringComparison.OrdinalIgnoreCase)) { FileMoveOperation fileMoveOperation = new FileMoveOperation(location); fileMoveOperations.Add(fileMoveOperation); //MessageBox.Show(fileMoveOperation.SourcePath); //MessageBox.Show(fileMoveOperation.OriginalFileName); } } } } if (fileMoveOperations.Count == 0) { string message = string.Format("The {0} selected reference(s) do not have any PDF files attached, that are stored inside the CitaviFiles folder.", references.Count); MessageBox.Show(message, "Citavi"); return; } #endregion Generate list of file locations to move #region Prompt user for target folder to move files to string targetFolderPath = string.Empty; using (var folderPicker = new CommonOpenFileDialog()) { folderPicker.IsFolderPicker = true; folderPicker.Title = string.Format("Select the folder where you want to move the {0} PDF files to.", fileMoveOperations.Count); folderPicker.InitialDirectory = Program.Settings.InitialDirectories.GetInitialDirectory(SwissAcademic.Citavi.Settings.InitialDirectoryContext.LocalFile, null); if (folderPicker.ShowDialog() == CommonFileDialogResult.Ok) { targetFolderPath = folderPicker.FileName; } else { MessageBox.Show("Macro execution cancelled upon user's request.", "Citavi"); return; } } #endregion Prompt user for target folder to move files to #region Copy the files to the new folder DirectoryInfo targetDirectory = new DirectoryInfo(targetFolderPath); foreach (FileMoveOperation fileMoveOperation in fileMoveOperations) { //avoid overwriting a possible existing file fileMoveOperation.TargetPath = Path2.GetUniqueFilePath(targetDirectory, fileMoveOperation.OriginalFileName); try { File.Copy(fileMoveOperation.SourcePath, fileMoveOperation.TargetPath, false); fileMoveOperation.CopySuccess = true; } catch (Exception exception) { fileMoveOperation.Errors.Add(exception); fileMoveOperation.CopySuccess = false; } } #endregion Copy the files to the new region #region Relink each reference to the new files foreach (FileMoveOperation fileMoveOperation in fileMoveOperations) { if (fileMoveOperation.CopySuccess) { try { fileMoveOperation.TargetLocation = fileMoveOperation.SourceLocation.Reference.Locations.Add(LocationType.ElectronicAddress, fileMoveOperation.TargetPath, string.Empty, false); fileMoveOperation.TargetLocation.Notes = fileMoveOperation.SourceLocation.Notes; fileMoveOperation.RelinkSuccess = true; } catch (Exception exception) { fileMoveOperation.Errors.Add(exception); fileMoveOperation.RelinkSuccess = false; } } } #endregion Relink each reference to the new files #region Delete the original locations and move the files in the CitaviFiles folder to CitaviFiles\RecycleBin foreach (FileMoveOperation fileMoveOperation in fileMoveOperations) { if (fileMoveOperation.RelinkSuccess) { try { Location locationToDelete = fileMoveOperation.SourceLocation; Project locationProject = locationToDelete.Project; if (locationToDelete == null) { continue; } ElectronicAddressUri addressUriToDelete = locationToDelete.AddressUri; locationToDelete.Reference.Locations.Remove(locationToDelete); Program.ActiveProjectShell.Save(primaryMainForm); DirectoryInfo recycleBinDirectory = new DirectoryInfo(locationProject.GetFolderPath(CitaviFolder.RecycleBin)); string deletedFilePath = Path2.GetUniqueFilePath(recycleBinDirectory, fileMoveOperation.OriginalFileName); File.Move(fileMoveOperation.SourcePath, deletedFilePath); fileMoveOperation.DeleteSuccess = true; } catch (Exception exception) { fileMoveOperation.Errors.Add(exception); fileMoveOperation.DeleteSuccess = false; } } } #endregion Delete the original locations and move the files in the CitaviFiles folder to CitaviFiles\RecycleBin MessageBox.Show("Macro has finished execution.", "Citavi"); }