Added test function to history manager

This commit is contained in:
Jaex 2019-10-21 09:36:30 +03:00
parent 0c784b5dec
commit 100f3cc447
5 changed files with 106 additions and 104 deletions

View file

@ -1,34 +0,0 @@
#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)
namespace ShareX.HistoryLib
{
public enum HistoryRefreshInfoResult
{
Success,
Same,
Invalid
}
}

View file

@ -312,14 +312,13 @@ private void UpdateTitle(HistoryItem[] historyItems = null)
private void UpdateControls()
{
switch (him.RefreshInfo())
if (him.RefreshInfo())
{
case HistoryRefreshInfoResult.Success:
UpdatePictureBox();
break;
case HistoryRefreshInfoResult.Invalid:
pbThumbnail.Reset();
break;
UpdatePictureBox();
}
else
{
pbThumbnail.Reset();
}
}

View file

@ -59,7 +59,7 @@ public HistoryItemManager(Action<string> uploadFile, Action<string> editImage)
InitializeComponent();
}
public HistoryRefreshInfoResult RefreshInfo()
public bool RefreshInfo()
{
HistoryItem tempHistoryItem = GetSelectedHistoryItem();
@ -81,14 +81,12 @@ public HistoryRefreshInfoResult RefreshInfo()
IsTextFile = IsFileExist && Helpers.IsTextFile(HistoryItem.Filepath);
UpdateButtons();
return HistoryRefreshInfoResult.Success;
return true;
}
return HistoryRefreshInfoResult.Same;
}
cmsHistory.Enabled = false;
return HistoryRefreshInfoResult.Invalid;
return false;
}
private HistoryItem GetSelectedHistoryItem()

View file

@ -29,6 +29,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
@ -53,7 +54,7 @@ public List<HistoryItem> GetHistoryItems()
{
try
{
return Load();
return Load(FilePath);
}
catch (Exception e)
{
@ -72,7 +73,7 @@ public bool AppendHistoryItem(HistoryItem historyItem)
{
if (IsValidHistoryItem(historyItem))
{
return Append(historyItem);
return Append(FilePath, historyItem);
}
}
catch (Exception e)
@ -89,11 +90,11 @@ private bool IsValidHistoryItem(HistoryItem historyItem)
(!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.Filepath));
}
private List<HistoryItem> Load()
private List<HistoryItem> Load(string filePath)
{
List<HistoryItem> historyItemList = new List<HistoryItem>();
if (!string.IsNullOrEmpty(FilePath) && File.Exists(FilePath))
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
lock (thisLock)
{
@ -103,7 +104,7 @@ private List<HistoryItem> Load()
IgnoreWhitespace = true
};
using (StreamReader streamReader = new StreamReader(FilePath, Encoding.UTF8))
using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8))
using (XmlReader reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
@ -132,58 +133,6 @@ private List<HistoryItem> Load()
return historyItemList;
}
private bool Append(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;
}
private HistoryItem ParseHistoryItem(XElement element)
{
HistoryItem hi = new HistoryItem();
@ -230,5 +179,96 @@ private HistoryItem ParseHistoryItem(XElement element)
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 void Test(int itemCount)
{
Test(FilePath, itemCount);
}
public void Test(string filePath, int itemCount)
{
HistoryItem historyItem = new HistoryItem()
{
Filename = "Example.png",
Filepath = @"C:\ShareX\Screenshots\Example.png",
DateTime = DateTime.Now,
Type = "Image",
Host = "Imgur",
URL = "https://example.com/Example.png",
ThumbnailURL = "https://example.com/Example.png",
DeletionURL = "https://example.com/Example.png",
ShortenedURL = "https://example.com/Example.png"
};
HistoryItem[] historyItems = new HistoryItem[itemCount];
for (int i = 0; i < itemCount; i++)
{
historyItems[i] = historyItem;
}
Thread.Sleep(1000);
DebugTimer saveTimer = new DebugTimer($"Saved {itemCount} items");
Append(filePath, historyItems);
saveTimer.WriteElapsedMilliseconds();
Thread.Sleep(1000);
DebugTimer loadTimer = new DebugTimer($"Loaded {itemCount} items");
Load(filePath);
loadTimer.WriteElapsedMilliseconds();
}
}
}

View file

@ -109,7 +109,6 @@
<Compile Include="UserControls\ObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Enums.cs" />
<Compile Include="Forms\HistoryForm.cs">
<SubType>Form</SubType>
</Compile>