Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a setting to remove mods/plugins/etc. to recycle bin instead of permanently #144

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/KKManager.Core/Functions/ZipmodTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using KKManager.Data.Zipmods;
using KKManager.Util;

namespace KKManager.Windows
{
Expand Down Expand Up @@ -121,7 +122,7 @@ private static void SafeFileDelete(string file)
try
{
if (!_simulate)
File.Delete(file);
file.SafeDelete().Wait();
}
catch (SystemException)
{
Expand Down
1 change: 1 addition & 0 deletions src/KKManager.Core/KKManager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Reference Include="Microsoft.NET.StringTools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.NET.StringTools.17.7.2\lib\net472\Microsoft.NET.StringTools.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="Mono.Cecil, Version=0.11.5.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.11.5\lib\net40\Mono.Cecil.dll</HintPath>
</Reference>
Expand Down
14 changes: 13 additions & 1 deletion src/KKManager.Core/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/KKManager.Core/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
<Setting Name="P2P_SettingsShown" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="DeleteToRecycleBin" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
75 changes: 74 additions & 1 deletion src/KKManager.Core/Util/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using BrightIdeasSoftware;
using KKManager.Properties;
using Microsoft.VisualBasic.FileIO;
using SharpCompress.Archives;
using SharpCompress.Readers;

Expand Down Expand Up @@ -113,7 +116,7 @@ public static string GetFullNameWithDifferentExtension(this FileInfo fi, string
{
if (fi == null) throw new ArgumentNullException(nameof(fi));
if (newExtension == null) throw new ArgumentNullException(nameof(newExtension));

return Path.Combine(fi.DirectoryName ?? throw new InvalidOperationException("DirectoryName null for " + fi),
fi.GetNameWithoutExtension() + newExtension);
}
Expand Down Expand Up @@ -141,5 +144,75 @@ public static XElement GetOrAddElement(this XElement e, string name)

public static void AddObjects<T>(this ObjectListView olv, IList<T> modelObjects) => olv.AddObjects((ICollection)modelObjects);
public static void RefreshObjects<T>(this ObjectListView olv, IList<T> modelObjects) => olv.RefreshObjects((IList)modelObjects);

public static async Task SafeDelete(this FileSystemInfo info)
{
if (info == null) return;

var toRecycleBin = Settings.Default.DeleteToRecycleBin;

try
{
if (info.Exists)
{
// Prevent issues removing readonly files
info.Attributes = FileAttributes.Normal;

if (info is DirectoryInfo dir)
{
try
{
if (toRecycleBin)
FileSystem.DeleteDirectory(dir.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
else
dir.Delete(true);
}
catch
{
await Task.Delay(100);
dir.Delete(true);
}
}
else if (info is FileInfo file)
{
try
{
if (toRecycleBin)
FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
else
file.Delete();
}
catch
{
await Task.Delay(100);
file.Delete();
}
}
else
{
throw new InvalidOperationException("wtf is" + info.GetType().AssemblyQualifiedName);
}
}
}
catch (Exception ex)
{
if (ex is AggregateException ae && ae.InnerExceptions.Count == 1)
ex = ae.InnerExceptions[0];

Console.WriteLine($"Failed to delete [{info.FullName}] because of error: {ex.ToStringDemystified()}");

throw;
}
}

public static Task SafeDelete(this string filename)
{
if (File.Exists(filename))
return new FileInfo(filename).SafeDelete();
else if (Directory.Exists(filename))
return new DirectoryInfo(filename).SafeDelete();
else
return Task.CompletedTask;
}
}
}
3 changes: 3 additions & 0 deletions src/KKManager.Core/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
<setting name="P2P_SettingsShown" serializeAs="String">
<value>False</value>
</setting>
<setting name="DeleteToRecycleBin" serializeAs="String">
<value>True</value>
</setting>
</KKManager.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /></startup></configuration>
4 changes: 1 addition & 3 deletions src/KKManager.Updater/Data/UpdateItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ public async Task Update(Progress<double> progressCallback, CancellationToken ca
if (TargetPath.Exists)
{
Console.WriteLine($"Deleting old file {TargetPath.FullName}");
// Prevent issues removing readonly files
TargetPath.Attributes = FileAttributes.Normal;
TargetPath.Delete();
await TargetPath.SafeDelete();
// Make sure the file gets deleted before continuing
await Task.Delay(200, cancellationToken).ConfigureAwait(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/KKManager.Updater/Sources/FtpUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private IEnumerable<FtpListItem> GetSubNodes(FtpListItem remoteDir)
private async Task UpdateItem(FtpListItem sourceItem, FileInfo targetPath, IProgress<double> progressCallback, CancellationToken cancellationToken)
{
// Delete old file if any exists so the download doesn't try to append to it. Append mode is needed for retrying downloads to resume instead of restarting
targetPath.Delete();
await targetPath.SafeDelete();

await Connect(cancellationToken).ConfigureAwait(false);

Expand Down
4 changes: 2 additions & 2 deletions src/KKManager/Windows/Content/CardWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ private void renameCardsToolStripMenuItem_Click(object sender, EventArgs e)
RenameCards.ShowDialog(this, _typedListView.SelectedObjects.ToArray());
}

private void toolStripButtonDelete_Click(object sender, EventArgs e)
private async void toolStripButtonDelete_Click(object sender, EventArgs e)
{
var selectedObjects = _typedListView.SelectedObjects;
if (!selectedObjects.Any()) return;
Expand All @@ -571,7 +571,7 @@ private void toolStripButtonDelete_Click(object sender, EventArgs e)
{
try
{
selectedObject.Location.Delete();
await selectedObject.Location.SafeDelete();
}
catch (Exception exception)
{
Expand Down
4 changes: 2 additions & 2 deletions src/KKManager/Windows/Content/PluginsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void SideloaderModsWindow_FormClosed(object sender, FormClosedEventArgs
CancelRefreshing();
}

private void toolStripButtonDelete_Click(object sender, EventArgs e)
private async void toolStripButtonDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("This will permanently delete all selected plugins, are you sure you want to continue?",
"Delete plugins", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
Expand All @@ -116,7 +116,7 @@ private void toolStripButtonDelete_Click(object sender, EventArgs e)
{
try
{
plug.Location.Delete();
await plug.Location.SafeDelete();
objectListView1.RemoveObject(plug);
}
catch (SystemException ex)
Expand Down
4 changes: 2 additions & 2 deletions src/KKManager/Windows/Content/SideloaderModsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private void toolStripButtonDisable_Click(object sender, EventArgs e)
SetZipmodEnabled(false, _listView.SelectedObjects);
}

private void toolStripButtonDelete_Click(object sender, EventArgs e)
private async void toolStripButtonDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("This will permanently delete all selected zipmods, are you sure you want to continue?",
"Delete zipmods", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
Expand All @@ -139,7 +139,7 @@ private void toolStripButtonDelete_Click(object sender, EventArgs e)
{
try
{
obj.Location.Delete();
await obj.Location.SafeDelete();
objectListView1.RemoveObject(obj);
}
catch (SystemException ex)
Expand Down
32 changes: 20 additions & 12 deletions src/KKManager/Windows/MainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/KKManager/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public MainWindow()

Settings.Default.Binder.BindControl(checkForUpdatesOnStartupToolStripMenuItem, settings => settings.AutoUpdateSearch, this);
Settings.Default.Binder.BindControl(useSystemProxyServerToolStripMenuItem, settings => settings.UseProxy, this);
Settings.Default.Binder.BindControl(tryToDeleteToRecycleBinToolStripMenuItem, settings => settings.DeleteToRecycleBin, this);
Settings.Default.Binder.SendUpdates(this);

// Before using the window location, check if isn't the default value and that it's actually visible on the screen
Expand Down
37 changes: 27 additions & 10 deletions src/KKManager/Windows/MainWindow.resx
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,22 @@
<value>&amp;Tools</value>
</data>
<data name="languagesToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>126, 22</value>
<value>215, 22</value>
</data>
<data name="languagesToolStripMenuItem.Text" xml:space="preserve">
<value>Language</value>
</data>
<data name="tryToDeleteToRecycleBinToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>215, 22</value>
</data>
<data name="tryToDeleteToRecycleBinToolStripMenuItem.Text" xml:space="preserve">
<value>Try to delete to Recycle Bin</value>
</data>
<data name="tryToDeleteToRecycleBinToolStripMenuItem.ToolTipText" xml:space="preserve">
<value>Attempt to move files to the recycle bin when deleting cards/mods/plugins and when updating mods.
If this fails for whatever reason (e.g. recycle bin is full or disabled) the files are deleted permanently.
</value>
</data>
<data name="settingsToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>61, 20</value>
</data>
Expand Down Expand Up @@ -2164,6 +2175,18 @@
<data name="&gt;&gt;fileToolStripMenuItem.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;openGameLogToolStripMenuItem.Name" xml:space="preserve">
<value>openGameLogToolStripMenuItem</value>
</data>
<data name="&gt;&gt;openGameLogToolStripMenuItem.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;toolStripSeparator7.Name" xml:space="preserve">
<value>toolStripSeparator7</value>
</data>
<data name="&gt;&gt;toolStripSeparator7.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripSeparator, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;changeGameInstallDirectoryToolStripMenuItem.Name" xml:space="preserve">
<value>changeGameInstallDirectoryToolStripMenuItem</value>
</data>
Expand Down Expand Up @@ -2464,18 +2487,12 @@
<data name="&gt;&gt;toolStripStatusLabelStatus.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripStatusLabel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;openGameLogToolStripMenuItem.Name" xml:space="preserve">
<value>openGameLogToolStripMenuItem</value>
<data name="&gt;&gt;tryToDeleteToRecycleBinToolStripMenuItem.Name" xml:space="preserve">
<value>tryToDeleteToRecycleBinToolStripMenuItem</value>
</data>
<data name="&gt;&gt;openGameLogToolStripMenuItem.Type" xml:space="preserve">
<data name="&gt;&gt;tryToDeleteToRecycleBinToolStripMenuItem.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;toolStripSeparator7.Name" xml:space="preserve">
<value>toolStripSeparator7</value>
</data>
<data name="&gt;&gt;toolStripSeparator7.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripSeparator, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;$this.Name" xml:space="preserve">
<value>MainWindow</value>
</data>
Expand Down
Loading