+ [navbar_ setBarStyle:1];
+ [navbar_ setDelegate:self];
+
+ UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:nil] autorelease];
+ [navbar_ pushNavigationItem:navitem];
+
+ CGRect bounds = [overlay_ bounds];
+ CGSize prgsize = [UIProgressBar defaultSize];
+
+ CGRect prgrect = {{
+ (bounds.size.width - prgsize.width) / 2,
+ bounds.size.height - prgsize.height - 20
+ }, prgsize};
+
+ progress_ = [[UIProgressBar alloc] initWithFrame:prgrect];
+ [progress_ setStyle:0];
+
+ status_ = [[UITextLabel alloc] initWithFrame:CGRectMake(
+ 10,
+ bounds.size.height - prgsize.height - 50,
+ bounds.size.width - 20,
+ 24
+ )];
+
+ [status_ setColor:White_];
+ [status_ setBackgroundColor:Clear_];
+
+ [status_ setCentersHorizontally:YES];
+ //[status_ setFont:font];
+
+ output_ = [[UITextView alloc] initWithFrame:CGRectMake(
+ 10,
+ navrect.size.height + 20,
+ bounds.size.width - 20,
+ bounds.size.height - navsize.height - 62 - navrect.size.height
+ )];
+
+ //[output_ setTextFont:@"Courier New"];
+ [output_ setTextSize:12];
+
+ [output_ setTextColor:White_];
+ [output_ setBackgroundColor:Clear_];
+
+ [output_ setMarginTop:0];
+ [output_ setAllowsRubberBanding:YES];
+ [output_ setEditable:NO];
+
+ [overlay_ addSubview:output_];
+
+ close_ = [[UIPushButton alloc] initWithFrame:CGRectMake(
+ 10,
+ bounds.size.height - prgsize.height - 50,
+ bounds.size.width - 20,
+ 32 + prgsize.height
+ )];
+
+ [close_ setAutosizesToFit:NO];
+ [close_ setDrawsShadow:YES];
+ [close_ setStretchBackground:YES];
+ [close_ setEnabled:YES];
+
+ GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 22);
+ [close_ setTitleFont:bold];
+ CFRelease(bold);
+
+ [close_ addTarget:self action:@selector(closeButtonPushed) forEvents:kUIControlEventMouseUpInside];
+ [close_ setBackground:[UIImage applicationImageNamed:@"green-up.png"] forState:0];
+ [close_ setBackground:[UIImage applicationImageNamed:@"green-dn.png"] forState:1];
+ } return self;
+}
+
+- (void) setContentView:(UIView *)view {
+ view_ = [view retain];
+}
+
+- (void) resetView {
+ [transition_ transition:6 toView:view_];
+}
+
+- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
+ NSString *context = [sheet context];
+ if ([context isEqualToString:@"conffile"]) {
+ FILE *input = [database_ input];
+
+ switch (button) {
+ case 1:
+ fprintf(input, "N\n");
+ fflush(input);
+ break;
+ case 2:
+ fprintf(input, "Y\n");
+ fflush(input);
+ break;
+ default:
+ _assert(false);
+ }
+ }
+
+ [sheet dismiss];
+}
+
+- (void) closeButtonPushed {
+ switch (Finish_) {
+ case 0:
+ [delegate_ progressViewIsComplete:self];
+ [self resetView];
+ break;
+
+ case 1:
+ [delegate_ suspendWithAnimation:YES];
+ break;
+
+ case 2:
+ system("killall SpringBoard");
+ break;
+
+ case 3:
+ system("launchctl unload /System/Library/LaunchDaemons/com.apple.SpringBoard.plist; launchctl load /System/Library/LaunchDaemons/com.apple.SpringBoard.plist");
+ break;
+
+ case 4:
+ system("reboot");
+ break;
+ }
+}
+
+- (void) _retachThread {
+ UINavigationItem *item = [navbar_ topItem];
+ [item setTitle:@"Complete"];
+
+ [overlay_ addSubview:close_];
+ [progress_ removeFromSuperview];
+ [status_ removeFromSuperview];
+
+ switch (Finish_) {
+ case 0: [close_ setTitle:@"Return to Cydia"]; break;
+ case 1: [close_ setTitle:@"Close Cydia (Restart)"]; break;
+ case 2: [close_ setTitle:@"Restart SpringBoard"]; break;
+ case 3: [close_ setTitle:@"Reload SpringBoard"]; break;
+ case 4: [close_ setTitle:@"Reboot Device"]; break;
+ }
+
+#ifdef __OBJC2__
+ notify_post("com.apple.mobile.application_installed");
+#endif
+
+ [delegate_ setStatusBarShowsProgress:NO];
+
+ running_ = NO;
+}
+
+- (void) _detachNewThreadData:(ProgressData *)data {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ [[data target] performSelector:[data selector] withObject:[data object]];
+ [data release];
+
+ [self performSelectorOnMainThread:@selector(_retachThread) withObject:nil waitUntilDone:YES];
+
+ [pool release];
+}
+
+- (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title {
+ UINavigationItem *item = [navbar_ topItem];
+ [item setTitle:title];
+
+ [status_ setText:nil];
+ [output_ setText:@""];
+ [progress_ setProgress:0];
+
+ [close_ removeFromSuperview];
+ [overlay_ addSubview:progress_];
+ [overlay_ addSubview:status_];
+
+ [delegate_ setStatusBarShowsProgress:YES];
+ running_ = YES;
+
+ [transition_ transition:6 toView:overlay_];
+
+ [NSThread
+ detachNewThreadSelector:@selector(_detachNewThreadData:)
+ toTarget:self
+ withObject:[[ProgressData alloc]
+ initWithSelector:selector
+ target:target
+ object:object
+ ]
+ ];
+}
+
+- (void) repairWithSelector:(SEL)selector {
+ [self
+ detachNewThreadSelector:selector
+ toTarget:database_
+ withObject:nil
+ title:@"Repairing..."
+ ];
+}
+
+- (void) setConfigurationData:(NSString *)data {
+ [self
+ performSelectorOnMainThread:@selector(_setConfigurationData:)
+ withObject:data
+ waitUntilDone:YES
+ ];
+}
+
+- (void) setProgressError:(NSString *)error forPackage:(NSString *)id {
+ Package *package = id == nil ? nil : [database_ packageWithName:id];
+
+ UIAlertSheet *sheet = [[[UIAlertSheet alloc]
+ initWithTitle:(package == nil ? @"Source Error" : [package name])
+ buttons:[NSArray arrayWithObjects:@"Okay", nil]
+ defaultButtonIndex:0
+ delegate:self
+ context:@"error"
+ ] autorelease];
+
+ [sheet setBodyText:error];
+ [sheet popupAlertAnimated:YES];
+}
+
+- (void) setProgressTitle:(NSString *)title {
+ [self
+ performSelectorOnMainThread:@selector(_setProgressTitle:)
+ withObject:title
+ waitUntilDone:YES
+ ];
+}
+
+- (void) setProgressPercent:(float)percent {
+ [self
+ performSelectorOnMainThread:@selector(_setProgressPercent:)
+ withObject:[NSNumber numberWithFloat:percent]
+ waitUntilDone:YES
+ ];
+}
+
+- (void) addProgressOutput:(NSString *)output {
+ [self
+ performSelectorOnMainThread:@selector(_addProgressOutput:)
+ withObject:output
+ waitUntilDone:YES
+ ];
+}
+
+- (void) _setConfigurationData:(NSString *)data {
+ static Pcre conffile_r("^'(.*)' '(.*)' ([01]) ([01])$");
+
+ _assert(conffile_r(data));
+
+ NSString *ofile = conffile_r[1];
+ //NSString *nfile = conffile_r[2];
+
+ UIAlertSheet *sheet = [[[UIAlertSheet alloc]
+ initWithTitle:@"Configuration Upgrade"
+ buttons:[NSArray arrayWithObjects:
+ @"Keep My Old Copy",
+ @"Accept The New Copy",
+ // XXX: @"See What Changed",
+ nil]
+ defaultButtonIndex:0
+ delegate:self
+ context:@"conffile"
+ ] autorelease];
+
+ [sheet setBodyText:[NSString stringWithFormat:
+ @"The following file has been changed by both the package maintainer and by you (or for you by a script).\n\n%@"
+ , ofile]];
+
+ [sheet popupAlertAnimated:YES];
+}
+
+- (void) _setProgressTitle:(NSString *)title {
+ [status_ setText:[title stringByAppendingString:@"..."]];
+}
+
+- (void) _setProgressPercent:(NSNumber *)percent {
+ [progress_ setProgress:[percent floatValue]];
+}
+
+- (void) _addProgressOutput:(NSString *)output {
+ [output_ setText:[NSString stringWithFormat:@"%@\n%@", [output_ text], output]];
+ CGSize size = [output_ contentSize];
+ CGRect rect = {{0, size.height}, {size.width, 0}};
+ [output_ scrollRectToVisible:rect animated:YES];
+}
+
+- (BOOL) isRunning {
+ return running_;
+}
+
+@end
+/* }}} */
+
+/* Package Cell {{{ */
+@interface PackageCell : UISimpleTableCell {
+ UIImage *icon_;
+ NSString *name_;
+ NSString *description_;
+ NSString *source_;
+ //UIImageView *trusted_;
+#ifdef USE_BADGES
+ UIImageView *badge_;
+ UITextLabel *status_;
+#endif
+}
+
+- (PackageCell *) init;
+- (void) setPackage:(Package *)package;
+
++ (int) heightForPackage:(Package *)package;
+
+@end
+
+@implementation PackageCell
+
+- (void) clearPackage {
+ if (icon_ != nil) {
+ [icon_ release];
+ icon_ = nil;
+ }
+
+ if (name_ != nil) {
+ [name_ release];
+ name_ = nil;
+ }
+
+ if (description_ != nil) {
+ [description_ release];
+ description_ = nil;
+ }
+
+ if (source_ != nil) {
+ [source_ release];
+ source_ = nil;
+ }
+}
+
+- (void) dealloc {
+ [self clearPackage];
+#ifdef USE_BADGES
+ [badge_ release];
+ [status_ release];
+#endif
+ //[trusted_ release];
+ [super dealloc];
+}
+
+- (PackageCell *) init {
+ if ((self = [super init]) != nil) {
+#ifdef USE_BADGES
+ badge_ = [[UIImageView alloc] initWithFrame:CGRectMake(17, 70, 16, 16)];
+
+ status_ = [[UITextLabel alloc] initWithFrame:CGRectMake(48, 68, 280, 20)];
+ [status_ setBackgroundColor:Clear_];
+ [status_ setFont:small];
+#endif
+ } return self;
+}
+
+- (void) setPackage:(Package *)package {
+ [self clearPackage];
+
+ Source *source = [package source];
+
+ icon_ = nil;
+ if (NSString *icon = [package icon])
+ icon_ = [UIImage imageAtPath:[icon substringFromIndex:6]];
+ if (icon_ == nil) if (NSString *section = [package section])
+ icon_ = [UIImage applicationImageNamed:[NSString stringWithFormat:@"Sections/%@.png", Simplify(section)]];
+ /*if (icon_ == nil) if (NSString *icon = [source defaultIcon])
+ icon_ = [UIImage imageAtPath:[icon substringFromIndex:6]];*/
+ if (icon_ == nil)
+ icon_ = [UIImage applicationImageNamed:@"unknown.png"];
+
+ icon_ = [icon_ retain];
+
+ name_ = [[package name] retain];
+ description_ = [[package tagline] retain];
+
+ NSString *label = nil;
+ bool trusted = false;
+
+ if (source != nil) {
+ label = [source label];
+ trusted = [source trusted];
+ } else if ([[package id] isEqualToString:@"firmware"])
+ label = @"Apple";
+ else
+ label = @"Unknown/Local";
+
+ NSString *from = [NSString stringWithFormat:@"from %@", label];
+
+ NSString *section = Simplify([package section]);
+ if (section != nil && ![section isEqualToString:label])
+ from = [from stringByAppendingString:[NSString stringWithFormat:@" (%@)", section]];
+
+ source_ = [from retain];
+
+#ifdef USE_BADGES
+ [badge_ removeFromSuperview];
+ [status_ removeFromSuperview];
+
+ if (NSString *mode = [package mode]) {
+ [badge_ setImage:[UIImage applicationImageNamed:
+ [mode isEqualToString:@"Remove"] || [mode isEqualToString:@"Purge"] ? @"removing.png" : @"installing.png"
+ ]];
+
+ [status_ setText:[NSString stringWithFormat:@"Queued for %@", mode]];
+ [status_ setColor:Blueish_];
+ } else if ([package half]) {
+ [badge_ setImage:[UIImage applicationImageNamed:@"damaged.png"]];
+ [status_ setText:@"Package Damaged"];
+ [status_ setColor:Red_];
+ } else {
+ [badge_ setImage:nil];
+ [status_ setText:nil];
+ goto done;
+ }
+
+ [self addSubview:badge_];
+ [self addSubview:status_];
+ done:;
+#endif
+}
+
+- (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_);
+ [name_ drawAtPoint:CGPointMake(48, 8) forWidth:240 withFont:Font18Bold_ ellipsis:2];
+ [source_ 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];
+}
+
++ (int) heightForPackage:(Package *)package {
+ NSString *tagline([package tagline]);
+ int height = tagline == nil || [tagline length] == 0 ? -17 : 0;
+#ifdef USE_BADGES
+ if ([package hasMode] || [package half])
+ return height + 96;
+ else
+#endif
+ return height + 73;
+}
+
+@end
+/* }}} */
+/* Section Cell {{{ */
+@interface SectionCell : UISimpleTableCell {
+ NSString *section_;
+ NSString *name_;
+ NSString *count_;
+ UIImage *icon_;
+ UISwitchControl *switch_;
+ BOOL editing_;
+}
+
+- (id) init;
+- (void) setSection:(Section *)section editing:(BOOL)editing;
+
+@end
+
+@implementation SectionCell
+
+- (void) clearSection {
+ if (section_ != nil) {
+ [section_ release];
+ section_ = nil;
+ }
+
+ if (name_ != nil) {
+ [name_ release];
+ name_ = nil;
+ }
+
+ if (count_ != nil) {
+ [count_ release];
+ count_ = nil;
+ }
+}
+
+- (void) dealloc {
+ [self clearSection];
+ [icon_ release];
+ [switch_ release];
+ [super dealloc];
+}
+
+- (id) init {
+ if ((self = [super init]) != nil) {
+ icon_ = [[UIImage applicationImageNamed:@"folder.png"] retain];
+
+ switch_ = [[UISwitchControl alloc] initWithFrame:CGRectMake(218, 9, 60, 25)];
+ [switch_ addTarget:self action:@selector(onSwitch:) forEvents:kUIControlEventMouseUpInside];
+ } return self;
+}
+
+- (void) onSwitch:(id)sender {
+ NSMutableDictionary *metadata = [Sections_ objectForKey:section_];
+ if (metadata == nil) {
+ metadata = [NSMutableDictionary dictionaryWithCapacity:2];
+ [Sections_ setObject:metadata forKey:section_];
+ }
+
+ Changed_ = true;
+ [metadata setObject:[NSNumber numberWithBool:([switch_ value] == 0)] forKey:@"Hidden"];
+}
+
+- (void) setSection:(Section *)section editing:(BOOL)editing {
+ if (editing != editing_) {
+ if (editing_)
+ [switch_ removeFromSuperview];
+ else
+ [self addSubview:switch_];
+ editing_ = editing;
+ }
+
+ [self clearSection];
+
+ if (section == nil) {
+ name_ = [@"All Packages" retain];
+ count_ = nil;
+ } else {
+ section_ = [section name];
+ if (section_ != nil)
+ section_ = [section_ retain];
+ name_ = [(section_ == nil ? @"(No Section)" : section_) retain];
+ count_ = [[NSString stringWithFormat:@"%d", [section count]] retain];
+
+ if (editing_)
+ [switch_ setValue:isSectionVisible(section_) animated:NO];