diff --git a/ShareX.UploadersLib/Forms/UploadersConfigForm.cs b/ShareX.UploadersLib/Forms/UploadersConfigForm.cs index 80f824a01..a51942f23 100644 --- a/ShareX.UploadersLib/Forms/UploadersConfigForm.cs +++ b/ShareX.UploadersLib/Forms/UploadersConfigForm.cs @@ -3041,10 +3041,19 @@ private void txtGfycatTitle_TextChanged(object sender, EventArgs e) #region YouTube - private async void oauth2YouTube_OpenButtonClicked() + private void oauth2YouTube_OpenButtonClicked() { OAuth2Info oauth = new OAuth2Info(APIKeys.GoogleClientID, APIKeys.GoogleClientSecret); - Config.YouTubeOAuth2Info = await OAuth2Loopback(new YouTube(oauth).OAuth2); + IOAuth2Loopback oauthLoopback = new YouTube(oauth).OAuth2; + + using (OAuthListenerForm form = new OAuthListenerForm(oauthLoopback)) + { + form.ShowDialog(); + Config.YouTubeOAuth2Info = form.OAuth2Info; + } + + this.ForceActivate(); + ConfigureOAuthStatus(oauth2YouTube, Config.YouTubeOAuth2Info != null); } private void oauth2YouTube_CompleteButtonClicked(string code) diff --git a/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs b/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs index 80cacf7ec..51a66de51 100644 --- a/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs +++ b/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs @@ -1084,32 +1084,6 @@ private bool OAuth2Complete(IOAuth2Basic uploader, string code, OAuthControl con return false; } - private async Task OAuth2Loopback(IOAuth2Loopback oauth) - { - try - { - using (OAuthListener listener = new OAuthListener(oauth)) - { - bool result = await listener.ConnectAsync(); - - this.ForceActivate(); - ConfigureOAuthStatus(oauth2YouTube, result); - - if (result) - { - return listener.OAuth.AuthInfo; - } - } - } - catch (Exception e) - { - DebugHelper.WriteException(e); - e.ShowError(); - } - - return null; - } - private bool OAuth2Refresh(IOAuth2 uploader, OAuthControl oauth2) { try diff --git a/ShareX.UploadersLib/OAuth/OAuthListener.cs b/ShareX.UploadersLib/OAuth/OAuthListener.cs index 259e26834..0ae0983be 100644 --- a/ShareX.UploadersLib/OAuth/OAuthListener.cs +++ b/ShareX.UploadersLib/OAuth/OAuthListener.cs @@ -46,8 +46,11 @@ public OAuthListener(IOAuth2Loopback oauth) public void Dispose() { - listener?.Close(); - listener = null; + if (listener != null) + { + listener.Close(); + listener = null; + } } public async Task ConnectAsync() @@ -118,6 +121,9 @@ public async Task ConnectAsync() } } } + catch (ObjectDisposedException) + { + } finally { Dispose(); diff --git a/ShareX.UploadersLib/OAuth/OAuthListenerForm.Designer.cs b/ShareX.UploadersLib/OAuth/OAuthListenerForm.Designer.cs new file mode 100644 index 000000000..bc918d53d --- /dev/null +++ b/ShareX.UploadersLib/OAuth/OAuthListenerForm.Designer.cs @@ -0,0 +1,67 @@ +namespace ShareX.UploadersLib +{ + partial class OAuthListenerForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(OAuthListenerForm)); + this.label1 = new System.Windows.Forms.Label(); + this.btnCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // label1 + // + resources.ApplyResources(this.label1, "label1"); + this.label1.Name = "label1"; + // + // btnCancel + // + resources.ApplyResources(this.btnCancel, "btnCancel"); + this.btnCancel.Name = "btnCancel"; + this.btnCancel.UseVisualStyleBackColor = true; + this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); + // + // OAuthListenerForm + // + resources.ApplyResources(this, "$this"); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; + this.Controls.Add(this.btnCancel); + this.Controls.Add(this.label1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.Name = "OAuthListenerForm"; + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.OAuthListenerForm_FormClosed); + this.Load += new System.EventHandler(this.OAuthListenerForm_Load); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Button btnCancel; + } +} \ No newline at end of file diff --git a/ShareX.UploadersLib/OAuth/OAuthListenerForm.cs b/ShareX.UploadersLib/OAuth/OAuthListenerForm.cs new file mode 100644 index 000000000..57ad9f8da --- /dev/null +++ b/ShareX.UploadersLib/OAuth/OAuthListenerForm.cs @@ -0,0 +1,88 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2022 ShareX Team + + 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 . +*/ + +#endregion License Information (GPL v3) + +using ShareX.HelpersLib; +using System; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ShareX.UploadersLib +{ + public partial class OAuthListenerForm : Form + { + public IOAuth2Loopback OAuth { get; private set; } + public OAuth2Info OAuth2Info { get; private set; } + + private OAuthListener listener; + + public OAuthListenerForm(IOAuth2Loopback oauth) + { + InitializeComponent(); + ShareXResources.ApplyTheme(this); + + OAuth = oauth; + } + + private async void OAuthListenerForm_Load(object sender, EventArgs e) + { + OAuth2Info = await ConnectAsync(OAuth); + + Close(); + } + + private void OAuthListenerForm_FormClosed(object sender, FormClosedEventArgs e) + { + listener?.Dispose(); + } + + private void btnCancel_Click(object sender, EventArgs e) + { + Close(); + } + + private async Task ConnectAsync(IOAuth2Loopback oauth) + { + try + { + using (listener = new OAuthListener(oauth)) + { + bool result = await listener.ConnectAsync(); + + if (result) + { + return listener.OAuth.AuthInfo; + } + } + } + catch (Exception e) + { + DebugHelper.WriteException(e); + e.ShowError(); + } + + return null; + } + } +} \ No newline at end of file diff --git a/ShareX.UploadersLib/OAuth/OAuthListenerForm.resx b/ShareX.UploadersLib/OAuth/OAuthListenerForm.resx new file mode 100644 index 000000000..a3fb7d493 --- /dev/null +++ b/ShareX.UploadersLib/OAuth/OAuthListenerForm.resx @@ -0,0 +1,195 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + 21, 24 + + + 424, 56 + + + + 0 + + + Waiting for authorization in your browser to complete. + + + label1 + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 1 + + + 328, 88 + + + 120, 40 + + + 1 + + + Cancel + + + btnCancel + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 0 + + + True + + + 96, 96 + + + 465, 145 + + + Segoe UI, 12pt + + + + CenterScreen + + + ShareX - Authorization + + + OAuthListenerForm + + + System.Windows.Forms.Form, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ShareX.UploadersLib/ShareX.UploadersLib.csproj b/ShareX.UploadersLib/ShareX.UploadersLib.csproj index d0c26416b..f705ebd1d 100644 --- a/ShareX.UploadersLib/ShareX.UploadersLib.csproj +++ b/ShareX.UploadersLib/ShareX.UploadersLib.csproj @@ -262,6 +262,12 @@ + + Form + + + OAuthListenerForm.cs + True @@ -1069,6 +1075,9 @@ YouTubeVideoOptionsForm.cs + + OAuthListenerForm.cs + Designer