Skip to content

Commit

Permalink
Fix issue #418: check setttings-file for "read-only"
Browse files Browse the repository at this point in the history
  • Loading branch information
clovett committed Sep 25, 2024
1 parent 4929697 commit 0b69987
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 45 deletions.
48 changes: 44 additions & 4 deletions src/Application/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Text;
using System.Windows.Forms;
using System.Xml;
using XmlNotepad.Properties;
using SR = XmlNotepad.StringResources;
using SystemTask = System.Threading.Tasks.Task;

Expand Down Expand Up @@ -577,10 +578,41 @@ protected override void OnClosing(CancelEventArgs e)
}
this.closing = true;
this._delayedActions.Close();
SaveConfig();

SaveSettings(e);

base.OnClosing(e);
}

private void SaveSettings(CancelEventArgs e)
{
try
{
SaveConfig();
}
catch (Exception ex)
{
if (this._settings.DiscardChanges)
{
// prompt the user only once per process.
}
else
{
var rc = MessageBox.Show(this, "Error saving " + this._settings.FileName + "\r\n\r\n" +
ex.Message + "\r\n\r\nWould you like to discard your changes to the settings?",
"Error saving settings", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
if (rc == DialogResult.No)
{
e.Cancel = true;
}
else
{
this._settings.DiscardChanges = true;
}
}
}
}

protected override void OnClosed(EventArgs e)
{
this.xmlTreeView1.Close();
Expand Down Expand Up @@ -1539,8 +1571,12 @@ public virtual void SaveConfig()
this._settings["TreeViewSize"] = this.xmlTreeView1.ResizerPosition;
this._settings["RecentFiles"] = this._recentFiles.ToArray();
this._settings["RecentXsltFiles"] = this._recentXsltFiles.ToArray();
var path = this._settings.FileName;
this._settings.Save(path);
if (this.Settings.IsDirty)
{
var path = this._settings.FileName;
Debug.WriteLine("Saving settings: " + path);
this._settings.Save(path);
}
}

#region ISite implementation
Expand Down Expand Up @@ -1624,7 +1660,7 @@ protected virtual void OnSettingsChanged(object sender, string name)
switch (name)
{
case "File":
// load the new settiongs but don't move the window or anything if another instances of xmlnotepad.exe changed
// load the new settings but don't move the window or anything if another instances of xmlnotepad.exe changed
// the settings.xml file.
if (!this._loading)
{
Expand Down Expand Up @@ -1718,6 +1754,10 @@ protected virtual void OnSettingsChanged(object sender, string name)
}
break;
}

this._delayedActions.StartDelayedAction("DelaySaveSettings",
() => SaveSettings(new CancelEventArgs()),
TimeSpan.FromSeconds(1));
}

public void SaveErrors(string filename)
Expand Down
87 changes: 47 additions & 40 deletions src/Model/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public class Settings : IDisposable
private readonly Hashtable _map = new Hashtable();
private DelayedActions _delayedActions = null;
private PersistentFileNames _pfn;

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

/// <summary>
Expand All @@ -278,6 +278,10 @@ public Settings()
_instance = this;
}

public bool IsDirty => this._isDirty;

public bool DiscardChanges { get; set; } = false;

public ValueMatchHandler Comparer
{
get => comparer;
Expand Down Expand Up @@ -373,6 +377,10 @@ public object this[string name]
if (!this.SettingValueMatches(this._map[name], value))
{
this._map[name] = value;
if (!(value is SchemaCache) && name != "SettingsLocation")
{
this._isDirty = true;
}
OnChanged(name);
}
}
Expand Down Expand Up @@ -445,6 +453,7 @@ public void Load(string filename)
Debug.WriteLine("Load settings failed: " + ex.Message);
}

this._isDirty = false;
this.FileName = filename;
}

Expand Down Expand Up @@ -525,53 +534,46 @@ public void Save(string filename)
{
// make sure directory exists!
Directory.CreateDirectory(Path.GetDirectoryName(filename));
try
using (var w = new XmlTextWriter(filename, System.Text.Encoding.UTF8))
{
using (var w = new XmlTextWriter(filename, System.Text.Encoding.UTF8))
w.Formatting = Formatting.Indented;
w.WriteStartElement("Settings");
// create save stability.
List<string> keys = new List<string>();
foreach (string key in _map.Keys)
{
w.Formatting = Formatting.Indented;
w.WriteStartElement("Settings");
// create save stability.
List<string> keys = new List<string>();
foreach (string key in _map.Keys)
{
keys.Add(key);
}
keys.Sort();
foreach (string key in keys)
keys.Add(key);
}
keys.Sort();
foreach (string key in keys)
{
object value = _map[key];
if (value != null)
{
object value = _map[key];
if (value != null)
if (value is Hashtable ht)
{
if (value is Hashtable ht)
{
w.WriteStartElement(key); // container element
WriteHashTable(w, ht);
w.WriteEndElement();
}
else if (value is Array va)
{
WriteArray(w, key, va);
}
else if (value is IXmlSerializable xs)
{
w.WriteStartElement(key); // container element
xs.WriteXml(w);
w.WriteEndElement();
}
else
{
string s = ConvertToString(value);
if (s != null) w.WriteElementString(key, s);
}
w.WriteStartElement(key); // container element
WriteHashTable(w, ht);
w.WriteEndElement();
}
else if (value is Array va)
{
WriteArray(w, key, va);
}
else if (value is IXmlSerializable xs)
{
w.WriteStartElement(key); // container element
xs.WriteXml(w);
w.WriteEndElement();
}
else
{
string s = ConvertToString(value);
if (s != null) w.WriteElementString(key, s);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

private void ReadHashTable(XmlReader r, Hashtable ht)
Expand Down Expand Up @@ -674,6 +676,9 @@ void OnDelay(int retries)
{
// make sure file is not still locked by the writer.
string text = File.ReadAllText(this._filename);

// The "File" property is a special signal to the listener that the entire
// settings need to be reloaded.
OnChanged("File");
}
catch (Exception)
Expand Down Expand Up @@ -1005,6 +1010,8 @@ public void SetDefaults()
this["TextEditor"] = Path.Combine(sysdir, "notepad.exe");
this["MouseCalibration"] = new Point[0];
this["PrimaryScreenSize"] = new Size();

this._isDirty = false;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Updates/Updates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<history>https://github.com/microsoft/XmlNotepad/blob/master/src/Updates/Updates.xml</history>
<frequency>1.00:00:00</frequency>
</application>
<version number="2.9.0.14">
<bug>Fix issue #418: check setttings-file for "read-only".</bug>
</version>
<version number="2.9.0.13">
<feature>Issue 409: Not able to validate XML against multiple not referenced Schemas</feature>
<feature>Issue 329: Incomplete schema validation of large XML files ( > ~20 MB).</feature>
Expand Down
2 changes: 1 addition & 1 deletion src/Version/Version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>2.9.0.13</ApplicationVersion>
<ApplicationVersion>2.9.0.14</ApplicationVersion>
<Version>$(ApplicationVersion)</Version>
<Authors>Chris Lovett</Authors>
<Product>XmlNotepad</Product>
Expand Down

0 comments on commit 0b69987

Please sign in to comment.