Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPUB3 Bindings #35

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions LauncherOSX/CocoaHTTPServer/Core/Responses/HTTPFileResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ - (NSDictionary *)httpHeaders {
else if([ext isEqualToString:@"css"]) {
return [NSDictionary dictionaryWithObject:@"text/css" forKey:@"Content-Type"];
}
else if([ext isEqualToString:@"xml"]) {
return [NSDictionary dictionaryWithObject:@"application/xml" forKey:@"Content-Type"];
}
else if([ext isEqualToString:@"xhtml"] || [ext isEqualToString:@"html"]) {
return [NSDictionary dictionaryWithObject:@"application/xhtml+xml" forKey:@"Content-Type"];
}
Expand Down
3 changes: 3 additions & 0 deletions LauncherOSX/LOXAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#import "LOXUtil.h"
#import "LOXMediaOverlay.h"
#import "LOXMediaOverlayController.h"
#import "LOXWebViewController.h"

using namespace ePub3;

Expand Down Expand Up @@ -97,6 +98,8 @@ - (id)init

-(void) awakeFromNib
{
[NSURLProtocol registerClass:[Epub3URLProtocol class]];

_epubApi = [[LOXePubSdkApi alloc] init];

self.spineViewController.currentPagesInfo = _currentPagesInfo;
Expand Down
4 changes: 4 additions & 0 deletions LauncherOSX/LOXWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
@class WebView;
@class PackageResourceServer;

@interface Epub3URLProtocol : NSURLProtocol

@end

@interface LOXWebViewController : NSObject<LOXSpineViewControllerDelegate> {

@private
Expand Down
161 changes: 150 additions & 11 deletions LauncherOSX/LOXWebViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,125 @@
#import "PackageResourceServer.h"
#import "RDPackageResource.h"
#import <ePub3/utilities/byte_stream.h>
#import <ePub3/utilities/iri.h>


@interface Epub3URLProtocol () //<NSURLConnectionDataDelegate>
//@property (nonatomic, strong) NSURLConnection *connection;
+(NSString *)scheme;
@end
@implementation Epub3URLProtocol

static NSString* EPUB3 = [NSString stringWithUTF8String:ePub3::IRI::gEPUBScheme.c_str()];

+(NSString *)scheme;{
return EPUB3;
}
//
//- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client {
//
// if ((self = [super initWithRequest:request cachedResponse:cachedResponse client:client])) {
//
// }
//
// return self;
//}
//- (void)dealloc {
//
// if (self.connection != nil)
// {
// self.connection.cancel;
// }
//}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
NSURL* requestURI = [request URL];
if (requestURI == nil)
{
return NO;
}
if (NSOrderedSame == [[requestURI scheme] caseInsensitiveCompare:EPUB3])
{
if ([NSURLProtocol propertyForKey:@"Epub3URLProtocol_Handled" inRequest:request])
{
return NO;
}

return YES;
}

return NO;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}

- (void)startLoading
{
NSMutableURLRequest *newRequest = [self.request mutableCopy];

[NSURLProtocol setProperty:@YES forKey:@"Epub3URLProtocol_Handled" inRequest:newRequest];

NSString * str = [NSURLProtocol propertyForKey:@"URLSTR" inRequest:newRequest];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[newRequest setURL: url];

NSURL* originalURL = [self.request URL];

NSURLResponse *response = [[NSURLResponse alloc] initWithURL:originalURL MIMEType:nil expectedContentLength:0 textEncodingName:nil];
[self.client URLProtocol:self wasRedirectedToRequest:newRequest redirectResponse:response];
[self.client URLProtocolDidFinishLoading:self];

//self.connection = [NSURLConnection connectionWithRequest:newRequest delegate:self];
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
return [super requestIsCacheEquivalent:a toRequest:b];
}

- (void)stopLoading
{
// if (self.connection != nil)
// {
// [self.connection cancel];
// }
// self.connection = nil;
}

//-(NSURLRequest *)connection:(NSURLConnection *)connection
// willSendRequest:(NSURLRequest *)request
// redirectResponse:(NSURLResponse *)redirectResponse
//
//{
// NSURLRequest *newRequest = request;
// if (redirectResponse) {
// newRequest = nil;
// }
// return newRequest;
//}
//- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
//{
// [self.client URLProtocol:self didLoadData:data];
//}
//- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
//{
// [self.client URLProtocol:self didFailWithError:error];
// self.connection = nil;
//}
//- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
//{
// [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
//}
//- (void)connectionDidFinishLoading:(NSURLConnection *)connection
//{
// [self.client URLProtocolDidFinishLoading:self];
// self.connection = nil;
//}
@end

@interface LOXWebViewController ()

-(void)onPageChanged:(NSNotification*) notification;
Expand Down Expand Up @@ -112,8 +229,8 @@ - (NSURLRequest *)webView:(WebView *)sender
NSString * math = @"/readium_MathJax.js";
if ([path hasPrefix:math]) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MathJax" ofType:@"js" inDirectory:@"Scripts/mathjax"];
NSString * str = [[NSString stringWithFormat:@"file://%@", filePath] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSString * str = [NSString stringWithFormat:@"file://%@", filePath];
NSURL *url = [NSURL URLWithString: [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *newRequest = [request mutableCopy];
[newRequest setURL: url];
Expand All @@ -124,8 +241,8 @@ - (NSURLRequest *)webView:(WebView *)sender
NSString * annotationsCSS = @"/readium_Annotations.css";
if ([path hasPrefix:annotationsCSS]) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"annotations" ofType:@"css" inDirectory:@"Scripts"];
NSString * str = [[NSString stringWithFormat:@"file://%@", filePath] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSString * str = [NSString stringWithFormat:@"file://%@", filePath];
NSURL *url = [NSURL URLWithString: [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *newRequest = [request mutableCopy];
[newRequest setURL: url];
Expand All @@ -142,18 +259,22 @@ - (NSURLRequest *)webView:(WebView *)sender
//NSString * prefix2 = [NSString stringWithFormat:@"%@%@", prefix1, folder];
//[path substringFromIndex: [path rangeOfString:prefix1].location]


if (schemeFile != NSOrderedSame && [path hasPrefix:folder]) {
NSString * str = [[NSString stringWithFormat:@"file://%@", path] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSString * str = [NSString stringWithFormat:@"file://%@", path];
NSURL *url = [NSURL URLWithString: [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *newRequest = [request mutableCopy];
[newRequest setURL: url];

return newRequest;
}

if (schemeFile != NSOrderedSame)
// ObjectPreprocessor and ContentHandler with epub3:// URI protocol
// See [NSURLProtocol registerClass:[Epub3URLProtocol class]];
//NSString* EPUB3 = [NSString stringWithUTF8String:ePub3::IRI::gEPUBScheme.c_str()];
NSComparisonResult schemeEPUB = [scheme caseInsensitiveCompare: [Epub3URLProtocol scheme]];

if (schemeFile != NSOrderedSame && schemeEPUB != NSOrderedSame)
{
return request;
}
Expand All @@ -163,6 +284,20 @@ - (NSURLRequest *)webView:(WebView *)sender
return request;
}

if (schemeEPUB == NSOrderedSame)
{
NSString* BASE = [NSString stringWithUTF8String:_package.sdkPackage->BasePath().c_str()];
if (![BASE hasPrefix:@"/"]) {
BASE = [NSString stringWithFormat:@"/%@", BASE];
}
if ([path hasPrefix:BASE])
{
path = [path substringFromIndex:[BASE length]];
}

//prefix1 = [NSString stringWithFormat:@"%@://", [Epub3URLProtocol scheme]];
}

if ([path hasPrefix:@"/"]) {
path = [path substringFromIndex:1];
}
Expand All @@ -178,12 +313,16 @@ - (NSURLRequest *)webView:(WebView *)sender
path = [NSString stringWithFormat:@"%@#%@", path, fragment];
}

NSString * str = [[NSString stringWithFormat:@"%@/%@/%@", prefix1, _package.packageUUID, path] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSString * str = [NSString stringWithFormat:@"%@/%@/%@", prefix1, _package.packageUUID, path];

if (schemeEPUB == NSOrderedSame) {
[NSURLProtocol setProperty:[NSString stringWithString:str] forKey:@"URLSTR" inRequest:request];
return request;
}

NSURL *url = [NSURL URLWithString: [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *newRequest = [request mutableCopy];
[newRequest setURL: url];

return newRequest;
}

Expand Down