Skip to content

Commit

Permalink
Updated source to modern obj-c syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaketmp committed Nov 4, 2014
1 parent 4e38bd0 commit 3647f50
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
*.pbxuser
*.mode1v3
xcuserdata/

36 changes: 19 additions & 17 deletions EpubTests/EpubTests.m
Expand Up @@ -111,9 +111,9 @@ - (void)testThreeAuthors
NSString *expected1 = @"Second Author";
NSString *expected2 = @"Third Author";
XCTAssertTrue([actual count] == 3, @"3 authors expected");
XCTAssertEqualObjects([actual objectAtIndex:0], expected0, @"First author is wrong");
XCTAssertEqualObjects([actual objectAtIndex:1], expected1, @"Second author is wrong");
XCTAssertEqualObjects([actual objectAtIndex:2], expected2, @"Third author is wrong");
XCTAssertEqualObjects(actual[0], expected0, @"First author is wrong");
XCTAssertEqualObjects(actual[1], expected1, @"Second author is wrong");
XCTAssertEqualObjects(actual[2], expected2, @"Third author is wrong");
}

#pragma mark Text Extraction
Expand Down Expand Up @@ -161,11 +161,11 @@ - (void)testStripping
- (void)testDateParsing
{
NSDate *d;
NSArray *goodDates = [NSArray arrayWithObjects:
@"2012", @"2012-02", @"2012-02-13",
@"2012-02-13T19:49Z",
@"2012-02-13T19:49+0100",
nil];
NSArray *goodDates = @[@"2012",
@"2012-02",
@"2012-02-13",
@"2012-02-13T19:49Z",
@"2012-02-13T19:49+0100"];
for (id date in goodDates) {
d = [NSDate dateFromOPFString:date];
XCTAssertNotNil(d, @"%@ should parse", date);
Expand Down Expand Up @@ -257,8 +257,10 @@ - (void)testMissingCover
- (void)testUntitledCreators
{
NSArray *actual = [untitledFile creators];
NSArray *expected = [NSArray arrayWithObjects:@"Test Author", @"Test Illustrator",
@"Test Editor", @"Test Translator", nil];
NSArray *expected = @[@"Test Author",
@"Test Illustrator",
@"Test Editor",
@"Test Translator"];
XCTAssertTrue([actual count] == [expected count], @"Untitled file has wrong number of creators");
for (id item in expected) {
XCTAssertTrue([actual containsObject:item], @"Creator is missing");
Expand All @@ -268,7 +270,7 @@ - (void)testUntitledCreators
- (void)testMetadataCreators
{
NSArray *actual = [metadataFile creators];
NSArray *expected = [NSArray arrayWithObjects:@"Primary Author", @"Second Author", @"Third Author", nil];
NSArray *expected = @[@"Primary Author", @"Second Author", @"Third Author"];
// metadata has one contributor for each known MARC role and then 3 authors
// Just check the authors are present, and the total is right.
XCTAssertTrue([actual count] == 228, @"metadata file has wrong number of creators");
Expand All @@ -281,7 +283,7 @@ - (void)testMetadataCreators
- (void)testUntitledLanguage
{
NSArray *actual = [untitledFile language];
NSArray *expected = [NSArray arrayWithObject:@"en"];
NSArray *expected = @[@"en"];

XCTAssertTrue([actual count] == [expected count], @"Untitled file has wrong number of languages");

Expand All @@ -293,11 +295,11 @@ - (void)testUntitledLanguage
- (void)testMetadataLanguage
{
NSArray *actual = [metadataFile language];
NSArray *expected = [NSArray arrayWithObjects:@"en",
@"fr",
@"ga",
@"ja",
@"km", nil];
NSArray *expected = @[@"en",
@"fr",
@"ga",
@"ja",
@"km"];

XCTAssertTrue([actual count] == [expected count], @"Metadata file has wrong number of languages");

Expand Down
32 changes: 16 additions & 16 deletions JTPepub/JTPepub.h
Expand Up @@ -11,12 +11,12 @@
#include <AppKit/AppKit.h>
#include "ZipArchive/ZipArchive.h"

typedef enum {
typedef NS_ENUM(NSInteger, JTPbookType) {
jtpUnknownBook = 0,
jtpEPUB2,
jtpEPUB3,
jtpiBooks
} JTPbookType;
} ;

@interface JTPepub : NSObject {
@private
Expand Down Expand Up @@ -44,23 +44,23 @@ typedef enum {
NSString *drm;
NSDate *expiryDate;
}
- (id)initWithFile:(NSString *)fileName;
- (instancetype)initWithFile:(NSString *)fileName NS_DESIGNATED_INITIALIZER;
- (BOOL)openEPUBFile:(NSString*)fileName;

- (NSString *)textFromManifestItem:(NSUInteger)n;
- (NSString *)title;
- (NSArray *)authors;
- (NSString *)publisher;
- (NSArray *)creators;
- (NSArray *)editors;
- (NSArray *)illustrators;
- (NSArray *)translators;
- (NSImage *)cover;
- (NSString *)synopsis;
- (NSDate *)publicationDate;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *title;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSArray *authors;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *publisher;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSArray *creators;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSArray *editors;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSArray *illustrators;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSArray *translators;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSImage *cover;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *synopsis;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSDate *publicationDate;
- (NSString *)isbn;
- (NSString *)drm;
- (NSDate *)expiryDate;
- (NSArray *)language;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *drm;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSDate *expiryDate;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSArray *language;

@end
12 changes: 6 additions & 6 deletions JTPepub/JTPepub.m
Expand Up @@ -35,7 +35,7 @@ + (void)initialize
}
}

- (id)initWithFile:(NSString *)fileName
- (instancetype)initWithFile:(NSString *)fileName
{
self = [super init];
if (self) {
Expand Down Expand Up @@ -95,12 +95,12 @@ - (BOOL)openEPUBFile:(NSString*)fileName {
NSArray *rootFile = [containerXML nodesForXPath:@"//ocf:rootfile"
namespaces:xmlns
error:&xmlError];
NSString *rootFileType = [[[rootFile objectAtIndex:0] attributeForName:@"media-type"] stringValue];
NSString *rootFileType = [[rootFile[0] attributeForName:@"media-type"] stringValue];


// This code is all designed arround oebps+xml epubs, DTBook is unsupported.
if([rootFileType caseInsensitiveCompare:@"application/oebps-package+xml"] == NSOrderedSame) {
rootFilePath = [[[[rootFile objectAtIndex:0] attributeForName:@"full-path"] stringValue] retain];
rootFilePath = [[[rootFile[0] attributeForName:@"full-path"] stringValue] retain];
}else{
[containerXML release];
return NO;
Expand Down Expand Up @@ -151,8 +151,8 @@ - (NSString *)textFromManifestItem:(NSUInteger)n
return nil;

NSString *contentRoot = [rootFilePath stringByDeletingLastPathComponent];
NSString *relativePath = [[manifest objectAtIndex:n] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray *textPathArray = [NSArray arrayWithObjects:contentRoot, relativePath, nil];
NSString *relativePath = [manifest[n] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray *textPathArray = @[contentRoot, relativePath];
NSString *path = [NSString pathWithComponents:textPathArray];

NSData *content = [epubFile dataForNamedFile:path];
Expand Down Expand Up @@ -503,7 +503,7 @@ - (NSImage *)cover
// The cover path is relative to the rootfile...
NSString *contentRoot = [rootFilePath stringByDeletingLastPathComponent];
NSString *coverPath = [coverURI stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray *coverPathArray = [NSArray arrayWithObjects:contentRoot, coverPath, nil];
NSArray *coverPathArray = @[contentRoot, coverPath];
NSString *fullCoverPath = [NSString pathWithComponents:coverPathArray];

NSData *coverData = [epubFile dataForNamedFile:fullCoverPath];
Expand Down
6 changes: 2 additions & 4 deletions JTPepub/NSDate+OPF.m
Expand Up @@ -17,15 +17,13 @@ + (NSDate *)dateFromOPFString:(NSString *)s
{
NSDate *date = nil;
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray *formats = [NSArray arrayWithObjects:
@"yyyy",
NSArray *formats = @[@"yyyy",
@"yyyy-MM",
@"yyyy-MM-dd",
@"yyyy-MM-dd'T'HH:mm'Z'",
@"yyyy-MM-dd'T'HH:mmZ",
@"yyyy-MM-dd'T'HH:mm:ss'Z'",
@"yyyy-MM-dd'T'HH:mm:ssZ",
nil];
@"yyyy-MM-dd'T'HH:mm:ssZ"];

for (id format in formats) {
[formatter setDateFormat:format];
Expand Down
4 changes: 2 additions & 2 deletions JTPepub/NSString+HTML.h
Expand Up @@ -9,6 +9,6 @@
#import <Foundation/Foundation.h>

@interface NSString (HTML)
- (NSString *)stringByEscapingHTML;
- (NSString *)stringByStrippingHTML;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *stringByEscapingHTML;
@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *stringByStrippingHTML;
@end
4 changes: 2 additions & 2 deletions JTPepub/ZipArchive/ZipArchive.h
Expand Up @@ -33,10 +33,10 @@
}


-(id) initWithZipFile:(NSString *)fileName;
-(instancetype) initWithZipFile:(NSString *)fileName NS_DESIGNATED_INITIALIZER;

-(BOOL) openZipFile:(NSString *)fileName;
-(BOOL) closeZipFile;
@property (NS_NONATOMIC_IOSONLY, readonly) BOOL closeZipFile;

-(BOOL) testForNamedFile:(NSString *)fileName;

Expand Down
4 changes: 2 additions & 2 deletions JTPepub/ZipArchive/ZipArchive.mm
Expand Up @@ -39,7 +39,7 @@ @interface ZipArchive (Private)
{
NSInteger *pos = (NSInteger *)opaque;
NSError *error;
NSData *d = [[NSData alloc] initWithContentsOfFile:[NSString stringWithUTF8String:filename]
NSData *d = [[NSData alloc] initWithContentsOfFile:@(filename)
options:NSDataReadingMapped
error:&error];
*pos = 0;
Expand Down Expand Up @@ -105,7 +105,7 @@ unsigned long mmap_zread(void *opaque, void *stream, void *buf, unsigned long si

@implementation ZipArchive

-(id) initWithZipFile:(NSString *)fileName {
-(instancetype) initWithZipFile:(NSString *)fileName {
self = [super init];
if (self) {
archiveName = [fileName retain];
Expand Down
4 changes: 4 additions & 0 deletions epub.xcodeproj/project.pbxproj
Expand Up @@ -753,6 +753,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = YES;
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
Expand All @@ -779,6 +780,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = YES;
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
Expand All @@ -801,6 +803,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = YES;
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
Expand All @@ -824,6 +827,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = YES;
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
Expand Down
41 changes: 41 additions & 0 deletions epub.xcodeproj/project.xcworkspace/xcshareddata/epub.xccheckout
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>A703295D-C6EE-4A2A-A1A3-87DFB6844318</string>
<key>IDESourceControlProjectName</key>
<string>epub</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>C4C12A927EA5DE242D60FD77DCD3A65ACDBC254F</key>
<string>https://github.com/jaketmp/ePub-quicklook.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>epub.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>C4C12A927EA5DE242D60FD77DCD3A65ACDBC254F</key>
<string>../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>https://github.com/jaketmp/ePub-quicklook.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>C4C12A927EA5DE242D60FD77DCD3A65ACDBC254F</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>C4C12A927EA5DE242D60FD77DCD3A65ACDBC254F</string>
<key>IDESourceControlWCCName</key>
<string>epub</string>
</dict>
</array>
</dict>
</plist>
26 changes: 13 additions & 13 deletions epubMDI/MySpotlightImporter.m
Expand Up @@ -17,58 +17,58 @@ - (BOOL)importFileAtPath:(NSString *)filePath attributes:(NSMutableDictionary *)
JTPepub *epub = [[JTPepub alloc] initWithFile:filePath];

NSString *title = [epub title];
[spotlightData setObject:title forKey:(NSString *)kMDItemDisplayName];
spotlightData[(NSString *)kMDItemDisplayName] = title;

// authors kMDItemAuthors (array of strings)
NSArray *authors = [epub authors];
[spotlightData setObject:authors forKey:(NSString *)kMDItemAuthors];
spotlightData[(NSString *)kMDItemAuthors] = authors;

// publisher kMDItemPublishers (array of strings)
NSString *publisher = [epub publisher];
if ([publisher length] > 0)
[spotlightData setObject:[NSArray arrayWithObject:publisher] forKey:(NSString *)kMDItemPublishers];
spotlightData[(NSString *)kMDItemPublishers] = @[publisher];

// creators kMDItemContributors ? (array of strings)
NSArray *creators = [epub creators];
if ([creators count] > 0)
[spotlightData setObject:creators forKey:(NSString *)kMDItemContributors];
spotlightData[(NSString *)kMDItemContributors] = creators;

// editors kMDItemEditors (array of strings)
NSArray *editors = [epub editors];
if ([editors count] > 0)
[spotlightData setObject:editors forKey:(NSString *)kMDItemEditors];
spotlightData[(NSString *)kMDItemEditors] = editors;

// illustrators
NSArray *illustrators = [epub illustrators];
if ([illustrators count] > 0)
[spotlightData setObject:illustrators forKey:@"org_idpf_epub_container_metadata_illustrators"];
spotlightData[@"org_idpf_epub_container_metadata_illustrators"] = illustrators;

// translators
NSArray *translators = [epub translators];
if ([translators count] > 0)
[spotlightData setObject:translators forKey:@"org_idpf_epub_container_metadata_translators"];
spotlightData[@"org_idpf_epub_container_metadata_translators"] = translators;

// synopsis kMDItemHeadline ?
NSString *synopsis = [[epub synopsis] stringByStrippingHTML];
if ([synopsis length] > 0)
[spotlightData setObject:synopsis forKey:(NSString *)kMDItemHeadline];
spotlightData[(NSString *)kMDItemHeadline] = synopsis;

// ISBN kMDItemIdentifier (string)
NSString *isbn = [epub isbn];
if ([isbn length] > 0)
[spotlightData setObject:isbn forKey:(NSString *)kMDItemIdentifier];
spotlightData[(NSString *)kMDItemIdentifier] = isbn;

// publicationDate not kMDItemContentCreationDate

// expiryDate kMDItemDueDate (date)
NSDate *expiryDate = [epub expiryDate];
if (expiryDate)
[spotlightData setObject:expiryDate forKey:(NSString *)kMDItemDueDate];
spotlightData[(NSString *)kMDItemDueDate] = expiryDate;

// drm kMDItemSecurityMethod (string)
NSString *drm = [epub drm];
if ([drm isEqualToString:@""]) drm = @"None"; // PDF uses "None" explicitly
[spotlightData setObject:drm forKey:(NSString *)kMDItemSecurityMethod];
spotlightData[(NSString *)kMDItemSecurityMethod] = drm;

// Don't try to extract text if there's any DRM
if ([drm isEqualToString:@"None"]) {
Expand All @@ -83,7 +83,7 @@ - (BOOL)importFileAtPath:(NSString *)filePath attributes:(NSMutableDictionary *)
} while (text);
//NSString *tmp = [NSString stringWithFormat:@"Indexed %lu", [content length]];
//[spotlightData setObject:tmp forKey:(NSString *)kMDItemComment];
[spotlightData setObject:content forKey:(NSString *)kMDItemTextContent];
spotlightData[(NSString *)kMDItemTextContent] = content;
[content release];
} else {
//[spotlightData setObject:@"No indexed content" forKey:(NSString *)kMDItemComment];
Expand All @@ -92,7 +92,7 @@ - (BOOL)importFileAtPath:(NSString *)filePath attributes:(NSMutableDictionary *)
// language kMDItemLanguages (string)
NSArray *language = [epub language];
if ([language count] > 0)
[spotlightData setObject:language forKey:(NSString *)kMDItemLanguages];
spotlightData[(NSString *)kMDItemLanguages] = language;


[epub release];
Expand Down

0 comments on commit 3647f50

Please sign in to comment.