1
1
using CommunityToolkit . Mvvm . ComponentModel ;
2
2
using CommunityToolkit . Mvvm . Input ;
3
+ using Leosac . KeyManager . Library . KeyGen ;
3
4
using Leosac . KeyManager . Library . KeyStore ;
4
5
using Leosac . KeyManager . Library . Plugin . UI ;
5
6
using Leosac . WpfApp ;
6
7
using MaterialDesignThemes . Wpf ;
7
8
using System . Collections . ObjectModel ;
8
9
using System . ComponentModel ;
10
+ using System . Text . RegularExpressions ;
11
+ using System . Windows ;
9
12
using System . Windows . Controls ;
10
13
using System . Windows . Controls . Primitives ;
11
14
using System . Windows . Data ;
15
+ using System . Windows . Documents ;
12
16
using System . Windows . Input ;
17
+ using System . Windows . Media ;
13
18
14
19
namespace Leosac . KeyManager . Library . UI . Domain
15
20
{
@@ -519,28 +524,201 @@ private void ToggleAllSelection(bool selected)
519
524
}
520
525
}
521
526
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
+
522
532
private async Task PrintSelection ( )
523
533
{
524
534
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 )
527
562
{
528
- if ( identifier . Selected && identifier . KeyEntryId != null && KeyStore != null )
563
+ if ( identifier . KeyEntryId != null && KeyStore != null )
529
564
{
530
565
var ke = await KeyStore . Get ( identifier . KeyEntryId , KeyEntryClass ) ;
531
566
if ( ke != null )
532
567
{
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
+ }
534
647
}
535
648
}
536
649
}
650
+ table . RowGroups . Add ( tableRows ) ;
651
+ content . Blocks . Add ( table ) ;
652
+ flow . Blocks . Add ( content ) ;
537
653
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 )
539
712
{
540
713
try
541
714
{
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" ) ;
544
722
}
545
723
catch ( Exception ex )
546
724
{
@@ -549,6 +727,25 @@ private async Task PrintSelection()
549
727
}
550
728
}
551
729
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
+
552
749
public RelayCommand < string > OrderingCommand { get ; }
553
750
private void Ordering ( string ? order )
554
751
{
0 commit comments