+/* Add Source View {{{ */
+@interface AddSourceView : RVPage {
+ _transient Database *database_;
+}
+
+- (id) initWithBook:(RVBook *)book database:(Database *)database;
+
+@end
+
+@implementation AddSourceView
+
+- (id) initWithBook:(RVBook *)book database:(Database *)database {
+ if ((self = [super initWithBook:book]) != nil) {
+ database_ = database;
+ } return self;
+}
+
+@end
+/* }}} */
+/* Source Cell {{{ */
+@interface SourceCell : UITableCell {
+ UIImage *icon_;
+ NSString *origin_;
+ NSString *description_;
+ NSString *label_;
+}
+
+- (void) dealloc;
+
+- (SourceCell *) initWithSource:(Source *)source;
+
+@end
+
+@implementation SourceCell
+
+- (void) dealloc {
+ [icon_ release];
+ [origin_ release];
+ [description_ release];
+ [label_ release];
+ [super dealloc];
+}
+
+- (SourceCell *) initWithSource:(Source *)source {
+ if ((self = [super init]) != nil) {
+ if (icon_ == nil)
+ icon_ = [UIImage applicationImageNamed:[NSString stringWithFormat:@"Sources/%@.png", [source host]]];
+ if (icon_ == nil)
+ icon_ = [UIImage applicationImageNamed:@"unknown.png"];
+ icon_ = [icon_ retain];
+
+ origin_ = [[source name] retain];
+ label_ = [[source uri] retain];
+ description_ = [[source description] retain];
+ } return self;
+}
+
+- (void) drawContentInRect:(CGRect)rect selected:(BOOL)selected {
+ if (icon_ != nil)
+ [icon_ drawInRect:CGRectMake(10, 10, 30, 30)];
+
+ if (selected)
+ UISetColor(White_);
+
+ if (!selected)
+ UISetColor(Black_);
+ [origin_ drawAtPoint:CGPointMake(48, 8) forWidth:240 withFont:Font18Bold_ ellipsis:2];
+
+ if (!selected)
+ UISetColor(Blue_);
+ [label_ drawAtPoint:CGPointMake(58, 29) forWidth:225 withFont:Font12_ ellipsis:2];
+
+ if (!selected)
+ UISetColor(Gray_);
+ [description_ drawAtPoint:CGPointMake(12, 46) forWidth:280 withFont:Font14_ ellipsis:2];
+
+ [super drawContentInRect:rect selected:selected];
+}
+
+@end
+/* }}} */
+/* Source Table {{{ */
+@interface SourceTable : RVPage {
+ _transient Database *database_;
+ UISectionList *list_;
+ NSMutableArray *sources_;
+ UIAlertSheet *alert_;
+ int offset_;
+
+ NSString *href_;
+ UIProgressHUD *hud_;
+ NSError *error_;
+
+ //NSURLConnection *installer_;
+ NSURLConnection *trivial_bz2_;
+ NSURLConnection *trivial_gz_;
+ //NSURLConnection *automatic_;
+
+ BOOL trivial_;
+}
+
+- (id) initWithBook:(RVBook *)book database:(Database *)database;
+
+@end
+
+@implementation SourceTable
+
+- (void) _deallocConnection:(NSURLConnection *)connection {
+ if (connection != nil) {
+ [connection cancel];
+ //[connection setDelegate:nil];
+ [connection release];
+ }
+}
+
+- (void) dealloc {
+ [[list_ table] setDelegate:nil];
+ [list_ setDataSource:nil];
+
+ if (href_ != nil)
+ [href_ release];
+ if (hud_ != nil)
+ [hud_ release];
+ if (error_ != nil)
+ [error_ release];
+
+ //[self _deallocConnection:installer_];
+ [self _deallocConnection:trivial_gz_];
+ [self _deallocConnection:trivial_bz2_];
+ //[self _deallocConnection:automatic_];
+
+ [sources_ release];
+ [list_ release];
+ [super dealloc];
+}
+
+- (int) numberOfSectionsInSectionList:(UISectionList *)list {
+ return offset_ == 0 ? 1 : 2;
+}
+
+- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section {
+ switch (section + (offset_ == 0 ? 1 : 0)) {
+ case 0: return @"Entered by User";
+ case 1: return @"Installed by Packages";
+
+ default:
+ _assert(false);
+ return nil;
+ }
+}
+
+- (int) sectionList:(UISectionList *)list rowForSection:(int)section {
+ switch (section + (offset_ == 0 ? 1 : 0)) {
+ case 0: return 0;
+ case 1: return offset_;
+
+ default:
+ _assert(false);
+ return -1;
+ }
+}
+
+- (int) numberOfRowsInTable:(UITable *)table {
+ return [sources_ count];
+}
+
+- (float) table:(UITable *)table heightForRow:(int)row {
+ Source *source = [sources_ objectAtIndex:row];
+ return [source description] == nil ? 56 : 73;
+}
+
+- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col {
+ Source *source = [sources_ objectAtIndex:row];
+ // XXX: weird warning, stupid selectors ;P
+ return [[[SourceCell alloc] initWithSource:(id)source] autorelease];
+}
+
+- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row {
+ return YES;
+}
+
+- (BOOL) table:(UITable *)table canSelectRow:(int)row {
+ return YES;
+}
+
+- (void) tableRowSelected:(NSNotification*)notification {
+ UITable *table([list_ table]);
+ int row([table selectedRow]);
+ if (row == INT_MAX)
+ return;
+
+ Source *source = [sources_ objectAtIndex:row];
+
+ PackageTable *packages = [[[PackageTable alloc]
+ initWithBook:book_
+ database:database_
+ title:[source label]
+ filter:@selector(isVisibleInSource:)
+ with:source
+ ] autorelease];
+
+ [packages setDelegate:delegate_];
+
+ [book_ pushPage:packages];
+}
+
+- (BOOL) table:(UITable *)table canDeleteRow:(int)row {
+ Source *source = [sources_ objectAtIndex:row];
+ return [source record] != nil;
+}
+
+- (void) table:(UITable *)table willSwipeToDeleteRow:(int)row {
+ [[list_ table] setDeleteConfirmationRow:row];
+}
+
+- (void) table:(UITable *)table deleteRow:(int)row {
+ Source *source = [sources_ objectAtIndex:row];
+ [Sources_ removeObjectForKey:[source key]];
+ [delegate_ syncData];
+}
+
+- (void) _endConnection:(NSURLConnection *)connection {
+ NSURLConnection **field = NULL;
+ if (connection == trivial_bz2_)
+ field = &trivial_bz2_;
+ else if (connection == trivial_gz_)
+ field = &trivial_gz_;
+ _assert(field != NULL);
+ [connection release];
+ *field = nil;
+
+ if (
+ trivial_bz2_ == nil &&
+ trivial_gz_ == nil
+ ) {
+ [delegate_ setStatusBarShowsProgress:NO];
+
+ [hud_ show:NO];
+ [hud_ removeFromSuperview];
+ [hud_ autorelease];
+ hud_ = nil;
+
+ if (trivial_) {
+ [Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ @"deb", @"Type",
+ href_, @"URI",
+ @"./", @"Distribution",
+ nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href_]];
+
+ [delegate_ syncData];
+ } else if (error_ != nil) {
+ UIAlertSheet *sheet = [[[UIAlertSheet alloc]
+ initWithTitle:@"Verification Error"
+ buttons:[NSArray arrayWithObjects:@"OK", nil]
+ defaultButtonIndex:0
+ delegate:self
+ context:@"urlerror"
+ ] autorelease];
+
+ [sheet setBodyText:[error_ localizedDescription]];
+ [sheet popupAlertAnimated:YES];
+ } else {
+ UIAlertSheet *sheet = [[[UIAlertSheet alloc]
+ initWithTitle:@"Did not Find Repository"
+ buttons:[NSArray arrayWithObjects:@"OK", nil]
+ defaultButtonIndex:0
+ delegate:self
+ context:@"trivial"
+ ] autorelease];
+
+ [sheet setBodyText:@"The indicated repository could not be found. This could be because you are trying to add a legacy Installer repository (these are not supported). Also, this interface is only capable of working with exact repository URLs. If you host a repository and are having issues please contact the author of Cydia with any questions you have."];
+ [sheet popupAlertAnimated:YES];
+ }
+
+ [href_ release];
+ href_ = nil;
+
+ if (error_ != nil) {
+ [error_ release];
+ error_ = nil;
+ }
+ }
+}
+
+- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response {
+ switch ([response statusCode]) {
+ case 200:
+ trivial_ = YES;
+ }
+}
+
+- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
+ fprintf(stderr, "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];
+
+ return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
+}
+
+- (void) alertSheet:(UIAlertSheet *)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_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];
+
+ trivial_ = false;
+
+ hud_ = [delegate_ addProgressHUD];
+ [hud_ setText:@"Verifying URL"];
+ } break;
+
+ case 2:
+ break;
+
+ default:
+ _assert(false);
+ }
+
+ [sheet dismiss];
+}
+
+- (id) initWithBook:(RVBook *)book database:(Database *)database {
+ if ((self = [super initWithBook:book]) != nil) {
+ database_ = database;
+ sources_ = [[NSMutableArray arrayWithCapacity:16] retain];
+
+ //list_ = [[UITable alloc] initWithFrame:[self bounds]];
+ list_ = [[UISectionList alloc] initWithFrame:[self bounds] showSectionIndex:NO];
+ [list_ setShouldHideHeaderInShortLists:NO];
+
+ [self addSubview:list_];
+ [list_ setDataSource:self];
+
+ UITableColumn *column = [[UITableColumn alloc]
+ initWithTitle:@"Name"
+ identifier:@"name"
+ width:[self frame].size.width
+ ];
+
+ UITable *table = [list_ table];
+ [table setSeparatorStyle:1];
+ [table addTableColumn:column];
+ [table setDelegate:self];
+
+ [self reloadData];
+ } return self;
+}
+
+- (void) reloadData {
+ pkgSourceList list;
+ _assert(list.ReadMainList());
+
+ [sources_ removeAllObjects];
+ [sources_ addObjectsFromArray:[database_ sources]];
+ [sources_ sortUsingSelector:@selector(compareByNameAndType:)];
+
+ int count = [sources_ count];
+ for (offset_ = 0; offset_ != count; ++offset_) {
+ Source *source = [sources_ objectAtIndex:offset_];
+ if ([source record] == nil)
+ break;
+ }
+
+ [list_ reloadData];
+}
+
+- (void) resetViewAnimated:(BOOL)animated {
+ [list_ resetViewAnimated:animated];
+}
+
+- (void) _leftButtonClicked {
+ /*[book_ pushPage:[[[AddSourceView alloc]
+ initWithBook:book_
+ database:database_
+ ] autorelease]];*/
+
+ UIAlertSheet *sheet = [[[UIAlertSheet alloc]
+ initWithTitle:@"Enter Cydia/APT URL"
+ buttons:[NSArray arrayWithObjects:@"Add Source", @"Cancel", nil]
+ defaultButtonIndex:0
+ delegate:self
+ context:@"source"
+ ] autorelease];
+
+ [sheet addTextFieldWithValue:@"http://" label:@""];
+
+ UITextTraits *traits = [[sheet textField] textTraits];
+ [traits setAutoCapsType:0];
+ [traits setPreferredKeyboardType:3];
+ [traits setAutoCorrectionType:1];
+
+ [sheet popupAlertAnimated:YES];
+}
+
+- (void) _rightButtonClicked {
+ UITable *table = [list_ table];
+ BOOL editing = [table isRowDeletionEnabled];
+ [table enableRowDeletion:!editing animated:YES];
+ [book_ reloadButtonsForPage:self];
+}
+
+- (NSString *) title {
+ return @"Sources";
+}
+
+- (NSString *) backButtonTitle {
+ return @"Sources";
+}
+
+- (NSString *) leftButtonTitle {
+ return [[list_ table] isRowDeletionEnabled] ? @"Add" : nil;
+}
+
+- (NSString *) rightButtonTitle {
+ return [[list_ table] isRowDeletionEnabled] ? @"Done" : @"Edit";
+}
+
+- (RVUINavBarButtonStyle) rightButtonStyle {
+ return [[list_ table] isRowDeletionEnabled] ? RVUINavBarButtonStyleHighlighted : RVUINavBarButtonStyleNormal;
+}
+
+@end
+/* }}} */
+
+/* Installed View {{{ */
+@interface InstalledView : RVPage {
+ _transient Database *database_;
+ PackageTable *packages_;
+ BOOL expert_;
+}
+
+- (id) initWithBook:(RVBook *)book database:(Database *)database;
+
+@end
+
+@implementation InstalledView
+
+- (void) dealloc {
+ [packages_ release];
+ [super dealloc];
+}
+
+- (id) initWithBook:(RVBook *)book database:(Database *)database {
+ if ((self = [super initWithBook:book]) != nil) {
+ database_ = database;
+
+ packages_ = [[PackageTable alloc]
+ initWithBook:book
+ database:database
+ title:nil
+ filter:@selector(isInstalledAndVisible:)
+ with:[NSNumber numberWithBool:YES]
+ ];
+
+ [self addSubview:packages_];
+ } return self;
+}
+
+- (void) resetViewAnimated:(BOOL)animated {
+ [packages_ resetViewAnimated:animated];
+}
+
+- (void) reloadData {
+ [packages_ reloadData];
+}
+
+- (void) _rightButtonClicked {
+ [packages_ setObject:[NSNumber numberWithBool:expert_]];
+ [packages_ reloadData];
+ expert_ = !expert_;
+ [book_ reloadButtonsForPage:self];
+}
+
+- (NSString *) title {
+ return @"Installed";
+}
+
+- (NSString *) backButtonTitle {
+ return @"Packages";
+}
+
+- (NSString *) rightButtonTitle {
+ return Role_ != nil && [Role_ isEqualToString:@"Developer"] ? nil : expert_ ? @"Expert" : @"Simple";
+}
+
+- (RVUINavBarButtonStyle) rightButtonStyle {
+ return expert_ ? RVUINavBarButtonStyleHighlighted : RVUINavBarButtonStyleNormal;
+}
+
+- (void) setDelegate:(id)delegate {
+ [super setDelegate:delegate];
+ [packages_ setDelegate:delegate];
+}
+
+@end
+/* }}} */
+
+/* Home View {{{ */
+@interface HomeView : BrowserView {
+}
+
+@end
+
+@implementation HomeView
+
+- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
+ [sheet dismiss];
+}
+
+- (void) _leftButtonClicked {
+ UIAlertSheet *sheet = [[[UIAlertSheet alloc]
+ initWithTitle:@"About Cydia Installer"
+ buttons:[NSArray arrayWithObjects:@"Close", nil]
+ defaultButtonIndex:0
+ delegate:self
+ context:@"about"
+ ] autorelease];
+
+ [sheet setBodyText:
+ @"Copyright (C) 2008\n"
+ "Jay Freeman (saurik)\n"
+ "saurik@saurik.com\n"
+ "http://www.saurik.com/\n"
+ "\n"
+ "The Okori Group\n"
+ "http://www.theokorigroup.com/\n"
+ "\n"
+ "College of Creative Studies,\n"
+ "University of California,\n"
+ "Santa Barbara\n"
+ "http://www.ccs.ucsb.edu/"
+ ];
+
+ [sheet popupAlertAnimated:YES];
+}
+
+- (NSString *) leftButtonTitle {
+ return @"About";
+}
+
+@end
+/* }}} */
+/* Manage View {{{ */
+@interface ManageView : BrowserView {
+}
+
+@end
+
+@implementation ManageView
+
+- (NSString *) title {
+ return @"Manage";
+}
+
+- (void) _leftButtonClicked {
+ [delegate_ askForSettings];
+}
+
+- (NSString *) leftButtonTitle {
+ return @"Settings";
+}
+
+- (NSString *) _rightButtonTitle {
+ return nil;
+}
+
+@end
+/* }}} */
+
+/* Browser Implementation {{{ */
+@implementation BrowserView
+