forked from atg/Ingredients
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IGKAnnotationManager.m
146 lines (112 loc) · 3.35 KB
/
IGKAnnotationManager.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
//
// IKGAnnotationManager.m
// Ingredients
//
// Created by Jean-Nicolas Jolivet on 10-04-30.
// Written in 2010 by SilverCocoa.
//
#import "IGKAnnotationManager.h"
#import "IGKAnnotation.h"
const int IGKAnnotationVersion = 1;
@implementation IGKAnnotationManager
@synthesize annotations;
- (BOOL)loadAnnotations
{
NSString *loadPath = [[[[NSApp delegate] kitController] applicationSupportDirectory] stringByAppendingPathComponent:@"Annotations.plist"];
if([[NSFileManager defaultManager] fileExistsAtPath:loadPath])
{
[self loadAnnotationsAtPath:loadPath];
return YES;
}
return NO;
}
- (void)saveAnnotations
{
NSString *savePath = [[[[NSApp delegate] kitController] applicationSupportDirectory] stringByAppendingPathComponent:@"Annotations.plist"];
[self saveAnnotationsAtPath:savePath];
}
- (void)loadAnnotationsAtPath:(NSString *)path
{
[annotations removeAllObjects];
NSDictionary *annotationDic = [NSDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath:path]];
// Root object should be a dic..will allow for some meta data like version info etc..
// annotations should be an array whose keypath is dic.annotations
NSArray *annotationsFromDic = [annotationDic objectForKey:@"annotations"];
for(NSDictionary *anAnnotation in annotationsFromDic)
{
IGKAnnotation *newAnnotation = [[IGKAnnotation alloc] initWithDict:anAnnotation];
[annotations addObject:newAnnotation];
}
}
- (void)saveAnnotationsAtPath:(NSString *)path
{
NSMutableDictionary *saveDic = [[NSMutableDictionary alloc] init];
NSMutableArray *saveArray = [[NSMutableArray alloc] init];
[saveDic setObject:[NSNumber numberWithInt:IGKAnnotationVersion] forKey:@"version"];
for(IGKAnnotation *anAnnotation in annotations)
{
[saveArray addObject:[anAnnotation annotationAsDict]];
}
[saveDic setObject:saveArray forKey:@"annotations"];
[saveDic writeToURL:[NSURL fileURLWithPath:path] atomically:YES];
}
- (void)addAnnotation:(IGKAnnotation *)newAnnotation
{
[annotations addObject:newAnnotation];
[self saveAnnotations];
}
- (NSArray *)annotationsForURL:(NSString *)URL
{
//FIXME: This could be made faster by using a pregenerated dictionary of URLs -> sorted annotation arrays
NSMutableArray *subset = [[NSMutableArray alloc] init];
for (IGKAnnotation *a in annotations)
{
if ([[a docurl] isCaseInsensitiveEqual:URL])
{
[subset addObject:a];
}
}
return [subset sortedArrayUsingSelector:@selector(compare:)];
}
#pragma mark Singleton
static IGKAnnotationManager *sharedAnnotationManager = nil;
+ (IGKAnnotationManager *)sharedAnnotationManager
{
@synchronized(self) {
if (sharedAnnotationManager == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedAnnotationManager;
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedAnnotationManager == nil) {
sharedAnnotationManager = [super allocWithZone:zone];
return sharedAnnotationManager; // assignment and return on first allocation
}
}
return sharedAnnotationManager; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
@end