]>
Commit | Line | Data |
---|---|---|
36bb2ca2 | 1 | /* Cydia - iPhone UIKit Front-End for Debian APT |
4c66fad9 | 2 | * Copyright (C) 2008-2015 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 | |
57cc2170 | 93 | #include <dirent.h> |
c21004b9 | 94 | #include <fcntl.h> |
2a8d9add | 95 | #include <notify.h> |
dddbc481 | 96 | #include <dlfcn.h> |
3178d79b | 97 | |
b4d89997 JF |
98 | extern "C" { |
99 | #include <mach-o/nlist.h> | |
100 | } | |
101 | ||
a0be02eb JF |
102 | #include <cstdio> |
103 | #include <cstdlib> | |
104 | #include <cstring> | |
b4d89997 | 105 | |
2367a917 | 106 | #include <errno.h> |
a9543575 | 107 | |
94b0b3e5 | 108 | #include <Cytore.hpp> |
25c1dafb | 109 | #include "Sources.h" |
94b0b3e5 | 110 | |
a09f7a48 | 111 | #include "Substrate.hpp" |
cb218676 JF |
112 | #include "Menes/Menes.h" |
113 | ||
8731fdb0 | 114 | #include "CyteKit/IndirectDelegate.h" |
b4fd1197 | 115 | #include "CyteKit/RegEx.hpp" |
b97fcfc6 | 116 | #include "CyteKit/TableViewCell.h" |
5fe2bcc6 | 117 | #include "CyteKit/TabBarController.h" |
f172aa8f | 118 | #include "CyteKit/WebScriptObject-Cyte.h" |
d458596e | 119 | #include "CyteKit/WebViewController.h" |
8731fdb0 | 120 | #include "CyteKit/WebViewTableViewCell.h" |
d458596e | 121 | #include "CyteKit/stringWithUTF8Bytes.h" |
449ef9d5 | 122 | |
7aa82ca2 | 123 | #include "Cydia/MIMEAddress.h" |
70750ab3 | 124 | #include "Cydia/LoadingViewController.h" |
cb218676 | 125 | #include "Cydia/ProgressEvent.h" |
28b8b687 | 126 | |
71cc7be1 | 127 | #include "SDURLCache/SDURLCache.h" |
b5e7eebb GP |
128 | /* }}} */ |
129 | ||
bfc87a4d | 130 | /* Profiler {{{ */ |
807ae6d7 JF |
131 | struct timeval _ltv; |
132 | bool _itv; | |
133 | ||
2083b866 JF |
134 | #define _timestamp ({ \ |
135 | struct timeval tv; \ | |
136 | gettimeofday(&tv, NULL); \ | |
137 | tv.tv_sec * 1000000 + tv.tv_usec; \ | |
138 | }) | |
139 | ||
808c6eb6 JF |
140 | typedef std::vector<class ProfileTime *> TimeList; |
141 | TimeList times_; | |
142 | ||
143 | class ProfileTime { | |
144 | private: | |
145 | const char *name_; | |
146 | uint64_t total_; | |
76933519 | 147 | uint64_t count_; |
808c6eb6 JF |
148 | |
149 | public: | |
150 | ProfileTime(const char *name) : | |
151 | name_(name), | |
152 | total_(0) | |
153 | { | |
154 | times_.push_back(this); | |
155 | } | |
156 | ||
157 | void AddTime(uint64_t time) { | |
158 | total_ += time; | |
76933519 | 159 | ++count_; |
808c6eb6 JF |
160 | } |
161 | ||
162 | void Print() { | |
163 | if (total_ != 0) | |
f15558c7 | 164 | std::cerr << std::setw(7) << count_ << ", " << std::setw(8) << total_ << " : " << name_ << std::endl; |
808c6eb6 | 165 | total_ = 0; |
76933519 | 166 | count_ = 0; |
808c6eb6 JF |
167 | } |
168 | }; | |
169 | ||
170 | class ProfileTimer { | |
171 | private: | |
172 | ProfileTime &time_; | |
173 | uint64_t start_; | |
174 | ||
175 | public: | |
176 | ProfileTimer(ProfileTime &time) : | |
177 | time_(time), | |
178 | start_(_timestamp) | |
179 | { | |
180 | } | |
181 | ||
182 | ~ProfileTimer() { | |
183 | time_.AddTime(_timestamp - start_); | |
184 | } | |
185 | }; | |
186 | ||
187 | void PrintTimes() { | |
188 | for (TimeList::const_iterator i(times_.begin()); i != times_.end(); ++i) | |
189 | (*i)->Print(); | |
190 | std::cerr << "========" << std::endl; | |
191 | } | |
192 | ||
193 | #define _profile(name) { \ | |
194 | static ProfileTime name(#name); \ | |
195 | ProfileTimer _ ## name(name); | |
196 | ||
197 | #define _end } | |
f79a4512 | 198 | /* }}} */ |
affeffc7 | 199 | |
4cc9e99a JF |
200 | // XXX: I hate clang. Apple: please get over your petty hatred of GPL and fix your gcc fork |
201 | #define synchronized(lock) \ | |
202 | synchronized(static_cast<NSObject *>(lock)) | |
203 | ||
3d45bad1 | 204 | extern NSString *Cydia_; |
f9ba524a | 205 | |
43c5d1cb JF |
206 | #define lprintf(args...) fprintf(stderr, args) |
207 | ||
208 | #define ForRelease 1 | |
209 | #define TraceLogging (1 && !ForRelease) | |
01b4e393 | 210 | #define HistogramInsertionSort (0 && !ForRelease) |
43c5d1cb JF |
211 | #define ProfileTimes (0 && !ForRelease) |
212 | #define ForSaurik (0 && !ForRelease) | |
213 | #define LogBrowser (0 && !ForRelease) | |
214 | #define TrackResize (0 && !ForRelease) | |
215 | #define ManualRefresh (1 && !ForRelease) | |
216 | #define ShowInternals (0 && !ForRelease) | |
217 | #define AlwaysReload (0 && !ForRelease) | |
43c5d1cb JF |
218 | |
219 | #if !TraceLogging | |
220 | #undef _trace | |
221 | #define _trace(args...) | |
222 | #endif | |
223 | ||
224 | #if !ProfileTimes | |
225 | #undef _profile | |
226 | #define _profile(name) { | |
227 | #undef _end | |
228 | #define _end } | |
229 | #define PrintTimes() do {} while (false) | |
230 | #endif | |
231 | ||
94b0b3e5 JF |
232 | // Hash Functions/Structures {{{ |
233 | extern "C" uint32_t hashlittle(const void *key, size_t length, uint32_t initval = 0); | |
234 | ||
235 | union SplitHash { | |
236 | uint32_t u32; | |
237 | uint16_t u16[2]; | |
238 | }; | |
239 | // }}} | |
240 | ||
5db5891a JF |
241 | static NSString *Colon_; |
242 | NSString *Elision_; | |
243 | static NSString *Error_; | |
244 | static NSString *Warning_; | |
245 | ||
d1c7f1fd | 246 | static NSString *Cache_; |
b44af625 JF |
247 | #define Cache(file) \ |
248 | [NSString stringWithFormat:@"%@/%s", Cache_, file] | |
d1c7f1fd | 249 | |
8a3b565c | 250 | static void (*$SBSSetInterceptsMenuButtonForever)(bool); |
c7e78d5f | 251 | static NSData *(*$SBSCopyIconImagePNGDataForDisplayIdentifier)(NSString *); |
8a3b565c | 252 | |
7c80833f JF |
253 | static CFStringRef (*$MGCopyAnswer)(CFStringRef); |
254 | ||
255 | static NSString *UniqueIdentifier(UIDevice *device = nil) { | |
256 | if (kCFCoreFoundationVersionNumber < 800) // iOS 7.x | |
257 | return [device ?: [UIDevice currentDevice] uniqueIdentifier]; | |
258 | else | |
259 | return [(id)$MGCopyAnswer(CFSTR("UniqueDeviceID")) autorelease]; | |
260 | } | |
261 | ||
6cbfbe28 JF |
262 | static bool IsReachable(const char *name) { |
263 | SCNetworkReachabilityFlags flags; { | |
37f1fb03 | 264 | SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, name)); |
6cbfbe28 JF |
265 | SCNetworkReachabilityGetFlags(reachability, &flags); |
266 | CFRelease(reachability); | |
267 | } | |
268 | ||
269 | // XXX: this elaborate mess is what Apple is using to determine this? :( | |
270 | // XXX: do we care if the user has to intervene? maybe that's ok? | |
271 | return | |
272 | (flags & kSCNetworkReachabilityFlagsReachable) != 0 && ( | |
273 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || ( | |
274 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 || | |
275 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0 | |
276 | ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 || | |
277 | (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0 | |
278 | ) | |
279 | ; | |
280 | } | |
281 | ||
04fe1349 JF |
282 | static const NSUInteger UIViewAutoresizingFlexibleBoth(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); |
283 | ||
670a0494 | 284 | static _finline NSString *CydiaURL(NSString *path) { |
e3d2a2f5 JF |
285 | char page[26]; |
286 | page[0] = 'h'; page[1] = 't'; page[2] = 't'; page[3] = 'p'; page[4] = 's'; | |
287 | page[5] = ':'; page[6] = '/'; page[7] = '/'; page[8] = 'c'; page[9] = 'y'; | |
288 | page[10] = 'd'; page[11] = 'i'; page[12] = 'a'; page[13] = '.'; page[14] = 's'; | |
289 | page[15] = 'a'; page[16] = 'u'; page[17] = 'r'; page[18] = 'i'; page[19] = 'k'; | |
290 | page[20] = '.'; page[21] = 'c'; page[22] = 'o'; page[23] = 'm'; page[24] = '/'; | |
291 | page[25] = '\0'; | |
670a0494 JF |
292 | return [[NSString stringWithUTF8String:page] stringByAppendingString:path]; |
293 | } | |
294 | ||
d8b0f6e3 JF |
295 | static NSString *ShellEscape(NSString *value) { |
296 | return [NSString stringWithFormat:@"'%@'", [value stringByReplacingOccurrencesOfString:@"'" withString:@"'\\''"]]; | |
297 | } | |
298 | ||
ef494bd8 RP |
299 | static _finline void UpdateExternalStatus(uint64_t newStatus) { |
300 | int notify_token; | |
301 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
302 | notify_set_state(notify_token, newStatus); | |
303 | notify_cancel(notify_token); | |
304 | } | |
305 | notify_post("com.saurik.Cydia.status"); | |
306 | } | |
307 | ||
0b7516cf | 308 | static CGFloat CYStatusBarHeight() { |
57daa971 | 309 | CGSize size([[UIApplication sharedApplication] statusBarFrame].size); |
0b7516cf | 310 | return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? size.height : size.width; |
57daa971 JF |
311 | } |
312 | ||
68f1828e | 313 | /* NSForcedOrderingSearch doesn't work on the iPhone */ |
808c6eb6 | 314 | static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch; |
677b8415 | 315 | static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch; |
aeeb755b | 316 | static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; |
1e7a90f5 | 317 | |
eef4ccaf JF |
318 | /* Insertion Sort {{{ */ |
319 | ||
df213583 JF |
320 | CFIndex SKBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
321 | const char *ptr = (const char *)list; | |
322 | while (0 < count) { | |
323 | CFIndex half = count / 2; | |
324 | const char *probe = ptr + elementSize * half; | |
325 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 326 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
df213583 JF |
327 | ptr = (cr < 0) ? ptr : probe + elementSize; |
328 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
329 | } | |
330 | return (ptr - (const char *)list) / elementSize; | |
331 | } | |
332 | ||
eef4ccaf JF |
333 | CFIndex CFBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
334 | const char *ptr = (const char *)list; | |
335 | while (0 < count) { | |
336 | CFIndex half = count / 2; | |
337 | const char *probe = ptr + elementSize * half; | |
338 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 339 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
eef4ccaf JF |
340 | ptr = (cr < 0) ? ptr : probe + elementSize; |
341 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
342 | } | |
343 | return (ptr - (const char *)list) / elementSize; | |
344 | } | |
345 | ||
346 | void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFComparatorFunction comparator, void *context) { | |
347 | if (range.length == 0) | |
348 | return; | |
349 | const void **values(new const void *[range.length]); | |
350 | CFArrayGetValues(array, range, values); | |
351 | ||
1539387a | 352 | #if HistogramInsertionSort > 0 |
df213583 JF |
353 | uint32_t total(0), *offsets(new uint32_t[range.length]); |
354 | #endif | |
355 | ||
eef4ccaf JF |
356 | for (CFIndex index(1); index != range.length; ++index) { |
357 | const void *value(values[index]); | |
df213583 JF |
358 | //CFIndex correct(SKBSearch_(&value, sizeof(const void *), values, index, comparator, context)); |
359 | CFIndex correct(index); | |
1539387a JF |
360 | while (comparator(value, values[correct - 1], context) == kCFCompareLessThan) { |
361 | #if HistogramInsertionSort > 1 | |
362 | NSLog(@"%@ < %@", value, values[correct - 1]); | |
363 | #endif | |
df213583 JF |
364 | if (--correct == 0) |
365 | break; | |
1539387a | 366 | } |
eef4ccaf | 367 | if (correct != index) { |
df213583 JF |
368 | size_t offset(index - correct); |
369 | #if HistogramInsertionSort | |
370 | total += offset; | |
371 | ++offsets[offset]; | |
372 | if (offset > 10) | |
373 | NSLog(@"Heavy Insertion Displacement: %u = %@", offset, value); | |
374 | #endif | |
375 | memmove(values + correct + 1, values + correct, sizeof(const void *) * offset); | |
eef4ccaf JF |
376 | values[correct] = value; |
377 | } | |
378 | } | |
379 | ||
380 | CFArrayReplaceValues(array, range, values, range.length); | |
381 | delete [] values; | |
df213583 | 382 | |
1539387a | 383 | #if HistogramInsertionSort > 0 |
df213583 JF |
384 | for (CFIndex index(0); index != range.length; ++index) |
385 | if (offsets[index] != 0) | |
386 | NSLog(@"Insertion Displacement [%u]: %u", index, offsets[index]); | |
387 | NSLog(@"Average Insertion Displacement: %f", double(total) / range.length); | |
388 | delete [] offsets; | |
389 | #endif | |
eef4ccaf JF |
390 | } |
391 | ||
807ae6d7 JF |
392 | /* }}} */ |
393 | ||
bf8476c8 JF |
394 | /* Apple Bug Fixes {{{ */ |
395 | @implementation UIWebDocumentView (Cydia) | |
396 | ||
397 | - (void) _setScrollerOffset:(CGPoint)offset { | |
398 | UIScroller *scroller([self _scroller]); | |
399 | ||
400 | CGSize size([scroller contentSize]); | |
401 | CGSize bounds([scroller bounds].size); | |
402 | ||
403 | CGPoint max; | |
404 | max.x = size.width - bounds.width; | |
405 | max.y = size.height - bounds.height; | |
406 | ||
407 | // wtf Apple?! | |
408 | if (max.x < 0) | |
409 | max.x = 0; | |
410 | if (max.y < 0) | |
411 | max.y = 0; | |
412 | ||
413 | offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; | |
414 | offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; | |
415 | ||
416 | [scroller setOffset:offset]; | |
417 | } | |
418 | ||
419 | @end | |
420 | /* }}} */ | |
421 | ||
680eb135 JF |
422 | NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { |
423 | size_t length([self length] - state->state); | |
424 | if (length <= 0) | |
425 | return 0; | |
426 | else if (length > count) | |
427 | length = count; | |
428 | for (size_t i(0); i != length; ++i) | |
429 | objects[i] = [self item:state->state++]; | |
430 | state->itemsPtr = objects; | |
431 | state->mutationsPtr = (unsigned long *) self; | |
432 | return length; | |
433 | } | |
434 | ||
bfc87a4d | 435 | /* Cydia NSString Additions {{{ */ |
2388b078 | 436 | @interface NSString (Cydia) |
a54b1c10 | 437 | - (NSComparisonResult) compareByPath:(NSString *)other; |
2fc76a2d | 438 | - (NSString *) stringByAddingPercentEscapesIncludingReserved; |
2388b078 JF |
439 | @end |
440 | ||
449ef9d5 JF |
441 | @implementation NSString (Cydia) |
442 | ||
a54b1c10 JF |
443 | - (NSComparisonResult) compareByPath:(NSString *)other { |
444 | NSString *prefix = [self commonPrefixWithString:other options:0]; | |
445 | size_t length = [prefix length]; | |
446 | ||
447 | NSRange lrange = NSMakeRange(length, [self length] - length); | |
448 | NSRange rrange = NSMakeRange(length, [other length] - length); | |
449 | ||
450 | lrange = [self rangeOfString:@"/" options:0 range:lrange]; | |
451 | rrange = [other rangeOfString:@"/" options:0 range:rrange]; | |
452 | ||
453 | NSComparisonResult value; | |
454 | ||
455 | if (lrange.location == NSNotFound && rrange.location == NSNotFound) | |
456 | value = NSOrderedSame; | |
457 | else if (lrange.location == NSNotFound) | |
458 | value = NSOrderedAscending; | |
459 | else if (rrange.location == NSNotFound) | |
460 | value = NSOrderedDescending; | |
461 | else | |
462 | value = NSOrderedSame; | |
463 | ||
464 | NSString *lpath = lrange.location == NSNotFound ? [self substringFromIndex:length] : | |
465 | [self substringWithRange:NSMakeRange(length, lrange.location - length)]; | |
466 | NSString *rpath = rrange.location == NSNotFound ? [other substringFromIndex:length] : | |
467 | [other substringWithRange:NSMakeRange(length, rrange.location - length)]; | |
468 | ||
469 | NSComparisonResult result = [lpath compare:rpath]; | |
470 | return result == NSOrderedSame ? value : result; | |
471 | } | |
472 | ||
2fc76a2d JF |
473 | - (NSString *) stringByAddingPercentEscapesIncludingReserved { |
474 | return [(id)CFURLCreateStringByAddingPercentEscapes( | |
bc11cf5b | 475 | kCFAllocatorDefault, |
2fc76a2d JF |
476 | (CFStringRef) self, |
477 | NULL, | |
478 | CFSTR(";/?:@&=+$,"), | |
479 | kCFStringEncodingUTF8 | |
480 | ) autorelease]; | |
481 | } | |
482 | ||
2388b078 | 483 | @end |
bfc87a4d | 484 | /* }}} */ |
2388b078 | 485 | |
bfc87a4d | 486 | /* C++ NSString Wrapper Cache {{{ */ |
8e8fca7f JF |
487 | static _finline CFStringRef CYStringCreate(const char *data, size_t size) { |
488 | return size == 0 ? NULL : | |
489 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingUTF8, NO, kCFAllocatorNull) ?: | |
490 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingISOLatin1, NO, kCFAllocatorNull); | |
491 | } | |
492 | ||
493 | static _finline CFStringRef CYStringCreate(const char *data) { | |
494 | return CYStringCreate(data, strlen(data)); | |
495 | } | |
496 | ||
f79a4512 JF |
497 | class CYString { |
498 | private: | |
499 | char *data_; | |
500 | size_t size_; | |
501 | CFStringRef cache_; | |
502 | ||
503 | _finline void clear_() { | |
18873623 | 504 | if (cache_ != NULL) { |
f79a4512 | 505 | CFRelease(cache_); |
18873623 JF |
506 | cache_ = NULL; |
507 | } | |
f79a4512 JF |
508 | } |
509 | ||
510 | public: | |
511 | _finline bool empty() const { | |
512 | return size_ == 0; | |
513 | } | |
514 | ||
515 | _finline size_t size() const { | |
516 | return size_; | |
517 | } | |
518 | ||
519 | _finline char *data() const { | |
520 | return data_; | |
521 | } | |
522 | ||
523 | _finline void clear() { | |
524 | size_ = 0; | |
525 | clear_(); | |
526 | } | |
527 | ||
528 | _finline CYString() : | |
529 | data_(0), | |
530 | size_(0), | |
18873623 | 531 | cache_(NULL) |
f79a4512 JF |
532 | { |
533 | } | |
534 | ||
535 | _finline ~CYString() { | |
536 | clear_(); | |
537 | } | |
538 | ||
539 | void operator =(const CYString &rhs) { | |
540 | data_ = rhs.data_; | |
541 | size_ = rhs.size_; | |
542 | ||
543 | if (rhs.cache_ == nil) | |
544 | cache_ = NULL; | |
545 | else | |
546 | cache_ = reinterpret_cast<CFStringRef>(CFRetain(rhs.cache_)); | |
547 | } | |
548 | ||
64edd9df JF |
549 | void copy(CYPool *pool) { |
550 | char *temp(pool->malloc<char>(size_ + 1)); | |
97814287 JF |
551 | memcpy(temp, data_, size_); |
552 | temp[size_] = '\0'; | |
553 | data_ = temp; | |
554 | } | |
555 | ||
64edd9df | 556 | void set(CYPool *pool, const char *data, size_t size) { |
f79a4512 JF |
557 | if (size == 0) |
558 | clear(); | |
559 | else { | |
560 | clear_(); | |
561 | ||
97814287 | 562 | data_ = const_cast<char *>(data); |
f79a4512 | 563 | size_ = size; |
97814287 JF |
564 | |
565 | if (pool != NULL) | |
566 | copy(pool); | |
f79a4512 JF |
567 | } |
568 | } | |
569 | ||
64edd9df | 570 | _finline void set(CYPool *pool, const char *data) { |
f79a4512 JF |
571 | set(pool, data, data == NULL ? 0 : strlen(data)); |
572 | } | |
573 | ||
64edd9df | 574 | _finline void set(CYPool *pool, const std::string &rhs) { |
f79a4512 JF |
575 | set(pool, rhs.data(), rhs.size()); |
576 | } | |
577 | ||
578 | bool operator ==(const CYString &rhs) const { | |
579 | return size_ == rhs.size_ && memcmp(data_, rhs.data_, size_) == 0; | |
580 | } | |
581 | ||
8e8fca7f JF |
582 | _finline operator CFStringRef() { |
583 | if (cache_ == NULL) | |
584 | cache_ = CYStringCreate(data_, size_); | |
585 | return cache_; | |
df213583 JF |
586 | } |
587 | ||
588 | _finline operator id() { | |
589 | return (NSString *) static_cast<CFStringRef>(*this); | |
f79a4512 | 590 | } |
4c0ed943 JF |
591 | |
592 | _finline operator const char *() { | |
593 | return reinterpret_cast<const char *>(data_); | |
594 | } | |
f79a4512 | 595 | }; |
bfc87a4d JF |
596 | /* }}} */ |
597 | /* C++ NSString Algorithm Adapters {{{ */ | |
f79a4512 JF |
598 | extern "C" { |
599 | CF_EXPORT CFHashCode CFStringHashNSString(CFStringRef str); | |
600 | } | |
601 | ||
602 | struct NSStringMapHash : | |
603 | std::unary_function<NSString *, size_t> | |
604 | { | |
605 | _finline size_t operator ()(NSString *value) const { | |
606 | return CFStringHashNSString((CFStringRef) value); | |
607 | } | |
608 | }; | |
609 | ||
610 | struct NSStringMapLess : | |
611 | std::binary_function<NSString *, NSString *, bool> | |
612 | { | |
613 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
614 | return [lhs compare:rhs] == NSOrderedAscending; | |
615 | } | |
616 | }; | |
617 | ||
618 | struct NSStringMapEqual : | |
619 | std::binary_function<NSString *, NSString *, bool> | |
620 | { | |
621 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
622 | return CFStringCompare((CFStringRef) lhs, (CFStringRef) rhs, 0) == kCFCompareEqualTo; | |
623 | //CFEqual((CFTypeRef) lhs, (CFTypeRef) rhs); | |
624 | //[lhs isEqualToString:rhs]; | |
625 | } | |
626 | }; | |
bfc87a4d | 627 | /* }}} */ |
f79a4512 | 628 | |
5f6bff8c | 629 | /* CoreGraphics Primitives {{{ */ |
02012733 | 630 | class CYColor { |
b4d89997 JF |
631 | private: |
632 | CGColorRef color_; | |
633 | ||
6a575b5e JF |
634 | static CGColorRef Create_(CGColorSpaceRef space, float red, float green, float blue, float alpha) { |
635 | CGFloat color[] = {red, green, blue, alpha}; | |
636 | return CGColorCreate(space, color); | |
637 | } | |
638 | ||
b4d89997 | 639 | public: |
02012733 | 640 | CYColor() : |
36bb2ca2 JF |
641 | color_(NULL) |
642 | { | |
643 | } | |
644 | ||
02012733 | 645 | CYColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) : |
6a575b5e | 646 | color_(Create_(space, red, green, blue, alpha)) |
36bb2ca2 JF |
647 | { |
648 | Set(space, red, green, blue, alpha); | |
649 | } | |
650 | ||
651 | void Clear() { | |
652 | if (color_ != NULL) | |
653 | CGColorRelease(color_); | |
b4d89997 JF |
654 | } |
655 | ||
02012733 | 656 | ~CYColor() { |
36bb2ca2 JF |
657 | Clear(); |
658 | } | |
659 | ||
660 | void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) { | |
661 | Clear(); | |
6a575b5e | 662 | color_ = Create_(space, red, green, blue, alpha); |
b4d89997 JF |
663 | } |
664 | ||
665 | operator CGColorRef() { | |
666 | return color_; | |
667 | } | |
668 | }; | |
b4d89997 JF |
669 | /* }}} */ |
670 | ||
36bb2ca2 | 671 | /* Random Global Variables {{{ */ |
c73d524b | 672 | static int PulseInterval_ = 500000; |
affeffc7 | 673 | |
57e8b225 JF |
674 | static const NSString *UI_; |
675 | ||
d791dce4 | 676 | static int Finish_; |
be860cc8 | 677 | static bool RestartSubstrate_; |
d791dce4 JF |
678 | static NSArray *Finishes_; |
679 | ||
affeffc7 | 680 | #define SpringBoard_ "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist" |
22f8bed9 | 681 | #define NotifyConfig_ "/etc/notify.conf" |
2a8d9add | 682 | |
dc63e78f JF |
683 | static bool Queuing_; |
684 | ||
02012733 JF |
685 | static CYColor Blue_; |
686 | static CYColor Blueish_; | |
687 | static CYColor Black_; | |
86a333c6 | 688 | static CYColor Folder_; |
02012733 JF |
689 | static CYColor Off_; |
690 | static CYColor White_; | |
691 | static CYColor Gray_; | |
692 | static CYColor Green_; | |
693 | static CYColor Purple_; | |
694 | static CYColor Purplish_; | |
3bd1c2a2 | 695 | |
dc63e78f JF |
696 | static UIColor *InstallingColor_; |
697 | static UIColor *RemovingColor_; | |
36bb2ca2 | 698 | |
7d2ac47f | 699 | static NSString *App_; |
d73cede2 | 700 | |
2388b078 | 701 | static BOOL Advanced_; |
a54b1c10 | 702 | static BOOL Ignored_; |
2388b078 | 703 | |
7b33d201 JF |
704 | static _H<UIFont> Font12_; |
705 | static _H<UIFont> Font12Bold_; | |
706 | static _H<UIFont> Font14_; | |
2cdc6e57 | 707 | static _H<UIFont> Font18_; |
7b33d201 JF |
708 | static _H<UIFont> Font18Bold_; |
709 | static _H<UIFont> Font22Bold_; | |
f641a0e5 | 710 | |
87f46a96 | 711 | static const char *Machine_ = NULL; |
e967efd5 | 712 | static _H<NSString> System_; |
56127854 JF |
713 | static NSString *SerialNumber_ = nil; |
714 | static NSString *ChipID_ = nil; | |
6ffdaae3 | 715 | static NSString *BBSNum_ = nil; |
7c80833f | 716 | static _H<NSString> UniqueID_; |
e967efd5 JF |
717 | static _H<NSString> UserAgent_; |
718 | static _H<NSString> Product_; | |
719 | static _H<NSString> Safari_; | |
2a8d9add | 720 | |
aeeb755b JF |
721 | static _H<NSLocale> CollationLocale_; |
722 | static _H<NSArray> CollationThumbs_; | |
723 | static std::vector<NSInteger> CollationOffset_; | |
724 | static _H<NSArray> CollationTitles_; | |
725 | static _H<NSArray> CollationStarts_; | |
22fd24dd JF |
726 | static UTransliterator *CollationTransl_; |
727 | //static Function<NSString *, NSString *> CollationModify_; | |
728 | ||
729 | typedef std::basic_string<UChar> ustring; | |
730 | static ustring CollationString_; | |
731 | ||
732 | #define CUC const ustring &str(*reinterpret_cast<const ustring *>(rep)) | |
733 | #define UC ustring &str(*reinterpret_cast<ustring *>(rep)) | |
734 | static struct UReplaceableCallbacks CollationUCalls_ = { | |
735 | .length = [](const UReplaceable *rep) -> int32_t { CUC; | |
736 | return str.size(); | |
737 | }, | |
738 | ||
739 | .charAt = [](const UReplaceable *rep, int32_t offset) -> UChar { CUC; | |
740 | //fprintf(stderr, "charAt(%d) : %d\n", offset, str.size()); | |
741 | if (offset >= str.size()) | |
742 | return 0xffff; | |
743 | return str[offset]; | |
744 | }, | |
745 | ||
746 | .char32At = [](const UReplaceable *rep, int32_t offset) -> UChar32 { CUC; | |
747 | //fprintf(stderr, "char32At(%d) : %d\n", offset, str.size()); | |
748 | if (offset >= str.size()) | |
749 | return 0xffff; | |
750 | UChar32 c; | |
751 | U16_GET(str.data(), 0, offset, str.size(), c); | |
752 | return c; | |
753 | }, | |
754 | ||
755 | .replace = [](UReplaceable *rep, int32_t start, int32_t limit, const UChar *text, int32_t length) -> void { UC; | |
756 | //fprintf(stderr, "replace(%d, %d, %d) : %d\n", start, limit, length, str.size()); | |
757 | str.replace(start, limit - start, text, length); | |
758 | }, | |
759 | ||
760 | .extract = [](UReplaceable *rep, int32_t start, int32_t limit, UChar *dst) -> void { UC; | |
761 | //fprintf(stderr, "extract(%d, %d) : %d\n", start, limit, str.size()); | |
762 | str.copy(dst, limit - start, start); | |
763 | }, | |
764 | ||
765 | .copy = [](UReplaceable *rep, int32_t start, int32_t limit, int32_t dest) -> void { UC; | |
766 | //fprintf(stderr, "copy(%d, %d, %d) : %d\n", start, limit, dest, str.size()); | |
767 | str.replace(dest, 0, str, start, limit - start); | |
768 | }, | |
769 | }; | |
aeeb755b | 770 | |
d791dce4 JF |
771 | static CFLocaleRef Locale_; |
772 | static NSArray *Languages_; | |
773 | static CGColorSpaceRef space_; | |
36bb2ca2 | 774 | |
6f87c61a | 775 | #define CacheState_ "/var/mobile/Library/Caches/com.saurik.Cydia/CacheState.plist" |
3d1e6f42 JF |
776 | #define SavedState_ "/var/mobile/Library/Caches/com.saurik.Cydia/SavedState.plist" |
777 | ||
46dbfd32 | 778 | static NSDictionary *SectionMap_; |
3d1e6f42 | 779 | static _H<NSDate> Backgrounded_; |
b3c8e69c | 780 | static _transient NSMutableDictionary *Values_; |
7b0ce2da | 781 | static _transient NSMutableDictionary *Sections_; |
25c1dafb | 782 | _H<NSMutableDictionary> Sources_; |
33e30380 | 783 | static _transient NSNumber *Version_; |
31bc18a7 | 784 | static time_t now_; |
8da60fb7 | 785 | |
f333f6c5 | 786 | bool IsWildcat_; |
f98962e5 | 787 | CGFloat ScreenScale_; |
c138614d | 788 | static NSString *Idiom_; |
407564b5 | 789 | static _H<NSString> Firmware_; |
fd825a2d | 790 | static NSString *Major_; |
5df7ecfb | 791 | |
7b33d201 JF |
792 | static _H<NSMutableDictionary> SessionData_; |
793 | static _H<NSObject> HostConfig_; | |
794 | static _H<NSMutableSet> BridgedHosts_; | |
2e1652a9 | 795 | static _H<NSMutableSet> InsecureHosts_; |
7b33d201 | 796 | static _H<NSMutableSet> PipelinedHosts_; |
5e845121 | 797 | static _H<NSMutableSet> CachedURLs_; |
389133be | 798 | |
e4123ce0 JF |
799 | static NSString *kCydiaProgressEventTypeError = @"Error"; |
800 | static NSString *kCydiaProgressEventTypeInformation = @"Information"; | |
801 | static NSString *kCydiaProgressEventTypeStatus = @"Status"; | |
802 | static NSString *kCydiaProgressEventTypeWarning = @"Warning"; | |
36bb2ca2 | 803 | /* }}} */ |
d791dce4 | 804 | |
36bb2ca2 JF |
805 | /* Display Helpers {{{ */ |
806 | inline float Interpolate(float begin, float end, float fraction) { | |
807 | return (end - begin) * fraction + begin; | |
808 | } | |
2367a917 | 809 | |
8323c1b9 JF |
810 | static inline double Retina(double value) { |
811 | value *= ScreenScale_; | |
812 | value = round(value); | |
813 | value /= ScreenScale_; | |
814 | return value; | |
815 | } | |
816 | ||
817 | static inline CGRect Retina(CGRect value) { | |
818 | value.origin.x *= ScreenScale_; | |
819 | value.origin.y *= ScreenScale_; | |
820 | value.size.width *= ScreenScale_; | |
821 | value.size.height *= ScreenScale_; | |
822 | value = CGRectIntegral(value); | |
823 | value.origin.x /= ScreenScale_; | |
824 | value.origin.y /= ScreenScale_; | |
825 | value.size.width /= ScreenScale_; | |
826 | value.size.height /= ScreenScale_; | |
827 | return value; | |
828 | } | |
829 | ||
1c1dfc2d | 830 | static _finline const char *StripVersion_(const char *version) { |
6a155117 | 831 | const char *colon(strchr(version, ':')); |
673ad3c3 | 832 | return colon == NULL ? version : colon + 1; |
6a155117 JF |
833 | } |
834 | ||
f79a4512 | 835 | NSString *LocalizeSection(NSString *section) { |
b4fd1197 | 836 | static RegEx title_r("(.*?) \\((.*)\\)"); |
9fcbca29 JF |
837 | if (title_r(section)) { |
838 | NSString *parent(title_r[1]); | |
839 | NSString *child(title_r[2]); | |
840 | ||
43f3d7f6 | 841 | return [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), |
9fcbca29 JF |
842 | LocalizeSection(parent), |
843 | LocalizeSection(child) | |
b1ce61ec | 844 | ]; |
9fcbca29 | 845 | } |
b1ce61ec JF |
846 | |
847 | return [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
f79a4512 JF |
848 | } |
849 | ||
4cf4165e JF |
850 | NSString *Simplify(NSString *title) { |
851 | const char *data = [title UTF8String]; | |
393a84a1 | 852 | size_t size = [title lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
4cf4165e | 853 | |
b4fd1197 | 854 | static RegEx square_r("\\[(.*)\\]"); |
7b0ce2da JF |
855 | if (square_r(data, size)) |
856 | return Simplify(square_r[1]); | |
857 | ||
b4fd1197 | 858 | static RegEx paren_r("\\((.*)\\)"); |
7b0ce2da JF |
859 | if (paren_r(data, size)) |
860 | return Simplify(paren_r[1]); | |
861 | ||
b4fd1197 | 862 | static RegEx title_r("(.*?) \\((.*)\\)"); |
4cf4165e | 863 | if (title_r(data, size)) |
7b0ce2da JF |
864 | return Simplify(title_r[1]); |
865 | ||
866 | return title; | |
4cf4165e | 867 | } |
36bb2ca2 JF |
868 | /* }}} */ |
869 | ||
6d9712c4 | 870 | bool isSectionVisible(NSString *section) { |
45447dc3 | 871 | NSDictionary *metadata([Sections_ objectForKey:(section ?: @"")]); |
677b8415 | 872 | NSNumber *hidden(metadata == nil ? nil : [metadata objectForKey:@"Hidden"]); |
6d9712c4 JF |
873 | return hidden == nil || ![hidden boolValue]; |
874 | } | |
875 | ||
c31d7cdc | 876 | static NSObject *CYIOGetValue(const char *path, NSString *property) { |
947a8eef JF |
877 | io_registry_entry_t entry(IORegistryEntryFromPath(kIOMasterPortDefault, path)); |
878 | if (entry == MACH_PORT_NULL) | |
879 | return nil; | |
880 | ||
881 | CFTypeRef value(IORegistryEntryCreateCFProperty(entry, (CFStringRef) property, kCFAllocatorDefault, 0)); | |
882 | IOObjectRelease(entry); | |
883 | ||
884 | if (value == NULL) | |
885 | return nil; | |
886 | return [(id) value autorelease]; | |
887 | } | |
888 | ||
c31d7cdc | 889 | static NSString *CYHex(NSData *data, bool reverse = false) { |
947a8eef JF |
890 | if (data == nil) |
891 | return nil; | |
892 | ||
893 | size_t length([data length]); | |
894 | uint8_t bytes[length]; | |
895 | [data getBytes:bytes]; | |
896 | ||
897 | char string[length * 2 + 1]; | |
898 | for (size_t i(0); i != length; ++i) | |
be45a862 | 899 | sprintf(string + i * 2, "%.2x", bytes[reverse ? length - i - 1 : i]); |
947a8eef JF |
900 | |
901 | return [NSString stringWithUTF8String:string]; | |
902 | } | |
903 | ||
8a2d167a JF |
904 | static NSString *VerifySource(NSString *href) { |
905 | static RegEx href_r("(http(s?)://|file:///)[^# ]*"); | |
906 | if (!href_r(href)) { | |
907 | [[[[UIAlertView alloc] | |
908 | initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("INVALID_URL")] | |
909 | message:UCLocalize("INVALID_URL_EX") | |
910 | delegate:nil | |
911 | cancelButtonTitle:UCLocalize("OK") | |
912 | otherButtonTitles:nil | |
913 | ] autorelease] show]; | |
914 | ||
915 | return nil; | |
916 | } | |
917 | ||
918 | if (![href hasSuffix:@"/"]) | |
919 | href = [href stringByAppendingString:@"/"]; | |
920 | return href; | |
921 | } | |
922 | ||
9cb0bff2 GP |
923 | @class Cydia; |
924 | ||
d36e83a3 | 925 | /* Delegate Prototypes {{{ */ |
36bb2ca2 JF |
926 | @class Package; |
927 | @class Source; | |
6915b806 | 928 | @class CydiaProgressEvent; |
36bb2ca2 | 929 | |
6915b806 | 930 | @protocol DatabaseDelegate |
5a09ae08 | 931 | - (void) repairWithSelector:(SEL)selector; |
8b29f8e6 | 932 | - (void) setConfigurationData:(NSString *)data; |
6915b806 | 933 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task; |
8b29f8e6 JF |
934 | @end |
935 | ||
f6e13561 | 936 | @class CYPackageController; |
f79a4512 | 937 | |
21ac0ce2 JF |
938 | @protocol SourceDelegate |
939 | - (void) setFetch:(NSNumber *)fetch; | |
940 | @end | |
941 | ||
942 | @protocol FetchDelegate | |
943 | - (bool) isSourceCancelled; | |
944 | - (void) startSourceFetch:(NSString *)uri; | |
945 | - (void) stopSourceFetch:(NSString *)uri; | |
946 | @end | |
947 | ||
36bb2ca2 | 948 | @protocol CydiaDelegate |
2925cbba | 949 | - (void) returnToCydia; |
9dd3045d | 950 | - (void) saveState; |
54043703 JF |
951 | - (void) retainNetworkActivityIndicator; |
952 | - (void) releaseNetworkActivityIndicator; | |
dc63e78f | 953 | - (void) clearPackage:(Package *)package; |
36bb2ca2 | 954 | - (void) installPackage:(Package *)package; |
77801ff1 | 955 | - (void) installPackages:(NSArray *)packages; |
36bb2ca2 | 956 | - (void) removePackage:(Package *)package; |
c21004b9 JF |
957 | - (void) beginUpdate; |
958 | - (BOOL) updating; | |
e67ebdad | 959 | - (bool) requestUpdate; |
36bb2ca2 | 960 | - (void) distUpgrade; |
fed0d010 | 961 | - (void) loadData; |
6d9712c4 | 962 | - (void) updateData; |
392ff7e4 | 963 | - (void) _saveConfig; |
7b0ce2da | 964 | - (void) syncData; |
33e30380 | 965 | - (void) addSource:(NSDictionary *)source; |
8a2d167a | 966 | - (BOOL) addTrivialSource:(NSString *)href; |
7b0ce2da | 967 | - (UIProgressHUD *) addProgressHUD; |
d061f4ba | 968 | - (void) removeProgressHUD:(UIProgressHUD *)hud; |
9daa7f25 | 969 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item; |
4ba8f30a | 970 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 971 | @end |
d36e83a3 | 972 | /* }}} */ |
b4d89997 | 973 | |
21ac0ce2 JF |
974 | /* CancelStatus {{{ */ |
975 | class CancelStatus : | |
dc5812ec JF |
976 | public pkgAcquireStatus |
977 | { | |
978 | private: | |
fc470c15 | 979 | bool cancelled_; |
dc5812ec JF |
980 | |
981 | public: | |
21ac0ce2 | 982 | CancelStatus() : |
fc470c15 | 983 | cancelled_(false) |
dc5812ec JF |
984 | { |
985 | } | |
986 | ||
dc5812ec JF |
987 | virtual bool MediaChange(std::string media, std::string drive) { |
988 | return false; | |
989 | } | |
990 | ||
10387810 JF |
991 | virtual void IMSHit(pkgAcquire::ItemDesc &desc) { |
992 | Done(desc); | |
dc5812ec JF |
993 | } |
994 | ||
21ac0ce2 JF |
995 | virtual bool Pulse_(pkgAcquire *Owner) = 0; |
996 | ||
997 | virtual bool Pulse(pkgAcquire *Owner) { | |
998 | if (pkgAcquireStatus::Pulse(Owner) && Pulse_(Owner)) | |
999 | return true; | |
1000 | else { | |
1001 | cancelled_ = true; | |
1002 | return false; | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | _finline bool WasCancelled() const { | |
1007 | return cancelled_; | |
1008 | } | |
1009 | }; | |
1010 | /* }}} */ | |
1011 | /* DelegateStatus {{{ */ | |
1012 | class CydiaStatus : | |
1013 | public CancelStatus | |
1014 | { | |
1015 | private: | |
1016 | _transient NSObject<ProgressDelegate> *delegate_; | |
1017 | ||
1018 | public: | |
1019 | CydiaStatus() : | |
1020 | delegate_(nil) | |
1021 | { | |
1022 | } | |
1023 | ||
1024 | void setDelegate(NSObject<ProgressDelegate> *delegate) { | |
1025 | delegate_ = delegate; | |
1026 | } | |
1027 | ||
10387810 JF |
1028 | virtual void Fetch(pkgAcquire::ItemDesc &desc) { |
1029 | NSString *name([NSString stringWithUTF8String:desc.ShortDesc.c_str()]); | |
1030 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), name] ofType:kCydiaProgressEventTypeStatus forItemDesc:desc]); | |
6915b806 | 1031 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1032 | } |
1033 | ||
10387810 JF |
1034 | virtual void Done(pkgAcquire::ItemDesc &desc) { |
1035 | NSString *name([NSString stringWithUTF8String:desc.ShortDesc.c_str()]); | |
1036 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:Colon_, UCLocalize("DONE"), name] ofType:kCydiaProgressEventTypeStatus forItemDesc:desc]); | |
d35bcbbf | 1037 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1038 | } |
1039 | ||
10387810 | 1040 | virtual void Fail(pkgAcquire::ItemDesc &desc) { |
1d80f6b9 | 1041 | if ( |
10387810 JF |
1042 | desc.Owner->Status == pkgAcquire::Item::StatIdle || |
1043 | desc.Owner->Status == pkgAcquire::Item::StatDone | |
1d80f6b9 JF |
1044 | ) |
1045 | return; | |
1046 | ||
10387810 | 1047 | std::string &error(desc.Owner->ErrorText); |
affeffc7 JF |
1048 | if (error.empty()) |
1049 | return; | |
1050 | ||
10387810 | 1051 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError forItemDesc:desc]); |
6915b806 | 1052 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1053 | } |
1054 | ||
21ac0ce2 | 1055 | virtual bool Pulse_(pkgAcquire *Owner) { |
bcbac8f7 | 1056 | double percent( |
2367a917 JF |
1057 | double(CurrentBytes + CurrentItems) / |
1058 | double(TotalBytes + TotalItems) | |
1059 | ); | |
1060 | ||
bcbac8f7 JF |
1061 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:[NSDictionary dictionaryWithObjectsAndKeys: |
1062 | [NSNumber numberWithDouble:percent], @"Percent", | |
1063 | ||
1064 | [NSNumber numberWithDouble:CurrentBytes], @"Current", | |
1065 | [NSNumber numberWithDouble:TotalBytes], @"Total", | |
1066 | [NSNumber numberWithDouble:CurrentCPS], @"Speed", | |
1067 | nil] waitUntilDone:YES]; | |
1068 | ||
21ac0ce2 | 1069 | return ![delegate_ isProgressCancelled]; |
dc5812ec JF |
1070 | } |
1071 | ||
1072 | virtual void Start() { | |
0210c2b5 | 1073 | pkgAcquireStatus::Start(); |
aaae308d | 1074 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:YES]; |
dc5812ec JF |
1075 | } |
1076 | ||
1077 | virtual void Stop() { | |
0210c2b5 | 1078 | pkgAcquireStatus::Stop(); |
aaae308d | 1079 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:YES]; |
bcbac8f7 | 1080 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:nil waitUntilDone:YES]; |
dc5812ec JF |
1081 | } |
1082 | }; | |
1083 | /* }}} */ | |
36bb2ca2 | 1084 | /* Database Interface {{{ */ |
68d927e2 JF |
1085 | typedef std::map< unsigned long, _H<Source> > SourceMap; |
1086 | ||
36bb2ca2 | 1087 | @interface Database : NSObject { |
f79a4512 | 1088 | NSZone *zone_; |
64edd9df | 1089 | CYPool pool_; |
f79a4512 | 1090 | |
a1440b10 | 1091 | unsigned era_; |
f26c90b1 | 1092 | _H<NSDate> delock_; |
a1440b10 | 1093 | |
36bb2ca2 | 1094 | pkgCacheFile cache_; |
5a09ae08 | 1095 | pkgDepCache::Policy *policy_; |
36bb2ca2 JF |
1096 | pkgRecords *records_; |
1097 | pkgProblemResolver *resolver_; | |
1098 | pkgAcquire *fetcher_; | |
1099 | FileFd *lock_; | |
1100 | SPtr<pkgPackageManager> manager_; | |
1101 | pkgSourceList *list_; | |
5f6bff8c | 1102 | |
34f70f5d | 1103 | SourceMap sourceMap_; |
7b33d201 | 1104 | _H<NSMutableArray> sourceList_; |
34f70f5d | 1105 | |
077e9d90 | 1106 | CFMutableArrayRef packages_; |
b4d89997 | 1107 | |
6915b806 JF |
1108 | _transient NSObject<DatabaseDelegate> *delegate_; |
1109 | _transient NSObject<ProgressDelegate> *progress_; | |
1110 | ||
21ac0ce2 | 1111 | CydiaStatus status_; |
8b29f8e6 | 1112 | |
77fcccaf | 1113 | int cydiafd_; |
36bb2ca2 | 1114 | int statusfd_; |
8b29f8e6 | 1115 | FILE *input_; |
fe33a23e JF |
1116 | |
1117 | std::map<const char *, _H<NSString> > sections_; | |
dc5812ec JF |
1118 | } |
1119 | ||
770f2a8e | 1120 | + (Database *) sharedInstance; |
a1440b10 | 1121 | - (unsigned) era; |
770f2a8e | 1122 | |
77fcccaf | 1123 | - (void) _readCydia:(NSNumber *)fd; |
36bb2ca2 JF |
1124 | - (void) _readStatus:(NSNumber *)fd; |
1125 | - (void) _readOutput:(NSNumber *)fd; | |
2367a917 | 1126 | |
8b29f8e6 JF |
1127 | - (FILE *) input; |
1128 | ||
36bb2ca2 | 1129 | - (Package *) packageWithName:(NSString *)name; |
dc5812ec | 1130 | |
36bb2ca2 | 1131 | - (pkgCacheFile &) cache; |
5a09ae08 | 1132 | - (pkgDepCache::Policy *) policy; |
36bb2ca2 JF |
1133 | - (pkgRecords *) records; |
1134 | - (pkgProblemResolver *) resolver; | |
1135 | - (pkgAcquire &) fetcher; | |
a3328c28 | 1136 | - (pkgSourceList &) list; |
36bb2ca2 | 1137 | - (NSArray *) packages; |
7b0ce2da | 1138 | - (NSArray *) sources; |
d669236d | 1139 | - (Source *) sourceWithKey:(NSString *)key; |
4ba8f30a | 1140 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 1141 | |
5a09ae08 | 1142 | - (void) configure; |
670a0494 | 1143 | - (bool) prepare; |
36bb2ca2 | 1144 | - (void) perform; |
670a0494 | 1145 | - (bool) upgrade; |
36bb2ca2 JF |
1146 | - (void) update; |
1147 | ||
21ac0ce2 | 1148 | - (void) updateWithStatus:(CancelStatus &)status; |
36bb2ca2 | 1149 | |
6915b806 JF |
1150 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate; |
1151 | ||
1152 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate; | |
1153 | - (NSObject<ProgressDelegate> *) progressDelegate; | |
1154 | ||
7376b55c | 1155 | - (Source *) getSource:(pkgCache::PkgFileIterator)file; |
21ac0ce2 | 1156 | - (void) setFetch:(bool)fetch forURI:(const char *)uri; |
9ed626f1 | 1157 | - (void) resetFetch; |
fe33a23e JF |
1158 | |
1159 | - (NSString *) mappedSectionForPointer:(const char *)pointer; | |
1160 | ||
670a0494 JF |
1161 | @end |
1162 | /* }}} */ | |
21ac0ce2 JF |
1163 | /* SourceStatus {{{ */ |
1164 | class SourceStatus : | |
1165 | public CancelStatus | |
1166 | { | |
1167 | private: | |
1168 | _transient NSObject<FetchDelegate> *delegate_; | |
1169 | _transient Database *database_; | |
e6e180a4 | 1170 | std::set<std::string> fetches_; |
21ac0ce2 JF |
1171 | |
1172 | public: | |
1173 | SourceStatus(NSObject<FetchDelegate> *delegate, Database *database) : | |
1174 | delegate_(delegate), | |
1175 | database_(database) | |
1176 | { | |
1177 | } | |
1178 | ||
e6e180a4 JF |
1179 | void Set(bool fetch, const std::string &uri) { |
1180 | if (fetch) { | |
1181 | if (!fetches_.insert(uri).second) | |
1182 | return; | |
1183 | } else { | |
1184 | if (fetches_.erase(uri) == 0) | |
1185 | return; | |
1186 | } | |
1187 | ||
1188 | //printf("Set(%s, %s)\n", fetch ? "true" : "false", uri.c_str()); | |
1189 | [database_ setFetch:fetch forURI:uri.c_str()]; | |
1190 | } | |
1191 | ||
1192 | _finline void Set(bool fetch, pkgAcquire::Item *item) { | |
1193 | /*unsigned long ID(fetch ? 1 : 0); | |
1194 | if (item->ID == ID) | |
1195 | return; | |
1196 | item->ID = ID;*/ | |
1197 | Set(fetch, item->DescURI()); | |
1198 | } | |
1199 | ||
1200 | void Log(const char *tag, pkgAcquire::Item *item) { | |
1201 | //printf("%s(%s) S:%u Q:%u\n", tag, item->DescURI().c_str(), item->Status, item->QueueCounter); | |
21ac0ce2 JF |
1202 | } |
1203 | ||
1204 | virtual void Fetch(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1205 | Log("Fetch", desc.Owner); |
1206 | Set(true, desc.Owner); | |
21ac0ce2 JF |
1207 | } |
1208 | ||
1209 | virtual void Done(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1210 | Log("Done", desc.Owner); |
1211 | Set(false, desc.Owner); | |
21ac0ce2 JF |
1212 | } |
1213 | ||
1214 | virtual void Fail(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1215 | Log("Fail", desc.Owner); |
1216 | Set(false, desc.Owner); | |
21ac0ce2 JF |
1217 | } |
1218 | ||
1219 | virtual bool Pulse_(pkgAcquire *Owner) { | |
e6e180a4 JF |
1220 | std::set<std::string> fetches; |
1221 | for (pkgAcquire::ItemCIterator item(Owner->ItemsBegin()); item != Owner->ItemsEnd(); ++item) { | |
1222 | bool fetch; | |
1223 | if ((*item)->QueueCounter == 0) | |
1224 | fetch = false; | |
1225 | else switch ((*item)->Status) { | |
1226 | case pkgAcquire::Item::StatFetching: | |
1227 | fetches.insert((*item)->DescURI()); | |
1228 | fetch = true; | |
1229 | break; | |
1230 | ||
1231 | default: | |
1232 | fetch = false; | |
1233 | break; | |
1234 | } | |
1235 | ||
1236 | Log(fetch ? "Pulse<true>" : "Pulse<false>", *item); | |
1237 | Set(fetch, *item); | |
1238 | } | |
1239 | ||
1240 | std::vector<std::string> stops; | |
1241 | std::set_difference(fetches_.begin(), fetches_.end(), fetches.begin(), fetches.end(), std::back_insert_iterator<std::vector<std::string>>(stops)); | |
1242 | for (std::vector<std::string>::const_iterator stop(stops.begin()); stop != stops.end(); ++stop) { | |
1243 | //printf("Stop(%s)\n", stop->c_str()); | |
1244 | Set(false, *stop); | |
1245 | } | |
1246 | ||
21ac0ce2 JF |
1247 | return ![delegate_ isSourceCancelled]; |
1248 | } | |
9ed626f1 JF |
1249 | |
1250 | virtual void Stop() { | |
1251 | pkgAcquireStatus::Stop(); | |
1252 | [database_ resetFetch]; | |
1253 | } | |
21ac0ce2 JF |
1254 | }; |
1255 | /* }}} */ | |
6915b806 JF |
1256 | /* ProgressEvent Implementation {{{ */ |
1257 | @implementation CydiaProgressEvent | |
670a0494 | 1258 | |
6915b806 JF |
1259 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type { |
1260 | return [[[CydiaProgressEvent alloc] initWithMessage:message ofType:type] autorelease]; | |
670a0494 JF |
1261 | } |
1262 | ||
6915b806 JF |
1263 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forPackage:(NSString *)package { |
1264 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); | |
1265 | [event setPackage:package]; | |
1266 | return event; | |
670a0494 JF |
1267 | } |
1268 | ||
10387810 | 1269 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forItemDesc:(pkgAcquire::ItemDesc &)desc { |
c57867ea JF |
1270 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); |
1271 | ||
10387810 | 1272 | NSString *description([NSString stringWithUTF8String:desc.Description.c_str()]); |
c57867ea JF |
1273 | NSArray *fields([description componentsSeparatedByString:@" "]); |
1274 | [event setItem:fields]; | |
1275 | ||
1276 | if ([fields count] > 3) { | |
1277 | [event setPackage:[fields objectAtIndex:2]]; | |
1278 | [event setVersion:[fields objectAtIndex:3]]; | |
1279 | } | |
1280 | ||
10387810 | 1281 | [event setURL:[NSString stringWithUTF8String:desc.URI.c_str()]]; |
c57867ea JF |
1282 | |
1283 | return event; | |
1284 | } | |
1285 | ||
4ede7a3f JF |
1286 | + (NSArray *) _attributeKeys { |
1287 | return [NSArray arrayWithObjects: | |
c57867ea | 1288 | @"item", |
4ede7a3f JF |
1289 | @"message", |
1290 | @"package", | |
1291 | @"type", | |
c57867ea JF |
1292 | @"url", |
1293 | @"version", | |
4ede7a3f JF |
1294 | nil]; |
1295 | } | |
1296 | ||
1297 | - (NSArray *) attributeKeys { | |
1298 | return [[self class] _attributeKeys]; | |
1299 | } | |
1300 | ||
1301 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1302 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1303 | } | |
1304 | ||
6915b806 JF |
1305 | - (id) initWithMessage:(NSString *)message ofType:(NSString *)type { |
1306 | if ((self = [super init]) != nil) { | |
1307 | message_ = message; | |
1308 | type_ = type; | |
1309 | } return self; | |
670a0494 JF |
1310 | } |
1311 | ||
6915b806 JF |
1312 | - (NSString *) message { |
1313 | return message_; | |
1314 | } | |
6067f1b8 | 1315 | |
6915b806 JF |
1316 | - (NSString *) type { |
1317 | return type_; | |
1318 | } | |
1319 | ||
c57867ea JF |
1320 | - (NSArray *) item { |
1321 | return (id) item_ ?: [NSNull null]; | |
1322 | } | |
1323 | ||
1324 | - (void) setItem:(NSArray *)item { | |
1325 | item_ = item; | |
6915b806 JF |
1326 | } |
1327 | ||
c57867ea JF |
1328 | - (NSString *) package { |
1329 | return (id) package_ ?: [NSNull null]; | |
6915b806 JF |
1330 | } |
1331 | ||
1332 | - (void) setPackage:(NSString *)package { | |
1333 | package_ = package; | |
1334 | } | |
1335 | ||
c57867ea JF |
1336 | - (NSString *) url { |
1337 | return (id) url_ ?: [NSNull null]; | |
1338 | } | |
1339 | ||
1340 | - (void) setURL:(NSString *)url { | |
1341 | url_ = url; | |
1342 | } | |
1343 | ||
1344 | - (void) setVersion:(NSString *)version { | |
1345 | version_ = version; | |
1346 | } | |
1347 | ||
1348 | - (NSString *) version { | |
1349 | return (id) version_ ?: [NSNull null]; | |
1350 | } | |
1351 | ||
6915b806 JF |
1352 | - (NSString *) compound:(NSString *)value { |
1353 | if (value != nil) { | |
1354 | NSString *mode(nil); { | |
1355 | NSString *type([self type]); | |
389133be | 1356 | if ([type isEqualToString:kCydiaProgressEventTypeError]) |
6915b806 | 1357 | mode = UCLocalize("ERROR"); |
389133be | 1358 | else if ([type isEqualToString:kCydiaProgressEventTypeWarning]) |
6915b806 JF |
1359 | mode = UCLocalize("WARNING"); |
1360 | } | |
1361 | ||
1362 | if (mode != nil) | |
1363 | value = [NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), mode, value]; | |
1364 | } | |
1365 | ||
1366 | return value; | |
1367 | } | |
1368 | ||
1369 | - (NSString *) compoundMessage { | |
1370 | return [self compound:[self message]]; | |
1371 | } | |
1372 | ||
1373 | - (NSString *) compoundTitle { | |
1374 | NSString *title; | |
1375 | ||
83b78e5f | 1376 | if (package_ == nil) |
6915b806 | 1377 | title = nil; |
83b78e5f JF |
1378 | else if (Package *package = [[Database sharedInstance] packageWithName:package_]) |
1379 | title = [package name]; | |
1380 | else | |
1381 | title = package_; | |
6915b806 JF |
1382 | |
1383 | return [self compound:title]; | |
670a0494 JF |
1384 | } |
1385 | ||
dc5812ec | 1386 | @end |
36bb2ca2 | 1387 | /* }}} */ |
dc5812ec | 1388 | |
94b0b3e5 JF |
1389 | // Cytore Definitions {{{ |
1390 | struct PackageValue : | |
1391 | Cytore::Block | |
1392 | { | |
94b0b3e5 JF |
1393 | Cytore::Offset<PackageValue> next_; |
1394 | ||
1395 | uint32_t index_ : 23; | |
1396 | uint32_t subscribed_ : 1; | |
1397 | uint32_t : 8; | |
1398 | ||
1399 | int32_t first_; | |
1400 | int32_t last_; | |
1401 | ||
1402 | uint16_t vhash_; | |
1403 | uint16_t nhash_; | |
1404 | ||
1405 | char version_[8]; | |
1406 | char name_[]; | |
1407 | }; | |
1408 | ||
1409 | struct MetaValue : | |
1410 | Cytore::Block | |
1411 | { | |
9f357d11 | 1412 | uint32_t active_; |
94b0b3e5 JF |
1413 | Cytore::Offset<PackageValue> packages_[1 << 16]; |
1414 | }; | |
1415 | ||
1416 | static Cytore::File<MetaValue> MetaFile_; | |
1417 | // }}} | |
1418 | // Cytore Helper Functions {{{ | |
c65611b9 | 1419 | static PackageValue *PackageFind(const char *name, size_t length, bool *fail = NULL) { |
94b0b3e5 JF |
1420 | SplitHash nhash = { hashlittle(name, length) }; |
1421 | ||
1422 | PackageValue *metadata; | |
1423 | ||
1424 | Cytore::Offset<PackageValue> *offset(&MetaFile_->packages_[nhash.u16[0]]); | |
ac5f7cb3 | 1425 | for (;; offset = &metadata->next_) { if (offset->IsNull()) { |
94b0b3e5 JF |
1426 | *offset = MetaFile_.New<PackageValue>(length + 1); |
1427 | metadata = &MetaFile_.Get(*offset); | |
1428 | ||
c65611b9 JF |
1429 | if (metadata == NULL) { |
1430 | if (fail != NULL) | |
1431 | *fail = true; | |
1432 | ||
1433 | metadata = new PackageValue(); | |
1434 | memset(metadata, 0, sizeof(*metadata)); | |
1435 | } | |
1436 | ||
ac5f7cb3 JF |
1437 | memcpy(metadata->name_, name, length); |
1438 | metadata->name_[length] = '\0'; | |
94b0b3e5 JF |
1439 | metadata->nhash_ = nhash.u16[1]; |
1440 | } else { | |
1441 | metadata = &MetaFile_.Get(*offset); | |
ac5f7cb3 JF |
1442 | if (metadata->nhash_ != nhash.u16[1]) |
1443 | continue; | |
1444 | if (strncmp(metadata->name_, name, length) != 0) | |
1445 | continue; | |
1446 | if (metadata->name_[length] != '\0') | |
1447 | continue; | |
1448 | } break; } | |
94b0b3e5 | 1449 | |
94b0b3e5 JF |
1450 | return metadata; |
1451 | } | |
1452 | ||
1453 | static void PackageImport(const void *key, const void *value, void *context) { | |
c65611b9 JF |
1454 | bool &fail(*reinterpret_cast<bool *>(context)); |
1455 | ||
94b0b3e5 JF |
1456 | char buffer[1024]; |
1457 | if (!CFStringGetCString((CFStringRef) key, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { | |
1458 | NSLog(@"failed to import package %@", key); | |
1459 | return; | |
1460 | } | |
1461 | ||
c65611b9 | 1462 | PackageValue *metadata(PackageFind(buffer, strlen(buffer), &fail)); |
94b0b3e5 JF |
1463 | NSDictionary *package((NSDictionary *) value); |
1464 | ||
1465 | if (NSNumber *subscribed = [package objectForKey:@"IsSubscribed"]) | |
2f856365 | 1466 | if ([subscribed boolValue] && !metadata->subscribed_) |
94b0b3e5 JF |
1467 | metadata->subscribed_ = true; |
1468 | ||
1469 | if (NSDate *date = [package objectForKey:@"FirstSeen"]) { | |
1470 | time_t time([date timeIntervalSince1970]); | |
1471 | if (metadata->first_ > time || metadata->first_ == 0) | |
1472 | metadata->first_ = time; | |
1473 | } | |
1474 | ||
2f856365 JF |
1475 | NSDate *date([package objectForKey:@"LastSeen"]); |
1476 | NSString *version([package objectForKey:@"LastVersion"]); | |
5a933937 | 1477 | |
2f856365 | 1478 | if (date != nil && version != nil) { |
94b0b3e5 | 1479 | time_t time([date timeIntervalSince1970]); |
2f856365 | 1480 | if (metadata->last_ < time || metadata->last_ == 0) |
5a933937 JF |
1481 | if (CFStringGetCString((CFStringRef) version, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { |
1482 | size_t length(strlen(buffer)); | |
1483 | uint16_t vhash(hashlittle(buffer, length)); | |
94b0b3e5 | 1484 | |
5a933937 JF |
1485 | size_t capped(std::min<size_t>(8, length)); |
1486 | char *latest(buffer + length - capped); | |
1487 | ||
1488 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); | |
1489 | metadata->vhash_ = vhash; | |
2f856365 JF |
1490 | |
1491 | metadata->last_ = time; | |
5a933937 | 1492 | } |
2f856365 | 1493 | } |
94b0b3e5 JF |
1494 | } |
1495 | // }}} | |
1496 | ||
f26c90b1 JF |
1497 | static NSDate *GetStatusDate() { |
1498 | return [[[NSFileManager defaultManager] attributesOfItemAtPath:@"/var/lib/dpkg/status" error:NULL] fileModificationDate]; | |
1499 | } | |
1500 | ||
4e479350 JF |
1501 | static void SaveConfig(NSObject *lock) { |
1502 | @synchronized (lock) { | |
1503 | _trace(); | |
1504 | MetaFile_.Sync(); | |
1505 | _trace(); | |
1506 | } | |
1507 | ||
1508 | CFPreferencesSetMultiple((CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys: | |
1509 | Values_, @"CydiaValues", | |
1510 | Sections_, @"CydiaSections", | |
1511 | (id) Sources_, @"CydiaSources", | |
1512 | Version_, @"CydiaVersion", | |
1513 | nil], NULL, CFSTR("com.saurik.Cydia"), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost); | |
1514 | ||
1515 | if (!CFPreferencesAppSynchronize(CFSTR("com.saurik.Cydia"))) | |
1516 | NSLog(@"CFPreferencesAppSynchronize(com.saurik.Cydia) == false"); | |
1517 | ||
1518 | CydiaWriteSources(); | |
1519 | } | |
1520 | ||
36bb2ca2 JF |
1521 | /* Source Class {{{ */ |
1522 | @interface Source : NSObject { | |
aec64d46 JF |
1523 | unsigned era_; |
1524 | Database *database_; | |
1525 | metaIndex *index_; | |
1526 | ||
6204f56a | 1527 | CYString depiction_; |
f9f6d9e8 JF |
1528 | CYString description_; |
1529 | CYString label_; | |
1530 | CYString origin_; | |
1531 | CYString support_; | |
dc5812ec | 1532 | |
f9f6d9e8 JF |
1533 | CYString uri_; |
1534 | CYString distribution_; | |
1535 | CYString type_; | |
8252b666 | 1536 | CYString base_; |
f9f6d9e8 | 1537 | CYString version_; |
2367a917 | 1538 | |
c08c8943 JF |
1539 | _H<NSString> host_; |
1540 | _H<NSString> authority_; | |
f9f6d9e8 JF |
1541 | |
1542 | CYString defaultIcon_; | |
dc5812ec | 1543 | |
4cc9e99a | 1544 | _H<NSMutableDictionary> record_; |
36bb2ca2 | 1545 | BOOL trusted_; |
21ac0ce2 JF |
1546 | |
1547 | std::set<std::string> fetches_; | |
1548 | std::set<std::string> files_; | |
1549 | _transient NSObject<SourceDelegate> *delegate_; | |
dc5812ec JF |
1550 | } |
1551 | ||
64edd9df | 1552 | - (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool; |
dc5812ec | 1553 | |
90ba4f86 | 1554 | - (NSComparisonResult) compareByName:(Source *)source; |
7b0ce2da | 1555 | |
6204f56a | 1556 | - (NSString *) depictionForPackage:(NSString *)package; |
dc63e78f JF |
1557 | - (NSString *) supportForPackage:(NSString *)package; |
1558 | ||
0c28a403 | 1559 | - (metaIndex *) metaIndex; |
7b0ce2da | 1560 | - (NSDictionary *) record; |
36bb2ca2 | 1561 | - (BOOL) trusted; |
dc5812ec | 1562 | |
7bd76e97 | 1563 | - (NSString *) rooturi; |
36bb2ca2 JF |
1564 | - (NSString *) distribution; |
1565 | - (NSString *) type; | |
8252b666 | 1566 | |
7b0ce2da JF |
1567 | - (NSString *) key; |
1568 | - (NSString *) host; | |
36bb2ca2 | 1569 | |
7b0ce2da | 1570 | - (NSString *) name; |
8d262908 | 1571 | - (NSString *) shortDescription; |
36bb2ca2 JF |
1572 | - (NSString *) label; |
1573 | - (NSString *) origin; | |
1574 | - (NSString *) version; | |
dc5812ec | 1575 | |
36bb2ca2 | 1576 | - (NSString *) defaultIcon; |
7bd76e97 | 1577 | - (NSURL *) iconURL; |
7b0ce2da | 1578 | |
21ac0ce2 | 1579 | - (void) setFetch:(bool)fetch forURI:(const char *)uri; |
9ed626f1 | 1580 | - (void) resetFetch; |
21ac0ce2 | 1581 | |
dc5812ec | 1582 | @end |
dc5812ec | 1583 | |
36bb2ca2 | 1584 | @implementation Source |
dc5812ec | 1585 | |
33e30380 JF |
1586 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
1587 | if (false); | |
1588 | else if (selector == @selector(addSection:)) | |
1589 | return @"addSection"; | |
aec64d46 JF |
1590 | else if (selector == @selector(getField:)) |
1591 | return @"getField"; | |
33e30380 JF |
1592 | else if (selector == @selector(removeSection:)) |
1593 | return @"removeSection"; | |
1594 | else if (selector == @selector(remove)) | |
1595 | return @"remove"; | |
1596 | else | |
1597 | return nil; | |
1598 | } | |
1599 | ||
1600 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
1601 | return [self webScriptNameForSelector:selector] == nil; | |
1602 | } | |
1603 | ||
6f1a15d9 | 1604 | + (NSArray *) _attributeKeys { |
e58ff941 | 1605 | return [NSArray arrayWithObjects: |
7bd76e97 | 1606 | @"baseuri", |
e58ff941 JF |
1607 | @"distribution", |
1608 | @"host", | |
1609 | @"key", | |
7bd76e97 | 1610 | @"iconuri", |
e58ff941 JF |
1611 | @"label", |
1612 | @"name", | |
1613 | @"origin", | |
7bd76e97 | 1614 | @"rooturi", |
33e30380 | 1615 | @"sections", |
8d262908 | 1616 | @"shortDescription", |
e58ff941 JF |
1617 | @"trusted", |
1618 | @"type", | |
e58ff941 JF |
1619 | @"version", |
1620 | nil]; | |
6f1a15d9 JF |
1621 | } |
1622 | ||
1623 | - (NSArray *) attributeKeys { | |
1624 | return [[self class] _attributeKeys]; | |
1625 | } | |
1626 | ||
1627 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1628 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1629 | } | |
1630 | ||
0c28a403 JF |
1631 | - (metaIndex *) metaIndex { |
1632 | return index_; | |
1633 | } | |
1634 | ||
64edd9df | 1635 | - (void) setMetaIndex:(metaIndex *)index inPool:(CYPool *)pool { |
a3328c28 JF |
1636 | trusted_ = index->IsTrusted(); |
1637 | ||
f9f6d9e8 JF |
1638 | uri_.set(pool, index->GetURI()); |
1639 | distribution_.set(pool, index->GetDist()); | |
1640 | type_.set(pool, index->GetType()); | |
a3328c28 JF |
1641 | |
1642 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index)); | |
1643 | if (dindex != NULL) { | |
21ac0ce2 | 1644 | std::string file(dindex->MetaIndexURI("")); |
21ac0ce2 JF |
1645 | base_.set(pool, file); |
1646 | ||
3f88f205 | 1647 | pkgAcquire acquire; |
1a83afc6 | 1648 | _profile(Source$setMetaIndex$GetIndexes) |
3f88f205 | 1649 | dindex->GetIndexes(&acquire, true); |
1a83afc6 JF |
1650 | _end |
1651 | _profile(Source$setMetaIndex$DescURI) | |
3f88f205 JF |
1652 | for (pkgAcquire::ItemIterator item(acquire.ItemsBegin()); item != acquire.ItemsEnd(); item++) { |
1653 | std::string file((*item)->DescURI()); | |
1654 | files_.insert(file); | |
1655 | if (file.length() < sizeof("Packages.bz2") || file.substr(file.length() - sizeof("Packages.bz2")) != "/Packages.bz2") | |
1656 | continue; | |
1657 | file = file.substr(0, file.length() - 4); | |
21ac0ce2 JF |
1658 | files_.insert(file); |
1659 | files_.insert(file + ".gz"); | |
21ac0ce2 | 1660 | files_.insert(file + "Index"); |
3f88f205 | 1661 | } |
1a83afc6 | 1662 | _end |
8252b666 | 1663 | |
18873623 JF |
1664 | FileFd fd; |
1665 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) | |
1666 | _error->Discard(); | |
1667 | else { | |
1668 | pkgTagFile tags(&fd); | |
f9f6d9e8 | 1669 | |
18873623 JF |
1670 | pkgTagSection section; |
1671 | tags.Step(section); | |
a3328c28 | 1672 | |
18873623 JF |
1673 | struct { |
1674 | const char *name_; | |
1675 | CYString *value_; | |
1676 | } names[] = { | |
1677 | {"default-icon", &defaultIcon_}, | |
6204f56a | 1678 | {"depiction", &depiction_}, |
18873623 JF |
1679 | {"description", &description_}, |
1680 | {"label", &label_}, | |
1681 | {"origin", &origin_}, | |
1682 | {"support", &support_}, | |
1683 | {"version", &version_}, | |
1684 | }; | |
f9f6d9e8 | 1685 | |
18873623 JF |
1686 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { |
1687 | const char *start, *end; | |
1688 | ||
1689 | if (section.Find(names[i].name_, start, end)) { | |
1690 | CYString &value(*names[i].value_); | |
1691 | value.set(pool, start, end - start); | |
1692 | } | |
f9f6d9e8 | 1693 | } |
36bb2ca2 | 1694 | } |
a3328c28 | 1695 | } |
7b0ce2da | 1696 | |
a3328c28 | 1697 | record_ = [Sources_ objectForKey:[self key]]; |
f9f6d9e8 | 1698 | |
7623f855 JF |
1699 | NSURL *url([NSURL URLWithString:uri_]); |
1700 | ||
1701 | host_ = [url host]; | |
1702 | if (host_ != nil) | |
c08c8943 | 1703 | host_ = [host_ lowercaseString]; |
7623f855 JF |
1704 | |
1705 | if (host_ != nil) | |
dfdb9ae0 | 1706 | authority_ = host_; |
7623f855 JF |
1707 | else |
1708 | authority_ = [url path]; | |
a3328c28 JF |
1709 | } |
1710 | ||
64edd9df | 1711 | - (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool { |
a3328c28 | 1712 | if ((self = [super init]) != nil) { |
aec64d46 JF |
1713 | era_ = [database era]; |
1714 | database_ = database; | |
1715 | index_ = index; | |
1716 | ||
1a83afc6 | 1717 | _profile(Source$initWithMetaIndex$setMetaIndex) |
f9f6d9e8 | 1718 | [self setMetaIndex:index inPool:pool]; |
1a83afc6 | 1719 | _end |
36bb2ca2 | 1720 | } return self; |
2367a917 | 1721 | } |
dc5812ec | 1722 | |
aec64d46 JF |
1723 | - (NSString *) getField:(NSString *)name { |
1724 | @synchronized (database_) { | |
1725 | if ([database_ era] != era_ || index_ == NULL) | |
1726 | return nil; | |
1727 | ||
1728 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index_)); | |
1729 | if (dindex == NULL) | |
1730 | return nil; | |
1731 | ||
1732 | FileFd fd; | |
1733 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) { | |
1734 | _error->Discard(); | |
1735 | return nil; | |
1736 | } | |
1737 | ||
1738 | pkgTagFile tags(&fd); | |
1739 | ||
1740 | pkgTagSection section; | |
1741 | tags.Step(section); | |
1742 | ||
1743 | const char *start, *end; | |
1744 | if (!section.Find([name UTF8String], start, end)) | |
1745 | return (NSString *) [NSNull null]; | |
1746 | ||
1747 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; | |
1748 | } } | |
1749 | ||
90ba4f86 | 1750 | - (NSComparisonResult) compareByName:(Source *)source { |
7b0ce2da JF |
1751 | NSString *lhs = [self name]; |
1752 | NSString *rhs = [source name]; | |
1753 | ||
1754 | if ([lhs length] != 0 && [rhs length] != 0) { | |
1755 | unichar lhc = [lhs characterAtIndex:0]; | |
1756 | unichar rhc = [rhs characterAtIndex:0]; | |
1757 | ||
1758 | if (isalpha(lhc) && !isalpha(rhc)) | |
1759 | return NSOrderedAscending; | |
1760 | else if (!isalpha(lhc) && isalpha(rhc)) | |
1761 | return NSOrderedDescending; | |
1762 | } | |
1763 | ||
68f1828e | 1764 | return [lhs compare:rhs options:LaxCompareOptions_]; |
7b0ce2da JF |
1765 | } |
1766 | ||
6204f56a | 1767 | - (NSString *) depictionForPackage:(NSString *)package { |
89b0ea4a | 1768 | return depiction_.empty() ? nil : [static_cast<id>(depiction_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
6204f56a JF |
1769 | } |
1770 | ||
dc63e78f | 1771 | - (NSString *) supportForPackage:(NSString *)package { |
89b0ea4a | 1772 | return support_.empty() ? nil : [static_cast<id>(support_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
dc63e78f JF |
1773 | } |
1774 | ||
33e30380 JF |
1775 | - (NSArray *) sections { |
1776 | return record_ == nil ? (id) [NSNull null] : [record_ objectForKey:@"Sections"] ?: [NSArray array]; | |
1777 | } | |
1778 | ||
1779 | - (void) _addSection:(NSString *)section { | |
1780 | if (record_ == nil) | |
1781 | return; | |
1782 | else if (NSMutableArray *sections = [record_ objectForKey:@"Sections"]) { | |
4e479350 | 1783 | if (![sections containsObject:section]) |
33e30380 | 1784 | [sections addObject:section]; |
4e479350 | 1785 | } else |
33e30380 | 1786 | [record_ setObject:[NSMutableArray arrayWithObject:section] forKey:@"Sections"]; |
33e30380 JF |
1787 | } |
1788 | ||
1789 | - (bool) addSection:(NSString *)section { | |
1790 | if (record_ == nil) | |
1791 | return false; | |
1792 | ||
1793 | [self performSelectorOnMainThread:@selector(_addSection:) withObject:section waitUntilDone:NO]; | |
1794 | return true; | |
1795 | } | |
1796 | ||
1797 | - (void) _removeSection:(NSString *)section { | |
1798 | if (record_ == nil) | |
1799 | return; | |
1800 | ||
1801 | if (NSMutableArray *sections = [record_ objectForKey:@"Sections"]) | |
4e479350 | 1802 | if ([sections containsObject:section]) |
33e30380 | 1803 | [sections removeObject:section]; |
33e30380 JF |
1804 | } |
1805 | ||
1806 | - (bool) removeSection:(NSString *)section { | |
1807 | if (record_ == nil) | |
1808 | return false; | |
1809 | ||
1810 | [self performSelectorOnMainThread:@selector(_removeSection:) withObject:section waitUntilDone:NO]; | |
1811 | return true; | |
1812 | } | |
1813 | ||
1814 | - (void) _remove { | |
1815 | [Sources_ removeObjectForKey:[self key]]; | |
33e30380 JF |
1816 | } |
1817 | ||
1818 | - (bool) remove { | |
1819 | bool value(record_ != nil); | |
1820 | [self performSelectorOnMainThread:@selector(_remove) withObject:nil waitUntilDone:NO]; | |
1821 | return value; | |
1822 | } | |
1823 | ||
7b0ce2da JF |
1824 | - (NSDictionary *) record { |
1825 | return record_; | |
1826 | } | |
1827 | ||
36bb2ca2 JF |
1828 | - (BOOL) trusted { |
1829 | return trusted_; | |
1830 | } | |
3178d79b | 1831 | |
7bd76e97 | 1832 | - (NSString *) rooturi { |
36bb2ca2 JF |
1833 | return uri_; |
1834 | } | |
ec97ef06 | 1835 | |
36bb2ca2 JF |
1836 | - (NSString *) distribution { |
1837 | return distribution_; | |
1838 | } | |
dc5812ec | 1839 | |
36bb2ca2 JF |
1840 | - (NSString *) type { |
1841 | return type_; | |
dc5812ec JF |
1842 | } |
1843 | ||
7bd76e97 JF |
1844 | - (NSString *) baseuri { |
1845 | return base_.empty() ? nil : (id) base_; | |
1846 | } | |
1847 | ||
1848 | - (NSString *) iconuri { | |
1849 | if (NSString *base = [self baseuri]) | |
1850 | return [base stringByAppendingString:@"CydiaIcon.png"]; | |
1851 | ||
1852 | return nil; | |
1853 | } | |
1854 | ||
1855 | - (NSURL *) iconURL { | |
1856 | if (NSString *uri = [self iconuri]) | |
1857 | return [NSURL URLWithString:uri]; | |
1858 | return nil; | |
8252b666 JF |
1859 | } |
1860 | ||
7b0ce2da | 1861 | - (NSString *) key { |
f9f6d9e8 | 1862 | return [NSString stringWithFormat:@"%@:%@:%@", (NSString *) type_, (NSString *) uri_, (NSString *) distribution_]; |
7b0ce2da JF |
1863 | } |
1864 | ||
1865 | - (NSString *) host { | |
f9f6d9e8 | 1866 | return host_; |
7b0ce2da JF |
1867 | } |
1868 | ||
1869 | - (NSString *) name { | |
c08c8943 | 1870 | return origin_.empty() ? (id) authority_ : origin_; |
7b0ce2da JF |
1871 | } |
1872 | ||
8d262908 | 1873 | - (NSString *) shortDescription { |
36bb2ca2 JF |
1874 | return description_; |
1875 | } | |
b4d89997 | 1876 | |
36bb2ca2 | 1877 | - (NSString *) label { |
c08c8943 | 1878 | return label_.empty() ? (id) authority_ : label_; |
36bb2ca2 | 1879 | } |
3178d79b | 1880 | |
36bb2ca2 JF |
1881 | - (NSString *) origin { |
1882 | return origin_; | |
1883 | } | |
3178d79b | 1884 | |
36bb2ca2 JF |
1885 | - (NSString *) version { |
1886 | return version_; | |
1887 | } | |
2367a917 | 1888 | |
36bb2ca2 JF |
1889 | - (NSString *) defaultIcon { |
1890 | return defaultIcon_; | |
1891 | } | |
2367a917 | 1892 | |
21ac0ce2 JF |
1893 | - (void) setDelegate:(NSObject<SourceDelegate> *)delegate { |
1894 | delegate_ = delegate; | |
1895 | } | |
1896 | ||
1897 | - (bool) fetch { | |
1898 | return !fetches_.empty(); | |
1899 | } | |
1900 | ||
1901 | - (void) setFetch:(bool)fetch forURI:(const char *)uri { | |
1902 | if (!fetch) { | |
1903 | if (fetches_.erase(uri) == 0) | |
1904 | return; | |
1905 | } else if (files_.find(uri) == files_.end()) | |
1906 | return; | |
1907 | else if (!fetches_.insert(uri).second) | |
1908 | return; | |
1909 | ||
1910 | [delegate_ performSelectorOnMainThread:@selector(setFetch:) withObject:[NSNumber numberWithBool:[self fetch]] waitUntilDone:NO]; | |
1911 | } | |
1912 | ||
9ed626f1 JF |
1913 | - (void) resetFetch { |
1914 | fetches_.clear(); | |
1915 | [delegate_ performSelectorOnMainThread:@selector(setFetch:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:NO]; | |
1916 | } | |
1917 | ||
2388b078 JF |
1918 | @end |
1919 | /* }}} */ | |
83682c75 JF |
1920 | /* CydiaOperation Class {{{ */ |
1921 | @interface CydiaOperation : NSObject { | |
7b33d201 JF |
1922 | _H<NSString> operator_; |
1923 | _H<NSString> value_; | |
2388b078 JF |
1924 | } |
1925 | ||
83682c75 JF |
1926 | - (NSString *) operator; |
1927 | - (NSString *) value; | |
2388b078 JF |
1928 | |
1929 | @end | |
1930 | ||
83682c75 | 1931 | @implementation CydiaOperation |
2388b078 | 1932 | |
83682c75 JF |
1933 | - (id) initWithOperator:(const char *)_operator value:(const char *)value { |
1934 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
1935 | operator_ = [NSString stringWithUTF8String:_operator]; |
1936 | value_ = [NSString stringWithUTF8String:value]; | |
83682c75 JF |
1937 | } return self; |
1938 | } | |
1939 | ||
1940 | + (NSArray *) _attributeKeys { | |
1941 | return [NSArray arrayWithObjects: | |
1942 | @"operator", | |
1943 | @"value", | |
1944 | nil]; | |
2388b078 JF |
1945 | } |
1946 | ||
83682c75 JF |
1947 | - (NSArray *) attributeKeys { |
1948 | return [[self class] _attributeKeys]; | |
2388b078 JF |
1949 | } |
1950 | ||
83682c75 JF |
1951 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
1952 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1953 | } | |
1954 | ||
1955 | - (NSString *) operator { | |
1956 | return operator_; | |
1957 | } | |
1958 | ||
1959 | - (NSString *) value { | |
1960 | return value_; | |
1961 | } | |
1962 | ||
1963 | @end | |
1964 | /* }}} */ | |
810c9763 JF |
1965 | /* CydiaClause Class {{{ */ |
1966 | @interface CydiaClause : NSObject { | |
7b33d201 JF |
1967 | _H<NSString> package_; |
1968 | _H<CydiaOperation> version_; | |
83682c75 JF |
1969 | } |
1970 | ||
83682c75 JF |
1971 | - (NSString *) package; |
1972 | - (CydiaOperation *) version; | |
1973 | ||
1974 | @end | |
1975 | ||
810c9763 | 1976 | @implementation CydiaClause |
83682c75 | 1977 | |
83682c75 JF |
1978 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
1979 | if ((self = [super init]) != nil) { | |
7b33d201 | 1980 | package_ = [NSString stringWithUTF8String:dep.TargetPkg().Name()]; |
83682c75 JF |
1981 | |
1982 | if (const char *version = dep.TargetVer()) | |
7b33d201 | 1983 | version_ = [[[CydiaOperation alloc] initWithOperator:dep.CompType() value:version] autorelease]; |
83682c75 | 1984 | else |
7b33d201 | 1985 | version_ = (id) [NSNull null]; |
83682c75 JF |
1986 | } return self; |
1987 | } | |
1988 | ||
1989 | + (NSArray *) _attributeKeys { | |
1990 | return [NSArray arrayWithObjects: | |
1991 | @"package", | |
83682c75 JF |
1992 | @"version", |
1993 | nil]; | |
1994 | } | |
1995 | ||
1996 | - (NSArray *) attributeKeys { | |
1997 | return [[self class] _attributeKeys]; | |
1998 | } | |
1999 | ||
2000 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2001 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2002 | } | |
2003 | ||
83682c75 JF |
2004 | - (NSString *) package { |
2005 | return package_; | |
2006 | } | |
2007 | ||
2008 | - (CydiaOperation *) version { | |
2009 | return version_; | |
2388b078 JF |
2010 | } |
2011 | ||
810c9763 JF |
2012 | @end |
2013 | /* }}} */ | |
2014 | /* CydiaRelation Class {{{ */ | |
2015 | @interface CydiaRelation : NSObject { | |
7b33d201 JF |
2016 | _H<NSString> relationship_; |
2017 | _H<NSMutableArray> clauses_; | |
810c9763 JF |
2018 | } |
2019 | ||
2020 | - (NSString *) relationship; | |
2021 | - (NSArray *) clauses; | |
2022 | ||
2023 | @end | |
2024 | ||
2025 | @implementation CydiaRelation | |
2026 | ||
810c9763 JF |
2027 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
2028 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
2029 | relationship_ = [NSString stringWithUTF8String:dep.DepType()]; |
2030 | clauses_ = [NSMutableArray arrayWithCapacity:8]; | |
810c9763 JF |
2031 | |
2032 | pkgCache::DepIterator start; | |
2033 | pkgCache::DepIterator end; | |
2034 | dep.GlobOr(start, end); // ++dep | |
2035 | ||
2036 | _forever { | |
2037 | [clauses_ addObject:[[[CydiaClause alloc] initWithIterator:start] autorelease]]; | |
2038 | ||
2039 | // yes, seriously. (wtf?) | |
2040 | if (start == end) | |
2041 | break; | |
2042 | ++start; | |
2043 | } | |
2044 | } return self; | |
2045 | } | |
2046 | ||
2047 | + (NSArray *) _attributeKeys { | |
2048 | return [NSArray arrayWithObjects: | |
2049 | @"clauses", | |
2050 | @"relationship", | |
2051 | nil]; | |
2052 | } | |
2053 | ||
2054 | - (NSArray *) attributeKeys { | |
2055 | return [[self class] _attributeKeys]; | |
2056 | } | |
2057 | ||
2058 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2059 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2060 | } | |
2061 | ||
2062 | - (NSString *) relationship { | |
2063 | return relationship_; | |
2064 | } | |
2065 | ||
2066 | - (NSArray *) clauses { | |
2067 | return clauses_; | |
2068 | } | |
2069 | ||
2070 | - (void) addClause:(CydiaClause *)clause { | |
2071 | [clauses_ addObject:clause]; | |
2072 | } | |
2073 | ||
dc5812ec | 2074 | @end |
b4d89997 | 2075 | /* }}} */ |
36bb2ca2 | 2076 | /* Package Class {{{ */ |
12016ee5 | 2077 | struct ParsedPackage { |
974742b2 | 2078 | CYString md5sum_; |
12016ee5 JF |
2079 | CYString tagline_; |
2080 | ||
a412f357 | 2081 | CYString architecture_; |
12016ee5 JF |
2082 | CYString icon_; |
2083 | ||
2084 | CYString depiction_; | |
2085 | CYString homepage_; | |
12016ee5 JF |
2086 | CYString author_; |
2087 | ||
12016ee5 JF |
2088 | CYString support_; |
2089 | }; | |
2090 | ||
36bb2ca2 | 2091 | @interface Package : NSObject { |
13902177 | 2092 | uint32_t era_ : 25; |
89bdef78 | 2093 | @public uint32_t role_ : 3; |
aab28f8b JF |
2094 | uint32_t essential_ : 1; |
2095 | uint32_t obsolete_ : 1; | |
2096 | uint32_t ignored_ : 1; | |
13902177 | 2097 | uint32_t pooled_ : 1; |
aab28f8b | 2098 | |
64edd9df | 2099 | CYPool *pool_; |
a1440b10 | 2100 | |
9c5737d5 JF |
2101 | uint32_t rank_; |
2102 | ||
aab28f8b JF |
2103 | _transient Database *database_; |
2104 | ||
7376b55c | 2105 | pkgCache::VerIterator version_; |
36bb2ca2 | 2106 | pkgCache::PkgIterator iterator_; |
36bb2ca2 | 2107 | pkgCache::VerFileIterator file_; |
bbb879fb | 2108 | |
aab28f8b JF |
2109 | CYString id_; |
2110 | CYString name_; | |
22fd24dd | 2111 | CYString transform_; |
807ae6d7 | 2112 | |
4c0ed943 | 2113 | CYString latest_; |
6a155117 | 2114 | CYString installed_; |
b3906a21 | 2115 | time_t upgraded_; |
dc5812ec | 2116 | |
fe33a23e | 2117 | const char *section_; |
aab28f8b | 2118 | _transient NSString *section$_; |
f79a4512 | 2119 | |
7b33d201 | 2120 | _H<Source> source_; |
2388b078 | 2121 | |
bb6bb6d6 | 2122 | PackageValue *metadata_; |
aab28f8b | 2123 | ParsedPackage *parsed_; |
94b0b3e5 | 2124 | |
7b33d201 | 2125 | _H<NSMutableArray> tags_; |
b4d89997 JF |
2126 | } |
2127 | ||
64edd9df JF |
2128 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; |
2129 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; | |
b4d89997 | 2130 | |
2388b078 | 2131 | - (pkgCache::PkgIterator) iterator; |
68d927e2 | 2132 | - (void) parse; |
2388b078 | 2133 | |
36bb2ca2 | 2134 | - (NSString *) section; |
dec6029f JF |
2135 | - (NSString *) simpleSection; |
2136 | ||
f79a4512 JF |
2137 | - (NSString *) longSection; |
2138 | - (NSString *) shortSection; | |
2139 | ||
a3328c28 JF |
2140 | - (NSString *) uri; |
2141 | ||
7aa82ca2 | 2142 | - (MIMEAddress *) maintainer; |
36bb2ca2 | 2143 | - (size_t) size; |
eef4ccaf JF |
2144 | - (NSString *) longDescription; |
2145 | - (NSString *) shortDescription; | |
808c6eb6 | 2146 | - (unichar) index; |
dc5812ec | 2147 | |
94b0b3e5 | 2148 | - (PackageValue *) metadata; |
31bc18a7 | 2149 | - (time_t) seen; |
94b0b3e5 JF |
2150 | |
2151 | - (bool) subscribed; | |
2152 | - (bool) setSubscribed:(bool)subscribed; | |
2153 | ||
807ae6d7 | 2154 | - (BOOL) ignored; |
9e98e020 | 2155 | |
36bb2ca2 JF |
2156 | - (NSString *) latest; |
2157 | - (NSString *) installed; | |
6a155117 | 2158 | - (BOOL) uninstalled; |
5a09ae08 | 2159 | |
31f3cfff | 2160 | - (BOOL) upgradableAndEssential:(BOOL)essential; |
36bb2ca2 JF |
2161 | - (BOOL) essential; |
2162 | - (BOOL) broken; | |
7d2ac47f | 2163 | - (BOOL) unfiltered; |
6d9712c4 | 2164 | - (BOOL) visible; |
b4d89997 | 2165 | |
9bedffaa JF |
2166 | - (BOOL) half; |
2167 | - (BOOL) halfConfigured; | |
2168 | - (BOOL) halfInstalled; | |
2169 | - (BOOL) hasMode; | |
2170 | - (NSString *) mode; | |
2171 | ||
36bb2ca2 JF |
2172 | - (NSString *) id; |
2173 | - (NSString *) name; | |
770f2a8e | 2174 | - (UIImage *) icon; |
6f1a15d9 | 2175 | - (NSString *) homepage; |
25a2158d | 2176 | - (NSString *) depiction; |
7aa82ca2 | 2177 | - (MIMEAddress *) author; |
b4d89997 | 2178 | |
dc63e78f JF |
2179 | - (NSString *) support; |
2180 | ||
affeffc7 | 2181 | - (NSArray *) files; |
affeffc7 JF |
2182 | - (NSArray *) warnings; |
2183 | - (NSArray *) applications; | |
2388b078 | 2184 | |
36bb2ca2 | 2185 | - (Source *) source; |
b4d89997 | 2186 | |
9c5737d5 | 2187 | - (uint32_t) rank; |
d84597fe | 2188 | - (BOOL) matches:(NSArray *)query; |
b4d89997 | 2189 | |
6d9712c4 | 2190 | - (BOOL) hasTag:(NSString *)tag; |
c390d3ab | 2191 | - (NSString *) primaryPurpose; |
770f2a8e | 2192 | - (NSArray *) purposes; |
3bd1c2a2 | 2193 | - (bool) isCommercial; |
6d9712c4 | 2194 | |
cd95e390 JF |
2195 | - (void) setIndex:(size_t)index; |
2196 | ||
df213583 JF |
2197 | - (CYString &) cyname; |
2198 | ||
f79a4512 | 2199 | - (uint32_t) compareBySection:(NSArray *)sections; |
807ae6d7 | 2200 | |
36bb2ca2 JF |
2201 | - (void) install; |
2202 | - (void) remove; | |
06aa974d | 2203 | |
36bb2ca2 | 2204 | @end |
b4d89997 | 2205 | |
f79a4512 JF |
2206 | uint32_t PackageChangesRadix(Package *self, void *) { |
2207 | union { | |
2208 | uint32_t key; | |
2209 | ||
2210 | struct { | |
2211 | uint32_t timestamp : 30; | |
2212 | uint32_t ignored : 1; | |
2213 | uint32_t upgradable : 1; | |
2214 | } bits; | |
2215 | } value; | |
2216 | ||
2217 | bool upgradable([self upgradableAndEssential:YES]); | |
2218 | value.bits.upgradable = upgradable ? 1 : 0; | |
2219 | ||
2220 | if (upgradable) { | |
2221 | value.bits.timestamp = 0; | |
2222 | value.bits.ignored = [self ignored] ? 0 : 1; | |
2223 | value.bits.upgradable = 1; | |
2224 | } else { | |
36047c66 | 2225 | value.bits.timestamp = [self seen] >> 2; |
f79a4512 JF |
2226 | value.bits.ignored = 0; |
2227 | value.bits.upgradable = 0; | |
2228 | } | |
2229 | ||
2230 | return _not(uint32_t) - value.key; | |
2231 | } | |
2232 | ||
aeeb755b JF |
2233 | CYString &(*PackageName)(Package *self, SEL sel); |
2234 | ||
df213583 JF |
2235 | uint32_t PackagePrefixRadix(Package *self, void *context) { |
2236 | size_t offset(reinterpret_cast<size_t>(context)); | |
aeeb755b | 2237 | CYString &name(PackageName(self, @selector(cyname))); |
677b8415 | 2238 | |
df213583 JF |
2239 | size_t size(name.size()); |
2240 | if (size == 0) | |
2241 | return 0; | |
2242 | char *text(name.data()); | |
677b8415 | 2243 | |
df213583 JF |
2244 | size_t zeros; |
2245 | if (!isdigit(text[0])) | |
2246 | zeros = 0; | |
2247 | else { | |
2248 | size_t digits(1); | |
2249 | while (size != digits && isdigit(text[digits])) | |
2250 | if (++digits == 4) | |
2251 | break; | |
2252 | zeros = 4 - digits; | |
2253 | } | |
677b8415 | 2254 | |
df213583 JF |
2255 | uint8_t data[4]; |
2256 | ||
df213583 JF |
2257 | if (offset == 0 && zeros != 0) { |
2258 | memset(data, '0', zeros); | |
2259 | memcpy(data + zeros, text, 4 - zeros); | |
2260 | } else { | |
2261 | /* XXX: there's some danger here if you request a non-zero offset < 4 and it gets zero padded */ | |
2262 | if (size <= offset - zeros) | |
2263 | return 0; | |
2264 | ||
2265 | text += offset - zeros; | |
2266 | size -= offset - zeros; | |
2267 | ||
2268 | if (size >= 4) | |
2269 | memcpy(data, text, 4); | |
2270 | else { | |
2271 | memcpy(data, text, size); | |
2272 | memset(data + size, 0, 4 - size); | |
2273 | } | |
2274 | ||
2275 | for (size_t i(0); i != 4; ++i) | |
2276 | if (isalpha(data[i])) | |
a7a59ee1 | 2277 | data[i] |= 0x20; |
df213583 JF |
2278 | } |
2279 | ||
2280 | if (offset == 0) | |
a7a59ee1 JF |
2281 | if (data[0] == '@') |
2282 | data[0] = 0x7f; | |
2283 | else | |
2284 | data[0] = (data[0] & 0x1f) | "\x80\x00\xc0\x40"[data[0] >> 6]; | |
df213583 JF |
2285 | |
2286 | /* XXX: ntohl may be more honest */ | |
2287 | return OSSwapInt32(*reinterpret_cast<uint32_t *>(data)); | |
2288 | } | |
2289 | ||
22fd24dd | 2290 | CFComparisonResult StringNameCompare(CFStringRef lhn, CFStringRef rhn, size_t length) { |
df213583 | 2291 | _profile(PackageNameCompare) |
5358f56f | 2292 | if (lhn == NULL) |
b129e6d9 | 2293 | return rhn == NULL ? kCFCompareEqualTo : kCFCompareLessThan; |
5358f56f | 2294 | else if (rhn == NULL) |
b129e6d9 | 2295 | return kCFCompareGreaterThan; |
5358f56f | 2296 | |
22fd24dd | 2297 | CFIndex length(CFStringGetLength(lhn)); |
aeeb755b | 2298 | |
677b8415 | 2299 | _profile(PackageNameCompare$NumbersLast) |
22fd24dd | 2300 | if (length != 0 && CFStringGetLength(rhn) != 0) { |
677b8415 JF |
2301 | UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); |
2302 | UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); | |
2303 | bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); | |
2304 | if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) | |
b129e6d9 | 2305 | return lha ? kCFCompareLessThan : kCFCompareGreaterThan; |
677b8415 JF |
2306 | } |
2307 | _end | |
2308 | ||
2309 | _profile(PackageNameCompare$Compare) | |
22fd24dd | 2310 | return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, (CFLocaleRef) (id) CollationLocale_); |
677b8415 JF |
2311 | _end |
2312 | _end | |
2313 | } | |
2314 | ||
22fd24dd JF |
2315 | _finline CFComparisonResult StringNameCompare(NSString *lhn, NSString*rhn, size_t length) { |
2316 | return StringNameCompare((CFStringRef) lhn, (CFStringRef) rhn, length); | |
2317 | } | |
2318 | ||
aeeb755b | 2319 | CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { |
22fd24dd JF |
2320 | CYString &lhn(PackageName(lhs, @selector(cyname))); |
2321 | NSString *rhn(PackageName(rhs, @selector(cyname))); | |
2322 | return StringNameCompare(lhn, rhn, lhn.size()); | |
aeeb755b JF |
2323 | } |
2324 | ||
2325 | CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *arg) { | |
2326 | return PackageNameCompare(*lhs, *rhs, arg); | |
677b8415 JF |
2327 | } |
2328 | ||
2329 | struct PackageNameOrdering : | |
2330 | std::binary_function<Package *, Package *, bool> | |
2331 | { | |
2332 | _finline bool operator ()(Package *lhs, Package *rhs) const { | |
b129e6d9 | 2333 | return PackageNameCompare(lhs, rhs, NULL) == kCFCompareLessThan; |
677b8415 JF |
2334 | } |
2335 | }; | |
2336 | ||
36bb2ca2 | 2337 | @implementation Package |
b4d89997 | 2338 | |
df213583 JF |
2339 | - (NSString *) description { |
2340 | return [NSString stringWithFormat:@"<Package:%@>", static_cast<NSString *>(name_)]; | |
2341 | } | |
2342 | ||
36bb2ca2 | 2343 | - (void) dealloc { |
13902177 | 2344 | if (!pooled_) |
64edd9df | 2345 | delete pool_; |
12016ee5 JF |
2346 | if (parsed_ != NULL) |
2347 | delete parsed_; | |
36bb2ca2 | 2348 | [super dealloc]; |
b4d89997 JF |
2349 | } |
2350 | ||
3bd1c2a2 | 2351 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 2352 | if (false); |
10d63f9e JF |
2353 | else if (selector == @selector(clear)) |
2354 | return @"clear"; | |
2cd1afd9 JF |
2355 | else if (selector == @selector(getField:)) |
2356 | return @"getField"; | |
8fb7c7a1 JF |
2357 | else if (selector == @selector(getRecord)) |
2358 | return @"getRecord"; | |
e58ff941 | 2359 | else if (selector == @selector(hasTag:)) |
3bd1c2a2 | 2360 | return @"hasTag"; |
10d63f9e JF |
2361 | else if (selector == @selector(install)) |
2362 | return @"install"; | |
2363 | else if (selector == @selector(remove)) | |
2364 | return @"remove"; | |
3bd1c2a2 JF |
2365 | else |
2366 | return nil; | |
2367 | } | |
2368 | ||
2369 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
2370 | return [self webScriptNameForSelector:selector] == nil; | |
2371 | } | |
2372 | ||
6f1a15d9 | 2373 | + (NSArray *) _attributeKeys { |
e58ff941 JF |
2374 | return [NSArray arrayWithObjects: |
2375 | @"applications", | |
a412f357 | 2376 | @"architecture", |
e58ff941 JF |
2377 | @"author", |
2378 | @"depiction", | |
2379 | @"essential", | |
2380 | @"homepage", | |
2381 | @"icon", | |
2382 | @"id", | |
2383 | @"installed", | |
2384 | @"latest", | |
2385 | @"longDescription", | |
2386 | @"longSection", | |
2387 | @"maintainer", | |
974742b2 | 2388 | @"md5sum", |
e58ff941 JF |
2389 | @"mode", |
2390 | @"name", | |
2391 | @"purposes", | |
83682c75 | 2392 | @"relations", |
e58ff941 | 2393 | @"section", |
de1ace71 | 2394 | @"selection", |
e58ff941 JF |
2395 | @"shortDescription", |
2396 | @"shortSection", | |
2397 | @"simpleSection", | |
2398 | @"size", | |
2399 | @"source", | |
5959b596 | 2400 | @"state", |
e58ff941 | 2401 | @"support", |
82aa2434 | 2402 | @"tags", |
1342f808 | 2403 | @"upgraded", |
e58ff941 JF |
2404 | @"warnings", |
2405 | nil]; | |
6f1a15d9 JF |
2406 | } |
2407 | ||
2408 | - (NSArray *) attributeKeys { | |
2409 | return [[self class] _attributeKeys]; | |
2410 | } | |
2411 | ||
2412 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2413 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2414 | } | |
2415 | ||
83682c75 JF |
2416 | - (NSArray *) relations { |
2417 | @synchronized (database_) { | |
2418 | NSMutableArray *relations([NSMutableArray arrayWithCapacity:16]); | |
810c9763 JF |
2419 | for (pkgCache::DepIterator dep(version_.DependsList()); !dep.end(); ++dep) |
2420 | [relations addObject:[[[CydiaRelation alloc] initWithIterator:dep] autorelease]]; | |
83682c75 JF |
2421 | return relations; |
2422 | } } | |
2423 | ||
a412f357 JF |
2424 | - (NSString *) architecture { |
2425 | [self parse]; | |
2426 | @synchronized (database_) { | |
2427 | return parsed_->architecture_.empty() ? [NSNull null] : (id) parsed_->architecture_; | |
2428 | } } | |
2429 | ||
2cd1afd9 JF |
2430 | - (NSString *) getField:(NSString *)name { |
2431 | @synchronized (database_) { | |
2432 | if ([database_ era] != era_ || file_.end()) | |
2433 | return nil; | |
2434 | ||
2435 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2436 | ||
2437 | const char *start, *end; | |
2438 | if (!parser.Find([name UTF8String], start, end)) | |
2439 | return (NSString *) [NSNull null]; | |
2440 | ||
b6f9e52a | 2441 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; |
2cd1afd9 JF |
2442 | } } |
2443 | ||
8fb7c7a1 JF |
2444 | - (NSString *) getRecord { |
2445 | @synchronized (database_) { | |
2446 | if ([database_ era] != era_ || file_.end()) | |
2447 | return nil; | |
2448 | ||
2449 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2450 | ||
2451 | const char *start, *end; | |
2452 | parser.GetRec(start, end); | |
2453 | ||
2454 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; | |
2455 | } } | |
2456 | ||
68d927e2 | 2457 | - (void) parse { |
12016ee5 | 2458 | if (parsed_ != NULL) |
68d927e2 | 2459 | return; |
12016ee5 JF |
2460 | @synchronized (database_) { |
2461 | if ([database_ era] != era_ || file_.end()) | |
68d927e2 JF |
2462 | return; |
2463 | ||
12016ee5 JF |
2464 | ParsedPackage *parsed(new ParsedPackage); |
2465 | parsed_ = parsed; | |
2466 | ||
68d927e2 JF |
2467 | _profile(Package$parse) |
2468 | pkgRecords::Parser *parser; | |
2469 | ||
2470 | _profile(Package$parse$Lookup) | |
2471 | parser = &[database_ records]->Lookup(file_); | |
2472 | _end | |
2473 | ||
00067a67 | 2474 | CYString bugs; |
68d927e2 JF |
2475 | CYString website; |
2476 | ||
2477 | _profile(Package$parse$Find) | |
2478 | struct { | |
2479 | const char *name_; | |
2480 | CYString *value_; | |
2481 | } names[] = { | |
a412f357 | 2482 | {"architecture", &parsed->architecture_}, |
12016ee5 JF |
2483 | {"icon", &parsed->icon_}, |
2484 | {"depiction", &parsed->depiction_}, | |
2485 | {"homepage", &parsed->homepage_}, | |
68d927e2 | 2486 | {"website", &website}, |
00067a67 | 2487 | {"bugs", &bugs}, |
12016ee5 | 2488 | {"support", &parsed->support_}, |
12016ee5 | 2489 | {"author", &parsed->author_}, |
974742b2 | 2490 | {"md5sum", &parsed->md5sum_}, |
68d927e2 JF |
2491 | }; |
2492 | ||
2493 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { | |
2494 | const char *start, *end; | |
2495 | ||
2496 | if (parser->Find(names[i].name_, start, end)) { | |
2497 | CYString &value(*names[i].value_); | |
2498 | _profile(Package$parse$Value) | |
2499 | value.set(pool_, start, end - start); | |
2500 | _end | |
2501 | } | |
2502 | } | |
2503 | _end | |
2504 | ||
2505 | _profile(Package$parse$Tagline) | |
2506 | const char *start, *end; | |
0dd0c302 | 2507 | if (parser->ShortDesc(start, end)) { |
68d927e2 JF |
2508 | const char *stop(reinterpret_cast<const char *>(memchr(start, '\n', end - start))); |
2509 | if (stop == NULL) | |
2510 | stop = end; | |
2511 | while (stop != start && stop[-1] == '\r') | |
2512 | --stop; | |
12016ee5 | 2513 | parsed->tagline_.set(pool_, start, stop - start); |
68d927e2 JF |
2514 | } |
2515 | _end | |
2516 | ||
2517 | _profile(Package$parse$Retain) | |
12016ee5 JF |
2518 | if (parsed->homepage_.empty()) |
2519 | parsed->homepage_ = website; | |
2520 | if (parsed->homepage_ == parsed->depiction_) | |
2521 | parsed->homepage_.clear(); | |
00067a67 JF |
2522 | if (parsed->support_.empty()) |
2523 | parsed->support_ = bugs; | |
68d927e2 JF |
2524 | _end |
2525 | _end | |
12016ee5 | 2526 | } } |
68d927e2 | 2527 | |
64edd9df | 2528 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { |
a1440b10 | 2529 | if ((self = [super init]) != nil) { |
7376b55c | 2530 | _profile(Package$initWithVersion) |
13902177 | 2531 | if (pool == NULL) |
64edd9df | 2532 | pool_ = new CYPool(); |
13902177 JF |
2533 | else { |
2534 | pool_ = pool; | |
2535 | pooled_ = true; | |
2536 | } | |
a1440b10 | 2537 | |
36bb2ca2 | 2538 | database_ = database; |
aab28f8b | 2539 | era_ = [database era]; |
b4d89997 | 2540 | |
aab28f8b | 2541 | version_ = version; |
2083b866 | 2542 | |
aab28f8b JF |
2543 | pkgCache::PkgIterator iterator(version.ParentPkg()); |
2544 | iterator_ = iterator; | |
06aa974d | 2545 | |
aab28f8b | 2546 | _profile(Package$initWithVersion$Version) |
826318ca | 2547 | file_ = version_.FileList(); |
68d927e2 | 2548 | _end |
808c6eb6 | 2549 | |
aab28f8b | 2550 | _profile(Package$initWithVersion$Cache) |
aab28f8b JF |
2551 | name_.set(NULL, iterator.Display()); |
2552 | ||
2553 | latest_.set(NULL, StripVersion_(version_.VerStr())); | |
2554 | ||
2555 | pkgCache::VerIterator current(iterator.CurrentVer()); | |
2556 | if (!current.end()) | |
2557 | installed_.set(NULL, StripVersion_(current.VerStr())); | |
808c6eb6 JF |
2558 | _end |
2559 | ||
22fd24dd JF |
2560 | _profile(Package$initWithVersion$Transliterate) do { |
2561 | if (CollationTransl_ == NULL) | |
2562 | break; | |
2563 | if (name_.empty()) | |
2564 | break; | |
2565 | ||
2566 | _profile(Package$initWithVersion$Transliterate$utf8) | |
2567 | const uint8_t *data(reinterpret_cast<const uint8_t *>(name_.data())); | |
2568 | for (size_t i(0), e(name_.size()); i != e; ++i) | |
2569 | if (data[i] >= 0x80) | |
2570 | goto extended; | |
2571 | break; extended:; | |
2572 | _end | |
2573 | ||
2574 | UErrorCode code(U_ZERO_ERROR); | |
2575 | int32_t length; | |
2576 | ||
2577 | _profile(Package$initWithVersion$Transliterate$u_strFromUTF8WithSub) | |
2578 | CollationString_.resize(name_.size()); | |
2579 | u_strFromUTF8WithSub(&CollationString_[0], CollationString_.size(), &length, name_.data(), name_.size(), 0xfffd, NULL, &code); | |
2580 | if (!U_SUCCESS(code)) | |
2581 | break; | |
2582 | CollationString_.resize(length); | |
2583 | _end | |
2584 | ||
2585 | _profile(Package$initWithVersion$Transliterate$utrans_trans) | |
2586 | length = CollationString_.size(); | |
2587 | utrans_trans(CollationTransl_, reinterpret_cast<UReplaceable *>(&CollationString_), &CollationUCalls_, 0, &length, &code); | |
2588 | if (!U_SUCCESS(code)) | |
2589 | break; | |
2590 | _assert(CollationString_.size() == length); | |
2591 | _end | |
2592 | ||
2593 | _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$preflight) | |
2594 | u_strToUTF8WithSub(NULL, 0, &length, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); | |
2595 | if (code == U_BUFFER_OVERFLOW_ERROR) | |
2596 | code = U_ZERO_ERROR; | |
2597 | else if (!U_SUCCESS(code)) | |
2598 | break; | |
2599 | _end | |
2600 | ||
2601 | char *transform; | |
2602 | _profile(Package$initWithVersion$Transliterate$apr_palloc) | |
64edd9df | 2603 | transform = pool_->malloc<char>(length); |
22fd24dd JF |
2604 | _end |
2605 | _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$transform) | |
2606 | u_strToUTF8WithSub(transform, length, NULL, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); | |
2607 | if (!U_SUCCESS(code)) | |
2608 | break; | |
2609 | _end | |
2610 | ||
2611 | transform_.set(NULL, transform, length); | |
2612 | } while (false); _end | |
2613 | ||
7376b55c | 2614 | _profile(Package$initWithVersion$Tags) |
aab28f8b | 2615 | pkgCache::TagIterator tag(iterator.TagList()); |
0dd0c302 | 2616 | if (!tag.end()) { |
7b33d201 | 2617 | tags_ = [NSMutableArray arrayWithCapacity:8]; |
5b625a2e JF |
2618 | |
2619 | goto tag; for (; !tag.end(); ++tag) tag: { | |
0dd0c302 | 2620 | const char *name(tag.Name()); |
5b625a2e JF |
2621 | NSString *string((NSString *) CYStringCreate(name)); |
2622 | if (string == nil) | |
2623 | continue; | |
2624 | ||
2625 | [tags_ addObject:[string autorelease]]; | |
0a377825 | 2626 | |
97a3d89e JF |
2627 | if (role_ == 0 && strncmp(name, "role::", 6) == 0 /*&& strcmp(name, "role::leaper") != 0*/) { |
2628 | if (strcmp(name + 6, "enduser") == 0) | |
2629 | role_ = 1; | |
2630 | else if (strcmp(name + 6, "hacker") == 0) | |
2631 | role_ = 2; | |
2632 | else if (strcmp(name + 6, "developer") == 0) | |
2633 | role_ = 3; | |
2634 | else if (strcmp(name + 6, "cydia") == 0) | |
2635 | role_ = 7; | |
2636 | else | |
2637 | role_ = 4; | |
2638 | } | |
0a377825 JF |
2639 | |
2640 | if (strncmp(name, "cydia::", 7) == 0) { | |
2641 | if (strcmp(name + 7, "essential") == 0) | |
2642 | essential_ = true; | |
2643 | else if (strcmp(name + 7, "obsolete") == 0) | |
2644 | obsolete_ = true; | |
2645 | } | |
5b625a2e | 2646 | } |
0dd0c302 | 2647 | } |
808c6eb6 | 2648 | _end |
7b0ce2da | 2649 | |
7376b55c | 2650 | _profile(Package$initWithVersion$Metadata) |
029c8b74 JF |
2651 | const char *mixed(iterator.Name()); |
2652 | size_t size(strlen(mixed)); | |
81bf4684 JF |
2653 | static const size_t prefix(sizeof("/var/lib/dpkg/info/") - 1); |
2654 | char lower[prefix + size + 5 + 1]; | |
029c8b74 JF |
2655 | |
2656 | for (size_t i(0); i != size; ++i) | |
81bf4684 JF |
2657 | lower[prefix + i] = mixed[i] | 0x20; |
2658 | ||
2659 | if (!installed_.empty()) { | |
2660 | memcpy(lower, "/var/lib/dpkg/info/", prefix); | |
2661 | memcpy(lower + prefix + size, ".list", 6); | |
2662 | struct stat info; | |
2663 | if (stat(lower, &info) != -1) | |
b3906a21 | 2664 | upgraded_ = info.st_birthtime; |
81bf4684 | 2665 | } |
029c8b74 | 2666 | |
81bf4684 | 2667 | PackageValue *metadata(PackageFind(lower + prefix, size)); |
bb6bb6d6 | 2668 | metadata_ = metadata; |
677b8415 | 2669 | |
029c8b74 JF |
2670 | id_.set(NULL, metadata->name_, size); |
2671 | ||
94b0b3e5 JF |
2672 | const char *latest(version_.VerStr()); |
2673 | size_t length(strlen(latest)); | |
a4b0ec52 | 2674 | |
94b0b3e5 | 2675 | uint16_t vhash(hashlittle(latest, length)); |
677b8415 | 2676 | |
94b0b3e5 JF |
2677 | size_t capped(std::min<size_t>(8, length)); |
2678 | latest = latest + length - capped; | |
677b8415 | 2679 | |
94b0b3e5 JF |
2680 | if (metadata->first_ == 0) |
2681 | metadata->first_ = now_; | |
808c6eb6 | 2682 | |
94b0b3e5 | 2683 | if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) { |
94b0b3e5 JF |
2684 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); |
2685 | metadata->vhash_ = vhash; | |
2f856365 JF |
2686 | metadata->last_ = now_; |
2687 | } else if (metadata->last_ == 0) | |
2688 | metadata->last_ = metadata->first_; | |
808c6eb6 | 2689 | _end |
a1440b10 | 2690 | |
7376b55c | 2691 | _profile(Package$initWithVersion$Section) |
0bc841de | 2692 | section_ = version_.Section(); |
f79a4512 | 2693 | _end |
a1440b10 | 2694 | |
aab28f8b JF |
2695 | _profile(Package$initWithVersion$Flags) |
2696 | essential_ |= ((iterator->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES); | |
2697 | ignored_ = iterator->SelectedState == pkgCache::State::Hold; | |
4fa77608 | 2698 | _end |
3dd53516 | 2699 | _end } return self; |
dc5812ec JF |
2700 | } |
2701 | ||
64edd9df | 2702 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { |
b1ce61ec JF |
2703 | pkgCache::VerIterator version; |
2704 | ||
2705 | _profile(Package$packageWithIterator$GetCandidateVer) | |
2706 | version = [database policy]->GetCandidateVer(iterator); | |
2707 | _end | |
2708 | ||
7376b55c JF |
2709 | if (version.end()) |
2710 | return nil; | |
b1ce61ec | 2711 | |
8564efc1 JF |
2712 | Package *package; |
2713 | ||
2714 | _profile(Package$packageWithIterator$Allocate) | |
2715 | package = [Package allocWithZone:zone]; | |
2716 | _end | |
2717 | ||
2718 | _profile(Package$packageWithIterator$Initialize) | |
2719 | package = [package | |
2720 | initWithVersion:version | |
2721 | withZone:zone | |
2722 | inPool:pool | |
2723 | database:database | |
2724 | ]; | |
2725 | _end | |
2726 | ||
2727 | _profile(Package$packageWithIterator$Autorelease) | |
2728 | package = [package autorelease]; | |
2729 | _end | |
2730 | ||
2731 | return package; | |
3dd53516 | 2732 | } |
dc5812ec | 2733 | |
2388b078 JF |
2734 | - (pkgCache::PkgIterator) iterator { |
2735 | return iterator_; | |
2736 | } | |
2737 | ||
c9f3aa21 JF |
2738 | - (NSArray *) downgrades { |
2739 | NSMutableArray *versions([NSMutableArray arrayWithCapacity:4]); | |
2740 | ||
2741 | for (auto version(iterator_.VersionList()); !version.end(); ++version) { | |
2742 | if (version == version_) | |
2743 | continue; | |
2744 | Package *package([[[Package allocWithZone:NULL] initWithVersion:version withZone:NULL inPool:NULL database:database_] autorelease]); | |
2745 | if ([package source] == nil) | |
2746 | continue; | |
2747 | [versions addObject:package]; | |
2748 | } | |
2749 | ||
2750 | return versions; | |
2751 | } | |
2752 | ||
36bb2ca2 | 2753 | - (NSString *) section { |
f79a4512 | 2754 | if (section$_ == nil) { |
fe33a23e | 2755 | if (section_ == NULL) |
f79a4512 JF |
2756 | return nil; |
2757 | ||
fe33a23e JF |
2758 | _profile(Package$section$mappedSectionForPointer) |
2759 | section$_ = [database_ mappedSectionForPointer:section_]; | |
e4f3d6e9 | 2760 | _end |
f79a4512 | 2761 | } return section$_; |
dc5812ec JF |
2762 | } |
2763 | ||
dec6029f JF |
2764 | - (NSString *) simpleSection { |
2765 | if (NSString *section = [self section]) | |
2766 | return Simplify(section); | |
2767 | else | |
2768 | return nil; | |
a3328c28 | 2769 | } |
dec6029f | 2770 | |
f79a4512 | 2771 | - (NSString *) longSection { |
18873623 | 2772 | return LocalizeSection([self section]); |
f79a4512 JF |
2773 | } |
2774 | ||
2775 | - (NSString *) shortSection { | |
2776 | return [[NSBundle mainBundle] localizedStringForKey:[self simpleSection] value:nil table:@"Sections"]; | |
2777 | } | |
2778 | ||
a3328c28 JF |
2779 | - (NSString *) uri { |
2780 | return nil; | |
2781 | #if 0 | |
2782 | pkgIndexFile *index; | |
2783 | pkgCache::PkgFileIterator file(file_.File()); | |
2784 | if (![database_ list].FindIndex(file, index)) | |
2785 | return nil; | |
2786 | return [NSString stringWithUTF8String:iterator_->Path]; | |
2787 | //return [NSString stringWithUTF8String:file.Site()]; | |
2788 | //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()]; | |
2789 | #endif | |
dec6029f JF |
2790 | } |
2791 | ||
7aa82ca2 | 2792 | - (MIMEAddress *) maintainer { |
884171d6 JF |
2793 | @synchronized (database_) { |
2794 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2795 | return nil; |
884171d6 | 2796 | |
36bb2ca2 | 2797 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
00e2109e | 2798 | const std::string &maintainer(parser->Maintainer()); |
7aa82ca2 | 2799 | return maintainer.empty() ? nil : [MIMEAddress addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]]; |
884171d6 | 2800 | } } |
8fe19fc1 | 2801 | |
974742b2 JF |
2802 | - (NSString *) md5sum { |
2803 | return parsed_ == NULL ? nil : (id) parsed_->md5sum_; | |
2804 | } | |
2805 | ||
36bb2ca2 | 2806 | - (size_t) size { |
884171d6 JF |
2807 | @synchronized (database_) { |
2808 | if ([database_ era] != era_ || version_.end()) | |
2809 | return 0; | |
2810 | ||
2811 | return version_->InstalledSize; | |
2812 | } } | |
dc5812ec | 2813 | |
eef4ccaf | 2814 | - (NSString *) longDescription { |
3dd53516 JF |
2815 | @synchronized (database_) { |
2816 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2817 | return nil; |
3dd53516 | 2818 | |
36bb2ca2 | 2819 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
2388b078 | 2820 | NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]); |
8fe19fc1 | 2821 | |
36bb2ca2 JF |
2822 | NSArray *lines = [description componentsSeparatedByString:@"\n"]; |
2823 | NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; | |
2824 | if ([lines count] < 2) | |
2825 | return nil; | |
3178d79b | 2826 | |
36bb2ca2 | 2827 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; |
c4dcf2c2 | 2828 | for (size_t i(1), e([lines count]); i != e; ++i) { |
36bb2ca2 JF |
2829 | NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; |
2830 | [trimmed addObject:trim]; | |
2831 | } | |
3178d79b | 2832 | |
36bb2ca2 | 2833 | return [trimmed componentsJoinedByString:@"\n"]; |
3dd53516 | 2834 | } } |
dc5812ec | 2835 | |
eef4ccaf | 2836 | - (NSString *) shortDescription { |
60bef540 JF |
2837 | if (parsed_ != NULL) |
2838 | return static_cast<NSString *>(parsed_->tagline_); | |
2839 | ||
2840 | @synchronized (database_) { | |
2841 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2842 | ||
2843 | const char *start, *end; | |
2844 | if (!parser.ShortDesc(start, end)) | |
2845 | return nil; | |
2846 | ||
abd93900 JF |
2847 | if (end - start > 200) |
2848 | end = start + 200; | |
60bef540 JF |
2849 | |
2850 | /* | |
2851 | if (const char *stop = reinterpret_cast<const char *>(memchr(start, '\n', end - start))) | |
2852 | end = stop; | |
2853 | ||
2854 | while (end != start && end[-1] == '\r') | |
2855 | --end; | |
2856 | */ | |
2857 | ||
2858 | return [(id) CYStringCreate(start, end - start) autorelease]; | |
2859 | } } | |
eef4ccaf | 2860 | |
808c6eb6 JF |
2861 | - (unichar) index { |
2862 | _profile(Package$index) | |
677b8415 JF |
2863 | CFStringRef name((CFStringRef) [self name]); |
2864 | if (CFStringGetLength(name) == 0) | |
808c6eb6 | 2865 | return '#'; |
677b8415 JF |
2866 | UniChar character(CFStringGetCharacterAtIndex(name, 0)); |
2867 | if (!CFUniCharIsMemberOf(character, kCFUniCharLetterCharacterSet)) | |
808c6eb6 | 2868 | return '#'; |
447db19d | 2869 | return toupper(character); |
808c6eb6 | 2870 | _end |
36bb2ca2 | 2871 | } |
3178d79b | 2872 | |
94b0b3e5 | 2873 | - (PackageValue *) metadata { |
bb6bb6d6 | 2874 | return metadata_; |
807ae6d7 JF |
2875 | } |
2876 | ||
31bc18a7 | 2877 | - (time_t) seen { |
94b0b3e5 JF |
2878 | PackageValue *metadata([self metadata]); |
2879 | return metadata->subscribed_ ? metadata->last_ : metadata->first_; | |
3178d79b JF |
2880 | } |
2881 | ||
94b0b3e5 JF |
2882 | - (bool) subscribed { |
2883 | return [self metadata]->subscribed_; | |
2884 | } | |
2885 | ||
2886 | - (bool) setSubscribed:(bool)subscribed { | |
2887 | PackageValue *metadata([self metadata]); | |
2888 | if (metadata->subscribed_ == subscribed) | |
2889 | return false; | |
2890 | metadata->subscribed_ = subscribed; | |
2891 | return true; | |
807ae6d7 JF |
2892 | } |
2893 | ||
2894 | - (BOOL) ignored { | |
1b18f026 | 2895 | return ignored_; |
807ae6d7 JF |
2896 | } |
2897 | ||
36bb2ca2 JF |
2898 | - (NSString *) latest { |
2899 | return latest_; | |
8fe19fc1 JF |
2900 | } |
2901 | ||
36bb2ca2 JF |
2902 | - (NSString *) installed { |
2903 | return installed_; | |
3178d79b JF |
2904 | } |
2905 | ||
6a155117 JF |
2906 | - (BOOL) uninstalled { |
2907 | return installed_.empty(); | |
2908 | } | |
2909 | ||
31f3cfff | 2910 | - (BOOL) upgradableAndEssential:(BOOL)essential { |
677b8415 JF |
2911 | _profile(Package$upgradableAndEssential) |
2912 | pkgCache::VerIterator current(iterator_.CurrentVer()); | |
2913 | if (current.end()) | |
e4f3d6e9 | 2914 | return essential && essential_; |
677b8415 | 2915 | else |
826318ca | 2916 | return version_ != current; |
677b8415 | 2917 | _end |
36bb2ca2 | 2918 | } |
3178d79b | 2919 | |
36bb2ca2 | 2920 | - (BOOL) essential { |
a1440b10 | 2921 | return essential_; |
3178d79b JF |
2922 | } |
2923 | ||
36bb2ca2 | 2924 | - (BOOL) broken { |
9bedffaa JF |
2925 | return [database_ cache][iterator_].InstBroken(); |
2926 | } | |
2927 | ||
7d2ac47f | 2928 | - (BOOL) unfiltered { |
4fa77608 | 2929 | _profile(Package$unfiltered$obsolete) |
cf48f656 | 2930 | if (_unlikely(obsolete_)) |
4fa77608 JF |
2931 | return false; |
2932 | _end | |
2933 | ||
4121c5e0 JF |
2934 | _profile(Package$unfiltered$role) |
2935 | if (_unlikely(role_ > 3)) | |
4fa77608 JF |
2936 | return false; |
2937 | _end | |
2938 | ||
e4f3d6e9 JF |
2939 | return true; |
2940 | } | |
4fa77608 | 2941 | |
e4f3d6e9 JF |
2942 | - (BOOL) visible { |
2943 | if (![self unfiltered]) | |
2944 | return false; | |
2945 | ||
fe33a23e JF |
2946 | NSString *section; |
2947 | ||
2948 | _profile(Package$visible$section) | |
2949 | section = [self section]; | |
2950 | _end | |
4fa77608 | 2951 | |
e4f3d6e9 | 2952 | _profile(Package$visible$isSectionVisible) |
45447dc3 | 2953 | if (!isSectionVisible(section)) |
4fa77608 JF |
2954 | return false; |
2955 | _end | |
2956 | ||
2957 | return true; | |
7d2ac47f JF |
2958 | } |
2959 | ||
9bedffaa | 2960 | - (BOOL) half { |
677b8415 | 2961 | unsigned char current(iterator_->CurrentState); |
9bedffaa JF |
2962 | return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled; |
2963 | } | |
2964 | ||
2965 | - (BOOL) halfConfigured { | |
2966 | return iterator_->CurrentState == pkgCache::State::HalfConfigured; | |
2967 | } | |
2968 | ||
2969 | - (BOOL) halfInstalled { | |
2970 | return iterator_->CurrentState == pkgCache::State::HalfInstalled; | |
2971 | } | |
2972 | ||
2973 | - (BOOL) hasMode { | |
f18b4a97 JF |
2974 | @synchronized (database_) { |
2975 | if ([database_ era] != era_ || iterator_.end()) | |
b129e6d9 | 2976 | return NO; |
f18b4a97 | 2977 | |
9bedffaa JF |
2978 | pkgDepCache::StateCache &state([database_ cache][iterator_]); |
2979 | return state.Mode != pkgDepCache::ModeKeep; | |
f18b4a97 | 2980 | } } |
9bedffaa JF |
2981 | |
2982 | - (NSString *) mode { | |
f18b4a97 JF |
2983 | @synchronized (database_) { |
2984 | if ([database_ era] != era_ || iterator_.end()) | |
2985 | return nil; | |
2986 | ||
9bedffaa JF |
2987 | pkgDepCache::StateCache &state([database_ cache][iterator_]); |
2988 | ||
2989 | switch (state.Mode) { | |
2990 | case pkgDepCache::ModeDelete: | |
2991 | if ((state.iFlags & pkgDepCache::Purge) != 0) | |
f79a4512 | 2992 | return @"PURGE"; |
9bedffaa | 2993 | else |
f79a4512 | 2994 | return @"REMOVE"; |
9bedffaa | 2995 | case pkgDepCache::ModeKeep: |
dc63e78f | 2996 | if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2997 | return @"REINSTALL"; |
dc63e78f JF |
2998 | /*else if ((state.iFlags & pkgDepCache::AutoKept) != 0) |
2999 | return nil;*/ | |
9bedffaa JF |
3000 | else |
3001 | return nil; | |
9bedffaa | 3002 | case pkgDepCache::ModeInstall: |
dc63e78f | 3003 | /*if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 3004 | return @"REINSTALL"; |
dc63e78f | 3005 | else*/ switch (state.Status) { |
9bedffaa | 3006 | case -1: |
f79a4512 | 3007 | return @"DOWNGRADE"; |
9bedffaa | 3008 | case 0: |
f79a4512 | 3009 | return @"INSTALL"; |
9bedffaa | 3010 | case 1: |
f79a4512 | 3011 | return @"UPGRADE"; |
9bedffaa | 3012 | case 2: |
f79a4512 | 3013 | return @"NEW_INSTALL"; |
670a0494 | 3014 | _nodefault |
9bedffaa | 3015 | } |
670a0494 | 3016 | _nodefault |
9bedffaa | 3017 | } |
f18b4a97 | 3018 | } } |
8fe19fc1 | 3019 | |
36bb2ca2 JF |
3020 | - (NSString *) id { |
3021 | return id_; | |
8fe19fc1 JF |
3022 | } |
3023 | ||
36bb2ca2 | 3024 | - (NSString *) name { |
9fcbca29 | 3025 | return name_.empty() ? id_ : name_; |
36bb2ca2 | 3026 | } |
8fe19fc1 | 3027 | |
770f2a8e | 3028 | - (UIImage *) icon { |
dec6029f | 3029 | NSString *section = [self simpleSection]; |
770f2a8e JF |
3030 | |
3031 | UIImage *icon(nil); | |
12016ee5 JF |
3032 | if (parsed_ != NULL) |
3033 | if (NSString *href = parsed_->icon_) | |
3034 | if ([href hasPrefix:@"file:///"]) | |
84851d87 | 3035 | icon = [UIImage imageAtPath:[[href substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; |
770f2a8e | 3036 | if (icon == nil) if (section != nil) |
84851d87 | 3037 | icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [section stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]; |
b9956841 | 3038 | if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon]) |
189a73d0 | 3039 | if ([dicon hasPrefix:@"file:///"]) |
84851d87 | 3040 | icon = [UIImage imageAtPath:[[dicon substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; |
770f2a8e | 3041 | if (icon == nil) |
8dbf3abc | 3042 | icon = [UIImage imageNamed:@"unknown.png"]; |
770f2a8e | 3043 | return icon; |
36bb2ca2 | 3044 | } |
8fe19fc1 | 3045 | |
6f1a15d9 | 3046 | - (NSString *) homepage { |
12016ee5 | 3047 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->homepage_); |
8fe19fc1 JF |
3048 | } |
3049 | ||
25a2158d | 3050 | - (NSString *) depiction { |
12016ee5 | 3051 | return parsed_ != NULL && !parsed_->depiction_.empty() ? parsed_->depiction_ : [[self source] depictionForPackage:id_]; |
25a2158d JF |
3052 | } |
3053 | ||
7aa82ca2 JF |
3054 | - (MIMEAddress *) author { |
3055 | return parsed_ == NULL || parsed_->author_.empty() ? nil : [MIMEAddress addressWithString:parsed_->author_]; | |
77fcccaf JF |
3056 | } |
3057 | ||
dc63e78f | 3058 | - (NSString *) support { |
00067a67 | 3059 | return parsed_ != NULL && !parsed_->support_.empty() ? parsed_->support_ : [[self source] supportForPackage:id_]; |
dc63e78f JF |
3060 | } |
3061 | ||
affeffc7 | 3062 | - (NSArray *) files { |
9fcbca29 | 3063 | NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", static_cast<NSString *>(id_)]; |
affeffc7 JF |
3064 | NSMutableArray *files = [NSMutableArray arrayWithCapacity:128]; |
3065 | ||
3066 | std::ifstream fin; | |
3067 | fin.open([path UTF8String]); | |
3068 | if (!fin.is_open()) | |
3069 | return nil; | |
3070 | ||
3071 | std::string line; | |
3072 | while (std::getline(fin, line)) | |
3073 | [files addObject:[NSString stringWithUTF8String:line.c_str()]]; | |
3074 | ||
3075 | return files; | |
3076 | } | |
3077 | ||
5959b596 JF |
3078 | - (NSString *) state { |
3079 | @synchronized (database_) { | |
3080 | if ([database_ era] != era_ || file_.end()) | |
3081 | return nil; | |
3082 | ||
3083 | switch (iterator_->CurrentState) { | |
3084 | case pkgCache::State::NotInstalled: | |
3085 | return @"NotInstalled"; | |
3086 | case pkgCache::State::UnPacked: | |
3087 | return @"UnPacked"; | |
3088 | case pkgCache::State::HalfConfigured: | |
3089 | return @"HalfConfigured"; | |
3090 | case pkgCache::State::HalfInstalled: | |
3091 | return @"HalfInstalled"; | |
3092 | case pkgCache::State::ConfigFiles: | |
3093 | return @"ConfigFiles"; | |
3094 | case pkgCache::State::Installed: | |
3095 | return @"Installed"; | |
3096 | case pkgCache::State::TriggersAwaited: | |
3097 | return @"TriggersAwaited"; | |
3098 | case pkgCache::State::TriggersPending: | |
3099 | return @"TriggersPending"; | |
3100 | } | |
3101 | ||
3102 | return (NSString *) [NSNull null]; | |
3103 | } } | |
3104 | ||
de1ace71 JF |
3105 | - (NSString *) selection { |
3106 | @synchronized (database_) { | |
3107 | if ([database_ era] != era_ || file_.end()) | |
3108 | return nil; | |
3109 | ||
3110 | switch (iterator_->SelectedState) { | |
3111 | case pkgCache::State::Unknown: | |
3112 | return @"Unknown"; | |
3113 | case pkgCache::State::Install: | |
3114 | return @"Install"; | |
3115 | case pkgCache::State::Hold: | |
3116 | return @"Hold"; | |
3117 | case pkgCache::State::DeInstall: | |
3118 | return @"DeInstall"; | |
3119 | case pkgCache::State::Purge: | |
3120 | return @"Purge"; | |
3121 | } | |
3122 | ||
3123 | return (NSString *) [NSNull null]; | |
3124 | } } | |
3125 | ||
affeffc7 | 3126 | - (NSArray *) warnings { |
d212b02f JF |
3127 | @synchronized (database_) { |
3128 | if ([database_ era] != era_ || file_.end()) | |
3129 | return nil; | |
3130 | ||
affeffc7 JF |
3131 | NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); |
3132 | const char *name(iterator_.Name()); | |
3133 | ||
3134 | size_t length(strlen(name)); | |
3135 | if (length < 2) invalid: | |
43f3d7f6 | 3136 | [warnings addObject:UCLocalize("ILLEGAL_PACKAGE_IDENTIFIER")]; |
affeffc7 JF |
3137 | else for (size_t i(0); i != length; ++i) |
3138 | if ( | |
9dd60d81 JF |
3139 | /* XXX: technically this is not allowed */ |
3140 | (name[i] < 'A' || name[i] > 'Z') && | |
affeffc7 JF |
3141 | (name[i] < 'a' || name[i] > 'z') && |
3142 | (name[i] < '0' || name[i] > '9') && | |
3143 | (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.') | |
3144 | ) goto invalid; | |
3145 | ||
3146 | if (strcmp(name, "cydia") != 0) { | |
3147 | bool cydia = false; | |
7623f855 | 3148 | bool user = false; |
9dd60d81 | 3149 | bool _private = false; |
affeffc7 | 3150 | bool stash = false; |
bcc58607 | 3151 | bool dbstash = false; |
1db5920d | 3152 | bool dsstore = false; |
affeffc7 | 3153 | |
9dd60d81 JF |
3154 | bool repository = [[self section] isEqualToString:@"Repositories"]; |
3155 | ||
affeffc7 JF |
3156 | if (NSArray *files = [self files]) |
3157 | for (NSString *file in files) | |
3158 | if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"]) | |
3159 | cydia = true; | |
7623f855 JF |
3160 | else if (!user && [file isEqualToString:@"/User"]) |
3161 | user = true; | |
9dd60d81 JF |
3162 | else if (!_private && [file isEqualToString:@"/private"]) |
3163 | _private = true; | |
affeffc7 JF |
3164 | else if (!stash && [file isEqualToString:@"/var/stash"]) |
3165 | stash = true; | |
bcc58607 JF |
3166 | else if (!dbstash && [file isEqualToString:@"/var/db/stash"]) |
3167 | dbstash = true; | |
1db5920d JF |
3168 | else if (!dsstore && [file hasSuffix:@"/.DS_Store"]) |
3169 | dsstore = true; | |
affeffc7 | 3170 | |
9dd60d81 JF |
3171 | /* XXX: this is not sensitive enough. only some folders are valid. */ |
3172 | if (cydia && !repository) | |
43f3d7f6 | 3173 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"Cydia.app"]]; |
7623f855 JF |
3174 | if (user) |
3175 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/User"]]; | |
9dd60d81 | 3176 | if (_private) |
43f3d7f6 | 3177 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; |
affeffc7 | 3178 | if (stash) |
43f3d7f6 | 3179 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; |
bcc58607 JF |
3180 | if (dbstash) |
3181 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/db/stash"]]; | |
1db5920d JF |
3182 | if (dsstore) |
3183 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @".DS_Store"]]; | |
affeffc7 JF |
3184 | } |
3185 | ||
3186 | return [warnings count] == 0 ? nil : warnings; | |
d212b02f | 3187 | } } |
affeffc7 JF |
3188 | |
3189 | - (NSArray *) applications { | |
3190 | NSString *me([[NSBundle mainBundle] bundleIdentifier]); | |
3191 | ||
3192 | NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); | |
3193 | ||
b4fd1197 | 3194 | static RegEx application_r("/Applications/(.*)\\.app/Info.plist"); |
affeffc7 JF |
3195 | if (NSArray *files = [self files]) |
3196 | for (NSString *file in files) | |
3197 | if (application_r(file)) { | |
3198 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); | |
75d2e426 JF |
3199 | if (info == nil) |
3200 | continue; | |
affeffc7 | 3201 | NSString *id([info objectForKey:@"CFBundleIdentifier"]); |
75d2e426 | 3202 | if (id == nil || [id isEqualToString:me]) |
affeffc7 JF |
3203 | continue; |
3204 | ||
3205 | NSString *display([info objectForKey:@"CFBundleDisplayName"]); | |
3206 | if (display == nil) | |
3207 | display = application_r[1]; | |
3208 | ||
3209 | NSString *bundle([file stringByDeletingLastPathComponent]); | |
3210 | NSString *icon([info objectForKey:@"CFBundleIconFile"]); | |
5e17a734 JF |
3211 | // XXX: maybe this should check if this is really a string, not just for length |
3212 | if (icon == nil || ![icon respondsToSelector:@selector(length)] || [icon length] == 0) | |
affeffc7 JF |
3213 | icon = @"icon.png"; |
3214 | NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]); | |
3215 | ||
3216 | NSMutableArray *application([NSMutableArray arrayWithCapacity:2]); | |
3217 | [applications addObject:application]; | |
3218 | ||
3219 | [application addObject:id]; | |
3220 | [application addObject:display]; | |
3221 | [application addObject:url]; | |
3222 | } | |
3223 | ||
3224 | return [applications count] == 0 ? nil : applications; | |
3225 | } | |
3226 | ||
36bb2ca2 | 3227 | - (Source *) source { |
5699667a | 3228 | if (source_ == nil) { |
a1440b10 JF |
3229 | @synchronized (database_) { |
3230 | if ([database_ era] != era_ || file_.end()) | |
5699667a JF |
3231 | source_ = (Source *) [NSNull null]; |
3232 | else | |
7b33d201 | 3233 | source_ = [database_ getSource:file_.File()] ?: (Source *) [NSNull null]; |
a1440b10 | 3234 | } |
bbb879fb JF |
3235 | } |
3236 | ||
5699667a | 3237 | return source_ == (Source *) [NSNull null] ? nil : source_; |
8fe19fc1 JF |
3238 | } |
3239 | ||
b3906a21 JF |
3240 | - (time_t) upgraded { |
3241 | return upgraded_; | |
98ddcefd JF |
3242 | } |
3243 | ||
b3906a21 JF |
3244 | - (uint32_t) recent { |
3245 | return std::numeric_limits<uint32_t>::max() - upgraded_; | |
821b1a0c JF |
3246 | } |
3247 | ||
9c5737d5 JF |
3248 | - (uint32_t) rank { |
3249 | return rank_; | |
3250 | } | |
3251 | ||
d84597fe JF |
3252 | - (BOOL) matches:(NSArray *)query { |
3253 | if (query == nil || [query count] == 0) | |
36bb2ca2 | 3254 | return NO; |
8fe19fc1 | 3255 | |
9c5737d5 JF |
3256 | rank_ = 0; |
3257 | ||
3258 | NSString *string; | |
36bb2ca2 | 3259 | NSRange range; |
9c5737d5 | 3260 | NSUInteger length; |
8fe19fc1 | 3261 | |
718c267c | 3262 | string = [self name]; |
d84597fe JF |
3263 | length = [string length]; |
3264 | ||
5af08e16 | 3265 | if (length != 0) |
d84597fe JF |
3266 | for (NSString *term in query) { |
3267 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
3268 | if (range.location != NSNotFound) | |
718c267c | 3269 | rank_ -= 6 * 1000000 / length; |
d84597fe | 3270 | } |
8fe19fc1 | 3271 | |
718c267c JF |
3272 | if (rank_ == 0) { |
3273 | string = [self id]; | |
3274 | length = [string length]; | |
8fe19fc1 | 3275 | |
5af08e16 | 3276 | if (length != 0) |
718c267c JF |
3277 | for (NSString *term in query) { |
3278 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
3279 | if (range.location != NSNotFound) | |
3280 | rank_ -= 6 * 1000000 / length; | |
3281 | } | |
d84597fe | 3282 | } |
c4b530a7 | 3283 | |
9c5737d5 JF |
3284 | string = [self shortDescription]; |
3285 | length = [string length]; | |
abd93900 | 3286 | NSUInteger stop(std::min<NSUInteger>(length, 200)); |
df289c5a | 3287 | |
5af08e16 | 3288 | if (length != 0) |
d84597fe JF |
3289 | for (NSString *term in query) { |
3290 | range = [string rangeOfString:term options:MatchCompareOptions_ range:NSMakeRange(0, stop)]; | |
3291 | if (range.location != NSNotFound) | |
718c267c | 3292 | rank_ -= 2 * 100000; |
d84597fe | 3293 | } |
8fe19fc1 | 3294 | |
9c5737d5 | 3295 | return rank_ != 0; |
36bb2ca2 | 3296 | } |
8fe19fc1 | 3297 | |
82aa2434 JF |
3298 | - (NSArray *) tags { |
3299 | return tags_; | |
3300 | } | |
3301 | ||
6d9712c4 JF |
3302 | - (BOOL) hasTag:(NSString *)tag { |
3303 | return tags_ == nil ? NO : [tags_ containsObject:tag]; | |
3304 | } | |
3305 | ||
c390d3ab | 3306 | - (NSString *) primaryPurpose { |
7b33d201 | 3307 | for (NSString *tag in (NSArray *) tags_) |
c390d3ab JF |
3308 | if ([tag hasPrefix:@"purpose::"]) |
3309 | return [tag substringFromIndex:9]; | |
3310 | return nil; | |
3311 | } | |
3312 | ||
770f2a8e JF |
3313 | - (NSArray *) purposes { |
3314 | NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]); | |
7b33d201 | 3315 | for (NSString *tag in (NSArray *) tags_) |
770f2a8e JF |
3316 | if ([tag hasPrefix:@"purpose::"]) |
3317 | [purposes addObject:[tag substringFromIndex:9]]; | |
3318 | return [purposes count] == 0 ? nil : purposes; | |
3319 | } | |
3320 | ||
3bd1c2a2 JF |
3321 | - (bool) isCommercial { |
3322 | return [self hasTag:@"cydia::commercial"]; | |
3323 | } | |
3324 | ||
cd95e390 JF |
3325 | - (void) setIndex:(size_t)index { |
3326 | if (metadata_->index_ != index) | |
3327 | metadata_->index_ = index; | |
3328 | } | |
3329 | ||
df213583 | 3330 | - (CYString &) cyname { |
22fd24dd | 3331 | return !transform_.empty() ? transform_ : !name_.empty() ? name_ : id_; |
f79a4512 JF |
3332 | } |
3333 | ||
f79a4512 JF |
3334 | - (uint32_t) compareBySection:(NSArray *)sections { |
3335 | NSString *section([self section]); | |
3336 | for (size_t i(0), e([sections count]); i != e; ++i) { | |
3337 | if ([section isEqualToString:[[sections objectAtIndex:i] name]]) | |
3338 | return i; | |
36bb2ca2 | 3339 | } |
3178d79b | 3340 | |
f79a4512 | 3341 | return _not(uint32_t); |
36bb2ca2 | 3342 | } |
3178d79b | 3343 | |
dc63e78f | 3344 | - (void) clear { |
c6ca67ba | 3345 | @synchronized (database_) { |
9fe109d1 JF |
3346 | if ([database_ era] != era_ || file_.end()) |
3347 | return; | |
3348 | ||
dc63e78f JF |
3349 | pkgProblemResolver *resolver = [database_ resolver]; |
3350 | resolver->Clear(iterator_); | |
c6ca67ba JF |
3351 | |
3352 | pkgCacheFile &cache([database_ cache]); | |
3353 | cache->SetReInstall(iterator_, false); | |
3354 | cache->MarkKeep(iterator_, false); | |
3355 | } } | |
dc63e78f | 3356 | |
36bb2ca2 | 3357 | - (void) install { |
c6ca67ba | 3358 | @synchronized (database_) { |
9fe109d1 JF |
3359 | if ([database_ era] != era_ || file_.end()) |
3360 | return; | |
3361 | ||
36bb2ca2 JF |
3362 | pkgProblemResolver *resolver = [database_ resolver]; |
3363 | resolver->Clear(iterator_); | |
3364 | resolver->Protect(iterator_); | |
c6ca67ba | 3365 | |
36bb2ca2 | 3366 | pkgCacheFile &cache([database_ cache]); |
c9f3aa21 | 3367 | cache->SetCandidateVersion(version_); |
c6ca67ba | 3368 | cache->SetReInstall(iterator_, false); |
36bb2ca2 | 3369 | cache->MarkInstall(iterator_, false); |
c6ca67ba | 3370 | |
36bb2ca2 JF |
3371 | pkgDepCache::StateCache &state((*cache)[iterator_]); |
3372 | if (!state.Install()) | |
3373 | cache->SetReInstall(iterator_, true); | |
c6ca67ba | 3374 | } } |
68a238ec | 3375 | |
36bb2ca2 | 3376 | - (void) remove { |
c6ca67ba | 3377 | @synchronized (database_) { |
9fe109d1 JF |
3378 | if ([database_ era] != era_ || file_.end()) |
3379 | return; | |
3380 | ||
36bb2ca2 JF |
3381 | pkgProblemResolver *resolver = [database_ resolver]; |
3382 | resolver->Clear(iterator_); | |
36bb2ca2 | 3383 | resolver->Remove(iterator_); |
c6ca67ba JF |
3384 | resolver->Protect(iterator_); |
3385 | ||
3386 | pkgCacheFile &cache([database_ cache]); | |
3387 | cache->SetReInstall(iterator_, false); | |
3388 | cache->MarkDelete(iterator_, true); | |
3389 | } } | |
8fe19fc1 | 3390 | |
36bb2ca2 JF |
3391 | @end |
3392 | /* }}} */ | |
3393 | /* Section Class {{{ */ | |
3394 | @interface Section : NSObject { | |
7b33d201 | 3395 | _H<NSString> name_; |
36bb2ca2 JF |
3396 | size_t row_; |
3397 | size_t count_; | |
7b33d201 | 3398 | _H<NSString> localized_; |
36bb2ca2 | 3399 | } |
3178d79b | 3400 | |
677b8415 | 3401 | - (NSComparisonResult) compareByLocalized:(Section *)section; |
9fcbca29 JF |
3402 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; |
3403 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; | |
3404 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; | |
aeeb755b | 3405 | |
36bb2ca2 | 3406 | - (NSString *) name; |
aeeb755b | 3407 | - (void) setName:(NSString *)name; |
f79a4512 | 3408 | |
36bb2ca2 JF |
3409 | - (size_t) row; |
3410 | - (size_t) count; | |
f79a4512 JF |
3411 | |
3412 | - (void) addToRow; | |
36bb2ca2 | 3413 | - (void) addToCount; |
b19871dd | 3414 | |
f79a4512 | 3415 | - (void) setCount:(size_t)count; |
677b8415 | 3416 | - (NSString *) localized; |
f79a4512 | 3417 | |
36bb2ca2 | 3418 | @end |
b19871dd | 3419 | |
36bb2ca2 | 3420 | @implementation Section |
b19871dd | 3421 | |
677b8415 | 3422 | - (NSComparisonResult) compareByLocalized:(Section *)section { |
9fcbca29 JF |
3423 | NSString *lhs(localized_); |
3424 | NSString *rhs([section localized]); | |
6d9712c4 | 3425 | |
9fcbca29 | 3426 | /*if ([lhs length] != 0 && [rhs length] != 0) { |
6d9712c4 JF |
3427 | unichar lhc = [lhs characterAtIndex:0]; |
3428 | unichar rhc = [rhs characterAtIndex:0]; | |
3429 | ||
3430 | if (isalpha(lhc) && !isalpha(rhc)) | |
3431 | return NSOrderedAscending; | |
3432 | else if (!isalpha(lhc) && isalpha(rhc)) | |
3433 | return NSOrderedDescending; | |
9fcbca29 | 3434 | }*/ |
6d9712c4 | 3435 | |
68f1828e | 3436 | return [lhs compare:rhs options:LaxCompareOptions_]; |
6d9712c4 JF |
3437 | } |
3438 | ||
9fcbca29 JF |
3439 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized { |
3440 | if ((self = [self initWithName:name localize:NO]) != nil) { | |
3441 | if (localized != nil) | |
7b33d201 | 3442 | localized_ = localized; |
9fcbca29 JF |
3443 | } return self; |
3444 | } | |
3445 | ||
3446 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize { | |
3447 | return [self initWithName:name row:0 localize:localize]; | |
6d9712c4 JF |
3448 | } |
3449 | ||
9fcbca29 | 3450 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { |
36bb2ca2 | 3451 | if ((self = [super init]) != nil) { |
7b33d201 | 3452 | name_ = name; |
808c6eb6 | 3453 | row_ = row; |
9fcbca29 | 3454 | if (localize) |
7b33d201 | 3455 | localized_ = LocalizeSection(name_); |
808c6eb6 JF |
3456 | } return self; |
3457 | } | |
3458 | ||
36bb2ca2 JF |
3459 | - (NSString *) name { |
3460 | return name_; | |
3461 | } | |
3462 | ||
aeeb755b JF |
3463 | - (void) setName:(NSString *)name { |
3464 | name_ = name; | |
808c6eb6 JF |
3465 | } |
3466 | ||
36bb2ca2 JF |
3467 | - (size_t) row { |
3468 | return row_; | |
3469 | } | |
3470 | ||
3471 | - (size_t) count { | |
3472 | return count_; | |
3473 | } | |
3474 | ||
f79a4512 JF |
3475 | - (void) addToRow { |
3476 | ++row_; | |
3477 | } | |
3478 | ||
36bb2ca2 JF |
3479 | - (void) addToCount { |
3480 | ++count_; | |
3481 | } | |
3482 | ||
f79a4512 JF |
3483 | - (void) setCount:(size_t)count { |
3484 | count_ = count; | |
3485 | } | |
3486 | ||
3487 | - (NSString *) localized { | |
3488 | return localized_; | |
3489 | } | |
3490 | ||
b19871dd JF |
3491 | @end |
3492 | /* }}} */ | |
3493 | ||
9f9ae81c JF |
3494 | class CydiaLogCleaner : |
3495 | public pkgArchiveCleaner | |
3496 | { | |
3497 | protected: | |
3498 | virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) { | |
3499 | unlink(File); | |
3500 | } | |
3501 | }; | |
3502 | ||
36bb2ca2 JF |
3503 | /* Database Implementation {{{ */ |
3504 | @implementation Database | |
ec97ef06 | 3505 | |
770f2a8e | 3506 | + (Database *) sharedInstance { |
7b33d201 | 3507 | static _H<Database> instance; |
770f2a8e | 3508 | if (instance == nil) |
7b33d201 | 3509 | instance = [[[Database alloc] init] autorelease]; |
770f2a8e JF |
3510 | return instance; |
3511 | } | |
3512 | ||
a1440b10 JF |
3513 | - (unsigned) era { |
3514 | return era_; | |
3515 | } | |
3516 | ||
0944377b JF |
3517 | - (void) releasePackages { |
3518 | CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL); | |
3519 | CFArrayRemoveAllValues(packages_); | |
3520 | } | |
3521 | ||
36bb2ca2 | 3522 | - (void) dealloc { |
3931b718 | 3523 | // XXX: actually implement this thing |
36bb2ca2 | 3524 | _assert(false); |
0944377b | 3525 | [self releasePackages]; |
0944377b | 3526 | NSRecycleZone(zone_); |
36bb2ca2 JF |
3527 | [super dealloc]; |
3528 | } | |
ec97ef06 | 3529 | |
d13edf44 | 3530 | - (void) _readCydia:(NSNumber *)fd { |
77fcccaf JF |
3531 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3532 | std::istream is(&ib); | |
3533 | std::string line; | |
3534 | ||
b4fd1197 | 3535 | static RegEx finish_r("finish:([^:]*)"); |
bc8cd583 | 3536 | |
77fcccaf | 3537 | while (std::getline(is, line)) { |
d13edf44 JF |
3538 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3539 | ||
77fcccaf | 3540 | const char *data(line.c_str()); |
bc8cd583 | 3541 | size_t size = line.size(); |
c390d3ab | 3542 | lprintf("C:%s\n", data); |
bc8cd583 JF |
3543 | |
3544 | if (finish_r(data, size)) { | |
3545 | NSString *finish = finish_r[1]; | |
3546 | int index = [Finishes_ indexOfObject:finish]; | |
3547 | if (index != INT_MAX && index > Finish_) | |
3548 | Finish_ = index; | |
3549 | } | |
d13edf44 JF |
3550 | |
3551 | [pool release]; | |
77fcccaf JF |
3552 | } |
3553 | ||
670a0494 | 3554 | _assume(false); |
77fcccaf JF |
3555 | } |
3556 | ||
d13edf44 | 3557 | - (void) _readStatus:(NSNumber *)fd { |
36bb2ca2 JF |
3558 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3559 | std::istream is(&ib); | |
3560 | std::string line; | |
ec97ef06 | 3561 | |
b4fd1197 JF |
3562 | static RegEx conffile_r("status: [^ ]* : conffile-prompt : (.*?) *"); |
3563 | static RegEx pmstatus_r("([^:]*):([^:]*):([^:]*):(.*)"); | |
ec97ef06 | 3564 | |
36bb2ca2 | 3565 | while (std::getline(is, line)) { |
d13edf44 JF |
3566 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3567 | ||
36bb2ca2 | 3568 | const char *data(line.c_str()); |
670a0494 | 3569 | size_t size(line.size()); |
c390d3ab | 3570 | lprintf("S:%s\n", data); |
ec97ef06 | 3571 | |
a08145a8 JF |
3572 | if (conffile_r(data, size)) { |
3573 | // status: /fail : conffile-prompt : '/fail' '/fail.dpkg-new' 1 1 | |
4187453f | 3574 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:conffile_r[1] waitUntilDone:YES]; |
a08145a8 JF |
3575 | } else if (strncmp(data, "status: ", 8) == 0) { |
3576 | // status: <package>: {unpacked,half-configured,installed} | |
389133be | 3577 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 8)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 JF |
3578 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3579 | } else if (strncmp(data, "processing: ", 12) == 0) { | |
3580 | // processing: configure: config-test | |
389133be | 3581 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 12)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 | 3582 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
6915b806 | 3583 | } else if (pmstatus_r(data, size)) { |
31f3cfff | 3584 | std::string type([pmstatus_r[1] UTF8String]); |
4a2dc82e | 3585 | |
6915b806 | 3586 | NSString *package = pmstatus_r[2]; |
4a2dc82e JF |
3587 | if ([package isEqualToString:@"dpkg-exec"]) |
3588 | package = nil; | |
31f3cfff | 3589 | |
5a09ae08 | 3590 | float percent([pmstatus_r[3] floatValue]); |
6915b806 | 3591 | [progress_ performSelectorOnMainThread:@selector(setProgressPercent:) withObject:[NSNumber numberWithFloat:(percent / 100)] waitUntilDone:YES]; |
5a09ae08 JF |
3592 | |
3593 | NSString *string = pmstatus_r[4]; | |
5a09ae08 | 3594 | |
6915b806 | 3595 | if (type == "pmerror") { |
389133be | 3596 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeError forPackage:package]); |
6915b806 JF |
3597 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3598 | } else if (type == "pmstatus") { | |
389133be | 3599 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeStatus forPackage:package]); |
6915b806 JF |
3600 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3601 | } else if (type == "pmconffile") | |
4187453f | 3602 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:string waitUntilDone:YES]; |
670a0494 JF |
3603 | else |
3604 | lprintf("E:unknown pmstatus\n"); | |
3605 | } else | |
3606 | lprintf("E:unknown status\n"); | |
d13edf44 JF |
3607 | |
3608 | [pool release]; | |
36bb2ca2 | 3609 | } |
ec97ef06 | 3610 | |
670a0494 | 3611 | _assume(false); |
36bb2ca2 | 3612 | } |
ec97ef06 | 3613 | |
d13edf44 | 3614 | - (void) _readOutput:(NSNumber *)fd { |
36bb2ca2 JF |
3615 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3616 | std::istream is(&ib); | |
3617 | std::string line; | |
3618 | ||
5a09ae08 | 3619 | while (std::getline(is, line)) { |
d13edf44 JF |
3620 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3621 | ||
c390d3ab | 3622 | lprintf("O:%s\n", line.c_str()); |
27024935 | 3623 | |
389133be | 3624 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:line.c_str()] ofType:kCydiaProgressEventTypeInformation]); |
6915b806 | 3625 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
d13edf44 JF |
3626 | |
3627 | [pool release]; | |
5a09ae08 | 3628 | } |
36bb2ca2 | 3629 | |
670a0494 | 3630 | _assume(false); |
ec97ef06 JF |
3631 | } |
3632 | ||
8b29f8e6 JF |
3633 | - (FILE *) input { |
3634 | return input_; | |
3635 | } | |
3636 | ||
36bb2ca2 | 3637 | - (Package *) packageWithName:(NSString *)name { |
c5f1a937 JF |
3638 | if (name == nil) |
3639 | return nil; | |
3dd53516 | 3640 | @synchronized (self) { |
5a09ae08 JF |
3641 | if (static_cast<pkgDepCache *>(cache_) == NULL) |
3642 | return nil; | |
36bb2ca2 | 3643 | pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); |
13902177 | 3644 | return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:NULL database:self]; |
28ce8704 | 3645 | } } |
36bb2ca2 | 3646 | |
eb30da80 | 3647 | - (id) init { |
ec97ef06 | 3648 | if ((self = [super init]) != nil) { |
5a09ae08 | 3649 | policy_ = NULL; |
36bb2ca2 JF |
3650 | records_ = NULL; |
3651 | resolver_ = NULL; | |
3652 | fetcher_ = NULL; | |
3653 | lock_ = NULL; | |
ec97ef06 | 3654 | |
f79a4512 | 3655 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3656 | |
9f357d11 JF |
3657 | size_t capacity(MetaFile_->active_); |
3658 | if (capacity == 0) | |
3659 | capacity = 16384; | |
3660 | else | |
3661 | capacity += 1024; | |
3662 | ||
3663 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, capacity, NULL); | |
7b33d201 | 3664 | sourceList_ = [NSMutableArray arrayWithCapacity:16]; |
ec97ef06 | 3665 | |
36bb2ca2 | 3666 | int fds[2]; |
ec97ef06 | 3667 | |
77fcccaf JF |
3668 | _assert(pipe(fds) != -1); |
3669 | cydiafd_ = fds[1]; | |
3670 | ||
3671 | _config->Set("APT::Keep-Fds::", cydiafd_); | |
bc8cd583 | 3672 | setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int)); |
77fcccaf JF |
3673 | |
3674 | [NSThread | |
3675 | detachNewThreadSelector:@selector(_readCydia:) | |
3676 | toTarget:self | |
3931b718 | 3677 | withObject:[NSNumber numberWithInt:fds[0]] |
77fcccaf JF |
3678 | ]; |
3679 | ||
36bb2ca2 JF |
3680 | _assert(pipe(fds) != -1); |
3681 | statusfd_ = fds[1]; | |
ec97ef06 | 3682 | |
36bb2ca2 JF |
3683 | [NSThread |
3684 | detachNewThreadSelector:@selector(_readStatus:) | |
3685 | toTarget:self | |
3931b718 | 3686 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3687 | ]; |
ec97ef06 | 3688 | |
8b29f8e6 JF |
3689 | _assert(pipe(fds) != -1); |
3690 | _assert(dup2(fds[0], 0) != -1); | |
3691 | _assert(close(fds[0]) != -1); | |
3692 | ||
3693 | input_ = fdopen(fds[1], "a"); | |
3694 | ||
36bb2ca2 JF |
3695 | _assert(pipe(fds) != -1); |
3696 | _assert(dup2(fds[1], 1) != -1); | |
3697 | _assert(close(fds[1]) != -1); | |
3698 | ||
3699 | [NSThread | |
3700 | detachNewThreadSelector:@selector(_readOutput:) | |
3701 | toTarget:self | |
3931b718 | 3702 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3703 | ]; |
ec97ef06 JF |
3704 | } return self; |
3705 | } | |
3706 | ||
36bb2ca2 JF |
3707 | - (pkgCacheFile &) cache { |
3708 | return cache_; | |
ec97ef06 JF |
3709 | } |
3710 | ||
5a09ae08 JF |
3711 | - (pkgDepCache::Policy *) policy { |
3712 | return policy_; | |
3713 | } | |
3714 | ||
36bb2ca2 JF |
3715 | - (pkgRecords *) records { |
3716 | return records_; | |
ec97ef06 JF |
3717 | } |
3718 | ||
36bb2ca2 JF |
3719 | - (pkgProblemResolver *) resolver { |
3720 | return resolver_; | |
ec97ef06 JF |
3721 | } |
3722 | ||
36bb2ca2 JF |
3723 | - (pkgAcquire &) fetcher { |
3724 | return *fetcher_; | |
ec97ef06 JF |
3725 | } |
3726 | ||
a3328c28 JF |
3727 | - (pkgSourceList &) list { |
3728 | return *list_; | |
3729 | } | |
3730 | ||
36bb2ca2 | 3731 | - (NSArray *) packages { |
077e9d90 | 3732 | return (NSArray *) packages_; |
ec97ef06 JF |
3733 | } |
3734 | ||
7b0ce2da | 3735 | - (NSArray *) sources { |
34f70f5d | 3736 | return sourceList_; |
7b0ce2da JF |
3737 | } |
3738 | ||
d669236d GP |
3739 | - (Source *) sourceWithKey:(NSString *)key { |
3740 | for (Source *source in [self sources]) { | |
3741 | if ([[source key] isEqualToString:key]) | |
3742 | return source; | |
3743 | } return nil; | |
3744 | } | |
3745 | ||
670a0494 JF |
3746 | - (bool) popErrorWithTitle:(NSString *)title { |
3747 | bool fatal(false); | |
670a0494 JF |
3748 | |
3749 | while (!_error->empty()) { | |
3750 | std::string error; | |
3751 | bool warning(!_error->PopMessage(error)); | |
3752 | if (!warning) | |
3753 | fatal = true; | |
cb6e2ccf | 3754 | |
670a0494 JF |
3755 | for (;;) { |
3756 | size_t size(error.size()); | |
3757 | if (size == 0 || error[size - 1] != '\n') | |
3758 | break; | |
3759 | error.resize(size - 1); | |
3760 | } | |
cb6e2ccf | 3761 | |
670a0494 JF |
3762 | lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); |
3763 | ||
b4fd1197 | 3764 | static RegEx no_pubkey("GPG error:.* NO_PUBKEY .*"); |
c8ce71c7 JF |
3765 | if (warning && no_pubkey(error.c_str())) |
3766 | continue; | |
3767 | ||
389133be | 3768 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
670a0494 JF |
3769 | } |
3770 | ||
670a0494 JF |
3771 | return fatal; |
3772 | } | |
3773 | ||
3774 | - (bool) popErrorWithTitle:(NSString *)title forOperation:(bool)success { | |
3775 | return [self popErrorWithTitle:title] || !success; | |
3776 | } | |
3777 | ||
7bc0d825 | 3778 | - (bool) popErrorWithTitle:(NSString *)title forReadList:(pkgSourceList &)list { |
19cd1e0d JF |
3779 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3780 | return true; | |
3781 | return false; | |
3782 | ||
57cc2170 JF |
3783 | list.Reset(); |
3784 | ||
3785 | bool error(false); | |
3786 | ||
3787 | if (access("/etc/apt/sources.list", F_OK) == 0) | |
3788 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend("/etc/apt/sources.list")]; | |
3789 | ||
3790 | std::string base("/etc/apt/sources.list.d"); | |
3791 | if (DIR *sources = opendir(base.c_str())) { | |
3792 | while (dirent *source = readdir(sources)) | |
3793 | if (source->d_name[0] != '.' && source->d_namlen > 5 && strcmp(source->d_name + source->d_namlen - 5, ".list") == 0 && strcmp(source->d_name, "cydia.list") != 0) | |
3794 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend((base + "/" + source->d_name).c_str())]; | |
3795 | closedir(sources); | |
3796 | } | |
3797 | ||
3798 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend(SOURCES_LIST)]; | |
3799 | ||
3800 | return error; | |
7bc0d825 JF |
3801 | } |
3802 | ||
d13edf44 | 3803 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
3dd53516 JF |
3804 | @synchronized (self) { |
3805 | ++era_; | |
a1440b10 | 3806 | |
0944377b | 3807 | [self releasePackages]; |
ab3f6a01 | 3808 | |
34f70f5d JF |
3809 | sourceMap_.clear(); |
3810 | [sourceList_ removeAllObjects]; | |
843e75b8 | 3811 | |
36bb2ca2 | 3812 | _error->Discard(); |
5a09ae08 | 3813 | |
36bb2ca2 | 3814 | delete list_; |
5a09ae08 | 3815 | list_ = NULL; |
36bb2ca2 JF |
3816 | manager_ = NULL; |
3817 | delete lock_; | |
5a09ae08 | 3818 | lock_ = NULL; |
36bb2ca2 | 3819 | delete fetcher_; |
5a09ae08 | 3820 | fetcher_ = NULL; |
36bb2ca2 | 3821 | delete resolver_; |
5a09ae08 | 3822 | resolver_ = NULL; |
36bb2ca2 | 3823 | delete records_; |
5a09ae08 JF |
3824 | records_ = NULL; |
3825 | delete policy_; | |
3826 | policy_ = NULL; | |
36bb2ca2 | 3827 | |
5a09ae08 | 3828 | cache_.Close(); |
8b29f8e6 | 3829 | |
64edd9df JF |
3830 | pool_.~CYPool(); |
3831 | new (&pool_) CYPool(); | |
a020a50e | 3832 | |
f79a4512 | 3833 | NSRecycleZone(zone_); |
a020a50e | 3834 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3835 | |
7376b55c JF |
3836 | int chk(creat("/tmp/cydia.chk", 0644)); |
3837 | if (chk != -1) | |
3838 | close(chk); | |
3839 | ||
4ba8f30a JF |
3840 | if (invocation != nil) |
3841 | [invocation invoke]; | |
3842 | ||
670a0494 JF |
3843 | NSString *title(UCLocalize("DATABASE")); |
3844 | ||
0c28a403 | 3845 | list_ = new pkgSourceList(); |
1a83afc6 | 3846 | _profile(reloadDataWithInvocation$ReadMainList) |
7bc0d825 | 3847 | if ([self popErrorWithTitle:title forReadList:*list_]) |
0c28a403 | 3848 | return; |
1a83afc6 | 3849 | _end |
0c28a403 | 3850 | |
1a83afc6 | 3851 | _profile(reloadDataWithInvocation$Source$initWithMetaIndex) |
0c28a403 | 3852 | for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { |
64edd9df | 3853 | Source *object([[[Source alloc] initWithMetaIndex:*source forDatabase:self inPool:&pool_] autorelease]); |
0c28a403 JF |
3854 | [sourceList_ addObject:object]; |
3855 | } | |
1a83afc6 | 3856 | _end |
0c28a403 | 3857 | |
9487f027 | 3858 | _trace(); |
124cea03 | 3859 | OpProgress progress; |
1a83afc6 | 3860 | bool opened; |
2b49cff9 | 3861 | open: |
5cf267f4 | 3862 | delock_ = GetStatusDate(); |
1a83afc6 | 3863 | _profile(reloadDataWithInvocation$pkgCacheFile) |
b44af625 | 3864 | opened = cache_.Open(progress, false); |
1a83afc6 JF |
3865 | _end |
3866 | if (!opened) { | |
2b49cff9 JF |
3867 | // XXX: what if there are errors, but Open() == true? this should be merged with popError: |
3868 | while (!_error->empty()) { | |
3869 | std::string error; | |
3870 | bool warning(!_error->PopMessage(error)); | |
3871 | ||
3872 | lprintf("cache_.Open():[%s]\n", error.c_str()); | |
3873 | ||
389133be | 3874 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
2b49cff9 JF |
3875 | |
3876 | SEL repair(NULL); | |
3877 | if (false); | |
3878 | else if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ") | |
3879 | repair = @selector(configure); | |
3880 | //else if (error == "The package lists or status file could not be parsed or opened.") | |
3881 | // repair = @selector(update); | |
3882 | // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)") | |
3883 | // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)") | |
3884 | // else if (error == "Malformed Status line") | |
3885 | // else if (error == "The list of sources could not be read.") | |
3886 | ||
3887 | if (repair != NULL) { | |
3888 | _error->Discard(); | |
3889 | [delegate_ repairWithSelector:repair]; | |
3890 | goto open; | |
3891 | } | |
efa53fa9 | 3892 | } |
5a09ae08 | 3893 | |
2b49cff9 | 3894 | return; |
36bb2ca2 | 3895 | } |
9487f027 | 3896 | _trace(); |
36bb2ca2 | 3897 | |
7376b55c JF |
3898 | unlink("/tmp/cydia.chk"); |
3899 | ||
31bc18a7 | 3900 | now_ = [[NSDate date] timeIntervalSince1970]; |
36bb2ca2 | 3901 | |
5a09ae08 | 3902 | policy_ = new pkgDepCache::Policy(); |
36bb2ca2 JF |
3903 | records_ = new pkgRecords(cache_); |
3904 | resolver_ = new pkgProblemResolver(cache_); | |
3905 | fetcher_ = new pkgAcquire(&status_); | |
3906 | lock_ = NULL; | |
3907 | ||
670a0494 | 3908 | if (cache_->DelCount() != 0 || cache_->InstCount() != 0) { |
389133be | 3909 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("COUNTS_NONZERO_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3910 | return; |
3911 | } | |
36bb2ca2 | 3912 | |
1a83afc6 | 3913 | _profile(reloadDataWithInvocation$pkgApplyStatus) |
670a0494 JF |
3914 | if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) |
3915 | return; | |
1a83afc6 | 3916 | _end |
9bedffaa JF |
3917 | |
3918 | if (cache_->BrokenCount() != 0) { | |
1a83afc6 | 3919 | _profile(pkgApplyStatus$pkgFixBroken) |
670a0494 JF |
3920 | if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) |
3921 | return; | |
1a83afc6 | 3922 | _end |
670a0494 JF |
3923 | |
3924 | if (cache_->BrokenCount() != 0) { | |
389133be | 3925 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("STILL_BROKEN_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3926 | return; |
3927 | } | |
3928 | ||
1a83afc6 | 3929 | _profile(pkgApplyStatus$pkgMinimizeUpgrade) |
670a0494 JF |
3930 | if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) |
3931 | return; | |
1a83afc6 | 3932 | _end |
9bedffaa JF |
3933 | } |
3934 | ||
0c28a403 JF |
3935 | for (Source *object in (id) sourceList_) { |
3936 | metaIndex *source([object metaIndex]); | |
3937 | std::vector<pkgIndexFile *> *indices = source->GetIndexFiles(); | |
68d927e2 JF |
3938 | for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index) |
3939 | // XXX: this could be more intelligent | |
3940 | if (dynamic_cast<debPackagesIndex *>(*index) != NULL) { | |
3941 | pkgCache::PkgFileIterator cached((*index)->FindInCache(cache_)); | |
34f70f5d JF |
3942 | if (!cached.end()) |
3943 | sourceMap_[cached->ID] = object; | |
68d927e2 | 3944 | } |
36bb2ca2 | 3945 | } |
68d927e2 | 3946 | |
677b8415 | 3947 | { |
9fcbca29 | 3948 | /*std::vector<Package *> packages; |
677b8415 | 3949 | packages.reserve(std::max(10000U, [packages_ count] + 1000)); |
9fcbca29 JF |
3950 | packages_ = nil;*/ |
3951 | ||
1a83afc6 | 3952 | _profile(reloadDataWithInvocation$packageWithIterator) |
677b8415 | 3953 | for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) |
64edd9df | 3954 | if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:&pool_ database:self]) |
9fcbca29 | 3955 | //packages.push_back(package); |
7b33d201 | 3956 | CFArrayAppendValue(packages_, CFRetain(package)); |
1a83afc6 | 3957 | _end |
677b8415 | 3958 | |
677b8415 | 3959 | |
9fcbca29 | 3960 | /*if (packages.empty()) |
677b8415 JF |
3961 | packages_ = [[NSArray alloc] init]; |
3962 | else | |
3963 | packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; | |
9fcbca29 JF |
3964 | _trace();*/ |
3965 | ||
1a83afc6 | 3966 | _profile(reloadDataWithInvocation$radix$8) |
9f0facbc | 3967 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(8)]; |
1a83afc6 JF |
3968 | _end |
3969 | ||
3970 | _profile(reloadDataWithInvocation$radix$4) | |
de42680b | 3971 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(4)]; |
1a83afc6 | 3972 | _end |
9fcbca29 | 3973 | |
1a83afc6 JF |
3974 | _profile(reloadDataWithInvocation$radix$0) |
3975 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(0)]; | |
3976 | _end | |
9fcbca29 | 3977 | |
1a83afc6 JF |
3978 | _profile(reloadDataWithInvocation$insertion) |
3979 | CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); | |
3980 | _end | |
9fcbca29 | 3981 | |
1a83afc6 JF |
3982 | /*_profile(reloadDataWithInvocation$CFQSortArray) |
3983 | CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare_), NULL); | |
3984 | _end*/ | |
9fcbca29 | 3985 | |
1a83afc6 JF |
3986 | /*_profile(reloadDataWithInvocation$stdsort) |
3987 | std::sort(packages.begin(), packages.end(), PackageNameOrdering()); | |
3988 | _end*/ | |
eef4ccaf | 3989 | |
1a83afc6 JF |
3990 | /*_profile(reloadDataWithInvocation$CFArraySortValues) |
3991 | CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); | |
3992 | _end*/ | |
9fcbca29 | 3993 | |
1a83afc6 JF |
3994 | /*_profile(reloadDataWithInvocation$sortUsingFunction) |
3995 | [packages_ sortUsingFunction:reinterpret_cast<NSComparisonResult (*)(id, id, void *)>(&PackageNameCompare) context:NULL]; | |
3996 | _end*/ | |
677b8415 | 3997 | |
cd95e390 JF |
3998 | |
3999 | size_t count(CFArrayGetCount(packages_)); | |
9f357d11 | 4000 | MetaFile_->active_ = count; |
cd95e390 JF |
4001 | for (size_t index(0); index != count; ++index) |
4002 | [(Package *) CFArrayGetValueAtIndex(packages_, index) setIndex:index]; | |
677b8415 | 4003 | } |
d13edf44 | 4004 | } } |
ec97ef06 | 4005 | |
c6ca67ba JF |
4006 | - (void) clear { |
4007 | @synchronized (self) { | |
4008 | delete resolver_; | |
4009 | resolver_ = new pkgProblemResolver(cache_); | |
4010 | ||
50c1653e JF |
4011 | for (pkgCache::PkgIterator iterator(cache_->PkgBegin()); !iterator.end(); ++iterator) |
4012 | if (!cache_[iterator].Keep()) | |
c6ca67ba | 4013 | cache_->MarkKeep(iterator, false); |
50c1653e | 4014 | else if ((cache_[iterator].iFlags & pkgDepCache::ReInstall) != 0) |
c6ca67ba | 4015 | cache_->SetReInstall(iterator, false); |
c6ca67ba JF |
4016 | } } |
4017 | ||
5a09ae08 | 4018 | - (void) configure { |
232b396b | 4019 | NSString *dpkg = [NSString stringWithFormat:@"/usr/libexec/cydo --configure -a --status-fd %u", statusfd_]; |
985d2dff | 4020 | _trace(); |
232b396b | 4021 | system([dpkg UTF8String]); |
985d2dff | 4022 | _trace(); |
5a09ae08 JF |
4023 | } |
4024 | ||
670a0494 | 4025 | - (bool) clean { |
0517651f | 4026 | @synchronized (self) { |
670a0494 | 4027 | // XXX: I don't remember this condition |
77fcccaf | 4028 | if (lock_ != NULL) |
670a0494 | 4029 | return false; |
77fcccaf JF |
4030 | |
4031 | FileFd Lock; | |
4032 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
4033 | |
4034 | NSString *title(UCLocalize("CLEAN_ARCHIVES")); | |
4035 | ||
4036 | if ([self popErrorWithTitle:title]) | |
4037 | return false; | |
77fcccaf JF |
4038 | |
4039 | pkgAcquire fetcher; | |
4040 | fetcher.Clean(_config->FindDir("Dir::Cache::Archives")); | |
4041 | ||
9f9ae81c | 4042 | CydiaLogCleaner cleaner; |
670a0494 JF |
4043 | if ([self popErrorWithTitle:title forOperation:cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)]) |
4044 | return false; | |
4045 | ||
4046 | return true; | |
0517651f | 4047 | } } |
77fcccaf | 4048 | |
670a0494 | 4049 | - (bool) prepare { |
744f398e JF |
4050 | fetcher_->Shutdown(); |
4051 | ||
36bb2ca2 JF |
4052 | pkgRecords records(cache_); |
4053 | ||
4054 | lock_ = new FileFd(); | |
4055 | lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
4056 | |
4057 | NSString *title(UCLocalize("PREPARE_ARCHIVES")); | |
4058 | ||
4059 | if ([self popErrorWithTitle:title]) | |
4060 | return false; | |
36bb2ca2 JF |
4061 | |
4062 | pkgSourceList list; | |
7bc0d825 | 4063 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4064 | return false; |
36bb2ca2 JF |
4065 | |
4066 | manager_ = (_system->CreatePM(cache_)); | |
670a0494 JF |
4067 | if ([self popErrorWithTitle:title forOperation:manager_->GetArchives(fetcher_, &list, &records)]) |
4068 | return false; | |
4069 | ||
4070 | return true; | |
ec97ef06 JF |
4071 | } |
4072 | ||
36bb2ca2 | 4073 | - (void) perform { |
be860cc8 JF |
4074 | bool substrate(RestartSubstrate_); |
4075 | RestartSubstrate_ = false; | |
4076 | ||
670a0494 JF |
4077 | NSString *title(UCLocalize("PERFORM_SELECTIONS")); |
4078 | ||
a72074b2 JF |
4079 | NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { |
4080 | pkgSourceList list; | |
7bc0d825 | 4081 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4082 | return; |
a72074b2 JF |
4083 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
4084 | [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
4085 | } | |
a0be02eb | 4086 | |
dcaecde2 | 4087 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
7ffd70fd | 4088 | |
eeb9b112 JF |
4089 | if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) { |
4090 | _trace(); | |
6e90508f | 4091 | [self popErrorWithTitle:title]; |
36bb2ca2 | 4092 | return; |
eeb9b112 JF |
4093 | } |
4094 | ||
4095 | bool failed = false; | |
4096 | for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) { | |
4097 | if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete) | |
4098 | continue; | |
6204f56a JF |
4099 | if ((*item)->Status == pkgAcquire::Item::StatIdle) |
4100 | continue; | |
eeb9b112 | 4101 | |
680a3c3c JF |
4102 | std::string uri = (*item)->DescURI(); |
4103 | std::string error = (*item)->ErrorText; | |
4104 | ||
4105 | lprintf("pAf:%s:%s\n", uri.c_str(), error.c_str()); | |
eeb9b112 | 4106 | failed = true; |
680a3c3c JF |
4107 | |
4108 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError]); | |
4109 | [delegate_ addProgressEventOnMainThread:event forTask:title]; | |
eeb9b112 JF |
4110 | } |
4111 | ||
dcaecde2 | 4112 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
54043703 | 4113 | |
eeb9b112 JF |
4114 | if (failed) { |
4115 | _trace(); | |
4116 | return; | |
4117 | } | |
36bb2ca2 | 4118 | |
be860cc8 JF |
4119 | if (substrate) |
4120 | RestartSubstrate_ = true; | |
4121 | ||
f26c90b1 JF |
4122 | if (![delock_ isEqual:GetStatusDate()]) { |
4123 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("DPKG_LOCKED") ofType:kCydiaProgressEventTypeError] forTask:title]; | |
4124 | return; | |
4125 | } | |
4126 | ||
4127 | delock_ = nil; | |
4128 | ||
d78cda50 JF |
4129 | pkgPackageManager::OrderResult result(manager_->DoInstall(statusfd_)); |
4130 | ||
49cc457a JF |
4131 | NSString *oextended(@"/var/lib/apt/extended_states"); |
4132 | NSString *nextended(Cache("extended_states")); | |
d78cda50 JF |
4133 | |
4134 | struct stat info; | |
ab1ec63e JF |
4135 | if (stat([nextended UTF8String], &info) != -1 && (info.st_mode & S_IFMT) == S_IFREG) |
4136 | system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/cp --remove-destination %@ %@", ShellEscape(nextended), ShellEscape(oextended)] UTF8String]); | |
d78cda50 | 4137 | |
49cc457a JF |
4138 | unlink([nextended UTF8String]); |
4139 | symlink([oextended UTF8String], [nextended UTF8String]); | |
4140 | ||
471683a3 | 4141 | if ([self popErrorWithTitle:title]) |
36bb2ca2 | 4142 | return; |
eeb9b112 JF |
4143 | |
4144 | if (result == pkgPackageManager::Failed) { | |
4145 | _trace(); | |
36bb2ca2 | 4146 | return; |
eeb9b112 JF |
4147 | } |
4148 | ||
4149 | if (result != pkgPackageManager::Completed) { | |
4150 | _trace(); | |
36bb2ca2 | 4151 | return; |
eeb9b112 | 4152 | } |
a0be02eb | 4153 | |
a72074b2 JF |
4154 | NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { |
4155 | pkgSourceList list; | |
7bc0d825 | 4156 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4157 | return; |
a72074b2 JF |
4158 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
4159 | [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
4160 | } | |
4161 | ||
4162 | if (![before isEqualToArray:after]) | |
4163 | [self update]; | |
ec97ef06 JF |
4164 | } |
4165 | ||
f26c90b1 JF |
4166 | - (bool) delocked { |
4167 | return ![delock_ isEqual:GetStatusDate()]; | |
4168 | } | |
4169 | ||
670a0494 JF |
4170 | - (bool) upgrade { |
4171 | NSString *title(UCLocalize("UPGRADE")); | |
4172 | if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) | |
4173 | return false; | |
4174 | return true; | |
c7c6384e JF |
4175 | } |
4176 | ||
36bb2ca2 JF |
4177 | - (void) update { |
4178 | [self updateWithStatus:status_]; | |
4179 | } | |
b4d89997 | 4180 | |
21ac0ce2 | 4181 | - (void) updateWithStatus:(CancelStatus &)status { |
670a0494 JF |
4182 | NSString *title(UCLocalize("REFRESHING_DATA")); |
4183 | ||
36bb2ca2 | 4184 | pkgSourceList list; |
7bc0d825 | 4185 | if ([self popErrorWithTitle:title forReadList:list]) |
53ca7fdd | 4186 | return; |
b4d89997 | 4187 | |
36bb2ca2 JF |
4188 | FileFd lock; |
4189 | lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
670a0494 JF |
4190 | if ([self popErrorWithTitle:title]) |
4191 | return; | |
18873623 | 4192 | |
aaae308d JF |
4193 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
4194 | ||
fc470c15 JF |
4195 | bool success(ListUpdate(status, list, PulseInterval_)); |
4196 | if (status.WasCancelled()) | |
4197 | _error->Discard(); | |
aa42c612 | 4198 | else { |
fc470c15 | 4199 | [self popErrorWithTitle:title forOperation:success]; |
6f87c61a JF |
4200 | |
4201 | [[NSDictionary dictionaryWithObjectsAndKeys: | |
4202 | [NSDate date], @"LastUpdate", | |
4203 | nil] writeToFile:@ CacheState_ atomically:YES]; | |
aa42c612 | 4204 | } |
36bb2ca2 | 4205 | |
aaae308d | 4206 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
b4d89997 JF |
4207 | } |
4208 | ||
6915b806 | 4209 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate { |
36bb2ca2 | 4210 | delegate_ = delegate; |
6915b806 JF |
4211 | } |
4212 | ||
4213 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate { | |
4214 | progress_ = delegate; | |
36bb2ca2 | 4215 | status_.setDelegate(delegate); |
36bb2ca2 | 4216 | } |
b4d89997 | 4217 | |
6915b806 JF |
4218 | - (NSObject<ProgressDelegate> *) progressDelegate { |
4219 | return progress_; | |
4220 | } | |
4221 | ||
7376b55c | 4222 | - (Source *) getSource:(pkgCache::PkgFileIterator)file { |
34f70f5d JF |
4223 | SourceMap::const_iterator i(sourceMap_.find(file->ID)); |
4224 | return i == sourceMap_.end() ? nil : i->second; | |
b19871dd JF |
4225 | } |
4226 | ||
21ac0ce2 JF |
4227 | - (void) setFetch:(bool)fetch forURI:(const char *)uri { |
4228 | for (Source *source in (id) sourceList_) | |
4229 | [source setFetch:fetch forURI:uri]; | |
4230 | } | |
4231 | ||
9ed626f1 JF |
4232 | - (void) resetFetch { |
4233 | for (Source *source in (id) sourceList_) | |
4234 | [source resetFetch]; | |
4235 | } | |
4236 | ||
fe33a23e | 4237 | - (NSString *) mappedSectionForPointer:(const char *)section { |
4905df00 | 4238 | _H<NSString> *mapped; |
fe33a23e | 4239 | |
4905df00 JF |
4240 | _profile(Database$mappedSectionForPointer$Cache) |
4241 | mapped = §ions_[section]; | |
4242 | _end | |
4243 | ||
4244 | if (*mapped == NULL) { | |
fe33a23e JF |
4245 | size_t length(strlen(section)); |
4246 | char spaced[length + 1]; | |
4247 | ||
4248 | _profile(Database$mappedSectionForPointer$Replace) | |
4249 | for (size_t index(0); index != length; ++index) | |
4250 | spaced[index] = section[index] == '_' ? ' ' : section[index]; | |
4251 | spaced[length] = '\0'; | |
4252 | _end | |
4253 | ||
4254 | NSString *string; | |
4255 | ||
4256 | _profile(Database$mappedSectionForPointer$stringWithUTF8String) | |
4257 | string = [NSString stringWithUTF8String:spaced]; | |
4258 | _end | |
4259 | ||
4260 | _profile(Database$mappedSectionForPointer$Map) | |
4905df00 | 4261 | string = [SectionMap_ objectForKey:string] ?: string; |
fe33a23e | 4262 | _end |
4905df00 JF |
4263 | |
4264 | *mapped = string; | |
4265 | } return *mapped; | |
fe33a23e JF |
4266 | } |
4267 | ||
36bb2ca2 JF |
4268 | @end |
4269 | /* }}} */ | |
b4d89997 | 4270 | |
7b33d201 | 4271 | static _H<NSMutableSet> Diversions_; |
7256476b | 4272 | |
775deead | 4273 | @interface Diversion : NSObject { |
b4fd1197 | 4274 | RegEx pattern_; |
775deead JF |
4275 | _H<NSString> key_; |
4276 | _H<NSString> format_; | |
4277 | } | |
4278 | ||
4279 | @end | |
4280 | ||
4281 | @implementation Diversion | |
4282 | ||
4283 | - (id) initWithFrom:(NSString *)from to:(NSString *)to { | |
4284 | if ((self = [super init]) != nil) { | |
4285 | pattern_ = [from UTF8String]; | |
4286 | key_ = from; | |
4287 | format_ = to; | |
4288 | } return self; | |
4289 | } | |
4290 | ||
4291 | - (NSString *) divert:(NSString *)url { | |
8ea598c0 | 4292 | return !pattern_(url) ? nil : pattern_->*format_; |
775deead JF |
4293 | } |
4294 | ||
7256476b JF |
4295 | + (NSURL *) divertURL:(NSURL *)url { |
4296 | divert: | |
4297 | NSString *href([url absoluteString]); | |
4298 | ||
7b33d201 | 4299 | for (Diversion *diversion in (id) Diversions_) |
7256476b | 4300 | if (NSString *diverted = [diversion divert:href]) { |
85d5d452 JF |
4301 | #if !ForRelease |
4302 | NSLog(@"div: %@", diverted); | |
4303 | #endif | |
7256476b JF |
4304 | url = [NSURL URLWithString:diverted]; |
4305 | goto divert; | |
4306 | } | |
4307 | ||
4308 | return url; | |
4309 | } | |
4310 | ||
775deead JF |
4311 | - (NSString *) key { |
4312 | return key_; | |
4313 | } | |
4314 | ||
4315 | - (NSUInteger) hash { | |
4316 | return [key_ hash]; | |
4317 | } | |
4318 | ||
4319 | - (BOOL) isEqual:(Diversion *)object { | |
4320 | return self == object || [self class] == [object class] && [key_ isEqual:[object key]]; | |
4321 | } | |
4322 | ||
4323 | @end | |
4324 | ||
43f3d7f6 | 4325 | @interface CydiaObject : NSObject { |
4cc9e99a | 4326 | _H<CyteWebViewController> indirect_; |
3931b718 | 4327 | _transient id delegate_; |
43f3d7f6 | 4328 | } |
c390d3ab | 4329 | |
43f3d7f6 | 4330 | - (id) initWithDelegate:(IndirectDelegate *)indirect; |
6840bff3 | 4331 | |
c390d3ab JF |
4332 | @end |
4333 | ||
9e130bc2 JF |
4334 | @class CydiaObject; |
4335 | ||
09e89a8a | 4336 | @interface CydiaWebViewController : CyteWebViewController { |
7b33d201 | 4337 | _H<CydiaObject> cydia_; |
775deead JF |
4338 | } |
4339 | ||
4340 | + (void) addDiversion:(Diversion *)diversion; | |
85ae5f42 | 4341 | + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request; |
9e130bc2 JF |
4342 | + (void) didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame withCydia:(CydiaObject *)cydia; |
4343 | - (void) setDelegate:(id)delegate; | |
775deead JF |
4344 | |
4345 | @end | |
4346 | ||
775deead | 4347 | /* Web Scripting {{{ */ |
43f3d7f6 | 4348 | @implementation CydiaObject |
c390d3ab | 4349 | |
43f3d7f6 JF |
4350 | - (id) initWithDelegate:(IndirectDelegate *)indirect { |
4351 | if ((self = [super init]) != nil) { | |
4cc9e99a | 4352 | indirect_ = (CyteWebViewController *) indirect; |
43f3d7f6 JF |
4353 | } return self; |
4354 | } | |
4355 | ||
77801ff1 JF |
4356 | - (void) setDelegate:(id)delegate { |
4357 | delegate_ = delegate; | |
4358 | } | |
4359 | ||
43f3d7f6 | 4360 | + (NSArray *) _attributeKeys { |
e58ff941 | 4361 | return [NSArray arrayWithObjects: |
6ffdaae3 | 4362 | @"bbsnum", |
e967efd5 JF |
4363 | @"build", |
4364 | @"coreFoundationVersionNumber", | |
e58ff941 | 4365 | @"device", |
56296da0 | 4366 | @"ecid", |
93d6d318 JF |
4367 | @"firmware", |
4368 | @"hostname", | |
4369 | @"idiom", | |
f87cac81 JF |
4370 | @"mcc", |
4371 | @"mnc", | |
56296da0 | 4372 | @"model", |
2656c992 | 4373 | @"operator", |
56296da0 | 4374 | @"role", |
e58ff941 | 4375 | @"serial", |
bf1d5e69 | 4376 | @"version", |
e58ff941 | 4377 | nil]; |
43f3d7f6 JF |
4378 | } |
4379 | ||
4380 | - (NSArray *) attributeKeys { | |
4381 | return [[self class] _attributeKeys]; | |
c390d3ab JF |
4382 | } |
4383 | ||
43f3d7f6 JF |
4384 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
4385 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
c390d3ab | 4386 | } |
43f3d7f6 | 4387 | |
bf1d5e69 | 4388 | - (NSString *) version { |
3d45bad1 | 4389 | return Cydia_; |
bf1d5e69 JF |
4390 | } |
4391 | ||
e967efd5 JF |
4392 | - (NSString *) build { |
4393 | return System_; | |
4394 | } | |
4395 | ||
4396 | - (NSString *) coreFoundationVersionNumber { | |
4397 | return [NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber]; | |
4398 | } | |
4399 | ||
43f3d7f6 | 4400 | - (NSString *) device { |
7c80833f | 4401 | return UniqueIdentifier(); |
c390d3ab JF |
4402 | } |
4403 | ||
93d6d318 JF |
4404 | - (NSString *) firmware { |
4405 | return [[UIDevice currentDevice] systemVersion]; | |
4406 | } | |
4407 | ||
4408 | - (NSString *) hostname { | |
4409 | return [[UIDevice currentDevice] name]; | |
4410 | } | |
4411 | ||
4412 | - (NSString *) idiom { | |
c138614d | 4413 | return (id) Idiom_ ?: [NSNull null]; |
93d6d318 JF |
4414 | } |
4415 | ||
f87cac81 JF |
4416 | - (NSString *) mcc { |
4417 | if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode"))) | |
4418 | return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault) autorelease]; | |
4419 | return nil; | |
4420 | } | |
4421 | ||
4422 | - (NSString *) mnc { | |
4423 | if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberNetworkCode"))) | |
4424 | return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(kCFAllocatorDefault) autorelease]; | |
4425 | return nil; | |
56296da0 JF |
4426 | } |
4427 | ||
2656c992 JF |
4428 | - (NSString *) operator { |
4429 | if (CFStringRef (*$CTRegistrationCopyOperatorName)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTRegistrationCopyOperatorName"))) | |
4430 | return [(NSString *) (*$CTRegistrationCopyOperatorName)(kCFAllocatorDefault) autorelease]; | |
4431 | return nil; | |
4432 | } | |
4433 | ||
6ffdaae3 JF |
4434 | - (NSString *) bbsnum { |
4435 | return (id) BBSNum_ ?: [NSNull null]; | |
4436 | } | |
4437 | ||
56296da0 | 4438 | - (NSString *) ecid { |
849cd6bf | 4439 | return (id) ChipID_ ?: [NSNull null]; |
affeffc7 JF |
4440 | } |
4441 | ||
43f3d7f6 | 4442 | - (NSString *) serial { |
56296da0 | 4443 | return SerialNumber_; |
43f3d7f6 | 4444 | } |
86316a91 | 4445 | |
56296da0 | 4446 | - (NSString *) role { |
4121c5e0 | 4447 | return (id) [NSNull null]; |
affeffc7 JF |
4448 | } |
4449 | ||
56296da0 JF |
4450 | - (NSString *) model { |
4451 | return [NSString stringWithUTF8String:Machine_]; | |
43f3d7f6 | 4452 | } |
43f3d7f6 JF |
4453 | |
4454 | + (NSString *) webScriptNameForSelector:(SEL)selector { | |
e58ff941 | 4455 | if (false); |
cfc7b442 JF |
4456 | else if (selector == @selector(addBridgedHost:)) |
4457 | return @"addBridgedHost"; | |
2e1652a9 JF |
4458 | else if (selector == @selector(addInsecureHost:)) |
4459 | return @"addInsecureHost"; | |
3f428e4e JF |
4460 | else if (selector == @selector(addInternalRedirect::)) |
4461 | return @"addInternalRedirect"; | |
e4b48f2f | 4462 | else if (selector == @selector(addPipelinedHost:scheme:)) |
834c18a4 | 4463 | return @"addPipelinedHost"; |
33e30380 JF |
4464 | else if (selector == @selector(addSource:::)) |
4465 | return @"addSource"; | |
b088c0cd JF |
4466 | else if (selector == @selector(addTrivialSource:)) |
4467 | return @"addTrivialSource"; | |
e58ff941 | 4468 | else if (selector == @selector(close)) |
43f3d7f6 | 4469 | return @"close"; |
e58ff941 JF |
4470 | else if (selector == @selector(du:)) |
4471 | return @"du"; | |
4472 | else if (selector == @selector(stringWithFormat:arguments:)) | |
4473 | return @"format"; | |
5c32f89e | 4474 | else if (selector == @selector(getAllSources)) |
caf0475e | 4475 | return @"getAllSources"; |
8c5b623f JF |
4476 | else if (selector == @selector(getApplicationInfo:value:)) |
4477 | return @"getApplicationInfoValue"; | |
375a4481 JF |
4478 | else if (selector == @selector(getDisplayIdentifiers)) |
4479 | return @"getDisplayIdentifiers"; | |
4480 | else if (selector == @selector(getLocalizedNameForDisplayIdentifier:)) | |
4481 | return @"getLocalizedNameForDisplayIdentifier"; | |
5bc1277a JF |
4482 | else if (selector == @selector(getKernelNumber:)) |
4483 | return @"getKernelNumber"; | |
4484 | else if (selector == @selector(getKernelString:)) | |
4485 | return @"getKernelString"; | |
8cc8eb1c JF |
4486 | else if (selector == @selector(getInstalledPackages)) |
4487 | return @"getInstalledPackages"; | |
c31d7cdc JF |
4488 | else if (selector == @selector(getIORegistryEntry::)) |
4489 | return @"getIORegistryEntry"; | |
948db680 JF |
4490 | else if (selector == @selector(getLocaleIdentifier)) |
4491 | return @"getLocaleIdentifier"; | |
4492 | else if (selector == @selector(getPreferredLanguages)) | |
4493 | return @"getPreferredLanguages"; | |
43f3d7f6 JF |
4494 | else if (selector == @selector(getPackageById:)) |
4495 | return @"getPackageById"; | |
37fa9338 JF |
4496 | else if (selector == @selector(getMetadataKeys)) |
4497 | return @"getMetadataKeys"; | |
b3c8e69c JF |
4498 | else if (selector == @selector(getMetadataValue:)) |
4499 | return @"getMetadataValue"; | |
ef974f52 JF |
4500 | else if (selector == @selector(getSessionValue:)) |
4501 | return @"getSessionValue"; | |
77801ff1 JF |
4502 | else if (selector == @selector(installPackages:)) |
4503 | return @"installPackages"; | |
518a552a JF |
4504 | else if (selector == @selector(isReachable:)) |
4505 | return @"isReachable"; | |
e58ff941 JF |
4506 | else if (selector == @selector(localizedStringForKey:value:table:)) |
4507 | return @"localize"; | |
8d497e2a JF |
4508 | else if (selector == @selector(popViewController:)) |
4509 | return @"popViewController"; | |
b088c0cd JF |
4510 | else if (selector == @selector(refreshSources)) |
4511 | return @"refreshSources"; | |
aa1e1906 JF |
4512 | else if (selector == @selector(registerFrame:)) |
4513 | return @"registerFrame"; | |
ed5566c7 JF |
4514 | else if (selector == @selector(removeButton)) |
4515 | return @"removeButton"; | |
33e30380 JF |
4516 | else if (selector == @selector(saveConfig)) |
4517 | return @"saveConfig"; | |
b3c8e69c JF |
4518 | else if (selector == @selector(setMetadataValue::)) |
4519 | return @"setMetadataValue"; | |
ef974f52 JF |
4520 | else if (selector == @selector(setSessionValue::)) |
4521 | return @"setSessionValue"; | |
8a126074 JF |
4522 | else if (selector == @selector(substitutePackageNames:)) |
4523 | return @"substitutePackageNames"; | |
8e3b68d4 JF |
4524 | else if (selector == @selector(scrollToBottom:)) |
4525 | return @"scrollToBottom"; | |
8366df5e JF |
4526 | else if (selector == @selector(setAllowsNavigationAction:)) |
4527 | return @"setAllowsNavigationAction"; | |
c31c825d JF |
4528 | else if (selector == @selector(setBadgeValue:)) |
4529 | return @"setBadgeValue"; | |
43f3d7f6 JF |
4530 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) |
4531 | return @"setButtonImage"; | |
4532 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
4533 | return @"setButtonTitle"; | |
b8a5d89d JF |
4534 | else if (selector == @selector(setHidesBackButton:)) |
4535 | return @"setHidesBackButton"; | |
5cdfcd6f JF |
4536 | else if (selector == @selector(setHidesNavigationBar:)) |
4537 | return @"setHidesNavigationBar"; | |
82406217 JF |
4538 | else if (selector == @selector(setNavigationBarStyle:)) |
4539 | return @"setNavigationBarStyle"; | |
00984204 JF |
4540 | else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:)) |
4541 | return @"setNavigationBarTintColor"; | |
36a20e14 JF |
4542 | else if (selector == @selector(setPasteboardString:)) |
4543 | return @"setPasteboardString"; | |
4544 | else if (selector == @selector(setPasteboardURL:)) | |
4545 | return @"setPasteboardURL"; | |
db698f42 JF |
4546 | else if (selector == @selector(setScrollAlwaysBounceVertical:)) |
4547 | return @"setScrollAlwaysBounceVertical"; | |
4886cc81 JF |
4548 | else if (selector == @selector(setScrollIndicatorStyle:)) |
4549 | return @"setScrollIndicatorStyle"; | |
ef055c6c JF |
4550 | else if (selector == @selector(setToken:)) |
4551 | return @"setToken"; | |
43f3d7f6 JF |
4552 | else if (selector == @selector(setViewportWidth:)) |
4553 | return @"setViewportWidth"; | |
43f3d7f6 JF |
4554 | else if (selector == @selector(statfs:)) |
4555 | return @"statfs"; | |
e58ff941 JF |
4556 | else if (selector == @selector(supports:)) |
4557 | return @"supports"; | |
7c218781 JF |
4558 | else if (selector == @selector(unload)) |
4559 | return @"unload"; | |
c390d3ab | 4560 | else |
43f3d7f6 JF |
4561 | return nil; |
4562 | } | |
4563 | ||
4564 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
4565 | return [self webScriptNameForSelector:selector] == nil; | |
c390d3ab JF |
4566 | } |
4567 | ||
43f3d7f6 JF |
4568 | - (BOOL) supports:(NSString *)feature { |
4569 | return [feature isEqualToString:@"window.open"]; | |
4570 | } | |
c390d3ab | 4571 | |
7c218781 JF |
4572 | - (void) unload { |
4573 | [delegate_ performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO]; | |
4574 | } | |
4575 | ||
db698f42 JF |
4576 | - (void) setScrollAlwaysBounceVertical:(NSNumber *)value { |
4577 | [indirect_ performSelectorOnMainThread:@selector(setScrollAlwaysBounceVerticalNumber:) withObject:value waitUntilDone:NO]; | |
4578 | } | |
4579 | ||
4886cc81 JF |
4580 | - (void) setScrollIndicatorStyle:(NSString *)style { |
4581 | [indirect_ performSelectorOnMainThread:@selector(setScrollIndicatorStyleWithName:) withObject:style waitUntilDone:NO]; | |
4582 | } | |
4583 | ||
3f428e4e | 4584 | - (void) addInternalRedirect:(NSString *)from :(NSString *)to { |
a576488f | 4585 | [CydiaWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO]; |
775deead JF |
4586 | } |
4587 | ||
8c5b623f JF |
4588 | - (NSDictionary *) getApplicationInfo:(NSString *)display value:(NSString *)key { |
4589 | char path[1024]; | |
4590 | if (SBBundlePathForDisplayIdentifier(SBSSpringBoardServerPort(), [display UTF8String], path) != 0) | |
4591 | return (id) [NSNull null]; | |
4592 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:[[NSString stringWithUTF8String:path] stringByAppendingString:@"/Info.plist"]]); | |
4593 | if (info == nil) | |
4594 | return (id) [NSNull null]; | |
4595 | return [info objectForKey:key]; | |
4596 | } | |
4597 | ||
375a4481 | 4598 | - (NSArray *) getDisplayIdentifiers { |
23d491b4 | 4599 | NSSet *set([SBSCopyApplicationDisplayIdentifiers() autorelease]); |
375a4481 JF |
4600 | if (set == nil || ![set isKindOfClass:[NSSet class]]) |
4601 | return [NSArray array]; | |
4602 | return [set allObjects]; | |
4603 | } | |
4604 | ||
4605 | - (NSString *) getLocalizedNameForDisplayIdentifier:(NSString *)identifier { | |
4606 | return [SBSCopyLocalizedApplicationNameForDisplayIdentifier(identifier) autorelease] ?: (id) [NSNull null]; | |
4607 | } | |
4608 | ||
5bc1277a JF |
4609 | - (NSNumber *) getKernelNumber:(NSString *)name { |
4610 | const char *string([name UTF8String]); | |
4611 | ||
4612 | size_t size; | |
4613 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
4614 | return (id) [NSNull null]; | |
4615 | ||
4616 | if (size != sizeof(int)) | |
4617 | return (id) [NSNull null]; | |
4618 | ||
4619 | int value; | |
4620 | if (sysctlbyname(string, &value, &size, NULL, 0) == -1) | |
4621 | return (id) [NSNull null]; | |
4622 | ||
4623 | return [NSNumber numberWithInt:value]; | |
4624 | } | |
4625 | ||
4626 | - (NSString *) getKernelString:(NSString *)name { | |
4627 | const char *string([name UTF8String]); | |
4628 | ||
4629 | size_t size; | |
4630 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
4631 | return (id) [NSNull null]; | |
4632 | ||
4633 | char value[size + 1]; | |
4634 | if (sysctlbyname(string, value, &size, NULL, 0) == -1) | |
4635 | return (id) [NSNull null]; | |
4636 | ||
4637 | // XXX: just in case you request something ludicrous | |
4638 | value[size] = '\0'; | |
4639 | ||
4640 | return [NSString stringWithCString:value]; | |
4641 | } | |
4642 | ||
c31d7cdc JF |
4643 | - (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry { |
4644 | NSObject *value(CYIOGetValue([path UTF8String], entry)); | |
4645 | ||
4646 | if (value != nil) | |
4647 | if ([value isKindOfClass:[NSData class]]) | |
4648 | value = CYHex((NSData *) value); | |
4649 | ||
4650 | return value; | |
4651 | } | |
4652 | ||
37fa9338 JF |
4653 | - (NSArray *) getMetadataKeys { |
4654 | @synchronized (Values_) { | |
4655 | return [Values_ allKeys]; | |
4656 | } } | |
4657 | ||
aa1e1906 JF |
4658 | - (void) registerFrame:(DOMHTMLIFrameElement *)iframe { |
4659 | WebFrame *frame([iframe contentFrame]); | |
4660 | [indirect_ registerFrame:frame]; | |
4661 | } | |
4662 | ||
b3c8e69c JF |
4663 | - (id) getMetadataValue:(NSString *)key { |
4664 | @synchronized (Values_) { | |
4665 | return [Values_ objectForKey:key]; | |
4666 | } } | |
4667 | ||
4668 | - (void) setMetadataValue:(NSString *)key :(NSString *)value { | |
4669 | @synchronized (Values_) { | |
33e30380 | 4670 | if (value == nil || value == (id) [WebUndefined undefined] || value == (id) [NSNull null]) |
b3c8e69c JF |
4671 | [Values_ removeObjectForKey:key]; |
4672 | else | |
4673 | [Values_ setObject:value forKey:key]; | |
b3c8e69c JF |
4674 | } } |
4675 | ||
ef974f52 JF |
4676 | - (id) getSessionValue:(NSString *)key { |
4677 | @synchronized (SessionData_) { | |
4678 | return [SessionData_ objectForKey:key]; | |
4679 | } } | |
4680 | ||
4681 | - (void) setSessionValue:(NSString *)key :(NSString *)value { | |
4682 | @synchronized (SessionData_) { | |
4683 | if (value == (id) [WebUndefined undefined]) | |
4684 | [SessionData_ removeObjectForKey:key]; | |
4685 | else | |
4686 | [SessionData_ setObject:value forKey:key]; | |
4687 | } } | |
4688 | ||
cfc7b442 | 4689 | - (void) addBridgedHost:(NSString *)host { |
48f1762f JF |
4690 | @synchronized (HostConfig_) { |
4691 | [BridgedHosts_ addObject:host]; | |
4692 | } } | |
5df7ecfb | 4693 | |
2e1652a9 JF |
4694 | - (void) addInsecureHost:(NSString *)host { |
4695 | @synchronized (HostConfig_) { | |
4696 | [InsecureHosts_ addObject:host]; | |
4697 | } } | |
4698 | ||
e4b48f2f | 4699 | - (void) addPipelinedHost:(NSString *)host scheme:(NSString *)scheme { |
48f1762f | 4700 | @synchronized (HostConfig_) { |
e4b48f2f JF |
4701 | if (scheme != (id) [WebUndefined undefined]) |
4702 | host = [NSString stringWithFormat:@"%@:%@", [scheme lowercaseString], host]; | |
4703 | ||
48f1762f JF |
4704 | [PipelinedHosts_ addObject:host]; |
4705 | } } | |
834c18a4 | 4706 | |
8d497e2a JF |
4707 | - (void) popViewController:(NSNumber *)value { |
4708 | if (value == (id) [WebUndefined undefined]) | |
4709 | value = [NSNumber numberWithBool:YES]; | |
4710 | [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO]; | |
4711 | } | |
4712 | ||
33e30380 JF |
4713 | - (void) addSource:(NSString *)href :(NSString *)distribution :(WebScriptObject *)sections { |
4714 | NSMutableArray *array([NSMutableArray arrayWithCapacity:[sections count]]); | |
4715 | ||
4716 | for (NSString *section in sections) | |
4717 | [array addObject:section]; | |
4718 | ||
4719 | [delegate_ performSelectorOnMainThread:@selector(addSource:) withObject:[NSMutableDictionary dictionaryWithObjectsAndKeys: | |
4720 | @"deb", @"Type", | |
4721 | href, @"URI", | |
4722 | distribution, @"Distribution", | |
4723 | array, @"Sections", | |
4724 | nil] waitUntilDone:NO]; | |
4725 | } | |
4726 | ||
8a2d167a JF |
4727 | - (BOOL) addTrivialSource:(NSString *)href { |
4728 | href = VerifySource(href); | |
4729 | if (href == nil) | |
4730 | return NO; | |
a1d85d42 | 4731 | [delegate_ performSelectorOnMainThread:@selector(addTrivialSource:) withObject:href waitUntilDone:NO]; |
8a2d167a | 4732 | return YES; |
b088c0cd JF |
4733 | } |
4734 | ||
4735 | - (void) refreshSources { | |
4736 | [delegate_ performSelectorOnMainThread:@selector(syncData) withObject:nil waitUntilDone:NO]; | |
4737 | } | |
4738 | ||
33e30380 JF |
4739 | - (void) saveConfig { |
4740 | [delegate_ performSelectorOnMainThread:@selector(_saveConfig) withObject:nil waitUntilDone:NO]; | |
4741 | } | |
4742 | ||
5c32f89e JF |
4743 | - (NSArray *) getAllSources { |
4744 | return [[Database sharedInstance] sources]; | |
4745 | } | |
4746 | ||
8cc8eb1c | 4747 | - (NSArray *) getInstalledPackages { |
26e6829b JF |
4748 | Database *database([Database sharedInstance]); |
4749 | @synchronized (database) { | |
4750 | NSArray *packages([database packages]); | |
f4db946e | 4751 | NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]); |
77801ff1 | 4752 | for (Package *package in packages) |
26e6829b | 4753 | if (![package uninstalled]) |
8cc8eb1c JF |
4754 | [installed addObject:package]; |
4755 | return installed; | |
26e6829b | 4756 | } } |
8cc8eb1c | 4757 | |
43f3d7f6 | 4758 | - (Package *) getPackageById:(NSString *)id { |
62cab237 JF |
4759 | if (Package *package = [[Database sharedInstance] packageWithName:id]) { |
4760 | [package parse]; | |
4761 | return package; | |
4762 | } else | |
4763 | return (Package *) [NSNull null]; | |
43f3d7f6 JF |
4764 | } |
4765 | ||
948db680 JF |
4766 | - (NSString *) getLocaleIdentifier { |
4767 | return Locale_ == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(Locale_); | |
4768 | } | |
4769 | ||
4770 | - (NSArray *) getPreferredLanguages { | |
4771 | return Languages_; | |
4772 | } | |
4773 | ||
43f3d7f6 JF |
4774 | - (NSArray *) statfs:(NSString *)path { |
4775 | struct statfs stat; | |
4776 | ||
4777 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
4778 | return nil; | |
4779 | ||
4780 | return [NSArray arrayWithObjects: | |
4781 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
4782 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
4783 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
4784 | nil]; | |
4785 | } | |
4786 | ||
e40620ab | 4787 | - (NSNumber *) du:(NSString *)path { |
d59938e3 JF |
4788 | NSNumber *value(nil); |
4789 | ||
d8b0f6e3 | 4790 | FILE *du(popen([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/du -ks %@", ShellEscape(path)] UTF8String], "r")); |
d59938e3 JF |
4791 | if (du != NULL) { |
4792 | char line[1024]; | |
4793 | while (fgets(line, sizeof(line), du) != NULL) { | |
4794 | size_t length(strlen(line)); | |
4795 | while (length != 0 && line[length - 1] == '\n') | |
4796 | line[--length] = '\0'; | |
4797 | if (char *tab = strchr(line, '\t')) { | |
4798 | *tab = '\0'; | |
4799 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
4800 | } | |
4801 | } | |
4802 | pclose(du); | |
4803 | } | |
4804 | ||
4805 | return value; | |
43f3d7f6 JF |
4806 | } |
4807 | ||
4808 | - (void) close { | |
e6417cea | 4809 | [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO]; |
43f3d7f6 JF |
4810 | } |
4811 | ||
518a552a JF |
4812 | - (NSNumber *) isReachable:(NSString *)name { |
4813 | return [NSNumber numberWithBool:IsReachable([name UTF8String])]; | |
4814 | } | |
4815 | ||
77801ff1 JF |
4816 | - (void) installPackages:(NSArray *)packages { |
4817 | [delegate_ performSelectorOnMainThread:@selector(installPackages:) withObject:packages waitUntilDone:NO]; | |
4818 | } | |
4819 | ||
8a126074 | 4820 | - (NSString *) substitutePackageNames:(NSString *)message { |
0c4fe0f4 | 4821 | NSMutableArray *words([[[message componentsSeparatedByString:@" "] mutableCopy] autorelease]); |
8a126074 JF |
4822 | for (size_t i(0), e([words count]); i != e; ++i) { |
4823 | NSString *word([words objectAtIndex:i]); | |
4824 | if (Package *package = [[Database sharedInstance] packageWithName:word]) | |
4825 | [words replaceObjectAtIndex:i withObject:[package name]]; | |
4826 | } | |
4827 | ||
4828 | return [words componentsJoinedByString:@" "]; | |
4829 | } | |
4830 | ||
ed5566c7 JF |
4831 | - (void) removeButton { |
4832 | [indirect_ removeButton]; | |
4833 | } | |
4834 | ||
43f3d7f6 JF |
4835 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
4836 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
4837 | } | |
4838 | ||
4839 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
4840 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
4841 | } | |
4842 | ||
c31c825d JF |
4843 | - (void) setBadgeValue:(id)value { |
4844 | [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO]; | |
4845 | } | |
4846 | ||
8366df5e JF |
4847 | - (void) setAllowsNavigationAction:(NSString *)value { |
4848 | [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO]; | |
4849 | } | |
4850 | ||
b8a5d89d JF |
4851 | - (void) setHidesBackButton:(NSString *)value { |
4852 | [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO]; | |
4853 | } | |
4854 | ||
5cdfcd6f JF |
4855 | - (void) setHidesNavigationBar:(NSString *)value { |
4856 | [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO]; | |
4857 | } | |
4858 | ||
82406217 JF |
4859 | - (void) setNavigationBarStyle:(NSString *)value { |
4860 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO]; | |
4861 | } | |
4862 | ||
00984204 JF |
4863 | - (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha { |
4864 | float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]); | |
4865 | UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]); | |
4866 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO]; | |
4867 | } | |
4868 | ||
36a20e14 JF |
4869 | - (void) setPasteboardString:(NSString *)value { |
4870 | [[objc_getClass("UIPasteboard") generalPasteboard] setString:value]; | |
4871 | } | |
4872 | ||
4873 | - (void) setPasteboardURL:(NSString *)value { | |
4874 | [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]]; | |
4875 | } | |
4876 | ||
673a6e1a | 4877 | - (void) setToken:(NSString *)token { |
7826a48c | 4878 | // XXX: the website expects this :/ |
673a6e1a JF |
4879 | } |
4880 | ||
8e3b68d4 JF |
4881 | - (void) scrollToBottom:(NSNumber *)animated { |
4882 | [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO]; | |
4883 | } | |
4884 | ||
43f3d7f6 | 4885 | - (void) setViewportWidth:(float)width { |
8dbdaafa | 4886 | [indirect_ setViewportWidthOnMainThread:width]; |
43f3d7f6 JF |
4887 | } |
4888 | ||
4889 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
4890 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
4891 | unsigned count([arguments count]); | |
4892 | id values[count]; | |
4893 | for (unsigned i(0); i != count; ++i) | |
4894 | values[i] = [arguments objectAtIndex:i]; | |
673e8fa3 | 4895 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; |
43f3d7f6 JF |
4896 | } |
4897 | ||
4898 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
a95e0405 JF |
4899 | if (reinterpret_cast<id>(value) == [WebUndefined undefined]) |
4900 | value = nil; | |
43f3d7f6 JF |
4901 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) |
4902 | table = nil; | |
4903 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
c390d3ab JF |
4904 | } |
4905 | ||
4906 | @end | |
4907 | /* }}} */ | |
f79a4512 | 4908 | |
2e1652a9 JF |
4909 | @interface NSURL (CydiaSecure) |
4910 | @end | |
4911 | ||
4912 | @implementation NSURL (CydiaSecure) | |
4913 | ||
4914 | - (bool) isCydiaSecure { | |
4915 | if ([[[self scheme] lowercaseString] isEqualToString:@"https"]) | |
4916 | return true; | |
4917 | ||
4918 | @synchronized (HostConfig_) { | |
4919 | if ([InsecureHosts_ containsObject:[self host]]) | |
4920 | return true; | |
4921 | } | |
4922 | ||
4923 | return false; | |
4924 | } | |
4925 | ||
4926 | @end | |
dd5f8161 | 4927 | |
80319240 | 4928 | /* Cydia Browser Controller {{{ */ |
a576488f | 4929 | @implementation CydiaWebViewController |
43f3d7f6 | 4930 | |
fe8e721f | 4931 | - (NSURL *) navigationURL { |
d323285e | 4932 | return request_ == nil ? nil : [NSURL URLWithString:[NSString stringWithFormat:@"cydia://url/%@", [[request_ URL] absoluteString]]]; |
fe8e721f GP |
4933 | } |
4934 | ||
3f9ab807 JF |
4935 | + (void) _initialize { |
4936 | [super _initialize]; | |
4937 | ||
7b33d201 | 4938 | Diversions_ = [NSMutableSet setWithCapacity:0]; |
775deead JF |
4939 | } |
4940 | ||
4941 | + (void) addDiversion:(Diversion *)diversion { | |
4942 | [Diversions_ addObject:diversion]; | |
4943 | } | |
4944 | ||
2634b249 JF |
4945 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4946 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
9e130bc2 JF |
4947 | [CydiaWebViewController didClearWindowObject:window forFrame:frame withCydia:cydia_]; |
4948 | } | |
01d93940 | 4949 | |
9e130bc2 | 4950 | + (void) didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame withCydia:(CydiaObject *)cydia { |
01d93940 JF |
4951 | WebDataSource *source([frame dataSource]); |
4952 | NSURLResponse *response([source response]); | |
4953 | NSURL *url([response URL]); | |
b8f1a18a JF |
4954 | NSString *scheme([[url scheme] lowercaseString]); |
4955 | ||
4956 | bool bridged(false); | |
8804004f | 4957 | |
48f1762f | 4958 | @synchronized (HostConfig_) { |
b8f1a18a JF |
4959 | if ([scheme isEqualToString:@"file"]) |
4960 | bridged = true; | |
4961 | else if ([scheme isEqualToString:@"https"]) | |
48f1762f | 4962 | if ([BridgedHosts_ containsObject:[url host]]) |
b8f1a18a | 4963 | bridged = true; |
48f1762f | 4964 | } |
b8f1a18a JF |
4965 | |
4966 | if (bridged) | |
9e130bc2 | 4967 | [window setValue:cydia forKey:@"cydia"]; |
43f3d7f6 JF |
4968 | } |
4969 | ||
22485d93 JF |
4970 | - (void) _setupMail:(MFMailComposeViewController *)controller { |
4971 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/cydia.log"] mimeType:@"text/plain" fileName:@"cydia.log"]; | |
4972 | ||
4973 | system("/usr/bin/dpkg -l >/tmp/dpkgl.log"); | |
4974 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/dpkgl.log"] mimeType:@"text/plain" fileName:@"dpkgl.log"]; | |
4975 | } | |
4976 | ||
f9b36dae JF |
4977 | - (NSURL *) URLWithURL:(NSURL *)url { |
4978 | return [Diversion divertURL:url]; | |
4979 | } | |
4980 | ||
9d1bf666 | 4981 | - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
85ae5f42 JF |
4982 | return [CydiaWebViewController requestWithHeaders:[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; |
4983 | } | |
4984 | ||
92de61aa JF |
4985 | - (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
4986 | return [CydiaWebViewController requestWithHeaders:[super webThreadWebView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; | |
4987 | } | |
4988 | ||
85ae5f42 | 4989 | + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request { |
0c4fe0f4 | 4990 | NSMutableURLRequest *copy([[request mutableCopy] autorelease]); |
9d1bf666 | 4991 | |
bc1cffbe | 4992 | NSURL *url([copy URL]); |
daa21f8e | 4993 | NSString *href([url absoluteString]); |
bc1cffbe JF |
4994 | NSString *host([url host]); |
4995 | ||
daa21f8e JF |
4996 | if ([href hasPrefix:@"https://cydia.saurik.com/TSS/"]) { |
4997 | if (NSString *agent = [copy valueForHTTPHeaderField:@"X-User-Agent"]) { | |
4998 | [copy setValue:agent forHTTPHeaderField:@"User-Agent"]; | |
4999 | [copy setValue:nil forHTTPHeaderField:@"X-User-Agent"]; | |
5000 | } | |
5001 | ||
5002 | [copy setValue:nil forHTTPHeaderField:@"Referer"]; | |
5003 | [copy setValue:nil forHTTPHeaderField:@"Origin"]; | |
5004 | ||
5005 | [copy setURL:[NSURL URLWithString:[@"http://gs.apple.com/TSS/" stringByAppendingString:[href substringFromIndex:29]]]]; | |
5006 | return copy; | |
5007 | } | |
5008 | ||
6f44d712 JF |
5009 | if ([copy valueForHTTPHeaderField:@"X-Cydia-Cf"] == nil) |
5010 | [copy setValue:[NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber] forHTTPHeaderField:@"X-Cydia-Cf"]; | |
1baae086 | 5011 | if (Machine_ != NULL && [copy valueForHTTPHeaderField:@"X-Machine"] == nil) |
9d1bf666 | 5012 | [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; |
247bedb6 | 5013 | |
7826a48c | 5014 | bool bridged; @synchronized (HostConfig_) { |
e7817a6b | 5015 | bridged = [BridgedHosts_ containsObject:host]; |
247bedb6 JF |
5016 | } |
5017 | ||
7826a48c JF |
5018 | if ([url isCydiaSecure] && bridged && UniqueID_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Id"] == nil) |
5019 | [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; | |
43f3d7f6 JF |
5020 | |
5021 | return copy; | |
a9a0661e JF |
5022 | } |
5023 | ||
77801ff1 JF |
5024 | - (void) setDelegate:(id)delegate { |
5025 | [super setDelegate:delegate]; | |
5026 | [cydia_ setDelegate:delegate]; | |
5027 | } | |
5028 | ||
c6cf66c7 | 5029 | - (NSString *) applicationNameForUserAgent { |
e967efd5 | 5030 | return UserAgent_; |
c6cf66c7 | 5031 | } |
43f3d7f6 | 5032 | |
c6cf66c7 JF |
5033 | - (id) init { |
5034 | if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) { | |
5035 | cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease]; | |
43f3d7f6 JF |
5036 | } return self; |
5037 | } | |
5038 | ||
29bb09d7 JF |
5039 | @end |
5040 | ||
5041 | @interface AppCacheController : CydiaWebViewController { | |
5042 | } | |
5043 | ||
5044 | @end | |
5045 | ||
5046 | @implementation AppCacheController | |
5047 | ||
5048 | - (void) didReceiveMemoryWarning { | |
6271cb57 | 5049 | // XXX: this doesn't work |
29bb09d7 JF |
5050 | } |
5051 | ||
2713be8e JF |
5052 | - (bool) retainsNetworkActivityIndicator { |
5053 | return false; | |
5054 | } | |
5055 | ||
43f3d7f6 | 5056 | @end |
80319240 | 5057 | /* }}} */ |
43f3d7f6 | 5058 | |
b1ca831d JF |
5059 | // CydiaScript {{{ |
5060 | @interface NSObject (CydiaScript) | |
5061 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; | |
5062 | @end | |
5063 | ||
5064 | @implementation NSObject (CydiaScript) | |
5065 | ||
5066 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5067 | return self; | |
5068 | } | |
5069 | ||
5070 | @end | |
5071 | ||
5072 | @implementation NSArray (CydiaScript) | |
5073 | ||
5074 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5075 | WebScriptObject *object([context evaluateWebScript:@"[]"]); | |
5076 | for (size_t i(0), e([self count]); i != e; ++i) | |
5077 | [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; | |
5078 | return object; | |
5079 | } | |
5080 | ||
5081 | @end | |
5082 | ||
5083 | @implementation NSDictionary (CydiaScript) | |
5084 | ||
5085 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5086 | WebScriptObject *object([context evaluateWebScript:@"({})"]); | |
5087 | for (id i in self) | |
5088 | [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; | |
5089 | return object; | |
5090 | } | |
5091 | ||
5092 | @end | |
5093 | // }}} | |
5094 | ||
5829aea2 GP |
5095 | /* Confirmation Controller {{{ */ |
5096 | bool DepSubstrate(const pkgCache::VerIterator &iterator) { | |
5097 | if (!iterator.end()) | |
5098 | for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) { | |
5099 | if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends) | |
5100 | continue; | |
5101 | pkgCache::PkgIterator package(dep.TargetPkg()); | |
5102 | if (package.end()) | |
5103 | continue; | |
5104 | if (strcmp(package.Name(), "mobilesubstrate") == 0) | |
5105 | return true; | |
5106 | } | |
5107 | ||
5108 | return false; | |
5109 | } | |
5110 | ||
adb61bda | 5111 | @protocol ConfirmationControllerDelegate |
674dce72 | 5112 | - (void) cancelAndClear:(bool)clear; |
b5e7eebb | 5113 | - (void) confirmWithNavigationController:(UINavigationController *)navigation; |
dc63e78f | 5114 | - (void) queue; |
36bb2ca2 | 5115 | @end |
2367a917 | 5116 | |
a576488f | 5117 | @interface ConfirmationController : CydiaWebViewController { |
770f2a8e | 5118 | _transient Database *database_; |
6ceb0959 | 5119 | |
7b33d201 | 5120 | _H<UIAlertView> essential_; |
6ceb0959 | 5121 | |
7b33d201 JF |
5122 | _H<NSDictionary> changes_; |
5123 | _H<NSMutableArray> issues_; | |
5124 | _H<NSDictionary> sizes_; | |
6ceb0959 | 5125 | |
a9a0661e | 5126 | BOOL substrate_; |
36bb2ca2 | 5127 | } |
dc5812ec | 5128 | |
b5e7eebb | 5129 | - (id) initWithDatabase:(Database *)database; |
b4d89997 | 5130 | |
dc5812ec JF |
5131 | @end |
5132 | ||
adb61bda | 5133 | @implementation ConfirmationController |
dc5812ec | 5134 | |
ab2cfc1e JF |
5135 | - (void) complete { |
5136 | if (substrate_) | |
be860cc8 | 5137 | RestartSubstrate_ = true; |
ab2cfc1e JF |
5138 | [delegate_ confirmWithNavigationController:[self navigationController]]; |
5139 | } | |
5140 | ||
b5e7eebb | 5141 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
3272e699 | 5142 | NSString *context([alert context]); |
9bedffaa | 5143 | |
1cedb821 | 5144 | if ([context isEqualToString:@"remove"]) { |
ab2cfc1e | 5145 | if (button == [alert cancelButtonIndex]) |
02069daf | 5146 | [self _doContinue]; |
ab2cfc1e | 5147 | else if (button == [alert firstOtherButtonIndex]) { |
d69dbfc5 | 5148 | [self performSelector:@selector(complete) withObject:nil afterDelay:0]; |
9bedffaa | 5149 | } |
9bedffaa | 5150 | |
3272e699 | 5151 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 5152 | } else if ([context isEqualToString:@"unable"]) { |
b5e7eebb | 5153 | [self dismissModalViewControllerAnimated:YES]; |
3272e699 GP |
5154 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
5155 | } else { | |
b5e7eebb GP |
5156 | [super alertView:alert clickedButtonAtIndex:button]; |
5157 | } | |
36bb2ca2 JF |
5158 | } |
5159 | ||
46c46f4f | 5160 | - (void) _doContinue { |
21ea11a4 | 5161 | [delegate_ cancelAndClear:NO]; |
12d3d98d | 5162 | [self dismissModalViewControllerAnimated:YES]; |
46c46f4f | 5163 | } |
bc11cf5b | 5164 | |
46c46f4f JF |
5165 | - (id) invokeDefaultMethodWithArguments:(NSArray *)args { |
5166 | [self performSelectorOnMainThread:@selector(_doContinue) withObject:nil waitUntilDone:NO]; | |
21ea11a4 GP |
5167 | return nil; |
5168 | } | |
5169 | ||
2634b249 JF |
5170 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5171 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
6ceb0959 | 5172 | |
781001d7 | 5173 | [window setValue:[[NSDictionary dictionaryWithObjectsAndKeys: |
7b33d201 JF |
5174 | (id) changes_, @"changes", |
5175 | (id) issues_, @"issues", | |
5176 | (id) sizes_, @"sizes", | |
781001d7 JF |
5177 | self, @"queue", |
5178 | nil] Cydia$webScriptObjectInContext:window] forKey:@"cydiaConfirm"]; | |
36bb2ca2 JF |
5179 | } |
5180 | ||
b5e7eebb GP |
5181 | - (id) initWithDatabase:(Database *)database { |
5182 | if ((self = [super init]) != nil) { | |
770f2a8e JF |
5183 | database_ = database; |
5184 | ||
6ceb0959 JF |
5185 | NSMutableArray *installs([NSMutableArray arrayWithCapacity:16]); |
5186 | NSMutableArray *reinstalls([NSMutableArray arrayWithCapacity:16]); | |
5187 | NSMutableArray *upgrades([NSMutableArray arrayWithCapacity:16]); | |
5188 | NSMutableArray *downgrades([NSMutableArray arrayWithCapacity:16]); | |
5189 | NSMutableArray *removes([NSMutableArray arrayWithCapacity:16]); | |
dc5812ec | 5190 | |
36bb2ca2 | 5191 | bool remove(false); |
dc5812ec | 5192 | |
6ceb0959 JF |
5193 | pkgCacheFile &cache([database_ cache]); |
5194 | NSArray *packages([database_ packages]); | |
a9a0661e JF |
5195 | pkgDepCache::Policy *policy([database_ policy]); |
5196 | ||
7b33d201 | 5197 | issues_ = [NSMutableArray arrayWithCapacity:4]; |
6ceb0959 | 5198 | |
c4dcf2c2 | 5199 | for (Package *package in packages) { |
6ceb0959 JF |
5200 | pkgCache::PkgIterator iterator([package iterator]); |
5201 | NSString *name([package id]); | |
5202 | ||
5203 | if ([package broken]) { | |
5204 | NSMutableArray *reasons([NSMutableArray arrayWithCapacity:4]); | |
5205 | ||
5206 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5207 | name, @"package", | |
5208 | reasons, @"reasons", | |
5209 | nil]]; | |
5210 | ||
5211 | pkgCache::VerIterator ver(cache[iterator].InstVerIter(cache)); | |
5212 | if (ver.end()) | |
5213 | continue; | |
5214 | ||
5215 | for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) { | |
5216 | pkgCache::DepIterator start; | |
5217 | pkgCache::DepIterator end; | |
5218 | dep.GlobOr(start, end); // ++dep | |
5219 | ||
5220 | if (!cache->IsImportantDep(end)) | |
5221 | continue; | |
5222 | if ((cache[end] & pkgDepCache::DepGInstall) != 0) | |
5223 | continue; | |
5224 | ||
810c9763 JF |
5225 | NSMutableArray *clauses([NSMutableArray arrayWithCapacity:4]); |
5226 | ||
5227 | [reasons addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5228 | [NSString stringWithUTF8String:start.DepType()], @"relationship", | |
5229 | clauses, @"clauses", | |
5230 | nil]]; | |
5231 | ||
6ceb0959 JF |
5232 | _forever { |
5233 | NSString *reason, *installed((NSString *) [WebUndefined undefined]); | |
5234 | ||
5235 | pkgCache::PkgIterator target(start.TargetPkg()); | |
5236 | if (target->ProvidesList != 0) | |
5237 | reason = @"missing"; | |
5238 | else { | |
5239 | pkgCache::VerIterator ver(cache[target].InstVerIter(cache)); | |
5240 | if (!ver.end()) { | |
5241 | reason = @"installed"; | |
5242 | installed = [NSString stringWithUTF8String:ver.VerStr()]; | |
5243 | } else if (!cache[target].CandidateVerIter(cache).end()) | |
5244 | reason = @"uninstalled"; | |
5245 | else if (target->ProvidesList == 0) | |
5246 | reason = @"uninstallable"; | |
5247 | else | |
5248 | reason = @"virtual"; | |
5249 | } | |
5250 | ||
3e5a9f5d | 5251 | NSDictionary *version(start.TargetVer() == 0 ? (NSDictionary *) [NSNull null] : [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5252 | [NSString stringWithUTF8String:start.CompType()], @"operator", |
5253 | [NSString stringWithUTF8String:start.TargetVer()], @"value", | |
5254 | nil]); | |
5255 | ||
810c9763 | 5256 | [clauses addObject:[NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5257 | [NSString stringWithUTF8String:start.TargetPkg().Name()], @"package", |
5258 | version, @"version", | |
5259 | reason, @"reason", | |
5260 | installed, @"installed", | |
5261 | nil]]; | |
5262 | ||
5263 | // yes, seriously. (wtf?) | |
5264 | if (start == end) | |
5265 | break; | |
5266 | ++start; | |
5267 | } | |
5268 | } | |
5269 | } | |
5270 | ||
36bb2ca2 | 5271 | pkgDepCache::StateCache &state(cache[iterator]); |
dc5812ec | 5272 | |
b4fd1197 | 5273 | static RegEx special_r("(firmware|gsc\\..*|cy\\+.*)"); |
2388b078 | 5274 | |
36bb2ca2 | 5275 | if (state.NewInstall()) |
6ceb0959 | 5276 | [installs addObject:name]; |
f8d15be2 | 5277 | // XXX: else if (state.Install()) |
36bb2ca2 | 5278 | else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall) |
6ceb0959 | 5279 | [reinstalls addObject:name]; |
f8d15be2 | 5280 | // XXX: move before previous if |
36bb2ca2 | 5281 | else if (state.Upgrade()) |
6ceb0959 | 5282 | [upgrades addObject:name]; |
36bb2ca2 | 5283 | else if (state.Downgrade()) |
6ceb0959 JF |
5284 | [downgrades addObject:name]; |
5285 | else if (!state.Delete()) | |
f8d15be2 | 5286 | // XXX: _assert(state.Keep()); |
6ceb0959 | 5287 | continue; |
1916f316 JF |
5288 | else if (special_r(name)) |
5289 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5290 | [NSNull null], @"package", | |
5291 | [NSArray arrayWithObjects: | |
5292 | [NSDictionary dictionaryWithObjectsAndKeys: | |
810c9763 JF |
5293 | @"Conflicts", @"relationship", |
5294 | [NSArray arrayWithObjects: | |
5295 | [NSDictionary dictionaryWithObjectsAndKeys: | |
5296 | name, @"package", | |
5297 | [NSNull null], @"version", | |
5298 | @"installed", @"reason", | |
5299 | nil], | |
5300 | nil], @"clauses", | |
1916f316 JF |
5301 | nil], |
5302 | nil], @"reasons", | |
5303 | nil]]; | |
5304 | else { | |
2388b078 | 5305 | if ([package essential]) |
36bb2ca2 | 5306 | remove = true; |
6ceb0959 JF |
5307 | [removes addObject:name]; |
5308 | } | |
a9a0661e JF |
5309 | |
5310 | substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator)); | |
5311 | substrate_ |= DepSubstrate(iterator.CurrentVer()); | |
36bb2ca2 | 5312 | } |
ec97ef06 | 5313 | |
36bb2ca2 JF |
5314 | if (!remove) |
5315 | essential_ = nil; | |
d791dce4 | 5316 | else if (Advanced_) { |
43f3d7f6 | 5317 | NSString *parenthetical(UCLocalize("PARENTHETICAL")); |
f79a4512 | 5318 | |
7b33d201 | 5319 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 5320 | initWithTitle:UCLocalize("REMOVING_ESSENTIALS") |
b5e7eebb GP |
5321 | message:UCLocalize("REMOVING_ESSENTIALS_EX") |
5322 | delegate:self | |
5323 | cancelButtonTitle:[NSString stringWithFormat:parenthetical, UCLocalize("CANCEL_OPERATION"), UCLocalize("SAFE")] | |
1aa29546 JF |
5324 | otherButtonTitles: |
5325 | [NSString stringWithFormat:parenthetical, UCLocalize("FORCE_REMOVAL"), UCLocalize("UNSAFE")], | |
5326 | nil | |
7b33d201 | 5327 | ] autorelease]; |
04fe1349 | 5328 | |
3272e699 | 5329 | [essential_ setContext:@"remove"]; |
9aaebfec | 5330 | [essential_ setNumberOfRows:2]; |
9bedffaa | 5331 | } else { |
7b33d201 | 5332 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 5333 | initWithTitle:UCLocalize("UNABLE_TO_COMPLY") |
b5e7eebb GP |
5334 | message:UCLocalize("UNABLE_TO_COMPLY_EX") |
5335 | delegate:self | |
5336 | cancelButtonTitle:UCLocalize("OKAY") | |
5337 | otherButtonTitles:nil | |
7b33d201 | 5338 | ] autorelease]; |
ec97ef06 | 5339 | |
b5e7eebb | 5340 | [essential_ setContext:@"unable"]; |
36bb2ca2 | 5341 | } |
ec97ef06 | 5342 | |
7b33d201 | 5343 | changes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5344 | installs, @"installs", |
5345 | reinstalls, @"reinstalls", | |
5346 | upgrades, @"upgrades", | |
5347 | downgrades, @"downgrades", | |
5348 | removes, @"removes", | |
affeffc7 | 5349 | nil]; |
dc5812ec | 5350 | |
7b33d201 | 5351 | sizes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5352 | [NSNumber numberWithInteger:[database_ fetcher].FetchNeeded()], @"downloading", |
5353 | [NSNumber numberWithInteger:[database_ fetcher].PartialPresent()], @"resuming", | |
affeffc7 | 5354 | nil]; |
dc5812ec | 5355 | |
90351d93 | 5356 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/confirm/", UI_]]]; |
36bb2ca2 | 5357 | } return self; |
b4d89997 | 5358 | } |
8da60fb7 | 5359 | |
e6124cb6 JF |
5360 | - (UIBarButtonItem *) leftButton { |
5361 | return [[[UIBarButtonItem alloc] | |
5362 | initWithTitle:UCLocalize("CANCEL") | |
5363 | style:UIBarButtonItemStylePlain | |
5364 | target:self | |
5365 | action:@selector(cancelButtonClicked) | |
5366 | ] autorelease]; | |
5367 | } | |
5368 | ||
614cd4f1 | 5369 | #if !AlwaysReload |
bcde1e70 | 5370 | - (void) applyRightButton { |
6ceb0959 | 5371 | if ([issues_ count] == 0 && ![self isLoading]) |
dd9de556 JF |
5372 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
5373 | initWithTitle:UCLocalize("CONFIRM") | |
2761d574 | 5374 | style:UIBarButtonItemStyleDone |
dd9de556 JF |
5375 | target:self |
5376 | action:@selector(confirmButtonClicked) | |
5377 | ] autorelease]]; | |
5378 | else | |
bcde1e70 | 5379 | [[self navigationItem] setRightBarButtonItem:nil]; |
affeffc7 | 5380 | } |
bcde1e70 | 5381 | #endif |
affeffc7 | 5382 | |
b5e7eebb | 5383 | - (void) cancelButtonClicked { |
21ea11a4 | 5384 | [delegate_ cancelAndClear:YES]; |
05452929 | 5385 | [self dismissModalViewControllerAnimated:YES]; |
affeffc7 JF |
5386 | } |
5387 | ||
9487f027 | 5388 | #if !AlwaysReload |
b5e7eebb | 5389 | - (void) confirmButtonClicked { |
affeffc7 | 5390 | if (essential_ != nil) |
b5e7eebb | 5391 | [essential_ show]; |
ab2cfc1e JF |
5392 | else |
5393 | [self complete]; | |
affeffc7 | 5394 | } |
9487f027 | 5395 | #endif |
affeffc7 | 5396 | |
36bb2ca2 JF |
5397 | @end |
5398 | /* }}} */ | |
8da60fb7 | 5399 | |
aaae308d JF |
5400 | /* Progress Data {{{ */ |
5401 | @interface CydiaProgressData : NSObject { | |
5402 | _transient id delegate_; | |
5403 | ||
5404 | bool running_; | |
b0b11d99 | 5405 | float percent_; |
aaae308d | 5406 | |
bcbac8f7 JF |
5407 | float current_; |
5408 | float total_; | |
5409 | float speed_; | |
5410 | ||
aaae308d JF |
5411 | _H<NSMutableArray> events_; |
5412 | _H<NSString> title_; | |
5413 | ||
5414 | _H<NSString> status_; | |
5415 | _H<NSString> finish_; | |
5416 | } | |
5417 | ||
5418 | @end | |
5419 | ||
5420 | @implementation CydiaProgressData | |
5421 | ||
5422 | + (NSArray *) _attributeKeys { | |
5423 | return [NSArray arrayWithObjects: | |
bcbac8f7 | 5424 | @"current", |
aaae308d JF |
5425 | @"events", |
5426 | @"finish", | |
b0b11d99 | 5427 | @"percent", |
aaae308d | 5428 | @"running", |
bcbac8f7 | 5429 | @"speed", |
aaae308d | 5430 | @"title", |
bcbac8f7 | 5431 | @"total", |
aaae308d JF |
5432 | nil]; |
5433 | } | |
5434 | ||
5435 | - (NSArray *) attributeKeys { | |
5436 | return [[self class] _attributeKeys]; | |
5437 | } | |
5438 | ||
5439 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
5440 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
5441 | } | |
5442 | ||
5443 | - (id) init { | |
5444 | if ((self = [super init]) != nil) { | |
5445 | events_ = [NSMutableArray arrayWithCapacity:32]; | |
5446 | } return self; | |
5447 | } | |
5448 | ||
353dda5b JF |
5449 | - (id) delegate { |
5450 | return delegate_; | |
5451 | } | |
5452 | ||
aaae308d JF |
5453 | - (void) setDelegate:(id)delegate { |
5454 | delegate_ = delegate; | |
5455 | } | |
5456 | ||
b0b11d99 JF |
5457 | - (void) setPercent:(float)value { |
5458 | percent_ = value; | |
aaae308d JF |
5459 | } |
5460 | ||
b0b11d99 JF |
5461 | - (NSNumber *) percent { |
5462 | return [NSNumber numberWithFloat:percent_]; | |
aaae308d JF |
5463 | } |
5464 | ||
bcbac8f7 JF |
5465 | - (void) setCurrent:(float)value { |
5466 | current_ = value; | |
5467 | } | |
5468 | ||
5469 | - (NSNumber *) current { | |
5470 | return [NSNumber numberWithFloat:current_]; | |
5471 | } | |
5472 | ||
5473 | - (void) setTotal:(float)value { | |
5474 | total_ = value; | |
5475 | } | |
5476 | ||
5477 | - (NSNumber *) total { | |
5478 | return [NSNumber numberWithFloat:total_]; | |
5479 | } | |
5480 | ||
5481 | - (void) setSpeed:(float)value { | |
5482 | speed_ = value; | |
5483 | } | |
5484 | ||
5485 | - (NSNumber *) speed { | |
5486 | return [NSNumber numberWithFloat:speed_]; | |
5487 | } | |
5488 | ||
aaae308d JF |
5489 | - (NSArray *) events { |
5490 | return events_; | |
5491 | } | |
5492 | ||
5493 | - (void) removeAllEvents { | |
5494 | [events_ removeAllObjects]; | |
5495 | } | |
5496 | ||
5497 | - (void) addEvent:(CydiaProgressEvent *)event { | |
5498 | [events_ addObject:event]; | |
5499 | } | |
5500 | ||
5501 | - (void) setTitle:(NSString *)text { | |
5502 | title_ = text; | |
5503 | } | |
5504 | ||
5505 | - (NSString *) title { | |
5506 | return title_; | |
5507 | } | |
5508 | ||
5509 | - (void) setFinish:(NSString *)text { | |
5510 | finish_ = text; | |
5511 | } | |
5512 | ||
5513 | - (NSString *) finish { | |
5514 | return (id) finish_ ?: [NSNull null]; | |
5515 | } | |
5516 | ||
5517 | - (void) setRunning:(bool)running { | |
5518 | running_ = running; | |
5519 | } | |
5520 | ||
5521 | - (NSNumber *) running { | |
5522 | return running_ ? (NSNumber *) kCFBooleanTrue : (NSNumber *) kCFBooleanFalse; | |
5523 | } | |
5524 | ||
5525 | @end | |
5526 | /* }}} */ | |
adb61bda | 5527 | /* Progress Controller {{{ */ |
a576488f | 5528 | @interface ProgressController : CydiaWebViewController < |
36bb2ca2 JF |
5529 | ProgressDelegate |
5530 | > { | |
8b29f8e6 | 5531 | _transient Database *database_; |
bf7c998c | 5532 | _H<CydiaProgressData, 1> progress_; |
aaae308d | 5533 | unsigned cancel_; |
2367a917 JF |
5534 | } |
5535 | ||
b5e7eebb | 5536 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
2367a917 | 5537 | |
6915b806 | 5538 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title; |
2367a917 | 5539 | |
6915b806 | 5540 | - (void) setTitle:(NSString *)title; |
aaae308d | 5541 | - (void) setCancellable:(bool)cancellable; |
bd150f54 | 5542 | |
36bb2ca2 JF |
5543 | @end |
5544 | ||
adb61bda | 5545 | @implementation ProgressController |
2367a917 JF |
5546 | |
5547 | - (void) dealloc { | |
6915b806 | 5548 | [database_ setProgressDelegate:nil]; |
2367a917 JF |
5549 | [super dealloc]; |
5550 | } | |
5551 | ||
63ae52be JF |
5552 | - (UIBarButtonItem *) leftButton { |
5553 | return cancel_ == 1 ? [[[UIBarButtonItem alloc] | |
3c62d654 JF |
5554 | initWithTitle:UCLocalize("CANCEL") |
5555 | style:UIBarButtonItemStylePlain | |
5556 | target:self | |
5557 | action:@selector(cancel) | |
63ae52be JF |
5558 | ] autorelease] : nil; |
5559 | } | |
5560 | ||
5561 | - (void) updateCancel { | |
5562 | [super applyLeftButton]; | |
3c62d654 JF |
5563 | } |
5564 | ||
b5e7eebb GP |
5565 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { |
5566 | if ((self = [super init]) != nil) { | |
8b29f8e6 | 5567 | database_ = database; |
36bb2ca2 | 5568 | delegate_ = delegate; |
ec97ef06 | 5569 | |
6915b806 JF |
5570 | [database_ setProgressDelegate:self]; |
5571 | ||
aaae308d JF |
5572 | progress_ = [[[CydiaProgressData alloc] init] autorelease]; |
5573 | [progress_ setDelegate:self]; | |
3c62d654 | 5574 | |
90351d93 | 5575 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/progress/", UI_]]]; |
3c62d654 JF |
5576 | |
5577 | [scroller_ setBackgroundColor:[UIColor blackColor]]; | |
5578 | ||
5579 | [[self navigationItem] setHidesBackButton:YES]; | |
5580 | ||
5581 | [self updateCancel]; | |
36bb2ca2 | 5582 | } return self; |
2367a917 JF |
5583 | } |
5584 | ||
aaae308d JF |
5585 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5586 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
5587 | [window setValue:progress_ forKey:@"cydiaProgress"]; | |
5588 | } | |
bc11cf5b | 5589 | |
aaae308d JF |
5590 | - (void) updateProgress { |
5591 | [self dispatchEvent:@"CydiaProgressUpdate"]; | |
5592 | } | |
b5e7eebb | 5593 | |
b5e7eebb | 5594 | - (void) viewWillAppear:(BOOL)animated { |
14e4ff09 | 5595 | [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlack]; |
3c62d654 | 5596 | [super viewWillAppear:animated]; |
2367a917 JF |
5597 | } |
5598 | ||
aaae308d | 5599 | - (void) close { |
ef494bd8 RP |
5600 | UpdateExternalStatus(0); |
5601 | ||
9dd3045d JF |
5602 | if (Finish_ > 1) |
5603 | [delegate_ saveState]; | |
5604 | ||
670a0494 JF |
5605 | switch (Finish_) { |
5606 | case 0: | |
2925cbba | 5607 | [delegate_ returnToCydia]; |
670a0494 JF |
5608 | break; |
5609 | ||
5610 | case 1: | |
c4899376 JF |
5611 | [delegate_ terminateWithSuccess]; |
5612 | /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) | |
5613 | [delegate_ suspendWithAnimation:YES]; | |
5614 | else | |
5615 | [delegate_ suspend];*/ | |
670a0494 JF |
5616 | break; |
5617 | ||
5618 | case 2: | |
985d2dff | 5619 | _trace(); |
ef812071 | 5620 | goto reload; |
670a0494 JF |
5621 | |
5622 | case 3: | |
985d2dff | 5623 | _trace(); |
ef812071 JF |
5624 | goto reload; |
5625 | ||
a3755a1e JF |
5626 | reload: { |
5627 | UIProgressHUD *hud([delegate_ addProgressHUD]); | |
5628 | [hud setText:UCLocalize("LOADING")]; | |
42bbf27d | 5629 | [delegate_ performSelector:@selector(reloadSpringBoard) withObject:nil afterDelay:0.5]; |
317eb8e3 JF |
5630 | return; |
5631 | } | |
670a0494 JF |
5632 | |
5633 | case 4: | |
985d2dff | 5634 | _trace(); |
0e371502 JF |
5635 | if (void (*SBReboot)(mach_port_t) = reinterpret_cast<void (*)(mach_port_t)>(dlsym(RTLD_DEFAULT, "SBReboot"))) |
5636 | SBReboot(SBSSpringBoardServerPort()); | |
5637 | else | |
bb0fe3c9 | 5638 | reboot2(RB_AUTOBOOT); |
670a0494 | 5639 | break; |
bc8cd583 | 5640 | } |
aaae308d JF |
5641 | |
5642 | [super close]; | |
670a0494 | 5643 | } |
bd150f54 | 5644 | |
6915b806 | 5645 | - (void) setTitle:(NSString *)title { |
aaae308d JF |
5646 | [progress_ setTitle:title]; |
5647 | [self updateProgress]; | |
5648 | } | |
5649 | ||
5650 | - (UIBarButtonItem *) rightButton { | |
d53628b6 | 5651 | return [[progress_ running] boolValue] ? [super rightButton] : [[[UIBarButtonItem alloc] |
aaae308d JF |
5652 | initWithTitle:UCLocalize("CLOSE") |
5653 | style:UIBarButtonItemStylePlain | |
5654 | target:self | |
5655 | action:@selector(close) | |
5656 | ] autorelease]; | |
6915b806 JF |
5657 | } |
5658 | ||
5659 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title { | |
5660 | UpdateExternalStatus(1); | |
5661 | ||
aaae308d | 5662 | [progress_ setRunning:true]; |
6915b806 | 5663 | [self setTitle:title]; |
aaae308d | 5664 | // implicit updateProgress |
6915b806 | 5665 | |
140710ba | 5666 | SHA1SumValue notifyconf; { |
6915b806 JF |
5667 | FileFd file; |
5668 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
5669 | _error->Discard(); | |
5670 | else { | |
5671 | MMap mmap(file, MMap::ReadOnly); | |
5672 | SHA1Summation sha1; | |
5673 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5674 | notifyconf = sha1.Result(); |
6915b806 JF |
5675 | } |
5676 | } | |
5677 | ||
140710ba | 5678 | SHA1SumValue springlist; { |
6915b806 JF |
5679 | FileFd file; |
5680 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
5681 | _error->Discard(); | |
5682 | else { | |
5683 | MMap mmap(file, MMap::ReadOnly); | |
5684 | SHA1Summation sha1; | |
5685 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5686 | springlist = sha1.Result(); |
6915b806 JF |
5687 | } |
5688 | } | |
5689 | ||
5690 | if (invocation != nil) { | |
5691 | [invocation yieldToSelector:@selector(invoke)]; | |
aaae308d | 5692 | [self setTitle:@"COMPLETE"]; |
6915b806 | 5693 | } |
670a0494 | 5694 | |
22f8bed9 | 5695 | if (Finish_ < 4) { |
70d45c1e JF |
5696 | FileFd file; |
5697 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
5698 | _error->Discard(); | |
5699 | else { | |
5700 | MMap mmap(file, MMap::ReadOnly); | |
5701 | SHA1Summation sha1; | |
5702 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5703 | if (!(notifyconf == sha1.Result())) |
70d45c1e JF |
5704 | Finish_ = 4; |
5705 | } | |
22f8bed9 JF |
5706 | } |
5707 | ||
affeffc7 | 5708 | if (Finish_ < 3) { |
70d45c1e JF |
5709 | FileFd file; |
5710 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
5711 | _error->Discard(); | |
5712 | else { | |
5713 | MMap mmap(file, MMap::ReadOnly); | |
5714 | SHA1Summation sha1; | |
5715 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5716 | if (!(springlist == sha1.Result())) |
70d45c1e JF |
5717 | Finish_ = 3; |
5718 | } | |
dddbc481 JF |
5719 | } |
5720 | ||
be860cc8 JF |
5721 | if (Finish_ < 2) { |
5722 | if (RestartSubstrate_) | |
5723 | Finish_ = 2; | |
5724 | } | |
5725 | ||
5726 | RestartSubstrate_ = false; | |
5727 | ||
bc8cd583 | 5728 | switch (Finish_) { |
aaae308d JF |
5729 | case 0: [progress_ setFinish:UCLocalize("RETURN_TO_CYDIA")]; break; /* XXX: Maybe UCLocalize("DONE")? */ |
5730 | case 1: [progress_ setFinish:UCLocalize("CLOSE_CYDIA")]; break; | |
5731 | case 2: [progress_ setFinish:UCLocalize("RESTART_SPRINGBOARD")]; break; | |
5732 | case 3: [progress_ setFinish:UCLocalize("RELOAD_SPRINGBOARD")]; break; | |
5733 | case 4: [progress_ setFinish:UCLocalize("REBOOT_DEVICE")]; break; | |
bc8cd583 JF |
5734 | } |
5735 | ||
eb403f34 | 5736 | UpdateExternalStatus(Finish_ == 0 ? 0 : 2); |
ef494bd8 | 5737 | |
aaae308d JF |
5738 | [progress_ setRunning:false]; |
5739 | [self updateProgress]; | |
5740 | ||
5741 | [self applyRightButton]; | |
31f3cfff JF |
5742 | } |
5743 | ||
6915b806 | 5744 | - (void) addProgressEvent:(CydiaProgressEvent *)event { |
aaae308d JF |
5745 | [progress_ addEvent:event]; |
5746 | [self updateProgress]; | |
baf80942 JF |
5747 | } |
5748 | ||
ff2d5dcd | 5749 | - (bool) isProgressCancelled { |
aaae308d JF |
5750 | return cancel_ == 2; |
5751 | } | |
5752 | ||
5753 | - (void) cancel { | |
5754 | cancel_ = 2; | |
5755 | [self updateCancel]; | |
5756 | } | |
5757 | ||
5758 | - (void) setCancellable:(bool)cancellable { | |
5759 | unsigned cancel(cancel_); | |
5760 | ||
5761 | if (!cancellable) | |
5762 | cancel_ = 0; | |
5763 | else if (cancel_ == 0) | |
5764 | cancel_ = 1; | |
5765 | ||
5766 | if (cancel != cancel_) | |
5767 | [self updateCancel]; | |
5768 | } | |
5769 | ||
5770 | - (void) setProgressCancellable:(NSNumber *)cancellable { | |
5771 | [self setCancellable:[cancellable boolValue]]; | |
baf80942 JF |
5772 | } |
5773 | ||
d885343d | 5774 | - (void) setProgressPercent:(NSNumber *)percent { |
b0b11d99 | 5775 | [progress_ setPercent:[percent floatValue]]; |
aaae308d | 5776 | [self updateProgress]; |
49048579 JF |
5777 | } |
5778 | ||
bcbac8f7 JF |
5779 | - (void) setProgressStatus:(NSDictionary *)status { |
5780 | if (status == nil) { | |
5781 | [progress_ setCurrent:0]; | |
5782 | [progress_ setTotal:0]; | |
5783 | [progress_ setSpeed:0]; | |
5784 | } else { | |
5785 | [progress_ setPercent:[[status objectForKey:@"Percent"] floatValue]]; | |
5786 | ||
5787 | [progress_ setCurrent:[[status objectForKey:@"Current"] floatValue]]; | |
5788 | [progress_ setTotal:[[status objectForKey:@"Total"] floatValue]]; | |
5789 | [progress_ setSpeed:[[status objectForKey:@"Speed"] floatValue]]; | |
5790 | } | |
5791 | ||
5792 | [self updateProgress]; | |
5793 | } | |
5794 | ||
36bb2ca2 JF |
5795 | @end |
5796 | /* }}} */ | |
dc088e63 | 5797 | |
46aa9775 | 5798 | /* Package Cell {{{ */ |
a9311516 | 5799 | @interface PackageCell : CyteTableViewCell < |
b97fcfc6 | 5800 | CyteTableViewCellDelegate |
c21004b9 | 5801 | > { |
7b33d201 JF |
5802 | _H<UIImage> icon_; |
5803 | _H<NSString> name_; | |
5804 | _H<NSString> description_; | |
3bd1c2a2 | 5805 | bool commercial_; |
7b33d201 JF |
5806 | _H<NSString> source_; |
5807 | _H<UIImage> badge_; | |
7b33d201 | 5808 | _H<UIImage> placard_; |
59f3d290 | 5809 | bool summarized_; |
36bb2ca2 | 5810 | } |
723a0072 | 5811 | |
36bb2ca2 | 5812 | - (PackageCell *) init; |
59f3d290 | 5813 | - (void) setPackage:(Package *)package asSummary:(bool)summary; |
ec97ef06 | 5814 | |
327624b6 JF |
5815 | - (void) drawContentRect:(CGRect)rect; |
5816 | ||
5817 | @end | |
5818 | ||
36bb2ca2 JF |
5819 | @implementation PackageCell |
5820 | ||
36bb2ca2 | 5821 | - (PackageCell *) init { |
327624b6 JF |
5822 | CGRect frame(CGRectMake(0, 0, 320, 74)); |
5823 | if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) { | |
5824 | UIView *content([self contentView]); | |
5825 | CGRect bounds([content bounds]); | |
04fe1349 | 5826 | |
b97fcfc6 | 5827 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
04fe1349 JF |
5828 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5829 | [content addSubview:content_]; | |
5830 | ||
327624b6 | 5831 | [content_ setDelegate:self]; |
327624b6 | 5832 | [content_ setOpaque:YES]; |
36bb2ca2 | 5833 | } return self; |
b4d89997 | 5834 | } |
b19871dd | 5835 | |
003fc610 | 5836 | - (NSString *) accessibilityLabel { |
353dda5b | 5837 | return name_; |
003fc610 GP |
5838 | } |
5839 | ||
59f3d290 JF |
5840 | - (void) setPackage:(Package *)package asSummary:(bool)summary { |
5841 | summarized_ = summary; | |
5842 | ||
7b33d201 JF |
5843 | icon_ = nil; |
5844 | name_ = nil; | |
5845 | description_ = nil; | |
5846 | source_ = nil; | |
5847 | badge_ = nil; | |
5848 | placard_ = nil; | |
7b33d201 | 5849 | |
80132602 JF |
5850 | if (package == nil) |
5851 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
5852 | else { | |
5853 | [package parse]; | |
31f3cfff | 5854 | |
80132602 | 5855 | Source *source = [package source]; |
b19871dd | 5856 | |
80132602 | 5857 | icon_ = [package icon]; |
78de2878 | 5858 | |
80132602 JF |
5859 | if (NSString *name = [package name]) |
5860 | name_ = [NSString stringWithString:name]; | |
ef055c6c | 5861 | |
9374f6b0 | 5862 | if (NSString *description = [package shortDescription]) |
80132602 | 5863 | description_ = [NSString stringWithString:description]; |
ef055c6c | 5864 | |
80132602 | 5865 | commercial_ = [package isCommercial]; |
36bb2ca2 | 5866 | |
80132602 JF |
5867 | NSString *label = nil; |
5868 | bool trusted = false; | |
b19871dd | 5869 | |
80132602 JF |
5870 | if (source != nil) { |
5871 | label = [source label]; | |
5872 | trusted = [source trusted]; | |
5873 | } else if ([[package id] isEqualToString:@"firmware"]) | |
5874 | label = UCLocalize("APPLE"); | |
5875 | else | |
5876 | label = [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("UNKNOWN"), UCLocalize("LOCAL")]; | |
b19871dd | 5877 | |
80132602 | 5878 | NSString *from(label); |
a54b1c10 | 5879 | |
80132602 JF |
5880 | NSString *section = [package simpleSection]; |
5881 | if (section != nil && ![section isEqualToString:label]) { | |
5882 | section = [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
5883 | from = [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), from, section]; | |
5884 | } | |
a54b1c10 | 5885 | |
80132602 | 5886 | source_ = [NSString stringWithFormat:UCLocalize("FROM"), from]; |
36bb2ca2 | 5887 | |
80132602 JF |
5888 | if (NSString *purpose = [package primaryPurpose]) |
5889 | badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]; | |
c390d3ab | 5890 | |
80132602 JF |
5891 | UIColor *color; |
5892 | NSString *placard; | |
5893 | ||
5894 | if (NSString *mode = [package mode]) { | |
5895 | if ([mode isEqualToString:@"REMOVE"] || [mode isEqualToString:@"PURGE"]) { | |
5896 | color = RemovingColor_; | |
86a333c6 | 5897 | placard = @"removing"; |
80132602 JF |
5898 | } else { |
5899 | color = InstallingColor_; | |
86a333c6 | 5900 | placard = @"installing"; |
80132602 | 5901 | } |
d832908d | 5902 | } else { |
80132602 JF |
5903 | color = [UIColor whiteColor]; |
5904 | ||
5905 | if ([package installed] != nil) | |
5906 | placard = @"installed"; | |
5907 | else | |
5908 | placard = nil; | |
d832908d JF |
5909 | } |
5910 | ||
80132602 | 5911 | [content_ setBackgroundColor:color]; |
d832908d | 5912 | |
80132602 JF |
5913 | if (placard != nil) |
5914 | placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/%@.png", App_, placard]]; | |
d832908d JF |
5915 | } |
5916 | ||
d832908d | 5917 | [self setNeedsDisplay]; |
327624b6 | 5918 | [content_ setNeedsDisplay]; |
3bd1c2a2 JF |
5919 | } |
5920 | ||
59f3d290 | 5921 | - (void) drawSummaryContentRect:(CGRect)rect { |
555aaf71 | 5922 | bool highlighted(highlighted_); |
ef055c6c | 5923 | float width([self bounds].size.width); |
dc63e78f | 5924 | |
59f3d290 JF |
5925 | if (icon_ != nil) { |
5926 | CGRect rect; | |
5927 | rect.size = [(UIImage *) icon_ size]; | |
5928 | ||
25c1dafb | 5929 | while (rect.size.width > 16 || rect.size.height > 16) { |
7e1f9f6a JF |
5930 | rect.size.width /= 2; |
5931 | rect.size.height /= 2; | |
5932 | } | |
59f3d290 | 5933 | |
86a333c6 JF |
5934 | rect.origin.x = 19 - rect.size.width / 2; |
5935 | rect.origin.y = 19 - rect.size.height / 2; | |
59f3d290 | 5936 | |
8323c1b9 | 5937 | [icon_ drawInRect:Retina(rect)]; |
59f3d290 JF |
5938 | } |
5939 | ||
5940 | if (badge_ != nil) { | |
5941 | CGRect rect; | |
5942 | rect.size = [(UIImage *) badge_ size]; | |
5943 | ||
5944 | rect.size.width /= 4; | |
5945 | rect.size.height /= 4; | |
5946 | ||
86a333c6 JF |
5947 | rect.origin.x = 25 - rect.size.width / 2; |
5948 | rect.origin.y = 25 - rect.size.height / 2; | |
59f3d290 | 5949 | |
8323c1b9 | 5950 | [badge_ drawInRect:Retina(rect)]; |
59f3d290 JF |
5951 | } |
5952 | ||
5d0438dc | 5953 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
59f3d290 JF |
5954 | UISetColor(White_); |
5955 | ||
5956 | if (!highlighted) | |
5957 | UISetColor(commercial_ ? Purple_ : Black_); | |
b129e6d9 | 5958 | [name_ drawAtPoint:CGPointMake(36, 8) forWidth:(width - (placard_ == nil ? 68 : 94)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
59f3d290 JF |
5959 | |
5960 | if (placard_ != nil) | |
86a333c6 | 5961 | [placard_ drawAtPoint:CGPointMake(width - 52, 11)]; |
59f3d290 JF |
5962 | } |
5963 | ||
5964 | - (void) drawNormalContentRect:(CGRect)rect { | |
5965 | bool highlighted(highlighted_); | |
5966 | float width([self bounds].size.width); | |
b19871dd | 5967 | |
baf80942 JF |
5968 | if (icon_ != nil) { |
5969 | CGRect rect; | |
7b33d201 | 5970 | rect.size = [(UIImage *) icon_ size]; |
baf80942 | 5971 | |
25c1dafb | 5972 | while (rect.size.width > 32 || rect.size.height > 32) { |
7e1f9f6a JF |
5973 | rect.size.width /= 2; |
5974 | rect.size.height /= 2; | |
5975 | } | |
baf80942 JF |
5976 | |
5977 | rect.origin.x = 25 - rect.size.width / 2; | |
5978 | rect.origin.y = 25 - rect.size.height / 2; | |
5979 | ||
8323c1b9 | 5980 | [icon_ drawInRect:Retina(rect)]; |
baf80942 | 5981 | } |
b19871dd | 5982 | |
c390d3ab | 5983 | if (badge_ != nil) { |
f79c810d | 5984 | CGRect rect; |
7b33d201 | 5985 | rect.size = [(UIImage *) badge_ size]; |
f79c810d JF |
5986 | |
5987 | rect.size.width /= 2; | |
5988 | rect.size.height /= 2; | |
5989 | ||
5990 | rect.origin.x = 36 - rect.size.width / 2; | |
5991 | rect.origin.y = 36 - rect.size.height / 2; | |
c390d3ab | 5992 | |
8323c1b9 | 5993 | [badge_ drawInRect:Retina(rect)]; |
c390d3ab JF |
5994 | } |
5995 | ||
5d0438dc | 5996 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
f641a0e5 | 5997 | UISetColor(White_); |
b19871dd | 5998 | |
555aaf71 | 5999 | if (!highlighted) |
3bd1c2a2 | 6000 | UISetColor(commercial_ ? Purple_ : Black_); |
b129e6d9 JF |
6001 | [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
6002 | [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; | |
b19871dd | 6003 | |
555aaf71 | 6004 | if (!highlighted) |
3bd1c2a2 | 6005 | UISetColor(commercial_ ? Purplish_ : Gray_); |
b129e6d9 | 6006 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:NSLineBreakByTruncatingTail]; |
670a0494 JF |
6007 | |
6008 | if (placard_ != nil) | |
01d93940 | 6009 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; |
ec97ef06 JF |
6010 | } |
6011 | ||
59f3d290 JF |
6012 | - (void) drawContentRect:(CGRect)rect { |
6013 | if (summarized_) | |
6014 | [self drawSummaryContentRect:rect]; | |
6015 | else | |
6016 | [self drawNormalContentRect:rect]; | |
6017 | } | |
6018 | ||
8da60fb7 JF |
6019 | @end |
6020 | /* }}} */ | |
36bb2ca2 | 6021 | /* Section Cell {{{ */ |
a9311516 | 6022 | @interface SectionCell : CyteTableViewCell < |
b97fcfc6 | 6023 | CyteTableViewCellDelegate |
c21004b9 | 6024 | > { |
7b33d201 JF |
6025 | _H<NSString> basic_; |
6026 | _H<NSString> section_; | |
6027 | _H<NSString> name_; | |
6028 | _H<NSString> count_; | |
6029 | _H<UIImage> icon_; | |
6030 | _H<UISwitch> switch_; | |
6d9712c4 | 6031 | BOOL editing_; |
b4d89997 | 6032 | } |
b19871dd | 6033 | |
6d9712c4 | 6034 | - (void) setSection:(Section *)section editing:(BOOL)editing; |
36bb2ca2 | 6035 | |
8da60fb7 JF |
6036 | @end |
6037 | ||
36bb2ca2 | 6038 | @implementation SectionCell |
8da60fb7 | 6039 | |
46aa9775 GP |
6040 | - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
6041 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
8dbf3abc | 6042 | icon_ = [UIImage imageNamed:@"folder.png"]; |
6415105e | 6043 | // XXX: this initial frame is wrong, but is fixed later |
7b33d201 | 6044 | switch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)] autorelease]; |
46aa9775 GP |
6045 | [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; |
6046 | ||
6047 | UIView *content([self contentView]); | |
6048 | CGRect bounds([content bounds]); | |
6049 | ||
b97fcfc6 | 6050 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
46aa9775 GP |
6051 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6052 | [content addSubview:content_]; | |
6053 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
6054 | ||
6055 | [content_ setDelegate:self]; | |
b4d89997 | 6056 | } return self; |
b19871dd JF |
6057 | } |
6058 | ||
6d9712c4 | 6059 | - (void) onSwitch:(id)sender { |
a171abd4 | 6060 | NSMutableDictionary *metadata([Sections_ objectForKey:basic_]); |
6d9712c4 JF |
6061 | if (metadata == nil) { |
6062 | metadata = [NSMutableDictionary dictionaryWithCapacity:2]; | |
0010fa77 | 6063 | [Sections_ setObject:metadata forKey:basic_]; |
6d9712c4 JF |
6064 | } |
6065 | ||
46aa9775 | 6066 | [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; |
6d9712c4 JF |
6067 | } |
6068 | ||
6069 | - (void) setSection:(Section *)section editing:(BOOL)editing { | |
6070 | if (editing != editing_) { | |
6071 | if (editing_) | |
6072 | [switch_ removeFromSuperview]; | |
6073 | else | |
6074 | [self addSubview:switch_]; | |
6075 | editing_ = editing; | |
6076 | } | |
6077 | ||
7b33d201 JF |
6078 | basic_ = nil; |
6079 | section_ = nil; | |
6080 | name_ = nil; | |
6081 | count_ = nil; | |
6d9712c4 | 6082 | |
36bb2ca2 | 6083 | if (section == nil) { |
7b33d201 | 6084 | name_ = UCLocalize("ALL_PACKAGES"); |
f641a0e5 | 6085 | count_ = nil; |
36bb2ca2 | 6086 | } else { |
e59669fd | 6087 | basic_ = [section name]; |
677b8415 | 6088 | section_ = [section localized]; |
0010fa77 | 6089 | |
7b33d201 | 6090 | name_ = section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : (NSString *) section_; |
3e5a9f5d | 6091 | count_ = [NSString stringWithFormat:@"%zd", [section count]]; |
6d9712c4 JF |
6092 | |
6093 | if (editing_) | |
46aa9775 | 6094 | [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; |
36bb2ca2 | 6095 | } |
46aa9775 | 6096 | |
c21004b9 | 6097 | [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; |
0abb648c JF |
6098 | [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; |
6099 | ||
46aa9775 | 6100 | [content_ setNeedsDisplay]; |
f641a0e5 JF |
6101 | } |
6102 | ||
77801ff1 JF |
6103 | - (void) setFrame:(CGRect)frame { |
6104 | [super setFrame:frame]; | |
46aa9775 | 6105 | |
77801ff1 | 6106 | CGRect rect([switch_ frame]); |
6415105e | 6107 | [switch_ setFrame:CGRectMake(frame.size.width - rect.size.width - 9, 9, rect.size.width, rect.size.height)]; |
77801ff1 JF |
6108 | } |
6109 | ||
003fc610 GP |
6110 | - (NSString *) accessibilityLabel { |
6111 | return name_; | |
6112 | } | |
6113 | ||
46aa9775 | 6114 | - (void) drawContentRect:(CGRect)rect { |
8cfba088 | 6115 | bool highlighted(highlighted_ && !editing_); |
bc11cf5b | 6116 | |
86a333c6 | 6117 | [icon_ drawInRect:CGRectMake(7, 7, 32, 32)]; |
f641a0e5 | 6118 | |
5d0438dc | 6119 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
f641a0e5 JF |
6120 | UISetColor(White_); |
6121 | ||
46aa9775 | 6122 | float width(rect.size.width); |
58241d4c | 6123 | if (editing_) |
6c1ec3c7 | 6124 | width -= 9 + [switch_ frame].size.width; |
58241d4c | 6125 | |
555aaf71 JF |
6126 | if (!highlighted) |
6127 | UISetColor(Black_); | |
b129e6d9 | 6128 | [name_ drawAtPoint:CGPointMake(48, 12) forWidth:(width - 58) withFont:Font18_ lineBreakMode:NSLineBreakByTruncatingTail]; |
6d9712c4 | 6129 | |
f641a0e5 JF |
6130 | CGSize size = [count_ sizeWithFont:Font14_]; |
6131 | ||
86a333c6 | 6132 | UISetColor(Folder_); |
f641a0e5 | 6133 | if (count_ != nil) |
8323c1b9 | 6134 | [count_ drawAtPoint:CGPointMake(Retina(10 + (30 - size.width) / 2), 18) withFont:Font12Bold_]; |
b19871dd JF |
6135 | } |
6136 | ||
36bb2ca2 JF |
6137 | @end |
6138 | /* }}} */ | |
b19871dd | 6139 | |
ab398adf | 6140 | /* File Table {{{ */ |
cd79e8cf | 6141 | @interface FileTable : CyteViewController < |
c21004b9 JF |
6142 | UITableViewDataSource, |
6143 | UITableViewDelegate | |
6144 | > { | |
36bb2ca2 | 6145 | _transient Database *database_; |
7b33d201 JF |
6146 | _H<Package> package_; |
6147 | _H<NSString> name_; | |
6148 | _H<NSMutableArray> files_; | |
bf7c998c | 6149 | _H<UITableView, 2> list_; |
ab398adf | 6150 | } |
b19871dd | 6151 | |
b5e7eebb | 6152 | - (id) initWithDatabase:(Database *)database; |
ab398adf JF |
6153 | - (void) setPackage:(Package *)package; |
6154 | ||
6155 | @end | |
6156 | ||
6157 | @implementation FileTable | |
6158 | ||
eb30da80 | 6159 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
ab398adf JF |
6160 | return files_ == nil ? 0 : [files_ count]; |
6161 | } | |
6162 | ||
21ea11a4 GP |
6163 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
6164 | return 24.0f; | |
6165 | }*/ | |
ab398adf | 6166 | |
46aa9775 | 6167 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
b5e7eebb GP |
6168 | static NSString *reuseIdentifier = @"Cell"; |
6169 | ||
46aa9775 GP |
6170 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
6171 | if (cell == nil) { | |
6172 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
6173 | [cell setFont:[UIFont systemFontOfSize:16]]; | |
ab398adf | 6174 | } |
46aa9775 | 6175 | [cell setText:[files_ objectAtIndex:indexPath.row]]; |
c21004b9 | 6176 | [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
b5e7eebb | 6177 | |
46aa9775 | 6178 | return cell; |
ab398adf | 6179 | } |
b19871dd | 6180 | |
fe8e721f GP |
6181 | - (NSURL *) navigationURL { |
6182 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/files", [package_ id]]]; | |
6183 | } | |
6184 | ||
6185 | - (void) loadView { | |
e8cbebe4 | 6186 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; |
fe8e721f GP |
6187 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6188 | [list_ setRowHeight:24.0f]; | |
7b33d201 | 6189 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 6190 | [list_ setDelegate:self]; |
e8cbebe4 | 6191 | [self setView:list_]; |
fe8e721f GP |
6192 | } |
6193 | ||
6194 | - (void) viewDidLoad { | |
7d887d0b JF |
6195 | [super viewDidLoad]; |
6196 | ||
fe8e721f GP |
6197 | [[self navigationItem] setTitle:UCLocalize("INSTALLED_FILES")]; |
6198 | } | |
6199 | ||
6200 | - (void) releaseSubviews { | |
fe8e721f | 6201 | list_ = nil; |
7be3eb32 | 6202 | |
4f9acb7c JF |
6203 | package_ = nil; |
6204 | files_ = nil; | |
6205 | ||
7be3eb32 | 6206 | [super releaseSubviews]; |
fe8e721f GP |
6207 | } |
6208 | ||
b5e7eebb GP |
6209 | - (id) initWithDatabase:(Database *)database { |
6210 | if ((self = [super init]) != nil) { | |
ab398adf | 6211 | database_ = database; |
ab398adf JF |
6212 | } return self; |
6213 | } | |
6214 | ||
6215 | - (void) setPackage:(Package *)package { | |
7b33d201 JF |
6216 | package_ = nil; |
6217 | name_ = nil; | |
ab398adf | 6218 | |
4f9acb7c | 6219 | files_ = [NSMutableArray arrayWithCapacity:32]; |
ab398adf JF |
6220 | |
6221 | if (package != nil) { | |
7b33d201 JF |
6222 | package_ = package; |
6223 | name_ = [package id]; | |
ab398adf | 6224 | |
affeffc7 JF |
6225 | if (NSArray *files = [package files]) |
6226 | [files_ addObjectsFromArray:files]; | |
ab398adf | 6227 | |
9ea8d159 JF |
6228 | if ([files_ count] != 0) { |
6229 | if ([[files_ objectAtIndex:0] isEqualToString:@"/."]) | |
6230 | [files_ removeObjectAtIndex:0]; | |
a54b1c10 | 6231 | [files_ sortUsingSelector:@selector(compareByPath:)]; |
2388b078 JF |
6232 | |
6233 | NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8]; | |
6234 | [stack addObject:@"/"]; | |
6235 | ||
6236 | for (int i(0), e([files_ count]); i != e; ++i) { | |
6237 | NSString *file = [files_ objectAtIndex:i]; | |
6238 | while (![file hasPrefix:[stack lastObject]]) | |
6239 | [stack removeLastObject]; | |
6240 | NSString *directory = [stack lastObject]; | |
6241 | [stack addObject:[file stringByAppendingString:@"/"]]; | |
6242 | [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", | |
a54b1c10 | 6243 | ([stack count] - 2) * 3, "", |
2388b078 JF |
6244 | [file substringFromIndex:[directory length]] |
6245 | ]]; | |
6246 | } | |
ab398adf JF |
6247 | } |
6248 | } | |
6249 | ||
6250 | [list_ reloadData]; | |
6251 | } | |
6252 | ||
ab398adf | 6253 | - (void) reloadData { |
fe8e721f GP |
6254 | [super reloadData]; |
6255 | ||
ab398adf | 6256 | [self setPackage:[database_ packageWithName:name_]]; |
ab398adf | 6257 | } |
b4d89997 | 6258 | |
b4d89997 | 6259 | @end |
36bb2ca2 | 6260 | /* }}} */ |
adb61bda | 6261 | /* Package Controller {{{ */ |
a576488f | 6262 | @interface CYPackageController : CydiaWebViewController < |
9daa7f25 DH |
6263 | UIActionSheetDelegate |
6264 | > { | |
770f2a8e | 6265 | _transient Database *database_; |
5d79f7bf JF |
6266 | _H<Package> package_; |
6267 | _H<NSString> name_; | |
3bd1c2a2 | 6268 | bool commercial_; |
3217d35f | 6269 | std::vector<std::pair<_H<NSString>, _H<NSString>>> buttons_; |
9fe109d1 | 6270 | _H<UIActionSheet> sheet_; |
5d79f7bf | 6271 | _H<UIBarButtonItem> button_; |
c9f3aa21 | 6272 | _H<NSArray> versions_; |
b31b87cc JF |
6273 | } |
6274 | ||
f050e4d9 | 6275 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer; |
b4d89997 | 6276 | |
36bb2ca2 | 6277 | @end |
b4d89997 | 6278 | |
f6e13561 | 6279 | @implementation CYPackageController |
b4d89997 | 6280 | |
fe8e721f | 6281 | - (NSURL *) navigationURL { |
5d79f7bf | 6282 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@", (id) name_]]; |
fe8e721f GP |
6283 | } |
6284 | ||
c9f3aa21 JF |
6285 | - (void) _clickButtonWithPackage:(Package *)package { |
6286 | [delegate_ installPackage:package]; | |
6287 | } | |
6288 | ||
5a09ae08 | 6289 | - (void) _clickButtonWithName:(NSString *)name { |
3217d35f | 6290 | if ([name isEqualToString:@"CLEAR"]) |
c9f3aa21 | 6291 | return [delegate_ clearPackage:package_]; |
3217d35f | 6292 | else if ([name isEqualToString:@"REMOVE"]) |
c9f3aa21 JF |
6293 | return [delegate_ removePackage:package_]; |
6294 | else if ([name isEqualToString:@"DOWNGRADE"]) { | |
6295 | sheet_ = [[[UIActionSheet alloc] | |
6296 | initWithTitle:nil | |
6297 | delegate:self | |
6298 | cancelButtonTitle:nil | |
6299 | destructiveButtonTitle:nil | |
6300 | otherButtonTitles:nil | |
6301 | ] autorelease]; | |
6302 | ||
6303 | for (Package *version in (id) versions_) | |
6304 | [sheet_ addButtonWithTitle:[version latest]]; | |
6305 | [sheet_ setContext:@"version"]; | |
6306 | ||
6307 | [delegate_ showActionSheet:sheet_ fromItem:[[self navigationItem] rightBarButtonItem]]; | |
6308 | return; | |
6309 | } | |
6310 | ||
6311 | else if ([name isEqualToString:@"INSTALL"]); | |
6312 | else if ([name isEqualToString:@"REINSTALL"]); | |
6313 | else if ([name isEqualToString:@"UPGRADE"]); | |
5a09ae08 | 6314 | else _assert(false); |
c9f3aa21 JF |
6315 | |
6316 | [delegate_ installPackage:package_]; | |
5a09ae08 JF |
6317 | } |
6318 | ||
674dce72 | 6319 | - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { |
1cedb821 | 6320 | NSString *context([sheet context]); |
9fe109d1 JF |
6321 | if (sheet_ == sheet) |
6322 | sheet_ = nil; | |
5a09ae08 | 6323 | |
1cedb821 | 6324 | if ([context isEqualToString:@"modify"]) { |
674dce72 | 6325 | if (button != [sheet cancelButtonIndex]) { |
60b6595c JF |
6326 | if (IsWildcat_) |
6327 | [self performSelector:@selector(_clickButtonWithName:) withObject:buttons_[button].first afterDelay:0]; | |
6328 | else | |
6329 | [self _clickButtonWithName:buttons_[button].first]; | |
674dce72 | 6330 | } |
bc11cf5b | 6331 | |
c9f3aa21 JF |
6332 | [sheet dismissWithClickedButtonIndex:button animated:YES]; |
6333 | } else if ([context isEqualToString:@"version"]) { | |
6334 | if (button != [sheet cancelButtonIndex]) { | |
6335 | Package *version([versions_ objectAtIndex:button]); | |
6336 | if (IsWildcat_) | |
6337 | [self performSelector:@selector(_clickButtonWithPackage:) withObject:version afterDelay:0]; | |
6338 | else | |
6339 | [self _clickButtonWithPackage:version]; | |
6340 | } | |
6341 | ||
60b6595c | 6342 | [sheet dismissWithClickedButtonIndex:button animated:YES]; |
674dce72 | 6343 | } |
b4d89997 | 6344 | } |
2367a917 | 6345 | |
3e9c9e85 | 6346 | - (bool) _allowJavaScriptPanel { |
3bd1c2a2 | 6347 | return commercial_; |
3e9c9e85 JF |
6348 | } |
6349 | ||
9487f027 | 6350 | #if !AlwaysReload |
9daa7f25 | 6351 | - (void) _customButtonClicked { |
3217d35f | 6352 | size_t count(buttons_.size()); |
670a0494 JF |
6353 | if (count == 0) |
6354 | return; | |
2367a917 | 6355 | |
5a09ae08 | 6356 | if (count == 1) |
3217d35f | 6357 | [self _clickButtonWithName:buttons_[0].first]; |
5a09ae08 | 6358 | else { |
674dce72 | 6359 | NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; |
3217d35f JF |
6360 | for (const auto &button : buttons_) |
6361 | [buttons addObject:button.second]; | |
36bb2ca2 | 6362 | |
9fe109d1 | 6363 | sheet_ = [[[UIActionSheet alloc] |
9ea8d159 | 6364 | initWithTitle:nil |
36bb2ca2 | 6365 | delegate:self |
674dce72 GP |
6366 | cancelButtonTitle:nil |
6367 | destructiveButtonTitle:nil | |
6368 | otherButtonTitles:nil | |
6369 | ] autorelease]; | |
6370 | ||
c9f3aa21 JF |
6371 | for (NSString *button in buttons) |
6372 | [sheet_ addButtonWithTitle:button]; | |
9fe109d1 | 6373 | [sheet_ setContext:@"modify"]; |
bc11cf5b | 6374 | |
9fe109d1 | 6375 | [delegate_ showActionSheet:sheet_ fromItem:[[self navigationItem] rightBarButtonItem]]; |
36bb2ca2 | 6376 | } |
b4d89997 | 6377 | } |
12b59862 | 6378 | |
2e6c1426 | 6379 | - (void) reloadButtonClicked { |
07d0e88e JF |
6380 | if (commercial_ && function_ == nil && [package_ uninstalled]) |
6381 | return; | |
6382 | [self customButtonClicked]; | |
2fad210a GP |
6383 | } |
6384 | ||
6385 | - (void) applyLoadingTitle { | |
6386 | // Don't show "Loading" as the title. Ever. | |
2e6c1426 | 6387 | } |
a5938ea5 DH |
6388 | |
6389 | - (UIBarButtonItem *) rightButton { | |
2634b249 | 6390 | return button_; |
a5938ea5 | 6391 | } |
9487f027 | 6392 | #endif |
2367a917 | 6393 | |
77259cab JF |
6394 | - (void) setPageColor:(UIColor *)color { |
6395 | return [super setPageColor:nil]; | |
6396 | } | |
6397 | ||
f050e4d9 | 6398 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer { |
b5e7eebb | 6399 | if ((self = [super init]) != nil) { |
36bb2ca2 | 6400 | database_ = database; |
5612913e | 6401 | name_ = name == nil ? @"" : [NSString stringWithString:name]; |
f050e4d9 | 6402 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]] withReferrer:referrer]; |
36bb2ca2 | 6403 | } return self; |
dc5812ec JF |
6404 | } |
6405 | ||
57e8b225 | 6406 | - (void) reloadData { |
9dac415b JF |
6407 | [super reloadData]; |
6408 | ||
9fe109d1 JF |
6409 | [sheet_ dismissWithClickedButtonIndex:[sheet_ cancelButtonIndex] animated:YES]; |
6410 | sheet_ = nil; | |
6411 | ||
57e8b225 | 6412 | package_ = [database_ packageWithName:name_]; |
c9f3aa21 | 6413 | versions_ = [package_ downgrades]; |
36bb2ca2 | 6414 | |
3217d35f | 6415 | buttons_.clear(); |
5a09ae08 | 6416 | |
57e8b225 | 6417 | if (package_ != nil) { |
5d79f7bf | 6418 | [(Package *) package_ parse]; |
68d927e2 | 6419 | |
57e8b225 | 6420 | commercial_ = [package_ isCommercial]; |
36bb2ca2 | 6421 | |
dc63e78f | 6422 | if ([package_ mode] != nil) |
3217d35f | 6423 | buttons_.push_back(std::make_pair(@"CLEAR", UCLocalize("CLEAR"))); |
5a09ae08 | 6424 | if ([package_ source] == nil); |
31f3cfff | 6425 | else if ([package_ upgradableAndEssential:NO]) |
3217d35f | 6426 | buttons_.push_back(std::make_pair(@"UPGRADE", UCLocalize("UPGRADE"))); |
6a155117 | 6427 | else if ([package_ uninstalled]) |
3217d35f | 6428 | buttons_.push_back(std::make_pair(@"INSTALL", UCLocalize("INSTALL"))); |
5a09ae08 | 6429 | else |
3217d35f | 6430 | buttons_.push_back(std::make_pair(@"REINSTALL", UCLocalize("REINSTALL"))); |
6a155117 | 6431 | if (![package_ uninstalled]) |
3217d35f | 6432 | buttons_.push_back(std::make_pair(@"REMOVE", UCLocalize("REMOVE"))); |
c9f3aa21 JF |
6433 | if ([versions_ count] != 0) |
6434 | buttons_.push_back(std::make_pair(@"DOWNGRADE", UCLocalize("DOWNGRADE"))); | |
2634b249 | 6435 | } |
f79a4512 | 6436 | |
2634b249 | 6437 | NSString *title; |
3217d35f | 6438 | switch (buttons_.size()) { |
2634b249 | 6439 | case 0: title = nil; break; |
3217d35f | 6440 | case 1: title = buttons_[0].second; break; |
2634b249 | 6441 | default: title = UCLocalize("MODIFY"); break; |
36bb2ca2 | 6442 | } |
2634b249 | 6443 | |
5d79f7bf | 6444 | button_ = [[[UIBarButtonItem alloc] |
2634b249 JF |
6445 | initWithTitle:title |
6446 | style:UIBarButtonItemStylePlain | |
6447 | target:self | |
6448 | action:@selector(customButtonClicked) | |
5d79f7bf | 6449 | ] autorelease]; |
b5e7eebb | 6450 | } |
f79a4512 | 6451 | |
3bd1c2a2 JF |
6452 | - (bool) isLoading { |
6453 | return commercial_ ? [super isLoading] : false; | |
9fe5e5f8 JF |
6454 | } |
6455 | ||
b4d89997 JF |
6456 | @end |
6457 | /* }}} */ | |
5829aea2 | 6458 | |
f50860ee | 6459 | /* Package List Controller {{{ */ |
cd79e8cf | 6460 | @interface PackageListController : CyteViewController < |
c21004b9 JF |
6461 | UITableViewDataSource, |
6462 | UITableViewDelegate | |
6463 | > { | |
36bb2ca2 | 6464 | _transient Database *database_; |
0175295c | 6465 | unsigned era_; |
56bf1e78 | 6466 | _H<NSArray> packages_; |
e5491e28 | 6467 | _H<NSArray> sections_; |
bf7c998c | 6468 | _H<UITableView, 2> list_; |
aeeb755b JF |
6469 | |
6470 | _H<NSArray> thumbs_; | |
6471 | std::vector<NSInteger> offset_; | |
6472 | ||
7b33d201 | 6473 | _H<NSString> title_; |
56bf1e78 | 6474 | unsigned reloading_; |
dc5812ec JF |
6475 | } |
6476 | ||
f50860ee | 6477 | - (id) initWithDatabase:(Database *)database title:(NSString *)title; |
b4d89997 | 6478 | - (void) setDelegate:(id)delegate; |
a0be02eb | 6479 | - (void) resetCursor; |
59f3d290 | 6480 | - (void) clearData; |
b4d89997 | 6481 | |
e5491e28 JF |
6482 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages; |
6483 | ||
b4d89997 JF |
6484 | @end |
6485 | ||
f50860ee | 6486 | @implementation PackageListController |
b4d89997 | 6487 | |
f050e4d9 JF |
6488 | - (NSURL *) referrerURL { |
6489 | return [self navigationURL]; | |
6490 | } | |
6491 | ||
59f3d290 JF |
6492 | - (bool) isSummarized { |
6493 | return false; | |
6494 | } | |
6495 | ||
9c5737d5 JF |
6496 | - (bool) showsSections { |
6497 | return true; | |
6498 | } | |
6499 | ||
f50860ee GP |
6500 | - (void) deselectWithAnimation:(BOOL)animated { |
6501 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
6502 | } | |
6503 | ||
bfb45dcb GP |
6504 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve { |
6505 | CGRect base = [[self view] bounds]; | |
6506 | base.size.height -= bounds.size.height; | |
6507 | base.origin = [list_ frame].origin; | |
6508 | ||
6509 | [UIView beginAnimations:nil context:NULL]; | |
6510 | [UIView setAnimationBeginsFromCurrentState:YES]; | |
6511 | [UIView setAnimationCurve:curve]; | |
6512 | [UIView setAnimationDuration:duration]; | |
6513 | [list_ setFrame:base]; | |
6514 | [UIView commitAnimations]; | |
6515 | } | |
6516 | ||
6517 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration { | |
6518 | [self resizeForKeyboardBounds:bounds duration:duration curve:UIViewAnimationCurveLinear]; | |
6519 | } | |
6520 | ||
6521 | - (void) resizeForKeyboardBounds:(CGRect)bounds { | |
6522 | [self resizeForKeyboardBounds:bounds duration:0]; | |
6523 | } | |
6524 | ||
655c7ded JF |
6525 | - (void) getKeyboardCurve:(UIViewAnimationCurve *)curve duration:(NSTimeInterval *)duration forNotification:(NSNotification *)notification { |
6526 | if (&UIKeyboardAnimationCurveUserInfoKey == NULL) | |
6527 | *curve = UIViewAnimationCurveEaseInOut; | |
6528 | else | |
6529 | [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:curve]; | |
6530 | ||
6531 | if (&UIKeyboardAnimationDurationUserInfoKey == NULL) | |
6532 | *duration = 0.3; | |
6533 | else | |
6534 | [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:duration]; | |
6535 | } | |
6536 | ||
bfb45dcb GP |
6537 | - (void) keyboardWillShow:(NSNotification *)notification { |
6538 | CGRect bounds; | |
6539 | CGPoint center; | |
bfb45dcb GP |
6540 | [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds]; |
6541 | [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:¢er]; | |
655c7ded JF |
6542 | |
6543 | NSTimeInterval duration; | |
6544 | UIViewAnimationCurve curve; | |
6545 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; | |
bfb45dcb | 6546 | |
8323c1b9 | 6547 | CGRect kbframe = CGRectMake(Retina(center.x - bounds.size.width / 2), Retina(center.y - bounds.size.height / 2), bounds.size.width, bounds.size.height); |
38991110 | 6548 | UIViewController *base = self; |
19f2d77f JF |
6549 | while ([base parentOrPresentingViewController] != nil) |
6550 | base = [base parentOrPresentingViewController]; | |
38991110 | 6551 | CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]]; |
bfb45dcb GP |
6552 | CGRect intersection = CGRectIntersection(viewframe, kbframe); |
6553 | ||
2e35f65f | 6554 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) |
0b7516cf | 6555 | intersection.size.height += CYStatusBarHeight(); |
2e35f65f | 6556 | |
bfb45dcb GP |
6557 | [self resizeForKeyboardBounds:intersection duration:duration curve:curve]; |
6558 | } | |
6559 | ||
6560 | - (void) keyboardWillHide:(NSNotification *)notification { | |
6561 | NSTimeInterval duration; | |
6562 | UIViewAnimationCurve curve; | |
655c7ded | 6563 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; |
bfb45dcb GP |
6564 | |
6565 | [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve]; | |
6566 | } | |
6567 | ||
6568 | - (void) viewWillAppear:(BOOL)animated { | |
6569 | [super viewWillAppear:animated]; | |
6570 | ||
6571 | [self resizeForKeyboardBounds:CGRectZero]; | |
6572 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
6573 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
6574 | } | |
6575 | ||
6576 | - (void) viewWillDisappear:(BOOL)animated { | |
6577 | [super viewWillDisappear:animated]; | |
6578 | ||
6579 | [self resizeForKeyboardBounds:CGRectZero]; | |
6580 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
6581 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
6582 | } | |
6583 | ||
f50860ee GP |
6584 | - (void) viewDidAppear:(BOOL)animated { |
6585 | [super viewDidAppear:animated]; | |
6586 | [self deselectWithAnimation:animated]; | |
6587 | } | |
6588 | ||
6589 | - (void) didSelectPackage:(Package *)package { | |
f050e4d9 | 6590 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id] withReferrer:[[self referrerURL] absoluteString]] autorelease]); |
f50860ee GP |
6591 | [view setDelegate:delegate_]; |
6592 | [[self navigationController] pushViewController:view animated:YES]; | |
6593 | } | |
6594 | ||
327624b6 JF |
6595 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
6596 | NSInteger count([sections_ count]); | |
6597 | return count == 0 ? 1 : count; | |
b4d89997 | 6598 | } |
2367a917 | 6599 | |
327624b6 | 6600 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
a891c345 | 6601 | if ([sections_ count] == 0 || [[sections_ objectAtIndex:section] count] == 0) |
327624b6 | 6602 | return nil; |
b4d89997 JF |
6603 | return [[sections_ objectAtIndex:section] name]; |
6604 | } | |
dc5812ec | 6605 | |
327624b6 JF |
6606 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
6607 | if ([sections_ count] == 0) | |
6608 | return 0; | |
6609 | return [[sections_ objectAtIndex:section] count]; | |
b4d89997 | 6610 | } |
dc5812ec | 6611 | |
327624b6 | 6612 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
0175295c JF |
6613 | @synchronized (database_) { |
6614 | if ([database_ era] != era_) | |
6615 | return nil; | |
6616 | ||
327624b6 JF |
6617 | Section *section([sections_ objectAtIndex:[path section]]); |
6618 | NSInteger row([path row]); | |
6619 | Package *package([packages_ objectAtIndex:([section row] + row)]); | |
0175295c JF |
6620 | return [[package retain] autorelease]; |
6621 | } } | |
dc5812ec | 6622 | |
327624b6 | 6623 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
c21004b9 | 6624 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
327624b6 JF |
6625 | if (cell == nil) |
6626 | cell = [[[PackageCell alloc] init] autorelease]; | |
60bef540 JF |
6627 | |
6628 | Package *package([database_ packageWithName:[[self packageAtIndexPath:path] id]]); | |
6629 | [cell setPackage:package asSummary:[self isSummarized]]; | |
327624b6 | 6630 | return cell; |
b4d89997 | 6631 | } |
dc5812ec | 6632 | |
f50860ee | 6633 | - (void) tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path { |
327624b6 | 6634 | Package *package([self packageAtIndexPath:path]); |
59c6ae22 | 6635 | package = [database_ packageWithName:[package id]]; |
f50860ee | 6636 | [self didSelectPackage:package]; |
327624b6 JF |
6637 | } |
6638 | ||
6639 | - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { | |
aeeb755b | 6640 | return thumbs_; |
327624b6 JF |
6641 | } |
6642 | ||
a891c345 | 6643 | - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { |
aeeb755b | 6644 | return offset_[index]; |
dc5812ec JF |
6645 | } |
6646 | ||
59f3d290 JF |
6647 | - (void) updateHeight { |
6648 | [list_ setRowHeight:([self isSummarized] ? 38 : 73)]; | |
6649 | } | |
6650 | ||
f50860ee GP |
6651 | - (id) initWithDatabase:(Database *)database title:(NSString *)title { |
6652 | if ((self = [super init]) != nil) { | |
36bb2ca2 | 6653 | database_ = database; |
f50860ee GP |
6654 | title_ = [title copy]; |
6655 | [[self navigationItem] setTitle:title_]; | |
61cc4dbc JF |
6656 | } return self; |
6657 | } | |
dc5812ec | 6658 | |
61cc4dbc | 6659 | - (void) loadView { |
b62b3788 JF |
6660 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
6661 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
6662 | [self setView:view]; | |
6663 | ||
6664 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; | |
61cc4dbc | 6665 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
b62b3788 | 6666 | [view addSubview:list_]; |
1527b095 | 6667 | |
61cc4dbc JF |
6668 | // XXX: is 20 the most optimal number here? |
6669 | [list_ setSectionIndexMinimumDisplayRowCount:20]; | |
59f3d290 | 6670 | |
61cc4dbc JF |
6671 | [(UITableView *) list_ setDataSource:self]; |
6672 | [list_ setDelegate:self]; | |
6673 | ||
6674 | [self updateHeight]; | |
6675 | } | |
6676 | ||
6677 | - (void) releaseSubviews { | |
6678 | list_ = nil; | |
7be3eb32 | 6679 | |
4f9acb7c JF |
6680 | packages_ = nil; |
6681 | sections_ = nil; | |
aeeb755b JF |
6682 | |
6683 | thumbs_ = nil; | |
6684 | offset_.clear(); | |
4f9acb7c | 6685 | |
7be3eb32 | 6686 | [super releaseSubviews]; |
dc5812ec JF |
6687 | } |
6688 | ||
6689 | - (void) setDelegate:(id)delegate { | |
2367a917 | 6690 | delegate_ = delegate; |
b4d89997 JF |
6691 | } |
6692 | ||
3025d5b4 JF |
6693 | - (bool) shouldYield { |
6694 | return false; | |
6695 | } | |
b4d89997 | 6696 | |
56bf1e78 JF |
6697 | - (bool) shouldBlock { |
6698 | return false; | |
6699 | } | |
6700 | ||
9c5737d5 | 6701 | - (NSMutableArray *) _reloadPackages { |
695fdd5c | 6702 | @synchronized (database_) { |
c28bc6f1 JF |
6703 | era_ = [database_ era]; |
6704 | NSArray *packages([database_ packages]); | |
6705 | ||
9c5737d5 | 6706 | return [NSMutableArray arrayWithArray:packages]; |
695fdd5c | 6707 | } } |
3025d5b4 JF |
6708 | |
6709 | - (void) _reloadData { | |
56bf1e78 JF |
6710 | if (reloading_ != 0) { |
6711 | reloading_ = 2; | |
6712 | return; | |
6713 | } | |
6714 | ||
e5491e28 | 6715 | NSMutableArray *packages; |
ae60e2c1 | 6716 | |
0c8f53c0 | 6717 | reload: |
3025d5b4 | 6718 | if ([self shouldYield]) { |
bec28dda JF |
6719 | do { |
6720 | UIProgressHUD *hud; | |
56bf1e78 | 6721 | |
bec28dda JF |
6722 | if (![self shouldBlock]) |
6723 | hud = nil; | |
6724 | else { | |
6725 | hud = [delegate_ addProgressHUD]; | |
6726 | [hud setText:UCLocalize("LOADING")]; | |
6727 | } | |
56bf1e78 | 6728 | |
56bf1e78 | 6729 | reloading_ = 1; |
ae60e2c1 | 6730 | packages = [self yieldToSelector:@selector(_reloadPackages)]; |
bec28dda JF |
6731 | |
6732 | if (hud != nil) | |
6733 | [delegate_ removeProgressHUD:hud]; | |
56bf1e78 | 6734 | } while (reloading_ == 2); |
3025d5b4 | 6735 | } else { |
ae60e2c1 | 6736 | packages = [self _reloadPackages]; |
3025d5b4 | 6737 | } |
36bb2ca2 | 6738 | |
0c8f53c0 JF |
6739 | @synchronized (database_) { |
6740 | if (era_ != [database_ era]) | |
6741 | goto reload; | |
6742 | reloading_ = 0; | |
6743 | ||
aeeb755b JF |
6744 | thumbs_ = nil; |
6745 | offset_.clear(); | |
6746 | ||
ae60e2c1 | 6747 | packages_ = packages; |
aeeb755b JF |
6748 | |
6749 | if ([self showsSections]) | |
6750 | sections_ = [self sectionsForPackages:packages]; | |
6751 | else { | |
6752 | Section *section([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); | |
6753 | [section setCount:[packages_ count]]; | |
6754 | sections_ = [NSArray arrayWithObject:section]; | |
6755 | } | |
ae60e2c1 | 6756 | |
e5491e28 JF |
6757 | [self updateHeight]; |
6758 | ||
6759 | _profile(PackageTable$reloadData$List) | |
6760 | [(UITableView *) list_ setDataSource:self]; | |
6761 | [list_ reloadData]; | |
6762 | _end | |
1a83afc6 JF |
6763 | } |
6764 | ||
6765 | PrintTimes(); | |
6766 | } | |
e5491e28 JF |
6767 | |
6768 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { | |
aeeb755b JF |
6769 | Section *prefix([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); |
6770 | size_t end([packages count]); | |
b4d89997 | 6771 | |
aeeb755b JF |
6772 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); |
6773 | Section *section(prefix); | |
c4dcf2c2 | 6774 | |
aeeb755b JF |
6775 | thumbs_ = CollationThumbs_; |
6776 | offset_ = CollationOffset_; | |
b4d89997 | 6777 | |
aeeb755b JF |
6778 | size_t offset(0); |
6779 | size_t offsets([CollationStarts_ count]); | |
b4d89997 | 6780 | |
aeeb755b JF |
6781 | NSString *start([CollationStarts_ objectAtIndex:offset]); |
6782 | size_t length([start length]); | |
327624b6 | 6783 | |
aeeb755b JF |
6784 | for (size_t index(0); index != end; ++index) { |
6785 | if (start != nil) { | |
6786 | Package *package([packages objectAtIndex:index]); | |
22fd24dd | 6787 | NSString *name(PackageName(package, @selector(cyname))); |
a891c345 | 6788 | |
aeeb755b | 6789 | //while ([start compare:name options:NSNumericSearch range:NSMakeRange(0, length) locale:CollationLocale_] != NSOrderedDescending) { |
22fd24dd | 6790 | while (StringNameCompare(start, name, length) != kCFCompareGreaterThan) { |
aeeb755b JF |
6791 | NSString *title([CollationTitles_ objectAtIndex:offset]); |
6792 | section = [[[Section alloc] initWithName:title row:index localize:NO] autorelease]; | |
6793 | [sections addObject:section]; | |
a891c345 | 6794 | |
aeeb755b JF |
6795 | start = ++offset == offsets ? nil : [CollationStarts_ objectAtIndex:offset]; |
6796 | if (start == nil) | |
6797 | break; | |
6798 | length = [start length]; | |
808c6eb6 | 6799 | } |
59f3d290 JF |
6800 | } |
6801 | ||
aeeb755b JF |
6802 | [section addToCount]; |
6803 | } | |
a891c345 | 6804 | |
aeeb755b JF |
6805 | for (; offset != offsets; ++offset) { |
6806 | NSString *title([CollationTitles_ objectAtIndex:offset]); | |
6807 | Section *section([[[Section alloc] initWithName:title row:end localize:NO] autorelease]); | |
6808 | [sections addObject:section]; | |
6809 | } | |
a891c345 | 6810 | |
aeeb755b JF |
6811 | if ([prefix count] != 0) { |
6812 | Section *suffix([sections lastObject]); | |
6813 | [prefix setName:[suffix name]]; | |
6814 | [suffix setName:nil]; | |
6815 | [sections insertObject:prefix atIndex:(offsets - 1)]; | |
a891c345 | 6816 | } |
b4d89997 | 6817 | |
e5491e28 JF |
6818 | return sections; |
6819 | } | |
b4d89997 | 6820 | |
3025d5b4 JF |
6821 | - (void) reloadData { |
6822 | [super reloadData]; | |
6d246265 JF |
6823 | |
6824 | if ([self shouldYield]) | |
6825 | [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; | |
6826 | else | |
6827 | [self _reloadData]; | |
3025d5b4 JF |
6828 | } |
6829 | ||
a0be02eb | 6830 | - (void) resetCursor { |
4c6a29cd | 6831 | [list_ scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; |
a0be02eb JF |
6832 | } |
6833 | ||
59f3d290 JF |
6834 | - (void) clearData { |
6835 | [self updateHeight]; | |
6836 | ||
6837 | [list_ setDataSource:nil]; | |
6838 | [list_ reloadData]; | |
6839 | ||
6840 | [self resetCursor]; | |
6841 | } | |
6842 | ||
a3328c28 JF |
6843 | @end |
6844 | /* }}} */ | |
f50860ee | 6845 | /* Filtered Package List Controller {{{ */ |
89bdef78 JF |
6846 | typedef Function<bool, Package *> PackageFilter; |
6847 | typedef Function<void, NSMutableArray *> PackageSorter; | |
f50860ee | 6848 | @interface FilteredPackageListController : PackageListController { |
89bdef78 JF |
6849 | PackageFilter filter_; |
6850 | PackageSorter sorter_; | |
a3328c28 JF |
6851 | } |
6852 | ||
89bdef78 | 6853 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter; |
3025d5b4 | 6854 | |
89bdef78 JF |
6855 | - (void) setFilter:(PackageFilter)filter; |
6856 | - (void) setSorter:(PackageSorter)sorter; | |
a3328c28 JF |
6857 | |
6858 | @end | |
6859 | ||
f50860ee | 6860 | @implementation FilteredPackageListController |
a3328c28 | 6861 | |
89bdef78 | 6862 | - (void) setFilter:(PackageFilter)filter { |
aa32d91b | 6863 | @synchronized (self) { |
01d93940 | 6864 | filter_ = filter; |
55066b9e JF |
6865 | } } |
6866 | ||
89bdef78 | 6867 | - (void) setSorter:(PackageSorter)sorter { |
55066b9e | 6868 | @synchronized (self) { |
89bdef78 | 6869 | sorter_ = sorter; |
d84597fe | 6870 | } } |
01d93940 | 6871 | |
9c5737d5 | 6872 | - (NSMutableArray *) _reloadPackages { |
aa32d91b | 6873 | @synchronized (database_) { |
c28bc6f1 | 6874 | era_ = [database_ era]; |
c28bc6f1 | 6875 | |
dd4e70dc JF |
6876 | NSArray *packages([database_ packages]); |
6877 | NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); | |
aa32d91b | 6878 | |
89bdef78 | 6879 | PackageFilter filter; |
dd4e70dc | 6880 | PackageSorter sorter; |
aa32d91b JF |
6881 | |
6882 | @synchronized (self) { | |
aa32d91b | 6883 | filter = filter_; |
89bdef78 | 6884 | sorter = sorter_; |
aa32d91b JF |
6885 | } |
6886 | ||
6887 | _profile(PackageTable$reloadData$Filter) | |
6888 | for (Package *package in packages) | |
826318ca | 6889 | if (filter(package)) |
aa32d91b | 6890 | [filtered addObject:package]; |
76933519 | 6891 | _end |
aa32d91b | 6892 | |
89bdef78 JF |
6893 | if (sorter) |
6894 | sorter(filtered); | |
aa32d91b | 6895 | return filtered; |
dd4e70dc | 6896 | } } |
55066b9e | 6897 | |
89bdef78 | 6898 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter { |
55066b9e JF |
6899 | if ((self = [super initWithDatabase:database title:title]) != nil) { |
6900 | [self setFilter:filter]; | |
a3328c28 JF |
6901 | } return self; |
6902 | } | |
6903 | ||
dc5812ec | 6904 | @end |
b5e7eebb GP |
6905 | /* }}} */ |
6906 | ||
5829aea2 | 6907 | /* Home Controller {{{ */ |
a576488f | 6908 | @interface HomeController : CydiaWebViewController { |
02f21c73 JF |
6909 | CFRunLoopRef runloop_; |
6910 | SCNetworkReachabilityRef reachability_; | |
b4d89997 | 6911 | } |
6840bff3 | 6912 | |
7b0ce2da | 6913 | @end |
b4d89997 | 6914 | |
5829aea2 | 6915 | @implementation HomeController |
46aa9775 | 6916 | |
02f21c73 JF |
6917 | static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachability, SCNetworkReachabilityFlags flags, void *info) { |
6918 | [(HomeController *) info dispatchEvent:@"CydiaReachabilityCallback"]; | |
6919 | } | |
6920 | ||
3c62d654 JF |
6921 | - (id) init { |
6922 | if ((self = [super init]) != nil) { | |
90351d93 | 6923 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/home/", UI_]]]; |
ed7bfd8c | 6924 | [self reloadData]; |
02f21c73 JF |
6925 | |
6926 | reachability_ = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "cydia.saurik.com"); | |
6927 | if (reachability_ != NULL) { | |
6928 | SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL}; | |
6929 | SCNetworkReachabilitySetCallback(reachability_, HomeControllerReachabilityCallback, &context); | |
6930 | ||
6931 | CFRunLoopRef runloop(CFRunLoopGetCurrent()); | |
6932 | if (SCNetworkReachabilityScheduleWithRunLoop(reachability_, runloop, kCFRunLoopDefaultMode)) | |
6933 | runloop_ = runloop; | |
6934 | } | |
3c62d654 JF |
6935 | } return self; |
6936 | } | |
6937 | ||
02f21c73 JF |
6938 | - (void) dealloc { |
6939 | if (reachability_ != NULL && runloop_ != NULL) | |
6940 | SCNetworkReachabilityUnscheduleFromRunLoop(reachability_, runloop_, kCFRunLoopDefaultMode); | |
6941 | [super dealloc]; | |
6942 | } | |
6943 | ||
fe8e721f GP |
6944 | - (NSURL *) navigationURL { |
6945 | return [NSURL URLWithString:@"cydia://home"]; | |
6946 | } | |
6947 | ||
5829aea2 GP |
6948 | - (void) aboutButtonClicked { |
6949 | UIAlertView *alert([[[UIAlertView alloc] init] autorelease]); | |
b4d89997 | 6950 | |
5829aea2 GP |
6951 | [alert setTitle:UCLocalize("ABOUT_CYDIA")]; |
6952 | [alert addButtonWithTitle:UCLocalize("CLOSE")]; | |
6953 | [alert setCancelButtonIndex:0]; | |
46aa9775 | 6954 | |
5829aea2 | 6955 | [alert setMessage: |
4c66fad9 | 6956 | @"Copyright \u00a9 2008-2015\n" |
7e865c1e JF |
6957 | "SaurikIT, LLC\n" |
6958 | "\n" | |
5829aea2 GP |
6959 | "Jay Freeman (saurik)\n" |
6960 | "saurik@saurik.com\n" | |
6961 | "http://www.saurik.com/" | |
6962 | ]; | |
46aa9775 | 6963 | |
5829aea2 | 6964 | [alert show]; |
a0376fc1 JF |
6965 | } |
6966 | ||
e6124cb6 JF |
6967 | - (UIBarButtonItem *) leftButton { |
6968 | return [[[UIBarButtonItem alloc] | |
35f0a3b5 GP |
6969 | initWithTitle:UCLocalize("ABOUT") |
6970 | style:UIBarButtonItemStylePlain | |
6971 | target:self | |
6972 | action:@selector(aboutButtonClicked) | |
e6124cb6 | 6973 | ] autorelease]; |
7b0ce2da JF |
6974 | } |
6975 | ||
5829aea2 GP |
6976 | @end |
6977 | /* }}} */ | |
7b0ce2da | 6978 | |
28e596e4 | 6979 | /* Cydia Navigation Controller Interface {{{ */ |
15f0d613 | 6980 | @interface UINavigationController (Cydia) |
28e596e4 JF |
6981 | |
6982 | - (NSArray *) navigationURLCollection; | |
15f0d613 | 6983 | - (void) unloadData; |
28e596e4 JF |
6984 | |
6985 | @end | |
6986 | /* }}} */ | |
7b0ce2da | 6987 | |
5829aea2 | 6988 | /* Cydia Tab Bar Controller {{{ */ |
5fe2bcc6 | 6989 | @interface CydiaTabBarController : CyteTabBarController < |
9f99f3da | 6990 | UITabBarControllerDelegate, |
21ac0ce2 | 6991 | FetchDelegate |
5829aea2 GP |
6992 | > { |
6993 | _transient Database *database_; | |
7b0ce2da | 6994 | |
e67ebdad JF |
6995 | _H<UIActivityIndicatorView> indicator_; |
6996 | ||
5829aea2 GP |
6997 | bool updating_; |
6998 | // XXX: ok, "updatedelegate_"?... | |
6999 | _transient NSObject<CydiaDelegate> *updatedelegate_; | |
7b0ce2da JF |
7000 | } |
7001 | ||
35f0a3b5 | 7002 | - (NSArray *) navigationURLCollection; |
5829aea2 | 7003 | - (void) beginUpdate; |
5829aea2 | 7004 | - (BOOL) updating; |
1cedb821 | 7005 | |
5829aea2 | 7006 | @end |
2fc76a2d | 7007 | |
5fe2bcc6 | 7008 | @implementation CydiaTabBarController |
9f99f3da | 7009 | |
35f0a3b5 | 7010 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
7011 | NSMutableArray *items([NSMutableArray array]); |
7012 | ||
35f0a3b5 | 7013 | // XXX: Should this deal with transient view controllers? |
fe8e721f | 7014 | for (id navigation in [self viewControllers]) { |
35f0a3b5 | 7015 | NSArray *stack = [navigation performSelector:@selector(navigationURLCollection)]; |
fe8e721f GP |
7016 | if (stack != nil) |
7017 | [items addObject:stack]; | |
2fc76a2d | 7018 | } |
c713af59 | 7019 | |
fe8e721f GP |
7020 | return items; |
7021 | } | |
7022 | ||
b5e7eebb GP |
7023 | - (id) initWithDatabase:(Database *)database { |
7024 | if ((self = [super init]) != nil) { | |
7b0ce2da | 7025 | database_ = database; |
9f99f3da | 7026 | [self setDelegate:self]; |
04fe1349 | 7027 | |
e67ebdad JF |
7028 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteTiny] autorelease]; |
7029 | [indicator_ setOrigin:CGPointMake(kCFCoreFoundationVersionNumber >= 800 ? 2 : 4, 2)]; | |
7b0ce2da | 7030 | |
e67ebdad | 7031 | [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b0ce2da JF |
7032 | } return self; |
7033 | } | |
7034 | ||
5829aea2 | 7035 | - (void) beginUpdate { |
20d59843 JF |
7036 | if (updating_) |
7037 | return; | |
7038 | ||
e67ebdad JF |
7039 | UIViewController *controller([[self viewControllers] objectAtIndex:1]); |
7040 | UITabBarItem *item([controller tabBarItem]); | |
7041 | ||
7042 | [item setBadgeValue:@""]; | |
7043 | UIView *badge(MSHookIvar<UIView *>([item view], "_badge")); | |
7044 | ||
7045 | [indicator_ startAnimating]; | |
7046 | [badge addSubview:indicator_]; | |
7b0ce2da | 7047 | |
5829aea2 GP |
7048 | [updatedelegate_ retainNetworkActivityIndicator]; |
7049 | updating_ = true; | |
7b0ce2da | 7050 | |
5829aea2 GP |
7051 | [NSThread |
7052 | detachNewThreadSelector:@selector(performUpdate) | |
7053 | toTarget:self | |
7054 | withObject:nil | |
7055 | ]; | |
7b0ce2da JF |
7056 | } |
7057 | ||
d13edf44 JF |
7058 | - (void) performUpdate { |
7059 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
7060 | ||
21ac0ce2 | 7061 | SourceStatus status(self, database_); |
5829aea2 GP |
7062 | [database_ updateWithStatus:status]; |
7063 | ||
7064 | [self | |
7065 | performSelectorOnMainThread:@selector(completeUpdate) | |
7066 | withObject:nil | |
7067 | waitUntilDone:NO | |
7068 | ]; | |
d13edf44 JF |
7069 | |
7070 | [pool release]; | |
f6e13561 GP |
7071 | } |
7072 | ||
5829aea2 GP |
7073 | - (void) stopUpdateWithSelector:(SEL)selector { |
7074 | updating_ = false; | |
7075 | [updatedelegate_ releaseNetworkActivityIndicator]; | |
b5e7eebb | 7076 | |
e67ebdad JF |
7077 | UIViewController *controller([[self viewControllers] objectAtIndex:1]); |
7078 | [[controller tabBarItem] setBadgeValue:nil]; | |
7079 | ||
7080 | [indicator_ removeFromSuperview]; | |
7081 | [indicator_ stopAnimating]; | |
bc11cf5b | 7082 | |
5829aea2 | 7083 | [updatedelegate_ performSelector:selector withObject:nil afterDelay:0]; |
2bdd73bd GP |
7084 | } |
7085 | ||
5829aea2 GP |
7086 | - (void) completeUpdate { |
7087 | if (!updating_) | |
7088 | return; | |
7089 | [self stopUpdateWithSelector:@selector(reloadData)]; | |
7b0ce2da JF |
7090 | } |
7091 | ||
5829aea2 | 7092 | - (void) cancelUpdate { |
383a58ac | 7093 | [self stopUpdateWithSelector:@selector(updateDataAndLoad)]; |
7b0ce2da JF |
7094 | } |
7095 | ||
5829aea2 GP |
7096 | - (void) cancelPressed { |
7097 | [self cancelUpdate]; | |
7098 | } | |
7b0ce2da | 7099 | |
5829aea2 GP |
7100 | - (BOOL) updating { |
7101 | return updating_; | |
7b0ce2da JF |
7102 | } |
7103 | ||
21ac0ce2 | 7104 | - (bool) isSourceCancelled { |
5829aea2 GP |
7105 | return !updating_; |
7106 | } | |
7b0ce2da | 7107 | |
21ac0ce2 | 7108 | - (void) startSourceFetch:(NSString *)uri { |
7b0ce2da JF |
7109 | } |
7110 | ||
21ac0ce2 | 7111 | - (void) stopSourceFetch:(NSString *)uri { |
bcbac8f7 JF |
7112 | } |
7113 | ||
5829aea2 GP |
7114 | - (void) setUpdateDelegate:(id)delegate { |
7115 | updatedelegate_ = delegate; | |
7b0ce2da JF |
7116 | } |
7117 | ||
7b0ce2da | 7118 | @end |
98228790 | 7119 | /* }}} */ |
f6e13561 | 7120 | |
28e596e4 | 7121 | /* Cydia Navigation Controller Implementation {{{ */ |
15f0d613 | 7122 | @implementation UINavigationController (Cydia) |
bc11cf5b | 7123 | |
35f0a3b5 | 7124 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
7125 | NSMutableArray *stack([NSMutableArray array]); |
7126 | ||
cd79e8cf | 7127 | for (CyteViewController *controller in [self viewControllers]) { |
fe8e721f GP |
7128 | NSString *url = [[controller navigationURL] absoluteString]; |
7129 | if (url != nil) | |
7130 | [stack addObject:url]; | |
7131 | } | |
7132 | ||
7133 | return stack; | |
7134 | } | |
7135 | ||
15f0d613 JF |
7136 | - (void) reloadData { |
7137 | [super reloadData]; | |
7138 | ||
21263088 JF |
7139 | UIViewController *visible([self visibleViewController]); |
7140 | if (visible != nil) | |
15f0d613 | 7141 | [visible reloadData]; |
21263088 JF |
7142 | |
7143 | // on the iPad, this view controller is ALSO visible. :( | |
7144 | if (IsWildcat_) | |
907ba491 JF |
7145 | if (UIViewController *modal = [self modalViewController]) |
7146 | if ([modal modalPresentationStyle] == UIModalPresentationFormSheet) | |
7147 | if (UIViewController *top = [self topViewController]) | |
7148 | if (top != visible) | |
7149 | [top reloadData]; | |
15f0d613 | 7150 | } |
6c0ba3d9 | 7151 | |
15f0d613 | 7152 | - (void) unloadData { |
cd79e8cf | 7153 | for (CyteViewController *page in [self viewControllers]) |
15f0d613 | 7154 | [page unloadData]; |
6c0ba3d9 | 7155 | |
15f0d613 | 7156 | [super unloadData]; |
dc63e78f JF |
7157 | } |
7158 | ||
5829aea2 GP |
7159 | @end |
7160 | /* }}} */ | |
7b0ce2da | 7161 | |
5829aea2 GP |
7162 | /* Cydia:// Protocol {{{ */ |
7163 | @interface CydiaURLProtocol : NSURLProtocol { | |
9fe5e5f8 JF |
7164 | } |
7165 | ||
7b0ce2da JF |
7166 | @end |
7167 | ||
5829aea2 GP |
7168 | @implementation CydiaURLProtocol |
7169 | ||
7170 | + (BOOL) canInitWithRequest:(NSURLRequest *)request { | |
7171 | NSURL *url([request URL]); | |
7172 | if (url == nil) | |
7173 | return NO; | |
b0a2900d | 7174 | |
5829aea2 | 7175 | NSString *scheme([[url scheme] lowercaseString]); |
b0a2900d JF |
7176 | if (scheme != nil && [scheme isEqualToString:@"cydia"]) |
7177 | return YES; | |
7178 | if ([[url absoluteString] hasPrefix:@"about:cydia-"]) | |
7179 | return YES; | |
7180 | ||
7181 | return NO; | |
aa5e5990 JF |
7182 | } |
7183 | ||
5829aea2 GP |
7184 | + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { |
7185 | return request; | |
7186 | } | |
aa5e5990 | 7187 | |
5829aea2 GP |
7188 | - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { |
7189 | id<NSURLProtocolClient> client([self client]); | |
7190 | if (icon == nil) | |
7191 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; | |
7192 | else { | |
7193 | NSData *data(UIImagePNGRepresentation(icon)); | |
01d93940 | 7194 | |
5829aea2 GP |
7195 | NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); |
7196 | [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; | |
7197 | [client URLProtocol:self didLoadData:data]; | |
7198 | [client URLProtocolDidFinishLoading:self]; | |
7199 | } | |
3931b718 JF |
7200 | } |
7201 | ||
5829aea2 GP |
7202 | - (void) startLoading { |
7203 | id<NSURLProtocolClient> client([self client]); | |
7204 | NSURLRequest *request([self request]); | |
bc11cf5b | 7205 | |
5829aea2 GP |
7206 | NSURL *url([request URL]); |
7207 | NSString *href([url absoluteString]); | |
b0a2900d JF |
7208 | NSString *scheme([[url scheme] lowercaseString]); |
7209 | ||
7210 | NSString *path; | |
7211 | ||
7212 | if ([scheme isEqualToString:@"cydia"]) | |
7213 | path = [href substringFromIndex:8]; | |
7214 | else if ([scheme isEqualToString:@"about"]) | |
7215 | path = [href substringFromIndex:12]; | |
7216 | else _assert(false); | |
bc11cf5b | 7217 | |
5829aea2 | 7218 | NSRange slash([path rangeOfString:@"/"]); |
bc11cf5b | 7219 | |
5829aea2 GP |
7220 | NSString *command; |
7221 | if (slash.location == NSNotFound) { | |
7222 | command = path; | |
7223 | path = nil; | |
7224 | } else { | |
7225 | command = [path substringToIndex:slash.location]; | |
7226 | path = [path substringFromIndex:(slash.location + 1)]; | |
7227 | } | |
39cda3a8 | 7228 | |
5829aea2 | 7229 | Database *database([Database sharedInstance]); |
bc11cf5b | 7230 | |
d0a5ea56 JF |
7231 | if (false); |
7232 | else if ([command isEqualToString:@"application-icon"]) { | |
7233 | if (path == nil) | |
7234 | goto fail; | |
7235 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
c7e78d5f JF |
7236 | |
7237 | UIImage *icon(nil); | |
7238 | ||
7239 | if (icon == nil && $SBSCopyIconImagePNGDataForDisplayIdentifier != NULL) { | |
7240 | NSData *data([$SBSCopyIconImagePNGDataForDisplayIdentifier(path) autorelease]); | |
d0a5ea56 | 7241 | icon = [UIImage imageWithData:data]; |
c7e78d5f JF |
7242 | } |
7243 | ||
7244 | if (icon == nil) | |
7245 | if (NSString *file = SBSCopyIconImagePathForDisplayIdentifier(path)) | |
7246 | icon = [UIImage imageAtPath:file]; | |
7247 | ||
7248 | if (icon == nil) | |
7249 | icon = [UIImage imageNamed:@"unknown.png"]; | |
7250 | ||
d0a5ea56 JF |
7251 | [self _returnPNGWithImage:icon forRequest:request]; |
7252 | } else if ([command isEqualToString:@"package-icon"]) { | |
5829aea2 GP |
7253 | if (path == nil) |
7254 | goto fail; | |
7255 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7256 | Package *package([database packageWithName:path]); | |
7257 | if (package == nil) | |
7258 | goto fail; | |
eff20a8d | 7259 | [package parse]; |
5829aea2 GP |
7260 | UIImage *icon([package icon]); |
7261 | [self _returnPNGWithImage:icon forRequest:request]; | |
5829aea2 GP |
7262 | } else if ([command isEqualToString:@"uikit-image"]) { |
7263 | if (path == nil) | |
7264 | goto fail; | |
7265 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7266 | UIImage *icon(_UIImageWithName(path)); | |
7267 | [self _returnPNGWithImage:icon forRequest:request]; | |
7268 | } else if ([command isEqualToString:@"section-icon"]) { | |
7269 | if (path == nil) | |
7270 | goto fail; | |
7271 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
611efc17 | 7272 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [path stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]); |
5829aea2 | 7273 | if (icon == nil) |
8dbf3abc | 7274 | icon = [UIImage imageNamed:@"unknown.png"]; |
5829aea2 GP |
7275 | [self _returnPNGWithImage:icon forRequest:request]; |
7276 | } else fail: { | |
7277 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; | |
7278 | } | |
39cda3a8 GP |
7279 | } |
7280 | ||
5829aea2 GP |
7281 | - (void) stopLoading { |
7282 | } | |
807ae6d7 | 7283 | |
5829aea2 GP |
7284 | @end |
7285 | /* }}} */ | |
807ae6d7 | 7286 | |
5829aea2 | 7287 | /* Section Controller {{{ */ |
f50860ee | 7288 | @interface SectionController : FilteredPackageListController { |
55066b9e | 7289 | _H<NSString> key_; |
123b380c | 7290 | _H<NSString> section_; |
5829aea2 | 7291 | } |
807ae6d7 | 7292 | |
55066b9e | 7293 | - (id) initWithDatabase:(Database *)database source:(Source *)source section:(NSString *)section; |
807ae6d7 | 7294 | |
5829aea2 | 7295 | @end |
bc11cf5b | 7296 | |
5829aea2 | 7297 | @implementation SectionController |
bc11cf5b | 7298 | |
f050e4d9 | 7299 | - (NSURL *) referrerURL { |
55066b9e JF |
7300 | NSString *name(section_); |
7301 | name = name ?: @"*"; | |
7302 | NSString *key(key_); | |
7303 | key = key ?: @"*"; | |
7304 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/sections/%@/%@", UI_, [key stringByAddingPercentEscapesIncludingReserved], [name stringByAddingPercentEscapesIncludingReserved]]]; | |
f050e4d9 JF |
7305 | } |
7306 | ||
fe8e721f | 7307 | - (NSURL *) navigationURL { |
55066b9e JF |
7308 | NSString *name(section_); |
7309 | name = name ?: @"*"; | |
7310 | NSString *key(key_); | |
7311 | key = key ?: @"*"; | |
7312 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sections/%@/%@", [key stringByAddingPercentEscapesIncludingReserved], [name stringByAddingPercentEscapesIncludingReserved]]]; | |
fe8e721f GP |
7313 | } |
7314 | ||
55066b9e | 7315 | - (id) initWithDatabase:(Database *)database source:(Source *)source section:(NSString *)section { |
5829aea2 | 7316 | NSString *title; |
55066b9e | 7317 | if (section == nil) |
5829aea2 | 7318 | title = UCLocalize("ALL_PACKAGES"); |
55066b9e JF |
7319 | else if (![section isEqual:@""]) |
7320 | title = [[NSBundle mainBundle] localizedStringForKey:Simplify(section) value:nil table:@"Sections"]; | |
3707eb56 | 7321 | else |
5829aea2 | 7322 | title = UCLocalize("NO_SECTION"); |
fe8e721f | 7323 | |
89bdef78 | 7324 | if ((self = [super initWithDatabase:database title:title]) != nil) { |
55066b9e JF |
7325 | key_ = [source key]; |
7326 | section_ = section; | |
5829aea2 | 7327 | } return self; |
b5e7eebb GP |
7328 | } |
7329 | ||
55066b9e | 7330 | - (void) reloadData { |
89bdef78 JF |
7331 | Source *source([database_ sourceWithKey:key_]); |
7332 | _H<NSString> name(section_); | |
7333 | ||
7334 | [self setFilter:[=](Package *package) { | |
7335 | NSString *section([package section]); | |
7336 | ||
7337 | return ( | |
7338 | name == nil || | |
7339 | section == nil && [name length] == 0 || | |
7340 | [name isEqualToString:section] | |
7341 | ) && ( | |
7342 | source == nil || | |
7343 | [package source] == source | |
7344 | ) && [package visible]; | |
7345 | }]; | |
7346 | ||
55066b9e JF |
7347 | [super reloadData]; |
7348 | } | |
7349 | ||
b5e7eebb GP |
7350 | @end |
7351 | /* }}} */ | |
5829aea2 | 7352 | /* Sections Controller {{{ */ |
cd79e8cf | 7353 | @interface SectionsController : CyteViewController < |
5829aea2 GP |
7354 | UITableViewDataSource, |
7355 | UITableViewDelegate | |
7585ce66 | 7356 | > { |
3931b718 | 7357 | _transient Database *database_; |
55066b9e | 7358 | _H<NSString> key_; |
7b33d201 JF |
7359 | _H<NSMutableArray> sections_; |
7360 | _H<NSMutableArray> filtered_; | |
bf7c998c | 7361 | _H<UITableView, 2> list_; |
b5e7eebb GP |
7362 | } |
7363 | ||
55066b9e | 7364 | - (id) initWithDatabase:(Database *)database source:(Source *)source; |
5829aea2 | 7365 | - (void) editButtonClicked; |
7585ce66 | 7366 | |
bc11cf5b | 7367 | @end |
b5e7eebb | 7368 | |
5829aea2 | 7369 | @implementation SectionsController |
b5e7eebb | 7370 | |
fe8e721f | 7371 | - (NSURL *) navigationURL { |
55066b9e JF |
7372 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sources/%@", [key_ stringByAddingPercentEscapesIncludingReserved]]]; |
7373 | } | |
7374 | ||
7375 | - (Source *) source { | |
7376 | if (key_ == nil) | |
7377 | return nil; | |
7378 | return [database_ sourceWithKey:key_]; | |
fe8e721f GP |
7379 | } |
7380 | ||
a784d0a4 | 7381 | - (void) updateNavigationItem { |
8e5b801a | 7382 | [[self navigationItem] setTitle:[self isEditing] ? UCLocalize("SECTION_VISIBILITY") : UCLocalize("SECTIONS")]; |
a784d0a4 GP |
7383 | if ([sections_ count] == 0) { |
7384 | [[self navigationItem] setRightBarButtonItem:nil]; | |
7385 | } else { | |
7386 | [[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] | |
8e5b801a | 7387 | initWithBarButtonSystemItem:([self isEditing] ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit) |
a784d0a4 GP |
7388 | target:self |
7389 | action:@selector(editButtonClicked) | |
7390 | ] animated:([[self navigationItem] rightBarButtonItem] != nil)]; | |
7391 | } | |
7392 | } | |
7393 | ||
8e5b801a GP |
7394 | - (void) setEditing:(BOOL)editing animated:(BOOL)animated { |
7395 | [super setEditing:editing animated:animated]; | |
35f0a3b5 | 7396 | |
8e5b801a | 7397 | if (editing) |
5829aea2 GP |
7398 | [list_ reloadData]; |
7399 | else | |
7400 | [delegate_ updateData]; | |
7585ce66 | 7401 | |
a784d0a4 | 7402 | [self updateNavigationItem]; |
b5e7eebb GP |
7403 | } |
7404 | ||
5829aea2 GP |
7405 | - (void) viewDidAppear:(BOOL)animated { |
7406 | [super viewDidAppear:animated]; | |
7407 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7585ce66 JF |
7408 | } |
7409 | ||
5829aea2 GP |
7410 | - (void) viewWillDisappear:(BOOL)animated { |
7411 | [super viewWillDisappear:animated]; | |
31eedaae | 7412 | [self setEditing:NO]; |
7585ce66 JF |
7413 | } |
7414 | ||
5829aea2 | 7415 | - (Section *) sectionAtIndexPath:(NSIndexPath *)indexPath { |
b3551da8 | 7416 | Section *section = nil; |
8e5b801a | 7417 | int index = [indexPath row]; |
b3551da8 | 7418 | if (![self isEditing]) { |
666b48ad | 7419 | index -= 1; |
b3551da8 GP |
7420 | if (index >= 0) |
7421 | section = [filtered_ objectAtIndex:index]; | |
7422 | } else { | |
7423 | section = [sections_ objectAtIndex:index]; | |
7424 | } | |
5829aea2 GP |
7425 | return section; |
7426 | } | |
7585ce66 | 7427 | |
5829aea2 | 7428 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
8e5b801a GP |
7429 | if ([self isEditing]) |
7430 | return [sections_ count]; | |
7431 | else | |
7432 | return [filtered_ count] + 1; | |
7585ce66 JF |
7433 | } |
7434 | ||
5829aea2 GP |
7435 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
7436 | return 45.0f; | |
7437 | }*/ | |
7585ce66 | 7438 | |
5829aea2 GP |
7439 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
7440 | static NSString *reuseIdentifier = @"SectionCell"; | |
7585ce66 | 7441 | |
35f0a3b5 | 7442 | SectionCell *cell = (SectionCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5829aea2 GP |
7443 | if (cell == nil) |
7444 | cell = [[[SectionCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
7585ce66 | 7445 | |
8e5b801a | 7446 | [cell setSection:[self sectionAtIndexPath:indexPath] editing:[self isEditing]]; |
54043703 | 7447 | |
5829aea2 | 7448 | return cell; |
54043703 JF |
7449 | } |
7450 | ||
5829aea2 | 7451 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
8e5b801a | 7452 | if ([self isEditing]) |
54043703 | 7453 | return; |
7585ce66 | 7454 | |
5829aea2 | 7455 | Section *section = [self sectionAtIndexPath:indexPath]; |
7585ce66 | 7456 | |
5829aea2 GP |
7457 | SectionController *controller = [[[SectionController alloc] |
7458 | initWithDatabase:database_ | |
55066b9e | 7459 | source:[self source] |
5829aea2 GP |
7460 | section:[section name] |
7461 | ] autorelease]; | |
7462 | [controller setDelegate:delegate_]; | |
7585ce66 | 7463 | |
5829aea2 | 7464 | [[self navigationController] pushViewController:controller animated:YES]; |
7585ce66 JF |
7465 | } |
7466 | ||
fe8e721f | 7467 | - (void) loadView { |
e8cbebe4 | 7468 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; |
fe8e721f | 7469 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
86a333c6 | 7470 | [list_ setRowHeight:46]; |
7b33d201 | 7471 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 7472 | [list_ setDelegate:self]; |
e8cbebe4 | 7473 | [self setView:list_]; |
fe8e721f GP |
7474 | } |
7475 | ||
7476 | - (void) viewDidLoad { | |
7d887d0b JF |
7477 | [super viewDidLoad]; |
7478 | ||
fe8e721f GP |
7479 | [[self navigationItem] setTitle:UCLocalize("SECTIONS")]; |
7480 | } | |
7481 | ||
7482 | - (void) releaseSubviews { | |
fe8e721f | 7483 | list_ = nil; |
7be3eb32 | 7484 | |
4f9acb7c JF |
7485 | sections_ = nil; |
7486 | filtered_ = nil; | |
7487 | ||
7be3eb32 | 7488 | [super releaseSubviews]; |
fe8e721f GP |
7489 | } |
7490 | ||
55066b9e | 7491 | - (id) initWithDatabase:(Database *)database source:(Source *)source { |
5829aea2 GP |
7492 | if ((self = [super init]) != nil) { |
7493 | database_ = database; | |
55066b9e | 7494 | key_ = [source key]; |
5829aea2 | 7495 | } return self; |
7585ce66 JF |
7496 | } |
7497 | ||
5829aea2 | 7498 | - (void) reloadData { |
fe8e721f GP |
7499 | [super reloadData]; |
7500 | ||
5829aea2 | 7501 | NSArray *packages = [database_ packages]; |
7585ce66 | 7502 | |
4f9acb7c JF |
7503 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
7504 | filtered_ = [NSMutableArray arrayWithCapacity:16]; | |
7585ce66 | 7505 | |
5829aea2 | 7506 | NSMutableDictionary *sections([NSMutableDictionary dictionaryWithCapacity:32]); |
7585ce66 | 7507 | |
55066b9e JF |
7508 | Source *source([self source]); |
7509 | ||
5829aea2 GP |
7510 | _trace(); |
7511 | for (Package *package in packages) { | |
55066b9e JF |
7512 | if (source != nil && [package source] != source) |
7513 | continue; | |
7514 | ||
5829aea2 GP |
7515 | NSString *name([package section]); |
7516 | NSString *key(name == nil ? @"" : name); | |
7585ce66 | 7517 | |
5829aea2 | 7518 | Section *section; |
7585ce66 | 7519 | |
5829aea2 GP |
7520 | _profile(SectionsView$reloadData$Section) |
7521 | section = [sections objectForKey:key]; | |
7522 | if (section == nil) { | |
7523 | _profile(SectionsView$reloadData$Section$Allocate) | |
729bc910 | 7524 | section = [[[Section alloc] initWithName:key localize:YES] autorelease]; |
5829aea2 GP |
7525 | [sections setObject:section forKey:key]; |
7526 | _end | |
7527 | } | |
7528 | _end | |
7585ce66 | 7529 | |
5829aea2 | 7530 | [section addToCount]; |
7585ce66 | 7531 | |
5829aea2 | 7532 | _profile(SectionsView$reloadData$Filter) |
826318ca | 7533 | if (![package visible]) |
5829aea2 GP |
7534 | continue; |
7535 | _end | |
7585ce66 | 7536 | |
5829aea2 GP |
7537 | [section addToRow]; |
7538 | } | |
7539 | _trace(); | |
7585ce66 | 7540 | |
5829aea2 | 7541 | [sections_ addObjectsFromArray:[sections allValues]]; |
7585ce66 | 7542 | |
5829aea2 | 7543 | [sections_ sortUsingSelector:@selector(compareByLocalized:)]; |
7585ce66 | 7544 | |
7b33d201 | 7545 | for (Section *section in (id) sections_) { |
5829aea2 GP |
7546 | size_t count([section row]); |
7547 | if (count == 0) | |
7548 | continue; | |
7585ce66 | 7549 | |
5829aea2 GP |
7550 | section = [[[Section alloc] initWithName:[section name] localized:[section localized]] autorelease]; |
7551 | [section setCount:count]; | |
7552 | [filtered_ addObject:section]; | |
7553 | } | |
7585ce66 | 7554 | |
a784d0a4 | 7555 | [self updateNavigationItem]; |
5829aea2 GP |
7556 | [list_ reloadData]; |
7557 | _trace(); | |
7558 | } | |
7585ce66 | 7559 | |
6840bff3 | 7560 | - (void) editButtonClicked { |
8e5b801a | 7561 | [self setEditing:![self isEditing] animated:YES]; |
5829aea2 | 7562 | } |
7585ce66 | 7563 | |
5829aea2 GP |
7564 | @end |
7565 | /* }}} */ | |
7585ce66 | 7566 | |
5829aea2 | 7567 | /* Changes Controller {{{ */ |
e5491e28 | 7568 | @interface ChangesController : FilteredPackageListController { |
5829aea2 | 7569 | unsigned upgrades_; |
5829aea2 | 7570 | } |
7585ce66 | 7571 | |
ea3bb538 | 7572 | - (id) initWithDatabase:(Database *)database; |
7585ce66 | 7573 | |
5829aea2 | 7574 | @end |
7585ce66 | 7575 | |
5829aea2 | 7576 | @implementation ChangesController |
7585ce66 | 7577 | |
e5491e28 JF |
7578 | - (NSURL *) referrerURL { |
7579 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/changes/", UI_]]; | |
b5e7eebb GP |
7580 | } |
7581 | ||
e5491e28 JF |
7582 | - (NSURL *) navigationURL { |
7583 | return [NSURL URLWithString:@"cydia://changes"]; | |
5829aea2 | 7584 | } |
b5e7eebb | 7585 | |
5829aea2 GP |
7586 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
7587 | @synchronized (database_) { | |
7588 | if ([database_ era] != era_) | |
7589 | return nil; | |
b5e7eebb | 7590 | |
5829aea2 GP |
7591 | NSUInteger sectionIndex([path section]); |
7592 | if (sectionIndex >= [sections_ count]) | |
7593 | return nil; | |
7594 | Section *section([sections_ objectAtIndex:sectionIndex]); | |
7595 | NSInteger row([path row]); | |
56bf1e78 | 7596 | return [[[packages_ objectAtIndex:([section row] + row)] retain] autorelease]; |
5829aea2 | 7597 | } } |
b5e7eebb | 7598 | |
0e15b67c JF |
7599 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
7600 | NSString *context([alert context]); | |
7601 | ||
7602 | if ([context isEqualToString:@"norefresh"]) | |
7603 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
7604 | } | |
7605 | ||
e67ebdad JF |
7606 | - (void) setLeftBarButtonItem { |
7607 | if ([delegate_ updating]) | |
7608 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7609 | initWithTitle:UCLocalize("CANCEL") | |
7610 | style:UIBarButtonItemStyleDone | |
7611 | target:self | |
7612 | action:@selector(cancelButtonClicked) | |
7613 | ] autorelease] animated:YES]; | |
7614 | else | |
7615 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7616 | initWithTitle:UCLocalize("REFRESH") | |
7617 | style:UIBarButtonItemStylePlain | |
7618 | target:self | |
7619 | action:@selector(refreshButtonClicked) | |
7620 | ] autorelease] animated:YES]; | |
7621 | } | |
7622 | ||
5829aea2 | 7623 | - (void) refreshButtonClicked { |
e67ebdad JF |
7624 | if ([delegate_ requestUpdate]) |
7625 | [self setLeftBarButtonItem]; | |
7626 | } | |
0e15b67c | 7627 | |
e67ebdad JF |
7628 | - (void) cancelButtonClicked { |
7629 | [delegate_ cancelUpdate]; | |
b5e7eebb GP |
7630 | } |
7631 | ||
5829aea2 GP |
7632 | - (void) upgradeButtonClicked { |
7633 | [delegate_ distUpgrade]; | |
cb6b3a7b | 7634 | [[self navigationItem] setRightBarButtonItem:nil animated:YES]; |
b5e7eebb GP |
7635 | } |
7636 | ||
e5491e28 JF |
7637 | - (bool) shouldYield { |
7638 | return true; | |
fe8e721f GP |
7639 | } |
7640 | ||
e5491e28 JF |
7641 | - (bool) shouldBlock { |
7642 | return true; | |
fe8e721f GP |
7643 | } |
7644 | ||
e5491e28 JF |
7645 | - (void) useFilter { |
7646 | @synchronized (self) { | |
7647 | [self setFilter:[](Package *package) { | |
7648 | return [package upgradableAndEssential:YES] || [package visible]; | |
7649 | }]; | |
4f9acb7c | 7650 | |
e5491e28 JF |
7651 | [self setSorter:[](NSMutableArray *packages) { |
7652 | [packages radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackageChangesRadix) withContext:NULL]; | |
7653 | }]; | |
7654 | } } | |
5829aea2 | 7655 | |
ea3bb538 | 7656 | - (id) initWithDatabase:(Database *)database { |
e5491e28 JF |
7657 | if ((self = [super initWithDatabase:database title:UCLocalize("CHANGES")]) != nil) { |
7658 | [self useFilter]; | |
5829aea2 | 7659 | } return self; |
807ae6d7 JF |
7660 | } |
7661 | ||
946c4377 JF |
7662 | - (void) viewDidLoad { |
7663 | [super viewDidLoad]; | |
7664 | [self setLeftBarButtonItem]; | |
7665 | } | |
7666 | ||
7667 | - (void) viewWillAppear:(BOOL)animated { | |
7668 | [super viewWillAppear:animated]; | |
7669 | [self setLeftBarButtonItem]; | |
7670 | } | |
7671 | ||
e5491e28 | 7672 | - (void) reloadData { |
e67ebdad | 7673 | [self setLeftBarButtonItem]; |
e5491e28 JF |
7674 | [super reloadData]; |
7675 | } | |
e67ebdad | 7676 | |
e5491e28 JF |
7677 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { |
7678 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); | |
bdca103d | 7679 | |
5829aea2 GP |
7680 | Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; |
7681 | Section *ignored = nil; | |
7682 | Section *section = nil; | |
7683 | time_t last = 0; | |
807ae6d7 | 7684 | |
5829aea2 GP |
7685 | upgrades_ = 0; |
7686 | bool unseens = false; | |
807ae6d7 | 7687 | |
5829aea2 | 7688 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); |
807ae6d7 | 7689 | |
e5491e28 JF |
7690 | for (size_t offset = 0, count = [packages count]; offset != count; ++offset) { |
7691 | Package *package = [packages objectAtIndex:offset]; | |
807ae6d7 | 7692 | |
5829aea2 | 7693 | BOOL uae = [package upgradableAndEssential:YES]; |
807ae6d7 | 7694 | |
5829aea2 GP |
7695 | if (!uae) { |
7696 | unseens = true; | |
7697 | time_t seen([package seen]); | |
d90a4cd6 | 7698 | |
5829aea2 GP |
7699 | if (section == nil || last != seen) { |
7700 | last = seen; | |
7701 | ||
7702 | NSString *name; | |
7703 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]); | |
7704 | [name autorelease]; | |
7705 | ||
7706 | _profile(ChangesController$reloadData$Allocate) | |
7707 | name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; | |
7708 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
e5491e28 | 7709 | [sections addObject:section]; |
5829aea2 GP |
7710 | _end |
7711 | } | |
7712 | ||
7713 | [section addToCount]; | |
7714 | } else if ([package ignored]) { | |
7715 | if (ignored == nil) { | |
7716 | ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease]; | |
7717 | } | |
7718 | [ignored addToCount]; | |
7719 | } else { | |
7720 | ++upgrades_; | |
7721 | [upgradable addToCount]; | |
7722 | } | |
7723 | } | |
7724 | _trace(); | |
7725 | ||
7726 | CFRelease(formatter); | |
7727 | ||
7728 | if (unseens) { | |
e5491e28 | 7729 | Section *last = [sections lastObject]; |
5829aea2 | 7730 | size_t count = [last count]; |
e5491e28 JF |
7731 | [packages removeObjectsInRange:NSMakeRange([packages count] - count, count)]; |
7732 | [sections removeLastObject]; | |
5829aea2 GP |
7733 | } |
7734 | ||
7735 | if ([ignored count] != 0) | |
e5491e28 | 7736 | [sections insertObject:ignored atIndex:0]; |
5829aea2 | 7737 | if (upgrades_ != 0) |
e5491e28 | 7738 | [sections insertObject:upgradable atIndex:0]; |
5829aea2 GP |
7739 | |
7740 | [list_ reloadData]; | |
7741 | ||
cb6b3a7b JF |
7742 | [[self navigationItem] setRightBarButtonItem:(upgrades_ == 0 ? nil : [[[UIBarButtonItem alloc] |
7743 | initWithTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("UPGRADE"), [NSString stringWithFormat:@"%u", upgrades_]] | |
7744 | style:UIBarButtonItemStylePlain | |
7745 | target:self | |
7746 | action:@selector(upgradeButtonClicked) | |
7747 | ] autorelease]) animated:YES]; | |
5829aea2 | 7748 | |
e5491e28 | 7749 | return sections; |
a70cd4ba JF |
7750 | } |
7751 | ||
5829aea2 GP |
7752 | @end |
7753 | /* }}} */ | |
7754 | /* Search Controller {{{ */ | |
f50860ee | 7755 | @interface SearchController : FilteredPackageListController < |
5829aea2 GP |
7756 | UISearchBarDelegate |
7757 | > { | |
bf7c998c | 7758 | _H<UISearchBar, 1> search_; |
fe8e721f | 7759 | BOOL searchloaded_; |
89bdef78 | 7760 | bool summary_; |
5829aea2 GP |
7761 | } |
7762 | ||
43625891 | 7763 | - (id) initWithDatabase:(Database *)database query:(NSString *)query; |
5829aea2 | 7764 | - (void) reloadData; |
d90a4cd6 GP |
7765 | |
7766 | @end | |
7767 | ||
5829aea2 | 7768 | @implementation SearchController |
d90a4cd6 | 7769 | |
f050e4d9 JF |
7770 | - (NSURL *) referrerURL { |
7771 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/search?q=%@", UI_, [([search_ text] ?: @"") stringByAddingPercentEscapesIncludingReserved]]]; | |
7772 | } | |
7773 | ||
fe8e721f | 7774 | - (NSURL *) navigationURL { |
35f0a3b5 GP |
7775 | if ([search_ text] == nil || [[search_ text] isEqualToString:@""]) |
7776 | return [NSURL URLWithString:@"cydia://search"]; | |
7777 | else | |
b90c7892 | 7778 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://search/%@", [[search_ text] stringByAddingPercentEscapesIncludingReserved]]]; |
fe8e721f GP |
7779 | } |
7780 | ||
096e25d8 | 7781 | - (NSArray *) termsForQuery:(NSString *)query { |
945812b9 JF |
7782 | NSMutableArray *terms([NSMutableArray arrayWithCapacity:2]); |
7783 | for (NSString *component in [query componentsSeparatedByString:@" "]) | |
7784 | if ([component length] != 0) | |
7785 | [terms addObject:component]; | |
7786 | ||
7787 | return terms; | |
096e25d8 JF |
7788 | } |
7789 | ||
59f3d290 | 7790 | - (void) useSearch { |
89bdef78 JF |
7791 | _H<NSArray> query([self termsForQuery:[search_ text]]); |
7792 | summary_ = false; | |
7793 | ||
7794 | @synchronized (self) { | |
7795 | [self setFilter:[=](Package *package) { | |
7796 | if (![package unfiltered]) | |
7797 | return false; | |
7798 | if (![package matches:query]) | |
7799 | return false; | |
7800 | return true; | |
7801 | }]; | |
7802 | ||
7803 | [self setSorter:[](NSMutableArray *packages) { | |
7804 | [packages radixSortUsingSelector:@selector(rank)]; | |
7805 | }]; | |
7806 | } | |
7807 | ||
59f3d290 JF |
7808 | [self clearData]; |
7809 | [self reloadData]; | |
7810 | } | |
7811 | ||
89bdef78 JF |
7812 | - (void) usePrefix:(NSString *)prefix { |
7813 | _H<NSString> query(prefix); | |
7814 | summary_ = true; | |
7815 | ||
7816 | @synchronized (self) { | |
7817 | [self setFilter:[=](Package *package) { | |
7818 | if ([query length] == 0) | |
7819 | return false; | |
7820 | if (![package unfiltered]) | |
7821 | return false; | |
7822 | if ([[package name] compare:query options:MatchCompareOptions_ range:NSMakeRange(0, [query length])] != NSOrderedSame) | |
7823 | return false; | |
7824 | return true; | |
7825 | }]; | |
7826 | ||
7827 | [self setSorter:nullptr]; | |
7828 | } | |
7829 | ||
7830 | [self reloadData]; | |
7831 | } | |
7832 | ||
3025d5b4 | 7833 | - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar { |
59f3d290 | 7834 | [self clearData]; |
89bdef78 | 7835 | [self usePrefix:[search_ text]]; |
3025d5b4 JF |
7836 | } |
7837 | ||
7838 | - (void) searchBarButtonClicked:(UISearchBar *)searchBar { | |
5829aea2 | 7839 | [search_ resignFirstResponder]; |
59f3d290 | 7840 | [self useSearch]; |
5829aea2 GP |
7841 | } |
7842 | ||
3025d5b4 JF |
7843 | - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar { |
7844 | [search_ setText:@""]; | |
7845 | [self searchBarButtonClicked:searchBar]; | |
7846 | } | |
7847 | ||
7848 | - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { | |
7849 | [self searchBarButtonClicked:searchBar]; | |
7850 | } | |
7851 | ||
5829aea2 | 7852 | - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { |
89bdef78 | 7853 | [self usePrefix:text]; |
5829aea2 GP |
7854 | } |
7855 | ||
3025d5b4 | 7856 | - (bool) shouldYield { |
56bf1e78 JF |
7857 | return YES; |
7858 | } | |
7859 | ||
7860 | - (bool) shouldBlock { | |
89bdef78 | 7861 | return !summary_; |
3025d5b4 JF |
7862 | } |
7863 | ||
59f3d290 | 7864 | - (bool) isSummarized { |
89bdef78 | 7865 | return summary_; |
59f3d290 JF |
7866 | } |
7867 | ||
9c5737d5 JF |
7868 | - (bool) showsSections { |
7869 | return false; | |
7870 | } | |
7871 | ||
43625891 | 7872 | - (id) initWithDatabase:(Database *)database query:(NSString *)query { |
89bdef78 | 7873 | if ((self = [super initWithDatabase:database title:UCLocalize("SEARCH")])) { |
900095fd | 7874 | search_ = [[[UISearchBar alloc] init] autorelease]; |
830efb5d | 7875 | [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; |
900095fd | 7876 | [search_ setDelegate:self]; |
43625891 | 7877 | |
830efb5d JF |
7878 | UITextField *textField; |
7879 | if ([search_ respondsToSelector:@selector(searchField)]) | |
7880 | textField = [search_ searchField]; | |
7881 | else | |
7882 | textField = MSHookIvar<UITextField *>(search_, "_searchField"); | |
7883 | ||
7884 | [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
7885 | [textField setEnablesReturnKeyAutomatically:NO]; | |
7886 | [[self navigationItem] setTitleView:textField]; | |
7887 | ||
43625891 JF |
7888 | if (query != nil) |
7889 | [search_ setText:query]; | |
89bdef78 | 7890 | [self useSearch]; |
fe8e721f | 7891 | } return self; |
5829aea2 GP |
7892 | } |
7893 | ||
6840bff3 | 7894 | - (void) viewDidAppear:(BOOL)animated { |
5829aea2 | 7895 | [super viewDidAppear:animated]; |
fe8e721f GP |
7896 | |
7897 | if (!searchloaded_) { | |
7898 | searchloaded_ = YES; | |
7899 | [search_ setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; | |
5829aea2 | 7900 | [search_ layoutSubviews]; |
d90a4cd6 | 7901 | } |
058813ae GP |
7902 | |
7903 | if ([self isSummarized]) | |
7904 | [search_ becomeFirstResponder]; | |
5829aea2 | 7905 | } |
d90a4cd6 | 7906 | |
d90a4cd6 | 7907 | - (void) reloadData { |
f50860ee | 7908 | [self resetCursor]; |
9dac415b | 7909 | [super reloadData]; |
d90a4cd6 GP |
7910 | } |
7911 | ||
5829aea2 GP |
7912 | - (void) didSelectPackage:(Package *)package { |
7913 | [search_ resignFirstResponder]; | |
7914 | [super didSelectPackage:package]; | |
d90a4cd6 GP |
7915 | } |
7916 | ||
7917 | @end | |
7918 | /* }}} */ | |
5829aea2 | 7919 | /* Package Settings Controller {{{ */ |
cd79e8cf | 7920 | @interface PackageSettingsController : CyteViewController < |
c21004b9 JF |
7921 | UITableViewDataSource, |
7922 | UITableViewDelegate | |
7923 | > { | |
807ae6d7 | 7924 | _transient Database *database_; |
7b33d201 JF |
7925 | _H<NSString> name_; |
7926 | _H<Package> package_; | |
bf7c998c | 7927 | _H<UITableView, 2> table_; |
7b33d201 JF |
7928 | _H<UISwitch> subscribedSwitch_; |
7929 | _H<UISwitch> ignoredSwitch_; | |
7930 | _H<UITableViewCell> subscribedCell_; | |
7931 | _H<UITableViewCell> ignoredCell_; | |
807ae6d7 JF |
7932 | } |
7933 | ||
5829aea2 | 7934 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
c21004b9 | 7935 | |
807ae6d7 JF |
7936 | @end |
7937 | ||
5829aea2 | 7938 | @implementation PackageSettingsController |
807ae6d7 | 7939 | |
fe8e721f | 7940 | - (NSURL *) navigationURL { |
8861e953 | 7941 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/settings", (id) name_]]; |
fe8e721f GP |
7942 | } |
7943 | ||
5829aea2 GP |
7944 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7945 | if (package_ == nil) | |
7946 | return 0; | |
e954c30a | 7947 | |
2136717a DH |
7948 | if ([package_ installed] == nil) |
7949 | return 1; | |
7950 | else | |
7951 | return 2; | |
e954c30a GP |
7952 | } |
7953 | ||
5829aea2 GP |
7954 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7955 | if (package_ == nil) | |
7956 | return 0; | |
7957 | ||
2136717a DH |
7958 | // both sections contain just one item right now. |
7959 | return 1; | |
b5e7eebb GP |
7960 | } |
7961 | ||
5829aea2 | 7962 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
2136717a | 7963 | return nil; |
e954c30a GP |
7964 | } |
7965 | ||
5829aea2 | 7966 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
2136717a DH |
7967 | if (section == 0) |
7968 | return UCLocalize("SHOW_ALL_CHANGES_EX"); | |
7969 | else | |
7970 | return UCLocalize("IGNORE_UPGRADES_EX"); | |
807ae6d7 JF |
7971 | } |
7972 | ||
5829aea2 GP |
7973 | - (void) onSubscribed:(id)control { |
7974 | bool value([control isOn]); | |
7975 | if (package_ == nil) | |
7976 | return; | |
7977 | if ([package_ setSubscribed:value]) | |
7978 | [delegate_ updateData]; | |
807ae6d7 JF |
7979 | } |
7980 | ||
e5e70358 JF |
7981 | - (void) _updateIgnored { |
7982 | const char *package([name_ UTF8String]); | |
7983 | bool on([ignoredSwitch_ isOn]); | |
7984 | ||
55eb40b9 JF |
7985 | FILE *dpkg(popen("/usr/libexec/cydia/cydo --set-selections", "w")); |
7986 | fwrite(package, strlen(package), 1, dpkg); | |
e5e70358 | 7987 | |
55eb40b9 JF |
7988 | if (on) |
7989 | fwrite(" hold\n", 6, 1, dpkg); | |
7990 | else | |
7991 | fwrite(" install\n", 9, 1, dpkg); | |
e5e70358 | 7992 | |
55eb40b9 | 7993 | pclose(dpkg); |
e5e70358 JF |
7994 | } |
7995 | ||
5829aea2 | 7996 | - (void) onIgnored:(id)control { |
e5e70358 JF |
7997 | NSInvocation *invocation([NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(_updateIgnored)]]); |
7998 | [invocation setTarget:self]; | |
7999 | [invocation setSelector:@selector(_updateIgnored)]; | |
8000 | ||
8001 | [delegate_ reloadDataWithInvocation:invocation]; | |
5829aea2 | 8002 | } |
807ae6d7 | 8003 | |
46aa9775 | 8004 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
8005 | if (package_ == nil) |
8006 | return nil; | |
b5e7eebb | 8007 | |
2136717a | 8008 | switch ([indexPath section]) { |
5829aea2 GP |
8009 | case 0: return subscribedCell_; |
8010 | case 1: return ignoredCell_; | |
36fbb2aa | 8011 | |
5829aea2 GP |
8012 | _nodefault |
8013 | } | |
807ae6d7 | 8014 | |
5829aea2 | 8015 | return nil; |
807ae6d7 JF |
8016 | } |
8017 | ||
fe8e721f | 8018 | - (void) loadView { |
39470a3a JF |
8019 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
8020 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
8021 | [self setView:view]; | |
807ae6d7 | 8022 | |
7b33d201 | 8023 | table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease]; |
fe8e721f | 8024 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b33d201 | 8025 | [(UITableView *) table_ setDataSource:self]; |
fe8e721f | 8026 | [table_ setDelegate:self]; |
39470a3a | 8027 | [view addSubview:table_]; |
807ae6d7 | 8028 | |
7b33d201 | 8029 | subscribedSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
8030 | [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
8031 | [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 8032 | |
7b33d201 | 8033 | ignoredSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
8034 | [ignoredSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
8035 | [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 8036 | |
7b33d201 | 8037 | subscribedCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
8038 | [subscribedCell_ setText:UCLocalize("SHOW_ALL_CHANGES")]; |
8039 | [subscribedCell_ setAccessoryView:subscribedSwitch_]; | |
8040 | [subscribedCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
5829aea2 | 8041 | |
7b33d201 | 8042 | ignoredCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
8043 | [ignoredCell_ setText:UCLocalize("IGNORE_UPGRADES")]; |
8044 | [ignoredCell_ setAccessoryView:ignoredSwitch_]; | |
8045 | [ignoredCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
fe8e721f | 8046 | } |
5829aea2 | 8047 | |
fe8e721f | 8048 | - (void) viewDidLoad { |
7d887d0b JF |
8049 | [super viewDidLoad]; |
8050 | ||
fe8e721f GP |
8051 | [[self navigationItem] setTitle:UCLocalize("SETTINGS")]; |
8052 | } | |
5829aea2 | 8053 | |
fe8e721f | 8054 | - (void) releaseSubviews { |
fe8e721f | 8055 | ignoredCell_ = nil; |
fe8e721f | 8056 | subscribedCell_ = nil; |
fe8e721f | 8057 | table_ = nil; |
fe8e721f | 8058 | ignoredSwitch_ = nil; |
fe8e721f | 8059 | subscribedSwitch_ = nil; |
7be3eb32 JF |
8060 | |
8061 | [super releaseSubviews]; | |
fe8e721f GP |
8062 | } |
8063 | ||
8064 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { | |
6840bff3 | 8065 | if ((self = [super init]) != nil) { |
fe8e721f | 8066 | database_ = database; |
7b33d201 | 8067 | name_ = package; |
807ae6d7 JF |
8068 | } return self; |
8069 | } | |
8070 | ||
8071 | - (void) reloadData { | |
fe8e721f GP |
8072 | [super reloadData]; |
8073 | ||
5829aea2 | 8074 | package_ = [database_ packageWithName:name_]; |
f3e2c0ac | 8075 | |
5829aea2 | 8076 | if (package_ != nil) { |
5829aea2 GP |
8077 | [subscribedSwitch_ setOn:([package_ subscribed] ? 1 : 0) animated:NO]; |
8078 | [ignoredSwitch_ setOn:([package_ ignored] ? 1 : 0) animated:NO]; | |
f3e2c0ac | 8079 | } // XXX: what now, G? |
dc5812ec | 8080 | |
5829aea2 GP |
8081 | [table_ reloadData]; |
8082 | } | |
6d9712c4 | 8083 | |
dc5812ec | 8084 | @end |
2367a917 | 8085 | /* }}} */ |
d90a4cd6 | 8086 | |
5829aea2 | 8087 | /* Installed Controller {{{ */ |
f50860ee | 8088 | @interface InstalledController : FilteredPackageListController { |
821b1a0c | 8089 | bool sectioned_; |
dc5812ec JF |
8090 | } |
8091 | ||
5829aea2 | 8092 | - (id) initWithDatabase:(Database *)database; |
5829aea2 | 8093 | - (void) queueStatusDidChange; |
dc5812ec | 8094 | |
dc5812ec JF |
8095 | @end |
8096 | ||
5829aea2 | 8097 | @implementation InstalledController |
b4d89997 | 8098 | |
f050e4d9 JF |
8099 | - (NSURL *) referrerURL { |
8100 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/installed/", UI_]]; | |
8101 | } | |
8102 | ||
fe8e721f GP |
8103 | - (NSURL *) navigationURL { |
8104 | return [NSURL URLWithString:@"cydia://installed"]; | |
8105 | } | |
b5e7eebb | 8106 | |
b3906a21 | 8107 | - (void) useRecent { |
821b1a0c JF |
8108 | sectioned_ = false; |
8109 | ||
8110 | @synchronized (self) { | |
8111 | [self setFilter:[](Package *package) { | |
8112 | return ![package uninstalled] && package->role_ < 7; | |
8113 | }]; | |
8114 | ||
8115 | [self setSorter:[](NSMutableArray *packages) { | |
b3906a21 | 8116 | [packages radixSortUsingSelector:@selector(recent)]; |
821b1a0c JF |
8117 | }]; |
8118 | } } | |
8119 | ||
89bdef78 | 8120 | - (void) useFilter:(UISegmentedControl *)segmented { |
821b1a0c JF |
8121 | NSInteger selected([segmented selectedSegmentIndex]); |
8122 | if (selected == 2) | |
b3906a21 | 8123 | return [self useRecent]; |
821b1a0c JF |
8124 | bool simple(selected == 0); |
8125 | sectioned_ = true; | |
89bdef78 JF |
8126 | |
8127 | @synchronized (self) { | |
8128 | [self setFilter:[=](Package *package) { | |
8129 | return ![package uninstalled] && package->role_ <= (simple ? 1 : 3); | |
8130 | }]; | |
821b1a0c JF |
8131 | |
8132 | [self setSorter:nullptr]; | |
89bdef78 JF |
8133 | } } |
8134 | ||
98ddcefd JF |
8135 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { |
8136 | if (sectioned_) | |
8137 | return [super sectionsForPackages:packages]; | |
8138 | ||
8139 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterLongStyle, kCFDateFormatterNoStyle)); | |
8140 | ||
8141 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); | |
7108bdd2 | 8142 | Section *section(nil); |
98ddcefd JF |
8143 | time_t last(0); |
8144 | ||
8145 | for (size_t offset(0), count([packages count]); offset != count; ++offset) { | |
8146 | Package *package([packages objectAtIndex:offset]); | |
8147 | ||
b3906a21 | 8148 | time_t upgraded([package upgraded]); |
0cadb352 JF |
8149 | if (upgraded < 1168364520) |
8150 | upgraded = 0; | |
8151 | else | |
8152 | upgraded -= upgraded % (60 * 60 * 24); | |
98ddcefd | 8153 | |
b3906a21 JF |
8154 | if (section == nil || upgraded != last) { |
8155 | last = upgraded; | |
98ddcefd JF |
8156 | |
8157 | NSString *name; | |
0cadb352 JF |
8158 | if (upgraded == 0) |
8159 | continue; // XXX: name = UCLocalize("..."); | |
8160 | else { | |
8161 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:upgraded]); | |
8162 | [name autorelease]; | |
8163 | } | |
98ddcefd JF |
8164 | |
8165 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
8166 | [sections addObject:section]; | |
8167 | } | |
8168 | ||
8169 | [section addToCount]; | |
8170 | } | |
8171 | ||
8172 | CFRelease(formatter); | |
8173 | return sections; | |
8174 | } | |
8175 | ||
5829aea2 | 8176 | - (id) initWithDatabase:(Database *)database { |
89bdef78 | 8177 | if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED")]) != nil) { |
c8fbe1f4 | 8178 | UISegmentedControl *segmented([[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:UCLocalize("USER"), UCLocalize("EXPERT"), UCLocalize("RECENT"), nil]] autorelease]); |
3544397d JF |
8179 | [segmented setSelectedSegmentIndex:0]; |
8180 | [segmented setSegmentedControlStyle:UISegmentedControlStyleBar]; | |
8181 | [[self navigationItem] setTitleView:segmented]; | |
8182 | ||
8183 | [segmented addTarget:self action:@selector(modeChanged:) forEvents:UIControlEventValueChanged]; | |
89bdef78 | 8184 | [self useFilter:segmented]; |
3544397d | 8185 | |
5829aea2 GP |
8186 | [self queueStatusDidChange]; |
8187 | } return self; | |
dc5812ec JF |
8188 | } |
8189 | ||
5829aea2 GP |
8190 | #if !AlwaysReload |
8191 | - (void) queueButtonClicked { | |
8192 | [delegate_ queue]; | |
dc5812ec | 8193 | } |
5829aea2 | 8194 | #endif |
dc5812ec | 8195 | |
5829aea2 GP |
8196 | - (void) queueStatusDidChange { |
8197 | #if !AlwaysReload | |
55066b9e | 8198 | if (Queuing_) { |
7d3660da | 8199 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
55066b9e JF |
8200 | initWithTitle:UCLocalize("QUEUE") |
8201 | style:UIBarButtonItemStyleDone | |
8202 | target:self | |
8203 | action:@selector(queueButtonClicked) | |
8204 | ] autorelease]]; | |
8205 | } else { | |
7d3660da | 8206 | [[self navigationItem] setRightBarButtonItem:nil]; |
5829aea2 GP |
8207 | } |
8208 | #endif | |
dc5812ec JF |
8209 | } |
8210 | ||
3544397d | 8211 | - (void) modeChanged:(UISegmentedControl *)segmented { |
89bdef78 | 8212 | [self useFilter:segmented]; |
f50860ee | 8213 | [self reloadData]; |
9a7b04c5 JF |
8214 | } |
8215 | ||
5829aea2 GP |
8216 | @end |
8217 | /* }}} */ | |
b5e7eebb | 8218 | |
5829aea2 | 8219 | /* Source Cell {{{ */ |
a9311516 | 8220 | @interface SourceCell : CyteTableViewCell < |
21ac0ce2 JF |
8221 | CyteTableViewCellDelegate, |
8222 | SourceDelegate | |
5829aea2 | 8223 | > { |
21ac0ce2 | 8224 | _H<Source, 1> source_; |
02735663 | 8225 | _H<NSURL> url_; |
7b33d201 JF |
8226 | _H<UIImage> icon_; |
8227 | _H<NSString> origin_; | |
8228 | _H<NSString> label_; | |
21ac0ce2 | 8229 | _H<UIActivityIndicatorView> indicator_; |
5829aea2 | 8230 | } |
dc5812ec | 8231 | |
5829aea2 | 8232 | - (void) setSource:(Source *)source; |
21ac0ce2 | 8233 | - (void) setFetch:(NSNumber *)fetch; |
6da1297d | 8234 | |
5829aea2 | 8235 | @end |
dc5812ec | 8236 | |
5829aea2 | 8237 | @implementation SourceCell |
36bb2ca2 | 8238 | |
02735663 JF |
8239 | - (void) _setImage:(NSArray *)data { |
8240 | if ([url_ isEqual:[data objectAtIndex:0]]) { | |
8241 | icon_ = [data objectAtIndex:1]; | |
8242 | [content_ setNeedsDisplay]; | |
8243 | } | |
8252b666 JF |
8244 | } |
8245 | ||
7bd76e97 | 8246 | - (void) _setSource:(NSURL *) url { |
8252b666 JF |
8247 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
8248 | ||
7bd76e97 JF |
8249 | if (NSData *data = [NSURLConnection |
8250 | sendSynchronousRequest:[NSURLRequest | |
8251 | requestWithURL:url | |
de5f2673 JF |
8252 | cachePolicy:NSURLRequestUseProtocolCachePolicy |
8253 | timeoutInterval:10 | |
7bd76e97 JF |
8254 | ] |
8255 | ||
8256 | returningResponse:NULL | |
8257 | error:NULL | |
8258 | ]) | |
8259 | if (UIImage *image = [UIImage imageWithData:data]) | |
02735663 | 8260 | [self performSelectorOnMainThread:@selector(_setImage:) withObject:[NSArray arrayWithObjects:url, image, nil] waitUntilDone:NO]; |
8252b666 JF |
8261 | |
8262 | [pool release]; | |
8263 | } | |
8264 | ||
5829aea2 | 8265 | - (void) setSource:(Source *)source { |
21ac0ce2 JF |
8266 | source_ = source; |
8267 | [source_ setDelegate:self]; | |
8268 | ||
8269 | [self setFetch:[NSNumber numberWithBool:[source_ fetch]]]; | |
8270 | ||
8dbf3abc | 8271 | icon_ = [UIImage imageNamed:@"unknown.png"]; |
5829aea2 | 8272 | |
7b33d201 | 8273 | origin_ = [source name]; |
7bd76e97 | 8274 | label_ = [source rooturi]; |
5829aea2 GP |
8275 | |
8276 | [content_ setNeedsDisplay]; | |
8252b666 | 8277 | |
02735663 JF |
8278 | url_ = [source iconURL]; |
8279 | [NSThread detachNewThreadSelector:@selector(_setSource:) toTarget:self withObject:url_]; | |
77801ff1 JF |
8280 | } |
8281 | ||
55066b9e | 8282 | - (void) setAllSource { |
21ac0ce2 JF |
8283 | source_ = nil; |
8284 | [indicator_ stopAnimating]; | |
8285 | ||
8dbf3abc | 8286 | icon_ = [UIImage imageNamed:@"folder.png"]; |
55066b9e JF |
8287 | origin_ = UCLocalize("ALL_SOURCES"); |
8288 | label_ = UCLocalize("ALL_SOURCES_EX"); | |
8289 | [content_ setNeedsDisplay]; | |
8290 | } | |
8291 | ||
5829aea2 GP |
8292 | - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
8293 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
8294 | UIView *content([self contentView]); | |
8295 | CGRect bounds([content bounds]); | |
77801ff1 | 8296 | |
b97fcfc6 | 8297 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
5829aea2 GP |
8298 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8299 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
8300 | [content addSubview:content_]; | |
dc5812ec | 8301 | |
5829aea2 GP |
8302 | [content_ setDelegate:self]; |
8303 | [content_ setOpaque:YES]; | |
53db9999 | 8304 | |
21ac0ce2 JF |
8305 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGraySmall] autorelease]; |
8306 | [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin];// | UIViewAutoresizingFlexibleBottomMargin]; | |
8307 | [content addSubview:indicator_]; | |
8308 | ||
53db9999 | 8309 | [[content_ layer] setContentsGravity:kCAGravityTopLeft]; |
5829aea2 GP |
8310 | } return self; |
8311 | } | |
b4d89997 | 8312 | |
21ac0ce2 JF |
8313 | - (void) layoutSubviews { |
8314 | [super layoutSubviews]; | |
8315 | ||
8316 | UIView *content([self contentView]); | |
8317 | CGRect bounds([content bounds]); | |
8318 | ||
8319 | CGRect frame([indicator_ frame]); | |
8320 | frame.origin.x = bounds.size.width - frame.size.width; | |
8323c1b9 | 8321 | frame.origin.y = Retina((bounds.size.height - frame.size.height) / 2); |
21ac0ce2 JF |
8322 | |
8323 | if (kCFCoreFoundationVersionNumber < 800) | |
8324 | frame.origin.x -= 8; | |
8325 | [indicator_ setFrame:frame]; | |
8326 | } | |
8327 | ||
003fc610 | 8328 | - (NSString *) accessibilityLabel { |
66abff39 | 8329 | return origin_; |
003fc610 GP |
8330 | } |
8331 | ||
5829aea2 GP |
8332 | - (void) drawContentRect:(CGRect)rect { |
8333 | bool highlighted(highlighted_); | |
8334 | float width(rect.size.width); | |
36bb2ca2 | 8335 | |
25c1dafb JF |
8336 | if (icon_ != nil) { |
8337 | CGRect rect; | |
8338 | rect.size = [(UIImage *) icon_ size]; | |
8339 | ||
8340 | while (rect.size.width > 32 || rect.size.height > 32) { | |
8341 | rect.size.width /= 2; | |
8342 | rect.size.height /= 2; | |
8343 | } | |
8344 | ||
86a333c6 JF |
8345 | rect.origin.x = 26 - rect.size.width / 2; |
8346 | rect.origin.y = 26 - rect.size.height / 2; | |
25c1dafb | 8347 | |
8323c1b9 | 8348 | [icon_ drawInRect:Retina(rect)]; |
25c1dafb | 8349 | } |
36bb2ca2 | 8350 | |
5d0438dc | 8351 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
5829aea2 | 8352 | UISetColor(White_); |
dc5812ec | 8353 | |
5829aea2 GP |
8354 | if (!highlighted) |
8355 | UISetColor(Black_); | |
ffb4fe6a | 8356 | [origin_ drawAtPoint:CGPointMake(52, 8) forWidth:(width - 49) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
f79a4512 | 8357 | |
5829aea2 | 8358 | if (!highlighted) |
e93fd095 | 8359 | UISetColor(Gray_); |
ffb4fe6a | 8360 | [label_ drawAtPoint:CGPointMake(52, 29) forWidth:(width - 49) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; |
5829aea2 | 8361 | } |
dc5812ec | 8362 | |
21ac0ce2 JF |
8363 | - (void) setFetch:(NSNumber *)fetch { |
8364 | if ([fetch boolValue]) | |
8365 | [indicator_ startAnimating]; | |
8366 | else | |
8367 | [indicator_ stopAnimating]; | |
8368 | } | |
8369 | ||
3f8edf70 GP |
8370 | @end |
8371 | /* }}} */ | |
8372 | /* Sources Controller {{{ */ | |
cd79e8cf | 8373 | @interface SourcesController : CyteViewController < |
5829aea2 GP |
8374 | UITableViewDataSource, |
8375 | UITableViewDelegate | |
8376 | > { | |
8377 | _transient Database *database_; | |
c33064f1 JF |
8378 | unsigned era_; |
8379 | ||
bf7c998c | 8380 | _H<UITableView, 2> list_; |
7b33d201 | 8381 | _H<NSMutableArray> sources_; |
5829aea2 | 8382 | int offset_; |
723a0072 | 8383 | |
7b33d201 JF |
8384 | _H<NSString> href_; |
8385 | _H<UIProgressHUD> hud_; | |
8386 | _H<NSError> error_; | |
dc63e78f | 8387 | |
5829aea2 GP |
8388 | NSURLConnection *trivial_bz2_; |
8389 | NSURLConnection *trivial_gz_; | |
b4d89997 | 8390 | |
5829aea2 GP |
8391 | BOOL cydia_; |
8392 | } | |
dc5812ec | 8393 | |
5829aea2 | 8394 | - (id) initWithDatabase:(Database *)database; |
31eedaae | 8395 | - (void) updateButtonsForEditingStatusAnimated:(BOOL)animated; |
5829aea2 GP |
8396 | |
8397 | @end | |
8398 | ||
8399 | @implementation SourcesController | |
8400 | ||
8401 | - (void) _releaseConnection:(NSURLConnection *)connection { | |
8402 | if (connection != nil) { | |
8403 | [connection cancel]; | |
8404 | //[connection setDelegate:nil]; | |
8405 | [connection release]; | |
36bb2ca2 | 8406 | } |
5829aea2 | 8407 | } |
dc5812ec | 8408 | |
5829aea2 | 8409 | - (void) dealloc { |
5829aea2 GP |
8410 | [self _releaseConnection:trivial_gz_]; |
8411 | [self _releaseConnection:trivial_bz2_]; | |
3178d79b | 8412 | |
5829aea2 GP |
8413 | [super dealloc]; |
8414 | } | |
b5e7eebb | 8415 | |
fe8e721f GP |
8416 | - (NSURL *) navigationURL { |
8417 | return [NSURL URLWithString:@"cydia://sources"]; | |
8418 | } | |
8419 | ||
5829aea2 GP |
8420 | - (void) viewDidAppear:(BOOL)animated { |
8421 | [super viewDidAppear:animated]; | |
8422 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
8423 | } | |
9bb3b295 | 8424 | |
5829aea2 | 8425 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
55066b9e | 8426 | return 2; |
5829aea2 | 8427 | } |
8fe19fc1 | 8428 | |
5829aea2 | 8429 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
55066b9e JF |
8430 | if (section == 1) |
8431 | return UCLocalize("INDIVIDUAL_SOURCES"); | |
90ba4f86 | 8432 | return nil; |
36bb2ca2 | 8433 | } |
b4d89997 | 8434 | |
5829aea2 | 8435 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
55066b9e JF |
8436 | switch (section) { |
8437 | case 0: return 1; | |
8438 | case 1: return [sources_ count]; | |
8439 | default: return 0; | |
8440 | } | |
5829aea2 | 8441 | } |
3178d79b | 8442 | |
5829aea2 | 8443 | - (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath { |
c33064f1 JF |
8444 | @synchronized (database_) { |
8445 | if ([database_ era] != era_) | |
8446 | return nil; | |
55066b9e JF |
8447 | if ([indexPath section] != 1) |
8448 | return nil; | |
8dc0d35d | 8449 | NSUInteger index([indexPath row]); |
55066b9e JF |
8450 | if (index >= [sources_ count]) |
8451 | return nil; | |
8452 | return [sources_ objectAtIndex:index]; | |
c33064f1 | 8453 | } } |
b4d89997 | 8454 | |
5829aea2 GP |
8455 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
8456 | static NSString *cellIdentifier = @"SourceCell"; | |
8457 | ||
8458 | SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
55066b9e | 8459 | if (cell == nil) cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease]; |
b89fa270 | 8460 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
5829aea2 | 8461 | |
55066b9e JF |
8462 | Source *source([self sourceAtIndexPath:indexPath]); |
8463 | if (source == nil) | |
8464 | [cell setAllSource]; | |
8465 | else | |
8466 | [cell setSource:source]; | |
8467 | ||
5829aea2 | 8468 | return cell; |
f6e13561 GP |
8469 | } |
8470 | ||
5829aea2 | 8471 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e | 8472 | SectionsController *controller([[[SectionsController alloc] |
5829aea2 | 8473 | initWithDatabase:database_ |
55066b9e JF |
8474 | source:[self sourceAtIndexPath:indexPath] |
8475 | ] autorelease]); | |
5829aea2 | 8476 | |
3f8edf70 | 8477 | [controller setDelegate:delegate_]; |
3f8edf70 | 8478 | [[self navigationController] pushViewController:controller animated:YES]; |
36bb2ca2 | 8479 | } |
b4d89997 | 8480 | |
6840bff3 | 8481 | - (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e JF |
8482 | if ([indexPath section] != 1) |
8483 | return false; | |
5829aea2 GP |
8484 | Source *source = [self sourceAtIndexPath:indexPath]; |
8485 | return [source record] != nil; | |
dcb47737 RP |
8486 | } |
8487 | ||
6840bff3 | 8488 | - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e | 8489 | _assert([indexPath section] == 1); |
0e1aa02c JF |
8490 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
8491 | Source *source = [self sourceAtIndexPath:indexPath]; | |
8dc0d35d JF |
8492 | if (source == nil) return; |
8493 | ||
0e1aa02c | 8494 | [Sources_ removeObjectForKey:[source key]]; |
c38cbbec | 8495 | |
392ff7e4 | 8496 | [delegate_ _saveConfig]; |
cc3b7d31 | 8497 | [delegate_ reloadDataWithInvocation:nil]; |
0e1aa02c | 8498 | } |
5829aea2 | 8499 | } |
bcccf498 | 8500 | |
51807490 JF |
8501 | - (void) tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { |
8502 | [self updateButtonsForEditingStatusAnimated:YES]; | |
8503 | } | |
8504 | ||
5829aea2 | 8505 | - (void) complete { |
93460555 | 8506 | [delegate_ addTrivialSource:href_]; |
da3ec19c JF |
8507 | href_ = nil; |
8508 | ||
5829aea2 | 8509 | [delegate_ syncData]; |
3178d79b JF |
8510 | } |
8511 | ||
5829aea2 GP |
8512 | - (NSString *) getWarning { |
8513 | NSString *href(href_); | |
8514 | NSRange colon([href rangeOfString:@"://"]); | |
8515 | if (colon.location != NSNotFound) | |
8516 | href = [href substringFromIndex:(colon.location + 3)]; | |
8517 | href = [href stringByAddingPercentEscapes]; | |
8518 | href = [CydiaURL(@"api/repotag/") stringByAppendingString:href]; | |
5829aea2 GP |
8519 | |
8520 | NSURL *url([NSURL URLWithString:href]); | |
8521 | ||
8522 | NSStringEncoding encoding; | |
8523 | NSError *error(nil); | |
8524 | ||
8525 | if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]) | |
8526 | return [warning length] == 0 ? nil : warning; | |
8527 | return nil; | |
807ae6d7 | 8528 | } |
b4d89997 | 8529 | |
5829aea2 GP |
8530 | - (void) _endConnection:(NSURLConnection *)connection { |
8531 | // XXX: the memory management in this method is horribly awkward | |
8532 | ||
8533 | NSURLConnection **field = NULL; | |
44990507 | 8534 | if (connection == trivial_bz2_) |
5829aea2 GP |
8535 | field = &trivial_bz2_; |
8536 | else if (connection == trivial_gz_) | |
8537 | field = &trivial_gz_; | |
8538 | _assert(field != NULL); | |
8539 | [connection release]; | |
8540 | *field = nil; | |
8541 | ||
8542 | if ( | |
5829aea2 GP |
8543 | trivial_bz2_ == nil && |
8544 | trivial_gz_ == nil | |
8545 | ) { | |
da3ec19c JF |
8546 | NSString *warning(cydia_ ? [self yieldToSelector:@selector(getWarning)] : nil); |
8547 | ||
9eae15b8 JF |
8548 | [delegate_ releaseNetworkActivityIndicator]; |
8549 | ||
8550 | [delegate_ removeProgressHUD:hud_]; | |
9eae15b8 JF |
8551 | hud_ = nil; |
8552 | ||
5829aea2 | 8553 | if (cydia_) { |
da3ec19c | 8554 | if (warning != nil) { |
5829aea2 GP |
8555 | UIAlertView *alert = [[[UIAlertView alloc] |
8556 | initWithTitle:UCLocalize("SOURCE_WARNING") | |
8557 | message:warning | |
8558 | delegate:self | |
8559 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8560 | otherButtonTitles: |
8561 | UCLocalize("ADD_ANYWAY"), | |
8562 | nil | |
5829aea2 GP |
8563 | ] autorelease]; |
8564 | ||
8565 | [alert setContext:@"warning"]; | |
8566 | [alert setNumberOfRows:1]; | |
8567 | [alert show]; | |
708cf61e JF |
8568 | |
8569 | // XXX: there used to be this great mechanism called yieldToPopup... who deleted it? | |
8570 | error_ = nil; | |
8571 | return; | |
8572 | } | |
8573 | ||
8574 | [self complete]; | |
5829aea2 GP |
8575 | } else if (error_ != nil) { |
8576 | UIAlertView *alert = [[[UIAlertView alloc] | |
8577 | initWithTitle:UCLocalize("VERIFICATION_ERROR") | |
8578 | message:[error_ localizedDescription] | |
8579 | delegate:self | |
8580 | cancelButtonTitle:UCLocalize("OK") | |
8581 | otherButtonTitles:nil | |
8582 | ] autorelease]; | |
8583 | ||
8584 | [alert setContext:@"urlerror"]; | |
8585 | [alert show]; | |
da3ec19c JF |
8586 | |
8587 | href_ = nil; | |
5829aea2 GP |
8588 | } else { |
8589 | UIAlertView *alert = [[[UIAlertView alloc] | |
8590 | initWithTitle:UCLocalize("NOT_REPOSITORY") | |
8591 | message:UCLocalize("NOT_REPOSITORY_EX") | |
8592 | delegate:self | |
8593 | cancelButtonTitle:UCLocalize("OK") | |
8594 | otherButtonTitles:nil | |
8595 | ] autorelease]; | |
8596 | ||
8597 | [alert setContext:@"trivial"]; | |
8598 | [alert show]; | |
da3ec19c JF |
8599 | |
8600 | href_ = nil; | |
5829aea2 GP |
8601 | } |
8602 | ||
7b33d201 | 8603 | error_ = nil; |
5829aea2 | 8604 | } |
807ae6d7 | 8605 | } |
8fe19fc1 | 8606 | |
5829aea2 GP |
8607 | - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { |
8608 | switch ([response statusCode]) { | |
8609 | case 200: | |
8610 | cydia_ = YES; | |
8611 | } | |
8e05f686 RP |
8612 | } |
8613 | ||
5829aea2 | 8614 | - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { |
47ff9ab8 | 8615 | lprintf("connection:\"%s\" didFailWithError:\"%s\"\n", [href_ UTF8String], [[error localizedDescription] UTF8String]); |
7b33d201 | 8616 | error_ = error; |
5829aea2 | 8617 | [self _endConnection:connection]; |
807ae6d7 | 8618 | } |
b4d89997 | 8619 | |
5829aea2 GP |
8620 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
8621 | [self _endConnection:connection]; | |
8622 | } | |
b4d89997 | 8623 | |
5829aea2 | 8624 | - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method { |
2e1652a9 JF |
8625 | NSURL *url([NSURL URLWithString:href]); |
8626 | ||
5829aea2 | 8627 | NSMutableURLRequest *request = [NSMutableURLRequest |
2e1652a9 | 8628 | requestWithURL:url |
5829aea2 | 8629 | cachePolicy:NSURLRequestUseProtocolCachePolicy |
dd6f3b7b | 8630 | timeoutInterval:10 |
5829aea2 | 8631 | ]; |
bc11cf5b | 8632 | |
5829aea2 | 8633 | [request setHTTPMethod:method]; |
b4d89997 | 8634 | |
5829aea2 GP |
8635 | if (Machine_ != NULL) |
8636 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
2e1652a9 | 8637 | |
1209b7de JF |
8638 | if (UniqueID_ != nil) |
8639 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
8640 | ||
2e1652a9 | 8641 | if ([url isCydiaSecure]) { |
1209b7de | 8642 | if (UniqueID_ != nil) |
c83678e8 | 8643 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; |
2e1652a9 | 8644 | } |
b4d89997 | 8645 | |
5829aea2 | 8646 | return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; |
3178d79b JF |
8647 | } |
8648 | ||
6840bff3 | 8649 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
5829aea2 | 8650 | NSString *context([alert context]); |
dc5812ec | 8651 | |
5829aea2 GP |
8652 | if ([context isEqualToString:@"source"]) { |
8653 | switch (button) { | |
8654 | case 1: { | |
8655 | NSString *href = [[alert textField] text]; | |
8a2d167a JF |
8656 | href = VerifySource(href); |
8657 | if (href == nil) | |
2595e4c3 | 8658 | break; |
8a2d167a | 8659 | href_ = href; |
b4d89997 | 8660 | |
5829aea2 GP |
8661 | trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; |
8662 | trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; | |
b4d89997 | 8663 | |
5829aea2 | 8664 | cydia_ = false; |
8fe19fc1 | 8665 | |
5829aea2 | 8666 | // XXX: this is stupid |
7b33d201 | 8667 | hud_ = [delegate_ addProgressHUD]; |
5829aea2 | 8668 | [hud_ setText:UCLocalize("VERIFYING_URL")]; |
dcaecde2 | 8669 | [delegate_ retainNetworkActivityIndicator]; |
5829aea2 | 8670 | } break; |
770f2a8e | 8671 | |
5829aea2 GP |
8672 | case 0: |
8673 | break; | |
bc11cf5b | 8674 | |
5829aea2 GP |
8675 | _nodefault |
8676 | } | |
770f2a8e | 8677 | |
5829aea2 GP |
8678 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8679 | } else if ([context isEqualToString:@"trivial"]) | |
8680 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8681 | else if ([context isEqualToString:@"urlerror"]) | |
8682 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8683 | else if ([context isEqualToString:@"warning"]) { | |
8684 | switch (button) { | |
8685 | case 1: | |
da3ec19c | 8686 | [self performSelector:@selector(complete) withObject:nil afterDelay:0]; |
5829aea2 | 8687 | break; |
770f2a8e | 8688 | |
5829aea2 GP |
8689 | case 0: |
8690 | break; | |
b5e7eebb | 8691 | |
5829aea2 GP |
8692 | _nodefault |
8693 | } | |
770f2a8e | 8694 | |
5829aea2 GP |
8695 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8696 | } | |
8697 | } | |
770f2a8e | 8698 | |
e67ebdad JF |
8699 | - (void) updateButtonsForEditingStatusAnimated:(BOOL)animated { |
8700 | BOOL editing([list_ isEditing]); | |
8701 | ||
8702 | if (editing) | |
8703 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8704 | initWithTitle:UCLocalize("ADD") | |
8705 | style:UIBarButtonItemStylePlain | |
8706 | target:self | |
8707 | action:@selector(addButtonClicked) | |
8708 | ] autorelease] animated:animated]; | |
8709 | else if ([delegate_ updating]) | |
8710 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8711 | initWithTitle:UCLocalize("CANCEL") | |
8712 | style:UIBarButtonItemStyleDone | |
8713 | target:self | |
8714 | action:@selector(cancelButtonClicked) | |
8715 | ] autorelease] animated:animated]; | |
8716 | else | |
8717 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8718 | initWithTitle:UCLocalize("REFRESH") | |
8719 | style:UIBarButtonItemStylePlain | |
8720 | target:self | |
8721 | action:@selector(refreshButtonClicked) | |
8722 | ] autorelease] animated:animated]; | |
8723 | ||
8724 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
8725 | initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) | |
8726 | style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
8727 | target:self | |
8728 | action:@selector(editButtonClicked) | |
8729 | ] autorelease] animated:animated]; | |
8730 | } | |
8731 | ||
fe8e721f | 8732 | - (void) loadView { |
e8cbebe4 | 8733 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain] autorelease]; |
fe8e721f | 8734 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6ac6e186 | 8735 | [list_ setRowHeight:53]; |
7b33d201 | 8736 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 8737 | [list_ setDelegate:self]; |
e8cbebe4 | 8738 | [self setView:list_]; |
fe8e721f | 8739 | } |
770f2a8e | 8740 | |
fe8e721f | 8741 | - (void) viewDidLoad { |
7d887d0b JF |
8742 | [super viewDidLoad]; |
8743 | ||
fe8e721f | 8744 | [[self navigationItem] setTitle:UCLocalize("SOURCES")]; |
31eedaae JF |
8745 | [self updateButtonsForEditingStatusAnimated:NO]; |
8746 | } | |
8747 | ||
393699d7 | 8748 | - (void) viewWillAppear:(BOOL)animated { |
31eedaae JF |
8749 | [super viewWillAppear:animated]; |
8750 | ||
8751 | [list_ setEditing:NO]; | |
8752 | [self updateButtonsForEditingStatusAnimated:NO]; | |
fe8e721f | 8753 | } |
770f2a8e | 8754 | |
fe8e721f | 8755 | - (void) releaseSubviews { |
fe8e721f | 8756 | list_ = nil; |
7be3eb32 | 8757 | |
4f9acb7c JF |
8758 | sources_ = nil; |
8759 | ||
7be3eb32 | 8760 | [super releaseSubviews]; |
fe8e721f | 8761 | } |
770f2a8e | 8762 | |
fe8e721f GP |
8763 | - (id) initWithDatabase:(Database *)database { |
8764 | if ((self = [super init]) != nil) { | |
8765 | database_ = database; | |
807ae6d7 JF |
8766 | } return self; |
8767 | } | |
770f2a8e | 8768 | |
807ae6d7 | 8769 | - (void) reloadData { |
fe8e721f | 8770 | [super reloadData]; |
e67ebdad | 8771 | [self updateButtonsForEditingStatusAnimated:YES]; |
fe8e721f | 8772 | |
c33064f1 JF |
8773 | @synchronized (database_) { |
8774 | era_ = [database_ era]; | |
8775 | ||
4f9acb7c | 8776 | sources_ = [NSMutableArray arrayWithCapacity:16]; |
5829aea2 GP |
8777 | [sources_ addObjectsFromArray:[database_ sources]]; |
8778 | _trace(); | |
90ba4f86 | 8779 | [sources_ sortUsingSelector:@selector(compareByName:)]; |
5829aea2 GP |
8780 | _trace(); |
8781 | ||
8782 | int count([sources_ count]); | |
8783 | offset_ = 0; | |
8784 | for (int i = 0; i != count; i++) { | |
8785 | if ([[sources_ objectAtIndex:i] record] == nil) | |
8786 | break; | |
8787 | offset_++; | |
770f2a8e | 8788 | } |
807ae6d7 | 8789 | |
5829aea2 | 8790 | [list_ reloadData]; |
c33064f1 | 8791 | } } |
770f2a8e | 8792 | |
5829aea2 GP |
8793 | - (void) showAddSourcePrompt { |
8794 | UIAlertView *alert = [[[UIAlertView alloc] | |
8795 | initWithTitle:UCLocalize("ENTER_APT_URL") | |
8796 | message:nil | |
8797 | delegate:self | |
8798 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8799 | otherButtonTitles: |
8800 | UCLocalize("ADD_SOURCE"), | |
8801 | nil | |
5829aea2 | 8802 | ] autorelease]; |
770f2a8e | 8803 | |
5829aea2 | 8804 | [alert setContext:@"source"]; |
770f2a8e | 8805 | |
5829aea2 GP |
8806 | [alert setNumberOfRows:1]; |
8807 | [alert addTextFieldWithValue:@"http://" label:@""]; | |
770f2a8e | 8808 | |
5829aea2 GP |
8809 | UITextInputTraits *traits = [[alert textField] textInputTraits]; |
8810 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
8811 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
8812 | [traits setKeyboardType:UIKeyboardTypeURL]; | |
8813 | // XXX: UIReturnKeyDone | |
8814 | [traits setReturnKeyType:UIReturnKeyNext]; | |
770f2a8e | 8815 | |
5829aea2 | 8816 | [alert show]; |
770f2a8e JF |
8817 | } |
8818 | ||
5829aea2 GP |
8819 | - (void) addButtonClicked { |
8820 | [self showAddSourcePrompt]; | |
770f2a8e JF |
8821 | } |
8822 | ||
e67ebdad JF |
8823 | - (void) refreshButtonClicked { |
8824 | if ([delegate_ requestUpdate]) | |
8825 | [self updateButtonsForEditingStatusAnimated:YES]; | |
8826 | } | |
5829aea2 | 8827 | |
e67ebdad JF |
8828 | - (void) cancelButtonClicked { |
8829 | [delegate_ cancelUpdate]; | |
5829aea2 GP |
8830 | } |
8831 | ||
8832 | - (void) editButtonClicked { | |
8833 | [list_ setEditing:![list_ isEditing] animated:YES]; | |
31eedaae | 8834 | [self updateButtonsForEditingStatusAnimated:YES]; |
770f2a8e JF |
8835 | } |
8836 | ||
21c6da4b GP |
8837 | @end |
8838 | /* }}} */ | |
f3e11d24 | 8839 | |
f3e11d24 | 8840 | /* Stash Controller {{{ */ |
cd79e8cf | 8841 | @interface StashController : CyteViewController { |
7b33d201 JF |
8842 | _H<UIActivityIndicatorView> spinner_; |
8843 | _H<UILabel> status_; | |
8844 | _H<UILabel> caption_; | |
f3e11d24 | 8845 | } |
6840bff3 | 8846 | |
f3e11d24 GP |
8847 | @end |
8848 | ||
5829aea2 | 8849 | @implementation StashController |
f3e11d24 | 8850 | |
fe8e721f | 8851 | - (void) loadView { |
39470a3a JF |
8852 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
8853 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
8854 | [self setView:view]; | |
8855 | ||
8856 | [view setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; | |
fe8e721f | 8857 | |
7b33d201 | 8858 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; |
fe8e721f | 8859 | CGRect spinrect = [spinner_ frame]; |
8323c1b9 | 8860 | spinrect.origin.x = Retina([[self view] frame].size.width / 2 - spinrect.size.width / 2); |
fe8e721f GP |
8861 | spinrect.origin.y = [[self view] frame].size.height - 80.0f; |
8862 | [spinner_ setFrame:spinrect]; | |
8863 | [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; | |
39470a3a | 8864 | [view addSubview:spinner_]; |
fe8e721f GP |
8865 | [spinner_ startAnimating]; |
8866 | ||
8867 | CGRect captrect; | |
8868 | captrect.size.width = [[self view] frame].size.width; | |
8869 | captrect.size.height = 40.0f; | |
8870 | captrect.origin.x = 0; | |
8323c1b9 | 8871 | captrect.origin.y = Retina([[self view] frame].size.height / 2 - captrect.size.height * 2); |
7b33d201 | 8872 | caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; |
fe8e721f GP |
8873 | [caption_ setText:UCLocalize("PREPARING_FILESYSTEM")]; |
8874 | [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
8875 | [caption_ setFont:[UIFont boldSystemFontOfSize:28.0f]]; | |
8876 | [caption_ setTextColor:[UIColor whiteColor]]; | |
8877 | [caption_ setBackgroundColor:[UIColor clearColor]]; | |
8878 | [caption_ setShadowColor:[UIColor blackColor]]; | |
b129e6d9 | 8879 | [caption_ setTextAlignment:NSTextAlignmentCenter]; |
39470a3a | 8880 | [view addSubview:caption_]; |
fe8e721f GP |
8881 | |
8882 | CGRect statusrect; | |
8883 | statusrect.size.width = [[self view] frame].size.width; | |
8884 | statusrect.size.height = 30.0f; | |
8885 | statusrect.origin.x = 0; | |
8323c1b9 | 8886 | statusrect.origin.y = Retina([[self view] frame].size.height / 2 - statusrect.size.height); |
7b33d201 | 8887 | status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; |
fe8e721f GP |
8888 | [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
8889 | [status_ setText:UCLocalize("EXIT_WHEN_COMPLETE")]; | |
8890 | [status_ setFont:[UIFont systemFontOfSize:16.0f]]; | |
8891 | [status_ setTextColor:[UIColor whiteColor]]; | |
8892 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
8893 | [status_ setShadowColor:[UIColor blackColor]]; | |
b129e6d9 | 8894 | [status_ setTextAlignment:NSTextAlignmentCenter]; |
39470a3a | 8895 | [view addSubview:status_]; |
fe8e721f GP |
8896 | } |
8897 | ||
67dd58c7 JF |
8898 | - (void) releaseSubviews { |
8899 | spinner_ = nil; | |
8900 | status_ = nil; | |
8901 | caption_ = nil; | |
7be3eb32 JF |
8902 | |
8903 | [super releaseSubviews]; | |
67dd58c7 JF |
8904 | } |
8905 | ||
770f2a8e | 8906 | @end |
807ae6d7 | 8907 | /* }}} */ |
770f2a8e | 8908 | |
a06e9179 JF |
8909 | @interface CYURLCache : SDURLCache { |
8910 | } | |
8911 | ||
8912 | @end | |
8913 | ||
8914 | @implementation CYURLCache | |
8915 | ||
8916 | - (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request { | |
8917 | #if !ForRelease | |
8918 | if (false); | |
8919 | else if ([event isEqualToString:@"no-cache"]) | |
8920 | event = @"!!!"; | |
8921 | else if ([event isEqualToString:@"store"]) | |
8922 | event = @">>>"; | |
8923 | else if ([event isEqualToString:@"invalid"]) | |
8924 | event = @"???"; | |
8925 | else if ([event isEqualToString:@"memory"]) | |
8926 | event = @"mem"; | |
8927 | else if ([event isEqualToString:@"disk"]) | |
8928 | event = @"ssd"; | |
8929 | else if ([event isEqualToString:@"miss"]) | |
8930 | event = @"---"; | |
8931 | ||
8932 | NSLog(@"%@: %@", event, [[request URL] absoluteString]); | |
8933 | #endif | |
8934 | } | |
8935 | ||
5e845121 JF |
8936 | - (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request { |
8937 | if (NSURLResponse *response = [cached response]) | |
8938 | if (NSString *mime = [response MIMEType]) | |
8939 | if ([mime isEqualToString:@"text/cache-manifest"]) { | |
8940 | NSURL *url([response URL]); | |
8941 | ||
8942 | #if !ForRelease | |
8943 | NSLog(@"###: %@", [url absoluteString]); | |
8944 | #endif | |
8945 | ||
8946 | @synchronized (HostConfig_) { | |
8947 | [CachedURLs_ addObject:url]; | |
8948 | } | |
8949 | } | |
8950 | ||
8951 | [super storeCachedResponse:cached forRequest:request]; | |
8952 | } | |
8953 | ||
584daea0 JF |
8954 | - (void) createDiskCachePath { |
8955 | [super createDiskCachePath]; | |
584daea0 JF |
8956 | } |
8957 | ||
a06e9179 JF |
8958 | @end |
8959 | ||
2367a917 | 8960 | @interface Cydia : UIApplication < |
adb61bda | 8961 | ConfirmationControllerDelegate, |
6915b806 | 8962 | DatabaseDelegate, |
c88974b1 | 8963 | CydiaDelegate |
2367a917 | 8964 | > { |
7b33d201 | 8965 | _H<UIWindow> window_; |
5fe2bcc6 | 8966 | _H<CydiaTabBarController> tabbar_; |
9549563e | 8967 | _H<CyteTabBarController> emulated_; |
fedd38fe | 8968 | _H<AppCacheController> appcache_; |
3931b718 | 8969 | |
7b33d201 JF |
8970 | _H<NSMutableArray> essential_; |
8971 | _H<NSMutableArray> broken_; | |
dc5812ec JF |
8972 | |
8973 | Database *database_; | |
dc5812ec | 8974 | |
7b33d201 | 8975 | _H<NSURL> starturl_; |
54043703 | 8976 | |
235f5487 | 8977 | unsigned locked_; |
54043703 | 8978 | unsigned activity_; |
9b619239 | 8979 | |
7b33d201 | 8980 | _H<StashController> stash_; |
f3e11d24 | 8981 | |
8c02abc8 | 8982 | bool loaded_; |
dc5812ec JF |
8983 | } |
8984 | ||
fed0d010 | 8985 | - (void) loadData; |
670a0494 | 8986 | |
dc5812ec JF |
8987 | @end |
8988 | ||
8989 | @implementation Cydia | |
8990 | ||
2ef6faad | 8991 | - (void) lockSuspend { |
8a3b565c JF |
8992 | if (locked_++ == 0) { |
8993 | if ($SBSSetInterceptsMenuButtonForever != NULL) | |
8994 | (*$SBSSetInterceptsMenuButtonForever)(true); | |
26c8a4c8 JF |
8995 | |
8996 | [self setIdleTimerDisabled:YES]; | |
8a3b565c | 8997 | } |
2ef6faad JF |
8998 | } |
8999 | ||
9000 | - (void) unlockSuspend { | |
8a3b565c | 9001 | if (--locked_ == 0) { |
26c8a4c8 JF |
9002 | [self setIdleTimerDisabled:NO]; |
9003 | ||
8a3b565c JF |
9004 | if ($SBSSetInterceptsMenuButtonForever != NULL) |
9005 | (*$SBSSetInterceptsMenuButtonForever)(false); | |
9006 | } | |
2ef6faad JF |
9007 | } |
9008 | ||
b5e7eebb | 9009 | - (void) beginUpdate { |
7585ce66 | 9010 | [tabbar_ beginUpdate]; |
b5e7eebb GP |
9011 | } |
9012 | ||
e67ebdad JF |
9013 | - (void) cancelUpdate { |
9014 | [tabbar_ cancelUpdate]; | |
9015 | } | |
9016 | ||
9017 | - (bool) requestUpdate { | |
9018 | if (IsReachable("cydia.saurik.com")) { | |
9019 | [self beginUpdate]; | |
9020 | return true; | |
9021 | } else { | |
9022 | UIAlertView *alert = [[[UIAlertView alloc] | |
9023 | initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("REFRESH")] | |
9024 | message:@"Host Unreachable" // XXX: Localize | |
9025 | delegate:self | |
9026 | cancelButtonTitle:UCLocalize("OK") | |
9027 | otherButtonTitles:nil | |
9028 | ] autorelease]; | |
9029 | ||
9030 | [alert setContext:@"norefresh"]; | |
9031 | [alert show]; | |
9032 | ||
9033 | return false; | |
9034 | } | |
9035 | } | |
9036 | ||
b5e7eebb | 9037 | - (BOOL) updating { |
7585ce66 | 9038 | return [tabbar_ updating]; |
b5e7eebb GP |
9039 | } |
9040 | ||
9bedffaa JF |
9041 | - (void) _loaded { |
9042 | if ([broken_ count] != 0) { | |
9043 | int count = [broken_ count]; | |
9044 | ||
37d2b2a9 | 9045 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 9046 | initWithTitle:(count == 1 ? UCLocalize("HALFINSTALLED_PACKAGE") : [NSString stringWithFormat:UCLocalize("HALFINSTALLED_PACKAGES"), count]) |
b5e7eebb GP |
9047 | message:UCLocalize("HALFINSTALLED_PACKAGE_EX") |
9048 | delegate:self | |
1dc38e9c | 9049 | cancelButtonTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("FORCIBLY_CLEAR"), UCLocalize("UNSAFE")] |
1aa29546 JF |
9050 | otherButtonTitles: |
9051 | UCLocalize("TEMPORARY_IGNORE"), | |
9052 | nil | |
9bedffaa JF |
9053 | ] autorelease]; |
9054 | ||
37d2b2a9 | 9055 | [alert setContext:@"fixhalf"]; |
59befad5 | 9056 | [alert setNumberOfRows:2]; |
37d2b2a9 | 9057 | [alert show]; |
9bedffaa JF |
9058 | } else if (!Ignored_ && [essential_ count] != 0) { |
9059 | int count = [essential_ count]; | |
9060 | ||
37d2b2a9 | 9061 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 9062 | initWithTitle:(count == 1 ? UCLocalize("ESSENTIAL_UPGRADE") : [NSString stringWithFormat:UCLocalize("ESSENTIAL_UPGRADES"), count]) |
b5e7eebb GP |
9063 | message:UCLocalize("ESSENTIAL_UPGRADE_EX") |
9064 | delegate:self | |
9065 | cancelButtonTitle:UCLocalize("TEMPORARY_IGNORE") | |
1aa29546 JF |
9066 | otherButtonTitles: |
9067 | UCLocalize("UPGRADE_ESSENTIAL"), | |
9068 | UCLocalize("COMPLETE_UPGRADE"), | |
9069 | nil | |
9bedffaa JF |
9070 | ] autorelease]; |
9071 | ||
37d2b2a9 GP |
9072 | [alert setContext:@"upgrade"]; |
9073 | [alert show]; | |
9bedffaa JF |
9074 | } |
9075 | } | |
9076 | ||
2925cbba JF |
9077 | - (void) returnToCydia { |
9078 | [self _loaded]; | |
9079 | } | |
9080 | ||
42bbf27d JF |
9081 | - (void) reloadSpringBoard { |
9082 | if (kCFCoreFoundationVersionNumber >= 700) // XXX: iOS 6.x | |
9083 | system("/bin/launchctl stop com.apple.backboardd"); | |
9084 | else | |
9085 | system("/bin/launchctl stop com.apple.SpringBoard"); | |
9086 | sleep(15); | |
9087 | system("/usr/bin/killall backboardd SpringBoard"); | |
9088 | } | |
9089 | ||
7623f855 | 9090 | - (void) _saveConfig { |
4e479350 | 9091 | SaveConfig(database_); |
7623f855 JF |
9092 | } |
9093 | ||
89571a5b | 9094 | // Navigation controller for the queuing badge. |
15f0d613 | 9095 | - (UINavigationController *) queueNavigationController { |
89571a5b GP |
9096 | NSArray *controllers = [tabbar_ viewControllers]; |
9097 | return [controllers objectAtIndex:3]; | |
9098 | } | |
9099 | ||
302bf91c JF |
9100 | - (void) unloadData { |
9101 | [tabbar_ unloadData]; | |
9102 | } | |
9103 | ||
7623f855 JF |
9104 | - (void) _updateData { |
9105 | [self _saveConfig]; | |
302bf91c | 9106 | [self unloadData]; |
f6371a33 | 9107 | |
15f0d613 | 9108 | UINavigationController *navigation = [self queueNavigationController]; |
b5e7eebb | 9109 | |
4305896c | 9110 | id queuedelegate = nil; |
89571a5b GP |
9111 | if ([[navigation viewControllers] count] > 0) |
9112 | queuedelegate = [[navigation viewControllers] objectAtIndex:0]; | |
bc11cf5b | 9113 | |
89571a5b GP |
9114 | [queuedelegate queueStatusDidChange]; |
9115 | [[navigation tabBarItem] setBadgeValue:(Queuing_ ? UCLocalize("Q_D") : nil)]; | |
7623f855 JF |
9116 | } |
9117 | ||
6f87c61a | 9118 | - (void) _refreshIfPossible { |
8c02abc8 | 9119 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
bc11cf5b | 9120 | |
6f87c61a JF |
9121 | NSDate *update([[NSDictionary dictionaryWithContentsOfFile:@ CacheState_] objectForKey:@"LastUpdate"]); |
9122 | ||
45e21ffa | 9123 | bool recently = false; |
45e21ffa GP |
9124 | if (update != nil) { |
9125 | NSTimeInterval interval([update timeIntervalSinceNow]); | |
805ba1b0 | 9126 | if (interval > -(15*60)) |
45e21ffa GP |
9127 | recently = true; |
9128 | } | |
9129 | ||
9130 | // Don't automatic refresh if: | |
9131 | // - We already refreshed recently. | |
9132 | // - We already auto-refreshed this launch. | |
9133 | // - Auto-refresh is disabled. | |
8d3505c5 JF |
9134 | // - Cydia's server is not reachable |
9135 | if (recently || loaded_ || ManualRefresh || !IsReachable("cydia.saurik.com")) { | |
35f0a3b5 | 9136 | // If we are cancelling, we need to make sure it knows it's already loaded. |
45e21ffa | 9137 | loaded_ = true; |
d13edf44 JF |
9138 | |
9139 | [self performSelectorOnMainThread:@selector(_loaded) withObject:nil waitUntilDone:NO]; | |
45e21ffa GP |
9140 | } else { |
9141 | // We are going to load, so remember that. | |
9142 | loaded_ = true; | |
45e21ffa | 9143 | |
6f87c61a | 9144 | [tabbar_ performSelectorOnMainThread:@selector(beginUpdate) withObject:nil waitUntilDone:NO]; |
d13edf44 | 9145 | } |
9aecdc9c | 9146 | |
8c02abc8 | 9147 | [pool release]; |
9aecdc9c GP |
9148 | } |
9149 | ||
9150 | - (void) refreshIfPossible { | |
6f87c61a | 9151 | [NSThread detachNewThreadSelector:@selector(_refreshIfPossible) toTarget:self withObject:nil]; |
9aecdc9c GP |
9152 | } |
9153 | ||
e09e1589 | 9154 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
1a83afc6 | 9155 | _profile(reloadDataWithInvocation) |
e09e1589 | 9156 | @synchronized (self) { |
851f4a99 | 9157 | UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); |
1450c2b3 JF |
9158 | if (hud != nil) |
9159 | [hud setText:UCLocalize("RELOADING_DATA")]; | |
dc5812ec | 9160 | |
4ba8f30a | 9161 | [database_ yieldToSelector:@selector(reloadDataWithInvocation:) withObject:invocation]; |
d061f4ba | 9162 | |
36bb2ca2 | 9163 | size_t changes(0); |
9bedffaa | 9164 | |
a54b1c10 | 9165 | [essential_ removeAllObjects]; |
9bedffaa | 9166 | [broken_ removeAllObjects]; |
b4d89997 | 9167 | |
1a83afc6 | 9168 | _profile(reloadDataWithInvocation$Essential) |
670a0494 | 9169 | NSArray *packages([database_ packages]); |
affeffc7 | 9170 | for (Package *package in packages) { |
9bedffaa JF |
9171 | if ([package half]) |
9172 | [broken_ addObject:package]; | |
823e2d97 JF |
9173 | if ([package upgradableAndEssential:YES] && ![package ignored]) { |
9174 | if ([package essential] && [package installed] != nil) | |
a54b1c10 | 9175 | [essential_ addObject:package]; |
36bb2ca2 | 9176 | ++changes; |
a54b1c10 | 9177 | } |
36bb2ca2 | 9178 | } |
1a83afc6 | 9179 | _end |
b4d89997 | 9180 | |
89571a5b | 9181 | UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem]; |
36bb2ca2 | 9182 | if (changes != 0) { |
0e1784b4 | 9183 | _trace(); |
36bb2ca2 | 9184 | NSString *badge([[NSNumber numberWithInt:changes] stringValue]); |
45e21ffa | 9185 | [changesItem setBadgeValue:badge]; |
65a03a7a | 9186 | [changesItem setAnimatedBadge:([essential_ count] > 0)]; |
0e1784b4 | 9187 | [self setApplicationIconBadgeNumber:changes]; |
c25a610d | 9188 | } else { |
0e1784b4 | 9189 | _trace(); |
45e21ffa GP |
9190 | [changesItem setBadgeValue:nil]; |
9191 | [changesItem setAnimatedBadge:NO]; | |
0e1784b4 | 9192 | [self setApplicationIconBadgeNumber:0]; |
c25a610d | 9193 | } |
b4d89997 | 9194 | |
838ec6e3 | 9195 | Queuing_ = false; |
7623f855 | 9196 | [self _updateData]; |
be64dfbf JF |
9197 | |
9198 | if (hud != nil) | |
9199 | [self removeProgressHUD:hud]; | |
1a83afc6 JF |
9200 | } |
9201 | _end | |
9202 | ||
9203 | PrintTimes(); | |
9204 | } | |
6d9712c4 | 9205 | |
7b0ce2da | 9206 | - (void) updateData { |
7623f855 | 9207 | [self _updateData]; |
b4d89997 JF |
9208 | } |
9209 | ||
383a58ac JF |
9210 | - (void) updateDataAndLoad { |
9211 | [self _updateData]; | |
9212 | if ([database_ progressDelegate] == nil) | |
9213 | [self _loaded]; | |
9214 | } | |
9215 | ||
7b0ce2da JF |
9216 | - (void) update_ { |
9217 | [database_ update]; | |
fca2f596 | 9218 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
7b0ce2da JF |
9219 | } |
9220 | ||
2e9123cb JF |
9221 | - (void) disemulate { |
9222 | if (emulated_ == nil) | |
9223 | return; | |
9224 | ||
81628115 JF |
9225 | if ([window_ respondsToSelector:@selector(setRootViewController:)]) |
9226 | [window_ setRootViewController:tabbar_]; | |
237c3d1c JF |
9227 | else { |
9228 | [window_ addSubview:[tabbar_ view]]; | |
9229 | [[emulated_ view] removeFromSuperview]; | |
9230 | } | |
9231 | ||
2e9123cb JF |
9232 | emulated_ = nil; |
9233 | [window_ setUserInteractionEnabled:YES]; | |
9234 | } | |
9235 | ||
9236 | - (void) presentModalViewController:(UIViewController *)controller force:(BOOL)force { | |
15f0d613 | 9237 | UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:controller] autorelease]); |
2e9123cb JF |
9238 | |
9239 | UIViewController *parent; | |
9240 | if (emulated_ == nil) | |
9241 | parent = tabbar_; | |
9242 | else if (!force) | |
9243 | parent = emulated_; | |
9244 | else { | |
9245 | [self disemulate]; | |
9246 | parent = tabbar_; | |
9247 | } | |
9248 | ||
483881e4 JF |
9249 | if (IsWildcat_) |
9250 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
2e9123cb | 9251 | [parent presentModalViewController:navigation animated:YES]; |
6915b806 JF |
9252 | } |
9253 | ||
9254 | - (ProgressController *) invokeNewProgress:(NSInvocation *)invocation forController:(UINavigationController *)navigation withTitle:(NSString *)title { | |
9255 | ProgressController *progress([[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]); | |
9256 | ||
9257 | if (navigation != nil) | |
9258 | [navigation pushViewController:progress animated:YES]; | |
9259 | else | |
2e9123cb | 9260 | [self presentModalViewController:progress force:YES]; |
6915b806 JF |
9261 | |
9262 | [progress invoke:invocation withTitle:title]; | |
9263 | return progress; | |
9264 | } | |
9265 | ||
9266 | - (void) detachNewProgressSelector:(SEL)selector toTarget:(id)target forController:(UINavigationController *)navigation title:(NSString *)title { | |
9267 | [self invokeNewProgress:[NSInvocation invocationWithSelector:selector forTarget:target] forController:navigation withTitle:title]; | |
9268 | } | |
9269 | ||
9270 | - (void) repairWithInvocation:(NSInvocation *)invocation { | |
9271 | _trace(); | |
8d5bc2ad | 9272 | [self invokeNewProgress:invocation forController:nil withTitle:@"REPAIRING"]; |
6915b806 JF |
9273 | _trace(); |
9274 | } | |
9275 | ||
9276 | - (void) repairWithSelector:(SEL)selector { | |
9277 | [self performSelectorOnMainThread:@selector(repairWithInvocation:) withObject:[NSInvocation invocationWithSelector:selector forTarget:database_] waitUntilDone:YES]; | |
9278 | } | |
9279 | ||
e09e1589 JF |
9280 | - (void) reloadData { |
9281 | [self reloadDataWithInvocation:nil]; | |
2925cbba JF |
9282 | if ([database_ progressDelegate] == nil) |
9283 | [self _loaded]; | |
e09e1589 JF |
9284 | } |
9285 | ||
7b0ce2da | 9286 | - (void) syncData { |
1fe5dc43 | 9287 | [self _saveConfig]; |
33e30380 JF |
9288 | [self detachNewProgressSelector:@selector(update_) toTarget:self forController:nil title:@"UPDATING_SOURCES"]; |
9289 | } | |
1fe5dc43 | 9290 | |
33e30380 | 9291 | - (void) addSource:(NSDictionary *) source { |
25c1dafb | 9292 | CydiaAddSource(source); |
33e30380 | 9293 | } |
7b0ce2da | 9294 | |
33e30380 | 9295 | - (void) addSource:(NSString *)href withDistribution:(NSString *)distribution andSections:(NSArray *)sections { |
25c1dafb | 9296 | CydiaAddSource(href, distribution, sections); |
7b0ce2da JF |
9297 | } |
9298 | ||
8a2d167a JF |
9299 | // XXX: this method should not return anything |
9300 | - (BOOL) addTrivialSource:(NSString *)href { | |
25c1dafb | 9301 | CydiaAddSource(href, @"./"); |
8a2d167a | 9302 | return YES; |
93460555 JF |
9303 | } |
9304 | ||
b4d89997 JF |
9305 | - (void) resolve { |
9306 | pkgProblemResolver *resolver = [database_ resolver]; | |
9307 | ||
9308 | resolver->InstallProtect(); | |
9309 | if (!resolver->Resolve(true)) | |
9310 | _error->Discard(); | |
2367a917 JF |
9311 | } |
9312 | ||
670a0494 | 9313 | - (bool) perform { |
d6c371f5 JF |
9314 | // XXX: this is a really crappy way of doing this. |
9315 | // like, seriously: this state machine is still broken, and cancelling this here doesn't really /fix/ that. | |
9316 | // for one, the user can still /start/ a reloading data event while they have a queue, which is stupid | |
9317 | // for two, this just means there is a race condition between the refresh completing and the confirmation controller appearing. | |
9318 | if ([tabbar_ updating]) | |
9319 | [tabbar_ cancelUpdate]; | |
9320 | ||
670a0494 JF |
9321 | if (![database_ prepare]) |
9322 | return false; | |
49048579 | 9323 | |
adb61bda | 9324 | ConfirmationController *page([[[ConfirmationController alloc] initWithDatabase:database_] autorelease]); |
affeffc7 | 9325 | [page setDelegate:self]; |
15f0d613 | 9326 | UINavigationController *confirm_([[[UINavigationController alloc] initWithRootViewController:page] autorelease]); |
49048579 | 9327 | |
36fbb2aa JF |
9328 | if (IsWildcat_) |
9329 | [confirm_ setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 9330 | [tabbar_ presentModalViewController:confirm_ animated:YES]; |
670a0494 JF |
9331 | |
9332 | return true; | |
3178d79b JF |
9333 | } |
9334 | ||
dc63e78f JF |
9335 | - (void) queue { |
9336 | @synchronized (self) { | |
9337 | [self perform]; | |
9338 | } | |
9339 | } | |
9340 | ||
9341 | - (void) clearPackage:(Package *)package { | |
9342 | @synchronized (self) { | |
9343 | [package clear]; | |
9344 | [self resolve]; | |
9345 | [self perform]; | |
9346 | } | |
9347 | } | |
9348 | ||
77801ff1 JF |
9349 | - (void) installPackages:(NSArray *)packages { |
9350 | @synchronized (self) { | |
9351 | for (Package *package in packages) | |
9352 | [package install]; | |
9353 | [self resolve]; | |
9354 | [self perform]; | |
9355 | } | |
9356 | } | |
9357 | ||
36bb2ca2 JF |
9358 | - (void) installPackage:(Package *)package { |
9359 | @synchronized (self) { | |
9360 | [package install]; | |
9361 | [self resolve]; | |
9362 | [self perform]; | |
9363 | } | |
9364 | } | |
9365 | ||
9366 | - (void) removePackage:(Package *)package { | |
9367 | @synchronized (self) { | |
9368 | [package remove]; | |
9369 | [self resolve]; | |
9370 | [self perform]; | |
9371 | } | |
9372 | } | |
9373 | ||
9374 | - (void) distUpgrade { | |
9375 | @synchronized (self) { | |
670a0494 JF |
9376 | if (![database_ upgrade]) |
9377 | return; | |
36bb2ca2 JF |
9378 | [self perform]; |
9379 | } | |
b4d89997 JF |
9380 | } |
9381 | ||
780cdb3b JF |
9382 | - (void) _uicache { |
9383 | _trace(); | |
5685d514 | 9384 | system("/usr/bin/uicache"); |
780cdb3b JF |
9385 | _trace(); |
9386 | } | |
9387 | ||
9388 | - (void) uicache { | |
9389 | UIProgressHUD *hud([self addProgressHUD]); | |
9390 | [hud setText:UCLocalize("LOADING")]; | |
9391 | [self yieldToSelector:@selector(_uicache)]; | |
9392 | [self removeProgressHUD:hud]; | |
9393 | } | |
9394 | ||
fca2f596 JF |
9395 | - (void) perform_ { |
9396 | [database_ perform]; | |
9397 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; | |
780cdb3b | 9398 | [self performSelectorOnMainThread:@selector(uicache) withObject:nil waitUntilDone:YES]; |
fca2f596 JF |
9399 | } |
9400 | ||
b5e7eebb | 9401 | - (void) confirmWithNavigationController:(UINavigationController *)navigation { |
f6371a33 | 9402 | Queuing_ = false; |
2ef6faad | 9403 | [self lockSuspend]; |
fca2f596 | 9404 | [self detachNewProgressSelector:@selector(perform_) toTarget:self forController:navigation title:@"RUNNING"]; |
2ef6faad | 9405 | [self unlockSuspend]; |
dc5812ec JF |
9406 | } |
9407 | ||
54043703 JF |
9408 | - (void) retainNetworkActivityIndicator { |
9409 | if (activity_++ == 0) | |
9410 | [self setNetworkActivityIndicatorVisible:YES]; | |
848ed88b JF |
9411 | |
9412 | #if TraceLogging | |
9413 | NSLog(@"retainNetworkActivityIndicator->%d", activity_); | |
9414 | #endif | |
54043703 JF |
9415 | } |
9416 | ||
9417 | - (void) releaseNetworkActivityIndicator { | |
9418 | if (--activity_ == 0) | |
9419 | [self setNetworkActivityIndicatorVisible:NO]; | |
848ed88b JF |
9420 | |
9421 | #if TraceLogging | |
9422 | NSLog(@"releaseNetworkActivityIndicator->%d", activity_); | |
9423 | #endif | |
9424 | ||
54043703 JF |
9425 | } |
9426 | ||
674dce72 GP |
9427 | - (void) cancelAndClear:(bool)clear { |
9428 | @synchronized (self) { | |
9429 | if (clear) { | |
c6ca67ba | 9430 | [database_ clear]; |
b5e7eebb | 9431 | Queuing_ = false; |
674dce72 | 9432 | } else { |
b5e7eebb | 9433 | Queuing_ = true; |
6067f1b8 JF |
9434 | } |
9435 | ||
a4217bbb | 9436 | [self _updateData]; |
674dce72 | 9437 | } |
37d2b2a9 | 9438 | } |
7b0ce2da | 9439 | |
b5e7eebb GP |
9440 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
9441 | NSString *context([alert context]); | |
bc11cf5b | 9442 | |
6915b806 JF |
9443 | if ([context isEqualToString:@"conffile"]) { |
9444 | FILE *input = [database_ input]; | |
9445 | if (button == [alert cancelButtonIndex]) | |
9446 | fprintf(input, "N\n"); | |
9447 | else if (button == [alert firstOtherButtonIndex]) | |
9448 | fprintf(input, "Y\n"); | |
9449 | fflush(input); | |
9450 | ||
9451 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
9452 | } else if ([context isEqualToString:@"fixhalf"]) { | |
efa53fa9 | 9453 | if (button == [alert cancelButtonIndex]) { |
37d2b2a9 | 9454 | @synchronized (self) { |
584daea0 | 9455 | for (Package *broken in (id) broken_) { |
37d2b2a9 | 9456 | [broken remove]; |
d8b0f6e3 | 9457 | NSString *id(ShellEscape([broken id])); |
7cf0c7b3 JF |
9458 | system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/rm -f" |
9459 | " /var/lib/dpkg/info/%@.prerm" | |
9460 | " /var/lib/dpkg/info/%@.postrm" | |
9461 | " /var/lib/dpkg/info/%@.preinst" | |
9462 | " /var/lib/dpkg/info/%@.postinst" | |
9463 | " /var/lib/dpkg/info/%@.extrainst_" | |
9464 | "", id, id, id, id, id] UTF8String]); | |
37d2b2a9 | 9465 | } |
7b0ce2da | 9466 | |
37d2b2a9 GP |
9467 | [self resolve]; |
9468 | [self perform]; | |
9469 | } | |
efa53fa9 | 9470 | } else if (button == [alert firstOtherButtonIndex]) { |
37d2b2a9 GP |
9471 | [broken_ removeAllObjects]; |
9472 | [self _loaded]; | |
7b0ce2da JF |
9473 | } |
9474 | ||
37d2b2a9 | 9475 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9476 | } else if ([context isEqualToString:@"upgrade"]) { |
37d2b2a9 GP |
9477 | if (button == [alert firstOtherButtonIndex]) { |
9478 | @synchronized (self) { | |
7b33d201 | 9479 | for (Package *essential in (id) essential_) |
37d2b2a9 | 9480 | [essential install]; |
7b0ce2da | 9481 | |
37d2b2a9 GP |
9482 | [self resolve]; |
9483 | [self perform]; | |
9484 | } | |
9485 | } else if (button == [alert firstOtherButtonIndex] + 1) { | |
9486 | [self distUpgrade]; | |
9487 | } else if (button == [alert cancelButtonIndex]) { | |
9488 | Ignored_ = YES; | |
7b0ce2da JF |
9489 | } |
9490 | ||
37d2b2a9 | 9491 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9492 | } |
7b0ce2da JF |
9493 | } |
9494 | ||
d13edf44 JF |
9495 | - (void) system:(NSString *)command { |
9496 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9497 | ||
985d2dff | 9498 | _trace(); |
0c0a966b | 9499 | system([command UTF8String]); |
985d2dff | 9500 | _trace(); |
d13edf44 JF |
9501 | |
9502 | [pool release]; | |
670a0494 JF |
9503 | } |
9504 | ||
9505 | - (void) applicationWillSuspend { | |
9506 | [database_ clean]; | |
9507 | [super applicationWillSuspend]; | |
bd150f54 JF |
9508 | } |
9509 | ||
235f5487 | 9510 | - (BOOL) isSafeToSuspend { |
5927fe03 JF |
9511 | if (locked_ != 0) { |
9512 | #if !ForRelease | |
9513 | NSLog(@"isSafeToSuspend: locked_ != 0"); | |
9514 | #endif | |
9515 | return false; | |
9516 | } | |
9517 | ||
7187b5b2 JF |
9518 | if ([tabbar_ modalViewController] != nil) |
9519 | return false; | |
9520 | ||
262afe11 GP |
9521 | // Use external process status API internally. |
9522 | // This is probably a really bad idea. | |
235f5487 | 9523 | // XXX: what is the point of this? does this solve anything at all? |
262afe11 GP |
9524 | uint64_t status = 0; |
9525 | int notify_token; | |
9526 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
9527 | notify_get_state(notify_token, &status); | |
9528 | notify_cancel(notify_token); | |
9529 | } | |
9530 | ||
5927fe03 JF |
9531 | if (status != 0) { |
9532 | #if !ForRelease | |
9533 | NSLog(@"isSafeToSuspend: status != 0"); | |
9534 | #endif | |
9535 | return false; | |
9536 | } | |
9537 | ||
9538 | #if !ForRelease | |
9539 | NSLog(@"isSafeToSuspend: -> true"); | |
9540 | #endif | |
9541 | return true; | |
235f5487 JF |
9542 | } |
9543 | ||
216f3dc6 JF |
9544 | - (void) suspendReturningToLastApp:(BOOL)returning { |
9545 | if ([self isSafeToSuspend]) | |
9546 | [super suspendReturningToLastApp:returning]; | |
9547 | } | |
9548 | ||
9549 | - (void) suspend { | |
9550 | if ([self isSafeToSuspend]) | |
9551 | [super suspend]; | |
9552 | } | |
9553 | ||
9554 | - (void) applicationSuspend { | |
9555 | if ([self isSafeToSuspend]) | |
9556 | [super applicationSuspend]; | |
9557 | } | |
9558 | ||
235f5487 JF |
9559 | - (void) applicationSuspend:(__GSEvent *)event { |
9560 | if ([self isSafeToSuspend]) | |
bd150f54 | 9561 | [super applicationSuspend:event]; |
bd150f54 JF |
9562 | } |
9563 | ||
6d9712c4 | 9564 | - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 { |
235f5487 | 9565 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9566 | [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3]; |
9567 | } | |
9568 | ||
9569 | - (void) _setSuspended:(BOOL)value { | |
235f5487 | 9570 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9571 | [super _setSuspended:value]; |
9572 | } | |
9573 | ||
7b0ce2da | 9574 | - (UIProgressHUD *) addProgressHUD { |
8c9453da | 9575 | UIProgressHUD *hud([[[UIProgressHUD alloc] init] autorelease]); |
04fe1349 JF |
9576 | [hud setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
9577 | ||
d061f4ba | 9578 | [window_ setUserInteractionEnabled:NO]; |
534e31fc | 9579 | |
f36e5eac JF |
9580 | UIViewController *target(tabbar_); |
9581 | if (UIViewController *modal = [target modalViewController]) | |
9582 | target = modal; | |
9583 | ||
22b6c4b8 | 9584 | [hud showInView:[target view]]; |
534e31fc | 9585 | |
2ef6faad | 9586 | [self lockSuspend]; |
7b0ce2da JF |
9587 | return hud; |
9588 | } | |
9589 | ||
d061f4ba | 9590 | - (void) removeProgressHUD:(UIProgressHUD *)hud { |
2ef6faad | 9591 | [self unlockSuspend]; |
8c9453da | 9592 | [hud hide]; |
d061f4ba JF |
9593 | [hud removeFromSuperview]; |
9594 | [window_ setUserInteractionEnabled:YES]; | |
9595 | } | |
9596 | ||
f050e4d9 JF |
9597 | - (CyteViewController *) pageForPackage:(NSString *)name withReferrer:(NSString *)referrer { |
9598 | return [[[CYPackageController alloc] initWithDatabase:database_ forPackage:name withReferrer:referrer] autorelease]; | |
c390d3ab JF |
9599 | } |
9600 | ||
f050e4d9 | 9601 | - (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external withReferrer:(NSString *)referrer { |
e47c4742 | 9602 | NSString *scheme([[url scheme] lowercaseString]); |
f6e13561 | 9603 | if ([[url absoluteString] length] <= [scheme length] + 3) |
e47c4742 | 9604 | return nil; |
f6e13561 | 9605 | NSString *path([[url absoluteString] substringFromIndex:[scheme length] + 3]); |
3a159223 | 9606 | NSArray *components([path componentsSeparatedByString:@"/"]); |
f6e13561 | 9607 | |
4fd0c466 | 9608 | if ([scheme isEqualToString:@"apptapp"] && [components count] > 0 && [[components objectAtIndex:0] isEqualToString:@"package"]) { |
f050e4d9 | 9609 | CyteViewController *controller([self pageForPackage:[components objectAtIndex:1] withReferrer:referrer]); |
4fd0c466 JF |
9610 | if (controller != nil) |
9611 | [controller setDelegate:self]; | |
9612 | return controller; | |
9613 | } | |
f6e13561 GP |
9614 | |
9615 | if ([components count] < 1 || ![scheme isEqualToString:@"cydia"]) | |
e47c4742 | 9616 | return nil; |
f6e13561 GP |
9617 | |
9618 | NSString *base([components objectAtIndex:0]); | |
9619 | ||
cd79e8cf | 9620 | CyteViewController *controller = nil; |
f5a17517 | 9621 | |
f70ea899 | 9622 | if ([base isEqualToString:@"url"]) { |
106d645f GP |
9623 | // This kind of URL can contain slashes in the argument, so we can't parse them below. |
9624 | NSString *destination = [[url absoluteString] substringFromIndex:([scheme length] + [@"://" length] + [base length] + [@"/" length])]; | |
a576488f | 9625 | controller = [[[CydiaWebViewController alloc] initWithURL:[NSURL URLWithString:destination]] autorelease]; |
028dbd1c | 9626 | } else if (!external && [components count] == 1) { |
f6e13561 | 9627 | if ([base isEqualToString:@"sources"]) { |
f5a17517 | 9628 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9629 | } |
9630 | ||
9631 | if ([base isEqualToString:@"home"]) { | |
f5a17517 | 9632 | controller = [[[HomeController alloc] init] autorelease]; |
f6e13561 GP |
9633 | } |
9634 | ||
9635 | if ([base isEqualToString:@"sections"]) { | |
55066b9e | 9636 | controller = [[[SectionsController alloc] initWithDatabase:database_ source:nil] autorelease]; |
f6e13561 GP |
9637 | } |
9638 | ||
9639 | if ([base isEqualToString:@"search"]) { | |
43625891 | 9640 | controller = [[[SearchController alloc] initWithDatabase:database_ query:nil] autorelease]; |
f6e13561 GP |
9641 | } |
9642 | ||
9643 | if ([base isEqualToString:@"changes"]) { | |
ea3bb538 | 9644 | controller = [[[ChangesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9645 | } |
9646 | ||
9647 | if ([base isEqualToString:@"installed"]) { | |
f5a17517 | 9648 | controller = [[[InstalledController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9649 | } |
9650 | } else if ([components count] == 2) { | |
8912cff7 | 9651 | NSString *argument = [[components objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
f6e13561 GP |
9652 | |
9653 | if ([base isEqualToString:@"package"]) { | |
f050e4d9 | 9654 | controller = [self pageForPackage:argument withReferrer:referrer]; |
f6e13561 GP |
9655 | } |
9656 | ||
028dbd1c | 9657 | if (!external && [base isEqualToString:@"search"]) { |
8912cff7 | 9658 | controller = [[[SearchController alloc] initWithDatabase:database_ query:argument] autorelease]; |
f6e13561 GP |
9659 | } |
9660 | ||
028dbd1c | 9661 | if (!external && [base isEqualToString:@"sections"]) { |
55066b9e | 9662 | if ([argument isEqualToString:@"all"] || [argument isEqualToString:@"*"]) |
f6e13561 | 9663 | argument = nil; |
55066b9e | 9664 | controller = [[[SectionController alloc] initWithDatabase:database_ source:nil section:argument] autorelease]; |
f6e13561 GP |
9665 | } |
9666 | ||
75d2e426 | 9667 | if ([base isEqualToString:@"sources"]) { |
f6e13561 | 9668 | if ([argument isEqualToString:@"add"]) { |
f5a17517 GP |
9669 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
9670 | [(SourcesController *)controller showAddSourcePrompt]; | |
f6e13561 | 9671 | } else { |
8912cff7 | 9672 | Source *source([database_ sourceWithKey:argument]); |
55066b9e | 9673 | controller = [[[SectionsController alloc] initWithDatabase:database_ source:source] autorelease]; |
f6e13561 GP |
9674 | } |
9675 | } | |
9676 | ||
028dbd1c | 9677 | if (!external && [base isEqualToString:@"launch"]) { |
f6e13561 | 9678 | [self launchApplicationWithIdentifier:argument suspended:NO]; |
f5a17517 | 9679 | return nil; |
f6e13561 | 9680 | } |
028dbd1c | 9681 | } else if (!external && [components count] == 3) { |
8912cff7 JF |
9682 | NSString *arg1 = [[components objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
9683 | NSString *arg2 = [[components objectAtIndex:2] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
f6e13561 GP |
9684 | |
9685 | if ([base isEqualToString:@"package"]) { | |
9686 | if ([arg2 isEqualToString:@"settings"]) { | |
f5a17517 | 9687 | controller = [[[PackageSettingsController alloc] initWithDatabase:database_ package:arg1] autorelease]; |
f6e13561 GP |
9688 | } else if ([arg2 isEqualToString:@"files"]) { |
9689 | if (Package *package = [database_ packageWithName:arg1]) { | |
f5a17517 GP |
9690 | controller = [[[FileTable alloc] initWithDatabase:database_] autorelease]; |
9691 | [(FileTable *)controller setPackage:package]; | |
f6e13561 GP |
9692 | } |
9693 | } | |
c390d3ab | 9694 | } |
55066b9e JF |
9695 | |
9696 | if ([base isEqualToString:@"sections"]) { | |
9697 | Source *source([arg1 isEqualToString:@"*"] ? nil : [database_ sourceWithKey:arg1]); | |
9698 | NSString *section([arg2 isEqualToString:@"*"] ? nil : arg2); | |
9699 | controller = [[[SectionController alloc] initWithDatabase:database_ source:source section:section] autorelease]; | |
9700 | } | |
c390d3ab JF |
9701 | } |
9702 | ||
f5a17517 GP |
9703 | [controller setDelegate:self]; |
9704 | return controller; | |
c390d3ab JF |
9705 | } |
9706 | ||
028dbd1c | 9707 | - (BOOL) openCydiaURL:(NSURL *)url forExternal:(BOOL)external { |
f050e4d9 | 9708 | CyteViewController *page([self pageForURL:url forExternal:external withReferrer:nil]); |
40364973 | 9709 | |
e965092a JF |
9710 | if (page != nil) |
9711 | [tabbar_ setUnselectedViewController:page]; | |
40364973 | 9712 | |
f6e13561 | 9713 | return page != nil; |
40364973 GP |
9714 | } |
9715 | ||
c390d3ab JF |
9716 | - (void) applicationOpenURL:(NSURL *)url { |
9717 | [super applicationOpenURL:url]; | |
d817e4de | 9718 | |
7b33d201 JF |
9719 | if (!loaded_) |
9720 | starturl_ = url; | |
9721 | else | |
9722 | [self openCydiaURL:url forExternal:YES]; | |
c390d3ab JF |
9723 | } |
9724 | ||
bc11cf5b | 9725 | - (void) applicationWillResignActive:(UIApplication *)application { |
7eff7ea6 | 9726 | // Stop refreshing if you get a phone call or lock the device. |
7585ce66 JF |
9727 | if ([tabbar_ updating]) |
9728 | [tabbar_ cancelUpdate]; | |
bc11cf5b | 9729 | |
ca584c15 GP |
9730 | if ([[self superclass] instancesRespondToSelector:@selector(applicationWillResignActive:)]) |
9731 | [super applicationWillResignActive:application]; | |
7eff7ea6 GP |
9732 | } |
9733 | ||
9dd3045d | 9734 | - (void) saveState { |
3d1e6f42 JF |
9735 | [[NSDictionary dictionaryWithObjectsAndKeys: |
9736 | @"InterfaceState", [tabbar_ navigationURLCollection], | |
9737 | @"LastClosed", [NSDate date], | |
9738 | @"InterfaceIndex", [NSNumber numberWithInt:[tabbar_ selectedIndex]], | |
9739 | nil] writeToFile:@ SavedState_ atomically:YES]; | |
fe8e721f GP |
9740 | |
9741 | [self _saveConfig]; | |
9742 | } | |
9743 | ||
9dd3045d JF |
9744 | - (void) applicationWillTerminate:(UIApplication *)application { |
9745 | [self saveState]; | |
9746 | } | |
9747 | ||
d4011d57 | 9748 | - (void) applicationDidEnterBackground:(UIApplication *)application { |
2acc4fa4 JF |
9749 | if (kCFCoreFoundationVersionNumber < 1000 && [self isSafeToSuspend]) |
9750 | return [self terminateWithSuccess]; | |
3d1e6f42 | 9751 | Backgrounded_ = [NSDate date]; |
d4011d57 JF |
9752 | [self saveState]; |
9753 | } | |
9754 | ||
9755 | - (void) applicationWillEnterForeground:(UIApplication *)application { | |
3d1e6f42 | 9756 | if (Backgrounded_ == nil) |
95cd61f0 JF |
9757 | return; |
9758 | ||
3d1e6f42 | 9759 | NSTimeInterval interval([Backgrounded_ timeIntervalSinceNow]); |
95cd61f0 | 9760 | |
6218c58c | 9761 | if (interval <= -(30*60)) { |
888667d5 JF |
9762 | [tabbar_ setSelectedIndex:0]; |
9763 | [[[tabbar_ viewControllers] objectAtIndex:0] popToRootViewControllerAnimated:NO]; | |
9764 | } | |
95cd61f0 | 9765 | |
6218c58c | 9766 | if (interval <= -(15*60)) { |
888667d5 JF |
9767 | if (IsReachable("cydia.saurik.com")) { |
9768 | [tabbar_ beginUpdate]; | |
9769 | [appcache_ reloadURLWithCache:YES]; | |
9770 | } | |
9771 | } | |
f26c90b1 JF |
9772 | |
9773 | if ([database_ delocked]) | |
9774 | [self reloadData]; | |
d4011d57 JF |
9775 | } |
9776 | ||
6915b806 | 9777 | - (void) setConfigurationData:(NSString *)data { |
b4fd1197 | 9778 | static RegEx conffile_r("'(.*)' '(.*)' ([01]) ([01])"); |
6915b806 JF |
9779 | |
9780 | if (!conffile_r(data)) { | |
9781 | lprintf("E:invalid conffile\n"); | |
9782 | return; | |
9783 | } | |
9784 | ||
9785 | NSString *ofile = conffile_r[1]; | |
9786 | //NSString *nfile = conffile_r[2]; | |
9787 | ||
9788 | UIAlertView *alert = [[[UIAlertView alloc] | |
9789 | initWithTitle:UCLocalize("CONFIGURATION_UPGRADE") | |
9790 | message:[NSString stringWithFormat:@"%@\n\n%@", UCLocalize("CONFIGURATION_UPGRADE_EX"), ofile] | |
9791 | delegate:self | |
9792 | cancelButtonTitle:UCLocalize("KEEP_OLD_COPY") | |
9793 | otherButtonTitles: | |
9794 | UCLocalize("ACCEPT_NEW_COPY"), | |
9795 | // XXX: UCLocalize("SEE_WHAT_CHANGED"), | |
9796 | nil | |
9797 | ] autorelease]; | |
9798 | ||
9799 | [alert setContext:@"conffile"]; | |
a08145a8 | 9800 | [alert setNumberOfRows:2]; |
6915b806 JF |
9801 | [alert show]; |
9802 | } | |
9803 | ||
f3e11d24 | 9804 | - (void) addStashController { |
2ef6faad | 9805 | [self lockSuspend]; |
7b33d201 | 9806 | stash_ = [[[StashController alloc] init] autorelease]; |
f3e11d24 GP |
9807 | [window_ addSubview:[stash_ view]]; |
9808 | } | |
9809 | ||
9810 | - (void) removeStashController { | |
9811 | [[stash_ view] removeFromSuperview]; | |
7b33d201 | 9812 | stash_ = nil; |
2ef6faad | 9813 | [self unlockSuspend]; |
f3e11d24 GP |
9814 | } |
9815 | ||
9816 | - (void) stash { | |
9e1f1e91 | 9817 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; |
f3e11d24 | 9818 | UpdateExternalStatus(1); |
0c0a966b | 9819 | [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/free.sh"]; |
f3e11d24 | 9820 | UpdateExternalStatus(0); |
f3e11d24 GP |
9821 | |
9822 | [self removeStashController]; | |
42bbf27d | 9823 | [self reloadSpringBoard]; |
f3e11d24 GP |
9824 | } |
9825 | ||
f6e13561 | 9826 | - (void) setupViewControllers { |
5fe2bcc6 | 9827 | tabbar_ = [[[CydiaTabBarController alloc] initWithDatabase:database_] autorelease]; |
851f4a99 | 9828 | |
6445279b JF |
9829 | NSMutableArray *items; |
9830 | if (kCFCoreFoundationVersionNumber < 800) { | |
9831 | items = [NSMutableArray arrayWithObjects: | |
8dbf3abc JF |
9832 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage imageNamed:@"home.png"] tag:0] autorelease], |
9833 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage imageNamed:@"install.png"] tag:0] autorelease], | |
9834 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage imageNamed:@"changes.png"] tag:0] autorelease], | |
9835 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage imageNamed:@"manage.png"] tag:0] autorelease], | |
9836 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage imageNamed:@"search.png"] tag:0] autorelease], | |
6445279b | 9837 | nil]; |
851f4a99 | 9838 | } else { |
6445279b | 9839 | items = [NSMutableArray arrayWithObjects: |
8dbf3abc JF |
9840 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage imageNamed:@"home7.png"] selectedImage:[UIImage imageNamed:@"home7s.png"]] autorelease], |
9841 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage imageNamed:@"install7.png"] selectedImage:[UIImage imageNamed:@"install7s.png"]] autorelease], | |
9842 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage imageNamed:@"changes7.png"] selectedImage:[UIImage imageNamed:@"changes7s.png"]] autorelease], | |
9843 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage imageNamed:@"manage7.png"] selectedImage:[UIImage imageNamed:@"manage7s.png"]] autorelease], | |
9844 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage imageNamed:@"search7.png"] selectedImage:[UIImage imageNamed:@"search7s.png"]] autorelease], | |
6445279b | 9845 | nil]; |
851f4a99 GP |
9846 | } |
9847 | ||
9848 | NSMutableArray *controllers([NSMutableArray array]); | |
851f4a99 | 9849 | for (UITabBarItem *item in items) { |
15f0d613 | 9850 | UINavigationController *controller([[[UINavigationController alloc] init] autorelease]); |
851f4a99 GP |
9851 | [controller setTabBarItem:item]; |
9852 | [controllers addObject:controller]; | |
9853 | } | |
851f4a99 | 9854 | [tabbar_ setViewControllers:controllers]; |
4305896c | 9855 | |
f6e13561 | 9856 | [tabbar_ setUpdateDelegate:self]; |
851f4a99 GP |
9857 | } |
9858 | ||
968afbcd | 9859 | - (void) _sendMemoryWarningNotification { |
85106ebe JF |
9860 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: maybe 4_0? |
9861 | [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification" object:[UIApplication sharedApplication]]; | |
9862 | else | |
9863 | [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidReceiveMemoryWarningNotification" object:[UIApplication sharedApplication]]; | |
968afbcd JF |
9864 | } |
9865 | ||
9866 | - (void) _sendMemoryWarningNotifications { | |
9867 | while (true) { | |
9868 | [self performSelectorOnMainThread:@selector(_sendMemoryWarningNotification) withObject:nil waitUntilDone:NO]; | |
740f9f75 JF |
9869 | sleep(2); |
9870 | //usleep(2000000); | |
968afbcd JF |
9871 | } |
9872 | } | |
9873 | ||
9256a1ce JF |
9874 | - (void) applicationDidReceiveMemoryWarning:(UIApplication *)application { |
9875 | NSLog(@"--"); | |
9876 | [[NSURLCache sharedURLCache] removeAllCachedResponses]; | |
9877 | } | |
9878 | ||
bd150f54 | 9879 | - (void) applicationDidFinishLaunching:(id)unused { |
968afbcd JF |
9880 | //[NSThread detachNewThreadSelector:@selector(_sendMemoryWarningNotifications) toTarget:self withObject:nil]; |
9881 | ||
7e30ba6d | 9882 | _trace(); |
1e94d48b JF |
9883 | if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) |
9884 | [self setApplicationSupportsShakeToEdit:NO]; | |
9885 | ||
48f1762f JF |
9886 | @synchronized (HostConfig_) { |
9887 | [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]]; | |
9888 | } | |
3171f7fe | 9889 | |
4058b165 JF |
9890 | [NSURLCache setSharedURLCache:[[[CYURLCache alloc] |
9891 | initWithMemoryCapacity:524288 | |
9892 | diskCapacity:10485760 | |
b44af625 | 9893 | diskPath:Cache("SDURLCache") |
4058b165 | 9894 | ] autorelease]]; |
71cc7be1 | 9895 | |
a576488f | 9896 | [CydiaWebViewController _initialize]; |
ea173384 | 9897 | |
bfc87a4d JF |
9898 | [NSURLProtocol registerClass:[CydiaURLProtocol class]]; |
9899 | ||
793aee35 JF |
9900 | // this would disallow http{,s} URLs from accessing this data |
9901 | //[WebView registerURLSchemeAsLocal:@"cydia"]; | |
9902 | ||
7b33d201 JF |
9903 | Font12_ = [UIFont systemFontOfSize:12]; |
9904 | Font12Bold_ = [UIFont boldSystemFontOfSize:12]; | |
9905 | Font14_ = [UIFont systemFontOfSize:14]; | |
2cdc6e57 | 9906 | Font18_ = [UIFont systemFontOfSize:18]; |
7b33d201 JF |
9907 | Font18Bold_ = [UIFont boldSystemFontOfSize:18]; |
9908 | Font22Bold_ = [UIFont boldSystemFontOfSize:22]; | |
f641a0e5 | 9909 | |
7b33d201 JF |
9910 | essential_ = [NSMutableArray arrayWithCapacity:4]; |
9911 | broken_ = [NSMutableArray arrayWithCapacity:4]; | |
bd150f54 | 9912 | |
4e89e880 | 9913 | // XXX: I really need this thing... like, seriously... I'm sorry |
fedd38fe JF |
9914 | appcache_ = [[[AppCacheController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/appcache/", UI_]]] autorelease]; |
9915 | [appcache_ reloadData]; | |
4e89e880 | 9916 | |
7b33d201 | 9917 | window_ = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
f641a0e5 JF |
9918 | [window_ orderFront:self]; |
9919 | [window_ makeKey:self]; | |
c390d3ab | 9920 | [window_ setHidden:NO]; |
04fe1349 | 9921 | |
a7b261d9 JF |
9922 | if (access("/.cydia_no_stash", F_OK) == 0); |
9923 | else { | |
9924 | ||
96ed699d | 9925 | if (false) stash: { |
f3e11d24 | 9926 | [self addStashController]; |
3931b718 JF |
9927 | // XXX: this would be much cleaner as a yieldToSelector: |
9928 | // that way the removeStashController could happen right here inline | |
9929 | // we also could no longer require the useless stash_ field anymore | |
f3e11d24 GP |
9930 | [self performSelector:@selector(stash) withObject:nil afterDelay:0]; |
9931 | return; | |
9932 | } | |
9933 | ||
2656fd54 JF |
9934 | struct stat root; |
9935 | int error(stat("/", &root)); | |
9936 | _assert(error != -1); | |
9937 | ||
96ed699d | 9938 | #define Stash_(path) do { \ |
2656fd54 JF |
9939 | struct stat folder; \ |
9940 | int error(lstat((path), &folder)); \ | |
9941 | if (error != -1 && ( \ | |
9942 | folder.st_dev == root.st_dev && \ | |
9943 | S_ISDIR(folder.st_mode) \ | |
9944 | ) || error == -1 && ( \ | |
9945 | errno == ENOENT || \ | |
9946 | errno == ENOTDIR \ | |
9947 | )) goto stash; \ | |
96ed699d JF |
9948 | } while (false) |
9949 | ||
a8c416fa | 9950 | Stash_("/Applications"); |
96ed699d JF |
9951 | Stash_("/Library/Ringtones"); |
9952 | Stash_("/Library/Wallpaper"); | |
9953 | //Stash_("/usr/bin"); | |
9954 | Stash_("/usr/include"); | |
96ed699d JF |
9955 | Stash_("/usr/share"); |
9956 | //Stash_("/var/lib"); | |
9957 | ||
a7b261d9 JF |
9958 | } |
9959 | ||
770f2a8e | 9960 | database_ = [Database sharedInstance]; |
6915b806 | 9961 | [database_ setDelegate:self]; |
bfc87a4d | 9962 | |
89571a5b | 9963 | [window_ setUserInteractionEnabled:NO]; |
0be165c8 | 9964 | [self setupViewControllers]; |
6915b806 | 9965 | |
9549563e JF |
9966 | CydiaLoadingViewController *loading([[[CydiaLoadingViewController alloc] init] autorelease]); |
9967 | UINavigationController *navigation([[[UINavigationController alloc] init] autorelease]); | |
9968 | [navigation setViewControllers:[NSArray arrayWithObject:loading]]; | |
9969 | ||
9970 | emulated_ = [[[CyteTabBarController alloc] init] autorelease]; | |
9971 | [emulated_ setViewControllers:[NSArray arrayWithObject:navigation]]; | |
9972 | [emulated_ setSelectedIndex:0]; | |
1c28763e JF |
9973 | |
9974 | if ([emulated_ respondsToSelector:@selector(concealTabBarSelection)]) | |
9975 | [emulated_ concealTabBarSelection]; | |
9549563e | 9976 | |
81628115 JF |
9977 | if ([window_ respondsToSelector:@selector(setRootViewController:)]) |
9978 | [window_ setRootViewController:emulated_]; | |
237c3d1c JF |
9979 | else |
9980 | [window_ addSubview:[emulated_ view]]; | |
5ccb47d8 | 9981 | |
fed0d010 | 9982 | [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; |
c626a63f | 9983 | _trace(); |
fed0d010 JF |
9984 | } |
9985 | ||
d3a28a81 GP |
9986 | - (NSArray *) defaultStartPages { |
9987 | NSMutableArray *standard = [NSMutableArray array]; | |
9988 | [standard addObject:[NSArray arrayWithObject:@"cydia://home"]]; | |
55066b9e | 9989 | [standard addObject:[NSArray arrayWithObject:@"cydia://sources"]]; |
d3a28a81 | 9990 | [standard addObject:[NSArray arrayWithObject:@"cydia://changes"]]; |
55066b9e | 9991 | [standard addObject:[NSArray arrayWithObject:@"cydia://installed"]]; |
d3a28a81 GP |
9992 | [standard addObject:[NSArray arrayWithObject:@"cydia://search"]]; |
9993 | return standard; | |
9994 | } | |
9995 | ||
fed0d010 | 9996 | - (void) loadData { |
c626a63f | 9997 | _trace(); |
4121c5e0 JF |
9998 | if ([emulated_ modalViewController] != nil) |
9999 | [emulated_ dismissModalViewControllerAnimated:YES]; | |
10000 | [window_ setUserInteractionEnabled:NO]; | |
fed0d010 | 10001 | |
2925cbba JF |
10002 | [self reloadDataWithInvocation:nil]; |
10003 | [self refreshIfPossible]; | |
2e9123cb | 10004 | [self disemulate]; |
5ccb47d8 | 10005 | |
3d1e6f42 JF |
10006 | NSDictionary *state([NSDictionary dictionaryWithContentsOfFile:@ SavedState_]); |
10007 | ||
10008 | int savedIndex = [[state objectForKey:@"InterfaceIndex"] intValue]; | |
10009 | NSArray *saved = [[[state objectForKey:@"InterfaceState"] mutableCopy] autorelease]; | |
d3a28a81 GP |
10010 | int standardIndex = 0; |
10011 | NSArray *standard = [self defaultStartPages]; | |
fe8e721f | 10012 | |
d3a28a81 GP |
10013 | BOOL valid = YES; |
10014 | ||
10015 | if (saved == nil) | |
10016 | valid = NO; | |
10017 | ||
3d1e6f42 | 10018 | NSDate *closed = [state objectForKey:@"LastClosed"]; |
d3a28a81 | 10019 | if (valid && closed != nil) { |
fe8e721f | 10020 | NSTimeInterval interval([closed timeIntervalSinceNow]); |
ade2267f | 10021 | if (interval <= -(30*60)) |
d3a28a81 | 10022 | valid = NO; |
fe8e721f GP |
10023 | } |
10024 | ||
d3a28a81 GP |
10025 | if (valid && [saved count] != [standard count]) |
10026 | valid = NO; | |
efa53fa9 | 10027 | |
d3a28a81 GP |
10028 | if (valid) { |
10029 | for (unsigned int i = 0; i < [standard count]; i++) { | |
10030 | NSArray *std = [standard objectAtIndex:i], *sav = [saved objectAtIndex:i]; | |
10031 | // XXX: The "hasPrefix" sanity check here could be, in theory, fooled, | |
10032 | // but it's good enough for now. | |
10033 | if ([sav count] == 0 || ![[sav objectAtIndex:0] hasPrefix:[std objectAtIndex:0]]) { | |
10034 | valid = NO; | |
10035 | break; | |
10036 | } | |
fe8e721f | 10037 | } |
fe8e721f GP |
10038 | } |
10039 | ||
d3a28a81 GP |
10040 | NSArray *items = nil; |
10041 | if (valid) { | |
10042 | [tabbar_ setSelectedIndex:savedIndex]; | |
10043 | items = saved; | |
10044 | } else { | |
10045 | [tabbar_ setSelectedIndex:standardIndex]; | |
10046 | items = standard; | |
10047 | } | |
10048 | ||
fe8e721f GP |
10049 | for (unsigned int tab = 0; tab < [[tabbar_ viewControllers] count]; tab++) { |
10050 | NSArray *stack = [items objectAtIndex:tab]; | |
15f0d613 | 10051 | UINavigationController *navigation = [[tabbar_ viewControllers] objectAtIndex:tab]; |
fe8e721f GP |
10052 | NSMutableArray *current = [NSMutableArray array]; |
10053 | ||
10054 | for (unsigned int nav = 0; nav < [stack count]; nav++) { | |
10055 | NSString *addr = [stack objectAtIndex:nav]; | |
10056 | NSURL *url = [NSURL URLWithString:addr]; | |
f050e4d9 | 10057 | CyteViewController *page = [self pageForURL:url forExternal:NO withReferrer:nil]; |
fe8e721f GP |
10058 | if (page != nil) |
10059 | [current addObject:page]; | |
10060 | } | |
10061 | ||
10062 | [navigation setViewControllers:current]; | |
10063 | } | |
f6e13561 | 10064 | |
89571a5b | 10065 | // (Try to) show the startup URL. |
f6e13561 | 10066 | if (starturl_ != nil) { |
f14bba69 | 10067 | [self openCydiaURL:starturl_ forExternal:YES]; |
f6e13561 GP |
10068 | starturl_ = nil; |
10069 | } | |
bd150f54 JF |
10070 | } |
10071 | ||
b5e7eebb | 10072 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { |
c9f3aa21 JF |
10073 | if (!IsWildcat_) { |
10074 | [sheet addButtonWithTitle:UCLocalize("CANCEL")]; | |
10075 | [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; | |
10076 | } | |
10077 | ||
674dce72 | 10078 | if (item != nil && IsWildcat_) { |
b5e7eebb | 10079 | [sheet showFromBarButtonItem:item animated:YES]; |
674dce72 GP |
10080 | } else { |
10081 | [sheet showInView:window_]; | |
10082 | } | |
36bb2ca2 JF |
10083 | } |
10084 | ||
6915b806 JF |
10085 | - (void) addProgressEvent:(CydiaProgressEvent *)event forTask:(NSString *)task { |
10086 | id<ProgressDelegate> progress([database_ progressDelegate] ?: [self invokeNewProgress:nil forController:nil withTitle:task]); | |
10087 | [progress setTitle:task]; | |
10088 | [progress addProgressEvent:event]; | |
10089 | } | |
10090 | ||
10091 | - (void) addProgressEventForTask:(NSArray *)data { | |
10092 | CydiaProgressEvent *event([data objectAtIndex:0]); | |
10093 | NSString *task([data count] < 2 ? nil : [data objectAtIndex:1]); | |
10094 | [self addProgressEvent:event forTask:task]; | |
10095 | } | |
10096 | ||
10097 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task { | |
10098 | [self performSelectorOnMainThread:@selector(addProgressEventForTask:) withObject:[NSArray arrayWithObjects:event, task, nil] waitUntilDone:YES]; | |
10099 | } | |
10100 | ||
dc5812ec JF |
10101 | @end |
10102 | ||
b4d89997 JF |
10103 | /*IMP alloc_; |
10104 | id Alloc_(id self, SEL selector) { | |
10105 | id object = alloc_(self, selector); | |
c390d3ab | 10106 | lprintf("[%s]A-%p\n", self->isa->name, object); |
b4d89997 JF |
10107 | return object; |
10108 | }*/ | |
10109 | ||
36bb2ca2 JF |
10110 | /*IMP dealloc_; |
10111 | id Dealloc_(id self, SEL selector) { | |
10112 | id object = dealloc_(self, selector); | |
c390d3ab | 10113 | lprintf("[%s]D-%p\n", self->isa->name, object); |
36bb2ca2 JF |
10114 | return object; |
10115 | }*/ | |
b4d89997 | 10116 | |
30c5be06 JF |
10117 | Class $NSURLConnection; |
10118 | ||
10119 | MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) { | |
0c4fe0f4 | 10120 | NSMutableURLRequest *copy([[request mutableCopy] autorelease]); |
30c5be06 JF |
10121 | |
10122 | NSURL *url([copy URL]); | |
5e845121 | 10123 | |
30c5be06 | 10124 | NSString *host([url host]); |
e4b48f2f JF |
10125 | NSString *scheme([[url scheme] lowercaseString]); |
10126 | ||
10127 | NSString *compound([NSString stringWithFormat:@"%@:%@", scheme, host]); | |
30c5be06 | 10128 | |
48f1762f JF |
10129 | @synchronized (HostConfig_) { |
10130 | if ([copy respondsToSelector:@selector(setHTTPShouldUsePipelining:)]) | |
10131 | if ([PipelinedHosts_ containsObject:host] || [PipelinedHosts_ containsObject:compound]) | |
10132 | [copy setHTTPShouldUsePipelining:YES]; | |
5e845121 JF |
10133 | |
10134 | if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"]) | |
10135 | if ([control isEqualToString:@"max-age=0"]) | |
3a23d36e | 10136 | if ([CachedURLs_ containsObject:url]) { |
5e845121 | 10137 | #if !ForRelease |
3a23d36e | 10138 | NSLog(@"~~~: %@", url); |
5e845121 JF |
10139 | #endif |
10140 | ||
10141 | [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; | |
10142 | ||
10143 | [copy setValue:nil forHTTPHeaderField:@"Cache-Control"]; | |
10144 | [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"]; | |
10145 | [copy setValue:nil forHTTPHeaderField:@"If-None-Match"]; | |
10146 | } | |
48f1762f | 10147 | } |
30c5be06 JF |
10148 | |
10149 | if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) { | |
10150 | } return self; | |
10151 | } | |
10152 | ||
b1497b56 JF |
10153 | Class $WAKWindow; |
10154 | ||
4cc9e99a | 10155 | static CGSize $WAKWindow$screenSize(WAKWindow *self, SEL _cmd) { |
b1497b56 JF |
10156 | CGSize size([[UIScreen mainScreen] bounds].size); |
10157 | /*if ([$WAKWindow respondsToSelector:@selector(hasLandscapeOrientation)]) | |
10158 | if ([$WAKWindow hasLandscapeOrientation]) | |
10159 | std::swap(size.width, size.height);*/ | |
10160 | return size; | |
438d6708 JF |
10161 | } |
10162 | ||
29cbf4e5 JF |
10163 | Class $NSUserDefaults; |
10164 | ||
10165 | MSHook(id, NSUserDefaults$objectForKey$, NSUserDefaults *self, SEL _cmd, NSString *key) { | |
10166 | if ([key respondsToSelector:@selector(isEqualToString:)] && [key isEqualToString:@"WebKitLocalStorageDatabasePathPreferenceKey"]) | |
b44af625 | 10167 | return Cache("LocalStorage"); |
29cbf4e5 JF |
10168 | return _NSUserDefaults$objectForKey$(self, _cmd, key); |
10169 | } | |
10170 | ||
9adfb865 JF |
10171 | static NSMutableDictionary *AutoreleaseDeepMutableCopyOfDictionary(CFTypeRef type) { |
10172 | if (type == NULL) | |
10173 | return nil; | |
10174 | if (CFGetTypeID(type) != CFDictionaryGetTypeID()) | |
10175 | return nil; | |
10176 | CFTypeRef copy(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, type, kCFPropertyListMutableContainers)); | |
10177 | CFRelease(type); | |
10178 | return [(NSMutableDictionary *) copy autorelease]; | |
10179 | } | |
10180 | ||
d13edf44 | 10181 | int main(int argc, char *argv[]) { |
db1e5e0d JF |
10182 | int fd(open("/tmp/cydia.log", O_WRONLY | O_APPEND | O_CREAT, 0644)); |
10183 | dup2(fd, 2); | |
10184 | close(fd); | |
10185 | ||
d13edf44 JF |
10186 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
10187 | ||
d6dad1b4 | 10188 | _trace(); |
f79a4512 | 10189 | |
9a60abe5 JF |
10190 | UpdateExternalStatus(0); |
10191 | ||
57e8b225 JF |
10192 | UIScreen *screen([UIScreen mainScreen]); |
10193 | if ([screen respondsToSelector:@selector(scale)]) | |
10194 | ScreenScale_ = [screen scale]; | |
10195 | else | |
10196 | ScreenScale_ = 1; | |
10197 | ||
c138614d | 10198 | UIDevice *device([UIDevice currentDevice]); |
08157100 | 10199 | if ([device respondsToSelector:@selector(userInterfaceIdiom)]) { |
c138614d | 10200 | UIUserInterfaceIdiom idiom([device userInterfaceIdiom]); |
08157100 JF |
10201 | if (idiom == UIUserInterfaceIdiomPad) |
10202 | IsWildcat_ = true; | |
c138614d JF |
10203 | } |
10204 | ||
08157100 JF |
10205 | Idiom_ = IsWildcat_ ? @"ipad" : @"iphone"; |
10206 | ||
b4fd1197 | 10207 | RegEx pattern("([0-9]+\\.[0-9]+).*"); |
fd825a2d | 10208 | |
9a4a4754 JF |
10209 | if (pattern([device systemVersion])) |
10210 | Firmware_ = pattern[1]; | |
fd825a2d JF |
10211 | if (pattern(Cydia_)) |
10212 | Major_ = pattern[1]; | |
9a4a4754 | 10213 | |
7b33d201 | 10214 | SessionData_ = [NSMutableDictionary dictionaryWithCapacity:4]; |
ef974f52 | 10215 | |
7b33d201 | 10216 | HostConfig_ = [[[NSObject alloc] init] autorelease]; |
48f1762f JF |
10217 | @synchronized (HostConfig_) { |
10218 | BridgedHosts_ = [NSMutableSet setWithCapacity:4]; | |
2e1652a9 | 10219 | InsecureHosts_ = [NSMutableSet setWithCapacity:4]; |
48f1762f | 10220 | PipelinedHosts_ = [NSMutableSet setWithCapacity:4]; |
5e845121 | 10221 | CachedURLs_ = [NSMutableSet setWithCapacity:32]; |
48f1762f | 10222 | } |
5df7ecfb | 10223 | |
de595d91 JF |
10224 | NSString *ui(@"ui/ios"); |
10225 | if (Idiom_ != nil) | |
10226 | ui = [ui stringByAppendingString:[NSString stringWithFormat:@"~%@", Idiom_]]; | |
fd825a2d | 10227 | ui = [ui stringByAppendingString:[NSString stringWithFormat:@"/%@", Major_]]; |
de595d91 | 10228 | UI_ = CydiaURL(ui); |
57e8b225 | 10229 | |
df213583 | 10230 | PackageName = reinterpret_cast<CYString &(*)(Package *, SEL)>(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); |
677b8415 | 10231 | |
7376b55c JF |
10232 | /* Library Hacks {{{ */ |
10233 | class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); | |
10234 | ||
b1497b56 JF |
10235 | $WAKWindow = objc_getClass("WAKWindow"); |
10236 | if ($WAKWindow != NULL) | |
438d6708 JF |
10237 | if (Method method = class_getInstanceMethod($WAKWindow, @selector(screenSize))) |
10238 | method_setImplementation(method, (IMP) &$WAKWindow$screenSize); | |
10239 | ||
30c5be06 JF |
10240 | $NSURLConnection = objc_getClass("NSURLConnection"); |
10241 | Method NSURLConnection$init$(class_getInstanceMethod($NSURLConnection, @selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:))); | |
10242 | if (NSURLConnection$init$ != NULL) { | |
10243 | _NSURLConnection$init$ = reinterpret_cast<id (*)(NSURLConnection *, SEL, NSURLRequest *, id, BOOL, int64_t, BOOL, NSDictionary *)>(method_getImplementation(NSURLConnection$init$)); | |
10244 | method_setImplementation(NSURLConnection$init$, reinterpret_cast<IMP>(&$NSURLConnection$init$)); | |
10245 | } | |
29cbf4e5 JF |
10246 | |
10247 | $NSUserDefaults = objc_getClass("NSUserDefaults"); | |
10248 | Method NSUserDefaults$objectForKey$(class_getInstanceMethod($NSUserDefaults, @selector(objectForKey:))); | |
10249 | if (NSUserDefaults$objectForKey$ != NULL) { | |
10250 | _NSUserDefaults$objectForKey$ = reinterpret_cast<id (*)(NSUserDefaults *, SEL, NSString *)>(method_getImplementation(NSUserDefaults$objectForKey$)); | |
10251 | method_setImplementation(NSUserDefaults$objectForKey$, reinterpret_cast<IMP>(&$NSUserDefaults$objectForKey$)); | |
10252 | } | |
7376b55c JF |
10253 | /* }}} */ |
10254 | /* Set Locale {{{ */ | |
f79a4512 | 10255 | Locale_ = CFLocaleCopyCurrent(); |
b1ce61ec | 10256 | Languages_ = [NSLocale preferredLanguages]; |
25fdc941 | 10257 | |
b1ce61ec | 10258 | //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); |
18876387 | 10259 | //NSLog(@"%@", [Languages_ description]); |
78430d06 | 10260 | |
b1ce61ec | 10261 | const char *lang; |
25fdc941 JF |
10262 | if (Locale_ != NULL) |
10263 | lang = [(NSString *) CFLocaleGetIdentifier(Locale_) UTF8String]; | |
3caee0a4 JF |
10264 | else if (Languages_ != nil && [Languages_ count] != 0) |
10265 | lang = [[Languages_ objectAtIndex:0] UTF8String]; | |
10266 | else | |
78430d06 | 10267 | // XXX: consider just setting to C and then falling through? |
b1ce61ec | 10268 | lang = NULL; |
3caee0a4 JF |
10269 | |
10270 | if (lang != NULL) { | |
b4fd1197 | 10271 | RegEx pattern("([a-z][a-z])(?:-[A-Za-z]*)?(_[A-Z][A-Z])?"); |
3caee0a4 | 10272 | lang = !pattern(lang) ? NULL : [pattern->*@"%1$@%2$@" UTF8String]; |
78430d06 JF |
10273 | } |
10274 | ||
b1ce61ec | 10275 | NSLog(@"Setting Language: %s", lang); |
3caee0a4 JF |
10276 | |
10277 | if (lang != NULL) { | |
10278 | setenv("LANG", lang, true); | |
10279 | std::setlocale(LC_ALL, lang); | |
10280 | } | |
7376b55c | 10281 | /* }}} */ |
aeeb755b | 10282 | /* Index Collation {{{ */ |
42e25bc5 | 10283 | if (Class $UILocalizedIndexedCollation = objc_getClass("UILocalizedIndexedCollation")) { @try { |
aeeb755b JF |
10284 | NSBundle *bundle([NSBundle bundleForClass:$UILocalizedIndexedCollation]); |
10285 | NSString *path([bundle pathForResource:@"UITableViewLocalizedSectionIndex" ofType:@"plist"]); | |
10286 | //path = @"/System/Library/Frameworks/UIKit.framework/.lproj/UITableViewLocalizedSectionIndex.plist"; | |
10287 | NSDictionary *dictionary([NSDictionary dictionaryWithContentsOfFile:path]); | |
1c28763e | 10288 | _H<UILocalizedIndexedCollation> collation([[[$UILocalizedIndexedCollation alloc] initWithDictionary:dictionary] autorelease]); |
aeeb755b JF |
10289 | |
10290 | CollationLocale_ = MSHookIvar<NSLocale *>(collation, "_locale"); | |
10291 | ||
f93f4997 JF |
10292 | if (kCFCoreFoundationVersionNumber >= 800 && [[CollationLocale_ localeIdentifier] isEqualToString:@"zh@collation=stroke"]) { |
10293 | CollationThumbs_ = [NSArray arrayWithObjects:@"1",@"•",@"4",@"•",@"7",@"•",@"10",@"•",@"13",@"•",@"16",@"•",@"19",@"A",@"•",@"E",@"•",@"I",@"•",@"M",@"•",@"R",@"•",@"V",@"•",@"Z",@"#",nil]; | |
10294 | 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}) | |
10295 | CollationOffset_.push_back(offset); | |
10296 | 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]; | |
10297 | 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]; | |
10298 | } else { | |
10299 | ||
aeeb755b JF |
10300 | CollationThumbs_ = [collation sectionIndexTitles]; |
10301 | for (size_t index(0), end([CollationThumbs_ count]); index != end; ++index) | |
10302 | CollationOffset_.push_back([collation sectionForSectionIndexTitleAtIndex:index]); | |
10303 | ||
10304 | CollationTitles_ = [collation sectionTitles]; | |
10305 | CollationStarts_ = MSHookIvar<NSArray *>(collation, "_sectionStartStrings"); | |
10306 | ||
22fd24dd JF |
10307 | NSString *&transform(MSHookIvar<NSString *>(collation, "_transform")); |
10308 | if (&transform != NULL && transform != nil) { | |
10309 | /*if ([collation respondsToSelector:@selector(transformedCollationStringForString:)]) | |
10310 | CollationModify_ = [=](NSString *value) { return [collation transformedCollationStringForString:value]; };*/ | |
10311 | const UChar *uid(reinterpret_cast<const UChar *>([transform cStringUsingEncoding:NSUnicodeStringEncoding])); | |
10312 | UErrorCode code(U_ZERO_ERROR); | |
10313 | CollationTransl_ = utrans_openU(uid, -1, UTRANS_FORWARD, NULL, 0, NULL, &code); | |
10314 | if (!U_SUCCESS(code)) | |
10315 | NSLog(@"%s", u_errorName(code)); | |
10316 | } | |
f93f4997 JF |
10317 | |
10318 | } | |
42e25bc5 JF |
10319 | } @catch (NSException *e) { |
10320 | NSLog(@"%@", e); | |
10321 | goto hard; | |
10322 | } } else hard: { | |
aeeb755b JF |
10323 | CollationLocale_ = [[[NSLocale alloc] initWithLocaleIdentifier:@"en@collation=dictionary"] autorelease]; |
10324 | ||
10325 | 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]; | |
10326 | for (NSInteger offset(0); offset != 28; ++offset) | |
10327 | CollationOffset_.push_back(offset); | |
10328 | ||
10329 | 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]; | |
10330 | 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]; | |
10331 | } | |
10332 | /* }}} */ | |
7376b55c | 10333 | /* Parse Arguments {{{ */ |
de3b1ab4 JF |
10334 | bool substrate(false); |
10335 | ||
10336 | if (argc != 0) { | |
10337 | char **args(argv); | |
10338 | int arge(1); | |
10339 | ||
10340 | for (int argi(1); argi != argc; ++argi) | |
10341 | if (strcmp(argv[argi], "--") == 0) { | |
10342 | arge = argi; | |
10343 | argv[argi] = argv[0]; | |
10344 | argv += argi; | |
10345 | argc -= argi; | |
10346 | break; | |
10347 | } | |
10348 | ||
10349 | for (int argi(1); argi != arge; ++argi) | |
d791dce4 | 10350 | if (strcmp(args[argi], "--substrate") == 0) |
de3b1ab4 JF |
10351 | substrate = true; |
10352 | else | |
10353 | fprintf(stderr, "unknown argument: %s\n", args[argi]); | |
10354 | } | |
7376b55c | 10355 | /* }}} */ |
d73cede2 | 10356 | |
7376b55c | 10357 | App_ = [[NSBundle mainBundle] bundlePath]; |
d791dce4 | 10358 | Advanced_ = YES; |
7376b55c | 10359 | |
b44af625 | 10360 | Cache_ = [[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia", @"/var/mobile"] retain]; |
57df20ac | 10361 | mkdir([Cache_ UTF8String], 0755); |
d1c7f1fd | 10362 | |
b4d89997 JF |
10363 | /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); |
10364 | alloc_ = alloc->method_imp; | |
10365 | alloc->method_imp = (IMP) &Alloc_;*/ | |
10366 | ||
36bb2ca2 JF |
10367 | /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc)); |
10368 | dealloc_ = dealloc->method_imp; | |
10369 | dealloc->method_imp = (IMP) &Dealloc_;*/ | |
10370 | ||
7c80833f JF |
10371 | void *gestalt(dlopen("/usr/lib/libMobileGestalt.dylib", RTLD_GLOBAL | RTLD_LAZY)); |
10372 | $MGCopyAnswer = reinterpret_cast<CFStringRef (*)(CFStringRef)>(dlsym(gestalt, "MGCopyAnswer")); | |
10373 | ||
d791dce4 | 10374 | /* System Information {{{ */ |
3178d79b | 10375 | size_t size; |
c390d3ab JF |
10376 | |
10377 | int maxproc; | |
10378 | size = sizeof(maxproc); | |
10379 | if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) | |
10380 | perror("sysctlbyname(\"kern.maxproc\", ?)"); | |
10381 | else if (maxproc < 64) { | |
10382 | maxproc = 64; | |
10383 | if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) | |
10384 | perror("sysctlbyname(\"kern.maxproc\", #)"); | |
10385 | } | |
10386 | ||
bfc87a4d JF |
10387 | sysctlbyname("kern.osversion", NULL, &size, NULL, 0); |
10388 | char *osversion = new char[size]; | |
10389 | if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1) | |
10390 | perror("sysctlbyname(\"kern.osversion\", ?)"); | |
10391 | else | |
10392 | System_ = [NSString stringWithUTF8String:osversion]; | |
10393 | ||
3178d79b JF |
10394 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
10395 | char *machine = new char[size]; | |
c390d3ab JF |
10396 | if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1) |
10397 | perror("sysctlbyname(\"hw.machine\", ?)"); | |
10398 | else | |
10399 | Machine_ = machine; | |
3178d79b | 10400 | |
8dcc32d4 | 10401 | int64_t usermem(0); |
ce1901de JF |
10402 | size = sizeof(usermem); |
10403 | if (sysctlbyname("hw.usermem", &usermem, &size, NULL, 0) == -1) | |
10404 | usermem = 0; | |
10405 | ||
c31d7cdc JF |
10406 | SerialNumber_ = (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber"); |
10407 | ChipID_ = [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString]; | |
10408 | BBSNum_ = CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false); | |
59dbe296 | 10409 | |
7c80833f | 10410 | UniqueID_ = UniqueIdentifier(device); |
3178d79b | 10411 | |
3e9c9e85 JF |
10412 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) { |
10413 | Product_ = [info objectForKey:@"SafariProductVersion"]; | |
10414 | Safari_ = [info objectForKey:@"CFBundleVersion"]; | |
10415 | } | |
e967efd5 | 10416 | |
dd58e110 | 10417 | NSString *agent([NSString stringWithFormat:@"Cydia/%@ CyF/%.2f", Cydia_, kCFCoreFoundationVersionNumber]); |
e967efd5 | 10418 | |
b4fd1197 JF |
10419 | if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Safari_)) |
10420 | agent = [NSString stringWithFormat:@"Safari/%@ %@", match[1], agent]; | |
10421 | if (RegEx match = RegEx("([0-9]+[A-Z][0-9]+[a-z]?).*", System_)) | |
10422 | agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[1], agent]; | |
10423 | if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Product_)) | |
10424 | agent = [NSString stringWithFormat:@"Version/%@ %@", match[1], agent]; | |
e967efd5 JF |
10425 | |
10426 | UserAgent_ = agent; | |
d791dce4 | 10427 | /* }}} */ |
7376b55c | 10428 | /* Load Database {{{ */ |
4e479350 JF |
10429 | SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; |
10430 | ||
d6dad1b4 | 10431 | _trace(); |
4e479350 JF |
10432 | mkdir("/var/mobile/Library/Cydia", 0755); |
10433 | MetaFile_.Open("/var/mobile/Library/Cydia/metadata.cb0"); | |
f79a4512 | 10434 | _trace(); |
d6dad1b4 | 10435 | |
9adfb865 JF |
10436 | Values_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaValues"), CFSTR("com.saurik.Cydia"))); |
10437 | Sections_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaSections"), CFSTR("com.saurik.Cydia"))); | |
10438 | Sources_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaSources"), CFSTR("com.saurik.Cydia"))); | |
4e479350 | 10439 | Version_ = [(NSNumber *) CFPreferencesCopyAppValue(CFSTR("CydiaVersion"), CFSTR("com.saurik.Cydia")) autorelease]; |
ef055c6c | 10440 | |
4e479350 JF |
10441 | _trace(); |
10442 | NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]); | |
7b0ce2da | 10443 | |
4e479350 JF |
10444 | if (Values_ == nil) |
10445 | Values_ = [metadata objectForKey:@"Values"]; | |
10446 | if (Values_ == nil) | |
b3c8e69c | 10447 | Values_ = [[[NSMutableDictionary alloc] initWithCapacity:4] autorelease]; |
b3c8e69c | 10448 | |
4e479350 JF |
10449 | if (Sections_ == nil) |
10450 | Sections_ = [metadata objectForKey:@"Sections"]; | |
10451 | if (Sections_ == nil) | |
7b0ce2da | 10452 | Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; |
7b0ce2da | 10453 | |
4e479350 JF |
10454 | if (Sources_ == nil) |
10455 | Sources_ = [metadata objectForKey:@"Sources"]; | |
10456 | if (Sources_ == nil) | |
7b0ce2da | 10457 | Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; |
33e30380 | 10458 | |
4e479350 JF |
10459 | // XXX: this wrong, but in a way that doesn't matter :/ |
10460 | if (Version_ == nil) | |
10461 | Version_ = [metadata objectForKey:@"Version"]; | |
10462 | if (Version_ == nil) | |
33e30380 | 10463 | Version_ = [NSNumber numberWithUnsignedInt:0]; |
4e479350 JF |
10464 | |
10465 | if (NSDictionary *packages = [metadata objectForKey:@"Packages"]) { | |
10466 | bool fail(false); | |
10467 | CFDictionaryApplyFunction((CFDictionaryRef) packages, &PackageImport, &fail); | |
10468 | _trace(); | |
10469 | if (fail) | |
10470 | NSLog(@"unable to import package preferences... from 2010? oh well :/"); | |
33e30380 JF |
10471 | } |
10472 | ||
33e30380 | 10473 | if ([Version_ unsignedIntValue] == 0) { |
25c1dafb JF |
10474 | CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]); |
10475 | CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]); | |
10476 | CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]); | |
10477 | CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./"); | |
33e30380 JF |
10478 | |
10479 | Version_ = [NSNumber numberWithUnsignedInt:1]; | |
33e30380 | 10480 | |
6f87c61a JF |
10481 | if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:@ CacheState_]) { |
10482 | [cache removeObjectForKey:@"LastUpdate"]; | |
10483 | [cache writeToFile:@ CacheState_ atomically:YES]; | |
10484 | } | |
33e30380 | 10485 | } |
2595e4c3 JF |
10486 | |
10487 | _H<NSMutableArray> broken([NSMutableArray array]); | |
10488 | for (NSString *key in (id) Sources_) | |
d93e3f22 | 10489 | if ([key rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"# "]].location != NSNotFound || ![([[Sources_ objectForKey:key] objectForKey:@"URI"] ?: @"/") hasSuffix:@"/"]) |
2595e4c3 | 10490 | [broken addObject:key]; |
4e479350 | 10491 | if ([broken count] != 0) |
2595e4c3 JF |
10492 | for (NSString *key in (id) broken) |
10493 | [Sources_ removeObjectForKey:key]; | |
4e479350 | 10494 | broken = nil; |
94b0b3e5 | 10495 | |
4e479350 JF |
10496 | SaveConfig(nil); |
10497 | system("/usr/libexec/cydia/cydo /bin/rm -f /var/lib/cydia/metadata.plist"); | |
10498 | /* }}} */ | |
94b0b3e5 | 10499 | |
d791dce4 JF |
10500 | Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; |
10501 | ||
6c9fe9af | 10502 | if (kCFCoreFoundationVersionNumber > 1000) |
0c0a966b | 10503 | system("/usr/libexec/cydia/cydo /usr/libexec/cydia/setnsfpn /var/lib"); |
6c9fe9af | 10504 | |
01d93940 JF |
10505 | int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); |
10506 | ||
ad502f71 | 10507 | if (access("/User", F_OK) != 0 || version != 6) { |
d6dad1b4 | 10508 | _trace(); |
0c0a966b | 10509 | system("/usr/libexec/cydia/cydo /usr/libexec/cydia/firmware.sh"); |
d6dad1b4 JF |
10510 | _trace(); |
10511 | } | |
9e98e020 | 10512 | |
7376b55c | 10513 | if (access("/tmp/cydia.chk", F_OK) == 0) { |
b44af625 | 10514 | if (unlink([Cache("pkgcache.bin") UTF8String]) == -1) |
7376b55c | 10515 | _assert(errno == ENOENT); |
b44af625 | 10516 | if (unlink([Cache("srcpkgcache.bin") UTF8String]) == -1) |
7376b55c JF |
10517 | _assert(errno == ENOENT); |
10518 | } | |
10519 | ||
19cd1e0d JF |
10520 | system("/usr/libexec/cydia/cydo /bin/ln -sf /var/mobile/Library/Caches/com.saurik.Cydia/sources.list /etc/apt/sources.list.d/cydia.list"); |
10521 | ||
59dbe296 | 10522 | /* APT Initialization {{{ */ |
b1ce61ec JF |
10523 | _assert(pkgInitConfig(*_config)); |
10524 | _assert(pkgInitSystem(*_config, _system)); | |
10525 | ||
10526 | if (lang != NULL) | |
10527 | _config->Set("APT::Acquire::Translation", lang); | |
cb94ff21 JF |
10528 | |
10529 | // XXX: this timeout might be important :( | |
10530 | //_config->Set("Acquire::http::Timeout", 15); | |
10531 | ||
ce1901de | 10532 | _config->Set("Acquire::http::MaxParallel", usermem >= 384 * 1024 * 1024 ? 16 : 3); |
b44af625 | 10533 | |
b44af625 JF |
10534 | mkdir([Cache("archives") UTF8String], 0755); |
10535 | mkdir([Cache("archives/partial") UTF8String], 0755); | |
10536 | _config->Set("Dir::Cache", [Cache_ UTF8String]); | |
10537 | ||
49cc457a JF |
10538 | symlink("/var/lib/apt/extended_states", [Cache("extended_states") UTF8String]); |
10539 | _config->Set("Dir::State", [Cache_ UTF8String]); | |
10540 | ||
b44af625 JF |
10541 | mkdir([Cache("lists") UTF8String], 0755); |
10542 | mkdir([Cache("lists/partial") UTF8String], 0755); | |
10543 | mkdir([Cache("periodic") UTF8String], 0755); | |
e6446ca0 | 10544 | _config->Set("Dir::State::Lists", [Cache("lists") UTF8String]); |
232b396b JF |
10545 | |
10546 | std::string logs("/var/mobile/Library/Logs/Cydia"); | |
10547 | mkdir(logs.c_str(), 0755); | |
10548 | _config->Set("Dir::Log::Terminal", logs + "/apt.log"); | |
10549 | ||
10550 | _config->Set("Dir::Bin::dpkg", "/usr/libexec/cydia/cydo"); | |
59dbe296 | 10551 | /* }}} */ |
7376b55c | 10552 | /* Color Choices {{{ */ |
36bb2ca2 JF |
10553 | space_ = CGColorSpaceCreateDeviceRGB(); |
10554 | ||
f641a0e5 | 10555 | Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0); |
77fcccaf | 10556 | Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0); |
36bb2ca2 | 10557 | Black_.Set(space_, 0.0, 0.0, 0.0, 1.0); |
86a333c6 | 10558 | Folder_.Set(space_, 0x8e/255.f, 0x8e/255.f, 0x93/255.f, 1.0); |
baf80942 | 10559 | Off_.Set(space_, 0.9, 0.9, 0.9, 1.0); |
36bb2ca2 | 10560 | White_.Set(space_, 1.0, 1.0, 1.0, 1.0); |
7b0ce2da | 10561 | Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0); |
3bd1c2a2 JF |
10562 | Green_.Set(space_, 0.0, 0.5, 0.0, 1.0); |
10563 | Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0); | |
10564 | Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0); | |
59dbe296 | 10565 | |
dc63e78f JF |
10566 | InstallingColor_ = [UIColor colorWithRed:0.88f green:1.00f blue:0.88f alpha:1.00f]; |
10567 | RemovingColor_ = [UIColor colorWithRed:1.00f green:0.88f blue:0.88f alpha:1.00f]; | |
7376b55c | 10568 | /* }}}*/ |
7376b55c | 10569 | /* UIKit Configuration {{{ */ |
600d005d JF |
10570 | // XXX: I have a feeling this was important |
10571 | //UIKeyboardDisableAutomaticAppearance(); | |
7376b55c | 10572 | /* }}} */ |
87f46a96 | 10573 | |
8a3b565c | 10574 | $SBSSetInterceptsMenuButtonForever = reinterpret_cast<void (*)(bool)>(dlsym(RTLD_DEFAULT, "SBSSetInterceptsMenuButtonForever")); |
c7e78d5f | 10575 | $SBSCopyIconImagePNGDataForDisplayIdentifier = reinterpret_cast<NSData *(*)(NSString *)>(dlsym(RTLD_DEFAULT, "SBSCopyIconImagePNGDataForDisplayIdentifier")); |
8a3b565c | 10576 | |
b37b0a4a JF |
10577 | const char *symbol(kCFCoreFoundationVersionNumber >= 800 ? "MGGetBoolAnswer" : "GSSystemHasCapability"); |
10578 | BOOL (*GSSystemHasCapability)(CFStringRef) = reinterpret_cast<BOOL (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, symbol)); | |
c73d524b JF |
10579 | bool fast = GSSystemHasCapability != NULL && GSSystemHasCapability(CFSTR("armv7")); |
10580 | ||
c73d524b | 10581 | PulseInterval_ = fast ? 50000 : 500000; |
8731fdb0 | 10582 | |
670a0494 | 10583 | Colon_ = UCLocalize("COLON_DELIMITED"); |
72fb3616 | 10584 | Elision_ = UCLocalize("ELISION"); |
670a0494 JF |
10585 | Error_ = UCLocalize("ERROR"); |
10586 | Warning_ = UCLocalize("WARNING"); | |
10587 | ||
d6dad1b4 | 10588 | _trace(); |
77df4f82 | 10589 | int value(UIApplicationMain(argc, argv, @"Cydia", @"Cydia")); |
36bb2ca2 JF |
10590 | |
10591 | CGColorSpaceRelease(space_); | |
199d0ba5 | 10592 | CFRelease(Locale_); |
36bb2ca2 | 10593 | |
d13edf44 | 10594 | [pool release]; |
36bb2ca2 | 10595 | return value; |
6d166849 | 10596 | } |