Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "EpubReaderViewController.h"
- @implementation EpubReaderViewController
- @synthesize _ePubContent;
- @synthesize _rootPath;
- @synthesize _strFileName;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [self setBackButton];
- [_webview setBackgroundColor:[UIColor clearColor]];
- //First unzip the epub file to documents directory
- [self unzipAndSaveFile];
- _xmlHandler=[[XMLHandler alloc] init];
- _xmlHandler.delegate=self;
- [_xmlHandler parseXMLFileAt:[self getRootFilePath]];
- }
- /*Function Name : setTitlename
- *Return Type : void
- *Parameters : NSString- Text to set as title
- *Purpose : To set the title for the view
- */
- - (void)setTitlename:(NSString*)titleText
- {
- NSLog(@"%@",titleText);
- // this will appear as the title in the navigation bar
- //CGRect frame = CGRectMake(0, 0, 200, 44);
- CGRect frame = CGRectMake(0, 0, 300, 44);
- UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
- label.backgroundColor = [UIColor clearColor];
- //label.font = [UIFont boldSystemFontOfSize:17.0];
- //label.font=[UIFont systemFontOfSize:17.0];
- label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
- label.textAlignment = UITextAlignmentCenter;
- label.numberOfLines=0;
- label.textColor=[UIColor whiteColor];
- self.navigationItem.titleView = label;
- label.text=titleText;
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [self performSelector:@selector(setBackButton)
- withObject:nil
- afterDelay:0.1];
- }
- /*Function Name : setBackButton
- *Return Type : void
- *Parameters : nil
- *Purpose : To set the back navigation button
- */
- -(void)setBackButton
- {
- UIBarButtonItem *objBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(onBack:)];
- self.navigationItem.leftBarButtonItem= objBarButtonItem;
- [objBarButtonItem release];
- }
- /*Function Name : unzipAndSaveFile
- *Return Type : void
- *Parameters : nil
- *Purpose : To unzip the epub file to documents directory
- */
- - (void)unzipAndSaveFile
- {
- UKFightLabAppDelegate *appdelegate = ( UKFightLabAppDelegate *)[[UIApplication sharedApplication]delegate];
- ZipArchive* za = [[ZipArchive alloc] init];
- if( [za UnzipOpenFile:[[NSBundle mainBundle] pathForResource:appdelegate.topic ofType:@"epub"]] )
- {
- NSLog(@"%@",appdelegate.topic);
- NSString *strPath=[NSString stringWithFormat:@"%@/UnzippedEpub",[self applicationDocumentsDirectory]];
- //Delete all the previous files
- NSFileManager *filemanager=[[NSFileManager alloc] init];
- if ([filemanager fileExistsAtPath:strPath])
- {
- NSError *error;
- [filemanager removeItemAtPath:strPath error:&error];
- }
- [filemanager release];
- filemanager=nil;
- //start unzip
- BOOL ret = [za UnzipFileTo:[NSString stringWithFormat:@"%@/",strPath] overWrite:YES];
- if( NO==ret )
- {
- // error handler here
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error"
- message:@"An unknown error occured"
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- alert=nil;
- }
- [za UnzipCloseFile];
- }
- [za release];
- }
- /*Function Name : applicationDocumentsDirectory
- *Return Type : NSString - Returns the path to documents directory
- *Parameters : nil
- *Purpose : To find the path to documents directory
- */
- - (NSString *)applicationDocumentsDirectory
- {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
- return basePath;
- }
- /*Function Name : getRootFilePath
- *Return Type : NSString - Returns the path to container.xml
- *Parameters : nil
- *Purpose : To find the path to container.xml.This file contains the file name which holds the epub informations
- */
- - (NSString*)getRootFilePath{
- //check whether root file path exists
- NSFileManager *filemanager=[[NSFileManager alloc] init];
- NSString *strFilePath=[NSString stringWithFormat:@"%@/UnzippedEpub/META-INF/container.xml",[self applicationDocumentsDirectory]];
- if ([filemanager fileExistsAtPath:strFilePath]) {
- //valid ePub
- //NSLog(@"Parse now");
- [filemanager release];
- filemanager=nil;
- return strFilePath;
- }
- else {
- //Invalid ePub file
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error"
- message:@"Root File not Valid"
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- alert=nil;
- }
- [filemanager release];
- filemanager=nil;
- return @"";
- }
- #pragma mark XMLHandler Delegate Methods
- - (void)foundRootPath:(NSString*)rootPath{
- //Found the path of *.opf file
- //get the full path of opf file
- NSString *strOpfFilePath=[NSString stringWithFormat:@"%@/UnzippedEpub/%@",[self applicationDocumentsDirectory],rootPath];
- NSFileManager *filemanager=[[NSFileManager alloc] init];
- self._rootPath=[strOpfFilePath stringByReplacingOccurrencesOfString:[strOpfFilePath lastPathComponent] withString:@""];
- if ([filemanager fileExistsAtPath:strOpfFilePath]) {
- //Now start parse this file
- [_xmlHandler parseXMLFileAt:strOpfFilePath];
- }
- else {
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error"
- message:@"OPF File not found"
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- alert=nil;
- }
- [filemanager release];
- filemanager=nil;
- }
- - (void)finishedParsing:(EpubContent*)ePubContents
- {
- //NSLog(@"Root Path---->%@",ePubContents._manifest);
- _pagesPath=[NSString stringWithFormat:@"%@%@",self._rootPath,[ePubContents._manifest valueForKey:[ePubContents._spine objectAtIndex:0]]];
- self._ePubContent=ePubContents;
- _pageNumber=0; //
- [self loadPage];
- }
- #pragma mark Button Actions
- - (IBAction)onPreviousOrNext:(id)sender{
- UIBarButtonItem *btnClicked=(UIBarButtonItem*)sender;
- if (btnClicked.tag==0) {
- if (_pageNumber>0) { //
- _pageNumber--;
- [self loadPage];
- }
- }
- else {
- if ([self._ePubContent._spine count]-1>_pageNumber) {
- _pageNumber++;
- [self loadPage];
- }
- }
- }
- - (IBAction)onBack:(id)sender
- {
- [self.navigationController popViewControllerAnimated:YES];
- }
- /*Function Name : loadPage
- *Return Type : void
- *Parameters : nil
- *Purpose : To load actual pages to webview
- */
- - (void)loadPage
- {
- _pagesPath=[NSString stringWithFormat:@"%@%@",self._rootPath,[self._ePubContent._manifest valueForKey:[self._ePubContent._spine objectAtIndex:_pageNumber]]];
- NSLog(@"Path--->%@",_pagesPath);
- ///-----------R&D---------------
- // NSString *html =[NSString stringWithFormat:@"<html><body> <p align='center'><FONT SIZE=5 color='#000000'>%@</FONT></p></body></html>",_pagesPath];
- // NSLog(@"String----->>%@",html);
- // [_webview loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.hitchhiker.com/message"]];
- //---------------------
- [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:_pagesPath]]];
- //set page number
- _pageNumberLbl.text=[NSString stringWithFormat:@"%d",_pageNumber+1];
- }
- #pragma mark Memory handlers
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload
- {
- }
- - (void)dealloc {
- [_webview release];
- _webview=nil;
- [_ePubContent release];
- _ePubContent=nil;
- _pagesPath=nil;
- _rootPath=nil;
- [_strFileName release];
- _strFileName=nil;
- [_backGroundImage release];
- _backGroundImage=nil;
- [super dealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement