2014-06-07 05:21:03 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2014-06-07 05:21:03 +12: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)
|
|
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Converters;
|
2014-12-11 12:19:28 +13:00
|
|
|
|
using ShareX.HelpersLib.Properties;
|
2014-06-07 05:21:03 +12:00
|
|
|
|
using System;
|
2015-12-22 03:31:59 +13:00
|
|
|
|
using System.ComponentModel;
|
2014-06-07 05:21:03 +12:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2018-08-04 01:38:18 +12:00
|
|
|
|
using System.Threading.Tasks;
|
2014-06-07 05:21:03 +12:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2014-06-07 05:21:03 +12:00
|
|
|
|
{
|
|
|
|
|
public partial class ExportImportControl : UserControl
|
|
|
|
|
{
|
|
|
|
|
public delegate object ExportEventHandler();
|
|
|
|
|
public event ExportEventHandler ExportRequested;
|
|
|
|
|
|
|
|
|
|
public delegate void ImportEventHandler(object obj);
|
|
|
|
|
public event ImportEventHandler ImportRequested;
|
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
public event Action ImportCompleted;
|
|
|
|
|
|
2014-06-07 06:51:09 +12:00
|
|
|
|
public delegate void UploadEventHandler(string json);
|
|
|
|
|
public static event UploadEventHandler UploadRequested;
|
|
|
|
|
|
2014-06-07 05:21:03 +12:00
|
|
|
|
// Can't use generic class because not works in form designer
|
|
|
|
|
public Type ObjectType { get; set; }
|
|
|
|
|
|
2017-09-06 11:18:36 +12:00
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
public bool ExportIgnoreDefaultValue { get; set; }
|
|
|
|
|
|
2015-12-22 03:31:59 +13:00
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
public bool ExportIgnoreNull { get; set; }
|
|
|
|
|
|
2017-01-17 09:49:08 +13:00
|
|
|
|
[DefaultValue("")]
|
|
|
|
|
public string CustomFilter { get; set; } = "";
|
|
|
|
|
|
2018-01-02 20:38:28 +13:00
|
|
|
|
public string DefaultFileName { get; set; }
|
|
|
|
|
|
2014-06-07 05:21:03 +12:00
|
|
|
|
public ExportImportControl()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-22 04:44:28 +13:00
|
|
|
|
public string Serialize(object obj)
|
2014-06-07 05:21:03 +12:00
|
|
|
|
{
|
|
|
|
|
if (obj != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder(256);
|
|
|
|
|
StringWriter stringWriter = new StringWriter(sb, CultureInfo.InvariantCulture);
|
|
|
|
|
|
|
|
|
|
using (JsonTextWriter textWriter = new JsonTextWriter(stringWriter))
|
|
|
|
|
{
|
|
|
|
|
textWriter.Formatting = Formatting.Indented;
|
|
|
|
|
|
|
|
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
|
|
|
serializer.ContractResolver = new WritablePropertiesOnlyResolver();
|
|
|
|
|
serializer.Converters.Add(new StringEnumConverter());
|
2017-09-06 11:18:36 +12:00
|
|
|
|
serializer.DefaultValueHandling = ExportIgnoreDefaultValue ? DefaultValueHandling.Ignore : DefaultValueHandling.Include;
|
2015-12-22 03:31:59 +13:00
|
|
|
|
serializer.NullValueHandling = ExportIgnoreNull ? NullValueHandling.Ignore : NullValueHandling.Include;
|
2014-06-07 05:21:03 +12:00
|
|
|
|
serializer.TypeNameHandling = TypeNameHandling.Auto;
|
|
|
|
|
serializer.Serialize(textWriter, obj, ObjectType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stringWriter.ToString();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
2015-02-17 23:46:00 +13:00
|
|
|
|
MessageBox.Show(Resources.ExportImportControl_Serialize_Export_failed_ + "\n\n" + e, "ShareX - " + Resources.Error,
|
2014-10-18 00:08:33 +13:00
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2014-06-07 05:21:03 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void tsmiExportClipboard_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (ExportRequested != null)
|
|
|
|
|
{
|
|
|
|
|
object obj = ExportRequested();
|
|
|
|
|
|
2014-06-21 00:05:04 +12:00
|
|
|
|
string json = Serialize(obj);
|
2014-06-07 05:21:03 +12:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(json) && ClipboardHelpers.CopyText(json))
|
|
|
|
|
{
|
2014-10-18 00:08:33 +13:00
|
|
|
|
MessageBox.Show(Resources.ExportImportControl_tsmiExportClipboard_Click_Settings_copied_to_your_clipboard_, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
2014-06-07 05:21:03 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void tsmiExportFile_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (ExportRequested != null)
|
|
|
|
|
{
|
|
|
|
|
object obj = ExportRequested();
|
|
|
|
|
|
2014-06-21 00:05:04 +12:00
|
|
|
|
string json = Serialize(obj);
|
2014-06-07 05:21:03 +12:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
|
|
|
|
{
|
2017-01-17 09:49:08 +13:00
|
|
|
|
string filter = "Settings (*.json)|*.json";
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(CustomFilter))
|
|
|
|
|
{
|
|
|
|
|
filter = CustomFilter + "|" + filter;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 20:38:28 +13:00
|
|
|
|
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = filter, FileName = DefaultFileName })
|
2014-06-07 05:21:03 +12:00
|
|
|
|
{
|
|
|
|
|
if (sfd.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
File.WriteAllText(sfd.FileName, json, Encoding.UTF8);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 06:51:09 +12:00
|
|
|
|
private void tsmiExportUpload_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (ExportRequested != null && UploadRequested != null)
|
|
|
|
|
{
|
|
|
|
|
object obj = ExportRequested();
|
|
|
|
|
|
2014-06-21 00:05:04 +12:00
|
|
|
|
string json = Serialize(obj);
|
2014-06-07 06:51:09 +12:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
|
|
|
|
{
|
|
|
|
|
UploadRequested(json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-22 04:44:28 +13:00
|
|
|
|
public object Deserialize(string json)
|
2014-06-07 05:21:03 +12:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (JsonTextReader textReader = new JsonTextReader(new StringReader(json)))
|
|
|
|
|
{
|
|
|
|
|
JsonSerializer serializer = new JsonSerializer();
|
|
|
|
|
serializer.Converters.Add(new StringEnumConverter());
|
|
|
|
|
serializer.Error += (sender, e) => e.ErrorContext.Handled = true;
|
2014-09-08 18:40:04 +12:00
|
|
|
|
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
|
2014-06-07 06:35:18 +12:00
|
|
|
|
serializer.TypeNameHandling = TypeNameHandling.Auto;
|
2014-06-07 05:21:03 +12:00
|
|
|
|
return serializer.Deserialize(textReader, ObjectType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e);
|
2015-02-17 23:46:00 +13:00
|
|
|
|
MessageBox.Show(Resources.ExportImportControl_Deserialize_Import_failed_ + "\n\n" + e, "ShareX - " + Resources.Error,
|
2014-10-18 00:08:33 +13:00
|
|
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2014-06-07 05:21:03 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
private void OnImportRequested(string json)
|
2014-06-21 00:05:04 +12:00
|
|
|
|
{
|
2019-01-09 07:28:34 +13:00
|
|
|
|
if (ImportRequested != null)
|
2014-06-21 00:05:04 +12:00
|
|
|
|
{
|
2019-01-09 07:28:34 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
2014-06-21 00:05:04 +12:00
|
|
|
|
{
|
2019-01-09 07:28:34 +13:00
|
|
|
|
object obj = Deserialize(json);
|
|
|
|
|
|
|
|
|
|
if (obj != null)
|
|
|
|
|
{
|
|
|
|
|
ImportRequested(obj);
|
|
|
|
|
}
|
2014-06-21 00:05:04 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
private void OnImportCompleted()
|
|
|
|
|
{
|
|
|
|
|
if (ImportCompleted != null)
|
|
|
|
|
{
|
|
|
|
|
ImportCompleted();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 05:21:03 +12:00
|
|
|
|
private void tsmiImportClipboard_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2019-12-13 01:03:22 +13:00
|
|
|
|
string json = ClipboardHelpers.GetText(true);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
2014-06-07 05:21:03 +12:00
|
|
|
|
{
|
2019-01-09 07:28:34 +13:00
|
|
|
|
OnImportRequested(json);
|
|
|
|
|
OnImportCompleted();
|
2014-06-07 05:21:03 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void tsmiImportFile_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2019-01-09 07:28:34 +13:00
|
|
|
|
string filter = "Settings (*.json)|*.json|All files (*.*)|*.*";
|
2017-01-17 09:49:08 +13:00
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(CustomFilter))
|
|
|
|
|
{
|
|
|
|
|
filter = CustomFilter + "|" + filter;
|
|
|
|
|
}
|
2017-01-17 09:49:08 +13:00
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = filter, Multiselect = true })
|
|
|
|
|
{
|
|
|
|
|
if (ofd.ShowDialog() == DialogResult.OK)
|
2014-06-07 05:21:03 +12:00
|
|
|
|
{
|
2019-01-09 07:28:34 +13:00
|
|
|
|
foreach (string filename in ofd.FileNames)
|
2014-06-07 05:21:03 +12:00
|
|
|
|
{
|
2019-01-09 07:28:34 +13:00
|
|
|
|
string json = File.ReadAllText(filename, Encoding.UTF8);
|
|
|
|
|
OnImportRequested(json);
|
2014-06-21 00:05:04 +12:00
|
|
|
|
}
|
2019-01-09 07:28:34 +13:00
|
|
|
|
|
|
|
|
|
OnImportCompleted();
|
2014-06-21 00:05:04 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-07 05:21:03 +12:00
|
|
|
|
|
2018-08-04 01:38:18 +12:00
|
|
|
|
private async void tsmiImportURL_Click(object sender, EventArgs e)
|
2014-06-21 00:05:04 +12:00
|
|
|
|
{
|
2019-01-09 07:28:34 +13:00
|
|
|
|
string url = InputBox.GetInputText(Resources.ExportImportControl_tsmiImportURL_Click_URL_to_download_settings_from);
|
2014-06-07 05:21:03 +12:00
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
btnImport.Enabled = false;
|
2014-06-21 09:11:13 +12:00
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
string json = null;
|
2014-06-21 00:05:04 +12:00
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
json = Helpers.DownloadString(url);
|
|
|
|
|
});
|
2018-08-04 01:38:18 +12:00
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
OnImportRequested(json);
|
|
|
|
|
OnImportCompleted();
|
2018-08-04 01:38:18 +12:00
|
|
|
|
|
2019-01-09 07:28:34 +13:00
|
|
|
|
btnImport.Enabled = true;
|
2014-06-07 05:21:03 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|