]> git.saurik.com Git - cydia.git/blob - CyteKit/extern.mm
Swap awkward va_list hack for awkward switch hack.
[cydia.git] / CyteKit / extern.mm
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <CyteKit/UCPlatform.h>
23
24 #include <CyteKit/RegEx.hpp>
25 #include <CyteKit/WebViewController.h>
26 #include <CyteKit/extern.h>
27
28 #include <SystemConfiguration/SystemConfiguration.h>
29 #include <UIKit/UIKit.h>
30
31 #include <sys/sysctl.h>
32
33 #include <Menes/ObjectHandle.h>
34
35 bool IsWildcat_;
36 CGFloat ScreenScale_;
37
38 char *Machine_;
39 const char *System_;
40
41 bool CyteIsReachable(const char *name) {
42 SCNetworkReachabilityFlags flags; {
43 SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, name));
44 SCNetworkReachabilityGetFlags(reachability, &flags);
45 CFRelease(reachability);
46 }
47
48 // XXX: this elaborate mess is what Apple is using to determine this? :(
49 // XXX: do we care if the user has to intervene? maybe that's ok?
50 return
51 (flags & kSCNetworkReachabilityFlagsReachable) != 0 && (
52 (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || (
53 (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 ||
54 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0
55 ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 ||
56 (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0
57 )
58 ;
59 }
60
61 void CyteInitialize(NSString *agent) {
62 UIScreen *screen([UIScreen mainScreen]);
63 if ([screen respondsToSelector:@selector(scale)])
64 ScreenScale_ = [screen scale];
65 else
66 ScreenScale_ = 1;
67
68 UIDevice *device([UIDevice currentDevice]);
69 if ([device respondsToSelector:@selector(userInterfaceIdiom)]) {
70 UIUserInterfaceIdiom idiom([device userInterfaceIdiom]);
71 if (idiom == UIUserInterfaceIdiomPad)
72 IsWildcat_ = true;
73 }
74
75 size_t size;
76
77 sysctlbyname("kern.osversion", NULL, &size, NULL, 0);
78 char *osversion = new char[size];
79 if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1)
80 perror("sysctlbyname(\"kern.osversion\", ?)");
81 else
82 System_ = osversion;
83
84 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
85 char *machine = new char[size];
86 if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1)
87 perror("sysctlbyname(\"hw.machine\", ?)");
88 else
89 Machine_ = machine;
90
91 _H<NSString> product;
92 _H<NSString> safari;
93
94 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) {
95 product = [info objectForKey:@"SafariProductVersion"] ?: [info objectForKey:@"CFBundleShortVersionString"];
96 safari = [info objectForKey:@"CFBundleVersion"];
97 }
98
99 agent = [NSString stringWithFormat:@"%@ CyF/%.2f", agent, kCFCoreFoundationVersionNumber];
100
101 if (safari != nil)
102 if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", safari))
103 agent = [NSString stringWithFormat:@"Safari/%@ %@", match[1], agent];
104 if (RegEx match = RegEx("([0-9]+[A-Z][0-9]+[a-z]?).*", System_))
105 agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[1], agent];
106 if (product != nil)
107 if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", product))
108 agent = [NSString stringWithFormat:@"Version/%@ %@", match[1], agent];
109
110 [CyteWebViewController setApplicationNameForUserAgent:agent];
111 }