-
Notifications
You must be signed in to change notification settings - Fork 3
/
Calendar.m
133 lines (101 loc) · 4.35 KB
/
Calendar.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
/*
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 "Calendar.h"
#import "Event.h"
@implementation Calendar
@dynamic edit_permission;
@dynamic updated;
@dynamic name;
@dynamic calid;
@dynamic link;
@dynamic color;
@dynamic has_many_events;
+(Calendar *)getCalendarWithId:(NSString *)calId andContext:(NSManagedObjectContext *) context{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Calendar" inManagedObjectContext:context];
[request setEntity:entity];
// retrive the objects with a given value for a certain property
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"calid == %@", calId];
[request setPredicate:predicate];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"calid" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:@"RootCalendar"];
aFetchedResultsController.delegate = self;
NSError *error = nil;
NSArray *result = [context executeFetchRequest:request error:&error];
[request release];
[sortDescriptor release];
[sortDescriptors release];
aFetchedResultsController.delegate = nil;
[aFetchedResultsController release];
if (error) return nil;
if(result != nil && [result count] == 1)
return (Calendar *)[result objectAtIndex:0];
return nil;
}
+(Calendar *)createCalendarFromGCal:(GDataEntryCalendar *)calendar withContext:(NSManagedObjectContext *)context {
Calendar *aCalendar;
aCalendar = [NSEntityDescription insertNewObjectForEntityForName:@"Calendar" inManagedObjectContext:context];
aCalendar.name = [[calendar title] stringValue];
aCalendar.color = [[calendar color] stringValue];
aCalendar.link = [[[calendar alternateLink] URL] relativeString];
aCalendar.calid = [calendar identifier];
aCalendar.updated = [[calendar updatedDate] date];
//NSLog(@"permiso de edicion a la creacion %d, %@", [calendar canEdit], );
NSNumber *permission;
NSString *accessLevel = [[calendar accessLevel] stringValue];
if ( [accessLevel isEqualToString:@"owner"] || [accessLevel isEqualToString:@"editor"])
permission= [NSNumber numberWithInt:1];
else
permission= [NSNumber numberWithInt:0];
aCalendar.edit_permission = permission;
NSError *core_data_error = nil;
if (![context save:&core_data_error]) {
NSLog(@"Unresolved error saving a calenadar%@, %@", core_data_error, [core_data_error userInfo]);
}
return aCalendar;
}
-(BOOL)updateCalendarFromGCal:(GDataEntryCalendar *)calendar withContext:(NSManagedObjectContext *)context {
self.name = [[calendar title] stringValue];
self.color = [[calendar color] stringValue];
self.calid = [calendar identifier];
self.link = [[[calendar alternateLink] URL] relativeString];
self.updated = [[calendar updatedDate] date];
NSNumber *permission;
NSString *accessLevel = [[calendar accessLevel] stringValue];
if ( [accessLevel isEqualToString:@"owner"] || [accessLevel isEqualToString:@"editor"])
permission= [NSNumber numberWithInt:1];
else
permission= [NSNumber numberWithInt:0];
self.edit_permission = permission;
NSError *core_data_error = nil;
if (![context save:&core_data_error]) {
NSLog(@"Unresolved error saving a calenadar%@, %@", core_data_error, [core_data_error userInfo]);
}
if (core_data_error) return NO;
else return YES;
}
-(NSString *)ownSectionSeparator{
if ( [self.edit_permission boolValue])
return @"1";
else
return @"2";
}
@end