@@ -22,6 +22,11 @@ public Options Options
2222
2323 private List < SaltSource > _saltSources = new ( ) ;
2424
25+ private List < WordList > _wordLists = new ( ) ;
26+ private readonly ListViewGroup [ ] _wordListCategories = Enum . GetNames ( typeof ( WordList . CategoryEnum ) )
27+ . Select ( wl => new ListViewGroup ( wl , wl ) ) . ToArray ( ) ;
28+
29+
2530 public DicewareOptionsForm ( )
2631 {
2732 InitializeComponent ( ) ;
@@ -49,24 +54,11 @@ private void InitializeEnumOptions()
4954 saltComboBox . SelectedIndex = 0 ;
5055
5156 _saltSources . Clear ( ) ;
52- activeSaltSourcesLabel . Text = string . Empty ;
53-
54- wordListsListView . Columns . Add ( "Word List" ) ;
55-
56- ListViewGroup [ ] wordListCategories = EnumTools . GetCategories < WordLists > ( )
57- . Select ( c => new ListViewGroup ( c , c ) )
58- . ToArray ( ) ;
57+ activeSaltSourcesListView . Columns . Add ( "Salt sources" ) ;
5958
60- wordListsListView . Groups . AddRange ( wordListCategories ) ;
61-
62- ListViewItem [ ] wordLists = EnumTools . GetDisplays < WordLists > ( )
63- . Select ( v =>
64- new ListViewItem ( $ "{ v } ", wordListCategories . First ( c => c . Name == v . Category ) ) )
65- . ToArray ( ) ;
66-
67- wordListsListView . Items . AddRange ( wordLists ) ;
68-
69- wordListsListView . Columns [ 0 ] . AutoResize ( ColumnHeaderAutoResizeStyle . ColumnContent ) ;
59+ _wordLists . Clear ( ) ;
60+ activeWordListsListView . Columns . Add ( "Word List" ) ;
61+ activeWordListsListView . Groups . AddRange ( _wordListCategories ) ;
7062 }
7163
7264 protected override void OnLoad ( EventArgs e )
@@ -90,31 +82,40 @@ private void PopulateInterface()
9082 l33tSpeakComboBox . SelectedIndex = l33tSpeakComboBox . FindStringExact ( Options . L33tSpeak . GetDescription ( ) ) ;
9183 saltComboBox . SelectedIndex = saltComboBox . FindStringExact ( Options . Salt . GetDescription ( ) ) ;
9284 _saltSources = new ( Options . SaltSources ) ;
93- UpdateSaltSourcesLabel ( ) ;
94- UpdateListView ( wordListsListView , Options . WordLists ) ;
85+ _wordLists = new ( Options . WordLists ) ;
86+ UpdateSaltSourcesListView ( ) ;
87+ UpdateWordListsListView ( ) ;
9588 }
9689
97- private void UpdateSaltSourcesLabel ( )
90+ private void UpdateSaltSourcesListView ( )
9891 {
99- IEnumerable < string > sources = _saltSources . Where ( ss => ss . Enabled )
92+ string [ ] sources = _saltSources . Where ( ss => ss . Enabled )
10093 . Select ( ss => $ "{ ss . Name } "
10194 + ( ss . MinimumAmount == ss . MaximumAmount
10295 ? $ "({ ss . MinimumAmount } )"
10396 : $ "({ ss . MinimumAmount } -{ ss . MaximumAmount } )")
104- ) ;
97+ ) . ToArray ( ) ;
10598
106- activeSaltSourcesLabel . Text = string . Join ( ", " , sources ) ;
99+ activeSaltSourcesListView . Items . Clear ( ) ;
100+ foreach ( string rowString in sources )
101+ {
102+ ListViewItem rowItem = new ListViewItem ( rowString ) ;
103+ activeSaltSourcesListView . Items . Add ( rowItem ) ;
104+ }
105+ activeSaltSourcesListView . AutoResizeColumns ( ColumnHeaderAutoResizeStyle . ColumnContent ) ;
107106 }
108107
109- private void UpdateListView < T > ( ListView view , T flags ) where T : Enum
108+ private void UpdateWordListsListView ( )
110109 {
111- foreach ( ListViewItem i in view . Items )
112- {
113- T flag = EnumTools . FromDescription < T > ( i . Text ) ;
114- i . Checked = flags . HasFlag ( flag ) ;
115- }
110+ ListViewItem [ ] lists = _wordLists . Where ( wl => wl . Enabled )
111+ . Select ( wl => new ListViewItem ( wl . Name ,
112+ _wordListCategories . First ( g => g . Name == Enum . GetName ( typeof ( WordList . CategoryEnum ) , wl . Category ) ) ) )
113+ . ToArray ( ) ;
114+
115+ activeWordListsListView . Items . Clear ( ) ;
116+ activeWordListsListView . Items . AddRange ( lists ) ;
116117
117- view . Refresh ( ) ;
118+ activeWordListsListView . AutoResizeColumns ( ColumnHeaderAutoResizeStyle . ColumnContent ) ;
118119 }
119120
120121 private Options GetOptions ( )
@@ -128,32 +129,15 @@ private Options GetOptions()
128129 AdvancedStrategy = EnumTools . FromDescription < AdvancedStrategy > ( advancedStrategyComboBox . SelectedItem . ToString ( ) ) ,
129130 Salt = EnumTools . FromDescription < SaltType > ( saltComboBox . SelectedItem . ToString ( ) ) ,
130131 SaltSources = new ( _saltSources ) ,
131- WordLists = GetListViewFlags < WordLists > ( wordListsListView )
132+ WordLists = new ( _wordLists )
132133 } ;
133134 }
134135
135- private T GetListViewFlags < T > ( ListView view ) where T : Enum
136- {
137- int result = 0 ;
138- foreach ( ListViewItem i in view . Items )
139- {
140- T flagEnum = EnumTools . FromDescription < T > ( i . Text ) ;
141- int flag = ( int ) Convert . ChangeType ( flagEnum , typeof ( int ) ) ;
142-
143- if ( i . Checked )
144- {
145- result |= flag ;
146- }
147- }
148-
149- return ( T ) Enum . ToObject ( typeof ( T ) , result ) ;
150- }
151-
152136 private void OkButton_Click ( object sender , System . EventArgs e )
153137 {
154138 Options = GetOptions ( ) ;
155139
156- if ( Options . WordLists == WordLists . None )
140+ if ( Options . WordLists . Count ( ) == 0 )
157141 {
158142 MessageBox . Show ( this , $ "No word lists were selected. This will prevent the plugin from generating passwords. Please select at least one list.", "Options Validation Failure" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
159143 DialogResult = DialogResult . Cancel ;
@@ -196,7 +180,30 @@ private void EditSaltSourcesButton_Click(object sender, EventArgs e)
196180 }
197181
198182 _saltSources = ssf . Result ;
199- UpdateSaltSourcesLabel ( ) ;
183+ UpdateSaltSourcesListView ( ) ;
184+ }
185+
186+ private void EditWordListsButton_Click ( object sender , EventArgs e )
187+ {
188+ WordListsForm wlf = new ( _wordLists ) ;
189+
190+ if ( wlf . ShowDialog ( this ) == DialogResult . Cancel )
191+ {
192+ return ;
193+ }
194+
195+ _wordLists = wlf . Result ;
196+ UpdateWordListsListView ( ) ;
197+ }
198+
199+ private void ActiveSaltSourcesListView_SelectedIndexChanged ( object sender , EventArgs e )
200+ {
201+ ( ( ListView ) sender ) . SelectedItems . Clear ( ) ;
202+ }
203+
204+ private void WordListsListView_SelectedIndexChanged ( object sender , EventArgs e )
205+ {
206+ ( ( ListView ) sender ) . SelectedItems . Clear ( ) ;
200207 }
201208 }
202209}
0 commit comments