ShareX/ShareX.UploadersLib/Forms/UploadersConfigFormHelper.cs

1123 lines
39 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2021-07-29 15:22:51 +12:00
Copyright (c) 2007-2021 ShareX Team
2013-11-03 23:53:49 +13:00
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 <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
using ShareX.UploadersLib.FileUploaders;
using ShareX.UploadersLib.ImageUploaders;
2014-12-11 12:19:28 +13:00
using ShareX.UploadersLib.Properties;
2014-12-11 09:25:20 +13:00
using ShareX.UploadersLib.TextUploaders;
2014-12-11 12:19:28 +13:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2014-12-11 12:19:28 +13:00
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib
2013-11-03 23:53:49 +13:00
{
2014-10-19 10:48:47 +13:00
public partial class UploadersConfigForm
2013-11-03 23:53:49 +13:00
{
#region Imgur
2015-04-28 13:33:53 +12:00
private void ImgurRefreshAlbumList()
2013-11-03 23:53:49 +13:00
{
try
{
lvImgurAlbumList.Items.Clear();
if (OAuth2Info.CheckOAuth(Config.ImgurOAuth2Info))
{
2015-07-20 06:11:34 +12:00
Config.ImgurAlbumList = new Imgur(Config.ImgurOAuth2Info).GetAlbums();
2014-09-07 13:11:55 +12:00
ImgurFillAlbumList();
lvImgurAlbumList.Focus();
2013-11-03 23:53:49 +13:00
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
2014-09-07 13:11:55 +12:00
private void ImgurFillAlbumList()
{
if (Config.ImgurAlbumList != null)
{
foreach (ImgurAlbumData album in Config.ImgurAlbumList)
{
ListViewItem lvi = new ListViewItem(album.id ?? "");
lvi.SubItems.Add(album.title ?? "");
lvi.SubItems.Add(album.description ?? "");
lvi.Selected = Config.ImgurSelectedAlbum != null && !string.IsNullOrEmpty(Config.ImgurSelectedAlbum.id) &&
album.id.Equals(Config.ImgurSelectedAlbum.id, StringComparison.InvariantCultureIgnoreCase);
lvi.Tag = album;
lvImgurAlbumList.Items.Add(lvi);
}
}
}
2013-11-03 23:53:49 +13:00
#endregion Imgur
#region Flickr
2017-10-04 14:29:39 +13:00
private void FlickrAuthOpen()
2013-11-03 23:53:49 +13:00
{
try
{
2017-10-04 14:29:39 +13:00
OAuthInfo oauth = new OAuthInfo(APIKeys.FlickrKey, APIKeys.FlickrSecret);
string url = new FlickrUploader(oauth).GetAuthorizationURL();
2013-11-03 23:53:49 +13:00
if (!string.IsNullOrEmpty(url))
{
2017-10-04 14:29:39 +13:00
Config.FlickrOAuthInfo = oauth;
URLHelpers.OpenURL(url);
2017-10-04 14:29:39 +13:00
DebugHelper.WriteLine("FlickrAuthOpen - Authorization URL is opened: " + url);
2013-11-03 23:53:49 +13:00
}
2017-10-04 14:29:39 +13:00
else
2013-11-03 23:53:49 +13:00
{
2017-10-04 14:29:39 +13:00
DebugHelper.WriteLine("FlickrAuthOpen - Authorization URL is empty.");
2013-11-03 23:53:49 +13:00
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
2017-10-04 14:29:39 +13:00
private void FlickrAuthComplete(string code)
2013-11-03 23:53:49 +13:00
{
try
{
2017-10-04 14:29:39 +13:00
if (!string.IsNullOrEmpty(code) && Config.FlickrOAuthInfo != null)
2013-11-03 23:53:49 +13:00
{
2017-10-04 14:29:39 +13:00
bool result = new FlickrUploader(Config.FlickrOAuthInfo).GetAccessToken(code);
if (result)
2013-11-03 23:53:49 +13:00
{
2017-10-04 14:29:39 +13:00
oauthFlickr.Status = OAuthLoginStatus.LoginSuccessful;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
2013-11-03 23:53:49 +13:00
}
2017-10-04 14:29:39 +13:00
else
{
oauthFlickr.Status = OAuthLoginStatus.LoginFailed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2013-11-03 23:53:49 +13:00
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
#endregion Flickr
#region Photobucket
public void PhotobucketAuthOpen()
{
try
{
OAuthInfo oauth = new OAuthInfo(APIKeys.PhotobucketConsumerKey, APIKeys.PhotobucketConsumerSecret);
string url = new Photobucket(oauth).GetAuthorizationURL();
if (!string.IsNullOrEmpty(url))
{
Config.PhotobucketOAuthInfo = oauth;
URLHelpers.OpenURL(url);
2013-11-03 23:53:49 +13:00
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
public void PhotobucketAuthComplete()
{
try
{
string verification = txtPhotobucketVerificationCode.Text;
if (!string.IsNullOrEmpty(verification) && Config.PhotobucketOAuthInfo != null &&
!string.IsNullOrEmpty(Config.PhotobucketOAuthInfo.AuthToken) && !string.IsNullOrEmpty(Config.PhotobucketOAuthInfo.AuthSecret))
{
Photobucket pb = new Photobucket(Config.PhotobucketOAuthInfo);
bool result = pb.GetAccessToken(verification);
if (result)
{
Config.PhotobucketAccountInfo = pb.GetAccountInfo();
lblPhotobucketAccountStatus.Text = Resources.UploadersConfigForm_Login_successful;
2013-11-03 23:53:49 +13:00
txtPhotobucketDefaultAlbumName.Text = Config.PhotobucketAccountInfo.AlbumID;
Config.PhotobucketAccountInfo.AlbumList.Add(Config.PhotobucketAccountInfo.AlbumID);
2021-08-30 22:01:36 +12:00
cbPhotobucketAlbumPaths.Items.Add(Config.PhotobucketAccountInfo.AlbumID);
cbPhotobucketAlbumPaths.SelectedIndex = 0;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
2013-11-03 23:53:49 +13:00
}
else
{
lblPhotobucketAccountStatus.Text = Resources.UploadersConfigForm_Login_failed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
2013-11-03 23:53:49 +13:00
}
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
public void PhotobucketCreateAlbum()
{
Photobucket pb = new Photobucket(Config.PhotobucketOAuthInfo, Config.PhotobucketAccountInfo);
if (pb.CreateAlbum(txtPhotobucketParentAlbumPath.Text, txtPhotobucketNewAlbumName.Text))
{
string albumPath = txtPhotobucketParentAlbumPath.Text + "/" + txtPhotobucketNewAlbumName.Text;
Config.PhotobucketAccountInfo.AlbumList.Add(albumPath);
2021-08-30 22:01:36 +12:00
cbPhotobucketAlbumPaths.Items.Add(albumPath);
MessageBox.Show(string.Format(Resources.UploadersConfigForm_PhotobucketCreateAlbum__0__successfully_created_, albumPath), "ShareX",
MessageBoxButtons.OK, MessageBoxIcon.Information);
2013-11-03 23:53:49 +13:00
}
}
#endregion Photobucket
2017-10-06 09:54:06 +13:00
#region Google Photos
2013-11-03 23:53:49 +13:00
2017-10-06 09:54:06 +13:00
public void GooglePhotosRefreshAlbumList()
2013-11-03 23:53:49 +13:00
{
try
{
lvPicasaAlbumList.Items.Clear();
if (OAuth2Info.CheckOAuth(Config.GooglePhotosOAuth2Info))
2013-11-03 23:53:49 +13:00
{
List<GooglePhotosAlbumInfo> albums = new GooglePhotos(Config.GooglePhotosOAuth2Info).GetAlbumList();
2013-11-03 23:53:49 +13:00
if (albums != null && albums.Count > 0)
{
foreach (GooglePhotosAlbumInfo album in albums)
2013-11-03 23:53:49 +13:00
{
ListViewItem lvi = new ListViewItem(album.ID);
lvi.SubItems.Add(album.Name ?? "");
lvi.SubItems.Add(album.Summary ?? "");
lvi.Tag = album;
lvPicasaAlbumList.Items.Add(lvi);
}
}
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
public void GooglePhotosCreateAlbum(string albumName)
{
if (OAuth2Info.CheckOAuth(Config.GooglePhotosOAuth2Info))
{
new GooglePhotos(Config.GooglePhotosOAuth2Info).CreateAlbum(albumName);
}
}
2017-10-06 09:54:06 +13:00
#endregion Google Photos
2013-11-03 23:53:49 +13:00
2014-05-06 11:24:00 +12:00
#region Amazon S3
private void UpdateAmazonS3Status()
{
2018-06-20 07:16:11 +12:00
AmazonS3 s3 = new AmazonS3(Config.AmazonS3Settings);
lblAmazonS3PathPreview.Text = s3.GetPreviewURL();
2014-05-06 11:24:00 +12:00
}
#endregion Amazon S3
2018-04-26 16:41:11 +12:00
#region Google Cloud Storage
private void UpdateGoogleCloudStorageStatus()
{
2018-06-20 07:16:11 +12:00
GoogleCloudStorage gcs = new GoogleCloudStorage(Config.GoogleCloudStorageOAuth2Info)
2018-04-26 16:41:11 +12:00
{
Bucket = Config.GoogleCloudStorageBucket,
Domain = Config.GoogleCloudStorageDomain,
Prefix = Config.GoogleCloudStorageObjectPrefix,
RemoveExtensionImage = Config.GoogleCloudStorageRemoveExtensionImage,
RemoveExtensionText = Config.GoogleCloudStorageRemoveExtensionText,
2019-04-08 08:42:47 +12:00
RemoveExtensionVideo = Config.GoogleCloudStorageRemoveExtensionVideo,
SetPublicACL = Config.GoogleCloudStorageSetPublicACL
2018-04-26 16:41:11 +12:00
};
2018-06-20 07:16:11 +12:00
lblGoogleCloudStoragePathPreview.Text = gcs.GetPreviewURL();
2018-04-26 16:41:11 +12:00
}
#endregion Google Cloud Storage
2018-06-20 07:16:11 +12:00
#region Azure Storage
private void UpdateAzureStorageStatus()
{
AzureStorage azure = new AzureStorage(Config.AzureStorageAccountName, Config.AzureStorageAccountAccessKey, Config.AzureStorageContainer,
Config.AzureStorageEnvironment, Config.AzureStorageCustomDomain, Config.AzureStorageUploadPath);
lblAzureStorageURLPreview.Text = azure.GetPreviewURL();
}
#endregion Azure Storage
#region Backblaze B2
private void B2UpdateCustomDomainPreview()
{
string uploadPath = NameParser.Parse(NameParserType.FolderPath, Config.B2UploadPath);
2019-09-04 18:55:05 +12:00
string url;
if (cbB2CustomUrl.Checked)
{
string customUrl = NameParser.Parse(NameParserType.FolderPath, Config.B2CustomUrl);
2019-09-04 18:55:05 +12:00
url = URLHelpers.CombineURL(customUrl, uploadPath, "example.png");
url = URLHelpers.FixPrefix(url, "https://");
}
else
{
2019-09-04 18:55:05 +12:00
string bucket = string.IsNullOrEmpty(Config.B2BucketName) ? "[bucket]" : URLHelpers.URLEncode(Config.B2BucketName);
url = URLHelpers.CombineURL("https://f001.backblazeb2.com/file", bucket, uploadPath, "example.png");
}
2019-09-04 18:55:05 +12:00
lblB2UrlPreview.Text = url;
}
#endregion Backblaze B2
2013-11-03 23:53:49 +13:00
#region Google Drive
2014-07-01 22:18:07 +12:00
private void GoogleDriveRefreshFolders()
{
try
{
lvGoogleDriveFoldersList.Items.Clear();
if (OAuth2Info.CheckOAuth(Config.GoogleDriveOAuth2Info))
{
List<GoogleDriveFile> folders = new GoogleDrive(Config.GoogleDriveOAuth2Info).GetFolders(Config.GoogleDriveSelectedDrive.id);
2014-07-01 22:18:07 +12:00
if (folders != null)
{
2016-09-17 19:07:02 +12:00
foreach (GoogleDriveFile folder in folders)
2014-07-01 22:18:07 +12:00
{
2018-04-11 16:30:09 +12:00
ListViewItem lvi = new ListViewItem(folder.name);
lvi.SubItems.Add(folder.description);
2014-07-01 22:18:07 +12:00
lvi.Tag = folder;
lvGoogleDriveFoldersList.Items.Add(lvi);
}
}
2013-11-03 23:53:49 +13:00
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
private void GoogleDriveRefreshDrives()
{
try
{
if (OAuth2Info.CheckOAuth(Config.GoogleDriveOAuth2Info))
{
List<GoogleDriveSharedDrive> drives = new GoogleDrive(Config.GoogleDriveOAuth2Info).GetDrives();
if (drives != null)
{
cbGoogleDriveSharedDrive.Items.Clear();
cbGoogleDriveSharedDrive.Items.Add(GoogleDrive.MyDrive);
foreach (GoogleDriveSharedDrive drive in drives)
{
cbGoogleDriveSharedDrive.Items.Add(drive);
}
GoogleDriveSelectConfigDrive();
}
}
}
catch (Exception ex)
{
ex.ShowError();
}
}
private void GoogleDriveSelectConfigDrive()
{
string driveID = Config.GoogleDriveSelectedDrive?.id;
cbGoogleDriveSharedDrive.SelectedItem = cbGoogleDriveSharedDrive.Items
.OfType<GoogleDriveSharedDrive>()
.Where(x => x.id == driveID)
.FirstOrDefault();
}
2013-11-03 23:53:49 +13:00
#endregion Google Drive
#region Box
public void BoxListFolders()
2014-04-17 11:34:31 +12:00
{
lvBoxFolders.Items.Clear();
BoxAddFolder(Box.RootFolder);
BoxListFolders(Box.RootFolder);
}
public void BoxListFolders(BoxFileEntry fileEntry)
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
if (!OAuth2Info.CheckOAuth(Config.BoxOAuth2Info))
2013-11-03 23:53:49 +13:00
{
2014-12-27 02:58:38 +13:00
MessageBox.Show(Resources.UploadersConfigForm_ListFolders_Authentication_required_, Resources.UploadersConfigForm_BoxListFolders_Box_refresh_folders_list_failed,
MessageBoxButtons.OK, MessageBoxIcon.Warning);
2013-11-03 23:53:49 +13:00
}
else
{
2014-04-17 04:18:25 +12:00
Box box = new Box(Config.BoxOAuth2Info);
2014-04-17 11:34:31 +12:00
BoxFileInfo files = box.GetFiles(fileEntry);
2014-04-17 08:35:10 +12:00
if (files != null && files.entries != null && files.entries.Length > 0)
{
foreach (BoxFileEntry folder in files.entries.Where(x => x.type == "folder"))
{
BoxAddFolder(folder);
}
}
2013-11-03 23:53:49 +13:00
}
}
2014-04-17 08:35:10 +12:00
private void BoxAddFolder(BoxFileEntry folder)
2013-11-03 23:53:49 +13:00
{
2014-04-17 08:35:10 +12:00
ListViewItem lvi = new ListViewItem(folder.name);
lvi.Tag = folder;
lvBoxFolders.Items.Add(lvi);
2013-11-03 23:53:49 +13:00
}
#endregion Box
2014-08-09 12:28:09 +12:00
#region OneDrive
public void OneDriveListFolders(OneDriveFileInfo fileEntry, TreeNode tnParent)
2014-12-27 02:58:38 +13:00
{
Application.DoEvents();
2018-04-14 03:10:58 +12:00
OneDrive oneDrive = new OneDrive(Config.OneDriveV2OAuth2Info);
OneDriveFileList oneDrivePathInfo = oneDrive.GetPathInfo(fileEntry.id);
tnParent.Nodes.Clear();
foreach (OneDriveFileInfo folder in oneDrivePathInfo.value)
2014-12-27 02:58:38 +13:00
{
OneDriveAddFolder(folder, tnParent);
2014-12-27 02:58:38 +13:00
}
}
private void OneDriveAddFolder(OneDriveFileInfo folder, TreeNode tnParent)
2014-12-27 02:58:38 +13:00
{
TreeNode tn = new TreeNode(folder.name);
tn.Tag = folder;
2014-12-28 12:47:48 +13:00
tn.Nodes.Add(new TreeNode(Resources.UploadersConfigForm_OneDriveAddFolder_Querying_folders___));
if (tnParent != null)
{
tnParent.Nodes.Add(tn);
}
else
{
tvOneDrive.Nodes.Add(tn);
}
2014-12-27 02:58:38 +13:00
}
2014-08-09 12:28:09 +12:00
#endregion OneDrive
2013-11-03 23:53:49 +13:00
#region FTP
private bool FTPCheckAccount(int index)
2013-11-03 23:53:49 +13:00
{
2017-04-21 01:56:16 +12:00
return Config.FTPAccountList.IsValidIndex(index);
2013-11-03 23:53:49 +13:00
}
private FTPAccount FTPGetSelectedAccount()
{
2017-04-21 01:56:16 +12:00
int index = cbFTPAccounts.SelectedIndex;
if (FTPCheckAccount(index))
{
2017-04-21 01:56:16 +12:00
return Config.FTPAccountList[index];
}
return null;
}
private void FTPAddAccount(FTPAccount account)
{
if (account != null)
{
Config.FTPAccountList.Add(account);
2017-04-21 01:56:16 +12:00
cbFTPAccounts.Items.Add(account);
FTPUpdateControls();
}
}
2017-04-21 01:56:16 +12:00
private void FTPUpdateControls()
{
int selected = cbFTPAccounts.SelectedIndex;
2013-11-03 23:53:49 +13:00
cbFTPAccounts.Items.Clear();
cbFTPImage.Items.Clear();
cbFTPText.Items.Clear();
cbFTPFile.Items.Clear();
2017-04-21 01:56:16 +12:00
if (Config.FTPAccountList.Count > 0)
{
foreach (FTPAccount account in Config.FTPAccountList)
{
cbFTPAccounts.Items.Add(account);
cbFTPImage.Items.Add(account);
cbFTPText.Items.Add(account);
cbFTPFile.Items.Add(account);
2017-04-21 01:56:16 +12:00
}
2017-04-21 04:38:20 +12:00
2019-05-24 07:59:16 +12:00
cbFTPAccounts.SelectedIndex = selected.Clamp(0, Config.FTPAccountList.Count - 1);
cbFTPImage.SelectedIndex = Config.FTPSelectedImage.Clamp(0, Config.FTPAccountList.Count - 1);
cbFTPText.SelectedIndex = Config.FTPSelectedText.Clamp(0, Config.FTPAccountList.Count - 1);
cbFTPFile.SelectedIndex = Config.FTPSelectedFile.Clamp(0, Config.FTPAccountList.Count - 1);
}
FTPUpdateEnabledStates();
2013-11-03 23:53:49 +13:00
}
2017-04-21 04:38:20 +12:00
private void FTPUpdateEnabledStates()
{
cbFTPImage.Enabled = cbFTPText.Enabled = cbFTPFile.Enabled = cbFTPAccounts.Enabled = cbFTPAccounts.Items.Count > 0;
btnFTPRemove.Enabled = btnFTPDuplicate.Enabled = gbFTPAccount.Enabled = cbFTPAccounts.SelectedIndex > -1;
2017-04-21 07:44:50 +12:00
FTPAccount account = FTPGetSelectedAccount();
if (account != null)
{
gbFTPS.Visible = account.Protocol == FTPProtocol.FTPS;
gbSFTP.Visible = account.Protocol == FTPProtocol.SFTP;
pFTPTransferMode.Enabled = account.Protocol == FTPProtocol.FTP || account.Protocol == FTPProtocol.FTPS;
2017-04-21 07:44:50 +12:00
}
else
{
gbFTPS.Visible = gbSFTP.Visible = false;
}
2017-04-21 04:38:20 +12:00
}
private void FTPRefreshNames()
{
cbFTPAccounts.RefreshItems();
cbFTPImage.RefreshItems();
cbFTPText.RefreshItems();
cbFTPFile.RefreshItems();
}
private void FTPLoadSelectedAccount()
2013-11-03 23:53:49 +13:00
{
2017-04-21 01:56:16 +12:00
FTPAccount account = FTPGetSelectedAccount();
2013-11-03 23:53:49 +13:00
if (account != null)
2013-11-03 23:53:49 +13:00
{
2017-04-21 01:56:16 +12:00
FTPLoadAccount(account);
}
}
private void FTPLoadAccount(FTPAccount account)
2017-04-21 01:56:16 +12:00
{
txtFTPName.Text = account.Name;
2017-04-21 01:56:16 +12:00
switch (account.Protocol)
{
case FTPProtocol.FTP:
rbFTPProtocolFTP.Checked = true;
break;
case FTPProtocol.FTPS:
rbFTPProtocolFTPS.Checked = true;
break;
case FTPProtocol.SFTP:
rbFTPProtocolSFTP.Checked = true;
break;
2013-11-03 23:53:49 +13:00
}
2017-04-21 01:56:16 +12:00
txtFTPHost.Text = account.Host;
nudFTPPort.Value = account.Port;
txtFTPUsername.Text = account.Username;
txtFTPPassword.Text = account.Password;
if (account.IsActive)
{
rbFTPTransferModeActive.Checked = true;
}
else
{
rbFTPTransferModePassive.Checked = true;
}
txtFTPRemoteDirectory.Text = account.SubFolderPath;
2017-04-21 04:38:20 +12:00
cbFTPURLPathProtocol.SelectedIndex = (int)account.BrowserProtocol;
2017-04-21 01:56:16 +12:00
txtFTPURLPath.Text = account.HttpHomePath;
cbFTPAppendRemoteDirectory.Checked = account.HttpHomePathAutoAddSubFolderPath;
cbFTPRemoveFileExtension.Checked = account.HttpHomePathNoExtension;
2017-04-21 04:38:20 +12:00
lblFTPURLPreviewValue.Text = account.PreviewHttpPath;
2017-04-21 07:44:50 +12:00
cbFTPSEncryption.SelectedIndex = (int)account.FTPSEncryption;
txtFTPSCertificateLocation.Text = account.FTPSCertificateLocation;
txtSFTPKeyLocation.Text = account.Keypath;
txtSFTPKeyPassphrase.Text = account.Passphrase;
FTPUpdateEnabledStates();
2017-04-21 04:38:20 +12:00
}
private void FTPClearFields()
2017-04-21 04:38:20 +12:00
{
FTPAccount account = new FTPAccount()
{
Name = "",
HttpHomePathAutoAddSubFolderPath = false
};
FTPLoadAccount(account);
}
private void FTPUpdateURLPreview()
2017-04-21 04:38:20 +12:00
{
FTPAccount account = FTPGetSelectedAccount();
if (account != null)
{
lblFTPURLPreviewValue.Text = account.PreviewHttpPath;
}
2017-04-21 01:56:16 +12:00
}
private async Task FTPTestAccountAsync(FTPAccount account)
2017-04-21 01:56:16 +12:00
{
if (account != null)
{
btnFTPTest.Enabled = false;
await Task.Run(() =>
2017-04-21 01:56:16 +12:00
{
FTPTestAccount(account);
});
btnFTPTest.Enabled = true;
2017-04-21 01:56:16 +12:00
}
2013-11-03 23:53:49 +13:00
}
private void FTPTestAccount(FTPAccount account)
2013-11-03 23:53:49 +13:00
{
2016-05-25 06:15:45 +12:00
string msg = "";
2014-05-31 02:59:08 +12:00
string remotePath = account.GetSubFolderPath();
List<string> directories = new List<string>();
2013-11-03 23:53:49 +13:00
2014-05-31 02:59:08 +12:00
try
2013-11-03 23:53:49 +13:00
{
2014-05-31 02:59:08 +12:00
if (account.Protocol == FTPProtocol.FTP || account.Protocol == FTPProtocol.FTPS)
{
using (FTP ftp = new FTP(account))
2013-11-03 23:53:49 +13:00
{
2014-05-31 02:59:08 +12:00
if (ftp.Connect())
2013-11-03 23:53:49 +13:00
{
2014-05-31 02:59:08 +12:00
if (!ftp.DirectoryExists(remotePath))
2013-11-03 23:53:49 +13:00
{
2014-05-31 02:59:08 +12:00
directories = ftp.CreateMultiDirectory(remotePath);
2013-11-03 23:53:49 +13:00
}
2014-05-31 02:59:08 +12:00
if (ftp.IsConnected)
{
if (directories.Count > 0)
2013-11-03 23:53:49 +13:00
{
msg = Resources.UploadersConfigForm_TestFTPAccount_Connected_Created_folders + "\r\n" + string.Join("\r\n", directories);
2013-11-03 23:53:49 +13:00
}
2014-05-31 02:59:08 +12:00
else
2013-11-03 23:53:49 +13:00
{
msg = Resources.UploadersConfigForm_TestFTPAccount_Connected_;
2013-11-03 23:53:49 +13:00
}
}
}
}
2014-05-31 02:59:08 +12:00
}
else if (account.Protocol == FTPProtocol.SFTP)
{
using (SFTP sftp = new SFTP(account))
2013-11-03 23:53:49 +13:00
{
2014-05-31 02:59:08 +12:00
if (sftp.Connect())
2013-11-03 23:53:49 +13:00
{
2014-05-31 02:59:08 +12:00
if (!sftp.DirectoryExists(remotePath))
2013-11-03 23:53:49 +13:00
{
2014-05-31 02:59:08 +12:00
directories = sftp.CreateMultiDirectory(remotePath);
}
if (sftp.IsConnected)
{
if (directories.Count > 0)
{
msg = Resources.UploadersConfigForm_TestFTPAccount_Connected_Created_folders + "\r\n" + string.Join("\r\n", directories);
2014-05-31 02:59:08 +12:00
}
else
{
msg = Resources.UploadersConfigForm_TestFTPAccount_Connected_;
2014-05-31 02:59:08 +12:00
}
2013-11-03 23:53:49 +13:00
}
}
}
2014-05-31 02:59:08 +12:00
}
2013-11-03 23:53:49 +13:00
}
2014-05-31 02:59:08 +12:00
catch (Exception e)
2013-11-03 23:53:49 +13:00
{
2014-05-31 02:59:08 +12:00
msg = e.Message;
2013-11-03 23:53:49 +13:00
}
2014-05-31 02:59:08 +12:00
MessageBox.Show(msg, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
2013-11-03 23:53:49 +13:00
}
2017-04-21 01:56:16 +12:00
2013-11-03 23:53:49 +13:00
#endregion FTP
#region SendSpace
public UserPassBox SendSpaceRegister()
{
UserPassBox upb = new UserPassBox(Resources.UploadersConfigForm_SendSpaceRegister_SendSpace_Registration___, "John Doe", "john.doe@gmail.com", "JohnDoe", "");
2013-11-03 23:53:49 +13:00
upb.ShowDialog();
if (upb.DialogResult == DialogResult.OK)
{
SendSpace sendSpace = new SendSpace(APIKeys.SendSpaceKey);
upb.Success = sendSpace.AuthRegister(upb.UserName, upb.FullName, upb.Email, upb.Password);
if (!upb.Success && sendSpace.Errors.Count > 0)
{
MessageBox.Show(sendSpace.ToErrorString(), Resources.UploadersConfigForm_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
2013-11-03 23:53:49 +13:00
}
}
return upb;
}
#endregion SendSpace
#region Pastebin
public void PastebinLogin()
{
if (Config.PastebinSettings != null)
{
try
{
Pastebin pastebin = new Pastebin(APIKeys.PastebinKey, Config.PastebinSettings);
if (pastebin.Login())
{
2014-09-15 21:33:29 +12:00
UpdatePastebinStatus();
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
2013-11-03 23:53:49 +13:00
}
else
{
2014-09-15 21:33:29 +12:00
UpdatePastebinStatus();
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
2013-11-03 23:53:49 +13:00
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
}
2014-09-15 21:33:29 +12:00
public void UpdatePastebinStatus()
{
if (Config.PastebinSettings == null || string.IsNullOrEmpty(Config.PastebinSettings.UserKey))
{
2018-02-19 15:35:57 +13:00
lblPastebinLoginStatus.Text = Resources.UploadersConfigForm_UpdatePastebinStatus_NotLoggedIn;
2014-09-15 21:33:29 +12:00
}
else
{
2018-02-19 15:35:57 +13:00
lblPastebinLoginStatus.Text = Resources.UploadersConfigForm_UpdatePastebinStatus_LoggedIn;
2014-09-15 21:33:29 +12:00
}
}
2013-11-03 23:53:49 +13:00
#endregion Pastebin
#region Pushbullet
public void PushbulletGetDevices()
{
2021-08-30 22:01:36 +12:00
cbPushbulletDevices.Items.Clear();
cbPushbulletDevices.ResetText();
2014-03-31 00:37:38 +13:00
Pushbullet pushbullet = new Pushbullet(Config.PushbulletSettings);
Config.PushbulletSettings.DeviceList = pushbullet.GetDeviceList();
2014-03-30 23:28:34 +13:00
if (Config.PushbulletSettings.DeviceList.Count > 0)
{
2014-03-31 00:37:38 +13:00
Config.PushbulletSettings.SelectedDevice = 0;
2021-08-30 22:01:36 +12:00
cbPushbulletDevices.Enabled = true;
2014-03-30 23:28:34 +13:00
Config.PushbulletSettings.DeviceList.ForEach(pbDevice =>
{
2020-08-31 17:03:50 +12:00
if (!string.IsNullOrEmpty(pbDevice.Name))
{
2021-08-30 22:01:36 +12:00
cbPushbulletDevices.Items.Add(pbDevice.Name);
2020-08-31 17:03:50 +12:00
}
2014-03-30 23:28:34 +13:00
});
2021-08-30 22:01:36 +12:00
cbPushbulletDevices.SelectedIndex = 0;
2014-03-30 23:28:34 +13:00
}
}
2014-03-27 21:17:28 +13:00
#endregion Pushbullet
2013-11-03 23:53:49 +13:00
#region Twitter
2015-04-28 13:33:53 +12:00
private OAuthInfo GetSelectedTwitterAccount()
{
return Config.TwitterOAuthInfoList.ReturnIfValidIndex(Config.TwitterSelectedAccount);
}
private bool CheckTwitterAccounts()
2013-11-03 23:53:49 +13:00
{
return Config.TwitterOAuthInfoList.IsValidIndex(Config.TwitterSelectedAccount);
}
2015-04-28 13:33:53 +12:00
private bool TwitterUpdateSelected()
2013-11-03 23:53:49 +13:00
{
Config.TwitterSelectedAccount = lbTwitterAccounts.SelectedIndex;
2015-04-28 13:33:53 +12:00
if (Config.TwitterSelectedAccount > -1)
2013-11-03 23:53:49 +13:00
{
OAuthInfo oauth = Config.TwitterOAuthInfoList[Config.TwitterSelectedAccount];
2013-11-03 23:53:49 +13:00
2015-04-28 13:33:53 +12:00
if (oauth != null)
{
txtTwitterDescription.Enabled = true;
2015-04-28 13:33:53 +12:00
txtTwitterDescription.Text = oauth.Description;
oauthTwitter.Enabled = true;
if (OAuthInfo.CheckOAuth(oauth))
{
oauthTwitter.Status = OAuthLoginStatus.LoginSuccessful;
}
else
{
oauthTwitter.Status = OAuthLoginStatus.LoginRequired;
}
return true;
}
}
2015-04-28 13:33:53 +12:00
txtTwitterDescription.Enabled = false;
2016-05-25 06:15:45 +12:00
txtTwitterDescription.Text = "";
2015-04-28 13:33:53 +12:00
oauthTwitter.Enabled = false;
return false;
2013-11-03 23:53:49 +13:00
}
2015-04-28 13:33:53 +12:00
private void TwitterAuthOpen()
2013-11-03 23:53:49 +13:00
{
if (CheckTwitterAccounts())
2013-11-03 23:53:49 +13:00
{
2015-04-28 13:33:53 +12:00
try
2013-11-03 23:53:49 +13:00
{
2015-04-28 13:33:53 +12:00
OAuthInfo oauth = new OAuthInfo(APIKeys.TwitterConsumerKey, APIKeys.TwitterConsumerSecret);
2015-04-28 13:33:53 +12:00
string url = new Twitter(oauth).GetAuthorizationURL();
if (!string.IsNullOrEmpty(url))
{
2015-04-28 13:33:53 +12:00
oauth.Description = Config.TwitterOAuthInfoList[Config.TwitterSelectedAccount].Description;
Config.TwitterOAuthInfoList[Config.TwitterSelectedAccount] = oauth;
URLHelpers.OpenURL(url);
DebugHelper.WriteLine("TwitterAuthOpen - Authorization URL is opened: " + url);
}
else
{
2015-04-28 13:33:53 +12:00
DebugHelper.WriteLine("TwitterAuthOpen - Authorization URL is empty.");
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2015-04-28 13:33:53 +12:00
}
}
}
private void TwitterAuthComplete(string code)
{
if (CheckTwitterAccounts())
{
try
{
OAuthInfo oauth = GetSelectedTwitterAccount();
if (oauth != null && !string.IsNullOrEmpty(oauth.AuthToken) && !string.IsNullOrEmpty(oauth.AuthSecret))
{
bool result = new Twitter(oauth).GetAccessToken(code);
if (result)
{
2016-05-25 06:15:45 +12:00
oauth.AuthVerifier = "";
2015-04-28 13:33:53 +12:00
oauthTwitter.Status = OAuthLoginStatus.LoginSuccessful;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
oauthTwitter.Status = OAuthLoginStatus.LoginFailed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2013-11-03 23:53:49 +13:00
}
2015-04-28 13:33:53 +12:00
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2015-04-28 13:33:53 +12:00
}
}
}
private void TwitterAuthClear()
{
if (CheckTwitterAccounts())
{
OAuthInfo oauth = new OAuthInfo();
OAuthInfo oauth2 = GetSelectedTwitterAccount();
if (oauth2 != null)
{
oauth.Description = oauth2.Description;
}
Config.TwitterOAuthInfoList[Config.TwitterSelectedAccount] = oauth;
2013-11-03 23:53:49 +13:00
}
}
#endregion Twitter
#region Jira
public void JiraAuthOpen()
{
try
{
OAuthInfo oauth = new OAuthInfo(APIKeys.JiraConsumerKey);
oauth.SignatureMethod = OAuthInfo.OAuthInfoSignatureMethod.RSA_SHA1;
oauth.ConsumerPrivateKey = Jira.PrivateKey;
string url = new Jira(Config.JiraHost, oauth).GetAuthorizationURL();
if (!string.IsNullOrEmpty(url))
{
Config.JiraOAuthInfo = oauth;
URLHelpers.OpenURL(url);
2013-11-03 23:53:49 +13:00
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
public void JiraAuthComplete(string code)
{
try
{
if (!string.IsNullOrEmpty(code) && Config.JiraOAuthInfo != null && !string.IsNullOrEmpty(Config.JiraOAuthInfo.AuthToken) && !string.IsNullOrEmpty(Config.JiraOAuthInfo.AuthSecret))
2013-11-03 23:53:49 +13:00
{
Jira jira = new Jira(Config.JiraHost, Config.JiraOAuthInfo);
bool result = jira.GetAccessToken(code);
if (result)
{
oAuthJira.Status = OAuthLoginStatus.LoginSuccessful;
MessageBox.Show(Resources.UploadersConfigForm_Login_successful, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
2013-11-03 23:53:49 +13:00
}
else
{
oAuthJira.Status = OAuthLoginStatus.LoginFailed;
MessageBox.Show(Resources.UploadersConfigForm_Login_failed, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
2013-11-03 23:53:49 +13:00
}
}
}
catch (Exception ex)
{
2017-04-22 08:42:52 +12:00
ex.ShowError();
2013-11-03 23:53:49 +13:00
}
}
#endregion Jira
2018-04-22 21:10:23 +12:00
#region Shared folder
private void SharedFolderUpdateControls()
{
int selected = lbSharedFolderAccounts.SelectedIndex;
lbSharedFolderAccounts.Items.Clear();
2021-08-30 22:01:36 +12:00
cbSharedFolderImages.Items.Clear();
cbSharedFolderText.Items.Clear();
cbSharedFolderFiles.Items.Clear();
2018-04-22 21:10:23 +12:00
if (Config.LocalhostAccountList.Count > 0)
{
foreach (LocalhostAccount account in Config.LocalhostAccountList)
{
lbSharedFolderAccounts.Items.Add(account);
2021-08-30 22:01:36 +12:00
cbSharedFolderImages.Items.Add(account);
cbSharedFolderText.Items.Add(account);
cbSharedFolderFiles.Items.Add(account);
2018-04-22 21:10:23 +12:00
}
2019-05-24 07:59:16 +12:00
lbSharedFolderAccounts.SelectedIndex = selected.Clamp(0, Config.LocalhostAccountList.Count - 1);
2021-08-30 22:01:36 +12:00
cbSharedFolderImages.SelectedIndex = Config.LocalhostSelectedImages.Clamp(0, Config.LocalhostAccountList.Count - 1);
cbSharedFolderText.SelectedIndex = Config.LocalhostSelectedText.Clamp(0, Config.LocalhostAccountList.Count - 1);
cbSharedFolderFiles.SelectedIndex = Config.LocalhostSelectedFiles.Clamp(0, Config.LocalhostAccountList.Count - 1);
2018-04-22 21:10:23 +12:00
}
SharedFolderUpdateEnabledStates();
}
private void SharedFolderUpdateEnabledStates()
{
2021-08-30 22:01:36 +12:00
cbSharedFolderImages.Enabled = cbSharedFolderText.Enabled = cbSharedFolderFiles.Enabled = Config.LocalhostAccountList.Count > 0;
2018-04-22 21:10:23 +12:00
btnSharedFolderRemove.Enabled = btnSharedFolderDuplicate.Enabled = lbSharedFolderAccounts.SelectedIndex > -1;
}
private void SharedFolderAddItem(LocalhostAccount account)
{
Config.LocalhostAccountList.Add(account);
lbSharedFolderAccounts.Items.Add(account);
lbSharedFolderAccounts.SelectedIndex = lbSharedFolderAccounts.Items.Count - 1;
SharedFolderUpdateControls();
}
private bool SharedFolderRemoveItem(int index)
{
if (index.IsBetween(0, lbSharedFolderAccounts.Items.Count - 1))
{
Config.LocalhostAccountList.RemoveAt(index);
lbSharedFolderAccounts.Items.RemoveAt(index);
if (lbSharedFolderAccounts.Items.Count > 0)
{
lbSharedFolderAccounts.SelectedIndex = index == lbSharedFolderAccounts.Items.Count ? lbSharedFolderAccounts.Items.Count - 1 : index;
pgSharedFolderAccount.SelectedObject = lbSharedFolderAccounts.Items[lbSharedFolderAccounts.SelectedIndex];
}
else
{
pgSharedFolderAccount.SelectedObject = null;
}
SharedFolderUpdateControls();
return true;
}
return false;
}
#endregion Shared folder
2018-05-16 21:40:15 +12:00
#region Generic OAuth2
private OAuth2Info OAuth2Open(IOAuth2Basic uploader)
2018-05-16 21:40:15 +12:00
{
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(IOAuth2Basic uploader, string code, OAuthControl control)
2018-05-16 21:40:15 +12:00
{
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
2013-11-03 23:53:49 +13:00
}
}