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