forked from vfr/Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderDocument.m
More file actions
356 lines (248 loc) · 9.61 KB
/
ReaderDocument.m
File metadata and controls
356 lines (248 loc) · 9.61 KB
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
//
// ReaderDocument.m
// Reader v2.8.6
//
// Created by Julius Oklamcak on 2011-07-01.
// Copyright © 2011-2015 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>
@interface ReaderDocument ()
@property (nonatomic, strong, readwrite) NSString *password;
@property (nonatomic, strong, readwrite) NSString *filePath;
@end
@implementation ReaderDocument
{
NSString *_guid;
NSDate *_fileDate;
NSDate *_lastOpen;
NSNumber *_fileSize;
NSNumber *_pageCount;
NSNumber *_pageNumber;
NSMutableIndexSet *_bookmarks;
NSString *_password;
NSString *_fileName;
NSString *_filePath;
NSURL *_fileURL;
}
#pragma mark - Properties
@synthesize guid = _guid;
@synthesize fileDate = _fileDate;
@synthesize lastOpen = _lastOpen;
@synthesize fileSize = _fileSize;
@synthesize pageCount = _pageCount;
@synthesize pageNumber = _pageNumber;
@synthesize bookmarks = _bookmarks;
@synthesize password = _password;
@synthesize filePath = _filePath;
@dynamic fileName, fileURL;
@dynamic canEmail, canExport, canPrint;
#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
{
NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
NSURL *pathURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
return [pathURL path]; // Path to the application's "~/Documents" directory
}
+ (NSString *)applicationSupportPath
{
NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
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
{
NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
NSString *applicationSupportPath = [ReaderDocument applicationSupportPath]; // See above
NSString *archivePath = [applicationSupportPath stringByAppendingPathComponent:@"Reader Metadata"];
[fileManager createDirectoryAtPath:archivePath withIntermediateDirectories:NO attributes:nil error:NULL];
NSString *archiveName = [[fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"plist"];
return [archivePath stringByAppendingPathComponent:archiveName]; // "{archivePath}/'fileName'.plist"
}
+ (ReaderDocument *)unarchiveFromFileName:(NSString *)filePath password:(NSString *)phrase
{
ReaderDocument *document = nil; // ReaderDocument object
NSString *fileName = [filePath lastPathComponent]; // File name only
NSString *archiveFilePath = [ReaderDocument archiveFilePath:fileName];
@try // Unarchive an archived ReaderDocument object from its property list
{
document = [NSKeyedUnarchiver unarchiveObjectWithFile:archiveFilePath];
if (document != nil) // Set the document's file path and password properties
{
document.filePath = [filePath copy]; document.password = [phrase copy];
}
}
@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 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
- (instancetype)initWithFilePath:(NSString *)filePath password:(NSString *)phrase
{
if ((self = [super init])) // Initialize superclass first
{
if ([ReaderDocument isPDF:filePath] == YES) // Valid PDF
{
_guid = [ReaderDocument GUID]; // Create document's GUID
_password = [phrase copy]; // Keep copy of document password
_filePath = [filePath copy]; // Keep copy of document file path
_pageNumber = [NSNumber numberWithInteger:1]; // Start on page one
_bookmarks = [NSMutableIndexSet new]; // Bookmarked pages index set
CFURLRef docURLRef = (__bridge CFURLRef)[self fileURL]; // CFURLRef from NSURL
CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateUsingUrl(docURLRef, _password);
if (thePDFDocRef != NULL) // Get the total 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
{
NSLog(@"pdfReader::CGPDFDocumentRef == NULL");
return nil;
//NSAssert(NO, @"CGPDFDocumentRef == NULL");
}
_lastOpen = [NSDate dateWithTimeIntervalSinceReferenceDate:0.0];
NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:_filePath error:NULL];
_fileDate = [fileAttributes objectForKey:NSFileModificationDate]; // File date
_fileSize = [fileAttributes objectForKey:NSFileSize]; // File size (bytes)
[self archiveDocumentProperties]; // Archive ReaderDocument object
}
else // Not a valid PDF file
{
self = nil;
}
}
return self;
}
- (NSString *)fileName
{
if (_fileName == nil) _fileName = [_filePath lastPathComponent];
return _fileName;
}
- (NSURL *)fileURL
{
if (_fileURL == nil) _fileURL = [[NSURL alloc] initFileURLWithPath:_filePath isDirectory:NO];
return _fileURL;
}
- (BOOL)canEmail
{
return YES;
}
- (BOOL)canExport
{
return YES;
}
- (BOOL)canPrint
{
return YES;
}
- (BOOL)archiveDocumentProperties
{
NSString *archiveFilePath = [ReaderDocument archiveFilePath:[self fileName]];
return [NSKeyedArchiver archiveRootObject:self toFile:archiveFilePath];
}
- (void)updateDocumentProperties
{
CFURLRef docURLRef = (__bridge CFURLRef)[self fileURL]; // CFURLRef from NSURL
CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateUsingUrl(docURLRef, _password);
if (thePDFDocRef != NULL) // Get the total number of pages in the document
{
NSInteger pageCount = CGPDFDocumentGetNumberOfPages(thePDFDocRef);
_pageCount = [NSNumber numberWithInteger:pageCount];
CGPDFDocumentRelease(thePDFDocRef); // Cleanup
}
NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:_filePath error:NULL];
_fileDate = [fileAttributes objectForKey:NSFileModificationDate]; // File date
_fileSize = [fileAttributes objectForKey:NSFileSize]; // File size (bytes)
}
#pragma mark - NSCoding protocol methods
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:_guid forKey:@"FileGUID"];
[encoder encodeObject:_fileDate forKey:@"FileDate"];
[encoder encodeObject:_pageCount forKey:@"PageCount"];
[encoder encodeObject:_pageNumber forKey:@"PageNumber"];
[encoder encodeObject:_bookmarks forKey:@"Bookmarks"];
[encoder encodeObject:_fileSize forKey:@"FileSize"];
[encoder encodeObject:_lastOpen forKey:@"LastOpen"];
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
if ((self = [super init])) // Superclass init
{
_guid = [decoder decodeObjectForKey:@"FileGUID"];
_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