Made HistoryManager class abstract, added HistoryManagerXML class

This commit is contained in:
Jaex 2019-10-21 10:54:24 +03:00
parent 705901703d
commit 0f56bd4239
6 changed files with 200 additions and 147 deletions

View file

@ -154,7 +154,7 @@ private HistoryItem[] GetHistoryItems()
{
if (history == null)
{
history = new HistoryManager(HistoryPath);
history = new HistoryManagerXML(HistoryPath);
}
IEnumerable<HistoryItem> tempHistoryItems = history.GetHistoryItems();

View file

@ -113,7 +113,7 @@ private IEnumerable<HistoryItem> GetHistoryItems()
{
if (history == null)
{
history = new HistoryManager(HistoryPath);
history = new HistoryManagerXML(HistoryPath);
}
List<HistoryItem> historyItems = history.GetHistoryItems();

View file

@ -27,19 +27,13 @@ You should have received a copy of the GNU General Public License
using ShareX.HistoryLib.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace ShareX.HistoryLib
{
public class HistoryManager
public abstract class HistoryManager
{
private static readonly object thisLock = new object();
public string FilePath { get; private set; }
public string BackupFolder { get; set; }
public bool CreateBackup { get; set; }
@ -54,7 +48,7 @@ public List<HistoryItem> GetHistoryItems()
{
try
{
return Load(FilePath);
return Load();
}
catch (Exception e)
{
@ -73,7 +67,7 @@ public bool AppendHistoryItem(HistoryItem historyItem)
{
if (IsValidHistoryItem(historyItem))
{
return Append(FilePath, historyItem);
return Append(historyItem);
}
}
catch (Exception e)
@ -90,147 +84,19 @@ private bool IsValidHistoryItem(HistoryItem historyItem)
(!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.Filepath));
}
private List<HistoryItem> Load(string filePath)
public List<HistoryItem> Load()
{
List<HistoryItem> historyItemList = new List<HistoryItem>();
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
lock (thisLock)
{
XmlReaderSettings settings = new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Auto,
IgnoreWhitespace = true
};
using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8))
using (XmlReader reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "HistoryItem")
{
XElement element = XNode.ReadFrom(reader) as XElement;
if (element != null)
{
HistoryItem hi = ParseHistoryItem(element);
historyItemList.Add(hi);
}
}
else
{
reader.Read();
}
}
}
}
return Load(FilePath);
}
return historyItemList;
public abstract List<HistoryItem> Load(string filePath);
public bool Append(params HistoryItem[] historyItems)
{
return Append(FilePath, historyItems);
}
private HistoryItem ParseHistoryItem(XElement element)
{
HistoryItem hi = new HistoryItem();
foreach (XElement child in element.Elements())
{
string name = child.Name.LocalName;
switch (name)
{
case "Filename":
hi.Filename = child.Value;
break;
case "Filepath":
hi.Filepath = child.Value;
break;
case "DateTimeUtc":
DateTime dateTime;
if (DateTime.TryParse(child.Value, out dateTime))
{
hi.DateTime = dateTime;
}
break;
case "Type":
hi.Type = child.Value;
break;
case "Host":
hi.Host = child.Value;
break;
case "URL":
hi.URL = child.Value;
break;
case "ThumbnailURL":
hi.ThumbnailURL = child.Value;
break;
case "DeletionURL":
hi.DeletionURL = child.Value;
break;
case "ShortenedURL":
hi.ShortenedURL = child.Value;
break;
}
}
return hi;
}
private bool Append(string filePath, params HistoryItem[] historyItems)
{
if (!string.IsNullOrEmpty(filePath))
{
lock (thisLock)
{
Helpers.CreateDirectoryFromFilePath(filePath);
using (FileStream fs = File.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.Read))
using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
foreach (HistoryItem historyItem in historyItems)
{
writer.WriteStartElement("HistoryItem");
writer.WriteElementIfNotEmpty("Filename", historyItem.Filename);
writer.WriteElementIfNotEmpty("Filepath", historyItem.Filepath);
writer.WriteElementIfNotEmpty("DateTimeUtc", historyItem.DateTime.ToString("o"));
writer.WriteElementIfNotEmpty("Type", historyItem.Type);
writer.WriteElementIfNotEmpty("Host", historyItem.Host);
writer.WriteElementIfNotEmpty("URL", historyItem.URL);
writer.WriteElementIfNotEmpty("ThumbnailURL", historyItem.ThumbnailURL);
writer.WriteElementIfNotEmpty("DeletionURL", historyItem.DeletionURL);
writer.WriteElementIfNotEmpty("ShortenedURL", historyItem.ShortenedURL);
writer.WriteEndElement();
}
writer.WriteWhitespace(Environment.NewLine);
}
if (!string.IsNullOrEmpty(BackupFolder))
{
if (CreateBackup)
{
Helpers.CopyFile(filePath, BackupFolder);
}
if (CreateWeeklyBackup)
{
Helpers.BackupFileWeekly(filePath, BackupFolder);
}
}
}
return true;
}
return false;
}
public abstract bool Append(string filePath, params HistoryItem[] historyItems);
public void Test(int itemCount)
{

View file

@ -0,0 +1,186 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2019 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ShareX.HistoryLib
{
public class HistoryManagerXML : HistoryManager
{
private static readonly object thisLock = new object();
public HistoryManagerXML(string filePath) : base(filePath)
{
}
public override List<HistoryItem> Load(string filePath)
{
List<HistoryItem> historyItemList = new List<HistoryItem>();
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
lock (thisLock)
{
XmlReaderSettings settings = new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Auto,
IgnoreWhitespace = true
};
using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8))
using (XmlReader reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "HistoryItem")
{
XElement element = XNode.ReadFrom(reader) as XElement;
if (element != null)
{
HistoryItem hi = ParseHistoryItem(element);
historyItemList.Add(hi);
}
}
else
{
reader.Read();
}
}
}
}
}
return historyItemList;
}
private HistoryItem ParseHistoryItem(XElement element)
{
HistoryItem hi = new HistoryItem();
foreach (XElement child in element.Elements())
{
string name = child.Name.LocalName;
switch (name)
{
case "Filename":
hi.Filename = child.Value;
break;
case "Filepath":
hi.Filepath = child.Value;
break;
case "DateTimeUtc":
DateTime dateTime;
if (DateTime.TryParse(child.Value, out dateTime))
{
hi.DateTime = dateTime;
}
break;
case "Type":
hi.Type = child.Value;
break;
case "Host":
hi.Host = child.Value;
break;
case "URL":
hi.URL = child.Value;
break;
case "ThumbnailURL":
hi.ThumbnailURL = child.Value;
break;
case "DeletionURL":
hi.DeletionURL = child.Value;
break;
case "ShortenedURL":
hi.ShortenedURL = child.Value;
break;
}
}
return hi;
}
public override bool Append(string filePath, params HistoryItem[] historyItems)
{
if (!string.IsNullOrEmpty(filePath))
{
lock (thisLock)
{
Helpers.CreateDirectoryFromFilePath(filePath);
using (FileStream fs = File.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.Read))
using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
foreach (HistoryItem historyItem in historyItems)
{
writer.WriteStartElement("HistoryItem");
writer.WriteElementIfNotEmpty("Filename", historyItem.Filename);
writer.WriteElementIfNotEmpty("Filepath", historyItem.Filepath);
writer.WriteElementIfNotEmpty("DateTimeUtc", historyItem.DateTime.ToString("o"));
writer.WriteElementIfNotEmpty("Type", historyItem.Type);
writer.WriteElementIfNotEmpty("Host", historyItem.Host);
writer.WriteElementIfNotEmpty("URL", historyItem.URL);
writer.WriteElementIfNotEmpty("ThumbnailURL", historyItem.ThumbnailURL);
writer.WriteElementIfNotEmpty("DeletionURL", historyItem.DeletionURL);
writer.WriteElementIfNotEmpty("ShortenedURL", historyItem.ShortenedURL);
writer.WriteEndElement();
}
writer.WriteWhitespace(Environment.NewLine);
}
if (!string.IsNullOrEmpty(BackupFolder))
{
if (CreateBackup)
{
Helpers.CopyFile(filePath, BackupFolder);
}
if (CreateWeeklyBackup)
{
Helpers.BackupFileWeekly(filePath, BackupFolder);
}
}
}
return true;
}
return false;
}
}
}

View file

@ -99,6 +99,7 @@
<Compile Include="Forms\ImageHistorySettingsForm.Designer.cs">
<DependentUpon>ImageHistorySettingsForm.cs</DependentUpon>
</Compile>
<Compile Include="HistoryManagerXML.cs" />
<Compile Include="HistorySettings.cs" />
<Compile Include="ImageHistorySettings.cs" />
<Compile Include="Properties\Resources.Designer.cs">

View file

@ -552,7 +552,7 @@ private static void AppendHistoryItemAsync(HistoryItem historyItem)
{
Task.Run(() =>
{
HistoryManager history = new HistoryManager(Program.HistoryFilePath)
HistoryManager history = new HistoryManagerXML(Program.HistoryFilePath)
{
BackupFolder = SettingManager.BackupFolder,
CreateBackup = false,