]> git.saurik.com Git - cydia.git/blobdiff - MobileCydia.mm
Add initWithRequest: to CyteWebViewController.
[cydia.git] / MobileCydia.mm
index 2c2734dbf0acb8097407db244653bf4de9ab4867..d8f39d7b3d855d9ff82aaa0e3d98aa2267da5796 100644 (file)
@@ -685,14 +685,15 @@ static _H<UIFont> Font18Bold_;
 static _H<UIFont> Font22Bold_;
 
 static const char *Machine_ = NULL;
-static NSString *System_ = nil;
+static _H<NSString> System_;
 static NSString *SerialNumber_ = nil;
 static NSString *ChipID_ = nil;
 static NSString *BBSNum_ = nil;
 static _H<NSString> Token_;
 static NSString *UniqueID_ = nil;
-static NSString *Product_ = nil;
-static NSString *Safari_ = nil;
+static _H<NSString> UserAgent_;
+static _H<NSString> Product_;
+static _H<NSString> Safari_;
 
 static CFLocaleRef Locale_;
 static NSArray *Languages_;
@@ -707,14 +708,13 @@ static _transient NSMutableDictionary *Values_;
 static _transient NSMutableDictionary *Sections_;
 _H<NSMutableDictionary> Sources_;
 static _transient NSNumber *Version_;
-_H<NSString> CydiaSource_;
 bool Changed_;
 static time_t now_;
 
 bool IsWildcat_;
 static CGFloat ScreenScale_;
 static NSString *Idiom_;
-_H<NSString> Firmware_;
+static _H<NSString> Firmware_;
 static NSString *Major_;
 
 static _H<NSMutableDictionary> SessionData_;
@@ -3954,7 +3954,8 @@ static _H<NSMutableSet> Diversions_;
 + (NSArray *) _attributeKeys {
     return [NSArray arrayWithObjects:
         @"bbsnum",
-        @"cydiaSource",
+        @"build",
+        @"coreFoundationVersionNumber",
         @"device",
         @"ecid",
         @"firmware",
@@ -3983,6 +3984,14 @@ static _H<NSMutableSet> Diversions_;
     return Cydia_;
 }
 
+- (NSString *) build {
+    return System_;
+}
+
+- (NSString *) coreFoundationVersionNumber {
+    return [NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber];
+}
+
 - (NSString *) device {
     return [[UIDevice currentDevice] uniqueIdentifier];
 }
@@ -4097,8 +4106,6 @@ static _H<NSMutableSet> Diversions_;
         return @"removeButton";
     else if (selector == @selector(saveConfig))
         return @"saveConfig";
-    else if (selector == @selector(setCydiaSource:))
-        return @"setCydiaSource";
     else if (selector == @selector(setMetadataValue::))
         return @"setMetadataValue";
     else if (selector == @selector(setSessionValue::))
@@ -4213,25 +4220,6 @@ static _H<NSMutableSet> Diversions_;
     return value;
 }
 
-- (void) _setCydiaSource:(NSString *)source {
-    @synchronized (HostConfig_) {
-        CydiaSource_ = source;
-        [Metadata_ setObject:source forKey:@"CydiaSource"];
-    }
-
-    Changed_ = true;
-}
-
-- (void) setCydiaSource:(NSString *)source {
-    [self performSelectorOnMainThread:@selector(_setCydiaSource:) withObject:source waitUntilDone:NO];
-}
-
-- (NSString *) cydiaSource {
-    @synchronized (HostConfig_) {
-        return (id) CydiaSource_ ?: [NSNull null];
-    }
-}
-
 - (NSArray *) getMetadataKeys {
 @synchronized (Values_) {
     return [Values_ allKeys];
@@ -4586,6 +4574,8 @@ static _H<NSMutableSet> Diversions_;
 
     NSMutableURLRequest *copy([[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source] mutableCopy]);
 
+    if ([copy valueForHTTPHeaderField:@"X-Cydia-Cf-Version"] == nil)
+        [copy setValue:[NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber] forHTTPHeaderField:@"X-Cydia-Cf-Version"];
     if (Machine_ != NULL && [copy valueForHTTPHeaderField:@"X-Machine"] == nil)
         [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
 
@@ -4616,16 +4606,7 @@ static _H<NSMutableSet> Diversions_;
 }
 
 - (NSString *) applicationNameForUserAgent {
-    NSString *application([NSString stringWithFormat:@"Cydia/%@", Cydia_]);
-
-    if (Safari_ != nil)
-        application = [NSString stringWithFormat:@"Safari/%@ %@", Safari_, application];
-    if (System_ != nil)
-        application = [NSString stringWithFormat:@"Mobile/%@ %@", System_, application];
-    if (Product_ != nil)
-        application = [NSString stringWithFormat:@"Version/%@ %@", Product_, application];
-
-    return application;
+    return UserAgent_;
 }
 
 - (id) init {
@@ -4644,6 +4625,11 @@ static _H<NSMutableSet> Diversions_;
 @implementation AppCacheController
 
 - (void) didReceiveMemoryWarning {
+    // XXX: this doesn't work
+}
+
+- (bool) retainsNetworkActivityIndicator {
+    return false;
 }
 
 @end
@@ -5790,14 +5776,12 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (void) loadView {
-    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
-
-    list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds]] autorelease];
+    list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
     [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
     [list_ setRowHeight:24.0f];
     [(UITableView *) list_ setDataSource:self];
     [list_ setDelegate:self];
-    [[self view] addSubview:list_];
+    [self setView:list_];
 }
 
 - (void) viewDidLoad {
@@ -6241,11 +6225,13 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (void) loadView {
-    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
+    UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]);
+    [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
+    [self setView:view];
 
     list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease];
     [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
-    [[self view] addSubview:list_];
+    [view addSubview:list_];
 
     // XXX: is 20 the most optimal number here?
     [list_ setSectionIndexMinimumDisplayRowCount:20];
@@ -6295,6 +6281,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
     NSArray *packages;
 
+  reload:
     if ([self shouldYield]) {
         do {
             UIProgressHUD *hud;
@@ -6312,12 +6299,15 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
             if (hud != nil)
                 [delegate_ removeProgressHUD:hud];
         } while (reloading_ == 2);
-
-        reloading_ = 0;
     } else {
         packages = [self _reloadPackages];
     }
 
+@synchronized (database_) {
+    if (era_ != [database_ era])
+        goto reload;
+    reloading_ = 0;
+
     packages_ = packages;
 
     indices_ = [NSMutableDictionary dictionaryWithCapacity:32];
@@ -6403,7 +6393,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
         [(UITableView *) list_ setDataSource:self];
         [list_ reloadData];
     _end
-}
+} }
 
 - (void) reloadData {
     [super reloadData];
@@ -6529,9 +6519,6 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     return [NSURL URLWithString:@"cydia://home"];
 }
 
-- (void) didReceiveMemoryWarning {
-}
-
 - (void) aboutButtonClicked {
     UIAlertView *alert([[[UIAlertView alloc] init] autorelease]);
 
@@ -6765,7 +6752,32 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 
 @implementation CYTabBarController
 
+- (void) didReceiveMemoryWarning {
+    [super didReceiveMemoryWarning];
+
+    // presenting a UINavigationController on 2.x does not update its transitionView
+    // it thereby will not allow its topViewController to be unloaded by memory pressure
+    if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) {
+        UIViewController *selected([self selectedViewController]);
+        for (UINavigationController *controller in [self viewControllers])
+            if (controller != selected)
+                if (UIViewController *top = [controller topViewController])
+                    [top unloadView];
+    }
+}
+
 - (void) setUnselectedViewController:(UIViewController *)transient {
+    if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) {
+        if (transient != nil) {
+            [[[self viewControllers] objectAtIndex:0] pushViewController:transient animated:YES];
+            [self setSelectedIndex:0];
+        } return;
+    }
+
+    UINavigationController *navigation([[[UINavigationController alloc] init] autorelease]);
+    [navigation setViewControllers:[NSArray arrayWithObject:transient]];
+    transient = navigation;
+
     NSMutableArray *controllers = [[self viewControllers] mutableCopy];
     if (transient != nil) {
         if (transient_ == nil)
@@ -6793,6 +6805,14 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 - (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
     if ([self unselectedViewController])
         [self setUnselectedViewController:nil];
+
+    // presenting a UINavigationController on 2.x does not update its transitionView
+    // if this view was unloaded, the tranitionView may currently be presenting nothing
+    if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) {
+        UINavigationController *navigation((UINavigationController *) viewController);
+        [navigation pushViewController:[[[UIViewController alloc] init] autorelease] animated:NO];
+        [navigation popViewControllerAnimated:NO];
+    }
 }
 
 - (NSArray *) navigationURLCollection {
@@ -7305,14 +7325,12 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (void) loadView {
-    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
-
-    list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds]] autorelease];
+    list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
     [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
     [list_ setRowHeight:45.0f];
     [(UITableView *) list_ setDataSource:self];
     [list_ setDelegate:self];
-    [[self view] addSubview:list_];
+    [self setView:list_];
 }
 
 - (void) viewDidLoad {
@@ -7487,14 +7505,12 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (void) loadView {
-    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
-
-    list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease];
+    list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain] autorelease];
     [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
     [list_ setRowHeight:73];
     [(UITableView *) list_ setDataSource:self];
     [list_ setDelegate:self];
-    [[self view] addSubview:list_];
+    [self setView:list_];
 }
 
 - (void) viewDidLoad {
@@ -7887,13 +7903,15 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (void) loadView {
-    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
+    UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]);
+    [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
+    [self setView:view];
 
     table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease];
     [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
     [(UITableView *) table_ setDataSource:self];
     [table_ setDelegate:self];
-    [[self view] addSubview:table_];
+    [view addSubview:table_];
 
     subscribedSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease];
     [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
@@ -8483,14 +8501,12 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (void) loadView {
-    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
-
-    list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease];
+    list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain] autorelease];
     [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
     [list_ setRowHeight:53];
     [(UITableView *) list_ setDataSource:self];
     [list_ setDelegate:self];
-    [[self view] addSubview:list_];
+    [self setView:list_];
 }
 
 - (void) viewDidLoad {
@@ -8631,13 +8647,11 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 @implementation SettingsController
 
 - (void) loadView {
-    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
-
-    table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease];
+    table_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped] autorelease];
     [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth];
     [table_ setDelegate:self];
     [(UITableView *) table_ setDataSource:self];
-    [[self view] addSubview:table_];
+    [self setView:table_];
 
     NSArray *items = [NSArray arrayWithObjects:
         UCLocalize("USER"),
@@ -8814,8 +8828,11 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 @implementation StashController
 
 - (void) loadView {
-    [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
-    [[self view] setBackgroundColor:[UIColor viewFlipsideBackgroundColor]];
+    UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]);
+    [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
+    [self setView:view];
+
+    [view setBackgroundColor:[UIColor viewFlipsideBackgroundColor]];
 
     spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
     CGRect spinrect = [spinner_ frame];
@@ -8823,7 +8840,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     spinrect.origin.y = [[self view] frame].size.height - 80.0f;
     [spinner_ setFrame:spinrect];
     [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin];
-    [[self view] addSubview:spinner_];
+    [view addSubview:spinner_];
     [spinner_ startAnimating];
 
     CGRect captrect;
@@ -8839,7 +8856,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     [caption_ setBackgroundColor:[UIColor clearColor]];
     [caption_ setShadowColor:[UIColor blackColor]];
     [caption_ setTextAlignment:UITextAlignmentCenter];
-    [[self view] addSubview:caption_];
+    [view addSubview:caption_];
 
     CGRect statusrect;
     statusrect.size.width = [[self view] frame].size.width;
@@ -8854,7 +8871,7 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
     [status_ setBackgroundColor:[UIColor clearColor]];
     [status_ setShadowColor:[UIColor blackColor]];
     [status_ setTextAlignment:UITextAlignmentCenter];
-    [[self view] addSubview:status_];
+    [view addSubview:status_];
 }
 
 - (void) releaseSubviews {
@@ -9605,11 +9622,8 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 - (BOOL) openCydiaURL:(NSURL *)url forExternal:(BOOL)external {
     CyteViewController *page([self pageForURL:url forExternal:external]);
 
-    if (page != nil) {
-        UINavigationController *nav = [[[UINavigationController alloc] init] autorelease];
-        [nav setViewControllers:[NSArray arrayWithObject:page]];
-        [tabbar_ setUnselectedViewController:nav];
-    }
+    if (page != nil)
+        [tabbar_ setUnselectedViewController:page];
 
     return page != nil;
 }
@@ -9733,16 +9747,25 @@ bool DepSubstrate(const pkgCache::VerIterator &iterator) {
 }
 
 - (void) _sendMemoryWarningNotification {
-    [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidReceiveMemoryWarningNotification" object:[UIApplication sharedApplication]];
+    if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: maybe 4_0?
+        [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification" object:[UIApplication sharedApplication]];
+    else
+        [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidReceiveMemoryWarningNotification" object:[UIApplication sharedApplication]];
 }
 
 - (void) _sendMemoryWarningNotifications {
     while (true) {
         [self performSelectorOnMainThread:@selector(_sendMemoryWarningNotification) withObject:nil waitUntilDone:NO];
-        usleep(250000);
+        sleep(2);
+        //usleep(2000000);
     }
 }
 
+- (void) applicationDidReceiveMemoryWarning:(UIApplication *)application {
+    NSLog(@"--");
+    [[NSURLCache sharedURLCache] removeAllCachedResponses];
+}
+
 - (void) applicationDidFinishLaunching:(id)unused {
     //[NSThread detachNewThreadSelector:@selector(_sendMemoryWarningNotifications) toTarget:self withObject:nil];
 
@@ -9863,8 +9886,8 @@ _trace();
     NSDate *closed = [Metadata_ objectForKey:@"LastClosed"];
     if (valid && closed != nil) {
         NSTimeInterval interval([closed timeIntervalSinceNow]);
-        // XXX: Is 60 minutes the optimal time here?
-        if (interval <= -(60*60))
+        // XXX: Is 30 minutes the optimal time here?
+        if (interval <= -(30*60))
             valid = NO;
     }
 
@@ -10237,6 +10260,17 @@ int main(int argc, char *argv[]) {
         Product_ = [info objectForKey:@"SafariProductVersion"];
         Safari_ = [info objectForKey:@"CFBundleVersion"];
     }
+
+    NSString *agent([NSString stringWithFormat:@"Cydia/%@ CF/%.2f", Cydia_, kCFCoreFoundationVersionNumber]);
+
+    if (Pcre match = Pcre("^[0-9]+(\\.[0-9]+)+", Safari_))
+        agent = [NSString stringWithFormat:@"Safari/%@ %@", match[0], agent];
+    if (Pcre match = Pcre("^[0-9]+[A-Z][0-9]+[a-z]?", System_))
+        agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[0], agent];
+    if (Pcre match = Pcre("^[0-9]+(\\.[0-9]+)+", Product_))
+        agent = [NSString stringWithFormat:@"Version/%@ %@", match[0], agent];
+
+    UserAgent_ = agent;
     /* }}} */
     /* Load Database {{{ */
     _trace();
@@ -10258,10 +10292,6 @@ int main(int argc, char *argv[]) {
         Token_ = [Metadata_ objectForKey:@"Token"];
 
         Version_ = [Metadata_ objectForKey:@"Version"];
-
-        @synchronized (HostConfig_) {
-            CydiaSource_ = [Metadata_ objectForKey:@"CydiaSource"];
-        }
     }
 
     if (Settings_ != nil)
@@ -10287,13 +10317,6 @@ int main(int argc, char *argv[]) {
         [Metadata_ setObject:Version_ forKey:@"Version"];
     }
 
-    @synchronized (HostConfig_) {
-        if (CydiaSource_ == nil) {
-            CydiaSource_ = @"apt.saurik.com";
-            [Metadata_ setObject:CydiaSource_ forKey:@"CydiaSource"];
-        }
-    }
-
     if ([Version_ unsignedIntValue] == 0) {
         CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
         CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
@@ -10346,11 +10369,7 @@ int main(int argc, char *argv[]) {
 
     int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]);
 
-    if (access("/tmp/.cydia.fw", F_OK) == 0) {
-        unlink("/tmp/.cydia.fw");
-        goto firmware;
-    } else if (access("/User", F_OK) != 0 || version < 4) {
-      firmware:
+    if (access("/User", F_OK) != 0 || version != 5) {
         _trace();
         system("/usr/libexec/cydia/firmware.sh");
         _trace();