-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLLibraryHost.m
207 lines (161 loc) · 5.73 KB
/
TLLibraryHost.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// TLLibraryHost.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 12/13/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "TLLibraryHost.h"
#import "TLMercatalogLibrary.h"
#import "TLPhoto.h"
#import "TLTrack.h"
@interface TLLibraryHost ()
@property (nonatomic, retain) NSArray* cachedTracks;
@property (nonatomic, retain) NSArray* cachedPhotos;
@end
@implementation TLLibraryHost
#pragma mark Class lifecycle
static NSString* const TLLibraryHostHostsKey = @"TLLibraryHost_Hosts";
+ (void)registerLibraryHost:(TLLibraryHost*)libraryHost
forContext:(NSManagedObjectContext*)aModelContext
{
NSMutableDictionary* perThread = [[NSThread currentThread] threadDictionary];
NSMapTable* threadHosts = [perThread objectForKey:TLLibraryHostHostsKey];
if (!threadHosts) {
threadHosts = [NSMapTable mapTableWithStrongToStrongObjects];
[perThread setObject:threadHosts forKey:TLLibraryHostHostsKey];
}
if (libraryHost) {
[threadHosts setObject:libraryHost forKey:aModelContext];
}
else {
[threadHosts removeObjectForKey:aModelContext];
}
}
+ (TLLibraryHost*)libraryHostForContext:(NSManagedObjectContext*)aModelContext {
NSMutableDictionary* perThread = [[NSThread currentThread] threadDictionary];
NSMapTable* threadHosts = [perThread objectForKey:TLLibraryHostHostsKey];
return [threadHosts objectForKey:aModelContext];
}
#pragma mark Lifecycle
- (void)registerNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(modelChanged:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:[self managedObjectContext]];
}
- (void)awakeFromInsert {
[self registerNotifications];
}
- (void)awakeFromFetch {
[self registerNotifications];
}
- (void)didTurnIntoFault {
[self setCachedTracks:nil];
[self setCachedPhotos:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
#pragma mark Accessors
@synthesize library;
@synthesize cachedTracks;
@synthesize cachedPhotos;
- (NSManagedObjectContext*)modelContext {
return [self managedObjectContext];
}
- (void)setHomeBase:(TLLocation*)newHomeBase {
TLLocation* homeBaseCopy = [newHomeBase copy];
[self setModelHomeBase:homeBaseCopy];
[homeBaseCopy release];
}
- (TLLocation*)homeBase {
return [self modelHomeBase];
}
- (void)setAlwaysShowTimeline:(BOOL)newAlwaysShowTimeline {
[self setModelAlwaysShowTimeline:[NSNumber numberWithBool:newAlwaysShowTimeline]];
}
- (BOOL)alwaysShowTimeline {
return [[self modelAlwaysShowTimeline] boolValue];
}
- (void)setAlwaysShowTracks:(BOOL)newAlwaysShowTracks {
[self setModelAlwaysShowTracks:[NSNumber numberWithBool:newAlwaysShowTracks]];
}
- (BOOL)alwaysShowTracks {
return [[self modelAlwaysShowTracks] boolValue];
}
#pragma mark Photo and thumbnail helpers
- (int64_t)makePhotoID {
int64_t photoId = [[self nextPhotoID] longLongValue];
NSAssert(photoId < LLONG_MAX,
@"Maximum photoId exceeded. Check for porcine precipitation");
[self setNextPhotoID:[NSNumber numberWithLongLong:(photoId + 1)]];
return photoId;
}
- (NSString*)photoOriginalsFolder {
static NSString* const originalsFolder = @"Originals";
[[self library] ensureDirectoryExists:originalsFolder];
return [[[self library] currentBundlePath]
stringByAppendingPathComponent:originalsFolder];
}
- (NSURL*)photoOriginalCopyLocation:(int64_t)uniqueID {
NSString* base = [self photoOriginalsFolder];
NSString* fileName = [NSString stringWithFormat:@"%lli.JPG", (long long)uniqueID];
NSString* fullPath = [base stringByAppendingPathComponent:fileName];
return [NSURL fileURLWithPath:fullPath isDirectory:NO];
}
- (NSURL*)photoThumbnailLocation:(int64_t)uniqueID forSize:(NSString*)sizeName {
static NSString* const thumbnailsBase = @"Cached Thumbnails";
NSString* thumbnailFolder = [thumbnailsBase stringByAppendingPathComponent:sizeName];
[[self library] ensureDirectoryExists:thumbnailFolder];
NSString* fullDirectory = [[[self library] currentBundlePath]
stringByAppendingPathComponent:thumbnailFolder];
NSString* photoFilename = [NSString stringWithFormat:@"%lli.jpg", (long long)uniqueID];
NSString* fullPath = [fullDirectory stringByAppendingPathComponent:photoFilename];
return [NSURL fileURLWithPath:fullPath isDirectory:NO];
}
#pragma mark Model object fetching
- (void)modelChanged:(NSNotification*)notification {
(void)notification;
/*
printf("modelChanged (%s): %s\n\n",
[[NSThread currentThread] isMainThread] ? "*" : "_",
[[[notification userInfo] description] UTF8String]);
*/
[self setCachedPhotos:nil];
[self setCachedTracks:nil];
}
- (NSArray*)visiblePhotos {
if (![self cachedPhotos]) {
NSFetchRequest* request = [[NSFetchRequest new] autorelease];
NSEntityDescription* entity = [NSEntityDescription entityForName:[TLPhoto entityName]
inManagedObjectContext:[self modelContext]];
[request setEntity:entity];
NSArray* visiblePhotos = [[self modelContext] executeFetchRequest:request error:NULL];
[self setCachedPhotos:visiblePhotos];
}
return [self cachedPhotos];
}
- (NSArray*)evidencePhotos {
NSArray* allPhotos = [self visiblePhotos];
NSMutableArray* evidencePhotos = [NSMutableArray array];
for (TLPhoto* photo in allPhotos) {
if ([photo isLocked]) {
[evidencePhotos addObject:photo];
}
}
return evidencePhotos;
}
- (NSArray*)allTracks {
if (![self cachedTracks]) {
NSFetchRequest* request = [[NSFetchRequest new] autorelease];
NSEntityDescription* entity = [NSEntityDescription entityForName:[TLTrack entityName]
inManagedObjectContext:[self modelContext]];
[request setEntity:entity];
NSArray* allTracks = [[self modelContext] executeFetchRequest:request error:NULL];
[self setCachedTracks:allTracks];
}
return [self cachedTracks];
}
@end