Skip to content

Commit

Permalink
[GUI] Replace file dialogs with file pickers.
Browse files Browse the repository at this point in the history
  • Loading branch information
claunia committed May 1, 2024
1 parent 5d4467c commit 81de9bc
Show file tree
Hide file tree
Showing 20 changed files with 370 additions and 449 deletions.
82 changes: 82 additions & 0 deletions Aaru.Gui/FilePickerFileTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : FilePickerFileTypes.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI.
//
// --[ Description ] ----------------------------------------------------------
//
// Common file types to use with Avalonia file pickers.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/

using Aaru.Localization;
using Avalonia.Platform.Storage;

namespace Aaru.Gui;

/// <summary>
/// Dictionary of well known file types.
/// </summary>
public static class FilePickerFileTypes
{
public static FilePickerFileType All { get; } = new(UI.Dialog_All_files)
{
Patterns = ["*.*"],
MimeTypes = ["*/*"]
};

public static FilePickerFileType PlainText { get; } = new(UI.Dialog_Text_files)
{
Patterns = ["*.txt"],
AppleUniformTypeIdentifiers = ["public.plain-text"],
MimeTypes = ["text/plain"]
};

public static FilePickerFileType Log { get; } = new(UI.Dialog_Log_files)
{
Patterns = ["*.log"],
AppleUniformTypeIdentifiers = ["public.plain-text"],
MimeTypes = ["text/plain"]
};

public static FilePickerFileType Binary { get; } = new(UI.Dialog_Binary_files)
{
Patterns = ["*.bin"],
MimeTypes = ["application/octet-stream"]
};

public static FilePickerFileType AaruMetadata { get; } = new(UI.Dialog_Aaru_Metadata)
{
Patterns = ["*.json"],
AppleUniformTypeIdentifiers = ["public.json"],
MimeTypes = ["application/json"]
};

public static FilePickerFileType AaruResumeFile { get; } = new(UI.Dialog_Aaru_Resume)
{
Patterns = ["*.json"],
AppleUniformTypeIdentifiers = ["public.json"],
MimeTypes = ["application/json"]
};
}
23 changes: 8 additions & 15 deletions Aaru.Gui/ViewModels/Dialogs/ConsoleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// ****************************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reactive;
Expand All @@ -39,7 +40,7 @@
using Aaru.CommonTypes.Interop;
using Aaru.Console;
using Aaru.Localization;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using JetBrains.Annotations;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
Expand Down Expand Up @@ -94,27 +95,19 @@ public bool DebugChecked

async Task ExecuteSaveCommand()
{
var dlgSave = new SaveFileDialog();

dlgSave.Filters?.Add(new FileDialogFilter
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Extensions =
[
..new[]
{
"log"
}
],
Name = UI.Dialog_Log_files
FileTypeChoices = new List<FilePickerFileType>
{
FilePickerFileTypes.Log
}
});

string result = await dlgSave.ShowAsync(_view);

if(result is null) return;

try
{
var logFs = new FileStream(result, FileMode.Create, FileAccess.ReadWrite);
var logFs = new FileStream(result.Path.AbsolutePath, FileMode.Create, FileAccess.ReadWrite);
var logSw = new StreamWriter(logFs);

logSw.WriteLine(UI.Log_saved_at_0, DateTime.Now);
Expand Down
22 changes: 8 additions & 14 deletions Aaru.Gui/ViewModels/Panels/DeviceInfoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// ****************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Reactive;
using System.Threading.Tasks;
Expand All @@ -40,6 +41,7 @@
using Aaru.Gui.Views.Tabs;
using Aaru.Localization;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Humanizer;
using Humanizer.Localisation;
using ReactiveUI;
Expand Down Expand Up @@ -1018,25 +1020,17 @@ public SdMmcInfo SdMmcInfo

async Task ExecuteSaveUsbDescriptorsCommand()
{
var dlgSaveBinary = new SaveFileDialog();

dlgSaveBinary.Filters?.Add(new FileDialogFilter
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Extensions =
[
..new[]
{
"*.bin"
}
],
Name = UI.Dialog_Binary_files
FileTypeChoices = new List<FilePickerFileType>
{
FilePickerFileTypes.Binary
}
});

string result = await dlgSaveBinary.ShowAsync(_view);

if(result is null) return;

var saveFs = new FileStream(result, FileMode.Create);
var saveFs = new FileStream(result.Path.AbsolutePath, FileMode.Create);
saveFs.Write(_devInfo.UsbDescriptors, 0, _devInfo.UsbDescriptors.Length);

saveFs.Close();
Expand Down
22 changes: 8 additions & 14 deletions Aaru.Gui/ViewModels/Panels/MediaInfoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// ****************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Reactive;
using System.Text;
Expand All @@ -43,6 +44,7 @@
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Platform.Storage;
using Humanizer.Bytes;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
Expand Down Expand Up @@ -388,25 +390,17 @@ public BlurayInfo BlurayInfo

async Task SaveElement(byte[] data)
{
var dlgSaveBinary = new SaveFileDialog();

dlgSaveBinary.Filters?.Add(new FileDialogFilter
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Extensions =
[
..new[]
{
"*.bin"
}
],
Name = UI.Dialog_Binary_files
FileTypeChoices = new List<FilePickerFileType>
{
FilePickerFileTypes.Binary
}
});

string result = await dlgSaveBinary.ShowAsync(_view);

if(result is null) return;

var saveFs = new FileStream(result, FileMode.Create);
var saveFs = new FileStream(result.Path.AbsolutePath, FileMode.Create);
saveFs.Write(data, 0, data.Length);

saveFs.Close();
Expand Down
17 changes: 9 additions & 8 deletions Aaru.Gui/ViewModels/Panels/SubdirectoryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
using Aaru.Gui.Models;
using Aaru.Localization;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using JetBrains.Annotations;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
Expand Down Expand Up @@ -141,18 +142,18 @@ async Task ExecuteExtractFilesCommand()
{
if(SelectedEntries.Count == 0) return;

var saveFilesFolderDialog = new OpenFolderDialog
{
Title = UI.Dialog_Choose_destination_folder
};

string result = await saveFilesFolderDialog.ShowAsync(_view);
IReadOnlyList<IStorageFolder> result =
await _view.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = UI.Dialog_Choose_destination_folder,
AllowMultiple = false
});

if(result is null) return;
if(result.Count != 1) return;

Statistics.AddCommand("extract-files");

string folder = saveFilesFolderDialog.Directory;
string folder = result[0].Path.AbsolutePath;

foreach(FileModel file in SelectedEntries)
{
Expand Down
42 changes: 14 additions & 28 deletions Aaru.Gui/ViewModels/Tabs/AtaInfoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/

using System.Collections.Generic;
using System.IO;
using System.Reactive;
using System.Threading.Tasks;
using Aaru.Decoders.ATA;
using Aaru.Localization;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using JetBrains.Annotations;
using ReactiveUI;

Expand Down Expand Up @@ -112,25 +114,17 @@ public sealed class AtaInfoViewModel : ViewModelBase

async Task ExecuteSaveAtaBinaryCommand()
{
var dlgSaveBinary = new SaveFileDialog();

dlgSaveBinary.Filters?.Add(new FileDialogFilter
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Extensions =
[
..new[]
{
"*.bin"
}
],
Name = UI.Dialog_Binary_files
FileTypeChoices = new List<FilePickerFileType>
{
FilePickerFileTypes.Binary
}
});

string result = await dlgSaveBinary.ShowAsync(_view);

if(result is null) return;

var saveFs = new FileStream(result, FileMode.Create);
var saveFs = new FileStream(result.Path.AbsolutePath, FileMode.Create);

if(_ata != null)
saveFs.Write(_ata, 0, _ata.Length);
Expand All @@ -141,25 +135,17 @@ async Task ExecuteSaveAtaBinaryCommand()

async Task ExecuteSaveAtaTextCommand()
{
var dlgSaveText = new SaveFileDialog();

dlgSaveText.Filters?.Add(new FileDialogFilter
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Extensions =
[
..new[]
{
"*.txt"
}
],
Name = UI.Dialog_Text_files
FileTypeChoices = new List<FilePickerFileType>
{
FilePickerFileTypes.PlainText
}
});

string result = await dlgSaveText.ShowAsync(_view);

if(result is null) return;

var saveFs = new FileStream(result, FileMode.Create);
var saveFs = new FileStream(result.Path.AbsolutePath, FileMode.Create);
var saveSw = new StreamWriter(saveFs);
await saveSw.WriteAsync(AtaIdentifyText);
saveFs.Close();
Expand Down
22 changes: 8 additions & 14 deletions Aaru.Gui/ViewModels/Tabs/BlurayInfoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/

using System.Collections.Generic;
using System.IO;
using System.Reactive;
using System.Threading.Tasks;
using Aaru.Decoders.Bluray;
using Aaru.Decoders.SCSI.MMC;
using Aaru.Localization;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using JetBrains.Annotations;
using ReactiveUI;

Expand Down Expand Up @@ -174,25 +176,17 @@ public sealed class BlurayInfoViewModel

async Task SaveElement(byte[] data)
{
var dlgSaveBinary = new SaveFileDialog();

dlgSaveBinary.Filters?.Add(new FileDialogFilter
IStorageFile result = await _view.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Extensions =
[
..new[]
{
"*.bin"
}
],
Name = UI.Dialog_Binary_files
FileTypeChoices = new List<FilePickerFileType>
{
FilePickerFileTypes.Binary
}
});

string result = await dlgSaveBinary.ShowAsync(_view);

if(result is null) return;

var saveFs = new FileStream(result, FileMode.Create);
var saveFs = new FileStream(result.Path.AbsolutePath, FileMode.Create);
saveFs.Write(data, 0, data.Length);

saveFs.Close();
Expand Down
Loading

0 comments on commit 81de9bc

Please sign in to comment.