Skip to content

Commit

Permalink
drag-and-drop for zip compressed log files. RolandPheasant#200
Browse files Browse the repository at this point in the history
  • Loading branch information
François-Karim Laben committed Mar 11, 2020
1 parent 0c0073e commit 29557cc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Source/TailBlazer/TailBlazer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Reactive.Core, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\packages\System.Reactive.Core.3.1.1\lib\net45\System.Reactive.Core.dll</HintPath>
</Reference>
Expand Down
10 changes: 6 additions & 4 deletions Source/TailBlazer/Views/Tail/TailViewModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public HeaderedView Create(ViewState state)
return new HeaderedView(new FileHeader(new FileInfo(file)), viewModel);
}

public HeaderedView Create(FileInfo fileInfo)
public HeaderedView Create(FileInfo fileInfo, bool autoTail = true)
{
var viewModel = CreateView(fileInfo);
var viewModel = CreateView(fileInfo, autoTail);
viewModel.ApplySettings();//apply default values
return new HeaderedView(new FileHeader(fileInfo), viewModel);
}

private TailViewModel CreateView(FileInfo fileInfo)
private TailViewModel CreateView(FileInfo fileInfo, bool autoTail = true)
{
if (fileInfo == null) throw new ArgumentNullException(nameof(fileInfo));

Expand All @@ -58,7 +58,9 @@ private TailViewModel CreateView(FileInfo fileInfo)
// new Argument<ICombinedSearchMetadataCollection>(combined)
};

return _objectProvider.Get<TailViewModel>(args);
var tailViewModel = _objectProvider.Get<TailViewModel>(args);
tailViewModel.AutoTail = autoTail;
return tailViewModel;
}

public string Key => TailViewModelConstants.ViewKey;
Expand Down
73 changes: 53 additions & 20 deletions Source/TailBlazer/Views/WindowManagement/WindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using TailBlazer.Views.Options;
using TailBlazer.Views.Recent;
using TailBlazer.Views.Tail;
using System.IO.Compression;

namespace TailBlazer.Views.WindowManagement
{
Expand Down Expand Up @@ -155,43 +156,75 @@ public void OpenFiles(IEnumerable<string> files=null)
if (files == null) return;

foreach (var file in files)
OpenFile(new FileInfo(file));
}
OpenFile(new FileInfo(file));
}

private void OpenFile(FileInfo file)
{
_schedulerProvider.Background.Schedule(() =>
{
_logger.Info($"Attempting to open '{file.FullName}'");
ICollection<FileInfo> filesToOpenFromUnzippedArchive = new LinkedList<FileInfo>();
try
{
_logger.Info($"Attempting to open '{file.FullName}'");

RecentFiles.Add(file);

//1. resolve TailViewModel
var factory = _objectProvider.Get<TailViewModelFactory>();
var newItem = factory.Create(file);

//2. Display it
_windowsController.Register(newItem);

_logger.Info($"Objects for '{file.FullName}' has been created.");
//do the work on the ui thread
_schedulerProvider.MainThread.Schedule(() =>
using (ZipArchive archive = ZipFile.OpenRead(file.FullName))
{
Views.Add(newItem);
_logger.Info($"Opened '{file.FullName}'");
Selected = newItem;
});
foreach (ZipArchiveEntry entry in archive.Entries)
{
var tempFileName = Path.GetTempFileName();
entry.ExtractToFile(tempFileName, true);
filesToOpenFromUnzippedArchive.Add(new FileInfo(tempFileName));
}
}

foreach (var tempFileInfo in filesToOpenFromUnzippedArchive)
{
OpenSingleFileInfo(tempFileInfo, false);
}
}
catch (Exception ex)
{
//TODO: Create a failed to load view
_logger.Error(ex, $"There was a problem opening '{file.FullName}'");
_logger.Error(ex, $"There was a problem opening '{file.FullName}': Probably not a zip archive file or not able to create/read temp file");
}

if (!filesToOpenFromUnzippedArchive.Any())
{
try
{
OpenSingleFileInfo(file);
}
catch (Exception ex)
{
//TODO: Create a failed to load view
_logger.Error(ex, $"There was a problem opening '{file.FullName}'");
}
}
});
}

private void OpenSingleFileInfo(FileInfo file, bool autoTail = true)
{
RecentFiles.Add(file);

//1. resolve TailViewModel
var factory = _objectProvider.Get<TailViewModelFactory>();
var newItem = factory.Create(file, autoTail);

//2. Display it
_windowsController.Register(newItem);

_logger.Info($"Objects for '{file.FullName}' has been created.");
//do the work on the ui thread
_schedulerProvider.MainThread.Schedule(() =>
{
Views.Add(newItem);
_logger.Info($"Opened '{file.FullName}'");
Selected = newItem;
});
}

//TODO: Abstract this
public void OpenView(HeaderedView headeredView)
{
Expand Down

0 comments on commit 29557cc

Please sign in to comment.