DirectOutputR1
DirectOutput framework R1 for virtual pinball cabinets.
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
GlobalConfigEdit.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Reflection;
5 using System.Windows.Forms;
6 using DirectOutput.GlobalConfiguration;
7 
8 namespace DirectOutput.Frontend
9 {
10  public partial class GlobalConfigEdit : Form
11  {
12  private bool _Modified = false;
13 
14  protected bool Modified
15  {
16  get { return _Modified; }
17  set { _Modified = value;
18  this.Text="Global configuration editor {0}".Build(_Modified?" <contains unsaved changes>":"");
19  }
20  }
21 
22 
23  private GlobalConfig GlobalConfig;
24 
25 
26  public bool SaveConfigData()
27  {
28  try
29  {
30 
31  if (!ValidateConfigData())
32  {
33  MessageBox.Show(this, "The data specified for the Global Configuration contains some invalid data.\nPlease correct the data before saving.", "Invalid global configuration data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
34  return false;
35  }
36 
37  if (GlobalConfig.GlobalConfigFilename.IsNullOrWhiteSpace())
38  {
39  SaveGlobalConfigDialog.InitialDirectory = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
40  if (SaveGlobalConfigDialog.ShowDialog(this) != DialogResult.OK)
41  {
42  return false;
43  }
44  GlobalConfig.GlobalConfigFilename = SaveGlobalConfigDialog.FileName;
45  }
46 
47 
48  UpdateGlobalConfigData(GlobalConfig);
49 
51 
52  MessageBox.Show(this, "Global config saved to\n{0}.\n\nYou must restart the framework to activate the new global config settings.".Build(GlobalConfig.GlobalConfigFilename), "Global configuration saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
53  Modified = false;
54  return true;
55  }
56  catch (Exception E)
57  {
58  MessageBox.Show(this, "Could not save global configuration.\nA exception occured:\n{0}".Build(E.Message), "Global config save error", MessageBoxButtons.OK, MessageBoxIcon.Error);
59  Log.Exception("Could not save global configuration.\nA exception occured", E);
60  return false;
61  }
62  }
63 
64 
65  #region Validate config data
66  public bool ValidateConfigData()
67  {
68  return ValidateLedcontrolFiles()
69  && ValidateFilePatterns(GlobalScriptFilePatterns)
70  && ValidateFilePatterns(CabinetConfigFilePatterns)
71 
72  && ValidateFilePatterns(TableScriptFilePatterns)
73  && ValidateFilePatterns(TableConfigFilePatterns)
74  && ValidateLogFilePattern();
75  }
76 
77  private bool ValidateLogFilePattern()
78  {
79  if (!LogFilePattern.Text.IsNullOrWhiteSpace())
80  {
81  if (!new FilePattern(LogFilePattern.Text).IsValid)
82  {
83  return false;
84  }
85  }
86  return true;
87  }
88 
89  private bool ValidateLedcontrolFiles()
90  {
91  return UpdateLedcontrolFileStatus();
92  }
93 
94  private bool ValidateFilePatterns(DataGridView Source)
95  {
96  for (int r = 0; r < Source.Rows.Count; r++)
97  {
98  if (!new FilePattern((string)Source[0, r].Value).IsValid)
99  {
100  return false;
101  };
102  }
103  return true;
104  }
105 
106  #endregion
107 
108 
109  #region Update global config data
110 
111 
112  public void UpdateGlobalConfigData(GlobalConfig Config)
113  {
114  if (ValidateConfigData())
115  {
116  UpdateLedcontrolFiles(Config);
117 
118  SaveFilePatterns(Config.GlobalScriptFilePatterns, GlobalScriptFilePatterns);
119 
120  SaveFilePatterns(Config.CabinetConfigFilePatterns, CabinetConfigFilePatterns);
121 
122 
123  SaveFilePatterns(Config.TableConfigFilePatterns, TableConfigFilePatterns);
124  SaveFilePatterns(Config.TableScriptFilePatterns, TableScriptFilePatterns);
125 
126 
127  Config.LedControlMinimumEffectDurationMs = (int)MinLedControlEffectDuration.Value;
128  Config.LedControlMinimumRGBEffectDurationMs = (int)MinLedControlRGBLedEffectDuration.Value;
129 
130  Config.LogFilePattern.Pattern = LogFilePattern.Text;
131  Config.EnableLogging = EnableLogging.Checked;
132  }
133  }
134 
135 
136  private void UpdateLedcontrolFiles(GlobalConfig Config)
137  {
138  Config.LedControlIniFiles.Clear();
139 
140  for (int r = 0; r < LedcontrolFiles.Rows.Count; r++)
141  {
142  Config.LedControlIniFiles.Add(new LedControlIniFile((string)LedcontrolFiles[1, r].Value, (int)LedcontrolFiles[0, r].Value));
143  }
144  }
145 
146 
147  private void SaveFilePatterns(FilePatternList Patterns, DataGridView Source)
148  {
149  Patterns.Clear();
150 
151  for (int r = 0; r < Source.Rows.Count; r++)
152  {
153  Patterns.Add(new FilePattern((string)Source[0, r].Value));
154  }
155  }
156 
157  #endregion
158 
159 
160 
161 
162 
163  #region Config data loading
164  private void LoadConfigData(GlobalConfig Config)
165  {
166  GlobalConfig = Config;
167 
168  LoadLedcontrolFiles(Config);
169 
170  LoadFilePatterns(Config.GlobalScriptFilePatterns, GlobalScriptFilePatterns);
171 
172  LoadFilePatterns(Config.CabinetConfigFilePatterns, CabinetConfigFilePatterns);
173 
174 
175  LoadFilePatterns(Config.TableConfigFilePatterns, TableConfigFilePatterns);
176  LoadFilePatterns(Config.TableScriptFilePatterns, TableScriptFilePatterns);
177 
178  MinLedControlEffectDuration.Value = Config.LedControlMinimumEffectDurationMs;
179  MinLedControlRGBLedEffectDuration.Value = Config.LedControlMinimumRGBEffectDurationMs;
180 
181  EnableLogging.Checked = Config.EnableLogging;
182  LogFilePattern.Text = Config.LogFilePattern.Pattern;
183  LogFilePatternStatus.Text = (Config.LogFilePattern.IsValid ? "OK" : "Invalid file pattern");
184  }
185 
186  private void LoadFilePatterns(FilePatternList Patterns, DataGridView Destination)
187  {
188  foreach (FilePattern FP in Patterns)
189  {
190  int RowIndex = Destination.Rows.Add();
191  Destination[0, RowIndex].Value = FP.Pattern;
192  Destination[1, RowIndex].Value = (FP.IsValid ? "OK" : "Invalid file pattern");
193  }
194 
195 
196  Destination.ClearSelection();
197  Destination.Refresh();
198  }
199 
200 
201  private void LoadLedcontrolFiles(GlobalConfig Config)
202  {
203  foreach (LedControlIniFile LCF in Config.LedControlIniFiles)
204  {
205  int RowIndex = LedcontrolFiles.Rows.Add();
206 
207  LedcontrolFiles[0, RowIndex].Value = LCF.LedWizNumber.ToString();
208  LedcontrolFiles[1, RowIndex].Value = LCF.Filename;
209  LedcontrolFiles[2, RowIndex].Value = LCF.Status;
210 
211  }
212  LedcontrolFiles.ClearSelection();
213  LedcontrolFiles.Refresh();
214  }
215 
216 
217  #endregion
218 
219  #region Constructor
221  : this(new GlobalConfiguration.GlobalConfig())
222  {
223 
224  }
225 
226 
228  {
229  InitializeComponent();
230 
231  LoadConfigData(Config);
232  }
233  #endregion
234 
235 
236 
237  private string LastDirectory = "";
238  private void LedcontrolFiles_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
239  {
240  if (e.ColumnIndex == 1 && e.RowIndex.IsBetween(0, LedcontrolFiles.Rows.Count - 1))
241  {
242  if (!LastDirectory.IsNullOrWhiteSpace())
243  {
244  SelectLedcontrolFile.InitialDirectory = LastDirectory;
245  }
246  else
247  {
248  SelectLedcontrolFile.InitialDirectory = Assembly.GetExecutingAssembly().Location;
249  };
250  if (SelectLedcontrolFile.ShowDialog(this) == DialogResult.OK)
251  {
252  FileInfo F = new FileInfo(SelectLedcontrolFile.FileName);
253  LastDirectory = F.Directory.FullName;
254  if (!LedcontrolFiles.IsCurrentCellInEditMode)
255  {
256  LedcontrolFiles.BeginEdit(true);
257  }
258  ;
259  LedcontrolFiles.EditingControl.Text = F.FullName;
260  LedcontrolFiles.CommitEdit(DataGridViewDataErrorContexts.Commit);
261  LedcontrolFiles.Refresh();
262  }
263 
264 
265  }
266  }
267 
268  private void LedcontrolFiles_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
269  {
270 
271 
272  int Nr = 0;
273  List<int> NrList = new List<int>();
274 
275  for (int i = 0; i < LedcontrolFiles.Rows.Count; i++)
276  {
277  if (Nr < Convert.ToInt32((string)LedcontrolFiles.Rows[i].Cells[0].Value))
278  {
279  Nr = Convert.ToInt32((string)LedcontrolFiles.Rows[i].Cells[0].Value);
280  }
281  NrList.Add(Convert.ToInt32((string)LedcontrolFiles.Rows[i].Cells[0].Value));
282  }
283  if (Nr > 0) Nr++;
284  if (!Nr.IsBetween(1, 16))
285  {
286  Nr = 1;
287  for (int i = 1; i < 17; i++)
288  {
289  if (!NrList.Contains(i))
290  {
291  Nr = i;
292  break;
293  }
294  }
295  }
296 
297  e.Row.Cells[0].Value = Nr.ToString();
298  e.Row.Cells[1].Value = "";
299  }
300 
301 
302  private void LedcontrolFiles_UserAddedRow(object sender, DataGridViewRowEventArgs e)
303  {
304  if (LedcontrolFiles.Rows.Count > 16)
305  {
306  LedcontrolFiles.AllowUserToAddRows = false;
307 
308  while (LedcontrolFiles.Rows.Count > 17)
309  {
310  LedcontrolFiles.Rows.RemoveAt(LedcontrolFiles.Rows.Count - 1);
311  LedcontrolFiles.Refresh();
312  }
313 
314  }
315  else
316  {
317  LedcontrolFiles.AllowUserToAddRows = true;
318  }
319  }
320 
321  private void LedcontrolFiles_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
322  {
323  LedcontrolFiles.AllowUserToAddRows = !(LedcontrolFiles.Rows.Count > 16);
324  }
325 
326  private void LedcontrolFiles_CellValueChanged(object sender, DataGridViewCellEventArgs e)
327  {
328  UpdateLedcontrolFileStatus();
329  Modified = true;
330 
331  }
332 
333  private void LedcontrolFiles_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
334  {
335  if (MessageBox.Show("Do you really want to delete this row?", "Delete row", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
336  {
337  e.Cancel = true;
338  }
339  }
340 
341 
342  private void UpdateFilePatternStatus(DataGridView DataGridView, int RowIndex)
343  {
344  if (RowIndex.IsBetween(0, DataGridView.Rows.Count - 1))
345  {
346  FilePattern F = new FilePattern((string)DataGridView[0, RowIndex].Value);
347  DataGridView[1, RowIndex].Value = (F.IsValid ? "OK" : "Invalid file pattern");
348  }
349  }
350 
351  private void TableConfigFilePatterns_CellValueChanged(object sender, DataGridViewCellEventArgs e)
352  {
353  UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
354  Modified = true;
355  }
356 
357  private void TableScriptFilePatterns_CellValueChanged(object sender, DataGridViewCellEventArgs e)
358  {
359  UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
360  Modified = true;
361  }
362 
363  private void CabinetConfigFilePatterns_CellValueChanged(object sender, DataGridViewCellEventArgs e)
364  {
365  UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
366  Modified = true;
367  }
368 
369  private void CabinetScriptFilePatterns_CellValueChanged(object sender, DataGridViewCellEventArgs e)
370  {
371  UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
372  Modified = true;
373  }
374 
375 
376  private bool UpdateLedcontrolFileStatus()
377  {
378 
379  bool AllEntriesValid = true;
380  Dictionary<int, int> N = new Dictionary<int, int>();
381  foreach (DataGridViewRow R in LedcontrolFiles.Rows)
382  {
383  int Nr = Convert.ToInt32(R.Cells[0].Value);
384  if (N.ContainsKey(Nr))
385  {
386  N[Nr]++;
387  }
388  else
389  {
390  N.Add(Nr, 1);
391  }
392  }
393 
394  foreach (DataGridViewRow R in LedcontrolFiles.Rows)
395  {
396  string S = "";
397  string F = (string)R.Cells[1].Value;
398  if (F.IsNullOrWhiteSpace())
399  {
400  S = "No filename set";
401  AllEntriesValid = false;
402  }
403  else
404  {
405  try
406  {
407  if (F.IndexOfAny(Path.GetInvalidPathChars()) == -1)
408  {
409 
410  FileInfo FI = new FileInfo(F);
411  if (FI.Exists)
412  {
413  if (N[Convert.ToInt32(R.Cells[0].Value)] > 1)
414  {
415  S = "Duplicate Ledwiz number";
416  AllEntriesValid = false;
417  }
418  else
419  {
420  S = "OK";
421  }
422  }
423  else
424  {
425  S = "File does not exist";
426  }
427  }
428  else
429  {
430  S = "Invalid filename";
431  AllEntriesValid = false;
432  }
433  }
434  catch
435  {
436  S = "Invalid filename";
437  }
438  AllEntriesValid = false;
439  }
440  R.Cells[2].Value = S;
441  }
442  return AllEntriesValid;
443  }
444 
445  private void LogFilePattern_TextChanged(object sender, EventArgs e)
446  {
447  LogFilePatternStatus.Text = (new FilePattern(LogFilePattern.Text).IsValid ? "OK" : "Invalid file pattern");
448  Modified = true;
449  }
450 
451  string LastLogDirectory = "";
452  private void ShowLogFileDialog_Click(object sender, EventArgs e)
453  {
454  if (LastLogDirectory.IsNullOrWhiteSpace())
455  {
456  SelectLogFile.InitialDirectory = Assembly.GetExecutingAssembly().Location;
457  }
458  else
459  {
460  SelectLogFile.InitialDirectory = LastLogDirectory;
461  }
462  if (SelectLogFile.ShowDialog(this) == DialogResult.OK)
463  {
464  LogFilePattern.Text = SelectLogFile.FileName;
465  LastLogDirectory = new FileInfo(SelectLogFile.FileName).Directory.FullName;
466  LogFilePatternStatus.Text = (new FilePattern(LogFilePattern.Text).IsValid ? "OK" : "Invalid file pattern");
467  Modified = true;
468  }
469  }
470 
471  private void UpdateTimerIntervalMs_ValueChanged(object sender, EventArgs e)
472  {
473  Modified = true;
474  }
475 
476  private void EnableLogging_CheckedChanged(object sender, EventArgs e)
477  {
478  Modified = true;
479  }
480 
481  private void GlobalScriptFilePatterns_CellValueChanged(object sender, DataGridViewCellEventArgs e)
482  {
483  UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
484  Modified = true;
485  }
486 
487  private void SaveButton_Click(object sender, EventArgs e)
488  {
489  SaveConfigData();
490  }
491 
492  private void SaveExitButton_Click(object sender, EventArgs e)
493  {
494  if (SaveConfigData())
495  {
496  this.Close();
497  };
498  }
499 
500  private void ExitButton_Click(object sender, EventArgs e)
501  {
502  this.Close();
503 
504 
505 
506 
507  }
508 
509  private void GlobalConfigEdit_FormClosing(object sender, FormClosingEventArgs e)
510  {
511  if (Modified)
512  {
513  switch (MessageBox.Show(this, "The global config editor contains unsaved changes.\nDo you want to save before closing the window?", "Close global config editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3))
514  {
515  case DialogResult.Cancel:
516  e.Cancel = true;
517  return;
518  case DialogResult.No:
519  break;
520  case DialogResult.Yes:
521  if (!SaveConfigData())
522  {
523  e.Cancel = true;
524  return;
525  }
526  break;
527  default:
528  return;
529  }
530  }
531 
532  }
533 
534 
535 
536 
537 
538 
539 
540 
541 
542 
543 
544 
545 
546 
547 
548  }
549 }