ShareX/ShareX.HistoryLib/HistoryManager.cs

93 lines
3 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
2018-01-02 03:59:14 +13:00
Copyright (c) 2007-2018 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;
2018-08-03 23:01:12 +12: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 class HistoryManager
{
private XMLManager manager;
2013-11-03 23:53:49 +13:00
public HistoryManager(string historyPath)
{
manager = new XMLManager(historyPath);
2013-11-03 23:53:49 +13:00
}
private bool IsValidHistoryItem(HistoryItem historyItem)
{
return historyItem != null && !string.IsNullOrEmpty(historyItem.Filename) && historyItem.DateTime != DateTime.MinValue &&
(!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.Filepath));
}
public List<HistoryItem> GetHistoryItems()
2013-11-03 23:53:49 +13:00
{
try
{
return manager.Load();
2013-11-03 23:53:49 +13:00
}
catch (Exception e)
{
DebugHelper.WriteException(e);
MessageBox.Show(string.Format(Resources.HistoryManager_GetHistoryItems_Error_occured_while_reading_XML_file___0_, manager.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
}
public bool AppendHistoryItem(HistoryItem historyItem)
2013-11-03 23:53:49 +13:00
{
try
{
if (IsValidHistoryItem(historyItem))
{
return manager.Append(historyItem);
}
2013-11-03 23:53:49 +13:00
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
return false;
2013-11-03 23:53:49 +13:00
}
public static void AddHistoryItemAsync(string historyPath, HistoryItem historyItem)
{
2018-08-03 23:01:12 +12:00
Task.Run(() =>
2013-11-03 23:53:49 +13:00
{
HistoryManager history = new HistoryManager(historyPath);
history.AppendHistoryItem(historyItem);
});
2013-11-03 23:53:49 +13:00
}
}
}