Skip to content

Commit

Permalink
v1.2 - Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Radioo committed Mar 24, 2022
1 parent eed41bb commit 9496697
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 35 deletions.
28 changes: 28 additions & 0 deletions WPFLinkTool/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace WPFLinkTool
{
public class Logger
{
public ObservableCollection<string> LogEntries;
public ScrollViewer Scroller;

public Logger(ScrollViewer scroller)
{
LogEntries = new();
Scroller = scroller;
}

public void Log(string message)
{
LogEntries.Add(DateTime.Now.ToString() + " " + message);
Scroller.ScrollToBottom();
}
}
}
12 changes: 10 additions & 2 deletions WPFLinkTool/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<RowDefinition MaxHeight="24" MinHeight="24"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition MaxHeight="24" MinHeight="24"></RowDefinition>
<RowDefinition MaxHeight="16" MinHeight="16"></RowDefinition>
<RowDefinition Height="0.3*"></RowDefinition>
</Grid.RowDefinitions>

<Grid>
Expand Down Expand Up @@ -53,7 +53,15 @@
</DataGrid>
<ProgressBar Grid.Row="3" Value="{Binding Progress}" MinHeight="20"></ProgressBar>

<TextBlock Grid.Row="4" Text="{Binding HeaderText}"></TextBlock>
<ScrollViewer x:Name="Scroller" Grid.Row="4">
<ItemsControl Grid.Row="1" ItemsSource="{Binding LogEntries}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Mode=OneWay}" TextWrapping="Wrap" Background="Transparent" BorderThickness="0" IsReadOnly="True"></TextBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>

</Grid>
</Border>
Expand Down
2 changes: 1 addition & 1 deletion WPFLinkTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public MainWindow()
{
InitializeComponent();
SharedViewModel shared = new();
DataContext = new MainWindowViewModel(shared);
DataContext = new MainWindowViewModel(shared, Scroller);
}
}
}
2 changes: 2 additions & 0 deletions WPFLinkTool/ViewModel/Commands/DeleteUnusedCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected override async Task ExecuteAsync(object parameter)
foreach (var file in dBFiles)
{
await Task.Run(() => File.Delete(dbDir + @"\" + file));
vm.Loggerr.Log($"Deleting {file}");
vm.Progress += progressStep;
}
}
Expand All @@ -58,6 +59,7 @@ protected override async Task ExecuteAsync(object parameter)
vm.UIEnabled = true;
vm.LinkButtonEnabled = true;
IsExecuting = false;
vm.Loggerr.Log("Delete operation finished.");
}
}
}
63 changes: 35 additions & 28 deletions WPFLinkTool/ViewModel/Commands/MakeHardLinksCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace WPFLinkTool
public class MakeHardLinksCommand : AsyncCommandBase
{
private readonly MainWindowViewModel vm;
private bool proceed = false;
private bool delet = false;
private string target;

Expand All @@ -25,70 +24,54 @@ protected override async Task ExecuteAsync(object parameter)
IsExecuting = true;
vm.UIEnabled = false;
vm.LinkButtonEnabled = false;
DataInstance selectedInstance = vm.SelectedInstance;


var prompt = MessageBox.Show($"Creating hard links from {selectedInstance.InstanceName}.\nProceed to link target selection?", "Make hard links", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
var prompt = MessageBox.Show($"Creating hard links from {vm.SelectedInstance.InstanceName}.\nProceed to link target selection?", "Make hard links", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (prompt == DialogResult.Yes)
{
FolderBrowserDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
target = dialog.SelectedPath;

foreach (var file in selectedInstance.InstanceFiles)
{
if (File.Exists(target + file.Location + @"\" + file.OriginalFileName))
{
var prompt2 = MessageBox.Show(@$"{file.Location}\{file.OriginalFileName} already exists in the target directory!" + "\n"
+ "Do you want to delete all encountered dupes before making hard links? (If you didn't know or forgot, overwriting hard linked files changes the originals) 'No' cancels the whole operation.", "Duplicate files detected!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
vm.Loggerr.Log("Checking for duplicates...");

if (prompt2 == DialogResult.Yes)
{
proceed = true;
delet = true;
break;
}
break;
}
else
{
proceed = true;
}
}
bool proceed = await Task.Run(() => CheckDupes());

if (delet)
{
foreach (var file in selectedInstance.InstanceFiles)
foreach (var file in vm.SelectedInstance.InstanceFiles)
{
if (File.Exists(target + file.Location + @"\" + file.OriginalFileName))
{
vm.Loggerr.Log(@$"Deleting {target + file.Location + @"\" + file.OriginalFileName}");
File.Delete(target + file.Location + @"\" + file.OriginalFileName);
}
}
}

if (proceed)
{
var uniqueFolders = (from folders in selectedInstance.InstanceFiles
var uniqueFolders = (from folders in vm.SelectedInstance.InstanceFiles
where folders.Location != string.Empty
select folders.Location).Distinct();

double progressStep = 100D / (selectedInstance.InstanceFiles.Count + uniqueFolders.Count());
double progressStep = 100D / (vm.SelectedInstance.InstanceFiles.Count + uniqueFolders.Count());

foreach (var folder in uniqueFolders)
{
if (!Directory.Exists(target + folder))
{
vm.Loggerr.Log($@"Creating directory {target + folder}");
await Task.Run(() => Directory.CreateDirectory(target + folder));
}
vm.Progress += progressStep;
}

foreach (var file in selectedInstance.InstanceFiles)
foreach (var file in vm.SelectedInstance.InstanceFiles)
{
if (!File.Exists(target + file.Location + @"\" + file.OriginalFileName))
{
vm.Loggerr.Log($@"Linking {target + file.Location + @"\" + file.OriginalFileName}");
await Task.Run(() => CreateHardLink(target + file.Location + @"\" + file.OriginalFileName,
dbDir + @"\" + file.HashedFileName, IntPtr.Zero));
}
Expand All @@ -101,13 +84,37 @@ await Task.Run(() => CreateHardLink(target + file.Location + @"\" + file.Origina

vm.Progress = 0;


vm.Loggerr.Log("Link operation finished");

vm.UIEnabled = true;
vm.LinkButtonEnabled = true;
IsExecuting = false;
}

bool CheckDupes()
{
foreach (var file in vm.SelectedInstance.InstanceFiles)
{
if (File.Exists(target + file.Location + @"\" + file.OriginalFileName))
{
var prompt2 = MessageBox.Show(@$"{file.Location}\{file.OriginalFileName} already exists in the target directory!" + "\n"
+ "Do you want to delete all encountered dupes before making hard links? (If you didn't know or forgot, overwriting hard linked files changes the originals). Nothing was linked yet. 'No' cancels the whole operation.", "Duplicate files detected!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

if (prompt2 == DialogResult.Yes)
{
delet = true;
return true;
}
else
{
return false;
}
}
}
return true;
}


[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
static extern bool CreateHardLink(
string lpFileName,
Expand Down
33 changes: 29 additions & 4 deletions WPFLinkTool/ViewModel/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Xml;
Expand All @@ -18,6 +19,22 @@ public class MainWindowViewModel : ViewModelBase
{
public SharedViewModel Shared;

public Logger Loggerr;

public ObservableCollection<string> LogEntries
{
get
{
return _logEntries;
}
set
{
_logEntries = value;
OnPropertyChanged(nameof(LogEntries));
}
}
private ObservableCollection<string> _logEntries;

private string _dBSize;
public string DBSize
{
Expand Down Expand Up @@ -130,18 +147,25 @@ public ObservableCollection<DataInstance> InstanceList
public ICommand MakeHardLinksCommand { get; private set; }
public ICommand DeleteUnusedCommand { get; private set; }

public MainWindowViewModel(SharedViewModel shared)
public ScrollViewer Scroller;

public MainWindowViewModel(SharedViewModel shared, ScrollViewer scroller)
{
if (!Directory.Exists(dbDir))
Directory.CreateDirectory(dbDir);
Shared = shared;
Scroller = scroller;
Loggerr = new(Scroller);
OpenAddInstanceWindow = new(OpenInstanceWindow);
DeleteInstanceCommand = new(DeleteInstance);
MakeHardLinksCommand = new MakeHardLinksCommand(this, (ex) => HeaderText = ex.Message);
DeleteUnusedCommand = new DeleteUnusedCommand(this, (ex) => HeaderText = ex.Message);
MakeHardLinksCommand = new MakeHardLinksCommand(this, (ex) => Loggerr.Log(ex.Message));
DeleteUnusedCommand = new DeleteUnusedCommand(this, (ex) => Loggerr.Log(ex.Message));
LoadDBInfo();
Task.Run(() => UpdateDBSize());
Task.Run(() => UpdateSumSize());
//HeaderText = "Loading finished, probably... Have a nice day!";
LogEntries = Loggerr.LogEntries;
Loggerr.Log("Loading finished, probably... Have a nice day!");
}

private void LoadDBInfo()
Expand Down Expand Up @@ -171,14 +195,15 @@ public void OpenInstanceWindow(object o)

public void DeleteInstance(object o)
{
var prompt = MessageBox.Show($"Delete {SelectedInstance.InstanceName}?", "Delete instance", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
var prompt = MessageBox.Show($"Delete {SelectedInstance.InstanceName}? This will not delete the files.", "Delete instance", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (prompt == DialogResult.Yes)
{
Shared.Info.InstanceList.Remove(SelectedInstance);
Shared.Info.SaveToXml();
LinkButtonEnabled = false;
UpdateSumSize();
OnPropertyChanged(nameof(InstanceList));
Loggerr.Log("Deleting instance operation finished.");
}
}
public void UpdateDBSize()
Expand Down
1 change: 1 addition & 0 deletions WPFLinkTool/WPFLinkTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>otoge.ico</ApplicationIcon>
<Version>$(VersionPrefix)</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 9496697

Please sign in to comment.