iOS web browser with a focus on security and privacy
at remove_ckhttpconnection 200 lines 7.0 kB view raw
1/* 2 * Endless 3 * Copyright (c) 2015 joshua stein <jcs@jcs.org> 4 * 5 * See LICENSE file for redistribution terms. 6 */ 7 8#import "AppDelegate.h" 9#import "Bookmark.h" 10#import "BookmarkController.h" 11 12@implementation BookmarkController { 13 AppDelegate *appDelegate; 14 UIBarButtonItem *addItem; 15 UIBarButtonItem *leftItem; 16} 17 18- (void)viewDidLoad 19{ 20 [super viewDidLoad]; 21 22 appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 23 24 self.title = @"Bookmarks"; 25 self.navigationItem.rightBarButtonItem = addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem:)]; 26 self.navigationItem.leftBarButtonItem = leftItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self.navigationController action:@selector(dismissModalViewControllerAnimated:)]; 27 28 UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)]; 29 lpgr.minimumPressDuration = 0.75f; 30 lpgr.delegate = self; 31 [[self tableView] addGestureRecognizer:lpgr]; 32 33 if ([[appDelegate webViewController] darkInterface]) 34 [[self tableView] setBackgroundColor:[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0]]; 35} 36 37- (void)viewWillDisappear:(BOOL)animated 38{ 39 [super viewWillDisappear:animated]; 40 [Bookmark persistList]; 41} 42 43#pragma mark - Table view data source 44 45- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 46{ 47 return 1; 48} 49 50- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 51{ 52 return [[Bookmark list] count]; 53} 54 55- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 56{ 57 if (self.embedded) 58 return @"Bookmarks"; 59 else 60 return @"(Tap to edit, hold to re-order)"; 61} 62 63- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 64{ 65 if (self.embedded && [view isKindOfClass:[UITableViewHeaderFooterView class]]) { 66 UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view; 67 int buttonSize = tableViewHeaderFooterView.frame.size.height - 8; 68 69 UIButton *b = [[UIButton alloc] init]; 70 [b setFrame:CGRectMake(tableViewHeaderFooterView.frame.size.width - buttonSize - 6, 3, buttonSize, buttonSize)]; 71 [b setBackgroundColor:[UIColor lightGrayColor]]; 72 [b setTitle:@"X" forState:UIControlStateNormal]; 73 [[b titleLabel] setFont:[UIFont boldSystemFontOfSize:12]]; 74 [[b layer] setCornerRadius:buttonSize / 2]; 75 [b setClipsToBounds:YES]; 76 77 [b addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside]; 78 79 [tableViewHeaderFooterView addSubview:b]; 80 } 81} 82 83- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 84{ 85 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bookmark"]; 86 if (cell == nil) 87 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"boomark"]; 88 89 Bookmark *b = [[Bookmark list] objectAtIndex:indexPath.row]; 90 if (b != nil) { 91 cell.textLabel.text = b.name; 92 cell.detailTextLabel.text = b.urlString; 93 } 94 95 [cell setShowsReorderControl:YES]; 96 97 if ([[appDelegate webViewController] darkInterface]) { 98 [cell setBackgroundColor:[UIColor clearColor]]; 99 [[cell textLabel] setTextColor:[UIColor whiteColor]]; 100 [[cell detailTextLabel] setTextColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]]; 101 } 102 103 return cell; 104} 105 106- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 107{ 108 Bookmark *bookmark = [Bookmark list][[indexPath row]]; 109 110 if (self.embedded) { 111 [[appDelegate webViewController] prepareForNewURLFromString:[bookmark urlString]]; 112 [[appDelegate webViewController] hideBookmarks]; 113 } 114 else { 115 [tableView deselectRowAtIndexPath:indexPath animated:YES]; 116 117 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Edit Bookmark" message:@"Enter the details of the URL to bookmark:" preferredStyle:UIAlertControllerStyleAlert]; 118 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 119 textField.placeholder = @"URL"; 120 textField.text = bookmark.urlString; 121 }]; 122 [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 123 textField.placeholder = @"Page Name (leave blank to use URL)"; 124 textField.text = bookmark.name; 125 }]; 126 127 UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 128 UITextField *url = alertController.textFields[0]; 129 bookmark.urlString = [url text]; 130 131 UITextField *name = alertController.textFields[1]; 132 bookmark.name = [name text]; 133 134 [self.tableView reloadData]; 135 }]; 136 137 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") style:UIAlertActionStyleCancel handler:nil]; 138 [alertController addAction:cancelAction]; 139 [alertController addAction:okAction]; 140 141 [self presentViewController:alertController animated:YES completion:nil]; 142 } 143} 144 145- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 146{ 147 return YES; 148} 149 150- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 151{ 152 if (editingStyle == UITableViewCellEditingStyleDelete) { 153 [[Bookmark list] removeObjectAtIndex:[indexPath row]]; 154 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 155 } 156} 157 158- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 159{ 160 Bookmark *s = [Bookmark list][[sourceIndexPath row]]; 161 [[Bookmark list] removeObjectAtIndex:[sourceIndexPath row]]; 162 [[Bookmark list] insertObject:s atIndex:[destinationIndexPath row]]; 163} 164 165- (void)addItem:sender 166{ 167 UIAlertController *uiac = [Bookmark addBookmarkDialogWithOkCallback:^{ 168 [self.tableView reloadData]; 169 }]; 170 171 [self presentViewController:uiac animated:YES completion:nil]; 172} 173 174- (void)didLongPress:(UILongPressGestureRecognizer *)gestureRecognizer 175{ 176 CGPoint p = [gestureRecognizer locationInView:[self tableView]]; 177 178 NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:p]; 179 if (gestureRecognizer.state == UIGestureRecognizerStateBegan && indexPath != nil) { 180 [[self tableView] setEditing:YES animated:YES]; 181 182 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneEditing:)]; 183 self.navigationItem.leftBarButtonItem = nil; 184 } 185} 186 187- (void)doneEditing:sender 188{ 189 [[self tableView] setEditing:NO animated:YES]; 190 self.navigationItem.rightBarButtonItem = addItem; 191 self.navigationItem.leftBarButtonItem = leftItem; 192} 193 194- (void)close 195{ 196 [self removeFromParentViewController]; 197 [[self view] removeFromSuperview]; 198} 199 200@end