}
@interface NSObject (Cydia)
-- (void) yieldToSelector:(SEL)selector withObject:(id)object;
+- (id) yieldToSelector:(SEL)selector withObject:(id)object;
+- (id) yieldToSelector:(SEL)selector;
@end
@implementation NSObject (Cydia)
- (void) doNothing {
}
-- (void) _yieldToContext:(NSArray *)context { _pooled
+- (void) _yieldToContext:(NSMutableArray *)context { _pooled
SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
id object([[context objectAtIndex:1] nonretainedObjectValue]);
volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
- [self performSelector:selector withObject:object];
+ /* XXX: deal with exceptions */
+ id value([self performSelector:selector withObject:object]);
+
+ [context removeAllObjects];
+ if (value != nil)
+ [context addObject:value];
stopped = true;
];
}
-- (void) yieldToSelector:(SEL)selector withObject:(id)object {
- [self performSelector:selector withObject:object];
- return;
+- (id) yieldToSelector:(SEL)selector withObject:(id)object {
+ /*return [self performSelector:selector withObject:object];*/
volatile bool stopped(false);
- NSArray *context([NSArray arrayWithObjects:
+ NSMutableArray *context([NSMutableArray arrayWithObjects:
[NSValue valueWithPointer:selector],
[NSValue valueWithNonretainedObject:object],
[NSValue valueWithPointer:const_cast<bool *>(&stopped)],
NSDate *future([NSDate distantFuture]);
while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
+
+ return [context count] == 0 ? nil : [context objectAtIndex:0];
+}
+
+- (id) yieldToSelector:(SEL)selector {
+ return [self yieldToSelector:selector withObject:nil];
}
@end
#define lprintf(args...) fprintf(stderr, args)
-#define ForRelease 0
+#define ForRelease 1
#define ForSaurik (1 && !ForRelease)
-#define ShowInternals (0 && !ForRelease)
+#define ShowInternals (1 && !ForRelease)
#define IgnoreInstall (0 && !ForRelease)
#define RecycleWebViews 0
#define AlwaysReload (1 && !ForRelease)
#undef _trace
#define _trace(args...)
#undef _profile
-#define _profile(name)
+#define _profile(name) {
#undef _end
-#define _end
+#define _end }
+#define PrintTimes() do {} while (false)
#endif
/* Radix Sort {{{ */
+ (NSString *) stringWithUTF8BytesNoCopy:(const char *)bytes length:(int)length;
+ (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length;
- (NSComparisonResult) compareByPath:(NSString *)other;
+- (NSString *) stringByCachingURLWithCurrentCDN;
+- (NSString *) stringByAddingPercentEscapesIncludingReserved;
@end
@implementation NSString (Cydia)
return result == NSOrderedSame ? value : result;
}
+- (NSString *) stringByCachingURLWithCurrentCDN {
+ return [self
+ stringByReplacingOccurrencesOfString:@"://"
+ withString:@"://ne.edgecastcdn.net/8003A4/"
+ options:0
+ /* XXX: this is somewhat inaccurate */
+ range:NSMakeRange(0, 10)
+ ];
+}
+
+- (NSString *) stringByAddingPercentEscapesIncludingReserved {
+ return [(id)CFURLCreateStringByAddingPercentEscapes(
+ kCFAllocatorDefault,
+ (CFStringRef) self,
+ NULL,
+ CFSTR(";/?:@&=+$,"),
+ kCFStringEncodingUTF8
+ ) autorelease];
+}
+
@end
/* Perl-Compatible RegEx {{{ */
static const char *Machine_ = NULL;
static const NSString *UniqueID_ = nil;
static const NSString *Build_ = nil;
+static const NSString *Product_ = nil;
+static const NSString *Safari_ = nil;
CFLocaleRef Locale_;
CGColorSpaceRef space_;
/* Database Interface {{{ */
@interface Database : NSObject {
+ unsigned era_;
+
pkgCacheFile cache_;
pkgDepCache::Policy *policy_;
pkgRecords *records_;
}
+ (Database *) sharedInstance;
+- (unsigned) era;
- (void) _readCydia:(NSNumber *)fd;
- (void) _readStatus:(NSNumber *)fd;
/* }}} */
/* Package Class {{{ */
@interface Package : NSObject {
+ unsigned era_;
+
pkgCache::PkgIterator iterator_;
_transient Database *database_;
pkgCache::VerIterator version_;
bool cached_;
NSString *section_;
+ bool essential_;
NSString *latest_;
NSString *installed_;
- (void) dealloc {
if (source_ != nil)
[source_ release];
-
if (section_ != nil)
[section_ release];
}
- (Package *) initWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database {
- if ((self = [super init]) != nil) { _profile(Package$initWithIterator)
+ if ((self = [super init]) != nil) {
+ _profile(Package$initWithIterator)
+ @synchronized (database) {
+ era_ = [database era];
+
iterator_ = iterator;
database_ = database;
Changed_ = true;
}
_end
- _end } return self;
+
+ const char *section(iterator_.Section());
+ if (section == NULL)
+ section_ = nil;
+ else {
+ NSString *name([[NSString stringWithUTF8String:section] stringByReplacingCharacter:' ' withCharacter:'_']);
+
+ lookup:
+ if (NSDictionary *value = [SectionMap_ objectForKey:name])
+ if (NSString *rename = [value objectForKey:@"Rename"]) {
+ name = rename;
+ goto lookup;
+ }
+
+ section_ = [[name stringByReplacingCharacter:'_' withCharacter:' '] retain];
+ }
+
+ essential_ = (iterator_->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES;
+ } _end } return self;
}
+ (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator database:(Database *)database {
}
- (NSString *) section {
- if (section_ != nil)
- return section_;
-
- const char *section = iterator_.Section();
- if (section == NULL)
- return nil;
-
- NSString *name = [[NSString stringWithUTF8String:section] stringByReplacingCharacter:' ' withCharacter:'_'];
-
- lookup:
- if (NSDictionary *value = [SectionMap_ objectForKey:name])
- if (NSString *rename = [value objectForKey:@"Rename"]) {
- name = rename;
- goto lookup;
- }
-
- section_ = [[name stringByReplacingCharacter:'_' withCharacter:' '] retain];
return section_;
}
unichar character([name characterAtIndex:0]);
if (!isalpha(character))
return '#';
- return character;
+ return toupper(character);
_end
}
}
- (BOOL) essential {
- return (iterator_->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES;
+ return essential_;
}
- (BOOL) broken {
- (Source *) source {
if (!cached_) {
- source_ = file_.end() ? nil : [[database_ getSource:file_.File()] retain];
- cached_ = true;
+ @synchronized (database_) {
+ if ([database_ era] != era_ || file_.end())
+ source_ = nil;
+ else {
+ source_ = [database_ getSource:file_.File()];
+ if (source_ != nil)
+ [source_ retain];
+ }
+
+ cached_ = true;
+ }
}
return source_;
- (Section *) initWithIndex:(unichar)index row:(size_t)row {
if ((self = [super init]) != nil) {
- name_ = [[NSString stringWithCharacters:&index length:1] retain];
+ name_ = [(index == '#' ? @"123" : [NSString stringWithCharacters:&index length:1]) retain];
index_ = index;
row_ = row;
} return self;
return instance;
}
+- (unsigned) era {
+ return era_;
+}
+
- (void) dealloc {
_assert(false);
[super dealloc];
}
- (void) reloadData { _pooled
+ @synchronized (self) {
+ ++era_;
+ }
+
_error->Discard();
delete list_;
return @"Cancel";
}
+- (id) rightButtonTitle {
+ return issues_ != nil ? nil : [super rightButtonTitle];
+}
+
- (id) _rightButtonTitle {
#if AlwaysReload || IgnoreInstall
- return @"Reload";
+ return [super _rightButtonTitle];
#else
- return issues_ == nil ? @"Confirm" : nil;
+ return @"Confirm";
#endif
}
[super webView:sender didClearWindowObject:window forFrame:frame];
}
+- (bool) _allowJavaScriptPanel {
+ return false;
+}
+
#if !AlwaysReload
- (void) _rightButtonClicked {
/*[super _rightButtonClicked];
[delegate_ syncData];
}
+- (void) complete {
+ [Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ @"deb", @"Type",
+ href_, @"URI",
+ @"./", @"Distribution",
+ nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href_]];
+
+ [delegate_ syncData];
+}
+
+- (NSString *) getWarning {
+ NSString *href(href_);
+ NSRange colon([href rangeOfString:@"://"]);
+ if (colon.location != NSNotFound)
+ href = [href substringFromIndex:(colon.location + 3)];
+ href = [href stringByAddingPercentEscapes];
+ href = [@"http://cydia.saurik.com/api/repotag/" stringByAppendingString:href];
+ href = [href stringByCachingURLWithCurrentCDN];
+
+ NSURL *url([NSURL URLWithString:href]);
+
+ NSStringEncoding encoding;
+ NSError *error(nil);
+
+ if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error])
+ return [warning length] == 0 ? nil : warning;
+ return nil;
+}
+
- (void) _endConnection:(NSURLConnection *)connection {
NSURLConnection **field = NULL;
if (connection == trivial_bz2_)
trivial_bz2_ == nil &&
trivial_gz_ == nil
) {
- [delegate_ setStatusBarShowsProgress:NO];
- [delegate_ removeProgressHUD:hud_];
-
- [hud_ autorelease];
- hud_ = nil;
+ bool defer(false);
if (trivial_) {
- [Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys:
- @"deb", @"Type",
- href_, @"URI",
- @"./", @"Distribution",
- nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href_]];
-
- [delegate_ syncData];
+ if (NSString *warning = [self yieldToSelector:@selector(getWarning)]) {
+ defer = true;
+
+ UIActionSheet *sheet = [[[UIActionSheet alloc]
+ initWithTitle:@"Source Warning"
+ buttons:[NSArray arrayWithObjects:@"Add Anyway", @"Cancel", nil]
+ defaultButtonIndex:0
+ delegate:self
+ context:@"warning"
+ ] autorelease];
+
+ [sheet setNumberOfRows:1];
+
+ [sheet setBodyText:warning];
+ [sheet popupAlertAnimated:YES];
+ } else
+ [self complete];
} else if (error_ != nil) {
UIActionSheet *sheet = [[[UIActionSheet alloc]
initWithTitle:@"Verification Error"
[sheet popupAlertAnimated:YES];
}
- [href_ release];
- href_ = nil;
+ [delegate_ setStatusBarShowsProgress:NO];
+ [delegate_ removeProgressHUD:hud_];
+
+ [hud_ autorelease];
+ hud_ = nil;
+
+ if (!defer) {
+ [href_ release];
+ href_ = nil;
+ }
if (error_ != nil) {
[error_ release];
[sheet dismiss];
else if ([context isEqualToString:@"urlerror"])
[sheet dismiss];
+ else if ([context isEqualToString:@"warning"]) {
+ switch (button) {
+ case 1:
+ [self complete];
+ break;
+
+ case 2:
+ break;
+
+ default:
+ _assert(false);
+ }
+
+ [href_ release];
+ href_ = nil;
+
+ [sheet dismiss];
+ }
}
- (id) initWithBook:(RVBook *)book database:(Database *)database {
] autorelease];
[sheet setBodyText:
- @"Copyright (C) 2008\n"
+ @"Copyright (C) 2008-2009\n"
"Jay Freeman (saurik)\n"
"saurik@saurik.com\n"
"http://www.saurik.com/\n"
- (void) _reloadData {
UIView *block();
+ static bool loaded(false);
UIProgressHUD *hud([self addProgressHUD]);
- [hud setText:@"Reloading Data"];
+ [hud setText:(loaded ? @"Reloading Data" : @"Loading Data")];
+ loaded = true;
[database_ yieldToSelector:@selector(reloadData) withObject:nil];
_trace();
// XXX: what is this line of code for?
if ([packages count] == 0);
- else if (Loaded_) loaded:
+ else if (Loaded_ || ForSaurik) loaded:
[self _loaded];
else {
Loaded_ = YES;
setuid(0);
setgid(0);
-#if 0 /* XXX: this costs 1.4s of startup performance */
+#if 1 /* XXX: this costs 1.4s of startup performance */
if (unlink("/var/cache/apt/pkgcache.bin") == -1)
_assert(errno == ENOENT);
if (unlink("/var/cache/apt/srcpkgcache.bin") == -1)
if (NSDictionary *system = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"])
Build_ = [system objectForKey:@"ProductBuildVersion"];
+ if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) {
+ Product_ = [info objectForKey:@"SafariProductVersion"];
+ Safari_ = [info objectForKey:@"CFBundleVersion"];
+ }
/*AddPreferences(@"/Applications/Preferences.app/Settings-iPhone.plist");
AddPreferences(@"/Applications/Preferences.app/Settings-iPod.plist");*/