-
Notifications
You must be signed in to change notification settings - Fork 3
/
Event.m
195 lines (130 loc) · 5.56 KB
/
Event.m
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
/*
Copyright (c) 2010 Rafael Chacon
g-Cal is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
g-Cal is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with g-Cal. If not, see <http://www.gnu.org/licenses/>.
*/
#import "Event.h"
#import "Calendar.h"
@implementation Event
@dynamic location;
@dynamic note;
@dynamic title;
@dynamic endDate;
@dynamic startDate;
@dynamic eventid;
@dynamic calendar;
@dynamic updated;
@dynamic editLink;
@dynamic etag;
@dynamic identifier;
@dynamic allDay;
+(Event *)getEventWithId:(NSString *)eventId forCalendar:(Calendar *)calendar andContext:(NSManagedObjectContext *) context{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];
[request setEntity:entity];
// retrive the objects with a given value for a certain property
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"eventid == %@ AND calendar == %@", eventId, calendar];
[request setPredicate:predicate];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"eventid" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:@"RootEvent"];
aFetchedResultsController.delegate = self;
NSError *error = nil;
NSArray *result = [context executeFetchRequest:request error:&error];
[request release];
[sortDescriptor release];
[sortDescriptors release];
[aFetchedResultsController release];
if (error) return nil;
if (error) return nil;
if(result != nil && [result count] == 1)
return (Event *)[result objectAtIndex:0];
return nil;
}
+(Event *)createEventFromGCal:(GDataEntryCalendarEvent *)event forCalendar:(Calendar *)calendar withContext:(NSManagedObjectContext *)context{
GDataWhen *when = [[event objectsForExtensionClass:[GDataWhen class]] objectAtIndex:0];
// Note: An event might have multiple locations. We're only displaying the first one.
GDataWhere *addr = [[event locations] objectAtIndex:0];
Event *anEvent;
anEvent = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context];
anEvent.title = [[event title] stringValue];
if( when ){
if (![[when startTime] hasTime ])
anEvent.allDay =[NSNumber numberWithBool:YES];
anEvent.startDate = [[when startTime] date];
anEvent.endDate = [[when endTime] date];
}
anEvent.editLink = [[event editLink] href];
anEvent.eventid = [event iCalUID];
anEvent.updated = [[event updatedDate] date];
anEvent.note = [[event content] stringValue];
if( addr )
anEvent.location = [addr stringValue];
anEvent.calendar = calendar;
anEvent.etag = [event ETag];
anEvent.identifier = [event identifier];
NSError *core_data_error = nil;
if (![context save:&core_data_error]) {
NSLog(@"Unresolved error saving a event %@, %@", core_data_error, [core_data_error userInfo]);
return nil;
}
return anEvent;
}
-(BOOL)updateEventFromGCal:(GDataEntryCalendarEvent *)event forCalendar:(Calendar *)calendar withContext:(NSManagedObjectContext *)context{
GDataWhen *when = [[event objectsForExtensionClass:[GDataWhen class]] objectAtIndex:0];
// Note: An event might have multiple locations. We're only displaying the first one.
GDataWhere *addr = [[event locations] objectAtIndex:0];
self.title = [[event title] stringValue];
if( when ){
if (![[when startTime] hasTime ])
self.allDay =[NSNumber numberWithBool:YES];
else
self.allDay =[NSNumber numberWithBool:NO];
self.startDate = [[when startTime] date];
self.endDate = [[when endTime] date];
}
self.eventid = [event iCalUID];
self.updated = [[event updatedDate] date];
self.note = [[event content] stringValue];
self.editLink = [[event editLink] href];
self.etag = [event ETag];
if( addr )
self.location = [addr stringValue];
self.calendar = calendar;
self.identifier = [event identifier];
//anEvent.calendar =
NSError *core_data_error = nil;
if (![context save:&core_data_error]) {
NSLog(@"Unresolved error saving a event %@, %@", core_data_error, [core_data_error userInfo]);
}
if (core_data_error) return NO;
return YES;
}
-(GDataEntryCalendarEvent *)eventGDataEntry{
GDataEntryCalendarEvent *newEntry = [GDataEntryCalendarEvent calendarEvent];
[newEntry setTitleWithString:self.title];
[newEntry addLocation:[GDataWhere whereWithString:self.location]];
if (self.startDate) {
GDataDateTime *startDate = [GDataDateTime dateTimeWithDate:self.startDate timeZone:[NSTimeZone systemTimeZone]];
GDataDateTime *endDate = [GDataDateTime dateTimeWithDate:self.endDate timeZone:[NSTimeZone systemTimeZone]];
[newEntry addTime:[GDataWhen whenWithStartTime:startDate endTime:endDate]];
}
[newEntry setContentWithString:self.note];
[newEntry setICalUID:self.eventid];
[newEntry setETag:self.etag];
return newEntry;
}
@end