From 16593934bff55e0ef8dc80a0314facb7e3ff9f0f Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 11 Jun 2014 23:46:00 +0300 Subject: [PATCH] ImageShack better error handling, Error form changes, Using ShowActivate instead BringToFront --- HelpersLib/Forms/ErrorForm.Designer.cs | 40 ++++++++------- HelpersLib/Forms/ErrorForm.cs | 51 +++++++------------ HelpersLib/Forms/ErrorForm.resx | 4 +- HelpersLib/Forms/InputBox.cs | 3 +- HelpersLib/Helpers/Helpers.cs | 20 +++++++- HistoryLib/HistoryForm.cs | 3 +- HistoryLib/ImageHistoryForm.cs | 3 +- ScreenCaptureLib/Forms/RectangleLight.cs | 2 +- ScreenCaptureLib/Forms/Surface.cs | 2 +- ShareX/Forms/AboutForm.cs | 3 +- ShareX/Forms/ApplicationSettingsForm.cs | 3 +- ShareX/Forms/MainForm.cs | 2 +- ShareX/Program.cs | 2 +- ShareX/UploadInfoManager.cs | 3 +- UploadersLib/FTPClient/FTPClientForm.cs | 2 +- .../ImageUploaders/ImageShackUploader.cs | 37 ++++++++++++-- 16 files changed, 104 insertions(+), 76 deletions(-) diff --git a/HelpersLib/Forms/ErrorForm.Designer.cs b/HelpersLib/Forms/ErrorForm.Designer.cs index 6efd5a86a..3b29b1f7e 100644 --- a/HelpersLib/Forms/ErrorForm.Designer.cs +++ b/HelpersLib/Forms/ErrorForm.Designer.cs @@ -44,17 +44,20 @@ private void InitializeComponent() this.txtException.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.txtException.Location = new System.Drawing.Point(16, 48); + this.txtException.BackColor = System.Drawing.Color.White; + this.txtException.Location = new System.Drawing.Point(8, 48); this.txtException.Multiline = true; this.txtException.Name = "txtException"; + this.txtException.ReadOnly = true; this.txtException.ScrollBars = System.Windows.Forms.ScrollBars.Both; - this.txtException.Size = new System.Drawing.Size(461, 222); + this.txtException.Size = new System.Drawing.Size(480, 222); this.txtException.TabIndex = 1; // // btnCopyAll // this.btnCopyAll.BackColor = System.Drawing.Color.Transparent; - this.btnCopyAll.Location = new System.Drawing.Point(3, 3); + this.btnCopyAll.Location = new System.Drawing.Point(0, 3); + this.btnCopyAll.Margin = new System.Windows.Forms.Padding(0, 3, 3, 3); this.btnCopyAll.Name = "btnCopyAll"; this.btnCopyAll.Size = new System.Drawing.Size(72, 24); this.btnCopyAll.TabIndex = 0; @@ -65,7 +68,7 @@ private void InitializeComponent() // btnSendBugReport // this.btnSendBugReport.BackColor = System.Drawing.Color.Transparent; - this.btnSendBugReport.Location = new System.Drawing.Point(176, 3); + this.btnSendBugReport.Location = new System.Drawing.Point(173, 3); this.btnSendBugReport.Name = "btnSendBugReport"; this.btnSendBugReport.Size = new System.Drawing.Size(104, 24); this.btnSendBugReport.TabIndex = 2; @@ -77,9 +80,9 @@ private void InitializeComponent() // this.btnClose.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.btnClose.BackColor = System.Drawing.Color.Transparent; - this.btnClose.Location = new System.Drawing.Point(286, 3); + this.btnClose.Location = new System.Drawing.Point(283, 3); this.btnClose.Name = "btnClose"; - this.btnClose.Size = new System.Drawing.Size(93, 24); + this.btnClose.Size = new System.Drawing.Size(106, 24); this.btnClose.TabIndex = 3; this.btnClose.Text = "Exit application"; this.btnClose.UseVisualStyleBackColor = false; @@ -88,7 +91,7 @@ private void InitializeComponent() // btnOpenLogFile // this.btnOpenLogFile.BackColor = System.Drawing.Color.Transparent; - this.btnOpenLogFile.Location = new System.Drawing.Point(81, 3); + this.btnOpenLogFile.Location = new System.Drawing.Point(78, 3); this.btnOpenLogFile.Name = "btnOpenLogFile"; this.btnOpenLogFile.Size = new System.Drawing.Size(89, 24); this.btnOpenLogFile.TabIndex = 1; @@ -105,17 +108,17 @@ private void InitializeComponent() this.flpMenu.Controls.Add(this.btnSendBugReport); this.flpMenu.Controls.Add(this.btnClose); this.flpMenu.Controls.Add(this.btnContinue); - this.flpMenu.Location = new System.Drawing.Point(16, 278); + this.flpMenu.Location = new System.Drawing.Point(8, 278); this.flpMenu.Name = "flpMenu"; - this.flpMenu.Size = new System.Drawing.Size(464, 32); + this.flpMenu.Size = new System.Drawing.Size(480, 32); this.flpMenu.TabIndex = 2; // // btnContinue // this.btnContinue.BackColor = System.Drawing.Color.Transparent; - this.btnContinue.Location = new System.Drawing.Point(385, 3); + this.btnContinue.Location = new System.Drawing.Point(395, 3); this.btnContinue.Name = "btnContinue"; - this.btnContinue.Size = new System.Drawing.Size(72, 24); + this.btnContinue.Size = new System.Drawing.Size(74, 24); this.btnContinue.TabIndex = 4; this.btnContinue.Text = "&OK"; this.btnContinue.UseVisualStyleBackColor = false; @@ -123,26 +126,27 @@ private void InitializeComponent() // // lblErrorMessage // - this.lblErrorMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162))); - this.lblErrorMessage.Location = new System.Drawing.Point(16, 8); + this.lblErrorMessage.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.lblErrorMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162))); + this.lblErrorMessage.Location = new System.Drawing.Point(7, 8); this.lblErrorMessage.Name = "lblErrorMessage"; - this.lblErrorMessage.Size = new System.Drawing.Size(464, 32); + this.lblErrorMessage.Size = new System.Drawing.Size(480, 34); this.lblErrorMessage.TabIndex = 0; - this.lblErrorMessage.Text = "Error"; + this.lblErrorMessage.Text = "Error\r\nError 2"; // // ErrorForm // this.AcceptButton = this.btnContinue; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(492, 318); + this.ClientSize = new System.Drawing.Size(495, 313); this.Controls.Add(this.lblErrorMessage); this.Controls.Add(this.flpMenu); this.Controls.Add(this.txtException); this.Name = "ErrorForm"; - this.ShowIcon = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "ErrorForm"; + this.Text = "ShareX - Error"; this.Shown += new System.EventHandler(this.ErrorForm_Shown); this.flpMenu.ResumeLayout(false); this.ResumeLayout(false); diff --git a/HelpersLib/Forms/ErrorForm.cs b/HelpersLib/Forms/ErrorForm.cs index 5b1b695a1..d4d96377d 100644 --- a/HelpersLib/Forms/ErrorForm.cs +++ b/HelpersLib/Forms/ErrorForm.cs @@ -32,80 +32,63 @@ namespace HelpersLib { public partial class ErrorForm : Form { - public string ApplicationName { get; private set; } - public Logger Logger { get; private set; } public string LogPath { get; private set; } public string BugReportPath { get; private set; } - public ErrorForm(string productName, Exception error, Logger logger, string logPath, string bugReportPath) - : this(productName, error.Message, error.ToString(), logger, logPath, bugReportPath) + public ErrorForm(Exception error, string logPath, string bugReportPath) + : this(error.Message, error.ToString(), logPath, bugReportPath) { } - public ErrorForm(string productName, string errorMessage, string errorString, Logger logger, string logPath, string bugReportPath) + public ErrorForm(string errorTitle, string errorMessage, string logPath, string bugReportPath) { InitializeComponent(); Icon = ShareXResources.Icon; - - ApplicationName = productName; - Logger = logger; LogPath = logPath; BugReportPath = bugReportPath; - Text = string.Format("{0} - Error", ApplicationName); - Logger.WriteException(errorString, "Unhandled exception"); + DebugHelper.WriteException(errorMessage, "Unhandled exception"); - lblErrorMessage.Text = errorMessage; - txtException.Text = errorString; + lblErrorMessage.Text = errorTitle; + txtException.Text = errorMessage; + txtException.SelectionStart = txtException.TextLength; btnOpenLogFile.Visible = !string.IsNullOrEmpty(LogPath) && File.Exists(LogPath); btnSendBugReport.Visible = !string.IsNullOrEmpty(BugReportPath); } + private void ErrorForm_Shown(object sender, EventArgs e) + { + this.ShowActivate(); + } + private void btnCopyAll_Click(object sender, EventArgs e) { - string text = txtException.Text; - - if (!string.IsNullOrEmpty(text)) - { - ClipboardHelpers.CopyText(text); - } + ClipboardHelpers.CopyText(txtException.Text); } private void btnOpenLogFile_Click(object sender, EventArgs e) { - if (!string.IsNullOrEmpty(LogPath) && File.Exists(LogPath)) - { - Process.Start(LogPath); - } + Helpers.OpenFile(LogPath); } private void btnSendBugReport_Click(object sender, EventArgs e) { - if (!string.IsNullOrEmpty(BugReportPath)) - { - Helpers.OpenURL(BugReportPath); - } + Helpers.OpenURL(BugReportPath); } private void btnContinue_Click(object sender, EventArgs e) { - Logger.WriteLine("{0} continue.", ProductName); + DebugHelper.WriteLine("ShareX continue."); Close(); } private void btnClose_Click(object sender, EventArgs e) { - Logger.WriteLine("{0} closing. Reason: Unhandled exception", ProductName); + DebugHelper.WriteLine("ShareX closing. Reason: Unhandled exception"); Application.Exit(); } - private void ErrorForm_Shown(object sender, EventArgs e) - { - Activate(); - BringToFront(); - } - public static void ThrowExceptionForTest() { throw new Exception("Error line one!\r\nError line two!"); diff --git a/HelpersLib/Forms/ErrorForm.resx b/HelpersLib/Forms/ErrorForm.resx index 7080a7d11..1af7de150 100644 --- a/HelpersLib/Forms/ErrorForm.resx +++ b/HelpersLib/Forms/ErrorForm.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/Forms/InputBox.cs b/HelpersLib/Forms/InputBox.cs index b5adec7e0..642a98b44 100644 --- a/HelpersLib/Forms/InputBox.cs +++ b/HelpersLib/Forms/InputBox.cs @@ -52,7 +52,8 @@ private void InputBox_Load(object sender, EventArgs e) private void InputBox_Shown(object sender, EventArgs e) { - BringToFront(); + this.ShowActivate(); + txtInputText.Focus(); txtInputText.SelectionLength = txtInputText.Text.Length; } diff --git a/HelpersLib/Helpers/Helpers.cs b/HelpersLib/Helpers/Helpers.cs index 0b2123d9a..f123cffe3 100644 --- a/HelpersLib/Helpers/Helpers.cs +++ b/HelpersLib/Helpers/Helpers.cs @@ -404,7 +404,25 @@ public static void OpenURL(string url) } catch (Exception e) { - DebugHelper.WriteException(e, "OpenURL(" + url + ") failed"); + DebugHelper.WriteException(e, string.Format("OpenURL({0}) failed", url)); + } + }); + } + } + + public static void OpenFile(string filepath) + { + if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath)) + { + TaskEx.Run(() => + { + try + { + Process.Start(filepath); + } + catch (Exception e) + { + DebugHelper.WriteException(e, string.Format("OpenFile({0}) failed", filepath)); } }); } diff --git a/HistoryLib/HistoryForm.cs b/HistoryLib/HistoryForm.cs index 747549d59..ec076539a 100644 --- a/HistoryLib/HistoryForm.cs +++ b/HistoryLib/HistoryForm.cs @@ -256,8 +256,7 @@ private void UpdatePictureBox() private void HistoryForm_Shown(object sender, EventArgs e) { Application.DoEvents(); - BringToFront(); - Activate(); + this.ShowActivate(); RefreshHistoryItems(); } diff --git a/HistoryLib/ImageHistoryForm.cs b/HistoryLib/ImageHistoryForm.cs index 79d733d01..5c74f4025 100644 --- a/HistoryLib/ImageHistoryForm.cs +++ b/HistoryLib/ImageHistoryForm.cs @@ -129,8 +129,7 @@ private HistoryItem[] him_GetHistoryItems() private void ImageHistoryForm_Shown(object sender, EventArgs e) { Application.DoEvents(); - BringToFront(); - Activate(); + this.ShowActivate(); RefreshHistoryItems(); } diff --git a/ScreenCaptureLib/Forms/RectangleLight.cs b/ScreenCaptureLib/Forms/RectangleLight.cs index 4213bc5f6..7e7fe8748 100644 --- a/ScreenCaptureLib/Forms/RectangleLight.cs +++ b/ScreenCaptureLib/Forms/RectangleLight.cs @@ -140,7 +140,7 @@ private void InitializeComponent() private void RectangleLight_Shown(object sender, EventArgs e) { - Activate(); + this.ShowActivate(); } private void RectangleLight_KeyUp(object sender, KeyEventArgs e) diff --git a/ScreenCaptureLib/Forms/Surface.cs b/ScreenCaptureLib/Forms/Surface.cs index 4d1b6ef19..4a6628663 100644 --- a/ScreenCaptureLib/Forms/Surface.cs +++ b/ScreenCaptureLib/Forms/Surface.cs @@ -127,7 +127,7 @@ public virtual void Prepare() private void Surface_Shown(object sender, EventArgs e) { - Activate(); + this.ShowActivate(); } private void Surface_KeyUp(object sender, KeyEventArgs e) diff --git a/ShareX/Forms/AboutForm.cs b/ShareX/Forms/AboutForm.cs index dcee623a5..fcf00d357 100644 --- a/ShareX/Forms/AboutForm.cs +++ b/ShareX/Forms/AboutForm.cs @@ -48,8 +48,7 @@ public AboutForm() private void AboutForm_Shown(object sender, EventArgs e) { - BringToFront(); - Activate(); + this.ShowActivate(); cLogo.Start(50); } diff --git a/ShareX/Forms/ApplicationSettingsForm.cs b/ShareX/Forms/ApplicationSettingsForm.cs index 920bface0..9d65ef111 100644 --- a/ShareX/Forms/ApplicationSettingsForm.cs +++ b/ShareX/Forms/ApplicationSettingsForm.cs @@ -134,8 +134,7 @@ private void LoadSettings() private void SettingsForm_Shown(object sender, EventArgs e) { - BringToFront(); - Activate(); + this.ShowActivate(); } private void SettingsForm_Resize(object sender, EventArgs e) diff --git a/ShareX/Forms/MainForm.cs b/ShareX/Forms/MainForm.cs index 660d953b8..915092e10 100644 --- a/ShareX/Forms/MainForm.cs +++ b/ShareX/Forms/MainForm.cs @@ -490,7 +490,7 @@ private void CheckUpdate() UpdateChecker updateChecker = TaskHelpers.CheckUpdate(); if (updateChecker.UpdateInfo != null && updateChecker.UpdateInfo.Status == UpdateStatus.UpdateAvailable && - MessageBox.Show("A newer version of ShareX is available.\r\nWould you like to download and install it?", string.Format("{0} v{1} is available", Application.ProductName, updateChecker.UpdateInfo.LatestVersion.ToString()), + MessageBox.Show("A newer version of ShareX is available.\r\nWould you like to download and install it?", string.Format("{0} {1} is available", Application.ProductName, updateChecker.UpdateInfo.LatestVersion.ToString()), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { using (DownloaderForm updaterForm = new DownloaderForm(updateChecker)) diff --git a/ShareX/Program.cs b/ShareX/Program.cs index 940c006c1..b37130293 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -456,7 +456,7 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc private static void OnError(Exception e) { - using (ErrorForm errorForm = new ErrorForm(Application.ProductName, e, DebugHelper.Logger, LogsFilePath, Links.URL_ISSUES)) + using (ErrorForm errorForm = new ErrorForm(e, LogsFilePath, Links.URL_ISSUES)) { errorForm.ShowDialog(); } diff --git a/ShareX/UploadInfoManager.cs b/ShareX/UploadInfoManager.cs index ec88cb925..caa05dbfa 100644 --- a/ShareX/UploadInfoManager.cs +++ b/ShareX/UploadInfoManager.cs @@ -276,8 +276,7 @@ public void ShowErrors() if (!string.IsNullOrEmpty(errors)) { - using (ErrorForm form = new ErrorForm(Application.ProductName, "Upload errors", errors, DebugHelper.Logger, - Program.LogsFilePath, Links.URL_ISSUES)) + using (ErrorForm form = new ErrorForm("Upload errors", errors, Program.LogsFilePath, Links.URL_ISSUES)) { form.ShowDialog(); } diff --git a/UploadersLib/FTPClient/FTPClientForm.cs b/UploadersLib/FTPClient/FTPClientForm.cs index b3e598327..0e01ac5ed 100644 --- a/UploadersLib/FTPClient/FTPClientForm.cs +++ b/UploadersLib/FTPClient/FTPClientForm.cs @@ -295,7 +295,7 @@ private void FTPCreateDirectory() using (InputBox ib = new InputBox { Text = "Create directory", Question = "Please enter the name of the directory which should be created:" }) { ib.ShowDialog(); - BringToFront(); + this.ShowActivate(); if (ib.DialogResult == DialogResult.OK) { Client.CreateDirectory(URLHelpers.CombineURL(currentDirectory, ib.InputText)); diff --git a/UploadersLib/ImageUploaders/ImageShackUploader.cs b/UploadersLib/ImageUploaders/ImageShackUploader.cs index 8f4f933b5..a7dc688e0 100644 --- a/UploadersLib/ImageUploaders/ImageShackUploader.cs +++ b/UploadersLib/ImageUploaders/ImageShackUploader.cs @@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License #endregion License Information (GPL v3) using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.IO; @@ -86,19 +87,45 @@ public override UploadResult Upload(Stream stream, string fileName) if (!string.IsNullOrEmpty(result.Response)) { - ImageShackUploadResponse resp = JsonConvert.DeserializeObject(result.Response); + JToken jsonResponse = JToken.Parse(result.Response); - if (resp != null && resp.result != null && resp.result.images.Count > 0) + if (jsonResponse != null) { - result.URL = "http://" + resp.result.images[0].direct_link; - result.ThumbnailURL = string.Format("http://imagizer.imageshack.us/v2/{0}x{1}q90/{2}/{3}", - Config.ThumbnailWidth, Config.ThumbnailHeight, resp.result.images[0].server, resp.result.images[0].filename); + bool isSuccess = jsonResponse["success"].Value(); + + if (isSuccess) + { + ImageShackUploadResult uploadResult = jsonResponse["result"].ToObject(); + + if (uploadResult != null && uploadResult.images.Count > 0) + { + result.URL = "http://" + uploadResult.images[0].direct_link; + result.ThumbnailURL = string.Format("http://imagizer.imageshack.us/v2/{0}x{1}q90/{2}/{3}", + Config.ThumbnailWidth, Config.ThumbnailHeight, uploadResult.images[0].server, uploadResult.images[0].filename); + } + } + else + { + ImageShackErrorInfo errorInfo = jsonResponse["error"].ToObject(); + Errors.Add(errorInfo.ToString()); + } } } return result; } + public class ImageShackErrorInfo + { + public int error_code { get; set; } + public string error_message { get; set; } + + public override string ToString() + { + return string.Format("Error message: {0}\r\nError code: {1}", error_message, error_code); + } + } + public class ImageShackLoginResponse { public bool success { get; set; }