Skip to content

Commit 41e9976

Browse files
committed
Some code cleanups from Manoochehr Mojgani
1 parent 2c33091 commit 41e9976

File tree

4 files changed

+58
-67
lines changed

4 files changed

+58
-67
lines changed

src/Application/FormMain.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ public partial class FormMain : Form, ISite
4848

4949
public FormMain()
5050
{
51-
this._settings = new Settings();
52-
this._settings.StartupPath = Application.StartupPath;
53-
this._settings.ExecutablePath = Application.ExecutablePath;
54-
this._settings.Resolver = new XmlProxyResolver(this);
51+
this.DoubleBuffered = true;
52+
this._settings = new Settings()
53+
{
54+
StartupPath = Application.StartupPath,
55+
ExecutablePath = Application.ExecutablePath,
56+
Resolver = new XmlProxyResolver(this)
57+
};
5558

5659
this._delayedActions = _settings.DelayedActions = new DelayedActions((action) =>
5760
{
@@ -184,10 +187,10 @@ private async System.Threading.Tasks.Task AsyncSetup()
184187
// install Xml notepad as an available editor for .xml files.
185188
FileAssociation.AddXmlProgids(Application.ExecutablePath);
186189

187-
CheckNetwork();
190+
await CheckNetwork();
188191
}
189192

190-
private void CheckNetwork()
193+
private async System.Threading.Tasks.Task CheckNetwork()
191194
{
192195
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
193196
{
@@ -196,7 +199,7 @@ private void CheckNetwork()
196199
client.UseDefaultCredentials = true;
197200
try
198201
{
199-
string html = client.DownloadString(Utilities.HelpBaseUri);
202+
string html = await client.DownloadStringTaskAsync(Utilities.HelpBaseUri);
200203
if (html.Contains("XML Notepad"))
201204
{
202205
this.BeginInvoke(new Action(FoundOnlineHelp));

src/Model/SchemaCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ public void ReadXml(XmlReader r)
602602
bool.TryParse(s, out disabled);
603603
}
604604
string filename = r.ReadString();
605-
this.Add(nsuri, pfn.GetAbsoluteFilename(filename), disabled);
605+
this.Add(nsuri, pfn.GetAbsoluteFileName(filename), disabled);
606606
}
607607
}
608608
}

src/Model/Settings.cs

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public enum ColorTheme
4040
public class Settings : IDisposable
4141
{
4242
private static Settings _instance;
43-
private string filename;
44-
private FileSystemWatcher watcher;
45-
private Hashtable map = new Hashtable();
46-
private System.Threading.Timer timer;
47-
private PersistentFileNames pfn;
43+
private string _filename;
44+
private FileSystemWatcher _watcher;
45+
private Hashtable _map = new Hashtable();
46+
private System.Threading.Timer _timer;
47+
private PersistentFileNames _pfn;
4848

4949
public static string DefaultUpdateLocation = "https://lovettsoftwarestorage.blob.core.windows.net/downloads/XmlNotepad/Updates.xml";
5050

@@ -103,10 +103,10 @@ public static Settings Instance
103103
/// </summary>
104104
public void StopWatchingFileChanges()
105105
{
106-
if (this.watcher != null)
106+
if (this._watcher != null)
107107
{
108-
this.watcher.Dispose();
109-
this.watcher = null;
108+
this._watcher.Dispose();
109+
this._watcher = null;
110110
}
111111
}
112112

@@ -135,12 +135,12 @@ public void OnChanged(string name)
135135
/// <returns>The setting value or null if not found.</returns>
136136
public object this[string name]
137137
{
138-
get { return this.map[name]; }
138+
get { return this._map[name]; }
139139
set
140140
{
141-
if (this.map[name] != value)
141+
if (this._map[name] != value)
142142
{
143-
this.map[name] = value;
143+
this._map[name] = value;
144144
OnChanged(name);
145145
}
146146
}
@@ -151,7 +151,7 @@ public object this[string name]
151151
/// </summary>
152152
public void Reload()
153153
{
154-
Load(this.filename);
154+
Load(this._filename);
155155
}
156156

157157
/// <summary>
@@ -161,7 +161,7 @@ public void Reload()
161161
/// <param name="filename">XmlNotepad settings xml file.</param>
162162
public void Load(string filename)
163163
{
164-
pfn = new PersistentFileNames(Settings.Instance.StartupPath);
164+
_pfn = new PersistentFileNames(Settings.Instance.StartupPath);
165165

166166
// we don't use the serializer because it's too slow to fire up.
167167
try
@@ -175,7 +175,7 @@ public void Load(string filename)
175175
if (r.NodeType == XmlNodeType.Element)
176176
{
177177
string name = r.Name;
178-
object o = map[name];
178+
object o = _map[name];
179179
if (o != null)
180180
{
181181
object value = null;
@@ -219,20 +219,20 @@ public void Load(string filename)
219219

220220
public string FileName
221221
{
222-
get { return this.filename; }
222+
get { return this._filename; }
223223
set
224224
{
225-
if (this.filename != value)
225+
if (this._filename != value)
226226
{
227-
this.filename = value;
227+
this._filename = value;
228228

229229
StopWatchingFileChanges();
230230

231-
this.watcher = new FileSystemWatcher(Path.GetDirectoryName(filename),
232-
Path.GetFileName(filename));
231+
this._watcher = new FileSystemWatcher(Path.GetDirectoryName(_filename),
232+
Path.GetFileName(_filename));
233233

234-
this.watcher.Changed += new FileSystemEventHandler(watcher_Changed);
235-
this.watcher.EnableRaisingEvents = true;
234+
this._watcher.Changed += new FileSystemEventHandler(watcher_Changed);
235+
this._watcher.EnableRaisingEvents = true;
236236
}
237237
}
238238
}
@@ -241,7 +241,7 @@ string ConvertToString(object value)
241241
{
242242
if (value is Uri)
243243
{
244-
return pfn.GetPersistentFileName((Uri)value);
244+
return _pfn.GetPersistentFileName((Uri)value);
245245
}
246246
else if (value is string)
247247
{
@@ -263,7 +263,7 @@ object ConvertToType(string value, Type type)
263263
{
264264
if (type == typeof(Uri))
265265
{
266-
return pfn.GetAbsoluteFilename(value);
266+
return _pfn.GetAbsoluteFileName(value);
267267
}
268268
else if (type == typeof(string))
269269
{
@@ -294,9 +294,9 @@ public void Save(string filename)
294294
{
295295
w.Formatting = Formatting.Indented;
296296
w.WriteStartElement("Settings");
297-
foreach (string key in map.Keys)
297+
foreach (string key in _map.Keys)
298298
{
299-
object value = map[key];
299+
object value = _map[key];
300300
if (value != null)
301301
{
302302
if (value is Hashtable)
@@ -418,19 +418,19 @@ private void watcher_Changed(object sender, FileSystemEventArgs e)
418418
// events and we don't want to have lots of dialogs popping up asking the
419419
// user to reload settings, so we insert a delay to let the events
420420
// settle down, then we tell the hosting app that the settings have changed.
421-
if (e.ChangeType == WatcherChangeTypes.Changed && this.timer == null)
421+
if (e.ChangeType == WatcherChangeTypes.Changed && this._timer == null)
422422
{
423-
this.timer = new System.Threading.Timer(new TimerCallback(OnDelay), this, 2000, Timeout.Infinite);
423+
this._timer = new System.Threading.Timer(new TimerCallback(OnDelay), this, 2000, Timeout.Infinite);
424424
}
425425
}
426426

427427
void OnDelay(object state)
428428
{
429429
OnChanged("File");
430-
if (this.timer != null)
430+
if (this._timer != null)
431431
{
432-
this.timer.Dispose();
433-
this.timer = null;
432+
this._timer.Dispose();
433+
this._timer = null;
434434
}
435435
}
436436

@@ -447,11 +447,11 @@ public void Dispose()
447447
protected virtual void Dispose(bool disposing)
448448
{
449449
this.StopWatchingFileChanges();
450-
if (this.timer != null)
450+
if (this._timer != null)
451451
{
452-
this.timer.Dispose();
452+
this._timer.Dispose();
453453
}
454-
this.timer = null;
454+
this._timer = null;
455455
GC.SuppressFinalize(this);
456456
}
457457

@@ -461,31 +461,19 @@ protected virtual void Dispose(bool disposing)
461461
public bool GetBoolean(string settingName, bool defaultValue = false)
462462
{
463463
object settingValue = this[settingName];
464-
if (settingValue is bool)
465-
{
466-
return (bool)settingValue;
467-
}
468-
return defaultValue;
464+
return settingValue is bool value ? value : defaultValue;
469465
}
470466

471467
public int GetInteger(string settingName, int defaultValue = 0)
472468
{
473469
object settingValue = this[settingName];
474-
if (settingValue is int)
475-
{
476-
return (int)settingValue;
477-
}
478-
return defaultValue;
470+
return settingValue is int value ? value : defaultValue;
479471
}
480472

481473
public string GetString(string settingName, string defaultValue = "")
482474
{
483475
object settingValue = this[settingName];
484-
if (settingValue != null)
485-
{
486-
return settingValue.ToString();
487-
}
488-
return defaultValue;
476+
return settingValue != null ? settingValue.ToString() : defaultValue;
489477
}
490478

491479
public Hashtable GetDefaultColors(ColorTheme theme)
@@ -551,14 +539,14 @@ public void AddDefaultColors(string name, ColorTheme theme)
551539
/// </summary>
552540
class PersistentFileNames
553541
{
554-
Hashtable variables = new Hashtable();
542+
private readonly Hashtable _variables = new Hashtable();
555543

556544
public PersistentFileNames(string startupPath)
557545
{
558-
variables["StartupPath"] = startupPath;
559-
variables["ProgramFiles"] = Environment.GetEnvironmentVariable("ProgramFiles");
560-
variables["UserProfile"] = Environment.GetEnvironmentVariable("UserProfile");
561-
variables["SystemRoot"] = Environment.GetEnvironmentVariable("SystemRoot");
546+
_variables["StartupPath"] = startupPath;
547+
_variables["ProgramFiles"] = Environment.GetEnvironmentVariable("ProgramFiles");
548+
_variables["UserProfile"] = Environment.GetEnvironmentVariable("UserProfile");
549+
_variables["SystemRoot"] = Environment.GetEnvironmentVariable("SystemRoot");
562550
}
563551

564552
public string GetPersistentFileName(Uri uri)
@@ -574,9 +562,9 @@ public string GetPersistentFileName(Uri uri)
574562
return null;
575563

576564
// replace absolute paths with variables.
577-
foreach (string key in variables.Keys)
565+
foreach (string key in _variables.Keys)
578566
{
579-
string baseDir = (string)variables[key];
567+
string baseDir = (string)_variables[key];
580568
if (!baseDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
581569
baseDir += Path.DirectorySeparatorChar;
582570
Uri baseUri = new Uri(baseDir);
@@ -602,17 +590,17 @@ public string GetPersistentFileName(Uri uri)
602590
return result;
603591
}
604592

605-
public Uri GetAbsoluteFilename(string filename)
593+
public Uri GetAbsoluteFileName(string filename)
606594
{
607595
try
608596
{
609597
// replace variables with absolute paths.
610-
foreach (string key in variables.Keys)
598+
foreach (string key in _variables.Keys)
611599
{
612600
string var = "%" + key + "%";
613601
if (filename.StartsWith(var, StringComparison.CurrentCultureIgnoreCase))
614602
{
615-
string baseDir = (string)variables[key];
603+
string baseDir = (string)_variables[key];
616604
string relPath = filename.Substring(var.Length);
617605
if (!baseDir.EndsWith(Path.DirectorySeparatorChar.ToString()))
618606
baseDir += Path.DirectorySeparatorChar;

src/XmlNotepad/XsltViewer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void DisplayXsltResults()
109109
output = this._model.XsltDefaultOutput;
110110
}
111111
output = this.xsltControl.DisplayXsltResults(this._model.Document, xpath, output);
112-
if (!string.IsNullOrEmpty(output))
112+
if (!string.IsNullOrWhiteSpace(output))
113113
{
114114
this.OutputFileName.Text = MakeRelative(output);
115115
}

0 commit comments

Comments
 (0)