diff --git a/ShareX.HelpersLib/Helpers/Helpers.cs b/ShareX.HelpersLib/Helpers/Helpers.cs index 5a1bb82b8..8fce9472c 100644 --- a/ShareX.HelpersLib/Helpers/Helpers.cs +++ b/ShareX.HelpersLib/Helpers/Helpers.cs @@ -177,26 +177,6 @@ public static string ChangeFilenameExtension(string filePath, string extension) return filePath; } - public static string RenameFile(string filePath, string newFileName) - { - try - { - if (File.Exists(filePath)) - { - string directory = Path.GetDirectoryName(filePath); - string newPath = Path.Combine(directory, newFileName); - File.Move(filePath, newPath); - return newPath; - } - } - catch (Exception e) - { - MessageBox.Show("Rename error:\r\n" + e.ToString(), "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); - } - - return filePath; - } - public static string AppendExtension(string filePath, string extension) { return filePath.TrimEnd('.') + '.' + extension.TrimStart('.'); @@ -966,15 +946,58 @@ public static bool IsValidFilePath(string path) return fi != null; } - public static void CopyFile(string filePath, string destinationFolder, bool overwrite = true) + public static string CopyFile(string filePath, string destinationFolder, bool overwrite = true) { - if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(destinationFolder)) + if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath) && !string.IsNullOrEmpty(destinationFolder)) { string fileName = Path.GetFileName(filePath); string destinationFilePath = Path.Combine(destinationFolder, fileName); CreateDirectoryFromDirectoryPath(destinationFolder); File.Copy(filePath, destinationFilePath, overwrite); + return destinationFilePath; } + + return null; + } + + public static string MoveFile(string filePath, string destinationFolder, bool overwrite = true) + { + if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath) && !string.IsNullOrEmpty(destinationFolder)) + { + string fileName = Path.GetFileName(filePath); + string destinationFilePath = Path.Combine(destinationFolder, fileName); + CreateDirectoryFromDirectoryPath(destinationFolder); + + if (overwrite && File.Exists(destinationFilePath)) + { + File.Delete(destinationFilePath); + } + + File.Move(filePath, destinationFilePath); + return destinationFilePath; + } + + return null; + } + + public static string RenameFile(string filePath, string newFileName) + { + try + { + if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) + { + string directory = Path.GetDirectoryName(filePath); + string newFilePath = Path.Combine(directory, newFileName); + File.Move(filePath, newFilePath); + return newFilePath; + } + } + catch (Exception e) + { + MessageBox.Show("Rename file error:\r\n" + e.ToString(), "ShareX - " + Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + return filePath; } public static string BackupFileWeekly(string filePath, string destinationFolder) diff --git a/ShareX.HistoryLib/Forms/HistoryForm.cs b/ShareX.HistoryLib/Forms/HistoryForm.cs index 41afa90e7..a43058242 100644 --- a/ShareX.HistoryLib/Forms/HistoryForm.cs +++ b/ShareX.HistoryLib/Forms/HistoryForm.cs @@ -154,7 +154,7 @@ private HistoryItem[] GetHistoryItems() { if (history == null) { - history = new HistoryManagerXML(HistoryPath); + history = new HistoryManagerJSON(HistoryPath); } IEnumerable tempHistoryItems = history.GetHistoryItems(); diff --git a/ShareX.HistoryLib/Forms/ImageHistoryForm.cs b/ShareX.HistoryLib/Forms/ImageHistoryForm.cs index a4697e951..8d3ae4dd6 100644 --- a/ShareX.HistoryLib/Forms/ImageHistoryForm.cs +++ b/ShareX.HistoryLib/Forms/ImageHistoryForm.cs @@ -113,7 +113,7 @@ private IEnumerable GetHistoryItems() { if (history == null) { - history = new HistoryManagerXML(HistoryPath); + history = new HistoryManagerJSON(HistoryPath); } List historyItems = history.GetHistoryItems(); diff --git a/ShareX.HistoryLib/HistoryManager.cs b/ShareX.HistoryLib/HistoryManager.cs index 936275b95..901114eb8 100644 --- a/ShareX.HistoryLib/HistoryManager.cs +++ b/ShareX.HistoryLib/HistoryManager.cs @@ -87,19 +87,19 @@ private bool IsValidHistoryItem(HistoryItem historyItem) (!string.IsNullOrEmpty(historyItem.URL) || !string.IsNullOrEmpty(historyItem.FilePath)); } - public List Load() + protected List Load() { return Load(FilePath); } - public abstract List Load(string filePath); + protected abstract List Load(string filePath); - public bool Append(IEnumerable historyItems) + protected bool Append(IEnumerable historyItems) { return Append(FilePath, historyItems); } - public abstract bool Append(string filePath, IEnumerable historyItems); + protected abstract bool Append(string filePath, IEnumerable historyItems); protected void Backup(string filePath) { diff --git a/ShareX.HistoryLib/HistoryManagerJSON.cs b/ShareX.HistoryLib/HistoryManagerJSON.cs index 081b8c854..58ad3d571 100644 --- a/ShareX.HistoryLib/HistoryManagerJSON.cs +++ b/ShareX.HistoryLib/HistoryManagerJSON.cs @@ -40,7 +40,7 @@ public HistoryManagerJSON(string filePath) : base(filePath) { } - public override List Load(string filePath) + protected override List Load(string filePath) { if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { @@ -60,7 +60,7 @@ public override List Load(string filePath) return new List(); } - public override bool Append(string filePath, IEnumerable historyItems) + protected override bool Append(string filePath, IEnumerable historyItems) { if (!string.IsNullOrEmpty(filePath)) { diff --git a/ShareX.HistoryLib/HistoryManagerXML.cs b/ShareX.HistoryLib/HistoryManagerXML.cs index bb5860ea8..e902b0377 100644 --- a/ShareX.HistoryLib/HistoryManagerXML.cs +++ b/ShareX.HistoryLib/HistoryManagerXML.cs @@ -41,7 +41,7 @@ public HistoryManagerXML(string filePath) : base(filePath) { } - public override List Load(string filePath) + protected override List Load(string filePath) { List historyItemList = new List(); @@ -131,7 +131,7 @@ private HistoryItem ParseHistoryItem(XElement element) return hi; } - public override bool Append(string filePath, IEnumerable historyItems) + protected override bool Append(string filePath, IEnumerable historyItems) { if (!string.IsNullOrEmpty(filePath)) { diff --git a/ShareX/Program.cs b/ShareX/Program.cs index 74e947907..8c90174c2 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -148,7 +148,7 @@ public static string PersonalFolder } } - public const string HistoryFilename = "History.xml"; + public const string HistoryFilename = "History.json"; public static string HistoryFilePath { @@ -160,6 +160,18 @@ public static string HistoryFilePath } } + public const string HistoryFilenameOld = "History.xml"; + + public static string HistoryFilePathOld + { + get + { + if (Sandbox) return null; + + return Path.Combine(PersonalFolder, HistoryFilenameOld); + } + } + public const string LogsFoldername = "Logs"; public static string LogsFolder => Path.Combine(PersonalFolder, LogsFoldername); diff --git a/ShareX/SettingManager.cs b/ShareX/SettingManager.cs index a2f1142cf..bef1298cf 100644 --- a/ShareX/SettingManager.cs +++ b/ShareX/SettingManager.cs @@ -24,6 +24,7 @@ #endregion License Information (GPL v3) using ShareX.HelpersLib; +using ShareX.HistoryLib; using ShareX.ScreenCaptureLib; using ShareX.UploadersLib; using ShareX.UploadersLib.FileUploaders; @@ -145,6 +146,7 @@ public static void LoadApplicationConfig() Settings.SettingsSaveFailed += Settings_SettingsSaveFailed; DefaultTaskSettings = Settings.DefaultTaskSettings; ApplicationConfigBackwardCompatibilityTasks(); + MigrateHistoryFile(); } private static void Settings_SettingsSaveFailed(Exception e) @@ -209,6 +211,28 @@ private static void ApplicationConfigBackwardCompatibilityTasks() } } + private static void MigrateHistoryFile() + { + if (File.Exists(Program.HistoryFilePathOld)) + { + if (!File.Exists(Program.HistoryFilePath)) + { + DebugHelper.WriteLine($"Migrating XML history file \"{Program.HistoryFilePathOld}\" to JSON history file \"{Program.HistoryFilePath}\""); + + HistoryManagerXML historyManagerXML = new HistoryManagerXML(Program.HistoryFilePathOld); + List historyItems = historyManagerXML.GetHistoryItems(); + + if (historyItems.Count > 0) + { + HistoryManagerJSON historyManagerJSON = new HistoryManagerJSON(Program.HistoryFilePath); + historyManagerJSON.AppendHistoryItems(historyItems); + } + } + + Helpers.MoveFile(Program.HistoryFilePathOld, BackupFolder); + } + } + private static void UploadersConfigBackwardCompatibilityTasks() { if (UploadersConfig.IsUpgradeFrom("11.6.0")) diff --git a/ShareX/TaskManager.cs b/ShareX/TaskManager.cs index 8c4855350..69fd276b3 100644 --- a/ShareX/TaskManager.cs +++ b/ShareX/TaskManager.cs @@ -552,7 +552,7 @@ private static void AppendHistoryItemAsync(HistoryItem historyItem) { Task.Run(() => { - HistoryManager history = new HistoryManagerXML(Program.HistoryFilePath) + HistoryManager history = new HistoryManagerJSON(Program.HistoryFilePath) { BackupFolder = SettingManager.BackupFolder, CreateBackup = false,