-
Notifications
You must be signed in to change notification settings - Fork 2
/
SourceTable.mm
417 lines (333 loc) · 12.3 KB
/
SourceTable.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#import "SourceTable.h"
#import "PackageTable.h"
#import "FilteredPackageTable.h"
@implementation SourceTable
- (void) _deallocConnection:(NSURLConnection *)connection {
if (connection != nil) {
[connection cancel];
//[connection setDelegate:nil];
[connection release];
}
}
- (void) dealloc {
if (href_ != nil)
[href_ release];
if (hud_ != nil)
[hud_ release];
if (error_ != nil)
[error_ release];
//[self _deallocConnection:installer_];
[self _deallocConnection:trivial_];
[self _deallocConnection:trivial_gz_];
[self _deallocConnection:trivial_bz2_];
//[self _deallocConnection:automatic_];
[sources_ release];
[list_ release];
[super dealloc];
}
- (int) numberOfSectionsInTableView:(UITableView *)tableView {
return offset_ == 0 ? 1 : 2;
}
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
switch (section + (offset_ == 0 ? 1 : 0)) {
case 0: return UCLocalize("ENTERED_BY_USER");
case 1: return UCLocalize("INSTALLED_BY_PACKAGE");
_nodefault
}
}
- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
int count = [sources_ count];
switch (section) {
case 0: return (offset_ == 0 ? count : offset_);
case 1: return count - offset_;
_nodefault
}
}
- (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath {
unsigned idx = 0;
switch (indexPath.section) {
case 0: idx = indexPath.row; break;
case 1: idx = indexPath.row + offset_; break;
_nodefault
}
return [sources_ objectAtIndex:idx];
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Source *source = [self sourceAtIndexPath:indexPath];
return [source description] == nil ? 56 : 73;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Source *source = [self sourceAtIndexPath:indexPath];
// XXX: weird warning, stupid selectors ;P
static NSString *cellIdentifier = @"Cell";
SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
cell = [[[SourceCell alloc] initWithSource:(id)source] autorelease];
else
[cell setSource:source];
return cell;
}
- (int) tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
return 1; //UITableViewCellAccessoryDisclosureIndicator?
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Source *source = [self sourceAtIndexPath:indexPath];
PackageTable *packages = [[[FilteredPackageTable alloc]
initWithBook:book_
database:database_
title:[source label]
filter:@selector(isVisibleInSource:)
with:source
] autorelease];
[packages setDelegate:delegate_];
[book_ pushPage:packages];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
Source *source = [self sourceAtIndexPath:indexPath];
return [source record] != nil;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
Source *source = [self sourceAtIndexPath:indexPath];
[Sources_ removeObjectForKey:[source key]];
[delegate_ syncData];
}
- (void) complete {
[Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys:
@"deb", @"Type",
href_, @"URI",
@"./", @"Distribution",
nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href_]];
[delegate_ syncData];
}
- (NSString *) getWarning {
NSString *href(href_);
NSRange colon([href rangeOfString:@"://"]);
if (colon.location != NSNotFound)
href = [href substringFromIndex:(colon.location + 3)];
href = [href stringByAddingPercentEscapes];
href = [CydiaURL(@"api/repotag/") stringByAppendingString:href];
href = [href stringByCachingURLWithCurrentCDN];
NSURL *url([NSURL URLWithString:href]);
NSStringEncoding encoding;
NSError *error(nil);
if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error])
return [warning length] == 0 ? nil : warning;
return nil;
}
- (void) _endConnection:(NSURLConnection *)connection {
NSURLConnection **field = NULL;
if (connection == trivial_)
field = &trivial_;
else if (connection == trivial_bz2_)
field = &trivial_bz2_;
else if (connection == trivial_gz_)
field = &trivial_gz_;
_assert(field != NULL);
[connection release];
*field = nil;
if (
trivial_ == nil &&
trivial_bz2_ == nil &&
trivial_gz_ == nil
) {
bool defer(false);
if (cydia_) {
if (NSString *warning = [self yieldToSelector:@selector(getWarning)]) {
defer = true;
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:UCLocalize("SOURCE_WARNING")
buttons:[NSArray arrayWithObjects:UCLocalize("ADD_ANYWAY"), UCLocalize("CANCEL"), nil]
defaultButtonIndex:0
delegate:self
context:@"warning"
] autorelease];
[sheet setNumberOfRows:1];
[sheet setBodyText:warning];
[sheet popupAlertAnimated:YES];
} else
[self complete];
} else if (error_ != nil) {
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:UCLocalize("VERIFICATION_ERROR")
buttons:[NSArray arrayWithObjects:UCLocalize("OK"), nil]
defaultButtonIndex:0
delegate:self
context:@"urlerror"
] autorelease];
[sheet setBodyText:[error_ localizedDescription]];
[sheet popupAlertAnimated:YES];
} else {
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:UCLocalize("NOT_REPOSITORY")
buttons:[NSArray arrayWithObjects:UCLocalize("OK"), nil]
defaultButtonIndex:0
delegate:self
context:@"trivial"
] autorelease];
[sheet setBodyText:UCLocalize("NOT_REPOSITORY_EX")];
[sheet popupAlertAnimated:YES];
}
[delegate_ setStatusBarShowsProgress:NO];
[delegate_ removeProgressHUD:hud_];
[hud_ autorelease];
hud_ = nil;
if (!defer) {
[href_ release];
href_ = nil;
}
if (error_ != nil) {
[error_ release];
error_ = nil;
}
}
}
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
switch ([response statusCode]) {
case 200:
cydia_ = YES;
}
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
lprintf("connection:\"%s\" didFailWithError:\"%s\"", [href_ UTF8String], [[error localizedDescription] UTF8String]);
if (error_ != nil)
error_ = [error retain];
[self _endConnection:connection];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[self _endConnection:connection];
}
- (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method {
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:href]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0
];
[request setHTTPMethod:method];
if (Machine_ != NULL)
[request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
if (UniqueID_ != nil)
[request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
if (Role_ != nil)
[request setValue:Role_ forHTTPHeaderField:@"X-Role"];
return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
}
- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
NSString *context([sheet context]);
if ([context isEqualToString:@"source"]) {
switch (button) {
case 1: {
NSString *href = [[sheet textField] text];
//installer_ = [[self _requestHRef:href method:@"GET"] retain];
if (![href hasSuffix:@"/"])
href_ = [href stringByAppendingString:@"/"];
else
href_ = href;
href_ = [href_ retain];
trivial_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages"] method:@"HEAD"] retain];
trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain];
trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain];
//trivial_bz2_ = [[self _requestHRef:[href stringByAppendingString:@"dists/Release"] method:@"HEAD"] retain];
cydia_ = false;
hud_ = [[delegate_ addProgressHUD] retain];
[hud_ setText:UCLocalize("VERIFYING_URL")];
} break;
case 2:
break;
_nodefault
}
[sheet dismiss];
} else if ([context isEqualToString:@"trivial"])
[sheet dismiss];
else if ([context isEqualToString:@"urlerror"])
[sheet dismiss];
else if ([context isEqualToString:@"warning"]) {
switch (button) {
case 1:
[self complete];
break;
case 2:
break;
_nodefault
}
[href_ release];
href_ = nil;
[sheet dismiss];
}
}
- (id) initWithBook:(RVBook *)book database:(Database *)database {
if ((self = [super initWithBook:book]) != nil) {
database_ = database;
sources_ = [[NSMutableArray arrayWithCapacity:16] retain];
list_ = [[UITableView alloc] initWithFrame:[self bounds] style:UITableViewStylePlain];
[self addSubview:list_];
[list_ setDataSource:self];
[list_ setDelegate:self];
[self reloadData];
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[list_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
} return self;
}
- (void) reloadData {
pkgSourceList list;
if (!list.ReadMainList())
return;
[sources_ removeAllObjects];
[sources_ addObjectsFromArray:[database_ sources]];
_trace();
[sources_ sortUsingSelector:@selector(compareByNameAndType:)];
_trace();
int count([sources_ count]);
int i = 0;
offset_ = 0;
for (i = 0; i != count; i++) {
Source *source = [sources_ objectAtIndex:i];
if ([source record] == nil)
break;
else
offset_++;
}
[list_ reloadData];
}
- (void) resetViewAnimated:(BOOL)animated {
[list_ resetViewAnimated:animated];
}
- (void) _leftButtonClicked {
/*[book_ pushPage:[[[AddSourceView alloc]
initWithBook:book_
database:database_
] autorelease]];*/
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:UCLocalize("ENTER_APT_URL")
buttons:[NSArray arrayWithObjects:UCLocalize("ADD_SOURCE"), UCLocalize("CANCEL"), nil]
defaultButtonIndex:0
delegate:self
context:@"source"
] autorelease];
[sheet setNumberOfRows:1];
[sheet addTextFieldWithValue:@"http://" label:@""];
UITextInputTraits *traits = [[sheet textField] textInputTraits];
[traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[traits setAutocorrectionType:UITextAutocorrectionTypeNo];
[traits setKeyboardType:UIKeyboardTypeURL];
// XXX: UIReturnKeyDone
[traits setReturnKeyType:UIReturnKeyNext];
[sheet popupAlertAnimated:YES];
}
- (void) _rightButtonClicked {
[list_ setEditing:![list_ isEditing] animated:YES];
[book_ reloadButtonsForPage:self];
}
- (NSString *) title {
return UCLocalize("SOURCES");
}
- (NSString *) leftButtonTitle {
return [list_ isEditing] ? UCLocalize("ADD") : nil;
}
- (id) rightButtonTitle {
return [list_ isEditing] ? UCLocalize("DONE") : UCLocalize("EDIT");
}
- (UINavigationButtonStyle) rightButtonStyle {
return [list_ isEditing] ? UINavigationButtonStyleHighlighted : UINavigationButtonStyleNormal;
}
@end
/* }}} */