This repository was archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectForm.cs
More file actions
322 lines (273 loc) · 10.4 KB
/
ProjectForm.cs
File metadata and controls
322 lines (273 loc) · 10.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
using System;
//using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using System.Drawing;
using System.Windows.Forms;
using Newtonsoft.Json.Linq; // For JSON parsing.
//using System.Threading.Tasks; // For requests.
using System.Collections.Generic;
namespace EmailToProject
{
public class ProjectForm
{
private static Dictionary<string, string> statuses;
private static string commType;
private MailItem mail;
private Button[] projectButtons;
private Form page;
private ProjectRequest request;
private TextBox tb;
private TextBox notes;
private ComboBox cmbox;
private Form login;
private TextBox userTB;
private TextBox passTB;
private string defaultStatus = "Returned";
private string contactEmail;
private string contactName;
public ProjectForm(MailItem mail, ProjectRequest request) {
this.mail = mail;
this.request = request;
if(prepForm() == true) return; // If there is an error, it will be displayed. Stop proccessing.
createForm();
if(searchEmail() == true) return;
page.Show();
}
public Boolean prepForm()
{
//if (setupAuth() == true) return true;
if (setupStatuses() == true) return true;
if (setupCommType() == true) return true;
return false;
}
private Boolean checkRequestError()
{
string errorMessage = request.getError();
if (errorMessage == "Invalid credentials")
{
getAuth();
return true;
}
if (errorMessage != "")
{
MessageBox.Show(errorMessage);
return true;
}
return false;
}
// Creates the form used to search and display projects.
private void createForm()
{
// Build the form
page = new Form();
page.Text = "Add email to project";
page.Width = 380;
page.Height = 205;
// Search box
tb = new TextBox();
tb.Width = 250;
tb.KeyDown += enterFromSearch;
page.Controls.Add(tb);
// Search button
Button search = new Button();
search.Text = "Search Projects";
search.Location = new Point(260, 0);
search.Width = 100;
search.Click += searchProjects;
page.Controls.Add(search);
// Subject/Notes field
notes = new TextBox();
notes.Width = 250;
notes.Location = new Point(0, 23);
notes.Text = mail.Subject;
page.Controls.Add(notes);
// Logout button
Button logout = new Button();
logout.Text = "Logout";
logout.Location = new Point(260, 140);
logout.Width = 100;
logout.Click += forgetAuth;
page.Controls.Add(logout);
// Status Type
cmbox = new ComboBox();
cmbox.DropDownStyle = ComboBoxStyle.DropDownList; // Make it a dropdown.
cmbox.Location = new Point(260, 24);
cmbox.Width = 100;
cmbox.Items.AddRange(System.Linq.Enumerable.ToArray(statuses.Keys));
cmbox.SelectedItem = defaultStatus; // I have to take only the names from the dictionary because otherwise I can't select the item I want.
page.Controls.Add(cmbox);
// Search results
projectButtons = new Button[5];
for (int i = 0; i < projectButtons.Length;i++ )
{
Button button = new Button();
button.Width = 250;
button.Location = new Point(0, (i + 2) * 23);
button.Click += attatchToProject;
button.Visible = false;
page.Controls.Add(button);
projectButtons[i] = button;
}
}
// Makes sure statuses is initilized and not in error. If it is, initilize it.
private Boolean setupStatuses()
{
if (statuses == null || statuses.ContainsKey("-1"))
{
statuses = new Dictionary<string, string>();
JToken stats = request.getStatuses();
if (checkRequestError() == true)
{
statuses = null;
return true;
}
foreach (var status in stats)
{
statuses.Add((string)status.SelectToken("text"), (string)status.SelectToken("id"));
}
}
return false;
}
// Makes sure the communication type is able to be set to Email.
private Boolean setupCommType()
{
if (commType == null || commType == "-1")
{
JToken jCommType = request.getCommType();
if (checkRequestError() == true)
{
commType = null;
return true;
}
foreach (var cType in jCommType)
{
if((string)cType.SelectToken("text") == "Email") {
commType = (string)cType.SelectToken("id");
break;
}
}
}
return false;
}
private void forgetAuth(Object sender, EventArgs e) {
request.forgetAuth();
page.Hide();
MessageBox.Show("You have been logged out.");
}
private void getAuth()
{
login = new Form();
login.Text = "Login";
login.Width = 220;
login.Height = 120;
userTB = new TextBox();
userTB.Location = new Point(75,1);
userTB.Width = 125;
login.Controls.Add(userTB);
Label userLabel = new Label();
userLabel.Text = "Email:";
userLabel.Width = 75;
userLabel.Location = new Point(1, 3);
login.Controls.Add(userLabel);
passTB = new TextBox();
passTB.Location = new Point(75, 25);
passTB.Width = 125;
passTB.UseSystemPasswordChar = true;
login.Controls.Add(passTB);
Label passLabel = new Label();
passLabel.Text = "Password:";
passLabel.Width = 75;
passLabel.Location = new Point(1, 28);
login.Controls.Add(passLabel);
Button loginButton = new Button();
loginButton.Text = "Login";
loginButton.Location = new Point(150, 50);
loginButton.Width = 50;
loginButton.Click += saveAuth;
login.Controls.Add(loginButton);
login.AcceptButton = loginButton;
Label messageLabel = new Label();
messageLabel.Text = "Please login and try your action again.";
messageLabel.Width = 150;
messageLabel.Height = 30;
messageLabel.Location = new Point(1, 50);
login.Controls.Add(messageLabel);
login.Show();
}
private void saveAuth(Object sender, EventArgs e)
{
request.getToken(userTB.Text, passTB.Text);
login.Hide();
checkRequestError();
}
private Boolean searchEmail() {
MAPIFolder folder = mail.Parent;
if (folder.Name == "Sent" || folder.Name == "Outbox")
{
// MessageBox.Show("Folder: Sent" + mail.To);
contactEmail = mail.To.Replace("\'", "");
contactName = contactEmail;
}
else
{
// MessageBox.Show("Folder: Other" + mail.SenderEmailAddress);
contactEmail = mail.SenderEmailAddress;
contactName = mail.SenderName;
}
JToken json = request.searchProjects(contactEmail);
if (checkRequestError() == true) return true;
updateButtons(json);
return false;
}
private void enterFromSearch(Object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
searchProjects(sender, e);
}
}
private void searchProjects(Object sender, EventArgs e)
{
JToken json = request.searchProjects(tb.Text);
checkRequestError(); // No error checking because it wouldn't go anywhere anyway.
updateButtons(json);
}
private void updateButtons(JToken projects)
{
int counter = 0;
foreach (var project in projects)
{
this.projectButtons[counter].Text = (string)project.SelectToken("title");
this.projectButtons[counter].Tag = (string)project.SelectToken("id");
this.projectButtons[counter].Visible = true;
counter++;
// Quit if the array is full.
if (counter >= projectButtons.Length)
{
break;
}
}
// Hides any unused buttons.
for (; counter < projectButtons.Length; counter++)
{
projectButtons[counter].Visible = false;
}
}
// Attatches an email as a communication to a project.
public void attatchToProject(Object sender, EventArgs e)
{
Button button = (Button)sender;
string id = (string)button.Tag;
string firstLine = mail.Subject == notes.Text ? mail.Subject : notes.Text + "\n" + mail.Subject;
string body = firstLine + "\n" + mail.Body;
JToken status = request.attachEmail(id, contactEmail, contactName, body, commType, statuses[(string)cmbox.SelectedItem], mail.ReceivedTime.ToString());
checkRequestError(); // No error checking because it wouldn't go anywhere anyway.
page.Hide();
if ((string)status.SelectToken("project_id") == id)
{
MessageBox.Show("Email added successfully.");
// If there is an error, the error handler is responsible for displaying it.
}
}
}
}