Skip to content

Commit

Permalink
Added more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RNoeldner committed May 2, 2023
1 parent d9cc0ce commit c603a81
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 110 deletions.
7 changes: 3 additions & 4 deletions Library/ClassLibraryCSV/CsvHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ FillGuessSettings fillGuessSettings
, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentException("File name can not be empty", nameof(fileName));

inspectionResult.FileName = fileName;
Logger.Information("Opening file");
throw new ArgumentException("File name can not be empty", nameof(fileName));

inspectionResult.FileName = fileName;
#if SupportPGP
var sourceAccess = new SourceAccess(fileName, privateKey: privateKey);
#else
Expand Down
28 changes: 18 additions & 10 deletions Library/ClassLibraryCSV/ImprovedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
*/
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Core;
Expand Down Expand Up @@ -456,7 +458,7 @@ private void OpenZGipOverBase()
cBufferSize);
}
}

private void OpenZipOverBase()
{
if (m_SourceAccess.Reading)
Expand Down Expand Up @@ -486,22 +488,28 @@ private void OpenZipOverBase()
var hasFile = false;
if (string.IsNullOrEmpty(m_SourceAccess.IdentifierInContainer))
{
var files = new List<ZipEntry>();
var entryEnumerator = m_ZipFile.GetEnumerator();
while (entryEnumerator.MoveNext())
{
var entry = entryEnumerator.Current as ZipEntry;
if (entry?.IsFile ?? false)
{
m_SourceAccess.IdentifierInContainer = entry.Name;
Logger.Debug(
"Unzipping {filename} {container}",
m_SourceAccess.Identifier,
m_SourceAccess.IdentifierInContainer);
AccessStream = m_ZipFile.GetInputStream(entry);
hasFile = true;
break;
files.Add(entry);
}
}
// get csv with highest priority
// get txt with second priority
// the by index in file
var bestEntry = files.OrderBy(x => x.ZipFileIndex +
(x.Name.EndsWith(".csv", StringComparison.OrdinalIgnoreCase) ? 0 :
x.Name.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) ? 500 :
1000)).First();

m_SourceAccess.IdentifierInContainer = bestEntry.Name;
Logger.Information("Using {container}", m_SourceAccess.IdentifierInContainer);
AccessStream = m_ZipFile.GetInputStream(bestEntry);
hasFile = true;
}
else
{
Expand All @@ -510,7 +518,7 @@ private void OpenZipOverBase()
throw new FileNotFoundException(
$"Could not find {m_SourceAccess.IdentifierInContainer} in {m_SourceAccess.Identifier}");

Logger.Debug("Unzipping {filename} {container}", m_SourceAccess.Identifier, m_SourceAccess.IdentifierInContainer);
Logger.Information("Using {container}", m_SourceAccess.IdentifierInContainer);
AccessStream = m_ZipFile.GetInputStream(entryIndex);
hasFile = true;
}
Expand Down
6 changes: 3 additions & 3 deletions Library/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
// Version information for an assembly consists of the following four values: Major Version Minor
// Version Build Number Revision You can specify all the values or you can default the Build and.Json
// Revision Numbers by using the '*' as shown below: [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.7.5.581")]
[assembly: AssemblyFileVersion("1.7.5.581")]
[assembly: AssemblyInformationalVersion("1.7.5.581")] // a.k.a. "Product version"
[assembly: AssemblyVersion("1.7.5.582")]
[assembly: AssemblyFileVersion("1.7.5.582")]
[assembly: AssemblyInformationalVersion("1.7.5.582")] // a.k.a. "Product version"
129 changes: 79 additions & 50 deletions Library/WinFormControls/FilteredDataGridView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public FilteredDataGridView()
FontChanged += PassOnFontChanges;
m_Filter = new List<ToolStripDataGridViewColumnFilter?>();

Scroll += (_,_) => SetRowHeight();
Scroll += (_, _) => SetRowHeight();
var resources = new ComponentResourceManager(typeof(FilteredDataGridView));
m_ImgFilterIndicator = (resources.GetObject("toolStripMenuItem2.Image") as Image) ??
throw new InvalidOperationException("Resource not found");
Expand Down Expand Up @@ -855,23 +855,29 @@ private void ContextMenuStripFilter_Opened(object? sender, EventArgs e)
/// </param>
private void FilteredDataGridView_CellMouseClick(object? sender, DataGridViewCellMouseEventArgs e)
{
SetToolStripMenu(e.ColumnIndex, e.RowIndex, e.Button);
if (e is { Button: MouseButtons.Left, RowIndex: >= 0 } && Columns[e.ColumnIndex] is DataGridViewButtonColumn)
try
{
using var frm = new FormTextDisplay(CurrentCell.Value?.ToString() ?? string.Empty);

// ReSharper disable once LocalizableElement
frm.Text = $"{Columns[e.ColumnIndex].DataPropertyName} - Row {e.RowIndex + 1:D}";
frm.SaveAction = s =>
SetToolStripMenu(e.ColumnIndex, e.RowIndex, e.Button);
if (e is { Button: MouseButtons.Left, RowIndex: >= 0 } && Columns[e.ColumnIndex] is DataGridViewButtonColumn)
{
if (s.Equals(CurrentCell.Value))
return;
CurrentCell.Value = s;
CurrentCell.ErrorText = CurrentCell.ErrorText.AddMessage(
"Value was modified".AddWarningId());
};
frm.ShowWithFont(this, true);
using var frm = new FormTextDisplay(CurrentCell.Value?.ToString() ?? string.Empty);

// ReSharper disable once LocalizableElement
frm.Text = $"{Columns[e.ColumnIndex].DataPropertyName} - Row {e.RowIndex + 1:D}";
frm.SaveAction = s =>
{
if (s.Equals(CurrentCell.Value))
return;
CurrentCell.Value = s;
CurrentCell.ErrorText = CurrentCell.ErrorText.AddMessage(
"Value was modified".AddWarningId());
};
frm.ShowWithFont(this, true);
}
}
catch (Exception ex)
{
Logger.Error(ex, "FilteredDataGridView: Mouse Click {columnIndex} {rowIndex} {button}", e.ColumnIndex, e.RowIndex, e.Button);
}
}

Expand Down Expand Up @@ -947,8 +953,7 @@ private void FilteredDataGridView_KeyDown(object? sender, KeyEventArgs e)
{
if (!e.Control || e.KeyCode != Keys.C)
return;
var html = new DataGridViewCopyPaste(HtmlStyle);
html.SelectedDataIntoClipboard(this, !e.Alt, e.Shift, m_CancellationTokenSource.Token);
Copy(!e.Alt, e.Shift);
e.Handled = true;
}

Expand Down Expand Up @@ -1288,13 +1293,20 @@ private void TimerColumnsFilter_Tick(object? sender, EventArgs e)
/// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
private void ToolStripMenuItemApply_Click(object? sender, EventArgs e)
{
if (m_Filter[m_MenuItemColumnIndex] != null)
try
{
if (m_Filter[m_MenuItemColumnIndex] != null)
m_Filter[m_MenuItemColumnIndex]!.ColumnFilterLogic.Active = true;

ApplyFilters();
contextMenuStripCell.Close();
contextMenuStripHeader.Close();
contextMenuStripFilter.Close();
}
catch (Exception ex)
{
Logger.Error(ex,"Apply Click");
}
}


Expand All @@ -1320,27 +1332,32 @@ private void ToolStripMenuItemCF_Click(object? sender, EventArgs e)
}
}

private void Copy(bool addErrorInfo, bool cutLength)
{
try
{
var html = new DataGridViewCopyPaste(HtmlStyle);
html.SelectedDataIntoClipboard(this, addErrorInfo, cutLength, m_CancellationTokenSource.Token);
}
catch (Exception ex)
{
Logger.Error(ex, "Issue during Copy");
}
}

/// <summary>
/// Handles the Click event of the toolStripMenuItemCopy control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private void ToolStripMenuItemCopy_Click(object? sender, EventArgs e)
{
var html = new DataGridViewCopyPaste(HtmlStyle);
html.SelectedDataIntoClipboard(this, false, false, m_CancellationTokenSource.Token);
}
private void ToolStripMenuItemCopy_Click(object? sender, EventArgs e) => Copy(false, false);

/// <summary>
/// Handles the Click event of the toolStripMenuItemCopyError control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private void ToolStripMenuItemCopyError_Click(object? sender, EventArgs e)
{
var html = new DataGridViewCopyPaste(HtmlStyle);
html.SelectedDataIntoClipboard(this, true, false, m_CancellationTokenSource.Token);
}
private void ToolStripMenuItemCopyError_Click(object? sender, EventArgs e) => Copy(true, false);

/// <summary>
/// Handles the Click event of the toolStripMenuItemFilled control.
Expand Down Expand Up @@ -1433,12 +1450,19 @@ private void ToolStripMenuItemLoadCol_Click(object? sender, EventArgs e)

public void SetViewStatus(string newSetting)
{
SuspendLayout();
if (ViewSetting.ReStoreViewSetting(newSetting, Columns, m_Filter, GetColumnFilter,
Sort))
ApplyFilters();
ColumnVisibilityChanged();
ResumeLayout(true);
try
{
SuspendLayout();
if (ViewSetting.ReStoreViewSetting(newSetting, Columns, m_Filter, GetColumnFilter,
Sort))
ApplyFilters();
ColumnVisibilityChanged();
ResumeLayout(true);
}
catch (Exception ex)
{
Logger.Error(ex, "SetViewStatus");
}
}

private async void ToolStripMenuItemSaveCol_Click(object? sender, EventArgs e)
Expand All @@ -1448,27 +1472,32 @@ private async void ToolStripMenuItemSaveCol_Click(object? sender, EventArgs e)
try
{
toolStripMenuItemSaveCol.Enabled = false;
// Select Path
var fileName = WindowsAPICodePackWrapper.Save(
m_FileSetting is IFileSettingPhysicalFile phy ? phy.FullPath.GetDirectoryName() : ".", "Save Column Setting",
"Column Config|*.col;*.conf|All files|*.*", ".col", false, DefFileNameColSetting(m_FileSetting, ".col"));
var text = GetViewStatus;
if (!string.IsNullOrEmpty(text))
{
// Select Path
var fileName = WindowsAPICodePackWrapper.Save(
m_FileSetting is IFileSettingPhysicalFile phy ? phy.FullPath.GetDirectoryName() : ".", "Save Column Setting",
"Column Config|*.col;*.conf|All files|*.*", ".col", false, DefFileNameColSetting(m_FileSetting, ".col"));

if (fileName is null || fileName.Length == 0)
return;

if (fileName is null || fileName.Length == 0)
return;
#if NET5_0_OR_GREATER
await
#endif
// ReSharper disable once UseAwaitUsing
using var stream = new ImprovedStream(new SourceAccess(fileName, false));
await
#endif
using var stream = new ImprovedStream(new SourceAccess(fileName, false));

#if NET5_0_OR_GREATER
await
await
#endif
using var writer = new StreamWriter(stream, Encoding.UTF8, 1024);
await writer.WriteAsync(GetViewStatus);
await writer.FlushAsync();
using var writer = new StreamWriter(stream, Encoding.UTF8, 1024);
await writer.WriteAsync(GetViewStatus);
await writer.FlushAsync();

if (m_FileSetting is BaseSettingPhysicalFile basePhysical)
basePhysical.ColumnFile = fileName;
if (m_FileSetting is BaseSettingPhysicalFile basePhysical)
basePhysical.ColumnFile = fileName;
}
}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit c603a81

Please sign in to comment.