From 154b3202cc55d2d32831c4c65cea01794b4518de Mon Sep 17 00:00:00 2001 From: Yukai Li Date: Fri, 21 Apr 2017 14:14:48 -0600 Subject: [PATCH] Implement rudimentary upload support --- SteamCloudFileManager/MainForm.Designer.cs | 58 ++++++++++++------ SteamCloudFileManager/MainForm.cs | 71 ++++++++++++++++++++++ SteamCloudFileManager/MainForm.resx | 3 + 3 files changed, 112 insertions(+), 20 deletions(-) diff --git a/SteamCloudFileManager/MainForm.Designer.cs b/SteamCloudFileManager/MainForm.Designer.cs index 6d093d7..c7054dc 100644 --- a/SteamCloudFileManager/MainForm.Designer.cs +++ b/SteamCloudFileManager/MainForm.Designer.cs @@ -45,6 +45,7 @@ private void InitializeComponent() this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.uploadButton = new System.Windows.Forms.Button(); + this.uploadBackgroundWorker = new System.ComponentModel.BackgroundWorker(); this.SuspendLayout(); // // remoteListView @@ -60,9 +61,10 @@ private void InitializeComponent() this.columnHeader5}); this.remoteListView.FullRowSelect = true; this.remoteListView.HideSelection = false; - this.remoteListView.Location = new System.Drawing.Point(15, 41); + this.remoteListView.Location = new System.Drawing.Point(20, 50); + this.remoteListView.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.remoteListView.Name = "remoteListView"; - this.remoteListView.Size = new System.Drawing.Size(591, 185); + this.remoteListView.Size = new System.Drawing.Size(787, 227); this.remoteListView.TabIndex = 0; this.remoteListView.UseCompatibleStateImageBehavior = false; this.remoteListView.View = System.Windows.Forms.View.Details; @@ -93,24 +95,27 @@ private void InitializeComponent() // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(12, 17); + this.label1.Location = new System.Drawing.Point(16, 21); + this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(40, 13); + this.label1.Size = new System.Drawing.Size(50, 17); this.label1.TabIndex = 1; this.label1.Text = "AppID:"; // // appIdTextBox // - this.appIdTextBox.Location = new System.Drawing.Point(58, 14); + this.appIdTextBox.Location = new System.Drawing.Point(77, 17); + this.appIdTextBox.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.appIdTextBox.Name = "appIdTextBox"; - this.appIdTextBox.Size = new System.Drawing.Size(100, 20); + this.appIdTextBox.Size = new System.Drawing.Size(132, 22); this.appIdTextBox.TabIndex = 2; // // connectButton // - this.connectButton.Location = new System.Drawing.Point(164, 12); + this.connectButton.Location = new System.Drawing.Point(219, 15); + this.connectButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.connectButton.Name = "connectButton"; - this.connectButton.Size = new System.Drawing.Size(75, 23); + this.connectButton.Size = new System.Drawing.Size(100, 28); this.connectButton.TabIndex = 3; this.connectButton.Text = "Connect"; this.connectButton.UseVisualStyleBackColor = true; @@ -119,9 +124,10 @@ private void InitializeComponent() // refreshButton // this.refreshButton.Enabled = false; - this.refreshButton.Location = new System.Drawing.Point(245, 12); + this.refreshButton.Location = new System.Drawing.Point(327, 15); + this.refreshButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.refreshButton.Name = "refreshButton"; - this.refreshButton.Size = new System.Drawing.Size(75, 23); + this.refreshButton.Size = new System.Drawing.Size(100, 28); this.refreshButton.TabIndex = 4; this.refreshButton.Text = "Refresh"; this.refreshButton.UseVisualStyleBackColor = true; @@ -131,9 +137,10 @@ private void InitializeComponent() // this.quotaLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.quotaLabel.Location = new System.Drawing.Point(326, 9); + this.quotaLabel.Location = new System.Drawing.Point(435, 11); + this.quotaLabel.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.quotaLabel.Name = "quotaLabel"; - this.quotaLabel.Size = new System.Drawing.Size(280, 23); + this.quotaLabel.Size = new System.Drawing.Size(373, 28); this.quotaLabel.TabIndex = 5; this.quotaLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // @@ -141,9 +148,10 @@ private void InitializeComponent() // this.deleteButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.deleteButton.Enabled = false; - this.deleteButton.Location = new System.Drawing.Point(93, 232); + this.deleteButton.Location = new System.Drawing.Point(124, 286); + this.deleteButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.deleteButton.Name = "deleteButton"; - this.deleteButton.Size = new System.Drawing.Size(75, 23); + this.deleteButton.Size = new System.Drawing.Size(100, 28); this.deleteButton.TabIndex = 6; this.deleteButton.Text = "Delete"; this.deleteButton.UseVisualStyleBackColor = true; @@ -153,9 +161,10 @@ private void InitializeComponent() // this.downloadButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.downloadButton.Enabled = false; - this.downloadButton.Location = new System.Drawing.Point(12, 232); + this.downloadButton.Location = new System.Drawing.Point(16, 286); + this.downloadButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.downloadButton.Name = "downloadButton"; - this.downloadButton.Size = new System.Drawing.Size(75, 23); + this.downloadButton.Size = new System.Drawing.Size(100, 28); this.downloadButton.TabIndex = 7; this.downloadButton.Text = "Download"; this.downloadButton.UseVisualStyleBackColor = true; @@ -173,18 +182,25 @@ private void InitializeComponent() // // uploadButton // - this.uploadButton.Location = new System.Drawing.Point(174, 232); + this.uploadButton.Location = new System.Drawing.Point(232, 286); + this.uploadButton.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.uploadButton.Name = "uploadButton"; - this.uploadButton.Size = new System.Drawing.Size(75, 23); + this.uploadButton.Size = new System.Drawing.Size(100, 28); this.uploadButton.TabIndex = 8; this.uploadButton.Text = "Upload"; this.uploadButton.UseVisualStyleBackColor = true; + this.uploadButton.Click += new System.EventHandler(this.uploadButton_Click); + // + // uploadBackgroundWorker + // + this.uploadBackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.uploadBackgroundWorker_DoWork); + this.uploadBackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.uploadBackgroundWorker_RunWorkerCompleted); // // MainForm // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(618, 267); + this.ClientSize = new System.Drawing.Size(824, 329); this.Controls.Add(this.uploadButton); this.Controls.Add(this.downloadButton); this.Controls.Add(this.deleteButton); @@ -195,6 +211,7 @@ private void InitializeComponent() this.Controls.Add(this.appIdTextBox); this.Controls.Add(this.label1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.Name = "MainForm"; this.Text = "Steam Cloud File Manager"; this.ResumeLayout(false); @@ -220,6 +237,7 @@ private void InitializeComponent() private System.Windows.Forms.SaveFileDialog saveFileDialog1; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.Button uploadButton; + private System.ComponentModel.BackgroundWorker uploadBackgroundWorker; } } diff --git a/SteamCloudFileManager/MainForm.cs b/SteamCloudFileManager/MainForm.cs index fe216aa..f3924a9 100644 --- a/SteamCloudFileManager/MainForm.cs +++ b/SteamCloudFileManager/MainForm.cs @@ -13,6 +13,8 @@ namespace SteamCloudFileManager public partial class MainForm : Form { IRemoteStorage storage; + // Item1 = cloud name, Item2 = path on disk + Queue> uploadQueue = new Queue>(); public MainForm() { @@ -154,5 +156,74 @@ private void remoteListView_SelectedIndexChanged(object sender, EventArgs e) { downloadButton.Enabled = deleteButton.Enabled = (storage != null && remoteListView.SelectedIndices.Count > 0); } + + private void uploadBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) + { + BackgroundWorker worker = (BackgroundWorker)sender; + List failedFiles = new List(); + while (uploadQueue.Count > 0) + { + var uploadItem = uploadQueue.Dequeue(); + IRemoteFile file = storage.GetFile(uploadItem.Item1); + try + { + byte[] data = File.ReadAllBytes(uploadItem.Item2); + if (!file.WriteAllBytes(data)) + failedFiles.Add(uploadItem.Item1); + } + catch (IOException ex) + { + failedFiles.Add(uploadItem.Item1); + } + } + + e.Result = failedFiles; + } + + private void uploadButton_Click(object sender, EventArgs e) + { + if (openFileDialog1.ShowDialog(this) == DialogResult.OK) + { + disableUploadGui(); + foreach (var selectedFile in openFileDialog1.FileNames) + { + uploadQueue.Enqueue(new Tuple(Path.GetFileName(selectedFile).ToLowerInvariant(), selectedFile)); + } + uploadBackgroundWorker.RunWorkerAsync(); + } + } + + void disableUploadGui() + { + // Disables app switching, refresh, and upload button + connectButton.Enabled = false; + refreshButton.Enabled = false; + uploadButton.Enabled = false; + uploadButton.Text = "Uploading..."; + } + + void enableUploadGui() + { + connectButton.Enabled = true; + refreshButton.Enabled = true; + uploadButton.Enabled = true; + uploadButton.Text = "Upload"; + } + + private void uploadBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + { + var failedList = e.Result as List; + if (failedList.Count == 0) + { + MessageBox.Show(this, "Upload complete.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + failedList.Insert(0, "The following files have failed to upload:"); + MessageBox.Show(this, string.Join(Environment.NewLine, failedList), Text, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + enableUploadGui(); + refreshButton_Click(this, EventArgs.Empty); + } } } diff --git a/SteamCloudFileManager/MainForm.resx b/SteamCloudFileManager/MainForm.resx index 0c9e6d8..311e6b2 100644 --- a/SteamCloudFileManager/MainForm.resx +++ b/SteamCloudFileManager/MainForm.resx @@ -123,6 +123,9 @@ 153, 17 + + 320, 17 + 63