forked from GMMan/SteamCloudFileManagerLite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
230 lines (210 loc) · 8.88 KB
/
MainForm.cs
File metadata and controls
230 lines (210 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SteamCloudFileManager
{
public partial class MainForm : Form
{
IRemoteStorage storage;
// Item1 = cloud name, Item2 = path on disk
Queue<Tuple<string, string>> uploadQueue = new Queue<Tuple<string, string>>();
public MainForm()
{
InitializeComponent();
}
private void connectButton_Click(object sender, EventArgs e)
{
try
{
uint appId;
if (string.IsNullOrWhiteSpace(appIdTextBox.Text))
{
MessageBox.Show(this, "Please enter an App ID.", "Failed to connect", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!uint.TryParse(appIdTextBox.Text.Trim(), out appId))
{
MessageBox.Show(this, "Please make sure the App ID you entered is valid.", "Failed to connect", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
storage = RemoteStorage.CreateInstance(uint.Parse(appIdTextBox.Text));
//storage = new RemoteStorageLocal("remote", uint.Parse(appIdTextBox.Text));
refreshButton.Enabled = true;
uploadButton.Enabled = true;
refreshButton_Click(this, EventArgs.Empty);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString(), "Failed to connect", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void refreshButton_Click(object sender, EventArgs e)
{
if (storage == null)
{
MessageBox.Show(this, "Not connected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
try
{
List<IRemoteFile> files = storage.GetFiles();
remoteListView.Items.Clear();
foreach (IRemoteFile file in files)
{
ListViewItem itm = new ListViewItem(new string[] { file.Name, file.Timestamp.ToString(), file.Size.ToString(), file.IsPersisted.ToString(), file.Exists.ToString() }) { Tag = file };
remoteListView.Items.Add(itm);
}
updateQuota();
}
catch (Exception ex)
{
MessageBox.Show(this, "Can't refresh." + Environment.NewLine + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
void updateQuota()
{
if (storage == null) throw new InvalidOperationException("Not connected");
ulong totalBytes, availBytes;
storage.GetQuota(out totalBytes, out availBytes);
quotaLabel.Text = string.Format("{0}/{1} bytes used", totalBytes - availBytes, totalBytes);
}
private void downloadButton_Click(object sender, EventArgs e)
{
if (storage == null)
{
MessageBox.Show(this, "Not connected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (remoteListView.SelectedIndices.Count != 1)
{
MessageBox.Show(this, "Please select only one file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
IRemoteFile file = remoteListView.SelectedItems[0].Tag as IRemoteFile;
saveFileDialog1.FileName = Path.GetFileName(file.Name);
if (saveFileDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
try
{
File.WriteAllBytes(saveFileDialog1.FileName, file.ReadAllBytes());
MessageBox.Show(this, "File downloaded.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(this, "File download failed." + Environment.NewLine + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (storage == null)
{
MessageBox.Show(this, "Not connected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (remoteListView.SelectedIndices.Count == 0)
{
MessageBox.Show(this, "Please select files to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (MessageBox.Show(this, "Are you sure you want to delete the selected files?", "Confirm deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == System.Windows.Forms.DialogResult.No) return;
bool allSuccess = true;
foreach (ListViewItem item in remoteListView.SelectedItems)
{
IRemoteFile file = item.Tag as IRemoteFile;
try
{
bool success = file.Delete();
if (!success)
{
allSuccess = false;
MessageBox.Show(this, file.Name + " failed to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
item.Remove();
}
}
catch (Exception ex)
{
MessageBox.Show(this, file.Name + " failed to delete." + Environment.NewLine + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
updateQuota();
if (allSuccess) MessageBox.Show(this, "Files deleted.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
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<string> failedFiles = new List<string>();
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<string, string>(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<string>;
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);
}
}
}