X-Git-Url: https://git.saurik.com/cydia.git/blobdiff_plain/4941f41dfb1c3472b4d8bc0839c299bc532661e1..9856894f5f6603e9938128d85cebf7f5792d452d:/Cydia.mm diff --git a/Cydia.mm b/Cydia.mm index fd83416b..8702e29a 100644 --- a/Cydia.mm +++ b/Cydia.mm @@ -1,6 +1,9 @@ /* #include Directives {{{ */ +#include #include -#import +#include + +#include #include #include @@ -10,12 +13,23 @@ #include #include #include +#include #include #include #include #include #include +#include +#include + +extern "C" { +#include +} + +#include +#include + #include #include #include @@ -29,8 +43,182 @@ exit(-1); \ } \ while (false) + +#define _not(type) ((type) ~ (type) 0) +/* }}} */ +/* Miscellaneous Messages {{{ */ +@interface WebView +- (void) setApplicationNameForUserAgent:(NSString *)applicationName; +- (id) frameLoadDelegate; +- (void) setFrameLoadDelegate:(id)delegate; +@end + +@interface NSString (Cydia) +- (NSString *) stringByAddingPercentEscapes; +- (NSString *) stringByReplacingCharacter:(unsigned short)arg0 withCharacter:(unsigned short)arg1; +@end +/* }}} */ + +#ifdef SRK_ASPEN +#define UITable UITableView +#endif + +OBJC_EXPORT const char *class_getName(Class cls); + +/* Reset View (UIView) {{{ */ +@interface UIView (CYResetView) +- (void) resetViewAnimated:(BOOL)animated; +@end + +@implementation UIView (CYResetView) + +- (void) resetViewAnimated:(BOOL)animated { + fprintf(stderr, "%s\n", class_getName(self->isa)); + _assert(false); +} + +@end +/* }}} */ +/* Reset View (UITable) {{{ */ +@interface UITable (CYResetView) +- (void) resetViewAnimated:(BOOL)animated; +@end + +@implementation UITable (CYResetView) + +- (void) resetViewAnimated:(BOOL)animated { + [self selectRow:-1 byExtendingSelection:NO withFade:animated]; +} + +@end +/* }}} */ +/* Reset View (UISectionList) {{{ */ +@interface UISectionList (CYResetView) +- (void) resetViewAnimated:(BOOL)animated; +@end + +@implementation UISectionList (CYResetView) + +- (void) resetViewAnimated:(BOOL)animated { + [[self table] resetViewAnimated:animated]; +} + +@end +/* }}} */ + +/* Perl-Compatible RegEx {{{ */ +class Pcre { + private: + pcre *code_; + pcre_extra *study_; + int capture_; + int *matches_; + const char *data_; + + public: + Pcre(const char *regex) : + study_(NULL) + { + const char *error; + int offset; + code_ = pcre_compile(regex, 0, &error, &offset, NULL); + + if (code_ == NULL) { + fprintf(stderr, "%d:%s\n", offset, error); + _assert(false); + } + + pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_); + matches_ = new int[(capture_ + 1) * 3]; + } + + ~Pcre() { + pcre_free(code_); + delete matches_; + } + + NSString *operator [](size_t match) { + return [NSString + stringWithCString:(data_ + matches_[match * 2]) + length:(matches_[match * 2 + 1] - matches_[match * 2]) + ]; + } + + bool operator ()(const char *data, size_t size) { + data_ = data; + return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0; + } +}; +/* }}} */ +/* CoreGraphicsServices Primitives {{{ */ +class CGColor { + private: + CGColorRef color_; + + public: + CGColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) { + float color[] = {red, green, blue, alpha}; + color_ = CGColorCreate(space, color); + } + + ~CGColor() { + CGColorRelease(color_); + } + + operator CGColorRef() { + return color_; + } +}; + +class GSFont { + private: + GSFontRef font_; + + public: + ~GSFont() { + /* XXX: no GSFontRelease()? */ + CFRelease(font_); + } +}; /* }}} */ +static const int PulseInterval_ = 50000; + +const char *Firmware_ = NULL; +const char *Machine_ = NULL; +const char *SerialNumber_ = NULL; + +unsigned Major_; +unsigned Minor_; +unsigned BugFix_; + +#define FW_LEAST(major, minor, bugfix) \ + (major < Major_ || major == Major_ && \ + (minor < Minor_ || minor == Minor_ && \ + bugfix <= BugFix_)) + +bool bootstrap_ = false; + +static NSMutableDictionary *Metadata_; +static NSMutableDictionary *Packages_; +static NSDate *now_; + +NSString *GetLastUpdate() { + NSDate *update = [Metadata_ objectForKey:@"LastUpdate"]; + + if (update == nil) + return @"Never or Unknown"; + + CFLocaleRef locale = CFLocaleCopyCurrent(); + CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, locale, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle); + CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update); + + CFRelease(formatter); + CFRelease(locale); + + return [(NSString *) formatted autorelease]; +} + @protocol ProgressDelegate - (void) setError:(NSString *)error; - (void) setTitle:(NSString *)title; @@ -38,6 +226,44 @@ while (false) - (void) addOutput:(NSString *)output; @end +NSString *SizeString(double size) { + unsigned power = 0; + while (size > 1024) { + size /= 1024; + ++power; + } + + static const char *powers_[] = {"B", "kB", "MB", "GB"}; + + return [NSString stringWithFormat:@"%.1f%s", size, powers_[power]]; +} + +static const float TextViewOffset_ = 22; + +UITextView *GetTextView(NSString *value, float left, bool html) { + UITextView *text([[[UITextView alloc] initWithFrame:CGRectMake(left, 3, 310 - left, 1000)] autorelease]); + [text setEditable:NO]; + [text setTextSize:16]; + if (html) + [text setHTML:value]; + else + [text setText:value]; + [text setEnabled:NO]; + + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGColor clear(space, 0, 0, 0, 0); + [text setBackgroundColor:clear]; + CGColorSpaceRelease(space); + + CGRect frame = [text frame]; + [text setFrame:frame]; + CGRect rect = [text visibleTextRect]; + frame.size.height = rect.size.height; + [text setFrame:frame]; + + return text; +} + /* Status Delegation {{{ */ class Status : public pkgAcquireStatus @@ -60,19 +286,23 @@ class Status : } virtual void IMSHit(pkgAcquire::ItemDesc &item) { - [delegate_ performSelectorOnMainThread:@selector(setStatusIMSHit) withObject:nil waitUntilDone:YES]; } virtual void Fetch(pkgAcquire::ItemDesc &item) { - [delegate_ setTitle:[NSString stringWithCString:item.Description.c_str()]]; + [delegate_ setTitle:[NSString stringWithCString:("Downloading " + item.ShortDesc).c_str()]]; } virtual void Done(pkgAcquire::ItemDesc &item) { - [delegate_ performSelectorOnMainThread:@selector(setStatusDone) withObject:nil waitUntilDone:YES]; } virtual void Fail(pkgAcquire::ItemDesc &item) { - [delegate_ performSelectorOnMainThread:@selector(setStatusFail) withObject:nil waitUntilDone:YES]; + if ( + item.Owner->Status == pkgAcquire::Item::StatIdle || + item.Owner->Status == pkgAcquire::Item::StatDone + ) + return; + + [delegate_ setError:[NSString stringWithCString:item.Owner->ErrorText.c_str()]]; } virtual bool Pulse(pkgAcquire *Owner) { @@ -137,6 +367,8 @@ extern NSString *kUIButtonBarButtonTitleWidth; extern NSString *kUIButtonBarButtonType; /* }}} */ /* Mime Addresses {{{ */ +Pcre email_r("^\"?(.*)\"? <([^>]*)>$"); + @interface Address : NSObject { NSString *name_; NSString *email_; @@ -155,7 +387,8 @@ extern NSString *kUIButtonBarButtonType; - (void) dealloc { [name_ release]; - [email_ release]; + if (email_ != nil) + [email_ release]; [super dealloc]; } @@ -173,27 +406,16 @@ extern NSString *kUIButtonBarButtonType; - (Address *) initWithString:(NSString *)string { if ((self = [super init]) != nil) { - const char *error; - int offset; - pcre *code = pcre_compile("^\"?(.*)\"? <([^>]*)>$", 0, &error, &offset, NULL); - - if (code == NULL) { - fprintf(stderr, "%d:%s\n", offset, error); - _assert(false); - } - - pcre_extra *study = NULL; - int capture; - pcre_fullinfo(code, study, PCRE_INFO_CAPTURECOUNT, &capture); - int matches[(capture + 1) * 3]; - - size_t size = [string length]; const char *data = [string UTF8String]; + size_t size = [string length]; - _assert(pcre_exec(code, study, data, size, 0, 0, matches, sizeof(matches) / sizeof(matches[0])) >= 0); - - name_ = [[NSString stringWithCString:(data + matches[2]) length:(matches[3] - matches[2])] retain]; - email_ = [[NSString stringWithCString:(data + matches[4]) length:(matches[5] - matches[4])] retain]; + if (email_r(data, size)) { + name_ = [email_r[1] retain]; + email_ = [email_r[2] retain]; + } else { + name_ = [[NSString stringWithCString:data length:size] retain]; + email_ = nil; + } } return self; } @@ -248,10 +470,21 @@ inline float interpolate(float begin, float end, float fraction) { } /* }}} */ +@class Package; +@class Source; + +/* Database Interface {{{ */ @interface Database : NSObject { pkgCacheFile cache_; pkgRecords *records_; pkgProblemResolver *resolver_; + pkgAcquire *fetcher_; + FileFd *lock_; + SPtr manager_; + pkgSourceList *list_; + + NSMutableDictionary *sources_; + NSMutableArray *packages_; id delegate_; Status status_; @@ -259,221 +492,291 @@ inline float interpolate(float begin, float end, float fraction) { int statusfd_; } +- (void) dealloc; + +- (void) _readStatus:(NSNumber *)fd; +- (void) _readOutput:(NSNumber *)fd; + +- (Package *) packageWithName:(NSString *)name; + - (Database *) init; - (pkgCacheFile &) cache; - (pkgRecords *) records; - (pkgProblemResolver *) resolver; +- (pkgAcquire &) fetcher; +- (NSArray *) packages; - (void) reloadData; +- (void) prepare; - (void) perform; - (void) update; - (void) upgrade; - (void) setDelegate:(id)delegate; +- (Source *) getSource:(const pkgCache::PkgFileIterator &)file; @end +/* }}} */ -/* Package Class {{{ */ -@interface Package : NSObject { - pkgCache::PkgIterator iterator_; - Database *database_; - pkgRecords::Parser *parser_; - pkgCache::VerIterator version_; - pkgCache::VerFileIterator file_; +/* Reset View {{{ */ +@interface ResetView : UIView { + UIPushButton *configure_; + UIPushButton *reload_; + NSMutableArray *views_; + UINavigationBar *navbar_; + UITransitionView *transition_; + bool resetting_; + id delegate_; } -- (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database version:(pkgCache::VerIterator)version file:(pkgCache::VerFileIterator)file; -+ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database; +- (void) dealloc; -- (NSString *) name; -- (NSString *) section; -- (BOOL) installed; -- (NSString *) version; -- (Address *) maintainer; -- (size_t) size; -- (NSString *) tagline; -- (NSString *) description; -- (NSComparisonResult) compareBySectionAndName:(Package *)package; +- (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item; +- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button; -- (void) install; -- (void) remove; -@end +- (id) initWithFrame:(CGRect)frame; +- (void) setDelegate:(id)delegate; -@implementation Package +- (void) configurePushed; +- (void) reloadPushed; -- (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database version:(pkgCache::VerIterator)version file:(pkgCache::VerFileIterator)file { - if ((self = [super init]) != nil) { - iterator_ = iterator; - database_ = database; +- (void) pushView:(UIView *)view withTitle:(NSString *)title backButtonTitle:(NSString *)back rightButton:(NSString *)right; +- (void) popViews:(unsigned)views; +- (void) resetView:(BOOL)clear; +- (void) _resetView; +- (void) setPrompt; +@end - version_ = version; - file_ = file; - parser_ = &[database_ records]->Lookup(file); - } return self; -} +@implementation ResetView -+ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database { - for (pkgCache::VerIterator version = iterator.VersionList(); !version.end(); ++version) - for (pkgCache::VerFileIterator file = version.FileList(); !file.end(); ++file) - return [[[Package alloc] - initWithIterator:iterator - database:database - version:version - file:file] - autorelease]; - return nil; +- (void) dealloc { + [configure_ release]; + [reload_ release]; + [transition_ release]; + [navbar_ release]; + [views_ release]; + [super dealloc]; } -- (NSString *) name { - return [NSString stringWithCString:iterator_.Name()]; -} +- (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item { + [views_ removeLastObject]; + UIView *view([views_ lastObject]); + [view resetViewAnimated:!resetting_]; -- (NSString *) section { - return [NSString stringWithCString:iterator_.Section()]; + if (!resetting_) { + [transition_ transition:2 toView:view]; + [self _resetView]; + } } -- (BOOL) installed { - return iterator_->CurrentState != pkgCache::State::NotInstalled; +- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button { + [sheet dismiss]; } -- (NSString *) version { - return [NSString stringWithCString:version_.VerStr()]; -} +- (id) initWithFrame:(CGRect)frame { + if ((self = [super initWithFrame:frame]) != nil) { + views_ = [[NSMutableArray arrayWithCapacity:4] retain]; -- (Address *) maintainer { - return [Address addressWithString:[NSString stringWithCString:parser_->Maintainer().c_str()]]; -} + struct CGRect bounds = [self bounds]; + CGSize navsize = [UINavigationBar defaultSizeWithPrompt]; + CGRect navrect = {{0, 0}, navsize}; -- (size_t) size { - return version_->InstalledSize; -} + navbar_ = [[UINavigationBar alloc] initWithFrame:navrect]; + [self addSubview:navbar_]; -- (NSString *) tagline { - return [NSString stringWithCString:parser_->ShortDesc().c_str()]; + [navbar_ setBarStyle:1]; + [navbar_ setDelegate:self]; + + transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake( + bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height + )]; + + //configure_ = [[UIPushButton alloc] initWithFrame:CGRectMake(15, 9, 17, 18)]; + configure_ = [[UIPushButton alloc] initWithFrame:CGRectMake(10, 9, 17, 18)]; + [configure_ setShowPressFeedback:YES]; + [configure_ setImage:[UIImage applicationImageNamed:@"configure.png"]]; + [configure_ addTarget:self action:@selector(configurePushed) forEvents:1]; + + //reload_ = [[UIPushButton alloc] initWithFrame:CGRectMake(288, 5, 18, 22)]; + reload_ = [[UIPushButton alloc] initWithFrame:CGRectMake(293, 5, 18, 22)]; + [reload_ setShowPressFeedback:YES]; + [reload_ setImage:[UIImage applicationImageNamed:@"reload.png"]]; + [reload_ addTarget:self action:@selector(reloadPushed) forEvents:1]; + + [navbar_ addSubview:configure_]; + [navbar_ addSubview:reload_]; + + [self addSubview:transition_]; + } return self; } -- (NSString *) description { - return [NSString stringWithCString:parser_->LongDesc().c_str()]; +- (void) setDelegate:(id)delegate { + delegate_ = delegate; } -- (NSComparisonResult) compareBySectionAndName:(Package *)package { - NSComparisonResult result = [[self section] compare:[package section]]; - if (result != NSOrderedSame) - return result; - return [[self name] compare:[package name]]; +- (void) configurePushed { + UIAlertSheet *sheet = [[[UIAlertSheet alloc] + initWithTitle:@"Sources Unimplemented" + buttons:[NSArray arrayWithObjects:@"Okay", nil] + defaultButtonIndex:0 + delegate:self + context:self + ] autorelease]; + + [sheet setBodyText:@"This feature will be implemented soon. In the mean time, you may add sources by adding .list files to '/etc/apt/sources.list.d' or modifying '/etc/apt/sources.list'."]; + [sheet popupAlertAnimated:YES]; } -- (void) install { - pkgProblemResolver *resolver = [database_ resolver]; - resolver->Clear(iterator_); - resolver->Protect(iterator_); - [database_ cache]->MarkInstall(iterator_, false); +- (void) reloadPushed { + [delegate_ update]; } -- (void) remove { - pkgProblemResolver *resolver = [database_ resolver]; - resolver->Clear(iterator_); - resolver->Protect(iterator_); - resolver->Remove(iterator_); - [database_ cache]->MarkDelete(iterator_, true); +- (void) pushView:(UIView *)view withTitle:(NSString *)title backButtonTitle:(NSString *)back rightButton:(NSString *)right { + UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease]; + [navbar_ pushNavigationItem:navitem]; + [navitem setBackButtonTitle:back]; + + [navbar_ showButtonsWithLeftTitle:nil rightTitle:right]; + + [transition_ transition:([views_ count] == 0 ? 0 : 1) toView:view]; + [views_ addObject:view]; } -@end -/* }}} */ -/* Section Class {{{ */ -@interface Section : NSObject { - NSString *name_; - size_t row_; - NSMutableArray *packages_; +- (void) popViews:(unsigned)views { + resetting_ = true; + for (unsigned i(0); i != views; ++i) + [navbar_ popNavigationItem]; + resetting_ = false; + + [self _resetView]; + [transition_ transition:2 toView:[views_ lastObject]]; } -- (void) dealloc; +- (void) resetView:(BOOL)clear { + resetting_ = true; -- (Section *) initWithName:(NSString *)name row:(size_t)row; -- (NSString *) name; -- (size_t) row; -- (void) addPackage:(Package *)package; -@end + if ([views_ count] > 1) { + [navbar_ disableAnimation]; + while ([views_ count] != (clear ? 1 : 2)) + [navbar_ popNavigationItem]; + [navbar_ enableAnimation]; + if (!clear) + [navbar_ popNavigationItem]; + } -@implementation Section + resetting_ = false; -- (void) dealloc { - [name_ release]; - [packages_ release]; - [super dealloc]; + [self _resetView]; + [transition_ transition:(clear ? 0 : 2) toView:[views_ lastObject]]; } -- (Section *) initWithName:(NSString *)name row:(size_t)row { - if ((self = [super init]) != nil) { - name_ = [name retain]; - row_ = row; - packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; - } return self; +- (void) _resetView { + [navbar_ showButtonsWithLeftTitle:nil rightTitle:nil]; } -- (NSString *) name { - return name_; -} - -- (size_t) row { - return row_; -} - -- (void) addPackage:(Package *)package { - [packages_ addObject:package]; +- (void) setPrompt { + [navbar_ setPrompt:[NSString stringWithFormat:@"Last Updated: %@", GetLastUpdate()]]; } @end /* }}} */ - /* Confirmation View {{{ */ -@interface ConfirmationView : UIView { +void AddTextView(NSMutableDictionary *fields, NSMutableArray *packages, NSString *key) { + if ([packages count] == 0) + return; + + UITextView *text = GetTextView([packages count] == 0 ? @"n/a" : [packages componentsJoinedByString:@", "], 110, false); + [fields setObject:text forKey:key]; + + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGColor blue(space, 0, 0, 0.4, 1); + [text setTextColor:blue]; + CGColorSpaceRelease(space); } +@protocol ConfirmationViewDelegate +- (void) cancel; +- (void) confirm; @end -@implementation ConfirmationView -@end -/* }}} */ -/* Package View {{{ */ -@interface PackageView : UIPreferencesTable { - Package *package_; +@interface ConfirmationView : UIView { Database *database_; - NSMutableArray *cells_; + id delegate_; + UITransitionView *transition_; + UIView *overlay_; + UINavigationBar *navbar_; + UIPreferencesTable *table_; + NSMutableDictionary *fields_; + UIAlertSheet *essential_; } - (void) dealloc; +- (void) cancel; + +- (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to; +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button; +- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button; - (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table; +- (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group; +- (float) preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed; - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group; - (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group; -- (PackageView *) initWithFrame:(struct CGRect)frame database:(Database *)database; -- (void) setPackage:(Package *)package; +- (id) initWithView:(UIView *)view database:(Database *)database delegate:(id)delegate; + @end -@implementation PackageView +@implementation ConfirmationView - (void) dealloc { - if (package_ != nil) - [package_ release]; - [database_ release]; - [cells_ release]; + [transition_ release]; + [overlay_ release]; + [navbar_ release]; + [table_ release]; + [fields_ release]; + if (essential_ != nil) + [essential_ release]; [super dealloc]; } -- (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table { - return 2; +- (void) cancel { + [transition_ transition:7 toView:nil]; + [delegate_ cancel]; } -- (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group { - switch (group) { +- (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to { + if (from != nil && to == nil) + [self removeFromSuperview]; +} + +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { + switch (button) { case 0: - return @"Specifics"; + if (essential_ != nil) + [essential_ popupAlertAnimated:YES]; + else + [delegate_ confirm]; break; case 1: - return @"Description"; + [self cancel]; break; + } +} + +- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button { + [essential_ dismiss]; + [self cancel]; +} + +- (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table { + return 2; +} + +- (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group { + switch (group) { + case 0: return @"Statistics"; + case 1: return @"Modifications"; default: _assert(false); } @@ -481,78 +784,58 @@ inline float interpolate(float begin, float end, float fraction) { - (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group { switch (group) { - case 0: - return 5; - break; - - case 1: - return 1; - break; + case 0: return 3; + case 1: return [fields_ count]; default: _assert(false); } } +- (float) preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed { + if (group != 1 || row == -1) + return proposed; + else { + _assert(size_t(row) < [fields_ count]); + return [[[fields_ allValues] objectAtIndex:row] visibleTextRect].size.height + TextViewOffset_; + } +} + - (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group { - UIPreferencesTableCell *cell; + UIPreferencesTableCell *cell = [[[UIPreferencesTableCell alloc] init] autorelease]; + [cell setShowSelection:NO]; switch (group) { case 0: switch (row) { - case 0: - cell = [cells_ objectAtIndex:0]; - [cell setTitle:@"Name"]; - [cell setValue:[package_ name]]; - break; + case 0: { + [cell setTitle:@"Downloading"]; + [cell setValue:SizeString([database_ fetcher].FetchNeeded())]; + } break; - case 1: - cell = [cells_ objectAtIndex:1]; - [cell setTitle:@"Version"]; - [cell setValue:[package_ version]]; - break; + case 1: { + [cell setTitle:@"Resuming At"]; + [cell setValue:SizeString([database_ fetcher].PartialPresent())]; + } break; - case 2: - cell = [cells_ objectAtIndex:2]; - [cell setTitle:@"Section"]; - [cell setValue:[package_ section]]; - break; + case 2: { + double size([database_ cache]->UsrSize()); - case 3: { - double size = [package_ size]; - unsigned power = 0; - while (size > 1024) { - size /= 1024; - ++power; + if (size < 0) { + [cell setTitle:@"Disk Freeing"]; + [cell setValue:SizeString(-size)]; + } else { + [cell setTitle:@"Disk Using"]; + [cell setValue:SizeString(size)]; } - - cell = [cells_ objectAtIndex:3]; - [cell setTitle:@"Size"]; - [cell setValue:[NSString stringWithFormat:@"%.1f%c", size, "bkMG"[power]]]; } break; - case 4: - cell = [cells_ objectAtIndex:4]; - [cell setTitle:@"Maintainer"]; - [cell setValue:[[package_ maintainer] name]]; - [cell setShowDisclosure:YES]; - [cell setShowSelection:YES]; - break; - default: _assert(false); } break; - case 1: switch (row) { - case 0: - cell = [cells_ objectAtIndex:5]; - [cell setTitle:nil]; - [cell setValue:[package_ tagline]]; - break; - - case 1: - cell = [cells_ objectAtIndex:6]; - [cell setTitle:@"Description"]; - [cell setValue:[package_ description]]; - break; - } break; + case 1: + _assert(size_t(row) < [fields_ count]); + [cell setTitle:[[fields_ allKeys] objectAtIndex:row]]; + [cell addSubview:[[fields_ allValues] objectAtIndex:row]]; + break; default: _assert(false); } @@ -560,875 +843,2607 @@ inline float interpolate(float begin, float end, float fraction) { return cell; } -- (PackageView *) initWithFrame:(struct CGRect)frame database:(Database *)database { - if ((self = [super initWithFrame:frame]) != nil) { - database_ = [database retain]; - [self setDataSource:self]; +- (id) initWithView:(UIView *)view database:(Database *)database delegate:(id)delegate { + if ((self = [super initWithFrame:[view bounds]]) != nil) { + database_ = database; + delegate_ = delegate; + + transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]]; + [self addSubview:transition_]; + + overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]]; - cells_ = [[NSMutableArray arrayWithCapacity:16] retain]; + CGSize navsize = [UINavigationBar defaultSize]; + CGRect navrect = {{0, 0}, navsize}; + CGRect bounds = [overlay_ bounds]; + + navbar_ = [[UINavigationBar alloc] initWithFrame:navrect]; + [navbar_ setBarStyle:1]; + [navbar_ setDelegate:self]; - for (unsigned i = 0; i != 6; ++i) { - struct CGRect frame = [self frameOfPreferencesCellAtRow:0 inGroup:0]; - UIPreferencesTableCell *cell = [[[UIPreferencesTableCell alloc] init] autorelease]; - [cell setShowSelection:NO]; - [cells_ addObject:cell]; + UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Confirm"] autorelease]; + [navbar_ pushNavigationItem:navitem]; + [navbar_ showButtonsWithLeftTitle:@"Cancel" rightTitle:@"Confirm"]; + + fields_ = [[NSMutableDictionary dictionaryWithCapacity:16] retain]; + + NSMutableArray *installing = [NSMutableArray arrayWithCapacity:16]; + NSMutableArray *upgrading = [NSMutableArray arrayWithCapacity:16]; + NSMutableArray *removing = [NSMutableArray arrayWithCapacity:16]; + + bool install(false); + bool upgrade(false); + bool remove(false); + + pkgCacheFile &cache([database_ cache]); + for (pkgCache::PkgIterator iterator = cache->PkgBegin(); !iterator.end(); ++iterator) { + NSString *name([NSString stringWithCString:iterator.Name()]); + bool essential((iterator->Flags & pkgCache::Flag::Essential) != 0); + + if (cache[iterator].NewInstall()) { + if (essential) + install = true; + [installing addObject:name]; + } else if (cache[iterator].Upgrade()) { + if (essential) + upgrade = true; + [upgrading addObject:name]; + } else if (cache[iterator].Delete()) { + if (essential) + remove = true; + [removing addObject:name]; + } } - } return self; -} -- (void) setPackage:(Package *)package { - package_ = [package retain]; - [self reloadData]; + if (!remove) + essential_ = nil; + else { + essential_ = [[UIAlertSheet alloc] + initWithTitle:@"Unable to Comply" + buttons:[NSArray arrayWithObjects:@"Okay", nil] + defaultButtonIndex:0 + delegate:self + context:self + ]; + + [essential_ setBodyText:@"One or more of the packages you are about to remove are marked 'Essential' and cannot be removed by Cydia. Please use apt-get."]; + } + + AddTextView(fields_, installing, @"Installing"); + AddTextView(fields_, upgrading, @"Upgrading"); + AddTextView(fields_, removing, @"Removing"); + + table_ = [[UIPreferencesTable alloc] initWithFrame:CGRectMake( + 0, navsize.height, bounds.size.width, bounds.size.height - navsize.height + )]; + + [table_ setReusesTableCells:YES]; + [table_ setDataSource:self]; + [table_ reloadData]; + + [overlay_ addSubview:navbar_]; + [overlay_ addSubview:table_]; + + [view addSubview:self]; + + [transition_ setDelegate:self]; + + UIView *blank = [[[UIView alloc] initWithFrame:[transition_ bounds]] autorelease]; + [transition_ transition:0 toView:blank]; + [transition_ transition:3 toView:overlay_]; + } return self; } @end /* }}} */ -/* Package Cell {{{ */ -@interface PackageCell : UITableCell { - UITextLabel *name_; - UIRightTextLabel *version_; - UITextLabel *description_; + +/* Source Class {{{ */ +@interface Source : NSObject { + NSString *description_; + NSString *label_; + NSString *origin_; + + NSString *uri_; + NSString *distribution_; + NSString *type_; + NSString *version_; + + NSString *defaultIcon_; + + BOOL trusted_; } - (void) dealloc; -- (PackageCell *) initWithPackage:(Package *)package; +- (Source *) initWithMetaIndex:(metaIndex *)index; -- (void) _setSelected:(float)fraction; -- (void) setSelected:(BOOL)selected; -- (void) setSelected:(BOOL)selected withFade:(BOOL)fade; -- (void) _setSelectionFadeFraction:(float)fraction; +- (BOOL) trusted; + +- (NSString *) uri; +- (NSString *) distribution; +- (NSString *) type; +- (NSString *) description; +- (NSString *) label; +- (NSString *) origin; +- (NSString *) version; + +- (NSString *) defaultIcon; @end -@implementation PackageCell +@implementation Source - (void) dealloc { - [name_ release]; - [version_ release]; - [description_ release]; + [uri_ release]; + [distribution_ release]; + [type_ release]; + + if (description_ != nil) + [description_ release]; + if (label_ != nil) + [label_ release]; + if (origin_ != nil) + [origin_ release]; + if (version_ != nil) + [version_ release]; + [super dealloc]; } -- (PackageCell *) initWithPackage:(Package *)package { +- (Source *) initWithMetaIndex:(metaIndex *)index { if ((self = [super init]) != nil) { - GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 22); - GSFontRef large = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 16); - GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 14); - - CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); - float clear[] = {0, 0, 0, 0}; - - name_ = [[UITextLabel alloc] initWithFrame:CGRectMake(12, 7, 250, 25)]; - [name_ setText:[package name]]; - [name_ setBackgroundColor:CGColorCreate(space, clear)]; - [name_ setFont:bold]; - - version_ = [[UIRightTextLabel alloc] initWithFrame:CGRectMake(290, 7, 70, 25)]; - [version_ setText:[package version]]; - [version_ setBackgroundColor:CGColorCreate(space, clear)]; - [version_ setFont:large]; - - description_ = [[UITextLabel alloc] initWithFrame:CGRectMake(13, 35, 315, 20)]; - [description_ setText:[package tagline]]; - [description_ setBackgroundColor:CGColorCreate(space, clear)]; - [description_ setFont:small]; - - [self addSubview:name_]; - [self addSubview:version_]; - [self addSubview:description_]; - - CFRelease(small); - CFRelease(large); - CFRelease(bold); + trusted_ = index->IsTrusted(); + + uri_ = [[NSString stringWithCString:index->GetURI().c_str()] retain]; + distribution_ = [[NSString stringWithCString:index->GetDist().c_str()] retain]; + type_ = [[NSString stringWithCString:index->GetType()] retain]; + + description_ = nil; + label_ = nil; + origin_ = nil; + + debReleaseIndex *dindex(dynamic_cast(index)); + if (dindex != NULL) { + std::ifstream release(dindex->MetaIndexFile("Release").c_str()); + std::string line; + while (std::getline(release, line)) { + std::string::size_type colon(line.find(':')); + if (colon == std::string::npos) + continue; + + std::string name(line.substr(0, colon)); + std::string value(line.substr(colon + 1)); + while (!value.empty() && value[0] == ' ') + value = value.substr(1); + + if (name == "Default-Icon") + defaultIcon_ = [[NSString stringWithCString:value.c_str()] retain]; + else if (name == "Description") + description_ = [[NSString stringWithCString:value.c_str()] retain]; + else if (name == "Label") + label_ = [[NSString stringWithCString:value.c_str()] retain]; + else if (name == "Origin") + origin_ = [[NSString stringWithCString:value.c_str()] retain]; + else if (name == "Version") + version_ = [[NSString stringWithCString:value.c_str()] retain]; + } + } } return self; } -- (void) _setSelected:(float)fraction { - CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); +- (BOOL) trusted { + return trusted_; +} - float black[] = { - interpolate(0.0, 1.0, fraction), - interpolate(0.0, 1.0, fraction), - interpolate(0.0, 1.0, fraction), - 1.0}; +- (NSString *) uri { + return uri_; +} - float blue[] = { - interpolate(0.2, 1.0, fraction), - interpolate(0.2, 1.0, fraction), - interpolate(1.0, 1.0, fraction), - 1.0}; +- (NSString *) distribution { + return distribution_; +} - float gray[] = { - interpolate(0.4, 1.0, fraction), - interpolate(0.4, 1.0, fraction), - interpolate(0.4, 1.0, fraction), - 1.0}; +- (NSString *) type { + return type_; +} - [name_ setColor:CGColorCreate(space, black)]; - [version_ setColor:CGColorCreate(space, blue)]; - [description_ setColor:CGColorCreate(space, gray)]; +- (NSString *) description { + return description_; } -- (void) setSelected:(BOOL)selected { - [self _setSelected:(selected ? 1.0 : 0.0)]; - [super setSelected:selected]; +- (NSString *) label { + return label_; } -- (void) setSelected:(BOOL)selected withFade:(BOOL)fade { - if (!fade) - [self _setSelected:(selected ? 1.0 : 0.0)]; - [super setSelected:selected withFade:fade]; +- (NSString *) origin { + return origin_; } -- (void) _setSelectionFadeFraction:(float)fraction { - [self _setSelected:fraction]; - [super _setSelectionFadeFraction:fraction]; +- (NSString *) version { + return version_; +} + +- (NSString *) defaultIcon { + return defaultIcon_; } @end /* }}} */ +/* Package Class {{{ */ +NSString *Scour(const char *field, const char *begin, const char *end) { + size_t i(0), l(strlen(field)); + + for (;;) { + const char *name = begin + i; + const char *colon = name + l; + const char *value = colon + 1; + + if ( + value < end && + *colon == ':' && + memcmp(name, field, l) == 0 + ) { + while (value != end && value[0] == ' ') + ++value; + const char *line = std::find(value, end, '\n'); + while (line != value && line[-1] == ' ') + --line; + return [NSString stringWithCString:value length:(line - value)]; + } else { + begin = std::find(begin, end, '\n'); + if (begin == end) + return nil; + ++begin; + } + } +} -@implementation Database +@interface Package : NSObject { + pkgCache::PkgIterator iterator_; + Database *database_; + pkgCache::VerIterator version_; + pkgCache::VerFileIterator file_; + Source *source_; -- (void) _readStatus:(NSNumber *)fd { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSString *latest_; + NSString *installed_; - __gnu_cxx::stdio_filebuf ib([fd intValue], std::ios::in); - std::istream is(&ib); - std::string line; + NSString *id_; + NSString *name_; + NSString *tagline_; + NSString *icon_; + NSString *website_; +} - const char *error; - int offset; - pcre *code = pcre_compile("^([^:]*):([^:]*):([^:]*):(.*)$", 0, &error, &offset, NULL); +- (void) dealloc; - pcre_extra *study = NULL; - int capture; - pcre_fullinfo(code, study, PCRE_INFO_CAPTURECOUNT, &capture); - int matches[(capture + 1) * 3]; +- (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database version:(pkgCache::VerIterator)version file:(pkgCache::VerFileIterator)file; ++ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database; - while (std::getline(is, line)) { - const char *data(line.c_str()); - fprintf(stderr, "fd(%s)\n", data); +- (NSString *) section; +- (Address *) maintainer; +- (size_t) size; +- (NSString *) description; +- (NSString *) index; - _assert(pcre_exec(code, study, data, line.size(), 0, 0, matches, sizeof(matches) / sizeof(matches[0])) >= 0); +- (NSDate *) seen; - std::string type(line.substr(matches[2], matches[3] - matches[2])); +- (NSString *) latest; +- (NSString *) installed; +- (BOOL) upgradable; +- (BOOL) essential; +- (BOOL) broken; - std::istringstream buffer(line.substr(matches[6], matches[7] - matches[6])); - float percent; - buffer >> percent; - [delegate_ setPercent:(percent / 100)]; +- (NSString *) id; +- (NSString *) name; +- (NSString *) tagline; +- (NSString *) icon; +- (NSString *) website; - NSString *string = [NSString stringWithCString:(data + matches[8]) length:(matches[9] - matches[8])]; +- (Source *) source; - if (type == "pmerror") - [delegate_ setError:string]; - else if (type == "pmstatus") - [delegate_ setTitle:string]; - } +- (BOOL) matches:(NSString *)text; - [pool release]; -} +- (NSComparisonResult) compareByName:(Package *)package; +- (NSComparisonResult) compareBySectionAndName:(Package *)package; +- (NSComparisonResult) compareForChanges:(Package *)package; -- (void) _readOutput:(NSNumber *)fd { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; +- (void) install; +- (void) remove; +@end - __gnu_cxx::stdio_filebuf ib([fd intValue], std::ios::in); - std::istream is(&ib); - std::string line; +@implementation Package - while (std::getline(is, line)) - [delegate_ addOutput:[NSString stringWithCString:line.c_str()]]; +- (void) dealloc { + [latest_ release]; + if (installed_ != nil) + [installed_ release]; + + [id_ release]; + if (name_ != nil) + [name_ release]; + [tagline_ release]; + if (icon_ != nil) + [icon_ release]; + if (website_ != nil) + [website_ release]; + + if (source_ != nil) + [source_ release]; - [pool release]; + [super dealloc]; } -- (Database *) init { +- (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database version:(pkgCache::VerIterator)version file:(pkgCache::VerFileIterator)file { if ((self = [super init]) != nil) { - records_ = NULL; - resolver_ = NULL; + iterator_ = iterator; + database_ = database; - int fds[2]; + version_ = version; + file_ = file; - _assert(pipe(fds) != -1); - printf("%d %d\n", fds[0], fds[1]); - statusfd_ = fds[1]; + pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); - [NSThread - detachNewThreadSelector:@selector(_readStatus:) - toTarget:self - withObject:[[NSNumber numberWithInt:fds[0]] retain] - ]; + const char *begin, *end; + parser->GetRec(begin, end); - _assert(pipe(fds) != -1); - printf("%d %d\n", fds[0], fds[1]); - _assert(dup2(fds[1], 1) != -1); - _assert(close(fds[1]) != -1); + latest_ = [[NSString stringWithCString:version_.VerStr()] retain]; + installed_ = iterator_.CurrentVer().end() ? nil : [[NSString stringWithCString:iterator_.CurrentVer().VerStr()] retain]; - [NSThread - detachNewThreadSelector:@selector(_readOutput:) - toTarget:self - withObject:[[NSNumber numberWithInt:fds[0]] retain] - ]; + id_ = [[[NSString stringWithCString:iterator_.Name()] lowercaseString] retain]; + name_ = Scour("Name", begin, end); + if (name_ != nil) + name_ = [name_ retain]; + tagline_ = [[NSString stringWithCString:parser->ShortDesc().c_str()] retain]; + icon_ = Scour("Icon", begin, end); + if (icon_ != nil) + icon_ = [icon_ retain]; + website_ = Scour("Website", begin, end); + if (website_ != nil) + website_ = [website_ retain]; + + source_ = [[database_ getSource:file_.File()] retain]; + + NSMutableDictionary *metadata = [Packages_ objectForKey:id_]; + if (metadata == nil || [metadata count] == 0) { + metadata = [NSMutableDictionary dictionaryWithObjectsAndKeys: + now_, @"FirstSeen", + nil]; + + [Packages_ setObject:metadata forKey:id_]; + } } return self; } -- (pkgCacheFile &) cache { - return cache_; ++ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database { + for (pkgCache::VerIterator version = iterator.VersionList(); !version.end(); ++version) + for (pkgCache::VerFileIterator file = version.FileList(); !file.end(); ++file) + return [[[Package alloc] + initWithIterator:iterator + database:database + version:version + file:file] + autorelease]; + return nil; } -- (pkgRecords *) records { - return records_; +- (NSString *) section { + const char *section = iterator_.Section(); + return section == NULL ? nil : [[NSString stringWithCString:section] stringByReplacingCharacter:'_' withCharacter:' ']; } -- (pkgProblemResolver *) resolver { - return resolver_; +- (Address *) maintainer { + pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); + return [Address addressWithString:[NSString stringWithCString:parser->Maintainer().c_str()]]; } -- (void) reloadData { - _trace(); - _error->Discard(); - _trace(); - delete resolver_; - _trace(); - delete records_; - _trace(); - cache_.Close(); - _trace(); - cache_.Open(progress_, true); - _trace(); - records_ = new pkgRecords(cache_); - _trace(); - resolver_ = new pkgProblemResolver(cache_); - _trace(); +- (size_t) size { + return version_->InstalledSize; } -- (void) perform { - _trace(); - pkgRecords records(cache_); +- (NSString *) description { + pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); + NSString *description([NSString stringWithCString:parser->LongDesc().c_str()]); + + NSArray *lines = [description componentsSeparatedByString:@"\n"]; + NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; + if ([lines count] < 2) + return nil; + + NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; + for (size_t i(1); i != [lines count]; ++i) { + NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; + [trimmed addObject:trim]; + } - _trace(); - FileFd lock; - lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); - _assert(!_error->PendingError()); + return [trimmed componentsJoinedByString:@"\n"]; +} - _trace(); - pkgAcquire fetcher(&status_); - pkgSourceList list; - _assert(list.ReadMainList()); +- (NSString *) index { + NSString *index = [[[self name] substringToIndex:1] uppercaseString]; + return [index length] != 0 && isalpha([index characterAtIndex:0]) ? index : @"123"; +} - _trace(); - SPtr manager(_system->CreatePM(cache_)); - _assert(manager->GetArchives(&fetcher, &list, &records)); - _assert(!_error->PendingError()); - _assert(fetcher.Run() != pkgAcquire::Failed); +- (NSDate *) seen { + return [[Packages_ objectForKey:id_] objectForKey:@"FirstSeen"]; +} - _trace(); - _system->UnLock(); - pkgPackageManager::OrderResult result = manager->DoInstall(statusfd_); +- (NSString *) latest { + return latest_; +} - if (result == pkgPackageManager::Failed) - return; - if (_error->PendingError()) - return; - if (result != pkgPackageManager::Completed) - return; +- (NSString *) installed { + return installed_; } -- (void) update { - pkgSourceList list; - _assert(list.ReadMainList()); +- (BOOL) upgradable { + if (NSString *installed = [self installed]) + return [[self latest] compare:installed] != NSOrderedSame ? YES : NO; + else + return [self essential]; +} - FileFd lock; - lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); - _assert(!_error->PendingError()); +- (BOOL) essential { + return (iterator_->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES; +} - pkgAcquire fetcher(&status_); - _assert(list.GetIndexes(&fetcher)); - _assert(fetcher.Run() != pkgAcquire::Failed); +- (BOOL) broken { + return (*[database_ cache])[iterator_].InstBroken(); +} - bool failed = false; - for (pkgAcquire::ItemIterator item = fetcher.ItemsBegin(); item != fetcher.ItemsEnd(); item++) - if ((*item)->Status != pkgAcquire::Item::StatDone) { - (*item)->Finished(); - failed = true; - } +- (NSString *) id { + return id_; +} - if (!failed && _config->FindB("APT::Get::List-Cleanup", true) == true) { - _assert(fetcher.Clean(_config->FindDir("Dir::State::lists"))); - _assert(fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/")); +- (NSString *) name { + return name_ == nil ? id_ : name_; +} + +- (NSString *) tagline { + return tagline_; +} + +- (NSString *) icon { + return icon_; +} + +- (NSString *) website { + return website_; +} + +- (Source *) source { + return source_; +} + +- (BOOL) matches:(NSString *)text { + if (text == nil) + return NO; + + NSRange range; + + range = [[self id] rangeOfString:text options:NSCaseInsensitiveSearch]; + if (range.location != NSNotFound) + return YES; + + range = [[self name] rangeOfString:text options:NSCaseInsensitiveSearch]; + if (range.location != NSNotFound) + return YES; + + range = [[self tagline] rangeOfString:text options:NSCaseInsensitiveSearch]; + if (range.location != NSNotFound) + return YES; + + return NO; +} + +- (NSComparisonResult) compareByName:(Package *)package { + NSString *lhs = [self name]; + NSString *rhs = [package name]; + + if ([lhs length] != 0 && [rhs length] != 0) { + unichar lhc = [lhs characterAtIndex:0]; + unichar rhc = [rhs characterAtIndex:0]; + + if (isalpha(lhc) && !isalpha(rhc)) + return NSOrderedAscending; + else if (!isalpha(lhc) && isalpha(rhc)) + return NSOrderedDescending; } + + return [lhs caseInsensitiveCompare:rhs]; } -- (void) upgrade { - _assert(cache_->DelCount() == 0 && cache_->InstCount() == 0); - _assert(pkgApplyStatus(cache_)); +- (NSComparisonResult) compareBySectionAndName:(Package *)package { + NSString *lhs = [self section]; + NSString *rhs = [package section]; + + if (lhs == NULL && rhs != NULL) + return NSOrderedAscending; + else if (lhs != NULL && rhs == NULL) + return NSOrderedDescending; + else if (lhs != NULL && rhs != NULL) { + NSComparisonResult result = [lhs compare:rhs]; + if (result != NSOrderedSame) + return result; + } - if (cache_->BrokenCount() != 0) { - _assert(pkgFixBroken(cache_)); - _assert(cache_->BrokenCount() == 0); - _assert(pkgMinimizeUpgrade(cache_)); + return [self compareByName:package]; +} + +- (NSComparisonResult) compareForChanges:(Package *)package { + BOOL lhs = [self upgradable]; + BOOL rhs = [package upgradable]; + + if (lhs != rhs) + return lhs ? NSOrderedAscending : NSOrderedDescending; + else if (!lhs) { + switch ([[self seen] compare:[package seen]]) { + case NSOrderedAscending: + return NSOrderedDescending; + case NSOrderedSame: + break; + case NSOrderedDescending: + return NSOrderedAscending; + default: + _assert(false); + } } - _assert(pkgDistUpgrade(cache_)); + return [self compareByName:package]; +} - //InstallPackages(cache_, true); +- (void) install { + pkgProblemResolver *resolver = [database_ resolver]; + resolver->Clear(iterator_); + resolver->Protect(iterator_); + [database_ cache]->MarkInstall(iterator_, false); } -- (void) setDelegate:(id)delegate { - delegate_ = delegate; - status_.setDelegate(delegate); - progress_.setDelegate(delegate); +- (void) remove { + pkgProblemResolver *resolver = [database_ resolver]; + resolver->Clear(iterator_); + resolver->Protect(iterator_); + resolver->Remove(iterator_); + [database_ cache]->MarkDelete(iterator_, true); } @end +/* }}} */ +/* Section Class {{{ */ +@interface Section : NSObject { + NSString *name_; + size_t row_; + NSMutableArray *packages_; +} -/* Progress Data {{{ */ -@interface ProgressData : NSObject { - SEL selector_; - id target_; - id object_; +- (void) dealloc; + +- (Section *) initWithName:(NSString *)name row:(size_t)row; +- (NSString *) name; +- (size_t) row; +- (NSArray *) packages; +- (size_t) count; +- (void) addPackage:(Package *)package; +@end + +@implementation Section + +- (void) dealloc { + [name_ release]; + [packages_ release]; + [super dealloc]; } -- (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object; +- (Section *) initWithName:(NSString *)name row:(size_t)row { + if ((self = [super init]) != nil) { + name_ = [name retain]; + row_ = row; + packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; + } return self; +} -- (SEL) selector; -- (id) target; -- (id) object; +- (NSString *) name { + return name_; +} + +- (size_t) row { + return row_; +} + +- (NSArray *) packages { + return packages_; +} + +- (size_t) count { + return [packages_ count]; +} + +- (void) addPackage:(Package *)package { + [packages_ addObject:package]; +} + +@end +/* }}} */ + +/* Package View {{{ */ +@protocol PackageViewDelegate +- (void) performPackage:(Package *)package; +@end + +@interface PackageView : UIView { + UIPreferencesTable *table_; + Package *package_; + UITextView *description_; + id delegate_; +} + +- (void) dealloc; + +- (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table; +- (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group; +- (float) preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed; +- (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group; +- (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group; + +- (BOOL) canSelectRow:(int)row; +- (void) tableRowSelected:(NSNotification *)notification; + +- (Package *) package; + +- (id) initWithFrame:(struct CGRect)frame; +- (void) setPackage:(Package *)package; +- (void) setDelegate:(id)delegate; @end -@implementation ProgressData +@implementation PackageView + +- (void) dealloc { + if (package_ != nil) + [package_ release]; + if (description_ != nil) + [description_ release]; + [table_ release]; + [super dealloc]; +} + +- (int) numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table { + return 3; +} + +- (NSString *) preferencesTable:(UIPreferencesTable *)table titleForGroup:(int)group { + switch (group) { + case 0: return nil; + case 1: return @"Package Details"; + case 2: return @"Source Information"; + + default: _assert(false); + } +} + +- (float) preferencesTable:(UIPreferencesTable *)table heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed { + if (group != 0 || row != 1) + return proposed; + else + return [description_ visibleTextRect].size.height + TextViewOffset_; +} + +- (int) preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group { + switch (group) { + case 0: return [package_ website] == nil ? 2 : 3; + case 1: return 5; + case 2: return 3; + + default: _assert(false); + } +} + +- (UIPreferencesTableCell *) preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group { + UIPreferencesTableCell *cell = [[[UIPreferencesTableCell alloc] init] autorelease]; + [cell setShowSelection:NO]; + + switch (group) { + case 0: switch (row) { + case 0: + [cell setTitle:[package_ name]]; + [cell setValue:[package_ latest]]; + break; + + case 1: + [cell addSubview:description_]; + break; + + case 2: + [cell setTitle:@"More Information"]; + [cell setShowDisclosure:YES]; + [cell setShowSelection:YES]; + break; + + default: _assert(false); + } break; + + case 1: switch (row) { + case 0: + [cell setTitle:@"Identifier"]; + [cell setValue:[package_ id]]; + break; + + case 1: { + [cell setTitle:@"Installed Version"]; + NSString *installed([package_ installed]); + [cell setValue:(installed == nil ? @"n/a" : installed)]; + } break; + + case 2: { + [cell setTitle:@"Section"]; + NSString *section([package_ section]); + [cell setValue:(section == nil ? @"n/a" : section)]; + } break; + + case 3: + [cell setTitle:@"Expanded Size"]; + [cell setValue:SizeString([package_ size])]; + break; + + case 4: + [cell setTitle:@"Maintainer"]; + [cell setValue:[[package_ maintainer] name]]; + [cell setShowDisclosure:YES]; + [cell setShowSelection:YES]; + break; + + default: _assert(false); + } break; + + case 2: switch (row) { + case 0: + [cell setTitle:[[package_ source] label]]; + [cell setValue:[[package_ source] version]]; + break; + + case 1: + [cell setValue:[[package_ source] description]]; + break; + + case 2: + [cell setTitle:@"Origin"]; + [cell setValue:[[package_ source] origin]]; + break; + + default: _assert(false); + } break; + + default: _assert(false); + } + + return cell; +} + +- (BOOL) canSelectRow:(int)row { + return YES; +} + +- (void) tableRowSelected:(NSNotification *)notification { + int row = [table_ selectedRow]; + NSString *website = [package_ website]; + + if (row == (website == nil ? 8 : 9)) + [delegate_ openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto:%@?subject=%@", + [[package_ maintainer] email], + [[NSString stringWithFormat:@"regarding apt package \"%@\"", [package_ name]] stringByAddingPercentEscapes] + ]]]; + else if (website != nil && row == 3) + [delegate_ openURL:[NSURL URLWithString:website]]; +} + +- (Package *) package { + return package_; +} + +- (id) initWithFrame:(struct CGRect)frame { + if ((self = [super initWithFrame:frame]) != nil) { + table_ = [[UIPreferencesTable alloc] initWithFrame:[self bounds]]; + [self addSubview:table_]; + + [table_ setDataSource:self]; + [table_ setDelegate:self]; + } return self; +} + +- (void) setPackage:(Package *)package { + if (package_ != nil) { + [package_ autorelease]; + package_ = nil; + } + + if (description_ != nil) { + [description_ release]; + description_ = nil; + } + + if (package != nil) { + package_ = [package retain]; + + NSString *description([package description]); + if (description == nil) + description = [package tagline]; + description_ = [GetTextView(description, 12, true) retain]; + + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGColor black(space, 0, 0, 0, 1); + [description_ setTextColor:black]; + CGColorSpaceRelease(space); + + [table_ reloadData]; + } +} + +- (void) setDelegate:(id)delegate { + delegate_ = delegate; +} + +@end +/* }}} */ +/* Package Cell {{{ */ +@interface PackageCell : UITableCell { + UIImageView *icon_; + UITextLabel *name_; + UITextLabel *description_; + UITextLabel *source_; + UITextLabel *version_; + UIImageView *trusted_; + SEL versioner_; +} + +- (void) dealloc; + +- (PackageCell *) initWithVersioner:(SEL)versioner; +- (void) setPackage:(Package *)package; + +- (void) _setSelected:(float)fraction; +- (void) setSelected:(BOOL)selected; +- (void) setSelected:(BOOL)selected withFade:(BOOL)fade; +- (void) _setSelectionFadeFraction:(float)fraction; + +@end + +@implementation PackageCell + +- (void) dealloc { + [icon_ release]; + [name_ release]; + [description_ release]; + [source_ release]; + [version_ release]; + [trusted_ release]; + [super dealloc]; +} + +- (PackageCell *) initWithVersioner:(SEL)versioner { + if ((self = [super init]) != nil) { + versioner_ = versioner; + + GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 22); + GSFontRef large = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 16); + GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 14); + + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + + CGColor clear(space, 0, 0, 0, 0); + + icon_ = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)]; + + name_ = [[UITextLabel alloc] initWithFrame:CGRectMake(48, 12, 240, 25)]; + [name_ setBackgroundColor:clear]; + [name_ setFont:bold]; + + description_ = [[UITextLabel alloc] initWithFrame:CGRectMake(12, 46, 280, 20)]; + [description_ setBackgroundColor:clear]; + [description_ setFont:small]; + + source_ = [[UITextLabel alloc] initWithFrame:CGRectMake(12, 72, 225, 20)]; + [source_ setBackgroundColor:clear]; + [source_ setFont:large]; + + version_ = [[UIRightTextLabel alloc] initWithFrame:CGRectMake(286, 69, 70, 25)]; + [version_ setBackgroundColor:clear]; + [version_ setFont:large]; + + //trusted_ = [[UIImageView alloc] initWithFrame:CGRectMake(278, 7, 16, 16)]; + trusted_ = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 16, 16)]; + [trusted_ setImage:[UIImage applicationImageNamed:@"trusted.png"]]; + + [self addSubview:icon_]; + [self addSubview:name_]; + [self addSubview:description_]; + [self addSubview:source_]; + [self addSubview:version_]; + + CGColorSpaceRelease(space); + + CFRelease(small); + CFRelease(large); + CFRelease(bold); + } return self; +} + +- (void) setPackage:(Package *)package { + Source *source = [package source]; + + UIImage *image = nil; + if (NSString *icon = [package icon]) + image = [UIImage imageAtPath:[icon substringFromIndex:6]]; + if (image == nil) if (NSString *icon = [source defaultIcon]) + image = [UIImage imageAtPath:[icon substringFromIndex:6]]; + if (image == nil) + image = [UIImage applicationImageNamed:@"unknown.png"]; + + [icon_ setImage:image]; + [icon_ zoomToScale:0.5f]; + [icon_ setFrame:CGRectMake(10, 10, 30, 30)]; + + [name_ setText:[package name]]; + [version_ setText:[package latest]]; + [description_ setText:[package tagline]]; + + NSString *label; + bool trusted; + + if (source != nil) { + label = [source label]; + trusted = [source trusted]; + } else if ([[package id] isEqualToString:@"firmware"]) { + label = @"Apple"; + trusted = false; + } else { + label = @"Unknown/Local"; + trusted = false; + } + + [source_ setText:[NSString stringWithFormat:@"from %@", label]]; + + [trusted_ removeFromSuperview]; + if (trusted) + [self addSubview:trusted_]; +} + +- (void) _setSelected:(float)fraction { + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + + CGColor black(space, + interpolate(0.0, 1.0, fraction), + interpolate(0.0, 1.0, fraction), + interpolate(0.0, 1.0, fraction), + 1.0); + + CGColor blue(space, + interpolate(0.2, 1.0, fraction), + interpolate(0.2, 1.0, fraction), + interpolate(1.0, 1.0, fraction), + 1.0); + + CGColor gray(space, + interpolate(0.4, 1.0, fraction), + interpolate(0.4, 1.0, fraction), + interpolate(0.4, 1.0, fraction), + 1.0); + + [name_ setColor:black]; + [description_ setColor:gray]; + [source_ setColor:black]; + [version_ setColor:blue]; + + CGColorSpaceRelease(space); +} + +- (void) setSelected:(BOOL)selected { + [self _setSelected:(selected ? 1.0 : 0.0)]; + [super setSelected:selected]; +} + +- (void) setSelected:(BOOL)selected withFade:(BOOL)fade { + if (!fade) + [self _setSelected:(selected ? 1.0 : 0.0)]; + [super setSelected:selected withFade:fade]; +} + +- (void) _setSelectionFadeFraction:(float)fraction { + [self _setSelected:fraction]; + [super _setSelectionFadeFraction:fraction]; +} + +@end +/* }}} */ + +/* Database Implementation {{{ */ +@implementation Database + +- (void) dealloc { + _assert(false); + [super dealloc]; +} + +- (void) _readStatus:(NSNumber *)fd { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + __gnu_cxx::stdio_filebuf ib([fd intValue], std::ios::in); + std::istream is(&ib); + std::string line; + + const char *error; + int offset; + pcre *code = pcre_compile("^([^:]*):([^:]*):([^:]*):(.*)$", 0, &error, &offset, NULL); + + pcre_extra *study = NULL; + int capture; + pcre_fullinfo(code, study, PCRE_INFO_CAPTURECOUNT, &capture); + int matches[(capture + 1) * 3]; + + while (std::getline(is, line)) { + const char *data(line.c_str()); + + _assert(pcre_exec(code, study, data, line.size(), 0, 0, matches, sizeof(matches) / sizeof(matches[0])) >= 0); + + std::istringstream buffer(line.substr(matches[6], matches[7] - matches[6])); + float percent; + buffer >> percent; + [delegate_ setPercent:(percent / 100)]; + + NSString *string = [NSString stringWithCString:(data + matches[8]) length:(matches[9] - matches[8])]; + std::string type(line.substr(matches[2], matches[3] - matches[2])); + + if (type == "pmerror") + [delegate_ setError:string]; + else if (type == "pmstatus") + [delegate_ setTitle:string]; + else if (type == "pmconffile") + ; + else _assert(false); + } + + [pool release]; + _assert(false); +} + +- (void) _readOutput:(NSNumber *)fd { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + __gnu_cxx::stdio_filebuf ib([fd intValue], std::ios::in); + std::istream is(&ib); + std::string line; + + while (std::getline(is, line)) + [delegate_ addOutput:[NSString stringWithCString:line.c_str()]]; + + [pool release]; + _assert(false); +} + +- (Package *) packageWithName:(NSString *)name { + pkgCache::PkgIterator iterator(cache_->FindPkg([name cString])); + return iterator.end() ? nil : [Package packageWithIterator:iterator database:self]; +} + +- (Database *) init { + if ((self = [super init]) != nil) { + records_ = NULL; + resolver_ = NULL; + fetcher_ = NULL; + lock_ = NULL; + + sources_ = [[NSMutableDictionary dictionaryWithCapacity:16] retain]; + packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; + + int fds[2]; + + _assert(pipe(fds) != -1); + statusfd_ = fds[1]; + + [NSThread + detachNewThreadSelector:@selector(_readStatus:) + toTarget:self + withObject:[[NSNumber numberWithInt:fds[0]] retain] + ]; + + _assert(pipe(fds) != -1); + _assert(dup2(fds[1], 1) != -1); + _assert(close(fds[1]) != -1); + + [NSThread + detachNewThreadSelector:@selector(_readOutput:) + toTarget:self + withObject:[[NSNumber numberWithInt:fds[0]] retain] + ]; + } return self; +} + +- (pkgCacheFile &) cache { + return cache_; +} + +- (pkgRecords *) records { + return records_; +} + +- (pkgProblemResolver *) resolver { + return resolver_; +} + +- (pkgAcquire &) fetcher { + return *fetcher_; +} + +- (NSArray *) packages { + return packages_; +} + +- (void) reloadData { + _error->Discard(); + delete list_; + manager_ = NULL; + delete lock_; + delete fetcher_; + delete resolver_; + delete records_; + cache_.Close(); + + if (!cache_.Open(progress_, true)) { + fprintf(stderr, "repairing corrupted database...\n"); + _error->Discard(); + [self update]; + _assert(cache_.Open(progress_, true)); + } + + now_ = [NSDate date]; + + records_ = new pkgRecords(cache_); + resolver_ = new pkgProblemResolver(cache_); + fetcher_ = new pkgAcquire(&status_); + lock_ = NULL; + + list_ = new pkgSourceList(); + _assert(list_->ReadMainList()); + + [sources_ removeAllObjects]; + for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { + std::vector *indices = (*source)->GetIndexFiles(); + for (std::vector::const_iterator index = indices->begin(); index != indices->end(); ++index) + [sources_ + setObject:[[[Source alloc] initWithMetaIndex:*source] autorelease] + forKey:[NSNumber numberWithLong:reinterpret_cast(*index)] + ]; + } + + [packages_ removeAllObjects]; + for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) + if (Package *package = [Package packageWithIterator:iterator database:self]) + if ([package source] != nil || [package installed] != nil) + [packages_ addObject:package]; +} + +- (void) prepare { + pkgRecords records(cache_); + + lock_ = new FileFd(); + lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); + _assert(!_error->PendingError()); + + pkgSourceList list; + _assert(list.ReadMainList()); + + manager_ = (_system->CreatePM(cache_)); + _assert(manager_->GetArchives(fetcher_, &list, &records)); + _assert(!_error->PendingError()); +} + +- (void) perform { + if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) + return; + + _system->UnLock(); + pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_); + + if (result == pkgPackageManager::Failed) + return; + if (_error->PendingError()) + return; + if (result != pkgPackageManager::Completed) + return; +} + +- (void) update { + pkgSourceList list; + _assert(list.ReadMainList()); + + FileFd lock; + lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); + _assert(!_error->PendingError()); + + pkgAcquire fetcher(&status_); + _assert(list.GetIndexes(&fetcher)); + _assert(fetcher.Run(PulseInterval_) != pkgAcquire::Failed); + + bool failed = false; + for (pkgAcquire::ItemIterator item = fetcher.ItemsBegin(); item != fetcher.ItemsEnd(); item++) + if ((*item)->Status != pkgAcquire::Item::StatDone) { + (*item)->Finished(); + failed = true; + } + + if (!failed && _config->FindB("APT::Get::List-Cleanup", true) == true) { + _assert(fetcher.Clean(_config->FindDir("Dir::State::lists"))); + _assert(fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/")); + } + + [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"]; +} + +- (void) upgrade { + _assert(cache_->DelCount() == 0 && cache_->InstCount() == 0); + _assert(pkgApplyStatus(cache_)); + + if (cache_->BrokenCount() != 0) { + _assert(pkgFixBroken(cache_)); + _assert(cache_->BrokenCount() == 0); + _assert(pkgMinimizeUpgrade(cache_)); + } + + _assert(pkgDistUpgrade(cache_)); +} + +- (void) setDelegate:(id)delegate { + delegate_ = delegate; + status_.setDelegate(delegate); + progress_.setDelegate(delegate); +} + +- (Source *) getSource:(const pkgCache::PkgFileIterator &)file { + pkgIndexFile *index(NULL); + list_->FindIndex(file, index); + return [sources_ objectForKey:[NSNumber numberWithLong:reinterpret_cast(index)]]; +} + +@end +/* }}} */ + +/* Progress Data {{{ */ +@interface ProgressData : NSObject { + SEL selector_; + id target_; + id object_; +} + +- (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object; + +- (SEL) selector; +- (id) target; +- (id) object; +@end + +@implementation ProgressData + +- (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object { + if ((self = [super init]) != nil) { + selector_ = selector; + target_ = target; + object_ = object; + } return self; +} + +- (SEL) selector { + return selector_; +} + +- (id) target { + return target_; +} + +- (id) object { + return object_; +} + +@end +/* }}} */ +/* Progress View {{{ */ +@interface ProgressView : UIView < + ProgressDelegate +> { + UIView *view_; + UIView *background_; + UITransitionView *transition_; + UIView *overlay_; + UINavigationBar *navbar_; + UIProgressBar *progress_; + UITextView *output_; + UITextLabel *status_; + id delegate_; +} + +- (void) dealloc; + +- (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to; + +- (ProgressView *) initWithFrame:(struct CGRect)frame delegate:(id)delegate; +- (void) setContentView:(UIView *)view; +- (void) resetView; + +- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button; + +- (void) _retachThread; +- (void) _detachNewThreadData:(ProgressData *)data; +- (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title; + +- (void) setError:(NSString *)error; +- (void) _setError:(NSString *)error; + +- (void) setTitle:(NSString *)title; +- (void) _setTitle:(NSString *)title; + +- (void) setPercent:(float)percent; +- (void) _setPercent:(NSNumber *)percent; + +- (void) addOutput:(NSString *)output; +- (void) _addOutput:(NSString *)output; +@end + +@protocol ProgressViewDelegate +- (void) progressViewIsComplete:(ProgressView *)sender; +@end + +@implementation ProgressView + +- (void) dealloc { + [view_ release]; + if (background_ != nil) + [background_ release]; + [transition_ release]; + [overlay_ release]; + [navbar_ release]; + [progress_ release]; + [output_ release]; + [status_ release]; + [super dealloc]; +} + +- (void) transitionViewDidComplete:(UITransitionView*)view fromView:(UIView*)from toView:(UIView*)to { + if (bootstrap_ && from == overlay_ && to == view_) + exit(0); +} + +- (ProgressView *) initWithFrame:(struct CGRect)frame delegate:(id)delegate { + if ((self = [super initWithFrame:frame]) != nil) { + delegate_ = delegate; + + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + + CGColor black(space, 0.0, 0.0, 0.0, 1.0); + CGColor white(space, 1.0, 1.0, 1.0, 1.0); + CGColor clear(space, 0.0, 0.0, 0.0, 0.0); + + transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]]; + [transition_ setDelegate:self]; + + overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]]; + + if (bootstrap_) + [overlay_ setBackgroundColor:black]; + else { + background_ = [[UIView alloc] initWithFrame:[self bounds]]; + [background_ setBackgroundColor:black]; + [self addSubview:background_]; + } + + [self addSubview:transition_]; + + CGSize navsize = [UINavigationBar defaultSize]; + CGRect navrect = {{0, 0}, navsize}; + + navbar_ = [[UINavigationBar alloc] initWithFrame:navrect]; + [overlay_ addSubview:navbar_]; + + [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]; + [overlay_ addSubview:progress_]; + + 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]; + + [overlay_ addSubview:output_]; + [overlay_ addSubview:status_]; + + [progress_ setStyle:0]; + + CGColorSpaceRelease(space); + } return self; +} + +- (void) setContentView:(UIView *)view { + view_ = [view retain]; +} + +- (void) resetView { + [transition_ transition:6 toView:view_]; +} + +- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button { + [sheet dismiss]; +} + +- (void) _retachThread { + [delegate_ progressViewIsComplete:self]; + [self resetView]; +} + +- (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 { + [navbar_ popNavigationItem]; + UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease]; + [navbar_ pushNavigationItem:navitem]; + + [status_ setText:nil]; + [output_ setText:@""]; + [progress_ setProgress:0]; + + [transition_ transition:6 toView:overlay_]; + + [NSThread + detachNewThreadSelector:@selector(_detachNewThreadData:) + toTarget:self + withObject:[[ProgressData alloc] + initWithSelector:selector + target:target + object:object + ] + ]; +} + +- (void) setError:(NSString *)error { + [self + performSelectorOnMainThread:@selector(_setError:) + withObject:error + waitUntilDone:YES + ]; +} + +- (void) _setError:(NSString *)error { + UIAlertSheet *sheet = [[[UIAlertSheet alloc] + initWithTitle:@"Package Error" + buttons:[NSArray arrayWithObjects:@"Okay", nil] + defaultButtonIndex:0 + delegate:self + context:self + ] autorelease]; + + [sheet setBodyText:error]; + [sheet popupAlertAnimated:YES]; +} + +- (void) setTitle:(NSString *)title { + [self + performSelectorOnMainThread:@selector(_setTitle:) + withObject:title + waitUntilDone:YES + ]; +} + +- (void) _setTitle:(NSString *)title { + [status_ setText:[title stringByAppendingString:@"..."]]; +} + +- (void) setPercent:(float)percent { + [self + performSelectorOnMainThread:@selector(_setPercent:) + withObject:[NSNumber numberWithFloat:percent] + waitUntilDone:YES + ]; +} + +- (void) _setPercent:(NSNumber *)percent { + [progress_ setProgress:[percent floatValue]]; +} + +- (void) addOutput:(NSString *)output { + [self + performSelectorOnMainThread:@selector(_addOutput:) + withObject:output + waitUntilDone:YES + ]; +} + +- (void) _addOutput:(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]; +} + +@end +/* }}} */ + +/* Package Table {{{ */ +@protocol PackageTableDelegate +- (void) packageTable:(id)table packageSelected:(Package *)package; +@end + +@interface PackageTable : UIView { + SEL versioner_; + UISectionList *list_; + + id delegate_; + NSArray *packages_; + NSMutableArray *sections_; +} + +- (void) dealloc; + +- (int) numberOfSectionsInSectionList:(UISectionList *)list; +- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section; +- (int) sectionList:(UISectionList *)list rowForSection:(int)section; + +- (int) numberOfRowsInTable:(UITable *)table; +- (float) table:(UITable *)table heightForRow:(int)row; +- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing; +- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row; +- (void) tableRowSelected:(NSNotification *)notification; + +- (id) initWithFrame:(CGRect)frame versioner:(SEL)versioner; + +- (void) setDelegate:(id)delegate; +- (void) setPackages:(NSArray *)packages; + +- (void) resetViewAnimated:(BOOL)animated; +- (UITable *) table; +@end + +@implementation PackageTable + +- (void) dealloc { + [list_ release]; + [sections_ release]; + if (packages_ != nil) + [packages_ release]; + [super dealloc]; +} + +- (int) numberOfSectionsInSectionList:(UISectionList *)list { + return [sections_ count]; +} + +- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section { + return [[sections_ objectAtIndex:section] name]; +} + +- (int) sectionList:(UISectionList *)list rowForSection:(int)section { + return [[sections_ objectAtIndex:section] row]; +} + +- (int) numberOfRowsInTable:(UITable *)table { + return [packages_ count]; +} + +- (float) table:(UITable *)table heightForRow:(int)row { + return 100; +} + +- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing { + if (reusing == nil) + reusing = [[[PackageCell alloc] initWithVersioner:versioner_] autorelease]; + [(PackageCell *)reusing setPackage:[packages_ objectAtIndex:row]]; + return reusing; +} + +- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row { + return NO; +} + +- (void) tableRowSelected:(NSNotification *)notification { + int row = [[notification object] selectedRow]; + [delegate_ packageTable:self packageSelected:(row == INT_MAX ? nil : [packages_ objectAtIndex:row])]; +} + +- (id) initWithFrame:(CGRect)frame versioner:(SEL)versioner { + if ((self = [super initWithFrame:frame]) != nil) { + versioner_ = versioner; + sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; + + list_ = [[UISectionList alloc] initWithFrame:[self bounds] showSectionIndex:YES]; + [list_ setDataSource:self]; + + UITableColumn *column = [[[UITableColumn alloc] + initWithTitle:@"Name" + identifier:@"name" + width:frame.size.width + ] autorelease]; + + UITable *table = [list_ table]; + [table setSeparatorStyle:1]; + [table addTableColumn:column]; + [table setDelegate:self]; + [table setReusesTableCells:YES]; + + [self addSubview:list_]; + } return self; +} + +- (void) setDelegate:(id)delegate { + delegate_ = delegate; +} + +- (void) setPackages:(NSArray *)packages { + if (packages_ != nil) + [packages_ autorelease]; + _assert(packages != nil); + packages_ = [packages retain]; + + [sections_ removeAllObjects]; + + Section *section = nil; + + for (size_t offset(0); offset != [packages_ count]; ++offset) { + Package *package = [packages_ objectAtIndex:offset]; + NSString *name = [package index]; + + if (section == nil || ![[section name] isEqual:name]) { + section = [[[Section alloc] initWithName:name row:offset] autorelease]; + [sections_ addObject:section]; + } + + [section addPackage:package]; + } + + [list_ reloadData]; +} + +- (void) resetViewAnimated:(BOOL)animated { + [[list_ table] selectRow:-1 byExtendingSelection:NO withFade:animated]; +} + +- (UITable *) table { + return [list_ table]; +} + +@end +/* }}} */ + +/* Section Cell {{{ */ +@interface SectionCell : UITableCell { + UITextLabel *name_; + UITextLabel *count_; +} + +- (void) dealloc; + +- (id) init; +- (void) setSection:(Section *)section; + +- (void) _setSelected:(float)fraction; +- (void) setSelected:(BOOL)selected; +- (void) setSelected:(BOOL)selected withFade:(BOOL)fade; +- (void) _setSelectionFadeFraction:(float)fraction; + +@end + +@implementation SectionCell + +- (void) dealloc { + [name_ release]; + [count_ release]; + [super dealloc]; +} + +- (id) init { + if ((self = [super init]) != nil) { + GSFontRef bold = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 22); + GSFontRef small = GSFontCreateWithName("Helvetica", kGSFontTraitBold, 12); + + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGColor clear(space, 0, 0, 0, 0); + CGColor white(space, 1, 1, 1, 1); + + name_ = [[UITextLabel alloc] initWithFrame:CGRectMake(48, 9, 250, 25)]; + [name_ setBackgroundColor:clear]; + [name_ setFont:bold]; + + count_ = [[UITextLabel alloc] initWithFrame:CGRectMake(11, 7, 29, 32)]; + [count_ setCentersHorizontally:YES]; + [count_ setBackgroundColor:clear]; + [count_ setFont:small]; + [count_ setColor:white]; + + UIImageView *folder = [[[UIImageView alloc] initWithFrame:CGRectMake(8, 7, 32, 32)] autorelease]; + [folder setImage:[UIImage applicationImageNamed:@"folder.png"]]; + + [self addSubview:folder]; + [self addSubview:name_]; + [self addSubview:count_]; + + [self _setSelected:0]; + + CGColorSpaceRelease(space); + + CFRelease(small); + CFRelease(bold); + } return self; +} + +- (void) setSection:(Section *)section { + if (section == nil) { + [name_ setText:@"All Packages"]; + [count_ setText:nil]; + } else { + NSString *name = [section name]; + [name_ setText:(name == nil ? @"(No Section)" : name)]; + [count_ setText:[NSString stringWithFormat:@"%d", [section count]]]; + } +} + +- (void) _setSelected:(float)fraction { + CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + + CGColor black(space, + interpolate(0.0, 1.0, fraction), + interpolate(0.0, 1.0, fraction), + interpolate(0.0, 1.0, fraction), + 1.0); + + [name_ setColor:black]; + + CGColorSpaceRelease(space); +} + +- (void) setSelected:(BOOL)selected { + [self _setSelected:(selected ? 1.0 : 0.0)]; + [super setSelected:selected]; +} + +- (void) setSelected:(BOOL)selected withFade:(BOOL)fade { + if (!fade) + [self _setSelected:(selected ? 1.0 : 0.0)]; + [super setSelected:selected withFade:fade]; +} + +- (void) _setSelectionFadeFraction:(float)fraction { + [self _setSelected:fraction]; + [super _setSelectionFadeFraction:fraction]; +} + +@end +/* }}} */ +/* Install View {{{ */ +@interface InstallView : ResetView < + PackageTableDelegate +> { + NSArray *sections_; + UITable *list_; + PackageTable *table_; + PackageView *view_; + NSString *section_; + NSString *package_; + NSMutableArray *packages_; +} + +- (void) dealloc; + +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button; + +- (int) numberOfRowsInTable:(UITable *)table; +- (float) table:(UITable *)table heightForRow:(int)row; +- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing; +- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row; +- (void) tableRowSelected:(NSNotification *)notification; + +- (void) packageTable:(id)table packageSelected:(Package *)package; + +- (id) initWithFrame:(CGRect)frame; +- (void) setPackages:(NSArray *)packages; +- (void) setDelegate:(id)delegate; +@end + +@implementation InstallView + +- (void) dealloc { + [packages_ release]; + if (sections_ != nil) + [sections_ release]; + if (list_ != nil) + [list_ release]; + if (table_ != nil) + [table_ release]; + if (view_ != nil) + [view_ release]; + if (section_ != nil) + [section_ release]; + if (package_ != nil) + [package_ release]; + [super dealloc]; +} + +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { + if (button == 0) { + [[view_ package] install]; + [delegate_ resolve]; + [delegate_ perform]; + } +} + +- (int) numberOfRowsInTable:(UITable *)table { + return [sections_ count] + 1; +} + +- (float) table:(UITable *)table heightForRow:(int)row { + return 45; +} + +- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing { + if (reusing == nil) + reusing = [[[SectionCell alloc] init] autorelease]; + if (row == 0) + [(SectionCell *)reusing setSection:nil]; + else + [(SectionCell *)reusing setSection:[sections_ objectAtIndex:(row - 1)]]; + return reusing; +} + +- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row { + return YES; +} + +- (void) tableRowSelected:(NSNotification *)notification { + int row = [[notification object] selectedRow]; + + if (row == INT_MAX) { + [section_ release]; + section_ = nil; + + [table_ release]; + table_ = nil; + } else { + _assert(section_ == nil); + _assert(table_ == nil); + + Section *section; + NSString *name; + + if (row == 0) { + section = nil; + section_ = nil; + name = @"All Packages"; + } else { + section = [sections_ objectAtIndex:(row - 1)]; + name = [section name]; + section_ = [name retain]; + } + + table_ = [[PackageTable alloc] initWithFrame:[transition_ bounds] versioner:@selector(latest)]; + [table_ setDelegate:self]; + [table_ setPackages:(section == nil ? packages_ : [section packages])]; + + [self pushView:table_ withTitle:name backButtonTitle:@"Packages" rightButton:nil]; + } +} + +- (void) packageTable:(id)table packageSelected:(Package *)package { + if (package == nil) { + [package_ release]; + package_ = nil; + + [view_ release]; + view_ = nil; + } else { + _assert(package_ == nil); + _assert(view_ == nil); + + package_ = [[package name] retain]; + + view_ = [[PackageView alloc] initWithFrame:[transition_ bounds]]; + [view_ setDelegate:delegate_]; + + [view_ setPackage:package]; + + [self pushView:view_ withTitle:[package name] backButtonTitle:nil rightButton:@"Install"]; + } +} + +- (id) initWithFrame:(CGRect)frame { + if ((self = [super initWithFrame:frame]) != nil) { + packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; + + list_ = [[UITable alloc] initWithFrame:[transition_ bounds]]; + [self pushView:list_ withTitle:@"Install" backButtonTitle:@"Sections" rightButton:nil]; + + UITableColumn *column = [[[UITableColumn alloc] + initWithTitle:@"Name" + identifier:@"name" + width:frame.size.width + ] autorelease]; + + [list_ setDataSource:self]; + [list_ setSeparatorStyle:1]; + [list_ addTableColumn:column]; + [list_ setDelegate:self]; + [list_ setReusesTableCells:YES]; + + [transition_ transition:0 toView:list_]; + } return self; +} + +- (void) setPackages:(NSArray *)packages { + [packages_ removeAllObjects]; + + for (size_t i(0); i != [packages count]; ++i) { + Package *package([packages objectAtIndex:i]); + if ([package installed] == nil) + [packages_ addObject:package]; + } + + [packages_ sortUsingSelector:@selector(compareBySectionAndName:)]; + NSMutableArray *sections = [NSMutableArray arrayWithCapacity:16]; + + Section *nsection = nil; + Package *npackage = nil; + + Section *section = nil; + for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) { + Package *package = [packages_ objectAtIndex:offset]; + NSString *name = [package section]; + + if (section == nil || name != nil && ![[section name] isEqual:name]) { + section = [[[Section alloc] initWithName:name row:offset] autorelease]; + + if (name == nil || [name isEqualToString:section_]) + nsection = section; + [sections addObject:section]; + } + + if ([[package name] isEqualToString:package_]) + npackage = package; + [section addPackage:package]; + } + + if (sections_ != nil) + [sections_ release]; + sections_ = [sections retain]; + + [packages_ sortUsingSelector:@selector(compareByName:)]; + + [list_ reloadData]; + + unsigned views(0); -- (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object { - if ((self = [super init]) != nil) { - selector_ = selector; - target_ = target; - object_ = object; - } return self; -} + if (npackage != nil) + [view_ setPackage:npackage]; + else if (package_ != nil) + ++views; -- (SEL) selector { - return selector_; -} + if (table_ != nil && section_ == nil) + [table_ setPackages:packages_]; + else if (nsection != nil) + [table_ setPackages:[nsection packages]]; + else if (section_ != nil) + ++views; -- (id) target { - return target_; + if (views != 0) + [self popViews:views]; + [self setPrompt]; } -- (id) object { - return object_; +- (void) setDelegate:(id)delegate { + if (view_ != nil) + [view_ setDelegate:delegate]; + [super setDelegate:delegate]; } @end /* }}} */ -/* Progress View {{{ */ -@interface ProgressView : UIView < - ProgressDelegate +/* Changes View {{{ */ +@interface ChangesView : ResetView < + PackageTableDelegate > { - UIView *view_; - UITransitionView *transition_; - UIView *overlay_; - UINavigationBar *navbar_; - UIProgressBar *progress_; - UITextView *output_; - UITextLabel *status_; - id delegate_; - UIAlertSheet *alert_; + UISectionList *list_; + NSMutableArray *packages_; + NSMutableArray *sections_; + PackageView *view_; + NSString *package_; + size_t count_; } - (void) dealloc; -- (ProgressView *) initWithFrame:(struct CGRect)frame delegate:(id)delegate; -- (void) setContentView:(UIView *)view; -- (void) resetView; - -- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button; - -- (void) _retachThread; -- (void) _detachNewThreadData:(ProgressData *)data; -- (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object; - -- (void) setError:(NSString *)error; -- (void) _setError:(NSString *)error; - -- (void) setTitle:(NSString *)title; -- (void) _setTitle:(NSString *)title; - -- (void) setPercent:(float)percent; -- (void) _setPercent:(NSNumber *)percent; +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button; -- (void) addOutput:(NSString *)output; -- (void) _addOutput:(NSString *)output; +- (int) numberOfSectionsInSectionList:(UISectionList *)list; +- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section; +- (int) sectionList:(UISectionList *)list rowForSection:(int)section; -- (void) setStatusIMSHit; -- (void) setStatusDone; -- (void) setStatusFail; +- (int) numberOfRowsInTable:(UITable *)table; +- (float) table:(UITable *)table heightForRow:(int)row; +- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing; +- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row; +- (void) tableRowSelected:(NSNotification *)notification; -- (void) setStatusStart; -- (void) setStatusStop; -@end +- (id) initWithFrame:(CGRect)frame; +- (void) setPackages:(NSArray *)packages; +- (void) _resetView; +- (size_t) count; -@protocol ProgressViewDelegate -- (void) progressViewIsComplete:(ProgressView *)sender; +- (void) setDelegate:(id)delegate; @end -@implementation ProgressView +@implementation ChangesView - (void) dealloc { - [view_ release]; - [transition_ release]; - [overlay_ release]; - [navbar_ release]; - [progress_ release]; - [output_ release]; - [status_ release]; + [list_ release]; + [packages_ release]; + [sections_ release]; + if (view_ != nil) + [view_ release]; + if (package_ != nil) + [package_ release]; [super dealloc]; } -- (ProgressView *) initWithFrame:(struct CGRect)frame delegate:(id)delegate { - if ((self = [super initWithFrame:frame]) != nil) { - delegate_ = delegate; - alert_ = nil; +- (int) numberOfSectionsInSectionList:(UISectionList *)list { + return [sections_ count]; +} - transition_ = [[UITransitionView alloc] initWithFrame:[self bounds]]; - [self addSubview:transition_]; +- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section { + return [[sections_ objectAtIndex:section] name]; +} - CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); - float black[] = {0.0, 0.0, 0.0, 1.0}; - float white[] = {1.0, 1.0, 1.0, 1.0}; - float clear[] = {0.0, 0.0, 0.0, 0.0}; +- (int) sectionList:(UISectionList *)list rowForSection:(int)section { + return [[sections_ objectAtIndex:section] row]; +} - overlay_ = [[UIView alloc] initWithFrame:[transition_ bounds]]; - [overlay_ setBackgroundColor:CGColorCreate(space, black)]; +- (int) numberOfRowsInTable:(UITable *)table { + return [packages_ count]; +} - CGSize navsize = [UINavigationBar defaultSize]; - CGRect navrect = {{0, 0}, navsize}; +- (float) table:(UITable *)table heightForRow:(int)row { + return 100; +} - navbar_ = [[UINavigationBar alloc] initWithFrame:navrect]; - [overlay_ addSubview:navbar_]; +- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col reusing:(UITableCell *)reusing { + if (reusing == nil) + reusing = [[[PackageCell alloc] initWithVersioner:NULL] autorelease]; + [(PackageCell *)reusing setPackage:[packages_ objectAtIndex:row]]; + return reusing; +} - [navbar_ setBarStyle:1]; - [navbar_ setDelegate:self]; +- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row { + return NO; +} - UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Running..."] autorelease]; - [navbar_ pushNavigationItem:navitem]; +- (void) tableRowSelected:(NSNotification *)notification { + int row = [[notification object] selectedRow]; + [self packageTable:self packageSelected:(row == INT_MAX ? nil : [packages_ objectAtIndex:row])]; +} - CGRect bounds = [overlay_ bounds]; - CGSize prgsize = [UIProgressBar defaultSize]; +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { + switch (button) { + case 0: + [[view_ package] install]; + [delegate_ resolve]; + [delegate_ perform]; + break; - CGRect prgrect = {{ - (bounds.size.width - prgsize.width) / 2, - bounds.size.height - prgsize.height - 20 - }, prgsize}; + case 1: + [delegate_ upgrade]; + break; + } +} - progress_ = [[UIProgressBar alloc] initWithFrame:prgrect]; - [overlay_ addSubview:progress_]; +- (void) packageTable:(id)table packageSelected:(Package *)package { + if (package == nil) { + [package_ release]; + package_ = nil; - status_ = [[UITextLabel alloc] initWithFrame:CGRectMake( - 10, - bounds.size.height - prgsize.height - 50, - bounds.size.width - 20, - 24 - )]; + [view_ release]; + view_ = nil; + } else { + _assert(package_ == nil); + _assert(view_ == nil); - [status_ setColor:CGColorCreate(space, white)]; - [status_ setBackgroundColor:CGColorCreate(space, clear)]; + package_ = [[package name] retain]; - [status_ setCentersHorizontally:YES]; + view_ = [[PackageView alloc] initWithFrame:[transition_ bounds]]; + [view_ setDelegate:delegate_]; - output_ = [[UITextView alloc] initWithFrame:CGRectMake( - 10, - navrect.size.height + 20, - bounds.size.width - 20, - bounds.size.height - navsize.height - 62 - navrect.size.height + [view_ setPackage:package]; + + [self pushView:view_ withTitle:[package name] backButtonTitle:nil rightButton:( + [package upgradable] ? @"Upgrade" : @"Install" )]; + } +} - //[output_ setTextFont:@"Courier New"]; - [output_ setTextSize:12]; +- (id) initWithFrame:(CGRect)frame { + if ((self = [super initWithFrame:frame]) != nil) { + packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; + sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; - [output_ setTextColor:CGColorCreate(space, white)]; - [output_ setBackgroundColor:CGColorCreate(space, clear)]; + list_ = [[UISectionList alloc] initWithFrame:[transition_ bounds] showSectionIndex:NO]; + [list_ setShouldHideHeaderInShortLists:NO]; + [list_ setDataSource:self]; + //[list_ setSectionListStyle:1]; - [output_ setMarginTop:0]; - [output_ setAllowsRubberBanding:YES]; + UITableColumn *column = [[[UITableColumn alloc] + initWithTitle:@"Name" + identifier:@"name" + width:frame.size.width + ] autorelease]; - [overlay_ addSubview:output_]; - [overlay_ addSubview:status_]; + UITable *table = [list_ table]; + [table setSeparatorStyle:1]; + [table addTableColumn:column]; + [table setDelegate:self]; + [table setReusesTableCells:YES]; - [progress_ setStyle:0]; + [self pushView:list_ withTitle:@"Changes" backButtonTitle:nil rightButton:nil]; } return self; } -- (void) setContentView:(UIView *)view { - view_ = view; -} - -- (void) resetView { - [transition_ transition:6 toView:view_]; - _trace(); -} +- (void) setPackages:(NSArray *)packages { + [packages_ removeAllObjects]; + for (size_t i(0); i != [packages count]; ++i) { + Package *package([packages objectAtIndex:i]); + if ([package installed] == nil || [package upgradable]) + [packages_ addObject:package]; + } -- (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button { - [alert_ dismiss]; - [alert_ release]; - alert_ = nil; -} + [packages_ sortUsingSelector:@selector(compareForChanges:)]; -- (void) _retachThread { - _trace(); - [delegate_ progressViewIsComplete:self]; - _trace(); - [self resetView]; - _trace(); -} + [sections_ removeAllObjects]; -- (void) _detachNewThreadData:(ProgressData *)data { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + Section *upgradable = [[[Section alloc] initWithName:@"Available Upgrades" row:0] autorelease]; + Section *section = nil; - _trace(); - [[data target] performSelector:[data selector] withObject:[data object]]; - _trace(); - [self performSelectorOnMainThread:@selector(_retachThread) withObject:nil waitUntilDone:YES]; - _trace(); + count_ = 0; + Package *npackage = nil; + for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) { + Package *package = [packages_ objectAtIndex:offset]; + if ([[package name] isEqualToString:package_]) + npackage = package; + + if ([package upgradable]) + [upgradable addPackage:package]; + else { + NSDate *seen = [package seen]; + + NSString *name; + CFStringRef formatted = NULL; + + if (seen == nil) + name = @"n/a ?"; + else { + CFLocaleRef locale = CFLocaleCopyCurrent(); + CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, locale, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle); + formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) seen); + name = (NSString *) formatted; + CFRelease(formatter); + CFRelease(locale); + } + + if (section == nil || ![[section name] isEqual:name]) { + section = [[[Section alloc] initWithName:name row:offset] autorelease]; + [sections_ addObject:section]; + } + + [section addPackage:package]; + + if (formatted != NULL) + CFRelease(formatted); + } + } - [data release]; - [pool release]; -} + count_ = [[upgradable packages] count]; + if (count_ != 0) + [sections_ insertObject:upgradable atIndex:0]; -- (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object { - [status_ setText:nil]; - [output_ setText:@""]; - [progress_ setProgress:0]; + [list_ reloadData]; - [transition_ transition:6 toView:overlay_]; + if (npackage != nil) + [view_ setPackage:npackage]; + else if (package_ != nil) + [self popViews:1]; - [NSThread - detachNewThreadSelector:@selector(_detachNewThreadData:) - toTarget:self - withObject:[[ProgressData alloc] - initWithSelector:selector - target:target - object:object - ] - ]; + [self _resetView]; + [self setPrompt]; } -- (void) setStatusIMSHit { - _trace(); +- (void) _resetView { + if ([views_ count] == 1) + [navbar_ showButtonsWithLeftTitle:(count_ == 0 ? nil : @"Upgrade All") rightTitle:nil]; } -- (void) setStatusDone { - _trace(); +- (size_t) count { + return count_; } -- (void) setStatusFail { - _trace(); +- (void) setDelegate:(id)delegate { + if (view_ != nil) + [view_ setDelegate:delegate]; + [super setDelegate:delegate]; } -- (void) setError:(NSString *)error { - [self - performSelectorOnMainThread:@selector(_setError:) - withObject:error - waitUntilDone:YES - ]; +@end +/* }}} */ +/* Manage View {{{ */ +@interface ManageView : ResetView < + PackageTableDelegate +> { + PackageTable *table_; + PackageView *view_; + NSString *package_; } -- (void) _setError:(NSString *)error { - _assert(alert_ == nil); +- (void) dealloc; - alert_ = [[UIAlertSheet alloc] - initWithTitle:@"Package Error" - buttons:[NSArray arrayWithObjects:@"Okay", nil] - defaultButtonIndex:0 - delegate:self - context:self - ]; +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button; - [alert_ setBodyText:error]; - [alert_ popupAlertAnimated:YES]; -} +- (void) packageTable:(id)table packageSelected:(Package *)package; -- (void) setTitle:(NSString *)title { - [self - performSelectorOnMainThread:@selector(_setTitle:) - withObject:title - waitUntilDone:YES - ]; -} +- (id) initWithFrame:(CGRect)frame; +- (void) setPackages:(NSArray *)packages; -- (void) _setTitle:(NSString *)title { - [status_ setText:[title stringByAppendingString:@"..."]]; -} +- (void) setDelegate:(id)delegate; +@end -- (void) setPercent:(float)percent { - [self - performSelectorOnMainThread:@selector(_setPercent:) - withObject:[NSNumber numberWithFloat:percent] - waitUntilDone:YES - ]; +@implementation ManageView + +- (void) dealloc { + [table_ release]; + if (view_ != nil) + [view_ release]; + if (package_ != nil) + [package_ release]; + [super dealloc]; } -- (void) _setPercent:(NSNumber *)percent { - [progress_ setProgress:[percent floatValue]]; +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { + if (button == 0) { + [[view_ package] remove]; + [delegate_ resolve]; + [delegate_ perform]; + } } -- (void) addOutput:(NSString *)output { - [self - performSelectorOnMainThread:@selector(_addOutput:) - withObject:output - waitUntilDone:YES - ]; +- (void) packageTable:(id)table packageSelected:(Package *)package { + if (package == nil) { + [package_ release]; + package_ = nil; + + [view_ release]; + view_ = nil; + } else { + _assert(package_ == nil); + _assert(view_ == nil); + + package_ = [[package name] retain]; + + view_ = [[PackageView alloc] initWithFrame:[transition_ bounds]]; + [view_ setDelegate:delegate_]; + + [view_ setPackage:package]; + + [self pushView:view_ withTitle:[package name] backButtonTitle:nil rightButton:@"Uninstall"]; + } } -- (void) _addOutput:(NSString *)output { - [output_ setText:[NSString stringWithFormat:@"%@\n%@", [output_ text], output]]; +- (id) initWithFrame:(CGRect)frame { + if ((self = [super initWithFrame:frame]) != nil) { + table_ = [[PackageTable alloc] initWithFrame:[transition_ bounds] versioner:@selector(latest)]; + [table_ setDelegate:self]; + + [self pushView:table_ withTitle:@"Uninstall" backButtonTitle:@"Packages" rightButton:nil]; + } return self; } -- (void) setStatusStart { - _trace(); +- (void) setPackages:(NSArray *)packages { + NSMutableArray *local = [NSMutableArray arrayWithCapacity:16]; + for (size_t i(0); i != [packages count]; ++i) { + Package *package([packages objectAtIndex:i]); + if ([package installed] != nil) + [local addObject:package]; + } + + [local sortUsingSelector:@selector(compareByName:)]; + + Package *npackage = nil; + for (size_t offset = 0, count = [local count]; offset != count; ++offset) { + Package *package = [local objectAtIndex:offset]; + if ([[package name] isEqualToString:package_]) + npackage = package; + } + + [table_ setPackages:local]; + + if (npackage != nil) + [view_ setPackage:npackage]; + else if (package_ != nil) + [self popViews:1]; + + [self setPrompt]; } -- (void) setStatusStop { - _trace(); +- (void) setDelegate:(id)delegate { + if (view_ != nil) + [view_ setDelegate:delegate]; + [super setDelegate:delegate]; } @end /* }}} */ - -@protocol PackagesDelegate - -- (void) perform; - +/* Search View {{{ */ +@protocol SearchViewDelegate +- (void) showKeyboard:(BOOL)show; @end -@interface Packages : UIView { - NSString *title_; - Database *database_; - bool (*filter_)(Package *package); +@interface SearchView : ResetView < + PackageTableDelegate +> { NSMutableArray *packages_; - NSMutableArray *sections_; - id delegate_; - UISectionList *list_; - UINavigationBar *navbar_; - UITransitionView *transition_; - Package *package_; - PackageView *pkgview_; - SEL selector_; + UIView *accessory_; + UISearchField *field_; + PackageTable *table_; + PackageView *view_; + NSString *package_; } -- (int) numberOfSectionsInSectionList:(UISectionList *)list; -- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section; -- (int) sectionList:(UISectionList *)list rowForSection:(int)section; - -- (int) numberOfRowsInTable:(UITable *)table; -- (float) table:(UITable *)table heightForRow:(int)row; -- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col; -- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row; -- (void) tableRowSelected:(NSNotification*)notification; +- (void) dealloc; - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button; -- (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item; +- (void) packageTable:(id)table packageSelected:(Package *)package; -- (Packages *) initWithFrame:(struct CGRect)frame title:(NSString *)title database:(Database *)database filter:(bool (*)(Package *))filter selector:(SEL)selector; -- (void) setDelegate:(id)delegate; -- (void) deselect; -- (void) reloadData; -@end +- (void) textFieldDidBecomeFirstResponder:(UITextField *)field; +- (void) textFieldDidResignFirstResponder:(UITextField *)field; -@implementation Packages +- (void) keyboardInputChanged:(UIFieldEditor *)editor; +- (BOOL) keyboardInput:(id)input shouldInsertText:(NSString *)text isMarkedText:(int)marked; -- (int) numberOfSectionsInSectionList:(UISectionList *)list { - return [sections_ count]; -} +- (id) initWithFrame:(CGRect)frame; +- (void) setPackages:(NSArray *)packages; -- (NSString *) sectionList:(UISectionList *)list titleForSection:(int)section { - return [[sections_ objectAtIndex:section] name]; -} +- (void) setDelegate:(id)delegate; +- (void) resetPackage:(Package *)package; +- (void) searchPackages; -- (int) sectionList:(UISectionList *)list rowForSection:(int)section { - return [[sections_ objectAtIndex:section] row]; -} +@end -- (int) numberOfRowsInTable:(UITable *)table { - return [packages_ count]; -} +@implementation SearchView -- (float) table:(UITable *)table heightForRow:(int)row { - return 64; +- (void) dealloc { + [packages_ release]; + [accessory_ release]; + [field_ release]; + [table_ release]; + if (view_ != nil) + [view_ release]; + if (package_ != nil) + [package_ release]; + [super dealloc]; } -- (UITableCell *) table:(UITable *)table cellForRow:(int)row column:(UITableColumn *)col { - Package *package = [packages_ objectAtIndex:row]; - PackageCell *cell = [[[PackageCell alloc] initWithPackage:package] autorelease]; - return cell; +- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { + if (button == 0) { + Package *package = [view_ package]; + if ([package installed] == nil) + [package install]; + else + [package remove]; + [delegate_ resolve]; + [delegate_ perform]; + } } -- (BOOL) table:(UITable *)table showDisclosureForRow:(int)row { - return YES; -} +- (void) packageTable:(id)table packageSelected:(Package *)package { + if (package == nil) { + [navbar_ setAccessoryView:accessory_ animate:(resetting_ ? NO : YES) goingBack:YES]; -- (void) tableRowSelected:(NSNotification*)notification { - int row = [[list_ table] selectedRow]; - if (row == INT_MAX) - return; + [package_ release]; + package_ = nil; - package_ = [packages_ objectAtIndex:row]; + [view_ release]; + view_ = nil; + } else { + [navbar_ setAccessoryView:nil animate:YES goingBack:NO]; - UINavigationItem *navitem = [[UINavigationItem alloc] initWithTitle:[package_ name]]; - [navbar_ pushNavigationItem:navitem]; + _assert(package_ == nil); + _assert(view_ == nil); + + package_ = [[package name] retain]; - [navbar_ showButtonsWithLeftTitle:nil rightTitle:title_]; + view_ = [[PackageView alloc] initWithFrame:[transition_ bounds]]; + [view_ setDelegate:delegate_]; - [pkgview_ setPackage:package_]; - [transition_ transition:1 toView:pkgview_]; + [self pushView:view_ withTitle:[package name] backButtonTitle:nil rightButton:nil]; + [self resetPackage:package]; + } } -- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { - if (button == 0) { - [package_ performSelector:selector_]; +- (void) textFieldDidBecomeFirstResponder:(UITextField *)field { + [delegate_ showKeyboard:YES]; + [table_ setEnabled:NO]; - pkgProblemResolver *resolver = [database_ resolver]; + /*CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); + CGColor dimmed(alpha, 0, 0, 0, 0.5); + [editor_ setBackgroundColor:dimmed]; + CGColorSpaceRelease(space);*/ +} - resolver->InstallProtect(); - if (!resolver->Resolve(true)) - _error->Discard(); +- (void) textFieldDidResignFirstResponder:(UITextField *)field { + [table_ setEnabled:YES]; + [delegate_ showKeyboard:NO]; +} - [delegate_ perform]; - } +- (void) keyboardInputChanged:(UIFieldEditor *)editor { + NSString *text([field_ text]); + [field_ setClearButtonStyle:(text == nil || [text length] == 0 ? 0 : 2)]; } -- (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item { - [self deselect]; - [navbar_ showButtonsWithLeftTitle:nil rightTitle:nil]; +- (BOOL) keyboardInput:(id)input shouldInsertText:(NSString *)text isMarkedText:(int)marked { + if ([text length] != 1 || [text characterAtIndex:0] != '\n') + return YES; + + [self searchPackages]; + [field_ resignFirstResponder]; + return NO; } -- (Packages *) initWithFrame:(struct CGRect)frame title:(NSString *)title database:(Database *)database filter:(bool (*)(Package *))filter selector:(SEL)selector { +- (id) initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame]) != nil) { - title_ = [title retain]; - database_ = [database retain]; - filter_ = filter; - selector_ = selector; + packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; - struct CGRect bounds = [self bounds]; - CGSize navsize = [UINavigationBar defaultSize]; - CGRect navrect = {{0, 0}, navsize}; + table_ = [[PackageTable alloc] initWithFrame:[transition_ bounds] versioner:@selector(latest)]; + [table_ setDelegate:self]; - navbar_ = [[UINavigationBar alloc] initWithFrame:navrect]; - [self addSubview:navbar_]; + CGRect area = [self bounds]; + area.origin.y = 30; + area.origin.x = 0; + area.size.width -= 12; + area.size.height = [UISearchField defaultHeight]; - [navbar_ setBarStyle:1]; - [navbar_ setDelegate:self]; + field_ = [[UISearchField alloc] initWithFrame:area]; - UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease]; - [navbar_ pushNavigationItem:navitem]; - [navitem setBackButtonTitle:@"Packages"]; + GSFontRef font = GSFontCreateWithName("Helvetica", kGSFontTraitNone, 16); + [field_ setFont:font]; + CFRelease(font); - transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake( - bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height - )]; + [field_ setPlaceholder:@"Package Names & Descriptions"]; + [field_ setPaddingTop:5]; + [field_ setDelegate:self]; - [self addSubview:transition_]; + UITextTraits *traits = [field_ textTraits]; + [traits setEditingDelegate:self]; + [traits setReturnKeyType:6]; - list_ = [[UISectionList alloc] initWithFrame:[transition_ bounds] showSectionIndex:NO]; - [list_ setDataSource:self]; + accessory_ = [[UIView alloc] initWithFrame:CGRectMake(6, 6, area.size.width, area.size.height + 30)]; + [accessory_ addSubview:field_]; - [transition_ transition:0 toView:list_]; + [navbar_ setAccessoryView:accessory_]; + [self pushView:table_ withTitle:nil backButtonTitle:@"Search" rightButton:nil]; - UITableColumn *column = [[UITableColumn alloc] - initWithTitle:@"Name" - identifier:@"name" - width:frame.size.width - ]; + /* XXX: for the love of god just fix this */ + [navbar_ removeFromSuperview]; + [reload_ removeFromSuperview]; + [configure_ removeFromSuperview]; + [self addSubview:navbar_]; + [self addSubview:reload_]; + [self addSubview:configure_]; + } return self; +} - UITable *table = [list_ table]; - [table setSeparatorStyle:1]; - [table addTableColumn:column]; - [table setDelegate:self]; +- (void) setPackages:(NSArray *)packages { + [packages_ removeAllObjects]; + [packages_ addObjectsFromArray:packages]; + [packages_ sortUsingSelector:@selector(compareByName:)]; - pkgview_ = [[PackageView alloc] initWithFrame:[transition_ bounds] database:database_]; - } return self; + Package *npackage = nil; + for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) { + Package *package = [packages_ objectAtIndex:offset]; + if ([[package name] isEqualToString:package_]) + npackage = package; + } + + [self searchPackages]; + + if (npackage != nil) + [self resetPackage:npackage]; + else if (package_ != nil) + [self popViews:1]; + + [self setPrompt]; } - (void) setDelegate:(id)delegate { - delegate_ = delegate; + if (view_ != nil) + [view_ setDelegate:delegate]; + [super setDelegate:delegate]; } -- (void) deselect { - [transition_ transition:2 toView:list_]; - UITable *table = [list_ table]; - [table selectRow:-1 byExtendingSelection:NO withFade:YES]; - package_ = nil; +- (void) resetPackage:(Package *)package { + [view_ setPackage:package]; + NSString *right = [package installed] == nil ? @"Install" : @"Uninstall"; + [navbar_ showButtonsWithLeftTitle:nil rightTitle:right]; } -- (void) reloadData { - packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; - - _trace(); - if (sections_ != nil) { - [sections_ release]; - sections_ = nil; - } - - _trace(); - for (pkgCache::PkgIterator iterator = [database_ cache]->PkgBegin(); !iterator.end(); ++iterator) { - Package *package = [Package packageWithIterator:iterator database:database_]; - if (package == nil) - continue; - if (filter_(package)) - [packages_ addObject:package]; - } +- (void) searchPackages { + NSString *text([field_ text]); - _trace(); - [packages_ sortUsingSelector:@selector(compareBySectionAndName:)]; - sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; + NSMutableArray *packages([NSMutableArray arrayWithCapacity:16]); - _trace(); - Section *section = nil; - _trace(); - for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) { - _trace(); + for (size_t offset(0), count([packages_ count]); offset != count; ++offset) { Package *package = [packages_ objectAtIndex:offset]; - _trace(); - NSString *name = [package section]; - - _trace(); - if (section == nil || ![[section name] isEqual:name]) { - section = [[Section alloc] initWithName:name row:offset]; - [sections_ addObject:section]; - } - - _trace(); - [section addPackage:package]; - _trace(); + if ([package matches:text]) + [packages addObject:package]; } - _trace(); - [list_ reloadData]; - _trace(); - if (package_ != nil) - [navbar_ popNavigationItem]; - _trace(); + [table_ setPackages:packages]; + [[table_ table] scrollPointVisibleAtTopLeft:CGPointMake(0, 0) animated:NO]; } @end - -bool IsInstalled(Package *package) { - return [package installed]; -} - -bool IsNotInstalled(Package *package) { - return ![package installed]; -} +/* }}} */ @interface Cydia : UIApplication < - PackagesDelegate, - ProgressViewDelegate + ConfirmationViewDelegate, + ProgressViewDelegate, + SearchViewDelegate > { UIWindow *window_; + UIView *underlay_; + UIView *overlay_; UITransitionView *transition_; UIButtonBar *buttonbar_; - UIAlertSheet *alert_; + + ConfirmationView *confirm_; Database *database_; ProgressView *progress_; @@ -1438,14 +3453,30 @@ bool IsNotInstalled(Package *package) { UIScroller *scroller_; UIWebView *webview_; NSURL *url_; + UIProgressIndicator *indicator_; + + InstallView *install_; + ChangesView *changes_; + ManageView *manage_; + SearchView *search_; - Packages *install_; - Packages *uninstall_; + bool restart_; + unsigned tag_; + + UIKeyboard *keyboard_; } - (void) loadNews; -- (void) reloadData; +- (void) reloadData:(BOOL)reset; +- (void) setPrompt; + +- (void) resolve; - (void) perform; +- (void) upgrade; +- (void) update; + +- (void) cancel; +- (void) confirm; - (void) progressViewIsComplete:(ProgressView *)progress; @@ -1453,45 +3484,162 @@ bool IsNotInstalled(Package *package) { - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button; - (void) buttonBarItemTapped:(id)sender; -- (void) view:(UIView *)sender didSetFrame:(CGRect)frame; -- (void) view:(UIView *)view didDrawInRect:(CGRect)rect duration:(float)duration; +- (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old; +- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame; + +- (void) applicationWillSuspend; - (void) applicationDidFinishLaunching:(id)unused; @end @implementation Cydia - (void) loadNews { - [webview_ loadRequest:[NSURLRequest + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url_ cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0 - ]]; + ]; + + [request addValue:[NSString stringWithCString:Firmware_] forHTTPHeaderField:@"X-Firmware"]; + [request addValue:[NSString stringWithCString:Machine_] forHTTPHeaderField:@"X-Machine"]; + [request addValue:[NSString stringWithCString:SerialNumber_] forHTTPHeaderField:@"X-Serial-Number"]; + + [webview_ loadRequest:request]; } -- (void) reloadData { - _trace(); +- (void) reloadData:(BOOL)reset { [database_ reloadData]; - _trace(); - [install_ reloadData]; - _trace(); - [uninstall_ reloadData]; - _trace(); + + size_t count = 16; + + if (Packages_ == nil) { + Packages_ = [[NSMutableDictionary alloc] initWithCapacity:count]; + [Metadata_ setObject:Packages_ forKey:@"Packages"]; + } + + NSArray *packages = [database_ packages]; + + [install_ setPackages:packages]; + [changes_ setPackages:packages]; + [manage_ setPackages:packages]; + [search_ setPackages:packages]; + + if (size_t count = [changes_ count]) { + NSString *badge([[NSNumber numberWithInt:count] stringValue]); + [buttonbar_ setBadgeValue:badge forButton:3]; + if ([buttonbar_ respondsToSelector:@selector(setBadgeAnimated:forButton:)]) + [buttonbar_ setBadgeAnimated:YES forButton:3]; + [self setApplicationBadge:badge]; + } else { + [buttonbar_ setBadgeValue:nil forButton:3]; + if ([buttonbar_ respondsToSelector:@selector(setBadgeAnimated:forButton:)]) + [buttonbar_ setBadgeAnimated:NO forButton:3]; + [self removeApplicationBadge]; + } + + _assert([Metadata_ writeToFile:@"/var/lib/cydia/metadata.plist" atomically:YES] == YES); +} + +- (void) setPrompt { + [navbar_ setPrompt:[NSString stringWithFormat:@"Last Updated: %@", GetLastUpdate()]]; +} + +- (void) resolve { + pkgProblemResolver *resolver = [database_ resolver]; + + resolver->InstallProtect(); + if (!resolver->Resolve(true)) + _error->Discard(); } - (void) perform { - _trace(); + [database_ prepare]; + + if ([database_ cache]->BrokenCount() == 0) + confirm_ = [[ConfirmationView alloc] initWithView:underlay_ database:database_ delegate:self]; + else { + NSMutableArray *broken = [NSMutableArray arrayWithCapacity:16]; + NSArray *packages = [database_ packages]; + + for (size_t i(0); i != [packages count]; ++i) { + Package *package = [packages objectAtIndex:i]; + if ([package broken]) + [broken addObject:[package name]]; + } + + UIAlertSheet *sheet = [[[UIAlertSheet alloc] + initWithTitle:[NSString stringWithFormat:@"%d Broken Packages", [database_ cache]->BrokenCount()] + buttons:[NSArray arrayWithObjects:@"Okay", nil] + defaultButtonIndex:0 + delegate:self + context:self + ] autorelease]; + + [sheet setBodyText:[NSString stringWithFormat:@"The following packages have unmet dependencies:\n\n%@", [broken componentsJoinedByString:@"\n"]]]; + [sheet popupAlertAnimated:YES]; + + [self reloadData:NO]; + } +} + +- (void) upgrade { + [database_ upgrade]; + [self perform]; +} + +- (void) cancel { + [self reloadData:NO]; + [confirm_ release]; + confirm_ = nil; +} + +- (void) confirm { + [overlay_ removeFromSuperview]; + restart_ = true; + [progress_ detachNewThreadSelector:@selector(perform) toTarget:database_ withObject:nil + title:@"Running..." + ]; +} + +- (void) bootstrap_ { + [database_ update]; + [database_ upgrade]; + [database_ prepare]; + [database_ perform]; +} + +- (void) bootstrap { + [progress_ + detachNewThreadSelector:@selector(bootstrap_) + toTarget:self + withObject:nil + title:@"Bootstrap Install..." + ]; +} + +- (void) update { + [progress_ + detachNewThreadSelector:@selector(update) + toTarget:database_ + withObject:nil + title:@"Refreshing Sources..." ]; } - (void) progressViewIsComplete:(ProgressView *)progress { - _trace(); - [self reloadData]; - _trace(); + [self reloadData:YES]; + + if (confirm_ != nil) { + [underlay_ addSubview:overlay_]; + [confirm_ removeFromSuperview]; + [confirm_ release]; + confirm_ = nil; + } } - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { @@ -1501,18 +3649,16 @@ bool IsNotInstalled(Package *package) { break; case 1: - _assert(alert_ == nil); - - alert_ = [[UIAlertSheet alloc] + UIAlertSheet *sheet = [[[UIAlertSheet alloc] initWithTitle:@"About Cydia Packager" buttons:[NSArray arrayWithObjects:@"Close", nil] defaultButtonIndex:0 delegate:self context:self - ]; + ] autorelease]; - [alert_ setBodyText: - @"Copyright (C) 2007\n" + [sheet setBodyText: + @"Copyright (C) 2008\n" "Jay Freeman (saurik)\n" "saurik@saurik.com\n" "http://www.saurik.com/\n" @@ -1526,50 +3672,75 @@ bool IsNotInstalled(Package *package) { "http://www.ccs.ucsb.edu/\n" "\n" "Special Thanks:\n" - "bad_, BHSPitMonkey, Cobra, core,\n" - "Corona, cromas, Darken, dtzWill,\n" - "francis, Godores, jerry, Kingstone,\n" - "lounger, rockabilly, tman, Wbiggs" + "bad_, BHSPitMonkey, cash, Cobra,\n" + "core, Corona, crashx, cromas,\n" + "Darken, dtzWill, Erica, francis,\n" + "Godores, jerry, Kingstone, lounger,\n" + "mbranes, rockabilly, tman, Wbiggs" ]; - [alert_ presentSheetFromButtonBar:buttonbar_]; + [sheet presentSheetFromButtonBar:buttonbar_]; break; } } - (void) alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button { - [alert_ dismiss]; - [alert_ release]; - alert_ = nil; + [sheet dismiss]; } - (void) buttonBarItemTapped:(id)sender { UIView *view; + unsigned tag = [sender tag]; - switch ([sender tag]) { + switch (tag) { case 1: view = featured_; break; case 2: view = install_; break; - case 4: view = uninstall_; break; + case 3: view = changes_; break; + case 4: view = manage_; break; + case 5: view = search_; break; default: _assert(false); } + if ([view respondsToSelector:@selector(resetView:)]) + [(id) view resetView:(tag == tag_ ? NO : YES)]; + tag_ = tag; [transition_ transition:0 toView:view]; } -- (void) view:(UIView *)view didSetFrame:(CGRect)frame { +- (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old { [scroller_ setContentSize:frame.size]; + [indicator_ stopAnimation]; +} + +- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { + [navbar_ setPrompt:title]; +} + +- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame { + [navbar_ setPrompt:@"Loading..."]; + [indicator_ startAnimation]; } -- (void) view:(UIView *)view didDrawInRect:(CGRect)rect duration:(float)duration { - [scroller_ setContentSize:[webview_ bounds].size]; +- (void) applicationWillSuspend { + if (restart_) + if (FW_LEAST(1,1,3)) + notify_post("com.apple.language.changed"); + else + system("launchctl stop com.apple.SpringBoard"); + + [super applicationWillSuspend]; } - (void) applicationDidFinishLaunching:(id)unused { _assert(pkgInitConfig(*_config)); _assert(pkgInitSystem(*_config, _system)); + confirm_ = nil; + restart_ = false; + tag_ = 1; + CGRect screenrect = [UIHardware fullScreenApplicationContentRect]; window_ = [[UIWindow alloc] initWithContentRect:screenrect]; @@ -1580,18 +3751,23 @@ bool IsNotInstalled(Package *package) { progress_ = [[ProgressView alloc] initWithFrame:[window_ bounds] delegate:self]; [window_ setContentView:progress_]; - UIView *view = [[UIView alloc] initWithFrame:[progress_ bounds]]; - [progress_ setContentView:view]; + underlay_ = [[UIView alloc] initWithFrame:[progress_ bounds]]; + [progress_ setContentView:underlay_]; + + overlay_ = [[UIView alloc] initWithFrame:[underlay_ bounds]]; + + if (!bootstrap_) + [underlay_ addSubview:overlay_]; transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake( 0, 0, screenrect.size.width, screenrect.size.height - 48 )]; - [view addSubview:transition_]; + [overlay_ addSubview:transition_]; featured_ = [[UIView alloc] initWithFrame:[transition_ bounds]]; - CGSize navsize = [UINavigationBar defaultSize]; + CGSize navsize = [UINavigationBar defaultSizeWithPrompt]; CGRect navrect = {{0, 0}, navsize}; navbar_ = [[UINavigationBar alloc] initWithFrame:navrect]; @@ -1602,14 +3778,14 @@ bool IsNotInstalled(Package *package) { [navbar_ showButtonsWithLeftTitle:@"About" rightTitle:@"Reload"]; - UINavigationItem *navitem = [[UINavigationItem alloc] initWithTitle:@"Featured"]; + UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:@"Featured"] autorelease]; [navbar_ pushNavigationItem:navitem]; struct CGRect subbounds = [featured_ bounds]; subbounds.origin.y += navsize.height; subbounds.size.height -= navsize.height; - UIImageView *pinstripe = [[UIImageView alloc] initWithFrame:subbounds]; + UIImageView *pinstripe = [[[UIImageView alloc] initWithFrame:subbounds] autorelease]; [pinstripe setImage:[UIImage applicationImageNamed:@"pinstripe.png"]]; [featured_ addSubview:pinstripe]; @@ -1623,7 +3799,10 @@ bool IsNotInstalled(Package *package) { [scroller_ setScrollDecelerationFactor:0.99]; [scroller_ setDelegate:self]; - webview_ = [[UIWebView alloc] initWithFrame:[scroller_ bounds]]; + CGRect webrect = [scroller_ bounds]; + webrect.size.height = 0; + + webview_ = [[UIWebView alloc] initWithFrame:webrect]; [scroller_ addSubview:webview_]; [webview_ setTilingEnabled:YES]; @@ -1631,8 +3810,10 @@ bool IsNotInstalled(Package *package) { [webview_ setAutoresizes:YES]; [webview_ setDelegate:self]; - url_ = [NSURL URLWithString:@"http://cydia.saurik.com/"]; - [self loadNews]; + CGSize indsize = [UIProgressIndicator defaultSizeForStyle:2]; + indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(87, 45, indsize.width, indsize.height)]; + [indicator_ setStyle:2]; + [featured_ addSubview:indicator_]; NSArray *buttonitems = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys: @@ -1657,18 +3838,18 @@ bool IsNotInstalled(Package *package) { [NSDictionary dictionaryWithObjectsAndKeys: @"buttonBarItemTapped:", kUIButtonBarButtonAction, - @"upgrade-up.png", kUIButtonBarButtonInfo, - @"upgrade-dn.png", kUIButtonBarButtonSelectedInfo, + @"changes-up.png", kUIButtonBarButtonInfo, + @"changes-dn.png", kUIButtonBarButtonSelectedInfo, [NSNumber numberWithInt:3], kUIButtonBarButtonTag, self, kUIButtonBarButtonTarget, - @"Upgrade", kUIButtonBarButtonTitle, + @"Changes", kUIButtonBarButtonTitle, @"0", kUIButtonBarButtonType, nil], [NSDictionary dictionaryWithObjectsAndKeys: @"buttonBarItemTapped:", kUIButtonBarButtonAction, - @"uninstall-up.png", kUIButtonBarButtonInfo, - @"uninstall-dn.png", kUIButtonBarButtonSelectedInfo, + @"manage-up.png", kUIButtonBarButtonInfo, + @"manage-dn.png", kUIButtonBarButtonSelectedInfo, [NSNumber numberWithInt:4], kUIButtonBarButtonTag, self, kUIButtonBarButtonTarget, @"Uninstall", kUIButtonBarButtonTitle, @@ -1677,17 +3858,17 @@ bool IsNotInstalled(Package *package) { [NSDictionary dictionaryWithObjectsAndKeys: @"buttonBarItemTapped:", kUIButtonBarButtonAction, - @"sources-up.png", kUIButtonBarButtonInfo, - @"sources-dn.png", kUIButtonBarButtonSelectedInfo, + @"search-up.png", kUIButtonBarButtonInfo, + @"search-dn.png", kUIButtonBarButtonSelectedInfo, [NSNumber numberWithInt:5], kUIButtonBarButtonTag, self, kUIButtonBarButtonTarget, - @"Sources", kUIButtonBarButtonTitle, + @"Search", kUIButtonBarButtonTitle, @"0", kUIButtonBarButtonType, nil], nil]; buttonbar_ = [[UIButtonBar alloc] - initInView:view + initInView:overlay_ withFrame:CGRectMake( 0, screenrect.size.height - 48, screenrect.size.width, 48 @@ -1711,47 +3892,171 @@ bool IsNotInstalled(Package *package) { [buttonbar_ showSelectionForButton:1]; [transition_ transition:0 toView:featured_]; - [view addSubview:buttonbar_]; + [overlay_ addSubview:buttonbar_]; + + [UIKeyboard initImplementationNow]; + + CGRect edtrect = [overlay_ bounds]; + edtrect.origin.y += navsize.height; + edtrect.size.height -= navsize.height; + + CGSize keysize = [UIKeyboard defaultSize]; + CGRect keyrect = {{0, [overlay_ bounds].size.height - keysize.height}, keysize}; + keyboard_ = [[UIKeyboard alloc] initWithFrame:keyrect]; database_ = [[Database alloc] init]; [database_ setDelegate:progress_]; - install_ = [[Packages alloc] initWithFrame:[transition_ bounds] title:@"Install" database:database_ filter:&IsNotInstalled selector:@selector(install)]; + install_ = [[InstallView alloc] initWithFrame:[transition_ bounds]]; [install_ setDelegate:self]; - uninstall_ = [[Packages alloc] initWithFrame:[transition_ bounds] title:@"Uninstall" database:database_ filter:&IsInstalled selector:@selector(remove)]; - [uninstall_ setDelegate:self]; + changes_ = [[ChangesView alloc] initWithFrame:[transition_ bounds]]; + [changes_ setDelegate:self]; -#if 0 + manage_ = [[ManageView alloc] initWithFrame:[transition_ bounds]]; + [manage_ setDelegate:self]; - UIAlertSheet *alert = [[UIAlertSheet alloc] - initWithTitle:@"Alert Title" - buttons:[NSArray arrayWithObjects:@"Yes", nil] - defaultButtonIndex:0 - delegate:self - context:self - ]; + search_ = [[SearchView alloc] initWithFrame:[transition_ bounds]]; + [search_ setDelegate:self]; + + [self reloadData:NO]; - NSLog(@"%p\n", [alert table]); - [[alert table] setDelegate:self]; - [[alert table] reloadData]; + Package *package([database_ packageWithName:@"cydia"]); + NSString *application = package == nil ? @"Cydia" : [NSString + stringWithFormat:@"Cydia/%@", + [package installed] + ]; - [alert addTextFieldWithValue:@"Title" label:@"Label"]; - [alert setShowsOverSpringBoardAlerts:YES]; - [alert setBodyText:@"This is an alert."]; - [alert presentSheetFromButtonBar:buttonbar_]; - //[alert popupAlertAnimated:YES]; + WebView *webview = [webview_ webView]; + [webview setApplicationNameForUserAgent:application]; + [webview setFrameLoadDelegate:self]; -#endif + url_ = [NSURL URLWithString:@"http://cydia.saurik.com/"]; + [self loadNews]; - [self reloadData]; [progress_ resetView]; + + if (bootstrap_) + [self bootstrap]; +} + +- (void) showKeyboard:(BOOL)show { + if (show) + [overlay_ addSubview:keyboard_]; + else + [keyboard_ removeFromSuperview]; } @end +void AddPreferences(NSString *plist) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + NSMutableDictionary *settings = [[[NSMutableDictionary alloc] initWithContentsOfFile:plist] autorelease]; + _assert(settings != NULL); + NSMutableArray *items = [settings objectForKey:@"items"]; + + bool cydia(false); + + for (size_t i(0); i != [items count]; ++i) { + NSMutableDictionary *item([items objectAtIndex:i]); + NSString *label = [item objectForKey:@"label"]; + if (label != nil && [label isEqualToString:@"Cydia"]) { + cydia = true; + break; + } + } + + if (!cydia) { + for (size_t i(0); i != [items count]; ++i) { + NSDictionary *item([items objectAtIndex:i]); + NSString *label = [item objectForKey:@"label"]; + if (label != nil && [label isEqualToString:@"General"]) { + [items insertObject:[NSDictionary dictionaryWithObjectsAndKeys: + @"CydiaSettings", @"bundle", + @"PSLinkCell", @"cell", + [NSNumber numberWithBool:YES], @"hasIcon", + [NSNumber numberWithBool:YES], @"isController", + @"Cydia", @"label", + nil] atIndex:(i + 1)]; + + break; + } + } + + _assert([settings writeToFile:plist atomically:YES] == YES); + } + + [pool release]; +} + +/*IMP alloc_; +id Alloc_(id self, SEL selector) { + id object = alloc_(self, selector); + fprintf(stderr, "[%s]A-%p\n", self->isa->name, object); + return object; +}*/ + int main(int argc, char *argv[]) { + struct nlist nl[2]; + memset(nl, 0, sizeof(nl)); + nl[0].n_un.n_name = "_useMDNSResponder"; + nlist("/usr/lib/libc.dylib", nl); + if (nl[0].n_type != N_UNDF) + *(int *) nl[0].n_value = 0; + + bootstrap_ = argc > 1 && strcmp(argv[1], "--bootstrap") == 0; + + setuid(0); + setgid(0); + + /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); + alloc_ = alloc->method_imp; + alloc->method_imp = (IMP) &Alloc_;*/ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + if (NSDictionary *sysver = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]) { + if (NSString *prover = [sysver valueForKey:@"ProductVersion"]) { + Firmware_ = strdup([prover cString]); + NSArray *versions = [prover componentsSeparatedByString:@"."]; + int count = [versions count]; + Major_ = count > 0 ? [[versions objectAtIndex:0] intValue] : 0; + Minor_ = count > 1 ? [[versions objectAtIndex:1] intValue] : 0; + BugFix_ = count > 2 ? [[versions objectAtIndex:2] intValue] : 0; + } + } + + size_t size; + sysctlbyname("hw.machine", NULL, &size, NULL, 0); + char *machine = new char[size]; + sysctlbyname("hw.machine", machine, &size, NULL, 0); + Machine_ = machine; + + if (CFMutableDictionaryRef dict = IOServiceMatching("IOPlatformExpertDevice")) + if (io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, dict)) { + if (CFTypeRef serial = IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0)) { + SerialNumber_ = strdup(CFStringGetCStringPtr((CFStringRef) serial, CFStringGetSystemEncoding())); + CFRelease(serial); + } + + IOObjectRelease(service); + } + + /*AddPreferences(@"/Applications/Preferences.app/Settings-iPhone.plist"); + AddPreferences(@"/Applications/Preferences.app/Settings-iPod.plist");*/ + + if ((Metadata_ = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"]) == NULL) + Metadata_ = [[NSMutableDictionary alloc] initWithCapacity:2]; + else + Packages_ = [Metadata_ objectForKey:@"Packages"]; + + setenv("CYDIA", "", _not(int)); + if (access("/User", F_OK) != 0) + system("/usr/libexec/cydia/firmware.sh"); + system("dpkg --configure -a"); + UIApplicationMain(argc, argv, [Cydia class]); [pool release]; + return 0; }