-
Notifications
You must be signed in to change notification settings - Fork 3
/
event_actions.cpp
539 lines (457 loc) · 16.7 KB
/
event_actions.cpp
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include <cstring>
//FIXME CHECK FOR ICAL ERRORS
//FIXME sizeof(all_props)/sizeof(all_props[0]) is probably bad
void add_class(icalcomponent* calendar, vector<string>& classes)
{
cout << "What is the name of the class?" << endl;
string class_name;
cin >> class_name;
classes.push_back(class_name);
//List each property field one at a time
string all_props[] = {"ATTACH", "ATTENDEE", "CATEGORIES", "CLASS", "COMMENT", "CONTACT", "CREATED", "DESCRIPTION", "DTEND", "DTSTAMP", "DTSTART", "DURATION", "EXDATE", "EXRULE", "GEO", "LAST-MOD", "LOCATION", "ORGANIZER", "PRIORITY", "RDATE", "RECURID", "RELATED", "RESOURCES", "RRULE", "RSTATUS", "SEQ", "STATUS", "SUMMARY", "TRANSP", "UID", "URL"};
//FIXME Start/end dates rely on default start/end of semester if user okays default
vector<string> values;
for (int i = 0; i < sizeof(all_props) / sizeof(all_props[0]); i++)
{
cout << all_props[i] << ": ";
std::flush(cout);
string value = "";
getline(cin, value);
if (cin.fail())
{
throw cin_fail_ex;
}
//FIXME Take user input and validate
values.push_back(value);
cout << endl;
}
struct icaltimetype atime = icaltime_from_timet(time(0), 0);
//Add the values to the event
icalcomponent* class_event = icalcomponent_vanew(
ICAL_VEVENT_COMPONENT,
icalproperty_new_dtstamp(atime),
icalproperty_new_contact(values[5].c_str()),
icalproperty_new_comment(values[4].c_str()),
icalproperty_vanew_attach(
NULL, //FIXME icalattach *icalattach_new_from_url (const char *url);
values[0].c_str(), //FIXME?
0
),
//FIXME icalproperty_new_duration(values[11].c_str()), //needs right type
//FIXME icalproperty_new_exdate(values[13].c_str()), //needs right type
//FIXME icalproperty_new_geo(values[14].c_str()), //needs right type
//FIXME icalproperty_new_lastmodified(values[15].c_str()),
icalproperty_new_priority(atoi(values[18].c_str())),
//FIXME icalproperty_vanew_recurid(),
//FIXME icalproperty_vanew_related(),
//FIXME icalproperty_vanew_resources(),
//FIXME icalproperty_vanew_sequence(),
//FIXME icalproperty_new_rrule(values[23].c_str()),
//FIXME icalproperty_new_status(values[26].c_str()),
//FIXME icalproperty_new_rstatus() may not exist...
/*FIXME icalproperty_vanew_rdate(
values[15].c_str(),
icalparameter_new_tzid("US-Central"), //FIXME should get timezone from user
0
),*/
/*FIXME icalproperty_vanew_exdate(
values[12].c_str(),
icalparameter_new_tzid("US-Central"), //FIXME should get timezone from user
0
),*/
//FIXME icalproperty_new_transp(values[28].c_str()),
icalproperty_new_uid(values[29].c_str()),
icalproperty_new_url(values[30].c_str()),
icalproperty_vanew_organizer(
values[17].c_str(),
icalparameter_new_role(ICAL_ROLE_CHAIR), //FIXME overwrites user settings
0
),
icalproperty_vanew_attendee(
values[1].c_str(),
icalparameter_new_role(ICAL_ROLE_REQPARTICIPANT), //FIXME overwrites user settings
//FIXME icalparameter_new_rsvp(1), //FIXME overwrites user settings
icalparameter_new_cutype(ICAL_CUTYPE_GROUP), //FIXME overwrites user settings
0
),
icalproperty_new_description(values[7].c_str()),
icalproperty_new_categories(values[2].c_str()),
icalproperty_new_class(ICAL_CLASS_PUBLIC), //FIXME overwrites user settings
icalproperty_new_created(atime), //FIXME overwrites user settings
icalproperty_new_summary(values[27].c_str()),
icalproperty_vanew_dtstart(
atime, //FIXME overwrites user settings
icalparameter_new_tzid("US-Central"), //FIXME should get timezone from user
0
),
icalproperty_vanew_dtend(
atime, //FIXME overwrites user settings
icalparameter_new_tzid("US-Central"), //FIXME should get timezone from user
0
),
icalproperty_new_location(values[16].c_str()),
0
);
//FIXME Handle alarm threads (to be implemented later)
//Add the class component to calendar
icalcomponent_add_component(calendar, class_event);
append_action_to_closed_log("Add Class", true);
}
void add_event(icalcomponent* calendar)
{
string class_name;
//Ask if the event is for a class
bool is_class_event = yes_no_prompt("Is this event for a class?");
//FIXME Following not implemented in this sprint
if (is_class_event)
{
//FIXME If event for class, make sure the semester is not over
//FIXME If the semester is over, notify the user
//If event for class, prompt for class id
cout << "What is the name of this class?" << endl;
cin >> class_name;
}
//List each property field one at a time
cout << "Enter information for each of the following properties." << endl;
string all_props[] = {"ATTACH", "ATTENDEE", "CATEGORIES", "CLASS", "COMMENT", "CONTACT", "CREATED", "DESCRIPTION", "DTEND", "DTSTAMP", "DTSTART", "DURATION", "EXDATE", "EXRULE", "GEO", "LAST-MOD", "LOCATION", "ORGANIZER", "PRIORITY", "RDATE", "RECURID", "RELATED", "RESOURCES", "RRULE", "RSTATUS", "SEQ", "STATUS", "SUMMARY", "TRANSP", "UID", "URL"};
vector<string> values;
for (int i = 0; i < sizeof(all_props) / sizeof(all_props[0]); i++)
{
cout << all_props[i] << ": ";
flush(cout);
string value = "";
getline(cin, value); //FIXME Check for failbit
//FIXME Take user input and validate
values.push_back(value);
cout << endl;
}
//If event for class, add property X-CLASS
string value = "";
if (is_class_event)
{
cout << "X-CLASS: ";
flush(cout);
getline(cin, value); //FIXME Check for failbit
//FIXME Take user input and validate
values.push_back(value);
cout << endl;
}
struct icaltimetype atime = icaltime_from_timet(time(0), 0);
//Add the values to the event
icalcomponent* event = icalcomponent_vanew(
ICAL_VEVENT_COMPONENT,
icalproperty_new_dtstamp(atime),
icalproperty_new_contact(values[5].c_str()),
icalproperty_new_comment(values[4].c_str()),
icalproperty_vanew_attach(
NULL, //FIXME is this the file encoding?
values[0].c_str(),
0
),
//FIXME icalproperty_new_duration(values[11].c_str()),
//FIXME icalproperty_new_exdate(values[13].c_str()),
//FIXME icalproperty_new_geo(values[14].c_str()),
//FIXME icalproperty_new_lastmod(values[15].c_str()),
icalproperty_new_priority(atoi(values[18].c_str())),
//FIXME icalproperty_vanew_recurid(),
//FIXME icalproperty_vanew_related(),
//FIXME icalproperty_vanew_resources(),
//FIXME icalproperty_vanew_sequence(),
//FIXME icalproperty_new_rrule(values[23].c_str()),
//FIXME icalproperty_new_status(values[26].c_str()),
//FIXME icalproperty_new_rstatus() may not exist...
/*FIXME icalproperty_vanew_rdate(
values[15].c_str(),
icalparameter_new_tzid("US-Central"), //FIXME should get timezone from user
0
),*/
/*FIXME icalproperty_vanew_exdate(
values[12].c_str(),
icalparameter_new_tzid("US-Central"), //FIXME should get timezone from user
0
),*/
//FIXME icalproperty_new_transp(values[28].c_str()),
icalproperty_new_uid(values[29].c_str()),
icalproperty_new_url(values[30].c_str()),
icalproperty_vanew_organizer(
values[17].c_str(),
icalparameter_new_role(ICAL_ROLE_CHAIR), //FIXME overwrites user settings
0
),
icalproperty_vanew_attendee(
values[1].c_str(),
icalparameter_new_role(ICAL_ROLE_REQPARTICIPANT), //FIXME overwrites user settings
//FIXME icalparameter_new_rsvp(1), //FIXME overwrites user settings
icalparameter_new_cutype(ICAL_CUTYPE_GROUP), //FIXME overwrites user settings
0
),
icalproperty_new_description(values[7].c_str()),
icalproperty_new_categories(values[2].c_str()),
icalproperty_new_class(ICAL_CLASS_PUBLIC), //FIXME overwrites user settings
icalproperty_new_created(atime), //FIXME overwrites user settings
icalproperty_new_summary(values[27].c_str()),
icalproperty_vanew_dtstart(
atime, //FIXME overwrites user settings
icalparameter_new_tzid("US-Central"), //FIXME should get timezone from user
0
),
icalproperty_vanew_dtend(
atime, //FIXME overwrites user settings
icalparameter_new_tzid("US-Central"), //FIXME should get timezone from user
0
),
icalproperty_new_location(values[16].c_str()),
0
);
if (is_class_event)
{
icalproperty* p;
icalproperty_set_x(p, value.c_str());
icalcomponent_add_property(event, p);
}
//Add component event to calendar
icalcomponent_add_component(calendar,event);
//FIXME Handle alarm threads (to be implemented later)
append_action_to_closed_log("Add Event", true);
}
//Order swapped from 7cterm.cpp due to dependencies
icalcomponent* find_event(icalcomponent* calendar)
{
int num_matches = 0;
//Display possible fields and prompt for which ones(!) to use
//FIXME Doesn't display possible properties for readability
cout << "Enter all properties you want to use in all caps, delimited by semicolons" << endl;
string properties;
cin >> properties;
char* c_properties = new char[properties.size()+1];
strcpy(c_properties, properties.c_str());
char* sel_props_c = strtok(c_properties, ";");
vector<string> sel_props;
vector<int> sel_props_pos;
while (sel_props_c != NULL)
{
sel_props.push_back(sel_props_c);
sel_props_pos.push_back(0);
sel_props_c = strtok(c_properties, ";");
}
string all_props[] = {"ATTACH", "ATTENDEE", "CATEGORIES", "CLASS", "COMMENT", "CONTACT", "CREATED", "DESCRIPTION", "DTEND", "DTSTAMP", "DTSTART", "DURATION", "EXDATE", "EXRULE", "GEO", "LAST-MOD", "LOCATION", "ORGANIZER", "PRIORITY", "RDATE", "RECURID", "RELATED", "RESOURCES", "RRULE", "RSTATUS", "SEQ", "STATUS", "SUMMARY", "TRANSP", "UID", "URL"};
for (int i = 0; i < sel_props.size() && i < sizeof(all_props) / sizeof(all_props[0]); i++)
{
for (int j = 0; j < 31; j++)
{
if (sel_props[i] == all_props[j])
{
sel_props_pos[i] = j;
break;
}
}
if (sel_props[i] == "X-CLASS")
{
sel_props_pos[i] = 32; //beyond the end of all_props
}
}
vector<string> values;
cout << "Enter values for: " << endl;
for (int i = 0; i < sel_props.size(); i++)
{
cout << sel_props[i] << ": ";
flush(cout);
string value;
cin >> value; //FIXME error check for type
values.push_back(value);
}
//Iterate through class events and non-class events to match properties
icalcomponent *c;
vector<icalcomponent*> matching_events;
for (c = icalcomponent_get_first_component(calendar,ICAL_VEVENT_COMPONENT); c != 0; c = icalcomponent_get_next_component(calendar,ICAL_VEVENT_COMPONENT))
{
bool is_match = true;
//Check each of the properties
for (int i = 0; i < sel_props.size(); i++)
{
if (sel_props_pos[i] == 32)
{
icalproperty* p = icalcomponent_get_first_property(c, ICAL_X_PROPERTY);
char* class_name;
strcpy(class_name, icalproperty_get_x(p));
for (int j = 0; class_name != 0 && j < values[i].size(); j++)
{
if (class_name[j] != values[i][j])
{
is_match = false;
break;
}
}
}else
{
//FIXME Iterate through all properties
icalproperty* p = icalcomponent_get_first_property(c,ICAL_ANY_PROPERTY);
char* prop_value;
strcpy(prop_value, icalproperty_get_comment(p));
for (int j = 0; prop_value != 0 && j < values[i].size(); j++)
{
if (prop_value[j] != values[i][j])
{
is_match = false;
break;
}
}
}
}
if (is_match)
{
matching_events.push_back(c);
num_matches++;
}
}
if (num_matches == 0)
{
cout << "No matches found" << endl;
return NULL;
}
cout << "Which event do you want?" << endl;
int user_choice;
while (true)
{
cin >> user_choice;
if ((user_choice >= 1) && (user_choice <= num_matches))
{
break;
}
cout << "Invalid selection (Not between 1 and "<< num_matches <<" inclusive)" << endl;
}
//Return the user_choice-th event from the results
return matching_events[user_choice];
}
void delete_event(icalcomponent* calendar)
{
icalcomponent* event = find_event(calendar);
if (!yes_no_prompt("Are you sure you want to delete this event? (y/n"))
{
return;
}
//Find the event in the components
icalcomponent* c = icalcomponent_get_first_component(calendar, ICAL_VEVENT_COMPONENT);
bool found_event = false;
while((c=icalcomponent_get_current_component(c)) != 0 )
{
if(icalcomponent_isa(c) == ICAL_VEVENT_COMPONENT)
{
if (c == event)
{
icalcomponent_remove_component(calendar, c);
found_event = true;
}
}else
{
icalcomponent_get_next_component(calendar, ICAL_VEVENT_COMPONENT);
}
}
if (!found_event)
{
append_action_to_closed_log("Delete event", false);
return;
}
append_action_to_closed_log("Delete event", true);
}
void edit_event(icalcomponent* calendar)
{
icalcomponent* event = find_event(calendar);
if (event == NULL)
{
append_action_to_closed_log("Edit event", false);
}
//Once event found by property, display all fields and prompt for which to edit
cout << "Which fields would you like to edit?" << endl;
string properties;
cin >>properties;
char* c_properties = new char[properties.size()+1];
strcpy(c_properties, properties.c_str());
char * sel_props_c = strtok(c_properties, ";");
vector<string> sel_props;
vector<int> sel_props_pos;
while (sel_props_c != NULL)
{
sel_props.push_back(sel_props_c);
sel_props_pos.push_back(0);
sel_props_c = strtok(c_properties, ";");
}
string all_props[] = {"ATTACH", "ATTENDEE", "CATEGORIES", "CLASS", "COMMENT", "CONTACT", "CREATED", "DESCRIPTION", "DTEND", "DTSTAMP", "DTSTART", "DURATION", "EXDATE", "EXRULE", "GEO", "LAST-MOD", "LOCATION", "ORGANIZER", "PRIORITY", "RDATE", "RECURID", "RELATED", "RESOURCES", "RRULE", "RSTATUS", "SEQ", "STATUS", "SUMMARY", "TRANSP", "UID", "URL"};
for (int i = 0; i < sel_props.size(); i++)
{
for (int j = 0; j < 31; j++)
{
if (sel_props[i] == all_props[j])
{
sel_props_pos[i] = j;
break;
}
}
if (sel_props[i] == "X-CLASS")
{
sel_props_pos[i] = 32; //beyond the end of all_props
}
}
//Update fields
vector<string> values;
cout << "Enter values for: " << endl;
for (int i = 0; i < sel_props.size(); i++)
{
cout << sel_props[i] << ": ";
flush(cout);
string value;
cin >> value; //FIXME error check for type
values.push_back(value);
}
//Check that we are in the right property
icalcomponent *c;
for (c = icalcomponent_get_first_component(event,ICAL_VEVENT_COMPONENT); c != 0; c = icalcomponent_get_next_component(event,ICAL_VEVENT_COMPONENT))
{
bool is_match = true;
//Check each of the properties
for (int i = 0; i < sel_props.size(); i++)
{
if (sel_props_pos[i] == 32)
{
icalproperty* p = icalcomponent_get_first_property(event, ICAL_X_PROPERTY);
icalproperty_set_x(ical_x_class_prop, values[i].c_str());
}else
{
icalproperty* p;
for( p = icalcomponent_get_first_property(event,ICAL_ANY_PROPERTY); p != 0; p = icalcomponent_get_next_property(event,ICAL_ANY_PROPERTY))
{
//FIXME no support for multi-parameter properties yet
//FIXME also no value checking, which matters
//FIXME use void icalproperty_set_value(icalproperty* prop, icalvalue* value);
}
}
}
}
append_action_to_closed_log("Edit event", true);
}
void view_event(icalcomponent* calendar)
{
//Ask user if they want null fields in addition to filled ones
bool show_null_fields_too = yes_no_prompt("Do you want to view empty fields? (y/n)");
icalcomponent* event = find_event(calendar);
if (event == NULL)
{
append_action_to_closed_log("View event", false);
}
//Once user selects desired one, displays all needed fields
icalproperty* p;
for(p = icalcomponent_get_first_property(event,ICAL_ANY_PROPERTY); p != 0; p = icalcomponent_get_next_property(event,ICAL_ANY_PROPERTY))
{
if ((icalproperty_get_comment(p) != NULL) || (show_null_fields_too))
{
cout << icalproperty_get_x_name(p) << ": ";
cout << icalproperty_get_comment(p) << endl;
}
}
append_action_to_closed_log("View event", true);
}
void delete_class(icalcomponent* calendar)
{
//FIXME Not implemented in this sprint
//FIXME Are we planning to remove ALL events associated with the class?
}