From 43bcd8a7516bfbd455d81de7e00d5e139abce438 Mon Sep 17 00:00:00 2001 From: Paul Gardiner Date: Tue, 17 Sep 2013 15:04:13 +0100 Subject: iOS: split the classes into separate files --- platform/ios/Classes/MuLibraryController.m | 225 +++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 platform/ios/Classes/MuLibraryController.m (limited to 'platform/ios/Classes/MuLibraryController.m') diff --git a/platform/ios/Classes/MuLibraryController.m b/platform/ios/Classes/MuLibraryController.m new file mode 100644 index 00000000..cc5a1ddd --- /dev/null +++ b/platform/ios/Classes/MuLibraryController.m @@ -0,0 +1,225 @@ +// +// MuLibraryController.m +// MuPDF +// +// Copyright (c) 2013 Artifex Software, Inc. All rights reserved. +// + +#include "common.h" +#import "MuDocumentController.h" +#import "MuLibraryController.h" + +static void showAlert(NSString *msg, NSString *filename) +{ + UIAlertView *alert = [[UIAlertView alloc] + initWithTitle: msg + message: filename + delegate: nil + cancelButtonTitle: @"Okay" + otherButtonTitles: nil]; + [alert show]; + [alert release]; +} + +@implementation MuLibraryController + +- (void) viewWillAppear: (BOOL)animated +{ + [self setTitle: @"PDF, XPS and CBZ Documents"]; + [self reload]; + printf("library viewWillAppear (starting reload timer)\n"); + timer = [NSTimer timerWithTimeInterval: 3 + target: self selector: @selector(reload) userInfo: nil + repeats: YES]; + [[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode]; +} + +- (void) viewWillDisappear: (BOOL)animated +{ + printf("library viewWillDisappear (stopping reload timer)\n"); + [timer invalidate]; + timer = nil; +} + +- (void) reload +{ + if (files) { + [files release]; + files = nil; + } + + NSFileManager *fileman = [NSFileManager defaultManager]; + NSString *docdir = [NSString stringWithFormat: @"%@/Documents", NSHomeDirectory()]; + NSMutableArray *outfiles = [[NSMutableArray alloc] init]; + NSDirectoryEnumerator *direnum = [fileman enumeratorAtPath:docdir]; + NSString *file; + BOOL isdir; + while (file = [direnum nextObject]) { + NSString *filepath = [docdir stringByAppendingPathComponent:file]; + NSLog(@"file %@\n", file); + if ([fileman fileExistsAtPath:filepath isDirectory:&isdir] && !isdir) { + [outfiles addObject:file]; + } + } + + files = outfiles; + + [[self tableView] reloadData]; +} + +- (void) dealloc +{ + [files release]; + [super dealloc]; +} + +- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)o +{ + return YES; +} + +- (NSInteger) numberOfSectionsInTableView: (UITableView*)tableView +{ + return 1; +} + +- (NSInteger) tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section +{ + return [files count]; +} + +- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex +{ + if (buttonIndex == [actionSheet destructiveButtonIndex]) + { + char filename[PATH_MAX]; + int row = [actionSheet tag]; + + dispatch_sync(queue, ^{}); + + strcpy(filename, [NSHomeDirectory() UTF8String]); + strcat(filename, "/Documents/"); + strcat(filename, [[files objectAtIndex: row - 1] UTF8String]); + + printf("delete document '%s'\n", filename); + + unlink(filename); + + [self reload]; + } +} + +- (void) onTapDelete: (UIControl*)sender +{ + int row = [sender tag]; + NSString *title = [NSString stringWithFormat: @"Delete %@?", [files objectAtIndex: row - 1]]; + UIActionSheet *sheet = [[UIActionSheet alloc] + initWithTitle: title + delegate: self + cancelButtonTitle: @"Cancel" + destructiveButtonTitle: @"Delete" + otherButtonTitles: nil]; + [sheet setTag: row]; + [sheet showInView: [self tableView]]; + [sheet release]; +} + +- (UITableViewCell*) tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath +{ + static NSString *cellid = @"MuCellIdent"; + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellid]; + if (!cell) + cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cellid] autorelease]; + int row = [indexPath row]; + [[cell textLabel] setText: [files objectAtIndex: row]]; + [[cell textLabel] setFont: [UIFont systemFontOfSize: 20]]; + + UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; + [deleteButton setImage: [UIImage imageNamed: @"x_alt_blue.png"] forState: UIControlStateNormal]; + [deleteButton setFrame: CGRectMake(0, 0, 35, 35)]; + [deleteButton addTarget: self action: @selector(onTapDelete:) forControlEvents: UIControlEventTouchUpInside]; + [deleteButton setTag: row]; + [cell setAccessoryView: deleteButton]; + + return cell; +} + +- (void) tableView: (UITableView*)tableView didSelectRowAtIndexPath: (NSIndexPath*)indexPath +{ + int row = [indexPath row]; + [self openDocument: [files objectAtIndex: row]]; +} + +- (void) openDocument: (NSString*)nsfilename +{ + char filename[PATH_MAX]; + + dispatch_sync(queue, ^{}); + + strcpy(filename, [NSHomeDirectory() UTF8String]); + strcat(filename, "/Documents/"); + strcat(filename, [nsfilename UTF8String]); + + printf("open document '%s'\n", filename); + + _filename = [nsfilename retain]; + _doc = fz_open_document(ctx, filename); + if (!_doc) { + showAlert(@"Cannot open document", nsfilename); + return; + } + + if (fz_needs_password(_doc)) + [self askForPassword: @"'%@' needs a password:"]; + else + [self onPasswordOkay]; +} + +- (void) askForPassword: (NSString*)prompt +{ + UIAlertView *passwordAlertView = [[UIAlertView alloc] + initWithTitle: @"Password Protected" + message: [NSString stringWithFormat: prompt, [_filename lastPathComponent]] + delegate: self + cancelButtonTitle: @"Cancel" + otherButtonTitles: @"Done", nil]; + [passwordAlertView setAlertViewStyle: UIAlertViewStyleSecureTextInput]; + [passwordAlertView show]; + [passwordAlertView release]; +} + +- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex +{ + char *password = (char*) [[[alertView textFieldAtIndex: 0] text] UTF8String]; + [alertView dismissWithClickedButtonIndex: buttonIndex animated: TRUE]; + if (buttonIndex == 1) { + if (fz_authenticate_password(_doc, password)) + [self onPasswordOkay]; + else + [self askForPassword: @"Wrong password for '%@'. Try again:"]; + } else { + [self onPasswordCancel]; + } +} + +- (void) onPasswordOkay +{ + MuDocumentController *document = [[MuDocumentController alloc] initWithFilename: _filename document: _doc]; + if (document) { + [self setTitle: @"Library"]; + [[self navigationController] pushViewController: document animated: YES]; + [document release]; + } + [_filename release]; + _doc = NULL; +} + +- (void) onPasswordCancel +{ + [_filename release]; + printf("close document (password cancel)\n"); + fz_close_document(_doc); + _doc = NULL; +} + +@end -- cgit v1.2.3