ReaderDocument.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // ReaderDocument.m
  3. // Reader v2.8.6
  4. //
  5. // Created by Julius Oklamcak on 2011-07-01.
  6. // Copyright © 2011-2015 Julius Oklamcak. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to
  13. // do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #import "ReaderDocument.h"
  26. #import "CGPDFDocument.h"
  27. #import <fcntl.h>
  28. @interface ReaderDocument ()
  29. @property (nonatomic, strong, readwrite) NSString *password;
  30. @property (nonatomic, strong, readwrite) NSString *filePath;
  31. @end
  32. @implementation ReaderDocument
  33. {
  34. NSString *_guid;
  35. NSDate *_fileDate;
  36. NSDate *_lastOpen;
  37. NSNumber *_fileSize;
  38. NSNumber *_pageCount;
  39. NSNumber *_pageNumber;
  40. NSMutableIndexSet *_bookmarks;
  41. NSString *_password;
  42. NSString *_fileName;
  43. NSString *_filePath;
  44. NSURL *_fileURL;
  45. }
  46. #pragma mark - Properties
  47. @synthesize guid = _guid;
  48. @synthesize fileDate = _fileDate;
  49. @synthesize lastOpen = _lastOpen;
  50. @synthesize fileSize = _fileSize;
  51. @synthesize pageCount = _pageCount;
  52. @synthesize pageNumber = _pageNumber;
  53. @synthesize bookmarks = _bookmarks;
  54. @synthesize password = _password;
  55. @synthesize filePath = _filePath;
  56. @dynamic fileName, fileURL;
  57. @dynamic canEmail, canExport, canPrint;
  58. #pragma mark - ReaderDocument class methods
  59. + (NSString *)GUID
  60. {
  61. CFUUIDRef theUUID = CFUUIDCreate(NULL);
  62. CFStringRef theString = CFUUIDCreateString(NULL, theUUID);
  63. NSString *unique = [NSString stringWithString:(__bridge id)theString];
  64. CFRelease(theString); CFRelease(theUUID); // Cleanup CF objects
  65. return unique;
  66. }
  67. + (NSString *)documentsPath
  68. {
  69. NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
  70. NSURL *pathURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
  71. return [pathURL path]; // Path to the application's "~/Documents" directory
  72. }
  73. + (NSString *)applicationSupportPath
  74. {
  75. NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
  76. NSURL *pathURL = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
  77. return [pathURL path]; // Path to the application's "~/Library/Application Support" directory
  78. }
  79. + (NSString *)archiveFilePath:(NSString *)fileName
  80. {
  81. NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
  82. NSString *applicationSupportPath = [ReaderDocument applicationSupportPath]; // See above
  83. NSString *archivePath = [applicationSupportPath stringByAppendingPathComponent:@"Reader Metadata"];
  84. [fileManager createDirectoryAtPath:archivePath withIntermediateDirectories:NO attributes:nil error:NULL];
  85. NSString *archiveName = [[fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"plist"];
  86. return [archivePath stringByAppendingPathComponent:archiveName]; // "{archivePath}/'fileName'.plist"
  87. }
  88. + (ReaderDocument *)unarchiveFromFileName:(NSString *)filePath password:(NSString *)phrase
  89. {
  90. ReaderDocument *document = nil; // ReaderDocument object
  91. NSString *fileName = [filePath lastPathComponent]; // File name only
  92. NSString *archiveFilePath = [ReaderDocument archiveFilePath:fileName];
  93. @try // Unarchive an archived ReaderDocument object from its property list
  94. {
  95. document = [NSKeyedUnarchiver unarchiveObjectWithFile:archiveFilePath];
  96. if (document != nil) // Set the document's file path and password properties
  97. {
  98. document.filePath = [filePath copy]; document.password = [phrase copy];
  99. }
  100. }
  101. @catch (NSException *exception) // Exception handling (just in case O_o)
  102. {
  103. #ifdef DEBUG
  104. NSLog(@"%s Caught %@: %@", __FUNCTION__, [exception name], [exception reason]);
  105. #endif
  106. }
  107. return document;
  108. }
  109. + (ReaderDocument *)withDocumentFilePath:(NSString *)filePath password:(NSString *)phrase
  110. {
  111. ReaderDocument *document = nil; // ReaderDocument object
  112. document = [ReaderDocument unarchiveFromFileName:filePath password:phrase];
  113. if (document == nil) // Unarchive failed so create a new ReaderDocument object
  114. {
  115. document = [[ReaderDocument alloc] initWithFilePath:filePath password:phrase];
  116. }
  117. return document;
  118. }
  119. + (BOOL)isPDF:(NSString *)filePath
  120. {
  121. BOOL state = NO;
  122. if (filePath != nil) // Must have a file path
  123. {
  124. const char *path = [filePath fileSystemRepresentation];
  125. int fd = open(path, O_RDONLY); // Open the file
  126. if (fd > 0) // We have a valid file descriptor
  127. {
  128. const char sig[1024]; // File signature buffer
  129. ssize_t len = read(fd, (void *)&sig, sizeof(sig));
  130. state = (strnstr(sig, "%PDF", len) != NULL);
  131. close(fd); // Close the file
  132. }
  133. }
  134. return state;
  135. }
  136. #pragma mark - ReaderDocument instance methods
  137. - (instancetype)initWithFilePath:(NSString *)filePath password:(NSString *)phrase
  138. {
  139. if ((self = [super init])) // Initialize superclass first
  140. {
  141. if ([ReaderDocument isPDF:filePath] == YES) // Valid PDF
  142. {
  143. _guid = [ReaderDocument GUID]; // Create document's GUID
  144. _password = [phrase copy]; // Keep copy of document password
  145. _filePath = [filePath copy]; // Keep copy of document file path
  146. _pageNumber = [NSNumber numberWithInteger:1]; // Start on page one
  147. _bookmarks = [NSMutableIndexSet new]; // Bookmarked pages index set
  148. CFURLRef docURLRef = (__bridge CFURLRef)[self fileURL]; // CFURLRef from NSURL
  149. CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateUsingUrl(docURLRef, _password);
  150. if (thePDFDocRef != NULL) // Get the total number of pages in the document
  151. {
  152. NSInteger pageCount = CGPDFDocumentGetNumberOfPages(thePDFDocRef);
  153. _pageCount = [NSNumber numberWithInteger:pageCount];
  154. CGPDFDocumentRelease(thePDFDocRef); // Cleanup
  155. }
  156. else // Cupertino, we have a problem with the document
  157. {
  158. NSAssert(NO, @"CGPDFDocumentRef == NULL");
  159. }
  160. _lastOpen = [NSDate dateWithTimeIntervalSinceReferenceDate:0.0];
  161. NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
  162. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:_filePath error:NULL];
  163. _fileDate = [fileAttributes objectForKey:NSFileModificationDate]; // File date
  164. _fileSize = [fileAttributes objectForKey:NSFileSize]; // File size (bytes)
  165. [self archiveDocumentProperties]; // Archive ReaderDocument object
  166. }
  167. else // Not a valid PDF file
  168. {
  169. self = nil;
  170. }
  171. }
  172. return self;
  173. }
  174. - (NSString *)fileName
  175. {
  176. if (_fileName == nil) _fileName = [_filePath lastPathComponent];
  177. return _fileName;
  178. }
  179. - (NSURL *)fileURL
  180. {
  181. if (_fileURL == nil) _fileURL = [[NSURL alloc] initFileURLWithPath:_filePath isDirectory:NO];
  182. return _fileURL;
  183. }
  184. - (BOOL)canEmail
  185. {
  186. return YES;
  187. }
  188. - (BOOL)canExport
  189. {
  190. return YES;
  191. }
  192. - (BOOL)canPrint
  193. {
  194. return YES;
  195. }
  196. - (BOOL)archiveDocumentProperties
  197. {
  198. NSString *archiveFilePath = [ReaderDocument archiveFilePath:[self fileName]];
  199. return [NSKeyedArchiver archiveRootObject:self toFile:archiveFilePath];
  200. }
  201. - (void)updateDocumentProperties
  202. {
  203. CFURLRef docURLRef = (__bridge CFURLRef)[self fileURL]; // CFURLRef from NSURL
  204. CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateUsingUrl(docURLRef, _password);
  205. if (thePDFDocRef != NULL) // Get the total number of pages in the document
  206. {
  207. NSInteger pageCount = CGPDFDocumentGetNumberOfPages(thePDFDocRef);
  208. _pageCount = [NSNumber numberWithInteger:pageCount];
  209. CGPDFDocumentRelease(thePDFDocRef); // Cleanup
  210. }
  211. NSFileManager *fileManager = [NSFileManager defaultManager]; // Singleton
  212. NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:_filePath error:NULL];
  213. _fileDate = [fileAttributes objectForKey:NSFileModificationDate]; // File date
  214. _fileSize = [fileAttributes objectForKey:NSFileSize]; // File size (bytes)
  215. }
  216. #pragma mark - NSCoding protocol methods
  217. - (void)encodeWithCoder:(NSCoder *)encoder
  218. {
  219. [encoder encodeObject:_guid forKey:@"FileGUID"];
  220. [encoder encodeObject:_fileDate forKey:@"FileDate"];
  221. [encoder encodeObject:_pageCount forKey:@"PageCount"];
  222. [encoder encodeObject:_pageNumber forKey:@"PageNumber"];
  223. [encoder encodeObject:_bookmarks forKey:@"Bookmarks"];
  224. [encoder encodeObject:_fileSize forKey:@"FileSize"];
  225. [encoder encodeObject:_lastOpen forKey:@"LastOpen"];
  226. }
  227. - (instancetype)initWithCoder:(NSCoder *)decoder
  228. {
  229. if ((self = [super init])) // Superclass init
  230. {
  231. _guid = [decoder decodeObjectForKey:@"FileGUID"];
  232. _fileDate = [decoder decodeObjectForKey:@"FileDate"];
  233. _pageCount = [decoder decodeObjectForKey:@"PageCount"];
  234. _pageNumber = [decoder decodeObjectForKey:@"PageNumber"];
  235. _bookmarks = [decoder decodeObjectForKey:@"Bookmarks"];
  236. _fileSize = [decoder decodeObjectForKey:@"FileSize"];
  237. _lastOpen = [decoder decodeObjectForKey:@"LastOpen"];
  238. if (_guid == nil) _guid = [ReaderDocument GUID];
  239. if (_bookmarks != nil)
  240. _bookmarks = [_bookmarks mutableCopy];
  241. else
  242. _bookmarks = [NSMutableIndexSet new];
  243. }
  244. return self;
  245. }
  246. @end