-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneDriveTile.cs
160 lines (138 loc) · 5.38 KB
/
OneDriveTile.cs
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
// ------------------------------------------------------------------------------
// 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.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class OneDriveTile : UserControl
{
private DriveItem _sourceItem;
private bool _selected;
private GraphServiceClient graphClient;
public OneDriveTile(GraphServiceClient graphClient)
{
this.graphClient = graphClient;
InitializeComponent();
}
public DriveItem SourceItem
{
get { return _sourceItem; }
set
{
if (value == _sourceItem)
return;
_sourceItem = value;
SourceItemChanged();
}
}
private void SourceItemChanged()
{
if (null == _sourceItem) return;
labelName.Text = _sourceItem.Name;
LoadThumbnail();
}
private async void LoadThumbnail()
{
var thumbnail = await this.ThumbnailUrlAsync("medium");
if (null != thumbnail)
{
string thumbnailUri = thumbnail.Url;
pictureBoxThumbnail.ImageLocation = thumbnailUri;
}
}
/// <summary>
/// Retrieve a specific size thumbnail's metadata. If it isn't already
/// available make a call to the service to retrieve it.
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public async Task<Microsoft.Graph.Thumbnail> ThumbnailUrlAsync(string size = "large")
{
bool loadedThumbnails = this._sourceItem != null && this._sourceItem.Thumbnails != null &&
this._sourceItem.Thumbnails.CurrentPage != null;
if (loadedThumbnails)
{
// See if we already have that thumbnail
Thumbnail thumbnail = null;
ThumbnailSet thumbnailSet = null;
switch (size.ToLower())
{
case "small":
thumbnailSet = this._sourceItem.Thumbnails.CurrentPage.FirstOrDefault(set => set.Small != null);
thumbnail = thumbnailSet == null ? null : thumbnailSet.Small;
break;
case "medium":
thumbnailSet = this._sourceItem.Thumbnails.CurrentPage.FirstOrDefault(set => set.Medium != null);
thumbnail = thumbnailSet == null ? null : thumbnailSet.Medium;
break;
case "large":
thumbnailSet = this._sourceItem.Thumbnails.CurrentPage.FirstOrDefault(set => set.Large != null);
thumbnail = thumbnailSet == null ? null : thumbnailSet.Large;
break;
default:
thumbnailSet = this._sourceItem.Thumbnails.CurrentPage.FirstOrDefault(set => set[size] != null);
thumbnail = thumbnailSet == null ? null : thumbnailSet[size];
break;
}
if (thumbnail != null)
{
return thumbnail;
}
}
if (!loadedThumbnails)
{
try
{
// Try to load the thumbnail from the service if we haven't loaded thumbnails.
return await this.graphClient.Drive.Items[this._sourceItem.Id].Thumbnails["0"][size].Request().GetAsync();
}
catch (ServiceException)
{
// Just swallow not found. We don't want an error popup and we just won't render a thumbnail
return null;
}
}
return null;
}
private void Control_Click(object sender, EventArgs e)
{
OnClick(EventArgs.Empty);
}
private void Control_DoubleClick(object sender, EventArgs e)
{
OnDoubleClick(EventArgs.Empty);
}
public bool Selected
{
get { return _selected; }
set
{
if (value != _selected)
{
_selected = value;
labelName.Font = _selected ? new Font(labelName.Font, FontStyle.Bold) : new Font(labelName.Font, FontStyle.Regular);
}
}
}
//protected override void OnPaint(PaintEventArgs e)
//{
// base.OnPaint(e);
// if (this.Selected)
// {
// Color color = Color.White;
// int width = 2;
// ButtonBorderStyle style = ButtonBorderStyle.Solid;
// ControlPaint.DrawBorder(e.Graphics, ClientRectangle,
// color, width, style,
// color, width, style,
// color, width, style,
// color, width, style);
// }
//}
}
}