Skip to content

Commit 9cc8845

Browse files
committed
Review key entries printing to use FlowDocument
Add KCV and Key Properties to the view
1 parent 61b1573 commit 9cc8845

File tree

7 files changed

+235
-120
lines changed

7 files changed

+235
-120
lines changed

KeyManager.Library.KeyGen/KeyChecksum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public string ComputeKCV(IEnumerable<string> tags, string key, string? iv)
3434
return Convert.ToHexString(ComputeKCV(new Key(tags, keySize, key), ivb));
3535
}
3636

37-
public string ComputeKCV(Key key, string? iv)
37+
public string ComputeKCV(Key key, string? iv = null)
3838
{
3939
byte[]? ivb = null;
4040
if (!string.IsNullOrEmpty(iv))

KeyManager.Library.UI/Domain/KeyEntriesControlViewModel.cs

Lines changed: 204 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
using CommunityToolkit.Mvvm.ComponentModel;
22
using CommunityToolkit.Mvvm.Input;
3+
using Leosac.KeyManager.Library.KeyGen;
34
using Leosac.KeyManager.Library.KeyStore;
45
using Leosac.KeyManager.Library.Plugin.UI;
56
using Leosac.WpfApp;
67
using MaterialDesignThemes.Wpf;
78
using System.Collections.ObjectModel;
89
using System.ComponentModel;
10+
using System.Text.RegularExpressions;
11+
using System.Windows;
912
using System.Windows.Controls;
1013
using System.Windows.Controls.Primitives;
1114
using System.Windows.Data;
15+
using System.Windows.Documents;
1216
using System.Windows.Input;
17+
using System.Windows.Media;
1318

1419
namespace Leosac.KeyManager.Library.UI.Domain
1520
{
@@ -519,28 +524,201 @@ private void ToggleAllSelection(bool selected)
519524
}
520525
}
521526

527+
private string ConvertPropertyNameToHumanFriendlyName(string propertyName)
528+
{
529+
return Regex.Replace(Regex.Replace(propertyName, @"(\P{Ll})(\P{Ll}\p{Ll})", "$1 $2"), @"(\p{Ll})(\P{Ll})", "$1 $2");
530+
}
531+
522532
private async Task PrintSelection()
523533
{
524534
var printDialog = new PrintDialog();
525-
var control = new KeyEntriesPrintControl();
526-
foreach (var identifier in Identifiers)
535+
536+
var flow = new FlowDocument();
537+
var header = new Section() { Background = Brushes.Orange };
538+
header.Blocks.Add(new Paragraph(new Run(Properties.Resources.KeyEntriesExportPrint) { FontSize = 20, FontWeight = FontWeights.Bold }));
539+
flow.Blocks.Add(header);
540+
var content = new Section();
541+
var table = new Table();
542+
table.Columns.Add(new TableColumn());
543+
table.Columns.Add(new TableColumn());
544+
table.Columns.Add(new TableColumn());
545+
var tableHeader = new TableRowGroup();
546+
tableHeader.Rows.Add(new TableRow
547+
{
548+
Background = Brushes.LightGray,
549+
FontWeight = FontWeights.Bold,
550+
Cells =
551+
{
552+
new TableCell(new Paragraph(new Run(Properties.Resources.KeyEntryIdentifier))),
553+
new TableCell(new Paragraph(new Run(Properties.Resources.KeyEntryLabel))),
554+
new TableCell(new Paragraph(new Run(Properties.Resources.KeyEntryVariant))),
555+
new TableCell(new Paragraph(new Run(Properties.Resources.KeyValue))),
556+
new TableCell(new Paragraph(new Run(Properties.Resources.KeyEntryProperties)))
557+
}
558+
});
559+
table.RowGroups.Add(tableHeader);
560+
var tableRows = new TableRowGroup();
561+
foreach (var identifier in GetSelectedIdentifiers() ?? Identifiers)
527562
{
528-
if (identifier.Selected && identifier.KeyEntryId != null && KeyStore != null)
563+
if (identifier.KeyEntryId != null && KeyStore != null)
529564
{
530565
var ke = await KeyStore.Get(identifier.KeyEntryId, KeyEntryClass);
531566
if (ke != null)
532567
{
533-
control.KeyEntries.Add(ke);
568+
var border = new Thickness(0, 1, 0, 0);
569+
var vcell = new TableCell() { BorderBrush = Brushes.Black, BorderThickness = border };
570+
var pcell = new TableCell() { BorderBrush = Brushes.Black, BorderThickness = border };
571+
tableHeader.Rows.Add(new TableRow
572+
{
573+
Cells =
574+
{
575+
new TableCell(new Paragraph(new Run(ke.Identifier.Id))) { BorderBrush = Brushes.Black, BorderThickness = border },
576+
new TableCell(new Paragraph(new Run(ke.Identifier.Label))) { BorderBrush = Brushes.Black, BorderThickness = border },
577+
new TableCell(new Paragraph(new Run(ke.Variant?.Name))) { BorderBrush = Brushes.Black, BorderThickness = border },
578+
vcell,
579+
pcell,
580+
}
581+
});
582+
if (ke.Properties != null)
583+
{
584+
var l = new List() { FontSize = 12, Margin = new Thickness(0) };
585+
var t = ke.Properties.GetType();
586+
var props = t.GetProperties();
587+
foreach (var p in props)
588+
{
589+
if (p.CanRead && p.CanWrite)
590+
{
591+
bool include = false;
592+
var v = p.GetValue(ke.Properties);
593+
if (v != null)
594+
{
595+
if (p.PropertyType == typeof(string) || p.PropertyType.IsEnum)
596+
{
597+
include = !string.IsNullOrWhiteSpace(v.ToString());
598+
}
599+
else if (p.PropertyType == typeof(bool))
600+
{
601+
include = (bool)v;
602+
}
603+
}
604+
if (include)
605+
{
606+
l.ListItems.Add(new ListItem(new Paragraph(new Run(ConvertPropertyNameToHumanFriendlyName(p.Name) + ": " + v))));
607+
}
608+
}
609+
}
610+
pcell.Blocks.Add(l);
611+
}
612+
if (ke.Variant != null)
613+
{
614+
var p = new Paragraph() { FontSize = 12 };
615+
foreach (var kc in ke.Variant.KeyContainers)
616+
{
617+
if (p.Inlines.Count > 0)
618+
{
619+
p.Inlines.Add(new LineBreak());
620+
}
621+
if (kc is KeyVersion kv)
622+
{
623+
p.Inlines.Add(new Run(kv.Version + ": "));
624+
}
625+
string? c = null;
626+
if (ke.KClass == KeyEntryClass.Symmetric)
627+
{
628+
var v = kc.Key.GetAggregatedValueAsString();
629+
if (!string.IsNullOrEmpty(v))
630+
{
631+
var kcv = new KCV();
632+
c = kcv.ComputeKCV(kc.Key);
633+
}
634+
}
635+
636+
if (!string.IsNullOrEmpty(c))
637+
{
638+
p.Inlines.Add(new Run(c));
639+
}
640+
else
641+
{
642+
p.Inlines.Add(new Run("-"));
643+
}
644+
}
645+
vcell.Blocks.Add(p);
646+
}
534647
}
535648
}
536649
}
650+
table.RowGroups.Add(tableRows);
651+
content.Blocks.Add(table);
652+
flow.Blocks.Add(content);
537653

538-
if (control.KeyEntries.Count > 0 && printDialog.ShowDialog() == true)
654+
var sign = new Section();
655+
var stable = new Table()
656+
{
657+
Columns =
658+
{
659+
new TableColumn(),
660+
new TableColumn() { Width = new GridLength(300) }
661+
}
662+
};
663+
stable.RowGroups.Add(new TableRowGroup()
664+
{
665+
Rows =
666+
{
667+
new TableRow()
668+
{
669+
Cells =
670+
{
671+
new TableCell(new Paragraph()
672+
{
673+
Inlines =
674+
{
675+
new Run(Properties.Resources.Notes + ":") { FontWeight = FontWeights.Bold },
676+
new LineBreak(),
677+
new LineBreak(),
678+
new LineBreak(),
679+
new LineBreak(),
680+
new LineBreak()
681+
}
682+
})
683+
{
684+
BorderBrush = Brushes.Black,
685+
BorderThickness = new Thickness(1)
686+
},
687+
new TableCell(new Section()
688+
{
689+
Blocks =
690+
{
691+
new Paragraph(new Run(Properties.Resources.Date + ": " + DateTime.Now.ToShortDateString()))
692+
},
693+
Margin = new Thickness(5)
694+
})
695+
}
696+
}
697+
}
698+
});
699+
sign.Blocks.Add(stable);
700+
flow.Blocks.Add(sign);
701+
702+
var footer = new Section() { Background = Brushes.Orange };
703+
var fp = new Paragraph(new Run(Properties.Resources.DocumentGenerated)) { Margin = new Thickness(5) };
704+
fp.Inlines.Add(new Run(" Leosac Key Manager ") { FontWeight = FontWeights.Bold });
705+
fp.Inlines.Add(new Run(" - "));
706+
fp.Inlines.Add(new Hyperlink(new Run("www.leosac.com")) { NavigateUri = new Uri("https://www.leosac.com") });
707+
footer.Blocks.Add(fp);
708+
flow.Blocks.Add(footer);
709+
710+
printDialog.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;
711+
if (printDialog.ShowDialog() == true)
539712
{
540713
try
541714
{
542-
// TODO: print a FlowDocument instead, as PrintVisual needs control to match 1 page
543-
printDialog.PrintVisual(control, "Leosac Key Manager - Key Entries Printing");
715+
flow.PageHeight = printDialog.PrintableAreaHeight;
716+
flow.PageWidth = printDialog.PrintableAreaWidth;
717+
flow.ColumnGap = 0;
718+
flow.ColumnWidth = double.MaxValue;
719+
720+
var idocument = flow as IDocumentPaginatorSource;
721+
printDialog.PrintDocument(idocument.DocumentPaginator, "Leosac Key Manager - Key Entries Printing");
544722
}
545723
catch (Exception ex)
546724
{
@@ -549,6 +727,25 @@ private async Task PrintSelection()
549727
}
550728
}
551729

730+
private IEnumerable<SelectableKeyEntryId>? GetSelectedIdentifiers()
731+
{
732+
if (!ShowSelection)
733+
return null;
734+
735+
var identifiers = new List<SelectableKeyEntryId>();
736+
foreach (var obj in _identifiersView)
737+
{
738+
if (obj is SelectableKeyEntryId identifier)
739+
{
740+
if (identifier.Selected && identifier.KeyEntryId != null)
741+
{
742+
identifiers.Add(identifier);
743+
}
744+
}
745+
}
746+
return identifiers;
747+
}
748+
552749
public RelayCommand<string> OrderingCommand { get; }
553750
private void Ordering(string? order)
554751
{

KeyManager.Library.UI/KeyEntriesPrintControl.xaml

Lines changed: 0 additions & 73 deletions
This file was deleted.

KeyManager.Library.UI/KeyEntriesPrintControl.xaml.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)