diff --git a/ShareX.HistoryLib/Enums.cs b/ShareX.HistoryLib/Enums.cs deleted file mode 100644 index ea7968d25..000000000 --- a/ShareX.HistoryLib/Enums.cs +++ /dev/null @@ -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 . -*/ - -#endregion License Information (GPL v3) - -namespace ShareX.HistoryLib -{ - public enum HistoryRefreshInfoResult - { - Success, - Same, - Invalid - } -} \ No newline at end of file diff --git a/ShareX.HistoryLib/Forms/HistoryForm.cs b/ShareX.HistoryLib/Forms/HistoryForm.cs index 5fa39b4af..d24c5f374 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.cs +++ b/ShareX.HistoryLib/Forms/HistoryForm.cs @@ -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(); } } diff --git a/ShareX.HistoryLib/HistoryItemManager.cs b/ShareX.HistoryLib/HistoryItemManager.cs index 35790e6d3..853bb979d 100644 --- a/ShareX.HistoryLib/HistoryItemManager.cs +++ b/ShareX.HistoryLib/HistoryItemManager.cs @@ -59,7 +59,7 @@ public HistoryItemManager(Action uploadFile, Action 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() diff --git a/ShareX.HistoryLib/HistoryManager.cs b/ShareX.HistoryLib/HistoryManager.cs index 62dee87ca..59439fce4 100644 --- a/ShareX.HistoryLib/HistoryManager.cs +++ b/ShareX.HistoryLib/HistoryManager.cs @@ -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 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 Load() + private List Load(string filePath) { List historyItemList = new List(); - if (!string.IsNullOrEmpty(FilePath) && File.Exists(FilePath)) + if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { lock (thisLock) { @@ -103,7 +104,7 @@ private List 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 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(); + } } } \ No newline at end of file diff --git a/ShareX.HistoryLib/ShareX.HistoryLib.csproj b/ShareX.HistoryLib/ShareX.HistoryLib.csproj index 59d8e238b..416a2f5be 100644 --- a/ShareX.HistoryLib/ShareX.HistoryLib.csproj +++ b/ShareX.HistoryLib/ShareX.HistoryLib.csproj @@ -109,7 +109,6 @@ Component - Form