Skip to content

Commit

Permalink
Added Context Menu support :) close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Dec 23, 2016
1 parent df02a9c commit 10d2256
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
52 changes: 51 additions & 1 deletion RudeFox.FrontEnd/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -68,7 +73,23 @@ public static async Task DeleteFileOrFolder(string path)
}
}

protected override void OnStartup(StartupEventArgs e)
public static async Task DeleteFilesOrFolders(List<string> 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);
Expand All @@ -78,6 +99,10 @@ protected override void OnStartup(StartupEventArgs e)
// register sentry as NLog target
Target.Register<Nlog.SentryTarget>("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);
Expand All @@ -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)
Expand Down Expand Up @@ -124,6 +152,28 @@ await Current.Dispatcher.BeginInvoke(new Action(() =>

return true; // updated and waiting for the app to exit
}

private static async Task<bool?> GetUserAgreedToDeleteAsync(List<string> 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
}
}
2 changes: 2 additions & 0 deletions RudeFox.FrontEnd/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
41 changes: 4 additions & 37 deletions RudeFox.FrontEnd/ViewModels/MainWindowVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -100,42 +100,9 @@ private async Task DeleteFolders()
if (result != true) return;

var path = dialog.SelectedPath;
await DeleteItems(new List<string> { path });
}
private async Task DeleteItems(List<string> 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<bool?> GetUserAgreedToDeleteAsync(List<string> 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<string> { path });
}

#endregion
}
}

0 comments on commit 10d2256

Please sign in to comment.