diff --git a/HelpersLib/Forms/InputBox.cs b/HelpersLib/Forms/InputBox.cs index 642a98b44..ad865284e 100644 --- a/HelpersLib/Forms/InputBox.cs +++ b/HelpersLib/Forms/InputBox.cs @@ -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; diff --git a/HelpersLib/Forms/InputBox.resx b/HelpersLib/Forms/InputBox.resx index 7080a7d11..1af7de150 100644 --- a/HelpersLib/Forms/InputBox.resx +++ b/HelpersLib/Forms/InputBox.resx @@ -112,9 +112,9 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 \ No newline at end of file diff --git a/HelpersLib/Helpers/Helpers.cs b/HelpersLib/Helpers/Helpers.cs index f123cffe3..e6c1a9e11 100644 --- a/HelpersLib/Helpers/Helpers.cs +++ b/HelpersLib/Helpers/Helpers.cs @@ -31,6 +31,7 @@ You should have received a copy of the GNU General Public License 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; + } } } \ No newline at end of file diff --git a/HelpersLib/UserControls/ExportImportControl.Designer.cs b/HelpersLib/UserControls/ExportImportControl.Designer.cs index e15f8fe45..84a5293a1 100644 --- a/HelpersLib/UserControls/ExportImportControl.Designer.cs +++ b/HelpersLib/UserControls/ExportImportControl.Designer.cs @@ -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; } } diff --git a/HelpersLib/UserControls/ExportImportControl.cs b/HelpersLib/UserControls/ExportImportControl.cs index 6f22fdced..0791288ff 100644 --- a/HelpersLib/UserControls/ExportImportControl.cs +++ b/HelpersLib/UserControls/ExportImportControl.cs @@ -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); + }); + } + } + } } } \ No newline at end of file diff --git a/ShareX/UploadManager.cs b/ShareX/UploadManager.cs index 29018cb5f..7e6e757f4 100644 --- a/ShareX/UploadManager.cs +++ b/ShareX/UploadManager.cs @@ -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); } } }, diff --git a/UploadersLib/FTPClient/FTPClientForm.cs b/UploadersLib/FTPClient/FTPClientForm.cs index 0e01ac5ed..7251ce689 100644 --- a/UploadersLib/FTPClient/FTPClientForm.cs +++ b/UploadersLib/FTPClient/FTPClientForm.cs @@ -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(); diff --git a/UploadersLib/Forms/DropboxFilesForm.cs b/UploadersLib/Forms/DropboxFilesForm.cs index e3c72ecc8..ab9243eac 100644 --- a/UploadersLib/Forms/DropboxFilesForm.cs +++ b/UploadersLib/Forms/DropboxFilesForm.cs @@ -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) {