diff --git a/LincleLINK/Logic/MainWindowLogic.cs b/LincleLINK/Logic/MainWindowLogic.cs index 9b21acf..b8db319 100644 --- a/LincleLINK/Logic/MainWindowLogic.cs +++ b/LincleLINK/Logic/MainWindowLogic.cs @@ -19,6 +19,7 @@ namespace LincleLINK { public static readonly string currentDir = Directory.GetCurrentDirectory(); //public static readonly string currentDir = @"F:\LincleLINK"; + //public static readonly string currentDir = @"F:\LinkTool"; public static readonly string dbDir = Path.Join(currentDir, "db"); public static readonly string instanceDir = Path.Join(currentDir, "instance"); @@ -193,6 +194,7 @@ namespace LincleLINK public RelayCommand FolderBrowseTorrentFileCommand { get; set; } public RelayCommand FolderBrowseTorrentDLPathCommand { get; set; } public AsyncRelayCommand LinkToTorrentCommand { get; set; } + public RelayCommand CopyFilesCommand { get; set; } public MainWindowLogic(MainWindowControls controls, SynchronizationContext uicontext) { @@ -209,6 +211,7 @@ namespace LincleLINK CheckPiecesCommand = new(CheckPieces, (ex) => LogList.Add(ex.Message)); FolderBrowseTorrentFileCommand = new(FolderBrowseTorrentPath); FolderBrowseTorrentDLPathCommand = new(FolderBrowserTorrentDLPath); + CopyFilesCommand = new(CopyFiles, CanDoActionWithInstance); LinkToTorrentCommand = new(LinkToTorrent, (ex) => LogList.Add(ex.Message)); MatchedList = new(); RelativePath = string.Empty; @@ -216,13 +219,19 @@ namespace LincleLINK IsFree = true; } + public async Task GetSelectedInstance() + { + using FileStream openStream = File.OpenRead(Path.Combine(instanceDir, SelectedInstance.InstanceName + ".json")); + return await JsonSerializer.DeserializeAsync(openStream); + } + public void OpenAddInstanceWindow(object o) { AddInstanceWindow window = new(); window.ShowDialog(); UpdateInstanceList(); } - + public void DeleteInstance(object o) { try @@ -408,6 +417,7 @@ namespace LincleLINK } IsFree = true; } + public bool CanDoActionWithInstance(object o) { if (SelectedInstance == null || !IsFree) @@ -746,6 +756,49 @@ namespace LincleLINK Savings = Instance.ReadableSize(instTotal - dbSize); } + public async void CopyFiles(object o) + { + try + { + IsFree = false; + + if (MessageBox.Show("This will copy all hashed files of an instance to your selected destination. " + + "Does not overwrite files. Proceed to destination selection?", "Copy files", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + FolderBrowserDialog folderDialog = new(); + if (folderDialog.ShowDialog() == DialogResult.OK) + { + string destDir = folderDialog.SelectedPath; + Progress = 0; + ProgressStep = 100D / SelectedInstance.FileCount; + + Instance instance = await GetSelectedInstance(); + foreach (var instanceFile in instance.FileList) + { + string destinationFile = Path.Combine(destDir, instanceFile.HashedFileName); + if (!File.Exists(destinationFile)) + { + await Task.Run(() => File.Copy(Path.Combine(dbDir, instanceFile.HashedFileName), destinationFile)); + } + else + { + LogList.Add(destinationFile + " already exists."); + } + Progress += ProgressStep; + } + } + folderDialog.Dispose(); + } + Progress = 0; + IsFree = true; + } + catch (Exception e) + { + LogList.Add(e.Message); + } + } + [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] static extern bool CreateHardLink( string lpFileName, diff --git a/LincleLINK/MainWindow.xaml b/LincleLINK/MainWindow.xaml index 691ca85..5e10344 100644 --- a/LincleLINK/MainWindow.xaml +++ b/LincleLINK/MainWindow.xaml @@ -39,6 +39,7 @@ + @@ -47,8 +48,9 @@ - + +