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
2014-05-13 21:06:40 +12:00
Copyright ( C ) 2007 - 2014 ShareX Developers
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 )
using HelpersLib ;
2013-12-03 01:08:09 +13:00
using Newtonsoft.Json ;
2013-11-03 23:53:49 +13:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2014-03-21 12:37:43 +13:00
using System.Linq ;
2014-05-31 02:59:08 +12:00
using System.Net.FtpClient ;
2013-11-03 23:53:49 +13:00
using System.Net.NetworkInformation ;
using System.Text ;
using System.Threading ;
using System.Windows.Forms ;
using UploadersLib.FileUploaders ;
using UploadersLib.Forms ;
2014-04-17 09:34:34 +12:00
using UploadersLib.GUI ;
2013-11-03 23:53:49 +13:00
using UploadersLib.HelperClasses ;
using UploadersLib.ImageUploaders ;
using UploadersLib.SocialServices ;
using UploadersLib.TextUploaders ;
using UploadersLib.URLShorteners ;
namespace UploadersLib
{
public partial class UploadersConfigForm : Form
{
#region Imgur
public void ImgurAuthOpen ( )
{
try
{
OAuth2Info oauth = new OAuth2Info ( APIKeys . ImgurClientID , APIKeys . ImgurClientSecret ) ;
string url = new Imgur_v3 ( oauth ) . GetAuthorizationURL ( ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
Config . ImgurOAuth2Info = oauth ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-11-03 23:53:49 +13:00
DebugHelper . WriteLine ( "ImgurAuthOpen - Authorization URL is opened: " + url ) ;
}
else
{
DebugHelper . WriteLine ( "ImgurAuthOpen - Authorization URL is empty." ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void ImgurAuthComplete ( string code )
{
try
{
if ( ! string . IsNullOrEmpty ( code ) & & Config . ImgurOAuth2Info ! = null )
{
bool result = new Imgur_v3 ( Config . ImgurOAuth2Info ) . GetAccessToken ( code ) ;
if ( result )
{
oauth2Imgur . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2Imgur . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
atcImgurAccountType . SelectedAccountType = AccountType . Anonymous ;
}
oauth2Imgur . LoginStatus = result ;
btnImgurRefreshAlbumList . Enabled = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void ImgurAuthRefresh ( )
{
try
{
if ( OAuth2Info . CheckOAuth ( Config . ImgurOAuth2Info ) )
{
bool result = new Imgur_v3 ( Config . ImgurOAuth2Info ) . RefreshAccessToken ( ) ;
if ( result )
{
oauth2Imgur . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2Imgur . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
atcImgurAccountType . SelectedAccountType = AccountType . Anonymous ;
}
btnImgurRefreshAlbumList . Enabled = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void ImgurRefreshAlbumList ( )
{
try
{
lvImgurAlbumList . Items . Clear ( ) ;
if ( OAuth2Info . CheckOAuth ( Config . ImgurOAuth2Info ) )
{
List < ImgurAlbumData > albums = new Imgur_v3 ( Config . ImgurOAuth2Info ) . GetAlbums ( ) ;
if ( albums ! = null & & albums . Count > 0 )
{
foreach ( ImgurAlbumData album in albums )
{
ListViewItem lvi = new ListViewItem ( album . id ) ;
lvi . SubItems . Add ( album . title ? ? "" ) ;
lvi . SubItems . Add ( album . description ? ? "" ) ;
lvi . Tag = album ;
lvImgurAlbumList . Items . Add ( lvi ) ;
}
}
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
#endregion Imgur
#region Flickr
public void FlickrAuthOpen ( )
{
try
{
FlickrUploader flickr = new FlickrUploader ( APIKeys . FlickrKey , APIKeys . FlickrSecret ) ;
btnFlickrOpenAuthorize . Tag = flickr . GetFrob ( ) ;
string url = flickr . GetAuthLink ( FlickrPermission . Write ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-11-03 23:53:49 +13:00
btnFlickrCompleteAuth . Enabled = true ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void FlickrAuthComplete ( )
{
try
{
string token = btnFlickrOpenAuthorize . Tag as string ;
if ( ! string . IsNullOrEmpty ( token ) )
{
FlickrUploader flickr = new FlickrUploader ( APIKeys . FlickrKey , APIKeys . FlickrSecret ) ;
Config . FlickrAuthInfo = flickr . GetToken ( token ) ;
pgFlickrAuthInfo . SelectedObject = Config . FlickrAuthInfo ;
// btnFlickrOpenImages.Text = string.Format("{0}'s photostream", Config.FlickrAuthInfo.Username);
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void FlickrCheckToken ( )
{
try
{
if ( Config . FlickrAuthInfo ! = null )
{
string token = Config . FlickrAuthInfo . Token ;
if ( ! string . IsNullOrEmpty ( token ) )
{
FlickrUploader flickr = new FlickrUploader ( APIKeys . FlickrKey , APIKeys . FlickrSecret ) ;
Config . FlickrAuthInfo = flickr . CheckToken ( token ) ;
pgFlickrAuthInfo . SelectedObject = Config . FlickrAuthInfo ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void FlickrOpenImages ( )
{
if ( Config . FlickrAuthInfo ! = null )
{
string userID = Config . FlickrAuthInfo . UserID ;
if ( ! string . IsNullOrEmpty ( userID ) )
{
FlickrUploader flickr = new FlickrUploader ( APIKeys . FlickrKey , APIKeys . FlickrSecret ) ;
string url = flickr . GetPhotosLink ( userID ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
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 ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-11-03 23:53:49 +13:00
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
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 ( ) ;
2014-05-30 23:38:23 +12:00
lblPhotobucketAccountStatus . Text = "Login successful." ;
2013-11-03 23:53:49 +13:00
txtPhotobucketDefaultAlbumName . Text = Config . PhotobucketAccountInfo . AlbumID ;
Config . PhotobucketAccountInfo . AlbumList . Add ( Config . PhotobucketAccountInfo . AlbumID ) ;
cboPhotobucketAlbumPaths . Items . Add ( Config . PhotobucketAccountInfo . AlbumID ) ;
cboPhotobucketAlbumPaths . SelectedIndex = 0 ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
lblPhotobucketAccountStatus . Text = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
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 ) ;
cboPhotobucketAlbumPaths . Items . Add ( albumPath ) ;
MessageBox . Show ( albumPath + " successfully created." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
}
#endregion Photobucket
#region Picasa
public void PicasaAuthOpen ( )
{
try
{
OAuth2Info oauth = new OAuth2Info ( APIKeys . GoogleClientID , APIKeys . GoogleClientSecret ) ;
string url = new Picasa ( oauth ) . GetAuthorizationURL ( ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
Config . PicasaOAuth2Info = oauth ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-11-03 23:53:49 +13:00
DebugHelper . WriteLine ( "PicasaAuthOpen - Authorization URL is opened: " + url ) ;
}
else
{
DebugHelper . WriteLine ( "PicasaAuthOpen - Authorization URL is empty." ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void PicasaAuthComplete ( string code )
{
try
{
if ( ! string . IsNullOrEmpty ( code ) & & Config . PicasaOAuth2Info ! = null )
{
bool result = new GoogleDrive ( Config . PicasaOAuth2Info ) . GetAccessToken ( code ) ;
if ( result )
{
oauth2Picasa . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2Picasa . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
oauth2Picasa . LoginStatus = result ;
btnPicasaRefreshAlbumList . Enabled = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void PicasaAuthRefresh ( )
{
try
{
if ( OAuth2Info . CheckOAuth ( Config . PicasaOAuth2Info ) )
{
bool result = new GoogleDrive ( Config . PicasaOAuth2Info ) . RefreshAccessToken ( ) ;
if ( result )
{
oauth2Picasa . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2Picasa . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
btnPicasaRefreshAlbumList . Enabled = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void PicasaRefreshAlbumList ( )
{
try
{
lvPicasaAlbumList . Items . Clear ( ) ;
if ( OAuth2Info . CheckOAuth ( Config . PicasaOAuth2Info ) )
{
List < PicasaAlbumInfo > albums = new Picasa ( Config . PicasaOAuth2Info ) . GetAlbumList ( ) ;
if ( albums ! = null & & albums . Count > 0 )
{
foreach ( PicasaAlbumInfo album in albums )
{
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 )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
#endregion Picasa
#region Dropbox
public void DropboxOpenFiles ( )
{
if ( OAuthInfo . CheckOAuth ( Config . DropboxOAuthInfo ) )
{
using ( DropboxFilesForm filesForm = new DropboxFilesForm ( Config . DropboxOAuthInfo , GetDropboxUploadPath ( ) , Config . DropboxAccountInfo ) )
{
if ( filesForm . ShowDialog ( ) = = DialogResult . OK )
{
txtDropboxPath . Text = filesForm . CurrentFolderPath ;
}
}
}
}
public void DropboxAuthOpen ( )
{
try
{
OAuthInfo oauth = new OAuthInfo ( APIKeys . DropboxConsumerKey , APIKeys . DropboxConsumerSecret ) ;
string url = new Dropbox ( oauth ) . GetAuthorizationURL ( ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
Config . DropboxOAuthInfo = oauth ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-11-03 23:53:49 +13:00
btnDropboxCompleteAuth . Enabled = true ;
DebugHelper . WriteLine ( "DropboxAuthOpen - Authorization URL is opened: " + url ) ;
}
else
{
DebugHelper . WriteLine ( "DropboxAuthOpen - Authorization URL is empty." ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void DropboxAuthComplete ( )
{
try
{
2014-05-30 23:38:23 +12:00
if ( Config . DropboxOAuthInfo ! = null & & ! string . IsNullOrEmpty ( Config . DropboxOAuthInfo . AuthToken ) & & ! string . IsNullOrEmpty ( Config . DropboxOAuthInfo . AuthSecret ) )
2013-11-03 23:53:49 +13:00
{
Dropbox dropbox = new Dropbox ( Config . DropboxOAuthInfo ) ;
bool result = dropbox . GetAccessToken ( ) ;
if ( result )
{
DropboxAccountInfo account = dropbox . GetAccountInfo ( ) ;
if ( account ! = null )
{
Config . DropboxAccountInfo = account ;
Config . DropboxUploadPath = txtDropboxPath . Text ;
UpdateDropboxStatus ( ) ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
return ;
}
MessageBox . Show ( "GetAccountInfo failed." , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
else
{
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
else
{
MessageBox . Show ( "You must give access from Authorize page first." , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
Config . DropboxOAuthInfo = null ;
UpdateDropboxStatus ( ) ;
}
catch ( Exception ex )
{
DebugHelper . WriteException ( ex ) ;
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
private void UpdateDropboxStatus ( )
{
if ( OAuthInfo . CheckOAuth ( Config . DropboxOAuthInfo ) & & Config . DropboxAccountInfo ! = null )
{
StringBuilder sb = new StringBuilder ( ) ;
sb . AppendLine ( "Login status: Successful" ) ;
sb . AppendLine ( "Email: " + Config . DropboxAccountInfo . Email ) ;
sb . AppendLine ( "Name: " + Config . DropboxAccountInfo . Display_name ) ;
sb . AppendLine ( "User ID: " + Config . DropboxAccountInfo . Uid . ToString ( ) ) ;
string uploadPath = GetDropboxUploadPath ( ) ;
sb . AppendLine ( "Upload path: " + uploadPath ) ;
2014-05-06 11:24:00 +12:00
sb . AppendLine ( "Download path: " + Dropbox . GetPublicURL ( Config . DropboxAccountInfo . Uid , uploadPath + "Example.png" ) ) ;
2013-11-03 23:53:49 +13:00
lblDropboxStatus . Text = sb . ToString ( ) ;
btnDropboxShowFiles . Enabled = true ;
}
else
{
lblDropboxStatus . Text = "Login status: Authorize required" ;
}
}
private string GetDropboxUploadPath ( )
{
2014-06-06 07:47:36 +12:00
return NameParser . Parse ( NameParserType . URL , Dropbox . TidyUploadPath ( Config . DropboxUploadPath ) ) ;
2013-11-03 23:53:49 +13:00
}
#endregion Dropbox
2014-06-06 06:19:45 +12:00
#region Copy
public void CopyAuthOpen ( )
{
try
{
OAuthInfo oauth = new OAuthInfo ( APIKeys . CopyConsumerKey , APIKeys . CopyConsumerSecret ) ;
string url = new Copy ( oauth ) . GetAuthorizationURL ( ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
Config . CopyOAuthInfo = oauth ;
Helpers . OpenURL ( url ) ;
DebugHelper . WriteLine ( "CopyAuthOpen - Authorization URL is opened: " + url ) ;
}
else
{
DebugHelper . WriteLine ( "CopyAuthOpen - Authorization URL is empty." ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void CopyAuthComplete ( string code )
{
try
{
2014-06-06 07:47:36 +12:00
if ( Config . CopyOAuthInfo ! = null & & ! string . IsNullOrEmpty ( Config . CopyOAuthInfo . AuthToken ) & &
! string . IsNullOrEmpty ( Config . CopyOAuthInfo . AuthSecret ) & & ! string . IsNullOrEmpty ( code ) )
2014-06-06 06:19:45 +12:00
{
Copy copy = new Copy ( Config . CopyOAuthInfo ) ;
bool result = copy . GetAccessToken ( code ) ;
if ( result )
{
CopyAccountInfo account = copy . GetAccountInfo ( ) ;
if ( account ! = null )
{
Config . CopyAccountInfo = account ;
Config . CopyUploadPath = txtCopyPath . Text ;
UpdateCopyStatus ( ) ;
2014-06-06 07:47:36 +12:00
oAuthCopy . Status = "Login successful." ;
2014-06-06 06:19:45 +12:00
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
return ;
}
MessageBox . Show ( "GetAccountInfo failed." , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
else
{
2014-06-06 07:47:36 +12:00
oAuthCopy . Status = "Login failed." ;
2014-06-06 06:19:45 +12:00
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
else
{
MessageBox . Show ( "You must gain access from the Authorize page and copy the verification code into the box first." , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
Config . CopyOAuthInfo = null ;
UpdateCopyStatus ( ) ;
}
catch ( Exception ex )
{
DebugHelper . WriteException ( ex ) ;
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
private void UpdateCopyStatus ( )
{
if ( OAuthInfo . CheckOAuth ( Config . CopyOAuthInfo ) & & Config . CopyAccountInfo ! = null )
{
StringBuilder sb = new StringBuilder ( ) ;
sb . AppendLine ( "Email: " + Config . CopyAccountInfo . email ) ;
sb . AppendLine ( "Name: " + Config . CopyAccountInfo . first_name + " " + Config . CopyAccountInfo . last_name ) ;
sb . AppendLine ( "User ID: " + Config . CopyAccountInfo . id . ToString ( ) ) ;
sb . AppendLine ( "Upload path: " + GetCopyUploadPath ( ) ) ;
lblCopyStatus . Text = sb . ToString ( ) ;
}
else
{
2014-06-06 09:06:49 +12:00
lblCopyStatus . Text = string . Empty ;
2014-06-06 06:19:45 +12:00
}
}
private string GetCopyUploadPath ( )
{
2014-06-06 07:47:36 +12:00
return NameParser . Parse ( NameParserType . URL , Copy . TidyUploadPath ( Config . CopyUploadPath ) ) ;
2014-06-06 06:19:45 +12:00
}
#endregion Copy
2014-05-06 11:24:00 +12:00
#region Amazon S3
private void UpdateAmazonS3Status ( )
{
lblAmazonS3PathPreview . Text = new AmazonS3 ( Config . AmazonS3Settings ) . GetURL ( "Example.png" ) ;
}
#endregion Amazon S3
2013-11-03 23:53:49 +13:00
#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 ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-11-03 23:53:49 +13:00
DebugHelper . WriteLine ( "GoogleDriveAuthOpen - Authorization URL is opened: " + url ) ;
}
else
{
DebugHelper . WriteLine ( "GoogleDriveAuthOpen - Authorization URL is empty." ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
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 = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2GoogleDrive . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
oauth2GoogleDrive . LoginStatus = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void GoogleDriveAuthRefresh ( )
{
try
{
if ( OAuth2Info . CheckOAuth ( Config . GoogleDriveOAuth2Info ) )
{
bool result = new GoogleDrive ( Config . GoogleDriveOAuth2Info ) . RefreshAccessToken ( ) ;
if ( result )
{
oauth2GoogleDrive . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2GoogleDrive . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
#endregion Google Drive
#region Box
public void BoxAuthOpen ( )
{
try
{
2014-04-17 04:18:25 +12:00
OAuth2Info oauth = new OAuth2Info ( APIKeys . BoxClientID , APIKeys . BoxClientSecret ) ;
2013-11-03 23:53:49 +13:00
2014-04-17 04:18:25 +12:00
string url = new Box ( oauth ) . GetAuthorizationURL ( ) ;
2013-11-03 23:53:49 +13:00
if ( ! string . IsNullOrEmpty ( url ) )
{
2014-04-17 04:18:25 +12:00
Config . BoxOAuth2Info = oauth ;
2014-04-17 09:34:34 +12:00
//Helpers.LoadBrowserAsync(url);
2014-04-17 04:18:25 +12:00
DebugHelper . WriteLine ( "BoxAuthOpen - Authorization URL is opened: " + url ) ;
2014-04-17 09:34:34 +12:00
// Workaround for authorization because we don't have callback url which starts with https://
using ( OAuthWebForm oauthForm = new OAuthWebForm ( url , "https://www.box.com/home/" ) )
{
if ( oauthForm . ShowDialog ( ) = = DialogResult . OK )
{
BoxAuthComplete ( oauthForm . Code ) ;
}
}
2014-04-17 04:18:25 +12:00
}
else
{
DebugHelper . WriteLine ( "BoxAuthOpen - Authorization URL is empty." ) ;
2013-11-03 23:53:49 +13:00
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
2014-04-17 04:18:25 +12:00
public void BoxAuthComplete ( string code )
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
try
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
if ( ! string . IsNullOrEmpty ( code ) & & Config . BoxOAuth2Info ! = null )
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
bool result = new Box ( Config . BoxOAuth2Info ) . GetAccessToken ( code ) ;
2013-11-03 23:53:49 +13:00
2014-04-17 04:18:25 +12:00
if ( result )
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
oauth2Box . Status = "Login successful." ;
2013-11-03 23:53:49 +13:00
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
2014-04-17 04:18:25 +12:00
oauth2Box . Status = "Login failed." ;
2013-11-03 23:53:49 +13:00
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
2014-04-17 04:18:25 +12:00
oauth2Box . LoginStatus = result ;
btnBoxRefreshFolders . Enabled = result ;
2013-11-03 23:53:49 +13:00
}
2014-04-17 04:18:25 +12:00
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void BoxAuthRefresh ( )
{
try
{
if ( OAuth2Info . CheckOAuth ( Config . BoxOAuth2Info ) )
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
bool result = new Box ( Config . BoxOAuth2Info ) . RefreshAccessToken ( ) ;
if ( result )
{
oauth2Box . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2Box . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
btnBoxRefreshFolders . Enabled = result ;
2013-11-03 23:53:49 +13:00
}
}
2014-04-17 04:18:25 +12:00
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
2013-11-03 23:53:49 +13:00
}
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
{
MessageBox . Show ( "Authentication required." , "Box refresh folders list failed" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
}
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
#region Minus
public void MinusAuth ( )
{
if ( ! string . IsNullOrEmpty ( txtMinusUsername . Text ) & & ! string . IsNullOrEmpty ( txtMinusPassword . Text ) )
{
btnMinusAuth . Enabled = false ;
2014-03-21 12:37:43 +13:00
btnMinusRefreshAuth . Enabled = false ;
2013-11-03 23:53:49 +13:00
try
{
Config . MinusConfig . Username = txtMinusUsername . Text ;
Config . MinusConfig . Password = txtMinusPassword . Text ;
2014-03-21 12:37:43 +13:00
Config . MinusOAuth2Info = new OAuth2Info ( APIKeys . MinusConsumerKey , APIKeys . MinusConsumerSecret ) ;
Minus minus = new Minus ( Config . MinusConfig , Config . MinusOAuth2Info ) ;
2013-11-03 23:53:49 +13:00
2014-03-21 12:37:43 +13:00
if ( minus . GetAccessToken ( ) )
2013-11-03 23:53:49 +13:00
{
2014-03-21 12:37:43 +13:00
minus . ReadFolderList ( ) ;
MinusUpdateControls ( ) ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
2013-11-03 23:53:49 +13:00
}
else
{
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( "Error: " + ex . Message , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
finally
{
btnMinusAuth . Enabled = true ;
2014-03-21 12:37:43 +13:00
btnMinusRefreshAuth . Enabled = true ;
2013-11-03 23:53:49 +13:00
}
}
}
public void MinusAuthRefresh ( )
{
2014-03-21 12:37:43 +13:00
btnMinusAuth . Enabled = false ;
btnMinusRefreshAuth . Enabled = false ;
try
2013-11-03 23:53:49 +13:00
{
2014-03-21 12:37:43 +13:00
if ( OAuth2Info . CheckOAuth ( Config . MinusOAuth2Info ) )
2013-11-03 23:53:49 +13:00
{
2014-03-21 12:37:43 +13:00
bool result = new Minus ( Config . MinusConfig , Config . MinusOAuth2Info ) . RefreshAccessToken ( ) ;
if ( result )
{
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
2013-11-03 23:53:49 +13:00
}
}
2014-03-21 12:37:43 +13:00
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
finally
{
btnMinusAuth . Enabled = true ;
btnMinusRefreshAuth . Enabled = true ;
}
2013-11-03 23:53:49 +13:00
}
public void MinusUpdateControls ( )
{
2014-03-21 12:37:43 +13:00
if ( Config . MinusConfig ! = null & & Config . MinusConfig . MinusUser ! = null & & OAuth2Info . CheckOAuth ( Config . MinusOAuth2Info ) )
2013-11-03 23:53:49 +13:00
{
2014-03-21 12:37:43 +13:00
lblMinusAuthStatus . Text = "Logged in as " + Config . MinusConfig . MinusUser . display_name + "." ;
2013-11-03 23:53:49 +13:00
txtMinusUsername . Text = Config . MinusConfig . Username ;
txtMinusPassword . Text = Config . MinusConfig . Password ;
cboMinusFolders . Items . Clear ( ) ;
if ( Config . MinusConfig . FolderList . Count > 0 )
{
cboMinusFolders . Items . AddRange ( Config . MinusConfig . FolderList . ToArray ( ) ) ;
cboMinusFolders . SelectedIndex = Config . MinusConfig . FolderID . BetweenOrDefault ( 0 , cboMinusFolders . Items . Count - 1 ) ;
}
cbMinusURLType . SelectedIndex = ( int ) Config . MinusConfig . LinkType ;
}
else
{
lblMinusAuthStatus . Text = "Not logged in." ;
2014-03-21 12:37:43 +13:00
btnMinusRefreshAuth . Enabled = false ;
2013-11-03 23:53:49 +13:00
}
}
2014-03-21 12:37:43 +13:00
private bool MinusHasFolder ( string name )
{
return cboMinusFolders . Items . Cast < MinusFolder > ( ) . Any ( mf = > mf . name = = name ) ;
}
2013-11-03 23:53:49 +13:00
#endregion Minus
#region FTP
public bool CheckFTPAccounts ( )
{
return Config . FTPAccountList . IsValidIndex ( Config . FTPSelectedImage ) ;
}
public void TestFTPAccountAsync ( FTPAccount acc )
{
if ( acc ! = null )
{
ucFTPAccounts . btnTest . Enabled = false ;
2014-05-24 03:39:14 +12:00
TaskEx . Run ( ( ) = >
2014-05-22 13:48:27 +12:00
{
2014-05-31 02:59:08 +12:00
TestFTPAccount ( acc ) ;
2014-05-22 13:48:27 +12:00
} ,
( ) = >
{
ucFTPAccounts . btnTest . Enabled = true ;
} ) ;
}
2013-11-03 23:53:49 +13:00
}
private void FTPOpenClient ( )
{
if ( CheckFTPAccounts ( ) )
{
2014-05-31 03:24:43 +12:00
new FTPClientForm ( Config . FTPAccountList [ ucFTPAccounts . lbAccounts . SelectedIndex ] ) . Show ( ) ;
2013-11-03 23:53:49 +13:00
}
}
private void FTPAccountsExport ( )
{
if ( Config . FTPAccountList ! = null )
{
SaveFileDialog dlg = new SaveFileDialog
{
FileName = string . Format ( "{0}-{1}-accounts" , Application . ProductName , DateTime . Now . ToString ( "yyyyMMdd" ) ) ,
Filter = "FTP Accounts(*.json)|*.json"
} ;
if ( dlg . ShowDialog ( ) = = DialogResult . OK )
{
2014-06-07 05:21:03 +12:00
//FTPAccountManager fam = new FTPAccountManager(Config.FTPAccountList);
//fam.Save(dlg.FileName);
2013-11-03 23:53:49 +13:00
}
}
}
private void FTPAccountsImport ( )
{
OpenFileDialog dlg = new OpenFileDialog { Filter = "FTP Accounts(*.json)|*.json" } ;
if ( dlg . ShowDialog ( ) = = DialogResult . OK )
{
2014-06-07 05:21:03 +12:00
//FTPAccountManager fam = FTPAccountManager.Read(dlg.FileName);
//FTPSetup(fam.FTPAccounts);
2013-11-03 23:53:49 +13:00
}
}
2014-05-31 02:59:08 +12:00
public static void TestFTPAccount ( FTPAccount account )
2013-11-03 23:53:49 +13:00
{
string msg = string . Empty ;
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
{
2014-05-31 02:59:08 +12:00
msg = "Connected!\r\nCreated 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
{
2014-05-31 02:59:08 +12:00
msg = "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 = "Connected!\r\nCreated folders:\r\n" + string . Join ( "\r\n" , directories ) ;
}
else
{
msg = "Connected!" ;
}
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 , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
2013-11-03 23:53:49 +13:00
}
public static string SendPing ( string host )
{
return SendPing ( host , 1 ) ;
}
public static string SendPing ( string host , int count )
{
string [ ] status = new string [ count ] ;
using ( Ping ping = new Ping ( ) )
{
PingReply reply ;
//byte[] buffer = Encoding.ASCII.GetBytes(new string('a', 32));
for ( int i = 0 ; i < count ; i + + )
{
reply = ping . Send ( host , 3000 ) ;
if ( reply . Status = = IPStatus . Success )
{
status [ i ] = reply . RoundtripTime . ToString ( ) + " ms" ;
}
else
{
status [ i ] = "Timeout" ;
}
Thread . Sleep ( 100 ) ;
}
}
return string . Join ( ", " , status ) ;
}
public FTPAccount GetFtpAcctActive ( )
{
FTPAccount acc = null ;
if ( CheckFTPAccounts ( ) )
{
acc = Config . FTPAccountList [ Config . FTPSelectedImage ] ;
}
return acc ;
}
#endregion FTP
#region SendSpace
public UserPassBox SendSpaceRegister ( )
{
UserPassBox upb = new UserPassBox ( "SendSpace Registration..." , "John Doe" , "john.doe@gmail.com" , "JohnDoe" , "" ) ;
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 ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
return upb ;
}
#endregion SendSpace
#region Ge . tt
public void Ge_ttLogin ( )
{
try
{
Ge_tt gett = new Ge_tt ( APIKeys . Ge_ttKey ) ;
Ge_ttLogin login = gett . Login ( txtGe_ttEmail . Text , txtGe_ttPassword . Text ) ;
Config . Ge_ttLogin = login ;
2014-05-30 23:38:23 +12:00
lblGe_ttStatus . Text = "Login successful." ;
2013-11-03 23:53:49 +13:00
}
catch ( Exception ex )
{
Config . Ge_ttLogin = null ;
2014-05-30 23:38:23 +12:00
lblGe_ttStatus . Text = "Login failed." ;
2013-11-03 23:53:49 +13:00
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
#endregion Ge . tt
#region Pastebin
public void PastebinLogin ( )
{
if ( Config . PastebinSettings ! = null )
{
try
{
Pastebin pastebin = new Pastebin ( APIKeys . PastebinKey , Config . PastebinSettings ) ;
if ( pastebin . Login ( ) )
{
pgPastebinSettings . SelectedObject = Config . PastebinSettings ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
}
#endregion Pastebin
2014-03-27 20:05:14 +13:00
#region Pushbullet
public void PushbulletGetDevices ( )
{
cboPushbulletDevices . Items . Clear ( ) ;
2014-03-31 00:37:38 +13:00
cboPushbulletDevices . ResetText ( ) ;
2014-03-27 20:05:14 +13:00
2014-03-31 00:37:38 +13:00
Pushbullet pushbullet = new Pushbullet ( Config . PushbulletSettings ) ;
2014-03-27 20:05:14 +13:00
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 ;
2014-03-27 20:05:14 +13:00
2014-03-30 23:28:34 +13:00
cboPushbulletDevices . Enabled = true ;
2014-03-27 20:05:14 +13:00
2014-03-30 23:28:34 +13:00
Config . PushbulletSettings . DeviceList . ForEach ( pbDevice = >
{
2014-05-08 12:09:28 +12:00
cboPushbulletDevices . Items . Add ( pbDevice . Name ? ? "Invalid device name" ) ;
2014-03-30 23:28:34 +13:00
} ) ;
2014-03-27 20:05:14 +13:00
2014-03-31 00:37:38 +13:00
cboPushbulletDevices . SelectedIndex = 0 ;
2014-03-30 23:28:34 +13:00
}
2014-03-27 20:05:14 +13:00
}
2014-03-27 21:17:28 +13:00
2014-03-27 20:05:14 +13:00
#endregion Pushbullet
2013-11-03 23:53:49 +13:00
#region Twitter
public bool CheckTwitterAccounts ( )
{
return Config . TwitterOAuthInfoList . IsValidIndex ( Config . TwitterSelectedAccount ) ;
}
2014-05-30 23:38:23 +12:00
public void TwitterAuthOpen ( )
2013-11-03 23:53:49 +13:00
{
if ( CheckTwitterAccounts ( ) )
{
2014-05-30 23:38:23 +12:00
OAuthInfo acc = new OAuthInfo ( APIKeys . TwitterConsumerKey , APIKeys . TwitterConsumerSecret ) ;
Twitter twitter = new Twitter ( acc ) ;
string url = twitter . GetAuthorizationURL ( ) ;
2013-11-03 23:53:49 +13:00
2014-05-30 23:38:23 +12:00
if ( ! string . IsNullOrEmpty ( url ) )
{
acc . Description = Config . TwitterOAuthInfoList [ Config . TwitterSelectedAccount ] . Description ;
Config . TwitterOAuthInfoList [ Config . TwitterSelectedAccount ] = acc ;
ucTwitterAccounts . pgSettings . SelectedObject = acc ;
Helpers . OpenURL ( url ) ;
btnTwitterLogin . Enabled = true ;
}
}
2013-11-03 23:53:49 +13:00
}
2014-05-30 23:38:23 +12:00
public void TwitterAuthComplete ( )
2013-11-03 23:53:49 +13:00
{
2014-05-30 23:38:23 +12:00
if ( CheckTwitterAccounts ( ) )
2013-11-03 23:53:49 +13:00
{
2014-05-30 23:38:23 +12:00
OAuthInfo acc = Config . TwitterOAuthInfoList [ Config . TwitterSelectedAccount ] ;
2013-11-03 23:53:49 +13:00
2014-05-30 23:38:23 +12:00
if ( acc ! = null & & ! string . IsNullOrEmpty ( acc . AuthToken ) & & ! string . IsNullOrEmpty ( acc . AuthSecret ) & & ! string . IsNullOrEmpty ( acc . AuthVerifier ) )
2013-11-03 23:53:49 +13:00
{
2014-05-30 23:38:23 +12:00
Twitter twitter = new Twitter ( acc ) ;
bool result = twitter . GetAccessToken ( acc . AuthVerifier ) ;
if ( result )
{
acc . AuthVerifier = string . Empty ;
Config . TwitterOAuthInfoList [ Config . TwitterSelectedAccount ] = acc ;
ucTwitterAccounts . pgSettings . SelectedObject = acc ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
2013-11-03 23:53:49 +13:00
}
}
}
#endregion Twitter
#region URL Shorteners
#region goo . gl
public void GoogleURLShortenerAuthOpen ( )
{
try
{
OAuth2Info oauth = new OAuth2Info ( APIKeys . GoogleClientID , APIKeys . GoogleClientSecret ) ;
string url = new GoogleURLShortener ( oauth ) . GetAuthorizationURL ( ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
Config . GoogleURLShortenerOAuth2Info = oauth ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-11-03 23:53:49 +13:00
DebugHelper . WriteLine ( "GoogleURLShortenerAuthOpen - Authorization URL is opened: " + url ) ;
}
else
{
DebugHelper . WriteLine ( "GoogleURLShortenerAuthOpen - Authorization URL is empty." ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void GoogleURLShortenerAuthComplete ( string code )
{
try
{
if ( ! string . IsNullOrEmpty ( code ) & & Config . GoogleURLShortenerOAuth2Info ! = null )
{
bool result = new GoogleDrive ( Config . GoogleURLShortenerOAuth2Info ) . GetAccessToken ( code ) ;
if ( result )
{
oauth2GoogleURLShortener . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2GoogleURLShortener . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
oauth2GoogleURLShortener . LoginStatus = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void GoogleURLShortenerAuthRefresh ( )
{
try
{
if ( OAuth2Info . CheckOAuth ( Config . GoogleURLShortenerOAuth2Info ) )
{
bool result = new GoogleDrive ( Config . GoogleURLShortenerOAuth2Info ) . RefreshAccessToken ( ) ;
if ( result )
{
oauth2GoogleURLShortener . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2GoogleURLShortener . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
#endregion goo . gl
2013-12-21 04:59:26 +13:00
#region bit . ly
public void BitlyAuthOpen ( )
{
try
{
OAuth2Info oauth = new OAuth2Info ( APIKeys . BitlyClientID , APIKeys . BitlyClientSecret ) ;
string url = new BitlyURLShortener ( oauth ) . GetAuthorizationURL ( ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
Config . BitlyOAuth2Info = oauth ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-12-21 04:59:26 +13:00
DebugHelper . WriteLine ( "BitlyAuthOpen - Authorization URL is opened: " + url ) ;
}
else
{
DebugHelper . WriteLine ( "BitlyAuthOpen - Authorization URL is empty." ) ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void BitlyAuthComplete ( string code )
{
try
{
if ( ! string . IsNullOrEmpty ( code ) & & Config . BitlyOAuth2Info ! = null )
{
bool result = new BitlyURLShortener ( Config . BitlyOAuth2Info ) . GetAccessToken ( code ) ;
if ( result )
{
oauth2Bitly . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oauth2Bitly . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
oauth2Bitly . LoginStatus = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
#endregion bit . ly
2013-11-03 23:53:49 +13:00
#endregion URL Shorteners
#region Custom uploader
private void UpdateCustomUploader ( )
{
int index = lbCustomUploaderList . SelectedIndex ;
2013-12-03 01:08:09 +13:00
if ( index > = 0 )
2013-11-03 23:53:49 +13:00
{
CustomUploaderItem customUploader = GetCustomUploaderFromFields ( ) ;
2013-12-03 01:08:09 +13:00
if ( customUploader ! = null & & ! string . IsNullOrEmpty ( customUploader . Name ) )
2013-11-03 23:53:49 +13:00
{
Config . CustomUploadersList [ index ] = customUploader ;
lbCustomUploaderList . Items [ index ] = customUploader . Name ;
PrepareCustomUploaderList ( ) ;
}
}
}
2014-06-07 05:21:03 +12:00
private CustomUploaderItem GetSelectedCustomUploader ( )
2013-12-03 01:08:09 +13:00
{
2014-06-07 05:21:03 +12:00
if ( lbCustomUploaderList . SelectedIndex > = 0 )
2013-12-03 01:08:09 +13:00
{
CustomUploaderItem customUploader = GetCustomUploaderFromFields ( ) ;
if ( customUploader ! = null & & ! string . IsNullOrEmpty ( customUploader . Name ) )
{
2014-06-07 05:21:03 +12:00
return customUploader ;
2013-12-03 01:08:09 +13:00
}
}
2014-06-07 05:21:03 +12:00
return null ;
2013-12-03 01:08:09 +13:00
}
2014-06-07 05:21:03 +12:00
private void AddCustomUploader ( CustomUploaderItem customUploader )
2013-12-03 01:08:09 +13:00
{
2014-06-07 05:21:03 +12:00
if ( customUploader ! = null & & ! string . IsNullOrEmpty ( customUploader . Name ) )
2013-12-03 01:08:09 +13:00
{
2014-06-07 05:21:03 +12:00
Config . CustomUploadersList . Add ( customUploader ) ;
lbCustomUploaderList . Items . Add ( customUploader . Name ) ;
lbCustomUploaderList . SelectedIndex = lbCustomUploaderList . Items . Count - 1 ;
PrepareCustomUploaderList ( ) ;
2013-12-03 01:08:09 +13:00
}
}
2013-11-03 23:53:49 +13:00
private void CustomUploaderClear ( )
{
LoadCustomUploader ( new CustomUploaderItem ( ) ) ;
}
private void PrepareCustomUploaderList ( )
{
cbCustomUploaderImageUploader . Items . Clear ( ) ;
cbCustomUploaderTextUploader . Items . Clear ( ) ;
cbCustomUploaderFileUploader . Items . Clear ( ) ;
cbCustomUploaderURLShortener . Items . Clear ( ) ;
foreach ( CustomUploaderItem item in Config . CustomUploadersList )
{
cbCustomUploaderImageUploader . Items . Add ( item ) ;
cbCustomUploaderTextUploader . Items . Add ( item ) ;
cbCustomUploaderFileUploader . Items . Add ( item ) ;
cbCustomUploaderURLShortener . Items . Add ( item ) ;
}
if ( Config . CustomUploadersList . IsValidIndex ( Config . CustomImageUploaderSelected ) )
{
cbCustomUploaderImageUploader . SelectedIndex = Config . CustomImageUploaderSelected ;
}
if ( Config . CustomUploadersList . IsValidIndex ( Config . CustomTextUploaderSelected ) )
{
cbCustomUploaderTextUploader . SelectedIndex = Config . CustomTextUploaderSelected ;
}
if ( Config . CustomUploadersList . IsValidIndex ( Config . CustomFileUploaderSelected ) )
{
cbCustomUploaderFileUploader . SelectedIndex = Config . CustomFileUploaderSelected ;
}
if ( Config . CustomUploadersList . IsValidIndex ( Config . CustomURLShortenerSelected ) )
{
cbCustomUploaderURLShortener . SelectedIndex = Config . CustomURLShortenerSelected ;
}
}
private void LoadCustomUploader ( CustomUploaderItem customUploader )
{
txtCustomUploaderName . Text = customUploader . Name ;
cbCustomUploaderRequestType . SelectedIndex = ( int ) customUploader . RequestType ;
txtCustomUploaderRequestURL . Text = customUploader . RequestURL ;
txtCustomUploaderFileForm . Text = customUploader . FileFormName ;
txtCustomUploaderFileForm . Enabled = customUploader . RequestType = = CustomUploaderRequestType . POST ;
txtCustomUploaderArgName . Text = string . Empty ;
txtCustomUploaderArgValue . Text = string . Empty ;
lvCustomUploaderArguments . Items . Clear ( ) ;
foreach ( KeyValuePair < string , string > arg in customUploader . Arguments )
{
lvCustomUploaderArguments . Items . Add ( arg . Key ) . SubItems . Add ( arg . Value ) ;
}
cbCustomUploaderResponseType . SelectedIndex = ( int ) customUploader . ResponseType ;
txtCustomUploaderRegexp . Text = string . Empty ;
lvCustomUploaderRegexps . Items . Clear ( ) ;
foreach ( string regexp in customUploader . RegexList )
{
lvCustomUploaderRegexps . Items . Add ( regexp ) ;
}
txtCustomUploaderURL . Text = customUploader . URL ;
txtCustomUploaderThumbnailURL . Text = customUploader . ThumbnailURL ;
txtCustomUploaderDeletionURL . Text = customUploader . DeletionURL ;
}
private CustomUploaderItem GetCustomUploaderFromFields ( )
{
CustomUploaderItem item = new CustomUploaderItem ( txtCustomUploaderName . Text ) ;
foreach ( ListViewItem lvItem in lvCustomUploaderArguments . Items )
{
item . Arguments . Add ( lvItem . Text , lvItem . SubItems [ 1 ] . Text ) ;
}
item . RequestType = ( CustomUploaderRequestType ) cbCustomUploaderRequestType . SelectedIndex ;
item . RequestURL = txtCustomUploaderRequestURL . Text ;
item . FileFormName = txtCustomUploaderFileForm . Text ;
item . ResponseType = ( ResponseType ) cbCustomUploaderResponseType . SelectedIndex ;
foreach ( ListViewItem lvItem in lvCustomUploaderRegexps . Items )
{
item . RegexList . Add ( lvItem . Text ) ;
}
item . URL = txtCustomUploaderURL . Text ;
item . ThumbnailURL = txtCustomUploaderThumbnailURL . Text ;
item . DeletionURL = txtCustomUploaderDeletionURL . Text ;
return item ;
}
2014-05-22 13:22:32 +12:00
private void TestCustomUploader ( CustomUploaderType type , CustomUploaderItem item )
2013-11-03 23:53:49 +13:00
{
UploadResult result = null ;
2014-01-20 20:14:36 +13:00
txtCustomUploaderLog . ResetText ( ) ;
2014-05-24 03:39:14 +12:00
TaskEx . Run ( ( ) = >
2013-11-03 23:53:49 +13:00
{
try
{
switch ( type )
{
case CustomUploaderType . Image :
2013-11-06 06:58:22 +13:00
using ( Stream stream = ShareXResources . Logo . GetStream ( ) )
2013-11-03 23:53:49 +13:00
{
CustomImageUploader imageUploader = new CustomImageUploader ( item ) ;
result = imageUploader . Upload ( stream , "Test.png" ) ;
result . Errors = imageUploader . Errors ;
}
break ;
case CustomUploaderType . Text :
CustomTextUploader textUploader = new CustomTextUploader ( item ) ;
result = textUploader . UploadText ( Application . ProductName + " text upload test" , "Test.txt" ) ;
result . Errors = textUploader . Errors ;
break ;
case CustomUploaderType . File :
2013-11-06 06:58:22 +13:00
using ( Stream stream = ShareXResources . Logo . GetStream ( ) )
2013-11-03 23:53:49 +13:00
{
CustomFileUploader fileUploader = new CustomFileUploader ( item ) ;
result = fileUploader . Upload ( stream , "Test.png" ) ;
result . Errors = fileUploader . Errors ;
}
break ;
case CustomUploaderType . URL :
CustomURLShortener urlShortener = new CustomURLShortener ( item ) ;
result = urlShortener . ShortenURL ( Links . URL_WEBSITE ) ;
result . Errors = urlShortener . Errors ;
break ;
}
}
catch ( Exception e )
{
result = new UploadResult ( ) ;
result . Errors . Add ( e . Message ) ;
}
2014-05-22 13:22:32 +12:00
} ,
( ) = >
2013-11-03 23:53:49 +13:00
{
2014-05-22 13:22:32 +12:00
if ( result ! = null )
2013-11-03 23:53:49 +13:00
{
2014-05-22 13:22:32 +12:00
if ( ( type ! = CustomUploaderType . URL & & ! string . IsNullOrEmpty ( result . URL ) ) | | ( type = = CustomUploaderType . URL & & ! string . IsNullOrEmpty ( result . ShortenedURL ) ) )
{
txtCustomUploaderLog . AppendText ( "URL: " + result + Environment . NewLine ) ;
}
else if ( result . IsError )
{
txtCustomUploaderLog . AppendText ( "Error: " + result . ErrorsToString ( ) + Environment . NewLine ) ;
}
else
{
txtCustomUploaderLog . AppendText ( "Error: Result is empty." + Environment . NewLine ) ;
}
2013-11-03 23:53:49 +13:00
2014-05-22 13:22:32 +12:00
txtCustomUploaderLog . ScrollToCaret ( ) ;
2013-11-03 23:53:49 +13:00
2014-05-22 13:22:32 +12:00
btnCustomUploaderShowLastResponse . Tag = result . Response ;
btnCustomUploaderShowLastResponse . Enabled = ! string . IsNullOrEmpty ( result . Response ) ;
}
2013-11-03 23:53:49 +13:00
2014-05-22 13:22:32 +12:00
btnCustomUploaderImageUploaderTest . Enabled = btnCustomUploaderTextUploaderTest . Enabled =
btnCustomUploaderFileUploaderTest . Enabled = btnCustomUploaderURLShortenerTest . Enabled = true ;
} ) ;
2013-11-03 23:53:49 +13:00
}
#endregion Custom uploader
#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 ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-11-03 23:53:49 +13:00
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void JiraAuthComplete ( string code )
{
try
{
2014-05-30 23:38:23 +12:00
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 = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oAuthJira . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
oAuthJira . LoginStatus = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
#endregion Jira
2013-12-03 12:11:30 +13:00
#region Gist
2013-12-04 10:49:15 +13:00
2013-12-03 12:11:30 +13:00
public void GistAuthOpen ( )
{
try
{
2014-05-02 01:52:46 +12:00
OAuth2Info oauth = new OAuth2Info ( APIKeys . GitHubID , APIKeys . GitHubSecret ) ;
2013-12-03 12:11:30 +13:00
string url = new Gist ( oauth ) . GetAuthorizationURL ( ) ;
if ( ! string . IsNullOrEmpty ( url ) )
{
Config . GistOAuth2Info = oauth ;
2014-05-03 05:56:51 +12:00
Helpers . OpenURL ( url ) ;
2013-12-03 12:11:30 +13:00
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
public void GistAuthComplete ( string code )
{
try
{
if ( ! string . IsNullOrEmpty ( code ) & & Config . GistOAuth2Info ! = null )
{
bool result = new Gist ( Config . GistOAuth2Info ) . GetAccessToken ( code ) ;
if ( result )
{
oAuth2Gist . Status = "Login successful." ;
MessageBox . Show ( "Login successful." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
}
else
{
oAuth2Gist . Status = "Login failed." ;
MessageBox . Show ( "Login failed." , Application . ProductName , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
atcGistAccountType . SelectedAccountType = AccountType . Anonymous ;
}
oAuth2Gist . LoginStatus = result ;
}
}
catch ( Exception ex )
{
MessageBox . Show ( ex . ToString ( ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
#endregion Gist
2013-11-03 23:53:49 +13:00
}
}