From 57644cf9fe2689b4071bfa5cd6a56d186e00155e Mon Sep 17 00:00:00 2001 From: Jaex Date: Tue, 1 Jul 2014 13:18:07 +0300 Subject: [PATCH] Google Drive folder listing --- UploadersLib/FileUploaders/GoogleDrive.cs | 30 ++++- .../Forms/UploadersConfigForm.Designer.cs | 122 +++++++++++++----- UploadersLib/Forms/UploadersConfigForm.cs | 25 ++++ .../Forms/UploadersConfigFormHelper.cs | 31 +++++ 4 files changed, 176 insertions(+), 32 deletions(-) diff --git a/UploadersLib/FileUploaders/GoogleDrive.cs b/UploadersLib/FileUploaders/GoogleDrive.cs index 24e213e9c..a987b8053 100644 --- a/UploadersLib/FileUploaders/GoogleDrive.cs +++ b/UploadersLib/FileUploaders/GoogleDrive.cs @@ -179,6 +179,26 @@ public void SetPermissions(string fileID, GoogleDrivePermissionRole role, Google string response = SendRequestJSON(url, json, headers: GetAuthHeaders()); } + public List GetFolders() + { + Dictionary args = new Dictionary(); + args.Add("q", "mimeType = 'application/vnd.google-apps.folder' and trashed = false and 'me' in writers"); + + string response = SendRequest(HttpMethod.GET, "https://www.googleapis.com/drive/v2/files", args, headers: GetAuthHeaders()); + + if (!string.IsNullOrEmpty(response)) + { + GoogleDriveFileList fileList = JsonConvert.DeserializeObject(response); + + if (fileList != null) + { + return fileList.items; + } + } + + return null; + } + public override UploadResult Upload(Stream stream, string fileName) { if (!CheckAuthorization()) @@ -190,7 +210,7 @@ public override UploadResult Upload(Stream stream, string fileName) if (!string.IsNullOrEmpty(result.Response)) { - GoogleDriveUpload upload = JsonConvert.DeserializeObject(result.Response); + GoogleDriveFile upload = JsonConvert.DeserializeObject(result.Response); if (upload != null) { @@ -210,10 +230,16 @@ public override UploadResult Upload(Stream stream, string fileName) return result; } - public class GoogleDriveUpload + public class GoogleDriveFile { public string id { get; set; } public string alternateLink { get; set; } + public string title { get; set; } + } + + public class GoogleDriveFileList + { + public List items { get; set; } } public enum GoogleDrivePermissionRole diff --git a/UploadersLib/Forms/UploadersConfigForm.Designer.cs b/UploadersLib/Forms/UploadersConfigForm.Designer.cs index 63cf0b9fa..2cd77332f 100644 --- a/UploadersLib/Forms/UploadersConfigForm.Designer.cs +++ b/UploadersLib/Forms/UploadersConfigForm.Designer.cs @@ -166,14 +166,19 @@ private void InitializeComponent() this.lblAmazonS3SecretKey = new System.Windows.Forms.Label(); this.lblAmazonS3AccessKey = new System.Windows.Forms.Label(); this.txtAmazonS3AccessKey = new System.Windows.Forms.TextBox(); + this.tpGoogleDrive = new System.Windows.Forms.TabPage(); + this.txtGoogleDriveFolderID = new System.Windows.Forms.TextBox(); + this.lblGoogleDriveFolderID = new System.Windows.Forms.Label(); + this.lvGoogleDriveFoldersList = new HelpersLib.MyListView(); + this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.btnGoogleDriveRefreshFolders = new System.Windows.Forms.Button(); + this.cbGoogleDriveIsPublic = new System.Windows.Forms.CheckBox(); this.tpPushbullet = new System.Windows.Forms.TabPage(); this.lblPushbulletDevices = new System.Windows.Forms.Label(); this.cboPushbulletDevices = new System.Windows.Forms.ComboBox(); this.btnPushbulletGetDeviceList = new System.Windows.Forms.Button(); this.lblPushbulletUserKey = new System.Windows.Forms.Label(); this.txtPushbulletUserKey = new System.Windows.Forms.TextBox(); - this.tpGoogleDrive = new System.Windows.Forms.TabPage(); - this.cbGoogleDriveIsPublic = new System.Windows.Forms.CheckBox(); this.tpBox = new System.Windows.Forms.TabPage(); this.lblBoxFolderTip = new System.Windows.Forms.Label(); this.cbBoxShare = new System.Windows.Forms.CheckBox(); @@ -374,8 +379,8 @@ private void InitializeComponent() this.tpFTP.SuspendLayout(); this.tpMega.SuspendLayout(); this.tpAmazonS3.SuspendLayout(); - this.tpPushbullet.SuspendLayout(); this.tpGoogleDrive.SuspendLayout(); + this.tpPushbullet.SuspendLayout(); this.tpBox.SuspendLayout(); this.tpRapidShare.SuspendLayout(); this.tpSendSpace.SuspendLayout(); @@ -1234,8 +1239,8 @@ private void InitializeComponent() this.tcFileUploaders.Controls.Add(this.tpFTP); this.tcFileUploaders.Controls.Add(this.tpMega); this.tcFileUploaders.Controls.Add(this.tpAmazonS3); - this.tcFileUploaders.Controls.Add(this.tpPushbullet); this.tcFileUploaders.Controls.Add(this.tpGoogleDrive); + this.tcFileUploaders.Controls.Add(this.tpPushbullet); this.tcFileUploaders.Controls.Add(this.tpBox); this.tcFileUploaders.Controls.Add(this.tpRapidShare); this.tcFileUploaders.Controls.Add(this.tpSendSpace); @@ -1837,6 +1842,81 @@ private void InitializeComponent() this.txtAmazonS3AccessKey.TabIndex = 1; this.txtAmazonS3AccessKey.TextChanged += new System.EventHandler(this.txtAmazonS3AccessKey_TextChanged); // + // tpGoogleDrive + // + this.tpGoogleDrive.Controls.Add(this.txtGoogleDriveFolderID); + this.tpGoogleDrive.Controls.Add(this.lblGoogleDriveFolderID); + this.tpGoogleDrive.Controls.Add(this.lvGoogleDriveFoldersList); + this.tpGoogleDrive.Controls.Add(this.btnGoogleDriveRefreshFolders); + this.tpGoogleDrive.Controls.Add(this.cbGoogleDriveIsPublic); + this.tpGoogleDrive.Controls.Add(this.oauth2GoogleDrive); + this.tpGoogleDrive.Location = new System.Drawing.Point(4, 22); + this.tpGoogleDrive.Name = "tpGoogleDrive"; + this.tpGoogleDrive.Padding = new System.Windows.Forms.Padding(3); + this.tpGoogleDrive.Size = new System.Drawing.Size(972, 493); + this.tpGoogleDrive.TabIndex = 1; + this.tpGoogleDrive.Text = "Google Drive"; + this.tpGoogleDrive.UseVisualStyleBackColor = true; + // + // txtGoogleDriveFolderID + // + this.txtGoogleDriveFolderID.Location = new System.Drawing.Point(592, 13); + this.txtGoogleDriveFolderID.Name = "txtGoogleDriveFolderID"; + this.txtGoogleDriveFolderID.Size = new System.Drawing.Size(192, 20); + this.txtGoogleDriveFolderID.TabIndex = 9; + this.txtGoogleDriveFolderID.TextChanged += new System.EventHandler(this.txtGoogleDriveFolderID_TextChanged); + // + // lblGoogleDriveFolderID + // + this.lblGoogleDriveFolderID.AutoSize = true; + this.lblGoogleDriveFolderID.Location = new System.Drawing.Point(352, 16); + this.lblGoogleDriveFolderID.Name = "lblGoogleDriveFolderID"; + this.lblGoogleDriveFolderID.Size = new System.Drawing.Size(231, 13); + this.lblGoogleDriveFolderID.TabIndex = 8; + this.lblGoogleDriveFolderID.Text = "Folder ID for upload (Empty = No folder upload):"; + // + // lvGoogleDriveFoldersList + // + this.lvGoogleDriveFoldersList.AutoFillColumn = true; + this.lvGoogleDriveFoldersList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { + this.columnHeader1}); + this.lvGoogleDriveFoldersList.FullRowSelect = true; + this.lvGoogleDriveFoldersList.Location = new System.Drawing.Point(352, 72); + this.lvGoogleDriveFoldersList.MultiSelect = false; + this.lvGoogleDriveFoldersList.Name = "lvGoogleDriveFoldersList"; + this.lvGoogleDriveFoldersList.Size = new System.Drawing.Size(432, 408); + this.lvGoogleDriveFoldersList.TabIndex = 11; + this.lvGoogleDriveFoldersList.UseCompatibleStateImageBehavior = false; + this.lvGoogleDriveFoldersList.View = System.Windows.Forms.View.Details; + this.lvGoogleDriveFoldersList.SelectedIndexChanged += new System.EventHandler(this.lvGoogleDriveFoldersList_SelectedIndexChanged); + // + // columnHeader1 + // + this.columnHeader1.Text = "Folder name"; + this.columnHeader1.Width = 400; + // + // btnGoogleDriveRefreshFolders + // + this.btnGoogleDriveRefreshFolders.Enabled = false; + this.btnGoogleDriveRefreshFolders.Location = new System.Drawing.Point(352, 40); + this.btnGoogleDriveRefreshFolders.Name = "btnGoogleDriveRefreshFolders"; + this.btnGoogleDriveRefreshFolders.Size = new System.Drawing.Size(200, 23); + this.btnGoogleDriveRefreshFolders.TabIndex = 10; + this.btnGoogleDriveRefreshFolders.Text = "Refresh folders list"; + this.btnGoogleDriveRefreshFolders.UseVisualStyleBackColor = true; + this.btnGoogleDriveRefreshFolders.Click += new System.EventHandler(this.btnGoogleDriveRefreshFolders_Click); + // + // cbGoogleDriveIsPublic + // + this.cbGoogleDriveIsPublic.AutoSize = true; + this.cbGoogleDriveIsPublic.Location = new System.Drawing.Point(16, 264); + this.cbGoogleDriveIsPublic.Name = "cbGoogleDriveIsPublic"; + this.cbGoogleDriveIsPublic.Size = new System.Drawing.Size(106, 17); + this.cbGoogleDriveIsPublic.TabIndex = 1; + this.cbGoogleDriveIsPublic.Text = "Is public upload?"; + this.cbGoogleDriveIsPublic.UseVisualStyleBackColor = true; + this.cbGoogleDriveIsPublic.CheckedChanged += new System.EventHandler(this.cbGoogleDriveIsPublic_CheckedChanged); + // // tpPushbullet // this.tpPushbullet.Controls.Add(this.lblPushbulletDevices); @@ -1901,29 +1981,6 @@ private void InitializeComponent() this.txtPushbulletUserKey.UseSystemPasswordChar = true; this.txtPushbulletUserKey.TextChanged += new System.EventHandler(this.txtPushbulletUserKey_TextChanged); // - // tpGoogleDrive - // - this.tpGoogleDrive.Controls.Add(this.cbGoogleDriveIsPublic); - this.tpGoogleDrive.Controls.Add(this.oauth2GoogleDrive); - this.tpGoogleDrive.Location = new System.Drawing.Point(4, 22); - this.tpGoogleDrive.Name = "tpGoogleDrive"; - this.tpGoogleDrive.Padding = new System.Windows.Forms.Padding(3); - this.tpGoogleDrive.Size = new System.Drawing.Size(972, 493); - this.tpGoogleDrive.TabIndex = 1; - this.tpGoogleDrive.Text = "Google Drive"; - this.tpGoogleDrive.UseVisualStyleBackColor = true; - // - // cbGoogleDriveIsPublic - // - this.cbGoogleDriveIsPublic.AutoSize = true; - this.cbGoogleDriveIsPublic.Location = new System.Drawing.Point(16, 264); - this.cbGoogleDriveIsPublic.Name = "cbGoogleDriveIsPublic"; - this.cbGoogleDriveIsPublic.Size = new System.Drawing.Size(106, 17); - this.cbGoogleDriveIsPublic.TabIndex = 1; - this.cbGoogleDriveIsPublic.Text = "Is public upload?"; - this.cbGoogleDriveIsPublic.UseVisualStyleBackColor = true; - this.cbGoogleDriveIsPublic.CheckedChanged += new System.EventHandler(this.cbGoogleDriveIsPublic_CheckedChanged); - // // tpBox // this.tpBox.Controls.Add(this.lblBoxFolderTip); @@ -2967,7 +3024,7 @@ private void InitializeComponent() // // txtImgurAlbumID // - this.txtImgurAlbumID.Location = new System.Drawing.Point(592, 12); + this.txtImgurAlbumID.Location = new System.Drawing.Point(592, 13); this.txtImgurAlbumID.Name = "txtImgurAlbumID"; this.txtImgurAlbumID.Size = new System.Drawing.Size(192, 20); this.txtImgurAlbumID.TabIndex = 5; @@ -3827,10 +3884,10 @@ private void InitializeComponent() this.tpMega.PerformLayout(); this.tpAmazonS3.ResumeLayout(false); this.tpAmazonS3.PerformLayout(); - this.tpPushbullet.ResumeLayout(false); - this.tpPushbullet.PerformLayout(); this.tpGoogleDrive.ResumeLayout(false); this.tpGoogleDrive.PerformLayout(); + this.tpPushbullet.ResumeLayout(false); + this.tpPushbullet.PerformLayout(); this.tpBox.ResumeLayout(false); this.tpBox.PerformLayout(); this.tpRapidShare.ResumeLayout(false); @@ -4215,5 +4272,10 @@ private void InitializeComponent() private OAuthControl oauth2Dropbox; private System.Windows.Forms.TextBox txtBitlyDomain; private System.Windows.Forms.Label lblBitlyDomain; + private System.Windows.Forms.TextBox txtGoogleDriveFolderID; + private System.Windows.Forms.Label lblGoogleDriveFolderID; + private HelpersLib.MyListView lvGoogleDriveFoldersList; + private System.Windows.Forms.ColumnHeader columnHeader1; + private System.Windows.Forms.Button btnGoogleDriveRefreshFolders; } } \ No newline at end of file diff --git a/UploadersLib/Forms/UploadersConfigForm.cs b/UploadersLib/Forms/UploadersConfigForm.cs index b1de2ca55..8ad021d62 100644 --- a/UploadersLib/Forms/UploadersConfigForm.cs +++ b/UploadersLib/Forms/UploadersConfigForm.cs @@ -296,9 +296,11 @@ public void LoadSettings(UploadersConfig uploadersConfig) if (OAuth2Info.CheckOAuth(Config.GoogleDriveOAuth2Info)) { oauth2GoogleDrive.Status = OAuthLoginStatus.LoginSuccessful; + btnGoogleDriveRefreshFolders.Enabled = true; } cbGoogleDriveIsPublic.Checked = Config.GoogleDriveIsPublic; + txtGoogleDriveFolderID.Text = Config.GoogleDriveFolderID; // Minus @@ -1001,6 +1003,29 @@ private void cbGoogleDriveIsPublic_CheckedChanged(object sender, EventArgs e) Config.GoogleDriveIsPublic = cbGoogleDriveIsPublic.Checked; } + private void txtGoogleDriveFolderID_TextChanged(object sender, EventArgs e) + { + Config.GoogleDriveFolderID = txtGoogleDriveFolderID.Text; + } + + private void btnGoogleDriveRefreshFolders_Click(object sender, EventArgs e) + { + GoogleDriveRefreshFolders(); + } + + private void lvGoogleDriveFoldersList_SelectedIndexChanged(object sender, EventArgs e) + { + if (lvGoogleDriveFoldersList.SelectedItems.Count > 0) + { + ListViewItem lvi = lvGoogleDriveFoldersList.SelectedItems[0]; + var folder = lvi.Tag as UploadersLib.FileUploaders.GoogleDrive.GoogleDriveFile; + if (folder != null) + { + txtGoogleDriveFolderID.Text = folder.id; + } + } + } + #endregion Google Drive #region Box diff --git a/UploadersLib/Forms/UploadersConfigFormHelper.cs b/UploadersLib/Forms/UploadersConfigFormHelper.cs index dfc56d1c5..72249ff1f 100644 --- a/UploadersLib/Forms/UploadersConfigFormHelper.cs +++ b/UploadersLib/Forms/UploadersConfigFormHelper.cs @@ -686,6 +686,8 @@ public void GoogleDriveAuthComplete(string code) oauth2GoogleDrive.Status = OAuthLoginStatus.LoginFailed; MessageBox.Show("Login failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } + + btnGoogleDriveRefreshFolders.Enabled = result; } } catch (Exception ex) @@ -712,6 +714,35 @@ public void GoogleDriveAuthRefresh() oauth2GoogleDrive.Status = OAuthLoginStatus.LoginFailed; MessageBox.Show("Login failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } + + btnGoogleDriveRefreshFolders.Enabled = result; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void GoogleDriveRefreshFolders() + { + try + { + lvGoogleDriveFoldersList.Items.Clear(); + + if (OAuth2Info.CheckOAuth(Config.GoogleDriveOAuth2Info)) + { + var folders = new GoogleDrive(Config.GoogleDriveOAuth2Info).GetFolders(); + + if (folders != null) + { + foreach (var folder in folders) + { + ListViewItem lvi = new ListViewItem(folder.title); + lvi.Tag = folder; + lvGoogleDriveFoldersList.Items.Add(lvi); + } + } } } catch (Exception ex)