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
|
2019-01-02 20:43:52 +13:00
|
|
|
|
Copyright (c) 2007-2019 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)
|
|
|
|
|
|
2016-01-28 21:44:46 +13:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Converters;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.IO;
|
2018-08-03 23:01:12 +12:00
|
|
|
|
using System.Threading.Tasks;
|
2014-07-03 06:19:51 +12:00
|
|
|
|
using System.Windows.Forms;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
public abstract class SettingsBase<T> where T : SettingsBase<T>, new()
|
|
|
|
|
{
|
2016-02-02 03:53:32 +13:00
|
|
|
|
public delegate void SettingsSavedEventHandler(T settings, string filePath, bool result);
|
2016-02-01 11:21:09 +13:00
|
|
|
|
public event SettingsSavedEventHandler SettingsSaved;
|
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
[Browsable(false), JsonIgnore]
|
2016-01-28 21:44:46 +13:00
|
|
|
|
public string FilePath { get; private set; }
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-07-10 07:30:18 +12:00
|
|
|
|
[Browsable(false)]
|
2014-07-03 06:19:51 +12:00
|
|
|
|
public string ApplicationVersion { get; set; }
|
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
[Browsable(false), JsonIgnore]
|
2016-06-20 17:50:49 +12:00
|
|
|
|
public bool IsFirstTimeRun { get; private set; }
|
2014-07-04 01:48:35 +12:00
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
[Browsable(false), JsonIgnore]
|
2016-06-20 17:50:49 +12:00
|
|
|
|
public bool IsUpgrade { get; private set; }
|
2014-07-04 01:48:35 +12:00
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
[Browsable(false), JsonIgnore]
|
|
|
|
|
public string BackupFolder { get; set; }
|
|
|
|
|
|
|
|
|
|
[Browsable(false), JsonIgnore]
|
|
|
|
|
public bool CreateBackup { get; set; }
|
|
|
|
|
|
|
|
|
|
[Browsable(false), JsonIgnore]
|
|
|
|
|
public bool CreateWeeklyBackup { get; set; }
|
|
|
|
|
|
2017-02-12 04:56:47 +13:00
|
|
|
|
public bool IsUpgradeFrom(string version)
|
|
|
|
|
{
|
|
|
|
|
return IsUpgrade && Helpers.CompareVersion(ApplicationVersion, version) <= 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 03:53:32 +13:00
|
|
|
|
protected virtual void OnSettingsSaved(string filePath, bool result)
|
2016-02-01 11:21:09 +13:00
|
|
|
|
{
|
|
|
|
|
if (SettingsSaved != null)
|
2016-02-02 03:53:32 +13:00
|
|
|
|
{
|
|
|
|
|
SettingsSaved((T)this, filePath, result);
|
|
|
|
|
}
|
2016-02-01 11:21:09 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 01:48:35 +12:00
|
|
|
|
public bool Save(string filePath)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
FilePath = filePath;
|
2014-07-03 06:19:51 +12:00
|
|
|
|
ApplicationVersion = Application.ProductVersion;
|
2016-01-28 21:44:46 +13:00
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
bool result = SaveInternal(FilePath);
|
2016-02-02 03:53:32 +13:00
|
|
|
|
|
|
|
|
|
OnSettingsSaved(FilePath, result);
|
2016-02-01 23:39:29 +13:00
|
|
|
|
|
|
|
|
|
return result;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 21:44:46 +13:00
|
|
|
|
public bool Save()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-01-28 21:44:46 +13:00
|
|
|
|
return Save(FilePath);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveAsync(string filePath)
|
|
|
|
|
{
|
2018-08-03 23:01:12 +12:00
|
|
|
|
Task.Run(() => Save(filePath));
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 11:43:04 +12:00
|
|
|
|
public void SaveAsync()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
SaveAsync(FilePath);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
public static T Load(string filePath, string backupFolder = null, bool createBackup = false, bool createWeeklyBackup = false)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2018-08-03 22:40:00 +12:00
|
|
|
|
T setting = LoadInternal(filePath, backupFolder);
|
2016-01-28 21:44:46 +13:00
|
|
|
|
|
|
|
|
|
if (setting != null)
|
|
|
|
|
{
|
|
|
|
|
setting.FilePath = filePath;
|
2016-06-20 17:50:49 +12:00
|
|
|
|
setting.IsFirstTimeRun = string.IsNullOrEmpty(setting.ApplicationVersion);
|
|
|
|
|
setting.IsUpgrade = !setting.IsFirstTimeRun && Helpers.CompareApplicationVersion(setting.ApplicationVersion) < 0;
|
2018-08-03 22:40:00 +12:00
|
|
|
|
setting.BackupFolder = backupFolder;
|
|
|
|
|
setting.CreateBackup = createBackup;
|
|
|
|
|
setting.CreateWeeklyBackup = createWeeklyBackup;
|
2016-01-28 21:44:46 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return setting;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
private bool SaveInternal(string filePath)
|
2016-01-28 21:44:46 +13:00
|
|
|
|
{
|
2018-08-03 22:40:00 +12:00
|
|
|
|
string typeName = GetType().Name;
|
2016-01-28 21:44:46 +13:00
|
|
|
|
DebugHelper.WriteLine("{0} save started: {1}", typeName, filePath);
|
|
|
|
|
|
|
|
|
|
bool isSuccess = false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(filePath))
|
|
|
|
|
{
|
2018-08-03 22:40:00 +12:00
|
|
|
|
lock (this)
|
2016-01-28 21:44:46 +13:00
|
|
|
|
{
|
2016-02-22 12:15:23 +13:00
|
|
|
|
Helpers.CreateDirectoryFromFilePath(filePath);
|
2016-01-28 21:44:46 +13:00
|
|
|
|
|
|
|
|
|
string tempFilePath = filePath + ".temp";
|
|
|
|
|
|
|
|
|
|
using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
|
|
|
using (StreamWriter streamWriter = new StreamWriter(fileStream))
|
|
|
|
|
using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
|
|
|
|
|
{
|
2017-06-12 08:01:11 +12:00
|
|
|
|
jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
|
2016-01-28 21:44:46 +13:00
|
|
|
|
jsonWriter.Formatting = Formatting.Indented;
|
2017-06-12 08:01:11 +12:00
|
|
|
|
|
2016-01-28 21:44:46 +13:00
|
|
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
|
|
|
serializer.ContractResolver = new WritablePropertiesOnlyResolver();
|
|
|
|
|
serializer.Converters.Add(new StringEnumConverter());
|
2018-08-03 22:40:00 +12:00
|
|
|
|
serializer.Serialize(jsonWriter, this);
|
2016-01-28 21:44:46 +13:00
|
|
|
|
jsonWriter.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (File.Exists(filePath))
|
|
|
|
|
{
|
2018-08-03 22:40:00 +12:00
|
|
|
|
if (CreateBackup)
|
2016-01-28 21:44:46 +13:00
|
|
|
|
{
|
2018-08-03 22:40:00 +12:00
|
|
|
|
Helpers.CopyFile(filePath, BackupFolder);
|
2016-01-28 21:44:46 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Delete(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Move(tempFilePath, filePath);
|
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder))
|
|
|
|
|
{
|
|
|
|
|
Helpers.BackupFileWeekly(filePath, BackupFolder);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 21:44:46 +13:00
|
|
|
|
isSuccess = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteLine("{0} save {1}: {2}", typeName, isSuccess ? "successful" : "failed", filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
private static T LoadInternal(string filePath, string backupFolder = null)
|
2016-01-28 21:44:46 +13:00
|
|
|
|
{
|
|
|
|
|
string typeName = typeof(T).Name;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(filePath))
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteLine("{0} load started: {1}", typeName, filePath);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
|
|
|
{
|
|
|
|
|
if (fileStream.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
T settings;
|
|
|
|
|
|
|
|
|
|
using (StreamReader streamReader = new StreamReader(fileStream))
|
|
|
|
|
using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
|
|
|
|
|
{
|
2017-06-12 08:01:11 +12:00
|
|
|
|
jsonReader.DateTimeZoneHandling = DateTimeZoneHandling.Local;
|
|
|
|
|
|
2016-01-28 21:44:46 +13:00
|
|
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
|
|
|
serializer.Converters.Add(new StringEnumConverter());
|
|
|
|
|
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
|
|
|
|
|
serializer.Error += (sender, e) => e.ErrorContext.Handled = true;
|
|
|
|
|
settings = serializer.Deserialize<T>(jsonReader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (settings == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(typeName + " object is null.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DebugHelper.WriteLine("{0} load finished: {1}", typeName, filePath);
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, typeName + " load failed: " + filePath);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 22:40:00 +12:00
|
|
|
|
if (!string.IsNullOrEmpty(backupFolder))
|
2016-01-28 21:44:46 +13:00
|
|
|
|
{
|
2018-08-03 22:40:00 +12:00
|
|
|
|
string fileName = Path.GetFileName(filePath);
|
|
|
|
|
string backupFilePath = Path.Combine(backupFolder, fileName);
|
|
|
|
|
return LoadInternal(backupFilePath);
|
2016-01-28 21:44:46 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DebugHelper.WriteLine("{0} not found. Loading new instance.", typeName);
|
|
|
|
|
|
|
|
|
|
return new T();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|