]> git.saurik.com Git - cydia.git/blobdiff - CyteKit/extern.mm
Objective-C seriously didn't notice my mistake :/.
[cydia.git] / CyteKit / extern.mm
index c1dc806ac07c92c6a7902778624ba2cc20c72bb3..cbf3774914d6de6c8e32d967855a109596d41280 100644 (file)
 **/
 /* }}} */
 
+#include <CyteKit/UCPlatform.h>
+
+#include <CyteKit/RegEx.hpp>
+#include <CyteKit/WebViewController.h>
 #include <CyteKit/extern.h>
+
+#include <SystemConfiguration/SystemConfiguration.h>
 #include <UIKit/UIKit.h>
 
+#include <sys/sysctl.h>
+
+#include <Menes/ObjectHandle.h>
+
 bool IsWildcat_;
 CGFloat ScreenScale_;
 
-__attribute__((__constructor__))
-void CyteKit_extern() {
+char *Machine_;
+const char *System_;
+
+bool CyteIsReachable(const char *name) {
+    SCNetworkReachabilityFlags flags; {
+        SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, name));
+        SCNetworkReachabilityGetFlags(reachability, &flags);
+        CFRelease(reachability);
+    }
+
+    // XXX: this elaborate mess is what Apple is using to determine this? :(
+    // XXX: do we care if the user has to intervene? maybe that's ok?
+    return
+        (flags & kSCNetworkReachabilityFlagsReachable) != 0 && (
+            (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || (
+                (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 ||
+                (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0
+            ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 ||
+            (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0
+        )
+    ;
+}
+
+void CyteInitialize(NSString *agent) {
     UIScreen *screen([UIScreen mainScreen]);
     if ([screen respondsToSelector:@selector(scale)])
         ScreenScale_ = [screen scale];
@@ -39,4 +71,41 @@ void CyteKit_extern() {
         if (idiom == UIUserInterfaceIdiomPad)
             IsWildcat_ = true;
     }
+
+    size_t size;
+
+    sysctlbyname("kern.osversion", NULL, &size, NULL, 0);
+    char *osversion = new char[size];
+    if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1)
+        perror("sysctlbyname(\"kern.osversion\", ?)");
+    else
+        System_ = osversion;
+
+    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
+    char *machine = new char[size];
+    if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1)
+        perror("sysctlbyname(\"hw.machine\", ?)");
+    else
+        Machine_ = machine;
+
+    _H<NSString> product;
+    _H<NSString> safari;
+
+    if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) {
+        product = [info objectForKey:@"SafariProductVersion"] ?: [info objectForKey:@"CFBundleShortVersionString"];
+        safari = [info objectForKey:@"CFBundleVersion"];
+    }
+
+    agent = [NSString stringWithFormat:@"%@ CyF/%.2f", agent, kCFCoreFoundationVersionNumber];
+
+    if (safari != nil)
+        if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", safari))
+            agent = [NSString stringWithFormat:@"Safari/%@ %@", match[1], agent];
+    if (RegEx match = RegEx("([0-9]+[A-Z][0-9]+[a-z]?).*", System_))
+        agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[1], agent];
+    if (product != nil)
+        if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", product))
+            agent = [NSString stringWithFormat:@"Version/%@ %@", match[1], agent];
+
+    [CyteWebViewController setApplicationNameForUserAgent:agent];
 }