ShareX/ShareX.HistoryLib/HistoryManager.cs

165 lines
5.2 KiB
C#
Raw Normal View History

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
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
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;
using ShareX.HistoryLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.Collections.Generic;
2019-10-22 01:43:44 +13:00
using System.Linq;
2019-10-21 19:36:30 +13:00
using System.Threading;
2022-01-01 11:36:47 +13:00
using System.Threading.Tasks;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.HistoryLib
2013-11-03 23:53:49 +13:00
{
public abstract class HistoryManager
2013-11-03 23:53:49 +13:00
{
2018-08-04 02:52:27 +12:00
public string FilePath { get; private set; }
public string BackupFolder { get; set; }
public bool CreateBackup { get; set; }
public bool CreateWeeklyBackup { get; set; }
2013-11-03 23:53:49 +13:00
2018-08-04 02:52:27 +12:00
public HistoryManager(string filePath)
{
2018-08-04 02:52:27 +12:00
FilePath = filePath;
}
public List<HistoryItem> GetHistoryItems()
2013-11-03 23:53:49 +13:00
{
try
{
return Load();
2013-11-03 23:53:49 +13:00
}
catch (Exception e)
{
DebugHelper.WriteException(e);
2020-02-15 19:45:14 +13:00
MessageBox.Show(Resources.ErrorOccuredWhileReadingHistoryFile + " " + FilePath + "\r\n\r\n" + e,
"ShareX - " + Resources.HistoryManager_GetHistoryItems_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
2013-11-03 23:53:49 +13:00
}
return new List<HistoryItem>();
2013-11-03 23:53:49 +13:00
}
2022-01-01 11:36:47 +13:00
public async Task<List<HistoryItem>> GetHistoryItemsAsync()
{
return await Task.Run(() => GetHistoryItems());
}
public bool AppendHistoryItem(HistoryItem historyItem)
2019-10-22 01:43:44 +13:00
{
return AppendHistoryItems(new HistoryItem[] { historyItem });
}
public bool AppendHistoryItems(IEnumerable<HistoryItem> historyItems)
2013-11-03 23:53:49 +13:00
{
try
{
2019-10-22 01:43:44 +13:00
return Append(historyItems.Where(x => IsValidHistoryItem(x)));
2013-11-03 23:53:49 +13:00
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
return false;
2013-11-03 23:53:49 +13:00
}
2018-08-04 02:52:27 +12:00
private bool IsValidHistoryItem(HistoryItem historyItem)
{
2019-10-22 00:20:21 +13:00
return historyItem != null && !string.IsNullOrEmpty(historyItem.FileName) && historyItem.DateTime != DateTime.MinValue &&
(!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.FilePath));
2018-08-04 02:52:27 +12:00
}
protected List<HistoryItem> Load()
2018-08-04 02:52:27 +12:00
{
return Load(FilePath);
2018-08-04 02:52:27 +12:00
}
protected abstract List<HistoryItem> Load(string filePath);
2019-10-21 19:36:30 +13:00
protected bool Append(IEnumerable<HistoryItem> historyItems)
2018-08-04 02:52:27 +12:00
{
return Append(FilePath, historyItems);
2018-08-04 02:52:27 +12:00
}
protected abstract bool Append(string filePath, IEnumerable<HistoryItem> historyItems);
2019-10-22 00:18:23 +13:00
protected void Backup(string filePath)
{
if (!string.IsNullOrEmpty(BackupFolder))
{
if (CreateBackup)
{
FileHelpers.CopyFile(filePath, BackupFolder);
2019-10-22 00:18:23 +13:00
}
if (CreateWeeklyBackup)
{
FileHelpers.BackupFileWeekly(filePath, BackupFolder);
2019-10-22 00:18:23 +13:00
}
}
}
2019-10-21 19:36:30 +13:00
public void Test(int itemCount)
2018-08-04 02:52:27 +12:00
{
2019-10-21 19:36:30 +13:00
Test(FilePath, itemCount);
}
2018-08-04 02:52:27 +12:00
2019-10-21 19:36:30 +13:00
public void Test(string filePath, int itemCount)
{
HistoryItem historyItem = new HistoryItem()
2018-08-04 02:52:27 +12:00
{
2019-10-22 00:20:21 +13:00
FileName = "Example.png",
FilePath = @"C:\ShareX\Screenshots\Example.png",
2019-10-21 19:36:30 +13:00
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;
2018-08-04 02:52:27 +12:00
}
2019-10-21 19:36:30 +13:00
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();
2018-08-04 02:52:27 +12:00
}
2013-11-03 23:53:49 +13:00
}
}