iOS web browser with a focus on security and privacy
1/*
2 * Endless
3 * Copyright (c) 2014-2015 joshua stein <jcs@jcs.org>
4 *
5 * See LICENSE file for redistribution terms.
6 */
7
8#import "RuleEditorController.h"
9
10@implementation RuleEditorController
11
12UISearchDisplayController *searchDisplayController;
13
14- (id)initWithStyle:(UITableViewStyle)style
15{
16 self = [super initWithStyle:style];
17
18 self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
19
20 self.sortedRuleNames = [[NSMutableArray alloc] init];
21 self.inUseRuleNames = [[NSMutableArray alloc] init];
22
23 self.searchResult = [[NSMutableArray alloc] init];
24
25 return self;
26}
27
28- (void)viewDidLoad
29{
30 [super viewDidLoad];
31
32 self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
33
34 searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
35 searchDisplayController.delegate = self;
36 searchDisplayController.searchResultsDataSource = self;
37
38 [[self tableView] setTableHeaderView:self.searchBar];
39}
40
41- (void)didReceiveMemoryWarning
42{
43 [super didReceiveMemoryWarning];
44 // Dispose of any resources that can be recreated.
45}
46
47#pragma mark - Table view data source
48
49- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
50{
51 return 2;
52}
53
54- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
55{
56 if (section == 0)
57 return @"Rules in use on current page";
58 else
59 return @"All rules";
60}
61
62- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
63{
64 if (section == 0)
65 return [self.inUseRuleNames count];
66 else if (tableView == self.searchDisplayController.searchResultsTableView)
67 return [self.searchResult count];
68 else
69 return [self.sortedRuleNames count];
70}
71
72- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
73{
74 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rule"];
75 if (cell == nil)
76 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"rule"];
77
78 /* TODO: once we have a per-rule view page, enable this */
79 //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
80
81 cell.textLabel.text = [self ruleForTableView:tableView atIndexPath:indexPath];
82
83 NSString *disabled = [self ruleDisabledReason:cell.textLabel.text];
84 if (disabled == nil) {
85 cell.textLabel.textColor = [UIColor darkTextColor];
86 cell.detailTextLabel.text = nil;
87 }
88 else {
89 cell.textLabel.textColor = [UIColor redColor];
90 cell.detailTextLabel.text = [NSString stringWithFormat:@"Disabled: %@", disabled];
91 cell.detailTextLabel.textColor = [UIColor redColor];
92 }
93
94 return cell;
95}
96
97- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
98{
99 return YES;
100}
101
102- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
103{
104 return NO;
105}
106
107- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
108{
109 if (editingStyle == UITableViewCellEditingStyleDelete) {
110 NSString *row = [self ruleForTableView:tableView atIndexPath:indexPath];
111
112 if ([self ruleDisabledReason:row] == nil)
113 [self disableRuleByName:row withReason:@"User disabled"];
114 else
115 [self enableRuleByName:row];
116 }
117
118 [tableView reloadData];
119}
120
121- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
122{
123 [tableView deselectRowAtIndexPath:indexPath animated:YES];
124}
125
126- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
127{
128 [self.searchResult removeAllObjects];
129
130 for (NSString *ruleName in self.sortedRuleNames) {
131 NSRange range = [ruleName rangeOfString:searchString options:NSCaseInsensitiveSearch];
132
133 if (range.length > 0)
134 [self.searchResult addObject:ruleName];
135 }
136
137 return YES;
138}
139
140-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
141 NSString *row = [self ruleForTableView:tableView atIndexPath:indexPath];
142
143 if ([self ruleDisabledReason:row] == nil)
144 return @"Disable";
145 else
146 return @"Enable";
147}
148
149- (NSString *)ruleForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
150{
151 NSMutableArray *group;
152
153 if ([indexPath section] == 0)
154 group = [self inUseRuleNames];
155 else if (tableView == self.searchDisplayController.searchResultsTableView)
156 group = [self searchResult];
157 else
158 group = [self sortedRuleNames];
159
160 if (group && [group count] > [indexPath row])
161 return [group objectAtIndex:indexPath.row];
162 else
163 return nil;
164}
165
166- (NSString *)ruleDisabledReason:(NSString *)rule
167{
168 return nil;
169}
170
171- (void)disableRuleByName:(NSString *)rule withReason:(NSString *)reason
172{
173 abort();
174}
175
176- (void)enableRuleByName:(NSString *)rule
177{
178 abort();
179}
180
181@end