implemented duplicate file actions dialog.

This commit is contained in:
morkt 2017-02-03 21:56:04 +04:00
parent 8213af356a
commit aedd41fde4
14 changed files with 216 additions and 91 deletions

View File

@ -6,13 +6,17 @@
Title="{Binding Title}" ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
ResizeMode="NoResize" SizeToContent="WidthAndHeight" ShowActivated="True"
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<StackPanel Orientation="Vertical">
<TextBox x:Name="ErrorText" Text="{Binding Text}" IsReadOnly="True" Background="Transparent" BorderThickness="0" Margin="10"/>
<Separator/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
<CheckBox x:Name="IgnoreErrors" Content="{x:Static s:guiStrings.LabelIgnoreErrors}" Margin="10" VerticalAlignment="Center"/>
<Button Content="{x:Static s:guiStrings.ButtonContinue}" Margin="10" Width="75" Height="25" IsDefault="True" Click="ContinueButton_Click"/>
<Button Content="{x:Static s:guiStrings.ButtonAbort}" Margin="10" Width="75" Height="25" IsCancel="True" Click="AbortButton_Click"/>
<StackPanel Orientation="Horizontal">
<Image Source="Images/64x64/alert.png" Width="64" Height="64" Margin="10"
SnapsToDevicePixels="True" VerticalAlignment="Top" RenderOptions.BitmapScalingMode="HighQuality"/>
<StackPanel Orientation="Vertical">
<TextBox x:Name="ErrorText" Text="{Binding Text}" IsReadOnly="True" Background="Transparent" BorderThickness="0" Margin="0,10,10,10"/>
<Separator/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
<CheckBox x:Name="IgnoreErrors" Content="{x:Static s:guiStrings.LabelIgnoreErrors}" Margin="0,10,10,10" VerticalAlignment="Center"/>
<Button Content="{x:Static s:guiStrings.ButtonContinue}" Margin="10" MinWidth="75" Height="25" IsDefault="True" Click="ContinueButton_Click"/>
<Button Content="{x:Static s:guiStrings.ButtonAbort}" Margin="10" MinWidth="75" Height="25" IsCancel="True" Click="AbortButton_Click"/>
</StackPanel>
</StackPanel>
</StackPanel>
<Window.InputBindings>

View File

@ -6,16 +6,20 @@
Title="File already exists" ShowInTaskbar="False" WindowStartupLocation="CenterOwner"
ResizeMode="NoResize" SizeToContent="WidthAndHeight" ShowActivated="True"
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="Notice" Text="File named {0} already exists in destination folder." Margin="10"/>
<TextBlock Text="{x:Static s:guiStrings.LabelDuplicateFileQuestion}" Margin="10"/>
<Separator/>
<CheckBox x:Name="ApplyToAll" Content="{x:Static s:guiStrings.LabelApplyToAll}" Margin="10"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
<Button Content="{x:Static s:guiStrings.ButtonSkip}" Margin="10" Width="75" Height="25" IsDefault="True" Click="SkipButton_Click"/>
<Button Content="{x:Static s:guiStrings.ButtonOverwrite}" Margin="10" Width="75" Height="25" Click="OverwriteButton_Click"/>
<Button Content="{x:Static s:guiStrings.ButtonRename}" Margin="10" Width="75" Height="25" Click="RenameButton_Click"/>
<Button Content="{x:Static s:guiStrings.ButtonAbort}" Margin="10" Width="75" Height="25" IsCancel="True" Click="AbortButton_Click"/>
<StackPanel Orientation="Horizontal">
<Image Source="Images/64x64/actions.png" Width="64" Height="64" Margin="10"
SnapsToDevicePixels="True" VerticalAlignment="Top" RenderOptions.BitmapScalingMode="HighQuality"/>
<StackPanel Orientation="Vertical">
<TextBlock x:Name="Notice" Text="File named {0} already exists in destination folder." Margin="0,10,10,10"/>
<TextBlock Text="{x:Static s:guiStrings.LabelDuplicateFileQuestion}" Margin="0,0,10,10"/>
<Separator/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
<Button Content="{x:Static s:guiStrings.ButtonSkip}" Margin="0,10,10,10" MinWidth="75" Height="25" IsDefault="True" Click="SkipButton_Click"/>
<Button Content="{x:Static s:guiStrings.ButtonOverwrite}" Margin="10" MinWidth="75" Height="25" Click="OverwriteButton_Click"/>
<Button Content="{x:Static s:guiStrings.ButtonRename}" Margin="10" MinWidth="75" Height="25" Click="RenameButton_Click"/>
<Button Content="{x:Static s:guiStrings.ButtonAbort}" Margin="10" MinWidth="75" Height="25" IsCancel="True" Click="AbortButton_Click"/>
</StackPanel>
<CheckBox x:Name="ApplyToAll" Content="{x:Static s:guiStrings.LabelApplyToAll}" Margin="0,10,10,10"/>
</StackPanel>
</StackPanel>
</w:ModalWindow>

View File

@ -153,6 +153,7 @@
<Compile Include="GarConvert.cs" />
<Compile Include="GarCreate.cs" />
<Compile Include="GarExtract.cs" />
<Compile Include="GarOperation.cs" />
<Compile Include="HistoryStack.cs" />
<Compile Include="ImagePreview.cs" />
<Compile Include="ListViewEx.cs" />
@ -299,6 +300,12 @@
<ItemGroup>
<Resource Include="Images\Cursors\grabbing.cur" />
</ItemGroup>
<ItemGroup>
<Resource Include="Images\64x64\actions.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="Images\64x64\alert.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>perl "$(SolutionDir)inc-revision.pl" "$(ProjectPath)" $(ConfigurationName) "$(SolutionDir)\"

View File

@ -106,21 +106,17 @@ namespace GARbro.GUI
}
}
internal class GarConvertMedia
internal class GarConvertMedia : GarOperation
{
private MainWindow m_main;
private ProgressDialog m_progress_dialog;
private IEnumerable<Entry> m_source;
private ImageFormat m_image_format;
private Exception m_pending_error;
private List<Tuple<string,string>> m_failed = new List<Tuple<string,string>>();
public bool IgnoreErrors { get; set; }
public IEnumerable<Tuple<string,string>> FailedFiles { get { return m_failed; } }
public GarConvertMedia (MainWindow parent)
public GarConvertMedia (MainWindow parent) : base (parent, guiStrings.TextMediaConvertError)
{
m_main = parent;
}
public void Convert (IEnumerable<Entry> images, ImageFormat format)
@ -163,12 +159,21 @@ namespace GARbro.GUI
else if ("audio" == entry.Type)
ConvertAudio (filename);
}
catch (SkipExistingFileException)
{
continue;
}
catch (OperationCanceledException X)
{
m_pending_error = X;
break;
}
catch (Exception X)
{
if (!IgnoreErrors)
{
var error_text = string.Format (guiStrings.TextErrorConverting, entry.Name, X.Message);
var result = m_main.Dispatcher.Invoke (() => m_main.ShowErrorDialog (guiStrings.TextMediaConvertError, error_text, m_progress_dialog.GetWindowHandle()));
var result = ShowErrorDialog (error_text);
if (!result.Continue)
break;
IgnoreErrors = result.IgnoreErrors;
@ -227,41 +232,20 @@ namespace GARbro.GUI
return;
file.Position = 0;
var image = src_format.Item1.Read (file, src_format.Item2);
var output = CreateNewFile (target_name);
try
{
using (var output = CreateNewFile (target_name))
m_image_format.Write (output, image);
m_image_format.Write (output, image);
}
catch // delete destination file on conversion failure
{
// FIXME if user chooses to overwrite file, and conversion results in error,
// then original file will be lost.
output.Dispose();
File.Delete (target_name);
throw;
}
}
}
/// <summary>
/// Creates new file with specified filename, or, if it's already exists, tries to open
/// files named "FILENAME.1.EXT", "FILENAME.2.EXT" and so on.
/// <exception cref="System.IOException">Throws exception after 100th failed attempt.</exception>
/// </summary>
public static Stream CreateNewFile (string filename)
{
string name = filename;
var ext = new Lazy<string> (() => Path.GetExtension (filename));
for (int attempt = 1; ; ++attempt)
{
try
{
return File.Open (name, FileMode.CreateNew);
}
catch (IOException) // file already exists
{
if (100 == attempt) // limit number of attempts
throw;
}
name = Path.ChangeExtension (filename, attempt.ToString()+ext.Value);
output.Dispose();
}
}

View File

@ -56,21 +56,17 @@ namespace GARbro.GUI
}
}
internal class GarCreate
internal class GarCreate : GarOperation
{
private MainWindow m_main;
private string m_arc_name;
private IList<Entry> m_file_list;
private ProgressDialog m_progress_dialog;
private ArchiveFormat m_format;
private ResourceOptions m_options;
private Exception m_pending_error;
delegate void AddFilesEnumerator (IList<Entry> list, string path, DirectoryInfo path_info);
public GarCreate (MainWindow parent)
public GarCreate (MainWindow parent) : base (parent, guiStrings.TextCreateArchiveError)
{
m_main = parent;
m_arc_name = Settings.Default.appLastCreatedArchive;
}

View File

@ -102,9 +102,8 @@ namespace GARbro.GUI
}
}
sealed internal class GarExtract : IDisposable
sealed internal class GarExtract : GarOperation, IDisposable
{
private MainWindow m_main;
private string m_arc_name;
private ArchiveFileSystem m_fs;
private readonly bool m_should_ascend;
@ -113,16 +112,14 @@ namespace GARbro.GUI
private bool m_skip_audio = false;
private bool m_adjust_image_offset = false;
private bool m_convert_audio;
private bool m_ignore_errors;
private ImageFormat m_image_format;
private int m_extract_count;
private int m_skip_count;
private bool m_extract_in_progress = false;
private ProgressDialog m_progress_dialog;
public bool IsActive { get { return m_extract_in_progress; } }
public GarExtract (MainWindow parent, string source)
public GarExtract (MainWindow parent, string source) : base (parent, guiStrings.TextExtractionError)
{
m_arc_name = Path.GetFileName (source);
try
@ -135,15 +132,13 @@ namespace GARbro.GUI
throw new OperationCanceledException (string.Format ("{1}: {0}", X.Message, m_arc_name));
}
m_fs = VFS.Top as ArchiveFileSystem;
m_main = parent;
}
public GarExtract (MainWindow parent, string source, ArchiveFileSystem fs)
public GarExtract (MainWindow parent, string source, ArchiveFileSystem fs) : base (parent, guiStrings.TextExtractionError)
{
if (null == fs)
throw new UnknownFormatException();
m_fs = fs;
m_main = parent;
m_arc_name = Path.GetFileName (source);
m_should_ascend = false;
}
@ -282,7 +277,7 @@ namespace GARbro.GUI
var arc = m_fs.Source;
int total = file_list.Count();
int progress_count = 0;
m_ignore_errors = false;
bool ignore_errors = false;
foreach (var entry in file_list)
{
if (m_progress_dialog.CancellationPending)
@ -299,15 +294,24 @@ namespace GARbro.GUI
arc.Extract (entry);
++m_extract_count;
}
catch (SkipExistingFileException)
{
++m_skip_count;
continue;
}
catch (OperationCanceledException)
{
break;
}
catch (Exception X)
{
if (!m_ignore_errors)
if (!ignore_errors)
{
var error_text = string.Format (guiStrings.TextErrorExtracting, entry.Name, X.Message);
var result = m_main.Dispatcher.Invoke (() => m_main.ShowErrorDialog (guiStrings.TextExtractionError, error_text, m_progress_dialog.GetWindowHandle()));
var result = ShowErrorDialog (error_text);
if (!result.Continue)
break;
m_ignore_errors = result.IgnoreErrors;
ignore_errors = result.IgnoreErrors;
}
++m_skip_count;
}
@ -320,11 +324,12 @@ namespace GARbro.GUI
{
var src_format = decoder.SourceFormat; // could be null
string target_ext = target_format.Extensions.FirstOrDefault() ?? "";
string outname = FindUniqueFileName (entry.Name, target_ext);
string outname = Path.ChangeExtension (entry.Name, target_ext);
outname = ArchiveFormat.CreatePath (outname);
if (src_format == target_format)
{
// source format is the same as a target, copy file as is
using (var output = ArchiveFormat.CreateFile (outname))
using (var output = CreateNewFile (outname))
decoder.Source.CopyTo (output);
return;
}
@ -333,7 +338,7 @@ namespace GARbro.GUI
{
image = AdjustImageOffset (image);
}
using (var outfile = ArchiveFormat.CreateFile (outname))
using (var outfile = CreateNewFile (outname))
{
target_format.Write (outfile, image);
}
@ -369,7 +374,7 @@ namespace GARbro.GUI
return new ImageData (bitmap);
}
static void ExtractAudio (ArcFile arc, Entry entry)
void ExtractAudio (ArcFile arc, Entry entry)
{
using (var file = arc.OpenBinaryEntry (entry))
using (var sound = AudioFormat.Read (file))
@ -380,13 +385,14 @@ namespace GARbro.GUI
}
}
public static void ConvertAudio (string entry_name, SoundInput input)
public void ConvertAudio (string filename, SoundInput input)
{
filename = ArchiveFormat.CreatePath (filename);
string source_format = input.SourceFormat;
if (GarConvertMedia.CommonAudioFormats.Contains (source_format))
{
string output_name = FindUniqueFileName (entry_name, source_format);
using (var output = ArchiveFormat.CreateFile (output_name))
var output_name = Path.ChangeExtension (filename, source_format);
using (var output = CreateNewFile (output_name))
{
input.Source.Position = 0;
input.Source.CopyTo (output);
@ -394,25 +400,12 @@ namespace GARbro.GUI
}
else
{
string output_name = FindUniqueFileName (entry_name, "wav");
using (var output = ArchiveFormat.CreateFile (output_name))
var output_name = Path.ChangeExtension (filename, "wav");
using (var output = CreateNewFile (output_name))
AudioFormat.Wav.Write (input, output);
}
}
public static string FindUniqueFileName (string source_filename, string target_ext)
{
string ext = target_ext;
for (int attempt = 1; attempt < 100; ++attempt)
{
string filename = Path.ChangeExtension (source_filename, ext);
if (!File.Exists (filename))
return filename;
ext = string.Format ("{0}.{1}", attempt, target_ext);
}
throw new IOException ("File aready exists");
}
void OnExtractComplete (object sender, RunWorkerCompletedEventArgs e)
{
m_extract_in_progress = false;

115
GUI/GarOperation.cs Normal file
View File

@ -0,0 +1,115 @@
//! \file GarOperation.cs
//! \date Fri Feb 03 19:06:52 2017
//! \brief Base class for GARbro file operation.
//
// Copyright (C) 2017 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.IO;
using GARbro.GUI.Strings;
namespace GARbro.GUI
{
internal class GarOperation
{
internal MainWindow m_main;
internal ProgressDialog m_progress_dialog;
internal Exception m_pending_error;
internal FileExistsDialogResult m_duplicate_action;
internal string m_title;
const int MaxRenameAttempts = 100;
protected GarOperation (MainWindow parent, string dialog_title)
{
m_main = parent;
m_title = dialog_title;
}
protected Stream CreateNewFile (string filename)
{
FileMode open_mode = FileMode.CreateNew;
if (m_duplicate_action.ApplyToAll &&
m_duplicate_action.Action == ExistingFileAction.Overwrite)
open_mode = FileMode.Create;
try
{
return File.Open (filename, open_mode);
}
catch (IOException) // file already exists?
{
if (!File.Exists (filename) || FileMode.Create == open_mode) // some unforseen I/O error, give up
throw;
}
if (!m_duplicate_action.ApplyToAll)
{
var msg_text = string.Format (guiStrings.TextFileAlreadyExists, Path.GetFileName (filename));
m_duplicate_action = m_main.Dispatcher.Invoke (() => m_main.ShowFileExistsDialog (m_title, msg_text, m_progress_dialog.GetWindowHandle()));
}
switch (m_duplicate_action.Action)
{
default:
case ExistingFileAction.Abort:
throw new OperationCanceledException();
case ExistingFileAction.Skip:
throw new SkipExistingFileException();
case ExistingFileAction.Rename:
return CreateRenamedFile (filename);
case ExistingFileAction.Overwrite:
return File.Open (filename, FileMode.Create);
}
}
/// <summary>
/// Creates new file with specified filename, or, if it's already exists, tries to open
/// files named "FILENAME.1.EXT", "FILENAME.2.EXT" and so on.
/// <exception cref="System.IOException">Throws exception after 100th failed attempt.</exception>
/// </summary>
public static Stream CreateRenamedFile (string filename)
{
var ext = Path.GetExtension (filename);
for (int attempt = 1; ; ++attempt)
{
var name = Path.ChangeExtension (filename, attempt.ToString()+ext);
try
{
return File.Open (name, FileMode.CreateNew);
}
catch (IOException) // file already exists
{
if (MaxRenameAttempts == attempt) // limit number of attempts
throw;
}
}
}
protected FileErrorDialogResult ShowErrorDialog (string error_text)
{
return m_main.Dispatcher.Invoke (() => m_main.ShowErrorDialog (m_title, error_text, m_progress_dialog.GetWindowHandle()));
}
}
internal class SkipExistingFileException : ApplicationException
{
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

BIN
GUI/Images/64x64/alert.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

@ -1075,6 +1075,15 @@ namespace GARbro.GUI.Strings {
}
}
/// <summary>
/// Looks up a localized string similar to File {0} already exists in the destination folder..
/// </summary>
public static string TextFileAlreadyExists {
get {
return ResourceManager.GetString("TextFileAlreadyExists", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Multimedia conversion error.
/// </summary>

View File

@ -496,4 +496,8 @@
{1}</value>
<comment>translation pending</comment>
</data>
<data name="TextFileAlreadyExists" xml:space="preserve">
<value>File {0} already exists in the destination folder.</value>
<comment>translation pending</comment>
</data>
</root>

View File

@ -494,4 +494,7 @@ Overwrite?</value>
{0}
{1}</value>
</data>
<data name="TextFileAlreadyExists" xml:space="preserve">
<value>File {0} already exists in the destination folder.</value>
</data>
</root>

View File

@ -515,4 +515,7 @@
{0}
{1}</value>
</data>
<data name="TextFileAlreadyExists" xml:space="preserve">
<value>Файл с именем {0} уже существует.</value>
</data>
</root>

View File

@ -502,4 +502,7 @@
{1}</value>
<comment>translation pending</comment>
</data>
<data name="TextFileAlreadyExists" xml:space="preserve">
<value>文件{0}已经存在。</value>
</data>
</root>