This repository was archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathReaderDocument.m
545 lines (391 loc) · 17.3 KB
/
ReaderDocument.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
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
540
541
542
543
544
545
//
// ReaderDocument.m
// Viewer v1.0.1
//
// Created by Julius Oklamcak on 2012-09-01.
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to
// do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "ReaderDocument.h"
#import "DocumentFolder.h"
#import "ReaderThumbCache.h"
#import "CGPDFDocument.h"
#import "AnnotationStore.h"
#import <fcntl.h>
@implementation ReaderDocument
{
NSMutableIndexSet *_bookmarks;
AnnotationStore *_annotations;
}
#pragma mark Constants
#define kReaderDocument @"ReaderDocument"
#pragma mark Properties
@dynamic guid;
@dynamic fileURL;
@dynamic fileName;
@dynamic filePath;
@dynamic password;
@dynamic pageCount;
@dynamic pageNumber;
@dynamic fileSize;
@dynamic fileDate;
@dynamic lastOpen;
@dynamic tagData;
@dynamic folder;
@dynamic bookmarks;
@synthesize isChecked;
#pragma mark ReaderDocument class methods
+ (NSString *)GUID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef theString = CFUUIDCreateString(NULL, theUUID);
NSString *unique = [NSString stringWithString:(__bridge id)theString];
CFRelease(theString); CFRelease(theUUID); // Cleanup CF objects
return unique;
}
+ (NSString *)applicationPath
{
static dispatch_once_t predicate = 0;
static NSString *applicationPath = nil; // Application path string
dispatch_once(&predicate, // Thread-safe create copy of the application path the first time it is needed
^{
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
applicationPath = [[[documentsPaths objectAtIndex:0] stringByDeletingLastPathComponent] copy]; // Strip "Documents"
});
return applicationPath;
}
+ (NSString *)relativeFilePath:(NSString *)fullFilePath
{
assert(fullFilePath != nil); // Ensure that the full file path is not nil
NSString *applicationPath = [ReaderDocument applicationPath]; // Get the application path
NSRange range = [fullFilePath rangeOfString:applicationPath]; // Look for the application path
assert(range.location != NSNotFound); // Ensure that the application path is in the full file path
return [fullFilePath stringByReplacingCharactersInRange:range withString:@""]; // Strip it out
}
#pragma mark ReaderDocument Core Data class methods
+ (NSArray *)allInMOC:(NSManagedObjectContext *)inMOC
{
assert(inMOC != nil); // Check parameter
NSFetchRequest *request = [NSFetchRequest new]; // Fetch request instance
[request setEntity:[NSEntityDescription entityForName:kReaderDocument inManagedObjectContext:inMOC]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"fileName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; // Sort order
[request setReturnsObjectsAsFaults:YES]; [request setFetchBatchSize:32]; // Optimize fetch
__autoreleasing NSError *error = nil; // Error information object
NSArray *objectList = [inMOC executeFetchRequest:request error:&error];
if (objectList == nil) { NSLog(@"%s %@", __FUNCTION__, error); assert(NO); }
return objectList;
}
+ (NSArray *)allInMOC:(NSManagedObjectContext *)inMOC withName:(NSString *)name
{
assert(inMOC != nil); assert(name != nil); // Check parameters
NSFetchRequest *request = [NSFetchRequest new]; // Fetch request instance
[request setEntity:[NSEntityDescription entityForName:kReaderDocument inManagedObjectContext:inMOC]];
[request setPredicate:[NSPredicate predicateWithFormat:@"fileName == %@", name]]; // Matching file name
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"fileName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; // Sort order
[request setReturnsObjectsAsFaults:YES]; [request setFetchBatchSize:32]; // Optimize fetch
__autoreleasing NSError *error = nil; // Error information object
NSArray *objectList = [inMOC executeFetchRequest:request error:&error];
if (objectList == nil) { NSLog(@"%s %@", __FUNCTION__, error); assert(NO); }
return objectList;
}
+ (NSArray *)allInMOC:(NSManagedObjectContext *)inMOC withFolder:(DocumentFolder *)object
{
assert(inMOC != nil); assert(object != nil); // Check parameters
NSPredicate *predicate = nil; NSSortDescriptor *sortDescriptor = nil;
switch ([object.type integerValue]) // Document folder type
{
case DocumentFolderTypeSamples:
case DocumentFolderTypeUser: // User folder type
{
predicate = [NSPredicate predicateWithFormat:@"folder == %@", object]; // Folder
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"fileName" ascending:YES];
break;
}
case DocumentFolderTypeDefault: // Default folder type
{
predicate = [NSPredicate predicateWithFormat:@"folder == %@", NULL]; // No folder
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"fileName" ascending:YES];
break;
}
case DocumentFolderTypeRecent: // Recent folder type
{
NSDate *since = [NSDate dateWithTimeIntervalSinceReferenceDate:0.0];
predicate = [NSPredicate predicateWithFormat:@"lastOpen > %@", since]; // Opened
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastOpen" ascending:NO];
break;
}
}
NSFetchRequest *request = [NSFetchRequest new]; // Fetch request instance
[request setEntity:[NSEntityDescription entityForName:kReaderDocument inManagedObjectContext:inMOC]];
[request setPredicate:predicate]; [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[request setReturnsObjectsAsFaults:YES]; [request setFetchBatchSize:32]; // Optimize fetch request
__autoreleasing NSError *error = nil; // Error information object
NSArray *objectList = [inMOC executeFetchRequest:request error:&error];
if (objectList == nil) { NSLog(@"%s %@", __FUNCTION__, error); assert(NO); }
return objectList;
}
+ (ReaderDocument *)insertInMOC:(NSManagedObjectContext *)inMOC name:(NSString *)name path:(NSString *)path
{
assert(inMOC != nil); assert(name != nil); assert(path != nil); // Check parameters
ReaderDocument *object = [NSEntityDescription insertNewObjectForEntityForName:kReaderDocument inManagedObjectContext:inMOC];
if ((object != nil) && ([object isMemberOfClass:[ReaderDocument class]])) // We have a valid ReaderDocument object
{
object.fileName = name; // Document file name
object.guid = [ReaderDocument GUID]; // Document GUID
object.pageNumber = [NSNumber numberWithInteger:1]; // Start on page 1
object.filePath = [ReaderDocument relativeFilePath:path]; // Relative path to file
object.lastOpen = [NSDate dateWithTimeIntervalSinceReferenceDate:0.0]; // Last opened
object.fileDate = [NSDate dateWithTimeIntervalSinceReferenceDate:0.0]; // File date
object.fileSize = [NSNumber numberWithUnsignedLongLong:0ull]; // File size
}
return object;
}
+ (void)renameInMOC:(NSManagedObjectContext *)inMOC object:(ReaderDocument *)object name:(NSString *)string
{
assert(inMOC != nil); assert(object != nil); assert(string != nil); // Check parameters
NSString *applicationPath = [ReaderDocument applicationPath]; // Application path
NSString *fullPath = [applicationPath stringByAppendingPathComponent:object.filePath];
NSString *oldFilePath = [fullPath stringByAppendingPathComponent:object.fileName];
NSString *newFilePath = [fullPath stringByAppendingPathComponent:string];
__autoreleasing NSError *error = nil; // Error information object
NSFileManager *fileManager = [NSFileManager new]; // File manager instance
BOOL status = [fileManager moveItemAtPath:oldFilePath toPath:newFilePath error:&error];
if (status == YES) // Check rename status
{
object.fileURL = nil; // Clear file URL
object.fileName = string; // New file name
if ([inMOC hasChanges] == YES) // Save changes
{
if ([inMOC save:&error] == YES) // Did save changes
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[object objectID] forKey:ReaderDocumentNotificationObjectID];
[notificationCenter postNotificationName:ReaderDocumentRenamedNotification object:nil userInfo:userInfo];
}
else // Log any errors
{
NSLog(@"%s %@", __FUNCTION__, error); assert(NO);
}
}
}
else // Rename failed
{
NSLog(@"%s %@", __FUNCTION__, error);
}
}
+ (void)deleteInMOC:(NSManagedObjectContext *)inMOC object:(ReaderDocument *)object fm:(NSFileManager *)fm
{
assert(inMOC != nil); assert(object != nil); assert(fm != nil); // Check parameters
[ReaderThumbCache removeThumbCacheWithGUID:object.guid]; // Delete the thumb cache
[fm removeItemAtURL:object.fileURL error:NULL]; // Delete the document file
[inMOC deleteObject:object]; // Delete the object
}
+ (BOOL)existsInMOC:(NSManagedObjectContext *)inMOC name:(NSString *)string
{
assert(inMOC != nil); assert(string != nil); // Check parameters
NSFetchRequest *request = [NSFetchRequest new]; // Fetch request instance
[request setEntity:[NSEntityDescription entityForName:kReaderDocument inManagedObjectContext:inMOC]];
[request setPredicate:[NSPredicate predicateWithFormat:@"fileName == %@", string]]; // Name predicate
__autoreleasing NSError *error = nil; // Error information object
NSUInteger count = [inMOC countForFetchRequest:request error:&error];
if (error != nil) { NSLog(@"%s %@", __FUNCTION__, error); assert(NO); }
return ((count > 0) ? YES : NO);
}
#pragma mark ReaderDocument Core Data instance methods
- (NSURL *)fileURL
{
[self willAccessValueForKey:@"fileURL"];
NSURL *theURL = [self primitiveFileURL];
[self didAccessValueForKey:@"fileURL"];
if (theURL == nil) // Create the file URL when needed
{
NSString *applicationPath = [ReaderDocument applicationPath]; // Application path
NSString *fullPath = [applicationPath stringByAppendingPathComponent:self.filePath];
theURL = [NSURL fileURLWithPath:[fullPath stringByAppendingPathComponent:self.fileName]];
[self setPrimitiveFileURL:theURL]; // Store the file URL for later use
}
return theURL;
}
- (void)setFileURL:(NSURL *)theURL
{
[self willChangeValueForKey:@"fileURL"];
[self setPrimitiveValue:theURL forKey:@"fileURL"];
[self didChangeValueForKey:@"fileURL"];
}
- (NSMutableIndexSet *)bookmarks
{
if (_bookmarks == nil) // Create on first access
{
if (self.tagData != nil) // Unarchive tag (bookmarks) data
{
NSIndexSet *set = [NSKeyedUnarchiver unarchiveObjectWithData:self.tagData];
if ((set != nil) && [set isKindOfClass:[NSIndexSet class]]) // Validate
{
_bookmarks = [set mutableCopy]; // Mutable copy of index set
}
}
if (_bookmarks == nil) // Create bookmarks set
{
_bookmarks = [NSMutableIndexSet new];
}
}
return _bookmarks;
}
- (void)updateProperties
{
CFURLRef docURLRef = (__bridge CFURLRef)self.fileURL; // File URL
CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateWithURL(docURLRef);
if (thePDFDocRef != NULL) // Get the number of pages in the document
{
NSInteger pageCount = CGPDFDocumentGetNumberOfPages(thePDFDocRef);
_annotations = [[AnnotationStore alloc] initWithPageCount:pageCount];
self.pageCount = [NSNumber numberWithInteger:pageCount];
CGPDFDocumentRelease(thePDFDocRef); // Cleanup
}
NSString *fullFilePath = [self.fileURL path]; // Full file path
NSFileManager *fileManager = [NSFileManager new]; // File manager instance
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:fullFilePath error:NULL];
self.fileDate = [fileAttributes objectForKey:NSFileModificationDate]; // File date
self.fileSize = [fileAttributes objectForKey:NSFileSize]; // File size
}
- (void)saveReaderDocument
{
if (_bookmarks != nil) // Archive bookmarks (tag) data
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_bookmarks];
if ([self.tagData isEqualToData:data] == NO) self.tagData = data;
}
NSManagedObjectContext *saveMOC = self.managedObjectContext;
if (saveMOC != nil) // Save managed object context
{
if ([saveMOC hasChanges] == YES) // Save changes
{
__autoreleasing NSError *error = nil; // Error information object
if ([saveMOC save:&error] == NO) // Log any errors
{
NSLog(@"%s %@", __FUNCTION__, error); assert(NO);
}
}
}
}
- (void)saveReaderDocumentWithAnnotations {
NSURL *annotatedDocURL = [ReaderDocument urlForAnnotatedDocument:self];
[[NSFileManager defaultManager] replaceItemAtURL:self.fileURL withItemAtURL:annotatedDocURL backupItemName:nil options:0 resultingItemURL:nil error:nil];
[self saveReaderDocument];
}
- (BOOL)fileExistsAndValid
{
BOOL state = NO; // Status
if (self.isDeleted == NO) // Not deleted
{
NSString *filePath = [self.fileURL path]; // Path
const char *path = [filePath fileSystemRepresentation];
int fd = open(path, O_RDONLY); // Open the file
if (fd > 0) // We have a valid file descriptor
{
const char sig[1024]; // File signature buffer
ssize_t len = read(fd, (void *)&sig, sizeof(sig));
state = (strnstr(sig, "%PDF", len) != NULL);
close(fd); // Close the file
}
}
return state;
}
- (void)willTurnIntoFault
{
_bookmarks = nil; self.isChecked = NO;
}
#pragma mark Annotations code
- (AnnotationStore*) annotations {
if (!_annotations) {
_annotations = [[AnnotationStore alloc] initWithPageCount:[self.pageCount intValue]];
}
return _annotations;
}
+ (NSURL*) urlForAnnotatedDocument:(ReaderDocument *)document
{
CGPDFDocumentRef doc = CGPDFDocumentCreateX((__bridge CFURLRef)document.fileURL, document.password);
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingString:@"annotated.pdf"];
//CGRectZero means the default page size is 8.5x11
//We don't care about the default anyway, because we set each page to be a specific size
UIGraphicsBeginPDFContextToFile(tempPath, CGRectZero, nil);
//Iterate over each page - 1-based indexing (obnoxious...)
int pages = [document.pageCount intValue];
for (int i = 1; i <= pages; i++) {
CGPDFPageRef page = CGPDFDocumentGetPage (doc, i); // grab page i of the PDF
CGRect bounds = [ReaderDocument boundsForPDFPage:page];
//Create a new page
UIGraphicsBeginPDFPageWithInfo(bounds, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
// flip context so page is right way up
CGContextTranslateCTM(context, 0, bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawPDFPage (context, page); // draw the page into graphics context
//Annotations
NSArray *annotations = [document.annotations annotationsForPage:i];
if (annotations) {
NSLog(@"Writing %d annotations", [annotations count]);
//Flip back right-side up
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0, -bounds.size.height);
for (Annotation *anno in annotations) {
[anno drawInContext:context];
}
}
}
UIGraphicsEndPDFContext();
CGPDFDocumentRelease (doc);
return [NSURL fileURLWithPath:tempPath];
}
+ (CGRect) boundsForPDFPage:(CGPDFPageRef) page{
CGRect cropBoxRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
CGRect mediaBoxRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
int pageAngle = CGPDFPageGetRotationAngle(page); // Angle
float pageWidth, pageHeight, pageOffsetX, pageOffsetY;
switch (pageAngle) // Page rotation angle (in degrees)
{
default: // Default case
case 0: case 180: // 0 and 180 degrees
{
pageWidth = effectiveRect.size.width;
pageHeight = effectiveRect.size.height;
pageOffsetX = effectiveRect.origin.x;
pageOffsetY = effectiveRect.origin.y;
break;
}
case 90: case 270: // 90 and 270 degrees
{
pageWidth = effectiveRect.size.height;
pageHeight = effectiveRect.size.width;
pageOffsetX = effectiveRect.origin.y;
pageOffsetY = effectiveRect.origin.x;
break;
}
}
return CGRectMake(pageOffsetX, pageOffsetY, pageWidth, pageHeight);
}
#pragma mark Notification name strings
//NSString *const ReaderDocumentAddedNotification = @"ReaderDocumentAddedNotification";
NSString *const ReaderDocumentRenamedNotification = @"ReaderDocumentRenamedNotification";
//NSString *const ReaderDocumentDeletedNotification = @"ReaderDocumentDeletedNotification";
NSString *const ReaderDocumentNotificationObjectID = @"ReaderDocumentNotificationObjectID";
@end