-
Notifications
You must be signed in to change notification settings - Fork 4
/
LinkedInUI.cs
331 lines (289 loc) · 8.72 KB
/
LinkedInUI.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LinkedIn.NET;
using LinkedIn.NET.Options;
using LinkedIn.NET.Groups;
namespace LinkedInDesktopUI
{
public static class ExtensionMethods
{
public static string LimitLength(this string s, int len)
{
string ret = s;
if (s.Length > len)
{
ret = s.Substring(0, len - 3) + "...";
}
return ret;
}
}
public partial class LinkedInUI : Form
{
protected LinkedInClient linkedInClient;
protected string consumerKey;
protected string consumerSecret;
protected string accessToken;
protected bool haveAccessToken;
protected LinkedInGroupPost selectedPost;
protected TreeNode selectedPostNode;
const string CRLF = "\r\n";
const string REDIRECT_URL = "https://pnotes.sourceforge.net/auth.htm";
const string STATE = "DCEEFWF45453sdffef424";
public LinkedInUI()
{
InitializeComponent();
LoadConfiguration();
InitializeClient();
btnComment.Enabled = false;
if (String.IsNullOrEmpty(accessToken))
{
Authenticate();
}
else
{
LoadGroups();
}
}
protected void MakeComment(object sender, EventArgs args)
{
AddComment(selectedPost, tbComment.Text);
}
protected void OnNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
StringBuilder sb = new StringBuilder();
if (e.Node.Tag is LinkedInGroup)
{
LinkedInGroup group = (LinkedInGroup)e.Node.Tag;
sb.Append("Group: " + group.Name);
sb.Append(CRLF);
sb.Append(CRLF);
sb.Append("Category: " + group.Category);
sb.Append(CRLF);
sb.Append(CRLF);
sb.Append("Description: " + group.ShortDescription);
btnComment.Enabled = false;
}
else if (e.Node.Tag is LinkedInGroupPost)
{
LinkedInGroupPost post = (LinkedInGroupPost)e.Node.Tag;
sb.Append("Title: " + post.Title);
sb.Append(CRLF);
sb.Append(CRLF);
sb.Append("By: " + post.Creator.FirstName + " " + post.Creator.LastName);
sb.Append(CRLF);
sb.Append(CRLF);
sb.Append("On: " + post.CreationTime.ToString());
sb.Append(CRLF);
sb.Append(CRLF);
sb.Append("Summary: " + post.Summary);
btnComment.Enabled = true;
selectedPost = post;
selectedPostNode = e.Node;
}
else if (e.Node.Tag is LinkedInGroupComment)
{
LinkedInGroupComment comment = (LinkedInGroupComment)e.Node.Tag;
sb.Append("By: " + comment.Creator.FirstName + " " + comment.Creator.LastName);
sb.Append(CRLF);
sb.Append(CRLF);
sb.Append("On: " + comment.CreationTime.ToString());
sb.Append(CRLF);
sb.Append(CRLF);
sb.Append(comment.Text);
btnComment.Enabled = true;
}
tbInfo.Text = sb.ToString();
}
protected void OnNodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Tag is LinkedInGroup)
{
LinkedInGroup group = (LinkedInGroup)e.Node.Tag;
LoadPostsForGroup(e.Node, group);
}
else if (e.Node.Tag is LinkedInGroupPost)
{
LinkedInGroupPost post = (LinkedInGroupPost)e.Node.Tag;
LoadCommentsForPost(e.Node, post);
}
}
/// <summary>
/// Load the configuration and check for the access token.
/// </summary>
protected void LoadConfiguration()
{
try
{
string[] linkedInConfig = File.ReadAllLines("linkedin.config");
consumerKey = linkedInConfig[0];
consumerSecret = linkedInConfig[1];
if (linkedInConfig.Length == 3)
{
accessToken = linkedInConfig[2];
haveAccessToken = true;
}
}
catch (Exception ex)
{
EmitException("LinkedIn.config file is missing or corrupt." + "\r\n" + ex.Message);
}
}
protected void SaveConfiguration()
{
File.WriteAllLines("linkedin.config", new string[]
{
consumerKey,
consumerSecret,
accessToken,
});
}
protected void InitializeClient()
{
if ((!String.IsNullOrEmpty(consumerKey)) && (!String.IsNullOrEmpty(consumerSecret)))
{
linkedInClient = new LinkedInClient(consumerKey, consumerSecret);
if (haveAccessToken)
{
linkedInClient.AccessToken = accessToken;
}
}
}
// The code in this method is heavily borrowed from the LinkedIn.NET's LNTest "authenticate" method (DlgExample.cs)
protected void Authenticate()
{
if (linkedInClient != null)
{
var options = new LinkedInAuthorizationOptions
{
RedirectUrl = REDIRECT_URL,
Permissions = LinkedInPermissions.Connections | LinkedInPermissions.ContactsInfo |
LinkedInPermissions.EmailAddress | LinkedInPermissions.FullProfile |
LinkedInPermissions.GroupDiscussions | LinkedInPermissions.Messages |
LinkedInPermissions.Updates,
State = STATE
};
//create new instance of authorization dialog using authorization link built by _Client
var dlgAuth = new DlgAuthorization(linkedInClient.GetAuthorizationUrl(options));
if (dlgAuth.ShowDialog(this) == DialogResult.OK)
{
//get access token using authorization code received
var response = linkedInClient.GetAccessToken(dlgAuth.AuthorizationCode, REDIRECT_URL);
if (response.Result != null && response.Status == LinkedInResponseStatus.OK)
{
accessToken = response.Result.AccessToken;
SaveConfiguration();
}
}
else
{
//show error information
MessageBox.Show(dlgAuth.OauthErrorDescription, dlgAuth.OauthError);
}
}
}
protected async void LoadGroups()
{
if (haveAccessToken)
{
tvGroups.Nodes.Clear();
tvGroups.Nodes.Add("Loading...");
LinkedInGetGroupOptions options = new LinkedInGetGroupOptions();
options.GroupOptions.SelectAll();
LinkedInResponse<IEnumerable<LinkedInGroup>> result = await Task.Run(() => linkedInClient.GetMemberGroups(options));
if (result.Result != null && result.Status == LinkedInResponseStatus.OK)
{
ShowMemberGroups(result);
}
else
{
ReRun(result.Status, result.Message);
}
}
}
protected async void LoadPostsForGroup(TreeNode node, LinkedInGroup group)
{
LinkedInGetGroupPostsOptions options = new LinkedInGetGroupPostsOptions();
options.PostOptions.SelectAll();
options.GroupId = group.Id;
ShowLoading(node);
await Task.Run(() => group.LoadPosts(options));
ShowGroupPosts(node, group);
node.ExpandAll();
}
protected async void LoadCommentsForPost(TreeNode node, LinkedInGroupPost post)
{
LinkedInGetGroupPostCommentsOptions options = new LinkedInGetGroupPostCommentsOptions();
options.CommentOptions.SelectAll();
options.PostId = post.Id;
ShowLoading(node);
await Task.Run(() => post.LoadComments(options));
ShowGroupPostComments(node, post);
node.ExpandAll();
}
protected void ShowMemberGroups(LinkedInResponse<IEnumerable<LinkedInGroup>> result)
{
tvGroups.Nodes.Clear();
foreach (LinkedInGroup group in result.Result)
{
TreeNode node = tvGroups.Nodes.Add(group.Name);
node.Tag = group;
}
}
protected void ShowGroupPosts(TreeNode node, LinkedInGroup group)
{
node.Nodes.Clear();
foreach (LinkedInGroupPost post in group.Posts)
{
TreeNode childNode = node.Nodes.Add(post.Title);
childNode.Tag = post;
}
}
protected void ShowGroupPostComments(TreeNode node, LinkedInGroupPost post)
{
node.Nodes.Clear();
foreach (LinkedInGroupComment comment in post.Comments)
{
TreeNode childNode = node.Nodes.Add(comment.Text.LimitLength(64));
childNode.Tag = comment;
}
}
protected void AddComment(LinkedInGroupPost post, string comment)
{
post.Comment(comment);
// After posting the comment, add it to the post's comment collection, select it, and display the comment in the info box.
tbComment.Text = "(posted)";
TreeNode childNode = selectedPostNode.Nodes.Add(comment.LimitLength(64));
tvGroups.SelectedNode = childNode;
tbInfo.Text = comment;
}
protected void ShowLoading(TreeNode node)
{
node.Nodes.Clear();
node.Nodes.Add("Loading...");
node.ExpandAll();
}
protected void ReRun(LinkedInResponseStatus status, string message)
{
switch (status)
{
case LinkedInResponseStatus.ExpiredToken:
case LinkedInResponseStatus.InvalidAccessToken:
case LinkedInResponseStatus.UnauthorizedAction:
Authenticate();
break;
default:
MessageBox.Show(message);
break;
}
}
protected void EmitException(string exception)
{
MessageBox.Show(exception, "An Error Has Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}