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
//
// ReaderDocument.m
// Reader v2.6.1
//
// Created by Julius Oklamcak on 2011-07-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 "CGPDFDocument.h"
#import <fcntl.h>
@implementation ReaderDocument
{
NSString *_guid;
NSDate *_fileDate;
NSDate *_lastOpen;
NSNumber *_fileSize;
NSNumber *_pageCount;
NSNumber *_pageNumber;
NSMutableIndexSet *_bookmarks;
NSString *_filePath;
NSString *_password;
NSURL *_fileURL;
}
#pragma mark Properties
@synthesize guid = _guid;
@synthesize fileDate = _fileDate;
@synthesize fileSize = _fileSize;
@synthesize pageCount = _pageCount;
@synthesize pageNumber = _pageNumber;
@synthesize bookmarks = _bookmarks;
@synthesize lastOpen = _lastOpen;
@synthesize password = _password;
@synthesize filePath = _filePath;
@dynamic fileURL;
#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 *)documentsPath
{
NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [documentsPaths objectAtIndex:0]; // Path to the application's "~/Documents" directory
}
+ (NSString *)applicationPath
{
return [[ReaderDocument documentsPath] stringByDeletingLastPathComponent]; // Strip "Documents" component
}
+ (NSString *)applicationSupportPath
{
NSFileManager *fileManager = [NSFileManager new]; // File manager instance
NSURL *pathURL = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
return [pathURL path]; // Path to the application's "~/Library/Application Support" directory
}
+ (NSString *)archiveFilePath:(NSString *)filename
{
assert(filename != nil); // Ensure that the archive file name is not nil
//NSString *archivePath = [ReaderDocument documentsPath]; // Application's "~/Documents" path
NSString *archivePath = [ReaderDocument applicationSupportPath]; // Application's "~/Library/Application Support" path
NSString *archiveName = [[filename stringByDeletingPathExtension] stringByAppendingPathExtension:@"plist"];
return [archivePath stringByAppendingPathComponent:archiveName]; // "{archivePath}/'filename'.plist"
}
+ (ReaderDocument *)unarchiveFromFileName:(NSString *)filename password:(NSString *)phrase
{
ReaderDocument *document = nil; // ReaderDocument object
NSString *withName = [filename lastPathComponent]; // File name only
NSString *archiveFilePath = [ReaderDocument archiveFilePath:withName];
@try // Unarchive an archived ReaderDocument object from its property list
{
document = [NSKeyedUnarchiver unarchiveObjectWithFile:archiveFilePath];
if ((document != nil) && (phrase != nil)) // Set the document password
{
[document setValue:[phrase copy] forKey:@"password"];
}
if ((document != nil) && ![[NSFileManager defaultManager] fileExistsAtPath:document.fileURL.path]) {
document = nil;
}
}
@catch (NSException *exception) // Exception handling (just in case O_o)
{
#ifdef DEBUG
NSLog(@"%s Caught %@: %@", __FUNCTION__, [exception name], [exception reason]);
#endif
}
return document;
}
+ (ReaderDocument *)withDocumentFilePath:(NSString *)filePath password:(NSString *)phrase
{
ReaderDocument *document = nil; // ReaderDocument object
document = [ReaderDocument unarchiveFromFileName:filePath password:phrase];
if (document == nil) // Unarchive failed so we create a new ReaderDocument object
{
document = [[ReaderDocument alloc] initWithFilePath:filePath password:phrase];
}
return document;
}
+ (BOOL)isPDF:(NSString *)filePath
{
BOOL state = NO;
if (filePath != nil) // Must have a file 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;
}
#pragma mark ReaderDocument instance methods
- (id)initWithFilePath:(NSString *)fullFilePath password:(NSString *)phrase
{
id object = nil; // ReaderDocument object
if ([ReaderDocument isPDF:fullFilePath] == YES) // File must exist
{
if ((self = [super init])) // Initialize superclass object first
{
_guid = [ReaderDocument GUID]; // Create a document GUID
_password = [phrase copy]; // Keep copy of any document password
_bookmarks = [NSMutableIndexSet new]; // Bookmarked pages index set
_pageNumber = [NSNumber numberWithInteger:1]; // Start on page 1
_filePath = fullFilePath; // File path
CFURLRef docURLRef = (__bridge CFURLRef)[self fileURL]; // CFURLRef from NSURL
CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateX(docURLRef, _password);
if (thePDFDocRef != NULL) // Get the number of pages in the document
{
NSInteger pageCount = CGPDFDocumentGetNumberOfPages(thePDFDocRef);
_pageCount = [NSNumber numberWithInteger:pageCount];
CGPDFDocumentRelease(thePDFDocRef); // Cleanup
}
else // Cupertino, we have a problem with the document
{
NSAssert(NO, @"CGPDFDocumentRef == NULL");
}
NSFileManager *fileManager = [NSFileManager new]; // File manager instance
_lastOpen = [NSDate dateWithTimeIntervalSinceReferenceDate:0.0]; // Last opened
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:fullFilePath error:NULL];
_fileDate = [fileAttributes objectForKey:NSFileModificationDate]; // File date
_fileSize = [fileAttributes objectForKey:NSFileSize]; // File size (bytes)
[self saveReaderDocument]; // Save the ReaderDocument object
object = self; // Return initialized ReaderDocument object
}
}
return object;
}
- (NSString *)fileName
{
return [_filePath lastPathComponent];
}
- (NSURL *)fileURL
{
if (_fileURL == nil) // Create and keep the file URL the first time it is requested
{
_fileURL = [[NSURL alloc] initFileURLWithPath:self.filePath isDirectory:NO]; // File URL from full file path
}
return _fileURL;
}
- (BOOL)archiveWithFileName:(NSString *)filename
{
NSString *archiveFilePath = [ReaderDocument archiveFilePath:filename];
return [NSKeyedArchiver archiveRootObject:self toFile:archiveFilePath];
}
- (void)saveReaderDocument
{
[self archiveWithFileName:[self fileName]];
}
- (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);
_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];
_fileDate = [fileAttributes objectForKey:NSFileModificationDate]; // File date
_fileSize = [fileAttributes objectForKey:NSFileSize]; // File size
}
#pragma mark NSCoding protocol methods
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:_guid forKey:@"FileGUID"];
[encoder encodeObject:_filePath forKey:@"FilePath"];
[encoder encodeObject:_fileDate forKey:@"FileDate"];
[encoder encodeObject:[NSNumber numberWithInteger:0] forKey:@"PageCount"];
[encoder encodeObject:_pageNumber forKey:@"PageNumber"];
[encoder encodeObject:_bookmarks forKey:@"Bookmarks"];
[encoder encodeObject:_fileSize forKey:@"FileSize"];
[encoder encodeObject:_lastOpen forKey:@"LastOpen"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
if ((self = [super init])) // Superclass init
{
_guid = [decoder decodeObjectForKey:@"FileGUID"];
_filePath = [decoder decodeObjectForKey:@"FilePath"];
if (_filePath == nil) {
_filePath = [decoder decodeObjectForKey:@"FileName"];
}
_fileDate = [decoder decodeObjectForKey:@"FileDate"];
_pageCount = [decoder decodeObjectForKey:@"PageCount"];
_pageNumber = [decoder decodeObjectForKey:@"PageNumber"];
_bookmarks = [decoder decodeObjectForKey:@"Bookmarks"];
_fileSize = [decoder decodeObjectForKey:@"FileSize"];
_lastOpen = [decoder decodeObjectForKey:@"LastOpen"];
if (_guid == nil) _guid = [ReaderDocument GUID];
if (_bookmarks != nil)
_bookmarks = [_bookmarks mutableCopy];
else
_bookmarks = [NSMutableIndexSet new];
}
return self;
}
@end