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

View file

@ -112,9 +112,9 @@
<value>2.0</value>
</resheader>
<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 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>
</root>

View file

@ -31,6 +31,7 @@
using System.IO;
using System.Linq;
using System.Media;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
@ -765,5 +766,28 @@ public static string SendPing(string host, int count)
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.tsmiExportClipboard = 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.tsmiImportClipboard = new System.Windows.Forms.ToolStripMenuItem();
this.tsmiImportFile = new System.Windows.Forms.ToolStripMenuItem();
this.btnImport = 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.cmsImport.SuspendLayout();
this.SuspendLayout();
@ -50,7 +51,7 @@ private void InitializeComponent()
this.tsmiExportUpload});
this.cmsExport.Name = "cmsExport";
this.cmsExport.ShowImageMargin = false;
this.cmsExport.Size = new System.Drawing.Size(145, 92);
this.cmsExport.Size = new System.Drawing.Size(145, 70);
//
// tsmiExportClipboard
//
@ -66,14 +67,22 @@ private void InitializeComponent()
this.tsmiExportFile.Text = "Save to file...";
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
//
this.cmsImport.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.tsmiImportClipboard,
this.tsmiImportFile});
this.tsmiImportFile,
this.tsmiImportURL});
this.cmsImport.Name = "cmsImport";
this.cmsImport.ShowImageMargin = false;
this.cmsImport.Size = new System.Drawing.Size(131, 48);
this.cmsImport.Size = new System.Drawing.Size(131, 92);
//
// tsmiImportClipboard
//
@ -113,12 +122,12 @@ private void InitializeComponent()
this.btnExport.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnExport.UseVisualStyleBackColor = true;
//
// tsmiExportUpload
// tsmiImportURL
//
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);
this.tsmiImportURL.Name = "tsmiImportURL";
this.tsmiImportURL.Size = new System.Drawing.Size(130, 22);
this.tsmiImportURL.Text = "From URL...";
this.tsmiImportURL.Click += new System.EventHandler(this.tsmiImportURL_Click);
//
// ExportImportControl
//
@ -145,5 +154,6 @@ private void InitializeComponent()
private MenuButton btnExport;
private MenuButton btnImport;
private System.Windows.Forms.ToolStripMenuItem tsmiExportUpload;
private System.Windows.Forms.ToolStripMenuItem tsmiImportURL;
}
}

View file

@ -57,7 +57,7 @@ public ExportImportControl()
InitializeComponent();
}
public string Export(object obj)
private string Serialize(object obj)
{
if (obj != null)
{
@ -95,7 +95,7 @@ private void tsmiExportClipboard_Click(object sender, EventArgs e)
{
object obj = ExportRequested();
string json = Export(obj);
string json = Serialize(obj);
if (!string.IsNullOrEmpty(json) && ClipboardHelpers.CopyText(json))
{
@ -110,11 +110,11 @@ private void tsmiExportFile_Click(object sender, EventArgs e)
{
object obj = ExportRequested();
string json = Export(obj);
string json = Serialize(obj);
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)
{
@ -131,7 +131,7 @@ private void tsmiExportUpload_Click(object sender, EventArgs e)
{
object obj = ExportRequested();
string json = Export(obj);
string json = Serialize(obj);
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
{
@ -162,6 +162,19 @@ public object Import(string json)
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)
{
if (ImportRequested != null)
@ -169,16 +182,7 @@ private void tsmiImportClipboard_Click(object sender, EventArgs e)
if (Clipboard.ContainsText())
{
string json = Clipboard.GetText();
if (!string.IsNullOrEmpty(json))
{
object obj = Import(json);
if (obj != null)
{
ImportRequested(obj);
}
}
Import(json);
}
}
}
@ -187,24 +191,37 @@ private void tsmiImportFile_Click(object sender, EventArgs e)
{
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)
{
string json = File.ReadAllText(ofd.FileName, Encoding.UTF8);
if (!string.IsNullOrEmpty(json))
{
object obj = Import(json);
if (obj != null)
{
ImportRequested(obj);
}
}
Import(json);
}
}
}
}
private void tsmiImportURL_Click(object sender, EventArgs e)
{
if (ImportRequested != null)
{
string url = InputBox.GetInputText("URL to download settings from");
if (!string.IsNullOrEmpty(url))
{
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)
{
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()
{
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();
this.ShowActivate();

View file

@ -210,7 +210,7 @@ private void tsmiRefresh_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)
{