]>
Commit | Line | Data |
---|---|---|
36bb2ca2 | 1 | /* Cydia - iPhone UIKit Front-End for Debian APT |
6fa0bb60 | 2 | * Copyright (C) 2008-2014 Jay Freeman (saurik) |
36bb2ca2 JF |
3 | */ |
4 | ||
6d9696a5 | 5 | /* GNU General Public License, Version 3 {{{ */ |
36bb2ca2 | 6 | /* |
6d9696a5 JF |
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. | |
36bb2ca2 | 11 | * |
6d9696a5 JF |
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. | |
36bb2ca2 | 16 | * |
6d9696a5 JF |
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 | **/ | |
bfc87a4d | 20 | /* }}} */ |
36bb2ca2 | 21 | |
b0ad8dee JF |
22 | // XXX: wtf/FastMalloc.h... wtf? |
23 | #define USE_SYSTEM_MALLOC 1 | |
24 | ||
dc5812ec | 25 | /* #include Directives {{{ */ |
819a0ab1 | 26 | #include "CyteKit/UCPlatform.h" |
d458596e | 27 | #include "CyteKit/Localize.h" |
807ae6d7 | 28 | |
22fd24dd JF |
29 | #include <unicode/ustring.h> |
30 | #include <unicode/utrans.h> | |
31 | ||
ab398adf JF |
32 | #include <objc/objc.h> |
33 | #include <objc/runtime.h> | |
34 | ||
5f6bff8c | 35 | #include <CoreGraphics/CoreGraphics.h> |
5f6bff8c | 36 | #include <Foundation/Foundation.h> |
7d2ac47f | 37 | |
eef4ccaf JF |
38 | #if 0 |
39 | #define DEPLOYMENT_TARGET_MACOSX 1 | |
40 | #define CF_BUILDING_CF 1 | |
41 | #include <CoreFoundation/CFInternal.h> | |
42 | #endif | |
43 | ||
677b8415 JF |
44 | #include <CoreFoundation/CFUniChar.h> |
45 | ||
afb5333a JF |
46 | #include <SystemConfiguration/SystemConfiguration.h> |
47 | ||
c21004b9 JF |
48 | #include <UIKit/UIKit.h> |
49 | #include "iPhonePrivate.h" | |
50 | ||
51 | #include <IOKit/IOKitLib.h> | |
a9543575 | 52 | |
53db9999 JF |
53 | #include <QuartzCore/CALayer.h> |
54 | ||
f79a4512 | 55 | #include <WebCore/WebCoreThread.h> |
aa1e1906 | 56 | #include <WebKit/DOMHTMLIFrameElement.h> |
7e9a36b6 | 57 | |
677b8415 | 58 | #include <algorithm> |
808c6eb6 | 59 | #include <iomanip> |
21ac0ce2 | 60 | #include <set> |
2367a917 | 61 | #include <sstream> |
a0be02eb JF |
62 | #include <string> |
63 | ||
2367a917 JF |
64 | #include <ext/stdio_filebuf.h> |
65 | ||
e59669fd JF |
66 | #undef ABS |
67 | ||
dc5812ec JF |
68 | #include <apt-pkg/acquire.h> |
69 | #include <apt-pkg/acquire-item.h> | |
70 | #include <apt-pkg/algorithms.h> | |
71 | #include <apt-pkg/cachefile.h> | |
77fcccaf | 72 | #include <apt-pkg/clean.h> |
dc5812ec | 73 | #include <apt-pkg/configuration.h> |
7376b55c | 74 | #include <apt-pkg/debindexfile.h> |
3178d79b | 75 | #include <apt-pkg/debmetaindex.h> |
dc5812ec JF |
76 | #include <apt-pkg/error.h> |
77 | #include <apt-pkg/init.h> | |
dddbc481 | 78 | #include <apt-pkg/mmap.h> |
dc5812ec | 79 | #include <apt-pkg/pkgrecords.h> |
dddbc481 | 80 | #include <apt-pkg/sha1.h> |
dc5812ec | 81 | #include <apt-pkg/sourcelist.h> |
2367a917 | 82 | #include <apt-pkg/sptr.h> |
affeffc7 | 83 | #include <apt-pkg/strutl.h> |
f9f6d9e8 | 84 | #include <apt-pkg/tagfile.h> |
dc5812ec | 85 | |
87f46a96 JF |
86 | #include <sys/types.h> |
87 | #include <sys/stat.h> | |
3178d79b | 88 | #include <sys/sysctl.h> |
59c011d8 JF |
89 | #include <sys/param.h> |
90 | #include <sys/mount.h> | |
bb0fe3c9 | 91 | #include <sys/reboot.h> |
87f46a96 | 92 | |
c21004b9 | 93 | #include <fcntl.h> |
2a8d9add | 94 | #include <notify.h> |
dddbc481 | 95 | #include <dlfcn.h> |
3178d79b | 96 | |
b4d89997 JF |
97 | extern "C" { |
98 | #include <mach-o/nlist.h> | |
99 | } | |
100 | ||
a0be02eb JF |
101 | #include <cstdio> |
102 | #include <cstdlib> | |
103 | #include <cstring> | |
b4d89997 | 104 | |
2367a917 | 105 | #include <errno.h> |
a9543575 | 106 | |
94b0b3e5 | 107 | #include <Cytore.hpp> |
25c1dafb | 108 | #include "Sources.h" |
94b0b3e5 | 109 | |
a09f7a48 | 110 | #include "Substrate.hpp" |
cb218676 JF |
111 | #include "Menes/Menes.h" |
112 | ||
8731fdb0 | 113 | #include "CyteKit/IndirectDelegate.h" |
b4fd1197 | 114 | #include "CyteKit/RegEx.hpp" |
b97fcfc6 | 115 | #include "CyteKit/TableViewCell.h" |
5fe2bcc6 | 116 | #include "CyteKit/TabBarController.h" |
f172aa8f | 117 | #include "CyteKit/WebScriptObject-Cyte.h" |
d458596e | 118 | #include "CyteKit/WebViewController.h" |
8731fdb0 | 119 | #include "CyteKit/WebViewTableViewCell.h" |
d458596e | 120 | #include "CyteKit/stringWithUTF8Bytes.h" |
449ef9d5 | 121 | |
7aa82ca2 | 122 | #include "Cydia/MIMEAddress.h" |
70750ab3 | 123 | #include "Cydia/LoadingViewController.h" |
cb218676 | 124 | #include "Cydia/ProgressEvent.h" |
28b8b687 | 125 | |
71cc7be1 | 126 | #include "SDURLCache/SDURLCache.h" |
b5e7eebb GP |
127 | /* }}} */ |
128 | ||
bfc87a4d | 129 | /* Profiler {{{ */ |
807ae6d7 JF |
130 | struct timeval _ltv; |
131 | bool _itv; | |
132 | ||
2083b866 JF |
133 | #define _timestamp ({ \ |
134 | struct timeval tv; \ | |
135 | gettimeofday(&tv, NULL); \ | |
136 | tv.tv_sec * 1000000 + tv.tv_usec; \ | |
137 | }) | |
138 | ||
808c6eb6 JF |
139 | typedef std::vector<class ProfileTime *> TimeList; |
140 | TimeList times_; | |
141 | ||
142 | class ProfileTime { | |
143 | private: | |
144 | const char *name_; | |
145 | uint64_t total_; | |
76933519 | 146 | uint64_t count_; |
808c6eb6 JF |
147 | |
148 | public: | |
149 | ProfileTime(const char *name) : | |
150 | name_(name), | |
151 | total_(0) | |
152 | { | |
153 | times_.push_back(this); | |
154 | } | |
155 | ||
156 | void AddTime(uint64_t time) { | |
157 | total_ += time; | |
76933519 | 158 | ++count_; |
808c6eb6 JF |
159 | } |
160 | ||
161 | void Print() { | |
162 | if (total_ != 0) | |
f15558c7 | 163 | std::cerr << std::setw(7) << count_ << ", " << std::setw(8) << total_ << " : " << name_ << std::endl; |
808c6eb6 | 164 | total_ = 0; |
76933519 | 165 | count_ = 0; |
808c6eb6 JF |
166 | } |
167 | }; | |
168 | ||
169 | class ProfileTimer { | |
170 | private: | |
171 | ProfileTime &time_; | |
172 | uint64_t start_; | |
173 | ||
174 | public: | |
175 | ProfileTimer(ProfileTime &time) : | |
176 | time_(time), | |
177 | start_(_timestamp) | |
178 | { | |
179 | } | |
180 | ||
181 | ~ProfileTimer() { | |
182 | time_.AddTime(_timestamp - start_); | |
183 | } | |
184 | }; | |
185 | ||
186 | void PrintTimes() { | |
187 | for (TimeList::const_iterator i(times_.begin()); i != times_.end(); ++i) | |
188 | (*i)->Print(); | |
189 | std::cerr << "========" << std::endl; | |
190 | } | |
191 | ||
192 | #define _profile(name) { \ | |
193 | static ProfileTime name(#name); \ | |
194 | ProfileTimer _ ## name(name); | |
195 | ||
196 | #define _end } | |
f79a4512 | 197 | /* }}} */ |
affeffc7 | 198 | |
4cc9e99a JF |
199 | // XXX: I hate clang. Apple: please get over your petty hatred of GPL and fix your gcc fork |
200 | #define synchronized(lock) \ | |
201 | synchronized(static_cast<NSObject *>(lock)) | |
202 | ||
3d45bad1 | 203 | extern NSString *Cydia_; |
f9ba524a | 204 | |
43c5d1cb JF |
205 | #define lprintf(args...) fprintf(stderr, args) |
206 | ||
207 | #define ForRelease 1 | |
208 | #define TraceLogging (1 && !ForRelease) | |
01b4e393 | 209 | #define HistogramInsertionSort (0 && !ForRelease) |
43c5d1cb JF |
210 | #define ProfileTimes (0 && !ForRelease) |
211 | #define ForSaurik (0 && !ForRelease) | |
212 | #define LogBrowser (0 && !ForRelease) | |
213 | #define TrackResize (0 && !ForRelease) | |
214 | #define ManualRefresh (1 && !ForRelease) | |
215 | #define ShowInternals (0 && !ForRelease) | |
216 | #define AlwaysReload (0 && !ForRelease) | |
43c5d1cb JF |
217 | |
218 | #if !TraceLogging | |
219 | #undef _trace | |
220 | #define _trace(args...) | |
221 | #endif | |
222 | ||
223 | #if !ProfileTimes | |
224 | #undef _profile | |
225 | #define _profile(name) { | |
226 | #undef _end | |
227 | #define _end } | |
228 | #define PrintTimes() do {} while (false) | |
229 | #endif | |
230 | ||
94b0b3e5 JF |
231 | // Hash Functions/Structures {{{ |
232 | extern "C" uint32_t hashlittle(const void *key, size_t length, uint32_t initval = 0); | |
233 | ||
234 | union SplitHash { | |
235 | uint32_t u32; | |
236 | uint16_t u16[2]; | |
237 | }; | |
238 | // }}} | |
239 | ||
34ac1598 JF |
240 | static void setreugid(uid_t uid, gid_t gid) { |
241 | _assert(setreuid(uid, uid) != -1); | |
242 | _assert(setregid(gid, gid) != -1); | |
243 | } | |
584daea0 | 244 | |
34ac1598 JF |
245 | static void setreguid(gid_t gid, uid_t uid) { |
246 | _assert(setregid(gid, gid) != -1); | |
247 | _assert(setreuid(uid, uid) != -1); | |
248 | } | |
b44af625 | 249 | |
584daea0 JF |
250 | struct Root { |
251 | Root() { | |
b44af625 | 252 | _trace(); |
34ac1598 | 253 | setreugid(0, 0); |
584daea0 | 254 | _assert(pthread_setugid_np(0, 0) != -1); |
34ac1598 | 255 | setreguid(501, 501); |
b44af625 JF |
256 | } |
257 | ||
258 | ~Root() { | |
b44af625 | 259 | _trace(); |
34ac1598 | 260 | setreugid(0, 0); |
584daea0 | 261 | _assert(pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE) != -1); |
34ac1598 | 262 | setreguid(501, 501); |
b44af625 JF |
263 | } |
264 | }; | |
265 | ||
584daea0 JF |
266 | #define _root(code) \ |
267 | ({ Root _root; code; }) | |
b44af625 | 268 | |
5db5891a JF |
269 | static NSString *Colon_; |
270 | NSString *Elision_; | |
271 | static NSString *Error_; | |
272 | static NSString *Warning_; | |
273 | ||
d1c7f1fd | 274 | static NSString *Cache_; |
b44af625 JF |
275 | #define Cache(file) \ |
276 | [NSString stringWithFormat:@"%@/%s", Cache_, file] | |
d1c7f1fd | 277 | |
8a3b565c JF |
278 | static void (*$SBSSetInterceptsMenuButtonForever)(bool); |
279 | ||
7c80833f JF |
280 | static CFStringRef (*$MGCopyAnswer)(CFStringRef); |
281 | ||
282 | static NSString *UniqueIdentifier(UIDevice *device = nil) { | |
283 | if (kCFCoreFoundationVersionNumber < 800) // iOS 7.x | |
284 | return [device ?: [UIDevice currentDevice] uniqueIdentifier]; | |
285 | else | |
286 | return [(id)$MGCopyAnswer(CFSTR("UniqueDeviceID")) autorelease]; | |
287 | } | |
288 | ||
6cbfbe28 JF |
289 | static bool IsReachable(const char *name) { |
290 | SCNetworkReachabilityFlags flags; { | |
37f1fb03 | 291 | SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, name)); |
6cbfbe28 JF |
292 | SCNetworkReachabilityGetFlags(reachability, &flags); |
293 | CFRelease(reachability); | |
294 | } | |
295 | ||
296 | // XXX: this elaborate mess is what Apple is using to determine this? :( | |
297 | // XXX: do we care if the user has to intervene? maybe that's ok? | |
298 | return | |
299 | (flags & kSCNetworkReachabilityFlagsReachable) != 0 && ( | |
300 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || ( | |
301 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 || | |
302 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0 | |
303 | ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 || | |
304 | (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0 | |
305 | ) | |
306 | ; | |
307 | } | |
308 | ||
04fe1349 JF |
309 | static const NSUInteger UIViewAutoresizingFlexibleBoth(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); |
310 | ||
670a0494 | 311 | static _finline NSString *CydiaURL(NSString *path) { |
e3d2a2f5 JF |
312 | char page[26]; |
313 | page[0] = 'h'; page[1] = 't'; page[2] = 't'; page[3] = 'p'; page[4] = 's'; | |
314 | page[5] = ':'; page[6] = '/'; page[7] = '/'; page[8] = 'c'; page[9] = 'y'; | |
315 | page[10] = 'd'; page[11] = 'i'; page[12] = 'a'; page[13] = '.'; page[14] = 's'; | |
316 | page[15] = 'a'; page[16] = 'u'; page[17] = 'r'; page[18] = 'i'; page[19] = 'k'; | |
317 | page[20] = '.'; page[21] = 'c'; page[22] = 'o'; page[23] = 'm'; page[24] = '/'; | |
318 | page[25] = '\0'; | |
670a0494 JF |
319 | return [[NSString stringWithUTF8String:page] stringByAppendingString:path]; |
320 | } | |
321 | ||
d99e3659 JF |
322 | static void ReapZombie(pid_t pid) { |
323 | int status; | |
324 | wait: | |
325 | if (waitpid(pid, &status, 0) == -1) | |
326 | if (errno == EINTR) | |
327 | goto wait; | |
328 | else _assert(false); | |
329 | } | |
330 | ||
ef494bd8 RP |
331 | static _finline void UpdateExternalStatus(uint64_t newStatus) { |
332 | int notify_token; | |
333 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
334 | notify_set_state(notify_token, newStatus); | |
335 | notify_cancel(notify_token); | |
336 | } | |
337 | notify_post("com.saurik.Cydia.status"); | |
338 | } | |
339 | ||
0b7516cf | 340 | static CGFloat CYStatusBarHeight() { |
57daa971 | 341 | CGSize size([[UIApplication sharedApplication] statusBarFrame].size); |
0b7516cf | 342 | return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? size.height : size.width; |
57daa971 JF |
343 | } |
344 | ||
68f1828e | 345 | /* NSForcedOrderingSearch doesn't work on the iPhone */ |
808c6eb6 | 346 | static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch; |
677b8415 | 347 | static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch; |
aeeb755b | 348 | static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; |
1e7a90f5 | 349 | |
eef4ccaf JF |
350 | /* Insertion Sort {{{ */ |
351 | ||
df213583 JF |
352 | CFIndex SKBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
353 | const char *ptr = (const char *)list; | |
354 | while (0 < count) { | |
355 | CFIndex half = count / 2; | |
356 | const char *probe = ptr + elementSize * half; | |
357 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 358 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
df213583 JF |
359 | ptr = (cr < 0) ? ptr : probe + elementSize; |
360 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
361 | } | |
362 | return (ptr - (const char *)list) / elementSize; | |
363 | } | |
364 | ||
eef4ccaf JF |
365 | CFIndex CFBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
366 | const char *ptr = (const char *)list; | |
367 | while (0 < count) { | |
368 | CFIndex half = count / 2; | |
369 | const char *probe = ptr + elementSize * half; | |
370 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 371 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
eef4ccaf JF |
372 | ptr = (cr < 0) ? ptr : probe + elementSize; |
373 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
374 | } | |
375 | return (ptr - (const char *)list) / elementSize; | |
376 | } | |
377 | ||
378 | void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFComparatorFunction comparator, void *context) { | |
379 | if (range.length == 0) | |
380 | return; | |
381 | const void **values(new const void *[range.length]); | |
382 | CFArrayGetValues(array, range, values); | |
383 | ||
1539387a | 384 | #if HistogramInsertionSort > 0 |
df213583 JF |
385 | uint32_t total(0), *offsets(new uint32_t[range.length]); |
386 | #endif | |
387 | ||
eef4ccaf JF |
388 | for (CFIndex index(1); index != range.length; ++index) { |
389 | const void *value(values[index]); | |
df213583 JF |
390 | //CFIndex correct(SKBSearch_(&value, sizeof(const void *), values, index, comparator, context)); |
391 | CFIndex correct(index); | |
1539387a JF |
392 | while (comparator(value, values[correct - 1], context) == kCFCompareLessThan) { |
393 | #if HistogramInsertionSort > 1 | |
394 | NSLog(@"%@ < %@", value, values[correct - 1]); | |
395 | #endif | |
df213583 JF |
396 | if (--correct == 0) |
397 | break; | |
1539387a | 398 | } |
eef4ccaf | 399 | if (correct != index) { |
df213583 JF |
400 | size_t offset(index - correct); |
401 | #if HistogramInsertionSort | |
402 | total += offset; | |
403 | ++offsets[offset]; | |
404 | if (offset > 10) | |
405 | NSLog(@"Heavy Insertion Displacement: %u = %@", offset, value); | |
406 | #endif | |
407 | memmove(values + correct + 1, values + correct, sizeof(const void *) * offset); | |
eef4ccaf JF |
408 | values[correct] = value; |
409 | } | |
410 | } | |
411 | ||
412 | CFArrayReplaceValues(array, range, values, range.length); | |
413 | delete [] values; | |
df213583 | 414 | |
1539387a | 415 | #if HistogramInsertionSort > 0 |
df213583 JF |
416 | for (CFIndex index(0); index != range.length; ++index) |
417 | if (offsets[index] != 0) | |
418 | NSLog(@"Insertion Displacement [%u]: %u", index, offsets[index]); | |
419 | NSLog(@"Average Insertion Displacement: %f", double(total) / range.length); | |
420 | delete [] offsets; | |
421 | #endif | |
eef4ccaf JF |
422 | } |
423 | ||
807ae6d7 JF |
424 | /* }}} */ |
425 | ||
bf8476c8 JF |
426 | /* Apple Bug Fixes {{{ */ |
427 | @implementation UIWebDocumentView (Cydia) | |
428 | ||
429 | - (void) _setScrollerOffset:(CGPoint)offset { | |
430 | UIScroller *scroller([self _scroller]); | |
431 | ||
432 | CGSize size([scroller contentSize]); | |
433 | CGSize bounds([scroller bounds].size); | |
434 | ||
435 | CGPoint max; | |
436 | max.x = size.width - bounds.width; | |
437 | max.y = size.height - bounds.height; | |
438 | ||
439 | // wtf Apple?! | |
440 | if (max.x < 0) | |
441 | max.x = 0; | |
442 | if (max.y < 0) | |
443 | max.y = 0; | |
444 | ||
445 | offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; | |
446 | offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; | |
447 | ||
448 | [scroller setOffset:offset]; | |
449 | } | |
450 | ||
451 | @end | |
452 | /* }}} */ | |
453 | ||
680eb135 JF |
454 | NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { |
455 | size_t length([self length] - state->state); | |
456 | if (length <= 0) | |
457 | return 0; | |
458 | else if (length > count) | |
459 | length = count; | |
460 | for (size_t i(0); i != length; ++i) | |
461 | objects[i] = [self item:state->state++]; | |
462 | state->itemsPtr = objects; | |
463 | state->mutationsPtr = (unsigned long *) self; | |
464 | return length; | |
465 | } | |
466 | ||
bfc87a4d | 467 | /* Cydia NSString Additions {{{ */ |
2388b078 | 468 | @interface NSString (Cydia) |
a54b1c10 | 469 | - (NSComparisonResult) compareByPath:(NSString *)other; |
2fc76a2d | 470 | - (NSString *) stringByAddingPercentEscapesIncludingReserved; |
2388b078 JF |
471 | @end |
472 | ||
449ef9d5 JF |
473 | @implementation NSString (Cydia) |
474 | ||
a54b1c10 JF |
475 | - (NSComparisonResult) compareByPath:(NSString *)other { |
476 | NSString *prefix = [self commonPrefixWithString:other options:0]; | |
477 | size_t length = [prefix length]; | |
478 | ||
479 | NSRange lrange = NSMakeRange(length, [self length] - length); | |
480 | NSRange rrange = NSMakeRange(length, [other length] - length); | |
481 | ||
482 | lrange = [self rangeOfString:@"/" options:0 range:lrange]; | |
483 | rrange = [other rangeOfString:@"/" options:0 range:rrange]; | |
484 | ||
485 | NSComparisonResult value; | |
486 | ||
487 | if (lrange.location == NSNotFound && rrange.location == NSNotFound) | |
488 | value = NSOrderedSame; | |
489 | else if (lrange.location == NSNotFound) | |
490 | value = NSOrderedAscending; | |
491 | else if (rrange.location == NSNotFound) | |
492 | value = NSOrderedDescending; | |
493 | else | |
494 | value = NSOrderedSame; | |
495 | ||
496 | NSString *lpath = lrange.location == NSNotFound ? [self substringFromIndex:length] : | |
497 | [self substringWithRange:NSMakeRange(length, lrange.location - length)]; | |
498 | NSString *rpath = rrange.location == NSNotFound ? [other substringFromIndex:length] : | |
499 | [other substringWithRange:NSMakeRange(length, rrange.location - length)]; | |
500 | ||
501 | NSComparisonResult result = [lpath compare:rpath]; | |
502 | return result == NSOrderedSame ? value : result; | |
503 | } | |
504 | ||
2fc76a2d JF |
505 | - (NSString *) stringByAddingPercentEscapesIncludingReserved { |
506 | return [(id)CFURLCreateStringByAddingPercentEscapes( | |
bc11cf5b | 507 | kCFAllocatorDefault, |
2fc76a2d JF |
508 | (CFStringRef) self, |
509 | NULL, | |
510 | CFSTR(";/?:@&=+$,"), | |
511 | kCFStringEncodingUTF8 | |
512 | ) autorelease]; | |
513 | } | |
514 | ||
2388b078 | 515 | @end |
bfc87a4d | 516 | /* }}} */ |
2388b078 | 517 | |
bfc87a4d | 518 | /* C++ NSString Wrapper Cache {{{ */ |
8e8fca7f JF |
519 | static _finline CFStringRef CYStringCreate(const char *data, size_t size) { |
520 | return size == 0 ? NULL : | |
521 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingUTF8, NO, kCFAllocatorNull) ?: | |
522 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingISOLatin1, NO, kCFAllocatorNull); | |
523 | } | |
524 | ||
525 | static _finline CFStringRef CYStringCreate(const char *data) { | |
526 | return CYStringCreate(data, strlen(data)); | |
527 | } | |
528 | ||
f79a4512 JF |
529 | class CYString { |
530 | private: | |
531 | char *data_; | |
532 | size_t size_; | |
533 | CFStringRef cache_; | |
534 | ||
535 | _finline void clear_() { | |
18873623 | 536 | if (cache_ != NULL) { |
f79a4512 | 537 | CFRelease(cache_); |
18873623 JF |
538 | cache_ = NULL; |
539 | } | |
f79a4512 JF |
540 | } |
541 | ||
542 | public: | |
543 | _finline bool empty() const { | |
544 | return size_ == 0; | |
545 | } | |
546 | ||
547 | _finline size_t size() const { | |
548 | return size_; | |
549 | } | |
550 | ||
551 | _finline char *data() const { | |
552 | return data_; | |
553 | } | |
554 | ||
555 | _finline void clear() { | |
556 | size_ = 0; | |
557 | clear_(); | |
558 | } | |
559 | ||
560 | _finline CYString() : | |
561 | data_(0), | |
562 | size_(0), | |
18873623 | 563 | cache_(NULL) |
f79a4512 JF |
564 | { |
565 | } | |
566 | ||
567 | _finline ~CYString() { | |
568 | clear_(); | |
569 | } | |
570 | ||
571 | void operator =(const CYString &rhs) { | |
572 | data_ = rhs.data_; | |
573 | size_ = rhs.size_; | |
574 | ||
575 | if (rhs.cache_ == nil) | |
576 | cache_ = NULL; | |
577 | else | |
578 | cache_ = reinterpret_cast<CFStringRef>(CFRetain(rhs.cache_)); | |
579 | } | |
580 | ||
64edd9df JF |
581 | void copy(CYPool *pool) { |
582 | char *temp(pool->malloc<char>(size_ + 1)); | |
97814287 JF |
583 | memcpy(temp, data_, size_); |
584 | temp[size_] = '\0'; | |
585 | data_ = temp; | |
586 | } | |
587 | ||
64edd9df | 588 | void set(CYPool *pool, const char *data, size_t size) { |
f79a4512 JF |
589 | if (size == 0) |
590 | clear(); | |
591 | else { | |
592 | clear_(); | |
593 | ||
97814287 | 594 | data_ = const_cast<char *>(data); |
f79a4512 | 595 | size_ = size; |
97814287 JF |
596 | |
597 | if (pool != NULL) | |
598 | copy(pool); | |
f79a4512 JF |
599 | } |
600 | } | |
601 | ||
64edd9df | 602 | _finline void set(CYPool *pool, const char *data) { |
f79a4512 JF |
603 | set(pool, data, data == NULL ? 0 : strlen(data)); |
604 | } | |
605 | ||
64edd9df | 606 | _finline void set(CYPool *pool, const std::string &rhs) { |
f79a4512 JF |
607 | set(pool, rhs.data(), rhs.size()); |
608 | } | |
609 | ||
610 | bool operator ==(const CYString &rhs) const { | |
611 | return size_ == rhs.size_ && memcmp(data_, rhs.data_, size_) == 0; | |
612 | } | |
613 | ||
8e8fca7f JF |
614 | _finline operator CFStringRef() { |
615 | if (cache_ == NULL) | |
616 | cache_ = CYStringCreate(data_, size_); | |
617 | return cache_; | |
df213583 JF |
618 | } |
619 | ||
620 | _finline operator id() { | |
621 | return (NSString *) static_cast<CFStringRef>(*this); | |
f79a4512 | 622 | } |
4c0ed943 JF |
623 | |
624 | _finline operator const char *() { | |
625 | return reinterpret_cast<const char *>(data_); | |
626 | } | |
f79a4512 | 627 | }; |
bfc87a4d JF |
628 | /* }}} */ |
629 | /* C++ NSString Algorithm Adapters {{{ */ | |
f79a4512 JF |
630 | extern "C" { |
631 | CF_EXPORT CFHashCode CFStringHashNSString(CFStringRef str); | |
632 | } | |
633 | ||
634 | struct NSStringMapHash : | |
635 | std::unary_function<NSString *, size_t> | |
636 | { | |
637 | _finline size_t operator ()(NSString *value) const { | |
638 | return CFStringHashNSString((CFStringRef) value); | |
639 | } | |
640 | }; | |
641 | ||
642 | struct NSStringMapLess : | |
643 | std::binary_function<NSString *, NSString *, bool> | |
644 | { | |
645 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
646 | return [lhs compare:rhs] == NSOrderedAscending; | |
647 | } | |
648 | }; | |
649 | ||
650 | struct NSStringMapEqual : | |
651 | std::binary_function<NSString *, NSString *, bool> | |
652 | { | |
653 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
654 | return CFStringCompare((CFStringRef) lhs, (CFStringRef) rhs, 0) == kCFCompareEqualTo; | |
655 | //CFEqual((CFTypeRef) lhs, (CFTypeRef) rhs); | |
656 | //[lhs isEqualToString:rhs]; | |
657 | } | |
658 | }; | |
bfc87a4d | 659 | /* }}} */ |
f79a4512 | 660 | |
5f6bff8c | 661 | /* CoreGraphics Primitives {{{ */ |
02012733 | 662 | class CYColor { |
b4d89997 JF |
663 | private: |
664 | CGColorRef color_; | |
665 | ||
6a575b5e JF |
666 | static CGColorRef Create_(CGColorSpaceRef space, float red, float green, float blue, float alpha) { |
667 | CGFloat color[] = {red, green, blue, alpha}; | |
668 | return CGColorCreate(space, color); | |
669 | } | |
670 | ||
b4d89997 | 671 | public: |
02012733 | 672 | CYColor() : |
36bb2ca2 JF |
673 | color_(NULL) |
674 | { | |
675 | } | |
676 | ||
02012733 | 677 | CYColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) : |
6a575b5e | 678 | color_(Create_(space, red, green, blue, alpha)) |
36bb2ca2 JF |
679 | { |
680 | Set(space, red, green, blue, alpha); | |
681 | } | |
682 | ||
683 | void Clear() { | |
684 | if (color_ != NULL) | |
685 | CGColorRelease(color_); | |
b4d89997 JF |
686 | } |
687 | ||
02012733 | 688 | ~CYColor() { |
36bb2ca2 JF |
689 | Clear(); |
690 | } | |
691 | ||
692 | void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) { | |
693 | Clear(); | |
6a575b5e | 694 | color_ = Create_(space, red, green, blue, alpha); |
b4d89997 JF |
695 | } |
696 | ||
697 | operator CGColorRef() { | |
698 | return color_; | |
699 | } | |
700 | }; | |
b4d89997 JF |
701 | /* }}} */ |
702 | ||
36bb2ca2 | 703 | /* Random Global Variables {{{ */ |
c73d524b | 704 | static int PulseInterval_ = 500000; |
affeffc7 | 705 | |
57e8b225 JF |
706 | static const NSString *UI_; |
707 | ||
d791dce4 | 708 | static int Finish_; |
be860cc8 | 709 | static bool RestartSubstrate_; |
856b8388 | 710 | static bool UpgradeCydia_; |
d791dce4 JF |
711 | static NSArray *Finishes_; |
712 | ||
affeffc7 | 713 | #define SpringBoard_ "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist" |
22f8bed9 | 714 | #define NotifyConfig_ "/etc/notify.conf" |
2a8d9add | 715 | |
dc63e78f JF |
716 | static bool Queuing_; |
717 | ||
02012733 JF |
718 | static CYColor Blue_; |
719 | static CYColor Blueish_; | |
720 | static CYColor Black_; | |
86a333c6 | 721 | static CYColor Folder_; |
02012733 JF |
722 | static CYColor Off_; |
723 | static CYColor White_; | |
724 | static CYColor Gray_; | |
725 | static CYColor Green_; | |
726 | static CYColor Purple_; | |
727 | static CYColor Purplish_; | |
3bd1c2a2 | 728 | |
dc63e78f JF |
729 | static UIColor *InstallingColor_; |
730 | static UIColor *RemovingColor_; | |
36bb2ca2 | 731 | |
7d2ac47f | 732 | static NSString *App_; |
d73cede2 | 733 | |
2388b078 | 734 | static BOOL Advanced_; |
a54b1c10 | 735 | static BOOL Ignored_; |
2388b078 | 736 | |
7b33d201 JF |
737 | static _H<UIFont> Font12_; |
738 | static _H<UIFont> Font12Bold_; | |
739 | static _H<UIFont> Font14_; | |
2cdc6e57 | 740 | static _H<UIFont> Font18_; |
7b33d201 JF |
741 | static _H<UIFont> Font18Bold_; |
742 | static _H<UIFont> Font22Bold_; | |
f641a0e5 | 743 | |
87f46a96 | 744 | static const char *Machine_ = NULL; |
e967efd5 | 745 | static _H<NSString> System_; |
56127854 JF |
746 | static NSString *SerialNumber_ = nil; |
747 | static NSString *ChipID_ = nil; | |
6ffdaae3 | 748 | static NSString *BBSNum_ = nil; |
9737d93e | 749 | static _H<NSString> Token_; |
7c80833f | 750 | static _H<NSString> UniqueID_; |
e967efd5 JF |
751 | static _H<NSString> UserAgent_; |
752 | static _H<NSString> Product_; | |
753 | static _H<NSString> Safari_; | |
2a8d9add | 754 | |
aeeb755b JF |
755 | static _H<NSLocale> CollationLocale_; |
756 | static _H<NSArray> CollationThumbs_; | |
757 | static std::vector<NSInteger> CollationOffset_; | |
758 | static _H<NSArray> CollationTitles_; | |
759 | static _H<NSArray> CollationStarts_; | |
22fd24dd JF |
760 | static UTransliterator *CollationTransl_; |
761 | //static Function<NSString *, NSString *> CollationModify_; | |
762 | ||
763 | typedef std::basic_string<UChar> ustring; | |
764 | static ustring CollationString_; | |
765 | ||
766 | #define CUC const ustring &str(*reinterpret_cast<const ustring *>(rep)) | |
767 | #define UC ustring &str(*reinterpret_cast<ustring *>(rep)) | |
768 | static struct UReplaceableCallbacks CollationUCalls_ = { | |
769 | .length = [](const UReplaceable *rep) -> int32_t { CUC; | |
770 | return str.size(); | |
771 | }, | |
772 | ||
773 | .charAt = [](const UReplaceable *rep, int32_t offset) -> UChar { CUC; | |
774 | //fprintf(stderr, "charAt(%d) : %d\n", offset, str.size()); | |
775 | if (offset >= str.size()) | |
776 | return 0xffff; | |
777 | return str[offset]; | |
778 | }, | |
779 | ||
780 | .char32At = [](const UReplaceable *rep, int32_t offset) -> UChar32 { CUC; | |
781 | //fprintf(stderr, "char32At(%d) : %d\n", offset, str.size()); | |
782 | if (offset >= str.size()) | |
783 | return 0xffff; | |
784 | UChar32 c; | |
785 | U16_GET(str.data(), 0, offset, str.size(), c); | |
786 | return c; | |
787 | }, | |
788 | ||
789 | .replace = [](UReplaceable *rep, int32_t start, int32_t limit, const UChar *text, int32_t length) -> void { UC; | |
790 | //fprintf(stderr, "replace(%d, %d, %d) : %d\n", start, limit, length, str.size()); | |
791 | str.replace(start, limit - start, text, length); | |
792 | }, | |
793 | ||
794 | .extract = [](UReplaceable *rep, int32_t start, int32_t limit, UChar *dst) -> void { UC; | |
795 | //fprintf(stderr, "extract(%d, %d) : %d\n", start, limit, str.size()); | |
796 | str.copy(dst, limit - start, start); | |
797 | }, | |
798 | ||
799 | .copy = [](UReplaceable *rep, int32_t start, int32_t limit, int32_t dest) -> void { UC; | |
800 | //fprintf(stderr, "copy(%d, %d, %d) : %d\n", start, limit, dest, str.size()); | |
801 | str.replace(dest, 0, str, start, limit - start); | |
802 | }, | |
803 | }; | |
aeeb755b | 804 | |
d791dce4 JF |
805 | static CFLocaleRef Locale_; |
806 | static NSArray *Languages_; | |
807 | static CGColorSpaceRef space_; | |
36bb2ca2 | 808 | |
46dbfd32 | 809 | static NSDictionary *SectionMap_; |
b4d89997 | 810 | static NSMutableDictionary *Metadata_; |
7b0ce2da | 811 | static _transient NSMutableDictionary *Settings_; |
7b0ce2da | 812 | static _transient NSMutableDictionary *Packages_; |
b3c8e69c | 813 | static _transient NSMutableDictionary *Values_; |
7b0ce2da | 814 | static _transient NSMutableDictionary *Sections_; |
25c1dafb | 815 | _H<NSMutableDictionary> Sources_; |
33e30380 | 816 | static _transient NSNumber *Version_; |
25c1dafb | 817 | bool Changed_; |
31bc18a7 | 818 | static time_t now_; |
8da60fb7 | 819 | |
f333f6c5 | 820 | bool IsWildcat_; |
f98962e5 | 821 | CGFloat ScreenScale_; |
c138614d | 822 | static NSString *Idiom_; |
407564b5 | 823 | static _H<NSString> Firmware_; |
fd825a2d | 824 | static NSString *Major_; |
5df7ecfb | 825 | |
7b33d201 JF |
826 | static _H<NSMutableDictionary> SessionData_; |
827 | static _H<NSObject> HostConfig_; | |
828 | static _H<NSMutableSet> BridgedHosts_; | |
247bedb6 | 829 | static _H<NSMutableSet> TokenHosts_; |
2e1652a9 | 830 | static _H<NSMutableSet> InsecureHosts_; |
7b33d201 | 831 | static _H<NSMutableSet> PipelinedHosts_; |
5e845121 | 832 | static _H<NSMutableSet> CachedURLs_; |
389133be | 833 | |
e4123ce0 JF |
834 | static NSString *kCydiaProgressEventTypeError = @"Error"; |
835 | static NSString *kCydiaProgressEventTypeInformation = @"Information"; | |
836 | static NSString *kCydiaProgressEventTypeStatus = @"Status"; | |
837 | static NSString *kCydiaProgressEventTypeWarning = @"Warning"; | |
36bb2ca2 | 838 | /* }}} */ |
d791dce4 | 839 | |
36bb2ca2 JF |
840 | /* Display Helpers {{{ */ |
841 | inline float Interpolate(float begin, float end, float fraction) { | |
842 | return (end - begin) * fraction + begin; | |
843 | } | |
2367a917 | 844 | |
8323c1b9 JF |
845 | static inline double Retina(double value) { |
846 | value *= ScreenScale_; | |
847 | value = round(value); | |
848 | value /= ScreenScale_; | |
849 | return value; | |
850 | } | |
851 | ||
852 | static inline CGRect Retina(CGRect value) { | |
853 | value.origin.x *= ScreenScale_; | |
854 | value.origin.y *= ScreenScale_; | |
855 | value.size.width *= ScreenScale_; | |
856 | value.size.height *= ScreenScale_; | |
857 | value = CGRectIntegral(value); | |
858 | value.origin.x /= ScreenScale_; | |
859 | value.origin.y /= ScreenScale_; | |
860 | value.size.width /= ScreenScale_; | |
861 | value.size.height /= ScreenScale_; | |
862 | return value; | |
863 | } | |
864 | ||
1c1dfc2d | 865 | static _finline const char *StripVersion_(const char *version) { |
6a155117 | 866 | const char *colon(strchr(version, ':')); |
673ad3c3 | 867 | return colon == NULL ? version : colon + 1; |
6a155117 JF |
868 | } |
869 | ||
f79a4512 | 870 | NSString *LocalizeSection(NSString *section) { |
b4fd1197 | 871 | static RegEx title_r("(.*?) \\((.*)\\)"); |
9fcbca29 JF |
872 | if (title_r(section)) { |
873 | NSString *parent(title_r[1]); | |
874 | NSString *child(title_r[2]); | |
875 | ||
43f3d7f6 | 876 | return [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), |
9fcbca29 JF |
877 | LocalizeSection(parent), |
878 | LocalizeSection(child) | |
b1ce61ec | 879 | ]; |
9fcbca29 | 880 | } |
b1ce61ec JF |
881 | |
882 | return [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
f79a4512 JF |
883 | } |
884 | ||
4cf4165e JF |
885 | NSString *Simplify(NSString *title) { |
886 | const char *data = [title UTF8String]; | |
393a84a1 | 887 | size_t size = [title lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
4cf4165e | 888 | |
b4fd1197 | 889 | static RegEx square_r("\\[(.*)\\]"); |
7b0ce2da JF |
890 | if (square_r(data, size)) |
891 | return Simplify(square_r[1]); | |
892 | ||
b4fd1197 | 893 | static RegEx paren_r("\\((.*)\\)"); |
7b0ce2da JF |
894 | if (paren_r(data, size)) |
895 | return Simplify(paren_r[1]); | |
896 | ||
b4fd1197 | 897 | static RegEx title_r("(.*?) \\((.*)\\)"); |
4cf4165e | 898 | if (title_r(data, size)) |
7b0ce2da JF |
899 | return Simplify(title_r[1]); |
900 | ||
901 | return title; | |
4cf4165e | 902 | } |
36bb2ca2 JF |
903 | /* }}} */ |
904 | ||
d791dce4 JF |
905 | NSString *GetLastUpdate() { |
906 | NSDate *update = [Metadata_ objectForKey:@"LastUpdate"]; | |
907 | ||
908 | if (update == nil) | |
909 | return UCLocalize("NEVER_OR_UNKNOWN"); | |
910 | ||
911 | CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle); | |
912 | CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update); | |
913 | ||
914 | CFRelease(formatter); | |
915 | ||
916 | return [(NSString *) formatted autorelease]; | |
917 | } | |
918 | ||
6d9712c4 | 919 | bool isSectionVisible(NSString *section) { |
45447dc3 | 920 | NSDictionary *metadata([Sections_ objectForKey:(section ?: @"")]); |
677b8415 | 921 | NSNumber *hidden(metadata == nil ? nil : [metadata objectForKey:@"Hidden"]); |
6d9712c4 JF |
922 | return hidden == nil || ![hidden boolValue]; |
923 | } | |
924 | ||
c31d7cdc | 925 | static NSObject *CYIOGetValue(const char *path, NSString *property) { |
947a8eef JF |
926 | io_registry_entry_t entry(IORegistryEntryFromPath(kIOMasterPortDefault, path)); |
927 | if (entry == MACH_PORT_NULL) | |
928 | return nil; | |
929 | ||
930 | CFTypeRef value(IORegistryEntryCreateCFProperty(entry, (CFStringRef) property, kCFAllocatorDefault, 0)); | |
931 | IOObjectRelease(entry); | |
932 | ||
933 | if (value == NULL) | |
934 | return nil; | |
935 | return [(id) value autorelease]; | |
936 | } | |
937 | ||
c31d7cdc | 938 | static NSString *CYHex(NSData *data, bool reverse = false) { |
947a8eef JF |
939 | if (data == nil) |
940 | return nil; | |
941 | ||
942 | size_t length([data length]); | |
943 | uint8_t bytes[length]; | |
944 | [data getBytes:bytes]; | |
945 | ||
946 | char string[length * 2 + 1]; | |
947 | for (size_t i(0); i != length; ++i) | |
be45a862 | 948 | sprintf(string + i * 2, "%.2x", bytes[reverse ? length - i - 1 : i]); |
947a8eef JF |
949 | |
950 | return [NSString stringWithUTF8String:string]; | |
951 | } | |
952 | ||
9cb0bff2 GP |
953 | @class Cydia; |
954 | ||
d36e83a3 | 955 | /* Delegate Prototypes {{{ */ |
36bb2ca2 JF |
956 | @class Package; |
957 | @class Source; | |
6915b806 | 958 | @class CydiaProgressEvent; |
36bb2ca2 | 959 | |
6915b806 | 960 | @protocol DatabaseDelegate |
5a09ae08 | 961 | - (void) repairWithSelector:(SEL)selector; |
8b29f8e6 | 962 | - (void) setConfigurationData:(NSString *)data; |
6915b806 | 963 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task; |
8b29f8e6 JF |
964 | @end |
965 | ||
f6e13561 | 966 | @class CYPackageController; |
f79a4512 | 967 | |
21ac0ce2 JF |
968 | @protocol SourceDelegate |
969 | - (void) setFetch:(NSNumber *)fetch; | |
970 | @end | |
971 | ||
972 | @protocol FetchDelegate | |
973 | - (bool) isSourceCancelled; | |
974 | - (void) startSourceFetch:(NSString *)uri; | |
975 | - (void) stopSourceFetch:(NSString *)uri; | |
976 | @end | |
977 | ||
36bb2ca2 | 978 | @protocol CydiaDelegate |
2925cbba | 979 | - (void) returnToCydia; |
9dd3045d | 980 | - (void) saveState; |
54043703 JF |
981 | - (void) retainNetworkActivityIndicator; |
982 | - (void) releaseNetworkActivityIndicator; | |
dc63e78f | 983 | - (void) clearPackage:(Package *)package; |
36bb2ca2 | 984 | - (void) installPackage:(Package *)package; |
77801ff1 | 985 | - (void) installPackages:(NSArray *)packages; |
36bb2ca2 | 986 | - (void) removePackage:(Package *)package; |
c21004b9 JF |
987 | - (void) beginUpdate; |
988 | - (BOOL) updating; | |
e67ebdad | 989 | - (bool) requestUpdate; |
36bb2ca2 | 990 | - (void) distUpgrade; |
fed0d010 | 991 | - (void) loadData; |
6d9712c4 | 992 | - (void) updateData; |
392ff7e4 | 993 | - (void) _saveConfig; |
7b0ce2da | 994 | - (void) syncData; |
33e30380 | 995 | - (void) addSource:(NSDictionary *)source; |
93460555 | 996 | - (void) addTrivialSource:(NSString *)href; |
7b0ce2da | 997 | - (UIProgressHUD *) addProgressHUD; |
d061f4ba | 998 | - (void) removeProgressHUD:(UIProgressHUD *)hud; |
9daa7f25 | 999 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item; |
4ba8f30a | 1000 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 1001 | @end |
d36e83a3 | 1002 | /* }}} */ |
b4d89997 | 1003 | |
21ac0ce2 JF |
1004 | /* CancelStatus {{{ */ |
1005 | class CancelStatus : | |
dc5812ec JF |
1006 | public pkgAcquireStatus |
1007 | { | |
1008 | private: | |
fc470c15 | 1009 | bool cancelled_; |
dc5812ec JF |
1010 | |
1011 | public: | |
21ac0ce2 | 1012 | CancelStatus() : |
fc470c15 | 1013 | cancelled_(false) |
dc5812ec JF |
1014 | { |
1015 | } | |
1016 | ||
dc5812ec JF |
1017 | virtual bool MediaChange(std::string media, std::string drive) { |
1018 | return false; | |
1019 | } | |
1020 | ||
10387810 JF |
1021 | virtual void IMSHit(pkgAcquire::ItemDesc &desc) { |
1022 | Done(desc); | |
dc5812ec JF |
1023 | } |
1024 | ||
21ac0ce2 JF |
1025 | virtual bool Pulse_(pkgAcquire *Owner) = 0; |
1026 | ||
1027 | virtual bool Pulse(pkgAcquire *Owner) { | |
1028 | if (pkgAcquireStatus::Pulse(Owner) && Pulse_(Owner)) | |
1029 | return true; | |
1030 | else { | |
1031 | cancelled_ = true; | |
1032 | return false; | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | _finline bool WasCancelled() const { | |
1037 | return cancelled_; | |
1038 | } | |
1039 | }; | |
1040 | /* }}} */ | |
1041 | /* DelegateStatus {{{ */ | |
1042 | class CydiaStatus : | |
1043 | public CancelStatus | |
1044 | { | |
1045 | private: | |
1046 | _transient NSObject<ProgressDelegate> *delegate_; | |
1047 | ||
1048 | public: | |
1049 | CydiaStatus() : | |
1050 | delegate_(nil) | |
1051 | { | |
1052 | } | |
1053 | ||
1054 | void setDelegate(NSObject<ProgressDelegate> *delegate) { | |
1055 | delegate_ = delegate; | |
1056 | } | |
1057 | ||
10387810 JF |
1058 | virtual void Fetch(pkgAcquire::ItemDesc &desc) { |
1059 | NSString *name([NSString stringWithUTF8String:desc.ShortDesc.c_str()]); | |
1060 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), name] ofType:kCydiaProgressEventTypeStatus forItemDesc:desc]); | |
6915b806 | 1061 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1062 | } |
1063 | ||
10387810 JF |
1064 | virtual void Done(pkgAcquire::ItemDesc &desc) { |
1065 | NSString *name([NSString stringWithUTF8String:desc.ShortDesc.c_str()]); | |
1066 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:Colon_, UCLocalize("DONE"), name] ofType:kCydiaProgressEventTypeStatus forItemDesc:desc]); | |
d35bcbbf | 1067 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1068 | } |
1069 | ||
10387810 | 1070 | virtual void Fail(pkgAcquire::ItemDesc &desc) { |
1d80f6b9 | 1071 | if ( |
10387810 JF |
1072 | desc.Owner->Status == pkgAcquire::Item::StatIdle || |
1073 | desc.Owner->Status == pkgAcquire::Item::StatDone | |
1d80f6b9 JF |
1074 | ) |
1075 | return; | |
1076 | ||
10387810 | 1077 | std::string &error(desc.Owner->ErrorText); |
affeffc7 JF |
1078 | if (error.empty()) |
1079 | return; | |
1080 | ||
10387810 | 1081 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError forItemDesc:desc]); |
6915b806 | 1082 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1083 | } |
1084 | ||
21ac0ce2 | 1085 | virtual bool Pulse_(pkgAcquire *Owner) { |
bcbac8f7 | 1086 | double percent( |
2367a917 JF |
1087 | double(CurrentBytes + CurrentItems) / |
1088 | double(TotalBytes + TotalItems) | |
1089 | ); | |
1090 | ||
bcbac8f7 JF |
1091 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:[NSDictionary dictionaryWithObjectsAndKeys: |
1092 | [NSNumber numberWithDouble:percent], @"Percent", | |
1093 | ||
1094 | [NSNumber numberWithDouble:CurrentBytes], @"Current", | |
1095 | [NSNumber numberWithDouble:TotalBytes], @"Total", | |
1096 | [NSNumber numberWithDouble:CurrentCPS], @"Speed", | |
1097 | nil] waitUntilDone:YES]; | |
1098 | ||
21ac0ce2 | 1099 | return ![delegate_ isProgressCancelled]; |
dc5812ec JF |
1100 | } |
1101 | ||
1102 | virtual void Start() { | |
0210c2b5 | 1103 | pkgAcquireStatus::Start(); |
aaae308d | 1104 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:YES]; |
dc5812ec JF |
1105 | } |
1106 | ||
1107 | virtual void Stop() { | |
0210c2b5 | 1108 | pkgAcquireStatus::Stop(); |
aaae308d | 1109 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:YES]; |
bcbac8f7 | 1110 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:nil waitUntilDone:YES]; |
dc5812ec JF |
1111 | } |
1112 | }; | |
1113 | /* }}} */ | |
36bb2ca2 | 1114 | /* Database Interface {{{ */ |
68d927e2 JF |
1115 | typedef std::map< unsigned long, _H<Source> > SourceMap; |
1116 | ||
36bb2ca2 | 1117 | @interface Database : NSObject { |
f79a4512 | 1118 | NSZone *zone_; |
64edd9df | 1119 | CYPool pool_; |
f79a4512 | 1120 | |
a1440b10 JF |
1121 | unsigned era_; |
1122 | ||
36bb2ca2 | 1123 | pkgCacheFile cache_; |
5a09ae08 | 1124 | pkgDepCache::Policy *policy_; |
36bb2ca2 JF |
1125 | pkgRecords *records_; |
1126 | pkgProblemResolver *resolver_; | |
1127 | pkgAcquire *fetcher_; | |
1128 | FileFd *lock_; | |
1129 | SPtr<pkgPackageManager> manager_; | |
1130 | pkgSourceList *list_; | |
5f6bff8c | 1131 | |
34f70f5d | 1132 | SourceMap sourceMap_; |
7b33d201 | 1133 | _H<NSMutableArray> sourceList_; |
34f70f5d | 1134 | |
077e9d90 | 1135 | CFMutableArrayRef packages_; |
b4d89997 | 1136 | |
6915b806 JF |
1137 | _transient NSObject<DatabaseDelegate> *delegate_; |
1138 | _transient NSObject<ProgressDelegate> *progress_; | |
1139 | ||
21ac0ce2 | 1140 | CydiaStatus status_; |
8b29f8e6 | 1141 | |
77fcccaf | 1142 | int cydiafd_; |
36bb2ca2 | 1143 | int statusfd_; |
8b29f8e6 | 1144 | FILE *input_; |
fe33a23e JF |
1145 | |
1146 | std::map<const char *, _H<NSString> > sections_; | |
dc5812ec JF |
1147 | } |
1148 | ||
770f2a8e | 1149 | + (Database *) sharedInstance; |
a1440b10 | 1150 | - (unsigned) era; |
770f2a8e | 1151 | |
77fcccaf | 1152 | - (void) _readCydia:(NSNumber *)fd; |
36bb2ca2 JF |
1153 | - (void) _readStatus:(NSNumber *)fd; |
1154 | - (void) _readOutput:(NSNumber *)fd; | |
2367a917 | 1155 | |
8b29f8e6 JF |
1156 | - (FILE *) input; |
1157 | ||
36bb2ca2 | 1158 | - (Package *) packageWithName:(NSString *)name; |
dc5812ec | 1159 | |
36bb2ca2 | 1160 | - (pkgCacheFile &) cache; |
5a09ae08 | 1161 | - (pkgDepCache::Policy *) policy; |
36bb2ca2 JF |
1162 | - (pkgRecords *) records; |
1163 | - (pkgProblemResolver *) resolver; | |
1164 | - (pkgAcquire &) fetcher; | |
a3328c28 | 1165 | - (pkgSourceList &) list; |
36bb2ca2 | 1166 | - (NSArray *) packages; |
7b0ce2da | 1167 | - (NSArray *) sources; |
d669236d | 1168 | - (Source *) sourceWithKey:(NSString *)key; |
4ba8f30a | 1169 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 1170 | |
5a09ae08 | 1171 | - (void) configure; |
670a0494 | 1172 | - (bool) prepare; |
36bb2ca2 | 1173 | - (void) perform; |
670a0494 | 1174 | - (bool) upgrade; |
36bb2ca2 JF |
1175 | - (void) update; |
1176 | ||
21ac0ce2 | 1177 | - (void) updateWithStatus:(CancelStatus &)status; |
36bb2ca2 | 1178 | |
6915b806 JF |
1179 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate; |
1180 | ||
1181 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate; | |
1182 | - (NSObject<ProgressDelegate> *) progressDelegate; | |
1183 | ||
7376b55c | 1184 | - (Source *) getSource:(pkgCache::PkgFileIterator)file; |
21ac0ce2 | 1185 | - (void) setFetch:(bool)fetch forURI:(const char *)uri; |
9ed626f1 | 1186 | - (void) resetFetch; |
fe33a23e JF |
1187 | |
1188 | - (NSString *) mappedSectionForPointer:(const char *)pointer; | |
1189 | ||
670a0494 JF |
1190 | @end |
1191 | /* }}} */ | |
21ac0ce2 JF |
1192 | /* SourceStatus {{{ */ |
1193 | class SourceStatus : | |
1194 | public CancelStatus | |
1195 | { | |
1196 | private: | |
1197 | _transient NSObject<FetchDelegate> *delegate_; | |
1198 | _transient Database *database_; | |
e6e180a4 | 1199 | std::set<std::string> fetches_; |
21ac0ce2 JF |
1200 | |
1201 | public: | |
1202 | SourceStatus(NSObject<FetchDelegate> *delegate, Database *database) : | |
1203 | delegate_(delegate), | |
1204 | database_(database) | |
1205 | { | |
1206 | } | |
1207 | ||
e6e180a4 JF |
1208 | void Set(bool fetch, const std::string &uri) { |
1209 | if (fetch) { | |
1210 | if (!fetches_.insert(uri).second) | |
1211 | return; | |
1212 | } else { | |
1213 | if (fetches_.erase(uri) == 0) | |
1214 | return; | |
1215 | } | |
1216 | ||
1217 | //printf("Set(%s, %s)\n", fetch ? "true" : "false", uri.c_str()); | |
1218 | [database_ setFetch:fetch forURI:uri.c_str()]; | |
1219 | } | |
1220 | ||
1221 | _finline void Set(bool fetch, pkgAcquire::Item *item) { | |
1222 | /*unsigned long ID(fetch ? 1 : 0); | |
1223 | if (item->ID == ID) | |
1224 | return; | |
1225 | item->ID = ID;*/ | |
1226 | Set(fetch, item->DescURI()); | |
1227 | } | |
1228 | ||
1229 | void Log(const char *tag, pkgAcquire::Item *item) { | |
1230 | //printf("%s(%s) S:%u Q:%u\n", tag, item->DescURI().c_str(), item->Status, item->QueueCounter); | |
21ac0ce2 JF |
1231 | } |
1232 | ||
1233 | virtual void Fetch(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1234 | Log("Fetch", desc.Owner); |
1235 | Set(true, desc.Owner); | |
21ac0ce2 JF |
1236 | } |
1237 | ||
1238 | virtual void Done(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1239 | Log("Done", desc.Owner); |
1240 | Set(false, desc.Owner); | |
21ac0ce2 JF |
1241 | } |
1242 | ||
1243 | virtual void Fail(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1244 | Log("Fail", desc.Owner); |
1245 | Set(false, desc.Owner); | |
21ac0ce2 JF |
1246 | } |
1247 | ||
1248 | virtual bool Pulse_(pkgAcquire *Owner) { | |
e6e180a4 JF |
1249 | std::set<std::string> fetches; |
1250 | for (pkgAcquire::ItemCIterator item(Owner->ItemsBegin()); item != Owner->ItemsEnd(); ++item) { | |
1251 | bool fetch; | |
1252 | if ((*item)->QueueCounter == 0) | |
1253 | fetch = false; | |
1254 | else switch ((*item)->Status) { | |
1255 | case pkgAcquire::Item::StatFetching: | |
1256 | fetches.insert((*item)->DescURI()); | |
1257 | fetch = true; | |
1258 | break; | |
1259 | ||
1260 | default: | |
1261 | fetch = false; | |
1262 | break; | |
1263 | } | |
1264 | ||
1265 | Log(fetch ? "Pulse<true>" : "Pulse<false>", *item); | |
1266 | Set(fetch, *item); | |
1267 | } | |
1268 | ||
1269 | std::vector<std::string> stops; | |
1270 | std::set_difference(fetches_.begin(), fetches_.end(), fetches.begin(), fetches.end(), std::back_insert_iterator<std::vector<std::string>>(stops)); | |
1271 | for (std::vector<std::string>::const_iterator stop(stops.begin()); stop != stops.end(); ++stop) { | |
1272 | //printf("Stop(%s)\n", stop->c_str()); | |
1273 | Set(false, *stop); | |
1274 | } | |
1275 | ||
21ac0ce2 JF |
1276 | return ![delegate_ isSourceCancelled]; |
1277 | } | |
9ed626f1 JF |
1278 | |
1279 | virtual void Stop() { | |
1280 | pkgAcquireStatus::Stop(); | |
1281 | [database_ resetFetch]; | |
1282 | } | |
21ac0ce2 JF |
1283 | }; |
1284 | /* }}} */ | |
6915b806 JF |
1285 | /* ProgressEvent Implementation {{{ */ |
1286 | @implementation CydiaProgressEvent | |
670a0494 | 1287 | |
6915b806 JF |
1288 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type { |
1289 | return [[[CydiaProgressEvent alloc] initWithMessage:message ofType:type] autorelease]; | |
670a0494 JF |
1290 | } |
1291 | ||
6915b806 JF |
1292 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forPackage:(NSString *)package { |
1293 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); | |
1294 | [event setPackage:package]; | |
1295 | return event; | |
670a0494 JF |
1296 | } |
1297 | ||
10387810 | 1298 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forItemDesc:(pkgAcquire::ItemDesc &)desc { |
c57867ea JF |
1299 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); |
1300 | ||
10387810 | 1301 | NSString *description([NSString stringWithUTF8String:desc.Description.c_str()]); |
c57867ea JF |
1302 | NSArray *fields([description componentsSeparatedByString:@" "]); |
1303 | [event setItem:fields]; | |
1304 | ||
1305 | if ([fields count] > 3) { | |
1306 | [event setPackage:[fields objectAtIndex:2]]; | |
1307 | [event setVersion:[fields objectAtIndex:3]]; | |
1308 | } | |
1309 | ||
10387810 | 1310 | [event setURL:[NSString stringWithUTF8String:desc.URI.c_str()]]; |
c57867ea JF |
1311 | |
1312 | return event; | |
1313 | } | |
1314 | ||
4ede7a3f JF |
1315 | + (NSArray *) _attributeKeys { |
1316 | return [NSArray arrayWithObjects: | |
c57867ea | 1317 | @"item", |
4ede7a3f JF |
1318 | @"message", |
1319 | @"package", | |
1320 | @"type", | |
c57867ea JF |
1321 | @"url", |
1322 | @"version", | |
4ede7a3f JF |
1323 | nil]; |
1324 | } | |
1325 | ||
1326 | - (NSArray *) attributeKeys { | |
1327 | return [[self class] _attributeKeys]; | |
1328 | } | |
1329 | ||
1330 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1331 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1332 | } | |
1333 | ||
6915b806 JF |
1334 | - (id) initWithMessage:(NSString *)message ofType:(NSString *)type { |
1335 | if ((self = [super init]) != nil) { | |
1336 | message_ = message; | |
1337 | type_ = type; | |
1338 | } return self; | |
670a0494 JF |
1339 | } |
1340 | ||
6915b806 JF |
1341 | - (NSString *) message { |
1342 | return message_; | |
1343 | } | |
6067f1b8 | 1344 | |
6915b806 JF |
1345 | - (NSString *) type { |
1346 | return type_; | |
1347 | } | |
1348 | ||
c57867ea JF |
1349 | - (NSArray *) item { |
1350 | return (id) item_ ?: [NSNull null]; | |
1351 | } | |
1352 | ||
1353 | - (void) setItem:(NSArray *)item { | |
1354 | item_ = item; | |
6915b806 JF |
1355 | } |
1356 | ||
c57867ea JF |
1357 | - (NSString *) package { |
1358 | return (id) package_ ?: [NSNull null]; | |
6915b806 JF |
1359 | } |
1360 | ||
1361 | - (void) setPackage:(NSString *)package { | |
1362 | package_ = package; | |
1363 | } | |
1364 | ||
c57867ea JF |
1365 | - (NSString *) url { |
1366 | return (id) url_ ?: [NSNull null]; | |
1367 | } | |
1368 | ||
1369 | - (void) setURL:(NSString *)url { | |
1370 | url_ = url; | |
1371 | } | |
1372 | ||
1373 | - (void) setVersion:(NSString *)version { | |
1374 | version_ = version; | |
1375 | } | |
1376 | ||
1377 | - (NSString *) version { | |
1378 | return (id) version_ ?: [NSNull null]; | |
1379 | } | |
1380 | ||
6915b806 JF |
1381 | - (NSString *) compound:(NSString *)value { |
1382 | if (value != nil) { | |
1383 | NSString *mode(nil); { | |
1384 | NSString *type([self type]); | |
389133be | 1385 | if ([type isEqualToString:kCydiaProgressEventTypeError]) |
6915b806 | 1386 | mode = UCLocalize("ERROR"); |
389133be | 1387 | else if ([type isEqualToString:kCydiaProgressEventTypeWarning]) |
6915b806 JF |
1388 | mode = UCLocalize("WARNING"); |
1389 | } | |
1390 | ||
1391 | if (mode != nil) | |
1392 | value = [NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), mode, value]; | |
1393 | } | |
1394 | ||
1395 | return value; | |
1396 | } | |
1397 | ||
1398 | - (NSString *) compoundMessage { | |
1399 | return [self compound:[self message]]; | |
1400 | } | |
1401 | ||
1402 | - (NSString *) compoundTitle { | |
1403 | NSString *title; | |
1404 | ||
83b78e5f | 1405 | if (package_ == nil) |
6915b806 | 1406 | title = nil; |
83b78e5f JF |
1407 | else if (Package *package = [[Database sharedInstance] packageWithName:package_]) |
1408 | title = [package name]; | |
1409 | else | |
1410 | title = package_; | |
6915b806 JF |
1411 | |
1412 | return [self compound:title]; | |
670a0494 JF |
1413 | } |
1414 | ||
dc5812ec | 1415 | @end |
36bb2ca2 | 1416 | /* }}} */ |
dc5812ec | 1417 | |
94b0b3e5 JF |
1418 | // Cytore Definitions {{{ |
1419 | struct PackageValue : | |
1420 | Cytore::Block | |
1421 | { | |
94b0b3e5 JF |
1422 | Cytore::Offset<PackageValue> next_; |
1423 | ||
1424 | uint32_t index_ : 23; | |
1425 | uint32_t subscribed_ : 1; | |
1426 | uint32_t : 8; | |
1427 | ||
1428 | int32_t first_; | |
1429 | int32_t last_; | |
1430 | ||
1431 | uint16_t vhash_; | |
1432 | uint16_t nhash_; | |
1433 | ||
1434 | char version_[8]; | |
1435 | char name_[]; | |
1436 | }; | |
1437 | ||
1438 | struct MetaValue : | |
1439 | Cytore::Block | |
1440 | { | |
9f357d11 | 1441 | uint32_t active_; |
94b0b3e5 JF |
1442 | Cytore::Offset<PackageValue> packages_[1 << 16]; |
1443 | }; | |
1444 | ||
1445 | static Cytore::File<MetaValue> MetaFile_; | |
1446 | // }}} | |
1447 | // Cytore Helper Functions {{{ | |
c65611b9 | 1448 | static PackageValue *PackageFind(const char *name, size_t length, bool *fail = NULL) { |
94b0b3e5 JF |
1449 | SplitHash nhash = { hashlittle(name, length) }; |
1450 | ||
1451 | PackageValue *metadata; | |
1452 | ||
1453 | Cytore::Offset<PackageValue> *offset(&MetaFile_->packages_[nhash.u16[0]]); | |
ac5f7cb3 | 1454 | for (;; offset = &metadata->next_) { if (offset->IsNull()) { |
94b0b3e5 JF |
1455 | *offset = MetaFile_.New<PackageValue>(length + 1); |
1456 | metadata = &MetaFile_.Get(*offset); | |
1457 | ||
c65611b9 JF |
1458 | if (metadata == NULL) { |
1459 | if (fail != NULL) | |
1460 | *fail = true; | |
1461 | ||
1462 | metadata = new PackageValue(); | |
1463 | memset(metadata, 0, sizeof(*metadata)); | |
1464 | } | |
1465 | ||
ac5f7cb3 JF |
1466 | memcpy(metadata->name_, name, length); |
1467 | metadata->name_[length] = '\0'; | |
94b0b3e5 JF |
1468 | metadata->nhash_ = nhash.u16[1]; |
1469 | } else { | |
1470 | metadata = &MetaFile_.Get(*offset); | |
ac5f7cb3 JF |
1471 | if (metadata->nhash_ != nhash.u16[1]) |
1472 | continue; | |
1473 | if (strncmp(metadata->name_, name, length) != 0) | |
1474 | continue; | |
1475 | if (metadata->name_[length] != '\0') | |
1476 | continue; | |
1477 | } break; } | |
94b0b3e5 | 1478 | |
94b0b3e5 JF |
1479 | return metadata; |
1480 | } | |
1481 | ||
1482 | static void PackageImport(const void *key, const void *value, void *context) { | |
c65611b9 JF |
1483 | bool &fail(*reinterpret_cast<bool *>(context)); |
1484 | ||
94b0b3e5 JF |
1485 | char buffer[1024]; |
1486 | if (!CFStringGetCString((CFStringRef) key, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { | |
1487 | NSLog(@"failed to import package %@", key); | |
1488 | return; | |
1489 | } | |
1490 | ||
c65611b9 | 1491 | PackageValue *metadata(PackageFind(buffer, strlen(buffer), &fail)); |
94b0b3e5 JF |
1492 | NSDictionary *package((NSDictionary *) value); |
1493 | ||
1494 | if (NSNumber *subscribed = [package objectForKey:@"IsSubscribed"]) | |
2f856365 | 1495 | if ([subscribed boolValue] && !metadata->subscribed_) |
94b0b3e5 JF |
1496 | metadata->subscribed_ = true; |
1497 | ||
1498 | if (NSDate *date = [package objectForKey:@"FirstSeen"]) { | |
1499 | time_t time([date timeIntervalSince1970]); | |
1500 | if (metadata->first_ > time || metadata->first_ == 0) | |
1501 | metadata->first_ = time; | |
1502 | } | |
1503 | ||
2f856365 JF |
1504 | NSDate *date([package objectForKey:@"LastSeen"]); |
1505 | NSString *version([package objectForKey:@"LastVersion"]); | |
5a933937 | 1506 | |
2f856365 | 1507 | if (date != nil && version != nil) { |
94b0b3e5 | 1508 | time_t time([date timeIntervalSince1970]); |
2f856365 | 1509 | if (metadata->last_ < time || metadata->last_ == 0) |
5a933937 JF |
1510 | if (CFStringGetCString((CFStringRef) version, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { |
1511 | size_t length(strlen(buffer)); | |
1512 | uint16_t vhash(hashlittle(buffer, length)); | |
94b0b3e5 | 1513 | |
5a933937 JF |
1514 | size_t capped(std::min<size_t>(8, length)); |
1515 | char *latest(buffer + length - capped); | |
1516 | ||
1517 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); | |
1518 | metadata->vhash_ = vhash; | |
2f856365 JF |
1519 | |
1520 | metadata->last_ = time; | |
5a933937 | 1521 | } |
2f856365 | 1522 | } |
94b0b3e5 JF |
1523 | } |
1524 | // }}} | |
1525 | ||
36bb2ca2 JF |
1526 | /* Source Class {{{ */ |
1527 | @interface Source : NSObject { | |
aec64d46 JF |
1528 | unsigned era_; |
1529 | Database *database_; | |
1530 | metaIndex *index_; | |
1531 | ||
6204f56a | 1532 | CYString depiction_; |
f9f6d9e8 JF |
1533 | CYString description_; |
1534 | CYString label_; | |
1535 | CYString origin_; | |
1536 | CYString support_; | |
dc5812ec | 1537 | |
f9f6d9e8 JF |
1538 | CYString uri_; |
1539 | CYString distribution_; | |
1540 | CYString type_; | |
8252b666 | 1541 | CYString base_; |
f9f6d9e8 | 1542 | CYString version_; |
2367a917 | 1543 | |
c08c8943 JF |
1544 | _H<NSString> host_; |
1545 | _H<NSString> authority_; | |
f9f6d9e8 JF |
1546 | |
1547 | CYString defaultIcon_; | |
dc5812ec | 1548 | |
4cc9e99a | 1549 | _H<NSMutableDictionary> record_; |
36bb2ca2 | 1550 | BOOL trusted_; |
21ac0ce2 JF |
1551 | |
1552 | std::set<std::string> fetches_; | |
1553 | std::set<std::string> files_; | |
1554 | _transient NSObject<SourceDelegate> *delegate_; | |
dc5812ec JF |
1555 | } |
1556 | ||
64edd9df | 1557 | - (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool; |
dc5812ec | 1558 | |
90ba4f86 | 1559 | - (NSComparisonResult) compareByName:(Source *)source; |
7b0ce2da | 1560 | |
6204f56a | 1561 | - (NSString *) depictionForPackage:(NSString *)package; |
dc63e78f JF |
1562 | - (NSString *) supportForPackage:(NSString *)package; |
1563 | ||
0c28a403 | 1564 | - (metaIndex *) metaIndex; |
7b0ce2da | 1565 | - (NSDictionary *) record; |
36bb2ca2 | 1566 | - (BOOL) trusted; |
dc5812ec | 1567 | |
7bd76e97 | 1568 | - (NSString *) rooturi; |
36bb2ca2 JF |
1569 | - (NSString *) distribution; |
1570 | - (NSString *) type; | |
8252b666 | 1571 | |
7b0ce2da JF |
1572 | - (NSString *) key; |
1573 | - (NSString *) host; | |
36bb2ca2 | 1574 | |
7b0ce2da | 1575 | - (NSString *) name; |
8d262908 | 1576 | - (NSString *) shortDescription; |
36bb2ca2 JF |
1577 | - (NSString *) label; |
1578 | - (NSString *) origin; | |
1579 | - (NSString *) version; | |
dc5812ec | 1580 | |
36bb2ca2 | 1581 | - (NSString *) defaultIcon; |
7bd76e97 | 1582 | - (NSURL *) iconURL; |
7b0ce2da | 1583 | |
21ac0ce2 | 1584 | - (void) setFetch:(bool)fetch forURI:(const char *)uri; |
9ed626f1 | 1585 | - (void) resetFetch; |
21ac0ce2 | 1586 | |
dc5812ec | 1587 | @end |
dc5812ec | 1588 | |
36bb2ca2 | 1589 | @implementation Source |
dc5812ec | 1590 | |
33e30380 JF |
1591 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
1592 | if (false); | |
1593 | else if (selector == @selector(addSection:)) | |
1594 | return @"addSection"; | |
aec64d46 JF |
1595 | else if (selector == @selector(getField:)) |
1596 | return @"getField"; | |
33e30380 JF |
1597 | else if (selector == @selector(removeSection:)) |
1598 | return @"removeSection"; | |
1599 | else if (selector == @selector(remove)) | |
1600 | return @"remove"; | |
1601 | else | |
1602 | return nil; | |
1603 | } | |
1604 | ||
1605 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
1606 | return [self webScriptNameForSelector:selector] == nil; | |
1607 | } | |
1608 | ||
6f1a15d9 | 1609 | + (NSArray *) _attributeKeys { |
e58ff941 | 1610 | return [NSArray arrayWithObjects: |
7bd76e97 | 1611 | @"baseuri", |
e58ff941 JF |
1612 | @"distribution", |
1613 | @"host", | |
1614 | @"key", | |
7bd76e97 | 1615 | @"iconuri", |
e58ff941 JF |
1616 | @"label", |
1617 | @"name", | |
1618 | @"origin", | |
7bd76e97 | 1619 | @"rooturi", |
33e30380 | 1620 | @"sections", |
8d262908 | 1621 | @"shortDescription", |
e58ff941 JF |
1622 | @"trusted", |
1623 | @"type", | |
e58ff941 JF |
1624 | @"version", |
1625 | nil]; | |
6f1a15d9 JF |
1626 | } |
1627 | ||
1628 | - (NSArray *) attributeKeys { | |
1629 | return [[self class] _attributeKeys]; | |
1630 | } | |
1631 | ||
1632 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1633 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1634 | } | |
1635 | ||
0c28a403 JF |
1636 | - (metaIndex *) metaIndex { |
1637 | return index_; | |
1638 | } | |
1639 | ||
64edd9df | 1640 | - (void) setMetaIndex:(metaIndex *)index inPool:(CYPool *)pool { |
a3328c28 JF |
1641 | trusted_ = index->IsTrusted(); |
1642 | ||
f9f6d9e8 JF |
1643 | uri_.set(pool, index->GetURI()); |
1644 | distribution_.set(pool, index->GetDist()); | |
1645 | type_.set(pool, index->GetType()); | |
a3328c28 JF |
1646 | |
1647 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index)); | |
1648 | if (dindex != NULL) { | |
21ac0ce2 | 1649 | std::string file(dindex->MetaIndexURI("")); |
21ac0ce2 JF |
1650 | base_.set(pool, file); |
1651 | ||
3f88f205 | 1652 | pkgAcquire acquire; |
1a83afc6 | 1653 | _profile(Source$setMetaIndex$GetIndexes) |
3f88f205 | 1654 | dindex->GetIndexes(&acquire, true); |
1a83afc6 JF |
1655 | _end |
1656 | _profile(Source$setMetaIndex$DescURI) | |
3f88f205 JF |
1657 | for (pkgAcquire::ItemIterator item(acquire.ItemsBegin()); item != acquire.ItemsEnd(); item++) { |
1658 | std::string file((*item)->DescURI()); | |
1659 | files_.insert(file); | |
1660 | if (file.length() < sizeof("Packages.bz2") || file.substr(file.length() - sizeof("Packages.bz2")) != "/Packages.bz2") | |
1661 | continue; | |
1662 | file = file.substr(0, file.length() - 4); | |
21ac0ce2 JF |
1663 | files_.insert(file); |
1664 | files_.insert(file + ".gz"); | |
21ac0ce2 | 1665 | files_.insert(file + "Index"); |
3f88f205 | 1666 | } |
1a83afc6 | 1667 | _end |
8252b666 | 1668 | |
18873623 JF |
1669 | FileFd fd; |
1670 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) | |
1671 | _error->Discard(); | |
1672 | else { | |
1673 | pkgTagFile tags(&fd); | |
f9f6d9e8 | 1674 | |
18873623 JF |
1675 | pkgTagSection section; |
1676 | tags.Step(section); | |
a3328c28 | 1677 | |
18873623 JF |
1678 | struct { |
1679 | const char *name_; | |
1680 | CYString *value_; | |
1681 | } names[] = { | |
1682 | {"default-icon", &defaultIcon_}, | |
6204f56a | 1683 | {"depiction", &depiction_}, |
18873623 JF |
1684 | {"description", &description_}, |
1685 | {"label", &label_}, | |
1686 | {"origin", &origin_}, | |
1687 | {"support", &support_}, | |
1688 | {"version", &version_}, | |
1689 | }; | |
f9f6d9e8 | 1690 | |
18873623 JF |
1691 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { |
1692 | const char *start, *end; | |
1693 | ||
1694 | if (section.Find(names[i].name_, start, end)) { | |
1695 | CYString &value(*names[i].value_); | |
1696 | value.set(pool, start, end - start); | |
1697 | } | |
f9f6d9e8 | 1698 | } |
36bb2ca2 | 1699 | } |
a3328c28 | 1700 | } |
7b0ce2da | 1701 | |
a3328c28 | 1702 | record_ = [Sources_ objectForKey:[self key]]; |
f9f6d9e8 | 1703 | |
7623f855 JF |
1704 | NSURL *url([NSURL URLWithString:uri_]); |
1705 | ||
1706 | host_ = [url host]; | |
1707 | if (host_ != nil) | |
c08c8943 | 1708 | host_ = [host_ lowercaseString]; |
7623f855 JF |
1709 | |
1710 | if (host_ != nil) | |
dfdb9ae0 | 1711 | authority_ = host_; |
7623f855 JF |
1712 | else |
1713 | authority_ = [url path]; | |
a3328c28 JF |
1714 | } |
1715 | ||
64edd9df | 1716 | - (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool { |
a3328c28 | 1717 | if ((self = [super init]) != nil) { |
aec64d46 JF |
1718 | era_ = [database era]; |
1719 | database_ = database; | |
1720 | index_ = index; | |
1721 | ||
1a83afc6 | 1722 | _profile(Source$initWithMetaIndex$setMetaIndex) |
f9f6d9e8 | 1723 | [self setMetaIndex:index inPool:pool]; |
1a83afc6 | 1724 | _end |
36bb2ca2 | 1725 | } return self; |
2367a917 | 1726 | } |
dc5812ec | 1727 | |
aec64d46 JF |
1728 | - (NSString *) getField:(NSString *)name { |
1729 | @synchronized (database_) { | |
1730 | if ([database_ era] != era_ || index_ == NULL) | |
1731 | return nil; | |
1732 | ||
1733 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index_)); | |
1734 | if (dindex == NULL) | |
1735 | return nil; | |
1736 | ||
1737 | FileFd fd; | |
1738 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) { | |
1739 | _error->Discard(); | |
1740 | return nil; | |
1741 | } | |
1742 | ||
1743 | pkgTagFile tags(&fd); | |
1744 | ||
1745 | pkgTagSection section; | |
1746 | tags.Step(section); | |
1747 | ||
1748 | const char *start, *end; | |
1749 | if (!section.Find([name UTF8String], start, end)) | |
1750 | return (NSString *) [NSNull null]; | |
1751 | ||
1752 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; | |
1753 | } } | |
1754 | ||
90ba4f86 | 1755 | - (NSComparisonResult) compareByName:(Source *)source { |
7b0ce2da JF |
1756 | NSString *lhs = [self name]; |
1757 | NSString *rhs = [source name]; | |
1758 | ||
1759 | if ([lhs length] != 0 && [rhs length] != 0) { | |
1760 | unichar lhc = [lhs characterAtIndex:0]; | |
1761 | unichar rhc = [rhs characterAtIndex:0]; | |
1762 | ||
1763 | if (isalpha(lhc) && !isalpha(rhc)) | |
1764 | return NSOrderedAscending; | |
1765 | else if (!isalpha(lhc) && isalpha(rhc)) | |
1766 | return NSOrderedDescending; | |
1767 | } | |
1768 | ||
68f1828e | 1769 | return [lhs compare:rhs options:LaxCompareOptions_]; |
7b0ce2da JF |
1770 | } |
1771 | ||
6204f56a | 1772 | - (NSString *) depictionForPackage:(NSString *)package { |
89b0ea4a | 1773 | return depiction_.empty() ? nil : [static_cast<id>(depiction_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
6204f56a JF |
1774 | } |
1775 | ||
dc63e78f | 1776 | - (NSString *) supportForPackage:(NSString *)package { |
89b0ea4a | 1777 | return support_.empty() ? nil : [static_cast<id>(support_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
dc63e78f JF |
1778 | } |
1779 | ||
33e30380 JF |
1780 | - (NSArray *) sections { |
1781 | return record_ == nil ? (id) [NSNull null] : [record_ objectForKey:@"Sections"] ?: [NSArray array]; | |
1782 | } | |
1783 | ||
1784 | - (void) _addSection:(NSString *)section { | |
1785 | if (record_ == nil) | |
1786 | return; | |
1787 | else if (NSMutableArray *sections = [record_ objectForKey:@"Sections"]) { | |
1788 | if (![sections containsObject:section]) { | |
1789 | [sections addObject:section]; | |
1790 | Changed_ = true; | |
1791 | } | |
1792 | } else { | |
1793 | [record_ setObject:[NSMutableArray arrayWithObject:section] forKey:@"Sections"]; | |
1794 | Changed_ = true; | |
1795 | } | |
1796 | } | |
1797 | ||
1798 | - (bool) addSection:(NSString *)section { | |
1799 | if (record_ == nil) | |
1800 | return false; | |
1801 | ||
1802 | [self performSelectorOnMainThread:@selector(_addSection:) withObject:section waitUntilDone:NO]; | |
1803 | return true; | |
1804 | } | |
1805 | ||
1806 | - (void) _removeSection:(NSString *)section { | |
1807 | if (record_ == nil) | |
1808 | return; | |
1809 | ||
1810 | if (NSMutableArray *sections = [record_ objectForKey:@"Sections"]) | |
1811 | if ([sections containsObject:section]) { | |
1812 | [sections removeObject:section]; | |
1813 | Changed_ = true; | |
1814 | } | |
1815 | } | |
1816 | ||
1817 | - (bool) removeSection:(NSString *)section { | |
1818 | if (record_ == nil) | |
1819 | return false; | |
1820 | ||
1821 | [self performSelectorOnMainThread:@selector(_removeSection:) withObject:section waitUntilDone:NO]; | |
1822 | return true; | |
1823 | } | |
1824 | ||
1825 | - (void) _remove { | |
1826 | [Sources_ removeObjectForKey:[self key]]; | |
1827 | Changed_ = true; | |
1828 | } | |
1829 | ||
1830 | - (bool) remove { | |
1831 | bool value(record_ != nil); | |
1832 | [self performSelectorOnMainThread:@selector(_remove) withObject:nil waitUntilDone:NO]; | |
1833 | return value; | |
1834 | } | |
1835 | ||
7b0ce2da JF |
1836 | - (NSDictionary *) record { |
1837 | return record_; | |
1838 | } | |
1839 | ||
36bb2ca2 JF |
1840 | - (BOOL) trusted { |
1841 | return trusted_; | |
1842 | } | |
3178d79b | 1843 | |
7bd76e97 | 1844 | - (NSString *) rooturi { |
36bb2ca2 JF |
1845 | return uri_; |
1846 | } | |
ec97ef06 | 1847 | |
36bb2ca2 JF |
1848 | - (NSString *) distribution { |
1849 | return distribution_; | |
1850 | } | |
dc5812ec | 1851 | |
36bb2ca2 JF |
1852 | - (NSString *) type { |
1853 | return type_; | |
dc5812ec JF |
1854 | } |
1855 | ||
7bd76e97 JF |
1856 | - (NSString *) baseuri { |
1857 | return base_.empty() ? nil : (id) base_; | |
1858 | } | |
1859 | ||
1860 | - (NSString *) iconuri { | |
1861 | if (NSString *base = [self baseuri]) | |
1862 | return [base stringByAppendingString:@"CydiaIcon.png"]; | |
1863 | ||
1864 | return nil; | |
1865 | } | |
1866 | ||
1867 | - (NSURL *) iconURL { | |
1868 | if (NSString *uri = [self iconuri]) | |
1869 | return [NSURL URLWithString:uri]; | |
1870 | return nil; | |
8252b666 JF |
1871 | } |
1872 | ||
7b0ce2da | 1873 | - (NSString *) key { |
f9f6d9e8 | 1874 | return [NSString stringWithFormat:@"%@:%@:%@", (NSString *) type_, (NSString *) uri_, (NSString *) distribution_]; |
7b0ce2da JF |
1875 | } |
1876 | ||
1877 | - (NSString *) host { | |
f9f6d9e8 | 1878 | return host_; |
7b0ce2da JF |
1879 | } |
1880 | ||
1881 | - (NSString *) name { | |
c08c8943 | 1882 | return origin_.empty() ? (id) authority_ : origin_; |
7b0ce2da JF |
1883 | } |
1884 | ||
8d262908 | 1885 | - (NSString *) shortDescription { |
36bb2ca2 JF |
1886 | return description_; |
1887 | } | |
b4d89997 | 1888 | |
36bb2ca2 | 1889 | - (NSString *) label { |
c08c8943 | 1890 | return label_.empty() ? (id) authority_ : label_; |
36bb2ca2 | 1891 | } |
3178d79b | 1892 | |
36bb2ca2 JF |
1893 | - (NSString *) origin { |
1894 | return origin_; | |
1895 | } | |
3178d79b | 1896 | |
36bb2ca2 JF |
1897 | - (NSString *) version { |
1898 | return version_; | |
1899 | } | |
2367a917 | 1900 | |
36bb2ca2 JF |
1901 | - (NSString *) defaultIcon { |
1902 | return defaultIcon_; | |
1903 | } | |
2367a917 | 1904 | |
21ac0ce2 JF |
1905 | - (void) setDelegate:(NSObject<SourceDelegate> *)delegate { |
1906 | delegate_ = delegate; | |
1907 | } | |
1908 | ||
1909 | - (bool) fetch { | |
1910 | return !fetches_.empty(); | |
1911 | } | |
1912 | ||
1913 | - (void) setFetch:(bool)fetch forURI:(const char *)uri { | |
1914 | if (!fetch) { | |
1915 | if (fetches_.erase(uri) == 0) | |
1916 | return; | |
1917 | } else if (files_.find(uri) == files_.end()) | |
1918 | return; | |
1919 | else if (!fetches_.insert(uri).second) | |
1920 | return; | |
1921 | ||
1922 | [delegate_ performSelectorOnMainThread:@selector(setFetch:) withObject:[NSNumber numberWithBool:[self fetch]] waitUntilDone:NO]; | |
1923 | } | |
1924 | ||
9ed626f1 JF |
1925 | - (void) resetFetch { |
1926 | fetches_.clear(); | |
1927 | [delegate_ performSelectorOnMainThread:@selector(setFetch:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:NO]; | |
1928 | } | |
1929 | ||
2388b078 JF |
1930 | @end |
1931 | /* }}} */ | |
83682c75 JF |
1932 | /* CydiaOperation Class {{{ */ |
1933 | @interface CydiaOperation : NSObject { | |
7b33d201 JF |
1934 | _H<NSString> operator_; |
1935 | _H<NSString> value_; | |
2388b078 JF |
1936 | } |
1937 | ||
83682c75 JF |
1938 | - (NSString *) operator; |
1939 | - (NSString *) value; | |
2388b078 JF |
1940 | |
1941 | @end | |
1942 | ||
83682c75 | 1943 | @implementation CydiaOperation |
2388b078 | 1944 | |
83682c75 JF |
1945 | - (id) initWithOperator:(const char *)_operator value:(const char *)value { |
1946 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
1947 | operator_ = [NSString stringWithUTF8String:_operator]; |
1948 | value_ = [NSString stringWithUTF8String:value]; | |
83682c75 JF |
1949 | } return self; |
1950 | } | |
1951 | ||
1952 | + (NSArray *) _attributeKeys { | |
1953 | return [NSArray arrayWithObjects: | |
1954 | @"operator", | |
1955 | @"value", | |
1956 | nil]; | |
2388b078 JF |
1957 | } |
1958 | ||
83682c75 JF |
1959 | - (NSArray *) attributeKeys { |
1960 | return [[self class] _attributeKeys]; | |
2388b078 JF |
1961 | } |
1962 | ||
83682c75 JF |
1963 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
1964 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1965 | } | |
1966 | ||
1967 | - (NSString *) operator { | |
1968 | return operator_; | |
1969 | } | |
1970 | ||
1971 | - (NSString *) value { | |
1972 | return value_; | |
1973 | } | |
1974 | ||
1975 | @end | |
1976 | /* }}} */ | |
810c9763 JF |
1977 | /* CydiaClause Class {{{ */ |
1978 | @interface CydiaClause : NSObject { | |
7b33d201 JF |
1979 | _H<NSString> package_; |
1980 | _H<CydiaOperation> version_; | |
83682c75 JF |
1981 | } |
1982 | ||
83682c75 JF |
1983 | - (NSString *) package; |
1984 | - (CydiaOperation *) version; | |
1985 | ||
1986 | @end | |
1987 | ||
810c9763 | 1988 | @implementation CydiaClause |
83682c75 | 1989 | |
83682c75 JF |
1990 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
1991 | if ((self = [super init]) != nil) { | |
7b33d201 | 1992 | package_ = [NSString stringWithUTF8String:dep.TargetPkg().Name()]; |
83682c75 JF |
1993 | |
1994 | if (const char *version = dep.TargetVer()) | |
7b33d201 | 1995 | version_ = [[[CydiaOperation alloc] initWithOperator:dep.CompType() value:version] autorelease]; |
83682c75 | 1996 | else |
7b33d201 | 1997 | version_ = (id) [NSNull null]; |
83682c75 JF |
1998 | } return self; |
1999 | } | |
2000 | ||
2001 | + (NSArray *) _attributeKeys { | |
2002 | return [NSArray arrayWithObjects: | |
2003 | @"package", | |
83682c75 JF |
2004 | @"version", |
2005 | nil]; | |
2006 | } | |
2007 | ||
2008 | - (NSArray *) attributeKeys { | |
2009 | return [[self class] _attributeKeys]; | |
2010 | } | |
2011 | ||
2012 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2013 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2014 | } | |
2015 | ||
83682c75 JF |
2016 | - (NSString *) package { |
2017 | return package_; | |
2018 | } | |
2019 | ||
2020 | - (CydiaOperation *) version { | |
2021 | return version_; | |
2388b078 JF |
2022 | } |
2023 | ||
810c9763 JF |
2024 | @end |
2025 | /* }}} */ | |
2026 | /* CydiaRelation Class {{{ */ | |
2027 | @interface CydiaRelation : NSObject { | |
7b33d201 JF |
2028 | _H<NSString> relationship_; |
2029 | _H<NSMutableArray> clauses_; | |
810c9763 JF |
2030 | } |
2031 | ||
2032 | - (NSString *) relationship; | |
2033 | - (NSArray *) clauses; | |
2034 | ||
2035 | @end | |
2036 | ||
2037 | @implementation CydiaRelation | |
2038 | ||
810c9763 JF |
2039 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
2040 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
2041 | relationship_ = [NSString stringWithUTF8String:dep.DepType()]; |
2042 | clauses_ = [NSMutableArray arrayWithCapacity:8]; | |
810c9763 JF |
2043 | |
2044 | pkgCache::DepIterator start; | |
2045 | pkgCache::DepIterator end; | |
2046 | dep.GlobOr(start, end); // ++dep | |
2047 | ||
2048 | _forever { | |
2049 | [clauses_ addObject:[[[CydiaClause alloc] initWithIterator:start] autorelease]]; | |
2050 | ||
2051 | // yes, seriously. (wtf?) | |
2052 | if (start == end) | |
2053 | break; | |
2054 | ++start; | |
2055 | } | |
2056 | } return self; | |
2057 | } | |
2058 | ||
2059 | + (NSArray *) _attributeKeys { | |
2060 | return [NSArray arrayWithObjects: | |
2061 | @"clauses", | |
2062 | @"relationship", | |
2063 | nil]; | |
2064 | } | |
2065 | ||
2066 | - (NSArray *) attributeKeys { | |
2067 | return [[self class] _attributeKeys]; | |
2068 | } | |
2069 | ||
2070 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2071 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2072 | } | |
2073 | ||
2074 | - (NSString *) relationship { | |
2075 | return relationship_; | |
2076 | } | |
2077 | ||
2078 | - (NSArray *) clauses { | |
2079 | return clauses_; | |
2080 | } | |
2081 | ||
2082 | - (void) addClause:(CydiaClause *)clause { | |
2083 | [clauses_ addObject:clause]; | |
2084 | } | |
2085 | ||
dc5812ec | 2086 | @end |
b4d89997 | 2087 | /* }}} */ |
36bb2ca2 | 2088 | /* Package Class {{{ */ |
12016ee5 | 2089 | struct ParsedPackage { |
974742b2 | 2090 | CYString md5sum_; |
12016ee5 JF |
2091 | CYString tagline_; |
2092 | ||
a412f357 | 2093 | CYString architecture_; |
12016ee5 JF |
2094 | CYString icon_; |
2095 | ||
2096 | CYString depiction_; | |
2097 | CYString homepage_; | |
12016ee5 JF |
2098 | CYString author_; |
2099 | ||
12016ee5 JF |
2100 | CYString support_; |
2101 | }; | |
2102 | ||
36bb2ca2 | 2103 | @interface Package : NSObject { |
13902177 | 2104 | uint32_t era_ : 25; |
89bdef78 | 2105 | @public uint32_t role_ : 3; |
aab28f8b JF |
2106 | uint32_t essential_ : 1; |
2107 | uint32_t obsolete_ : 1; | |
2108 | uint32_t ignored_ : 1; | |
13902177 | 2109 | uint32_t pooled_ : 1; |
aab28f8b | 2110 | |
64edd9df | 2111 | CYPool *pool_; |
a1440b10 | 2112 | |
9c5737d5 JF |
2113 | uint32_t rank_; |
2114 | ||
aab28f8b JF |
2115 | _transient Database *database_; |
2116 | ||
7376b55c | 2117 | pkgCache::VerIterator version_; |
36bb2ca2 | 2118 | pkgCache::PkgIterator iterator_; |
36bb2ca2 | 2119 | pkgCache::VerFileIterator file_; |
bbb879fb | 2120 | |
aab28f8b JF |
2121 | CYString id_; |
2122 | CYString name_; | |
22fd24dd | 2123 | CYString transform_; |
807ae6d7 | 2124 | |
4c0ed943 | 2125 | CYString latest_; |
6a155117 | 2126 | CYString installed_; |
b3906a21 | 2127 | time_t upgraded_; |
dc5812ec | 2128 | |
fe33a23e | 2129 | const char *section_; |
aab28f8b | 2130 | _transient NSString *section$_; |
f79a4512 | 2131 | |
7b33d201 | 2132 | _H<Source> source_; |
2388b078 | 2133 | |
bb6bb6d6 | 2134 | PackageValue *metadata_; |
aab28f8b | 2135 | ParsedPackage *parsed_; |
94b0b3e5 | 2136 | |
7b33d201 | 2137 | _H<NSMutableArray> tags_; |
b4d89997 JF |
2138 | } |
2139 | ||
64edd9df JF |
2140 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; |
2141 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; | |
b4d89997 | 2142 | |
2388b078 | 2143 | - (pkgCache::PkgIterator) iterator; |
68d927e2 | 2144 | - (void) parse; |
2388b078 | 2145 | |
36bb2ca2 | 2146 | - (NSString *) section; |
dec6029f JF |
2147 | - (NSString *) simpleSection; |
2148 | ||
f79a4512 JF |
2149 | - (NSString *) longSection; |
2150 | - (NSString *) shortSection; | |
2151 | ||
a3328c28 JF |
2152 | - (NSString *) uri; |
2153 | ||
7aa82ca2 | 2154 | - (MIMEAddress *) maintainer; |
36bb2ca2 | 2155 | - (size_t) size; |
eef4ccaf JF |
2156 | - (NSString *) longDescription; |
2157 | - (NSString *) shortDescription; | |
808c6eb6 | 2158 | - (unichar) index; |
dc5812ec | 2159 | |
94b0b3e5 | 2160 | - (PackageValue *) metadata; |
31bc18a7 | 2161 | - (time_t) seen; |
94b0b3e5 JF |
2162 | |
2163 | - (bool) subscribed; | |
2164 | - (bool) setSubscribed:(bool)subscribed; | |
2165 | ||
807ae6d7 | 2166 | - (BOOL) ignored; |
9e98e020 | 2167 | |
36bb2ca2 JF |
2168 | - (NSString *) latest; |
2169 | - (NSString *) installed; | |
6a155117 | 2170 | - (BOOL) uninstalled; |
5a09ae08 JF |
2171 | |
2172 | - (BOOL) valid; | |
31f3cfff | 2173 | - (BOOL) upgradableAndEssential:(BOOL)essential; |
36bb2ca2 JF |
2174 | - (BOOL) essential; |
2175 | - (BOOL) broken; | |
7d2ac47f | 2176 | - (BOOL) unfiltered; |
6d9712c4 | 2177 | - (BOOL) visible; |
b4d89997 | 2178 | |
9bedffaa JF |
2179 | - (BOOL) half; |
2180 | - (BOOL) halfConfigured; | |
2181 | - (BOOL) halfInstalled; | |
2182 | - (BOOL) hasMode; | |
2183 | - (NSString *) mode; | |
2184 | ||
36bb2ca2 JF |
2185 | - (NSString *) id; |
2186 | - (NSString *) name; | |
770f2a8e | 2187 | - (UIImage *) icon; |
6f1a15d9 | 2188 | - (NSString *) homepage; |
25a2158d | 2189 | - (NSString *) depiction; |
7aa82ca2 | 2190 | - (MIMEAddress *) author; |
b4d89997 | 2191 | |
dc63e78f JF |
2192 | - (NSString *) support; |
2193 | ||
affeffc7 | 2194 | - (NSArray *) files; |
affeffc7 JF |
2195 | - (NSArray *) warnings; |
2196 | - (NSArray *) applications; | |
2388b078 | 2197 | |
36bb2ca2 | 2198 | - (Source *) source; |
b4d89997 | 2199 | |
9c5737d5 | 2200 | - (uint32_t) rank; |
d84597fe | 2201 | - (BOOL) matches:(NSArray *)query; |
b4d89997 | 2202 | |
6d9712c4 | 2203 | - (BOOL) hasTag:(NSString *)tag; |
c390d3ab | 2204 | - (NSString *) primaryPurpose; |
770f2a8e | 2205 | - (NSArray *) purposes; |
3bd1c2a2 | 2206 | - (bool) isCommercial; |
6d9712c4 | 2207 | |
cd95e390 JF |
2208 | - (void) setIndex:(size_t)index; |
2209 | ||
df213583 JF |
2210 | - (CYString &) cyname; |
2211 | ||
f79a4512 | 2212 | - (uint32_t) compareBySection:(NSArray *)sections; |
807ae6d7 | 2213 | |
36bb2ca2 JF |
2214 | - (void) install; |
2215 | - (void) remove; | |
06aa974d | 2216 | |
36bb2ca2 | 2217 | @end |
b4d89997 | 2218 | |
f79a4512 JF |
2219 | uint32_t PackageChangesRadix(Package *self, void *) { |
2220 | union { | |
2221 | uint32_t key; | |
2222 | ||
2223 | struct { | |
2224 | uint32_t timestamp : 30; | |
2225 | uint32_t ignored : 1; | |
2226 | uint32_t upgradable : 1; | |
2227 | } bits; | |
2228 | } value; | |
2229 | ||
2230 | bool upgradable([self upgradableAndEssential:YES]); | |
2231 | value.bits.upgradable = upgradable ? 1 : 0; | |
2232 | ||
2233 | if (upgradable) { | |
2234 | value.bits.timestamp = 0; | |
2235 | value.bits.ignored = [self ignored] ? 0 : 1; | |
2236 | value.bits.upgradable = 1; | |
2237 | } else { | |
36047c66 | 2238 | value.bits.timestamp = [self seen] >> 2; |
f79a4512 JF |
2239 | value.bits.ignored = 0; |
2240 | value.bits.upgradable = 0; | |
2241 | } | |
2242 | ||
2243 | return _not(uint32_t) - value.key; | |
2244 | } | |
2245 | ||
aeeb755b JF |
2246 | CYString &(*PackageName)(Package *self, SEL sel); |
2247 | ||
df213583 JF |
2248 | uint32_t PackagePrefixRadix(Package *self, void *context) { |
2249 | size_t offset(reinterpret_cast<size_t>(context)); | |
aeeb755b | 2250 | CYString &name(PackageName(self, @selector(cyname))); |
677b8415 | 2251 | |
df213583 JF |
2252 | size_t size(name.size()); |
2253 | if (size == 0) | |
2254 | return 0; | |
2255 | char *text(name.data()); | |
677b8415 | 2256 | |
df213583 JF |
2257 | size_t zeros; |
2258 | if (!isdigit(text[0])) | |
2259 | zeros = 0; | |
2260 | else { | |
2261 | size_t digits(1); | |
2262 | while (size != digits && isdigit(text[digits])) | |
2263 | if (++digits == 4) | |
2264 | break; | |
2265 | zeros = 4 - digits; | |
2266 | } | |
677b8415 | 2267 | |
df213583 JF |
2268 | uint8_t data[4]; |
2269 | ||
df213583 JF |
2270 | if (offset == 0 && zeros != 0) { |
2271 | memset(data, '0', zeros); | |
2272 | memcpy(data + zeros, text, 4 - zeros); | |
2273 | } else { | |
2274 | /* XXX: there's some danger here if you request a non-zero offset < 4 and it gets zero padded */ | |
2275 | if (size <= offset - zeros) | |
2276 | return 0; | |
2277 | ||
2278 | text += offset - zeros; | |
2279 | size -= offset - zeros; | |
2280 | ||
2281 | if (size >= 4) | |
2282 | memcpy(data, text, 4); | |
2283 | else { | |
2284 | memcpy(data, text, size); | |
2285 | memset(data + size, 0, 4 - size); | |
2286 | } | |
2287 | ||
2288 | for (size_t i(0); i != 4; ++i) | |
2289 | if (isalpha(data[i])) | |
a7a59ee1 | 2290 | data[i] |= 0x20; |
df213583 JF |
2291 | } |
2292 | ||
2293 | if (offset == 0) | |
a7a59ee1 JF |
2294 | if (data[0] == '@') |
2295 | data[0] = 0x7f; | |
2296 | else | |
2297 | data[0] = (data[0] & 0x1f) | "\x80\x00\xc0\x40"[data[0] >> 6]; | |
df213583 JF |
2298 | |
2299 | /* XXX: ntohl may be more honest */ | |
2300 | return OSSwapInt32(*reinterpret_cast<uint32_t *>(data)); | |
2301 | } | |
2302 | ||
22fd24dd | 2303 | CFComparisonResult StringNameCompare(CFStringRef lhn, CFStringRef rhn, size_t length) { |
df213583 | 2304 | _profile(PackageNameCompare) |
5358f56f | 2305 | if (lhn == NULL) |
b129e6d9 | 2306 | return rhn == NULL ? kCFCompareEqualTo : kCFCompareLessThan; |
5358f56f | 2307 | else if (rhn == NULL) |
b129e6d9 | 2308 | return kCFCompareGreaterThan; |
5358f56f | 2309 | |
22fd24dd | 2310 | CFIndex length(CFStringGetLength(lhn)); |
aeeb755b | 2311 | |
677b8415 | 2312 | _profile(PackageNameCompare$NumbersLast) |
22fd24dd | 2313 | if (length != 0 && CFStringGetLength(rhn) != 0) { |
677b8415 JF |
2314 | UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); |
2315 | UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); | |
2316 | bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); | |
2317 | if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) | |
b129e6d9 | 2318 | return lha ? kCFCompareLessThan : kCFCompareGreaterThan; |
677b8415 JF |
2319 | } |
2320 | _end | |
2321 | ||
2322 | _profile(PackageNameCompare$Compare) | |
22fd24dd | 2323 | return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, (CFLocaleRef) (id) CollationLocale_); |
677b8415 JF |
2324 | _end |
2325 | _end | |
2326 | } | |
2327 | ||
22fd24dd JF |
2328 | _finline CFComparisonResult StringNameCompare(NSString *lhn, NSString*rhn, size_t length) { |
2329 | return StringNameCompare((CFStringRef) lhn, (CFStringRef) rhn, length); | |
2330 | } | |
2331 | ||
aeeb755b | 2332 | CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { |
22fd24dd JF |
2333 | CYString &lhn(PackageName(lhs, @selector(cyname))); |
2334 | NSString *rhn(PackageName(rhs, @selector(cyname))); | |
2335 | return StringNameCompare(lhn, rhn, lhn.size()); | |
aeeb755b JF |
2336 | } |
2337 | ||
2338 | CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *arg) { | |
2339 | return PackageNameCompare(*lhs, *rhs, arg); | |
677b8415 JF |
2340 | } |
2341 | ||
2342 | struct PackageNameOrdering : | |
2343 | std::binary_function<Package *, Package *, bool> | |
2344 | { | |
2345 | _finline bool operator ()(Package *lhs, Package *rhs) const { | |
b129e6d9 | 2346 | return PackageNameCompare(lhs, rhs, NULL) == kCFCompareLessThan; |
677b8415 JF |
2347 | } |
2348 | }; | |
2349 | ||
36bb2ca2 | 2350 | @implementation Package |
b4d89997 | 2351 | |
df213583 JF |
2352 | - (NSString *) description { |
2353 | return [NSString stringWithFormat:@"<Package:%@>", static_cast<NSString *>(name_)]; | |
2354 | } | |
2355 | ||
36bb2ca2 | 2356 | - (void) dealloc { |
13902177 | 2357 | if (!pooled_) |
64edd9df | 2358 | delete pool_; |
12016ee5 JF |
2359 | if (parsed_ != NULL) |
2360 | delete parsed_; | |
36bb2ca2 | 2361 | [super dealloc]; |
b4d89997 JF |
2362 | } |
2363 | ||
3bd1c2a2 | 2364 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 2365 | if (false); |
10d63f9e JF |
2366 | else if (selector == @selector(clear)) |
2367 | return @"clear"; | |
2cd1afd9 JF |
2368 | else if (selector == @selector(getField:)) |
2369 | return @"getField"; | |
8fb7c7a1 JF |
2370 | else if (selector == @selector(getRecord)) |
2371 | return @"getRecord"; | |
e58ff941 | 2372 | else if (selector == @selector(hasTag:)) |
3bd1c2a2 | 2373 | return @"hasTag"; |
10d63f9e JF |
2374 | else if (selector == @selector(install)) |
2375 | return @"install"; | |
2376 | else if (selector == @selector(remove)) | |
2377 | return @"remove"; | |
3bd1c2a2 JF |
2378 | else |
2379 | return nil; | |
2380 | } | |
2381 | ||
2382 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
2383 | return [self webScriptNameForSelector:selector] == nil; | |
2384 | } | |
2385 | ||
6f1a15d9 | 2386 | + (NSArray *) _attributeKeys { |
e58ff941 JF |
2387 | return [NSArray arrayWithObjects: |
2388 | @"applications", | |
a412f357 | 2389 | @"architecture", |
e58ff941 JF |
2390 | @"author", |
2391 | @"depiction", | |
2392 | @"essential", | |
2393 | @"homepage", | |
2394 | @"icon", | |
2395 | @"id", | |
2396 | @"installed", | |
2397 | @"latest", | |
2398 | @"longDescription", | |
2399 | @"longSection", | |
2400 | @"maintainer", | |
974742b2 | 2401 | @"md5sum", |
e58ff941 JF |
2402 | @"mode", |
2403 | @"name", | |
2404 | @"purposes", | |
83682c75 | 2405 | @"relations", |
e58ff941 | 2406 | @"section", |
de1ace71 | 2407 | @"selection", |
e58ff941 JF |
2408 | @"shortDescription", |
2409 | @"shortSection", | |
2410 | @"simpleSection", | |
2411 | @"size", | |
2412 | @"source", | |
5959b596 | 2413 | @"state", |
e58ff941 | 2414 | @"support", |
82aa2434 | 2415 | @"tags", |
1342f808 | 2416 | @"upgraded", |
e58ff941 JF |
2417 | @"warnings", |
2418 | nil]; | |
6f1a15d9 JF |
2419 | } |
2420 | ||
2421 | - (NSArray *) attributeKeys { | |
2422 | return [[self class] _attributeKeys]; | |
2423 | } | |
2424 | ||
2425 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2426 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2427 | } | |
2428 | ||
83682c75 JF |
2429 | - (NSArray *) relations { |
2430 | @synchronized (database_) { | |
2431 | NSMutableArray *relations([NSMutableArray arrayWithCapacity:16]); | |
810c9763 JF |
2432 | for (pkgCache::DepIterator dep(version_.DependsList()); !dep.end(); ++dep) |
2433 | [relations addObject:[[[CydiaRelation alloc] initWithIterator:dep] autorelease]]; | |
83682c75 JF |
2434 | return relations; |
2435 | } } | |
2436 | ||
a412f357 JF |
2437 | - (NSString *) architecture { |
2438 | [self parse]; | |
2439 | @synchronized (database_) { | |
2440 | return parsed_->architecture_.empty() ? [NSNull null] : (id) parsed_->architecture_; | |
2441 | } } | |
2442 | ||
2cd1afd9 JF |
2443 | - (NSString *) getField:(NSString *)name { |
2444 | @synchronized (database_) { | |
2445 | if ([database_ era] != era_ || file_.end()) | |
2446 | return nil; | |
2447 | ||
2448 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2449 | ||
2450 | const char *start, *end; | |
2451 | if (!parser.Find([name UTF8String], start, end)) | |
2452 | return (NSString *) [NSNull null]; | |
2453 | ||
b6f9e52a | 2454 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; |
2cd1afd9 JF |
2455 | } } |
2456 | ||
8fb7c7a1 JF |
2457 | - (NSString *) getRecord { |
2458 | @synchronized (database_) { | |
2459 | if ([database_ era] != era_ || file_.end()) | |
2460 | return nil; | |
2461 | ||
2462 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2463 | ||
2464 | const char *start, *end; | |
2465 | parser.GetRec(start, end); | |
2466 | ||
2467 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; | |
2468 | } } | |
2469 | ||
68d927e2 | 2470 | - (void) parse { |
12016ee5 | 2471 | if (parsed_ != NULL) |
68d927e2 | 2472 | return; |
12016ee5 JF |
2473 | @synchronized (database_) { |
2474 | if ([database_ era] != era_ || file_.end()) | |
68d927e2 JF |
2475 | return; |
2476 | ||
12016ee5 JF |
2477 | ParsedPackage *parsed(new ParsedPackage); |
2478 | parsed_ = parsed; | |
2479 | ||
68d927e2 JF |
2480 | _profile(Package$parse) |
2481 | pkgRecords::Parser *parser; | |
2482 | ||
2483 | _profile(Package$parse$Lookup) | |
2484 | parser = &[database_ records]->Lookup(file_); | |
2485 | _end | |
2486 | ||
00067a67 | 2487 | CYString bugs; |
68d927e2 JF |
2488 | CYString website; |
2489 | ||
2490 | _profile(Package$parse$Find) | |
2491 | struct { | |
2492 | const char *name_; | |
2493 | CYString *value_; | |
2494 | } names[] = { | |
a412f357 | 2495 | {"architecture", &parsed->architecture_}, |
12016ee5 JF |
2496 | {"icon", &parsed->icon_}, |
2497 | {"depiction", &parsed->depiction_}, | |
2498 | {"homepage", &parsed->homepage_}, | |
68d927e2 | 2499 | {"website", &website}, |
00067a67 | 2500 | {"bugs", &bugs}, |
12016ee5 | 2501 | {"support", &parsed->support_}, |
12016ee5 | 2502 | {"author", &parsed->author_}, |
974742b2 | 2503 | {"md5sum", &parsed->md5sum_}, |
68d927e2 JF |
2504 | }; |
2505 | ||
2506 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { | |
2507 | const char *start, *end; | |
2508 | ||
2509 | if (parser->Find(names[i].name_, start, end)) { | |
2510 | CYString &value(*names[i].value_); | |
2511 | _profile(Package$parse$Value) | |
2512 | value.set(pool_, start, end - start); | |
2513 | _end | |
2514 | } | |
2515 | } | |
2516 | _end | |
2517 | ||
2518 | _profile(Package$parse$Tagline) | |
2519 | const char *start, *end; | |
0dd0c302 | 2520 | if (parser->ShortDesc(start, end)) { |
68d927e2 JF |
2521 | const char *stop(reinterpret_cast<const char *>(memchr(start, '\n', end - start))); |
2522 | if (stop == NULL) | |
2523 | stop = end; | |
2524 | while (stop != start && stop[-1] == '\r') | |
2525 | --stop; | |
12016ee5 | 2526 | parsed->tagline_.set(pool_, start, stop - start); |
68d927e2 JF |
2527 | } |
2528 | _end | |
2529 | ||
2530 | _profile(Package$parse$Retain) | |
12016ee5 JF |
2531 | if (parsed->homepage_.empty()) |
2532 | parsed->homepage_ = website; | |
2533 | if (parsed->homepage_ == parsed->depiction_) | |
2534 | parsed->homepage_.clear(); | |
00067a67 JF |
2535 | if (parsed->support_.empty()) |
2536 | parsed->support_ = bugs; | |
68d927e2 JF |
2537 | _end |
2538 | _end | |
12016ee5 | 2539 | } } |
68d927e2 | 2540 | |
64edd9df | 2541 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { |
a1440b10 | 2542 | if ((self = [super init]) != nil) { |
7376b55c | 2543 | _profile(Package$initWithVersion) |
13902177 | 2544 | if (pool == NULL) |
64edd9df | 2545 | pool_ = new CYPool(); |
13902177 JF |
2546 | else { |
2547 | pool_ = pool; | |
2548 | pooled_ = true; | |
2549 | } | |
a1440b10 | 2550 | |
36bb2ca2 | 2551 | database_ = database; |
aab28f8b | 2552 | era_ = [database era]; |
b4d89997 | 2553 | |
aab28f8b | 2554 | version_ = version; |
2083b866 | 2555 | |
aab28f8b JF |
2556 | pkgCache::PkgIterator iterator(version.ParentPkg()); |
2557 | iterator_ = iterator; | |
06aa974d | 2558 | |
aab28f8b | 2559 | _profile(Package$initWithVersion$Version) |
68d927e2 JF |
2560 | if (!version_.end()) |
2561 | file_ = version_.FileList(); | |
2562 | else { | |
2563 | pkgCache &cache([database_ cache]); | |
2564 | file_ = pkgCache::VerFileIterator(cache, cache.VerFileP); | |
2565 | } | |
2566 | _end | |
808c6eb6 | 2567 | |
aab28f8b | 2568 | _profile(Package$initWithVersion$Cache) |
aab28f8b JF |
2569 | name_.set(NULL, iterator.Display()); |
2570 | ||
2571 | latest_.set(NULL, StripVersion_(version_.VerStr())); | |
2572 | ||
2573 | pkgCache::VerIterator current(iterator.CurrentVer()); | |
2574 | if (!current.end()) | |
2575 | installed_.set(NULL, StripVersion_(current.VerStr())); | |
808c6eb6 JF |
2576 | _end |
2577 | ||
22fd24dd JF |
2578 | _profile(Package$initWithVersion$Transliterate) do { |
2579 | if (CollationTransl_ == NULL) | |
2580 | break; | |
2581 | if (name_.empty()) | |
2582 | break; | |
2583 | ||
2584 | _profile(Package$initWithVersion$Transliterate$utf8) | |
2585 | const uint8_t *data(reinterpret_cast<const uint8_t *>(name_.data())); | |
2586 | for (size_t i(0), e(name_.size()); i != e; ++i) | |
2587 | if (data[i] >= 0x80) | |
2588 | goto extended; | |
2589 | break; extended:; | |
2590 | _end | |
2591 | ||
2592 | UErrorCode code(U_ZERO_ERROR); | |
2593 | int32_t length; | |
2594 | ||
2595 | _profile(Package$initWithVersion$Transliterate$u_strFromUTF8WithSub) | |
2596 | CollationString_.resize(name_.size()); | |
2597 | u_strFromUTF8WithSub(&CollationString_[0], CollationString_.size(), &length, name_.data(), name_.size(), 0xfffd, NULL, &code); | |
2598 | if (!U_SUCCESS(code)) | |
2599 | break; | |
2600 | CollationString_.resize(length); | |
2601 | _end | |
2602 | ||
2603 | _profile(Package$initWithVersion$Transliterate$utrans_trans) | |
2604 | length = CollationString_.size(); | |
2605 | utrans_trans(CollationTransl_, reinterpret_cast<UReplaceable *>(&CollationString_), &CollationUCalls_, 0, &length, &code); | |
2606 | if (!U_SUCCESS(code)) | |
2607 | break; | |
2608 | _assert(CollationString_.size() == length); | |
2609 | _end | |
2610 | ||
2611 | _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$preflight) | |
2612 | u_strToUTF8WithSub(NULL, 0, &length, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); | |
2613 | if (code == U_BUFFER_OVERFLOW_ERROR) | |
2614 | code = U_ZERO_ERROR; | |
2615 | else if (!U_SUCCESS(code)) | |
2616 | break; | |
2617 | _end | |
2618 | ||
2619 | char *transform; | |
2620 | _profile(Package$initWithVersion$Transliterate$apr_palloc) | |
64edd9df | 2621 | transform = pool_->malloc<char>(length); |
22fd24dd JF |
2622 | _end |
2623 | _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$transform) | |
2624 | u_strToUTF8WithSub(transform, length, NULL, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); | |
2625 | if (!U_SUCCESS(code)) | |
2626 | break; | |
2627 | _end | |
2628 | ||
2629 | transform_.set(NULL, transform, length); | |
2630 | } while (false); _end | |
2631 | ||
7376b55c | 2632 | _profile(Package$initWithVersion$Tags) |
aab28f8b | 2633 | pkgCache::TagIterator tag(iterator.TagList()); |
0dd0c302 | 2634 | if (!tag.end()) { |
7b33d201 | 2635 | tags_ = [NSMutableArray arrayWithCapacity:8]; |
5b625a2e JF |
2636 | |
2637 | goto tag; for (; !tag.end(); ++tag) tag: { | |
0dd0c302 | 2638 | const char *name(tag.Name()); |
5b625a2e JF |
2639 | NSString *string((NSString *) CYStringCreate(name)); |
2640 | if (string == nil) | |
2641 | continue; | |
2642 | ||
2643 | [tags_ addObject:[string autorelease]]; | |
0a377825 | 2644 | |
97a3d89e JF |
2645 | if (role_ == 0 && strncmp(name, "role::", 6) == 0 /*&& strcmp(name, "role::leaper") != 0*/) { |
2646 | if (strcmp(name + 6, "enduser") == 0) | |
2647 | role_ = 1; | |
2648 | else if (strcmp(name + 6, "hacker") == 0) | |
2649 | role_ = 2; | |
2650 | else if (strcmp(name + 6, "developer") == 0) | |
2651 | role_ = 3; | |
2652 | else if (strcmp(name + 6, "cydia") == 0) | |
2653 | role_ = 7; | |
2654 | else | |
2655 | role_ = 4; | |
2656 | } | |
0a377825 JF |
2657 | |
2658 | if (strncmp(name, "cydia::", 7) == 0) { | |
2659 | if (strcmp(name + 7, "essential") == 0) | |
2660 | essential_ = true; | |
2661 | else if (strcmp(name + 7, "obsolete") == 0) | |
2662 | obsolete_ = true; | |
2663 | } | |
5b625a2e | 2664 | } |
0dd0c302 | 2665 | } |
808c6eb6 | 2666 | _end |
7b0ce2da | 2667 | |
7376b55c | 2668 | _profile(Package$initWithVersion$Metadata) |
029c8b74 JF |
2669 | const char *mixed(iterator.Name()); |
2670 | size_t size(strlen(mixed)); | |
81bf4684 JF |
2671 | static const size_t prefix(sizeof("/var/lib/dpkg/info/") - 1); |
2672 | char lower[prefix + size + 5 + 1]; | |
029c8b74 JF |
2673 | |
2674 | for (size_t i(0); i != size; ++i) | |
81bf4684 JF |
2675 | lower[prefix + i] = mixed[i] | 0x20; |
2676 | ||
2677 | if (!installed_.empty()) { | |
2678 | memcpy(lower, "/var/lib/dpkg/info/", prefix); | |
2679 | memcpy(lower + prefix + size, ".list", 6); | |
2680 | struct stat info; | |
2681 | if (stat(lower, &info) != -1) | |
b3906a21 | 2682 | upgraded_ = info.st_birthtime; |
81bf4684 | 2683 | } |
029c8b74 | 2684 | |
81bf4684 | 2685 | PackageValue *metadata(PackageFind(lower + prefix, size)); |
bb6bb6d6 | 2686 | metadata_ = metadata; |
677b8415 | 2687 | |
029c8b74 JF |
2688 | id_.set(NULL, metadata->name_, size); |
2689 | ||
94b0b3e5 JF |
2690 | const char *latest(version_.VerStr()); |
2691 | size_t length(strlen(latest)); | |
a4b0ec52 | 2692 | |
94b0b3e5 | 2693 | uint16_t vhash(hashlittle(latest, length)); |
677b8415 | 2694 | |
94b0b3e5 JF |
2695 | size_t capped(std::min<size_t>(8, length)); |
2696 | latest = latest + length - capped; | |
677b8415 | 2697 | |
94b0b3e5 JF |
2698 | if (metadata->first_ == 0) |
2699 | metadata->first_ = now_; | |
808c6eb6 | 2700 | |
94b0b3e5 | 2701 | if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) { |
94b0b3e5 JF |
2702 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); |
2703 | metadata->vhash_ = vhash; | |
2f856365 JF |
2704 | metadata->last_ = now_; |
2705 | } else if (metadata->last_ == 0) | |
2706 | metadata->last_ = metadata->first_; | |
808c6eb6 | 2707 | _end |
a1440b10 | 2708 | |
7376b55c | 2709 | _profile(Package$initWithVersion$Section) |
0bc841de | 2710 | section_ = version_.Section(); |
f79a4512 | 2711 | _end |
a1440b10 | 2712 | |
aab28f8b JF |
2713 | _profile(Package$initWithVersion$Flags) |
2714 | essential_ |= ((iterator->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES); | |
2715 | ignored_ = iterator->SelectedState == pkgCache::State::Hold; | |
4fa77608 | 2716 | _end |
3dd53516 | 2717 | _end } return self; |
dc5812ec JF |
2718 | } |
2719 | ||
64edd9df | 2720 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { |
b1ce61ec JF |
2721 | pkgCache::VerIterator version; |
2722 | ||
2723 | _profile(Package$packageWithIterator$GetCandidateVer) | |
2724 | version = [database policy]->GetCandidateVer(iterator); | |
2725 | _end | |
2726 | ||
7376b55c JF |
2727 | if (version.end()) |
2728 | return nil; | |
b1ce61ec | 2729 | |
8564efc1 JF |
2730 | Package *package; |
2731 | ||
2732 | _profile(Package$packageWithIterator$Allocate) | |
2733 | package = [Package allocWithZone:zone]; | |
2734 | _end | |
2735 | ||
2736 | _profile(Package$packageWithIterator$Initialize) | |
2737 | package = [package | |
2738 | initWithVersion:version | |
2739 | withZone:zone | |
2740 | inPool:pool | |
2741 | database:database | |
2742 | ]; | |
2743 | _end | |
2744 | ||
2745 | _profile(Package$packageWithIterator$Autorelease) | |
2746 | package = [package autorelease]; | |
2747 | _end | |
2748 | ||
2749 | return package; | |
3dd53516 | 2750 | } |
dc5812ec | 2751 | |
2388b078 JF |
2752 | - (pkgCache::PkgIterator) iterator { |
2753 | return iterator_; | |
2754 | } | |
2755 | ||
36bb2ca2 | 2756 | - (NSString *) section { |
f79a4512 | 2757 | if (section$_ == nil) { |
fe33a23e | 2758 | if (section_ == NULL) |
f79a4512 JF |
2759 | return nil; |
2760 | ||
fe33a23e JF |
2761 | _profile(Package$section$mappedSectionForPointer) |
2762 | section$_ = [database_ mappedSectionForPointer:section_]; | |
e4f3d6e9 | 2763 | _end |
f79a4512 | 2764 | } return section$_; |
dc5812ec JF |
2765 | } |
2766 | ||
dec6029f JF |
2767 | - (NSString *) simpleSection { |
2768 | if (NSString *section = [self section]) | |
2769 | return Simplify(section); | |
2770 | else | |
2771 | return nil; | |
a3328c28 | 2772 | } |
dec6029f | 2773 | |
f79a4512 | 2774 | - (NSString *) longSection { |
18873623 | 2775 | return LocalizeSection([self section]); |
f79a4512 JF |
2776 | } |
2777 | ||
2778 | - (NSString *) shortSection { | |
2779 | return [[NSBundle mainBundle] localizedStringForKey:[self simpleSection] value:nil table:@"Sections"]; | |
2780 | } | |
2781 | ||
a3328c28 JF |
2782 | - (NSString *) uri { |
2783 | return nil; | |
2784 | #if 0 | |
2785 | pkgIndexFile *index; | |
2786 | pkgCache::PkgFileIterator file(file_.File()); | |
2787 | if (![database_ list].FindIndex(file, index)) | |
2788 | return nil; | |
2789 | return [NSString stringWithUTF8String:iterator_->Path]; | |
2790 | //return [NSString stringWithUTF8String:file.Site()]; | |
2791 | //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()]; | |
2792 | #endif | |
dec6029f JF |
2793 | } |
2794 | ||
7aa82ca2 | 2795 | - (MIMEAddress *) maintainer { |
884171d6 JF |
2796 | @synchronized (database_) { |
2797 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2798 | return nil; |
884171d6 | 2799 | |
36bb2ca2 | 2800 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
00e2109e | 2801 | const std::string &maintainer(parser->Maintainer()); |
7aa82ca2 | 2802 | return maintainer.empty() ? nil : [MIMEAddress addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]]; |
884171d6 | 2803 | } } |
8fe19fc1 | 2804 | |
974742b2 JF |
2805 | - (NSString *) md5sum { |
2806 | return parsed_ == NULL ? nil : (id) parsed_->md5sum_; | |
2807 | } | |
2808 | ||
36bb2ca2 | 2809 | - (size_t) size { |
884171d6 JF |
2810 | @synchronized (database_) { |
2811 | if ([database_ era] != era_ || version_.end()) | |
2812 | return 0; | |
2813 | ||
2814 | return version_->InstalledSize; | |
2815 | } } | |
dc5812ec | 2816 | |
eef4ccaf | 2817 | - (NSString *) longDescription { |
3dd53516 JF |
2818 | @synchronized (database_) { |
2819 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2820 | return nil; |
3dd53516 | 2821 | |
36bb2ca2 | 2822 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
2388b078 | 2823 | NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]); |
8fe19fc1 | 2824 | |
36bb2ca2 JF |
2825 | NSArray *lines = [description componentsSeparatedByString:@"\n"]; |
2826 | NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; | |
2827 | if ([lines count] < 2) | |
2828 | return nil; | |
3178d79b | 2829 | |
36bb2ca2 | 2830 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; |
c4dcf2c2 | 2831 | for (size_t i(1), e([lines count]); i != e; ++i) { |
36bb2ca2 JF |
2832 | NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; |
2833 | [trimmed addObject:trim]; | |
2834 | } | |
3178d79b | 2835 | |
36bb2ca2 | 2836 | return [trimmed componentsJoinedByString:@"\n"]; |
3dd53516 | 2837 | } } |
dc5812ec | 2838 | |
eef4ccaf | 2839 | - (NSString *) shortDescription { |
60bef540 JF |
2840 | if (parsed_ != NULL) |
2841 | return static_cast<NSString *>(parsed_->tagline_); | |
2842 | ||
2843 | @synchronized (database_) { | |
2844 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2845 | ||
2846 | const char *start, *end; | |
2847 | if (!parser.ShortDesc(start, end)) | |
2848 | return nil; | |
2849 | ||
abd93900 JF |
2850 | if (end - start > 200) |
2851 | end = start + 200; | |
60bef540 JF |
2852 | |
2853 | /* | |
2854 | if (const char *stop = reinterpret_cast<const char *>(memchr(start, '\n', end - start))) | |
2855 | end = stop; | |
2856 | ||
2857 | while (end != start && end[-1] == '\r') | |
2858 | --end; | |
2859 | */ | |
2860 | ||
2861 | return [(id) CYStringCreate(start, end - start) autorelease]; | |
2862 | } } | |
eef4ccaf | 2863 | |
808c6eb6 JF |
2864 | - (unichar) index { |
2865 | _profile(Package$index) | |
677b8415 JF |
2866 | CFStringRef name((CFStringRef) [self name]); |
2867 | if (CFStringGetLength(name) == 0) | |
808c6eb6 | 2868 | return '#'; |
677b8415 JF |
2869 | UniChar character(CFStringGetCharacterAtIndex(name, 0)); |
2870 | if (!CFUniCharIsMemberOf(character, kCFUniCharLetterCharacterSet)) | |
808c6eb6 | 2871 | return '#'; |
447db19d | 2872 | return toupper(character); |
808c6eb6 | 2873 | _end |
36bb2ca2 | 2874 | } |
3178d79b | 2875 | |
94b0b3e5 | 2876 | - (PackageValue *) metadata { |
bb6bb6d6 | 2877 | return metadata_; |
807ae6d7 JF |
2878 | } |
2879 | ||
31bc18a7 | 2880 | - (time_t) seen { |
94b0b3e5 JF |
2881 | PackageValue *metadata([self metadata]); |
2882 | return metadata->subscribed_ ? metadata->last_ : metadata->first_; | |
3178d79b JF |
2883 | } |
2884 | ||
94b0b3e5 JF |
2885 | - (bool) subscribed { |
2886 | return [self metadata]->subscribed_; | |
2887 | } | |
2888 | ||
2889 | - (bool) setSubscribed:(bool)subscribed { | |
2890 | PackageValue *metadata([self metadata]); | |
2891 | if (metadata->subscribed_ == subscribed) | |
2892 | return false; | |
2893 | metadata->subscribed_ = subscribed; | |
2894 | return true; | |
807ae6d7 JF |
2895 | } |
2896 | ||
2897 | - (BOOL) ignored { | |
1b18f026 | 2898 | return ignored_; |
807ae6d7 JF |
2899 | } |
2900 | ||
36bb2ca2 JF |
2901 | - (NSString *) latest { |
2902 | return latest_; | |
8fe19fc1 JF |
2903 | } |
2904 | ||
36bb2ca2 JF |
2905 | - (NSString *) installed { |
2906 | return installed_; | |
3178d79b JF |
2907 | } |
2908 | ||
6a155117 JF |
2909 | - (BOOL) uninstalled { |
2910 | return installed_.empty(); | |
2911 | } | |
2912 | ||
5a09ae08 JF |
2913 | - (BOOL) valid { |
2914 | return !version_.end(); | |
2915 | } | |
2916 | ||
31f3cfff | 2917 | - (BOOL) upgradableAndEssential:(BOOL)essential { |
677b8415 JF |
2918 | _profile(Package$upgradableAndEssential) |
2919 | pkgCache::VerIterator current(iterator_.CurrentVer()); | |
2920 | if (current.end()) | |
e4f3d6e9 | 2921 | return essential && essential_; |
677b8415 | 2922 | else |
e4f3d6e9 | 2923 | return !version_.end() && version_ != current; |
677b8415 | 2924 | _end |
36bb2ca2 | 2925 | } |
3178d79b | 2926 | |
36bb2ca2 | 2927 | - (BOOL) essential { |
a1440b10 | 2928 | return essential_; |
3178d79b JF |
2929 | } |
2930 | ||
36bb2ca2 | 2931 | - (BOOL) broken { |
9bedffaa JF |
2932 | return [database_ cache][iterator_].InstBroken(); |
2933 | } | |
2934 | ||
7d2ac47f | 2935 | - (BOOL) unfiltered { |
4fa77608 | 2936 | _profile(Package$unfiltered$obsolete) |
cf48f656 | 2937 | if (_unlikely(obsolete_)) |
4fa77608 JF |
2938 | return false; |
2939 | _end | |
2940 | ||
4121c5e0 JF |
2941 | _profile(Package$unfiltered$role) |
2942 | if (_unlikely(role_ > 3)) | |
4fa77608 JF |
2943 | return false; |
2944 | _end | |
2945 | ||
e4f3d6e9 JF |
2946 | return true; |
2947 | } | |
4fa77608 | 2948 | |
e4f3d6e9 JF |
2949 | - (BOOL) visible { |
2950 | if (![self unfiltered]) | |
2951 | return false; | |
2952 | ||
fe33a23e JF |
2953 | NSString *section; |
2954 | ||
2955 | _profile(Package$visible$section) | |
2956 | section = [self section]; | |
2957 | _end | |
4fa77608 | 2958 | |
e4f3d6e9 | 2959 | _profile(Package$visible$isSectionVisible) |
45447dc3 | 2960 | if (!isSectionVisible(section)) |
4fa77608 JF |
2961 | return false; |
2962 | _end | |
2963 | ||
2964 | return true; | |
7d2ac47f JF |
2965 | } |
2966 | ||
9bedffaa | 2967 | - (BOOL) half { |
677b8415 | 2968 | unsigned char current(iterator_->CurrentState); |
9bedffaa JF |
2969 | return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled; |
2970 | } | |
2971 | ||
2972 | - (BOOL) halfConfigured { | |
2973 | return iterator_->CurrentState == pkgCache::State::HalfConfigured; | |
2974 | } | |
2975 | ||
2976 | - (BOOL) halfInstalled { | |
2977 | return iterator_->CurrentState == pkgCache::State::HalfInstalled; | |
2978 | } | |
2979 | ||
2980 | - (BOOL) hasMode { | |
f18b4a97 JF |
2981 | @synchronized (database_) { |
2982 | if ([database_ era] != era_ || iterator_.end()) | |
b129e6d9 | 2983 | return NO; |
f18b4a97 | 2984 | |
9bedffaa JF |
2985 | pkgDepCache::StateCache &state([database_ cache][iterator_]); |
2986 | return state.Mode != pkgDepCache::ModeKeep; | |
f18b4a97 | 2987 | } } |
9bedffaa JF |
2988 | |
2989 | - (NSString *) mode { | |
f18b4a97 JF |
2990 | @synchronized (database_) { |
2991 | if ([database_ era] != era_ || iterator_.end()) | |
2992 | return nil; | |
2993 | ||
9bedffaa JF |
2994 | pkgDepCache::StateCache &state([database_ cache][iterator_]); |
2995 | ||
2996 | switch (state.Mode) { | |
2997 | case pkgDepCache::ModeDelete: | |
2998 | if ((state.iFlags & pkgDepCache::Purge) != 0) | |
f79a4512 | 2999 | return @"PURGE"; |
9bedffaa | 3000 | else |
f79a4512 | 3001 | return @"REMOVE"; |
9bedffaa | 3002 | case pkgDepCache::ModeKeep: |
dc63e78f | 3003 | if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 3004 | return @"REINSTALL"; |
dc63e78f JF |
3005 | /*else if ((state.iFlags & pkgDepCache::AutoKept) != 0) |
3006 | return nil;*/ | |
9bedffaa JF |
3007 | else |
3008 | return nil; | |
9bedffaa | 3009 | case pkgDepCache::ModeInstall: |
dc63e78f | 3010 | /*if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 3011 | return @"REINSTALL"; |
dc63e78f | 3012 | else*/ switch (state.Status) { |
9bedffaa | 3013 | case -1: |
f79a4512 | 3014 | return @"DOWNGRADE"; |
9bedffaa | 3015 | case 0: |
f79a4512 | 3016 | return @"INSTALL"; |
9bedffaa | 3017 | case 1: |
f79a4512 | 3018 | return @"UPGRADE"; |
9bedffaa | 3019 | case 2: |
f79a4512 | 3020 | return @"NEW_INSTALL"; |
670a0494 | 3021 | _nodefault |
9bedffaa | 3022 | } |
670a0494 | 3023 | _nodefault |
9bedffaa | 3024 | } |
f18b4a97 | 3025 | } } |
8fe19fc1 | 3026 | |
36bb2ca2 JF |
3027 | - (NSString *) id { |
3028 | return id_; | |
8fe19fc1 JF |
3029 | } |
3030 | ||
36bb2ca2 | 3031 | - (NSString *) name { |
9fcbca29 | 3032 | return name_.empty() ? id_ : name_; |
36bb2ca2 | 3033 | } |
8fe19fc1 | 3034 | |
770f2a8e | 3035 | - (UIImage *) icon { |
dec6029f | 3036 | NSString *section = [self simpleSection]; |
770f2a8e JF |
3037 | |
3038 | UIImage *icon(nil); | |
12016ee5 JF |
3039 | if (parsed_ != NULL) |
3040 | if (NSString *href = parsed_->icon_) | |
3041 | if ([href hasPrefix:@"file:///"]) | |
84851d87 | 3042 | icon = [UIImage imageAtPath:[[href substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; |
770f2a8e | 3043 | if (icon == nil) if (section != nil) |
84851d87 | 3044 | icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [section stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]; |
b9956841 | 3045 | if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon]) |
189a73d0 | 3046 | if ([dicon hasPrefix:@"file:///"]) |
84851d87 | 3047 | icon = [UIImage imageAtPath:[[dicon substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; |
770f2a8e | 3048 | if (icon == nil) |
8dbf3abc | 3049 | icon = [UIImage imageNamed:@"unknown.png"]; |
770f2a8e | 3050 | return icon; |
36bb2ca2 | 3051 | } |
8fe19fc1 | 3052 | |
6f1a15d9 | 3053 | - (NSString *) homepage { |
12016ee5 | 3054 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->homepage_); |
8fe19fc1 JF |
3055 | } |
3056 | ||
25a2158d | 3057 | - (NSString *) depiction { |
12016ee5 | 3058 | return parsed_ != NULL && !parsed_->depiction_.empty() ? parsed_->depiction_ : [[self source] depictionForPackage:id_]; |
25a2158d JF |
3059 | } |
3060 | ||
7aa82ca2 JF |
3061 | - (MIMEAddress *) author { |
3062 | return parsed_ == NULL || parsed_->author_.empty() ? nil : [MIMEAddress addressWithString:parsed_->author_]; | |
77fcccaf JF |
3063 | } |
3064 | ||
dc63e78f | 3065 | - (NSString *) support { |
00067a67 | 3066 | return parsed_ != NULL && !parsed_->support_.empty() ? parsed_->support_ : [[self source] supportForPackage:id_]; |
dc63e78f JF |
3067 | } |
3068 | ||
affeffc7 | 3069 | - (NSArray *) files { |
9fcbca29 | 3070 | NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", static_cast<NSString *>(id_)]; |
affeffc7 JF |
3071 | NSMutableArray *files = [NSMutableArray arrayWithCapacity:128]; |
3072 | ||
3073 | std::ifstream fin; | |
3074 | fin.open([path UTF8String]); | |
3075 | if (!fin.is_open()) | |
3076 | return nil; | |
3077 | ||
3078 | std::string line; | |
3079 | while (std::getline(fin, line)) | |
3080 | [files addObject:[NSString stringWithUTF8String:line.c_str()]]; | |
3081 | ||
3082 | return files; | |
3083 | } | |
3084 | ||
5959b596 JF |
3085 | - (NSString *) state { |
3086 | @synchronized (database_) { | |
3087 | if ([database_ era] != era_ || file_.end()) | |
3088 | return nil; | |
3089 | ||
3090 | switch (iterator_->CurrentState) { | |
3091 | case pkgCache::State::NotInstalled: | |
3092 | return @"NotInstalled"; | |
3093 | case pkgCache::State::UnPacked: | |
3094 | return @"UnPacked"; | |
3095 | case pkgCache::State::HalfConfigured: | |
3096 | return @"HalfConfigured"; | |
3097 | case pkgCache::State::HalfInstalled: | |
3098 | return @"HalfInstalled"; | |
3099 | case pkgCache::State::ConfigFiles: | |
3100 | return @"ConfigFiles"; | |
3101 | case pkgCache::State::Installed: | |
3102 | return @"Installed"; | |
3103 | case pkgCache::State::TriggersAwaited: | |
3104 | return @"TriggersAwaited"; | |
3105 | case pkgCache::State::TriggersPending: | |
3106 | return @"TriggersPending"; | |
3107 | } | |
3108 | ||
3109 | return (NSString *) [NSNull null]; | |
3110 | } } | |
3111 | ||
de1ace71 JF |
3112 | - (NSString *) selection { |
3113 | @synchronized (database_) { | |
3114 | if ([database_ era] != era_ || file_.end()) | |
3115 | return nil; | |
3116 | ||
3117 | switch (iterator_->SelectedState) { | |
3118 | case pkgCache::State::Unknown: | |
3119 | return @"Unknown"; | |
3120 | case pkgCache::State::Install: | |
3121 | return @"Install"; | |
3122 | case pkgCache::State::Hold: | |
3123 | return @"Hold"; | |
3124 | case pkgCache::State::DeInstall: | |
3125 | return @"DeInstall"; | |
3126 | case pkgCache::State::Purge: | |
3127 | return @"Purge"; | |
3128 | } | |
3129 | ||
3130 | return (NSString *) [NSNull null]; | |
3131 | } } | |
3132 | ||
affeffc7 | 3133 | - (NSArray *) warnings { |
d212b02f JF |
3134 | @synchronized (database_) { |
3135 | if ([database_ era] != era_ || file_.end()) | |
3136 | return nil; | |
3137 | ||
affeffc7 JF |
3138 | NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); |
3139 | const char *name(iterator_.Name()); | |
3140 | ||
3141 | size_t length(strlen(name)); | |
3142 | if (length < 2) invalid: | |
43f3d7f6 | 3143 | [warnings addObject:UCLocalize("ILLEGAL_PACKAGE_IDENTIFIER")]; |
affeffc7 JF |
3144 | else for (size_t i(0); i != length; ++i) |
3145 | if ( | |
9dd60d81 JF |
3146 | /* XXX: technically this is not allowed */ |
3147 | (name[i] < 'A' || name[i] > 'Z') && | |
affeffc7 JF |
3148 | (name[i] < 'a' || name[i] > 'z') && |
3149 | (name[i] < '0' || name[i] > '9') && | |
3150 | (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.') | |
3151 | ) goto invalid; | |
3152 | ||
3153 | if (strcmp(name, "cydia") != 0) { | |
3154 | bool cydia = false; | |
7623f855 | 3155 | bool user = false; |
9dd60d81 | 3156 | bool _private = false; |
affeffc7 | 3157 | bool stash = false; |
bcc58607 | 3158 | bool dbstash = false; |
1db5920d | 3159 | bool dsstore = false; |
affeffc7 | 3160 | |
9dd60d81 JF |
3161 | bool repository = [[self section] isEqualToString:@"Repositories"]; |
3162 | ||
affeffc7 JF |
3163 | if (NSArray *files = [self files]) |
3164 | for (NSString *file in files) | |
3165 | if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"]) | |
3166 | cydia = true; | |
7623f855 JF |
3167 | else if (!user && [file isEqualToString:@"/User"]) |
3168 | user = true; | |
9dd60d81 JF |
3169 | else if (!_private && [file isEqualToString:@"/private"]) |
3170 | _private = true; | |
affeffc7 JF |
3171 | else if (!stash && [file isEqualToString:@"/var/stash"]) |
3172 | stash = true; | |
bcc58607 JF |
3173 | else if (!dbstash && [file isEqualToString:@"/var/db/stash"]) |
3174 | dbstash = true; | |
1db5920d JF |
3175 | else if (!dsstore && [file hasSuffix:@"/.DS_Store"]) |
3176 | dsstore = true; | |
affeffc7 | 3177 | |
9dd60d81 JF |
3178 | /* XXX: this is not sensitive enough. only some folders are valid. */ |
3179 | if (cydia && !repository) | |
43f3d7f6 | 3180 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"Cydia.app"]]; |
7623f855 JF |
3181 | if (user) |
3182 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/User"]]; | |
9dd60d81 | 3183 | if (_private) |
43f3d7f6 | 3184 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; |
affeffc7 | 3185 | if (stash) |
43f3d7f6 | 3186 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; |
bcc58607 JF |
3187 | if (dbstash) |
3188 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/db/stash"]]; | |
1db5920d JF |
3189 | if (dsstore) |
3190 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @".DS_Store"]]; | |
affeffc7 JF |
3191 | } |
3192 | ||
3193 | return [warnings count] == 0 ? nil : warnings; | |
d212b02f | 3194 | } } |
affeffc7 JF |
3195 | |
3196 | - (NSArray *) applications { | |
3197 | NSString *me([[NSBundle mainBundle] bundleIdentifier]); | |
3198 | ||
3199 | NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); | |
3200 | ||
b4fd1197 | 3201 | static RegEx application_r("/Applications/(.*)\\.app/Info.plist"); |
affeffc7 JF |
3202 | if (NSArray *files = [self files]) |
3203 | for (NSString *file in files) | |
3204 | if (application_r(file)) { | |
3205 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); | |
3206 | NSString *id([info objectForKey:@"CFBundleIdentifier"]); | |
3207 | if ([id isEqualToString:me]) | |
3208 | continue; | |
3209 | ||
3210 | NSString *display([info objectForKey:@"CFBundleDisplayName"]); | |
3211 | if (display == nil) | |
3212 | display = application_r[1]; | |
3213 | ||
3214 | NSString *bundle([file stringByDeletingLastPathComponent]); | |
3215 | NSString *icon([info objectForKey:@"CFBundleIconFile"]); | |
5e17a734 JF |
3216 | // XXX: maybe this should check if this is really a string, not just for length |
3217 | if (icon == nil || ![icon respondsToSelector:@selector(length)] || [icon length] == 0) | |
affeffc7 JF |
3218 | icon = @"icon.png"; |
3219 | NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]); | |
3220 | ||
3221 | NSMutableArray *application([NSMutableArray arrayWithCapacity:2]); | |
3222 | [applications addObject:application]; | |
3223 | ||
3224 | [application addObject:id]; | |
3225 | [application addObject:display]; | |
3226 | [application addObject:url]; | |
3227 | } | |
3228 | ||
3229 | return [applications count] == 0 ? nil : applications; | |
3230 | } | |
3231 | ||
36bb2ca2 | 3232 | - (Source *) source { |
5699667a | 3233 | if (source_ == nil) { |
a1440b10 JF |
3234 | @synchronized (database_) { |
3235 | if ([database_ era] != era_ || file_.end()) | |
5699667a JF |
3236 | source_ = (Source *) [NSNull null]; |
3237 | else | |
7b33d201 | 3238 | source_ = [database_ getSource:file_.File()] ?: (Source *) [NSNull null]; |
a1440b10 | 3239 | } |
bbb879fb JF |
3240 | } |
3241 | ||
5699667a | 3242 | return source_ == (Source *) [NSNull null] ? nil : source_; |
8fe19fc1 JF |
3243 | } |
3244 | ||
b3906a21 JF |
3245 | - (time_t) upgraded { |
3246 | return upgraded_; | |
98ddcefd JF |
3247 | } |
3248 | ||
b3906a21 JF |
3249 | - (uint32_t) recent { |
3250 | return std::numeric_limits<uint32_t>::max() - upgraded_; | |
821b1a0c JF |
3251 | } |
3252 | ||
9c5737d5 JF |
3253 | - (uint32_t) rank { |
3254 | return rank_; | |
3255 | } | |
3256 | ||
d84597fe JF |
3257 | - (BOOL) matches:(NSArray *)query { |
3258 | if (query == nil || [query count] == 0) | |
36bb2ca2 | 3259 | return NO; |
8fe19fc1 | 3260 | |
9c5737d5 JF |
3261 | rank_ = 0; |
3262 | ||
3263 | NSString *string; | |
36bb2ca2 | 3264 | NSRange range; |
9c5737d5 | 3265 | NSUInteger length; |
8fe19fc1 | 3266 | |
718c267c | 3267 | string = [self name]; |
d84597fe JF |
3268 | length = [string length]; |
3269 | ||
5af08e16 | 3270 | if (length != 0) |
d84597fe JF |
3271 | for (NSString *term in query) { |
3272 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
3273 | if (range.location != NSNotFound) | |
718c267c | 3274 | rank_ -= 6 * 1000000 / length; |
d84597fe | 3275 | } |
8fe19fc1 | 3276 | |
718c267c JF |
3277 | if (rank_ == 0) { |
3278 | string = [self id]; | |
3279 | length = [string length]; | |
8fe19fc1 | 3280 | |
5af08e16 | 3281 | if (length != 0) |
718c267c JF |
3282 | for (NSString *term in query) { |
3283 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
3284 | if (range.location != NSNotFound) | |
3285 | rank_ -= 6 * 1000000 / length; | |
3286 | } | |
d84597fe | 3287 | } |
c4b530a7 | 3288 | |
9c5737d5 JF |
3289 | string = [self shortDescription]; |
3290 | length = [string length]; | |
abd93900 | 3291 | NSUInteger stop(std::min<NSUInteger>(length, 200)); |
df289c5a | 3292 | |
5af08e16 | 3293 | if (length != 0) |
d84597fe JF |
3294 | for (NSString *term in query) { |
3295 | range = [string rangeOfString:term options:MatchCompareOptions_ range:NSMakeRange(0, stop)]; | |
3296 | if (range.location != NSNotFound) | |
718c267c | 3297 | rank_ -= 2 * 100000; |
d84597fe | 3298 | } |
8fe19fc1 | 3299 | |
9c5737d5 | 3300 | return rank_ != 0; |
36bb2ca2 | 3301 | } |
8fe19fc1 | 3302 | |
82aa2434 JF |
3303 | - (NSArray *) tags { |
3304 | return tags_; | |
3305 | } | |
3306 | ||
6d9712c4 JF |
3307 | - (BOOL) hasTag:(NSString *)tag { |
3308 | return tags_ == nil ? NO : [tags_ containsObject:tag]; | |
3309 | } | |
3310 | ||
c390d3ab | 3311 | - (NSString *) primaryPurpose { |
7b33d201 | 3312 | for (NSString *tag in (NSArray *) tags_) |
c390d3ab JF |
3313 | if ([tag hasPrefix:@"purpose::"]) |
3314 | return [tag substringFromIndex:9]; | |
3315 | return nil; | |
3316 | } | |
3317 | ||
770f2a8e JF |
3318 | - (NSArray *) purposes { |
3319 | NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]); | |
7b33d201 | 3320 | for (NSString *tag in (NSArray *) tags_) |
770f2a8e JF |
3321 | if ([tag hasPrefix:@"purpose::"]) |
3322 | [purposes addObject:[tag substringFromIndex:9]]; | |
3323 | return [purposes count] == 0 ? nil : purposes; | |
3324 | } | |
3325 | ||
3bd1c2a2 JF |
3326 | - (bool) isCommercial { |
3327 | return [self hasTag:@"cydia::commercial"]; | |
3328 | } | |
3329 | ||
cd95e390 JF |
3330 | - (void) setIndex:(size_t)index { |
3331 | if (metadata_->index_ != index) | |
3332 | metadata_->index_ = index; | |
3333 | } | |
3334 | ||
df213583 | 3335 | - (CYString &) cyname { |
22fd24dd | 3336 | return !transform_.empty() ? transform_ : !name_.empty() ? name_ : id_; |
f79a4512 JF |
3337 | } |
3338 | ||
f79a4512 JF |
3339 | - (uint32_t) compareBySection:(NSArray *)sections { |
3340 | NSString *section([self section]); | |
3341 | for (size_t i(0), e([sections count]); i != e; ++i) { | |
3342 | if ([section isEqualToString:[[sections objectAtIndex:i] name]]) | |
3343 | return i; | |
36bb2ca2 | 3344 | } |
3178d79b | 3345 | |
f79a4512 | 3346 | return _not(uint32_t); |
36bb2ca2 | 3347 | } |
3178d79b | 3348 | |
dc63e78f | 3349 | - (void) clear { |
c6ca67ba | 3350 | @synchronized (database_) { |
dc63e78f JF |
3351 | pkgProblemResolver *resolver = [database_ resolver]; |
3352 | resolver->Clear(iterator_); | |
c6ca67ba JF |
3353 | |
3354 | pkgCacheFile &cache([database_ cache]); | |
3355 | cache->SetReInstall(iterator_, false); | |
3356 | cache->MarkKeep(iterator_, false); | |
3357 | } } | |
dc63e78f | 3358 | |
36bb2ca2 | 3359 | - (void) install { |
c6ca67ba | 3360 | @synchronized (database_) { |
36bb2ca2 JF |
3361 | pkgProblemResolver *resolver = [database_ resolver]; |
3362 | resolver->Clear(iterator_); | |
3363 | resolver->Protect(iterator_); | |
c6ca67ba | 3364 | |
36bb2ca2 | 3365 | pkgCacheFile &cache([database_ cache]); |
c6ca67ba | 3366 | cache->SetReInstall(iterator_, false); |
36bb2ca2 | 3367 | cache->MarkInstall(iterator_, false); |
c6ca67ba | 3368 | |
36bb2ca2 JF |
3369 | pkgDepCache::StateCache &state((*cache)[iterator_]); |
3370 | if (!state.Install()) | |
3371 | cache->SetReInstall(iterator_, true); | |
c6ca67ba | 3372 | } } |
68a238ec | 3373 | |
36bb2ca2 | 3374 | - (void) remove { |
c6ca67ba | 3375 | @synchronized (database_) { |
36bb2ca2 JF |
3376 | pkgProblemResolver *resolver = [database_ resolver]; |
3377 | resolver->Clear(iterator_); | |
36bb2ca2 | 3378 | resolver->Remove(iterator_); |
c6ca67ba JF |
3379 | resolver->Protect(iterator_); |
3380 | ||
3381 | pkgCacheFile &cache([database_ cache]); | |
3382 | cache->SetReInstall(iterator_, false); | |
3383 | cache->MarkDelete(iterator_, true); | |
3384 | } } | |
8fe19fc1 | 3385 | |
36bb2ca2 JF |
3386 | @end |
3387 | /* }}} */ | |
3388 | /* Section Class {{{ */ | |
3389 | @interface Section : NSObject { | |
7b33d201 | 3390 | _H<NSString> name_; |
36bb2ca2 JF |
3391 | size_t row_; |
3392 | size_t count_; | |
7b33d201 | 3393 | _H<NSString> localized_; |
36bb2ca2 | 3394 | } |
3178d79b | 3395 | |
677b8415 | 3396 | - (NSComparisonResult) compareByLocalized:(Section *)section; |
9fcbca29 JF |
3397 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; |
3398 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; | |
3399 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; | |
aeeb755b | 3400 | |
36bb2ca2 | 3401 | - (NSString *) name; |
aeeb755b | 3402 | - (void) setName:(NSString *)name; |
f79a4512 | 3403 | |
36bb2ca2 JF |
3404 | - (size_t) row; |
3405 | - (size_t) count; | |
f79a4512 JF |
3406 | |
3407 | - (void) addToRow; | |
36bb2ca2 | 3408 | - (void) addToCount; |
b19871dd | 3409 | |
f79a4512 | 3410 | - (void) setCount:(size_t)count; |
677b8415 | 3411 | - (NSString *) localized; |
f79a4512 | 3412 | |
36bb2ca2 | 3413 | @end |
b19871dd | 3414 | |
36bb2ca2 | 3415 | @implementation Section |
b19871dd | 3416 | |
677b8415 | 3417 | - (NSComparisonResult) compareByLocalized:(Section *)section { |
9fcbca29 JF |
3418 | NSString *lhs(localized_); |
3419 | NSString *rhs([section localized]); | |
6d9712c4 | 3420 | |
9fcbca29 | 3421 | /*if ([lhs length] != 0 && [rhs length] != 0) { |
6d9712c4 JF |
3422 | unichar lhc = [lhs characterAtIndex:0]; |
3423 | unichar rhc = [rhs characterAtIndex:0]; | |
3424 | ||
3425 | if (isalpha(lhc) && !isalpha(rhc)) | |
3426 | return NSOrderedAscending; | |
3427 | else if (!isalpha(lhc) && isalpha(rhc)) | |
3428 | return NSOrderedDescending; | |
9fcbca29 | 3429 | }*/ |
6d9712c4 | 3430 | |
68f1828e | 3431 | return [lhs compare:rhs options:LaxCompareOptions_]; |
6d9712c4 JF |
3432 | } |
3433 | ||
9fcbca29 JF |
3434 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized { |
3435 | if ((self = [self initWithName:name localize:NO]) != nil) { | |
3436 | if (localized != nil) | |
7b33d201 | 3437 | localized_ = localized; |
9fcbca29 JF |
3438 | } return self; |
3439 | } | |
3440 | ||
3441 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize { | |
3442 | return [self initWithName:name row:0 localize:localize]; | |
6d9712c4 JF |
3443 | } |
3444 | ||
9fcbca29 | 3445 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { |
36bb2ca2 | 3446 | if ((self = [super init]) != nil) { |
7b33d201 | 3447 | name_ = name; |
808c6eb6 | 3448 | row_ = row; |
9fcbca29 | 3449 | if (localize) |
7b33d201 | 3450 | localized_ = LocalizeSection(name_); |
808c6eb6 JF |
3451 | } return self; |
3452 | } | |
3453 | ||
36bb2ca2 JF |
3454 | - (NSString *) name { |
3455 | return name_; | |
3456 | } | |
3457 | ||
aeeb755b JF |
3458 | - (void) setName:(NSString *)name { |
3459 | name_ = name; | |
808c6eb6 JF |
3460 | } |
3461 | ||
36bb2ca2 JF |
3462 | - (size_t) row { |
3463 | return row_; | |
3464 | } | |
3465 | ||
3466 | - (size_t) count { | |
3467 | return count_; | |
3468 | } | |
3469 | ||
f79a4512 JF |
3470 | - (void) addToRow { |
3471 | ++row_; | |
3472 | } | |
3473 | ||
36bb2ca2 JF |
3474 | - (void) addToCount { |
3475 | ++count_; | |
3476 | } | |
3477 | ||
f79a4512 JF |
3478 | - (void) setCount:(size_t)count { |
3479 | count_ = count; | |
3480 | } | |
3481 | ||
3482 | - (NSString *) localized { | |
3483 | return localized_; | |
3484 | } | |
3485 | ||
b19871dd JF |
3486 | @end |
3487 | /* }}} */ | |
3488 | ||
9f9ae81c JF |
3489 | class CydiaLogCleaner : |
3490 | public pkgArchiveCleaner | |
3491 | { | |
3492 | protected: | |
3493 | virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) { | |
3494 | unlink(File); | |
3495 | } | |
3496 | }; | |
3497 | ||
36bb2ca2 JF |
3498 | /* Database Implementation {{{ */ |
3499 | @implementation Database | |
ec97ef06 | 3500 | |
770f2a8e | 3501 | + (Database *) sharedInstance { |
7b33d201 | 3502 | static _H<Database> instance; |
770f2a8e | 3503 | if (instance == nil) |
7b33d201 | 3504 | instance = [[[Database alloc] init] autorelease]; |
770f2a8e JF |
3505 | return instance; |
3506 | } | |
3507 | ||
a1440b10 JF |
3508 | - (unsigned) era { |
3509 | return era_; | |
3510 | } | |
3511 | ||
0944377b JF |
3512 | - (void) releasePackages { |
3513 | CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL); | |
3514 | CFArrayRemoveAllValues(packages_); | |
3515 | } | |
3516 | ||
36bb2ca2 | 3517 | - (void) dealloc { |
3931b718 | 3518 | // XXX: actually implement this thing |
36bb2ca2 | 3519 | _assert(false); |
0944377b | 3520 | [self releasePackages]; |
0944377b | 3521 | NSRecycleZone(zone_); |
36bb2ca2 JF |
3522 | [super dealloc]; |
3523 | } | |
ec97ef06 | 3524 | |
d13edf44 | 3525 | - (void) _readCydia:(NSNumber *)fd { |
77fcccaf JF |
3526 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3527 | std::istream is(&ib); | |
3528 | std::string line; | |
3529 | ||
b4fd1197 | 3530 | static RegEx finish_r("finish:([^:]*)"); |
bc8cd583 | 3531 | |
77fcccaf | 3532 | while (std::getline(is, line)) { |
d13edf44 JF |
3533 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3534 | ||
77fcccaf | 3535 | const char *data(line.c_str()); |
bc8cd583 | 3536 | size_t size = line.size(); |
c390d3ab | 3537 | lprintf("C:%s\n", data); |
bc8cd583 JF |
3538 | |
3539 | if (finish_r(data, size)) { | |
3540 | NSString *finish = finish_r[1]; | |
3541 | int index = [Finishes_ indexOfObject:finish]; | |
3542 | if (index != INT_MAX && index > Finish_) | |
3543 | Finish_ = index; | |
3544 | } | |
d13edf44 JF |
3545 | |
3546 | [pool release]; | |
77fcccaf JF |
3547 | } |
3548 | ||
670a0494 | 3549 | _assume(false); |
77fcccaf JF |
3550 | } |
3551 | ||
d13edf44 | 3552 | - (void) _readStatus:(NSNumber *)fd { |
36bb2ca2 JF |
3553 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3554 | std::istream is(&ib); | |
3555 | std::string line; | |
ec97ef06 | 3556 | |
b4fd1197 JF |
3557 | static RegEx conffile_r("status: [^ ]* : conffile-prompt : (.*?) *"); |
3558 | static RegEx pmstatus_r("([^:]*):([^:]*):([^:]*):(.*)"); | |
ec97ef06 | 3559 | |
36bb2ca2 | 3560 | while (std::getline(is, line)) { |
d13edf44 JF |
3561 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3562 | ||
36bb2ca2 | 3563 | const char *data(line.c_str()); |
670a0494 | 3564 | size_t size(line.size()); |
c390d3ab | 3565 | lprintf("S:%s\n", data); |
ec97ef06 | 3566 | |
a08145a8 JF |
3567 | if (conffile_r(data, size)) { |
3568 | // status: /fail : conffile-prompt : '/fail' '/fail.dpkg-new' 1 1 | |
4187453f | 3569 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:conffile_r[1] waitUntilDone:YES]; |
a08145a8 JF |
3570 | } else if (strncmp(data, "status: ", 8) == 0) { |
3571 | // status: <package>: {unpacked,half-configured,installed} | |
389133be | 3572 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 8)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 JF |
3573 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3574 | } else if (strncmp(data, "processing: ", 12) == 0) { | |
3575 | // processing: configure: config-test | |
389133be | 3576 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 12)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 | 3577 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
6915b806 | 3578 | } else if (pmstatus_r(data, size)) { |
31f3cfff | 3579 | std::string type([pmstatus_r[1] UTF8String]); |
4a2dc82e | 3580 | |
6915b806 | 3581 | NSString *package = pmstatus_r[2]; |
4a2dc82e JF |
3582 | if ([package isEqualToString:@"dpkg-exec"]) |
3583 | package = nil; | |
31f3cfff | 3584 | |
5a09ae08 | 3585 | float percent([pmstatus_r[3] floatValue]); |
6915b806 | 3586 | [progress_ performSelectorOnMainThread:@selector(setProgressPercent:) withObject:[NSNumber numberWithFloat:(percent / 100)] waitUntilDone:YES]; |
5a09ae08 JF |
3587 | |
3588 | NSString *string = pmstatus_r[4]; | |
5a09ae08 | 3589 | |
6915b806 | 3590 | if (type == "pmerror") { |
389133be | 3591 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeError forPackage:package]); |
6915b806 JF |
3592 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3593 | } else if (type == "pmstatus") { | |
389133be | 3594 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeStatus forPackage:package]); |
6915b806 JF |
3595 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3596 | } else if (type == "pmconffile") | |
4187453f | 3597 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:string waitUntilDone:YES]; |
670a0494 JF |
3598 | else |
3599 | lprintf("E:unknown pmstatus\n"); | |
3600 | } else | |
3601 | lprintf("E:unknown status\n"); | |
d13edf44 JF |
3602 | |
3603 | [pool release]; | |
36bb2ca2 | 3604 | } |
ec97ef06 | 3605 | |
670a0494 | 3606 | _assume(false); |
36bb2ca2 | 3607 | } |
ec97ef06 | 3608 | |
d13edf44 | 3609 | - (void) _readOutput:(NSNumber *)fd { |
36bb2ca2 JF |
3610 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3611 | std::istream is(&ib); | |
3612 | std::string line; | |
3613 | ||
5a09ae08 | 3614 | while (std::getline(is, line)) { |
d13edf44 JF |
3615 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3616 | ||
c390d3ab | 3617 | lprintf("O:%s\n", line.c_str()); |
27024935 | 3618 | |
389133be | 3619 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:line.c_str()] ofType:kCydiaProgressEventTypeInformation]); |
6915b806 | 3620 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
d13edf44 JF |
3621 | |
3622 | [pool release]; | |
5a09ae08 | 3623 | } |
36bb2ca2 | 3624 | |
670a0494 | 3625 | _assume(false); |
ec97ef06 JF |
3626 | } |
3627 | ||
8b29f8e6 JF |
3628 | - (FILE *) input { |
3629 | return input_; | |
3630 | } | |
3631 | ||
36bb2ca2 | 3632 | - (Package *) packageWithName:(NSString *)name { |
c5f1a937 JF |
3633 | if (name == nil) |
3634 | return nil; | |
3dd53516 | 3635 | @synchronized (self) { |
5a09ae08 JF |
3636 | if (static_cast<pkgDepCache *>(cache_) == NULL) |
3637 | return nil; | |
36bb2ca2 | 3638 | pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); |
13902177 | 3639 | return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:NULL database:self]; |
28ce8704 | 3640 | } } |
36bb2ca2 | 3641 | |
eb30da80 | 3642 | - (id) init { |
ec97ef06 | 3643 | if ((self = [super init]) != nil) { |
5a09ae08 | 3644 | policy_ = NULL; |
36bb2ca2 JF |
3645 | records_ = NULL; |
3646 | resolver_ = NULL; | |
3647 | fetcher_ = NULL; | |
3648 | lock_ = NULL; | |
ec97ef06 | 3649 | |
f79a4512 | 3650 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3651 | |
9f357d11 JF |
3652 | size_t capacity(MetaFile_->active_); |
3653 | if (capacity == 0) | |
3654 | capacity = 16384; | |
3655 | else | |
3656 | capacity += 1024; | |
3657 | ||
3658 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, capacity, NULL); | |
7b33d201 | 3659 | sourceList_ = [NSMutableArray arrayWithCapacity:16]; |
ec97ef06 | 3660 | |
36bb2ca2 | 3661 | int fds[2]; |
ec97ef06 | 3662 | |
77fcccaf JF |
3663 | _assert(pipe(fds) != -1); |
3664 | cydiafd_ = fds[1]; | |
3665 | ||
3666 | _config->Set("APT::Keep-Fds::", cydiafd_); | |
bc8cd583 | 3667 | setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int)); |
77fcccaf JF |
3668 | |
3669 | [NSThread | |
3670 | detachNewThreadSelector:@selector(_readCydia:) | |
3671 | toTarget:self | |
3931b718 | 3672 | withObject:[NSNumber numberWithInt:fds[0]] |
77fcccaf JF |
3673 | ]; |
3674 | ||
36bb2ca2 JF |
3675 | _assert(pipe(fds) != -1); |
3676 | statusfd_ = fds[1]; | |
ec97ef06 | 3677 | |
36bb2ca2 JF |
3678 | [NSThread |
3679 | detachNewThreadSelector:@selector(_readStatus:) | |
3680 | toTarget:self | |
3931b718 | 3681 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3682 | ]; |
ec97ef06 | 3683 | |
8b29f8e6 JF |
3684 | _assert(pipe(fds) != -1); |
3685 | _assert(dup2(fds[0], 0) != -1); | |
3686 | _assert(close(fds[0]) != -1); | |
3687 | ||
3688 | input_ = fdopen(fds[1], "a"); | |
3689 | ||
36bb2ca2 JF |
3690 | _assert(pipe(fds) != -1); |
3691 | _assert(dup2(fds[1], 1) != -1); | |
3692 | _assert(close(fds[1]) != -1); | |
3693 | ||
3694 | [NSThread | |
3695 | detachNewThreadSelector:@selector(_readOutput:) | |
3696 | toTarget:self | |
3931b718 | 3697 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3698 | ]; |
ec97ef06 JF |
3699 | } return self; |
3700 | } | |
3701 | ||
36bb2ca2 JF |
3702 | - (pkgCacheFile &) cache { |
3703 | return cache_; | |
ec97ef06 JF |
3704 | } |
3705 | ||
5a09ae08 JF |
3706 | - (pkgDepCache::Policy *) policy { |
3707 | return policy_; | |
3708 | } | |
3709 | ||
36bb2ca2 JF |
3710 | - (pkgRecords *) records { |
3711 | return records_; | |
ec97ef06 JF |
3712 | } |
3713 | ||
36bb2ca2 JF |
3714 | - (pkgProblemResolver *) resolver { |
3715 | return resolver_; | |
ec97ef06 JF |
3716 | } |
3717 | ||
36bb2ca2 JF |
3718 | - (pkgAcquire &) fetcher { |
3719 | return *fetcher_; | |
ec97ef06 JF |
3720 | } |
3721 | ||
a3328c28 JF |
3722 | - (pkgSourceList &) list { |
3723 | return *list_; | |
3724 | } | |
3725 | ||
36bb2ca2 | 3726 | - (NSArray *) packages { |
077e9d90 | 3727 | return (NSArray *) packages_; |
ec97ef06 JF |
3728 | } |
3729 | ||
7b0ce2da | 3730 | - (NSArray *) sources { |
34f70f5d | 3731 | return sourceList_; |
7b0ce2da JF |
3732 | } |
3733 | ||
d669236d GP |
3734 | - (Source *) sourceWithKey:(NSString *)key { |
3735 | for (Source *source in [self sources]) { | |
3736 | if ([[source key] isEqualToString:key]) | |
3737 | return source; | |
3738 | } return nil; | |
3739 | } | |
3740 | ||
670a0494 JF |
3741 | - (bool) popErrorWithTitle:(NSString *)title { |
3742 | bool fatal(false); | |
670a0494 JF |
3743 | |
3744 | while (!_error->empty()) { | |
3745 | std::string error; | |
3746 | bool warning(!_error->PopMessage(error)); | |
3747 | if (!warning) | |
3748 | fatal = true; | |
cb6e2ccf | 3749 | |
670a0494 JF |
3750 | for (;;) { |
3751 | size_t size(error.size()); | |
3752 | if (size == 0 || error[size - 1] != '\n') | |
3753 | break; | |
3754 | error.resize(size - 1); | |
3755 | } | |
cb6e2ccf | 3756 | |
670a0494 JF |
3757 | lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); |
3758 | ||
b4fd1197 | 3759 | static RegEx no_pubkey("GPG error:.* NO_PUBKEY .*"); |
c8ce71c7 JF |
3760 | if (warning && no_pubkey(error.c_str())) |
3761 | continue; | |
3762 | ||
389133be | 3763 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
670a0494 JF |
3764 | } |
3765 | ||
670a0494 JF |
3766 | return fatal; |
3767 | } | |
3768 | ||
3769 | - (bool) popErrorWithTitle:(NSString *)title forOperation:(bool)success { | |
3770 | return [self popErrorWithTitle:title] || !success; | |
3771 | } | |
3772 | ||
7bc0d825 JF |
3773 | - (bool) popErrorWithTitle:(NSString *)title forReadList:(pkgSourceList &)list { |
3774 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) | |
3775 | return true; | |
f6b68b2c JF |
3776 | if ([self popErrorWithTitle:title forOperation:list.Read(SOURCES_LIST)]) |
3777 | return true; | |
7bc0d825 JF |
3778 | return false; |
3779 | } | |
3780 | ||
d13edf44 | 3781 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
3dd53516 JF |
3782 | @synchronized (self) { |
3783 | ++era_; | |
a1440b10 | 3784 | |
0944377b | 3785 | [self releasePackages]; |
ab3f6a01 | 3786 | |
34f70f5d JF |
3787 | sourceMap_.clear(); |
3788 | [sourceList_ removeAllObjects]; | |
843e75b8 | 3789 | |
36bb2ca2 | 3790 | _error->Discard(); |
5a09ae08 | 3791 | |
36bb2ca2 | 3792 | delete list_; |
5a09ae08 | 3793 | list_ = NULL; |
36bb2ca2 JF |
3794 | manager_ = NULL; |
3795 | delete lock_; | |
5a09ae08 | 3796 | lock_ = NULL; |
36bb2ca2 | 3797 | delete fetcher_; |
5a09ae08 | 3798 | fetcher_ = NULL; |
36bb2ca2 | 3799 | delete resolver_; |
5a09ae08 | 3800 | resolver_ = NULL; |
36bb2ca2 | 3801 | delete records_; |
5a09ae08 JF |
3802 | records_ = NULL; |
3803 | delete policy_; | |
3804 | policy_ = NULL; | |
36bb2ca2 | 3805 | |
5a09ae08 | 3806 | cache_.Close(); |
8b29f8e6 | 3807 | |
64edd9df JF |
3808 | pool_.~CYPool(); |
3809 | new (&pool_) CYPool(); | |
a020a50e | 3810 | |
f79a4512 | 3811 | NSRecycleZone(zone_); |
a020a50e | 3812 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3813 | |
7376b55c JF |
3814 | int chk(creat("/tmp/cydia.chk", 0644)); |
3815 | if (chk != -1) | |
3816 | close(chk); | |
3817 | ||
4ba8f30a JF |
3818 | if (invocation != nil) |
3819 | [invocation invoke]; | |
3820 | ||
670a0494 JF |
3821 | NSString *title(UCLocalize("DATABASE")); |
3822 | ||
0c28a403 | 3823 | list_ = new pkgSourceList(); |
1a83afc6 | 3824 | _profile(reloadDataWithInvocation$ReadMainList) |
7bc0d825 | 3825 | if ([self popErrorWithTitle:title forReadList:*list_]) |
0c28a403 | 3826 | return; |
1a83afc6 | 3827 | _end |
0c28a403 | 3828 | |
1a83afc6 | 3829 | _profile(reloadDataWithInvocation$Source$initWithMetaIndex) |
0c28a403 | 3830 | for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { |
64edd9df | 3831 | Source *object([[[Source alloc] initWithMetaIndex:*source forDatabase:self inPool:&pool_] autorelease]); |
0c28a403 JF |
3832 | [sourceList_ addObject:object]; |
3833 | } | |
1a83afc6 | 3834 | _end |
0c28a403 | 3835 | |
584daea0 | 3836 | _root(_system->Lock()); |
eb0dda0a | 3837 | |
9487f027 | 3838 | _trace(); |
124cea03 | 3839 | OpProgress progress; |
1a83afc6 | 3840 | bool opened; |
2b49cff9 | 3841 | open: |
1a83afc6 | 3842 | _profile(reloadDataWithInvocation$pkgCacheFile) |
b44af625 | 3843 | opened = cache_.Open(progress, false); |
1a83afc6 JF |
3844 | _end |
3845 | if (!opened) { | |
2b49cff9 JF |
3846 | // XXX: what if there are errors, but Open() == true? this should be merged with popError: |
3847 | while (!_error->empty()) { | |
3848 | std::string error; | |
3849 | bool warning(!_error->PopMessage(error)); | |
3850 | ||
3851 | lprintf("cache_.Open():[%s]\n", error.c_str()); | |
3852 | ||
389133be | 3853 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
2b49cff9 JF |
3854 | |
3855 | SEL repair(NULL); | |
3856 | if (false); | |
3857 | else if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ") | |
3858 | repair = @selector(configure); | |
3859 | //else if (error == "The package lists or status file could not be parsed or opened.") | |
3860 | // repair = @selector(update); | |
3861 | // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)") | |
3862 | // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)") | |
3863 | // else if (error == "Malformed Status line") | |
3864 | // else if (error == "The list of sources could not be read.") | |
3865 | ||
3866 | if (repair != NULL) { | |
3867 | _error->Discard(); | |
3868 | [delegate_ repairWithSelector:repair]; | |
3869 | goto open; | |
3870 | } | |
efa53fa9 | 3871 | } |
5a09ae08 | 3872 | |
eb0dda0a | 3873 | _system->UnLock(); |
2b49cff9 | 3874 | return; |
36bb2ca2 | 3875 | } |
9487f027 | 3876 | _trace(); |
36bb2ca2 | 3877 | |
7376b55c JF |
3878 | unlink("/tmp/cydia.chk"); |
3879 | ||
31bc18a7 | 3880 | now_ = [[NSDate date] timeIntervalSince1970]; |
36bb2ca2 | 3881 | |
5a09ae08 | 3882 | policy_ = new pkgDepCache::Policy(); |
36bb2ca2 JF |
3883 | records_ = new pkgRecords(cache_); |
3884 | resolver_ = new pkgProblemResolver(cache_); | |
3885 | fetcher_ = new pkgAcquire(&status_); | |
3886 | lock_ = NULL; | |
3887 | ||
670a0494 | 3888 | if (cache_->DelCount() != 0 || cache_->InstCount() != 0) { |
389133be | 3889 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("COUNTS_NONZERO_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3890 | return; |
3891 | } | |
36bb2ca2 | 3892 | |
1a83afc6 | 3893 | _profile(reloadDataWithInvocation$pkgApplyStatus) |
670a0494 JF |
3894 | if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) |
3895 | return; | |
1a83afc6 | 3896 | _end |
9bedffaa JF |
3897 | |
3898 | if (cache_->BrokenCount() != 0) { | |
1a83afc6 | 3899 | _profile(pkgApplyStatus$pkgFixBroken) |
670a0494 JF |
3900 | if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) |
3901 | return; | |
1a83afc6 | 3902 | _end |
670a0494 JF |
3903 | |
3904 | if (cache_->BrokenCount() != 0) { | |
389133be | 3905 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("STILL_BROKEN_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3906 | return; |
3907 | } | |
3908 | ||
1a83afc6 | 3909 | _profile(pkgApplyStatus$pkgMinimizeUpgrade) |
670a0494 JF |
3910 | if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) |
3911 | return; | |
1a83afc6 | 3912 | _end |
9bedffaa JF |
3913 | } |
3914 | ||
0c28a403 JF |
3915 | for (Source *object in (id) sourceList_) { |
3916 | metaIndex *source([object metaIndex]); | |
3917 | std::vector<pkgIndexFile *> *indices = source->GetIndexFiles(); | |
68d927e2 JF |
3918 | for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index) |
3919 | // XXX: this could be more intelligent | |
3920 | if (dynamic_cast<debPackagesIndex *>(*index) != NULL) { | |
3921 | pkgCache::PkgFileIterator cached((*index)->FindInCache(cache_)); | |
34f70f5d JF |
3922 | if (!cached.end()) |
3923 | sourceMap_[cached->ID] = object; | |
68d927e2 | 3924 | } |
36bb2ca2 | 3925 | } |
68d927e2 | 3926 | |
677b8415 | 3927 | { |
9fcbca29 | 3928 | /*std::vector<Package *> packages; |
677b8415 | 3929 | packages.reserve(std::max(10000U, [packages_ count] + 1000)); |
9fcbca29 JF |
3930 | packages_ = nil;*/ |
3931 | ||
1a83afc6 | 3932 | _profile(reloadDataWithInvocation$packageWithIterator) |
677b8415 | 3933 | for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) |
64edd9df | 3934 | if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:&pool_ database:self]) |
9fcbca29 | 3935 | //packages.push_back(package); |
7b33d201 | 3936 | CFArrayAppendValue(packages_, CFRetain(package)); |
1a83afc6 | 3937 | _end |
677b8415 | 3938 | |
677b8415 | 3939 | |
9fcbca29 | 3940 | /*if (packages.empty()) |
677b8415 JF |
3941 | packages_ = [[NSArray alloc] init]; |
3942 | else | |
3943 | packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; | |
9fcbca29 JF |
3944 | _trace();*/ |
3945 | ||
1a83afc6 | 3946 | _profile(reloadDataWithInvocation$radix$8) |
9f0facbc | 3947 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(8)]; |
1a83afc6 JF |
3948 | _end |
3949 | ||
3950 | _profile(reloadDataWithInvocation$radix$4) | |
de42680b | 3951 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(4)]; |
1a83afc6 | 3952 | _end |
9fcbca29 | 3953 | |
1a83afc6 JF |
3954 | _profile(reloadDataWithInvocation$radix$0) |
3955 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(0)]; | |
3956 | _end | |
9fcbca29 | 3957 | |
1a83afc6 JF |
3958 | _profile(reloadDataWithInvocation$insertion) |
3959 | CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); | |
3960 | _end | |
9fcbca29 | 3961 | |
1a83afc6 JF |
3962 | /*_profile(reloadDataWithInvocation$CFQSortArray) |
3963 | CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare_), NULL); | |
3964 | _end*/ | |
9fcbca29 | 3965 | |
1a83afc6 JF |
3966 | /*_profile(reloadDataWithInvocation$stdsort) |
3967 | std::sort(packages.begin(), packages.end(), PackageNameOrdering()); | |
3968 | _end*/ | |
eef4ccaf | 3969 | |
1a83afc6 JF |
3970 | /*_profile(reloadDataWithInvocation$CFArraySortValues) |
3971 | CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); | |
3972 | _end*/ | |
9fcbca29 | 3973 | |
1a83afc6 JF |
3974 | /*_profile(reloadDataWithInvocation$sortUsingFunction) |
3975 | [packages_ sortUsingFunction:reinterpret_cast<NSComparisonResult (*)(id, id, void *)>(&PackageNameCompare) context:NULL]; | |
3976 | _end*/ | |
677b8415 | 3977 | |
cd95e390 JF |
3978 | |
3979 | size_t count(CFArrayGetCount(packages_)); | |
9f357d11 | 3980 | MetaFile_->active_ = count; |
cd95e390 JF |
3981 | for (size_t index(0); index != count; ++index) |
3982 | [(Package *) CFArrayGetValueAtIndex(packages_, index) setIndex:index]; | |
677b8415 | 3983 | } |
d13edf44 | 3984 | } } |
ec97ef06 | 3985 | |
c6ca67ba JF |
3986 | - (void) clear { |
3987 | @synchronized (self) { | |
3988 | delete resolver_; | |
3989 | resolver_ = new pkgProblemResolver(cache_); | |
3990 | ||
50c1653e JF |
3991 | for (pkgCache::PkgIterator iterator(cache_->PkgBegin()); !iterator.end(); ++iterator) |
3992 | if (!cache_[iterator].Keep()) | |
c6ca67ba | 3993 | cache_->MarkKeep(iterator, false); |
50c1653e | 3994 | else if ((cache_[iterator].iFlags & pkgDepCache::ReInstall) != 0) |
c6ca67ba | 3995 | cache_->SetReInstall(iterator, false); |
c6ca67ba JF |
3996 | } } |
3997 | ||
5a09ae08 | 3998 | - (void) configure { |
232b396b | 3999 | NSString *dpkg = [NSString stringWithFormat:@"/usr/libexec/cydo --configure -a --status-fd %u", statusfd_]; |
985d2dff | 4000 | _trace(); |
232b396b | 4001 | system([dpkg UTF8String]); |
985d2dff | 4002 | _trace(); |
5a09ae08 JF |
4003 | } |
4004 | ||
670a0494 | 4005 | - (bool) clean { |
0517651f | 4006 | @synchronized (self) { |
670a0494 | 4007 | // XXX: I don't remember this condition |
77fcccaf | 4008 | if (lock_ != NULL) |
670a0494 | 4009 | return false; |
77fcccaf JF |
4010 | |
4011 | FileFd Lock; | |
4012 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
4013 | |
4014 | NSString *title(UCLocalize("CLEAN_ARCHIVES")); | |
4015 | ||
4016 | if ([self popErrorWithTitle:title]) | |
4017 | return false; | |
77fcccaf JF |
4018 | |
4019 | pkgAcquire fetcher; | |
4020 | fetcher.Clean(_config->FindDir("Dir::Cache::Archives")); | |
4021 | ||
9f9ae81c | 4022 | CydiaLogCleaner cleaner; |
670a0494 JF |
4023 | if ([self popErrorWithTitle:title forOperation:cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)]) |
4024 | return false; | |
4025 | ||
4026 | return true; | |
0517651f | 4027 | } } |
77fcccaf | 4028 | |
670a0494 | 4029 | - (bool) prepare { |
744f398e JF |
4030 | fetcher_->Shutdown(); |
4031 | ||
36bb2ca2 JF |
4032 | pkgRecords records(cache_); |
4033 | ||
4034 | lock_ = new FileFd(); | |
4035 | lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
4036 | |
4037 | NSString *title(UCLocalize("PREPARE_ARCHIVES")); | |
4038 | ||
4039 | if ([self popErrorWithTitle:title]) | |
4040 | return false; | |
36bb2ca2 JF |
4041 | |
4042 | pkgSourceList list; | |
7bc0d825 | 4043 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4044 | return false; |
36bb2ca2 JF |
4045 | |
4046 | manager_ = (_system->CreatePM(cache_)); | |
670a0494 JF |
4047 | if ([self popErrorWithTitle:title forOperation:manager_->GetArchives(fetcher_, &list, &records)]) |
4048 | return false; | |
4049 | ||
4050 | return true; | |
ec97ef06 JF |
4051 | } |
4052 | ||
36bb2ca2 | 4053 | - (void) perform { |
be860cc8 JF |
4054 | bool substrate(RestartSubstrate_); |
4055 | RestartSubstrate_ = false; | |
4056 | ||
670a0494 JF |
4057 | NSString *title(UCLocalize("PERFORM_SELECTIONS")); |
4058 | ||
a72074b2 JF |
4059 | NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { |
4060 | pkgSourceList list; | |
7bc0d825 | 4061 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4062 | return; |
a72074b2 JF |
4063 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
4064 | [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
4065 | } | |
a0be02eb | 4066 | |
dcaecde2 | 4067 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
7ffd70fd | 4068 | |
eeb9b112 JF |
4069 | if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) { |
4070 | _trace(); | |
6e90508f | 4071 | [self popErrorWithTitle:title]; |
36bb2ca2 | 4072 | return; |
eeb9b112 JF |
4073 | } |
4074 | ||
4075 | bool failed = false; | |
4076 | for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) { | |
4077 | if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete) | |
4078 | continue; | |
6204f56a JF |
4079 | if ((*item)->Status == pkgAcquire::Item::StatIdle) |
4080 | continue; | |
eeb9b112 | 4081 | |
680a3c3c JF |
4082 | std::string uri = (*item)->DescURI(); |
4083 | std::string error = (*item)->ErrorText; | |
4084 | ||
4085 | lprintf("pAf:%s:%s\n", uri.c_str(), error.c_str()); | |
eeb9b112 | 4086 | failed = true; |
680a3c3c JF |
4087 | |
4088 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError]); | |
4089 | [delegate_ addProgressEventOnMainThread:event forTask:title]; | |
eeb9b112 JF |
4090 | } |
4091 | ||
dcaecde2 | 4092 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
54043703 | 4093 | |
eeb9b112 JF |
4094 | if (failed) { |
4095 | _trace(); | |
4096 | return; | |
4097 | } | |
36bb2ca2 | 4098 | |
be860cc8 JF |
4099 | if (substrate) |
4100 | RestartSubstrate_ = true; | |
4101 | ||
36bb2ca2 | 4102 | _system->UnLock(); |
232b396b | 4103 | pkgPackageManager::OrderResult result(manager_->DoInstall(statusfd_)); |
471683a3 | 4104 | if ([self popErrorWithTitle:title]) |
36bb2ca2 | 4105 | return; |
eeb9b112 JF |
4106 | |
4107 | if (result == pkgPackageManager::Failed) { | |
4108 | _trace(); | |
36bb2ca2 | 4109 | return; |
eeb9b112 JF |
4110 | } |
4111 | ||
4112 | if (result != pkgPackageManager::Completed) { | |
4113 | _trace(); | |
36bb2ca2 | 4114 | return; |
eeb9b112 | 4115 | } |
a0be02eb | 4116 | |
a72074b2 JF |
4117 | NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { |
4118 | pkgSourceList list; | |
7bc0d825 | 4119 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4120 | return; |
a72074b2 JF |
4121 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
4122 | [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
4123 | } | |
4124 | ||
4125 | if (![before isEqualToArray:after]) | |
4126 | [self update]; | |
ec97ef06 JF |
4127 | } |
4128 | ||
670a0494 JF |
4129 | - (bool) upgrade { |
4130 | NSString *title(UCLocalize("UPGRADE")); | |
4131 | if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) | |
4132 | return false; | |
4133 | return true; | |
c7c6384e JF |
4134 | } |
4135 | ||
36bb2ca2 JF |
4136 | - (void) update { |
4137 | [self updateWithStatus:status_]; | |
4138 | } | |
b4d89997 | 4139 | |
21ac0ce2 | 4140 | - (void) updateWithStatus:(CancelStatus &)status { |
670a0494 JF |
4141 | NSString *title(UCLocalize("REFRESHING_DATA")); |
4142 | ||
36bb2ca2 | 4143 | pkgSourceList list; |
7bc0d825 | 4144 | if ([self popErrorWithTitle:title forReadList:list]) |
53ca7fdd | 4145 | return; |
b4d89997 | 4146 | |
36bb2ca2 JF |
4147 | FileFd lock; |
4148 | lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
670a0494 JF |
4149 | if ([self popErrorWithTitle:title]) |
4150 | return; | |
18873623 | 4151 | |
aaae308d JF |
4152 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
4153 | ||
fc470c15 JF |
4154 | bool success(ListUpdate(status, list, PulseInterval_)); |
4155 | if (status.WasCancelled()) | |
4156 | _error->Discard(); | |
aa42c612 | 4157 | else { |
fc470c15 | 4158 | [self popErrorWithTitle:title forOperation:success]; |
aa42c612 JF |
4159 | [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"]; |
4160 | Changed_ = true; | |
4161 | } | |
36bb2ca2 | 4162 | |
aaae308d | 4163 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
b4d89997 JF |
4164 | } |
4165 | ||
6915b806 | 4166 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate { |
36bb2ca2 | 4167 | delegate_ = delegate; |
6915b806 JF |
4168 | } |
4169 | ||
4170 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate { | |
4171 | progress_ = delegate; | |
36bb2ca2 | 4172 | status_.setDelegate(delegate); |
36bb2ca2 | 4173 | } |
b4d89997 | 4174 | |
6915b806 JF |
4175 | - (NSObject<ProgressDelegate> *) progressDelegate { |
4176 | return progress_; | |
4177 | } | |
4178 | ||
7376b55c | 4179 | - (Source *) getSource:(pkgCache::PkgFileIterator)file { |
34f70f5d JF |
4180 | SourceMap::const_iterator i(sourceMap_.find(file->ID)); |
4181 | return i == sourceMap_.end() ? nil : i->second; | |
b19871dd JF |
4182 | } |
4183 | ||
21ac0ce2 JF |
4184 | - (void) setFetch:(bool)fetch forURI:(const char *)uri { |
4185 | for (Source *source in (id) sourceList_) | |
4186 | [source setFetch:fetch forURI:uri]; | |
4187 | } | |
4188 | ||
9ed626f1 JF |
4189 | - (void) resetFetch { |
4190 | for (Source *source in (id) sourceList_) | |
4191 | [source resetFetch]; | |
4192 | } | |
4193 | ||
fe33a23e | 4194 | - (NSString *) mappedSectionForPointer:(const char *)section { |
4905df00 | 4195 | _H<NSString> *mapped; |
fe33a23e | 4196 | |
4905df00 JF |
4197 | _profile(Database$mappedSectionForPointer$Cache) |
4198 | mapped = §ions_[section]; | |
4199 | _end | |
4200 | ||
4201 | if (*mapped == NULL) { | |
fe33a23e JF |
4202 | size_t length(strlen(section)); |
4203 | char spaced[length + 1]; | |
4204 | ||
4205 | _profile(Database$mappedSectionForPointer$Replace) | |
4206 | for (size_t index(0); index != length; ++index) | |
4207 | spaced[index] = section[index] == '_' ? ' ' : section[index]; | |
4208 | spaced[length] = '\0'; | |
4209 | _end | |
4210 | ||
4211 | NSString *string; | |
4212 | ||
4213 | _profile(Database$mappedSectionForPointer$stringWithUTF8String) | |
4214 | string = [NSString stringWithUTF8String:spaced]; | |
4215 | _end | |
4216 | ||
4217 | _profile(Database$mappedSectionForPointer$Map) | |
4905df00 | 4218 | string = [SectionMap_ objectForKey:string] ?: string; |
fe33a23e | 4219 | _end |
4905df00 JF |
4220 | |
4221 | *mapped = string; | |
4222 | } return *mapped; | |
fe33a23e JF |
4223 | } |
4224 | ||
36bb2ca2 JF |
4225 | @end |
4226 | /* }}} */ | |
b4d89997 | 4227 | |
7b33d201 | 4228 | static _H<NSMutableSet> Diversions_; |
7256476b | 4229 | |
775deead | 4230 | @interface Diversion : NSObject { |
b4fd1197 | 4231 | RegEx pattern_; |
775deead JF |
4232 | _H<NSString> key_; |
4233 | _H<NSString> format_; | |
4234 | } | |
4235 | ||
4236 | @end | |
4237 | ||
4238 | @implementation Diversion | |
4239 | ||
4240 | - (id) initWithFrom:(NSString *)from to:(NSString *)to { | |
4241 | if ((self = [super init]) != nil) { | |
4242 | pattern_ = [from UTF8String]; | |
4243 | key_ = from; | |
4244 | format_ = to; | |
4245 | } return self; | |
4246 | } | |
4247 | ||
4248 | - (NSString *) divert:(NSString *)url { | |
8ea598c0 | 4249 | return !pattern_(url) ? nil : pattern_->*format_; |
775deead JF |
4250 | } |
4251 | ||
7256476b JF |
4252 | + (NSURL *) divertURL:(NSURL *)url { |
4253 | divert: | |
4254 | NSString *href([url absoluteString]); | |
4255 | ||
7b33d201 | 4256 | for (Diversion *diversion in (id) Diversions_) |
7256476b | 4257 | if (NSString *diverted = [diversion divert:href]) { |
85d5d452 JF |
4258 | #if !ForRelease |
4259 | NSLog(@"div: %@", diverted); | |
4260 | #endif | |
7256476b JF |
4261 | url = [NSURL URLWithString:diverted]; |
4262 | goto divert; | |
4263 | } | |
4264 | ||
4265 | return url; | |
4266 | } | |
4267 | ||
775deead JF |
4268 | - (NSString *) key { |
4269 | return key_; | |
4270 | } | |
4271 | ||
4272 | - (NSUInteger) hash { | |
4273 | return [key_ hash]; | |
4274 | } | |
4275 | ||
4276 | - (BOOL) isEqual:(Diversion *)object { | |
4277 | return self == object || [self class] == [object class] && [key_ isEqual:[object key]]; | |
4278 | } | |
4279 | ||
4280 | @end | |
4281 | ||
43f3d7f6 | 4282 | @interface CydiaObject : NSObject { |
4cc9e99a | 4283 | _H<CyteWebViewController> indirect_; |
3931b718 | 4284 | _transient id delegate_; |
43f3d7f6 | 4285 | } |
c390d3ab | 4286 | |
43f3d7f6 | 4287 | - (id) initWithDelegate:(IndirectDelegate *)indirect; |
6840bff3 | 4288 | |
c390d3ab JF |
4289 | @end |
4290 | ||
9e130bc2 JF |
4291 | @class CydiaObject; |
4292 | ||
09e89a8a | 4293 | @interface CydiaWebViewController : CyteWebViewController { |
7b33d201 | 4294 | _H<CydiaObject> cydia_; |
775deead JF |
4295 | } |
4296 | ||
4297 | + (void) addDiversion:(Diversion *)diversion; | |
85ae5f42 | 4298 | + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request; |
9e130bc2 JF |
4299 | + (void) didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame withCydia:(CydiaObject *)cydia; |
4300 | - (void) setDelegate:(id)delegate; | |
775deead JF |
4301 | |
4302 | @end | |
4303 | ||
775deead | 4304 | /* Web Scripting {{{ */ |
43f3d7f6 | 4305 | @implementation CydiaObject |
c390d3ab | 4306 | |
43f3d7f6 JF |
4307 | - (id) initWithDelegate:(IndirectDelegate *)indirect { |
4308 | if ((self = [super init]) != nil) { | |
4cc9e99a | 4309 | indirect_ = (CyteWebViewController *) indirect; |
43f3d7f6 JF |
4310 | } return self; |
4311 | } | |
4312 | ||
77801ff1 JF |
4313 | - (void) setDelegate:(id)delegate { |
4314 | delegate_ = delegate; | |
4315 | } | |
4316 | ||
43f3d7f6 | 4317 | + (NSArray *) _attributeKeys { |
e58ff941 | 4318 | return [NSArray arrayWithObjects: |
6ffdaae3 | 4319 | @"bbsnum", |
e967efd5 JF |
4320 | @"build", |
4321 | @"coreFoundationVersionNumber", | |
e58ff941 | 4322 | @"device", |
56296da0 | 4323 | @"ecid", |
93d6d318 JF |
4324 | @"firmware", |
4325 | @"hostname", | |
4326 | @"idiom", | |
f87cac81 JF |
4327 | @"mcc", |
4328 | @"mnc", | |
56296da0 | 4329 | @"model", |
2656c992 | 4330 | @"operator", |
56296da0 | 4331 | @"role", |
e58ff941 | 4332 | @"serial", |
3ea82d91 | 4333 | @"token", |
bf1d5e69 | 4334 | @"version", |
e58ff941 | 4335 | nil]; |
43f3d7f6 JF |
4336 | } |
4337 | ||
4338 | - (NSArray *) attributeKeys { | |
4339 | return [[self class] _attributeKeys]; | |
c390d3ab JF |
4340 | } |
4341 | ||
43f3d7f6 JF |
4342 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
4343 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
c390d3ab | 4344 | } |
43f3d7f6 | 4345 | |
bf1d5e69 | 4346 | - (NSString *) version { |
3d45bad1 | 4347 | return Cydia_; |
bf1d5e69 JF |
4348 | } |
4349 | ||
e967efd5 JF |
4350 | - (NSString *) build { |
4351 | return System_; | |
4352 | } | |
4353 | ||
4354 | - (NSString *) coreFoundationVersionNumber { | |
4355 | return [NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber]; | |
4356 | } | |
4357 | ||
43f3d7f6 | 4358 | - (NSString *) device { |
7c80833f | 4359 | return UniqueIdentifier(); |
c390d3ab JF |
4360 | } |
4361 | ||
93d6d318 JF |
4362 | - (NSString *) firmware { |
4363 | return [[UIDevice currentDevice] systemVersion]; | |
4364 | } | |
4365 | ||
4366 | - (NSString *) hostname { | |
4367 | return [[UIDevice currentDevice] name]; | |
4368 | } | |
4369 | ||
4370 | - (NSString *) idiom { | |
c138614d | 4371 | return (id) Idiom_ ?: [NSNull null]; |
93d6d318 JF |
4372 | } |
4373 | ||
f87cac81 JF |
4374 | - (NSString *) mcc { |
4375 | if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode"))) | |
4376 | return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault) autorelease]; | |
4377 | return nil; | |
4378 | } | |
4379 | ||
4380 | - (NSString *) mnc { | |
4381 | if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberNetworkCode"))) | |
4382 | return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(kCFAllocatorDefault) autorelease]; | |
4383 | return nil; | |
56296da0 JF |
4384 | } |
4385 | ||
2656c992 JF |
4386 | - (NSString *) operator { |
4387 | if (CFStringRef (*$CTRegistrationCopyOperatorName)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTRegistrationCopyOperatorName"))) | |
4388 | return [(NSString *) (*$CTRegistrationCopyOperatorName)(kCFAllocatorDefault) autorelease]; | |
4389 | return nil; | |
4390 | } | |
4391 | ||
6ffdaae3 JF |
4392 | - (NSString *) bbsnum { |
4393 | return (id) BBSNum_ ?: [NSNull null]; | |
4394 | } | |
4395 | ||
56296da0 | 4396 | - (NSString *) ecid { |
849cd6bf | 4397 | return (id) ChipID_ ?: [NSNull null]; |
affeffc7 JF |
4398 | } |
4399 | ||
43f3d7f6 | 4400 | - (NSString *) serial { |
56296da0 | 4401 | return SerialNumber_; |
43f3d7f6 | 4402 | } |
86316a91 | 4403 | |
56296da0 | 4404 | - (NSString *) role { |
4121c5e0 | 4405 | return (id) [NSNull null]; |
affeffc7 JF |
4406 | } |
4407 | ||
56296da0 JF |
4408 | - (NSString *) model { |
4409 | return [NSString stringWithUTF8String:Machine_]; | |
43f3d7f6 | 4410 | } |
43f3d7f6 | 4411 | |
3ea82d91 JF |
4412 | - (NSString *) token { |
4413 | return (id) Token_ ?: [NSNull null]; | |
4414 | } | |
4415 | ||
43f3d7f6 | 4416 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 4417 | if (false); |
cfc7b442 JF |
4418 | else if (selector == @selector(addBridgedHost:)) |
4419 | return @"addBridgedHost"; | |
2e1652a9 JF |
4420 | else if (selector == @selector(addInsecureHost:)) |
4421 | return @"addInsecureHost"; | |
3f428e4e JF |
4422 | else if (selector == @selector(addInternalRedirect::)) |
4423 | return @"addInternalRedirect"; | |
e4b48f2f | 4424 | else if (selector == @selector(addPipelinedHost:scheme:)) |
834c18a4 | 4425 | return @"addPipelinedHost"; |
33e30380 JF |
4426 | else if (selector == @selector(addSource:::)) |
4427 | return @"addSource"; | |
247bedb6 JF |
4428 | else if (selector == @selector(addTokenHost:)) |
4429 | return @"addTokenHost"; | |
b088c0cd JF |
4430 | else if (selector == @selector(addTrivialSource:)) |
4431 | return @"addTrivialSource"; | |
e58ff941 | 4432 | else if (selector == @selector(close)) |
43f3d7f6 | 4433 | return @"close"; |
e58ff941 JF |
4434 | else if (selector == @selector(du:)) |
4435 | return @"du"; | |
4436 | else if (selector == @selector(stringWithFormat:arguments:)) | |
4437 | return @"format"; | |
5c32f89e | 4438 | else if (selector == @selector(getAllSources)) |
caf0475e | 4439 | return @"getAllSources"; |
8c5b623f JF |
4440 | else if (selector == @selector(getApplicationInfo:value:)) |
4441 | return @"getApplicationInfoValue"; | |
5bc1277a JF |
4442 | else if (selector == @selector(getKernelNumber:)) |
4443 | return @"getKernelNumber"; | |
4444 | else if (selector == @selector(getKernelString:)) | |
4445 | return @"getKernelString"; | |
8cc8eb1c JF |
4446 | else if (selector == @selector(getInstalledPackages)) |
4447 | return @"getInstalledPackages"; | |
c31d7cdc JF |
4448 | else if (selector == @selector(getIORegistryEntry::)) |
4449 | return @"getIORegistryEntry"; | |
948db680 JF |
4450 | else if (selector == @selector(getLocaleIdentifier)) |
4451 | return @"getLocaleIdentifier"; | |
4452 | else if (selector == @selector(getPreferredLanguages)) | |
4453 | return @"getPreferredLanguages"; | |
43f3d7f6 JF |
4454 | else if (selector == @selector(getPackageById:)) |
4455 | return @"getPackageById"; | |
37fa9338 JF |
4456 | else if (selector == @selector(getMetadataKeys)) |
4457 | return @"getMetadataKeys"; | |
b3c8e69c JF |
4458 | else if (selector == @selector(getMetadataValue:)) |
4459 | return @"getMetadataValue"; | |
ef974f52 JF |
4460 | else if (selector == @selector(getSessionValue:)) |
4461 | return @"getSessionValue"; | |
77801ff1 JF |
4462 | else if (selector == @selector(installPackages:)) |
4463 | return @"installPackages"; | |
518a552a JF |
4464 | else if (selector == @selector(isReachable:)) |
4465 | return @"isReachable"; | |
e58ff941 JF |
4466 | else if (selector == @selector(localizedStringForKey:value:table:)) |
4467 | return @"localize"; | |
8d497e2a JF |
4468 | else if (selector == @selector(popViewController:)) |
4469 | return @"popViewController"; | |
b088c0cd JF |
4470 | else if (selector == @selector(refreshSources)) |
4471 | return @"refreshSources"; | |
aa1e1906 JF |
4472 | else if (selector == @selector(registerFrame:)) |
4473 | return @"registerFrame"; | |
ed5566c7 JF |
4474 | else if (selector == @selector(removeButton)) |
4475 | return @"removeButton"; | |
33e30380 JF |
4476 | else if (selector == @selector(saveConfig)) |
4477 | return @"saveConfig"; | |
b3c8e69c JF |
4478 | else if (selector == @selector(setMetadataValue::)) |
4479 | return @"setMetadataValue"; | |
ef974f52 JF |
4480 | else if (selector == @selector(setSessionValue::)) |
4481 | return @"setSessionValue"; | |
8a126074 JF |
4482 | else if (selector == @selector(substitutePackageNames:)) |
4483 | return @"substitutePackageNames"; | |
8e3b68d4 JF |
4484 | else if (selector == @selector(scrollToBottom:)) |
4485 | return @"scrollToBottom"; | |
8366df5e JF |
4486 | else if (selector == @selector(setAllowsNavigationAction:)) |
4487 | return @"setAllowsNavigationAction"; | |
c31c825d JF |
4488 | else if (selector == @selector(setBadgeValue:)) |
4489 | return @"setBadgeValue"; | |
43f3d7f6 JF |
4490 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) |
4491 | return @"setButtonImage"; | |
4492 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
4493 | return @"setButtonTitle"; | |
b8a5d89d JF |
4494 | else if (selector == @selector(setHidesBackButton:)) |
4495 | return @"setHidesBackButton"; | |
5cdfcd6f JF |
4496 | else if (selector == @selector(setHidesNavigationBar:)) |
4497 | return @"setHidesNavigationBar"; | |
82406217 JF |
4498 | else if (selector == @selector(setNavigationBarStyle:)) |
4499 | return @"setNavigationBarStyle"; | |
00984204 JF |
4500 | else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:)) |
4501 | return @"setNavigationBarTintColor"; | |
36a20e14 JF |
4502 | else if (selector == @selector(setPasteboardString:)) |
4503 | return @"setPasteboardString"; | |
4504 | else if (selector == @selector(setPasteboardURL:)) | |
4505 | return @"setPasteboardURL"; | |
db698f42 JF |
4506 | else if (selector == @selector(setScrollAlwaysBounceVertical:)) |
4507 | return @"setScrollAlwaysBounceVertical"; | |
4886cc81 JF |
4508 | else if (selector == @selector(setScrollIndicatorStyle:)) |
4509 | return @"setScrollIndicatorStyle"; | |
ef055c6c JF |
4510 | else if (selector == @selector(setToken:)) |
4511 | return @"setToken"; | |
43f3d7f6 JF |
4512 | else if (selector == @selector(setViewportWidth:)) |
4513 | return @"setViewportWidth"; | |
43f3d7f6 JF |
4514 | else if (selector == @selector(statfs:)) |
4515 | return @"statfs"; | |
e58ff941 JF |
4516 | else if (selector == @selector(supports:)) |
4517 | return @"supports"; | |
7c218781 JF |
4518 | else if (selector == @selector(unload)) |
4519 | return @"unload"; | |
c390d3ab | 4520 | else |
43f3d7f6 JF |
4521 | return nil; |
4522 | } | |
4523 | ||
4524 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
4525 | return [self webScriptNameForSelector:selector] == nil; | |
c390d3ab JF |
4526 | } |
4527 | ||
43f3d7f6 JF |
4528 | - (BOOL) supports:(NSString *)feature { |
4529 | return [feature isEqualToString:@"window.open"]; | |
4530 | } | |
c390d3ab | 4531 | |
7c218781 JF |
4532 | - (void) unload { |
4533 | [delegate_ performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO]; | |
4534 | } | |
4535 | ||
db698f42 JF |
4536 | - (void) setScrollAlwaysBounceVertical:(NSNumber *)value { |
4537 | [indirect_ performSelectorOnMainThread:@selector(setScrollAlwaysBounceVerticalNumber:) withObject:value waitUntilDone:NO]; | |
4538 | } | |
4539 | ||
4886cc81 JF |
4540 | - (void) setScrollIndicatorStyle:(NSString *)style { |
4541 | [indirect_ performSelectorOnMainThread:@selector(setScrollIndicatorStyleWithName:) withObject:style waitUntilDone:NO]; | |
4542 | } | |
4543 | ||
3f428e4e | 4544 | - (void) addInternalRedirect:(NSString *)from :(NSString *)to { |
a576488f | 4545 | [CydiaWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO]; |
775deead JF |
4546 | } |
4547 | ||
8c5b623f JF |
4548 | - (NSDictionary *) getApplicationInfo:(NSString *)display value:(NSString *)key { |
4549 | char path[1024]; | |
4550 | if (SBBundlePathForDisplayIdentifier(SBSSpringBoardServerPort(), [display UTF8String], path) != 0) | |
4551 | return (id) [NSNull null]; | |
4552 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:[[NSString stringWithUTF8String:path] stringByAppendingString:@"/Info.plist"]]); | |
4553 | if (info == nil) | |
4554 | return (id) [NSNull null]; | |
4555 | return [info objectForKey:key]; | |
4556 | } | |
4557 | ||
5bc1277a JF |
4558 | - (NSNumber *) getKernelNumber:(NSString *)name { |
4559 | const char *string([name UTF8String]); | |
4560 | ||
4561 | size_t size; | |
4562 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
4563 | return (id) [NSNull null]; | |
4564 | ||
4565 | if (size != sizeof(int)) | |
4566 | return (id) [NSNull null]; | |
4567 | ||
4568 | int value; | |
4569 | if (sysctlbyname(string, &value, &size, NULL, 0) == -1) | |
4570 | return (id) [NSNull null]; | |
4571 | ||
4572 | return [NSNumber numberWithInt:value]; | |
4573 | } | |
4574 | ||
4575 | - (NSString *) getKernelString:(NSString *)name { | |
4576 | const char *string([name UTF8String]); | |
4577 | ||
4578 | size_t size; | |
4579 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
4580 | return (id) [NSNull null]; | |
4581 | ||
4582 | char value[size + 1]; | |
4583 | if (sysctlbyname(string, value, &size, NULL, 0) == -1) | |
4584 | return (id) [NSNull null]; | |
4585 | ||
4586 | // XXX: just in case you request something ludicrous | |
4587 | value[size] = '\0'; | |
4588 | ||
4589 | return [NSString stringWithCString:value]; | |
4590 | } | |
4591 | ||
c31d7cdc JF |
4592 | - (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry { |
4593 | NSObject *value(CYIOGetValue([path UTF8String], entry)); | |
4594 | ||
4595 | if (value != nil) | |
4596 | if ([value isKindOfClass:[NSData class]]) | |
4597 | value = CYHex((NSData *) value); | |
4598 | ||
4599 | return value; | |
4600 | } | |
4601 | ||
37fa9338 JF |
4602 | - (NSArray *) getMetadataKeys { |
4603 | @synchronized (Values_) { | |
4604 | return [Values_ allKeys]; | |
4605 | } } | |
4606 | ||
aa1e1906 JF |
4607 | - (void) registerFrame:(DOMHTMLIFrameElement *)iframe { |
4608 | WebFrame *frame([iframe contentFrame]); | |
4609 | [indirect_ registerFrame:frame]; | |
4610 | } | |
4611 | ||
b3c8e69c JF |
4612 | - (id) getMetadataValue:(NSString *)key { |
4613 | @synchronized (Values_) { | |
4614 | return [Values_ objectForKey:key]; | |
4615 | } } | |
4616 | ||
4617 | - (void) setMetadataValue:(NSString *)key :(NSString *)value { | |
4618 | @synchronized (Values_) { | |
33e30380 | 4619 | if (value == nil || value == (id) [WebUndefined undefined] || value == (id) [NSNull null]) |
b3c8e69c JF |
4620 | [Values_ removeObjectForKey:key]; |
4621 | else | |
4622 | [Values_ setObject:value forKey:key]; | |
4623 | ||
4624 | [delegate_ performSelectorOnMainThread:@selector(updateValues) withObject:nil waitUntilDone:YES]; | |
4625 | } } | |
4626 | ||
ef974f52 JF |
4627 | - (id) getSessionValue:(NSString *)key { |
4628 | @synchronized (SessionData_) { | |
4629 | return [SessionData_ objectForKey:key]; | |
4630 | } } | |
4631 | ||
4632 | - (void) setSessionValue:(NSString *)key :(NSString *)value { | |
4633 | @synchronized (SessionData_) { | |
4634 | if (value == (id) [WebUndefined undefined]) | |
4635 | [SessionData_ removeObjectForKey:key]; | |
4636 | else | |
4637 | [SessionData_ setObject:value forKey:key]; | |
4638 | } } | |
4639 | ||
cfc7b442 | 4640 | - (void) addBridgedHost:(NSString *)host { |
48f1762f JF |
4641 | @synchronized (HostConfig_) { |
4642 | [BridgedHosts_ addObject:host]; | |
4643 | } } | |
5df7ecfb | 4644 | |
2e1652a9 JF |
4645 | - (void) addInsecureHost:(NSString *)host { |
4646 | @synchronized (HostConfig_) { | |
4647 | [InsecureHosts_ addObject:host]; | |
4648 | } } | |
4649 | ||
247bedb6 JF |
4650 | - (void) addTokenHost:(NSString *)host { |
4651 | @synchronized (HostConfig_) { | |
4652 | [TokenHosts_ addObject:host]; | |
4653 | } } | |
4654 | ||
e4b48f2f | 4655 | - (void) addPipelinedHost:(NSString *)host scheme:(NSString *)scheme { |
48f1762f | 4656 | @synchronized (HostConfig_) { |
e4b48f2f JF |
4657 | if (scheme != (id) [WebUndefined undefined]) |
4658 | host = [NSString stringWithFormat:@"%@:%@", [scheme lowercaseString], host]; | |
4659 | ||
48f1762f JF |
4660 | [PipelinedHosts_ addObject:host]; |
4661 | } } | |
834c18a4 | 4662 | |
8d497e2a JF |
4663 | - (void) popViewController:(NSNumber *)value { |
4664 | if (value == (id) [WebUndefined undefined]) | |
4665 | value = [NSNumber numberWithBool:YES]; | |
4666 | [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO]; | |
4667 | } | |
4668 | ||
33e30380 JF |
4669 | - (void) addSource:(NSString *)href :(NSString *)distribution :(WebScriptObject *)sections { |
4670 | NSMutableArray *array([NSMutableArray arrayWithCapacity:[sections count]]); | |
4671 | ||
4672 | for (NSString *section in sections) | |
4673 | [array addObject:section]; | |
4674 | ||
4675 | [delegate_ performSelectorOnMainThread:@selector(addSource:) withObject:[NSMutableDictionary dictionaryWithObjectsAndKeys: | |
4676 | @"deb", @"Type", | |
4677 | href, @"URI", | |
4678 | distribution, @"Distribution", | |
4679 | array, @"Sections", | |
4680 | nil] waitUntilDone:NO]; | |
4681 | } | |
4682 | ||
b088c0cd | 4683 | - (void) addTrivialSource:(NSString *)href { |
a1d85d42 | 4684 | [delegate_ performSelectorOnMainThread:@selector(addTrivialSource:) withObject:href waitUntilDone:NO]; |
b088c0cd JF |
4685 | } |
4686 | ||
4687 | - (void) refreshSources { | |
4688 | [delegate_ performSelectorOnMainThread:@selector(syncData) withObject:nil waitUntilDone:NO]; | |
4689 | } | |
4690 | ||
33e30380 JF |
4691 | - (void) saveConfig { |
4692 | [delegate_ performSelectorOnMainThread:@selector(_saveConfig) withObject:nil waitUntilDone:NO]; | |
4693 | } | |
4694 | ||
5c32f89e JF |
4695 | - (NSArray *) getAllSources { |
4696 | return [[Database sharedInstance] sources]; | |
4697 | } | |
4698 | ||
8cc8eb1c | 4699 | - (NSArray *) getInstalledPackages { |
26e6829b JF |
4700 | Database *database([Database sharedInstance]); |
4701 | @synchronized (database) { | |
4702 | NSArray *packages([database packages]); | |
f4db946e | 4703 | NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]); |
77801ff1 | 4704 | for (Package *package in packages) |
26e6829b | 4705 | if (![package uninstalled]) |
8cc8eb1c JF |
4706 | [installed addObject:package]; |
4707 | return installed; | |
26e6829b | 4708 | } } |
8cc8eb1c | 4709 | |
43f3d7f6 | 4710 | - (Package *) getPackageById:(NSString *)id { |
62cab237 JF |
4711 | if (Package *package = [[Database sharedInstance] packageWithName:id]) { |
4712 | [package parse]; | |
4713 | return package; | |
4714 | } else | |
4715 | return (Package *) [NSNull null]; | |
43f3d7f6 JF |
4716 | } |
4717 | ||
948db680 JF |
4718 | - (NSString *) getLocaleIdentifier { |
4719 | return Locale_ == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(Locale_); | |
4720 | } | |
4721 | ||
4722 | - (NSArray *) getPreferredLanguages { | |
4723 | return Languages_; | |
4724 | } | |
4725 | ||
43f3d7f6 JF |
4726 | - (NSArray *) statfs:(NSString *)path { |
4727 | struct statfs stat; | |
4728 | ||
4729 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
4730 | return nil; | |
4731 | ||
4732 | return [NSArray arrayWithObjects: | |
4733 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
4734 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
4735 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
4736 | nil]; | |
4737 | } | |
4738 | ||
e40620ab | 4739 | ssize_t DiskUsage(const char *path); |
43f3d7f6 | 4740 | |
e40620ab JF |
4741 | - (NSNumber *) du:(NSString *)path { |
4742 | ssize_t usage(DiskUsage([path UTF8String])); | |
4743 | if (usage != -1) | |
4744 | usage /= 1024; | |
4745 | return [NSNumber numberWithUnsignedLong:usage]; | |
43f3d7f6 JF |
4746 | } |
4747 | ||
4748 | - (void) close { | |
e6417cea | 4749 | [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO]; |
43f3d7f6 JF |
4750 | } |
4751 | ||
518a552a JF |
4752 | - (NSNumber *) isReachable:(NSString *)name { |
4753 | return [NSNumber numberWithBool:IsReachable([name UTF8String])]; | |
4754 | } | |
4755 | ||
77801ff1 JF |
4756 | - (void) installPackages:(NSArray *)packages { |
4757 | [delegate_ performSelectorOnMainThread:@selector(installPackages:) withObject:packages waitUntilDone:NO]; | |
4758 | } | |
4759 | ||
8a126074 | 4760 | - (NSString *) substitutePackageNames:(NSString *)message { |
0c4fe0f4 | 4761 | NSMutableArray *words([[[message componentsSeparatedByString:@" "] mutableCopy] autorelease]); |
8a126074 JF |
4762 | for (size_t i(0), e([words count]); i != e; ++i) { |
4763 | NSString *word([words objectAtIndex:i]); | |
4764 | if (Package *package = [[Database sharedInstance] packageWithName:word]) | |
4765 | [words replaceObjectAtIndex:i withObject:[package name]]; | |
4766 | } | |
4767 | ||
4768 | return [words componentsJoinedByString:@" "]; | |
4769 | } | |
4770 | ||
ed5566c7 JF |
4771 | - (void) removeButton { |
4772 | [indirect_ removeButton]; | |
4773 | } | |
4774 | ||
43f3d7f6 JF |
4775 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
4776 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
4777 | } | |
4778 | ||
4779 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
4780 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
4781 | } | |
4782 | ||
c31c825d JF |
4783 | - (void) setBadgeValue:(id)value { |
4784 | [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO]; | |
4785 | } | |
4786 | ||
8366df5e JF |
4787 | - (void) setAllowsNavigationAction:(NSString *)value { |
4788 | [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO]; | |
4789 | } | |
4790 | ||
b8a5d89d JF |
4791 | - (void) setHidesBackButton:(NSString *)value { |
4792 | [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO]; | |
4793 | } | |
4794 | ||
5cdfcd6f JF |
4795 | - (void) setHidesNavigationBar:(NSString *)value { |
4796 | [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO]; | |
4797 | } | |
4798 | ||
82406217 JF |
4799 | - (void) setNavigationBarStyle:(NSString *)value { |
4800 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO]; | |
4801 | } | |
4802 | ||
00984204 JF |
4803 | - (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha { |
4804 | float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]); | |
4805 | UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]); | |
4806 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO]; | |
4807 | } | |
4808 | ||
36a20e14 JF |
4809 | - (void) setPasteboardString:(NSString *)value { |
4810 | [[objc_getClass("UIPasteboard") generalPasteboard] setString:value]; | |
4811 | } | |
4812 | ||
4813 | - (void) setPasteboardURL:(NSString *)value { | |
4814 | [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]]; | |
4815 | } | |
4816 | ||
673a6e1a | 4817 | - (void) _setToken:(NSString *)token { |
9737d93e JF |
4818 | Token_ = token; |
4819 | ||
4820 | if (token == nil) | |
4821 | [Metadata_ removeObjectForKey:@"Token"]; | |
4822 | else | |
4823 | [Metadata_ setObject:Token_ forKey:@"Token"]; | |
ef055c6c | 4824 | |
ef055c6c JF |
4825 | Changed_ = true; |
4826 | } | |
4827 | ||
673a6e1a JF |
4828 | - (void) setToken:(NSString *)token { |
4829 | [self performSelectorOnMainThread:@selector(_setToken:) withObject:token waitUntilDone:NO]; | |
4830 | } | |
4831 | ||
8e3b68d4 JF |
4832 | - (void) scrollToBottom:(NSNumber *)animated { |
4833 | [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO]; | |
4834 | } | |
4835 | ||
43f3d7f6 | 4836 | - (void) setViewportWidth:(float)width { |
8dbdaafa | 4837 | [indirect_ setViewportWidthOnMainThread:width]; |
43f3d7f6 JF |
4838 | } |
4839 | ||
4840 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
4841 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
4842 | unsigned count([arguments count]); | |
4843 | id values[count]; | |
4844 | for (unsigned i(0); i != count; ++i) | |
4845 | values[i] = [arguments objectAtIndex:i]; | |
673e8fa3 | 4846 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; |
43f3d7f6 JF |
4847 | } |
4848 | ||
4849 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
a95e0405 JF |
4850 | if (reinterpret_cast<id>(value) == [WebUndefined undefined]) |
4851 | value = nil; | |
43f3d7f6 JF |
4852 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) |
4853 | table = nil; | |
4854 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
c390d3ab JF |
4855 | } |
4856 | ||
4857 | @end | |
4858 | /* }}} */ | |
f79a4512 | 4859 | |
2e1652a9 JF |
4860 | @interface NSURL (CydiaSecure) |
4861 | @end | |
4862 | ||
4863 | @implementation NSURL (CydiaSecure) | |
4864 | ||
4865 | - (bool) isCydiaSecure { | |
4866 | if ([[[self scheme] lowercaseString] isEqualToString:@"https"]) | |
4867 | return true; | |
4868 | ||
4869 | @synchronized (HostConfig_) { | |
4870 | if ([InsecureHosts_ containsObject:[self host]]) | |
4871 | return true; | |
4872 | } | |
4873 | ||
4874 | return false; | |
4875 | } | |
4876 | ||
4877 | @end | |
dd5f8161 | 4878 | |
80319240 | 4879 | /* Cydia Browser Controller {{{ */ |
a576488f | 4880 | @implementation CydiaWebViewController |
43f3d7f6 | 4881 | |
fe8e721f | 4882 | - (NSURL *) navigationURL { |
d323285e | 4883 | return request_ == nil ? nil : [NSURL URLWithString:[NSString stringWithFormat:@"cydia://url/%@", [[request_ URL] absoluteString]]]; |
fe8e721f GP |
4884 | } |
4885 | ||
3f9ab807 JF |
4886 | + (void) _initialize { |
4887 | [super _initialize]; | |
4888 | ||
7b33d201 | 4889 | Diversions_ = [NSMutableSet setWithCapacity:0]; |
775deead JF |
4890 | } |
4891 | ||
4892 | + (void) addDiversion:(Diversion *)diversion { | |
4893 | [Diversions_ addObject:diversion]; | |
4894 | } | |
4895 | ||
2634b249 JF |
4896 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4897 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
9e130bc2 JF |
4898 | [CydiaWebViewController didClearWindowObject:window forFrame:frame withCydia:cydia_]; |
4899 | } | |
01d93940 | 4900 | |
9e130bc2 | 4901 | + (void) didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame withCydia:(CydiaObject *)cydia { |
01d93940 JF |
4902 | WebDataSource *source([frame dataSource]); |
4903 | NSURLResponse *response([source response]); | |
4904 | NSURL *url([response URL]); | |
b8f1a18a JF |
4905 | NSString *scheme([[url scheme] lowercaseString]); |
4906 | ||
4907 | bool bridged(false); | |
8804004f | 4908 | |
48f1762f | 4909 | @synchronized (HostConfig_) { |
b8f1a18a JF |
4910 | if ([scheme isEqualToString:@"file"]) |
4911 | bridged = true; | |
4912 | else if ([scheme isEqualToString:@"https"]) | |
48f1762f | 4913 | if ([BridgedHosts_ containsObject:[url host]]) |
b8f1a18a | 4914 | bridged = true; |
48f1762f | 4915 | } |
b8f1a18a JF |
4916 | |
4917 | if (bridged) | |
9e130bc2 | 4918 | [window setValue:cydia forKey:@"cydia"]; |
43f3d7f6 JF |
4919 | } |
4920 | ||
22485d93 JF |
4921 | - (void) _setupMail:(MFMailComposeViewController *)controller { |
4922 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/cydia.log"] mimeType:@"text/plain" fileName:@"cydia.log"]; | |
4923 | ||
4924 | system("/usr/bin/dpkg -l >/tmp/dpkgl.log"); | |
4925 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/dpkgl.log"] mimeType:@"text/plain" fileName:@"dpkgl.log"]; | |
4926 | } | |
4927 | ||
f9b36dae JF |
4928 | - (NSURL *) URLWithURL:(NSURL *)url { |
4929 | return [Diversion divertURL:url]; | |
4930 | } | |
4931 | ||
9d1bf666 | 4932 | - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
85ae5f42 JF |
4933 | return [CydiaWebViewController requestWithHeaders:[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; |
4934 | } | |
4935 | ||
92de61aa JF |
4936 | - (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
4937 | return [CydiaWebViewController requestWithHeaders:[super webThreadWebView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; | |
4938 | } | |
4939 | ||
85ae5f42 | 4940 | + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request { |
0c4fe0f4 | 4941 | NSMutableURLRequest *copy([[request mutableCopy] autorelease]); |
9d1bf666 | 4942 | |
bc1cffbe | 4943 | NSURL *url([copy URL]); |
daa21f8e | 4944 | NSString *href([url absoluteString]); |
bc1cffbe JF |
4945 | NSString *host([url host]); |
4946 | ||
daa21f8e JF |
4947 | if ([href hasPrefix:@"https://cydia.saurik.com/TSS/"]) { |
4948 | if (NSString *agent = [copy valueForHTTPHeaderField:@"X-User-Agent"]) { | |
4949 | [copy setValue:agent forHTTPHeaderField:@"User-Agent"]; | |
4950 | [copy setValue:nil forHTTPHeaderField:@"X-User-Agent"]; | |
4951 | } | |
4952 | ||
4953 | [copy setValue:nil forHTTPHeaderField:@"Referer"]; | |
4954 | [copy setValue:nil forHTTPHeaderField:@"Origin"]; | |
4955 | ||
4956 | [copy setURL:[NSURL URLWithString:[@"http://gs.apple.com/TSS/" stringByAppendingString:[href substringFromIndex:29]]]]; | |
4957 | return copy; | |
4958 | } | |
4959 | ||
6f44d712 JF |
4960 | if ([copy valueForHTTPHeaderField:@"X-Cydia-Cf"] == nil) |
4961 | [copy setValue:[NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber] forHTTPHeaderField:@"X-Cydia-Cf"]; | |
1baae086 | 4962 | if (Machine_ != NULL && [copy valueForHTTPHeaderField:@"X-Machine"] == nil) |
9d1bf666 | 4963 | [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; |
247bedb6 | 4964 | |
e7817a6b | 4965 | bool bridged; |
247bedb6 | 4966 | bool token; |
e7817a6b | 4967 | |
247bedb6 | 4968 | @synchronized (HostConfig_) { |
e7817a6b JF |
4969 | bridged = [BridgedHosts_ containsObject:host]; |
4970 | token = [TokenHosts_ containsObject:host]; | |
247bedb6 JF |
4971 | } |
4972 | ||
e7817a6b JF |
4973 | if ([url isCydiaSecure]) { |
4974 | if (bridged) { | |
4975 | if (UniqueID_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Id"] == nil) | |
4976 | [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; | |
4977 | } else if (token) { | |
4978 | if (Token_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Token"] == nil) | |
4979 | [copy setValue:Token_ forHTTPHeaderField:@"X-Cydia-Token"]; | |
4980 | } | |
247bedb6 | 4981 | } |
43f3d7f6 JF |
4982 | |
4983 | return copy; | |
a9a0661e JF |
4984 | } |
4985 | ||
77801ff1 JF |
4986 | - (void) setDelegate:(id)delegate { |
4987 | [super setDelegate:delegate]; | |
4988 | [cydia_ setDelegate:delegate]; | |
4989 | } | |
4990 | ||
c6cf66c7 | 4991 | - (NSString *) applicationNameForUserAgent { |
e967efd5 | 4992 | return UserAgent_; |
c6cf66c7 | 4993 | } |
43f3d7f6 | 4994 | |
c6cf66c7 JF |
4995 | - (id) init { |
4996 | if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) { | |
4997 | cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease]; | |
43f3d7f6 JF |
4998 | } return self; |
4999 | } | |
5000 | ||
29bb09d7 JF |
5001 | @end |
5002 | ||
5003 | @interface AppCacheController : CydiaWebViewController { | |
5004 | } | |
5005 | ||
5006 | @end | |
5007 | ||
5008 | @implementation AppCacheController | |
5009 | ||
5010 | - (void) didReceiveMemoryWarning { | |
6271cb57 | 5011 | // XXX: this doesn't work |
29bb09d7 JF |
5012 | } |
5013 | ||
2713be8e JF |
5014 | - (bool) retainsNetworkActivityIndicator { |
5015 | return false; | |
5016 | } | |
5017 | ||
43f3d7f6 | 5018 | @end |
80319240 | 5019 | /* }}} */ |
43f3d7f6 | 5020 | |
b1ca831d JF |
5021 | // CydiaScript {{{ |
5022 | @interface NSObject (CydiaScript) | |
5023 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; | |
5024 | @end | |
5025 | ||
5026 | @implementation NSObject (CydiaScript) | |
5027 | ||
5028 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5029 | return self; | |
5030 | } | |
5031 | ||
5032 | @end | |
5033 | ||
5034 | @implementation NSArray (CydiaScript) | |
5035 | ||
5036 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5037 | WebScriptObject *object([context evaluateWebScript:@"[]"]); | |
5038 | for (size_t i(0), e([self count]); i != e; ++i) | |
5039 | [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; | |
5040 | return object; | |
5041 | } | |
5042 | ||
5043 | @end | |
5044 | ||
5045 | @implementation NSDictionary (CydiaScript) | |
5046 | ||
5047 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5048 | WebScriptObject *object([context evaluateWebScript:@"({})"]); | |
5049 | for (id i in self) | |
5050 | [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; | |
5051 | return object; | |
5052 | } | |
5053 | ||
5054 | @end | |
5055 | // }}} | |
5056 | ||
5829aea2 GP |
5057 | /* Confirmation Controller {{{ */ |
5058 | bool DepSubstrate(const pkgCache::VerIterator &iterator) { | |
5059 | if (!iterator.end()) | |
5060 | for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) { | |
5061 | if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends) | |
5062 | continue; | |
5063 | pkgCache::PkgIterator package(dep.TargetPkg()); | |
5064 | if (package.end()) | |
5065 | continue; | |
5066 | if (strcmp(package.Name(), "mobilesubstrate") == 0) | |
5067 | return true; | |
5068 | } | |
5069 | ||
5070 | return false; | |
5071 | } | |
5072 | ||
adb61bda | 5073 | @protocol ConfirmationControllerDelegate |
674dce72 | 5074 | - (void) cancelAndClear:(bool)clear; |
b5e7eebb | 5075 | - (void) confirmWithNavigationController:(UINavigationController *)navigation; |
dc63e78f | 5076 | - (void) queue; |
36bb2ca2 | 5077 | @end |
2367a917 | 5078 | |
a576488f | 5079 | @interface ConfirmationController : CydiaWebViewController { |
770f2a8e | 5080 | _transient Database *database_; |
6ceb0959 | 5081 | |
7b33d201 | 5082 | _H<UIAlertView> essential_; |
6ceb0959 | 5083 | |
7b33d201 JF |
5084 | _H<NSDictionary> changes_; |
5085 | _H<NSMutableArray> issues_; | |
5086 | _H<NSDictionary> sizes_; | |
6ceb0959 | 5087 | |
a9a0661e | 5088 | BOOL substrate_; |
36bb2ca2 | 5089 | } |
dc5812ec | 5090 | |
b5e7eebb | 5091 | - (id) initWithDatabase:(Database *)database; |
b4d89997 | 5092 | |
dc5812ec JF |
5093 | @end |
5094 | ||
adb61bda | 5095 | @implementation ConfirmationController |
dc5812ec | 5096 | |
ab2cfc1e JF |
5097 | - (void) complete { |
5098 | if (substrate_) | |
be860cc8 | 5099 | RestartSubstrate_ = true; |
ab2cfc1e JF |
5100 | [delegate_ confirmWithNavigationController:[self navigationController]]; |
5101 | } | |
5102 | ||
b5e7eebb | 5103 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
3272e699 | 5104 | NSString *context([alert context]); |
9bedffaa | 5105 | |
1cedb821 | 5106 | if ([context isEqualToString:@"remove"]) { |
ab2cfc1e | 5107 | if (button == [alert cancelButtonIndex]) |
02069daf | 5108 | [self _doContinue]; |
ab2cfc1e | 5109 | else if (button == [alert firstOtherButtonIndex]) { |
d69dbfc5 | 5110 | [self performSelector:@selector(complete) withObject:nil afterDelay:0]; |
9bedffaa | 5111 | } |
9bedffaa | 5112 | |
3272e699 | 5113 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 5114 | } else if ([context isEqualToString:@"unable"]) { |
b5e7eebb | 5115 | [self dismissModalViewControllerAnimated:YES]; |
3272e699 GP |
5116 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
5117 | } else { | |
b5e7eebb GP |
5118 | [super alertView:alert clickedButtonAtIndex:button]; |
5119 | } | |
36bb2ca2 JF |
5120 | } |
5121 | ||
46c46f4f | 5122 | - (void) _doContinue { |
21ea11a4 | 5123 | [delegate_ cancelAndClear:NO]; |
12d3d98d | 5124 | [self dismissModalViewControllerAnimated:YES]; |
46c46f4f | 5125 | } |
bc11cf5b | 5126 | |
46c46f4f JF |
5127 | - (id) invokeDefaultMethodWithArguments:(NSArray *)args { |
5128 | [self performSelectorOnMainThread:@selector(_doContinue) withObject:nil waitUntilDone:NO]; | |
21ea11a4 GP |
5129 | return nil; |
5130 | } | |
5131 | ||
2634b249 JF |
5132 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5133 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
6ceb0959 | 5134 | |
781001d7 | 5135 | [window setValue:[[NSDictionary dictionaryWithObjectsAndKeys: |
7b33d201 JF |
5136 | (id) changes_, @"changes", |
5137 | (id) issues_, @"issues", | |
5138 | (id) sizes_, @"sizes", | |
781001d7 JF |
5139 | self, @"queue", |
5140 | nil] Cydia$webScriptObjectInContext:window] forKey:@"cydiaConfirm"]; | |
36bb2ca2 JF |
5141 | } |
5142 | ||
b5e7eebb GP |
5143 | - (id) initWithDatabase:(Database *)database { |
5144 | if ((self = [super init]) != nil) { | |
770f2a8e JF |
5145 | database_ = database; |
5146 | ||
6ceb0959 JF |
5147 | NSMutableArray *installs([NSMutableArray arrayWithCapacity:16]); |
5148 | NSMutableArray *reinstalls([NSMutableArray arrayWithCapacity:16]); | |
5149 | NSMutableArray *upgrades([NSMutableArray arrayWithCapacity:16]); | |
5150 | NSMutableArray *downgrades([NSMutableArray arrayWithCapacity:16]); | |
5151 | NSMutableArray *removes([NSMutableArray arrayWithCapacity:16]); | |
dc5812ec | 5152 | |
36bb2ca2 | 5153 | bool remove(false); |
dc5812ec | 5154 | |
6ceb0959 JF |
5155 | pkgCacheFile &cache([database_ cache]); |
5156 | NSArray *packages([database_ packages]); | |
a9a0661e JF |
5157 | pkgDepCache::Policy *policy([database_ policy]); |
5158 | ||
7b33d201 | 5159 | issues_ = [NSMutableArray arrayWithCapacity:4]; |
6ceb0959 | 5160 | |
856b8388 JF |
5161 | UpgradeCydia_ = false; |
5162 | ||
c4dcf2c2 | 5163 | for (Package *package in packages) { |
6ceb0959 JF |
5164 | pkgCache::PkgIterator iterator([package iterator]); |
5165 | NSString *name([package id]); | |
5166 | ||
5167 | if ([package broken]) { | |
5168 | NSMutableArray *reasons([NSMutableArray arrayWithCapacity:4]); | |
5169 | ||
5170 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5171 | name, @"package", | |
5172 | reasons, @"reasons", | |
5173 | nil]]; | |
5174 | ||
5175 | pkgCache::VerIterator ver(cache[iterator].InstVerIter(cache)); | |
5176 | if (ver.end()) | |
5177 | continue; | |
5178 | ||
5179 | for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) { | |
5180 | pkgCache::DepIterator start; | |
5181 | pkgCache::DepIterator end; | |
5182 | dep.GlobOr(start, end); // ++dep | |
5183 | ||
5184 | if (!cache->IsImportantDep(end)) | |
5185 | continue; | |
5186 | if ((cache[end] & pkgDepCache::DepGInstall) != 0) | |
5187 | continue; | |
5188 | ||
810c9763 JF |
5189 | NSMutableArray *clauses([NSMutableArray arrayWithCapacity:4]); |
5190 | ||
5191 | [reasons addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5192 | [NSString stringWithUTF8String:start.DepType()], @"relationship", | |
5193 | clauses, @"clauses", | |
5194 | nil]]; | |
5195 | ||
6ceb0959 JF |
5196 | _forever { |
5197 | NSString *reason, *installed((NSString *) [WebUndefined undefined]); | |
5198 | ||
5199 | pkgCache::PkgIterator target(start.TargetPkg()); | |
5200 | if (target->ProvidesList != 0) | |
5201 | reason = @"missing"; | |
5202 | else { | |
5203 | pkgCache::VerIterator ver(cache[target].InstVerIter(cache)); | |
5204 | if (!ver.end()) { | |
5205 | reason = @"installed"; | |
5206 | installed = [NSString stringWithUTF8String:ver.VerStr()]; | |
5207 | } else if (!cache[target].CandidateVerIter(cache).end()) | |
5208 | reason = @"uninstalled"; | |
5209 | else if (target->ProvidesList == 0) | |
5210 | reason = @"uninstallable"; | |
5211 | else | |
5212 | reason = @"virtual"; | |
5213 | } | |
5214 | ||
3e5a9f5d | 5215 | NSDictionary *version(start.TargetVer() == 0 ? (NSDictionary *) [NSNull null] : [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5216 | [NSString stringWithUTF8String:start.CompType()], @"operator", |
5217 | [NSString stringWithUTF8String:start.TargetVer()], @"value", | |
5218 | nil]); | |
5219 | ||
810c9763 | 5220 | [clauses addObject:[NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5221 | [NSString stringWithUTF8String:start.TargetPkg().Name()], @"package", |
5222 | version, @"version", | |
5223 | reason, @"reason", | |
5224 | installed, @"installed", | |
5225 | nil]]; | |
5226 | ||
5227 | // yes, seriously. (wtf?) | |
5228 | if (start == end) | |
5229 | break; | |
5230 | ++start; | |
5231 | } | |
5232 | } | |
5233 | } | |
5234 | ||
36bb2ca2 | 5235 | pkgDepCache::StateCache &state(cache[iterator]); |
dc5812ec | 5236 | |
b4fd1197 | 5237 | static RegEx special_r("(firmware|gsc\\..*|cy\\+.*)"); |
2388b078 | 5238 | |
36bb2ca2 | 5239 | if (state.NewInstall()) |
6ceb0959 | 5240 | [installs addObject:name]; |
f8d15be2 | 5241 | // XXX: else if (state.Install()) |
36bb2ca2 | 5242 | else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall) |
6ceb0959 | 5243 | [reinstalls addObject:name]; |
f8d15be2 | 5244 | // XXX: move before previous if |
36bb2ca2 | 5245 | else if (state.Upgrade()) |
6ceb0959 | 5246 | [upgrades addObject:name]; |
36bb2ca2 | 5247 | else if (state.Downgrade()) |
6ceb0959 JF |
5248 | [downgrades addObject:name]; |
5249 | else if (!state.Delete()) | |
f8d15be2 | 5250 | // XXX: _assert(state.Keep()); |
6ceb0959 | 5251 | continue; |
1916f316 JF |
5252 | else if (special_r(name)) |
5253 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5254 | [NSNull null], @"package", | |
5255 | [NSArray arrayWithObjects: | |
5256 | [NSDictionary dictionaryWithObjectsAndKeys: | |
810c9763 JF |
5257 | @"Conflicts", @"relationship", |
5258 | [NSArray arrayWithObjects: | |
5259 | [NSDictionary dictionaryWithObjectsAndKeys: | |
5260 | name, @"package", | |
5261 | [NSNull null], @"version", | |
5262 | @"installed", @"reason", | |
5263 | nil], | |
5264 | nil], @"clauses", | |
1916f316 JF |
5265 | nil], |
5266 | nil], @"reasons", | |
5267 | nil]]; | |
5268 | else { | |
2388b078 | 5269 | if ([package essential]) |
36bb2ca2 | 5270 | remove = true; |
6ceb0959 JF |
5271 | [removes addObject:name]; |
5272 | } | |
a9a0661e | 5273 | |
856b8388 JF |
5274 | if ([name isEqualToString:@"cydia"]) |
5275 | UpgradeCydia_ = true; | |
5276 | ||
a9a0661e JF |
5277 | substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator)); |
5278 | substrate_ |= DepSubstrate(iterator.CurrentVer()); | |
36bb2ca2 | 5279 | } |
ec97ef06 | 5280 | |
36bb2ca2 JF |
5281 | if (!remove) |
5282 | essential_ = nil; | |
d791dce4 | 5283 | else if (Advanced_) { |
43f3d7f6 | 5284 | NSString *parenthetical(UCLocalize("PARENTHETICAL")); |
f79a4512 | 5285 | |
7b33d201 | 5286 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 5287 | initWithTitle:UCLocalize("REMOVING_ESSENTIALS") |
b5e7eebb GP |
5288 | message:UCLocalize("REMOVING_ESSENTIALS_EX") |
5289 | delegate:self | |
5290 | cancelButtonTitle:[NSString stringWithFormat:parenthetical, UCLocalize("CANCEL_OPERATION"), UCLocalize("SAFE")] | |
1aa29546 JF |
5291 | otherButtonTitles: |
5292 | [NSString stringWithFormat:parenthetical, UCLocalize("FORCE_REMOVAL"), UCLocalize("UNSAFE")], | |
5293 | nil | |
7b33d201 | 5294 | ] autorelease]; |
04fe1349 | 5295 | |
3272e699 | 5296 | [essential_ setContext:@"remove"]; |
9aaebfec | 5297 | [essential_ setNumberOfRows:2]; |
9bedffaa | 5298 | } else { |
7b33d201 | 5299 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 5300 | initWithTitle:UCLocalize("UNABLE_TO_COMPLY") |
b5e7eebb GP |
5301 | message:UCLocalize("UNABLE_TO_COMPLY_EX") |
5302 | delegate:self | |
5303 | cancelButtonTitle:UCLocalize("OKAY") | |
5304 | otherButtonTitles:nil | |
7b33d201 | 5305 | ] autorelease]; |
ec97ef06 | 5306 | |
b5e7eebb | 5307 | [essential_ setContext:@"unable"]; |
36bb2ca2 | 5308 | } |
ec97ef06 | 5309 | |
7b33d201 | 5310 | changes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5311 | installs, @"installs", |
5312 | reinstalls, @"reinstalls", | |
5313 | upgrades, @"upgrades", | |
5314 | downgrades, @"downgrades", | |
5315 | removes, @"removes", | |
affeffc7 | 5316 | nil]; |
dc5812ec | 5317 | |
7b33d201 | 5318 | sizes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5319 | [NSNumber numberWithInteger:[database_ fetcher].FetchNeeded()], @"downloading", |
5320 | [NSNumber numberWithInteger:[database_ fetcher].PartialPresent()], @"resuming", | |
affeffc7 | 5321 | nil]; |
dc5812ec | 5322 | |
90351d93 | 5323 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/confirm/", UI_]]]; |
36bb2ca2 | 5324 | } return self; |
b4d89997 | 5325 | } |
8da60fb7 | 5326 | |
e6124cb6 JF |
5327 | - (UIBarButtonItem *) leftButton { |
5328 | return [[[UIBarButtonItem alloc] | |
5329 | initWithTitle:UCLocalize("CANCEL") | |
5330 | style:UIBarButtonItemStylePlain | |
5331 | target:self | |
5332 | action:@selector(cancelButtonClicked) | |
5333 | ] autorelease]; | |
5334 | } | |
5335 | ||
614cd4f1 | 5336 | #if !AlwaysReload |
bcde1e70 | 5337 | - (void) applyRightButton { |
6ceb0959 | 5338 | if ([issues_ count] == 0 && ![self isLoading]) |
dd9de556 JF |
5339 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
5340 | initWithTitle:UCLocalize("CONFIRM") | |
2761d574 | 5341 | style:UIBarButtonItemStyleDone |
dd9de556 JF |
5342 | target:self |
5343 | action:@selector(confirmButtonClicked) | |
5344 | ] autorelease]]; | |
5345 | else | |
bcde1e70 | 5346 | [[self navigationItem] setRightBarButtonItem:nil]; |
affeffc7 | 5347 | } |
bcde1e70 | 5348 | #endif |
affeffc7 | 5349 | |
b5e7eebb | 5350 | - (void) cancelButtonClicked { |
21ea11a4 | 5351 | [delegate_ cancelAndClear:YES]; |
05452929 | 5352 | [self dismissModalViewControllerAnimated:YES]; |
affeffc7 JF |
5353 | } |
5354 | ||
9487f027 | 5355 | #if !AlwaysReload |
b5e7eebb | 5356 | - (void) confirmButtonClicked { |
affeffc7 | 5357 | if (essential_ != nil) |
b5e7eebb | 5358 | [essential_ show]; |
ab2cfc1e JF |
5359 | else |
5360 | [self complete]; | |
affeffc7 | 5361 | } |
9487f027 | 5362 | #endif |
affeffc7 | 5363 | |
36bb2ca2 JF |
5364 | @end |
5365 | /* }}} */ | |
8da60fb7 | 5366 | |
aaae308d JF |
5367 | /* Progress Data {{{ */ |
5368 | @interface CydiaProgressData : NSObject { | |
5369 | _transient id delegate_; | |
5370 | ||
5371 | bool running_; | |
b0b11d99 | 5372 | float percent_; |
aaae308d | 5373 | |
bcbac8f7 JF |
5374 | float current_; |
5375 | float total_; | |
5376 | float speed_; | |
5377 | ||
aaae308d JF |
5378 | _H<NSMutableArray> events_; |
5379 | _H<NSString> title_; | |
5380 | ||
5381 | _H<NSString> status_; | |
5382 | _H<NSString> finish_; | |
5383 | } | |
5384 | ||
5385 | @end | |
5386 | ||
5387 | @implementation CydiaProgressData | |
5388 | ||
5389 | + (NSArray *) _attributeKeys { | |
5390 | return [NSArray arrayWithObjects: | |
bcbac8f7 | 5391 | @"current", |
aaae308d JF |
5392 | @"events", |
5393 | @"finish", | |
b0b11d99 | 5394 | @"percent", |
aaae308d | 5395 | @"running", |
bcbac8f7 | 5396 | @"speed", |
aaae308d | 5397 | @"title", |
bcbac8f7 | 5398 | @"total", |
aaae308d JF |
5399 | nil]; |
5400 | } | |
5401 | ||
5402 | - (NSArray *) attributeKeys { | |
5403 | return [[self class] _attributeKeys]; | |
5404 | } | |
5405 | ||
5406 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
5407 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
5408 | } | |
5409 | ||
5410 | - (id) init { | |
5411 | if ((self = [super init]) != nil) { | |
5412 | events_ = [NSMutableArray arrayWithCapacity:32]; | |
5413 | } return self; | |
5414 | } | |
5415 | ||
353dda5b JF |
5416 | - (id) delegate { |
5417 | return delegate_; | |
5418 | } | |
5419 | ||
aaae308d JF |
5420 | - (void) setDelegate:(id)delegate { |
5421 | delegate_ = delegate; | |
5422 | } | |
5423 | ||
b0b11d99 JF |
5424 | - (void) setPercent:(float)value { |
5425 | percent_ = value; | |
aaae308d JF |
5426 | } |
5427 | ||
b0b11d99 JF |
5428 | - (NSNumber *) percent { |
5429 | return [NSNumber numberWithFloat:percent_]; | |
aaae308d JF |
5430 | } |
5431 | ||
bcbac8f7 JF |
5432 | - (void) setCurrent:(float)value { |
5433 | current_ = value; | |
5434 | } | |
5435 | ||
5436 | - (NSNumber *) current { | |
5437 | return [NSNumber numberWithFloat:current_]; | |
5438 | } | |
5439 | ||
5440 | - (void) setTotal:(float)value { | |
5441 | total_ = value; | |
5442 | } | |
5443 | ||
5444 | - (NSNumber *) total { | |
5445 | return [NSNumber numberWithFloat:total_]; | |
5446 | } | |
5447 | ||
5448 | - (void) setSpeed:(float)value { | |
5449 | speed_ = value; | |
5450 | } | |
5451 | ||
5452 | - (NSNumber *) speed { | |
5453 | return [NSNumber numberWithFloat:speed_]; | |
5454 | } | |
5455 | ||
aaae308d JF |
5456 | - (NSArray *) events { |
5457 | return events_; | |
5458 | } | |
5459 | ||
5460 | - (void) removeAllEvents { | |
5461 | [events_ removeAllObjects]; | |
5462 | } | |
5463 | ||
5464 | - (void) addEvent:(CydiaProgressEvent *)event { | |
5465 | [events_ addObject:event]; | |
5466 | } | |
5467 | ||
5468 | - (void) setTitle:(NSString *)text { | |
5469 | title_ = text; | |
5470 | } | |
5471 | ||
5472 | - (NSString *) title { | |
5473 | return title_; | |
5474 | } | |
5475 | ||
5476 | - (void) setFinish:(NSString *)text { | |
5477 | finish_ = text; | |
5478 | } | |
5479 | ||
5480 | - (NSString *) finish { | |
5481 | return (id) finish_ ?: [NSNull null]; | |
5482 | } | |
5483 | ||
5484 | - (void) setRunning:(bool)running { | |
5485 | running_ = running; | |
5486 | } | |
5487 | ||
5488 | - (NSNumber *) running { | |
5489 | return running_ ? (NSNumber *) kCFBooleanTrue : (NSNumber *) kCFBooleanFalse; | |
5490 | } | |
5491 | ||
5492 | @end | |
5493 | /* }}} */ | |
adb61bda | 5494 | /* Progress Controller {{{ */ |
a576488f | 5495 | @interface ProgressController : CydiaWebViewController < |
36bb2ca2 JF |
5496 | ProgressDelegate |
5497 | > { | |
8b29f8e6 | 5498 | _transient Database *database_; |
bf7c998c | 5499 | _H<CydiaProgressData, 1> progress_; |
aaae308d | 5500 | unsigned cancel_; |
2367a917 JF |
5501 | } |
5502 | ||
b5e7eebb | 5503 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
2367a917 | 5504 | |
6915b806 | 5505 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title; |
2367a917 | 5506 | |
6915b806 | 5507 | - (void) setTitle:(NSString *)title; |
aaae308d | 5508 | - (void) setCancellable:(bool)cancellable; |
bd150f54 | 5509 | |
36bb2ca2 JF |
5510 | @end |
5511 | ||
adb61bda | 5512 | @implementation ProgressController |
2367a917 JF |
5513 | |
5514 | - (void) dealloc { | |
6915b806 | 5515 | [database_ setProgressDelegate:nil]; |
2367a917 JF |
5516 | [super dealloc]; |
5517 | } | |
5518 | ||
63ae52be JF |
5519 | - (UIBarButtonItem *) leftButton { |
5520 | return cancel_ == 1 ? [[[UIBarButtonItem alloc] | |
3c62d654 JF |
5521 | initWithTitle:UCLocalize("CANCEL") |
5522 | style:UIBarButtonItemStylePlain | |
5523 | target:self | |
5524 | action:@selector(cancel) | |
63ae52be JF |
5525 | ] autorelease] : nil; |
5526 | } | |
5527 | ||
5528 | - (void) updateCancel { | |
5529 | [super applyLeftButton]; | |
3c62d654 JF |
5530 | } |
5531 | ||
b5e7eebb GP |
5532 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { |
5533 | if ((self = [super init]) != nil) { | |
8b29f8e6 | 5534 | database_ = database; |
36bb2ca2 | 5535 | delegate_ = delegate; |
ec97ef06 | 5536 | |
6915b806 JF |
5537 | [database_ setProgressDelegate:self]; |
5538 | ||
aaae308d JF |
5539 | progress_ = [[[CydiaProgressData alloc] init] autorelease]; |
5540 | [progress_ setDelegate:self]; | |
3c62d654 | 5541 | |
90351d93 | 5542 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/progress/", UI_]]]; |
3c62d654 JF |
5543 | |
5544 | [scroller_ setBackgroundColor:[UIColor blackColor]]; | |
5545 | ||
5546 | [[self navigationItem] setHidesBackButton:YES]; | |
5547 | ||
5548 | [self updateCancel]; | |
36bb2ca2 | 5549 | } return self; |
2367a917 JF |
5550 | } |
5551 | ||
aaae308d JF |
5552 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5553 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
5554 | [window setValue:progress_ forKey:@"cydiaProgress"]; | |
5555 | } | |
bc11cf5b | 5556 | |
aaae308d JF |
5557 | - (void) updateProgress { |
5558 | [self dispatchEvent:@"CydiaProgressUpdate"]; | |
5559 | } | |
b5e7eebb | 5560 | |
b5e7eebb | 5561 | - (void) viewWillAppear:(BOOL)animated { |
14e4ff09 | 5562 | [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlack]; |
3c62d654 | 5563 | [super viewWillAppear:animated]; |
2367a917 JF |
5564 | } |
5565 | ||
a3755a1e | 5566 | - (void) reloadSpringBoard { |
2b6abb56 JF |
5567 | if (kCFCoreFoundationVersionNumber > 700) { // XXX: iOS 6.x |
5568 | system("/bin/launchctl stop com.apple.backboardd"); | |
5569 | sleep(15); | |
5570 | system("/usr/bin/killall backboardd SpringBoard sbreload"); | |
5571 | return; | |
5572 | } | |
5573 | ||
1b120913 JF |
5574 | pid_t pid(ExecFork()); |
5575 | if (pid == 0) { | |
024cdbbf JF |
5576 | if (setsid() == -1) |
5577 | perror("setsid"); | |
5578 | ||
1b120913 JF |
5579 | pid_t pid(ExecFork()); |
5580 | if (pid == 0) { | |
0c0a966b | 5581 | execl("/usr/libexec/cydia/cydo", "cydo", "/usr/bin/sbreload", NULL); |
1b120913 | 5582 | perror("sbreload"); |
51c8106a | 5583 | |
1b120913 | 5584 | exit(0); |
51c8106a | 5585 | } ReapZombie(pid); |
1b120913 JF |
5586 | |
5587 | exit(0); | |
51c8106a | 5588 | } ReapZombie(pid); |
1b120913 JF |
5589 | |
5590 | sleep(15); | |
2b6abb56 | 5591 | system("/usr/bin/killall backboardd SpringBoard sbreload"); |
a3755a1e JF |
5592 | } |
5593 | ||
aaae308d | 5594 | - (void) close { |
ef494bd8 RP |
5595 | UpdateExternalStatus(0); |
5596 | ||
9dd3045d JF |
5597 | if (Finish_ > 1) |
5598 | [delegate_ saveState]; | |
5599 | ||
670a0494 JF |
5600 | switch (Finish_) { |
5601 | case 0: | |
2925cbba | 5602 | [delegate_ returnToCydia]; |
670a0494 JF |
5603 | break; |
5604 | ||
5605 | case 1: | |
c4899376 JF |
5606 | [delegate_ terminateWithSuccess]; |
5607 | /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) | |
5608 | [delegate_ suspendWithAnimation:YES]; | |
5609 | else | |
5610 | [delegate_ suspend];*/ | |
670a0494 JF |
5611 | break; |
5612 | ||
5613 | case 2: | |
985d2dff | 5614 | _trace(); |
ef812071 | 5615 | goto reload; |
670a0494 JF |
5616 | |
5617 | case 3: | |
985d2dff | 5618 | _trace(); |
ef812071 JF |
5619 | goto reload; |
5620 | ||
a3755a1e JF |
5621 | reload: { |
5622 | UIProgressHUD *hud([delegate_ addProgressHUD]); | |
5623 | [hud setText:UCLocalize("LOADING")]; | |
4a4dcb68 | 5624 | [self performSelector:@selector(reloadSpringBoard) withObject:nil afterDelay:0.5]; |
317eb8e3 JF |
5625 | return; |
5626 | } | |
670a0494 JF |
5627 | |
5628 | case 4: | |
985d2dff | 5629 | _trace(); |
0e371502 JF |
5630 | if (void (*SBReboot)(mach_port_t) = reinterpret_cast<void (*)(mach_port_t)>(dlsym(RTLD_DEFAULT, "SBReboot"))) |
5631 | SBReboot(SBSSpringBoardServerPort()); | |
5632 | else | |
bb0fe3c9 | 5633 | reboot2(RB_AUTOBOOT); |
670a0494 | 5634 | break; |
bc8cd583 | 5635 | } |
aaae308d JF |
5636 | |
5637 | [super close]; | |
670a0494 | 5638 | } |
bd150f54 | 5639 | |
6915b806 | 5640 | - (void) setTitle:(NSString *)title { |
aaae308d JF |
5641 | [progress_ setTitle:title]; |
5642 | [self updateProgress]; | |
5643 | } | |
5644 | ||
5645 | - (UIBarButtonItem *) rightButton { | |
d53628b6 | 5646 | return [[progress_ running] boolValue] ? [super rightButton] : [[[UIBarButtonItem alloc] |
aaae308d JF |
5647 | initWithTitle:UCLocalize("CLOSE") |
5648 | style:UIBarButtonItemStylePlain | |
5649 | target:self | |
5650 | action:@selector(close) | |
5651 | ] autorelease]; | |
6915b806 JF |
5652 | } |
5653 | ||
5654 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title { | |
5655 | UpdateExternalStatus(1); | |
5656 | ||
aaae308d | 5657 | [progress_ setRunning:true]; |
6915b806 | 5658 | [self setTitle:title]; |
aaae308d | 5659 | // implicit updateProgress |
6915b806 | 5660 | |
140710ba | 5661 | SHA1SumValue notifyconf; { |
6915b806 JF |
5662 | FileFd file; |
5663 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
5664 | _error->Discard(); | |
5665 | else { | |
5666 | MMap mmap(file, MMap::ReadOnly); | |
5667 | SHA1Summation sha1; | |
5668 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5669 | notifyconf = sha1.Result(); |
6915b806 JF |
5670 | } |
5671 | } | |
5672 | ||
140710ba | 5673 | SHA1SumValue springlist; { |
6915b806 JF |
5674 | FileFd file; |
5675 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
5676 | _error->Discard(); | |
5677 | else { | |
5678 | MMap mmap(file, MMap::ReadOnly); | |
5679 | SHA1Summation sha1; | |
5680 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5681 | springlist = sha1.Result(); |
6915b806 JF |
5682 | } |
5683 | } | |
5684 | ||
5685 | if (invocation != nil) { | |
5686 | [invocation yieldToSelector:@selector(invoke)]; | |
aaae308d | 5687 | [self setTitle:@"COMPLETE"]; |
6915b806 | 5688 | } |
670a0494 | 5689 | |
22f8bed9 | 5690 | if (Finish_ < 4) { |
70d45c1e JF |
5691 | FileFd file; |
5692 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
5693 | _error->Discard(); | |
5694 | else { | |
5695 | MMap mmap(file, MMap::ReadOnly); | |
5696 | SHA1Summation sha1; | |
5697 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5698 | if (!(notifyconf == sha1.Result())) |
70d45c1e JF |
5699 | Finish_ = 4; |
5700 | } | |
22f8bed9 JF |
5701 | } |
5702 | ||
affeffc7 | 5703 | if (Finish_ < 3) { |
70d45c1e JF |
5704 | FileFd file; |
5705 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
5706 | _error->Discard(); | |
5707 | else { | |
5708 | MMap mmap(file, MMap::ReadOnly); | |
5709 | SHA1Summation sha1; | |
5710 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5711 | if (!(springlist == sha1.Result())) |
70d45c1e JF |
5712 | Finish_ = 3; |
5713 | } | |
dddbc481 JF |
5714 | } |
5715 | ||
be860cc8 JF |
5716 | if (Finish_ < 2) { |
5717 | if (RestartSubstrate_) | |
5718 | Finish_ = 2; | |
5719 | } | |
5720 | ||
5721 | RestartSubstrate_ = false; | |
5722 | ||
bc8cd583 | 5723 | switch (Finish_) { |
aaae308d JF |
5724 | case 0: [progress_ setFinish:UCLocalize("RETURN_TO_CYDIA")]; break; /* XXX: Maybe UCLocalize("DONE")? */ |
5725 | case 1: [progress_ setFinish:UCLocalize("CLOSE_CYDIA")]; break; | |
5726 | case 2: [progress_ setFinish:UCLocalize("RESTART_SPRINGBOARD")]; break; | |
5727 | case 3: [progress_ setFinish:UCLocalize("RELOAD_SPRINGBOARD")]; break; | |
5728 | case 4: [progress_ setFinish:UCLocalize("REBOOT_DEVICE")]; break; | |
bc8cd583 JF |
5729 | } |
5730 | ||
eb403f34 | 5731 | UpdateExternalStatus(Finish_ == 0 ? 0 : 2); |
ef494bd8 | 5732 | |
aaae308d JF |
5733 | [progress_ setRunning:false]; |
5734 | [self updateProgress]; | |
5735 | ||
5736 | [self applyRightButton]; | |
31f3cfff JF |
5737 | } |
5738 | ||
6915b806 | 5739 | - (void) addProgressEvent:(CydiaProgressEvent *)event { |
aaae308d JF |
5740 | [progress_ addEvent:event]; |
5741 | [self updateProgress]; | |
baf80942 JF |
5742 | } |
5743 | ||
ff2d5dcd | 5744 | - (bool) isProgressCancelled { |
aaae308d JF |
5745 | return cancel_ == 2; |
5746 | } | |
5747 | ||
5748 | - (void) cancel { | |
5749 | cancel_ = 2; | |
5750 | [self updateCancel]; | |
5751 | } | |
5752 | ||
5753 | - (void) setCancellable:(bool)cancellable { | |
5754 | unsigned cancel(cancel_); | |
5755 | ||
5756 | if (!cancellable) | |
5757 | cancel_ = 0; | |
5758 | else if (cancel_ == 0) | |
5759 | cancel_ = 1; | |
5760 | ||
5761 | if (cancel != cancel_) | |
5762 | [self updateCancel]; | |
5763 | } | |
5764 | ||
5765 | - (void) setProgressCancellable:(NSNumber *)cancellable { | |
5766 | [self setCancellable:[cancellable boolValue]]; | |
baf80942 JF |
5767 | } |
5768 | ||
d885343d | 5769 | - (void) setProgressPercent:(NSNumber *)percent { |
b0b11d99 | 5770 | [progress_ setPercent:[percent floatValue]]; |
aaae308d | 5771 | [self updateProgress]; |
49048579 JF |
5772 | } |
5773 | ||
bcbac8f7 JF |
5774 | - (void) setProgressStatus:(NSDictionary *)status { |
5775 | if (status == nil) { | |
5776 | [progress_ setCurrent:0]; | |
5777 | [progress_ setTotal:0]; | |
5778 | [progress_ setSpeed:0]; | |
5779 | } else { | |
5780 | [progress_ setPercent:[[status objectForKey:@"Percent"] floatValue]]; | |
5781 | ||
5782 | [progress_ setCurrent:[[status objectForKey:@"Current"] floatValue]]; | |
5783 | [progress_ setTotal:[[status objectForKey:@"Total"] floatValue]]; | |
5784 | [progress_ setSpeed:[[status objectForKey:@"Speed"] floatValue]]; | |
5785 | } | |
5786 | ||
5787 | [self updateProgress]; | |
5788 | } | |
5789 | ||
36bb2ca2 JF |
5790 | @end |
5791 | /* }}} */ | |
dc088e63 | 5792 | |
46aa9775 | 5793 | /* Package Cell {{{ */ |
a9311516 | 5794 | @interface PackageCell : CyteTableViewCell < |
b97fcfc6 | 5795 | CyteTableViewCellDelegate |
c21004b9 | 5796 | > { |
7b33d201 JF |
5797 | _H<UIImage> icon_; |
5798 | _H<NSString> name_; | |
5799 | _H<NSString> description_; | |
3bd1c2a2 | 5800 | bool commercial_; |
7b33d201 JF |
5801 | _H<NSString> source_; |
5802 | _H<UIImage> badge_; | |
7b33d201 | 5803 | _H<UIImage> placard_; |
59f3d290 | 5804 | bool summarized_; |
36bb2ca2 | 5805 | } |
723a0072 | 5806 | |
36bb2ca2 | 5807 | - (PackageCell *) init; |
59f3d290 | 5808 | - (void) setPackage:(Package *)package asSummary:(bool)summary; |
ec97ef06 | 5809 | |
327624b6 JF |
5810 | - (void) drawContentRect:(CGRect)rect; |
5811 | ||
5812 | @end | |
5813 | ||
36bb2ca2 JF |
5814 | @implementation PackageCell |
5815 | ||
36bb2ca2 | 5816 | - (PackageCell *) init { |
327624b6 JF |
5817 | CGRect frame(CGRectMake(0, 0, 320, 74)); |
5818 | if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) { | |
5819 | UIView *content([self contentView]); | |
5820 | CGRect bounds([content bounds]); | |
04fe1349 | 5821 | |
b97fcfc6 | 5822 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
04fe1349 JF |
5823 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5824 | [content addSubview:content_]; | |
5825 | ||
327624b6 | 5826 | [content_ setDelegate:self]; |
327624b6 | 5827 | [content_ setOpaque:YES]; |
36bb2ca2 | 5828 | } return self; |
b4d89997 | 5829 | } |
b19871dd | 5830 | |
003fc610 | 5831 | - (NSString *) accessibilityLabel { |
353dda5b | 5832 | return name_; |
003fc610 GP |
5833 | } |
5834 | ||
59f3d290 JF |
5835 | - (void) setPackage:(Package *)package asSummary:(bool)summary { |
5836 | summarized_ = summary; | |
5837 | ||
7b33d201 JF |
5838 | icon_ = nil; |
5839 | name_ = nil; | |
5840 | description_ = nil; | |
5841 | source_ = nil; | |
5842 | badge_ = nil; | |
5843 | placard_ = nil; | |
7b33d201 | 5844 | |
80132602 JF |
5845 | if (package == nil) |
5846 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
5847 | else { | |
5848 | [package parse]; | |
31f3cfff | 5849 | |
80132602 | 5850 | Source *source = [package source]; |
b19871dd | 5851 | |
80132602 | 5852 | icon_ = [package icon]; |
78de2878 | 5853 | |
80132602 JF |
5854 | if (NSString *name = [package name]) |
5855 | name_ = [NSString stringWithString:name]; | |
ef055c6c | 5856 | |
9374f6b0 | 5857 | if (NSString *description = [package shortDescription]) |
80132602 | 5858 | description_ = [NSString stringWithString:description]; |
ef055c6c | 5859 | |
80132602 | 5860 | commercial_ = [package isCommercial]; |
36bb2ca2 | 5861 | |
80132602 JF |
5862 | NSString *label = nil; |
5863 | bool trusted = false; | |
b19871dd | 5864 | |
80132602 JF |
5865 | if (source != nil) { |
5866 | label = [source label]; | |
5867 | trusted = [source trusted]; | |
5868 | } else if ([[package id] isEqualToString:@"firmware"]) | |
5869 | label = UCLocalize("APPLE"); | |
5870 | else | |
5871 | label = [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("UNKNOWN"), UCLocalize("LOCAL")]; | |
b19871dd | 5872 | |
80132602 | 5873 | NSString *from(label); |
a54b1c10 | 5874 | |
80132602 JF |
5875 | NSString *section = [package simpleSection]; |
5876 | if (section != nil && ![section isEqualToString:label]) { | |
5877 | section = [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
5878 | from = [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), from, section]; | |
5879 | } | |
a54b1c10 | 5880 | |
80132602 | 5881 | source_ = [NSString stringWithFormat:UCLocalize("FROM"), from]; |
36bb2ca2 | 5882 | |
80132602 JF |
5883 | if (NSString *purpose = [package primaryPurpose]) |
5884 | badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]; | |
c390d3ab | 5885 | |
80132602 JF |
5886 | UIColor *color; |
5887 | NSString *placard; | |
5888 | ||
5889 | if (NSString *mode = [package mode]) { | |
5890 | if ([mode isEqualToString:@"REMOVE"] || [mode isEqualToString:@"PURGE"]) { | |
5891 | color = RemovingColor_; | |
86a333c6 | 5892 | placard = @"removing"; |
80132602 JF |
5893 | } else { |
5894 | color = InstallingColor_; | |
86a333c6 | 5895 | placard = @"installing"; |
80132602 | 5896 | } |
d832908d | 5897 | } else { |
80132602 JF |
5898 | color = [UIColor whiteColor]; |
5899 | ||
5900 | if ([package installed] != nil) | |
5901 | placard = @"installed"; | |
5902 | else | |
5903 | placard = nil; | |
d832908d JF |
5904 | } |
5905 | ||
80132602 | 5906 | [content_ setBackgroundColor:color]; |
d832908d | 5907 | |
80132602 JF |
5908 | if (placard != nil) |
5909 | placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/%@.png", App_, placard]]; | |
d832908d JF |
5910 | } |
5911 | ||
d832908d | 5912 | [self setNeedsDisplay]; |
327624b6 | 5913 | [content_ setNeedsDisplay]; |
3bd1c2a2 JF |
5914 | } |
5915 | ||
59f3d290 | 5916 | - (void) drawSummaryContentRect:(CGRect)rect { |
555aaf71 | 5917 | bool highlighted(highlighted_); |
ef055c6c | 5918 | float width([self bounds].size.width); |
dc63e78f | 5919 | |
59f3d290 JF |
5920 | if (icon_ != nil) { |
5921 | CGRect rect; | |
5922 | rect.size = [(UIImage *) icon_ size]; | |
5923 | ||
25c1dafb | 5924 | while (rect.size.width > 16 || rect.size.height > 16) { |
7e1f9f6a JF |
5925 | rect.size.width /= 2; |
5926 | rect.size.height /= 2; | |
5927 | } | |
59f3d290 | 5928 | |
86a333c6 JF |
5929 | rect.origin.x = 19 - rect.size.width / 2; |
5930 | rect.origin.y = 19 - rect.size.height / 2; | |
59f3d290 | 5931 | |
8323c1b9 | 5932 | [icon_ drawInRect:Retina(rect)]; |
59f3d290 JF |
5933 | } |
5934 | ||
5935 | if (badge_ != nil) { | |
5936 | CGRect rect; | |
5937 | rect.size = [(UIImage *) badge_ size]; | |
5938 | ||
5939 | rect.size.width /= 4; | |
5940 | rect.size.height /= 4; | |
5941 | ||
86a333c6 JF |
5942 | rect.origin.x = 25 - rect.size.width / 2; |
5943 | rect.origin.y = 25 - rect.size.height / 2; | |
59f3d290 | 5944 | |
8323c1b9 | 5945 | [badge_ drawInRect:Retina(rect)]; |
59f3d290 JF |
5946 | } |
5947 | ||
5d0438dc | 5948 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
59f3d290 JF |
5949 | UISetColor(White_); |
5950 | ||
5951 | if (!highlighted) | |
5952 | UISetColor(commercial_ ? Purple_ : Black_); | |
b129e6d9 | 5953 | [name_ drawAtPoint:CGPointMake(36, 8) forWidth:(width - (placard_ == nil ? 68 : 94)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
59f3d290 JF |
5954 | |
5955 | if (placard_ != nil) | |
86a333c6 | 5956 | [placard_ drawAtPoint:CGPointMake(width - 52, 11)]; |
59f3d290 JF |
5957 | } |
5958 | ||
5959 | - (void) drawNormalContentRect:(CGRect)rect { | |
5960 | bool highlighted(highlighted_); | |
5961 | float width([self bounds].size.width); | |
b19871dd | 5962 | |
baf80942 JF |
5963 | if (icon_ != nil) { |
5964 | CGRect rect; | |
7b33d201 | 5965 | rect.size = [(UIImage *) icon_ size]; |
baf80942 | 5966 | |
25c1dafb | 5967 | while (rect.size.width > 32 || rect.size.height > 32) { |
7e1f9f6a JF |
5968 | rect.size.width /= 2; |
5969 | rect.size.height /= 2; | |
5970 | } | |
baf80942 JF |
5971 | |
5972 | rect.origin.x = 25 - rect.size.width / 2; | |
5973 | rect.origin.y = 25 - rect.size.height / 2; | |
5974 | ||
8323c1b9 | 5975 | [icon_ drawInRect:Retina(rect)]; |
baf80942 | 5976 | } |
b19871dd | 5977 | |
c390d3ab | 5978 | if (badge_ != nil) { |
f79c810d | 5979 | CGRect rect; |
7b33d201 | 5980 | rect.size = [(UIImage *) badge_ size]; |
f79c810d JF |
5981 | |
5982 | rect.size.width /= 2; | |
5983 | rect.size.height /= 2; | |
5984 | ||
5985 | rect.origin.x = 36 - rect.size.width / 2; | |
5986 | rect.origin.y = 36 - rect.size.height / 2; | |
c390d3ab | 5987 | |
8323c1b9 | 5988 | [badge_ drawInRect:Retina(rect)]; |
c390d3ab JF |
5989 | } |
5990 | ||
5d0438dc | 5991 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
f641a0e5 | 5992 | UISetColor(White_); |
b19871dd | 5993 | |
555aaf71 | 5994 | if (!highlighted) |
3bd1c2a2 | 5995 | UISetColor(commercial_ ? Purple_ : Black_); |
b129e6d9 JF |
5996 | [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
5997 | [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; | |
b19871dd | 5998 | |
555aaf71 | 5999 | if (!highlighted) |
3bd1c2a2 | 6000 | UISetColor(commercial_ ? Purplish_ : Gray_); |
b129e6d9 | 6001 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:NSLineBreakByTruncatingTail]; |
670a0494 JF |
6002 | |
6003 | if (placard_ != nil) | |
01d93940 | 6004 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; |
ec97ef06 JF |
6005 | } |
6006 | ||
59f3d290 JF |
6007 | - (void) drawContentRect:(CGRect)rect { |
6008 | if (summarized_) | |
6009 | [self drawSummaryContentRect:rect]; | |
6010 | else | |
6011 | [self drawNormalContentRect:rect]; | |
6012 | } | |
6013 | ||
8da60fb7 JF |
6014 | @end |
6015 | /* }}} */ | |
36bb2ca2 | 6016 | /* Section Cell {{{ */ |
a9311516 | 6017 | @interface SectionCell : CyteTableViewCell < |
b97fcfc6 | 6018 | CyteTableViewCellDelegate |
c21004b9 | 6019 | > { |
7b33d201 JF |
6020 | _H<NSString> basic_; |
6021 | _H<NSString> section_; | |
6022 | _H<NSString> name_; | |
6023 | _H<NSString> count_; | |
6024 | _H<UIImage> icon_; | |
6025 | _H<UISwitch> switch_; | |
6d9712c4 | 6026 | BOOL editing_; |
b4d89997 | 6027 | } |
b19871dd | 6028 | |
6d9712c4 | 6029 | - (void) setSection:(Section *)section editing:(BOOL)editing; |
36bb2ca2 | 6030 | |
8da60fb7 JF |
6031 | @end |
6032 | ||
36bb2ca2 | 6033 | @implementation SectionCell |
8da60fb7 | 6034 | |
46aa9775 GP |
6035 | - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
6036 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
8dbf3abc | 6037 | icon_ = [UIImage imageNamed:@"folder.png"]; |
6415105e | 6038 | // XXX: this initial frame is wrong, but is fixed later |
7b33d201 | 6039 | switch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)] autorelease]; |
46aa9775 GP |
6040 | [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; |
6041 | ||
6042 | UIView *content([self contentView]); | |
6043 | CGRect bounds([content bounds]); | |
6044 | ||
b97fcfc6 | 6045 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
46aa9775 GP |
6046 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6047 | [content addSubview:content_]; | |
6048 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
6049 | ||
6050 | [content_ setDelegate:self]; | |
b4d89997 | 6051 | } return self; |
b19871dd JF |
6052 | } |
6053 | ||
6d9712c4 | 6054 | - (void) onSwitch:(id)sender { |
a171abd4 | 6055 | NSMutableDictionary *metadata([Sections_ objectForKey:basic_]); |
6d9712c4 JF |
6056 | if (metadata == nil) { |
6057 | metadata = [NSMutableDictionary dictionaryWithCapacity:2]; | |
0010fa77 | 6058 | [Sections_ setObject:metadata forKey:basic_]; |
6d9712c4 JF |
6059 | } |
6060 | ||
46aa9775 | 6061 | [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; |
a171abd4 | 6062 | Changed_ = true; |
6d9712c4 JF |
6063 | } |
6064 | ||
6065 | - (void) setSection:(Section *)section editing:(BOOL)editing { | |
6066 | if (editing != editing_) { | |
6067 | if (editing_) | |
6068 | [switch_ removeFromSuperview]; | |
6069 | else | |
6070 | [self addSubview:switch_]; | |
6071 | editing_ = editing; | |
6072 | } | |
6073 | ||
7b33d201 JF |
6074 | basic_ = nil; |
6075 | section_ = nil; | |
6076 | name_ = nil; | |
6077 | count_ = nil; | |
6d9712c4 | 6078 | |
36bb2ca2 | 6079 | if (section == nil) { |
7b33d201 | 6080 | name_ = UCLocalize("ALL_PACKAGES"); |
f641a0e5 | 6081 | count_ = nil; |
36bb2ca2 | 6082 | } else { |
e59669fd | 6083 | basic_ = [section name]; |
677b8415 | 6084 | section_ = [section localized]; |
0010fa77 | 6085 | |
7b33d201 | 6086 | name_ = section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : (NSString *) section_; |
3e5a9f5d | 6087 | count_ = [NSString stringWithFormat:@"%zd", [section count]]; |
6d9712c4 JF |
6088 | |
6089 | if (editing_) | |
46aa9775 | 6090 | [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; |
36bb2ca2 | 6091 | } |
46aa9775 | 6092 | |
c21004b9 | 6093 | [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; |
0abb648c JF |
6094 | [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; |
6095 | ||
46aa9775 | 6096 | [content_ setNeedsDisplay]; |
f641a0e5 JF |
6097 | } |
6098 | ||
77801ff1 JF |
6099 | - (void) setFrame:(CGRect)frame { |
6100 | [super setFrame:frame]; | |
46aa9775 | 6101 | |
77801ff1 | 6102 | CGRect rect([switch_ frame]); |
6415105e | 6103 | [switch_ setFrame:CGRectMake(frame.size.width - rect.size.width - 9, 9, rect.size.width, rect.size.height)]; |
77801ff1 JF |
6104 | } |
6105 | ||
003fc610 GP |
6106 | - (NSString *) accessibilityLabel { |
6107 | return name_; | |
6108 | } | |
6109 | ||
46aa9775 | 6110 | - (void) drawContentRect:(CGRect)rect { |
8cfba088 | 6111 | bool highlighted(highlighted_ && !editing_); |
bc11cf5b | 6112 | |
86a333c6 | 6113 | [icon_ drawInRect:CGRectMake(7, 7, 32, 32)]; |
f641a0e5 | 6114 | |
5d0438dc | 6115 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
f641a0e5 JF |
6116 | UISetColor(White_); |
6117 | ||
46aa9775 | 6118 | float width(rect.size.width); |
58241d4c | 6119 | if (editing_) |
6c1ec3c7 | 6120 | width -= 9 + [switch_ frame].size.width; |
58241d4c | 6121 | |
555aaf71 JF |
6122 | if (!highlighted) |
6123 | UISetColor(Black_); | |
b129e6d9 | 6124 | [name_ drawAtPoint:CGPointMake(48, 12) forWidth:(width - 58) withFont:Font18_ lineBreakMode:NSLineBreakByTruncatingTail]; |
6d9712c4 | 6125 | |
f641a0e5 JF |
6126 | CGSize size = [count_ sizeWithFont:Font14_]; |
6127 | ||
86a333c6 | 6128 | UISetColor(Folder_); |
f641a0e5 | 6129 | if (count_ != nil) |
8323c1b9 | 6130 | [count_ drawAtPoint:CGPointMake(Retina(10 + (30 - size.width) / 2), 18) withFont:Font12Bold_]; |
b19871dd JF |
6131 | } |
6132 | ||
36bb2ca2 JF |
6133 | @end |
6134 | /* }}} */ | |
b19871dd | 6135 | |
ab398adf | 6136 | /* File Table {{{ */ |
cd79e8cf | 6137 | @interface FileTable : CyteViewController < |
c21004b9 JF |
6138 | UITableViewDataSource, |
6139 | UITableViewDelegate | |
6140 | > { | |
36bb2ca2 | 6141 | _transient Database *database_; |
7b33d201 JF |
6142 | _H<Package> package_; |
6143 | _H<NSString> name_; | |
6144 | _H<NSMutableArray> files_; | |
bf7c998c | 6145 | _H<UITableView, 2> list_; |
ab398adf | 6146 | } |
b19871dd | 6147 | |
b5e7eebb | 6148 | - (id) initWithDatabase:(Database *)database; |
ab398adf JF |
6149 | - (void) setPackage:(Package *)package; |
6150 | ||
6151 | @end | |
6152 | ||
6153 | @implementation FileTable | |
6154 | ||
eb30da80 | 6155 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
ab398adf JF |
6156 | return files_ == nil ? 0 : [files_ count]; |
6157 | } | |
6158 | ||
21ea11a4 GP |
6159 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
6160 | return 24.0f; | |
6161 | }*/ | |
ab398adf | 6162 | |
46aa9775 | 6163 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
b5e7eebb GP |
6164 | static NSString *reuseIdentifier = @"Cell"; |
6165 | ||
46aa9775 GP |
6166 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
6167 | if (cell == nil) { | |
6168 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
6169 | [cell setFont:[UIFont systemFontOfSize:16]]; | |
ab398adf | 6170 | } |
46aa9775 | 6171 | [cell setText:[files_ objectAtIndex:indexPath.row]]; |
c21004b9 | 6172 | [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
b5e7eebb | 6173 | |
46aa9775 | 6174 | return cell; |
ab398adf | 6175 | } |
b19871dd | 6176 | |
fe8e721f GP |
6177 | - (NSURL *) navigationURL { |
6178 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/files", [package_ id]]]; | |
6179 | } | |
6180 | ||
6181 | - (void) loadView { | |
e8cbebe4 | 6182 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; |
fe8e721f GP |
6183 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6184 | [list_ setRowHeight:24.0f]; | |
7b33d201 | 6185 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 6186 | [list_ setDelegate:self]; |
e8cbebe4 | 6187 | [self setView:list_]; |
fe8e721f GP |
6188 | } |
6189 | ||
6190 | - (void) viewDidLoad { | |
7d887d0b JF |
6191 | [super viewDidLoad]; |
6192 | ||
fe8e721f GP |
6193 | [[self navigationItem] setTitle:UCLocalize("INSTALLED_FILES")]; |
6194 | } | |
6195 | ||
6196 | - (void) releaseSubviews { | |
fe8e721f | 6197 | list_ = nil; |
7be3eb32 | 6198 | |
4f9acb7c JF |
6199 | package_ = nil; |
6200 | files_ = nil; | |
6201 | ||
7be3eb32 | 6202 | [super releaseSubviews]; |
fe8e721f GP |
6203 | } |
6204 | ||
b5e7eebb GP |
6205 | - (id) initWithDatabase:(Database *)database { |
6206 | if ((self = [super init]) != nil) { | |
ab398adf | 6207 | database_ = database; |
ab398adf JF |
6208 | } return self; |
6209 | } | |
6210 | ||
6211 | - (void) setPackage:(Package *)package { | |
7b33d201 JF |
6212 | package_ = nil; |
6213 | name_ = nil; | |
ab398adf | 6214 | |
4f9acb7c | 6215 | files_ = [NSMutableArray arrayWithCapacity:32]; |
ab398adf JF |
6216 | |
6217 | if (package != nil) { | |
7b33d201 JF |
6218 | package_ = package; |
6219 | name_ = [package id]; | |
ab398adf | 6220 | |
affeffc7 JF |
6221 | if (NSArray *files = [package files]) |
6222 | [files_ addObjectsFromArray:files]; | |
ab398adf | 6223 | |
9ea8d159 JF |
6224 | if ([files_ count] != 0) { |
6225 | if ([[files_ objectAtIndex:0] isEqualToString:@"/."]) | |
6226 | [files_ removeObjectAtIndex:0]; | |
a54b1c10 | 6227 | [files_ sortUsingSelector:@selector(compareByPath:)]; |
2388b078 JF |
6228 | |
6229 | NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8]; | |
6230 | [stack addObject:@"/"]; | |
6231 | ||
6232 | for (int i(0), e([files_ count]); i != e; ++i) { | |
6233 | NSString *file = [files_ objectAtIndex:i]; | |
6234 | while (![file hasPrefix:[stack lastObject]]) | |
6235 | [stack removeLastObject]; | |
6236 | NSString *directory = [stack lastObject]; | |
6237 | [stack addObject:[file stringByAppendingString:@"/"]]; | |
6238 | [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", | |
a54b1c10 | 6239 | ([stack count] - 2) * 3, "", |
2388b078 JF |
6240 | [file substringFromIndex:[directory length]] |
6241 | ]]; | |
6242 | } | |
ab398adf JF |
6243 | } |
6244 | } | |
6245 | ||
6246 | [list_ reloadData]; | |
6247 | } | |
6248 | ||
ab398adf | 6249 | - (void) reloadData { |
fe8e721f GP |
6250 | [super reloadData]; |
6251 | ||
ab398adf | 6252 | [self setPackage:[database_ packageWithName:name_]]; |
ab398adf | 6253 | } |
b4d89997 | 6254 | |
b4d89997 | 6255 | @end |
36bb2ca2 | 6256 | /* }}} */ |
adb61bda | 6257 | /* Package Controller {{{ */ |
a576488f | 6258 | @interface CYPackageController : CydiaWebViewController < |
9daa7f25 DH |
6259 | UIActionSheetDelegate |
6260 | > { | |
770f2a8e | 6261 | _transient Database *database_; |
5d79f7bf JF |
6262 | _H<Package> package_; |
6263 | _H<NSString> name_; | |
3bd1c2a2 | 6264 | bool commercial_; |
3217d35f | 6265 | std::vector<std::pair<_H<NSString>, _H<NSString>>> buttons_; |
5d79f7bf | 6266 | _H<UIBarButtonItem> button_; |
b31b87cc JF |
6267 | } |
6268 | ||
f050e4d9 | 6269 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer; |
b4d89997 | 6270 | |
36bb2ca2 | 6271 | @end |
b4d89997 | 6272 | |
f6e13561 | 6273 | @implementation CYPackageController |
b4d89997 | 6274 | |
fe8e721f | 6275 | - (NSURL *) navigationURL { |
5d79f7bf | 6276 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@", (id) name_]]; |
fe8e721f GP |
6277 | } |
6278 | ||
5a09ae08 | 6279 | - (void) _clickButtonWithName:(NSString *)name { |
3217d35f | 6280 | if ([name isEqualToString:@"CLEAR"]) |
dc63e78f | 6281 | [delegate_ clearPackage:package_]; |
3217d35f | 6282 | else if ([name isEqualToString:@"INSTALL"]) |
5a09ae08 | 6283 | [delegate_ installPackage:package_]; |
3217d35f | 6284 | else if ([name isEqualToString:@"REINSTALL"]) |
5a09ae08 | 6285 | [delegate_ installPackage:package_]; |
3217d35f | 6286 | else if ([name isEqualToString:@"REMOVE"]) |
5a09ae08 | 6287 | [delegate_ removePackage:package_]; |
3217d35f | 6288 | else if ([name isEqualToString:@"UPGRADE"]) |
5a09ae08 JF |
6289 | [delegate_ installPackage:package_]; |
6290 | else _assert(false); | |
6291 | } | |
6292 | ||
674dce72 | 6293 | - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { |
1cedb821 | 6294 | NSString *context([sheet context]); |
5a09ae08 | 6295 | |
1cedb821 | 6296 | if ([context isEqualToString:@"modify"]) { |
674dce72 | 6297 | if (button != [sheet cancelButtonIndex]) { |
60b6595c JF |
6298 | if (IsWildcat_) |
6299 | [self performSelector:@selector(_clickButtonWithName:) withObject:buttons_[button].first afterDelay:0]; | |
6300 | else | |
6301 | [self _clickButtonWithName:buttons_[button].first]; | |
674dce72 | 6302 | } |
bc11cf5b | 6303 | |
60b6595c | 6304 | [sheet dismissWithClickedButtonIndex:button animated:YES]; |
674dce72 | 6305 | } |
b4d89997 | 6306 | } |
2367a917 | 6307 | |
3e9c9e85 | 6308 | - (bool) _allowJavaScriptPanel { |
3bd1c2a2 | 6309 | return commercial_; |
3e9c9e85 JF |
6310 | } |
6311 | ||
9487f027 | 6312 | #if !AlwaysReload |
9daa7f25 | 6313 | - (void) _customButtonClicked { |
3217d35f | 6314 | size_t count(buttons_.size()); |
670a0494 JF |
6315 | if (count == 0) |
6316 | return; | |
2367a917 | 6317 | |
5a09ae08 | 6318 | if (count == 1) |
3217d35f | 6319 | [self _clickButtonWithName:buttons_[0].first]; |
5a09ae08 | 6320 | else { |
674dce72 | 6321 | NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; |
3217d35f JF |
6322 | for (const auto &button : buttons_) |
6323 | [buttons addObject:button.second]; | |
36bb2ca2 | 6324 | |
674dce72 | 6325 | UIActionSheet *sheet = [[[UIActionSheet alloc] |
9ea8d159 | 6326 | initWithTitle:nil |
36bb2ca2 | 6327 | delegate:self |
674dce72 GP |
6328 | cancelButtonTitle:nil |
6329 | destructiveButtonTitle:nil | |
6330 | otherButtonTitles:nil | |
6331 | ] autorelease]; | |
6332 | ||
6333 | for (NSString *button in buttons) [sheet addButtonWithTitle:button]; | |
6334 | if (!IsWildcat_) { | |
6335 | [sheet addButtonWithTitle:UCLocalize("CANCEL")]; | |
6336 | [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; | |
6337 | } | |
6338 | [sheet setContext:@"modify"]; | |
bc11cf5b | 6339 | |
b5e7eebb | 6340 | [delegate_ showActionSheet:sheet fromItem:[[self navigationItem] rightBarButtonItem]]; |
36bb2ca2 | 6341 | } |
b4d89997 | 6342 | } |
12b59862 | 6343 | |
2e6c1426 | 6344 | - (void) reloadButtonClicked { |
07d0e88e JF |
6345 | if (commercial_ && function_ == nil && [package_ uninstalled]) |
6346 | return; | |
6347 | [self customButtonClicked]; | |
2fad210a GP |
6348 | } |
6349 | ||
6350 | - (void) applyLoadingTitle { | |
6351 | // Don't show "Loading" as the title. Ever. | |
2e6c1426 | 6352 | } |
a5938ea5 DH |
6353 | |
6354 | - (UIBarButtonItem *) rightButton { | |
2634b249 | 6355 | return button_; |
a5938ea5 | 6356 | } |
9487f027 | 6357 | #endif |
2367a917 | 6358 | |
77259cab JF |
6359 | - (void) setPageColor:(UIColor *)color { |
6360 | return [super setPageColor:nil]; | |
6361 | } | |
6362 | ||
f050e4d9 | 6363 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer { |
b5e7eebb | 6364 | if ((self = [super init]) != nil) { |
36bb2ca2 | 6365 | database_ = database; |
5612913e | 6366 | name_ = name == nil ? @"" : [NSString stringWithString:name]; |
f050e4d9 | 6367 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]] withReferrer:referrer]; |
36bb2ca2 | 6368 | } return self; |
dc5812ec JF |
6369 | } |
6370 | ||
57e8b225 | 6371 | - (void) reloadData { |
9dac415b JF |
6372 | [super reloadData]; |
6373 | ||
57e8b225 | 6374 | package_ = [database_ packageWithName:name_]; |
36bb2ca2 | 6375 | |
3217d35f | 6376 | buttons_.clear(); |
5a09ae08 | 6377 | |
57e8b225 | 6378 | if (package_ != nil) { |
5d79f7bf | 6379 | [(Package *) package_ parse]; |
68d927e2 | 6380 | |
57e8b225 | 6381 | commercial_ = [package_ isCommercial]; |
36bb2ca2 | 6382 | |
dc63e78f | 6383 | if ([package_ mode] != nil) |
3217d35f | 6384 | buttons_.push_back(std::make_pair(@"CLEAR", UCLocalize("CLEAR"))); |
5a09ae08 | 6385 | if ([package_ source] == nil); |
31f3cfff | 6386 | else if ([package_ upgradableAndEssential:NO]) |
3217d35f | 6387 | buttons_.push_back(std::make_pair(@"UPGRADE", UCLocalize("UPGRADE"))); |
6a155117 | 6388 | else if ([package_ uninstalled]) |
3217d35f | 6389 | buttons_.push_back(std::make_pair(@"INSTALL", UCLocalize("INSTALL"))); |
5a09ae08 | 6390 | else |
3217d35f | 6391 | buttons_.push_back(std::make_pair(@"REINSTALL", UCLocalize("REINSTALL"))); |
6a155117 | 6392 | if (![package_ uninstalled]) |
3217d35f | 6393 | buttons_.push_back(std::make_pair(@"REMOVE", UCLocalize("REMOVE"))); |
2634b249 | 6394 | } |
f79a4512 | 6395 | |
2634b249 | 6396 | NSString *title; |
3217d35f | 6397 | switch (buttons_.size()) { |
2634b249 | 6398 | case 0: title = nil; break; |
3217d35f | 6399 | case 1: title = buttons_[0].second; break; |
2634b249 | 6400 | default: title = UCLocalize("MODIFY"); break; |
36bb2ca2 | 6401 | } |
2634b249 | 6402 | |
5d79f7bf | 6403 | button_ = [[[UIBarButtonItem alloc] |
2634b249 JF |
6404 | initWithTitle:title |
6405 | style:UIBarButtonItemStylePlain | |
6406 | target:self | |
6407 | action:@selector(customButtonClicked) | |
5d79f7bf | 6408 | ] autorelease]; |
b5e7eebb | 6409 | } |
f79a4512 | 6410 | |
3bd1c2a2 JF |
6411 | - (bool) isLoading { |
6412 | return commercial_ ? [super isLoading] : false; | |
9fe5e5f8 JF |
6413 | } |
6414 | ||
b4d89997 JF |
6415 | @end |
6416 | /* }}} */ | |
5829aea2 | 6417 | |
f50860ee | 6418 | /* Package List Controller {{{ */ |
cd79e8cf | 6419 | @interface PackageListController : CyteViewController < |
c21004b9 JF |
6420 | UITableViewDataSource, |
6421 | UITableViewDelegate | |
6422 | > { | |
36bb2ca2 | 6423 | _transient Database *database_; |
0175295c | 6424 | unsigned era_; |
56bf1e78 | 6425 | _H<NSArray> packages_; |
e5491e28 | 6426 | _H<NSArray> sections_; |
bf7c998c | 6427 | _H<UITableView, 2> list_; |
aeeb755b JF |
6428 | |
6429 | _H<NSArray> thumbs_; | |
6430 | std::vector<NSInteger> offset_; | |
6431 | ||
7b33d201 | 6432 | _H<NSString> title_; |
56bf1e78 | 6433 | unsigned reloading_; |
dc5812ec JF |
6434 | } |
6435 | ||
f50860ee | 6436 | - (id) initWithDatabase:(Database *)database title:(NSString *)title; |
b4d89997 | 6437 | - (void) setDelegate:(id)delegate; |
a0be02eb | 6438 | - (void) resetCursor; |
59f3d290 | 6439 | - (void) clearData; |
b4d89997 | 6440 | |
e5491e28 JF |
6441 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages; |
6442 | ||
b4d89997 JF |
6443 | @end |
6444 | ||
f50860ee | 6445 | @implementation PackageListController |
b4d89997 | 6446 | |
f050e4d9 JF |
6447 | - (NSURL *) referrerURL { |
6448 | return [self navigationURL]; | |
6449 | } | |
6450 | ||
59f3d290 JF |
6451 | - (bool) isSummarized { |
6452 | return false; | |
6453 | } | |
6454 | ||
9c5737d5 JF |
6455 | - (bool) showsSections { |
6456 | return true; | |
6457 | } | |
6458 | ||
f50860ee GP |
6459 | - (void) deselectWithAnimation:(BOOL)animated { |
6460 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
6461 | } | |
6462 | ||
bfb45dcb GP |
6463 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve { |
6464 | CGRect base = [[self view] bounds]; | |
6465 | base.size.height -= bounds.size.height; | |
6466 | base.origin = [list_ frame].origin; | |
6467 | ||
6468 | [UIView beginAnimations:nil context:NULL]; | |
6469 | [UIView setAnimationBeginsFromCurrentState:YES]; | |
6470 | [UIView setAnimationCurve:curve]; | |
6471 | [UIView setAnimationDuration:duration]; | |
6472 | [list_ setFrame:base]; | |
6473 | [UIView commitAnimations]; | |
6474 | } | |
6475 | ||
6476 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration { | |
6477 | [self resizeForKeyboardBounds:bounds duration:duration curve:UIViewAnimationCurveLinear]; | |
6478 | } | |
6479 | ||
6480 | - (void) resizeForKeyboardBounds:(CGRect)bounds { | |
6481 | [self resizeForKeyboardBounds:bounds duration:0]; | |
6482 | } | |
6483 | ||
655c7ded JF |
6484 | - (void) getKeyboardCurve:(UIViewAnimationCurve *)curve duration:(NSTimeInterval *)duration forNotification:(NSNotification *)notification { |
6485 | if (&UIKeyboardAnimationCurveUserInfoKey == NULL) | |
6486 | *curve = UIViewAnimationCurveEaseInOut; | |
6487 | else | |
6488 | [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:curve]; | |
6489 | ||
6490 | if (&UIKeyboardAnimationDurationUserInfoKey == NULL) | |
6491 | *duration = 0.3; | |
6492 | else | |
6493 | [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:duration]; | |
6494 | } | |
6495 | ||
bfb45dcb GP |
6496 | - (void) keyboardWillShow:(NSNotification *)notification { |
6497 | CGRect bounds; | |
6498 | CGPoint center; | |
bfb45dcb GP |
6499 | [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds]; |
6500 | [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:¢er]; | |
655c7ded JF |
6501 | |
6502 | NSTimeInterval duration; | |
6503 | UIViewAnimationCurve curve; | |
6504 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; | |
bfb45dcb | 6505 | |
8323c1b9 | 6506 | CGRect kbframe = CGRectMake(Retina(center.x - bounds.size.width / 2), Retina(center.y - bounds.size.height / 2), bounds.size.width, bounds.size.height); |
38991110 | 6507 | UIViewController *base = self; |
19f2d77f JF |
6508 | while ([base parentOrPresentingViewController] != nil) |
6509 | base = [base parentOrPresentingViewController]; | |
38991110 | 6510 | CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]]; |
bfb45dcb GP |
6511 | CGRect intersection = CGRectIntersection(viewframe, kbframe); |
6512 | ||
2e35f65f | 6513 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) |
0b7516cf | 6514 | intersection.size.height += CYStatusBarHeight(); |
2e35f65f | 6515 | |
bfb45dcb GP |
6516 | [self resizeForKeyboardBounds:intersection duration:duration curve:curve]; |
6517 | } | |
6518 | ||
6519 | - (void) keyboardWillHide:(NSNotification *)notification { | |
6520 | NSTimeInterval duration; | |
6521 | UIViewAnimationCurve curve; | |
655c7ded | 6522 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; |
bfb45dcb GP |
6523 | |
6524 | [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve]; | |
6525 | } | |
6526 | ||
6527 | - (void) viewWillAppear:(BOOL)animated { | |
6528 | [super viewWillAppear:animated]; | |
6529 | ||
6530 | [self resizeForKeyboardBounds:CGRectZero]; | |
6531 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
6532 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
6533 | } | |
6534 | ||
6535 | - (void) viewWillDisappear:(BOOL)animated { | |
6536 | [super viewWillDisappear:animated]; | |
6537 | ||
6538 | [self resizeForKeyboardBounds:CGRectZero]; | |
6539 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
6540 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
6541 | } | |
6542 | ||
f50860ee GP |
6543 | - (void) viewDidAppear:(BOOL)animated { |
6544 | [super viewDidAppear:animated]; | |
6545 | [self deselectWithAnimation:animated]; | |
6546 | } | |
6547 | ||
6548 | - (void) didSelectPackage:(Package *)package { | |
f050e4d9 | 6549 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id] withReferrer:[[self referrerURL] absoluteString]] autorelease]); |
f50860ee GP |
6550 | [view setDelegate:delegate_]; |
6551 | [[self navigationController] pushViewController:view animated:YES]; | |
6552 | } | |
6553 | ||
327624b6 JF |
6554 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
6555 | NSInteger count([sections_ count]); | |
6556 | return count == 0 ? 1 : count; | |
b4d89997 | 6557 | } |
2367a917 | 6558 | |
327624b6 | 6559 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
a891c345 | 6560 | if ([sections_ count] == 0 || [[sections_ objectAtIndex:section] count] == 0) |
327624b6 | 6561 | return nil; |
b4d89997 JF |
6562 | return [[sections_ objectAtIndex:section] name]; |
6563 | } | |
dc5812ec | 6564 | |
327624b6 JF |
6565 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
6566 | if ([sections_ count] == 0) | |
6567 | return 0; | |
6568 | return [[sections_ objectAtIndex:section] count]; | |
b4d89997 | 6569 | } |
dc5812ec | 6570 | |
327624b6 | 6571 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
0175295c JF |
6572 | @synchronized (database_) { |
6573 | if ([database_ era] != era_) | |
6574 | return nil; | |
6575 | ||
327624b6 JF |
6576 | Section *section([sections_ objectAtIndex:[path section]]); |
6577 | NSInteger row([path row]); | |
6578 | Package *package([packages_ objectAtIndex:([section row] + row)]); | |
0175295c JF |
6579 | return [[package retain] autorelease]; |
6580 | } } | |
dc5812ec | 6581 | |
327624b6 | 6582 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
c21004b9 | 6583 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
327624b6 JF |
6584 | if (cell == nil) |
6585 | cell = [[[PackageCell alloc] init] autorelease]; | |
60bef540 JF |
6586 | |
6587 | Package *package([database_ packageWithName:[[self packageAtIndexPath:path] id]]); | |
6588 | [cell setPackage:package asSummary:[self isSummarized]]; | |
327624b6 | 6589 | return cell; |
b4d89997 | 6590 | } |
dc5812ec | 6591 | |
f50860ee | 6592 | - (void) tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path { |
327624b6 | 6593 | Package *package([self packageAtIndexPath:path]); |
59c6ae22 | 6594 | package = [database_ packageWithName:[package id]]; |
f50860ee | 6595 | [self didSelectPackage:package]; |
327624b6 JF |
6596 | } |
6597 | ||
6598 | - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { | |
aeeb755b | 6599 | return thumbs_; |
327624b6 JF |
6600 | } |
6601 | ||
a891c345 | 6602 | - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { |
aeeb755b | 6603 | return offset_[index]; |
dc5812ec JF |
6604 | } |
6605 | ||
59f3d290 JF |
6606 | - (void) updateHeight { |
6607 | [list_ setRowHeight:([self isSummarized] ? 38 : 73)]; | |
6608 | } | |
6609 | ||
f50860ee GP |
6610 | - (id) initWithDatabase:(Database *)database title:(NSString *)title { |
6611 | if ((self = [super init]) != nil) { | |
36bb2ca2 | 6612 | database_ = database; |
f50860ee GP |
6613 | title_ = [title copy]; |
6614 | [[self navigationItem] setTitle:title_]; | |
61cc4dbc JF |
6615 | } return self; |
6616 | } | |
dc5812ec | 6617 | |
61cc4dbc | 6618 | - (void) loadView { |
b62b3788 JF |
6619 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
6620 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
6621 | [self setView:view]; | |
6622 | ||
6623 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; | |
61cc4dbc | 6624 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
b62b3788 | 6625 | [view addSubview:list_]; |
1527b095 | 6626 | |
61cc4dbc JF |
6627 | // XXX: is 20 the most optimal number here? |
6628 | [list_ setSectionIndexMinimumDisplayRowCount:20]; | |
59f3d290 | 6629 | |
61cc4dbc JF |
6630 | [(UITableView *) list_ setDataSource:self]; |
6631 | [list_ setDelegate:self]; | |
6632 | ||
6633 | [self updateHeight]; | |
6634 | } | |
6635 | ||
6636 | - (void) releaseSubviews { | |
6637 | list_ = nil; | |
7be3eb32 | 6638 | |
4f9acb7c JF |
6639 | packages_ = nil; |
6640 | sections_ = nil; | |
aeeb755b JF |
6641 | |
6642 | thumbs_ = nil; | |
6643 | offset_.clear(); | |
4f9acb7c | 6644 | |
7be3eb32 | 6645 | [super releaseSubviews]; |
dc5812ec JF |
6646 | } |
6647 | ||
6648 | - (void) setDelegate:(id)delegate { | |
2367a917 | 6649 | delegate_ = delegate; |
b4d89997 JF |
6650 | } |
6651 | ||
3025d5b4 JF |
6652 | - (bool) shouldYield { |
6653 | return false; | |
6654 | } | |
b4d89997 | 6655 | |
56bf1e78 JF |
6656 | - (bool) shouldBlock { |
6657 | return false; | |
6658 | } | |
6659 | ||
9c5737d5 | 6660 | - (NSMutableArray *) _reloadPackages { |
695fdd5c | 6661 | @synchronized (database_) { |
c28bc6f1 JF |
6662 | era_ = [database_ era]; |
6663 | NSArray *packages([database_ packages]); | |
6664 | ||
9c5737d5 | 6665 | return [NSMutableArray arrayWithArray:packages]; |
695fdd5c | 6666 | } } |
3025d5b4 JF |
6667 | |
6668 | - (void) _reloadData { | |
56bf1e78 JF |
6669 | if (reloading_ != 0) { |
6670 | reloading_ = 2; | |
6671 | return; | |
6672 | } | |
6673 | ||
e5491e28 | 6674 | NSMutableArray *packages; |
ae60e2c1 | 6675 | |
0c8f53c0 | 6676 | reload: |
3025d5b4 | 6677 | if ([self shouldYield]) { |
bec28dda JF |
6678 | do { |
6679 | UIProgressHUD *hud; | |
56bf1e78 | 6680 | |
bec28dda JF |
6681 | if (![self shouldBlock]) |
6682 | hud = nil; | |
6683 | else { | |
6684 | hud = [delegate_ addProgressHUD]; | |
6685 | [hud setText:UCLocalize("LOADING")]; | |
6686 | } | |
56bf1e78 | 6687 | |
56bf1e78 | 6688 | reloading_ = 1; |
ae60e2c1 | 6689 | packages = [self yieldToSelector:@selector(_reloadPackages)]; |
bec28dda JF |
6690 | |
6691 | if (hud != nil) | |
6692 | [delegate_ removeProgressHUD:hud]; | |
56bf1e78 | 6693 | } while (reloading_ == 2); |
3025d5b4 | 6694 | } else { |
ae60e2c1 | 6695 | packages = [self _reloadPackages]; |
3025d5b4 | 6696 | } |
36bb2ca2 | 6697 | |
0c8f53c0 JF |
6698 | @synchronized (database_) { |
6699 | if (era_ != [database_ era]) | |
6700 | goto reload; | |
6701 | reloading_ = 0; | |
6702 | ||
aeeb755b JF |
6703 | thumbs_ = nil; |
6704 | offset_.clear(); | |
6705 | ||
ae60e2c1 | 6706 | packages_ = packages; |
aeeb755b JF |
6707 | |
6708 | if ([self showsSections]) | |
6709 | sections_ = [self sectionsForPackages:packages]; | |
6710 | else { | |
6711 | Section *section([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); | |
6712 | [section setCount:[packages_ count]]; | |
6713 | sections_ = [NSArray arrayWithObject:section]; | |
6714 | } | |
ae60e2c1 | 6715 | |
e5491e28 JF |
6716 | [self updateHeight]; |
6717 | ||
6718 | _profile(PackageTable$reloadData$List) | |
6719 | [(UITableView *) list_ setDataSource:self]; | |
6720 | [list_ reloadData]; | |
6721 | _end | |
1a83afc6 JF |
6722 | } |
6723 | ||
6724 | PrintTimes(); | |
6725 | } | |
e5491e28 JF |
6726 | |
6727 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { | |
aeeb755b JF |
6728 | Section *prefix([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); |
6729 | size_t end([packages count]); | |
b4d89997 | 6730 | |
aeeb755b JF |
6731 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); |
6732 | Section *section(prefix); | |
c4dcf2c2 | 6733 | |
aeeb755b JF |
6734 | thumbs_ = CollationThumbs_; |
6735 | offset_ = CollationOffset_; | |
b4d89997 | 6736 | |
aeeb755b JF |
6737 | size_t offset(0); |
6738 | size_t offsets([CollationStarts_ count]); | |
b4d89997 | 6739 | |
aeeb755b JF |
6740 | NSString *start([CollationStarts_ objectAtIndex:offset]); |
6741 | size_t length([start length]); | |
327624b6 | 6742 | |
aeeb755b JF |
6743 | for (size_t index(0); index != end; ++index) { |
6744 | if (start != nil) { | |
6745 | Package *package([packages objectAtIndex:index]); | |
22fd24dd | 6746 | NSString *name(PackageName(package, @selector(cyname))); |
a891c345 | 6747 | |
aeeb755b | 6748 | //while ([start compare:name options:NSNumericSearch range:NSMakeRange(0, length) locale:CollationLocale_] != NSOrderedDescending) { |
22fd24dd | 6749 | while (StringNameCompare(start, name, length) != kCFCompareGreaterThan) { |
aeeb755b JF |
6750 | NSString *title([CollationTitles_ objectAtIndex:offset]); |
6751 | section = [[[Section alloc] initWithName:title row:index localize:NO] autorelease]; | |
6752 | [sections addObject:section]; | |
a891c345 | 6753 | |
aeeb755b JF |
6754 | start = ++offset == offsets ? nil : [CollationStarts_ objectAtIndex:offset]; |
6755 | if (start == nil) | |
6756 | break; | |
6757 | length = [start length]; | |
808c6eb6 | 6758 | } |
59f3d290 JF |
6759 | } |
6760 | ||
aeeb755b JF |
6761 | [section addToCount]; |
6762 | } | |
a891c345 | 6763 | |
aeeb755b JF |
6764 | for (; offset != offsets; ++offset) { |
6765 | NSString *title([CollationTitles_ objectAtIndex:offset]); | |
6766 | Section *section([[[Section alloc] initWithName:title row:end localize:NO] autorelease]); | |
6767 | [sections addObject:section]; | |
6768 | } | |
a891c345 | 6769 | |
aeeb755b JF |
6770 | if ([prefix count] != 0) { |
6771 | Section *suffix([sections lastObject]); | |
6772 | [prefix setName:[suffix name]]; | |
6773 | [suffix setName:nil]; | |
6774 | [sections insertObject:prefix atIndex:(offsets - 1)]; | |
a891c345 | 6775 | } |
b4d89997 | 6776 | |
e5491e28 JF |
6777 | return sections; |
6778 | } | |
b4d89997 | 6779 | |
3025d5b4 JF |
6780 | - (void) reloadData { |
6781 | [super reloadData]; | |
6d246265 JF |
6782 | |
6783 | if ([self shouldYield]) | |
6784 | [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; | |
6785 | else | |
6786 | [self _reloadData]; | |
3025d5b4 JF |
6787 | } |
6788 | ||
a0be02eb | 6789 | - (void) resetCursor { |
4c6a29cd | 6790 | [list_ scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; |
a0be02eb JF |
6791 | } |
6792 | ||
59f3d290 JF |
6793 | - (void) clearData { |
6794 | [self updateHeight]; | |
6795 | ||
6796 | [list_ setDataSource:nil]; | |
6797 | [list_ reloadData]; | |
6798 | ||
6799 | [self resetCursor]; | |
6800 | } | |
6801 | ||
a3328c28 JF |
6802 | @end |
6803 | /* }}} */ | |
f50860ee | 6804 | /* Filtered Package List Controller {{{ */ |
89bdef78 JF |
6805 | typedef Function<bool, Package *> PackageFilter; |
6806 | typedef Function<void, NSMutableArray *> PackageSorter; | |
f50860ee | 6807 | @interface FilteredPackageListController : PackageListController { |
89bdef78 JF |
6808 | PackageFilter filter_; |
6809 | PackageSorter sorter_; | |
a3328c28 JF |
6810 | } |
6811 | ||
89bdef78 | 6812 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter; |
3025d5b4 | 6813 | |
89bdef78 JF |
6814 | - (void) setFilter:(PackageFilter)filter; |
6815 | - (void) setSorter:(PackageSorter)sorter; | |
a3328c28 JF |
6816 | |
6817 | @end | |
6818 | ||
f50860ee | 6819 | @implementation FilteredPackageListController |
a3328c28 | 6820 | |
89bdef78 | 6821 | - (void) setFilter:(PackageFilter)filter { |
aa32d91b | 6822 | @synchronized (self) { |
01d93940 | 6823 | filter_ = filter; |
55066b9e JF |
6824 | } } |
6825 | ||
89bdef78 | 6826 | - (void) setSorter:(PackageSorter)sorter { |
55066b9e | 6827 | @synchronized (self) { |
89bdef78 | 6828 | sorter_ = sorter; |
d84597fe | 6829 | } } |
01d93940 | 6830 | |
9c5737d5 | 6831 | - (NSMutableArray *) _reloadPackages { |
aa32d91b | 6832 | @synchronized (database_) { |
c28bc6f1 | 6833 | era_ = [database_ era]; |
c28bc6f1 | 6834 | |
dd4e70dc JF |
6835 | NSArray *packages([database_ packages]); |
6836 | NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); | |
aa32d91b | 6837 | |
89bdef78 | 6838 | PackageFilter filter; |
dd4e70dc | 6839 | PackageSorter sorter; |
aa32d91b JF |
6840 | |
6841 | @synchronized (self) { | |
aa32d91b | 6842 | filter = filter_; |
89bdef78 | 6843 | sorter = sorter_; |
aa32d91b JF |
6844 | } |
6845 | ||
6846 | _profile(PackageTable$reloadData$Filter) | |
6847 | for (Package *package in packages) | |
89bdef78 | 6848 | if ([package valid] && filter(package)) |
aa32d91b | 6849 | [filtered addObject:package]; |
76933519 | 6850 | _end |
aa32d91b | 6851 | |
89bdef78 JF |
6852 | if (sorter) |
6853 | sorter(filtered); | |
aa32d91b | 6854 | return filtered; |
dd4e70dc | 6855 | } } |
55066b9e | 6856 | |
89bdef78 | 6857 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter { |
55066b9e JF |
6858 | if ((self = [super initWithDatabase:database title:title]) != nil) { |
6859 | [self setFilter:filter]; | |
a3328c28 JF |
6860 | } return self; |
6861 | } | |
6862 | ||
dc5812ec | 6863 | @end |
b5e7eebb GP |
6864 | /* }}} */ |
6865 | ||
5829aea2 | 6866 | /* Home Controller {{{ */ |
a576488f | 6867 | @interface HomeController : CydiaWebViewController { |
02f21c73 JF |
6868 | CFRunLoopRef runloop_; |
6869 | SCNetworkReachabilityRef reachability_; | |
b4d89997 | 6870 | } |
6840bff3 | 6871 | |
7b0ce2da | 6872 | @end |
b4d89997 | 6873 | |
5829aea2 | 6874 | @implementation HomeController |
46aa9775 | 6875 | |
02f21c73 JF |
6876 | static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachability, SCNetworkReachabilityFlags flags, void *info) { |
6877 | [(HomeController *) info dispatchEvent:@"CydiaReachabilityCallback"]; | |
6878 | } | |
6879 | ||
3c62d654 JF |
6880 | - (id) init { |
6881 | if ((self = [super init]) != nil) { | |
90351d93 | 6882 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/home/", UI_]]]; |
ed7bfd8c | 6883 | [self reloadData]; |
02f21c73 JF |
6884 | |
6885 | reachability_ = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "cydia.saurik.com"); | |
6886 | if (reachability_ != NULL) { | |
6887 | SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL}; | |
6888 | SCNetworkReachabilitySetCallback(reachability_, HomeControllerReachabilityCallback, &context); | |
6889 | ||
6890 | CFRunLoopRef runloop(CFRunLoopGetCurrent()); | |
6891 | if (SCNetworkReachabilityScheduleWithRunLoop(reachability_, runloop, kCFRunLoopDefaultMode)) | |
6892 | runloop_ = runloop; | |
6893 | } | |
3c62d654 JF |
6894 | } return self; |
6895 | } | |
6896 | ||
02f21c73 JF |
6897 | - (void) dealloc { |
6898 | if (reachability_ != NULL && runloop_ != NULL) | |
6899 | SCNetworkReachabilityUnscheduleFromRunLoop(reachability_, runloop_, kCFRunLoopDefaultMode); | |
6900 | [super dealloc]; | |
6901 | } | |
6902 | ||
fe8e721f GP |
6903 | - (NSURL *) navigationURL { |
6904 | return [NSURL URLWithString:@"cydia://home"]; | |
6905 | } | |
6906 | ||
5829aea2 GP |
6907 | - (void) aboutButtonClicked { |
6908 | UIAlertView *alert([[[UIAlertView alloc] init] autorelease]); | |
b4d89997 | 6909 | |
5829aea2 GP |
6910 | [alert setTitle:UCLocalize("ABOUT_CYDIA")]; |
6911 | [alert addButtonWithTitle:UCLocalize("CLOSE")]; | |
6912 | [alert setCancelButtonIndex:0]; | |
46aa9775 | 6913 | |
5829aea2 | 6914 | [alert setMessage: |
6fa0bb60 | 6915 | @"Copyright \u00a9 2008-2014\n" |
7e865c1e JF |
6916 | "SaurikIT, LLC\n" |
6917 | "\n" | |
5829aea2 GP |
6918 | "Jay Freeman (saurik)\n" |
6919 | "saurik@saurik.com\n" | |
6920 | "http://www.saurik.com/" | |
6921 | ]; | |
46aa9775 | 6922 | |
5829aea2 | 6923 | [alert show]; |
a0376fc1 JF |
6924 | } |
6925 | ||
e6124cb6 JF |
6926 | - (UIBarButtonItem *) leftButton { |
6927 | return [[[UIBarButtonItem alloc] | |
35f0a3b5 GP |
6928 | initWithTitle:UCLocalize("ABOUT") |
6929 | style:UIBarButtonItemStylePlain | |
6930 | target:self | |
6931 | action:@selector(aboutButtonClicked) | |
e6124cb6 | 6932 | ] autorelease]; |
7b0ce2da JF |
6933 | } |
6934 | ||
5829aea2 GP |
6935 | @end |
6936 | /* }}} */ | |
7b0ce2da | 6937 | |
28e596e4 | 6938 | /* Cydia Navigation Controller Interface {{{ */ |
15f0d613 | 6939 | @interface UINavigationController (Cydia) |
28e596e4 JF |
6940 | |
6941 | - (NSArray *) navigationURLCollection; | |
15f0d613 | 6942 | - (void) unloadData; |
28e596e4 JF |
6943 | |
6944 | @end | |
6945 | /* }}} */ | |
7b0ce2da | 6946 | |
5829aea2 | 6947 | /* Cydia Tab Bar Controller {{{ */ |
5fe2bcc6 | 6948 | @interface CydiaTabBarController : CyteTabBarController < |
9f99f3da | 6949 | UITabBarControllerDelegate, |
21ac0ce2 | 6950 | FetchDelegate |
5829aea2 GP |
6951 | > { |
6952 | _transient Database *database_; | |
7b0ce2da | 6953 | |
e67ebdad JF |
6954 | _H<UIActivityIndicatorView> indicator_; |
6955 | ||
5829aea2 GP |
6956 | bool updating_; |
6957 | // XXX: ok, "updatedelegate_"?... | |
6958 | _transient NSObject<CydiaDelegate> *updatedelegate_; | |
7b0ce2da JF |
6959 | } |
6960 | ||
35f0a3b5 | 6961 | - (NSArray *) navigationURLCollection; |
5829aea2 | 6962 | - (void) beginUpdate; |
5829aea2 | 6963 | - (BOOL) updating; |
1cedb821 | 6964 | |
5829aea2 | 6965 | @end |
2fc76a2d | 6966 | |
5fe2bcc6 | 6967 | @implementation CydiaTabBarController |
9f99f3da | 6968 | |
35f0a3b5 | 6969 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
6970 | NSMutableArray *items([NSMutableArray array]); |
6971 | ||
35f0a3b5 | 6972 | // XXX: Should this deal with transient view controllers? |
fe8e721f | 6973 | for (id navigation in [self viewControllers]) { |
35f0a3b5 | 6974 | NSArray *stack = [navigation performSelector:@selector(navigationURLCollection)]; |
fe8e721f GP |
6975 | if (stack != nil) |
6976 | [items addObject:stack]; | |
2fc76a2d | 6977 | } |
c713af59 | 6978 | |
fe8e721f GP |
6979 | return items; |
6980 | } | |
6981 | ||
b5e7eebb GP |
6982 | - (id) initWithDatabase:(Database *)database { |
6983 | if ((self = [super init]) != nil) { | |
7b0ce2da | 6984 | database_ = database; |
9f99f3da | 6985 | [self setDelegate:self]; |
04fe1349 | 6986 | |
e67ebdad JF |
6987 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteTiny] autorelease]; |
6988 | [indicator_ setOrigin:CGPointMake(kCFCoreFoundationVersionNumber >= 800 ? 2 : 4, 2)]; | |
7b0ce2da | 6989 | |
e67ebdad | 6990 | [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b0ce2da JF |
6991 | } return self; |
6992 | } | |
6993 | ||
5829aea2 GP |
6994 | - (void) setUpdate:(NSDate *)date { |
6995 | [self beginUpdate]; | |
6996 | } | |
1cedb821 | 6997 | |
5829aea2 | 6998 | - (void) beginUpdate { |
20d59843 JF |
6999 | if (updating_) |
7000 | return; | |
7001 | ||
e67ebdad JF |
7002 | UIViewController *controller([[self viewControllers] objectAtIndex:1]); |
7003 | UITabBarItem *item([controller tabBarItem]); | |
7004 | ||
7005 | [item setBadgeValue:@""]; | |
7006 | UIView *badge(MSHookIvar<UIView *>([item view], "_badge")); | |
7007 | ||
7008 | [indicator_ startAnimating]; | |
7009 | [badge addSubview:indicator_]; | |
7b0ce2da | 7010 | |
5829aea2 GP |
7011 | [updatedelegate_ retainNetworkActivityIndicator]; |
7012 | updating_ = true; | |
7b0ce2da | 7013 | |
5829aea2 GP |
7014 | [NSThread |
7015 | detachNewThreadSelector:@selector(performUpdate) | |
7016 | toTarget:self | |
7017 | withObject:nil | |
7018 | ]; | |
7b0ce2da JF |
7019 | } |
7020 | ||
d13edf44 JF |
7021 | - (void) performUpdate { |
7022 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
7023 | ||
21ac0ce2 | 7024 | SourceStatus status(self, database_); |
5829aea2 GP |
7025 | [database_ updateWithStatus:status]; |
7026 | ||
7027 | [self | |
7028 | performSelectorOnMainThread:@selector(completeUpdate) | |
7029 | withObject:nil | |
7030 | waitUntilDone:NO | |
7031 | ]; | |
d13edf44 JF |
7032 | |
7033 | [pool release]; | |
f6e13561 GP |
7034 | } |
7035 | ||
5829aea2 GP |
7036 | - (void) stopUpdateWithSelector:(SEL)selector { |
7037 | updating_ = false; | |
7038 | [updatedelegate_ releaseNetworkActivityIndicator]; | |
b5e7eebb | 7039 | |
e67ebdad JF |
7040 | UIViewController *controller([[self viewControllers] objectAtIndex:1]); |
7041 | [[controller tabBarItem] setBadgeValue:nil]; | |
7042 | ||
7043 | [indicator_ removeFromSuperview]; | |
7044 | [indicator_ stopAnimating]; | |
bc11cf5b | 7045 | |
5829aea2 | 7046 | [updatedelegate_ performSelector:selector withObject:nil afterDelay:0]; |
2bdd73bd GP |
7047 | } |
7048 | ||
5829aea2 GP |
7049 | - (void) completeUpdate { |
7050 | if (!updating_) | |
7051 | return; | |
7052 | [self stopUpdateWithSelector:@selector(reloadData)]; | |
7b0ce2da JF |
7053 | } |
7054 | ||
5829aea2 | 7055 | - (void) cancelUpdate { |
383a58ac | 7056 | [self stopUpdateWithSelector:@selector(updateDataAndLoad)]; |
7b0ce2da JF |
7057 | } |
7058 | ||
5829aea2 GP |
7059 | - (void) cancelPressed { |
7060 | [self cancelUpdate]; | |
7061 | } | |
7b0ce2da | 7062 | |
5829aea2 GP |
7063 | - (BOOL) updating { |
7064 | return updating_; | |
7b0ce2da JF |
7065 | } |
7066 | ||
21ac0ce2 | 7067 | - (bool) isSourceCancelled { |
5829aea2 GP |
7068 | return !updating_; |
7069 | } | |
7b0ce2da | 7070 | |
21ac0ce2 | 7071 | - (void) startSourceFetch:(NSString *)uri { |
7b0ce2da JF |
7072 | } |
7073 | ||
21ac0ce2 | 7074 | - (void) stopSourceFetch:(NSString *)uri { |
bcbac8f7 JF |
7075 | } |
7076 | ||
5829aea2 GP |
7077 | - (void) setUpdateDelegate:(id)delegate { |
7078 | updatedelegate_ = delegate; | |
7b0ce2da JF |
7079 | } |
7080 | ||
7b0ce2da | 7081 | @end |
98228790 | 7082 | /* }}} */ |
f6e13561 | 7083 | |
28e596e4 | 7084 | /* Cydia Navigation Controller Implementation {{{ */ |
15f0d613 | 7085 | @implementation UINavigationController (Cydia) |
bc11cf5b | 7086 | |
35f0a3b5 | 7087 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
7088 | NSMutableArray *stack([NSMutableArray array]); |
7089 | ||
cd79e8cf | 7090 | for (CyteViewController *controller in [self viewControllers]) { |
fe8e721f GP |
7091 | NSString *url = [[controller navigationURL] absoluteString]; |
7092 | if (url != nil) | |
7093 | [stack addObject:url]; | |
7094 | } | |
7095 | ||
7096 | return stack; | |
7097 | } | |
7098 | ||
15f0d613 JF |
7099 | - (void) reloadData { |
7100 | [super reloadData]; | |
7101 | ||
21263088 JF |
7102 | UIViewController *visible([self visibleViewController]); |
7103 | if (visible != nil) | |
15f0d613 | 7104 | [visible reloadData]; |
21263088 JF |
7105 | |
7106 | // on the iPad, this view controller is ALSO visible. :( | |
7107 | if (IsWildcat_) | |
907ba491 JF |
7108 | if (UIViewController *modal = [self modalViewController]) |
7109 | if ([modal modalPresentationStyle] == UIModalPresentationFormSheet) | |
7110 | if (UIViewController *top = [self topViewController]) | |
7111 | if (top != visible) | |
7112 | [top reloadData]; | |
15f0d613 | 7113 | } |
6c0ba3d9 | 7114 | |
15f0d613 | 7115 | - (void) unloadData { |
cd79e8cf | 7116 | for (CyteViewController *page in [self viewControllers]) |
15f0d613 | 7117 | [page unloadData]; |
6c0ba3d9 | 7118 | |
15f0d613 | 7119 | [super unloadData]; |
dc63e78f JF |
7120 | } |
7121 | ||
5829aea2 GP |
7122 | @end |
7123 | /* }}} */ | |
7b0ce2da | 7124 | |
5829aea2 GP |
7125 | /* Cydia:// Protocol {{{ */ |
7126 | @interface CydiaURLProtocol : NSURLProtocol { | |
9fe5e5f8 JF |
7127 | } |
7128 | ||
7b0ce2da JF |
7129 | @end |
7130 | ||
5829aea2 GP |
7131 | @implementation CydiaURLProtocol |
7132 | ||
7133 | + (BOOL) canInitWithRequest:(NSURLRequest *)request { | |
7134 | NSURL *url([request URL]); | |
7135 | if (url == nil) | |
7136 | return NO; | |
b0a2900d | 7137 | |
5829aea2 | 7138 | NSString *scheme([[url scheme] lowercaseString]); |
b0a2900d JF |
7139 | if (scheme != nil && [scheme isEqualToString:@"cydia"]) |
7140 | return YES; | |
7141 | if ([[url absoluteString] hasPrefix:@"about:cydia-"]) | |
7142 | return YES; | |
7143 | ||
7144 | return NO; | |
aa5e5990 JF |
7145 | } |
7146 | ||
5829aea2 GP |
7147 | + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { |
7148 | return request; | |
7149 | } | |
aa5e5990 | 7150 | |
5829aea2 GP |
7151 | - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { |
7152 | id<NSURLProtocolClient> client([self client]); | |
7153 | if (icon == nil) | |
7154 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; | |
7155 | else { | |
7156 | NSData *data(UIImagePNGRepresentation(icon)); | |
01d93940 | 7157 | |
5829aea2 GP |
7158 | NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); |
7159 | [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; | |
7160 | [client URLProtocol:self didLoadData:data]; | |
7161 | [client URLProtocolDidFinishLoading:self]; | |
7162 | } | |
3931b718 JF |
7163 | } |
7164 | ||
5829aea2 GP |
7165 | - (void) startLoading { |
7166 | id<NSURLProtocolClient> client([self client]); | |
7167 | NSURLRequest *request([self request]); | |
bc11cf5b | 7168 | |
5829aea2 GP |
7169 | NSURL *url([request URL]); |
7170 | NSString *href([url absoluteString]); | |
b0a2900d JF |
7171 | NSString *scheme([[url scheme] lowercaseString]); |
7172 | ||
7173 | NSString *path; | |
7174 | ||
7175 | if ([scheme isEqualToString:@"cydia"]) | |
7176 | path = [href substringFromIndex:8]; | |
7177 | else if ([scheme isEqualToString:@"about"]) | |
7178 | path = [href substringFromIndex:12]; | |
7179 | else _assert(false); | |
bc11cf5b | 7180 | |
5829aea2 | 7181 | NSRange slash([path rangeOfString:@"/"]); |
bc11cf5b | 7182 | |
5829aea2 GP |
7183 | NSString *command; |
7184 | if (slash.location == NSNotFound) { | |
7185 | command = path; | |
7186 | path = nil; | |
7187 | } else { | |
7188 | command = [path substringToIndex:slash.location]; | |
7189 | path = [path substringFromIndex:(slash.location + 1)]; | |
7190 | } | |
39cda3a8 | 7191 | |
5829aea2 | 7192 | Database *database([Database sharedInstance]); |
bc11cf5b | 7193 | |
5829aea2 GP |
7194 | if ([command isEqualToString:@"package-icon"]) { |
7195 | if (path == nil) | |
7196 | goto fail; | |
7197 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7198 | Package *package([database packageWithName:path]); | |
7199 | if (package == nil) | |
7200 | goto fail; | |
eff20a8d | 7201 | [package parse]; |
5829aea2 GP |
7202 | UIImage *icon([package icon]); |
7203 | [self _returnPNGWithImage:icon forRequest:request]; | |
5829aea2 GP |
7204 | } else if ([command isEqualToString:@"uikit-image"]) { |
7205 | if (path == nil) | |
7206 | goto fail; | |
7207 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7208 | UIImage *icon(_UIImageWithName(path)); | |
7209 | [self _returnPNGWithImage:icon forRequest:request]; | |
7210 | } else if ([command isEqualToString:@"section-icon"]) { | |
7211 | if (path == nil) | |
7212 | goto fail; | |
7213 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
611efc17 | 7214 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [path stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]); |
5829aea2 | 7215 | if (icon == nil) |
8dbf3abc | 7216 | icon = [UIImage imageNamed:@"unknown.png"]; |
5829aea2 GP |
7217 | [self _returnPNGWithImage:icon forRequest:request]; |
7218 | } else fail: { | |
7219 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; | |
7220 | } | |
39cda3a8 GP |
7221 | } |
7222 | ||
5829aea2 GP |
7223 | - (void) stopLoading { |
7224 | } | |
807ae6d7 | 7225 | |
5829aea2 GP |
7226 | @end |
7227 | /* }}} */ | |
807ae6d7 | 7228 | |
5829aea2 | 7229 | /* Section Controller {{{ */ |
f50860ee | 7230 | @interface SectionController : FilteredPackageListController { |
55066b9e | 7231 | _H<NSString> key_; |
123b380c | 7232 | _H<NSString> section_; |
5829aea2 | 7233 | } |
807ae6d7 | 7234 | |
55066b9e | 7235 | - (id) initWithDatabase:(Database *)database source:(Source *)source section:(NSString *)section; |
807ae6d7 | 7236 | |
5829aea2 | 7237 | @end |
bc11cf5b | 7238 | |
5829aea2 | 7239 | @implementation SectionController |
bc11cf5b | 7240 | |
f050e4d9 | 7241 | - (NSURL *) referrerURL { |
55066b9e JF |
7242 | NSString *name(section_); |
7243 | name = name ?: @"*"; | |
7244 | NSString *key(key_); | |
7245 | key = key ?: @"*"; | |
7246 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/sections/%@/%@", UI_, [key stringByAddingPercentEscapesIncludingReserved], [name stringByAddingPercentEscapesIncludingReserved]]]; | |
f050e4d9 JF |
7247 | } |
7248 | ||
fe8e721f | 7249 | - (NSURL *) navigationURL { |
55066b9e JF |
7250 | NSString *name(section_); |
7251 | name = name ?: @"*"; | |
7252 | NSString *key(key_); | |
7253 | key = key ?: @"*"; | |
7254 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sections/%@/%@", [key stringByAddingPercentEscapesIncludingReserved], [name stringByAddingPercentEscapesIncludingReserved]]]; | |
fe8e721f GP |
7255 | } |
7256 | ||
55066b9e | 7257 | - (id) initWithDatabase:(Database *)database source:(Source *)source section:(NSString *)section { |
5829aea2 | 7258 | NSString *title; |
55066b9e | 7259 | if (section == nil) |
5829aea2 | 7260 | title = UCLocalize("ALL_PACKAGES"); |
55066b9e JF |
7261 | else if (![section isEqual:@""]) |
7262 | title = [[NSBundle mainBundle] localizedStringForKey:Simplify(section) value:nil table:@"Sections"]; | |
3707eb56 | 7263 | else |
5829aea2 | 7264 | title = UCLocalize("NO_SECTION"); |
fe8e721f | 7265 | |
89bdef78 | 7266 | if ((self = [super initWithDatabase:database title:title]) != nil) { |
55066b9e JF |
7267 | key_ = [source key]; |
7268 | section_ = section; | |
5829aea2 | 7269 | } return self; |
b5e7eebb GP |
7270 | } |
7271 | ||
55066b9e | 7272 | - (void) reloadData { |
89bdef78 JF |
7273 | Source *source([database_ sourceWithKey:key_]); |
7274 | _H<NSString> name(section_); | |
7275 | ||
7276 | [self setFilter:[=](Package *package) { | |
7277 | NSString *section([package section]); | |
7278 | ||
7279 | return ( | |
7280 | name == nil || | |
7281 | section == nil && [name length] == 0 || | |
7282 | [name isEqualToString:section] | |
7283 | ) && ( | |
7284 | source == nil || | |
7285 | [package source] == source | |
7286 | ) && [package visible]; | |
7287 | }]; | |
7288 | ||
55066b9e JF |
7289 | [super reloadData]; |
7290 | } | |
7291 | ||
b5e7eebb GP |
7292 | @end |
7293 | /* }}} */ | |
5829aea2 | 7294 | /* Sections Controller {{{ */ |
cd79e8cf | 7295 | @interface SectionsController : CyteViewController < |
5829aea2 GP |
7296 | UITableViewDataSource, |
7297 | UITableViewDelegate | |
7585ce66 | 7298 | > { |
3931b718 | 7299 | _transient Database *database_; |
55066b9e | 7300 | _H<NSString> key_; |
7b33d201 JF |
7301 | _H<NSMutableArray> sections_; |
7302 | _H<NSMutableArray> filtered_; | |
bf7c998c | 7303 | _H<UITableView, 2> list_; |
b5e7eebb GP |
7304 | } |
7305 | ||
55066b9e | 7306 | - (id) initWithDatabase:(Database *)database source:(Source *)source; |
5829aea2 | 7307 | - (void) editButtonClicked; |
7585ce66 | 7308 | |
bc11cf5b | 7309 | @end |
b5e7eebb | 7310 | |
5829aea2 | 7311 | @implementation SectionsController |
b5e7eebb | 7312 | |
fe8e721f | 7313 | - (NSURL *) navigationURL { |
55066b9e JF |
7314 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sources/%@", [key_ stringByAddingPercentEscapesIncludingReserved]]]; |
7315 | } | |
7316 | ||
7317 | - (Source *) source { | |
7318 | if (key_ == nil) | |
7319 | return nil; | |
7320 | return [database_ sourceWithKey:key_]; | |
fe8e721f GP |
7321 | } |
7322 | ||
a784d0a4 | 7323 | - (void) updateNavigationItem { |
8e5b801a | 7324 | [[self navigationItem] setTitle:[self isEditing] ? UCLocalize("SECTION_VISIBILITY") : UCLocalize("SECTIONS")]; |
a784d0a4 GP |
7325 | if ([sections_ count] == 0) { |
7326 | [[self navigationItem] setRightBarButtonItem:nil]; | |
7327 | } else { | |
7328 | [[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] | |
8e5b801a | 7329 | initWithBarButtonSystemItem:([self isEditing] ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit) |
a784d0a4 GP |
7330 | target:self |
7331 | action:@selector(editButtonClicked) | |
7332 | ] animated:([[self navigationItem] rightBarButtonItem] != nil)]; | |
7333 | } | |
7334 | } | |
7335 | ||
8e5b801a GP |
7336 | - (void) setEditing:(BOOL)editing animated:(BOOL)animated { |
7337 | [super setEditing:editing animated:animated]; | |
35f0a3b5 | 7338 | |
8e5b801a | 7339 | if (editing) |
5829aea2 GP |
7340 | [list_ reloadData]; |
7341 | else | |
7342 | [delegate_ updateData]; | |
7585ce66 | 7343 | |
a784d0a4 | 7344 | [self updateNavigationItem]; |
b5e7eebb GP |
7345 | } |
7346 | ||
5829aea2 GP |
7347 | - (void) viewDidAppear:(BOOL)animated { |
7348 | [super viewDidAppear:animated]; | |
7349 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7585ce66 JF |
7350 | } |
7351 | ||
5829aea2 GP |
7352 | - (void) viewWillDisappear:(BOOL)animated { |
7353 | [super viewWillDisappear:animated]; | |
31eedaae | 7354 | [self setEditing:NO]; |
7585ce66 JF |
7355 | } |
7356 | ||
5829aea2 | 7357 | - (Section *) sectionAtIndexPath:(NSIndexPath *)indexPath { |
b3551da8 | 7358 | Section *section = nil; |
8e5b801a | 7359 | int index = [indexPath row]; |
b3551da8 | 7360 | if (![self isEditing]) { |
666b48ad | 7361 | index -= 1; |
b3551da8 GP |
7362 | if (index >= 0) |
7363 | section = [filtered_ objectAtIndex:index]; | |
7364 | } else { | |
7365 | section = [sections_ objectAtIndex:index]; | |
7366 | } | |
5829aea2 GP |
7367 | return section; |
7368 | } | |
7585ce66 | 7369 | |
5829aea2 | 7370 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
8e5b801a GP |
7371 | if ([self isEditing]) |
7372 | return [sections_ count]; | |
7373 | else | |
7374 | return [filtered_ count] + 1; | |
7585ce66 JF |
7375 | } |
7376 | ||
5829aea2 GP |
7377 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
7378 | return 45.0f; | |
7379 | }*/ | |
7585ce66 | 7380 | |
5829aea2 GP |
7381 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
7382 | static NSString *reuseIdentifier = @"SectionCell"; | |
7585ce66 | 7383 | |
35f0a3b5 | 7384 | SectionCell *cell = (SectionCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5829aea2 GP |
7385 | if (cell == nil) |
7386 | cell = [[[SectionCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
7585ce66 | 7387 | |
8e5b801a | 7388 | [cell setSection:[self sectionAtIndexPath:indexPath] editing:[self isEditing]]; |
54043703 | 7389 | |
5829aea2 | 7390 | return cell; |
54043703 JF |
7391 | } |
7392 | ||
5829aea2 | 7393 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
8e5b801a | 7394 | if ([self isEditing]) |
54043703 | 7395 | return; |
7585ce66 | 7396 | |
5829aea2 | 7397 | Section *section = [self sectionAtIndexPath:indexPath]; |
7585ce66 | 7398 | |
5829aea2 GP |
7399 | SectionController *controller = [[[SectionController alloc] |
7400 | initWithDatabase:database_ | |
55066b9e | 7401 | source:[self source] |
5829aea2 GP |
7402 | section:[section name] |
7403 | ] autorelease]; | |
7404 | [controller setDelegate:delegate_]; | |
7585ce66 | 7405 | |
5829aea2 | 7406 | [[self navigationController] pushViewController:controller animated:YES]; |
7585ce66 JF |
7407 | } |
7408 | ||
fe8e721f | 7409 | - (void) loadView { |
e8cbebe4 | 7410 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; |
fe8e721f | 7411 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
86a333c6 | 7412 | [list_ setRowHeight:46]; |
7b33d201 | 7413 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 7414 | [list_ setDelegate:self]; |
e8cbebe4 | 7415 | [self setView:list_]; |
fe8e721f GP |
7416 | } |
7417 | ||
7418 | - (void) viewDidLoad { | |
7d887d0b JF |
7419 | [super viewDidLoad]; |
7420 | ||
fe8e721f GP |
7421 | [[self navigationItem] setTitle:UCLocalize("SECTIONS")]; |
7422 | } | |
7423 | ||
7424 | - (void) releaseSubviews { | |
fe8e721f | 7425 | list_ = nil; |
7be3eb32 | 7426 | |
4f9acb7c JF |
7427 | sections_ = nil; |
7428 | filtered_ = nil; | |
7429 | ||
7be3eb32 | 7430 | [super releaseSubviews]; |
fe8e721f GP |
7431 | } |
7432 | ||
55066b9e | 7433 | - (id) initWithDatabase:(Database *)database source:(Source *)source { |
5829aea2 GP |
7434 | if ((self = [super init]) != nil) { |
7435 | database_ = database; | |
55066b9e | 7436 | key_ = [source key]; |
5829aea2 | 7437 | } return self; |
7585ce66 JF |
7438 | } |
7439 | ||
5829aea2 | 7440 | - (void) reloadData { |
fe8e721f GP |
7441 | [super reloadData]; |
7442 | ||
5829aea2 | 7443 | NSArray *packages = [database_ packages]; |
7585ce66 | 7444 | |
4f9acb7c JF |
7445 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
7446 | filtered_ = [NSMutableArray arrayWithCapacity:16]; | |
7585ce66 | 7447 | |
5829aea2 | 7448 | NSMutableDictionary *sections([NSMutableDictionary dictionaryWithCapacity:32]); |
7585ce66 | 7449 | |
55066b9e JF |
7450 | Source *source([self source]); |
7451 | ||
5829aea2 GP |
7452 | _trace(); |
7453 | for (Package *package in packages) { | |
55066b9e JF |
7454 | if (source != nil && [package source] != source) |
7455 | continue; | |
7456 | ||
5829aea2 GP |
7457 | NSString *name([package section]); |
7458 | NSString *key(name == nil ? @"" : name); | |
7585ce66 | 7459 | |
5829aea2 | 7460 | Section *section; |
7585ce66 | 7461 | |
5829aea2 GP |
7462 | _profile(SectionsView$reloadData$Section) |
7463 | section = [sections objectForKey:key]; | |
7464 | if (section == nil) { | |
7465 | _profile(SectionsView$reloadData$Section$Allocate) | |
729bc910 | 7466 | section = [[[Section alloc] initWithName:key localize:YES] autorelease]; |
5829aea2 GP |
7467 | [sections setObject:section forKey:key]; |
7468 | _end | |
7469 | } | |
7470 | _end | |
7585ce66 | 7471 | |
5829aea2 | 7472 | [section addToCount]; |
7585ce66 | 7473 | |
5829aea2 GP |
7474 | _profile(SectionsView$reloadData$Filter) |
7475 | if (![package valid] || ![package visible]) | |
7476 | continue; | |
7477 | _end | |
7585ce66 | 7478 | |
5829aea2 GP |
7479 | [section addToRow]; |
7480 | } | |
7481 | _trace(); | |
7585ce66 | 7482 | |
5829aea2 | 7483 | [sections_ addObjectsFromArray:[sections allValues]]; |
7585ce66 | 7484 | |
5829aea2 | 7485 | [sections_ sortUsingSelector:@selector(compareByLocalized:)]; |
7585ce66 | 7486 | |
7b33d201 | 7487 | for (Section *section in (id) sections_) { |
5829aea2 GP |
7488 | size_t count([section row]); |
7489 | if (count == 0) | |
7490 | continue; | |
7585ce66 | 7491 | |
5829aea2 GP |
7492 | section = [[[Section alloc] initWithName:[section name] localized:[section localized]] autorelease]; |
7493 | [section setCount:count]; | |
7494 | [filtered_ addObject:section]; | |
7495 | } | |
7585ce66 | 7496 | |
a784d0a4 | 7497 | [self updateNavigationItem]; |
5829aea2 GP |
7498 | [list_ reloadData]; |
7499 | _trace(); | |
7500 | } | |
7585ce66 | 7501 | |
6840bff3 | 7502 | - (void) editButtonClicked { |
8e5b801a | 7503 | [self setEditing:![self isEditing] animated:YES]; |
5829aea2 | 7504 | } |
7585ce66 | 7505 | |
5829aea2 GP |
7506 | @end |
7507 | /* }}} */ | |
7585ce66 | 7508 | |
5829aea2 | 7509 | /* Changes Controller {{{ */ |
e5491e28 | 7510 | @interface ChangesController : FilteredPackageListController { |
5829aea2 | 7511 | unsigned upgrades_; |
5829aea2 | 7512 | } |
7585ce66 | 7513 | |
ea3bb538 | 7514 | - (id) initWithDatabase:(Database *)database; |
7585ce66 | 7515 | |
5829aea2 | 7516 | @end |
7585ce66 | 7517 | |
5829aea2 | 7518 | @implementation ChangesController |
7585ce66 | 7519 | |
e5491e28 JF |
7520 | - (NSURL *) referrerURL { |
7521 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/changes/", UI_]]; | |
b5e7eebb GP |
7522 | } |
7523 | ||
e5491e28 JF |
7524 | - (NSURL *) navigationURL { |
7525 | return [NSURL URLWithString:@"cydia://changes"]; | |
5829aea2 | 7526 | } |
b5e7eebb | 7527 | |
5829aea2 GP |
7528 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
7529 | @synchronized (database_) { | |
7530 | if ([database_ era] != era_) | |
7531 | return nil; | |
b5e7eebb | 7532 | |
5829aea2 GP |
7533 | NSUInteger sectionIndex([path section]); |
7534 | if (sectionIndex >= [sections_ count]) | |
7535 | return nil; | |
7536 | Section *section([sections_ objectAtIndex:sectionIndex]); | |
7537 | NSInteger row([path row]); | |
56bf1e78 | 7538 | return [[[packages_ objectAtIndex:([section row] + row)] retain] autorelease]; |
5829aea2 | 7539 | } } |
b5e7eebb | 7540 | |
0e15b67c JF |
7541 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
7542 | NSString *context([alert context]); | |
7543 | ||
7544 | if ([context isEqualToString:@"norefresh"]) | |
7545 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
7546 | } | |
7547 | ||
e67ebdad JF |
7548 | - (void) setLeftBarButtonItem { |
7549 | if ([delegate_ updating]) | |
7550 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7551 | initWithTitle:UCLocalize("CANCEL") | |
7552 | style:UIBarButtonItemStyleDone | |
7553 | target:self | |
7554 | action:@selector(cancelButtonClicked) | |
7555 | ] autorelease] animated:YES]; | |
7556 | else | |
7557 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7558 | initWithTitle:UCLocalize("REFRESH") | |
7559 | style:UIBarButtonItemStylePlain | |
7560 | target:self | |
7561 | action:@selector(refreshButtonClicked) | |
7562 | ] autorelease] animated:YES]; | |
7563 | } | |
7564 | ||
5829aea2 | 7565 | - (void) refreshButtonClicked { |
e67ebdad JF |
7566 | if ([delegate_ requestUpdate]) |
7567 | [self setLeftBarButtonItem]; | |
7568 | } | |
0e15b67c | 7569 | |
e67ebdad JF |
7570 | - (void) cancelButtonClicked { |
7571 | [delegate_ cancelUpdate]; | |
b5e7eebb GP |
7572 | } |
7573 | ||
5829aea2 GP |
7574 | - (void) upgradeButtonClicked { |
7575 | [delegate_ distUpgrade]; | |
cb6b3a7b | 7576 | [[self navigationItem] setRightBarButtonItem:nil animated:YES]; |
b5e7eebb GP |
7577 | } |
7578 | ||
e5491e28 JF |
7579 | - (bool) shouldYield { |
7580 | return true; | |
fe8e721f GP |
7581 | } |
7582 | ||
e5491e28 JF |
7583 | - (bool) shouldBlock { |
7584 | return true; | |
fe8e721f GP |
7585 | } |
7586 | ||
e5491e28 JF |
7587 | - (void) useFilter { |
7588 | @synchronized (self) { | |
7589 | [self setFilter:[](Package *package) { | |
7590 | return [package upgradableAndEssential:YES] || [package visible]; | |
7591 | }]; | |
4f9acb7c | 7592 | |
e5491e28 JF |
7593 | [self setSorter:[](NSMutableArray *packages) { |
7594 | [packages radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackageChangesRadix) withContext:NULL]; | |
7595 | }]; | |
7596 | } } | |
5829aea2 | 7597 | |
ea3bb538 | 7598 | - (id) initWithDatabase:(Database *)database { |
e5491e28 JF |
7599 | if ((self = [super initWithDatabase:database title:UCLocalize("CHANGES")]) != nil) { |
7600 | [self useFilter]; | |
5829aea2 | 7601 | } return self; |
807ae6d7 JF |
7602 | } |
7603 | ||
946c4377 JF |
7604 | - (void) viewDidLoad { |
7605 | [super viewDidLoad]; | |
7606 | [self setLeftBarButtonItem]; | |
7607 | } | |
7608 | ||
7609 | - (void) viewWillAppear:(BOOL)animated { | |
7610 | [super viewWillAppear:animated]; | |
7611 | [self setLeftBarButtonItem]; | |
7612 | } | |
7613 | ||
e5491e28 | 7614 | - (void) reloadData { |
e67ebdad | 7615 | [self setLeftBarButtonItem]; |
e5491e28 JF |
7616 | [super reloadData]; |
7617 | } | |
e67ebdad | 7618 | |
e5491e28 JF |
7619 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { |
7620 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); | |
bdca103d | 7621 | |
5829aea2 GP |
7622 | Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; |
7623 | Section *ignored = nil; | |
7624 | Section *section = nil; | |
7625 | time_t last = 0; | |
807ae6d7 | 7626 | |
5829aea2 GP |
7627 | upgrades_ = 0; |
7628 | bool unseens = false; | |
807ae6d7 | 7629 | |
5829aea2 | 7630 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); |
807ae6d7 | 7631 | |
e5491e28 JF |
7632 | for (size_t offset = 0, count = [packages count]; offset != count; ++offset) { |
7633 | Package *package = [packages objectAtIndex:offset]; | |
807ae6d7 | 7634 | |
5829aea2 | 7635 | BOOL uae = [package upgradableAndEssential:YES]; |
807ae6d7 | 7636 | |
5829aea2 GP |
7637 | if (!uae) { |
7638 | unseens = true; | |
7639 | time_t seen([package seen]); | |
d90a4cd6 | 7640 | |
5829aea2 GP |
7641 | if (section == nil || last != seen) { |
7642 | last = seen; | |
7643 | ||
7644 | NSString *name; | |
7645 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]); | |
7646 | [name autorelease]; | |
7647 | ||
7648 | _profile(ChangesController$reloadData$Allocate) | |
7649 | name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; | |
7650 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
e5491e28 | 7651 | [sections addObject:section]; |
5829aea2 GP |
7652 | _end |
7653 | } | |
7654 | ||
7655 | [section addToCount]; | |
7656 | } else if ([package ignored]) { | |
7657 | if (ignored == nil) { | |
7658 | ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease]; | |
7659 | } | |
7660 | [ignored addToCount]; | |
7661 | } else { | |
7662 | ++upgrades_; | |
7663 | [upgradable addToCount]; | |
7664 | } | |
7665 | } | |
7666 | _trace(); | |
7667 | ||
7668 | CFRelease(formatter); | |
7669 | ||
7670 | if (unseens) { | |
e5491e28 | 7671 | Section *last = [sections lastObject]; |
5829aea2 | 7672 | size_t count = [last count]; |
e5491e28 JF |
7673 | [packages removeObjectsInRange:NSMakeRange([packages count] - count, count)]; |
7674 | [sections removeLastObject]; | |
5829aea2 GP |
7675 | } |
7676 | ||
7677 | if ([ignored count] != 0) | |
e5491e28 | 7678 | [sections insertObject:ignored atIndex:0]; |
5829aea2 | 7679 | if (upgrades_ != 0) |
e5491e28 | 7680 | [sections insertObject:upgradable atIndex:0]; |
5829aea2 GP |
7681 | |
7682 | [list_ reloadData]; | |
7683 | ||
cb6b3a7b JF |
7684 | [[self navigationItem] setRightBarButtonItem:(upgrades_ == 0 ? nil : [[[UIBarButtonItem alloc] |
7685 | initWithTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("UPGRADE"), [NSString stringWithFormat:@"%u", upgrades_]] | |
7686 | style:UIBarButtonItemStylePlain | |
7687 | target:self | |
7688 | action:@selector(upgradeButtonClicked) | |
7689 | ] autorelease]) animated:YES]; | |
5829aea2 | 7690 | |
e5491e28 | 7691 | return sections; |
a70cd4ba JF |
7692 | } |
7693 | ||
5829aea2 GP |
7694 | @end |
7695 | /* }}} */ | |
7696 | /* Search Controller {{{ */ | |
f50860ee | 7697 | @interface SearchController : FilteredPackageListController < |
5829aea2 GP |
7698 | UISearchBarDelegate |
7699 | > { | |
bf7c998c | 7700 | _H<UISearchBar, 1> search_; |
fe8e721f | 7701 | BOOL searchloaded_; |
89bdef78 | 7702 | bool summary_; |
5829aea2 GP |
7703 | } |
7704 | ||
43625891 | 7705 | - (id) initWithDatabase:(Database *)database query:(NSString *)query; |
5829aea2 | 7706 | - (void) reloadData; |
d90a4cd6 GP |
7707 | |
7708 | @end | |
7709 | ||
5829aea2 | 7710 | @implementation SearchController |
d90a4cd6 | 7711 | |
f050e4d9 JF |
7712 | - (NSURL *) referrerURL { |
7713 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/search?q=%@", UI_, [([search_ text] ?: @"") stringByAddingPercentEscapesIncludingReserved]]]; | |
7714 | } | |
7715 | ||
fe8e721f | 7716 | - (NSURL *) navigationURL { |
35f0a3b5 GP |
7717 | if ([search_ text] == nil || [[search_ text] isEqualToString:@""]) |
7718 | return [NSURL URLWithString:@"cydia://search"]; | |
7719 | else | |
b90c7892 | 7720 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://search/%@", [[search_ text] stringByAddingPercentEscapesIncludingReserved]]]; |
fe8e721f GP |
7721 | } |
7722 | ||
096e25d8 | 7723 | - (NSArray *) termsForQuery:(NSString *)query { |
945812b9 JF |
7724 | NSMutableArray *terms([NSMutableArray arrayWithCapacity:2]); |
7725 | for (NSString *component in [query componentsSeparatedByString:@" "]) | |
7726 | if ([component length] != 0) | |
7727 | [terms addObject:component]; | |
7728 | ||
7729 | return terms; | |
096e25d8 JF |
7730 | } |
7731 | ||
59f3d290 | 7732 | - (void) useSearch { |
89bdef78 JF |
7733 | _H<NSArray> query([self termsForQuery:[search_ text]]); |
7734 | summary_ = false; | |
7735 | ||
7736 | @synchronized (self) { | |
7737 | [self setFilter:[=](Package *package) { | |
7738 | if (![package unfiltered]) | |
7739 | return false; | |
7740 | if (![package matches:query]) | |
7741 | return false; | |
7742 | return true; | |
7743 | }]; | |
7744 | ||
7745 | [self setSorter:[](NSMutableArray *packages) { | |
7746 | [packages radixSortUsingSelector:@selector(rank)]; | |
7747 | }]; | |
7748 | } | |
7749 | ||
59f3d290 JF |
7750 | [self clearData]; |
7751 | [self reloadData]; | |
7752 | } | |
7753 | ||
89bdef78 JF |
7754 | - (void) usePrefix:(NSString *)prefix { |
7755 | _H<NSString> query(prefix); | |
7756 | summary_ = true; | |
7757 | ||
7758 | @synchronized (self) { | |
7759 | [self setFilter:[=](Package *package) { | |
7760 | if ([query length] == 0) | |
7761 | return false; | |
7762 | if (![package unfiltered]) | |
7763 | return false; | |
7764 | if ([[package name] compare:query options:MatchCompareOptions_ range:NSMakeRange(0, [query length])] != NSOrderedSame) | |
7765 | return false; | |
7766 | return true; | |
7767 | }]; | |
7768 | ||
7769 | [self setSorter:nullptr]; | |
7770 | } | |
7771 | ||
7772 | [self reloadData]; | |
7773 | } | |
7774 | ||
3025d5b4 | 7775 | - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar { |
59f3d290 | 7776 | [self clearData]; |
89bdef78 | 7777 | [self usePrefix:[search_ text]]; |
3025d5b4 JF |
7778 | } |
7779 | ||
7780 | - (void) searchBarButtonClicked:(UISearchBar *)searchBar { | |
5829aea2 | 7781 | [search_ resignFirstResponder]; |
59f3d290 | 7782 | [self useSearch]; |
5829aea2 GP |
7783 | } |
7784 | ||
3025d5b4 JF |
7785 | - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar { |
7786 | [search_ setText:@""]; | |
7787 | [self searchBarButtonClicked:searchBar]; | |
7788 | } | |
7789 | ||
7790 | - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { | |
7791 | [self searchBarButtonClicked:searchBar]; | |
7792 | } | |
7793 | ||
5829aea2 | 7794 | - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { |
89bdef78 | 7795 | [self usePrefix:text]; |
5829aea2 GP |
7796 | } |
7797 | ||
3025d5b4 | 7798 | - (bool) shouldYield { |
56bf1e78 JF |
7799 | return YES; |
7800 | } | |
7801 | ||
7802 | - (bool) shouldBlock { | |
89bdef78 | 7803 | return !summary_; |
3025d5b4 JF |
7804 | } |
7805 | ||
59f3d290 | 7806 | - (bool) isSummarized { |
89bdef78 | 7807 | return summary_; |
59f3d290 JF |
7808 | } |
7809 | ||
9c5737d5 JF |
7810 | - (bool) showsSections { |
7811 | return false; | |
7812 | } | |
7813 | ||
43625891 | 7814 | - (id) initWithDatabase:(Database *)database query:(NSString *)query { |
89bdef78 | 7815 | if ((self = [super initWithDatabase:database title:UCLocalize("SEARCH")])) { |
900095fd | 7816 | search_ = [[[UISearchBar alloc] init] autorelease]; |
830efb5d | 7817 | [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; |
900095fd | 7818 | [search_ setDelegate:self]; |
43625891 | 7819 | |
830efb5d JF |
7820 | UITextField *textField; |
7821 | if ([search_ respondsToSelector:@selector(searchField)]) | |
7822 | textField = [search_ searchField]; | |
7823 | else | |
7824 | textField = MSHookIvar<UITextField *>(search_, "_searchField"); | |
7825 | ||
7826 | [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
7827 | [textField setEnablesReturnKeyAutomatically:NO]; | |
7828 | [[self navigationItem] setTitleView:textField]; | |
7829 | ||
43625891 JF |
7830 | if (query != nil) |
7831 | [search_ setText:query]; | |
89bdef78 | 7832 | [self useSearch]; |
fe8e721f | 7833 | } return self; |
5829aea2 GP |
7834 | } |
7835 | ||
6840bff3 | 7836 | - (void) viewDidAppear:(BOOL)animated { |
5829aea2 | 7837 | [super viewDidAppear:animated]; |
fe8e721f GP |
7838 | |
7839 | if (!searchloaded_) { | |
7840 | searchloaded_ = YES; | |
7841 | [search_ setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; | |
5829aea2 | 7842 | [search_ layoutSubviews]; |
d90a4cd6 | 7843 | } |
058813ae GP |
7844 | |
7845 | if ([self isSummarized]) | |
7846 | [search_ becomeFirstResponder]; | |
5829aea2 | 7847 | } |
d90a4cd6 | 7848 | |
d90a4cd6 | 7849 | - (void) reloadData { |
f50860ee | 7850 | [self resetCursor]; |
9dac415b | 7851 | [super reloadData]; |
d90a4cd6 GP |
7852 | } |
7853 | ||
5829aea2 GP |
7854 | - (void) didSelectPackage:(Package *)package { |
7855 | [search_ resignFirstResponder]; | |
7856 | [super didSelectPackage:package]; | |
d90a4cd6 GP |
7857 | } |
7858 | ||
7859 | @end | |
7860 | /* }}} */ | |
5829aea2 | 7861 | /* Package Settings Controller {{{ */ |
cd79e8cf | 7862 | @interface PackageSettingsController : CyteViewController < |
c21004b9 JF |
7863 | UITableViewDataSource, |
7864 | UITableViewDelegate | |
7865 | > { | |
807ae6d7 | 7866 | _transient Database *database_; |
7b33d201 JF |
7867 | _H<NSString> name_; |
7868 | _H<Package> package_; | |
bf7c998c | 7869 | _H<UITableView, 2> table_; |
7b33d201 JF |
7870 | _H<UISwitch> subscribedSwitch_; |
7871 | _H<UISwitch> ignoredSwitch_; | |
7872 | _H<UITableViewCell> subscribedCell_; | |
7873 | _H<UITableViewCell> ignoredCell_; | |
807ae6d7 JF |
7874 | } |
7875 | ||
5829aea2 | 7876 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
c21004b9 | 7877 | |
807ae6d7 JF |
7878 | @end |
7879 | ||
5829aea2 | 7880 | @implementation PackageSettingsController |
807ae6d7 | 7881 | |
fe8e721f | 7882 | - (NSURL *) navigationURL { |
8861e953 | 7883 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/settings", (id) name_]]; |
fe8e721f GP |
7884 | } |
7885 | ||
5829aea2 GP |
7886 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7887 | if (package_ == nil) | |
7888 | return 0; | |
e954c30a | 7889 | |
2136717a DH |
7890 | if ([package_ installed] == nil) |
7891 | return 1; | |
7892 | else | |
7893 | return 2; | |
e954c30a GP |
7894 | } |
7895 | ||
5829aea2 GP |
7896 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7897 | if (package_ == nil) | |
7898 | return 0; | |
7899 | ||
2136717a DH |
7900 | // both sections contain just one item right now. |
7901 | return 1; | |
b5e7eebb GP |
7902 | } |
7903 | ||
5829aea2 | 7904 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
2136717a | 7905 | return nil; |
e954c30a GP |
7906 | } |
7907 | ||
5829aea2 | 7908 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
2136717a DH |
7909 | if (section == 0) |
7910 | return UCLocalize("SHOW_ALL_CHANGES_EX"); | |
7911 | else | |
7912 | return UCLocalize("IGNORE_UPGRADES_EX"); | |
807ae6d7 JF |
7913 | } |
7914 | ||
5829aea2 GP |
7915 | - (void) onSubscribed:(id)control { |
7916 | bool value([control isOn]); | |
7917 | if (package_ == nil) | |
7918 | return; | |
7919 | if ([package_ setSubscribed:value]) | |
7920 | [delegate_ updateData]; | |
807ae6d7 JF |
7921 | } |
7922 | ||
e5e70358 JF |
7923 | - (void) _updateIgnored { |
7924 | const char *package([name_ UTF8String]); | |
7925 | bool on([ignoredSwitch_ isOn]); | |
7926 | ||
7927 | pid_t pid(ExecFork()); | |
7928 | if (pid == 0) { | |
232b396b | 7929 | FILE *dpkg(popen("/usr/libexec/cydo --set-selections", "w")); |
e5e70358 JF |
7930 | fwrite(package, strlen(package), 1, dpkg); |
7931 | ||
7932 | if (on) | |
7933 | fwrite(" hold\n", 6, 1, dpkg); | |
7934 | else | |
7935 | fwrite(" install\n", 9, 1, dpkg); | |
7936 | ||
7937 | pclose(dpkg); | |
7938 | ||
7939 | exit(0); | |
51c8106a | 7940 | } ReapZombie(pid); |
e5e70358 JF |
7941 | } |
7942 | ||
5829aea2 | 7943 | - (void) onIgnored:(id)control { |
e5e70358 JF |
7944 | NSInvocation *invocation([NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(_updateIgnored)]]); |
7945 | [invocation setTarget:self]; | |
7946 | [invocation setSelector:@selector(_updateIgnored)]; | |
7947 | ||
7948 | [delegate_ reloadDataWithInvocation:invocation]; | |
5829aea2 | 7949 | } |
807ae6d7 | 7950 | |
46aa9775 | 7951 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
7952 | if (package_ == nil) |
7953 | return nil; | |
b5e7eebb | 7954 | |
2136717a | 7955 | switch ([indexPath section]) { |
5829aea2 GP |
7956 | case 0: return subscribedCell_; |
7957 | case 1: return ignoredCell_; | |
36fbb2aa | 7958 | |
5829aea2 GP |
7959 | _nodefault |
7960 | } | |
807ae6d7 | 7961 | |
5829aea2 | 7962 | return nil; |
807ae6d7 JF |
7963 | } |
7964 | ||
fe8e721f | 7965 | - (void) loadView { |
39470a3a JF |
7966 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
7967 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
7968 | [self setView:view]; | |
807ae6d7 | 7969 | |
7b33d201 | 7970 | table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease]; |
fe8e721f | 7971 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b33d201 | 7972 | [(UITableView *) table_ setDataSource:self]; |
fe8e721f | 7973 | [table_ setDelegate:self]; |
39470a3a | 7974 | [view addSubview:table_]; |
807ae6d7 | 7975 | |
7b33d201 | 7976 | subscribedSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7977 | [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7978 | [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7979 | |
7b33d201 | 7980 | ignoredSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7981 | [ignoredSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7982 | [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7983 | |
7b33d201 | 7984 | subscribedCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7985 | [subscribedCell_ setText:UCLocalize("SHOW_ALL_CHANGES")]; |
7986 | [subscribedCell_ setAccessoryView:subscribedSwitch_]; | |
7987 | [subscribedCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
5829aea2 | 7988 | |
7b33d201 | 7989 | ignoredCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7990 | [ignoredCell_ setText:UCLocalize("IGNORE_UPGRADES")]; |
7991 | [ignoredCell_ setAccessoryView:ignoredSwitch_]; | |
7992 | [ignoredCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
fe8e721f | 7993 | } |
5829aea2 | 7994 | |
fe8e721f | 7995 | - (void) viewDidLoad { |
7d887d0b JF |
7996 | [super viewDidLoad]; |
7997 | ||
fe8e721f GP |
7998 | [[self navigationItem] setTitle:UCLocalize("SETTINGS")]; |
7999 | } | |
5829aea2 | 8000 | |
fe8e721f | 8001 | - (void) releaseSubviews { |
fe8e721f | 8002 | ignoredCell_ = nil; |
fe8e721f | 8003 | subscribedCell_ = nil; |
fe8e721f | 8004 | table_ = nil; |
fe8e721f | 8005 | ignoredSwitch_ = nil; |
fe8e721f | 8006 | subscribedSwitch_ = nil; |
7be3eb32 JF |
8007 | |
8008 | [super releaseSubviews]; | |
fe8e721f GP |
8009 | } |
8010 | ||
8011 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { | |
6840bff3 | 8012 | if ((self = [super init]) != nil) { |
fe8e721f | 8013 | database_ = database; |
7b33d201 | 8014 | name_ = package; |
807ae6d7 JF |
8015 | } return self; |
8016 | } | |
8017 | ||
8018 | - (void) reloadData { | |
fe8e721f GP |
8019 | [super reloadData]; |
8020 | ||
5829aea2 | 8021 | package_ = [database_ packageWithName:name_]; |
f3e2c0ac | 8022 | |
5829aea2 | 8023 | if (package_ != nil) { |
5829aea2 GP |
8024 | [subscribedSwitch_ setOn:([package_ subscribed] ? 1 : 0) animated:NO]; |
8025 | [ignoredSwitch_ setOn:([package_ ignored] ? 1 : 0) animated:NO]; | |
f3e2c0ac | 8026 | } // XXX: what now, G? |
dc5812ec | 8027 | |
5829aea2 GP |
8028 | [table_ reloadData]; |
8029 | } | |
6d9712c4 | 8030 | |
dc5812ec | 8031 | @end |
2367a917 | 8032 | /* }}} */ |
d90a4cd6 | 8033 | |
5829aea2 | 8034 | /* Installed Controller {{{ */ |
f50860ee | 8035 | @interface InstalledController : FilteredPackageListController { |
821b1a0c | 8036 | bool sectioned_; |
dc5812ec JF |
8037 | } |
8038 | ||
5829aea2 | 8039 | - (id) initWithDatabase:(Database *)database; |
5829aea2 | 8040 | - (void) queueStatusDidChange; |
dc5812ec | 8041 | |
dc5812ec JF |
8042 | @end |
8043 | ||
5829aea2 | 8044 | @implementation InstalledController |
b4d89997 | 8045 | |
f050e4d9 JF |
8046 | - (NSURL *) referrerURL { |
8047 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/installed/", UI_]]; | |
8048 | } | |
8049 | ||
fe8e721f GP |
8050 | - (NSURL *) navigationURL { |
8051 | return [NSURL URLWithString:@"cydia://installed"]; | |
8052 | } | |
b5e7eebb | 8053 | |
b3906a21 | 8054 | - (void) useRecent { |
821b1a0c JF |
8055 | sectioned_ = false; |
8056 | ||
8057 | @synchronized (self) { | |
8058 | [self setFilter:[](Package *package) { | |
8059 | return ![package uninstalled] && package->role_ < 7; | |
8060 | }]; | |
8061 | ||
8062 | [self setSorter:[](NSMutableArray *packages) { | |
b3906a21 | 8063 | [packages radixSortUsingSelector:@selector(recent)]; |
821b1a0c JF |
8064 | }]; |
8065 | } } | |
8066 | ||
89bdef78 | 8067 | - (void) useFilter:(UISegmentedControl *)segmented { |
821b1a0c JF |
8068 | NSInteger selected([segmented selectedSegmentIndex]); |
8069 | if (selected == 2) | |
b3906a21 | 8070 | return [self useRecent]; |
821b1a0c JF |
8071 | bool simple(selected == 0); |
8072 | sectioned_ = true; | |
89bdef78 JF |
8073 | |
8074 | @synchronized (self) { | |
8075 | [self setFilter:[=](Package *package) { | |
8076 | return ![package uninstalled] && package->role_ <= (simple ? 1 : 3); | |
8077 | }]; | |
821b1a0c JF |
8078 | |
8079 | [self setSorter:nullptr]; | |
89bdef78 JF |
8080 | } } |
8081 | ||
98ddcefd JF |
8082 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { |
8083 | if (sectioned_) | |
8084 | return [super sectionsForPackages:packages]; | |
8085 | ||
8086 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterLongStyle, kCFDateFormatterNoStyle)); | |
8087 | ||
8088 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); | |
7108bdd2 | 8089 | Section *section(nil); |
98ddcefd JF |
8090 | time_t last(0); |
8091 | ||
8092 | for (size_t offset(0), count([packages count]); offset != count; ++offset) { | |
8093 | Package *package([packages objectAtIndex:offset]); | |
8094 | ||
b3906a21 | 8095 | time_t upgraded([package upgraded]); |
0cadb352 JF |
8096 | if (upgraded < 1168364520) |
8097 | upgraded = 0; | |
8098 | else | |
8099 | upgraded -= upgraded % (60 * 60 * 24); | |
98ddcefd | 8100 | |
b3906a21 JF |
8101 | if (section == nil || upgraded != last) { |
8102 | last = upgraded; | |
98ddcefd JF |
8103 | |
8104 | NSString *name; | |
0cadb352 JF |
8105 | if (upgraded == 0) |
8106 | continue; // XXX: name = UCLocalize("..."); | |
8107 | else { | |
8108 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:upgraded]); | |
8109 | [name autorelease]; | |
8110 | } | |
98ddcefd JF |
8111 | |
8112 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
8113 | [sections addObject:section]; | |
8114 | } | |
8115 | ||
8116 | [section addToCount]; | |
8117 | } | |
8118 | ||
8119 | CFRelease(formatter); | |
8120 | return sections; | |
8121 | } | |
8122 | ||
5829aea2 | 8123 | - (id) initWithDatabase:(Database *)database { |
89bdef78 | 8124 | if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED")]) != nil) { |
c8fbe1f4 | 8125 | UISegmentedControl *segmented([[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:UCLocalize("USER"), UCLocalize("EXPERT"), UCLocalize("RECENT"), nil]] autorelease]); |
3544397d JF |
8126 | [segmented setSelectedSegmentIndex:0]; |
8127 | [segmented setSegmentedControlStyle:UISegmentedControlStyleBar]; | |
8128 | [[self navigationItem] setTitleView:segmented]; | |
8129 | ||
8130 | [segmented addTarget:self action:@selector(modeChanged:) forEvents:UIControlEventValueChanged]; | |
89bdef78 | 8131 | [self useFilter:segmented]; |
3544397d | 8132 | |
5829aea2 GP |
8133 | [self queueStatusDidChange]; |
8134 | } return self; | |
dc5812ec JF |
8135 | } |
8136 | ||
5829aea2 GP |
8137 | #if !AlwaysReload |
8138 | - (void) queueButtonClicked { | |
8139 | [delegate_ queue]; | |
dc5812ec | 8140 | } |
5829aea2 | 8141 | #endif |
dc5812ec | 8142 | |
5829aea2 GP |
8143 | - (void) queueStatusDidChange { |
8144 | #if !AlwaysReload | |
55066b9e | 8145 | if (Queuing_) { |
7d3660da | 8146 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
55066b9e JF |
8147 | initWithTitle:UCLocalize("QUEUE") |
8148 | style:UIBarButtonItemStyleDone | |
8149 | target:self | |
8150 | action:@selector(queueButtonClicked) | |
8151 | ] autorelease]]; | |
8152 | } else { | |
7d3660da | 8153 | [[self navigationItem] setRightBarButtonItem:nil]; |
5829aea2 GP |
8154 | } |
8155 | #endif | |
dc5812ec JF |
8156 | } |
8157 | ||
3544397d | 8158 | - (void) modeChanged:(UISegmentedControl *)segmented { |
89bdef78 | 8159 | [self useFilter:segmented]; |
f50860ee | 8160 | [self reloadData]; |
9a7b04c5 JF |
8161 | } |
8162 | ||
5829aea2 GP |
8163 | @end |
8164 | /* }}} */ | |
b5e7eebb | 8165 | |
5829aea2 | 8166 | /* Source Cell {{{ */ |
a9311516 | 8167 | @interface SourceCell : CyteTableViewCell < |
21ac0ce2 JF |
8168 | CyteTableViewCellDelegate, |
8169 | SourceDelegate | |
5829aea2 | 8170 | > { |
21ac0ce2 | 8171 | _H<Source, 1> source_; |
02735663 | 8172 | _H<NSURL> url_; |
7b33d201 JF |
8173 | _H<UIImage> icon_; |
8174 | _H<NSString> origin_; | |
8175 | _H<NSString> label_; | |
21ac0ce2 | 8176 | _H<UIActivityIndicatorView> indicator_; |
5829aea2 | 8177 | } |
dc5812ec | 8178 | |
5829aea2 | 8179 | - (void) setSource:(Source *)source; |
21ac0ce2 | 8180 | - (void) setFetch:(NSNumber *)fetch; |
6da1297d | 8181 | |
5829aea2 | 8182 | @end |
dc5812ec | 8183 | |
5829aea2 | 8184 | @implementation SourceCell |
36bb2ca2 | 8185 | |
02735663 JF |
8186 | - (void) _setImage:(NSArray *)data { |
8187 | if ([url_ isEqual:[data objectAtIndex:0]]) { | |
8188 | icon_ = [data objectAtIndex:1]; | |
8189 | [content_ setNeedsDisplay]; | |
8190 | } | |
8252b666 JF |
8191 | } |
8192 | ||
7bd76e97 | 8193 | - (void) _setSource:(NSURL *) url { |
8252b666 JF |
8194 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
8195 | ||
7bd76e97 JF |
8196 | if (NSData *data = [NSURLConnection |
8197 | sendSynchronousRequest:[NSURLRequest | |
8198 | requestWithURL:url | |
de5f2673 JF |
8199 | cachePolicy:NSURLRequestUseProtocolCachePolicy |
8200 | timeoutInterval:10 | |
7bd76e97 JF |
8201 | ] |
8202 | ||
8203 | returningResponse:NULL | |
8204 | error:NULL | |
8205 | ]) | |
8206 | if (UIImage *image = [UIImage imageWithData:data]) | |
02735663 | 8207 | [self performSelectorOnMainThread:@selector(_setImage:) withObject:[NSArray arrayWithObjects:url, image, nil] waitUntilDone:NO]; |
8252b666 JF |
8208 | |
8209 | [pool release]; | |
8210 | } | |
8211 | ||
5829aea2 | 8212 | - (void) setSource:(Source *)source { |
21ac0ce2 JF |
8213 | source_ = source; |
8214 | [source_ setDelegate:self]; | |
8215 | ||
8216 | [self setFetch:[NSNumber numberWithBool:[source_ fetch]]]; | |
8217 | ||
8dbf3abc | 8218 | icon_ = [UIImage imageNamed:@"unknown.png"]; |
5829aea2 | 8219 | |
7b33d201 | 8220 | origin_ = [source name]; |
7bd76e97 | 8221 | label_ = [source rooturi]; |
5829aea2 GP |
8222 | |
8223 | [content_ setNeedsDisplay]; | |
8252b666 | 8224 | |
02735663 JF |
8225 | url_ = [source iconURL]; |
8226 | [NSThread detachNewThreadSelector:@selector(_setSource:) toTarget:self withObject:url_]; | |
77801ff1 JF |
8227 | } |
8228 | ||
55066b9e | 8229 | - (void) setAllSource { |
21ac0ce2 JF |
8230 | source_ = nil; |
8231 | [indicator_ stopAnimating]; | |
8232 | ||
8dbf3abc | 8233 | icon_ = [UIImage imageNamed:@"folder.png"]; |
55066b9e JF |
8234 | origin_ = UCLocalize("ALL_SOURCES"); |
8235 | label_ = UCLocalize("ALL_SOURCES_EX"); | |
8236 | [content_ setNeedsDisplay]; | |
8237 | } | |
8238 | ||
5829aea2 GP |
8239 | - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
8240 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
8241 | UIView *content([self contentView]); | |
8242 | CGRect bounds([content bounds]); | |
77801ff1 | 8243 | |
b97fcfc6 | 8244 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
5829aea2 GP |
8245 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8246 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
8247 | [content addSubview:content_]; | |
dc5812ec | 8248 | |
5829aea2 GP |
8249 | [content_ setDelegate:self]; |
8250 | [content_ setOpaque:YES]; | |
53db9999 | 8251 | |
21ac0ce2 JF |
8252 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGraySmall] autorelease]; |
8253 | [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin];// | UIViewAutoresizingFlexibleBottomMargin]; | |
8254 | [content addSubview:indicator_]; | |
8255 | ||
53db9999 | 8256 | [[content_ layer] setContentsGravity:kCAGravityTopLeft]; |
5829aea2 GP |
8257 | } return self; |
8258 | } | |
b4d89997 | 8259 | |
21ac0ce2 JF |
8260 | - (void) layoutSubviews { |
8261 | [super layoutSubviews]; | |
8262 | ||
8263 | UIView *content([self contentView]); | |
8264 | CGRect bounds([content bounds]); | |
8265 | ||
8266 | CGRect frame([indicator_ frame]); | |
8267 | frame.origin.x = bounds.size.width - frame.size.width; | |
8323c1b9 | 8268 | frame.origin.y = Retina((bounds.size.height - frame.size.height) / 2); |
21ac0ce2 JF |
8269 | |
8270 | if (kCFCoreFoundationVersionNumber < 800) | |
8271 | frame.origin.x -= 8; | |
8272 | [indicator_ setFrame:frame]; | |
8273 | } | |
8274 | ||
003fc610 | 8275 | - (NSString *) accessibilityLabel { |
66abff39 | 8276 | return origin_; |
003fc610 GP |
8277 | } |
8278 | ||
5829aea2 GP |
8279 | - (void) drawContentRect:(CGRect)rect { |
8280 | bool highlighted(highlighted_); | |
8281 | float width(rect.size.width); | |
36bb2ca2 | 8282 | |
25c1dafb JF |
8283 | if (icon_ != nil) { |
8284 | CGRect rect; | |
8285 | rect.size = [(UIImage *) icon_ size]; | |
8286 | ||
8287 | while (rect.size.width > 32 || rect.size.height > 32) { | |
8288 | rect.size.width /= 2; | |
8289 | rect.size.height /= 2; | |
8290 | } | |
8291 | ||
86a333c6 JF |
8292 | rect.origin.x = 26 - rect.size.width / 2; |
8293 | rect.origin.y = 26 - rect.size.height / 2; | |
25c1dafb | 8294 | |
8323c1b9 | 8295 | [icon_ drawInRect:Retina(rect)]; |
25c1dafb | 8296 | } |
36bb2ca2 | 8297 | |
5d0438dc | 8298 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
5829aea2 | 8299 | UISetColor(White_); |
dc5812ec | 8300 | |
5829aea2 GP |
8301 | if (!highlighted) |
8302 | UISetColor(Black_); | |
ffb4fe6a | 8303 | [origin_ drawAtPoint:CGPointMake(52, 8) forWidth:(width - 49) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
f79a4512 | 8304 | |
5829aea2 | 8305 | if (!highlighted) |
e93fd095 | 8306 | UISetColor(Gray_); |
ffb4fe6a | 8307 | [label_ drawAtPoint:CGPointMake(52, 29) forWidth:(width - 49) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; |
5829aea2 | 8308 | } |
dc5812ec | 8309 | |
21ac0ce2 JF |
8310 | - (void) setFetch:(NSNumber *)fetch { |
8311 | if ([fetch boolValue]) | |
8312 | [indicator_ startAnimating]; | |
8313 | else | |
8314 | [indicator_ stopAnimating]; | |
8315 | } | |
8316 | ||
3f8edf70 GP |
8317 | @end |
8318 | /* }}} */ | |
8319 | /* Sources Controller {{{ */ | |
cd79e8cf | 8320 | @interface SourcesController : CyteViewController < |
5829aea2 GP |
8321 | UITableViewDataSource, |
8322 | UITableViewDelegate | |
8323 | > { | |
8324 | _transient Database *database_; | |
c33064f1 JF |
8325 | unsigned era_; |
8326 | ||
bf7c998c | 8327 | _H<UITableView, 2> list_; |
7b33d201 | 8328 | _H<NSMutableArray> sources_; |
5829aea2 | 8329 | int offset_; |
723a0072 | 8330 | |
7b33d201 JF |
8331 | _H<NSString> href_; |
8332 | _H<UIProgressHUD> hud_; | |
8333 | _H<NSError> error_; | |
dc63e78f | 8334 | |
5829aea2 GP |
8335 | NSURLConnection *trivial_bz2_; |
8336 | NSURLConnection *trivial_gz_; | |
b4d89997 | 8337 | |
5829aea2 GP |
8338 | BOOL cydia_; |
8339 | } | |
dc5812ec | 8340 | |
5829aea2 | 8341 | - (id) initWithDatabase:(Database *)database; |
31eedaae | 8342 | - (void) updateButtonsForEditingStatusAnimated:(BOOL)animated; |
5829aea2 GP |
8343 | |
8344 | @end | |
8345 | ||
8346 | @implementation SourcesController | |
8347 | ||
8348 | - (void) _releaseConnection:(NSURLConnection *)connection { | |
8349 | if (connection != nil) { | |
8350 | [connection cancel]; | |
8351 | //[connection setDelegate:nil]; | |
8352 | [connection release]; | |
36bb2ca2 | 8353 | } |
5829aea2 | 8354 | } |
dc5812ec | 8355 | |
5829aea2 | 8356 | - (void) dealloc { |
5829aea2 GP |
8357 | [self _releaseConnection:trivial_gz_]; |
8358 | [self _releaseConnection:trivial_bz2_]; | |
3178d79b | 8359 | |
5829aea2 GP |
8360 | [super dealloc]; |
8361 | } | |
b5e7eebb | 8362 | |
fe8e721f GP |
8363 | - (NSURL *) navigationURL { |
8364 | return [NSURL URLWithString:@"cydia://sources"]; | |
8365 | } | |
8366 | ||
5829aea2 GP |
8367 | - (void) viewDidAppear:(BOOL)animated { |
8368 | [super viewDidAppear:animated]; | |
8369 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
8370 | } | |
9bb3b295 | 8371 | |
5829aea2 | 8372 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
55066b9e | 8373 | return 2; |
5829aea2 | 8374 | } |
8fe19fc1 | 8375 | |
5829aea2 | 8376 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
55066b9e JF |
8377 | if (section == 1) |
8378 | return UCLocalize("INDIVIDUAL_SOURCES"); | |
90ba4f86 | 8379 | return nil; |
36bb2ca2 | 8380 | } |
b4d89997 | 8381 | |
5829aea2 | 8382 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
55066b9e JF |
8383 | switch (section) { |
8384 | case 0: return 1; | |
8385 | case 1: return [sources_ count]; | |
8386 | default: return 0; | |
8387 | } | |
5829aea2 | 8388 | } |
3178d79b | 8389 | |
5829aea2 | 8390 | - (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath { |
c33064f1 JF |
8391 | @synchronized (database_) { |
8392 | if ([database_ era] != era_) | |
8393 | return nil; | |
55066b9e JF |
8394 | if ([indexPath section] != 1) |
8395 | return nil; | |
8dc0d35d | 8396 | NSUInteger index([indexPath row]); |
55066b9e JF |
8397 | if (index >= [sources_ count]) |
8398 | return nil; | |
8399 | return [sources_ objectAtIndex:index]; | |
c33064f1 | 8400 | } } |
b4d89997 | 8401 | |
5829aea2 GP |
8402 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
8403 | static NSString *cellIdentifier = @"SourceCell"; | |
8404 | ||
8405 | SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
55066b9e | 8406 | if (cell == nil) cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease]; |
b89fa270 | 8407 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
5829aea2 | 8408 | |
55066b9e JF |
8409 | Source *source([self sourceAtIndexPath:indexPath]); |
8410 | if (source == nil) | |
8411 | [cell setAllSource]; | |
8412 | else | |
8413 | [cell setSource:source]; | |
8414 | ||
5829aea2 | 8415 | return cell; |
f6e13561 GP |
8416 | } |
8417 | ||
5829aea2 | 8418 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e | 8419 | SectionsController *controller([[[SectionsController alloc] |
5829aea2 | 8420 | initWithDatabase:database_ |
55066b9e JF |
8421 | source:[self sourceAtIndexPath:indexPath] |
8422 | ] autorelease]); | |
5829aea2 | 8423 | |
3f8edf70 | 8424 | [controller setDelegate:delegate_]; |
3f8edf70 | 8425 | [[self navigationController] pushViewController:controller animated:YES]; |
36bb2ca2 | 8426 | } |
b4d89997 | 8427 | |
6840bff3 | 8428 | - (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e JF |
8429 | if ([indexPath section] != 1) |
8430 | return false; | |
5829aea2 GP |
8431 | Source *source = [self sourceAtIndexPath:indexPath]; |
8432 | return [source record] != nil; | |
dcb47737 RP |
8433 | } |
8434 | ||
6840bff3 | 8435 | - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e | 8436 | _assert([indexPath section] == 1); |
0e1aa02c JF |
8437 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
8438 | Source *source = [self sourceAtIndexPath:indexPath]; | |
8dc0d35d JF |
8439 | if (source == nil) return; |
8440 | ||
0e1aa02c | 8441 | [Sources_ removeObjectForKey:[source key]]; |
c38cbbec JF |
8442 | Changed_ = true; |
8443 | ||
392ff7e4 | 8444 | [delegate_ _saveConfig]; |
cc3b7d31 | 8445 | [delegate_ reloadDataWithInvocation:nil]; |
0e1aa02c | 8446 | } |
5829aea2 | 8447 | } |
bcccf498 | 8448 | |
51807490 JF |
8449 | - (void) tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { |
8450 | [self updateButtonsForEditingStatusAnimated:YES]; | |
8451 | } | |
8452 | ||
5829aea2 | 8453 | - (void) complete { |
93460555 | 8454 | [delegate_ addTrivialSource:href_]; |
da3ec19c JF |
8455 | href_ = nil; |
8456 | ||
5829aea2 | 8457 | [delegate_ syncData]; |
3178d79b JF |
8458 | } |
8459 | ||
5829aea2 GP |
8460 | - (NSString *) getWarning { |
8461 | NSString *href(href_); | |
8462 | NSRange colon([href rangeOfString:@"://"]); | |
8463 | if (colon.location != NSNotFound) | |
8464 | href = [href substringFromIndex:(colon.location + 3)]; | |
8465 | href = [href stringByAddingPercentEscapes]; | |
8466 | href = [CydiaURL(@"api/repotag/") stringByAppendingString:href]; | |
5829aea2 GP |
8467 | |
8468 | NSURL *url([NSURL URLWithString:href]); | |
8469 | ||
8470 | NSStringEncoding encoding; | |
8471 | NSError *error(nil); | |
8472 | ||
8473 | if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]) | |
8474 | return [warning length] == 0 ? nil : warning; | |
8475 | return nil; | |
807ae6d7 | 8476 | } |
b4d89997 | 8477 | |
5829aea2 GP |
8478 | - (void) _endConnection:(NSURLConnection *)connection { |
8479 | // XXX: the memory management in this method is horribly awkward | |
8480 | ||
8481 | NSURLConnection **field = NULL; | |
44990507 | 8482 | if (connection == trivial_bz2_) |
5829aea2 GP |
8483 | field = &trivial_bz2_; |
8484 | else if (connection == trivial_gz_) | |
8485 | field = &trivial_gz_; | |
8486 | _assert(field != NULL); | |
8487 | [connection release]; | |
8488 | *field = nil; | |
8489 | ||
8490 | if ( | |
5829aea2 GP |
8491 | trivial_bz2_ == nil && |
8492 | trivial_gz_ == nil | |
8493 | ) { | |
da3ec19c JF |
8494 | NSString *warning(cydia_ ? [self yieldToSelector:@selector(getWarning)] : nil); |
8495 | ||
9eae15b8 JF |
8496 | [delegate_ releaseNetworkActivityIndicator]; |
8497 | ||
8498 | [delegate_ removeProgressHUD:hud_]; | |
9eae15b8 JF |
8499 | hud_ = nil; |
8500 | ||
5829aea2 | 8501 | if (cydia_) { |
da3ec19c | 8502 | if (warning != nil) { |
5829aea2 GP |
8503 | UIAlertView *alert = [[[UIAlertView alloc] |
8504 | initWithTitle:UCLocalize("SOURCE_WARNING") | |
8505 | message:warning | |
8506 | delegate:self | |
8507 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8508 | otherButtonTitles: |
8509 | UCLocalize("ADD_ANYWAY"), | |
8510 | nil | |
5829aea2 GP |
8511 | ] autorelease]; |
8512 | ||
8513 | [alert setContext:@"warning"]; | |
8514 | [alert setNumberOfRows:1]; | |
8515 | [alert show]; | |
708cf61e JF |
8516 | |
8517 | // XXX: there used to be this great mechanism called yieldToPopup... who deleted it? | |
8518 | error_ = nil; | |
8519 | return; | |
8520 | } | |
8521 | ||
8522 | [self complete]; | |
5829aea2 GP |
8523 | } else if (error_ != nil) { |
8524 | UIAlertView *alert = [[[UIAlertView alloc] | |
8525 | initWithTitle:UCLocalize("VERIFICATION_ERROR") | |
8526 | message:[error_ localizedDescription] | |
8527 | delegate:self | |
8528 | cancelButtonTitle:UCLocalize("OK") | |
8529 | otherButtonTitles:nil | |
8530 | ] autorelease]; | |
8531 | ||
8532 | [alert setContext:@"urlerror"]; | |
8533 | [alert show]; | |
da3ec19c JF |
8534 | |
8535 | href_ = nil; | |
5829aea2 GP |
8536 | } else { |
8537 | UIAlertView *alert = [[[UIAlertView alloc] | |
8538 | initWithTitle:UCLocalize("NOT_REPOSITORY") | |
8539 | message:UCLocalize("NOT_REPOSITORY_EX") | |
8540 | delegate:self | |
8541 | cancelButtonTitle:UCLocalize("OK") | |
8542 | otherButtonTitles:nil | |
8543 | ] autorelease]; | |
8544 | ||
8545 | [alert setContext:@"trivial"]; | |
8546 | [alert show]; | |
da3ec19c JF |
8547 | |
8548 | href_ = nil; | |
5829aea2 GP |
8549 | } |
8550 | ||
7b33d201 | 8551 | error_ = nil; |
5829aea2 | 8552 | } |
807ae6d7 | 8553 | } |
8fe19fc1 | 8554 | |
5829aea2 GP |
8555 | - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { |
8556 | switch ([response statusCode]) { | |
8557 | case 200: | |
8558 | cydia_ = YES; | |
8559 | } | |
8e05f686 RP |
8560 | } |
8561 | ||
5829aea2 | 8562 | - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { |
47ff9ab8 | 8563 | lprintf("connection:\"%s\" didFailWithError:\"%s\"\n", [href_ UTF8String], [[error localizedDescription] UTF8String]); |
7b33d201 | 8564 | error_ = error; |
5829aea2 | 8565 | [self _endConnection:connection]; |
807ae6d7 | 8566 | } |
b4d89997 | 8567 | |
5829aea2 GP |
8568 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
8569 | [self _endConnection:connection]; | |
8570 | } | |
b4d89997 | 8571 | |
5829aea2 | 8572 | - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method { |
2e1652a9 JF |
8573 | NSURL *url([NSURL URLWithString:href]); |
8574 | ||
5829aea2 | 8575 | NSMutableURLRequest *request = [NSMutableURLRequest |
2e1652a9 | 8576 | requestWithURL:url |
5829aea2 | 8577 | cachePolicy:NSURLRequestUseProtocolCachePolicy |
dd6f3b7b | 8578 | timeoutInterval:10 |
5829aea2 | 8579 | ]; |
bc11cf5b | 8580 | |
5829aea2 | 8581 | [request setHTTPMethod:method]; |
b4d89997 | 8582 | |
5829aea2 GP |
8583 | if (Machine_ != NULL) |
8584 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
2e1652a9 | 8585 | |
1209b7de JF |
8586 | if (UniqueID_ != nil) |
8587 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
8588 | ||
2e1652a9 | 8589 | if ([url isCydiaSecure]) { |
1209b7de | 8590 | if (UniqueID_ != nil) |
c83678e8 | 8591 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; |
2e1652a9 | 8592 | } |
b4d89997 | 8593 | |
5829aea2 | 8594 | return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; |
3178d79b JF |
8595 | } |
8596 | ||
6840bff3 | 8597 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
5829aea2 | 8598 | NSString *context([alert context]); |
dc5812ec | 8599 | |
5829aea2 GP |
8600 | if ([context isEqualToString:@"source"]) { |
8601 | switch (button) { | |
8602 | case 1: { | |
8603 | NSString *href = [[alert textField] text]; | |
baf80942 | 8604 | |
b4fd1197 | 8605 | static RegEx href_r("(http(s?)://|file:///)[^# ]*"); |
2595e4c3 JF |
8606 | if (!href_r(href)) { |
8607 | UIAlertView *alert = [[[UIAlertView alloc] | |
f27e8745 JF |
8608 | initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("INVALID_URL")] |
8609 | message:UCLocalize("INVALID_URL_EX") | |
2595e4c3 JF |
8610 | delegate:self |
8611 | cancelButtonTitle:UCLocalize("OK") | |
8612 | otherButtonTitles:nil | |
8613 | ] autorelease]; | |
8614 | ||
8615 | [alert setContext:@"badurl"]; | |
8616 | [alert show]; | |
8617 | ||
8618 | break; | |
8619 | } | |
8620 | ||
5829aea2 GP |
8621 | if (![href hasSuffix:@"/"]) |
8622 | href_ = [href stringByAppendingString:@"/"]; | |
8623 | else | |
8624 | href_ = href; | |
b4d89997 | 8625 | |
5829aea2 GP |
8626 | trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; |
8627 | trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; | |
b4d89997 | 8628 | |
5829aea2 | 8629 | cydia_ = false; |
8fe19fc1 | 8630 | |
5829aea2 | 8631 | // XXX: this is stupid |
7b33d201 | 8632 | hud_ = [delegate_ addProgressHUD]; |
5829aea2 | 8633 | [hud_ setText:UCLocalize("VERIFYING_URL")]; |
dcaecde2 | 8634 | [delegate_ retainNetworkActivityIndicator]; |
5829aea2 | 8635 | } break; |
770f2a8e | 8636 | |
5829aea2 GP |
8637 | case 0: |
8638 | break; | |
bc11cf5b | 8639 | |
5829aea2 GP |
8640 | _nodefault |
8641 | } | |
770f2a8e | 8642 | |
5829aea2 GP |
8643 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8644 | } else if ([context isEqualToString:@"trivial"]) | |
8645 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8646 | else if ([context isEqualToString:@"urlerror"]) | |
8647 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8648 | else if ([context isEqualToString:@"warning"]) { | |
8649 | switch (button) { | |
8650 | case 1: | |
da3ec19c | 8651 | [self performSelector:@selector(complete) withObject:nil afterDelay:0]; |
5829aea2 | 8652 | break; |
770f2a8e | 8653 | |
5829aea2 GP |
8654 | case 0: |
8655 | break; | |
b5e7eebb | 8656 | |
5829aea2 GP |
8657 | _nodefault |
8658 | } | |
770f2a8e | 8659 | |
5829aea2 GP |
8660 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8661 | } | |
8662 | } | |
770f2a8e | 8663 | |
e67ebdad JF |
8664 | - (void) updateButtonsForEditingStatusAnimated:(BOOL)animated { |
8665 | BOOL editing([list_ isEditing]); | |
8666 | ||
8667 | if (editing) | |
8668 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8669 | initWithTitle:UCLocalize("ADD") | |
8670 | style:UIBarButtonItemStylePlain | |
8671 | target:self | |
8672 | action:@selector(addButtonClicked) | |
8673 | ] autorelease] animated:animated]; | |
8674 | else if ([delegate_ updating]) | |
8675 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8676 | initWithTitle:UCLocalize("CANCEL") | |
8677 | style:UIBarButtonItemStyleDone | |
8678 | target:self | |
8679 | action:@selector(cancelButtonClicked) | |
8680 | ] autorelease] animated:animated]; | |
8681 | else | |
8682 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8683 | initWithTitle:UCLocalize("REFRESH") | |
8684 | style:UIBarButtonItemStylePlain | |
8685 | target:self | |
8686 | action:@selector(refreshButtonClicked) | |
8687 | ] autorelease] animated:animated]; | |
8688 | ||
8689 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
8690 | initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) | |
8691 | style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
8692 | target:self | |
8693 | action:@selector(editButtonClicked) | |
8694 | ] autorelease] animated:animated]; | |
8695 | } | |
8696 | ||
fe8e721f | 8697 | - (void) loadView { |
e8cbebe4 | 8698 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain] autorelease]; |
fe8e721f | 8699 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6ac6e186 | 8700 | [list_ setRowHeight:53]; |
7b33d201 | 8701 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 8702 | [list_ setDelegate:self]; |
e8cbebe4 | 8703 | [self setView:list_]; |
fe8e721f | 8704 | } |
770f2a8e | 8705 | |
fe8e721f | 8706 | - (void) viewDidLoad { |
7d887d0b JF |
8707 | [super viewDidLoad]; |
8708 | ||
fe8e721f | 8709 | [[self navigationItem] setTitle:UCLocalize("SOURCES")]; |
31eedaae JF |
8710 | [self updateButtonsForEditingStatusAnimated:NO]; |
8711 | } | |
8712 | ||
393699d7 | 8713 | - (void) viewWillAppear:(BOOL)animated { |
31eedaae JF |
8714 | [super viewWillAppear:animated]; |
8715 | ||
8716 | [list_ setEditing:NO]; | |
8717 | [self updateButtonsForEditingStatusAnimated:NO]; | |
fe8e721f | 8718 | } |
770f2a8e | 8719 | |
fe8e721f | 8720 | - (void) releaseSubviews { |
fe8e721f | 8721 | list_ = nil; |
7be3eb32 | 8722 | |
4f9acb7c JF |
8723 | sources_ = nil; |
8724 | ||
7be3eb32 | 8725 | [super releaseSubviews]; |
fe8e721f | 8726 | } |
770f2a8e | 8727 | |
fe8e721f GP |
8728 | - (id) initWithDatabase:(Database *)database { |
8729 | if ((self = [super init]) != nil) { | |
8730 | database_ = database; | |
807ae6d7 JF |
8731 | } return self; |
8732 | } | |
770f2a8e | 8733 | |
807ae6d7 | 8734 | - (void) reloadData { |
fe8e721f | 8735 | [super reloadData]; |
e67ebdad | 8736 | [self updateButtonsForEditingStatusAnimated:YES]; |
fe8e721f | 8737 | |
c33064f1 JF |
8738 | @synchronized (database_) { |
8739 | era_ = [database_ era]; | |
8740 | ||
4f9acb7c | 8741 | sources_ = [NSMutableArray arrayWithCapacity:16]; |
5829aea2 GP |
8742 | [sources_ addObjectsFromArray:[database_ sources]]; |
8743 | _trace(); | |
90ba4f86 | 8744 | [sources_ sortUsingSelector:@selector(compareByName:)]; |
5829aea2 GP |
8745 | _trace(); |
8746 | ||
8747 | int count([sources_ count]); | |
8748 | offset_ = 0; | |
8749 | for (int i = 0; i != count; i++) { | |
8750 | if ([[sources_ objectAtIndex:i] record] == nil) | |
8751 | break; | |
8752 | offset_++; | |
770f2a8e | 8753 | } |
807ae6d7 | 8754 | |
5829aea2 | 8755 | [list_ reloadData]; |
c33064f1 | 8756 | } } |
770f2a8e | 8757 | |
5829aea2 GP |
8758 | - (void) showAddSourcePrompt { |
8759 | UIAlertView *alert = [[[UIAlertView alloc] | |
8760 | initWithTitle:UCLocalize("ENTER_APT_URL") | |
8761 | message:nil | |
8762 | delegate:self | |
8763 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8764 | otherButtonTitles: |
8765 | UCLocalize("ADD_SOURCE"), | |
8766 | nil | |
5829aea2 | 8767 | ] autorelease]; |
770f2a8e | 8768 | |
5829aea2 | 8769 | [alert setContext:@"source"]; |
770f2a8e | 8770 | |
5829aea2 GP |
8771 | [alert setNumberOfRows:1]; |
8772 | [alert addTextFieldWithValue:@"http://" label:@""]; | |
770f2a8e | 8773 | |
5829aea2 GP |
8774 | UITextInputTraits *traits = [[alert textField] textInputTraits]; |
8775 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
8776 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
8777 | [traits setKeyboardType:UIKeyboardTypeURL]; | |
8778 | // XXX: UIReturnKeyDone | |
8779 | [traits setReturnKeyType:UIReturnKeyNext]; | |
770f2a8e | 8780 | |
5829aea2 | 8781 | [alert show]; |
770f2a8e JF |
8782 | } |
8783 | ||
5829aea2 GP |
8784 | - (void) addButtonClicked { |
8785 | [self showAddSourcePrompt]; | |
770f2a8e JF |
8786 | } |
8787 | ||
e67ebdad JF |
8788 | - (void) refreshButtonClicked { |
8789 | if ([delegate_ requestUpdate]) | |
8790 | [self updateButtonsForEditingStatusAnimated:YES]; | |
8791 | } | |
5829aea2 | 8792 | |
e67ebdad JF |
8793 | - (void) cancelButtonClicked { |
8794 | [delegate_ cancelUpdate]; | |
5829aea2 GP |
8795 | } |
8796 | ||
8797 | - (void) editButtonClicked { | |
8798 | [list_ setEditing:![list_ isEditing] animated:YES]; | |
31eedaae | 8799 | [self updateButtonsForEditingStatusAnimated:YES]; |
770f2a8e JF |
8800 | } |
8801 | ||
21c6da4b GP |
8802 | @end |
8803 | /* }}} */ | |
f3e11d24 | 8804 | |
f3e11d24 | 8805 | /* Stash Controller {{{ */ |
cd79e8cf | 8806 | @interface StashController : CyteViewController { |
7b33d201 JF |
8807 | _H<UIActivityIndicatorView> spinner_; |
8808 | _H<UILabel> status_; | |
8809 | _H<UILabel> caption_; | |
f3e11d24 | 8810 | } |
6840bff3 | 8811 | |
f3e11d24 GP |
8812 | @end |
8813 | ||
5829aea2 | 8814 | @implementation StashController |
f3e11d24 | 8815 | |
fe8e721f | 8816 | - (void) loadView { |
39470a3a JF |
8817 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
8818 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
8819 | [self setView:view]; | |
8820 | ||
8821 | [view setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; | |
fe8e721f | 8822 | |
7b33d201 | 8823 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; |
fe8e721f | 8824 | CGRect spinrect = [spinner_ frame]; |
8323c1b9 | 8825 | spinrect.origin.x = Retina([[self view] frame].size.width / 2 - spinrect.size.width / 2); |
fe8e721f GP |
8826 | spinrect.origin.y = [[self view] frame].size.height - 80.0f; |
8827 | [spinner_ setFrame:spinrect]; | |
8828 | [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; | |
39470a3a | 8829 | [view addSubview:spinner_]; |
fe8e721f GP |
8830 | [spinner_ startAnimating]; |
8831 | ||
8832 | CGRect captrect; | |
8833 | captrect.size.width = [[self view] frame].size.width; | |
8834 | captrect.size.height = 40.0f; | |
8835 | captrect.origin.x = 0; | |
8323c1b9 | 8836 | captrect.origin.y = Retina([[self view] frame].size.height / 2 - captrect.size.height * 2); |
7b33d201 | 8837 | caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; |
fe8e721f GP |
8838 | [caption_ setText:UCLocalize("PREPARING_FILESYSTEM")]; |
8839 | [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
8840 | [caption_ setFont:[UIFont boldSystemFontOfSize:28.0f]]; | |
8841 | [caption_ setTextColor:[UIColor whiteColor]]; | |
8842 | [caption_ setBackgroundColor:[UIColor clearColor]]; | |
8843 | [caption_ setShadowColor:[UIColor blackColor]]; | |
b129e6d9 | 8844 | [caption_ setTextAlignment:NSTextAlignmentCenter]; |
39470a3a | 8845 | [view addSubview:caption_]; |
fe8e721f GP |
8846 | |
8847 | CGRect statusrect; | |
8848 | statusrect.size.width = [[self view] frame].size.width; | |
8849 | statusrect.size.height = 30.0f; | |
8850 | statusrect.origin.x = 0; | |
8323c1b9 | 8851 | statusrect.origin.y = Retina([[self view] frame].size.height / 2 - statusrect.size.height); |
7b33d201 | 8852 | status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; |
fe8e721f GP |
8853 | [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
8854 | [status_ setText:UCLocalize("EXIT_WHEN_COMPLETE")]; | |
8855 | [status_ setFont:[UIFont systemFontOfSize:16.0f]]; | |
8856 | [status_ setTextColor:[UIColor whiteColor]]; | |
8857 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
8858 | [status_ setShadowColor:[UIColor blackColor]]; | |
b129e6d9 | 8859 | [status_ setTextAlignment:NSTextAlignmentCenter]; |
39470a3a | 8860 | [view addSubview:status_]; |
fe8e721f GP |
8861 | } |
8862 | ||
67dd58c7 JF |
8863 | - (void) releaseSubviews { |
8864 | spinner_ = nil; | |
8865 | status_ = nil; | |
8866 | caption_ = nil; | |
7be3eb32 JF |
8867 | |
8868 | [super releaseSubviews]; | |
67dd58c7 JF |
8869 | } |
8870 | ||
770f2a8e | 8871 | @end |
807ae6d7 | 8872 | /* }}} */ |
770f2a8e | 8873 | |
a06e9179 JF |
8874 | @interface CYURLCache : SDURLCache { |
8875 | } | |
8876 | ||
8877 | @end | |
8878 | ||
8879 | @implementation CYURLCache | |
8880 | ||
8881 | - (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request { | |
8882 | #if !ForRelease | |
8883 | if (false); | |
8884 | else if ([event isEqualToString:@"no-cache"]) | |
8885 | event = @"!!!"; | |
8886 | else if ([event isEqualToString:@"store"]) | |
8887 | event = @">>>"; | |
8888 | else if ([event isEqualToString:@"invalid"]) | |
8889 | event = @"???"; | |
8890 | else if ([event isEqualToString:@"memory"]) | |
8891 | event = @"mem"; | |
8892 | else if ([event isEqualToString:@"disk"]) | |
8893 | event = @"ssd"; | |
8894 | else if ([event isEqualToString:@"miss"]) | |
8895 | event = @"---"; | |
8896 | ||
8897 | NSLog(@"%@: %@", event, [[request URL] absoluteString]); | |
8898 | #endif | |
8899 | } | |
8900 | ||
5e845121 JF |
8901 | - (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request { |
8902 | if (NSURLResponse *response = [cached response]) | |
8903 | if (NSString *mime = [response MIMEType]) | |
8904 | if ([mime isEqualToString:@"text/cache-manifest"]) { | |
8905 | NSURL *url([response URL]); | |
8906 | ||
8907 | #if !ForRelease | |
8908 | NSLog(@"###: %@", [url absoluteString]); | |
8909 | #endif | |
8910 | ||
8911 | @synchronized (HostConfig_) { | |
8912 | [CachedURLs_ addObject:url]; | |
8913 | } | |
8914 | } | |
8915 | ||
8916 | [super storeCachedResponse:cached forRequest:request]; | |
8917 | } | |
8918 | ||
584daea0 JF |
8919 | - (void) createDiskCachePath { |
8920 | [super createDiskCachePath]; | |
584daea0 JF |
8921 | } |
8922 | ||
a06e9179 JF |
8923 | @end |
8924 | ||
2367a917 | 8925 | @interface Cydia : UIApplication < |
adb61bda | 8926 | ConfirmationControllerDelegate, |
6915b806 | 8927 | DatabaseDelegate, |
c88974b1 | 8928 | CydiaDelegate |
2367a917 | 8929 | > { |
7b33d201 | 8930 | _H<UIWindow> window_; |
5fe2bcc6 | 8931 | _H<CydiaTabBarController> tabbar_; |
9549563e | 8932 | _H<CyteTabBarController> emulated_; |
fedd38fe | 8933 | _H<AppCacheController> appcache_; |
3931b718 | 8934 | |
7b33d201 JF |
8935 | _H<NSMutableArray> essential_; |
8936 | _H<NSMutableArray> broken_; | |
dc5812ec JF |
8937 | |
8938 | Database *database_; | |
dc5812ec | 8939 | |
7b33d201 | 8940 | _H<NSURL> starturl_; |
54043703 | 8941 | |
235f5487 | 8942 | unsigned locked_; |
54043703 | 8943 | unsigned activity_; |
9b619239 | 8944 | |
7b33d201 | 8945 | _H<StashController> stash_; |
f3e11d24 | 8946 | |
8c02abc8 | 8947 | bool loaded_; |
dc5812ec JF |
8948 | } |
8949 | ||
fed0d010 | 8950 | - (void) loadData; |
670a0494 | 8951 | |
dc5812ec JF |
8952 | @end |
8953 | ||
8954 | @implementation Cydia | |
8955 | ||
2ef6faad | 8956 | - (void) lockSuspend { |
8a3b565c JF |
8957 | if (locked_++ == 0) { |
8958 | if ($SBSSetInterceptsMenuButtonForever != NULL) | |
8959 | (*$SBSSetInterceptsMenuButtonForever)(true); | |
26c8a4c8 JF |
8960 | |
8961 | [self setIdleTimerDisabled:YES]; | |
8a3b565c | 8962 | } |
2ef6faad JF |
8963 | } |
8964 | ||
8965 | - (void) unlockSuspend { | |
8a3b565c | 8966 | if (--locked_ == 0) { |
26c8a4c8 JF |
8967 | [self setIdleTimerDisabled:NO]; |
8968 | ||
8a3b565c JF |
8969 | if ($SBSSetInterceptsMenuButtonForever != NULL) |
8970 | (*$SBSSetInterceptsMenuButtonForever)(false); | |
8971 | } | |
2ef6faad JF |
8972 | } |
8973 | ||
b5e7eebb | 8974 | - (void) beginUpdate { |
7585ce66 | 8975 | [tabbar_ beginUpdate]; |
b5e7eebb GP |
8976 | } |
8977 | ||
e67ebdad JF |
8978 | - (void) cancelUpdate { |
8979 | [tabbar_ cancelUpdate]; | |
8980 | } | |
8981 | ||
8982 | - (bool) requestUpdate { | |
8983 | if (IsReachable("cydia.saurik.com")) { | |
8984 | [self beginUpdate]; | |
8985 | return true; | |
8986 | } else { | |
8987 | UIAlertView *alert = [[[UIAlertView alloc] | |
8988 | initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("REFRESH")] | |
8989 | message:@"Host Unreachable" // XXX: Localize | |
8990 | delegate:self | |
8991 | cancelButtonTitle:UCLocalize("OK") | |
8992 | otherButtonTitles:nil | |
8993 | ] autorelease]; | |
8994 | ||
8995 | [alert setContext:@"norefresh"]; | |
8996 | [alert show]; | |
8997 | ||
8998 | return false; | |
8999 | } | |
9000 | } | |
9001 | ||
b5e7eebb | 9002 | - (BOOL) updating { |
7585ce66 | 9003 | return [tabbar_ updating]; |
b5e7eebb GP |
9004 | } |
9005 | ||
9bedffaa JF |
9006 | - (void) _loaded { |
9007 | if ([broken_ count] != 0) { | |
9008 | int count = [broken_ count]; | |
9009 | ||
37d2b2a9 | 9010 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 9011 | initWithTitle:(count == 1 ? UCLocalize("HALFINSTALLED_PACKAGE") : [NSString stringWithFormat:UCLocalize("HALFINSTALLED_PACKAGES"), count]) |
b5e7eebb GP |
9012 | message:UCLocalize("HALFINSTALLED_PACKAGE_EX") |
9013 | delegate:self | |
1dc38e9c | 9014 | cancelButtonTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("FORCIBLY_CLEAR"), UCLocalize("UNSAFE")] |
1aa29546 JF |
9015 | otherButtonTitles: |
9016 | UCLocalize("TEMPORARY_IGNORE"), | |
9017 | nil | |
9bedffaa JF |
9018 | ] autorelease]; |
9019 | ||
37d2b2a9 | 9020 | [alert setContext:@"fixhalf"]; |
59befad5 | 9021 | [alert setNumberOfRows:2]; |
37d2b2a9 | 9022 | [alert show]; |
9bedffaa JF |
9023 | } else if (!Ignored_ && [essential_ count] != 0) { |
9024 | int count = [essential_ count]; | |
9025 | ||
37d2b2a9 | 9026 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 9027 | initWithTitle:(count == 1 ? UCLocalize("ESSENTIAL_UPGRADE") : [NSString stringWithFormat:UCLocalize("ESSENTIAL_UPGRADES"), count]) |
b5e7eebb GP |
9028 | message:UCLocalize("ESSENTIAL_UPGRADE_EX") |
9029 | delegate:self | |
9030 | cancelButtonTitle:UCLocalize("TEMPORARY_IGNORE") | |
1aa29546 JF |
9031 | otherButtonTitles: |
9032 | UCLocalize("UPGRADE_ESSENTIAL"), | |
9033 | UCLocalize("COMPLETE_UPGRADE"), | |
9034 | nil | |
9bedffaa JF |
9035 | ] autorelease]; |
9036 | ||
37d2b2a9 GP |
9037 | [alert setContext:@"upgrade"]; |
9038 | [alert show]; | |
9bedffaa JF |
9039 | } |
9040 | } | |
9041 | ||
2925cbba JF |
9042 | - (void) returnToCydia { |
9043 | [self _loaded]; | |
9044 | } | |
9045 | ||
7623f855 | 9046 | - (void) _saveConfig { |
9ac1ef9e JF |
9047 | @synchronized (database_) { |
9048 | _trace(); | |
9049 | MetaFile_.Sync(); | |
9050 | _trace(); | |
9051 | } | |
ffbb3bd5 | 9052 | |
7623f855 | 9053 | if (Changed_) { |
7623f855 | 9054 | NSString *error(nil); |
ffbb3bd5 | 9055 | |
7623f855 JF |
9056 | if (NSData *data = [NSPropertyListSerialization dataFromPropertyList:Metadata_ format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error]) { |
9057 | _trace(); | |
9058 | NSError *error(nil); | |
6b19945f | 9059 | if (!_root([data writeToFile:@"/var/lib/cydia/metadata.plist" options:NSAtomicWrite error:&error])) |
7623f855 JF |
9060 | NSLog(@"failure to save metadata data: %@", error); |
9061 | _trace(); | |
ffbb3bd5 JF |
9062 | |
9063 | Changed_ = false; | |
7623f855 JF |
9064 | } else { |
9065 | NSLog(@"failure to serialize metadata: %@", error); | |
7623f855 | 9066 | } |
7623f855 | 9067 | } |
33e30380 | 9068 | |
f6b68b2c | 9069 | CydiaWriteSources(); |
7623f855 JF |
9070 | } |
9071 | ||
89571a5b | 9072 | // Navigation controller for the queuing badge. |
15f0d613 | 9073 | - (UINavigationController *) queueNavigationController { |
89571a5b GP |
9074 | NSArray *controllers = [tabbar_ viewControllers]; |
9075 | return [controllers objectAtIndex:3]; | |
9076 | } | |
9077 | ||
302bf91c JF |
9078 | - (void) unloadData { |
9079 | [tabbar_ unloadData]; | |
9080 | } | |
9081 | ||
7623f855 JF |
9082 | - (void) _updateData { |
9083 | [self _saveConfig]; | |
302bf91c | 9084 | [self unloadData]; |
f6371a33 | 9085 | |
15f0d613 | 9086 | UINavigationController *navigation = [self queueNavigationController]; |
b5e7eebb | 9087 | |
4305896c | 9088 | id queuedelegate = nil; |
89571a5b GP |
9089 | if ([[navigation viewControllers] count] > 0) |
9090 | queuedelegate = [[navigation viewControllers] objectAtIndex:0]; | |
bc11cf5b | 9091 | |
89571a5b GP |
9092 | [queuedelegate queueStatusDidChange]; |
9093 | [[navigation tabBarItem] setBadgeValue:(Queuing_ ? UCLocalize("Q_D") : nil)]; | |
7623f855 JF |
9094 | } |
9095 | ||
53fb38da | 9096 | - (void) _refreshIfPossible:(NSDate *)update { |
8c02abc8 | 9097 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
bc11cf5b | 9098 | |
45e21ffa | 9099 | bool recently = false; |
45e21ffa GP |
9100 | if (update != nil) { |
9101 | NSTimeInterval interval([update timeIntervalSinceNow]); | |
805ba1b0 | 9102 | if (interval > -(15*60)) |
45e21ffa GP |
9103 | recently = true; |
9104 | } | |
9105 | ||
9106 | // Don't automatic refresh if: | |
9107 | // - We already refreshed recently. | |
9108 | // - We already auto-refreshed this launch. | |
9109 | // - Auto-refresh is disabled. | |
8d3505c5 JF |
9110 | // - Cydia's server is not reachable |
9111 | if (recently || loaded_ || ManualRefresh || !IsReachable("cydia.saurik.com")) { | |
35f0a3b5 | 9112 | // If we are cancelling, we need to make sure it knows it's already loaded. |
45e21ffa | 9113 | loaded_ = true; |
d13edf44 JF |
9114 | |
9115 | [self performSelectorOnMainThread:@selector(_loaded) withObject:nil waitUntilDone:NO]; | |
45e21ffa GP |
9116 | } else { |
9117 | // We are going to load, so remember that. | |
9118 | loaded_ = true; | |
45e21ffa | 9119 | |
8d3505c5 | 9120 | [tabbar_ performSelectorOnMainThread:@selector(setUpdate:) withObject:update waitUntilDone:NO]; |
d13edf44 | 9121 | } |
9aecdc9c | 9122 | |
8c02abc8 | 9123 | [pool release]; |
9aecdc9c GP |
9124 | } |
9125 | ||
9126 | - (void) refreshIfPossible { | |
53fb38da | 9127 | [NSThread detachNewThreadSelector:@selector(_refreshIfPossible:) toTarget:self withObject:[Metadata_ objectForKey:@"LastUpdate"]]; |
9aecdc9c GP |
9128 | } |
9129 | ||
e09e1589 | 9130 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
1a83afc6 | 9131 | _profile(reloadDataWithInvocation) |
e09e1589 | 9132 | @synchronized (self) { |
851f4a99 | 9133 | UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); |
1450c2b3 JF |
9134 | if (hud != nil) |
9135 | [hud setText:UCLocalize("RELOADING_DATA")]; | |
dc5812ec | 9136 | |
4ba8f30a | 9137 | [database_ yieldToSelector:@selector(reloadDataWithInvocation:) withObject:invocation]; |
d061f4ba | 9138 | |
36bb2ca2 | 9139 | size_t changes(0); |
9bedffaa | 9140 | |
a54b1c10 | 9141 | [essential_ removeAllObjects]; |
9bedffaa | 9142 | [broken_ removeAllObjects]; |
b4d89997 | 9143 | |
1a83afc6 | 9144 | _profile(reloadDataWithInvocation$Essential) |
670a0494 | 9145 | NSArray *packages([database_ packages]); |
affeffc7 | 9146 | for (Package *package in packages) { |
9bedffaa JF |
9147 | if ([package half]) |
9148 | [broken_ addObject:package]; | |
823e2d97 JF |
9149 | if ([package upgradableAndEssential:YES] && ![package ignored]) { |
9150 | if ([package essential] && [package installed] != nil) | |
a54b1c10 | 9151 | [essential_ addObject:package]; |
36bb2ca2 | 9152 | ++changes; |
a54b1c10 | 9153 | } |
36bb2ca2 | 9154 | } |
1a83afc6 | 9155 | _end |
b4d89997 | 9156 | |
89571a5b | 9157 | UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem]; |
36bb2ca2 | 9158 | if (changes != 0) { |
0e1784b4 | 9159 | _trace(); |
36bb2ca2 | 9160 | NSString *badge([[NSNumber numberWithInt:changes] stringValue]); |
45e21ffa | 9161 | [changesItem setBadgeValue:badge]; |
65a03a7a | 9162 | [changesItem setAnimatedBadge:([essential_ count] > 0)]; |
0e1784b4 | 9163 | [self setApplicationIconBadgeNumber:changes]; |
c25a610d | 9164 | } else { |
0e1784b4 | 9165 | _trace(); |
45e21ffa GP |
9166 | [changesItem setBadgeValue:nil]; |
9167 | [changesItem setAnimatedBadge:NO]; | |
0e1784b4 | 9168 | [self setApplicationIconBadgeNumber:0]; |
c25a610d | 9169 | } |
b4d89997 | 9170 | |
838ec6e3 | 9171 | Queuing_ = false; |
7623f855 | 9172 | [self _updateData]; |
be64dfbf JF |
9173 | |
9174 | if (hud != nil) | |
9175 | [self removeProgressHUD:hud]; | |
1a83afc6 JF |
9176 | } |
9177 | _end | |
9178 | ||
9179 | PrintTimes(); | |
9180 | } | |
6d9712c4 | 9181 | |
7b0ce2da | 9182 | - (void) updateData { |
7623f855 | 9183 | [self _updateData]; |
b4d89997 JF |
9184 | } |
9185 | ||
383a58ac JF |
9186 | - (void) updateDataAndLoad { |
9187 | [self _updateData]; | |
9188 | if ([database_ progressDelegate] == nil) | |
9189 | [self _loaded]; | |
9190 | } | |
9191 | ||
7b0ce2da JF |
9192 | - (void) update_ { |
9193 | [database_ update]; | |
fca2f596 | 9194 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
7b0ce2da JF |
9195 | } |
9196 | ||
2e9123cb JF |
9197 | - (void) disemulate { |
9198 | if (emulated_ == nil) | |
9199 | return; | |
9200 | ||
81628115 JF |
9201 | if ([window_ respondsToSelector:@selector(setRootViewController:)]) |
9202 | [window_ setRootViewController:tabbar_]; | |
237c3d1c JF |
9203 | else { |
9204 | [window_ addSubview:[tabbar_ view]]; | |
9205 | [[emulated_ view] removeFromSuperview]; | |
9206 | } | |
9207 | ||
2e9123cb JF |
9208 | emulated_ = nil; |
9209 | [window_ setUserInteractionEnabled:YES]; | |
9210 | } | |
9211 | ||
9212 | - (void) presentModalViewController:(UIViewController *)controller force:(BOOL)force { | |
15f0d613 | 9213 | UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:controller] autorelease]); |
2e9123cb JF |
9214 | |
9215 | UIViewController *parent; | |
9216 | if (emulated_ == nil) | |
9217 | parent = tabbar_; | |
9218 | else if (!force) | |
9219 | parent = emulated_; | |
9220 | else { | |
9221 | [self disemulate]; | |
9222 | parent = tabbar_; | |
9223 | } | |
9224 | ||
483881e4 JF |
9225 | if (IsWildcat_) |
9226 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
2e9123cb | 9227 | [parent presentModalViewController:navigation animated:YES]; |
6915b806 JF |
9228 | } |
9229 | ||
9230 | - (ProgressController *) invokeNewProgress:(NSInvocation *)invocation forController:(UINavigationController *)navigation withTitle:(NSString *)title { | |
9231 | ProgressController *progress([[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]); | |
9232 | ||
9233 | if (navigation != nil) | |
9234 | [navigation pushViewController:progress animated:YES]; | |
9235 | else | |
2e9123cb | 9236 | [self presentModalViewController:progress force:YES]; |
6915b806 JF |
9237 | |
9238 | [progress invoke:invocation withTitle:title]; | |
9239 | return progress; | |
9240 | } | |
9241 | ||
9242 | - (void) detachNewProgressSelector:(SEL)selector toTarget:(id)target forController:(UINavigationController *)navigation title:(NSString *)title { | |
9243 | [self invokeNewProgress:[NSInvocation invocationWithSelector:selector forTarget:target] forController:navigation withTitle:title]; | |
9244 | } | |
9245 | ||
9246 | - (void) repairWithInvocation:(NSInvocation *)invocation { | |
9247 | _trace(); | |
8d5bc2ad | 9248 | [self invokeNewProgress:invocation forController:nil withTitle:@"REPAIRING"]; |
6915b806 JF |
9249 | _trace(); |
9250 | } | |
9251 | ||
9252 | - (void) repairWithSelector:(SEL)selector { | |
9253 | [self performSelectorOnMainThread:@selector(repairWithInvocation:) withObject:[NSInvocation invocationWithSelector:selector forTarget:database_] waitUntilDone:YES]; | |
9254 | } | |
9255 | ||
e09e1589 JF |
9256 | - (void) reloadData { |
9257 | [self reloadDataWithInvocation:nil]; | |
2925cbba JF |
9258 | if ([database_ progressDelegate] == nil) |
9259 | [self _loaded]; | |
e09e1589 JF |
9260 | } |
9261 | ||
7b0ce2da | 9262 | - (void) syncData { |
1fe5dc43 | 9263 | [self _saveConfig]; |
33e30380 JF |
9264 | [self detachNewProgressSelector:@selector(update_) toTarget:self forController:nil title:@"UPDATING_SOURCES"]; |
9265 | } | |
1fe5dc43 | 9266 | |
33e30380 | 9267 | - (void) addSource:(NSDictionary *) source { |
25c1dafb | 9268 | CydiaAddSource(source); |
33e30380 | 9269 | } |
7b0ce2da | 9270 | |
33e30380 | 9271 | - (void) addSource:(NSString *)href withDistribution:(NSString *)distribution andSections:(NSArray *)sections { |
25c1dafb | 9272 | CydiaAddSource(href, distribution, sections); |
7b0ce2da JF |
9273 | } |
9274 | ||
93460555 | 9275 | - (void) addTrivialSource:(NSString *)href { |
25c1dafb | 9276 | CydiaAddSource(href, @"./"); |
93460555 JF |
9277 | } |
9278 | ||
b3c8e69c JF |
9279 | - (void) updateValues { |
9280 | Changed_ = true; | |
9281 | } | |
9282 | ||
b4d89997 JF |
9283 | - (void) resolve { |
9284 | pkgProblemResolver *resolver = [database_ resolver]; | |
9285 | ||
9286 | resolver->InstallProtect(); | |
9287 | if (!resolver->Resolve(true)) | |
9288 | _error->Discard(); | |
2367a917 JF |
9289 | } |
9290 | ||
670a0494 | 9291 | - (bool) perform { |
d6c371f5 JF |
9292 | // XXX: this is a really crappy way of doing this. |
9293 | // like, seriously: this state machine is still broken, and cancelling this here doesn't really /fix/ that. | |
9294 | // for one, the user can still /start/ a reloading data event while they have a queue, which is stupid | |
9295 | // for two, this just means there is a race condition between the refresh completing and the confirmation controller appearing. | |
9296 | if ([tabbar_ updating]) | |
9297 | [tabbar_ cancelUpdate]; | |
9298 | ||
670a0494 JF |
9299 | if (![database_ prepare]) |
9300 | return false; | |
49048579 | 9301 | |
adb61bda | 9302 | ConfirmationController *page([[[ConfirmationController alloc] initWithDatabase:database_] autorelease]); |
affeffc7 | 9303 | [page setDelegate:self]; |
15f0d613 | 9304 | UINavigationController *confirm_([[[UINavigationController alloc] initWithRootViewController:page] autorelease]); |
49048579 | 9305 | |
36fbb2aa JF |
9306 | if (IsWildcat_) |
9307 | [confirm_ setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 9308 | [tabbar_ presentModalViewController:confirm_ animated:YES]; |
670a0494 JF |
9309 | |
9310 | return true; | |
3178d79b JF |
9311 | } |
9312 | ||
dc63e78f JF |
9313 | - (void) queue { |
9314 | @synchronized (self) { | |
9315 | [self perform]; | |
9316 | } | |
9317 | } | |
9318 | ||
9319 | - (void) clearPackage:(Package *)package { | |
9320 | @synchronized (self) { | |
9321 | [package clear]; | |
9322 | [self resolve]; | |
9323 | [self perform]; | |
9324 | } | |
9325 | } | |
9326 | ||
77801ff1 JF |
9327 | - (void) installPackages:(NSArray *)packages { |
9328 | @synchronized (self) { | |
9329 | for (Package *package in packages) | |
9330 | [package install]; | |
9331 | [self resolve]; | |
9332 | [self perform]; | |
9333 | } | |
9334 | } | |
9335 | ||
36bb2ca2 JF |
9336 | - (void) installPackage:(Package *)package { |
9337 | @synchronized (self) { | |
9338 | [package install]; | |
9339 | [self resolve]; | |
9340 | [self perform]; | |
9341 | } | |
9342 | } | |
9343 | ||
9344 | - (void) removePackage:(Package *)package { | |
9345 | @synchronized (self) { | |
9346 | [package remove]; | |
9347 | [self resolve]; | |
9348 | [self perform]; | |
9349 | } | |
9350 | } | |
9351 | ||
9352 | - (void) distUpgrade { | |
9353 | @synchronized (self) { | |
670a0494 JF |
9354 | if (![database_ upgrade]) |
9355 | return; | |
36bb2ca2 JF |
9356 | [self perform]; |
9357 | } | |
b4d89997 JF |
9358 | } |
9359 | ||
780cdb3b JF |
9360 | - (void) _uicache { |
9361 | _trace(); | |
856b8388 JF |
9362 | |
9363 | if (UpgradeCydia_ && Finish_ > 0) { | |
34ac1598 | 9364 | setreugid(0, 0); |
856b8388 JF |
9365 | system("su -c /usr/bin/uicache mobile"); |
9366 | } else { | |
9367 | system("/usr/bin/uicache"); | |
9368 | } | |
9369 | ||
780cdb3b JF |
9370 | _trace(); |
9371 | } | |
9372 | ||
9373 | - (void) uicache { | |
9374 | UIProgressHUD *hud([self addProgressHUD]); | |
9375 | [hud setText:UCLocalize("LOADING")]; | |
9376 | [self yieldToSelector:@selector(_uicache)]; | |
9377 | [self removeProgressHUD:hud]; | |
9378 | } | |
9379 | ||
fca2f596 JF |
9380 | - (void) perform_ { |
9381 | [database_ perform]; | |
9382 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; | |
780cdb3b | 9383 | [self performSelectorOnMainThread:@selector(uicache) withObject:nil waitUntilDone:YES]; |
fca2f596 JF |
9384 | } |
9385 | ||
b5e7eebb | 9386 | - (void) confirmWithNavigationController:(UINavigationController *)navigation { |
f6371a33 | 9387 | Queuing_ = false; |
2ef6faad | 9388 | [self lockSuspend]; |
fca2f596 | 9389 | [self detachNewProgressSelector:@selector(perform_) toTarget:self forController:navigation title:@"RUNNING"]; |
2ef6faad | 9390 | [self unlockSuspend]; |
dc5812ec JF |
9391 | } |
9392 | ||
54043703 JF |
9393 | - (void) retainNetworkActivityIndicator { |
9394 | if (activity_++ == 0) | |
9395 | [self setNetworkActivityIndicatorVisible:YES]; | |
848ed88b JF |
9396 | |
9397 | #if TraceLogging | |
9398 | NSLog(@"retainNetworkActivityIndicator->%d", activity_); | |
9399 | #endif | |
54043703 JF |
9400 | } |
9401 | ||
9402 | - (void) releaseNetworkActivityIndicator { | |
9403 | if (--activity_ == 0) | |
9404 | [self setNetworkActivityIndicatorVisible:NO]; | |
848ed88b JF |
9405 | |
9406 | #if TraceLogging | |
9407 | NSLog(@"releaseNetworkActivityIndicator->%d", activity_); | |
9408 | #endif | |
9409 | ||
54043703 JF |
9410 | } |
9411 | ||
674dce72 GP |
9412 | - (void) cancelAndClear:(bool)clear { |
9413 | @synchronized (self) { | |
9414 | if (clear) { | |
c6ca67ba | 9415 | [database_ clear]; |
b5e7eebb | 9416 | Queuing_ = false; |
674dce72 | 9417 | } else { |
b5e7eebb | 9418 | Queuing_ = true; |
6067f1b8 JF |
9419 | } |
9420 | ||
a4217bbb | 9421 | [self _updateData]; |
674dce72 | 9422 | } |
37d2b2a9 | 9423 | } |
7b0ce2da | 9424 | |
b5e7eebb GP |
9425 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
9426 | NSString *context([alert context]); | |
bc11cf5b | 9427 | |
6915b806 JF |
9428 | if ([context isEqualToString:@"conffile"]) { |
9429 | FILE *input = [database_ input]; | |
9430 | if (button == [alert cancelButtonIndex]) | |
9431 | fprintf(input, "N\n"); | |
9432 | else if (button == [alert firstOtherButtonIndex]) | |
9433 | fprintf(input, "Y\n"); | |
9434 | fflush(input); | |
9435 | ||
9436 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
9437 | } else if ([context isEqualToString:@"fixhalf"]) { | |
efa53fa9 | 9438 | if (button == [alert cancelButtonIndex]) { |
37d2b2a9 | 9439 | @synchronized (self) { |
584daea0 | 9440 | for (Package *broken in (id) broken_) { |
37d2b2a9 | 9441 | [broken remove]; |
37d2b2a9 | 9442 | NSString *id = [broken id]; |
584daea0 JF |
9443 | |
9444 | _root({ | |
9445 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.prerm", id] UTF8String]); | |
9446 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postrm", id] UTF8String]); | |
9447 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.preinst", id] UTF8String]); | |
9448 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postinst", id] UTF8String]); | |
9449 | }); | |
37d2b2a9 | 9450 | } |
7b0ce2da | 9451 | |
37d2b2a9 GP |
9452 | [self resolve]; |
9453 | [self perform]; | |
9454 | } | |
efa53fa9 | 9455 | } else if (button == [alert firstOtherButtonIndex]) { |
37d2b2a9 GP |
9456 | [broken_ removeAllObjects]; |
9457 | [self _loaded]; | |
7b0ce2da JF |
9458 | } |
9459 | ||
37d2b2a9 | 9460 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9461 | } else if ([context isEqualToString:@"upgrade"]) { |
37d2b2a9 GP |
9462 | if (button == [alert firstOtherButtonIndex]) { |
9463 | @synchronized (self) { | |
7b33d201 | 9464 | for (Package *essential in (id) essential_) |
37d2b2a9 | 9465 | [essential install]; |
7b0ce2da | 9466 | |
37d2b2a9 GP |
9467 | [self resolve]; |
9468 | [self perform]; | |
9469 | } | |
9470 | } else if (button == [alert firstOtherButtonIndex] + 1) { | |
9471 | [self distUpgrade]; | |
9472 | } else if (button == [alert cancelButtonIndex]) { | |
9473 | Ignored_ = YES; | |
7b0ce2da JF |
9474 | } |
9475 | ||
37d2b2a9 | 9476 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9477 | } |
7b0ce2da JF |
9478 | } |
9479 | ||
d13edf44 JF |
9480 | - (void) system:(NSString *)command { |
9481 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9482 | ||
985d2dff | 9483 | _trace(); |
0c0a966b | 9484 | system([command UTF8String]); |
985d2dff | 9485 | _trace(); |
d13edf44 JF |
9486 | |
9487 | [pool release]; | |
670a0494 JF |
9488 | } |
9489 | ||
9490 | - (void) applicationWillSuspend { | |
9491 | [database_ clean]; | |
9492 | [super applicationWillSuspend]; | |
bd150f54 JF |
9493 | } |
9494 | ||
235f5487 | 9495 | - (BOOL) isSafeToSuspend { |
5927fe03 JF |
9496 | if (locked_ != 0) { |
9497 | #if !ForRelease | |
9498 | NSLog(@"isSafeToSuspend: locked_ != 0"); | |
9499 | #endif | |
9500 | return false; | |
9501 | } | |
9502 | ||
7187b5b2 JF |
9503 | if ([tabbar_ modalViewController] != nil) |
9504 | return false; | |
9505 | ||
262afe11 GP |
9506 | // Use external process status API internally. |
9507 | // This is probably a really bad idea. | |
235f5487 | 9508 | // XXX: what is the point of this? does this solve anything at all? |
262afe11 GP |
9509 | uint64_t status = 0; |
9510 | int notify_token; | |
9511 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
9512 | notify_get_state(notify_token, &status); | |
9513 | notify_cancel(notify_token); | |
9514 | } | |
9515 | ||
5927fe03 JF |
9516 | if (status != 0) { |
9517 | #if !ForRelease | |
9518 | NSLog(@"isSafeToSuspend: status != 0"); | |
9519 | #endif | |
9520 | return false; | |
9521 | } | |
9522 | ||
9523 | #if !ForRelease | |
9524 | NSLog(@"isSafeToSuspend: -> true"); | |
9525 | #endif | |
9526 | return true; | |
235f5487 JF |
9527 | } |
9528 | ||
216f3dc6 JF |
9529 | - (void) suspendReturningToLastApp:(BOOL)returning { |
9530 | if ([self isSafeToSuspend]) | |
9531 | [super suspendReturningToLastApp:returning]; | |
9532 | } | |
9533 | ||
9534 | - (void) suspend { | |
9535 | if ([self isSafeToSuspend]) | |
9536 | [super suspend]; | |
9537 | } | |
9538 | ||
9539 | - (void) applicationSuspend { | |
9540 | if ([self isSafeToSuspend]) | |
9541 | [super applicationSuspend]; | |
9542 | } | |
9543 | ||
235f5487 JF |
9544 | - (void) applicationSuspend:(__GSEvent *)event { |
9545 | if ([self isSafeToSuspend]) | |
bd150f54 | 9546 | [super applicationSuspend:event]; |
bd150f54 JF |
9547 | } |
9548 | ||
6d9712c4 | 9549 | - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 { |
235f5487 | 9550 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9551 | [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3]; |
9552 | } | |
9553 | ||
9554 | - (void) _setSuspended:(BOOL)value { | |
235f5487 | 9555 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9556 | [super _setSuspended:value]; |
9557 | } | |
9558 | ||
7b0ce2da | 9559 | - (UIProgressHUD *) addProgressHUD { |
8c9453da | 9560 | UIProgressHUD *hud([[[UIProgressHUD alloc] init] autorelease]); |
04fe1349 JF |
9561 | [hud setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
9562 | ||
d061f4ba | 9563 | [window_ setUserInteractionEnabled:NO]; |
534e31fc | 9564 | |
f36e5eac JF |
9565 | UIViewController *target(tabbar_); |
9566 | if (UIViewController *modal = [target modalViewController]) | |
9567 | target = modal; | |
9568 | ||
22b6c4b8 | 9569 | [hud showInView:[target view]]; |
534e31fc | 9570 | |
2ef6faad | 9571 | [self lockSuspend]; |
7b0ce2da JF |
9572 | return hud; |
9573 | } | |
9574 | ||
d061f4ba | 9575 | - (void) removeProgressHUD:(UIProgressHUD *)hud { |
2ef6faad | 9576 | [self unlockSuspend]; |
8c9453da | 9577 | [hud hide]; |
d061f4ba JF |
9578 | [hud removeFromSuperview]; |
9579 | [window_ setUserInteractionEnabled:YES]; | |
9580 | } | |
9581 | ||
f050e4d9 JF |
9582 | - (CyteViewController *) pageForPackage:(NSString *)name withReferrer:(NSString *)referrer { |
9583 | return [[[CYPackageController alloc] initWithDatabase:database_ forPackage:name withReferrer:referrer] autorelease]; | |
c390d3ab JF |
9584 | } |
9585 | ||
f050e4d9 | 9586 | - (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external withReferrer:(NSString *)referrer { |
e47c4742 | 9587 | NSString *scheme([[url scheme] lowercaseString]); |
f6e13561 | 9588 | if ([[url absoluteString] length] <= [scheme length] + 3) |
e47c4742 | 9589 | return nil; |
f6e13561 | 9590 | NSString *path([[url absoluteString] substringFromIndex:[scheme length] + 3]); |
3a159223 | 9591 | NSArray *components([path componentsSeparatedByString:@"/"]); |
f6e13561 | 9592 | |
4fd0c466 | 9593 | if ([scheme isEqualToString:@"apptapp"] && [components count] > 0 && [[components objectAtIndex:0] isEqualToString:@"package"]) { |
f050e4d9 | 9594 | CyteViewController *controller([self pageForPackage:[components objectAtIndex:1] withReferrer:referrer]); |
4fd0c466 JF |
9595 | if (controller != nil) |
9596 | [controller setDelegate:self]; | |
9597 | return controller; | |
9598 | } | |
f6e13561 GP |
9599 | |
9600 | if ([components count] < 1 || ![scheme isEqualToString:@"cydia"]) | |
e47c4742 | 9601 | return nil; |
f6e13561 GP |
9602 | |
9603 | NSString *base([components objectAtIndex:0]); | |
9604 | ||
cd79e8cf | 9605 | CyteViewController *controller = nil; |
f5a17517 | 9606 | |
f70ea899 | 9607 | if ([base isEqualToString:@"url"]) { |
106d645f GP |
9608 | // This kind of URL can contain slashes in the argument, so we can't parse them below. |
9609 | NSString *destination = [[url absoluteString] substringFromIndex:([scheme length] + [@"://" length] + [base length] + [@"/" length])]; | |
a576488f | 9610 | controller = [[[CydiaWebViewController alloc] initWithURL:[NSURL URLWithString:destination]] autorelease]; |
028dbd1c | 9611 | } else if (!external && [components count] == 1) { |
f6e13561 | 9612 | if ([base isEqualToString:@"sources"]) { |
f5a17517 | 9613 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9614 | } |
9615 | ||
9616 | if ([base isEqualToString:@"home"]) { | |
f5a17517 | 9617 | controller = [[[HomeController alloc] init] autorelease]; |
f6e13561 GP |
9618 | } |
9619 | ||
9620 | if ([base isEqualToString:@"sections"]) { | |
55066b9e | 9621 | controller = [[[SectionsController alloc] initWithDatabase:database_ source:nil] autorelease]; |
f6e13561 GP |
9622 | } |
9623 | ||
9624 | if ([base isEqualToString:@"search"]) { | |
43625891 | 9625 | controller = [[[SearchController alloc] initWithDatabase:database_ query:nil] autorelease]; |
f6e13561 GP |
9626 | } |
9627 | ||
9628 | if ([base isEqualToString:@"changes"]) { | |
ea3bb538 | 9629 | controller = [[[ChangesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9630 | } |
9631 | ||
9632 | if ([base isEqualToString:@"installed"]) { | |
f5a17517 | 9633 | controller = [[[InstalledController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9634 | } |
9635 | } else if ([components count] == 2) { | |
8912cff7 | 9636 | NSString *argument = [[components objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
f6e13561 GP |
9637 | |
9638 | if ([base isEqualToString:@"package"]) { | |
f050e4d9 | 9639 | controller = [self pageForPackage:argument withReferrer:referrer]; |
f6e13561 GP |
9640 | } |
9641 | ||
028dbd1c | 9642 | if (!external && [base isEqualToString:@"search"]) { |
8912cff7 | 9643 | controller = [[[SearchController alloc] initWithDatabase:database_ query:argument] autorelease]; |
f6e13561 GP |
9644 | } |
9645 | ||
028dbd1c | 9646 | if (!external && [base isEqualToString:@"sections"]) { |
55066b9e | 9647 | if ([argument isEqualToString:@"all"] || [argument isEqualToString:@"*"]) |
f6e13561 | 9648 | argument = nil; |
55066b9e | 9649 | controller = [[[SectionController alloc] initWithDatabase:database_ source:nil section:argument] autorelease]; |
f6e13561 GP |
9650 | } |
9651 | ||
028dbd1c | 9652 | if (!external && [base isEqualToString:@"sources"]) { |
f6e13561 | 9653 | if ([argument isEqualToString:@"add"]) { |
f5a17517 GP |
9654 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
9655 | [(SourcesController *)controller showAddSourcePrompt]; | |
f6e13561 | 9656 | } else { |
8912cff7 | 9657 | Source *source([database_ sourceWithKey:argument]); |
55066b9e | 9658 | controller = [[[SectionsController alloc] initWithDatabase:database_ source:source] autorelease]; |
f6e13561 GP |
9659 | } |
9660 | } | |
9661 | ||
028dbd1c | 9662 | if (!external && [base isEqualToString:@"launch"]) { |
f6e13561 | 9663 | [self launchApplicationWithIdentifier:argument suspended:NO]; |
f5a17517 | 9664 | return nil; |
f6e13561 | 9665 | } |
028dbd1c | 9666 | } else if (!external && [components count] == 3) { |
8912cff7 JF |
9667 | NSString *arg1 = [[components objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
9668 | NSString *arg2 = [[components objectAtIndex:2] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
f6e13561 GP |
9669 | |
9670 | if ([base isEqualToString:@"package"]) { | |
9671 | if ([arg2 isEqualToString:@"settings"]) { | |
f5a17517 | 9672 | controller = [[[PackageSettingsController alloc] initWithDatabase:database_ package:arg1] autorelease]; |
f6e13561 GP |
9673 | } else if ([arg2 isEqualToString:@"files"]) { |
9674 | if (Package *package = [database_ packageWithName:arg1]) { | |
f5a17517 GP |
9675 | controller = [[[FileTable alloc] initWithDatabase:database_] autorelease]; |
9676 | [(FileTable *)controller setPackage:package]; | |
f6e13561 GP |
9677 | } |
9678 | } | |
c390d3ab | 9679 | } |
55066b9e JF |
9680 | |
9681 | if ([base isEqualToString:@"sections"]) { | |
9682 | Source *source([arg1 isEqualToString:@"*"] ? nil : [database_ sourceWithKey:arg1]); | |
9683 | NSString *section([arg2 isEqualToString:@"*"] ? nil : arg2); | |
9684 | controller = [[[SectionController alloc] initWithDatabase:database_ source:source section:section] autorelease]; | |
9685 | } | |
c390d3ab JF |
9686 | } |
9687 | ||
f5a17517 GP |
9688 | [controller setDelegate:self]; |
9689 | return controller; | |
c390d3ab JF |
9690 | } |
9691 | ||
028dbd1c | 9692 | - (BOOL) openCydiaURL:(NSURL *)url forExternal:(BOOL)external { |
f050e4d9 | 9693 | CyteViewController *page([self pageForURL:url forExternal:external withReferrer:nil]); |
40364973 | 9694 | |
e965092a JF |
9695 | if (page != nil) |
9696 | [tabbar_ setUnselectedViewController:page]; | |
40364973 | 9697 | |
f6e13561 | 9698 | return page != nil; |
40364973 GP |
9699 | } |
9700 | ||
c390d3ab JF |
9701 | - (void) applicationOpenURL:(NSURL *)url { |
9702 | [super applicationOpenURL:url]; | |
d817e4de | 9703 | |
7b33d201 JF |
9704 | if (!loaded_) |
9705 | starturl_ = url; | |
9706 | else | |
9707 | [self openCydiaURL:url forExternal:YES]; | |
c390d3ab JF |
9708 | } |
9709 | ||
bc11cf5b | 9710 | - (void) applicationWillResignActive:(UIApplication *)application { |
7eff7ea6 | 9711 | // Stop refreshing if you get a phone call or lock the device. |
7585ce66 JF |
9712 | if ([tabbar_ updating]) |
9713 | [tabbar_ cancelUpdate]; | |
bc11cf5b | 9714 | |
ca584c15 GP |
9715 | if ([[self superclass] instancesRespondToSelector:@selector(applicationWillResignActive:)]) |
9716 | [super applicationWillResignActive:application]; | |
7eff7ea6 GP |
9717 | } |
9718 | ||
9dd3045d | 9719 | - (void) saveState { |
35f0a3b5 | 9720 | [Metadata_ setObject:[tabbar_ navigationURLCollection] forKey:@"InterfaceState"]; |
fe8e721f GP |
9721 | [Metadata_ setObject:[NSDate date] forKey:@"LastClosed"]; |
9722 | [Metadata_ setObject:[NSNumber numberWithInt:[tabbar_ selectedIndex]] forKey:@"InterfaceIndex"]; | |
9dd3045d | 9723 | Changed_ = true; |
fe8e721f GP |
9724 | |
9725 | [self _saveConfig]; | |
9726 | } | |
9727 | ||
9dd3045d JF |
9728 | - (void) applicationWillTerminate:(UIApplication *)application { |
9729 | [self saveState]; | |
9730 | } | |
9731 | ||
d4011d57 | 9732 | - (void) applicationDidEnterBackground:(UIApplication *)application { |
2acc4fa4 JF |
9733 | if (kCFCoreFoundationVersionNumber < 1000 && [self isSafeToSuspend]) |
9734 | return [self terminateWithSuccess]; | |
d4011d57 JF |
9735 | [self saveState]; |
9736 | } | |
9737 | ||
9738 | - (void) applicationWillEnterForeground:(UIApplication *)application { | |
95cd61f0 JF |
9739 | NSDate *closed = [Metadata_ objectForKey:@"LastClosed"]; |
9740 | if (closed == nil) | |
9741 | return; | |
9742 | ||
9743 | NSTimeInterval interval([closed timeIntervalSinceNow]); | |
95cd61f0 | 9744 | |
6218c58c | 9745 | if (interval <= -(30*60)) { |
888667d5 JF |
9746 | [tabbar_ setSelectedIndex:0]; |
9747 | [[[tabbar_ viewControllers] objectAtIndex:0] popToRootViewControllerAnimated:NO]; | |
9748 | } | |
95cd61f0 | 9749 | |
6218c58c | 9750 | if (interval <= -(15*60)) { |
888667d5 JF |
9751 | if (IsReachable("cydia.saurik.com")) { |
9752 | [tabbar_ beginUpdate]; | |
9753 | [appcache_ reloadURLWithCache:YES]; | |
9754 | } | |
9755 | } | |
d4011d57 JF |
9756 | } |
9757 | ||
6915b806 | 9758 | - (void) setConfigurationData:(NSString *)data { |
b4fd1197 | 9759 | static RegEx conffile_r("'(.*)' '(.*)' ([01]) ([01])"); |
6915b806 JF |
9760 | |
9761 | if (!conffile_r(data)) { | |
9762 | lprintf("E:invalid conffile\n"); | |
9763 | return; | |
9764 | } | |
9765 | ||
9766 | NSString *ofile = conffile_r[1]; | |
9767 | //NSString *nfile = conffile_r[2]; | |
9768 | ||
9769 | UIAlertView *alert = [[[UIAlertView alloc] | |
9770 | initWithTitle:UCLocalize("CONFIGURATION_UPGRADE") | |
9771 | message:[NSString stringWithFormat:@"%@\n\n%@", UCLocalize("CONFIGURATION_UPGRADE_EX"), ofile] | |
9772 | delegate:self | |
9773 | cancelButtonTitle:UCLocalize("KEEP_OLD_COPY") | |
9774 | otherButtonTitles: | |
9775 | UCLocalize("ACCEPT_NEW_COPY"), | |
9776 | // XXX: UCLocalize("SEE_WHAT_CHANGED"), | |
9777 | nil | |
9778 | ] autorelease]; | |
9779 | ||
9780 | [alert setContext:@"conffile"]; | |
a08145a8 | 9781 | [alert setNumberOfRows:2]; |
6915b806 JF |
9782 | [alert show]; |
9783 | } | |
9784 | ||
f3e11d24 | 9785 | - (void) addStashController { |
2ef6faad | 9786 | [self lockSuspend]; |
7b33d201 | 9787 | stash_ = [[[StashController alloc] init] autorelease]; |
f3e11d24 GP |
9788 | [window_ addSubview:[stash_ view]]; |
9789 | } | |
9790 | ||
9791 | - (void) removeStashController { | |
9792 | [[stash_ view] removeFromSuperview]; | |
7b33d201 | 9793 | stash_ = nil; |
2ef6faad | 9794 | [self unlockSuspend]; |
f3e11d24 GP |
9795 | } |
9796 | ||
9797 | - (void) stash { | |
9e1f1e91 | 9798 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; |
f3e11d24 | 9799 | UpdateExternalStatus(1); |
0c0a966b | 9800 | [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/free.sh"]; |
f3e11d24 | 9801 | UpdateExternalStatus(0); |
f3e11d24 GP |
9802 | |
9803 | [self removeStashController]; | |
9804 | ||
d99e3659 JF |
9805 | pid_t pid(ExecFork()); |
9806 | if (pid == 0) { | |
f3e11d24 GP |
9807 | execlp("launchctl", "launchctl", "stop", "com.apple.SpringBoard", NULL); |
9808 | perror("launchctl stop"); | |
d99e3659 | 9809 | |
51c8106a JF |
9810 | exit(0); |
9811 | } ReapZombie(pid); | |
f3e11d24 GP |
9812 | } |
9813 | ||
f6e13561 | 9814 | - (void) setupViewControllers { |
5fe2bcc6 | 9815 | tabbar_ = [[[CydiaTabBarController alloc] initWithDatabase:database_] autorelease]; |
851f4a99 | 9816 | |
6445279b JF |
9817 | NSMutableArray *items; |
9818 | if (kCFCoreFoundationVersionNumber < 800) { | |
9819 | items = [NSMutableArray arrayWithObjects: | |
8dbf3abc JF |
9820 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage imageNamed:@"home.png"] tag:0] autorelease], |
9821 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage imageNamed:@"install.png"] tag:0] autorelease], | |
9822 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage imageNamed:@"changes.png"] tag:0] autorelease], | |
9823 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage imageNamed:@"manage.png"] tag:0] autorelease], | |
9824 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage imageNamed:@"search.png"] tag:0] autorelease], | |
6445279b | 9825 | nil]; |
851f4a99 | 9826 | } else { |
6445279b | 9827 | items = [NSMutableArray arrayWithObjects: |
8dbf3abc JF |
9828 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage imageNamed:@"home7.png"] selectedImage:[UIImage imageNamed:@"home7s.png"]] autorelease], |
9829 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage imageNamed:@"install7.png"] selectedImage:[UIImage imageNamed:@"install7s.png"]] autorelease], | |
9830 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage imageNamed:@"changes7.png"] selectedImage:[UIImage imageNamed:@"changes7s.png"]] autorelease], | |
9831 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage imageNamed:@"manage7.png"] selectedImage:[UIImage imageNamed:@"manage7s.png"]] autorelease], | |
9832 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage imageNamed:@"search7.png"] selectedImage:[UIImage imageNamed:@"search7s.png"]] autorelease], | |
6445279b | 9833 | nil]; |
851f4a99 GP |
9834 | } |
9835 | ||
9836 | NSMutableArray *controllers([NSMutableArray array]); | |
851f4a99 | 9837 | for (UITabBarItem *item in items) { |
15f0d613 | 9838 | UINavigationController *controller([[[UINavigationController alloc] init] autorelease]); |
851f4a99 GP |
9839 | [controller setTabBarItem:item]; |
9840 | [controllers addObject:controller]; | |
9841 | } | |
851f4a99 | 9842 | [tabbar_ setViewControllers:controllers]; |
4305896c | 9843 | |
f6e13561 | 9844 | [tabbar_ setUpdateDelegate:self]; |
851f4a99 GP |
9845 | } |
9846 | ||
968afbcd | 9847 | - (void) _sendMemoryWarningNotification { |
85106ebe JF |
9848 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: maybe 4_0? |
9849 | [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification" object:[UIApplication sharedApplication]]; | |
9850 | else | |
9851 | [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidReceiveMemoryWarningNotification" object:[UIApplication sharedApplication]]; | |
968afbcd JF |
9852 | } |
9853 | ||
9854 | - (void) _sendMemoryWarningNotifications { | |
9855 | while (true) { | |
9856 | [self performSelectorOnMainThread:@selector(_sendMemoryWarningNotification) withObject:nil waitUntilDone:NO]; | |
740f9f75 JF |
9857 | sleep(2); |
9858 | //usleep(2000000); | |
968afbcd JF |
9859 | } |
9860 | } | |
9861 | ||
9256a1ce JF |
9862 | - (void) applicationDidReceiveMemoryWarning:(UIApplication *)application { |
9863 | NSLog(@"--"); | |
9864 | [[NSURLCache sharedURLCache] removeAllCachedResponses]; | |
9865 | } | |
9866 | ||
bd150f54 | 9867 | - (void) applicationDidFinishLaunching:(id)unused { |
968afbcd JF |
9868 | //[NSThread detachNewThreadSelector:@selector(_sendMemoryWarningNotifications) toTarget:self withObject:nil]; |
9869 | ||
7e30ba6d | 9870 | _trace(); |
1e94d48b JF |
9871 | if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) |
9872 | [self setApplicationSupportsShakeToEdit:NO]; | |
9873 | ||
48f1762f JF |
9874 | @synchronized (HostConfig_) { |
9875 | [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]]; | |
9876 | } | |
3171f7fe | 9877 | |
4058b165 JF |
9878 | [NSURLCache setSharedURLCache:[[[CYURLCache alloc] |
9879 | initWithMemoryCapacity:524288 | |
9880 | diskCapacity:10485760 | |
b44af625 | 9881 | diskPath:Cache("SDURLCache") |
4058b165 | 9882 | ] autorelease]]; |
71cc7be1 | 9883 | |
a576488f | 9884 | [CydiaWebViewController _initialize]; |
ea173384 | 9885 | |
bfc87a4d JF |
9886 | [NSURLProtocol registerClass:[CydiaURLProtocol class]]; |
9887 | ||
793aee35 JF |
9888 | // this would disallow http{,s} URLs from accessing this data |
9889 | //[WebView registerURLSchemeAsLocal:@"cydia"]; | |
9890 | ||
7b33d201 JF |
9891 | Font12_ = [UIFont systemFontOfSize:12]; |
9892 | Font12Bold_ = [UIFont boldSystemFontOfSize:12]; | |
9893 | Font14_ = [UIFont systemFontOfSize:14]; | |
2cdc6e57 | 9894 | Font18_ = [UIFont systemFontOfSize:18]; |
7b33d201 JF |
9895 | Font18Bold_ = [UIFont boldSystemFontOfSize:18]; |
9896 | Font22Bold_ = [UIFont boldSystemFontOfSize:22]; | |
f641a0e5 | 9897 | |
7b33d201 JF |
9898 | essential_ = [NSMutableArray arrayWithCapacity:4]; |
9899 | broken_ = [NSMutableArray arrayWithCapacity:4]; | |
bd150f54 | 9900 | |
4e89e880 | 9901 | // XXX: I really need this thing... like, seriously... I'm sorry |
fedd38fe JF |
9902 | appcache_ = [[[AppCacheController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/appcache/", UI_]]] autorelease]; |
9903 | [appcache_ reloadData]; | |
4e89e880 | 9904 | |
7b33d201 | 9905 | window_ = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
f641a0e5 JF |
9906 | [window_ orderFront:self]; |
9907 | [window_ makeKey:self]; | |
c390d3ab | 9908 | [window_ setHidden:NO]; |
04fe1349 | 9909 | |
96ed699d | 9910 | if (false) stash: { |
f3e11d24 | 9911 | [self addStashController]; |
3931b718 JF |
9912 | // XXX: this would be much cleaner as a yieldToSelector: |
9913 | // that way the removeStashController could happen right here inline | |
9914 | // we also could no longer require the useless stash_ field anymore | |
f3e11d24 GP |
9915 | [self performSelector:@selector(stash) withObject:nil afterDelay:0]; |
9916 | return; | |
9917 | } | |
9918 | ||
2656fd54 JF |
9919 | struct stat root; |
9920 | int error(stat("/", &root)); | |
9921 | _assert(error != -1); | |
9922 | ||
96ed699d | 9923 | #define Stash_(path) do { \ |
2656fd54 JF |
9924 | struct stat folder; \ |
9925 | int error(lstat((path), &folder)); \ | |
9926 | if (error != -1 && ( \ | |
9927 | folder.st_dev == root.st_dev && \ | |
9928 | S_ISDIR(folder.st_mode) \ | |
9929 | ) || error == -1 && ( \ | |
9930 | errno == ENOENT || \ | |
9931 | errno == ENOTDIR \ | |
9932 | )) goto stash; \ | |
96ed699d JF |
9933 | } while (false) |
9934 | ||
40bb5513 | 9935 | Stash_("/Applications"); |
96ed699d JF |
9936 | Stash_("/Library/Ringtones"); |
9937 | Stash_("/Library/Wallpaper"); | |
9938 | //Stash_("/usr/bin"); | |
9939 | Stash_("/usr/include"); | |
96ed699d JF |
9940 | Stash_("/usr/share"); |
9941 | //Stash_("/var/lib"); | |
9942 | ||
770f2a8e | 9943 | database_ = [Database sharedInstance]; |
6915b806 | 9944 | [database_ setDelegate:self]; |
bfc87a4d | 9945 | |
89571a5b | 9946 | [window_ setUserInteractionEnabled:NO]; |
0be165c8 | 9947 | [self setupViewControllers]; |
6915b806 | 9948 | |
9549563e JF |
9949 | CydiaLoadingViewController *loading([[[CydiaLoadingViewController alloc] init] autorelease]); |
9950 | UINavigationController *navigation([[[UINavigationController alloc] init] autorelease]); | |
9951 | [navigation setViewControllers:[NSArray arrayWithObject:loading]]; | |
9952 | ||
9953 | emulated_ = [[[CyteTabBarController alloc] init] autorelease]; | |
9954 | [emulated_ setViewControllers:[NSArray arrayWithObject:navigation]]; | |
9955 | [emulated_ setSelectedIndex:0]; | |
1c28763e JF |
9956 | |
9957 | if ([emulated_ respondsToSelector:@selector(concealTabBarSelection)]) | |
9958 | [emulated_ concealTabBarSelection]; | |
9549563e | 9959 | |
81628115 JF |
9960 | if ([window_ respondsToSelector:@selector(setRootViewController:)]) |
9961 | [window_ setRootViewController:emulated_]; | |
237c3d1c JF |
9962 | else |
9963 | [window_ addSubview:[emulated_ view]]; | |
5ccb47d8 | 9964 | |
fed0d010 | 9965 | [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; |
c626a63f | 9966 | _trace(); |
fed0d010 JF |
9967 | } |
9968 | ||
d3a28a81 GP |
9969 | - (NSArray *) defaultStartPages { |
9970 | NSMutableArray *standard = [NSMutableArray array]; | |
9971 | [standard addObject:[NSArray arrayWithObject:@"cydia://home"]]; | |
55066b9e | 9972 | [standard addObject:[NSArray arrayWithObject:@"cydia://sources"]]; |
d3a28a81 | 9973 | [standard addObject:[NSArray arrayWithObject:@"cydia://changes"]]; |
55066b9e | 9974 | [standard addObject:[NSArray arrayWithObject:@"cydia://installed"]]; |
d3a28a81 GP |
9975 | [standard addObject:[NSArray arrayWithObject:@"cydia://search"]]; |
9976 | return standard; | |
9977 | } | |
9978 | ||
fed0d010 | 9979 | - (void) loadData { |
c626a63f | 9980 | _trace(); |
4121c5e0 JF |
9981 | if ([emulated_ modalViewController] != nil) |
9982 | [emulated_ dismissModalViewControllerAnimated:YES]; | |
9983 | [window_ setUserInteractionEnabled:NO]; | |
fed0d010 | 9984 | |
2925cbba JF |
9985 | [self reloadDataWithInvocation:nil]; |
9986 | [self refreshIfPossible]; | |
2e9123cb | 9987 | [self disemulate]; |
5ccb47d8 | 9988 | |
d3a28a81 | 9989 | int savedIndex = [[Metadata_ objectForKey:@"InterfaceIndex"] intValue]; |
0c4fe0f4 | 9990 | NSArray *saved = [[[Metadata_ objectForKey:@"InterfaceState"] mutableCopy] autorelease]; |
d3a28a81 GP |
9991 | int standardIndex = 0; |
9992 | NSArray *standard = [self defaultStartPages]; | |
fe8e721f | 9993 | |
d3a28a81 GP |
9994 | BOOL valid = YES; |
9995 | ||
9996 | if (saved == nil) | |
9997 | valid = NO; | |
9998 | ||
9999 | NSDate *closed = [Metadata_ objectForKey:@"LastClosed"]; | |
10000 | if (valid && closed != nil) { | |
fe8e721f | 10001 | NSTimeInterval interval([closed timeIntervalSinceNow]); |
ade2267f | 10002 | if (interval <= -(30*60)) |
d3a28a81 | 10003 | valid = NO; |
fe8e721f GP |
10004 | } |
10005 | ||
d3a28a81 GP |
10006 | if (valid && [saved count] != [standard count]) |
10007 | valid = NO; | |
efa53fa9 | 10008 | |
d3a28a81 GP |
10009 | if (valid) { |
10010 | for (unsigned int i = 0; i < [standard count]; i++) { | |
10011 | NSArray *std = [standard objectAtIndex:i], *sav = [saved objectAtIndex:i]; | |
10012 | // XXX: The "hasPrefix" sanity check here could be, in theory, fooled, | |
10013 | // but it's good enough for now. | |
10014 | if ([sav count] == 0 || ![[sav objectAtIndex:0] hasPrefix:[std objectAtIndex:0]]) { | |
10015 | valid = NO; | |
10016 | break; | |
10017 | } | |
fe8e721f | 10018 | } |
fe8e721f GP |
10019 | } |
10020 | ||
d3a28a81 GP |
10021 | NSArray *items = nil; |
10022 | if (valid) { | |
10023 | [tabbar_ setSelectedIndex:savedIndex]; | |
10024 | items = saved; | |
10025 | } else { | |
10026 | [tabbar_ setSelectedIndex:standardIndex]; | |
10027 | items = standard; | |
10028 | } | |
10029 | ||
fe8e721f GP |
10030 | for (unsigned int tab = 0; tab < [[tabbar_ viewControllers] count]; tab++) { |
10031 | NSArray *stack = [items objectAtIndex:tab]; | |
15f0d613 | 10032 | UINavigationController *navigation = [[tabbar_ viewControllers] objectAtIndex:tab]; |
fe8e721f GP |
10033 | NSMutableArray *current = [NSMutableArray array]; |
10034 | ||
10035 | for (unsigned int nav = 0; nav < [stack count]; nav++) { | |
10036 | NSString *addr = [stack objectAtIndex:nav]; | |
10037 | NSURL *url = [NSURL URLWithString:addr]; | |
f050e4d9 | 10038 | CyteViewController *page = [self pageForURL:url forExternal:NO withReferrer:nil]; |
fe8e721f GP |
10039 | if (page != nil) |
10040 | [current addObject:page]; | |
10041 | } | |
10042 | ||
10043 | [navigation setViewControllers:current]; | |
10044 | } | |
f6e13561 | 10045 | |
89571a5b | 10046 | // (Try to) show the startup URL. |
f6e13561 | 10047 | if (starturl_ != nil) { |
f14bba69 | 10048 | [self openCydiaURL:starturl_ forExternal:YES]; |
f6e13561 GP |
10049 | starturl_ = nil; |
10050 | } | |
bd150f54 JF |
10051 | } |
10052 | ||
b5e7eebb | 10053 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { |
674dce72 | 10054 | if (item != nil && IsWildcat_) { |
b5e7eebb | 10055 | [sheet showFromBarButtonItem:item animated:YES]; |
674dce72 GP |
10056 | } else { |
10057 | [sheet showInView:window_]; | |
10058 | } | |
36bb2ca2 JF |
10059 | } |
10060 | ||
6915b806 JF |
10061 | - (void) addProgressEvent:(CydiaProgressEvent *)event forTask:(NSString *)task { |
10062 | id<ProgressDelegate> progress([database_ progressDelegate] ?: [self invokeNewProgress:nil forController:nil withTitle:task]); | |
10063 | [progress setTitle:task]; | |
10064 | [progress addProgressEvent:event]; | |
10065 | } | |
10066 | ||
10067 | - (void) addProgressEventForTask:(NSArray *)data { | |
10068 | CydiaProgressEvent *event([data objectAtIndex:0]); | |
10069 | NSString *task([data count] < 2 ? nil : [data objectAtIndex:1]); | |
10070 | [self addProgressEvent:event forTask:task]; | |
10071 | } | |
10072 | ||
10073 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task { | |
10074 | [self performSelectorOnMainThread:@selector(addProgressEventForTask:) withObject:[NSArray arrayWithObjects:event, task, nil] waitUntilDone:YES]; | |
10075 | } | |
10076 | ||
dc5812ec JF |
10077 | @end |
10078 | ||
b4d89997 JF |
10079 | /*IMP alloc_; |
10080 | id Alloc_(id self, SEL selector) { | |
10081 | id object = alloc_(self, selector); | |
c390d3ab | 10082 | lprintf("[%s]A-%p\n", self->isa->name, object); |
b4d89997 JF |
10083 | return object; |
10084 | }*/ | |
10085 | ||
36bb2ca2 JF |
10086 | /*IMP dealloc_; |
10087 | id Dealloc_(id self, SEL selector) { | |
10088 | id object = dealloc_(self, selector); | |
c390d3ab | 10089 | lprintf("[%s]D-%p\n", self->isa->name, object); |
36bb2ca2 JF |
10090 | return object; |
10091 | }*/ | |
b4d89997 | 10092 | |
30c5be06 JF |
10093 | Class $NSURLConnection; |
10094 | ||
10095 | MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) { | |
0c4fe0f4 | 10096 | NSMutableURLRequest *copy([[request mutableCopy] autorelease]); |
30c5be06 JF |
10097 | |
10098 | NSURL *url([copy URL]); | |
5e845121 | 10099 | |
30c5be06 | 10100 | NSString *host([url host]); |
e4b48f2f JF |
10101 | NSString *scheme([[url scheme] lowercaseString]); |
10102 | ||
10103 | NSString *compound([NSString stringWithFormat:@"%@:%@", scheme, host]); | |
30c5be06 | 10104 | |
48f1762f JF |
10105 | @synchronized (HostConfig_) { |
10106 | if ([copy respondsToSelector:@selector(setHTTPShouldUsePipelining:)]) | |
10107 | if ([PipelinedHosts_ containsObject:host] || [PipelinedHosts_ containsObject:compound]) | |
10108 | [copy setHTTPShouldUsePipelining:YES]; | |
5e845121 JF |
10109 | |
10110 | if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"]) | |
10111 | if ([control isEqualToString:@"max-age=0"]) | |
3a23d36e | 10112 | if ([CachedURLs_ containsObject:url]) { |
5e845121 | 10113 | #if !ForRelease |
3a23d36e | 10114 | NSLog(@"~~~: %@", url); |
5e845121 JF |
10115 | #endif |
10116 | ||
10117 | [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; | |
10118 | ||
10119 | [copy setValue:nil forHTTPHeaderField:@"Cache-Control"]; | |
10120 | [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"]; | |
10121 | [copy setValue:nil forHTTPHeaderField:@"If-None-Match"]; | |
10122 | } | |
48f1762f | 10123 | } |
30c5be06 JF |
10124 | |
10125 | if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) { | |
10126 | } return self; | |
10127 | } | |
10128 | ||
b1497b56 JF |
10129 | Class $WAKWindow; |
10130 | ||
4cc9e99a | 10131 | static CGSize $WAKWindow$screenSize(WAKWindow *self, SEL _cmd) { |
b1497b56 JF |
10132 | CGSize size([[UIScreen mainScreen] bounds].size); |
10133 | /*if ([$WAKWindow respondsToSelector:@selector(hasLandscapeOrientation)]) | |
10134 | if ([$WAKWindow hasLandscapeOrientation]) | |
10135 | std::swap(size.width, size.height);*/ | |
10136 | return size; | |
438d6708 JF |
10137 | } |
10138 | ||
29cbf4e5 JF |
10139 | Class $NSUserDefaults; |
10140 | ||
10141 | MSHook(id, NSUserDefaults$objectForKey$, NSUserDefaults *self, SEL _cmd, NSString *key) { | |
10142 | if ([key respondsToSelector:@selector(isEqualToString:)] && [key isEqualToString:@"WebKitLocalStorageDatabasePathPreferenceKey"]) | |
b44af625 | 10143 | return Cache("LocalStorage"); |
29cbf4e5 JF |
10144 | return _NSUserDefaults$objectForKey$(self, _cmd, key); |
10145 | } | |
10146 | ||
d13edf44 | 10147 | int main(int argc, char *argv[]) { |
34ac1598 | 10148 | setreugid(501, 501); |
b44af625 | 10149 | |
d13edf44 JF |
10150 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
10151 | ||
d6dad1b4 | 10152 | _trace(); |
f79a4512 | 10153 | |
9a60abe5 JF |
10154 | UpdateExternalStatus(0); |
10155 | ||
57e8b225 JF |
10156 | UIScreen *screen([UIScreen mainScreen]); |
10157 | if ([screen respondsToSelector:@selector(scale)]) | |
10158 | ScreenScale_ = [screen scale]; | |
10159 | else | |
10160 | ScreenScale_ = 1; | |
10161 | ||
c138614d | 10162 | UIDevice *device([UIDevice currentDevice]); |
08157100 | 10163 | if ([device respondsToSelector:@selector(userInterfaceIdiom)]) { |
c138614d | 10164 | UIUserInterfaceIdiom idiom([device userInterfaceIdiom]); |
08157100 JF |
10165 | if (idiom == UIUserInterfaceIdiomPad) |
10166 | IsWildcat_ = true; | |
c138614d JF |
10167 | } |
10168 | ||
08157100 JF |
10169 | Idiom_ = IsWildcat_ ? @"ipad" : @"iphone"; |
10170 | ||
b4fd1197 | 10171 | RegEx pattern("([0-9]+\\.[0-9]+).*"); |
fd825a2d | 10172 | |
9a4a4754 JF |
10173 | if (pattern([device systemVersion])) |
10174 | Firmware_ = pattern[1]; | |
fd825a2d JF |
10175 | if (pattern(Cydia_)) |
10176 | Major_ = pattern[1]; | |
9a4a4754 | 10177 | |
7b33d201 | 10178 | SessionData_ = [NSMutableDictionary dictionaryWithCapacity:4]; |
ef974f52 | 10179 | |
7b33d201 | 10180 | HostConfig_ = [[[NSObject alloc] init] autorelease]; |
48f1762f JF |
10181 | @synchronized (HostConfig_) { |
10182 | BridgedHosts_ = [NSMutableSet setWithCapacity:4]; | |
247bedb6 | 10183 | TokenHosts_ = [NSMutableSet setWithCapacity:4]; |
2e1652a9 | 10184 | InsecureHosts_ = [NSMutableSet setWithCapacity:4]; |
48f1762f | 10185 | PipelinedHosts_ = [NSMutableSet setWithCapacity:4]; |
5e845121 | 10186 | CachedURLs_ = [NSMutableSet setWithCapacity:32]; |
48f1762f | 10187 | } |
5df7ecfb | 10188 | |
de595d91 JF |
10189 | NSString *ui(@"ui/ios"); |
10190 | if (Idiom_ != nil) | |
10191 | ui = [ui stringByAppendingString:[NSString stringWithFormat:@"~%@", Idiom_]]; | |
fd825a2d | 10192 | ui = [ui stringByAppendingString:[NSString stringWithFormat:@"/%@", Major_]]; |
de595d91 | 10193 | UI_ = CydiaURL(ui); |
57e8b225 | 10194 | |
df213583 | 10195 | PackageName = reinterpret_cast<CYString &(*)(Package *, SEL)>(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); |
677b8415 | 10196 | |
7376b55c JF |
10197 | /* Library Hacks {{{ */ |
10198 | class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); | |
10199 | ||
b1497b56 JF |
10200 | $WAKWindow = objc_getClass("WAKWindow"); |
10201 | if ($WAKWindow != NULL) | |
438d6708 JF |
10202 | if (Method method = class_getInstanceMethod($WAKWindow, @selector(screenSize))) |
10203 | method_setImplementation(method, (IMP) &$WAKWindow$screenSize); | |
10204 | ||
30c5be06 JF |
10205 | $NSURLConnection = objc_getClass("NSURLConnection"); |
10206 | Method NSURLConnection$init$(class_getInstanceMethod($NSURLConnection, @selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:))); | |
10207 | if (NSURLConnection$init$ != NULL) { | |
10208 | _NSURLConnection$init$ = reinterpret_cast<id (*)(NSURLConnection *, SEL, NSURLRequest *, id, BOOL, int64_t, BOOL, NSDictionary *)>(method_getImplementation(NSURLConnection$init$)); | |
10209 | method_setImplementation(NSURLConnection$init$, reinterpret_cast<IMP>(&$NSURLConnection$init$)); | |
10210 | } | |
29cbf4e5 JF |
10211 | |
10212 | $NSUserDefaults = objc_getClass("NSUserDefaults"); | |
10213 | Method NSUserDefaults$objectForKey$(class_getInstanceMethod($NSUserDefaults, @selector(objectForKey:))); | |
10214 | if (NSUserDefaults$objectForKey$ != NULL) { | |
10215 | _NSUserDefaults$objectForKey$ = reinterpret_cast<id (*)(NSUserDefaults *, SEL, NSString *)>(method_getImplementation(NSUserDefaults$objectForKey$)); | |
10216 | method_setImplementation(NSUserDefaults$objectForKey$, reinterpret_cast<IMP>(&$NSUserDefaults$objectForKey$)); | |
10217 | } | |
7376b55c JF |
10218 | /* }}} */ |
10219 | /* Set Locale {{{ */ | |
f79a4512 | 10220 | Locale_ = CFLocaleCopyCurrent(); |
b1ce61ec | 10221 | Languages_ = [NSLocale preferredLanguages]; |
25fdc941 | 10222 | |
b1ce61ec | 10223 | //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); |
18876387 | 10224 | //NSLog(@"%@", [Languages_ description]); |
78430d06 | 10225 | |
b1ce61ec | 10226 | const char *lang; |
25fdc941 JF |
10227 | if (Locale_ != NULL) |
10228 | lang = [(NSString *) CFLocaleGetIdentifier(Locale_) UTF8String]; | |
3caee0a4 JF |
10229 | else if (Languages_ != nil && [Languages_ count] != 0) |
10230 | lang = [[Languages_ objectAtIndex:0] UTF8String]; | |
10231 | else | |
78430d06 | 10232 | // XXX: consider just setting to C and then falling through? |
b1ce61ec | 10233 | lang = NULL; |
3caee0a4 JF |
10234 | |
10235 | if (lang != NULL) { | |
b4fd1197 | 10236 | RegEx pattern("([a-z][a-z])(?:-[A-Za-z]*)?(_[A-Z][A-Z])?"); |
3caee0a4 | 10237 | lang = !pattern(lang) ? NULL : [pattern->*@"%1$@%2$@" UTF8String]; |
78430d06 JF |
10238 | } |
10239 | ||
b1ce61ec | 10240 | NSLog(@"Setting Language: %s", lang); |
3caee0a4 JF |
10241 | |
10242 | if (lang != NULL) { | |
10243 | setenv("LANG", lang, true); | |
10244 | std::setlocale(LC_ALL, lang); | |
10245 | } | |
7376b55c | 10246 | /* }}} */ |
aeeb755b | 10247 | /* Index Collation {{{ */ |
42e25bc5 | 10248 | if (Class $UILocalizedIndexedCollation = objc_getClass("UILocalizedIndexedCollation")) { @try { |
aeeb755b JF |
10249 | NSBundle *bundle([NSBundle bundleForClass:$UILocalizedIndexedCollation]); |
10250 | NSString *path([bundle pathForResource:@"UITableViewLocalizedSectionIndex" ofType:@"plist"]); | |
10251 | //path = @"/System/Library/Frameworks/UIKit.framework/.lproj/UITableViewLocalizedSectionIndex.plist"; | |
10252 | NSDictionary *dictionary([NSDictionary dictionaryWithContentsOfFile:path]); | |
1c28763e | 10253 | _H<UILocalizedIndexedCollation> collation([[[$UILocalizedIndexedCollation alloc] initWithDictionary:dictionary] autorelease]); |
aeeb755b JF |
10254 | |
10255 | CollationLocale_ = MSHookIvar<NSLocale *>(collation, "_locale"); | |
10256 | ||
f93f4997 JF |
10257 | if (kCFCoreFoundationVersionNumber >= 800 && [[CollationLocale_ localeIdentifier] isEqualToString:@"zh@collation=stroke"]) { |
10258 | CollationThumbs_ = [NSArray arrayWithObjects:@"1",@"•",@"4",@"•",@"7",@"•",@"10",@"•",@"13",@"•",@"16",@"•",@"19",@"A",@"•",@"E",@"•",@"I",@"•",@"M",@"•",@"R",@"•",@"V",@"•",@"Z",@"#",nil]; | |
10259 | for (NSInteger offset : (NSInteger[]) {0,1,3,4,6,7,9,10,12,13,15,16,18,25,26,29,30,33,34,37,38,42,43,46,47,50,51}) | |
10260 | CollationOffset_.push_back(offset); | |
10261 | CollationTitles_ = [NSArray arrayWithObjects:@"1 畫",@"2 畫",@"3 畫",@"4 畫",@"5 畫",@"6 畫",@"7 畫",@"8 畫",@"9 畫",@"10 畫",@"11 畫",@"12 畫",@"13 畫",@"14 畫",@"15 畫",@"16 畫",@"17 畫",@"18 畫",@"19 畫",@"20 畫",@"21 畫",@"22 畫",@"23 畫",@"24 畫",@"25 畫以上",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; | |
10262 | CollationStarts_ = [NSArray arrayWithObjects:@"一",@"丁",@"丈",@"不",@"且",@"丞",@"串",@"並",@"亭",@"乘",@"乾",@"傀",@"亂",@"僎",@"僵",@"儐",@"償",@"叢",@"儳",@"嚴",@"儷",@"儻",@"囌",@"囑",@"廳",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",@"ʒ",nil]; | |
10263 | } else { | |
10264 | ||
aeeb755b JF |
10265 | CollationThumbs_ = [collation sectionIndexTitles]; |
10266 | for (size_t index(0), end([CollationThumbs_ count]); index != end; ++index) | |
10267 | CollationOffset_.push_back([collation sectionForSectionIndexTitleAtIndex:index]); | |
10268 | ||
10269 | CollationTitles_ = [collation sectionTitles]; | |
10270 | CollationStarts_ = MSHookIvar<NSArray *>(collation, "_sectionStartStrings"); | |
10271 | ||
22fd24dd JF |
10272 | NSString *&transform(MSHookIvar<NSString *>(collation, "_transform")); |
10273 | if (&transform != NULL && transform != nil) { | |
10274 | /*if ([collation respondsToSelector:@selector(transformedCollationStringForString:)]) | |
10275 | CollationModify_ = [=](NSString *value) { return [collation transformedCollationStringForString:value]; };*/ | |
10276 | const UChar *uid(reinterpret_cast<const UChar *>([transform cStringUsingEncoding:NSUnicodeStringEncoding])); | |
10277 | UErrorCode code(U_ZERO_ERROR); | |
10278 | CollationTransl_ = utrans_openU(uid, -1, UTRANS_FORWARD, NULL, 0, NULL, &code); | |
10279 | if (!U_SUCCESS(code)) | |
10280 | NSLog(@"%s", u_errorName(code)); | |
10281 | } | |
f93f4997 JF |
10282 | |
10283 | } | |
42e25bc5 JF |
10284 | } @catch (NSException *e) { |
10285 | NSLog(@"%@", e); | |
10286 | goto hard; | |
10287 | } } else hard: { | |
aeeb755b JF |
10288 | CollationLocale_ = [[[NSLocale alloc] initWithLocaleIdentifier:@"en@collation=dictionary"] autorelease]; |
10289 | ||
10290 | CollationThumbs_ = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; | |
10291 | for (NSInteger offset(0); offset != 28; ++offset) | |
10292 | CollationOffset_.push_back(offset); | |
10293 | ||
10294 | CollationTitles_ = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"#",nil]; | |
10295 | CollationStarts_ = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",@"ʒ",nil]; | |
10296 | } | |
10297 | /* }}} */ | |
7376b55c | 10298 | /* Parse Arguments {{{ */ |
de3b1ab4 JF |
10299 | bool substrate(false); |
10300 | ||
10301 | if (argc != 0) { | |
10302 | char **args(argv); | |
10303 | int arge(1); | |
10304 | ||
10305 | for (int argi(1); argi != argc; ++argi) | |
10306 | if (strcmp(argv[argi], "--") == 0) { | |
10307 | arge = argi; | |
10308 | argv[argi] = argv[0]; | |
10309 | argv += argi; | |
10310 | argc -= argi; | |
10311 | break; | |
10312 | } | |
10313 | ||
10314 | for (int argi(1); argi != arge; ++argi) | |
d791dce4 | 10315 | if (strcmp(args[argi], "--substrate") == 0) |
de3b1ab4 JF |
10316 | substrate = true; |
10317 | else | |
10318 | fprintf(stderr, "unknown argument: %s\n", args[argi]); | |
10319 | } | |
7376b55c | 10320 | /* }}} */ |
d73cede2 | 10321 | |
7376b55c | 10322 | App_ = [[NSBundle mainBundle] bundlePath]; |
d791dce4 | 10323 | Advanced_ = YES; |
7376b55c | 10324 | |
b44af625 | 10325 | Cache_ = [[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia", @"/var/mobile"] retain]; |
d1c7f1fd | 10326 | |
b4d89997 JF |
10327 | /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); |
10328 | alloc_ = alloc->method_imp; | |
10329 | alloc->method_imp = (IMP) &Alloc_;*/ | |
10330 | ||
36bb2ca2 JF |
10331 | /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc)); |
10332 | dealloc_ = dealloc->method_imp; | |
10333 | dealloc->method_imp = (IMP) &Dealloc_;*/ | |
10334 | ||
7c80833f JF |
10335 | void *gestalt(dlopen("/usr/lib/libMobileGestalt.dylib", RTLD_GLOBAL | RTLD_LAZY)); |
10336 | $MGCopyAnswer = reinterpret_cast<CFStringRef (*)(CFStringRef)>(dlsym(gestalt, "MGCopyAnswer")); | |
10337 | ||
d791dce4 | 10338 | /* System Information {{{ */ |
3178d79b | 10339 | size_t size; |
c390d3ab JF |
10340 | |
10341 | int maxproc; | |
10342 | size = sizeof(maxproc); | |
10343 | if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) | |
10344 | perror("sysctlbyname(\"kern.maxproc\", ?)"); | |
10345 | else if (maxproc < 64) { | |
10346 | maxproc = 64; | |
10347 | if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) | |
10348 | perror("sysctlbyname(\"kern.maxproc\", #)"); | |
10349 | } | |
10350 | ||
bfc87a4d JF |
10351 | sysctlbyname("kern.osversion", NULL, &size, NULL, 0); |
10352 | char *osversion = new char[size]; | |
10353 | if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1) | |
10354 | perror("sysctlbyname(\"kern.osversion\", ?)"); | |
10355 | else | |
10356 | System_ = [NSString stringWithUTF8String:osversion]; | |
10357 | ||
3178d79b JF |
10358 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
10359 | char *machine = new char[size]; | |
c390d3ab JF |
10360 | if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1) |
10361 | perror("sysctlbyname(\"hw.machine\", ?)"); | |
10362 | else | |
10363 | Machine_ = machine; | |
3178d79b | 10364 | |
8dcc32d4 | 10365 | int64_t usermem(0); |
ce1901de JF |
10366 | size = sizeof(usermem); |
10367 | if (sysctlbyname("hw.usermem", &usermem, &size, NULL, 0) == -1) | |
10368 | usermem = 0; | |
10369 | ||
c31d7cdc JF |
10370 | SerialNumber_ = (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber"); |
10371 | ChipID_ = [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString]; | |
10372 | BBSNum_ = CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false); | |
59dbe296 | 10373 | |
7c80833f | 10374 | UniqueID_ = UniqueIdentifier(device); |
3178d79b | 10375 | |
3e9c9e85 JF |
10376 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) { |
10377 | Product_ = [info objectForKey:@"SafariProductVersion"]; | |
10378 | Safari_ = [info objectForKey:@"CFBundleVersion"]; | |
10379 | } | |
e967efd5 | 10380 | |
dd58e110 | 10381 | NSString *agent([NSString stringWithFormat:@"Cydia/%@ CyF/%.2f", Cydia_, kCFCoreFoundationVersionNumber]); |
e967efd5 | 10382 | |
b4fd1197 JF |
10383 | if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Safari_)) |
10384 | agent = [NSString stringWithFormat:@"Safari/%@ %@", match[1], agent]; | |
10385 | if (RegEx match = RegEx("([0-9]+[A-Z][0-9]+[a-z]?).*", System_)) | |
10386 | agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[1], agent]; | |
10387 | if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Product_)) | |
10388 | agent = [NSString stringWithFormat:@"Version/%@ %@", match[1], agent]; | |
e967efd5 JF |
10389 | |
10390 | UserAgent_ = agent; | |
d791dce4 | 10391 | /* }}} */ |
7376b55c | 10392 | /* Load Database {{{ */ |
d6dad1b4 | 10393 | _trace(); |
f79a4512 JF |
10394 | Metadata_ = [[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]; |
10395 | _trace(); | |
10396 | SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; | |
d6dad1b4 JF |
10397 | |
10398 | if (Metadata_ == NULL) | |
f79a4512 | 10399 | Metadata_ = [NSMutableDictionary dictionaryWithCapacity:2]; |
6d9712c4 | 10400 | else { |
2bdd73bd | 10401 | Settings_ = [Metadata_ objectForKey:@"Settings"]; |
7b0ce2da | 10402 | |
b4d89997 | 10403 | Packages_ = [Metadata_ objectForKey:@"Packages"]; |
b3c8e69c JF |
10404 | |
10405 | Values_ = [Metadata_ objectForKey:@"Values"]; | |
6d9712c4 | 10406 | Sections_ = [Metadata_ objectForKey:@"Sections"]; |
7b0ce2da | 10407 | Sources_ = [Metadata_ objectForKey:@"Sources"]; |
ef055c6c JF |
10408 | |
10409 | Token_ = [Metadata_ objectForKey:@"Token"]; | |
33e30380 JF |
10410 | |
10411 | Version_ = [Metadata_ objectForKey:@"Version"]; | |
7b0ce2da JF |
10412 | } |
10413 | ||
b3c8e69c JF |
10414 | if (Values_ == nil) { |
10415 | Values_ = [[[NSMutableDictionary alloc] initWithCapacity:4] autorelease]; | |
10416 | [Metadata_ setObject:Values_ forKey:@"Values"]; | |
10417 | } | |
10418 | ||
7b0ce2da JF |
10419 | if (Sections_ == nil) { |
10420 | Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; | |
10421 | [Metadata_ setObject:Sections_ forKey:@"Sections"]; | |
10422 | } | |
10423 | ||
10424 | if (Sources_ == nil) { | |
10425 | Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; | |
10426 | [Metadata_ setObject:Sources_ forKey:@"Sources"]; | |
6d9712c4 | 10427 | } |
33e30380 JF |
10428 | |
10429 | if (Version_ == nil) { | |
10430 | Version_ = [NSNumber numberWithUnsignedInt:0]; | |
10431 | [Metadata_ setObject:Version_ forKey:@"Version"]; | |
10432 | } | |
10433 | ||
33e30380 | 10434 | if ([Version_ unsignedIntValue] == 0) { |
25c1dafb JF |
10435 | CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]); |
10436 | CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]); | |
10437 | CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]); | |
10438 | CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./"); | |
33e30380 JF |
10439 | |
10440 | Version_ = [NSNumber numberWithUnsignedInt:1]; | |
10441 | [Metadata_ setObject:Version_ forKey:@"Version"]; | |
10442 | ||
a26ad329 JF |
10443 | [Metadata_ removeObjectForKey:@"LastUpdate"]; |
10444 | ||
33e30380 JF |
10445 | Changed_ = true; |
10446 | } | |
2595e4c3 JF |
10447 | |
10448 | _H<NSMutableArray> broken([NSMutableArray array]); | |
10449 | for (NSString *key in (id) Sources_) | |
10450 | if ([key rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"# "]].location != NSNotFound) | |
10451 | [broken addObject:key]; | |
10452 | if ([broken count] != 0) { | |
10453 | for (NSString *key in (id) broken) | |
10454 | [Sources_ removeObjectForKey:key]; | |
10455 | Changed_ = true; | |
10456 | } broken = nil; | |
7376b55c | 10457 | /* }}} */ |
b4d89997 | 10458 | |
f6b68b2c | 10459 | CydiaWriteSources(); |
33e30380 | 10460 | |
94b0b3e5 | 10461 | _trace(); |
a4a93d59 JF |
10462 | mkdir("/var/mobile/Library/Cydia", 0755); |
10463 | MetaFile_.Open("/var/mobile/Library/Cydia/metadata.cb0"); | |
94b0b3e5 JF |
10464 | _trace(); |
10465 | ||
10466 | if (Packages_ != nil) { | |
c65611b9 JF |
10467 | bool fail(false); |
10468 | CFDictionaryApplyFunction((CFDictionaryRef) Packages_, &PackageImport, &fail); | |
94b0b3e5 | 10469 | _trace(); |
c65611b9 JF |
10470 | |
10471 | if (!fail) { | |
10472 | [Metadata_ removeObjectForKey:@"Packages"]; | |
10473 | Packages_ = nil; | |
10474 | Changed_ = true; | |
10475 | } | |
94b0b3e5 JF |
10476 | } |
10477 | ||
d791dce4 JF |
10478 | Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; |
10479 | ||
d71f3a07 | 10480 | #define MobileSubstrate_(name) \ |
d13577cf JF |
10481 | if (substrate && access("/Library/MobileSubstrate/DynamicLibraries/" #name ".dylib", F_OK) == 0) { \ |
10482 | void *handle(dlopen("/Library/MobileSubstrate/DynamicLibraries/" #name ".dylib", RTLD_LAZY | RTLD_GLOBAL)); \ | |
10483 | if (handle == NULL) \ | |
10484 | NSLog(@"%s", dlerror()); \ | |
10485 | } | |
d71f3a07 JF |
10486 | |
10487 | MobileSubstrate_(Activator) | |
10488 | MobileSubstrate_(libstatusbar) | |
10489 | MobileSubstrate_(SimulatedKeyEvents) | |
10490 | MobileSubstrate_(WinterBoard) | |
10491 | ||
9dd60d81 JF |
10492 | /*if (substrate && access("/Library/MobileSubstrate/MobileSubstrate.dylib", F_OK) == 0) |
10493 | dlopen("/Library/MobileSubstrate/MobileSubstrate.dylib", RTLD_LAZY | RTLD_GLOBAL);*/ | |
dddbc481 | 10494 | |
6c9fe9af | 10495 | if (kCFCoreFoundationVersionNumber > 1000) |
0c0a966b | 10496 | system("/usr/libexec/cydia/cydo /usr/libexec/cydia/setnsfpn /var/lib"); |
6c9fe9af | 10497 | |
01d93940 JF |
10498 | int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); |
10499 | ||
ad502f71 | 10500 | if (access("/User", F_OK) != 0 || version != 6) { |
d6dad1b4 | 10501 | _trace(); |
0c0a966b | 10502 | system("/usr/libexec/cydia/cydo /usr/libexec/cydia/firmware.sh"); |
d6dad1b4 JF |
10503 | _trace(); |
10504 | } | |
9e98e020 | 10505 | |
7376b55c | 10506 | if (access("/tmp/cydia.chk", F_OK) == 0) { |
b44af625 | 10507 | if (unlink([Cache("pkgcache.bin") UTF8String]) == -1) |
7376b55c | 10508 | _assert(errno == ENOENT); |
b44af625 | 10509 | if (unlink([Cache("srcpkgcache.bin") UTF8String]) == -1) |
7376b55c JF |
10510 | _assert(errno == ENOENT); |
10511 | } | |
10512 | ||
59dbe296 | 10513 | /* APT Initialization {{{ */ |
b1ce61ec JF |
10514 | _assert(pkgInitConfig(*_config)); |
10515 | _assert(pkgInitSystem(*_config, _system)); | |
10516 | ||
10517 | if (lang != NULL) | |
10518 | _config->Set("APT::Acquire::Translation", lang); | |
cb94ff21 JF |
10519 | |
10520 | // XXX: this timeout might be important :( | |
10521 | //_config->Set("Acquire::http::Timeout", 15); | |
10522 | ||
ce1901de | 10523 | _config->Set("Acquire::http::MaxParallel", usermem >= 384 * 1024 * 1024 ? 16 : 3); |
b44af625 JF |
10524 | |
10525 | mkdir([Cache_ UTF8String], 0755); | |
10526 | mkdir([Cache("archives") UTF8String], 0755); | |
10527 | mkdir([Cache("archives/partial") UTF8String], 0755); | |
10528 | _config->Set("Dir::Cache", [Cache_ UTF8String]); | |
10529 | ||
10530 | mkdir([Cache("lists") UTF8String], 0755); | |
10531 | mkdir([Cache("lists/partial") UTF8String], 0755); | |
10532 | mkdir([Cache("periodic") UTF8String], 0755); | |
e6446ca0 | 10533 | _config->Set("Dir::State::Lists", [Cache("lists") UTF8String]); |
232b396b JF |
10534 | |
10535 | std::string logs("/var/mobile/Library/Logs/Cydia"); | |
10536 | mkdir(logs.c_str(), 0755); | |
10537 | _config->Set("Dir::Log::Terminal", logs + "/apt.log"); | |
10538 | ||
10539 | _config->Set("Dir::Bin::dpkg", "/usr/libexec/cydia/cydo"); | |
59dbe296 | 10540 | /* }}} */ |
7376b55c | 10541 | /* Color Choices {{{ */ |
36bb2ca2 JF |
10542 | space_ = CGColorSpaceCreateDeviceRGB(); |
10543 | ||
f641a0e5 | 10544 | Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0); |
77fcccaf | 10545 | Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0); |
36bb2ca2 | 10546 | Black_.Set(space_, 0.0, 0.0, 0.0, 1.0); |
86a333c6 | 10547 | Folder_.Set(space_, 0x8e/255.f, 0x8e/255.f, 0x93/255.f, 1.0); |
baf80942 | 10548 | Off_.Set(space_, 0.9, 0.9, 0.9, 1.0); |
36bb2ca2 | 10549 | White_.Set(space_, 1.0, 1.0, 1.0, 1.0); |
7b0ce2da | 10550 | Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0); |
3bd1c2a2 JF |
10551 | Green_.Set(space_, 0.0, 0.5, 0.0, 1.0); |
10552 | Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0); | |
10553 | Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0); | |
59dbe296 | 10554 | |
dc63e78f JF |
10555 | InstallingColor_ = [UIColor colorWithRed:0.88f green:1.00f blue:0.88f alpha:1.00f]; |
10556 | RemovingColor_ = [UIColor colorWithRed:1.00f green:0.88f blue:0.88f alpha:1.00f]; | |
7376b55c | 10557 | /* }}}*/ |
7376b55c | 10558 | /* UIKit Configuration {{{ */ |
600d005d JF |
10559 | // XXX: I have a feeling this was important |
10560 | //UIKeyboardDisableAutomaticAppearance(); | |
7376b55c | 10561 | /* }}} */ |
87f46a96 | 10562 | |
8a3b565c JF |
10563 | $SBSSetInterceptsMenuButtonForever = reinterpret_cast<void (*)(bool)>(dlsym(RTLD_DEFAULT, "SBSSetInterceptsMenuButtonForever")); |
10564 | ||
b37b0a4a JF |
10565 | const char *symbol(kCFCoreFoundationVersionNumber >= 800 ? "MGGetBoolAnswer" : "GSSystemHasCapability"); |
10566 | BOOL (*GSSystemHasCapability)(CFStringRef) = reinterpret_cast<BOOL (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, symbol)); | |
c73d524b JF |
10567 | bool fast = GSSystemHasCapability != NULL && GSSystemHasCapability(CFSTR("armv7")); |
10568 | ||
c73d524b | 10569 | PulseInterval_ = fast ? 50000 : 500000; |
8731fdb0 | 10570 | |
670a0494 | 10571 | Colon_ = UCLocalize("COLON_DELIMITED"); |
72fb3616 | 10572 | Elision_ = UCLocalize("ELISION"); |
670a0494 JF |
10573 | Error_ = UCLocalize("ERROR"); |
10574 | Warning_ = UCLocalize("WARNING"); | |
10575 | ||
d6dad1b4 | 10576 | _trace(); |
77df4f82 | 10577 | int value(UIApplicationMain(argc, argv, @"Cydia", @"Cydia")); |
36bb2ca2 JF |
10578 | |
10579 | CGColorSpaceRelease(space_); | |
199d0ba5 | 10580 | CFRelease(Locale_); |
36bb2ca2 | 10581 | |
d13edf44 | 10582 | [pool release]; |
36bb2ca2 | 10583 | return value; |
6d166849 | 10584 | } |