-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathFormBrowser.cs
More file actions
507 lines (426 loc) · 16.4 KB
/
FormBrowser.cs
File metadata and controls
507 lines (426 loc) · 16.4 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
namespace OneDriveApiBrowser
{
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class FormBrowser : Form
{
public const string MsaClientId = "";
public const string MsaReturnUrl = "";
private enum ClientType
{
Consumer,
Business
}
private const int UploadChunkSize = 10 * 1024 * 1024; // 10 MB
//private IOneDriveClient oneDriveClient { get; set; }
private GraphServiceClient graphClient { get; set; }
private ClientType clientType { get; set; }
private DriveItem CurrentFolder { get; set; }
private DriveItem SelectedItem { get; set; }
private OneDriveTile _selectedTile;
public FormBrowser()
{
InitializeComponent();
}
private void ShowWork(bool working)
{
this.UseWaitCursor = working;
this.progressBar1.Visible = working;
}
private async Task LoadFolderFromId(string id)
{
if (null == this.graphClient) return;
// Update the UI for loading something new
ShowWork(true);
LoadChildren(new DriveItem[0]);
try
{
var expandString = this.clientType == ClientType.Consumer
? "thumbnails,children($expand=thumbnails)"
: "thumbnails,children";
var folder =
await this.graphClient.Drive.Items[id].Request().Expand(expandString).GetAsync();
ProcessFolder(folder);
}
catch (Exception exception)
{
PresentServiceException(exception);
}
ShowWork(false);
}
private async Task LoadFolderFromPath(string path = null)
{
if (null == this.graphClient) return;
// Update the UI for loading something new
ShowWork(true);
LoadChildren(new DriveItem[0]);
try
{
DriveItem folder;
var expandValue = this.clientType == ClientType.Consumer
? "thumbnails,children($expand=thumbnails)"
: "thumbnails,children";
if (path == null)
{
folder = await this.graphClient.Drive.Root.Request().Expand(expandValue).GetAsync();
}
else
{
folder =
await
this.graphClient.Drive.Root.ItemWithPath("/" + path)
.Request()
.Expand(expandValue)
.GetAsync();
}
ProcessFolder(folder);
}
catch (Exception exception)
{
PresentServiceException(exception);
}
ShowWork(false);
}
private void ProcessFolder(DriveItem folder)
{
if (folder != null)
{
this.CurrentFolder = folder;
LoadProperties(folder);
if (folder.Folder != null && folder.Children != null && folder.Children.CurrentPage != null)
{
LoadChildren(folder.Children.CurrentPage);
}
}
}
private void LoadProperties(DriveItem item)
{
this.SelectedItem = item;
objectBrowser.SelectedItem = item;
}
private void LoadChildren(IList<DriveItem> items)
{
flowLayoutContents.SuspendLayout();
flowLayoutContents.Controls.Clear();
// Load the children
foreach (var obj in items)
{
AddItemToFolderContents(obj);
}
flowLayoutContents.ResumeLayout();
}
private void AddItemToFolderContents(DriveItem obj)
{
flowLayoutContents.Controls.Add(CreateControlForChildObject(obj));
}
private void RemoveItemFromFolderContents(DriveItem itemToDelete)
{
flowLayoutContents.Controls.RemoveByKey(itemToDelete.Id);
}
private Control CreateControlForChildObject(DriveItem item)
{
OneDriveTile tile = new OneDriveTile(this.graphClient);
tile.SourceItem = item;
tile.Click += ChildObject_Click;
tile.DoubleClick += ChildObject_DoubleClick;
tile.Name = item.Id;
return tile;
}
void ChildObject_DoubleClick(object sender, EventArgs e)
{
var item = ((OneDriveTile)sender).SourceItem;
// Look up the object by ID
NavigateToFolder(item);
}
void ChildObject_Click(object sender, EventArgs e)
{
if (null != _selectedTile)
{
_selectedTile.Selected = false;
}
var item = ((OneDriveTile)sender).SourceItem;
LoadProperties(item);
_selectedTile = (OneDriveTile)sender;
_selectedTile.Selected = true;
}
private void FormBrowser_Load(object sender, EventArgs e)
{
}
private void NavigateToFolder(DriveItem folder)
{
Task t = LoadFolderFromId(folder.Id);
// Fix up the breadcrumbs
var breadcrumbs = flowLayoutPanelBreadcrumb.Controls;
bool existingCrumb = false;
foreach (LinkLabel crumb in breadcrumbs)
{
if (crumb.Tag == folder)
{
RemoveDeeperBreadcrumbs(crumb);
existingCrumb = true;
break;
}
}
if (!existingCrumb)
{
LinkLabel label = new LinkLabel();
label.Text = "> " + folder.Name;
label.LinkArea = new LinkArea(2, folder.Name.Length);
label.LinkClicked += linkLabelBreadcrumb_LinkClicked;
label.AutoSize = true;
label.Tag = folder;
flowLayoutPanelBreadcrumb.Controls.Add(label);
}
}
private void linkLabelBreadcrumb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel link = (LinkLabel)sender;
RemoveDeeperBreadcrumbs(link);
DriveItem item = link.Tag as DriveItem;
if (null == item)
{
Task t = LoadFolderFromPath(null);
}
else
{
Task t = LoadFolderFromId(item.Id);
}
}
private void RemoveDeeperBreadcrumbs(LinkLabel link)
{
// Remove the breadcrumbs deeper than this item
var breadcrumbs = flowLayoutPanelBreadcrumb.Controls;
int indexOfControl = breadcrumbs.IndexOf(link);
for (int i = breadcrumbs.Count - 1; i > indexOfControl; i--)
{
breadcrumbs.RemoveAt(i);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void UpdateConnectedStateUx(bool connected)
{
signInMsaToolStripMenuItem.Visible = !connected;
signOutToolStripMenuItem.Visible = connected;
flowLayoutPanelBreadcrumb.Visible = connected;
flowLayoutContents.Visible = connected;
}
private async void signInMsaToolStripMenuItem_Click(object sender, EventArgs e)
{
await this.SignIn();
}
private async Task SignIn()
{
try
{
this.graphClient = AuthenticationHelper.GetAuthenticatedClient();
}
catch (ServiceException exception)
{
PresentServiceException(exception);
}
try
{
await LoadFolderFromPath();
UpdateConnectedStateUx(true);
}
catch (ServiceException exception)
{
PresentServiceException(exception);
this.graphClient = null;
}
}
private void signOutToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.graphClient != null)
{
AuthenticationHelper.SignOut();
}
UpdateConnectedStateUx(false);
}
private System.IO.Stream GetFileStreamForUpload(string targetFolderName, out string originalFilename)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Upload to " + targetFolderName;
dialog.Filter = "All Files (*.*)|*.*";
dialog.CheckFileExists = true;
var response = dialog.ShowDialog();
if (response != DialogResult.OK)
{
originalFilename = null;
return null;
}
try
{
originalFilename = System.IO.Path.GetFileName(dialog.FileName);
return new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Open);
}
catch (Exception ex)
{
MessageBox.Show("Error uploading file: " + ex.Message);
originalFilename = null;
return null;
}
}
private async void simpleUploadToolStripMenuItem_Click(object sender, EventArgs e)
{
var targetFolder = this.CurrentFolder;
string filename;
using (var stream = GetFileStreamForUpload(targetFolder.Name, out filename))
{
if (stream != null)
{
// Since the ItemWithPath method is available only at Drive.Root, we need to strip
// /drive/root: (12 characters) from the parent path string.
string folderPath = targetFolder.ParentReference == null
? ""
: targetFolder.ParentReference.Path.Remove(0, 12) + "/" + Uri.EscapeUriString(targetFolder.Name);
var uploadPath = folderPath + "/" + Uri.EscapeUriString(System.IO.Path.GetFileName(filename));
try
{
var uploadedItem =
await
this.graphClient.Drive.Root.ItemWithPath(uploadPath).Content.Request().PutAsync<DriveItem>(stream);
AddItemToFolderContents(uploadedItem);
MessageBox.Show("Uploaded with ID: " + uploadedItem.Id);
}
catch (Exception exception)
{
PresentServiceException(exception);
}
}
}
}
private async void simpleIDbasedToolStripMenuItem_Click(object sender, EventArgs e)
{
var targetFolder = this.CurrentFolder;
string filename;
using (var stream = GetFileStreamForUpload(targetFolder.Name, out filename))
{
if (stream != null)
{
try
{
var uploadedItem =
await
this.graphClient.Drive.Items[targetFolder.Id].ItemWithPath(filename).Content.Request()
.PutAsync<DriveItem>(stream);
AddItemToFolderContents(uploadedItem);
MessageBox.Show("Uploaded with ID: " + uploadedItem.Id);
}
catch (Exception exception)
{
PresentServiceException(exception);
}
}
}
}
private async void createFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
FormInputDialog dialog = new FormInputDialog("Create Folder", "New folder name:");
var result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(dialog.InputText))
{
try
{
var folderToCreate = new DriveItem { Name = dialog.InputText, Folder = new Folder() };
var newFolder =
await this.graphClient.Drive.Items[this.SelectedItem.Id].Children.Request()
.AddAsync(folderToCreate);
if (newFolder != null)
{
MessageBox.Show("Created new folder with ID " + newFolder.Id);
this.AddItemToFolderContents(newFolder);
}
}
catch(ServiceException exception)
{
PresentServiceException(exception);
}
catch (Exception exception)
{
PresentServiceException(exception);
}
}
}
private static void PresentServiceException(Exception exception)
{
string message = null;
var oneDriveException = exception as ServiceException;
if (oneDriveException == null)
{
message = exception.Message;
}
else
{
message = string.Format("{0}{1}", Environment.NewLine, oneDriveException.ToString());
}
MessageBox.Show(string.Format("OneDrive reported the following error: {0}", message));
}
private async void deleteSelectedItemToolStripMenuItem_Click(object sender, EventArgs e)
{
var itemToDelete = this.SelectedItem;
var result = MessageBox.Show("Are you sure you want to delete " + itemToDelete.Name + "?", "Confirm Delete", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
try
{
await this.graphClient.Drive.Items[itemToDelete.Id].Request().DeleteAsync();
RemoveItemFromFolderContents(itemToDelete);
MessageBox.Show("Item was deleted successfully");
}
catch (Exception exception)
{
PresentServiceException(exception);
}
}
}
private async void getChangesHereToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var result =
await this.graphClient.Drive.Items[this.CurrentFolder.Id].Delta().Request().GetAsync();
foreach ( DriveItem item in result)
{
Console.WriteLine(item.Name);
}
}
catch (Exception ex)
{
PresentServiceException(ex);
}
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private async void saveSelectedFileToolStripMenuItem_Click(object sender, EventArgs e)
{
var item = this.SelectedItem;
if (null == item)
{
MessageBox.Show("Nothing selected.");
return;
}
var dialog = new SaveFileDialog();
dialog.FileName = item.Name;
dialog.Filter = "All Files (*.*)|*.*";
var result = dialog.ShowDialog();
if (result != System.Windows.Forms.DialogResult.OK)
return;
using (var stream = await this.graphClient.Drive.Items[item.Id].Content.Request().GetAsync())
using (var outputStream = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Create))
{
await stream.CopyToAsync(outputStream);
}
}
}
}