Esempio n. 1
0
    public async Task AddFilesAsync(IEnumerable <FileSystemPath> files)
    {
        // Get the manager
        IArchiveDataManager manager = Archive.Manager;

        int modifiedCount = 0;

        // Add every file
        foreach (FileSystemPath file in files)
        {
            string fileName = file.Name;
            string dir      = FullPath;

            ArchiveFileViewModel?existingFile = Files.FirstOrDefault(x => x.FileName.Equals(fileName, StringComparison.OrdinalIgnoreCase));

            // Check if the file name conflicts with an existing file
            if (existingFile != null)
            {
                if (!await Services.MessageUI.DisplayMessageAsync(String.Format(Resources.Archive_AddFiles_Conflict, file), Resources.Archive_AddFiles_ConflictHeader, MessageType.Warning, true))
                {
                    continue;
                }
            }

            try
            {
                // Open the file as a stream
                using FileStream fileStream = File.OpenRead(file);

                ArchiveFileViewModel fileViewModel = existingFile ?? new ArchiveFileViewModel(new ArchiveFileItem(manager, fileName, dir, manager.GetNewFileEntry(Archive.ArchiveData ?? throw new Exception("Archive data has not been loaded"), dir, fileName)), this);

                // Replace the file with the import data
                if (await Task.Run(() => fileViewModel.ReplaceFile(fileStream)))
                {
                    modifiedCount++;
                }

                // Add the file to the list if it was created
                if (existingFile == null)
                {
                    Files.Add(fileViewModel);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Adding files to archive directory {0}", DisplayName);

                await Services.MessageUI.DisplayExceptionMessageAsync(ex, String.Format(Resources.Archive_AddFiles_Error, file.Name));

                return;
            }
        }

        Archive.AddModifiedFiles(modifiedCount);
        Archive.ExplorerDialogViewModel.RefreshStatusBar();
    }
Esempio n. 2
0
    /// <summary>
    /// Deletes the directory
    /// </summary>
    /// <returns>The task</returns>
    public async Task DeleteDirectoryAsync()
    {
        // Make sure the archive supports creating directories
        if (!Archive.Manager.CanModifyDirectories)
        {
            Logger.Trace("The directory {0} was not removed due to the manager not allowing directory modifications", DisplayName);
            return;
        }

        // We can't delete the root directory
        if (IsRoot)
        {
            return;
        }

        Logger.Trace("The archive directory {0} is being removed...", DisplayName);

        // Run as a load operation
        using (await Archive.LoadOperation.RunAsync())
        {
            // Lock the access to the archive
            using (await Archive.ArchiveLock.LockAsync())
            {
                // Remove from parent directory
                Parent !.Remove(this);

                // Dispose the directory and the sub-directory
                this.GetAllChildren(true).DisposeAll();

                // Add as modified file
                Archive.AddModifiedFiles();

                Logger.Trace("The archive directory has been removed");
            }
        }
    }