Added import settings from URL support

This commit is contained in:
Jaex 2014-06-20 15:05:04 +03:00
parent 847b152717
commit 6286ddaba5
8 changed files with 114 additions and 82 deletions

View file

@ -30,51 +30,42 @@ namespace HelpersLib
{ {
public class InputBox : Form public class InputBox : Form
{ {
public string Question { get; set; } public string Title { get; set; }
public string InputText { get; set; } public string InputText { get; set; }
public InputBox() public InputBox(string title = null, string inputText = null)
{ {
InitializeComponent(); InitializeComponent();
} Icon = ShareXResources.Icon;
public InputBox(string question, string inputText) Title = title;
{
Question = question;
InputText = inputText; InputText = inputText;
}
private void InputBox_Load(object sender, EventArgs e) if (!string.IsNullOrEmpty(Title)) Text = Title;
{ if (!string.IsNullOrEmpty(InputText)) txtInputText.Text = InputText;
lblQuestion.Text = Question ?? "";
txtInputText.Text = InputText ?? "";
} }
private void InputBox_Shown(object sender, EventArgs e) private void InputBox_Shown(object sender, EventArgs e)
{ {
this.ShowActivate(); this.ShowActivate();
txtInputText.Focus();
txtInputText.SelectionLength = txtInputText.Text.Length; txtInputText.SelectionLength = txtInputText.Text.Length;
} }
private void btnOK_Click(object sender, EventArgs e) private void btnOK_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtInputText.Text))
{ {
InputText = txtInputText.Text; InputText = txtInputText.Text;
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
} }
}
private void btnCancel_Click(object sender, EventArgs e) private void btnCancel_Click(object sender, EventArgs e)
{ {
DialogResult = DialogResult.Cancel; DialogResult = DialogResult.Cancel;
} }
public static string GetInputText(string question = "", string inputText = "") public static string GetInputText(string title = null, string inputText = null)
{ {
using (InputBox form = new InputBox(question, inputText)) using (InputBox form = new InputBox(title, inputText))
{ {
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
@ -103,70 +94,60 @@ private void InitializeComponent()
this.btnOK = new System.Windows.Forms.Button(); this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button();
this.txtInputText = new System.Windows.Forms.TextBox(); this.txtInputText = new System.Windows.Forms.TextBox();
this.lblQuestion = new System.Windows.Forms.Label();
this.SuspendLayout(); this.SuspendLayout();
// //
// btnOK // btnOK
// //
this.btnOK.Location = new System.Drawing.Point(208, 56); this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnOK.Location = new System.Drawing.Point(208, 32);
this.btnOK.Name = "btnOK"; this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(72, 24); this.btnOK.Size = new System.Drawing.Size(72, 24);
this.btnOK.TabIndex = 2; this.btnOK.TabIndex = 1;
this.btnOK.Text = "OK"; this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true; this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click); this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
// //
// btnCancel // btnCancel
// //
this.btnCancel.Location = new System.Drawing.Point(288, 56); this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnCancel.Location = new System.Drawing.Point(288, 32);
this.btnCancel.Name = "btnCancel"; this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(72, 24); this.btnCancel.Size = new System.Drawing.Size(72, 24);
this.btnCancel.TabIndex = 3; this.btnCancel.TabIndex = 2;
this.btnCancel.Text = "Cancel"; this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true; this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
// //
// txtInputText // txtInputText
// //
this.txtInputText.Location = new System.Drawing.Point(8, 32); this.txtInputText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtInputText.Location = new System.Drawing.Point(8, 8);
this.txtInputText.Name = "txtInputText"; this.txtInputText.Name = "txtInputText";
this.txtInputText.Size = new System.Drawing.Size(352, 20); this.txtInputText.Size = new System.Drawing.Size(352, 20);
this.txtInputText.TabIndex = 1; this.txtInputText.TabIndex = 0;
//
// lblQuestion
//
this.lblQuestion.AutoSize = true;
this.lblQuestion.Location = new System.Drawing.Point(8, 8);
this.lblQuestion.Name = "lblQuestion";
this.lblQuestion.Size = new System.Drawing.Size(31, 13);
this.lblQuestion.TabIndex = 0;
this.lblQuestion.Text = "Input";
// //
// InputBox // InputBox
// //
this.AcceptButton = this.btnOK; this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(369, 89); this.ClientSize = new System.Drawing.Size(368, 64);
this.Controls.Add(this.lblQuestion);
this.Controls.Add(this.txtInputText); this.Controls.Add(this.txtInputText);
this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK); this.Controls.Add(this.btnOK);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false; this.MaximizeBox = false;
this.MinimizeBox = false; this.MinimizeBox = false;
this.Name = "InputBox"; this.Name = "InputBox";
this.ShowInTaskbar = false; this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "InputBox"; this.Text = "ShareX - Input box";
this.TopMost = true; this.TopMost = true;
this.Load += new System.EventHandler(this.InputBox_Load);
this.Shown += new System.EventHandler(this.InputBox_Shown); this.Shown += new System.EventHandler(this.InputBox_Shown);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
} }
private System.Windows.Forms.Label lblQuestion;
private System.Windows.Forms.Button btnOK; private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.TextBox txtInputText; private System.Windows.Forms.TextBox txtInputText;

View file

@ -112,9 +112,9 @@
<value>2.0</value> <value>2.0</value>
</resheader> </resheader>
<resheader name="reader"> <resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
</root> </root>

View file

@ -31,6 +31,7 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Media; using System.Media;
using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
@ -765,5 +766,28 @@ public static string SendPing(string host, int count)
return string.Join(", ", status); return string.Join(", ", status);
} }
public static string DownloadString(string url)
{
if (!string.IsNullOrEmpty(url))
{
try
{
using (WebClient wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
wc.Proxy = ProxyInfo.Current.GetWebProxy();
return wc.DownloadString(url);
}
}
catch (Exception e)
{
DebugHelper.WriteException(e);
MessageBox.Show("Download failed:\r\n" + e.ToString(), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return null;
}
} }
} }

View file

@ -32,12 +32,13 @@ private void InitializeComponent()
this.cmsExport = new System.Windows.Forms.ContextMenuStrip(this.components); this.cmsExport = new System.Windows.Forms.ContextMenuStrip(this.components);
this.tsmiExportClipboard = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiExportClipboard = new System.Windows.Forms.ToolStripMenuItem();
this.tsmiExportFile = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiExportFile = new System.Windows.Forms.ToolStripMenuItem();
this.tsmiExportUpload = new System.Windows.Forms.ToolStripMenuItem();
this.cmsImport = new System.Windows.Forms.ContextMenuStrip(this.components); this.cmsImport = new System.Windows.Forms.ContextMenuStrip(this.components);
this.tsmiImportClipboard = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiImportClipboard = new System.Windows.Forms.ToolStripMenuItem();
this.tsmiImportFile = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiImportFile = new System.Windows.Forms.ToolStripMenuItem();
this.btnImport = new HelpersLib.MenuButton(); this.btnImport = new HelpersLib.MenuButton();
this.btnExport = new HelpersLib.MenuButton(); this.btnExport = new HelpersLib.MenuButton();
this.tsmiExportUpload = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiImportURL = new System.Windows.Forms.ToolStripMenuItem();
this.cmsExport.SuspendLayout(); this.cmsExport.SuspendLayout();
this.cmsImport.SuspendLayout(); this.cmsImport.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
@ -50,7 +51,7 @@ private void InitializeComponent()
this.tsmiExportUpload}); this.tsmiExportUpload});
this.cmsExport.Name = "cmsExport"; this.cmsExport.Name = "cmsExport";
this.cmsExport.ShowImageMargin = false; this.cmsExport.ShowImageMargin = false;
this.cmsExport.Size = new System.Drawing.Size(145, 92); this.cmsExport.Size = new System.Drawing.Size(145, 70);
// //
// tsmiExportClipboard // tsmiExportClipboard
// //
@ -66,14 +67,22 @@ private void InitializeComponent()
this.tsmiExportFile.Text = "Save to file..."; this.tsmiExportFile.Text = "Save to file...";
this.tsmiExportFile.Click += new System.EventHandler(this.tsmiExportFile_Click); this.tsmiExportFile.Click += new System.EventHandler(this.tsmiExportFile_Click);
// //
// tsmiExportUpload
//
this.tsmiExportUpload.Name = "tsmiExportUpload";
this.tsmiExportUpload.Size = new System.Drawing.Size(144, 22);
this.tsmiExportUpload.Text = "Upload as text";
this.tsmiExportUpload.Click += new System.EventHandler(this.tsmiExportUpload_Click);
//
// cmsImport // cmsImport
// //
this.cmsImport.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.cmsImport.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsmiImportClipboard, this.tsmiImportClipboard,
this.tsmiImportFile}); this.tsmiImportFile,
this.tsmiImportURL});
this.cmsImport.Name = "cmsImport"; this.cmsImport.Name = "cmsImport";
this.cmsImport.ShowImageMargin = false; this.cmsImport.ShowImageMargin = false;
this.cmsImport.Size = new System.Drawing.Size(131, 48); this.cmsImport.Size = new System.Drawing.Size(131, 92);
// //
// tsmiImportClipboard // tsmiImportClipboard
// //
@ -113,12 +122,12 @@ private void InitializeComponent()
this.btnExport.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.btnExport.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnExport.UseVisualStyleBackColor = true; this.btnExport.UseVisualStyleBackColor = true;
// //
// tsmiExportUpload // tsmiImportURL
// //
this.tsmiExportUpload.Name = "tsmiExportUpload"; this.tsmiImportURL.Name = "tsmiImportURL";
this.tsmiExportUpload.Size = new System.Drawing.Size(144, 22); this.tsmiImportURL.Size = new System.Drawing.Size(130, 22);
this.tsmiExportUpload.Text = "Upload as text"; this.tsmiImportURL.Text = "From URL...";
this.tsmiExportUpload.Click += new System.EventHandler(this.tsmiExportUpload_Click); this.tsmiImportURL.Click += new System.EventHandler(this.tsmiImportURL_Click);
// //
// ExportImportControl // ExportImportControl
// //
@ -145,5 +154,6 @@ private void InitializeComponent()
private MenuButton btnExport; private MenuButton btnExport;
private MenuButton btnImport; private MenuButton btnImport;
private System.Windows.Forms.ToolStripMenuItem tsmiExportUpload; private System.Windows.Forms.ToolStripMenuItem tsmiExportUpload;
private System.Windows.Forms.ToolStripMenuItem tsmiImportURL;
} }
} }

View file

@ -57,7 +57,7 @@ public ExportImportControl()
InitializeComponent(); InitializeComponent();
} }
public string Export(object obj) private string Serialize(object obj)
{ {
if (obj != null) if (obj != null)
{ {
@ -95,7 +95,7 @@ private void tsmiExportClipboard_Click(object sender, EventArgs e)
{ {
object obj = ExportRequested(); object obj = ExportRequested();
string json = Export(obj); string json = Serialize(obj);
if (!string.IsNullOrEmpty(json) && ClipboardHelpers.CopyText(json)) if (!string.IsNullOrEmpty(json) && ClipboardHelpers.CopyText(json))
{ {
@ -110,11 +110,11 @@ private void tsmiExportFile_Click(object sender, EventArgs e)
{ {
object obj = ExportRequested(); object obj = ExportRequested();
string json = Export(obj); string json = Serialize(obj);
if (!string.IsNullOrEmpty(json)) if (!string.IsNullOrEmpty(json))
{ {
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Settings(*.json)|*.json" }) using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Settings (*.json)|*.json" })
{ {
if (sfd.ShowDialog() == DialogResult.OK) if (sfd.ShowDialog() == DialogResult.OK)
{ {
@ -131,7 +131,7 @@ private void tsmiExportUpload_Click(object sender, EventArgs e)
{ {
object obj = ExportRequested(); object obj = ExportRequested();
string json = Export(obj); string json = Serialize(obj);
if (!string.IsNullOrEmpty(json)) if (!string.IsNullOrEmpty(json))
{ {
@ -140,7 +140,7 @@ private void tsmiExportUpload_Click(object sender, EventArgs e)
} }
} }
public object Import(string json) private object Deserialize(string json)
{ {
try try
{ {
@ -162,6 +162,19 @@ public object Import(string json)
return null; return null;
} }
private void Import(string json)
{
if (!string.IsNullOrEmpty(json))
{
object obj = Deserialize(json);
if (obj != null)
{
ImportRequested(obj);
}
}
}
private void tsmiImportClipboard_Click(object sender, EventArgs e) private void tsmiImportClipboard_Click(object sender, EventArgs e)
{ {
if (ImportRequested != null) if (ImportRequested != null)
@ -169,16 +182,7 @@ private void tsmiImportClipboard_Click(object sender, EventArgs e)
if (Clipboard.ContainsText()) if (Clipboard.ContainsText())
{ {
string json = Clipboard.GetText(); string json = Clipboard.GetText();
Import(json);
if (!string.IsNullOrEmpty(json))
{
object obj = Import(json);
if (obj != null)
{
ImportRequested(obj);
}
}
} }
} }
} }
@ -187,22 +191,35 @@ private void tsmiImportFile_Click(object sender, EventArgs e)
{ {
if (ImportRequested != null) if (ImportRequested != null)
{ {
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Settings(*.json)|*.json" }) using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Settings (*.txt, *.json)|*.txt;*.json|All files (*.*)|*.*" })
{ {
if (ofd.ShowDialog() == DialogResult.OK) if (ofd.ShowDialog() == DialogResult.OK)
{ {
string json = File.ReadAllText(ofd.FileName, Encoding.UTF8); string json = File.ReadAllText(ofd.FileName, Encoding.UTF8);
Import(json);
}
}
}
}
if (!string.IsNullOrEmpty(json)) private void tsmiImportURL_Click(object sender, EventArgs e)
{ {
object obj = Import(json); if (ImportRequested != null)
{
string url = InputBox.GetInputText("URL to download settings from");
if (obj != null) if (!string.IsNullOrEmpty(url))
{ {
ImportRequested(obj); string json = null;
}
} TaskEx.Run(() =>
} {
json = Helpers.DownloadString(url);
},
() =>
{
Import(json);
});
} }
} }
} }

View file

@ -356,7 +356,7 @@ public static void DownloadAndUploadFile(string url, string filename, TaskSettin
catch (Exception e) catch (Exception e)
{ {
DebugHelper.WriteException(e); DebugHelper.WriteException(e);
MessageBox.Show("Download failed: " + e.ToString(), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Download failed:\r\n" + e.ToString(), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
}, },

View file

@ -292,7 +292,7 @@ private void FTPDelete()
private void FTPCreateDirectory() private void FTPCreateDirectory()
{ {
using (InputBox ib = new InputBox { Text = "Create directory", Question = "Please enter the name of the directory which should be created:" }) using (InputBox ib = new InputBox("Directory name to create"))
{ {
ib.ShowDialog(); ib.ShowDialog();
this.ShowActivate(); this.ShowActivate();

View file

@ -210,7 +210,7 @@ private void tsmiRefresh_Click(object sender, EventArgs e)
private void tsmiCreateDirectory_Click(object sender, EventArgs e) private void tsmiCreateDirectory_Click(object sender, EventArgs e)
{ {
using (InputBox ib = new InputBox { Text = "Dropbox - Create directory", Question = "Please enter the name of the directory which should be created:" }) using (InputBox ib = new InputBox("Directory name to create"))
{ {
if (ib.ShowDialog() == DialogResult.OK) if (ib.ShowDialog() == DialogResult.OK)
{ {