SHA256
204 lines
6.0 KiB
C#
204 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Xml.Linq;
|
|
using System.Windows.Controls;
|
|
using System.Windows;
|
|
using Microsoft.Win32;
|
|
using System.Collections.ObjectModel;
|
|
using TapeCatalog;
|
|
|
|
namespace XML
|
|
{
|
|
public static class Methods
|
|
{
|
|
public class TapeEntry
|
|
{
|
|
public string Medium { get; set; }
|
|
public string Filename { get; set; }
|
|
public string Modified { get; set; }
|
|
}
|
|
|
|
public static void ExportTapeEntries(Window owner, string medium, IEnumerable<TapeEntry> entries)
|
|
{
|
|
if (entries == null) return;
|
|
|
|
var dlg = new Microsoft.Win32.SaveFileDialog()
|
|
{
|
|
Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
|
|
DefaultExt = "xml",
|
|
AddExtension = true,
|
|
FileName = $"tape_{medium}.xml"
|
|
};
|
|
|
|
var res = dlg.ShowDialog(owner);
|
|
if (res != true) return;
|
|
|
|
try
|
|
{
|
|
var doc = new XDocument(
|
|
new XElement("TapeEntries",
|
|
new XElement("Medium", medium ?? string.Empty),
|
|
entries.Select(e => new XElement("Entry",
|
|
new XElement("Medium", e.Medium ?? string.Empty),
|
|
new XElement("Filename", e.Filename ?? string.Empty),
|
|
new XElement("Modified", e.Modified ?? string.Empty)
|
|
))
|
|
)
|
|
);
|
|
|
|
doc.Save(dlg.FileName);
|
|
MessageBox.Show(owner, $"Export erfolgreich nach {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(owner, $"Fehler beim Export: {ex.Message}", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
public static void CopyToTapeCatalog(Window owner, IEnumerable<FileEntry> sourceFiles, ListView targetListView)
|
|
{
|
|
if (sourceFiles == null || targetListView == null) return;
|
|
|
|
// add entries: Medium = ArcDrive, Dateiname = Filename, Änderungsdatum = SizeArc (archive size)
|
|
foreach (var f in sourceFiles)
|
|
{
|
|
try
|
|
{
|
|
var te = new TapeEntry
|
|
{
|
|
Medium = f.ArcDrive ?? string.Empty,
|
|
Filename = f.Filename ?? string.Empty,
|
|
Modified = f.SizeArc ?? string.Empty
|
|
};
|
|
|
|
// add to ListView on UI thread
|
|
if (!targetListView.Dispatcher.CheckAccess())
|
|
{
|
|
targetListView.Dispatcher.Invoke(() => targetListView.Items.Add(te));
|
|
}
|
|
else
|
|
{
|
|
targetListView.Items.Add(te);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore malformed entries
|
|
}
|
|
}
|
|
}
|
|
public static void ImportList(Window owner, ObservableCollection<FileEntry> files, TextBox sourceTextBox, TextBox archiveTextBox)
|
|
{
|
|
if (files == null) return;
|
|
|
|
if (files.Count > 0)
|
|
{
|
|
var ask = MessageBox.Show(owner, "Aktuelle Liste speichern?\nJa = speichern, Nein = löschen, Abbrechen = abbrechen.", "Liste vorhanden", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
|
|
if (ask == MessageBoxResult.Cancel) return;
|
|
|
|
if (ask == MessageBoxResult.Yes)
|
|
{
|
|
// save current list
|
|
ExportList(owner, sourceTextBox?.Text ?? string.Empty, archiveTextBox?.Text ?? string.Empty, files);
|
|
files.Clear();
|
|
}
|
|
else if (ask == MessageBoxResult.No)
|
|
{
|
|
files.Clear();
|
|
}
|
|
}
|
|
|
|
var dlg = new Microsoft.Win32.OpenFileDialog()
|
|
{
|
|
Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
|
|
DefaultExt = "xml",
|
|
Multiselect = false
|
|
};
|
|
|
|
var res = dlg.ShowDialog(owner);
|
|
if (res != true) return;
|
|
|
|
try
|
|
{
|
|
var doc = XDocument.Load(dlg.FileName);
|
|
var root = doc.Element("Files");
|
|
if (root == null) return;
|
|
|
|
var src = root.Element("SourceDir")?.Value ?? string.Empty;
|
|
var arc = root.Element("ArchiveDir")?.Value ?? string.Empty;
|
|
if (sourceTextBox != null) sourceTextBox.Text = src;
|
|
if (archiveTextBox != null) archiveTextBox.Text = arc;
|
|
|
|
foreach (var fe in root.Elements("File"))
|
|
{
|
|
try
|
|
{
|
|
var entry = new FileEntry
|
|
{
|
|
Filename = fe.Element("Filename")?.Value ?? string.Empty,
|
|
FileDir = fe.Element("FileDir")?.Value ?? string.Empty,
|
|
SizeSrc = fe.Element("SizeSrc")?.Value ?? string.Empty,
|
|
SizeArc = fe.Element("SizeArc")?.Value ?? string.Empty,
|
|
Status = fe.Element("Status")?.Value ?? string.Empty,
|
|
ArcDrive = fe.Element("ArcDrive")?.Value ?? string.Empty,
|
|
ModSrc = fe.Element("ModSrc")?.Value ?? string.Empty,
|
|
ModArc = fe.Element("ModArc")?.Value ?? string.Empty
|
|
};
|
|
files.Add(entry);
|
|
}
|
|
catch
|
|
{
|
|
// ignore malformed entries
|
|
}
|
|
}
|
|
MessageBox.Show(owner, $"Import erfolgreich von {dlg.FileName}", "Import", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(owner, $"Fehler beim Import: {ex.Message}", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
public static void ExportList(Window owner, string sourceDir, string archiveDir, IEnumerable<FileEntry> files)
|
|
{
|
|
var dlg = new SaveFileDialog()
|
|
{
|
|
Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
|
|
DefaultExt = "xml",
|
|
AddExtension = true,
|
|
FileName = "filelist.xml"
|
|
};
|
|
|
|
var res = dlg.ShowDialog(owner);
|
|
if (res != true)
|
|
return;
|
|
|
|
try
|
|
{
|
|
var doc = new XDocument(
|
|
new XElement("Files",
|
|
new XElement("SourceDir", sourceDir ?? string.Empty),
|
|
new XElement("ArchiveDir", archiveDir ?? string.Empty),
|
|
files.Select(f => new XElement("File",
|
|
new XElement("Filename", f.Filename ?? string.Empty),
|
|
new XElement("FileDir", f.FileDir ?? string.Empty),
|
|
new XElement("SizeSrc", f.SizeSrc ?? string.Empty),
|
|
new XElement("SizeArc", f.SizeArc ?? string.Empty),
|
|
new XElement("Status", f.Status ?? string.Empty),
|
|
new XElement("ArcDrive", f.ArcDrive ?? string.Empty),
|
|
new XElement("ModSrc", f.ModSrc ?? string.Empty),
|
|
new XElement("ModArc", f.ModArc ?? string.Empty)
|
|
))
|
|
)
|
|
);
|
|
|
|
doc.Save(dlg.FileName);
|
|
MessageBox.Show(owner, $"Export erfolgreich nach {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(owner, $"Fehler beim Export: {ex.Message}", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|