From 10d225688c7235aeca9f8b18038a6db390ea9592 Mon Sep 17 00:00:00 2001 From: Encryptor Date: Fri, 23 Dec 2016 19:44:39 +0300 Subject: [PATCH] Added Context Menu support :) close #4 --- RudeFox.FrontEnd/App.xaml.cs | 52 ++++++++++++++++++++- RudeFox.FrontEnd/Helpers/Constants.cs | 2 + RudeFox.FrontEnd/ViewModels/MainWindowVM.cs | 41 ++-------------- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/RudeFox.FrontEnd/App.xaml.cs b/RudeFox.FrontEnd/App.xaml.cs index daf28dd..485a629 100644 --- a/RudeFox.FrontEnd/App.xaml.cs +++ b/RudeFox.FrontEnd/App.xaml.cs @@ -9,6 +9,11 @@ using System.Collections.ObjectModel; using RudeFox.ViewModels; using NLog.Targets; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using RudeFox.Models; +using RudeFox.Helpers; namespace RudeFox.FrontEnd { @@ -68,7 +73,23 @@ public static async Task DeleteFileOrFolder(string path) } } - protected override void OnStartup(StartupEventArgs e) + public static async Task DeleteFilesOrFolders(List paths) + { + paths.Remove(Constants.SENDTO_PREFIX); + var userAgreed = await GetUserAgreedToDeleteAsync(paths); + if (userAgreed != true) return; + + var duplicates = Operations.Select(item => item.Path).Intersect(paths); + paths.RemoveAll(p => duplicates.Contains(p)); + + var validPaths = paths.Where(path => System.IO.File.Exists(path) || Directory.Exists(path)); + var tasks = validPaths.Select(item => DeleteFileOrFolder(item)).ToList(); + + await Task.WhenAll(tasks); + } + + + protected override async void OnStartup(StartupEventArgs e) { // handle the unhandled global exceptions AppDomain.CurrentDomain.UnhandledException += (sender, args) => LogUnhandledException((Exception)args.ExceptionObject); @@ -78,6 +99,10 @@ protected override void OnStartup(StartupEventArgs e) // register sentry as NLog target Target.Register("Sentry"); + Task deleteTask = null; + if (e.Args.Length > 1 && e.Args[0].Equals(Constants.SENDTO_PREFIX, StringComparison.InvariantCultureIgnoreCase)) + deleteTask = DeleteFilesOrFolders(e.Args.ToList()); + #if !DEBUG // check for updates UpdateManager.Initialize(Keys.DROPBOX_API_KEY); @@ -88,6 +113,9 @@ protected override void OnStartup(StartupEventArgs e) var window = new MainWindow(); this.MainWindow = window; window.Show(); + + if (deleteTask != null) + await deleteTask; } private void LogUnhandledException(Exception e) @@ -124,6 +152,28 @@ await Current.Dispatcher.BeginInvoke(new Action(() => return true; // updated and waiting for the app to exit } + + private static async Task GetUserAgreedToDeleteAsync(List paths) + { + string message; + string okText = "Delete "; + var itemName = System.IO.File.Exists(paths[0]) ? "file" : "folder"; + + if (paths.Count == 1) + { + message = $"Are you sure you want to delete this {itemName}?{Environment.NewLine}"; + message += Path.GetFileName(paths[0]); + okText += "it"; + } + else + { + message = $"Are you sure you want to delete these {paths.Count} items?"; + okText += "them"; + } + + return await DialogService.Instance.GetMessageDialog("Deleting items", message, MessageIcon.Exclamation, okText, "Cancel", true).ShowDialogAsync(); + } + #endregion } } diff --git a/RudeFox.FrontEnd/Helpers/Constants.cs b/RudeFox.FrontEnd/Helpers/Constants.cs index 9450f8a..3fe637c 100644 --- a/RudeFox.FrontEnd/Helpers/Constants.cs +++ b/RudeFox.FrontEnd/Helpers/Constants.cs @@ -13,5 +13,7 @@ class Constants public const int GIGABYTE = MEGABYTE * KILOBYTE; public const string WEBSITE_URL = "https://github.com/encrypt0r/RudeFox"; + + public const string SENDTO_PREFIX = "--sendto"; } } diff --git a/RudeFox.FrontEnd/ViewModels/MainWindowVM.cs b/RudeFox.FrontEnd/ViewModels/MainWindowVM.cs index 0efffc0..6a77173 100644 --- a/RudeFox.FrontEnd/ViewModels/MainWindowVM.cs +++ b/RudeFox.FrontEnd/ViewModels/MainWindowVM.cs @@ -73,7 +73,7 @@ async void IDropTarget.Drop(IDropInfo dropInfo) if (data.GetDataPresent(DataFormats.FileDrop)) { string[] paths = (string[])data.GetData(DataFormats.FileDrop); - await DeleteItems(paths.ToList()); + await App.DeleteFilesOrFolders(paths.ToList()); } } #endregion @@ -88,7 +88,7 @@ private async Task DeleteFiles() if (result != true) return; var files = dialog.FileNames; - await DeleteItems(files.ToList()); + await App.DeleteFilesOrFolders(files.ToList()); } private async Task DeleteFolders() @@ -100,42 +100,9 @@ private async Task DeleteFolders() if (result != true) return; var path = dialog.SelectedPath; - await DeleteItems(new List { path }); - } - private async Task DeleteItems(List paths) - { - var userAgreed = await GetUserAgreedToDeleteAsync(paths); - if (userAgreed != true) return; - - var duplicates = App.Operations.Select(item => item.Path).Intersect(paths); - paths.RemoveAll(p => duplicates.Contains(p)); - - var validPaths = paths.Where(path => File.Exists(path) || Directory.Exists(path)); - var tasks = validPaths.Select(item => App.DeleteFileOrFolder(item)).ToList(); - - await Task.WhenAll(tasks); - } - - private async Task GetUserAgreedToDeleteAsync(List paths) - { - string message; - string okText = "Delete "; - var itemName = File.Exists(paths[0]) ? "file" : "folder"; - - if (paths.Count == 1) - { - message = $"Are you sure you want to delete this {itemName}?{Environment.NewLine}"; - message += Path.GetFileName(paths[0]); - okText += "it"; - } - else - { - message = $"Are you sure you want to delete these {paths.Count} items?"; - okText += "them"; - } - - return await DialogService.Instance.GetMessageDialog("Deleting items", message, MessageIcon.Exclamation, okText, "Cancel", true).ShowDialogAsync(); + await App.DeleteFilesOrFolders(new List { path }); } + #endregion } }