Advertisement
Guest User

ePubreader

a guest
Sep 3rd, 2011
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #import "EpubReaderViewController.h"
  3.  
  4.  
  5. @implementation EpubReaderViewController
  6. @synthesize _ePubContent;
  7. @synthesize _rootPath;
  8. @synthesize _strFileName;
  9.  
  10.  
  11. - (void)viewDidLoad
  12. {
  13.     [super viewDidLoad];
  14.     [self setBackButton];
  15.     [_webview setBackgroundColor:[UIColor clearColor]];
  16.     //First unzip the epub file to documents directory
  17.     [self unzipAndSaveFile];
  18.     _xmlHandler=[[XMLHandler alloc] init];
  19.     _xmlHandler.delegate=self;
  20.     [_xmlHandler parseXMLFileAt:[self getRootFilePath]];
  21. }
  22.  
  23. /*Function Name : setTitlename
  24.  *Return Type   : void
  25.  *Parameters    : NSString- Text to set as title
  26.  *Purpose       : To set the title for the view
  27.  */
  28.  
  29. - (void)setTitlename:(NSString*)titleText
  30. {
  31.     NSLog(@"%@",titleText);
  32.     // this will appear as the title in the navigation bar
  33.     //CGRect frame = CGRectMake(0, 0, 200, 44);
  34.     CGRect frame = CGRectMake(0, 0, 300, 44);
  35.     UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
  36.     label.backgroundColor = [UIColor clearColor];
  37.     //label.font = [UIFont boldSystemFontOfSize:17.0];
  38.     //label.font=[UIFont systemFontOfSize:17.0];
  39.     label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
  40.     label.textAlignment = UITextAlignmentCenter;
  41.     label.numberOfLines=0;
  42.     label.textColor=[UIColor whiteColor];
  43.     self.navigationItem.titleView = label;
  44.     label.text=titleText;
  45. }
  46.  
  47. - (void)viewWillAppear:(BOOL)animated
  48. {
  49.  
  50.     [self performSelector:@selector(setBackButton)
  51.                withObject:nil
  52.                afterDelay:0.1];
  53. }
  54.  
  55. /*Function Name : setBackButton
  56.  *Return Type   : void
  57.  *Parameters    : nil
  58.  *Purpose       : To set the back navigation button
  59.  */
  60.  
  61. -(void)setBackButton
  62. {
  63.  
  64.     UIBarButtonItem *objBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(onBack:)];
  65.     self.navigationItem.leftBarButtonItem= objBarButtonItem;      
  66.     [objBarButtonItem release];
  67.    
  68. }
  69.  
  70. /*Function Name : unzipAndSaveFile
  71.  *Return Type   : void
  72.  *Parameters    : nil
  73.  *Purpose       : To unzip the epub file to documents directory
  74. */
  75.  
  76. - (void)unzipAndSaveFile
  77. {
  78.    
  79.     UKFightLabAppDelegate *appdelegate = ( UKFightLabAppDelegate *)[[UIApplication sharedApplication]delegate];
  80.  
  81.     ZipArchive* za = [[ZipArchive alloc] init];
  82.     if( [za UnzipOpenFile:[[NSBundle mainBundle] pathForResource:appdelegate.topic ofType:@"epub"]] )
  83.     {
  84.         NSLog(@"%@",appdelegate.topic);
  85.  
  86.         NSString *strPath=[NSString stringWithFormat:@"%@/UnzippedEpub",[self applicationDocumentsDirectory]];
  87.         //Delete all the previous files
  88.         NSFileManager *filemanager=[[NSFileManager alloc] init];
  89.        
  90.         if ([filemanager fileExistsAtPath:strPath])
  91.             {
  92.            
  93.                 NSError *error;
  94.                 [filemanager removeItemAtPath:strPath error:&error];
  95.             }
  96.         [filemanager release];
  97.         filemanager=nil;
  98.        
  99.         //start unzip
  100.         BOOL ret = [za UnzipFileTo:[NSString stringWithFormat:@"%@/",strPath] overWrite:YES];
  101.         if( NO==ret )
  102.         {
  103.             // error handler here
  104.             UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error"
  105.                                                           message:@"An unknown error occured"
  106.                                                          delegate:self
  107.                                                 cancelButtonTitle:@"OK"
  108.                                                 otherButtonTitles:nil];
  109.             [alert show];
  110.             [alert release];
  111.             alert=nil;
  112.         }
  113.         [za UnzipCloseFile];
  114.     }                  
  115.     [za release];
  116. }
  117.  
  118. /*Function Name : applicationDocumentsDirectory
  119.  *Return Type   : NSString - Returns the path to documents directory
  120.  *Parameters    : nil
  121.  *Purpose       : To find the path to documents directory
  122.  */
  123.  
  124. - (NSString *)applicationDocumentsDirectory
  125. {
  126.    
  127.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  128.     NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  129.     return basePath;
  130. }
  131.  
  132. /*Function Name : getRootFilePath
  133.  *Return Type   : NSString - Returns the path to container.xml
  134.  *Parameters    : nil
  135.  *Purpose       : To find the path to container.xml.This file contains the file name which holds the epub informations
  136.  */
  137.  
  138. - (NSString*)getRootFilePath{
  139.    
  140.     //check whether root file path exists
  141.     NSFileManager *filemanager=[[NSFileManager alloc] init];
  142.     NSString *strFilePath=[NSString stringWithFormat:@"%@/UnzippedEpub/META-INF/container.xml",[self applicationDocumentsDirectory]];
  143.     if ([filemanager fileExistsAtPath:strFilePath]) {
  144.        
  145.         //valid ePub
  146.         //NSLog(@"Parse now");
  147.        
  148.         [filemanager release];
  149.         filemanager=nil;
  150.        
  151.         return strFilePath;
  152.     }
  153.     else {
  154.        
  155.         //Invalid ePub file
  156.         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error"
  157.                                                       message:@"Root File not Valid"
  158.                                                      delegate:self
  159.                                             cancelButtonTitle:@"OK"
  160.                                             otherButtonTitles:nil];
  161.         [alert show];
  162.         [alert release];
  163.         alert=nil;
  164.        
  165.     }
  166.     [filemanager release];
  167.     filemanager=nil;
  168.     return @"";
  169. }
  170.  
  171.  
  172. #pragma mark XMLHandler Delegate Methods
  173.  
  174. - (void)foundRootPath:(NSString*)rootPath{
  175.    
  176.     //Found the path of *.opf file
  177.    
  178.     //get the full path of opf file
  179.     NSString *strOpfFilePath=[NSString stringWithFormat:@"%@/UnzippedEpub/%@",[self applicationDocumentsDirectory],rootPath];
  180.     NSFileManager *filemanager=[[NSFileManager alloc] init];
  181.    
  182.     self._rootPath=[strOpfFilePath stringByReplacingOccurrencesOfString:[strOpfFilePath lastPathComponent] withString:@""];
  183.    
  184.     if ([filemanager fileExistsAtPath:strOpfFilePath]) {
  185.        
  186.         //Now start parse this file
  187.         [_xmlHandler parseXMLFileAt:strOpfFilePath];
  188.     }
  189.     else {
  190.        
  191.         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error"
  192.                                                       message:@"OPF File not found"
  193.                                                      delegate:self
  194.                                             cancelButtonTitle:@"OK"
  195.                                             otherButtonTitles:nil];
  196.         [alert show];
  197.         [alert release];
  198.         alert=nil;
  199.     }
  200.     [filemanager release];
  201.     filemanager=nil;
  202.    
  203. }
  204.  
  205.  
  206. - (void)finishedParsing:(EpubContent*)ePubContents
  207. {
  208.  
  209.     //NSLog(@"Root Path---->%@",ePubContents._manifest);
  210.     _pagesPath=[NSString stringWithFormat:@"%@%@",self._rootPath,[ePubContents._manifest valueForKey:[ePubContents._spine objectAtIndex:0]]];
  211.     self._ePubContent=ePubContents;
  212.     _pageNumber=0;                              //
  213.     [self loadPage];
  214. }
  215.  
  216. #pragma mark Button Actions
  217.  
  218. - (IBAction)onPreviousOrNext:(id)sender{
  219.    
  220.    
  221.     UIBarButtonItem *btnClicked=(UIBarButtonItem*)sender;
  222.     if (btnClicked.tag==0) {
  223.        
  224.         if (_pageNumber>0) {                        //
  225.            
  226.             _pageNumber--;
  227.             [self loadPage];
  228.         }
  229.     }
  230.     else {
  231.        
  232.         if ([self._ePubContent._spine count]-1>_pageNumber) {
  233.            
  234.             _pageNumber++;
  235.             [self loadPage];
  236.         }
  237.     }
  238. }
  239.  
  240. - (IBAction)onBack:(id)sender
  241. {
  242.  
  243.     [self.navigationController popViewControllerAnimated:YES];
  244. }
  245.  
  246. /*Function Name : loadPage
  247.  *Return Type   : void
  248.  *Parameters    : nil
  249.  *Purpose       : To load actual pages to webview
  250.  */
  251.  
  252. - (void)loadPage
  253. {
  254.    
  255.     _pagesPath=[NSString stringWithFormat:@"%@%@",self._rootPath,[self._ePubContent._manifest valueForKey:[self._ePubContent._spine objectAtIndex:_pageNumber]]];
  256.     NSLog(@"Path--->%@",_pagesPath);
  257.    
  258.     ///-----------R&D---------------
  259.    
  260.    // NSString *html =[NSString stringWithFormat:@"<html><body> <p align='center'><FONT SIZE=5 color='#000000'>%@</FONT></p></body></html>",_pagesPath];
  261.    // NSLog(@"String----->>%@",html);
  262.    
  263.    
  264.    
  265.    // [_webview loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.hitchhiker.com/message"]];
  266.    
  267.     //---------------------
  268.    
  269.     [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:_pagesPath]]];
  270.     //set page number
  271.    
  272.     _pageNumberLbl.text=[NSString stringWithFormat:@"%d",_pageNumber+1];
  273. }
  274.  
  275. #pragma mark Memory handlers
  276.  
  277. - (void)didReceiveMemoryWarning {
  278.    
  279.     // Releases the view if it doesn't have a superview.
  280.     [super didReceiveMemoryWarning];
  281. }
  282.  
  283. - (void)viewDidUnload
  284. {
  285.  
  286. }
  287.  
  288.  
  289. - (void)dealloc {
  290.    
  291.     [_webview release];
  292.     _webview=nil;
  293.    
  294.     [_ePubContent release];
  295.     _ePubContent=nil;
  296.    
  297.     _pagesPath=nil;
  298.     _rootPath=nil;
  299.    
  300.     [_strFileName release];
  301.     _strFileName=nil;
  302.    
  303.     [_backGroundImage release];
  304.     _backGroundImage=nil;
  305.    
  306.     [super dealloc];
  307. }
  308.  
  309. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement