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

Option to unzip zip archives. #80

Merged
merged 1 commit into from
Aug 30, 2024
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
18 changes: 18 additions & 0 deletions src/SmartCommander/Assets/Resources.Designer.cs

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

6 changes: 6 additions & 0 deletions src/SmartCommander/Assets/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,10 @@
<data name="Zip" xml:space="preserve">
<value>Zip</value>
</data>
<data name="Unzip" xml:space="preserve">
<value>Unzip</value>
</data>
<data name="DirectoryExists" xml:space="preserve">
<value>Directory {0} already exists.</value>
</data>
</root>
12 changes: 12 additions & 0 deletions src/SmartCommander/ViewModels/FilesPaneViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using MsBox.Avalonia.Enums;
using ReactiveUI;
using SmartCommander.Assets;
Expand All @@ -11,6 +12,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -61,6 +63,9 @@ public string CurrentDirectoryInfo

public List<FileViewModel> CurrentItems { get; set; } = new List<FileViewModel>();

public bool IsUnzip => CurrentItems.Count > 0 && CurrentItems[0].Extension == "zip";


public SortingBy Sorting
{
get => _sorting;
Expand Down Expand Up @@ -115,13 +120,15 @@ public FilesPaneViewModel(MainWindowViewModel mainVM)
ViewCommand = ReactiveCommand.Create(View);
EditCommand = ReactiveCommand.Create(Edit);
ZipCommand = ReactiveCommand.Create(Zip);
UnzipCommand = ReactiveCommand.Create(Unzip);
_mainVM = mainVM;
}

public ReactiveCommand<Unit, Unit>? EnterCommand { get; }
public ReactiveCommand<Unit, Unit>? ViewCommand { get; }
public ReactiveCommand<Unit, Unit>? EditCommand { get; }
public ReactiveCommand<Unit, Unit>? ZipCommand { get; }
public ReactiveCommand<Unit, Unit>? UnzipCommand { get; }

public void CellPointerPressed(object sender, object parameter)
{
Expand Down Expand Up @@ -317,6 +324,11 @@ public void Zip()
_mainVM.Zip();
}

public void Unzip()
{
_mainVM.Unzip();
}

private void LaunchProcess(string program, string argument)
{
var process = new Process();
Expand Down
50 changes: 43 additions & 7 deletions src/SmartCommander/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ public FilesPaneViewModel SelectedPane
}

public void Execute()
{
{
SelectedPane.Execute(CommandText);
CommandText = "";
CommandText = "";
}

public void View()
Expand All @@ -206,7 +206,7 @@ public void View()
return;
}
_F3Busy = true;
SelectedPane.View(F3Finished);
SelectedPane.View(F3Finished);
}

public void Edit()
Expand All @@ -216,17 +216,17 @@ public void Edit()
return;
}
_F4Busy = true;
SelectedPane.Edit(F4Finished);
SelectedPane.Edit(F4Finished);
}

public bool IsBackgroundOperation { get { return tokenSource != null && !tokenSource.IsDisposed; } }

public void Cancel()
{
if (tokenSource != null && !tokenSource.IsDisposed)
{
tokenSource.Cancel();
}
{
tokenSource.Cancel();
}
}

public async void Zip()
Expand All @@ -241,6 +241,42 @@ public async void Zip()
}
}

public async void Unzip()
{
if (SelectedPane.CurrentItems.Count < 1)
return;

using (tokenSource = new SmartCancellationTokenSource())
{
await Task.Run(() => UnzipAsync(tokenSource.Token));
SelectedPane.Update();
}

}
public void UnzipAsync(CancellationToken ct)
{
try
{
if (ct.IsCancellationRequested)
{
ct.ThrowIfCancellationRequested();
}
if (SelectedPane.CurrentItems.Count < 1)
return;
var destDir = Path.Combine(SelectedPane.CurrentDirectory, SelectedPane.CurrentItems[0].Name);
if (Directory.Exists(destDir))
{
MessageBox_Show(null, string.Format(Resources.DirectoryExists, destDir), Resources.Alert);
return;
}
_progress?.Report(0);
ZipFile.ExtractToDirectory(SelectedPane.CurrentItems[0].FullName, destDir);
_progress?.Report(100);

}
catch { }
}

public void ZipAsync(CancellationToken ct)
{
try
Expand Down
15 changes: 8 additions & 7 deletions src/SmartCommander/Views/FilesPane.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@
<Setter Property="IsVisible" Value="False" />
</Style>
</DataGrid.Styles>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="{x:Static assets:Resources.View}" Command="{Binding ViewCommand}"></MenuItem>
<MenuItem Header="{x:Static assets:Resources.Edit}" Command="{Binding EditCommand}"></MenuItem>
<MenuItem Header="{x:Static assets:Resources.Zip}" Command="{Binding ZipCommand}"></MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
<DataGrid.ContextFlyout>
<MenuFlyout>
<MenuItem Header="{x:Static assets:Resources.View}" Command="{Binding ViewCommand}"></MenuItem>
<MenuItem Header="{x:Static assets:Resources.Edit}" Command="{Binding EditCommand}"></MenuItem>
<MenuItem Header="{x:Static assets:Resources.Zip}" IsVisible="{Binding !IsUnzip, Mode=TwoWay}" Command="{Binding ZipCommand}"></MenuItem>
<MenuItem Header="{x:Static assets:Resources.Unzip}" IsVisible="{Binding IsUnzip, Mode=TwoWay}" Command="{Binding UnzipCommand}"></MenuItem>
</MenuFlyout>
</DataGrid.ContextFlyout>
<DataGrid.Columns>
<DataGridTemplateColumn Header="" Width="20" MaxWidth="20">
<DataGridTemplateColumn.CellTemplate>
Expand Down
Loading