Decrease code duplication

This commit is contained in:
Jaex 2018-05-16 12:40:15 +03:00
parent fe14c2607e
commit bd83a80c97
2 changed files with 87 additions and 172 deletions

View file

@ -1769,17 +1769,18 @@ private void tvOneDrive_AfterExpand(object sender, TreeViewEventArgs e)
private void oauth2GoogleDrive_OpenButtonClicked()
{
GoogleDriveAuthOpen();
OAuth2Info oauth = new OAuth2Info(APIKeys.GoogleClientID, APIKeys.GoogleClientSecret);
Config.GoogleDriveOAuth2Info = OAuth2Open(new GoogleDrive(oauth));
}
private void oauth2GoogleDrive_CompleteButtonClicked(string code)
{
GoogleDriveAuthComplete(code);
btnGoogleDriveRefreshFolders.Enabled = OAuth2Complete(new GoogleDrive(Config.GoogleDriveOAuth2Info), code, oauth2GoogleDrive);
}
private void oauth2GoogleDrive_RefreshButtonClicked()
{
GoogleDriveAuthRefresh();
btnGoogleDriveRefreshFolders.Enabled = OAuth2Refresh(new GoogleDrive(Config.GoogleDriveOAuth2Info), oauth2GoogleDrive);
}
private void oauth2GoogleDrive_ClearButtonClicked()
@ -2982,7 +2983,7 @@ private void oauth2YouTube_OpenButtonClicked()
private void oauth2YouTube_CompleteButtonClicked(string code)
{
OAuth2Complete(new YouTube(Config.YouTubeOAuth2Info), oauth2YouTube, code);
OAuth2Complete(new YouTube(Config.YouTubeOAuth2Info), code, oauth2YouTube);
}
private void oauth2YouTube_RefreshButtonClicked()
@ -3016,7 +3017,7 @@ private void oauth2GoogleCloudStorage_ClearButtonClicked()
private void oauth2GoogleCloudStorage_CompleteButtonClicked(string code)
{
OAuth2Complete(new GoogleCloudStorage(Config.GoogleCloudStorageOAuth2Info), oauth2GoogleCloudStorage, code);
OAuth2Complete(new GoogleCloudStorage(Config.GoogleCloudStorageOAuth2Info), code, oauth2GoogleCloudStorage);
}
private void oauth2GoogleCloudStorage_OpenButtonClicked()

View file

@ -489,87 +489,6 @@ private void UpdateGoogleCloudStorageStatus()
#region Google Drive
public void GoogleDriveAuthOpen()
{
try
{
OAuth2Info oauth = new OAuth2Info(APIKeys.GoogleClientID, APIKeys.GoogleClientSecret);
string url = new GoogleDrive(oauth).GetAuthorizationURL();
if (!string.IsNullOrEmpty(url))
{
Config.GoogleDriveOAuth2Info = oauth;
URLHelpers.OpenURL(url);
DebugHelper.WriteLine("GoogleDriveAuthOpen - Authorization URL is opened: " + url);
}
else
{
DebugHelper.WriteLine("GoogleDriveAuthOpen - Authorization URL is empty.");
}
}
catch (Exception ex)
{
ex.ShowError();
}
}
public void GoogleDriveAuthComplete(string code)
{
try
{
if (!string.IsNullOrEmpty(code) && Config.GoogleDriveOAuth2Info != null)
{
bool result = new GoogleDrive(Config.GoogleDriveOAuth2Info).GetAccessToken(code);
if (result)
{
oauth2GoogleDrive.Status = OAuthLoginStatus.LoginSuccessful;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
oauth2GoogleDrive.Status = OAuthLoginStatus.LoginFailed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
btnGoogleDriveRefreshFolders.Enabled = result;
}
}
catch (Exception ex)
{
ex.ShowError();
}
}
public void GoogleDriveAuthRefresh()
{
try
{
if (OAuth2Info.CheckOAuth(Config.GoogleDriveOAuth2Info))
{
bool result = new GoogleDrive(Config.GoogleDriveOAuth2Info).RefreshAccessToken();
if (result)
{
oauth2GoogleDrive.Status = OAuthLoginStatus.LoginSuccessful;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
oauth2GoogleDrive.Status = OAuthLoginStatus.LoginFailed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
btnGoogleDriveRefreshFolders.Enabled = result;
}
}
catch (Exception ex)
{
ex.ShowError();
}
}
private void GoogleDriveRefreshFolders()
{
try
@ -2037,92 +1956,6 @@ private void GfycatAuthRefresh()
#endregion Gfycat
#region Generic OAuth2
public OAuth2Info OAuth2Open(IOAuth2 uploader)
{
try
{
string url = uploader.GetAuthorizationURL();
if (!string.IsNullOrEmpty(url))
{
URLHelpers.OpenURL(url);
DebugHelper.WriteLine(uploader.ToString() + " - Authorization URL is opened: " + url);
return uploader.AuthInfo;
}
else
{
DebugHelper.WriteLine(uploader.ToString() + " - Authorization URL is empty.");
}
}
catch (Exception ex)
{
ex.ShowError();
}
return null;
}
public bool OAuth2Complete(IOAuth2 uploader, OAuthControl oauth2, string code)
{
try
{
if (!string.IsNullOrEmpty(code) && uploader.AuthInfo != null)
{
bool result = uploader.GetAccessToken(code);
if (result)
{
oauth2.Status = OAuthLoginStatus.LoginSuccessful;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
oauth2.Status = OAuthLoginStatus.LoginFailed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return result;
}
}
catch (Exception ex)
{
ex.ShowError();
}
return false;
}
public bool OAuth2Refresh(IOAuth2 uploader, OAuthControl oauth2)
{
try
{
if (OAuth2Info.CheckOAuth(uploader.AuthInfo))
{
bool result = uploader.RefreshAccessToken();
if (result)
{
oauth2.Status = OAuthLoginStatus.LoginSuccessful;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
oauth2.Status = OAuthLoginStatus.LoginFailed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return result;
}
}
catch (Exception ex)
{
ex.ShowError();
}
return false;
}
#endregion Generic OAuth2
#region Shared folder
private void SharedFolderUpdateControls()
@ -2193,5 +2026,86 @@ private bool SharedFolderRemoveItem(int index)
}
#endregion Shared folder
#region Generic OAuth2
private OAuth2Info OAuth2Open(IOAuth2 uploader)
{
try
{
string url = uploader.GetAuthorizationURL();
if (!string.IsNullOrEmpty(url))
{
URLHelpers.OpenURL(url);
DebugHelper.WriteLine(uploader.ToString() + " - Authorization URL is opened: " + url);
return uploader.AuthInfo;
}
else
{
DebugHelper.WriteLine(uploader.ToString() + " - Authorization URL is empty.");
}
}
catch (Exception ex)
{
ex.ShowError();
}
return null;
}
private bool OAuth2Complete(IOAuth2 uploader, string code, OAuthControl control)
{
try
{
if (!string.IsNullOrEmpty(code) && uploader.AuthInfo != null)
{
bool result = uploader.GetAccessToken(code);
ConfigureOAuthStatus(control, result);
return result;
}
}
catch (Exception ex)
{
ex.ShowError();
}
return false;
}
private bool OAuth2Refresh(IOAuth2 uploader, OAuthControl oauth2)
{
try
{
if (OAuth2Info.CheckOAuth(uploader.AuthInfo))
{
bool result = uploader.RefreshAccessToken();
ConfigureOAuthStatus(oauth2, result);
return result;
}
}
catch (Exception ex)
{
ex.ShowError();
}
return false;
}
private void ConfigureOAuthStatus(OAuthControl oauth2, bool result)
{
if (result)
{
oauth2.Status = OAuthLoginStatus.LoginSuccessful;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
oauth2.Status = OAuthLoginStatus.LoginFailed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion Generic OAuth2
}
}