DirectOuput Framework R2
DirectOutput framework R2 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.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using DirectOutput.GlobalConfiguration;
10 using System.IO;
11 
12 namespace GlobalConfigEditor
13 {
14  public partial class GlobalConfigEdit : Form
15  {
16 
17  GlobalConfig Config = new GlobalConfig();
18 
19  string ConfigFilename = "";
20 
22  {
23  InitializeComponent();
24  }
25 
26  private void GlobalConfigEdit_Load(object sender, EventArgs e)
27  {
28  LoadConfig();
29  }
30 
31 
32 
33  private void SaveConfig(string Filename)
34  {
35  Config.IniFilesPath = IniFilesPath.Text;
36  Config.CabinetConfigFilePattern.Pattern = CabinetFilename.Text;
37  Config.LogFilePattern.Pattern = LogFilename.Text;
38  Config.EnableLogging = LoggingEnabled.Checked;
39  Config.ClearLogOnSessionStart = ClearLogOnSessionStart.Checked;
40 
41  try
42  {
43  Config.SaveGlobalConfig(Filename);
44 
45  this.Text = "Global Configuration Editor - {0}".Build(Filename);
46  }
47  catch (Exception E)
48  {
49  MessageBox.Show("Could not save global configuration to file: {0}.\n{1}".Build(Filename, E.Message), "Save error", MessageBoxButtons.OK, MessageBoxIcon.Error);
50 
51  }
52  }
53 
54 
55  private void LoadConfig(string Filename = "")
56  {
57  ConfigFilename = "";
58  this.Text = "Global Configuration Editor - <New global configuration>".Build(Filename);
59  if (!Filename.IsNullOrWhiteSpace())
60  {
61  try
62  {
64  ConfigFilename = Filename;
65 
66  this.Text = "Global Configuration Editor - {0}".Build(Filename);
67  }
68  catch (Exception E)
69  {
70  MessageBox.Show("A exception occured when loading the global config file {0}.\nWill use empty global config instead.".Build(Filename), "Global config loading error", MessageBoxButtons.OK, MessageBoxIcon.Error);
71  Config = new GlobalConfig();
72  }
73  }
74  else
75  {
76  Config = new GlobalConfig();
77  }
78 
79 
80 
81  IniFilesPath.Text = Config.IniFilesPath;
82 
83  LoggingEnabled.Checked = Config.EnableLogging;
84  ClearLogOnSessionStart.Checked = Config.ClearLogOnSessionStart;
85  LogFilename.Text = Config.LogFilePattern.Pattern;
86 
87  CabinetFilename.Text = Config.CabinetConfigFilePattern.Pattern;
88 
89  }
90 
91  private void SelectIniFilePathButton_Click(object sender, EventArgs e)
92  {
93  DirectoryInfo DI = null;
94  if (!IniFilesPath.Text.IsNullOrWhiteSpace())
95  {
96  try
97  {
98  DI = new DirectoryInfo(IniFilesPath.Text);
99  if (DI.Exists)
100  {
101  SelectIniFileDirectoryDialog.SelectedPath = DI.FullName;
102  }
103  }
104  catch { }
105  }
106 
107  if (SelectIniFileDirectoryDialog.ShowDialog(Owner) == DialogResult.OK)
108  {
109 
110  IniFilesPath.Text = SelectIniFileDirectoryDialog.SelectedPath;
111 
112 
113  }
114 
115  }
116 
117 
118 
119  private void SelectCabinetConfigFileButton_Click(object sender, EventArgs e)
120  {
121  FileInfo FI = null;
122  if (!CabinetFilename.Text.IsNullOrWhiteSpace())
123  {
124  try
125  {
126  FI = new FileInfo(CabinetFilename.Text);
127  if (FI.Exists)
128  {
129  SelectCabinetConfigFileDialog.FileName = FI.FullName;
130  }
131  }
132  catch { }
133  }
134 
135  if (SelectCabinetConfigFileDialog.ShowDialog(Owner) == DialogResult.OK)
136  {
137 
138  CabinetFilename.Text = SelectCabinetConfigFileDialog.FileName;
139 
140 
141 
142  }
143  }
144 
145 
146 
147 
148 
149  private void SelectLogFileButton_Click(object sender, EventArgs e)
150  {
151  FileInfo FI = null;
152  if (!LogFilename.Text.IsNullOrWhiteSpace())
153  {
154  try
155  {
156  FI = new FileInfo(LogFilename.Text);
157  if (FI.Exists)
158  {
159  SelectLogFileDialog.FileName = FI.FullName;
160  }
161  }
162  catch { }
163  }
164 
165  if (SelectLogFileDialog.ShowDialog(Owner) == DialogResult.OK)
166  {
167 
168  LogFilename.Text = SelectLogFileDialog.FileName;
169 
170 
171 
172  }
173  }
174 
175 
176 
177  private void saveToolStripMenuItem_Click(object sender, EventArgs e)
178  {
179  if (!ConfigFilename.IsNullOrWhiteSpace())
180  {
181  SaveConfig(ConfigFilename);
182  }
183  else
184  {
185  saveAsToolStripMenuItem_Click(sender, e);
186  }
187  }
188 
189  private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
190  {
191  SaveGlobalConfigDialog.FileName = ConfigFilename;
192 
193  if (SaveGlobalConfigDialog.ShowDialog(Owner) == DialogResult.OK)
194  {
195  SaveConfig(SaveGlobalConfigDialog.FileName);
196 
197  }
198  }
199 
200  private void newToolStripMenuItem_Click(object sender, EventArgs e)
201  {
202  if (ContinueDialog())
203  {
204  LoadConfig("");
205  }
206  }
207 
208  private void loadToolStripMenuItem_Click(object sender, EventArgs e)
209  {
210  if (ContinueDialog())
211  {
212  OpenGlobalConfigDialog.InitialDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
213 
214  if (OpenGlobalConfigDialog.ShowDialog(Owner) == DialogResult.OK)
215  {
216  LoadConfig(OpenGlobalConfigDialog.FileName);
217  }
218 
219  }
220  }
221 
222  private bool ContinueDialog()
223  {
224  if (ConfigHasChanged)
225  {
226  switch (MessageBox.Show(Owner, "The current configuration has changed. Do you want to save the changes?\nPress Yes to save your changes.\nPress No to discard your changes.\nPress Cancel to abort this operation.", "Save changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
227  {
228  case System.Windows.Forms.DialogResult.Yes:
229  SaveGlobalConfigDialog.FileName = ConfigFilename;
230 
231  if (SaveGlobalConfigDialog.ShowDialog(Owner) == DialogResult.OK)
232  {
233  SaveConfig(SaveGlobalConfigDialog.FileName);
234 
235  }
236  else
237  {
238  if (MessageBox.Show(Owner, "You config has not been saved.\nDo you want to continue and discard your changes?", "Discard changes", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
239  {
240  return false;
241  }
242  }
243  return true;
244 
245  case System.Windows.Forms.DialogResult.No:
246  return true;
247 
248  default:
249  return false;
250 
251  }
252 
253  }
254  else
255  {
256  return true;
257 
258  }
259  }
260 
261  private void quitToolStripMenuItem_Click(object sender, EventArgs e)
262  {
263 
264  this.Close();
265 
266  }
267 
268  private void GlobalConfigEdit_FormClosing(object sender, FormClosingEventArgs e)
269  {
270  e.Cancel = !ContinueDialog();
271  }
272 
273 
274  private bool ConfigHasChanged
275  {
276  get
277  {
278  return (Config.IniFilesPath != IniFilesPath.Text || Config.CabinetConfigFilePattern.Pattern != CabinetFilename.Text || Config.EnableLogging != LoggingEnabled.Checked || Config.ClearLogOnSessionStart != ClearLogOnSessionStart.Checked || Config.LogFilePattern.Pattern != LogFilename.Text);
279 
280 
281 
282 
283 
284  }
285  }
286  }
287 }