2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2014-12-31 22:41:32 +13:00
|
|
|
|
Copyright © 2007-2015 ShareX Developers
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HistoryLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
internal class XMLManager
|
|
|
|
|
{
|
2015-05-25 00:39:31 +12:00
|
|
|
|
private static readonly object thisLock = new object();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-05-25 00:39:31 +12:00
|
|
|
|
public string FilePath { get; private set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-05-25 00:39:31 +12:00
|
|
|
|
public XMLManager(string filePath)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2015-05-25 00:39:31 +12:00
|
|
|
|
FilePath = filePath;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<HistoryItem> Load()
|
|
|
|
|
{
|
|
|
|
|
List<HistoryItem> historyItemList = new List<HistoryItem>();
|
|
|
|
|
|
2015-05-25 00:39:31 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(FilePath) && File.Exists(FilePath))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
lock (thisLock)
|
|
|
|
|
{
|
|
|
|
|
XmlReaderSettings settings = new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
ConformanceLevel = ConformanceLevel.Auto,
|
|
|
|
|
IgnoreWhitespace = true
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-25 00:39:31 +12:00
|
|
|
|
using (StreamReader streamReader = new StreamReader(FilePath, Encoding.UTF8))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Append(params HistoryItem[] historyItems)
|
|
|
|
|
{
|
2015-05-25 00:39:31 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(FilePath))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
lock (thisLock)
|
|
|
|
|
{
|
2015-05-25 00:39:31 +12:00
|
|
|
|
Helpers.CreateDirectoryIfNotExist(FilePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2015-05-25 00:39:31 +12:00
|
|
|
|
using (FileStream fs = File.Open(FilePath, FileMode.Append, FileAccess.Write, FileShare.Read))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
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.DateTimeUtc.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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.DateTimeUtc = 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|