Error message box in history form if xml read failed

This commit is contained in:
Jaex 2015-05-24 15:39:31 +03:00
parent a71cd2bf80
commit 2b57e1b6a1
6 changed files with 69 additions and 30 deletions

View File

@ -24,28 +24,52 @@
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using ShareX.HistoryLib.Properties;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ShareX.HistoryLib
{
public class HistoryManager
{
internal XMLManager Manager { get; private set; }
private XMLManager manager;
public HistoryManager(string historyPath)
{
Manager = new XMLManager(historyPath);
manager = new XMLManager(historyPath);
}
private bool IsValidHistoryItem(HistoryItem historyItem)
{
return historyItem != null && !string.IsNullOrEmpty(historyItem.Filename) && historyItem.DateTimeUtc != DateTime.MinValue &&
(!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.Filepath));
}
public List<HistoryItem> GetHistoryItems()
{
try
{
return manager.Load();
}
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);
}
return new List<HistoryItem>();
}
public bool AppendHistoryItem(HistoryItem historyItem)
{
try
{
if (historyItem != null && !string.IsNullOrEmpty(historyItem.Filename) && historyItem.DateTimeUtc != DateTime.MinValue &&
(!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.Filepath)))
if (IsValidHistoryItem(historyItem))
{
return Manager.Append(historyItem);
return manager.Append(historyItem);
}
}
catch (Exception e)
@ -56,20 +80,6 @@ namespace ShareX.HistoryLib
return false;
}
public List<HistoryItem> GetHistoryItems()
{
try
{
return Manager.Load();
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
return new List<HistoryItem>();
}
public static void AddHistoryItemAsync(string historyPath, HistoryItem historyItem)
{
TaskEx.Run(() =>

View File

@ -104,8 +104,7 @@ namespace ShareX.HistoryLib
private HistoryItem[] GetHistoryItems()
{
IEnumerable<HistoryItem> tempHistoryItems = history.GetHistoryItems().Where(x => !string.IsNullOrEmpty(x.Filepath) &&
Helpers.IsImageFile(x.Filepath) && File.Exists(x.Filepath));
IEnumerable<HistoryItem> tempHistoryItems = history.GetHistoryItems().Where(x => !string.IsNullOrEmpty(x.Filepath) && Helpers.IsImageFile(x.Filepath) && File.Exists(x.Filepath));
if (MaxItemCount > -1)
{

View File

@ -312,6 +312,24 @@ namespace ShareX.HistoryLib.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Error.
/// </summary>
internal static string HistoryManager_GetHistoryItems_Error {
get {
return ResourceManager.GetString("HistoryManager_GetHistoryItems_Error", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Error occured while reading XML file: {0}.
/// </summary>
internal static string HistoryManager_GetHistoryItems_Error_occured_while_reading_XML_file___0_ {
get {
return ResourceManager.GetString("HistoryManager_GetHistoryItems_Error_occured_while_reading_XML_file___0_", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Copy name.
/// </summary>

View File

@ -193,4 +193,10 @@
<data name="HistoryForm_UpdateItemCount___Filtered___0_" xml:space="preserve">
<value>Filtered: {0}</value>
</data>
<data name="HistoryManager_GetHistoryItems_Error_occured_while_reading_XML_file___0_" xml:space="preserve">
<value>Error occured while reading XML file: {0}</value>
</data>
<data name="HistoryManager_GetHistoryItems_Error" xml:space="preserve">
<value>Error</value>
</data>
</root>

View File

@ -213,4 +213,10 @@
<data name="ObjectListView_ObjectListView_Value" xml:space="preserve">
<value>Değer</value>
</data>
<data name="HistoryManager_GetHistoryItems_Error" xml:space="preserve">
<value>Hata</value>
</data>
<data name="HistoryManager_GetHistoryItems_Error_occured_while_reading_XML_file___0_" xml:space="preserve">
<value>XML dosyası okunurken hata oluştu: {0}</value>
</data>
</root>

View File

@ -35,20 +35,20 @@ namespace ShareX.HistoryLib
{
internal class XMLManager
{
private static object thisLock = new object();
private static readonly object thisLock = new object();
private string xmlPath;
public string FilePath { get; private set; }
public XMLManager(string xmlFilePath)
public XMLManager(string filePath)
{
xmlPath = xmlFilePath;
FilePath = filePath;
}
public List<HistoryItem> Load()
{
List<HistoryItem> historyItemList = new List<HistoryItem>();
if (!string.IsNullOrEmpty(xmlPath) && File.Exists(xmlPath))
if (!string.IsNullOrEmpty(FilePath) && File.Exists(FilePath))
{
lock (thisLock)
{
@ -58,7 +58,7 @@ namespace ShareX.HistoryLib
IgnoreWhitespace = true
};
using (StreamReader streamReader = new StreamReader(xmlPath, Encoding.UTF8))
using (StreamReader streamReader = new StreamReader(FilePath, Encoding.UTF8))
using (XmlReader reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
@ -89,13 +89,13 @@ namespace ShareX.HistoryLib
public bool Append(params HistoryItem[] historyItems)
{
if (!string.IsNullOrEmpty(xmlPath))
if (!string.IsNullOrEmpty(FilePath))
{
lock (thisLock)
{
Helpers.CreateDirectoryIfNotExist(xmlPath);
Helpers.CreateDirectoryIfNotExist(FilePath);
using (FileStream fs = File.Open(xmlPath, FileMode.Append, FileAccess.Write, FileShare.Read))
using (FileStream fs = File.Open(FilePath, FileMode.Append, FileAccess.Write, FileShare.Read))
using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;