]>
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 JF |
2159 | |
2160 | - (BOOL) valid; | |
31f3cfff | 2161 | - (BOOL) upgradableAndEssential:(BOOL)essential; |
36bb2ca2 JF |
2162 | - (BOOL) essential; |
2163 | - (BOOL) broken; | |
7d2ac47f | 2164 | - (BOOL) unfiltered; |
6d9712c4 | 2165 | - (BOOL) visible; |
b4d89997 | 2166 | |
9bedffaa JF |
2167 | - (BOOL) half; |
2168 | - (BOOL) halfConfigured; | |
2169 | - (BOOL) halfInstalled; | |
2170 | - (BOOL) hasMode; | |
2171 | - (NSString *) mode; | |
2172 | ||
36bb2ca2 JF |
2173 | - (NSString *) id; |
2174 | - (NSString *) name; | |
770f2a8e | 2175 | - (UIImage *) icon; |
6f1a15d9 | 2176 | - (NSString *) homepage; |
25a2158d | 2177 | - (NSString *) depiction; |
7aa82ca2 | 2178 | - (MIMEAddress *) author; |
b4d89997 | 2179 | |
dc63e78f JF |
2180 | - (NSString *) support; |
2181 | ||
affeffc7 | 2182 | - (NSArray *) files; |
affeffc7 JF |
2183 | - (NSArray *) warnings; |
2184 | - (NSArray *) applications; | |
2388b078 | 2185 | |
36bb2ca2 | 2186 | - (Source *) source; |
b4d89997 | 2187 | |
9c5737d5 | 2188 | - (uint32_t) rank; |
d84597fe | 2189 | - (BOOL) matches:(NSArray *)query; |
b4d89997 | 2190 | |
6d9712c4 | 2191 | - (BOOL) hasTag:(NSString *)tag; |
c390d3ab | 2192 | - (NSString *) primaryPurpose; |
770f2a8e | 2193 | - (NSArray *) purposes; |
3bd1c2a2 | 2194 | - (bool) isCommercial; |
6d9712c4 | 2195 | |
cd95e390 JF |
2196 | - (void) setIndex:(size_t)index; |
2197 | ||
df213583 JF |
2198 | - (CYString &) cyname; |
2199 | ||
f79a4512 | 2200 | - (uint32_t) compareBySection:(NSArray *)sections; |
807ae6d7 | 2201 | |
36bb2ca2 JF |
2202 | - (void) install; |
2203 | - (void) remove; | |
06aa974d | 2204 | |
36bb2ca2 | 2205 | @end |
b4d89997 | 2206 | |
f79a4512 JF |
2207 | uint32_t PackageChangesRadix(Package *self, void *) { |
2208 | union { | |
2209 | uint32_t key; | |
2210 | ||
2211 | struct { | |
2212 | uint32_t timestamp : 30; | |
2213 | uint32_t ignored : 1; | |
2214 | uint32_t upgradable : 1; | |
2215 | } bits; | |
2216 | } value; | |
2217 | ||
2218 | bool upgradable([self upgradableAndEssential:YES]); | |
2219 | value.bits.upgradable = upgradable ? 1 : 0; | |
2220 | ||
2221 | if (upgradable) { | |
2222 | value.bits.timestamp = 0; | |
2223 | value.bits.ignored = [self ignored] ? 0 : 1; | |
2224 | value.bits.upgradable = 1; | |
2225 | } else { | |
36047c66 | 2226 | value.bits.timestamp = [self seen] >> 2; |
f79a4512 JF |
2227 | value.bits.ignored = 0; |
2228 | value.bits.upgradable = 0; | |
2229 | } | |
2230 | ||
2231 | return _not(uint32_t) - value.key; | |
2232 | } | |
2233 | ||
aeeb755b JF |
2234 | CYString &(*PackageName)(Package *self, SEL sel); |
2235 | ||
df213583 JF |
2236 | uint32_t PackagePrefixRadix(Package *self, void *context) { |
2237 | size_t offset(reinterpret_cast<size_t>(context)); | |
aeeb755b | 2238 | CYString &name(PackageName(self, @selector(cyname))); |
677b8415 | 2239 | |
df213583 JF |
2240 | size_t size(name.size()); |
2241 | if (size == 0) | |
2242 | return 0; | |
2243 | char *text(name.data()); | |
677b8415 | 2244 | |
df213583 JF |
2245 | size_t zeros; |
2246 | if (!isdigit(text[0])) | |
2247 | zeros = 0; | |
2248 | else { | |
2249 | size_t digits(1); | |
2250 | while (size != digits && isdigit(text[digits])) | |
2251 | if (++digits == 4) | |
2252 | break; | |
2253 | zeros = 4 - digits; | |
2254 | } | |
677b8415 | 2255 | |
df213583 JF |
2256 | uint8_t data[4]; |
2257 | ||
df213583 JF |
2258 | if (offset == 0 && zeros != 0) { |
2259 | memset(data, '0', zeros); | |
2260 | memcpy(data + zeros, text, 4 - zeros); | |
2261 | } else { | |
2262 | /* XXX: there's some danger here if you request a non-zero offset < 4 and it gets zero padded */ | |
2263 | if (size <= offset - zeros) | |
2264 | return 0; | |
2265 | ||
2266 | text += offset - zeros; | |
2267 | size -= offset - zeros; | |
2268 | ||
2269 | if (size >= 4) | |
2270 | memcpy(data, text, 4); | |
2271 | else { | |
2272 | memcpy(data, text, size); | |
2273 | memset(data + size, 0, 4 - size); | |
2274 | } | |
2275 | ||
2276 | for (size_t i(0); i != 4; ++i) | |
2277 | if (isalpha(data[i])) | |
a7a59ee1 | 2278 | data[i] |= 0x20; |
df213583 JF |
2279 | } |
2280 | ||
2281 | if (offset == 0) | |
a7a59ee1 JF |
2282 | if (data[0] == '@') |
2283 | data[0] = 0x7f; | |
2284 | else | |
2285 | data[0] = (data[0] & 0x1f) | "\x80\x00\xc0\x40"[data[0] >> 6]; | |
df213583 JF |
2286 | |
2287 | /* XXX: ntohl may be more honest */ | |
2288 | return OSSwapInt32(*reinterpret_cast<uint32_t *>(data)); | |
2289 | } | |
2290 | ||
22fd24dd | 2291 | CFComparisonResult StringNameCompare(CFStringRef lhn, CFStringRef rhn, size_t length) { |
df213583 | 2292 | _profile(PackageNameCompare) |
5358f56f | 2293 | if (lhn == NULL) |
b129e6d9 | 2294 | return rhn == NULL ? kCFCompareEqualTo : kCFCompareLessThan; |
5358f56f | 2295 | else if (rhn == NULL) |
b129e6d9 | 2296 | return kCFCompareGreaterThan; |
5358f56f | 2297 | |
22fd24dd | 2298 | CFIndex length(CFStringGetLength(lhn)); |
aeeb755b | 2299 | |
677b8415 | 2300 | _profile(PackageNameCompare$NumbersLast) |
22fd24dd | 2301 | if (length != 0 && CFStringGetLength(rhn) != 0) { |
677b8415 JF |
2302 | UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); |
2303 | UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); | |
2304 | bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); | |
2305 | if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) | |
b129e6d9 | 2306 | return lha ? kCFCompareLessThan : kCFCompareGreaterThan; |
677b8415 JF |
2307 | } |
2308 | _end | |
2309 | ||
2310 | _profile(PackageNameCompare$Compare) | |
22fd24dd | 2311 | return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, (CFLocaleRef) (id) CollationLocale_); |
677b8415 JF |
2312 | _end |
2313 | _end | |
2314 | } | |
2315 | ||
22fd24dd JF |
2316 | _finline CFComparisonResult StringNameCompare(NSString *lhn, NSString*rhn, size_t length) { |
2317 | return StringNameCompare((CFStringRef) lhn, (CFStringRef) rhn, length); | |
2318 | } | |
2319 | ||
aeeb755b | 2320 | CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { |
22fd24dd JF |
2321 | CYString &lhn(PackageName(lhs, @selector(cyname))); |
2322 | NSString *rhn(PackageName(rhs, @selector(cyname))); | |
2323 | return StringNameCompare(lhn, rhn, lhn.size()); | |
aeeb755b JF |
2324 | } |
2325 | ||
2326 | CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *arg) { | |
2327 | return PackageNameCompare(*lhs, *rhs, arg); | |
677b8415 JF |
2328 | } |
2329 | ||
2330 | struct PackageNameOrdering : | |
2331 | std::binary_function<Package *, Package *, bool> | |
2332 | { | |
2333 | _finline bool operator ()(Package *lhs, Package *rhs) const { | |
b129e6d9 | 2334 | return PackageNameCompare(lhs, rhs, NULL) == kCFCompareLessThan; |
677b8415 JF |
2335 | } |
2336 | }; | |
2337 | ||
36bb2ca2 | 2338 | @implementation Package |
b4d89997 | 2339 | |
df213583 JF |
2340 | - (NSString *) description { |
2341 | return [NSString stringWithFormat:@"<Package:%@>", static_cast<NSString *>(name_)]; | |
2342 | } | |
2343 | ||
36bb2ca2 | 2344 | - (void) dealloc { |
13902177 | 2345 | if (!pooled_) |
64edd9df | 2346 | delete pool_; |
12016ee5 JF |
2347 | if (parsed_ != NULL) |
2348 | delete parsed_; | |
36bb2ca2 | 2349 | [super dealloc]; |
b4d89997 JF |
2350 | } |
2351 | ||
3bd1c2a2 | 2352 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 2353 | if (false); |
10d63f9e JF |
2354 | else if (selector == @selector(clear)) |
2355 | return @"clear"; | |
2cd1afd9 JF |
2356 | else if (selector == @selector(getField:)) |
2357 | return @"getField"; | |
8fb7c7a1 JF |
2358 | else if (selector == @selector(getRecord)) |
2359 | return @"getRecord"; | |
e58ff941 | 2360 | else if (selector == @selector(hasTag:)) |
3bd1c2a2 | 2361 | return @"hasTag"; |
10d63f9e JF |
2362 | else if (selector == @selector(install)) |
2363 | return @"install"; | |
2364 | else if (selector == @selector(remove)) | |
2365 | return @"remove"; | |
3bd1c2a2 JF |
2366 | else |
2367 | return nil; | |
2368 | } | |
2369 | ||
2370 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
2371 | return [self webScriptNameForSelector:selector] == nil; | |
2372 | } | |
2373 | ||
6f1a15d9 | 2374 | + (NSArray *) _attributeKeys { |
e58ff941 JF |
2375 | return [NSArray arrayWithObjects: |
2376 | @"applications", | |
a412f357 | 2377 | @"architecture", |
e58ff941 JF |
2378 | @"author", |
2379 | @"depiction", | |
2380 | @"essential", | |
2381 | @"homepage", | |
2382 | @"icon", | |
2383 | @"id", | |
2384 | @"installed", | |
2385 | @"latest", | |
2386 | @"longDescription", | |
2387 | @"longSection", | |
2388 | @"maintainer", | |
974742b2 | 2389 | @"md5sum", |
e58ff941 JF |
2390 | @"mode", |
2391 | @"name", | |
2392 | @"purposes", | |
83682c75 | 2393 | @"relations", |
e58ff941 | 2394 | @"section", |
de1ace71 | 2395 | @"selection", |
e58ff941 JF |
2396 | @"shortDescription", |
2397 | @"shortSection", | |
2398 | @"simpleSection", | |
2399 | @"size", | |
2400 | @"source", | |
5959b596 | 2401 | @"state", |
e58ff941 | 2402 | @"support", |
82aa2434 | 2403 | @"tags", |
1342f808 | 2404 | @"upgraded", |
e58ff941 JF |
2405 | @"warnings", |
2406 | nil]; | |
6f1a15d9 JF |
2407 | } |
2408 | ||
2409 | - (NSArray *) attributeKeys { | |
2410 | return [[self class] _attributeKeys]; | |
2411 | } | |
2412 | ||
2413 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2414 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2415 | } | |
2416 | ||
83682c75 JF |
2417 | - (NSArray *) relations { |
2418 | @synchronized (database_) { | |
2419 | NSMutableArray *relations([NSMutableArray arrayWithCapacity:16]); | |
810c9763 JF |
2420 | for (pkgCache::DepIterator dep(version_.DependsList()); !dep.end(); ++dep) |
2421 | [relations addObject:[[[CydiaRelation alloc] initWithIterator:dep] autorelease]]; | |
83682c75 JF |
2422 | return relations; |
2423 | } } | |
2424 | ||
a412f357 JF |
2425 | - (NSString *) architecture { |
2426 | [self parse]; | |
2427 | @synchronized (database_) { | |
2428 | return parsed_->architecture_.empty() ? [NSNull null] : (id) parsed_->architecture_; | |
2429 | } } | |
2430 | ||
2cd1afd9 JF |
2431 | - (NSString *) getField:(NSString *)name { |
2432 | @synchronized (database_) { | |
2433 | if ([database_ era] != era_ || file_.end()) | |
2434 | return nil; | |
2435 | ||
2436 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2437 | ||
2438 | const char *start, *end; | |
2439 | if (!parser.Find([name UTF8String], start, end)) | |
2440 | return (NSString *) [NSNull null]; | |
2441 | ||
b6f9e52a | 2442 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; |
2cd1afd9 JF |
2443 | } } |
2444 | ||
8fb7c7a1 JF |
2445 | - (NSString *) getRecord { |
2446 | @synchronized (database_) { | |
2447 | if ([database_ era] != era_ || file_.end()) | |
2448 | return nil; | |
2449 | ||
2450 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2451 | ||
2452 | const char *start, *end; | |
2453 | parser.GetRec(start, end); | |
2454 | ||
2455 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; | |
2456 | } } | |
2457 | ||
68d927e2 | 2458 | - (void) parse { |
12016ee5 | 2459 | if (parsed_ != NULL) |
68d927e2 | 2460 | return; |
12016ee5 JF |
2461 | @synchronized (database_) { |
2462 | if ([database_ era] != era_ || file_.end()) | |
68d927e2 JF |
2463 | return; |
2464 | ||
12016ee5 JF |
2465 | ParsedPackage *parsed(new ParsedPackage); |
2466 | parsed_ = parsed; | |
2467 | ||
68d927e2 JF |
2468 | _profile(Package$parse) |
2469 | pkgRecords::Parser *parser; | |
2470 | ||
2471 | _profile(Package$parse$Lookup) | |
2472 | parser = &[database_ records]->Lookup(file_); | |
2473 | _end | |
2474 | ||
00067a67 | 2475 | CYString bugs; |
68d927e2 JF |
2476 | CYString website; |
2477 | ||
2478 | _profile(Package$parse$Find) | |
2479 | struct { | |
2480 | const char *name_; | |
2481 | CYString *value_; | |
2482 | } names[] = { | |
a412f357 | 2483 | {"architecture", &parsed->architecture_}, |
12016ee5 JF |
2484 | {"icon", &parsed->icon_}, |
2485 | {"depiction", &parsed->depiction_}, | |
2486 | {"homepage", &parsed->homepage_}, | |
68d927e2 | 2487 | {"website", &website}, |
00067a67 | 2488 | {"bugs", &bugs}, |
12016ee5 | 2489 | {"support", &parsed->support_}, |
12016ee5 | 2490 | {"author", &parsed->author_}, |
974742b2 | 2491 | {"md5sum", &parsed->md5sum_}, |
68d927e2 JF |
2492 | }; |
2493 | ||
2494 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { | |
2495 | const char *start, *end; | |
2496 | ||
2497 | if (parser->Find(names[i].name_, start, end)) { | |
2498 | CYString &value(*names[i].value_); | |
2499 | _profile(Package$parse$Value) | |
2500 | value.set(pool_, start, end - start); | |
2501 | _end | |
2502 | } | |
2503 | } | |
2504 | _end | |
2505 | ||
2506 | _profile(Package$parse$Tagline) | |
2507 | const char *start, *end; | |
0dd0c302 | 2508 | if (parser->ShortDesc(start, end)) { |
68d927e2 JF |
2509 | const char *stop(reinterpret_cast<const char *>(memchr(start, '\n', end - start))); |
2510 | if (stop == NULL) | |
2511 | stop = end; | |
2512 | while (stop != start && stop[-1] == '\r') | |
2513 | --stop; | |
12016ee5 | 2514 | parsed->tagline_.set(pool_, start, stop - start); |
68d927e2 JF |
2515 | } |
2516 | _end | |
2517 | ||
2518 | _profile(Package$parse$Retain) | |
12016ee5 JF |
2519 | if (parsed->homepage_.empty()) |
2520 | parsed->homepage_ = website; | |
2521 | if (parsed->homepage_ == parsed->depiction_) | |
2522 | parsed->homepage_.clear(); | |
00067a67 JF |
2523 | if (parsed->support_.empty()) |
2524 | parsed->support_ = bugs; | |
68d927e2 JF |
2525 | _end |
2526 | _end | |
12016ee5 | 2527 | } } |
68d927e2 | 2528 | |
64edd9df | 2529 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { |
a1440b10 | 2530 | if ((self = [super init]) != nil) { |
7376b55c | 2531 | _profile(Package$initWithVersion) |
13902177 | 2532 | if (pool == NULL) |
64edd9df | 2533 | pool_ = new CYPool(); |
13902177 JF |
2534 | else { |
2535 | pool_ = pool; | |
2536 | pooled_ = true; | |
2537 | } | |
a1440b10 | 2538 | |
36bb2ca2 | 2539 | database_ = database; |
aab28f8b | 2540 | era_ = [database era]; |
b4d89997 | 2541 | |
aab28f8b | 2542 | version_ = version; |
2083b866 | 2543 | |
aab28f8b JF |
2544 | pkgCache::PkgIterator iterator(version.ParentPkg()); |
2545 | iterator_ = iterator; | |
06aa974d | 2546 | |
aab28f8b | 2547 | _profile(Package$initWithVersion$Version) |
68d927e2 JF |
2548 | if (!version_.end()) |
2549 | file_ = version_.FileList(); | |
2550 | else { | |
2551 | pkgCache &cache([database_ cache]); | |
2552 | file_ = pkgCache::VerFileIterator(cache, cache.VerFileP); | |
2553 | } | |
2554 | _end | |
808c6eb6 | 2555 | |
aab28f8b | 2556 | _profile(Package$initWithVersion$Cache) |
aab28f8b JF |
2557 | name_.set(NULL, iterator.Display()); |
2558 | ||
2559 | latest_.set(NULL, StripVersion_(version_.VerStr())); | |
2560 | ||
2561 | pkgCache::VerIterator current(iterator.CurrentVer()); | |
2562 | if (!current.end()) | |
2563 | installed_.set(NULL, StripVersion_(current.VerStr())); | |
808c6eb6 JF |
2564 | _end |
2565 | ||
22fd24dd JF |
2566 | _profile(Package$initWithVersion$Transliterate) do { |
2567 | if (CollationTransl_ == NULL) | |
2568 | break; | |
2569 | if (name_.empty()) | |
2570 | break; | |
2571 | ||
2572 | _profile(Package$initWithVersion$Transliterate$utf8) | |
2573 | const uint8_t *data(reinterpret_cast<const uint8_t *>(name_.data())); | |
2574 | for (size_t i(0), e(name_.size()); i != e; ++i) | |
2575 | if (data[i] >= 0x80) | |
2576 | goto extended; | |
2577 | break; extended:; | |
2578 | _end | |
2579 | ||
2580 | UErrorCode code(U_ZERO_ERROR); | |
2581 | int32_t length; | |
2582 | ||
2583 | _profile(Package$initWithVersion$Transliterate$u_strFromUTF8WithSub) | |
2584 | CollationString_.resize(name_.size()); | |
2585 | u_strFromUTF8WithSub(&CollationString_[0], CollationString_.size(), &length, name_.data(), name_.size(), 0xfffd, NULL, &code); | |
2586 | if (!U_SUCCESS(code)) | |
2587 | break; | |
2588 | CollationString_.resize(length); | |
2589 | _end | |
2590 | ||
2591 | _profile(Package$initWithVersion$Transliterate$utrans_trans) | |
2592 | length = CollationString_.size(); | |
2593 | utrans_trans(CollationTransl_, reinterpret_cast<UReplaceable *>(&CollationString_), &CollationUCalls_, 0, &length, &code); | |
2594 | if (!U_SUCCESS(code)) | |
2595 | break; | |
2596 | _assert(CollationString_.size() == length); | |
2597 | _end | |
2598 | ||
2599 | _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$preflight) | |
2600 | u_strToUTF8WithSub(NULL, 0, &length, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); | |
2601 | if (code == U_BUFFER_OVERFLOW_ERROR) | |
2602 | code = U_ZERO_ERROR; | |
2603 | else if (!U_SUCCESS(code)) | |
2604 | break; | |
2605 | _end | |
2606 | ||
2607 | char *transform; | |
2608 | _profile(Package$initWithVersion$Transliterate$apr_palloc) | |
64edd9df | 2609 | transform = pool_->malloc<char>(length); |
22fd24dd JF |
2610 | _end |
2611 | _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$transform) | |
2612 | u_strToUTF8WithSub(transform, length, NULL, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); | |
2613 | if (!U_SUCCESS(code)) | |
2614 | break; | |
2615 | _end | |
2616 | ||
2617 | transform_.set(NULL, transform, length); | |
2618 | } while (false); _end | |
2619 | ||
7376b55c | 2620 | _profile(Package$initWithVersion$Tags) |
aab28f8b | 2621 | pkgCache::TagIterator tag(iterator.TagList()); |
0dd0c302 | 2622 | if (!tag.end()) { |
7b33d201 | 2623 | tags_ = [NSMutableArray arrayWithCapacity:8]; |
5b625a2e JF |
2624 | |
2625 | goto tag; for (; !tag.end(); ++tag) tag: { | |
0dd0c302 | 2626 | const char *name(tag.Name()); |
5b625a2e JF |
2627 | NSString *string((NSString *) CYStringCreate(name)); |
2628 | if (string == nil) | |
2629 | continue; | |
2630 | ||
2631 | [tags_ addObject:[string autorelease]]; | |
0a377825 | 2632 | |
97a3d89e JF |
2633 | if (role_ == 0 && strncmp(name, "role::", 6) == 0 /*&& strcmp(name, "role::leaper") != 0*/) { |
2634 | if (strcmp(name + 6, "enduser") == 0) | |
2635 | role_ = 1; | |
2636 | else if (strcmp(name + 6, "hacker") == 0) | |
2637 | role_ = 2; | |
2638 | else if (strcmp(name + 6, "developer") == 0) | |
2639 | role_ = 3; | |
2640 | else if (strcmp(name + 6, "cydia") == 0) | |
2641 | role_ = 7; | |
2642 | else | |
2643 | role_ = 4; | |
2644 | } | |
0a377825 JF |
2645 | |
2646 | if (strncmp(name, "cydia::", 7) == 0) { | |
2647 | if (strcmp(name + 7, "essential") == 0) | |
2648 | essential_ = true; | |
2649 | else if (strcmp(name + 7, "obsolete") == 0) | |
2650 | obsolete_ = true; | |
2651 | } | |
5b625a2e | 2652 | } |
0dd0c302 | 2653 | } |
808c6eb6 | 2654 | _end |
7b0ce2da | 2655 | |
7376b55c | 2656 | _profile(Package$initWithVersion$Metadata) |
029c8b74 JF |
2657 | const char *mixed(iterator.Name()); |
2658 | size_t size(strlen(mixed)); | |
81bf4684 JF |
2659 | static const size_t prefix(sizeof("/var/lib/dpkg/info/") - 1); |
2660 | char lower[prefix + size + 5 + 1]; | |
029c8b74 JF |
2661 | |
2662 | for (size_t i(0); i != size; ++i) | |
81bf4684 JF |
2663 | lower[prefix + i] = mixed[i] | 0x20; |
2664 | ||
2665 | if (!installed_.empty()) { | |
2666 | memcpy(lower, "/var/lib/dpkg/info/", prefix); | |
2667 | memcpy(lower + prefix + size, ".list", 6); | |
2668 | struct stat info; | |
2669 | if (stat(lower, &info) != -1) | |
b3906a21 | 2670 | upgraded_ = info.st_birthtime; |
81bf4684 | 2671 | } |
029c8b74 | 2672 | |
81bf4684 | 2673 | PackageValue *metadata(PackageFind(lower + prefix, size)); |
bb6bb6d6 | 2674 | metadata_ = metadata; |
677b8415 | 2675 | |
029c8b74 JF |
2676 | id_.set(NULL, metadata->name_, size); |
2677 | ||
94b0b3e5 JF |
2678 | const char *latest(version_.VerStr()); |
2679 | size_t length(strlen(latest)); | |
a4b0ec52 | 2680 | |
94b0b3e5 | 2681 | uint16_t vhash(hashlittle(latest, length)); |
677b8415 | 2682 | |
94b0b3e5 JF |
2683 | size_t capped(std::min<size_t>(8, length)); |
2684 | latest = latest + length - capped; | |
677b8415 | 2685 | |
94b0b3e5 JF |
2686 | if (metadata->first_ == 0) |
2687 | metadata->first_ = now_; | |
808c6eb6 | 2688 | |
94b0b3e5 | 2689 | if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) { |
94b0b3e5 JF |
2690 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); |
2691 | metadata->vhash_ = vhash; | |
2f856365 JF |
2692 | metadata->last_ = now_; |
2693 | } else if (metadata->last_ == 0) | |
2694 | metadata->last_ = metadata->first_; | |
808c6eb6 | 2695 | _end |
a1440b10 | 2696 | |
7376b55c | 2697 | _profile(Package$initWithVersion$Section) |
0bc841de | 2698 | section_ = version_.Section(); |
f79a4512 | 2699 | _end |
a1440b10 | 2700 | |
aab28f8b JF |
2701 | _profile(Package$initWithVersion$Flags) |
2702 | essential_ |= ((iterator->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES); | |
2703 | ignored_ = iterator->SelectedState == pkgCache::State::Hold; | |
4fa77608 | 2704 | _end |
3dd53516 | 2705 | _end } return self; |
dc5812ec JF |
2706 | } |
2707 | ||
64edd9df | 2708 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { |
b1ce61ec JF |
2709 | pkgCache::VerIterator version; |
2710 | ||
2711 | _profile(Package$packageWithIterator$GetCandidateVer) | |
2712 | version = [database policy]->GetCandidateVer(iterator); | |
2713 | _end | |
2714 | ||
7376b55c JF |
2715 | if (version.end()) |
2716 | return nil; | |
b1ce61ec | 2717 | |
8564efc1 JF |
2718 | Package *package; |
2719 | ||
2720 | _profile(Package$packageWithIterator$Allocate) | |
2721 | package = [Package allocWithZone:zone]; | |
2722 | _end | |
2723 | ||
2724 | _profile(Package$packageWithIterator$Initialize) | |
2725 | package = [package | |
2726 | initWithVersion:version | |
2727 | withZone:zone | |
2728 | inPool:pool | |
2729 | database:database | |
2730 | ]; | |
2731 | _end | |
2732 | ||
2733 | _profile(Package$packageWithIterator$Autorelease) | |
2734 | package = [package autorelease]; | |
2735 | _end | |
2736 | ||
2737 | return package; | |
3dd53516 | 2738 | } |
dc5812ec | 2739 | |
2388b078 JF |
2740 | - (pkgCache::PkgIterator) iterator { |
2741 | return iterator_; | |
2742 | } | |
2743 | ||
36bb2ca2 | 2744 | - (NSString *) section { |
f79a4512 | 2745 | if (section$_ == nil) { |
fe33a23e | 2746 | if (section_ == NULL) |
f79a4512 JF |
2747 | return nil; |
2748 | ||
fe33a23e JF |
2749 | _profile(Package$section$mappedSectionForPointer) |
2750 | section$_ = [database_ mappedSectionForPointer:section_]; | |
e4f3d6e9 | 2751 | _end |
f79a4512 | 2752 | } return section$_; |
dc5812ec JF |
2753 | } |
2754 | ||
dec6029f JF |
2755 | - (NSString *) simpleSection { |
2756 | if (NSString *section = [self section]) | |
2757 | return Simplify(section); | |
2758 | else | |
2759 | return nil; | |
a3328c28 | 2760 | } |
dec6029f | 2761 | |
f79a4512 | 2762 | - (NSString *) longSection { |
18873623 | 2763 | return LocalizeSection([self section]); |
f79a4512 JF |
2764 | } |
2765 | ||
2766 | - (NSString *) shortSection { | |
2767 | return [[NSBundle mainBundle] localizedStringForKey:[self simpleSection] value:nil table:@"Sections"]; | |
2768 | } | |
2769 | ||
a3328c28 JF |
2770 | - (NSString *) uri { |
2771 | return nil; | |
2772 | #if 0 | |
2773 | pkgIndexFile *index; | |
2774 | pkgCache::PkgFileIterator file(file_.File()); | |
2775 | if (![database_ list].FindIndex(file, index)) | |
2776 | return nil; | |
2777 | return [NSString stringWithUTF8String:iterator_->Path]; | |
2778 | //return [NSString stringWithUTF8String:file.Site()]; | |
2779 | //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()]; | |
2780 | #endif | |
dec6029f JF |
2781 | } |
2782 | ||
7aa82ca2 | 2783 | - (MIMEAddress *) maintainer { |
884171d6 JF |
2784 | @synchronized (database_) { |
2785 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2786 | return nil; |
884171d6 | 2787 | |
36bb2ca2 | 2788 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
00e2109e | 2789 | const std::string &maintainer(parser->Maintainer()); |
7aa82ca2 | 2790 | return maintainer.empty() ? nil : [MIMEAddress addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]]; |
884171d6 | 2791 | } } |
8fe19fc1 | 2792 | |
974742b2 JF |
2793 | - (NSString *) md5sum { |
2794 | return parsed_ == NULL ? nil : (id) parsed_->md5sum_; | |
2795 | } | |
2796 | ||
36bb2ca2 | 2797 | - (size_t) size { |
884171d6 JF |
2798 | @synchronized (database_) { |
2799 | if ([database_ era] != era_ || version_.end()) | |
2800 | return 0; | |
2801 | ||
2802 | return version_->InstalledSize; | |
2803 | } } | |
dc5812ec | 2804 | |
eef4ccaf | 2805 | - (NSString *) longDescription { |
3dd53516 JF |
2806 | @synchronized (database_) { |
2807 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2808 | return nil; |
3dd53516 | 2809 | |
36bb2ca2 | 2810 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
2388b078 | 2811 | NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]); |
8fe19fc1 | 2812 | |
36bb2ca2 JF |
2813 | NSArray *lines = [description componentsSeparatedByString:@"\n"]; |
2814 | NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; | |
2815 | if ([lines count] < 2) | |
2816 | return nil; | |
3178d79b | 2817 | |
36bb2ca2 | 2818 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; |
c4dcf2c2 | 2819 | for (size_t i(1), e([lines count]); i != e; ++i) { |
36bb2ca2 JF |
2820 | NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; |
2821 | [trimmed addObject:trim]; | |
2822 | } | |
3178d79b | 2823 | |
36bb2ca2 | 2824 | return [trimmed componentsJoinedByString:@"\n"]; |
3dd53516 | 2825 | } } |
dc5812ec | 2826 | |
eef4ccaf | 2827 | - (NSString *) shortDescription { |
60bef540 JF |
2828 | if (parsed_ != NULL) |
2829 | return static_cast<NSString *>(parsed_->tagline_); | |
2830 | ||
2831 | @synchronized (database_) { | |
2832 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2833 | ||
2834 | const char *start, *end; | |
2835 | if (!parser.ShortDesc(start, end)) | |
2836 | return nil; | |
2837 | ||
abd93900 JF |
2838 | if (end - start > 200) |
2839 | end = start + 200; | |
60bef540 JF |
2840 | |
2841 | /* | |
2842 | if (const char *stop = reinterpret_cast<const char *>(memchr(start, '\n', end - start))) | |
2843 | end = stop; | |
2844 | ||
2845 | while (end != start && end[-1] == '\r') | |
2846 | --end; | |
2847 | */ | |
2848 | ||
2849 | return [(id) CYStringCreate(start, end - start) autorelease]; | |
2850 | } } | |
eef4ccaf | 2851 | |
808c6eb6 JF |
2852 | - (unichar) index { |
2853 | _profile(Package$index) | |
677b8415 JF |
2854 | CFStringRef name((CFStringRef) [self name]); |
2855 | if (CFStringGetLength(name) == 0) | |
808c6eb6 | 2856 | return '#'; |
677b8415 JF |
2857 | UniChar character(CFStringGetCharacterAtIndex(name, 0)); |
2858 | if (!CFUniCharIsMemberOf(character, kCFUniCharLetterCharacterSet)) | |
808c6eb6 | 2859 | return '#'; |
447db19d | 2860 | return toupper(character); |
808c6eb6 | 2861 | _end |
36bb2ca2 | 2862 | } |
3178d79b | 2863 | |
94b0b3e5 | 2864 | - (PackageValue *) metadata { |
bb6bb6d6 | 2865 | return metadata_; |
807ae6d7 JF |
2866 | } |
2867 | ||
31bc18a7 | 2868 | - (time_t) seen { |
94b0b3e5 JF |
2869 | PackageValue *metadata([self metadata]); |
2870 | return metadata->subscribed_ ? metadata->last_ : metadata->first_; | |
3178d79b JF |
2871 | } |
2872 | ||
94b0b3e5 JF |
2873 | - (bool) subscribed { |
2874 | return [self metadata]->subscribed_; | |
2875 | } | |
2876 | ||
2877 | - (bool) setSubscribed:(bool)subscribed { | |
2878 | PackageValue *metadata([self metadata]); | |
2879 | if (metadata->subscribed_ == subscribed) | |
2880 | return false; | |
2881 | metadata->subscribed_ = subscribed; | |
2882 | return true; | |
807ae6d7 JF |
2883 | } |
2884 | ||
2885 | - (BOOL) ignored { | |
1b18f026 | 2886 | return ignored_; |
807ae6d7 JF |
2887 | } |
2888 | ||
36bb2ca2 JF |
2889 | - (NSString *) latest { |
2890 | return latest_; | |
8fe19fc1 JF |
2891 | } |
2892 | ||
36bb2ca2 JF |
2893 | - (NSString *) installed { |
2894 | return installed_; | |
3178d79b JF |
2895 | } |
2896 | ||
6a155117 JF |
2897 | - (BOOL) uninstalled { |
2898 | return installed_.empty(); | |
2899 | } | |
2900 | ||
5a09ae08 JF |
2901 | - (BOOL) valid { |
2902 | return !version_.end(); | |
2903 | } | |
2904 | ||
31f3cfff | 2905 | - (BOOL) upgradableAndEssential:(BOOL)essential { |
677b8415 JF |
2906 | _profile(Package$upgradableAndEssential) |
2907 | pkgCache::VerIterator current(iterator_.CurrentVer()); | |
2908 | if (current.end()) | |
e4f3d6e9 | 2909 | return essential && essential_; |
677b8415 | 2910 | else |
e4f3d6e9 | 2911 | return !version_.end() && version_ != current; |
677b8415 | 2912 | _end |
36bb2ca2 | 2913 | } |
3178d79b | 2914 | |
36bb2ca2 | 2915 | - (BOOL) essential { |
a1440b10 | 2916 | return essential_; |
3178d79b JF |
2917 | } |
2918 | ||
36bb2ca2 | 2919 | - (BOOL) broken { |
9bedffaa JF |
2920 | return [database_ cache][iterator_].InstBroken(); |
2921 | } | |
2922 | ||
7d2ac47f | 2923 | - (BOOL) unfiltered { |
4fa77608 | 2924 | _profile(Package$unfiltered$obsolete) |
cf48f656 | 2925 | if (_unlikely(obsolete_)) |
4fa77608 JF |
2926 | return false; |
2927 | _end | |
2928 | ||
4121c5e0 JF |
2929 | _profile(Package$unfiltered$role) |
2930 | if (_unlikely(role_ > 3)) | |
4fa77608 JF |
2931 | return false; |
2932 | _end | |
2933 | ||
e4f3d6e9 JF |
2934 | return true; |
2935 | } | |
4fa77608 | 2936 | |
e4f3d6e9 JF |
2937 | - (BOOL) visible { |
2938 | if (![self unfiltered]) | |
2939 | return false; | |
2940 | ||
fe33a23e JF |
2941 | NSString *section; |
2942 | ||
2943 | _profile(Package$visible$section) | |
2944 | section = [self section]; | |
2945 | _end | |
4fa77608 | 2946 | |
e4f3d6e9 | 2947 | _profile(Package$visible$isSectionVisible) |
45447dc3 | 2948 | if (!isSectionVisible(section)) |
4fa77608 JF |
2949 | return false; |
2950 | _end | |
2951 | ||
2952 | return true; | |
7d2ac47f JF |
2953 | } |
2954 | ||
9bedffaa | 2955 | - (BOOL) half { |
677b8415 | 2956 | unsigned char current(iterator_->CurrentState); |
9bedffaa JF |
2957 | return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled; |
2958 | } | |
2959 | ||
2960 | - (BOOL) halfConfigured { | |
2961 | return iterator_->CurrentState == pkgCache::State::HalfConfigured; | |
2962 | } | |
2963 | ||
2964 | - (BOOL) halfInstalled { | |
2965 | return iterator_->CurrentState == pkgCache::State::HalfInstalled; | |
2966 | } | |
2967 | ||
2968 | - (BOOL) hasMode { | |
f18b4a97 JF |
2969 | @synchronized (database_) { |
2970 | if ([database_ era] != era_ || iterator_.end()) | |
b129e6d9 | 2971 | return NO; |
f18b4a97 | 2972 | |
9bedffaa JF |
2973 | pkgDepCache::StateCache &state([database_ cache][iterator_]); |
2974 | return state.Mode != pkgDepCache::ModeKeep; | |
f18b4a97 | 2975 | } } |
9bedffaa JF |
2976 | |
2977 | - (NSString *) mode { | |
f18b4a97 JF |
2978 | @synchronized (database_) { |
2979 | if ([database_ era] != era_ || iterator_.end()) | |
2980 | return nil; | |
2981 | ||
9bedffaa JF |
2982 | pkgDepCache::StateCache &state([database_ cache][iterator_]); |
2983 | ||
2984 | switch (state.Mode) { | |
2985 | case pkgDepCache::ModeDelete: | |
2986 | if ((state.iFlags & pkgDepCache::Purge) != 0) | |
f79a4512 | 2987 | return @"PURGE"; |
9bedffaa | 2988 | else |
f79a4512 | 2989 | return @"REMOVE"; |
9bedffaa | 2990 | case pkgDepCache::ModeKeep: |
dc63e78f | 2991 | if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2992 | return @"REINSTALL"; |
dc63e78f JF |
2993 | /*else if ((state.iFlags & pkgDepCache::AutoKept) != 0) |
2994 | return nil;*/ | |
9bedffaa JF |
2995 | else |
2996 | return nil; | |
9bedffaa | 2997 | case pkgDepCache::ModeInstall: |
dc63e78f | 2998 | /*if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2999 | return @"REINSTALL"; |
dc63e78f | 3000 | else*/ switch (state.Status) { |
9bedffaa | 3001 | case -1: |
f79a4512 | 3002 | return @"DOWNGRADE"; |
9bedffaa | 3003 | case 0: |
f79a4512 | 3004 | return @"INSTALL"; |
9bedffaa | 3005 | case 1: |
f79a4512 | 3006 | return @"UPGRADE"; |
9bedffaa | 3007 | case 2: |
f79a4512 | 3008 | return @"NEW_INSTALL"; |
670a0494 | 3009 | _nodefault |
9bedffaa | 3010 | } |
670a0494 | 3011 | _nodefault |
9bedffaa | 3012 | } |
f18b4a97 | 3013 | } } |
8fe19fc1 | 3014 | |
36bb2ca2 JF |
3015 | - (NSString *) id { |
3016 | return id_; | |
8fe19fc1 JF |
3017 | } |
3018 | ||
36bb2ca2 | 3019 | - (NSString *) name { |
9fcbca29 | 3020 | return name_.empty() ? id_ : name_; |
36bb2ca2 | 3021 | } |
8fe19fc1 | 3022 | |
770f2a8e | 3023 | - (UIImage *) icon { |
dec6029f | 3024 | NSString *section = [self simpleSection]; |
770f2a8e JF |
3025 | |
3026 | UIImage *icon(nil); | |
12016ee5 JF |
3027 | if (parsed_ != NULL) |
3028 | if (NSString *href = parsed_->icon_) | |
3029 | if ([href hasPrefix:@"file:///"]) | |
84851d87 | 3030 | icon = [UIImage imageAtPath:[[href substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; |
770f2a8e | 3031 | if (icon == nil) if (section != nil) |
84851d87 | 3032 | icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [section stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]; |
b9956841 | 3033 | if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon]) |
189a73d0 | 3034 | if ([dicon hasPrefix:@"file:///"]) |
84851d87 | 3035 | icon = [UIImage imageAtPath:[[dicon substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; |
770f2a8e | 3036 | if (icon == nil) |
8dbf3abc | 3037 | icon = [UIImage imageNamed:@"unknown.png"]; |
770f2a8e | 3038 | return icon; |
36bb2ca2 | 3039 | } |
8fe19fc1 | 3040 | |
6f1a15d9 | 3041 | - (NSString *) homepage { |
12016ee5 | 3042 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->homepage_); |
8fe19fc1 JF |
3043 | } |
3044 | ||
25a2158d | 3045 | - (NSString *) depiction { |
12016ee5 | 3046 | return parsed_ != NULL && !parsed_->depiction_.empty() ? parsed_->depiction_ : [[self source] depictionForPackage:id_]; |
25a2158d JF |
3047 | } |
3048 | ||
7aa82ca2 JF |
3049 | - (MIMEAddress *) author { |
3050 | return parsed_ == NULL || parsed_->author_.empty() ? nil : [MIMEAddress addressWithString:parsed_->author_]; | |
77fcccaf JF |
3051 | } |
3052 | ||
dc63e78f | 3053 | - (NSString *) support { |
00067a67 | 3054 | return parsed_ != NULL && !parsed_->support_.empty() ? parsed_->support_ : [[self source] supportForPackage:id_]; |
dc63e78f JF |
3055 | } |
3056 | ||
affeffc7 | 3057 | - (NSArray *) files { |
9fcbca29 | 3058 | NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", static_cast<NSString *>(id_)]; |
affeffc7 JF |
3059 | NSMutableArray *files = [NSMutableArray arrayWithCapacity:128]; |
3060 | ||
3061 | std::ifstream fin; | |
3062 | fin.open([path UTF8String]); | |
3063 | if (!fin.is_open()) | |
3064 | return nil; | |
3065 | ||
3066 | std::string line; | |
3067 | while (std::getline(fin, line)) | |
3068 | [files addObject:[NSString stringWithUTF8String:line.c_str()]]; | |
3069 | ||
3070 | return files; | |
3071 | } | |
3072 | ||
5959b596 JF |
3073 | - (NSString *) state { |
3074 | @synchronized (database_) { | |
3075 | if ([database_ era] != era_ || file_.end()) | |
3076 | return nil; | |
3077 | ||
3078 | switch (iterator_->CurrentState) { | |
3079 | case pkgCache::State::NotInstalled: | |
3080 | return @"NotInstalled"; | |
3081 | case pkgCache::State::UnPacked: | |
3082 | return @"UnPacked"; | |
3083 | case pkgCache::State::HalfConfigured: | |
3084 | return @"HalfConfigured"; | |
3085 | case pkgCache::State::HalfInstalled: | |
3086 | return @"HalfInstalled"; | |
3087 | case pkgCache::State::ConfigFiles: | |
3088 | return @"ConfigFiles"; | |
3089 | case pkgCache::State::Installed: | |
3090 | return @"Installed"; | |
3091 | case pkgCache::State::TriggersAwaited: | |
3092 | return @"TriggersAwaited"; | |
3093 | case pkgCache::State::TriggersPending: | |
3094 | return @"TriggersPending"; | |
3095 | } | |
3096 | ||
3097 | return (NSString *) [NSNull null]; | |
3098 | } } | |
3099 | ||
de1ace71 JF |
3100 | - (NSString *) selection { |
3101 | @synchronized (database_) { | |
3102 | if ([database_ era] != era_ || file_.end()) | |
3103 | return nil; | |
3104 | ||
3105 | switch (iterator_->SelectedState) { | |
3106 | case pkgCache::State::Unknown: | |
3107 | return @"Unknown"; | |
3108 | case pkgCache::State::Install: | |
3109 | return @"Install"; | |
3110 | case pkgCache::State::Hold: | |
3111 | return @"Hold"; | |
3112 | case pkgCache::State::DeInstall: | |
3113 | return @"DeInstall"; | |
3114 | case pkgCache::State::Purge: | |
3115 | return @"Purge"; | |
3116 | } | |
3117 | ||
3118 | return (NSString *) [NSNull null]; | |
3119 | } } | |
3120 | ||
affeffc7 | 3121 | - (NSArray *) warnings { |
d212b02f JF |
3122 | @synchronized (database_) { |
3123 | if ([database_ era] != era_ || file_.end()) | |
3124 | return nil; | |
3125 | ||
affeffc7 JF |
3126 | NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); |
3127 | const char *name(iterator_.Name()); | |
3128 | ||
3129 | size_t length(strlen(name)); | |
3130 | if (length < 2) invalid: | |
43f3d7f6 | 3131 | [warnings addObject:UCLocalize("ILLEGAL_PACKAGE_IDENTIFIER")]; |
affeffc7 JF |
3132 | else for (size_t i(0); i != length; ++i) |
3133 | if ( | |
9dd60d81 JF |
3134 | /* XXX: technically this is not allowed */ |
3135 | (name[i] < 'A' || name[i] > 'Z') && | |
affeffc7 JF |
3136 | (name[i] < 'a' || name[i] > 'z') && |
3137 | (name[i] < '0' || name[i] > '9') && | |
3138 | (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.') | |
3139 | ) goto invalid; | |
3140 | ||
3141 | if (strcmp(name, "cydia") != 0) { | |
3142 | bool cydia = false; | |
7623f855 | 3143 | bool user = false; |
9dd60d81 | 3144 | bool _private = false; |
affeffc7 | 3145 | bool stash = false; |
bcc58607 | 3146 | bool dbstash = false; |
1db5920d | 3147 | bool dsstore = false; |
affeffc7 | 3148 | |
9dd60d81 JF |
3149 | bool repository = [[self section] isEqualToString:@"Repositories"]; |
3150 | ||
affeffc7 JF |
3151 | if (NSArray *files = [self files]) |
3152 | for (NSString *file in files) | |
3153 | if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"]) | |
3154 | cydia = true; | |
7623f855 JF |
3155 | else if (!user && [file isEqualToString:@"/User"]) |
3156 | user = true; | |
9dd60d81 JF |
3157 | else if (!_private && [file isEqualToString:@"/private"]) |
3158 | _private = true; | |
affeffc7 JF |
3159 | else if (!stash && [file isEqualToString:@"/var/stash"]) |
3160 | stash = true; | |
bcc58607 JF |
3161 | else if (!dbstash && [file isEqualToString:@"/var/db/stash"]) |
3162 | dbstash = true; | |
1db5920d JF |
3163 | else if (!dsstore && [file hasSuffix:@"/.DS_Store"]) |
3164 | dsstore = true; | |
affeffc7 | 3165 | |
9dd60d81 JF |
3166 | /* XXX: this is not sensitive enough. only some folders are valid. */ |
3167 | if (cydia && !repository) | |
43f3d7f6 | 3168 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"Cydia.app"]]; |
7623f855 JF |
3169 | if (user) |
3170 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/User"]]; | |
9dd60d81 | 3171 | if (_private) |
43f3d7f6 | 3172 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; |
affeffc7 | 3173 | if (stash) |
43f3d7f6 | 3174 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; |
bcc58607 JF |
3175 | if (dbstash) |
3176 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/db/stash"]]; | |
1db5920d JF |
3177 | if (dsstore) |
3178 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @".DS_Store"]]; | |
affeffc7 JF |
3179 | } |
3180 | ||
3181 | return [warnings count] == 0 ? nil : warnings; | |
d212b02f | 3182 | } } |
affeffc7 JF |
3183 | |
3184 | - (NSArray *) applications { | |
3185 | NSString *me([[NSBundle mainBundle] bundleIdentifier]); | |
3186 | ||
3187 | NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); | |
3188 | ||
b4fd1197 | 3189 | static RegEx application_r("/Applications/(.*)\\.app/Info.plist"); |
affeffc7 JF |
3190 | if (NSArray *files = [self files]) |
3191 | for (NSString *file in files) | |
3192 | if (application_r(file)) { | |
3193 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); | |
75d2e426 JF |
3194 | if (info == nil) |
3195 | continue; | |
affeffc7 | 3196 | NSString *id([info objectForKey:@"CFBundleIdentifier"]); |
75d2e426 | 3197 | if (id == nil || [id isEqualToString:me]) |
affeffc7 JF |
3198 | continue; |
3199 | ||
3200 | NSString *display([info objectForKey:@"CFBundleDisplayName"]); | |
3201 | if (display == nil) | |
3202 | display = application_r[1]; | |
3203 | ||
3204 | NSString *bundle([file stringByDeletingLastPathComponent]); | |
3205 | NSString *icon([info objectForKey:@"CFBundleIconFile"]); | |
5e17a734 JF |
3206 | // XXX: maybe this should check if this is really a string, not just for length |
3207 | if (icon == nil || ![icon respondsToSelector:@selector(length)] || [icon length] == 0) | |
affeffc7 JF |
3208 | icon = @"icon.png"; |
3209 | NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]); | |
3210 | ||
3211 | NSMutableArray *application([NSMutableArray arrayWithCapacity:2]); | |
3212 | [applications addObject:application]; | |
3213 | ||
3214 | [application addObject:id]; | |
3215 | [application addObject:display]; | |
3216 | [application addObject:url]; | |
3217 | } | |
3218 | ||
3219 | return [applications count] == 0 ? nil : applications; | |
3220 | } | |
3221 | ||
36bb2ca2 | 3222 | - (Source *) source { |
5699667a | 3223 | if (source_ == nil) { |
a1440b10 JF |
3224 | @synchronized (database_) { |
3225 | if ([database_ era] != era_ || file_.end()) | |
5699667a JF |
3226 | source_ = (Source *) [NSNull null]; |
3227 | else | |
7b33d201 | 3228 | source_ = [database_ getSource:file_.File()] ?: (Source *) [NSNull null]; |
a1440b10 | 3229 | } |
bbb879fb JF |
3230 | } |
3231 | ||
5699667a | 3232 | return source_ == (Source *) [NSNull null] ? nil : source_; |
8fe19fc1 JF |
3233 | } |
3234 | ||
b3906a21 JF |
3235 | - (time_t) upgraded { |
3236 | return upgraded_; | |
98ddcefd JF |
3237 | } |
3238 | ||
b3906a21 JF |
3239 | - (uint32_t) recent { |
3240 | return std::numeric_limits<uint32_t>::max() - upgraded_; | |
821b1a0c JF |
3241 | } |
3242 | ||
9c5737d5 JF |
3243 | - (uint32_t) rank { |
3244 | return rank_; | |
3245 | } | |
3246 | ||
d84597fe JF |
3247 | - (BOOL) matches:(NSArray *)query { |
3248 | if (query == nil || [query count] == 0) | |
36bb2ca2 | 3249 | return NO; |
8fe19fc1 | 3250 | |
9c5737d5 JF |
3251 | rank_ = 0; |
3252 | ||
3253 | NSString *string; | |
36bb2ca2 | 3254 | NSRange range; |
9c5737d5 | 3255 | NSUInteger length; |
8fe19fc1 | 3256 | |
718c267c | 3257 | string = [self name]; |
d84597fe JF |
3258 | length = [string length]; |
3259 | ||
5af08e16 | 3260 | if (length != 0) |
d84597fe JF |
3261 | for (NSString *term in query) { |
3262 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
3263 | if (range.location != NSNotFound) | |
718c267c | 3264 | rank_ -= 6 * 1000000 / length; |
d84597fe | 3265 | } |
8fe19fc1 | 3266 | |
718c267c JF |
3267 | if (rank_ == 0) { |
3268 | string = [self id]; | |
3269 | length = [string length]; | |
8fe19fc1 | 3270 | |
5af08e16 | 3271 | if (length != 0) |
718c267c JF |
3272 | for (NSString *term in query) { |
3273 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
3274 | if (range.location != NSNotFound) | |
3275 | rank_ -= 6 * 1000000 / length; | |
3276 | } | |
d84597fe | 3277 | } |
c4b530a7 | 3278 | |
9c5737d5 JF |
3279 | string = [self shortDescription]; |
3280 | length = [string length]; | |
abd93900 | 3281 | NSUInteger stop(std::min<NSUInteger>(length, 200)); |
df289c5a | 3282 | |
5af08e16 | 3283 | if (length != 0) |
d84597fe JF |
3284 | for (NSString *term in query) { |
3285 | range = [string rangeOfString:term options:MatchCompareOptions_ range:NSMakeRange(0, stop)]; | |
3286 | if (range.location != NSNotFound) | |
718c267c | 3287 | rank_ -= 2 * 100000; |
d84597fe | 3288 | } |
8fe19fc1 | 3289 | |
9c5737d5 | 3290 | return rank_ != 0; |
36bb2ca2 | 3291 | } |
8fe19fc1 | 3292 | |
82aa2434 JF |
3293 | - (NSArray *) tags { |
3294 | return tags_; | |
3295 | } | |
3296 | ||
6d9712c4 JF |
3297 | - (BOOL) hasTag:(NSString *)tag { |
3298 | return tags_ == nil ? NO : [tags_ containsObject:tag]; | |
3299 | } | |
3300 | ||
c390d3ab | 3301 | - (NSString *) primaryPurpose { |
7b33d201 | 3302 | for (NSString *tag in (NSArray *) tags_) |
c390d3ab JF |
3303 | if ([tag hasPrefix:@"purpose::"]) |
3304 | return [tag substringFromIndex:9]; | |
3305 | return nil; | |
3306 | } | |
3307 | ||
770f2a8e JF |
3308 | - (NSArray *) purposes { |
3309 | NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]); | |
7b33d201 | 3310 | for (NSString *tag in (NSArray *) tags_) |
770f2a8e JF |
3311 | if ([tag hasPrefix:@"purpose::"]) |
3312 | [purposes addObject:[tag substringFromIndex:9]]; | |
3313 | return [purposes count] == 0 ? nil : purposes; | |
3314 | } | |
3315 | ||
3bd1c2a2 JF |
3316 | - (bool) isCommercial { |
3317 | return [self hasTag:@"cydia::commercial"]; | |
3318 | } | |
3319 | ||
cd95e390 JF |
3320 | - (void) setIndex:(size_t)index { |
3321 | if (metadata_->index_ != index) | |
3322 | metadata_->index_ = index; | |
3323 | } | |
3324 | ||
df213583 | 3325 | - (CYString &) cyname { |
22fd24dd | 3326 | return !transform_.empty() ? transform_ : !name_.empty() ? name_ : id_; |
f79a4512 JF |
3327 | } |
3328 | ||
f79a4512 JF |
3329 | - (uint32_t) compareBySection:(NSArray *)sections { |
3330 | NSString *section([self section]); | |
3331 | for (size_t i(0), e([sections count]); i != e; ++i) { | |
3332 | if ([section isEqualToString:[[sections objectAtIndex:i] name]]) | |
3333 | return i; | |
36bb2ca2 | 3334 | } |
3178d79b | 3335 | |
f79a4512 | 3336 | return _not(uint32_t); |
36bb2ca2 | 3337 | } |
3178d79b | 3338 | |
dc63e78f | 3339 | - (void) clear { |
c6ca67ba | 3340 | @synchronized (database_) { |
dc63e78f JF |
3341 | pkgProblemResolver *resolver = [database_ resolver]; |
3342 | resolver->Clear(iterator_); | |
c6ca67ba JF |
3343 | |
3344 | pkgCacheFile &cache([database_ cache]); | |
3345 | cache->SetReInstall(iterator_, false); | |
3346 | cache->MarkKeep(iterator_, false); | |
3347 | } } | |
dc63e78f | 3348 | |
36bb2ca2 | 3349 | - (void) install { |
c6ca67ba | 3350 | @synchronized (database_) { |
36bb2ca2 JF |
3351 | pkgProblemResolver *resolver = [database_ resolver]; |
3352 | resolver->Clear(iterator_); | |
3353 | resolver->Protect(iterator_); | |
c6ca67ba | 3354 | |
36bb2ca2 | 3355 | pkgCacheFile &cache([database_ cache]); |
c6ca67ba | 3356 | cache->SetReInstall(iterator_, false); |
36bb2ca2 | 3357 | cache->MarkInstall(iterator_, false); |
c6ca67ba | 3358 | |
36bb2ca2 JF |
3359 | pkgDepCache::StateCache &state((*cache)[iterator_]); |
3360 | if (!state.Install()) | |
3361 | cache->SetReInstall(iterator_, true); | |
c6ca67ba | 3362 | } } |
68a238ec | 3363 | |
36bb2ca2 | 3364 | - (void) remove { |
c6ca67ba | 3365 | @synchronized (database_) { |
36bb2ca2 JF |
3366 | pkgProblemResolver *resolver = [database_ resolver]; |
3367 | resolver->Clear(iterator_); | |
36bb2ca2 | 3368 | resolver->Remove(iterator_); |
c6ca67ba JF |
3369 | resolver->Protect(iterator_); |
3370 | ||
3371 | pkgCacheFile &cache([database_ cache]); | |
3372 | cache->SetReInstall(iterator_, false); | |
3373 | cache->MarkDelete(iterator_, true); | |
3374 | } } | |
8fe19fc1 | 3375 | |
36bb2ca2 JF |
3376 | @end |
3377 | /* }}} */ | |
3378 | /* Section Class {{{ */ | |
3379 | @interface Section : NSObject { | |
7b33d201 | 3380 | _H<NSString> name_; |
36bb2ca2 JF |
3381 | size_t row_; |
3382 | size_t count_; | |
7b33d201 | 3383 | _H<NSString> localized_; |
36bb2ca2 | 3384 | } |
3178d79b | 3385 | |
677b8415 | 3386 | - (NSComparisonResult) compareByLocalized:(Section *)section; |
9fcbca29 JF |
3387 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; |
3388 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; | |
3389 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; | |
aeeb755b | 3390 | |
36bb2ca2 | 3391 | - (NSString *) name; |
aeeb755b | 3392 | - (void) setName:(NSString *)name; |
f79a4512 | 3393 | |
36bb2ca2 JF |
3394 | - (size_t) row; |
3395 | - (size_t) count; | |
f79a4512 JF |
3396 | |
3397 | - (void) addToRow; | |
36bb2ca2 | 3398 | - (void) addToCount; |
b19871dd | 3399 | |
f79a4512 | 3400 | - (void) setCount:(size_t)count; |
677b8415 | 3401 | - (NSString *) localized; |
f79a4512 | 3402 | |
36bb2ca2 | 3403 | @end |
b19871dd | 3404 | |
36bb2ca2 | 3405 | @implementation Section |
b19871dd | 3406 | |
677b8415 | 3407 | - (NSComparisonResult) compareByLocalized:(Section *)section { |
9fcbca29 JF |
3408 | NSString *lhs(localized_); |
3409 | NSString *rhs([section localized]); | |
6d9712c4 | 3410 | |
9fcbca29 | 3411 | /*if ([lhs length] != 0 && [rhs length] != 0) { |
6d9712c4 JF |
3412 | unichar lhc = [lhs characterAtIndex:0]; |
3413 | unichar rhc = [rhs characterAtIndex:0]; | |
3414 | ||
3415 | if (isalpha(lhc) && !isalpha(rhc)) | |
3416 | return NSOrderedAscending; | |
3417 | else if (!isalpha(lhc) && isalpha(rhc)) | |
3418 | return NSOrderedDescending; | |
9fcbca29 | 3419 | }*/ |
6d9712c4 | 3420 | |
68f1828e | 3421 | return [lhs compare:rhs options:LaxCompareOptions_]; |
6d9712c4 JF |
3422 | } |
3423 | ||
9fcbca29 JF |
3424 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized { |
3425 | if ((self = [self initWithName:name localize:NO]) != nil) { | |
3426 | if (localized != nil) | |
7b33d201 | 3427 | localized_ = localized; |
9fcbca29 JF |
3428 | } return self; |
3429 | } | |
3430 | ||
3431 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize { | |
3432 | return [self initWithName:name row:0 localize:localize]; | |
6d9712c4 JF |
3433 | } |
3434 | ||
9fcbca29 | 3435 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { |
36bb2ca2 | 3436 | if ((self = [super init]) != nil) { |
7b33d201 | 3437 | name_ = name; |
808c6eb6 | 3438 | row_ = row; |
9fcbca29 | 3439 | if (localize) |
7b33d201 | 3440 | localized_ = LocalizeSection(name_); |
808c6eb6 JF |
3441 | } return self; |
3442 | } | |
3443 | ||
36bb2ca2 JF |
3444 | - (NSString *) name { |
3445 | return name_; | |
3446 | } | |
3447 | ||
aeeb755b JF |
3448 | - (void) setName:(NSString *)name { |
3449 | name_ = name; | |
808c6eb6 JF |
3450 | } |
3451 | ||
36bb2ca2 JF |
3452 | - (size_t) row { |
3453 | return row_; | |
3454 | } | |
3455 | ||
3456 | - (size_t) count { | |
3457 | return count_; | |
3458 | } | |
3459 | ||
f79a4512 JF |
3460 | - (void) addToRow { |
3461 | ++row_; | |
3462 | } | |
3463 | ||
36bb2ca2 JF |
3464 | - (void) addToCount { |
3465 | ++count_; | |
3466 | } | |
3467 | ||
f79a4512 JF |
3468 | - (void) setCount:(size_t)count { |
3469 | count_ = count; | |
3470 | } | |
3471 | ||
3472 | - (NSString *) localized { | |
3473 | return localized_; | |
3474 | } | |
3475 | ||
b19871dd JF |
3476 | @end |
3477 | /* }}} */ | |
3478 | ||
9f9ae81c JF |
3479 | class CydiaLogCleaner : |
3480 | public pkgArchiveCleaner | |
3481 | { | |
3482 | protected: | |
3483 | virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) { | |
3484 | unlink(File); | |
3485 | } | |
3486 | }; | |
3487 | ||
36bb2ca2 JF |
3488 | /* Database Implementation {{{ */ |
3489 | @implementation Database | |
ec97ef06 | 3490 | |
770f2a8e | 3491 | + (Database *) sharedInstance { |
7b33d201 | 3492 | static _H<Database> instance; |
770f2a8e | 3493 | if (instance == nil) |
7b33d201 | 3494 | instance = [[[Database alloc] init] autorelease]; |
770f2a8e JF |
3495 | return instance; |
3496 | } | |
3497 | ||
a1440b10 JF |
3498 | - (unsigned) era { |
3499 | return era_; | |
3500 | } | |
3501 | ||
0944377b JF |
3502 | - (void) releasePackages { |
3503 | CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL); | |
3504 | CFArrayRemoveAllValues(packages_); | |
3505 | } | |
3506 | ||
36bb2ca2 | 3507 | - (void) dealloc { |
3931b718 | 3508 | // XXX: actually implement this thing |
36bb2ca2 | 3509 | _assert(false); |
0944377b | 3510 | [self releasePackages]; |
0944377b | 3511 | NSRecycleZone(zone_); |
36bb2ca2 JF |
3512 | [super dealloc]; |
3513 | } | |
ec97ef06 | 3514 | |
d13edf44 | 3515 | - (void) _readCydia:(NSNumber *)fd { |
77fcccaf JF |
3516 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3517 | std::istream is(&ib); | |
3518 | std::string line; | |
3519 | ||
b4fd1197 | 3520 | static RegEx finish_r("finish:([^:]*)"); |
bc8cd583 | 3521 | |
77fcccaf | 3522 | while (std::getline(is, line)) { |
d13edf44 JF |
3523 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3524 | ||
77fcccaf | 3525 | const char *data(line.c_str()); |
bc8cd583 | 3526 | size_t size = line.size(); |
c390d3ab | 3527 | lprintf("C:%s\n", data); |
bc8cd583 JF |
3528 | |
3529 | if (finish_r(data, size)) { | |
3530 | NSString *finish = finish_r[1]; | |
3531 | int index = [Finishes_ indexOfObject:finish]; | |
3532 | if (index != INT_MAX && index > Finish_) | |
3533 | Finish_ = index; | |
3534 | } | |
d13edf44 JF |
3535 | |
3536 | [pool release]; | |
77fcccaf JF |
3537 | } |
3538 | ||
670a0494 | 3539 | _assume(false); |
77fcccaf JF |
3540 | } |
3541 | ||
d13edf44 | 3542 | - (void) _readStatus:(NSNumber *)fd { |
36bb2ca2 JF |
3543 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3544 | std::istream is(&ib); | |
3545 | std::string line; | |
ec97ef06 | 3546 | |
b4fd1197 JF |
3547 | static RegEx conffile_r("status: [^ ]* : conffile-prompt : (.*?) *"); |
3548 | static RegEx pmstatus_r("([^:]*):([^:]*):([^:]*):(.*)"); | |
ec97ef06 | 3549 | |
36bb2ca2 | 3550 | while (std::getline(is, line)) { |
d13edf44 JF |
3551 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3552 | ||
36bb2ca2 | 3553 | const char *data(line.c_str()); |
670a0494 | 3554 | size_t size(line.size()); |
c390d3ab | 3555 | lprintf("S:%s\n", data); |
ec97ef06 | 3556 | |
a08145a8 JF |
3557 | if (conffile_r(data, size)) { |
3558 | // status: /fail : conffile-prompt : '/fail' '/fail.dpkg-new' 1 1 | |
4187453f | 3559 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:conffile_r[1] waitUntilDone:YES]; |
a08145a8 JF |
3560 | } else if (strncmp(data, "status: ", 8) == 0) { |
3561 | // status: <package>: {unpacked,half-configured,installed} | |
389133be | 3562 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 8)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 JF |
3563 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3564 | } else if (strncmp(data, "processing: ", 12) == 0) { | |
3565 | // processing: configure: config-test | |
389133be | 3566 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 12)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 | 3567 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
6915b806 | 3568 | } else if (pmstatus_r(data, size)) { |
31f3cfff | 3569 | std::string type([pmstatus_r[1] UTF8String]); |
4a2dc82e | 3570 | |
6915b806 | 3571 | NSString *package = pmstatus_r[2]; |
4a2dc82e JF |
3572 | if ([package isEqualToString:@"dpkg-exec"]) |
3573 | package = nil; | |
31f3cfff | 3574 | |
5a09ae08 | 3575 | float percent([pmstatus_r[3] floatValue]); |
6915b806 | 3576 | [progress_ performSelectorOnMainThread:@selector(setProgressPercent:) withObject:[NSNumber numberWithFloat:(percent / 100)] waitUntilDone:YES]; |
5a09ae08 JF |
3577 | |
3578 | NSString *string = pmstatus_r[4]; | |
5a09ae08 | 3579 | |
6915b806 | 3580 | if (type == "pmerror") { |
389133be | 3581 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeError forPackage:package]); |
6915b806 JF |
3582 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3583 | } else if (type == "pmstatus") { | |
389133be | 3584 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeStatus forPackage:package]); |
6915b806 JF |
3585 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3586 | } else if (type == "pmconffile") | |
4187453f | 3587 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:string waitUntilDone:YES]; |
670a0494 JF |
3588 | else |
3589 | lprintf("E:unknown pmstatus\n"); | |
3590 | } else | |
3591 | lprintf("E:unknown status\n"); | |
d13edf44 JF |
3592 | |
3593 | [pool release]; | |
36bb2ca2 | 3594 | } |
ec97ef06 | 3595 | |
670a0494 | 3596 | _assume(false); |
36bb2ca2 | 3597 | } |
ec97ef06 | 3598 | |
d13edf44 | 3599 | - (void) _readOutput:(NSNumber *)fd { |
36bb2ca2 JF |
3600 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3601 | std::istream is(&ib); | |
3602 | std::string line; | |
3603 | ||
5a09ae08 | 3604 | while (std::getline(is, line)) { |
d13edf44 JF |
3605 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3606 | ||
c390d3ab | 3607 | lprintf("O:%s\n", line.c_str()); |
27024935 | 3608 | |
389133be | 3609 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:line.c_str()] ofType:kCydiaProgressEventTypeInformation]); |
6915b806 | 3610 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
d13edf44 JF |
3611 | |
3612 | [pool release]; | |
5a09ae08 | 3613 | } |
36bb2ca2 | 3614 | |
670a0494 | 3615 | _assume(false); |
ec97ef06 JF |
3616 | } |
3617 | ||
8b29f8e6 JF |
3618 | - (FILE *) input { |
3619 | return input_; | |
3620 | } | |
3621 | ||
36bb2ca2 | 3622 | - (Package *) packageWithName:(NSString *)name { |
c5f1a937 JF |
3623 | if (name == nil) |
3624 | return nil; | |
3dd53516 | 3625 | @synchronized (self) { |
5a09ae08 JF |
3626 | if (static_cast<pkgDepCache *>(cache_) == NULL) |
3627 | return nil; | |
36bb2ca2 | 3628 | pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); |
13902177 | 3629 | return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:NULL database:self]; |
28ce8704 | 3630 | } } |
36bb2ca2 | 3631 | |
eb30da80 | 3632 | - (id) init { |
ec97ef06 | 3633 | if ((self = [super init]) != nil) { |
5a09ae08 | 3634 | policy_ = NULL; |
36bb2ca2 JF |
3635 | records_ = NULL; |
3636 | resolver_ = NULL; | |
3637 | fetcher_ = NULL; | |
3638 | lock_ = NULL; | |
ec97ef06 | 3639 | |
f79a4512 | 3640 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3641 | |
9f357d11 JF |
3642 | size_t capacity(MetaFile_->active_); |
3643 | if (capacity == 0) | |
3644 | capacity = 16384; | |
3645 | else | |
3646 | capacity += 1024; | |
3647 | ||
3648 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, capacity, NULL); | |
7b33d201 | 3649 | sourceList_ = [NSMutableArray arrayWithCapacity:16]; |
ec97ef06 | 3650 | |
36bb2ca2 | 3651 | int fds[2]; |
ec97ef06 | 3652 | |
77fcccaf JF |
3653 | _assert(pipe(fds) != -1); |
3654 | cydiafd_ = fds[1]; | |
3655 | ||
3656 | _config->Set("APT::Keep-Fds::", cydiafd_); | |
bc8cd583 | 3657 | setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int)); |
77fcccaf JF |
3658 | |
3659 | [NSThread | |
3660 | detachNewThreadSelector:@selector(_readCydia:) | |
3661 | toTarget:self | |
3931b718 | 3662 | withObject:[NSNumber numberWithInt:fds[0]] |
77fcccaf JF |
3663 | ]; |
3664 | ||
36bb2ca2 JF |
3665 | _assert(pipe(fds) != -1); |
3666 | statusfd_ = fds[1]; | |
ec97ef06 | 3667 | |
36bb2ca2 JF |
3668 | [NSThread |
3669 | detachNewThreadSelector:@selector(_readStatus:) | |
3670 | toTarget:self | |
3931b718 | 3671 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3672 | ]; |
ec97ef06 | 3673 | |
8b29f8e6 JF |
3674 | _assert(pipe(fds) != -1); |
3675 | _assert(dup2(fds[0], 0) != -1); | |
3676 | _assert(close(fds[0]) != -1); | |
3677 | ||
3678 | input_ = fdopen(fds[1], "a"); | |
3679 | ||
36bb2ca2 JF |
3680 | _assert(pipe(fds) != -1); |
3681 | _assert(dup2(fds[1], 1) != -1); | |
3682 | _assert(close(fds[1]) != -1); | |
3683 | ||
3684 | [NSThread | |
3685 | detachNewThreadSelector:@selector(_readOutput:) | |
3686 | toTarget:self | |
3931b718 | 3687 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3688 | ]; |
ec97ef06 JF |
3689 | } return self; |
3690 | } | |
3691 | ||
36bb2ca2 JF |
3692 | - (pkgCacheFile &) cache { |
3693 | return cache_; | |
ec97ef06 JF |
3694 | } |
3695 | ||
5a09ae08 JF |
3696 | - (pkgDepCache::Policy *) policy { |
3697 | return policy_; | |
3698 | } | |
3699 | ||
36bb2ca2 JF |
3700 | - (pkgRecords *) records { |
3701 | return records_; | |
ec97ef06 JF |
3702 | } |
3703 | ||
36bb2ca2 JF |
3704 | - (pkgProblemResolver *) resolver { |
3705 | return resolver_; | |
ec97ef06 JF |
3706 | } |
3707 | ||
36bb2ca2 JF |
3708 | - (pkgAcquire &) fetcher { |
3709 | return *fetcher_; | |
ec97ef06 JF |
3710 | } |
3711 | ||
a3328c28 JF |
3712 | - (pkgSourceList &) list { |
3713 | return *list_; | |
3714 | } | |
3715 | ||
36bb2ca2 | 3716 | - (NSArray *) packages { |
077e9d90 | 3717 | return (NSArray *) packages_; |
ec97ef06 JF |
3718 | } |
3719 | ||
7b0ce2da | 3720 | - (NSArray *) sources { |
34f70f5d | 3721 | return sourceList_; |
7b0ce2da JF |
3722 | } |
3723 | ||
d669236d GP |
3724 | - (Source *) sourceWithKey:(NSString *)key { |
3725 | for (Source *source in [self sources]) { | |
3726 | if ([[source key] isEqualToString:key]) | |
3727 | return source; | |
3728 | } return nil; | |
3729 | } | |
3730 | ||
670a0494 JF |
3731 | - (bool) popErrorWithTitle:(NSString *)title { |
3732 | bool fatal(false); | |
670a0494 JF |
3733 | |
3734 | while (!_error->empty()) { | |
3735 | std::string error; | |
3736 | bool warning(!_error->PopMessage(error)); | |
3737 | if (!warning) | |
3738 | fatal = true; | |
cb6e2ccf | 3739 | |
670a0494 JF |
3740 | for (;;) { |
3741 | size_t size(error.size()); | |
3742 | if (size == 0 || error[size - 1] != '\n') | |
3743 | break; | |
3744 | error.resize(size - 1); | |
3745 | } | |
cb6e2ccf | 3746 | |
670a0494 JF |
3747 | lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); |
3748 | ||
b4fd1197 | 3749 | static RegEx no_pubkey("GPG error:.* NO_PUBKEY .*"); |
c8ce71c7 JF |
3750 | if (warning && no_pubkey(error.c_str())) |
3751 | continue; | |
3752 | ||
389133be | 3753 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
670a0494 JF |
3754 | } |
3755 | ||
670a0494 JF |
3756 | return fatal; |
3757 | } | |
3758 | ||
3759 | - (bool) popErrorWithTitle:(NSString *)title forOperation:(bool)success { | |
3760 | return [self popErrorWithTitle:title] || !success; | |
3761 | } | |
3762 | ||
7bc0d825 | 3763 | - (bool) popErrorWithTitle:(NSString *)title forReadList:(pkgSourceList &)list { |
19cd1e0d JF |
3764 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3765 | return true; | |
3766 | return false; | |
3767 | ||
57cc2170 JF |
3768 | list.Reset(); |
3769 | ||
3770 | bool error(false); | |
3771 | ||
3772 | if (access("/etc/apt/sources.list", F_OK) == 0) | |
3773 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend("/etc/apt/sources.list")]; | |
3774 | ||
3775 | std::string base("/etc/apt/sources.list.d"); | |
3776 | if (DIR *sources = opendir(base.c_str())) { | |
3777 | while (dirent *source = readdir(sources)) | |
3778 | 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) | |
3779 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend((base + "/" + source->d_name).c_str())]; | |
3780 | closedir(sources); | |
3781 | } | |
3782 | ||
3783 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend(SOURCES_LIST)]; | |
3784 | ||
3785 | return error; | |
7bc0d825 JF |
3786 | } |
3787 | ||
d13edf44 | 3788 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
3dd53516 JF |
3789 | @synchronized (self) { |
3790 | ++era_; | |
a1440b10 | 3791 | |
0944377b | 3792 | [self releasePackages]; |
ab3f6a01 | 3793 | |
34f70f5d JF |
3794 | sourceMap_.clear(); |
3795 | [sourceList_ removeAllObjects]; | |
843e75b8 | 3796 | |
36bb2ca2 | 3797 | _error->Discard(); |
5a09ae08 | 3798 | |
36bb2ca2 | 3799 | delete list_; |
5a09ae08 | 3800 | list_ = NULL; |
36bb2ca2 JF |
3801 | manager_ = NULL; |
3802 | delete lock_; | |
5a09ae08 | 3803 | lock_ = NULL; |
36bb2ca2 | 3804 | delete fetcher_; |
5a09ae08 | 3805 | fetcher_ = NULL; |
36bb2ca2 | 3806 | delete resolver_; |
5a09ae08 | 3807 | resolver_ = NULL; |
36bb2ca2 | 3808 | delete records_; |
5a09ae08 JF |
3809 | records_ = NULL; |
3810 | delete policy_; | |
3811 | policy_ = NULL; | |
36bb2ca2 | 3812 | |
5a09ae08 | 3813 | cache_.Close(); |
8b29f8e6 | 3814 | |
64edd9df JF |
3815 | pool_.~CYPool(); |
3816 | new (&pool_) CYPool(); | |
a020a50e | 3817 | |
f79a4512 | 3818 | NSRecycleZone(zone_); |
a020a50e | 3819 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3820 | |
7376b55c JF |
3821 | int chk(creat("/tmp/cydia.chk", 0644)); |
3822 | if (chk != -1) | |
3823 | close(chk); | |
3824 | ||
4ba8f30a JF |
3825 | if (invocation != nil) |
3826 | [invocation invoke]; | |
3827 | ||
670a0494 JF |
3828 | NSString *title(UCLocalize("DATABASE")); |
3829 | ||
0c28a403 | 3830 | list_ = new pkgSourceList(); |
1a83afc6 | 3831 | _profile(reloadDataWithInvocation$ReadMainList) |
7bc0d825 | 3832 | if ([self popErrorWithTitle:title forReadList:*list_]) |
0c28a403 | 3833 | return; |
1a83afc6 | 3834 | _end |
0c28a403 | 3835 | |
1a83afc6 | 3836 | _profile(reloadDataWithInvocation$Source$initWithMetaIndex) |
0c28a403 | 3837 | for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { |
64edd9df | 3838 | Source *object([[[Source alloc] initWithMetaIndex:*source forDatabase:self inPool:&pool_] autorelease]); |
0c28a403 JF |
3839 | [sourceList_ addObject:object]; |
3840 | } | |
1a83afc6 | 3841 | _end |
0c28a403 | 3842 | |
9487f027 | 3843 | _trace(); |
124cea03 | 3844 | OpProgress progress; |
1a83afc6 | 3845 | bool opened; |
2b49cff9 | 3846 | open: |
5cf267f4 | 3847 | delock_ = GetStatusDate(); |
1a83afc6 | 3848 | _profile(reloadDataWithInvocation$pkgCacheFile) |
b44af625 | 3849 | opened = cache_.Open(progress, false); |
1a83afc6 JF |
3850 | _end |
3851 | if (!opened) { | |
2b49cff9 JF |
3852 | // XXX: what if there are errors, but Open() == true? this should be merged with popError: |
3853 | while (!_error->empty()) { | |
3854 | std::string error; | |
3855 | bool warning(!_error->PopMessage(error)); | |
3856 | ||
3857 | lprintf("cache_.Open():[%s]\n", error.c_str()); | |
3858 | ||
389133be | 3859 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
2b49cff9 JF |
3860 | |
3861 | SEL repair(NULL); | |
3862 | if (false); | |
3863 | else if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ") | |
3864 | repair = @selector(configure); | |
3865 | //else if (error == "The package lists or status file could not be parsed or opened.") | |
3866 | // repair = @selector(update); | |
3867 | // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)") | |
3868 | // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)") | |
3869 | // else if (error == "Malformed Status line") | |
3870 | // else if (error == "The list of sources could not be read.") | |
3871 | ||
3872 | if (repair != NULL) { | |
3873 | _error->Discard(); | |
3874 | [delegate_ repairWithSelector:repair]; | |
3875 | goto open; | |
3876 | } | |
efa53fa9 | 3877 | } |
5a09ae08 | 3878 | |
2b49cff9 | 3879 | return; |
36bb2ca2 | 3880 | } |
9487f027 | 3881 | _trace(); |
36bb2ca2 | 3882 | |
7376b55c JF |
3883 | unlink("/tmp/cydia.chk"); |
3884 | ||
31bc18a7 | 3885 | now_ = [[NSDate date] timeIntervalSince1970]; |
36bb2ca2 | 3886 | |
5a09ae08 | 3887 | policy_ = new pkgDepCache::Policy(); |
36bb2ca2 JF |
3888 | records_ = new pkgRecords(cache_); |
3889 | resolver_ = new pkgProblemResolver(cache_); | |
3890 | fetcher_ = new pkgAcquire(&status_); | |
3891 | lock_ = NULL; | |
3892 | ||
670a0494 | 3893 | if (cache_->DelCount() != 0 || cache_->InstCount() != 0) { |
389133be | 3894 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("COUNTS_NONZERO_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3895 | return; |
3896 | } | |
36bb2ca2 | 3897 | |
1a83afc6 | 3898 | _profile(reloadDataWithInvocation$pkgApplyStatus) |
670a0494 JF |
3899 | if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) |
3900 | return; | |
1a83afc6 | 3901 | _end |
9bedffaa JF |
3902 | |
3903 | if (cache_->BrokenCount() != 0) { | |
1a83afc6 | 3904 | _profile(pkgApplyStatus$pkgFixBroken) |
670a0494 JF |
3905 | if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) |
3906 | return; | |
1a83afc6 | 3907 | _end |
670a0494 JF |
3908 | |
3909 | if (cache_->BrokenCount() != 0) { | |
389133be | 3910 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("STILL_BROKEN_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3911 | return; |
3912 | } | |
3913 | ||
1a83afc6 | 3914 | _profile(pkgApplyStatus$pkgMinimizeUpgrade) |
670a0494 JF |
3915 | if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) |
3916 | return; | |
1a83afc6 | 3917 | _end |
9bedffaa JF |
3918 | } |
3919 | ||
0c28a403 JF |
3920 | for (Source *object in (id) sourceList_) { |
3921 | metaIndex *source([object metaIndex]); | |
3922 | std::vector<pkgIndexFile *> *indices = source->GetIndexFiles(); | |
68d927e2 JF |
3923 | for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index) |
3924 | // XXX: this could be more intelligent | |
3925 | if (dynamic_cast<debPackagesIndex *>(*index) != NULL) { | |
3926 | pkgCache::PkgFileIterator cached((*index)->FindInCache(cache_)); | |
34f70f5d JF |
3927 | if (!cached.end()) |
3928 | sourceMap_[cached->ID] = object; | |
68d927e2 | 3929 | } |
36bb2ca2 | 3930 | } |
68d927e2 | 3931 | |
677b8415 | 3932 | { |
9fcbca29 | 3933 | /*std::vector<Package *> packages; |
677b8415 | 3934 | packages.reserve(std::max(10000U, [packages_ count] + 1000)); |
9fcbca29 JF |
3935 | packages_ = nil;*/ |
3936 | ||
1a83afc6 | 3937 | _profile(reloadDataWithInvocation$packageWithIterator) |
677b8415 | 3938 | for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) |
64edd9df | 3939 | if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:&pool_ database:self]) |
9fcbca29 | 3940 | //packages.push_back(package); |
7b33d201 | 3941 | CFArrayAppendValue(packages_, CFRetain(package)); |
1a83afc6 | 3942 | _end |
677b8415 | 3943 | |
677b8415 | 3944 | |
9fcbca29 | 3945 | /*if (packages.empty()) |
677b8415 JF |
3946 | packages_ = [[NSArray alloc] init]; |
3947 | else | |
3948 | packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; | |
9fcbca29 JF |
3949 | _trace();*/ |
3950 | ||
1a83afc6 | 3951 | _profile(reloadDataWithInvocation$radix$8) |
9f0facbc | 3952 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(8)]; |
1a83afc6 JF |
3953 | _end |
3954 | ||
3955 | _profile(reloadDataWithInvocation$radix$4) | |
de42680b | 3956 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(4)]; |
1a83afc6 | 3957 | _end |
9fcbca29 | 3958 | |
1a83afc6 JF |
3959 | _profile(reloadDataWithInvocation$radix$0) |
3960 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(0)]; | |
3961 | _end | |
9fcbca29 | 3962 | |
1a83afc6 JF |
3963 | _profile(reloadDataWithInvocation$insertion) |
3964 | CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); | |
3965 | _end | |
9fcbca29 | 3966 | |
1a83afc6 JF |
3967 | /*_profile(reloadDataWithInvocation$CFQSortArray) |
3968 | CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare_), NULL); | |
3969 | _end*/ | |
9fcbca29 | 3970 | |
1a83afc6 JF |
3971 | /*_profile(reloadDataWithInvocation$stdsort) |
3972 | std::sort(packages.begin(), packages.end(), PackageNameOrdering()); | |
3973 | _end*/ | |
eef4ccaf | 3974 | |
1a83afc6 JF |
3975 | /*_profile(reloadDataWithInvocation$CFArraySortValues) |
3976 | CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); | |
3977 | _end*/ | |
9fcbca29 | 3978 | |
1a83afc6 JF |
3979 | /*_profile(reloadDataWithInvocation$sortUsingFunction) |
3980 | [packages_ sortUsingFunction:reinterpret_cast<NSComparisonResult (*)(id, id, void *)>(&PackageNameCompare) context:NULL]; | |
3981 | _end*/ | |
677b8415 | 3982 | |
cd95e390 JF |
3983 | |
3984 | size_t count(CFArrayGetCount(packages_)); | |
9f357d11 | 3985 | MetaFile_->active_ = count; |
cd95e390 JF |
3986 | for (size_t index(0); index != count; ++index) |
3987 | [(Package *) CFArrayGetValueAtIndex(packages_, index) setIndex:index]; | |
677b8415 | 3988 | } |
d13edf44 | 3989 | } } |
ec97ef06 | 3990 | |
c6ca67ba JF |
3991 | - (void) clear { |
3992 | @synchronized (self) { | |
3993 | delete resolver_; | |
3994 | resolver_ = new pkgProblemResolver(cache_); | |
3995 | ||
50c1653e JF |
3996 | for (pkgCache::PkgIterator iterator(cache_->PkgBegin()); !iterator.end(); ++iterator) |
3997 | if (!cache_[iterator].Keep()) | |
c6ca67ba | 3998 | cache_->MarkKeep(iterator, false); |
50c1653e | 3999 | else if ((cache_[iterator].iFlags & pkgDepCache::ReInstall) != 0) |
c6ca67ba | 4000 | cache_->SetReInstall(iterator, false); |
c6ca67ba JF |
4001 | } } |
4002 | ||
5a09ae08 | 4003 | - (void) configure { |
232b396b | 4004 | NSString *dpkg = [NSString stringWithFormat:@"/usr/libexec/cydo --configure -a --status-fd %u", statusfd_]; |
985d2dff | 4005 | _trace(); |
232b396b | 4006 | system([dpkg UTF8String]); |
985d2dff | 4007 | _trace(); |
5a09ae08 JF |
4008 | } |
4009 | ||
670a0494 | 4010 | - (bool) clean { |
0517651f | 4011 | @synchronized (self) { |
670a0494 | 4012 | // XXX: I don't remember this condition |
77fcccaf | 4013 | if (lock_ != NULL) |
670a0494 | 4014 | return false; |
77fcccaf JF |
4015 | |
4016 | FileFd Lock; | |
4017 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
4018 | |
4019 | NSString *title(UCLocalize("CLEAN_ARCHIVES")); | |
4020 | ||
4021 | if ([self popErrorWithTitle:title]) | |
4022 | return false; | |
77fcccaf JF |
4023 | |
4024 | pkgAcquire fetcher; | |
4025 | fetcher.Clean(_config->FindDir("Dir::Cache::Archives")); | |
4026 | ||
9f9ae81c | 4027 | CydiaLogCleaner cleaner; |
670a0494 JF |
4028 | if ([self popErrorWithTitle:title forOperation:cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)]) |
4029 | return false; | |
4030 | ||
4031 | return true; | |
0517651f | 4032 | } } |
77fcccaf | 4033 | |
670a0494 | 4034 | - (bool) prepare { |
744f398e JF |
4035 | fetcher_->Shutdown(); |
4036 | ||
36bb2ca2 JF |
4037 | pkgRecords records(cache_); |
4038 | ||
4039 | lock_ = new FileFd(); | |
4040 | lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
4041 | |
4042 | NSString *title(UCLocalize("PREPARE_ARCHIVES")); | |
4043 | ||
4044 | if ([self popErrorWithTitle:title]) | |
4045 | return false; | |
36bb2ca2 JF |
4046 | |
4047 | pkgSourceList list; | |
7bc0d825 | 4048 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4049 | return false; |
36bb2ca2 JF |
4050 | |
4051 | manager_ = (_system->CreatePM(cache_)); | |
670a0494 JF |
4052 | if ([self popErrorWithTitle:title forOperation:manager_->GetArchives(fetcher_, &list, &records)]) |
4053 | return false; | |
4054 | ||
4055 | return true; | |
ec97ef06 JF |
4056 | } |
4057 | ||
36bb2ca2 | 4058 | - (void) perform { |
be860cc8 JF |
4059 | bool substrate(RestartSubstrate_); |
4060 | RestartSubstrate_ = false; | |
4061 | ||
670a0494 JF |
4062 | NSString *title(UCLocalize("PERFORM_SELECTIONS")); |
4063 | ||
a72074b2 JF |
4064 | NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { |
4065 | pkgSourceList list; | |
7bc0d825 | 4066 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4067 | return; |
a72074b2 JF |
4068 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
4069 | [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
4070 | } | |
a0be02eb | 4071 | |
dcaecde2 | 4072 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
7ffd70fd | 4073 | |
eeb9b112 JF |
4074 | if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) { |
4075 | _trace(); | |
6e90508f | 4076 | [self popErrorWithTitle:title]; |
36bb2ca2 | 4077 | return; |
eeb9b112 JF |
4078 | } |
4079 | ||
4080 | bool failed = false; | |
4081 | for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) { | |
4082 | if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete) | |
4083 | continue; | |
6204f56a JF |
4084 | if ((*item)->Status == pkgAcquire::Item::StatIdle) |
4085 | continue; | |
eeb9b112 | 4086 | |
680a3c3c JF |
4087 | std::string uri = (*item)->DescURI(); |
4088 | std::string error = (*item)->ErrorText; | |
4089 | ||
4090 | lprintf("pAf:%s:%s\n", uri.c_str(), error.c_str()); | |
eeb9b112 | 4091 | failed = true; |
680a3c3c JF |
4092 | |
4093 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError]); | |
4094 | [delegate_ addProgressEventOnMainThread:event forTask:title]; | |
eeb9b112 JF |
4095 | } |
4096 | ||
dcaecde2 | 4097 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
54043703 | 4098 | |
eeb9b112 JF |
4099 | if (failed) { |
4100 | _trace(); | |
4101 | return; | |
4102 | } | |
36bb2ca2 | 4103 | |
be860cc8 JF |
4104 | if (substrate) |
4105 | RestartSubstrate_ = true; | |
4106 | ||
f26c90b1 JF |
4107 | if (![delock_ isEqual:GetStatusDate()]) { |
4108 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("DPKG_LOCKED") ofType:kCydiaProgressEventTypeError] forTask:title]; | |
4109 | return; | |
4110 | } | |
4111 | ||
4112 | delock_ = nil; | |
4113 | ||
d78cda50 JF |
4114 | pkgPackageManager::OrderResult result(manager_->DoInstall(statusfd_)); |
4115 | ||
49cc457a JF |
4116 | NSString *oextended(@"/var/lib/apt/extended_states"); |
4117 | NSString *nextended(Cache("extended_states")); | |
d78cda50 JF |
4118 | |
4119 | struct stat info; | |
4120 | if (stat([nextended UTF8String], &info) != -1 && (info.st_mode & S_IFMT) == S_IFREG) { | |
d8b0f6e3 JF |
4121 | system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/mv -f %@ %@", ShellEscape(nextended), ShellEscape(oextended)] UTF8String]); |
4122 | system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/chown 0:0 %@", ShellEscape(oextended)] UTF8String]); | |
d78cda50 JF |
4123 | } |
4124 | ||
49cc457a JF |
4125 | unlink([nextended UTF8String]); |
4126 | symlink([oextended UTF8String], [nextended UTF8String]); | |
4127 | ||
471683a3 | 4128 | if ([self popErrorWithTitle:title]) |
36bb2ca2 | 4129 | return; |
eeb9b112 JF |
4130 | |
4131 | if (result == pkgPackageManager::Failed) { | |
4132 | _trace(); | |
36bb2ca2 | 4133 | return; |
eeb9b112 JF |
4134 | } |
4135 | ||
4136 | if (result != pkgPackageManager::Completed) { | |
4137 | _trace(); | |
36bb2ca2 | 4138 | return; |
eeb9b112 | 4139 | } |
a0be02eb | 4140 | |
a72074b2 JF |
4141 | NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { |
4142 | pkgSourceList list; | |
7bc0d825 | 4143 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4144 | return; |
a72074b2 JF |
4145 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
4146 | [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
4147 | } | |
4148 | ||
4149 | if (![before isEqualToArray:after]) | |
4150 | [self update]; | |
ec97ef06 JF |
4151 | } |
4152 | ||
f26c90b1 JF |
4153 | - (bool) delocked { |
4154 | return ![delock_ isEqual:GetStatusDate()]; | |
4155 | } | |
4156 | ||
670a0494 JF |
4157 | - (bool) upgrade { |
4158 | NSString *title(UCLocalize("UPGRADE")); | |
4159 | if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) | |
4160 | return false; | |
4161 | return true; | |
c7c6384e JF |
4162 | } |
4163 | ||
36bb2ca2 JF |
4164 | - (void) update { |
4165 | [self updateWithStatus:status_]; | |
4166 | } | |
b4d89997 | 4167 | |
21ac0ce2 | 4168 | - (void) updateWithStatus:(CancelStatus &)status { |
670a0494 JF |
4169 | NSString *title(UCLocalize("REFRESHING_DATA")); |
4170 | ||
36bb2ca2 | 4171 | pkgSourceList list; |
7bc0d825 | 4172 | if ([self popErrorWithTitle:title forReadList:list]) |
53ca7fdd | 4173 | return; |
b4d89997 | 4174 | |
36bb2ca2 JF |
4175 | FileFd lock; |
4176 | lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
670a0494 JF |
4177 | if ([self popErrorWithTitle:title]) |
4178 | return; | |
18873623 | 4179 | |
aaae308d JF |
4180 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
4181 | ||
fc470c15 JF |
4182 | bool success(ListUpdate(status, list, PulseInterval_)); |
4183 | if (status.WasCancelled()) | |
4184 | _error->Discard(); | |
aa42c612 | 4185 | else { |
fc470c15 | 4186 | [self popErrorWithTitle:title forOperation:success]; |
6f87c61a JF |
4187 | |
4188 | [[NSDictionary dictionaryWithObjectsAndKeys: | |
4189 | [NSDate date], @"LastUpdate", | |
4190 | nil] writeToFile:@ CacheState_ atomically:YES]; | |
aa42c612 | 4191 | } |
36bb2ca2 | 4192 | |
aaae308d | 4193 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
b4d89997 JF |
4194 | } |
4195 | ||
6915b806 | 4196 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate { |
36bb2ca2 | 4197 | delegate_ = delegate; |
6915b806 JF |
4198 | } |
4199 | ||
4200 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate { | |
4201 | progress_ = delegate; | |
36bb2ca2 | 4202 | status_.setDelegate(delegate); |
36bb2ca2 | 4203 | } |
b4d89997 | 4204 | |
6915b806 JF |
4205 | - (NSObject<ProgressDelegate> *) progressDelegate { |
4206 | return progress_; | |
4207 | } | |
4208 | ||
7376b55c | 4209 | - (Source *) getSource:(pkgCache::PkgFileIterator)file { |
34f70f5d JF |
4210 | SourceMap::const_iterator i(sourceMap_.find(file->ID)); |
4211 | return i == sourceMap_.end() ? nil : i->second; | |
b19871dd JF |
4212 | } |
4213 | ||
21ac0ce2 JF |
4214 | - (void) setFetch:(bool)fetch forURI:(const char *)uri { |
4215 | for (Source *source in (id) sourceList_) | |
4216 | [source setFetch:fetch forURI:uri]; | |
4217 | } | |
4218 | ||
9ed626f1 JF |
4219 | - (void) resetFetch { |
4220 | for (Source *source in (id) sourceList_) | |
4221 | [source resetFetch]; | |
4222 | } | |
4223 | ||
fe33a23e | 4224 | - (NSString *) mappedSectionForPointer:(const char *)section { |
4905df00 | 4225 | _H<NSString> *mapped; |
fe33a23e | 4226 | |
4905df00 JF |
4227 | _profile(Database$mappedSectionForPointer$Cache) |
4228 | mapped = §ions_[section]; | |
4229 | _end | |
4230 | ||
4231 | if (*mapped == NULL) { | |
fe33a23e JF |
4232 | size_t length(strlen(section)); |
4233 | char spaced[length + 1]; | |
4234 | ||
4235 | _profile(Database$mappedSectionForPointer$Replace) | |
4236 | for (size_t index(0); index != length; ++index) | |
4237 | spaced[index] = section[index] == '_' ? ' ' : section[index]; | |
4238 | spaced[length] = '\0'; | |
4239 | _end | |
4240 | ||
4241 | NSString *string; | |
4242 | ||
4243 | _profile(Database$mappedSectionForPointer$stringWithUTF8String) | |
4244 | string = [NSString stringWithUTF8String:spaced]; | |
4245 | _end | |
4246 | ||
4247 | _profile(Database$mappedSectionForPointer$Map) | |
4905df00 | 4248 | string = [SectionMap_ objectForKey:string] ?: string; |
fe33a23e | 4249 | _end |
4905df00 JF |
4250 | |
4251 | *mapped = string; | |
4252 | } return *mapped; | |
fe33a23e JF |
4253 | } |
4254 | ||
36bb2ca2 JF |
4255 | @end |
4256 | /* }}} */ | |
b4d89997 | 4257 | |
7b33d201 | 4258 | static _H<NSMutableSet> Diversions_; |
7256476b | 4259 | |
775deead | 4260 | @interface Diversion : NSObject { |
b4fd1197 | 4261 | RegEx pattern_; |
775deead JF |
4262 | _H<NSString> key_; |
4263 | _H<NSString> format_; | |
4264 | } | |
4265 | ||
4266 | @end | |
4267 | ||
4268 | @implementation Diversion | |
4269 | ||
4270 | - (id) initWithFrom:(NSString *)from to:(NSString *)to { | |
4271 | if ((self = [super init]) != nil) { | |
4272 | pattern_ = [from UTF8String]; | |
4273 | key_ = from; | |
4274 | format_ = to; | |
4275 | } return self; | |
4276 | } | |
4277 | ||
4278 | - (NSString *) divert:(NSString *)url { | |
8ea598c0 | 4279 | return !pattern_(url) ? nil : pattern_->*format_; |
775deead JF |
4280 | } |
4281 | ||
7256476b JF |
4282 | + (NSURL *) divertURL:(NSURL *)url { |
4283 | divert: | |
4284 | NSString *href([url absoluteString]); | |
4285 | ||
7b33d201 | 4286 | for (Diversion *diversion in (id) Diversions_) |
7256476b | 4287 | if (NSString *diverted = [diversion divert:href]) { |
85d5d452 JF |
4288 | #if !ForRelease |
4289 | NSLog(@"div: %@", diverted); | |
4290 | #endif | |
7256476b JF |
4291 | url = [NSURL URLWithString:diverted]; |
4292 | goto divert; | |
4293 | } | |
4294 | ||
4295 | return url; | |
4296 | } | |
4297 | ||
775deead JF |
4298 | - (NSString *) key { |
4299 | return key_; | |
4300 | } | |
4301 | ||
4302 | - (NSUInteger) hash { | |
4303 | return [key_ hash]; | |
4304 | } | |
4305 | ||
4306 | - (BOOL) isEqual:(Diversion *)object { | |
4307 | return self == object || [self class] == [object class] && [key_ isEqual:[object key]]; | |
4308 | } | |
4309 | ||
4310 | @end | |
4311 | ||
43f3d7f6 | 4312 | @interface CydiaObject : NSObject { |
4cc9e99a | 4313 | _H<CyteWebViewController> indirect_; |
3931b718 | 4314 | _transient id delegate_; |
43f3d7f6 | 4315 | } |
c390d3ab | 4316 | |
43f3d7f6 | 4317 | - (id) initWithDelegate:(IndirectDelegate *)indirect; |
6840bff3 | 4318 | |
c390d3ab JF |
4319 | @end |
4320 | ||
9e130bc2 JF |
4321 | @class CydiaObject; |
4322 | ||
09e89a8a | 4323 | @interface CydiaWebViewController : CyteWebViewController { |
7b33d201 | 4324 | _H<CydiaObject> cydia_; |
775deead JF |
4325 | } |
4326 | ||
4327 | + (void) addDiversion:(Diversion *)diversion; | |
85ae5f42 | 4328 | + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request; |
9e130bc2 JF |
4329 | + (void) didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame withCydia:(CydiaObject *)cydia; |
4330 | - (void) setDelegate:(id)delegate; | |
775deead JF |
4331 | |
4332 | @end | |
4333 | ||
775deead | 4334 | /* Web Scripting {{{ */ |
43f3d7f6 | 4335 | @implementation CydiaObject |
c390d3ab | 4336 | |
43f3d7f6 JF |
4337 | - (id) initWithDelegate:(IndirectDelegate *)indirect { |
4338 | if ((self = [super init]) != nil) { | |
4cc9e99a | 4339 | indirect_ = (CyteWebViewController *) indirect; |
43f3d7f6 JF |
4340 | } return self; |
4341 | } | |
4342 | ||
77801ff1 JF |
4343 | - (void) setDelegate:(id)delegate { |
4344 | delegate_ = delegate; | |
4345 | } | |
4346 | ||
43f3d7f6 | 4347 | + (NSArray *) _attributeKeys { |
e58ff941 | 4348 | return [NSArray arrayWithObjects: |
6ffdaae3 | 4349 | @"bbsnum", |
e967efd5 JF |
4350 | @"build", |
4351 | @"coreFoundationVersionNumber", | |
e58ff941 | 4352 | @"device", |
56296da0 | 4353 | @"ecid", |
93d6d318 JF |
4354 | @"firmware", |
4355 | @"hostname", | |
4356 | @"idiom", | |
f87cac81 JF |
4357 | @"mcc", |
4358 | @"mnc", | |
56296da0 | 4359 | @"model", |
2656c992 | 4360 | @"operator", |
56296da0 | 4361 | @"role", |
e58ff941 | 4362 | @"serial", |
bf1d5e69 | 4363 | @"version", |
e58ff941 | 4364 | nil]; |
43f3d7f6 JF |
4365 | } |
4366 | ||
4367 | - (NSArray *) attributeKeys { | |
4368 | return [[self class] _attributeKeys]; | |
c390d3ab JF |
4369 | } |
4370 | ||
43f3d7f6 JF |
4371 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
4372 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
c390d3ab | 4373 | } |
43f3d7f6 | 4374 | |
bf1d5e69 | 4375 | - (NSString *) version { |
3d45bad1 | 4376 | return Cydia_; |
bf1d5e69 JF |
4377 | } |
4378 | ||
e967efd5 JF |
4379 | - (NSString *) build { |
4380 | return System_; | |
4381 | } | |
4382 | ||
4383 | - (NSString *) coreFoundationVersionNumber { | |
4384 | return [NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber]; | |
4385 | } | |
4386 | ||
43f3d7f6 | 4387 | - (NSString *) device { |
7c80833f | 4388 | return UniqueIdentifier(); |
c390d3ab JF |
4389 | } |
4390 | ||
93d6d318 JF |
4391 | - (NSString *) firmware { |
4392 | return [[UIDevice currentDevice] systemVersion]; | |
4393 | } | |
4394 | ||
4395 | - (NSString *) hostname { | |
4396 | return [[UIDevice currentDevice] name]; | |
4397 | } | |
4398 | ||
4399 | - (NSString *) idiom { | |
c138614d | 4400 | return (id) Idiom_ ?: [NSNull null]; |
93d6d318 JF |
4401 | } |
4402 | ||
f87cac81 JF |
4403 | - (NSString *) mcc { |
4404 | if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode"))) | |
4405 | return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault) autorelease]; | |
4406 | return nil; | |
4407 | } | |
4408 | ||
4409 | - (NSString *) mnc { | |
4410 | if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberNetworkCode"))) | |
4411 | return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(kCFAllocatorDefault) autorelease]; | |
4412 | return nil; | |
56296da0 JF |
4413 | } |
4414 | ||
2656c992 JF |
4415 | - (NSString *) operator { |
4416 | if (CFStringRef (*$CTRegistrationCopyOperatorName)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTRegistrationCopyOperatorName"))) | |
4417 | return [(NSString *) (*$CTRegistrationCopyOperatorName)(kCFAllocatorDefault) autorelease]; | |
4418 | return nil; | |
4419 | } | |
4420 | ||
6ffdaae3 JF |
4421 | - (NSString *) bbsnum { |
4422 | return (id) BBSNum_ ?: [NSNull null]; | |
4423 | } | |
4424 | ||
56296da0 | 4425 | - (NSString *) ecid { |
849cd6bf | 4426 | return (id) ChipID_ ?: [NSNull null]; |
affeffc7 JF |
4427 | } |
4428 | ||
43f3d7f6 | 4429 | - (NSString *) serial { |
56296da0 | 4430 | return SerialNumber_; |
43f3d7f6 | 4431 | } |
86316a91 | 4432 | |
56296da0 | 4433 | - (NSString *) role { |
4121c5e0 | 4434 | return (id) [NSNull null]; |
affeffc7 JF |
4435 | } |
4436 | ||
56296da0 JF |
4437 | - (NSString *) model { |
4438 | return [NSString stringWithUTF8String:Machine_]; | |
43f3d7f6 | 4439 | } |
43f3d7f6 JF |
4440 | |
4441 | + (NSString *) webScriptNameForSelector:(SEL)selector { | |
e58ff941 | 4442 | if (false); |
cfc7b442 JF |
4443 | else if (selector == @selector(addBridgedHost:)) |
4444 | return @"addBridgedHost"; | |
2e1652a9 JF |
4445 | else if (selector == @selector(addInsecureHost:)) |
4446 | return @"addInsecureHost"; | |
3f428e4e JF |
4447 | else if (selector == @selector(addInternalRedirect::)) |
4448 | return @"addInternalRedirect"; | |
e4b48f2f | 4449 | else if (selector == @selector(addPipelinedHost:scheme:)) |
834c18a4 | 4450 | return @"addPipelinedHost"; |
33e30380 JF |
4451 | else if (selector == @selector(addSource:::)) |
4452 | return @"addSource"; | |
b088c0cd JF |
4453 | else if (selector == @selector(addTrivialSource:)) |
4454 | return @"addTrivialSource"; | |
e58ff941 | 4455 | else if (selector == @selector(close)) |
43f3d7f6 | 4456 | return @"close"; |
e58ff941 JF |
4457 | else if (selector == @selector(du:)) |
4458 | return @"du"; | |
4459 | else if (selector == @selector(stringWithFormat:arguments:)) | |
4460 | return @"format"; | |
5c32f89e | 4461 | else if (selector == @selector(getAllSources)) |
caf0475e | 4462 | return @"getAllSources"; |
8c5b623f JF |
4463 | else if (selector == @selector(getApplicationInfo:value:)) |
4464 | return @"getApplicationInfoValue"; | |
375a4481 JF |
4465 | else if (selector == @selector(getDisplayIdentifiers)) |
4466 | return @"getDisplayIdentifiers"; | |
4467 | else if (selector == @selector(getLocalizedNameForDisplayIdentifier:)) | |
4468 | return @"getLocalizedNameForDisplayIdentifier"; | |
5bc1277a JF |
4469 | else if (selector == @selector(getKernelNumber:)) |
4470 | return @"getKernelNumber"; | |
4471 | else if (selector == @selector(getKernelString:)) | |
4472 | return @"getKernelString"; | |
8cc8eb1c JF |
4473 | else if (selector == @selector(getInstalledPackages)) |
4474 | return @"getInstalledPackages"; | |
c31d7cdc JF |
4475 | else if (selector == @selector(getIORegistryEntry::)) |
4476 | return @"getIORegistryEntry"; | |
948db680 JF |
4477 | else if (selector == @selector(getLocaleIdentifier)) |
4478 | return @"getLocaleIdentifier"; | |
4479 | else if (selector == @selector(getPreferredLanguages)) | |
4480 | return @"getPreferredLanguages"; | |
43f3d7f6 JF |
4481 | else if (selector == @selector(getPackageById:)) |
4482 | return @"getPackageById"; | |
37fa9338 JF |
4483 | else if (selector == @selector(getMetadataKeys)) |
4484 | return @"getMetadataKeys"; | |
b3c8e69c JF |
4485 | else if (selector == @selector(getMetadataValue:)) |
4486 | return @"getMetadataValue"; | |
ef974f52 JF |
4487 | else if (selector == @selector(getSessionValue:)) |
4488 | return @"getSessionValue"; | |
77801ff1 JF |
4489 | else if (selector == @selector(installPackages:)) |
4490 | return @"installPackages"; | |
518a552a JF |
4491 | else if (selector == @selector(isReachable:)) |
4492 | return @"isReachable"; | |
e58ff941 JF |
4493 | else if (selector == @selector(localizedStringForKey:value:table:)) |
4494 | return @"localize"; | |
8d497e2a JF |
4495 | else if (selector == @selector(popViewController:)) |
4496 | return @"popViewController"; | |
b088c0cd JF |
4497 | else if (selector == @selector(refreshSources)) |
4498 | return @"refreshSources"; | |
aa1e1906 JF |
4499 | else if (selector == @selector(registerFrame:)) |
4500 | return @"registerFrame"; | |
ed5566c7 JF |
4501 | else if (selector == @selector(removeButton)) |
4502 | return @"removeButton"; | |
33e30380 JF |
4503 | else if (selector == @selector(saveConfig)) |
4504 | return @"saveConfig"; | |
b3c8e69c JF |
4505 | else if (selector == @selector(setMetadataValue::)) |
4506 | return @"setMetadataValue"; | |
ef974f52 JF |
4507 | else if (selector == @selector(setSessionValue::)) |
4508 | return @"setSessionValue"; | |
8a126074 JF |
4509 | else if (selector == @selector(substitutePackageNames:)) |
4510 | return @"substitutePackageNames"; | |
8e3b68d4 JF |
4511 | else if (selector == @selector(scrollToBottom:)) |
4512 | return @"scrollToBottom"; | |
8366df5e JF |
4513 | else if (selector == @selector(setAllowsNavigationAction:)) |
4514 | return @"setAllowsNavigationAction"; | |
c31c825d JF |
4515 | else if (selector == @selector(setBadgeValue:)) |
4516 | return @"setBadgeValue"; | |
43f3d7f6 JF |
4517 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) |
4518 | return @"setButtonImage"; | |
4519 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
4520 | return @"setButtonTitle"; | |
b8a5d89d JF |
4521 | else if (selector == @selector(setHidesBackButton:)) |
4522 | return @"setHidesBackButton"; | |
5cdfcd6f JF |
4523 | else if (selector == @selector(setHidesNavigationBar:)) |
4524 | return @"setHidesNavigationBar"; | |
82406217 JF |
4525 | else if (selector == @selector(setNavigationBarStyle:)) |
4526 | return @"setNavigationBarStyle"; | |
00984204 JF |
4527 | else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:)) |
4528 | return @"setNavigationBarTintColor"; | |
36a20e14 JF |
4529 | else if (selector == @selector(setPasteboardString:)) |
4530 | return @"setPasteboardString"; | |
4531 | else if (selector == @selector(setPasteboardURL:)) | |
4532 | return @"setPasteboardURL"; | |
db698f42 JF |
4533 | else if (selector == @selector(setScrollAlwaysBounceVertical:)) |
4534 | return @"setScrollAlwaysBounceVertical"; | |
4886cc81 JF |
4535 | else if (selector == @selector(setScrollIndicatorStyle:)) |
4536 | return @"setScrollIndicatorStyle"; | |
ef055c6c JF |
4537 | else if (selector == @selector(setToken:)) |
4538 | return @"setToken"; | |
43f3d7f6 JF |
4539 | else if (selector == @selector(setViewportWidth:)) |
4540 | return @"setViewportWidth"; | |
43f3d7f6 JF |
4541 | else if (selector == @selector(statfs:)) |
4542 | return @"statfs"; | |
e58ff941 JF |
4543 | else if (selector == @selector(supports:)) |
4544 | return @"supports"; | |
7c218781 JF |
4545 | else if (selector == @selector(unload)) |
4546 | return @"unload"; | |
c390d3ab | 4547 | else |
43f3d7f6 JF |
4548 | return nil; |
4549 | } | |
4550 | ||
4551 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
4552 | return [self webScriptNameForSelector:selector] == nil; | |
c390d3ab JF |
4553 | } |
4554 | ||
43f3d7f6 JF |
4555 | - (BOOL) supports:(NSString *)feature { |
4556 | return [feature isEqualToString:@"window.open"]; | |
4557 | } | |
c390d3ab | 4558 | |
7c218781 JF |
4559 | - (void) unload { |
4560 | [delegate_ performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO]; | |
4561 | } | |
4562 | ||
db698f42 JF |
4563 | - (void) setScrollAlwaysBounceVertical:(NSNumber *)value { |
4564 | [indirect_ performSelectorOnMainThread:@selector(setScrollAlwaysBounceVerticalNumber:) withObject:value waitUntilDone:NO]; | |
4565 | } | |
4566 | ||
4886cc81 JF |
4567 | - (void) setScrollIndicatorStyle:(NSString *)style { |
4568 | [indirect_ performSelectorOnMainThread:@selector(setScrollIndicatorStyleWithName:) withObject:style waitUntilDone:NO]; | |
4569 | } | |
4570 | ||
3f428e4e | 4571 | - (void) addInternalRedirect:(NSString *)from :(NSString *)to { |
a576488f | 4572 | [CydiaWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO]; |
775deead JF |
4573 | } |
4574 | ||
8c5b623f JF |
4575 | - (NSDictionary *) getApplicationInfo:(NSString *)display value:(NSString *)key { |
4576 | char path[1024]; | |
4577 | if (SBBundlePathForDisplayIdentifier(SBSSpringBoardServerPort(), [display UTF8String], path) != 0) | |
4578 | return (id) [NSNull null]; | |
4579 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:[[NSString stringWithUTF8String:path] stringByAppendingString:@"/Info.plist"]]); | |
4580 | if (info == nil) | |
4581 | return (id) [NSNull null]; | |
4582 | return [info objectForKey:key]; | |
4583 | } | |
4584 | ||
375a4481 JF |
4585 | - (NSArray *) getDisplayIdentifiers { |
4586 | NSSet *set([SBSCopyDisplayIdentifiers() autorelease]); | |
4587 | if (set == nil || ![set isKindOfClass:[NSSet class]]) | |
4588 | return [NSArray array]; | |
4589 | return [set allObjects]; | |
4590 | } | |
4591 | ||
4592 | - (NSString *) getLocalizedNameForDisplayIdentifier:(NSString *)identifier { | |
4593 | return [SBSCopyLocalizedApplicationNameForDisplayIdentifier(identifier) autorelease] ?: (id) [NSNull null]; | |
4594 | } | |
4595 | ||
5bc1277a JF |
4596 | - (NSNumber *) getKernelNumber:(NSString *)name { |
4597 | const char *string([name UTF8String]); | |
4598 | ||
4599 | size_t size; | |
4600 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
4601 | return (id) [NSNull null]; | |
4602 | ||
4603 | if (size != sizeof(int)) | |
4604 | return (id) [NSNull null]; | |
4605 | ||
4606 | int value; | |
4607 | if (sysctlbyname(string, &value, &size, NULL, 0) == -1) | |
4608 | return (id) [NSNull null]; | |
4609 | ||
4610 | return [NSNumber numberWithInt:value]; | |
4611 | } | |
4612 | ||
4613 | - (NSString *) getKernelString:(NSString *)name { | |
4614 | const char *string([name UTF8String]); | |
4615 | ||
4616 | size_t size; | |
4617 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
4618 | return (id) [NSNull null]; | |
4619 | ||
4620 | char value[size + 1]; | |
4621 | if (sysctlbyname(string, value, &size, NULL, 0) == -1) | |
4622 | return (id) [NSNull null]; | |
4623 | ||
4624 | // XXX: just in case you request something ludicrous | |
4625 | value[size] = '\0'; | |
4626 | ||
4627 | return [NSString stringWithCString:value]; | |
4628 | } | |
4629 | ||
c31d7cdc JF |
4630 | - (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry { |
4631 | NSObject *value(CYIOGetValue([path UTF8String], entry)); | |
4632 | ||
4633 | if (value != nil) | |
4634 | if ([value isKindOfClass:[NSData class]]) | |
4635 | value = CYHex((NSData *) value); | |
4636 | ||
4637 | return value; | |
4638 | } | |
4639 | ||
37fa9338 JF |
4640 | - (NSArray *) getMetadataKeys { |
4641 | @synchronized (Values_) { | |
4642 | return [Values_ allKeys]; | |
4643 | } } | |
4644 | ||
aa1e1906 JF |
4645 | - (void) registerFrame:(DOMHTMLIFrameElement *)iframe { |
4646 | WebFrame *frame([iframe contentFrame]); | |
4647 | [indirect_ registerFrame:frame]; | |
4648 | } | |
4649 | ||
b3c8e69c JF |
4650 | - (id) getMetadataValue:(NSString *)key { |
4651 | @synchronized (Values_) { | |
4652 | return [Values_ objectForKey:key]; | |
4653 | } } | |
4654 | ||
4655 | - (void) setMetadataValue:(NSString *)key :(NSString *)value { | |
4656 | @synchronized (Values_) { | |
33e30380 | 4657 | if (value == nil || value == (id) [WebUndefined undefined] || value == (id) [NSNull null]) |
b3c8e69c JF |
4658 | [Values_ removeObjectForKey:key]; |
4659 | else | |
4660 | [Values_ setObject:value forKey:key]; | |
b3c8e69c JF |
4661 | } } |
4662 | ||
ef974f52 JF |
4663 | - (id) getSessionValue:(NSString *)key { |
4664 | @synchronized (SessionData_) { | |
4665 | return [SessionData_ objectForKey:key]; | |
4666 | } } | |
4667 | ||
4668 | - (void) setSessionValue:(NSString *)key :(NSString *)value { | |
4669 | @synchronized (SessionData_) { | |
4670 | if (value == (id) [WebUndefined undefined]) | |
4671 | [SessionData_ removeObjectForKey:key]; | |
4672 | else | |
4673 | [SessionData_ setObject:value forKey:key]; | |
4674 | } } | |
4675 | ||
cfc7b442 | 4676 | - (void) addBridgedHost:(NSString *)host { |
48f1762f JF |
4677 | @synchronized (HostConfig_) { |
4678 | [BridgedHosts_ addObject:host]; | |
4679 | } } | |
5df7ecfb | 4680 | |
2e1652a9 JF |
4681 | - (void) addInsecureHost:(NSString *)host { |
4682 | @synchronized (HostConfig_) { | |
4683 | [InsecureHosts_ addObject:host]; | |
4684 | } } | |
4685 | ||
e4b48f2f | 4686 | - (void) addPipelinedHost:(NSString *)host scheme:(NSString *)scheme { |
48f1762f | 4687 | @synchronized (HostConfig_) { |
e4b48f2f JF |
4688 | if (scheme != (id) [WebUndefined undefined]) |
4689 | host = [NSString stringWithFormat:@"%@:%@", [scheme lowercaseString], host]; | |
4690 | ||
48f1762f JF |
4691 | [PipelinedHosts_ addObject:host]; |
4692 | } } | |
834c18a4 | 4693 | |
8d497e2a JF |
4694 | - (void) popViewController:(NSNumber *)value { |
4695 | if (value == (id) [WebUndefined undefined]) | |
4696 | value = [NSNumber numberWithBool:YES]; | |
4697 | [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO]; | |
4698 | } | |
4699 | ||
33e30380 JF |
4700 | - (void) addSource:(NSString *)href :(NSString *)distribution :(WebScriptObject *)sections { |
4701 | NSMutableArray *array([NSMutableArray arrayWithCapacity:[sections count]]); | |
4702 | ||
4703 | for (NSString *section in sections) | |
4704 | [array addObject:section]; | |
4705 | ||
4706 | [delegate_ performSelectorOnMainThread:@selector(addSource:) withObject:[NSMutableDictionary dictionaryWithObjectsAndKeys: | |
4707 | @"deb", @"Type", | |
4708 | href, @"URI", | |
4709 | distribution, @"Distribution", | |
4710 | array, @"Sections", | |
4711 | nil] waitUntilDone:NO]; | |
4712 | } | |
4713 | ||
8a2d167a JF |
4714 | - (BOOL) addTrivialSource:(NSString *)href { |
4715 | href = VerifySource(href); | |
4716 | if (href == nil) | |
4717 | return NO; | |
a1d85d42 | 4718 | [delegate_ performSelectorOnMainThread:@selector(addTrivialSource:) withObject:href waitUntilDone:NO]; |
8a2d167a | 4719 | return YES; |
b088c0cd JF |
4720 | } |
4721 | ||
4722 | - (void) refreshSources { | |
4723 | [delegate_ performSelectorOnMainThread:@selector(syncData) withObject:nil waitUntilDone:NO]; | |
4724 | } | |
4725 | ||
33e30380 JF |
4726 | - (void) saveConfig { |
4727 | [delegate_ performSelectorOnMainThread:@selector(_saveConfig) withObject:nil waitUntilDone:NO]; | |
4728 | } | |
4729 | ||
5c32f89e JF |
4730 | - (NSArray *) getAllSources { |
4731 | return [[Database sharedInstance] sources]; | |
4732 | } | |
4733 | ||
8cc8eb1c | 4734 | - (NSArray *) getInstalledPackages { |
26e6829b JF |
4735 | Database *database([Database sharedInstance]); |
4736 | @synchronized (database) { | |
4737 | NSArray *packages([database packages]); | |
f4db946e | 4738 | NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]); |
77801ff1 | 4739 | for (Package *package in packages) |
26e6829b | 4740 | if (![package uninstalled]) |
8cc8eb1c JF |
4741 | [installed addObject:package]; |
4742 | return installed; | |
26e6829b | 4743 | } } |
8cc8eb1c | 4744 | |
43f3d7f6 | 4745 | - (Package *) getPackageById:(NSString *)id { |
62cab237 JF |
4746 | if (Package *package = [[Database sharedInstance] packageWithName:id]) { |
4747 | [package parse]; | |
4748 | return package; | |
4749 | } else | |
4750 | return (Package *) [NSNull null]; | |
43f3d7f6 JF |
4751 | } |
4752 | ||
948db680 JF |
4753 | - (NSString *) getLocaleIdentifier { |
4754 | return Locale_ == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(Locale_); | |
4755 | } | |
4756 | ||
4757 | - (NSArray *) getPreferredLanguages { | |
4758 | return Languages_; | |
4759 | } | |
4760 | ||
43f3d7f6 JF |
4761 | - (NSArray *) statfs:(NSString *)path { |
4762 | struct statfs stat; | |
4763 | ||
4764 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
4765 | return nil; | |
4766 | ||
4767 | return [NSArray arrayWithObjects: | |
4768 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
4769 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
4770 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
4771 | nil]; | |
4772 | } | |
4773 | ||
e40620ab | 4774 | - (NSNumber *) du:(NSString *)path { |
d59938e3 JF |
4775 | NSNumber *value(nil); |
4776 | ||
d8b0f6e3 | 4777 | FILE *du(popen([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/du -ks %@", ShellEscape(path)] UTF8String], "r")); |
d59938e3 JF |
4778 | if (du != NULL) { |
4779 | char line[1024]; | |
4780 | while (fgets(line, sizeof(line), du) != NULL) { | |
4781 | size_t length(strlen(line)); | |
4782 | while (length != 0 && line[length - 1] == '\n') | |
4783 | line[--length] = '\0'; | |
4784 | if (char *tab = strchr(line, '\t')) { | |
4785 | *tab = '\0'; | |
4786 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
4787 | } | |
4788 | } | |
4789 | pclose(du); | |
4790 | } | |
4791 | ||
4792 | return value; | |
43f3d7f6 JF |
4793 | } |
4794 | ||
4795 | - (void) close { | |
e6417cea | 4796 | [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO]; |
43f3d7f6 JF |
4797 | } |
4798 | ||
518a552a JF |
4799 | - (NSNumber *) isReachable:(NSString *)name { |
4800 | return [NSNumber numberWithBool:IsReachable([name UTF8String])]; | |
4801 | } | |
4802 | ||
77801ff1 JF |
4803 | - (void) installPackages:(NSArray *)packages { |
4804 | [delegate_ performSelectorOnMainThread:@selector(installPackages:) withObject:packages waitUntilDone:NO]; | |
4805 | } | |
4806 | ||
8a126074 | 4807 | - (NSString *) substitutePackageNames:(NSString *)message { |
0c4fe0f4 | 4808 | NSMutableArray *words([[[message componentsSeparatedByString:@" "] mutableCopy] autorelease]); |
8a126074 JF |
4809 | for (size_t i(0), e([words count]); i != e; ++i) { |
4810 | NSString *word([words objectAtIndex:i]); | |
4811 | if (Package *package = [[Database sharedInstance] packageWithName:word]) | |
4812 | [words replaceObjectAtIndex:i withObject:[package name]]; | |
4813 | } | |
4814 | ||
4815 | return [words componentsJoinedByString:@" "]; | |
4816 | } | |
4817 | ||
ed5566c7 JF |
4818 | - (void) removeButton { |
4819 | [indirect_ removeButton]; | |
4820 | } | |
4821 | ||
43f3d7f6 JF |
4822 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
4823 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
4824 | } | |
4825 | ||
4826 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
4827 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
4828 | } | |
4829 | ||
c31c825d JF |
4830 | - (void) setBadgeValue:(id)value { |
4831 | [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO]; | |
4832 | } | |
4833 | ||
8366df5e JF |
4834 | - (void) setAllowsNavigationAction:(NSString *)value { |
4835 | [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO]; | |
4836 | } | |
4837 | ||
b8a5d89d JF |
4838 | - (void) setHidesBackButton:(NSString *)value { |
4839 | [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO]; | |
4840 | } | |
4841 | ||
5cdfcd6f JF |
4842 | - (void) setHidesNavigationBar:(NSString *)value { |
4843 | [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO]; | |
4844 | } | |
4845 | ||
82406217 JF |
4846 | - (void) setNavigationBarStyle:(NSString *)value { |
4847 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO]; | |
4848 | } | |
4849 | ||
00984204 JF |
4850 | - (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha { |
4851 | float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]); | |
4852 | UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]); | |
4853 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO]; | |
4854 | } | |
4855 | ||
36a20e14 JF |
4856 | - (void) setPasteboardString:(NSString *)value { |
4857 | [[objc_getClass("UIPasteboard") generalPasteboard] setString:value]; | |
4858 | } | |
4859 | ||
4860 | - (void) setPasteboardURL:(NSString *)value { | |
4861 | [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]]; | |
4862 | } | |
4863 | ||
673a6e1a | 4864 | - (void) setToken:(NSString *)token { |
7826a48c | 4865 | // XXX: the website expects this :/ |
673a6e1a JF |
4866 | } |
4867 | ||
8e3b68d4 JF |
4868 | - (void) scrollToBottom:(NSNumber *)animated { |
4869 | [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO]; | |
4870 | } | |
4871 | ||
43f3d7f6 | 4872 | - (void) setViewportWidth:(float)width { |
8dbdaafa | 4873 | [indirect_ setViewportWidthOnMainThread:width]; |
43f3d7f6 JF |
4874 | } |
4875 | ||
4876 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
4877 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
4878 | unsigned count([arguments count]); | |
4879 | id values[count]; | |
4880 | for (unsigned i(0); i != count; ++i) | |
4881 | values[i] = [arguments objectAtIndex:i]; | |
673e8fa3 | 4882 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; |
43f3d7f6 JF |
4883 | } |
4884 | ||
4885 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
a95e0405 JF |
4886 | if (reinterpret_cast<id>(value) == [WebUndefined undefined]) |
4887 | value = nil; | |
43f3d7f6 JF |
4888 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) |
4889 | table = nil; | |
4890 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
c390d3ab JF |
4891 | } |
4892 | ||
4893 | @end | |
4894 | /* }}} */ | |
f79a4512 | 4895 | |
2e1652a9 JF |
4896 | @interface NSURL (CydiaSecure) |
4897 | @end | |
4898 | ||
4899 | @implementation NSURL (CydiaSecure) | |
4900 | ||
4901 | - (bool) isCydiaSecure { | |
4902 | if ([[[self scheme] lowercaseString] isEqualToString:@"https"]) | |
4903 | return true; | |
4904 | ||
4905 | @synchronized (HostConfig_) { | |
4906 | if ([InsecureHosts_ containsObject:[self host]]) | |
4907 | return true; | |
4908 | } | |
4909 | ||
4910 | return false; | |
4911 | } | |
4912 | ||
4913 | @end | |
dd5f8161 | 4914 | |
80319240 | 4915 | /* Cydia Browser Controller {{{ */ |
a576488f | 4916 | @implementation CydiaWebViewController |
43f3d7f6 | 4917 | |
fe8e721f | 4918 | - (NSURL *) navigationURL { |
d323285e | 4919 | return request_ == nil ? nil : [NSURL URLWithString:[NSString stringWithFormat:@"cydia://url/%@", [[request_ URL] absoluteString]]]; |
fe8e721f GP |
4920 | } |
4921 | ||
3f9ab807 JF |
4922 | + (void) _initialize { |
4923 | [super _initialize]; | |
4924 | ||
7b33d201 | 4925 | Diversions_ = [NSMutableSet setWithCapacity:0]; |
775deead JF |
4926 | } |
4927 | ||
4928 | + (void) addDiversion:(Diversion *)diversion { | |
4929 | [Diversions_ addObject:diversion]; | |
4930 | } | |
4931 | ||
2634b249 JF |
4932 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4933 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
9e130bc2 JF |
4934 | [CydiaWebViewController didClearWindowObject:window forFrame:frame withCydia:cydia_]; |
4935 | } | |
01d93940 | 4936 | |
9e130bc2 | 4937 | + (void) didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame withCydia:(CydiaObject *)cydia { |
01d93940 JF |
4938 | WebDataSource *source([frame dataSource]); |
4939 | NSURLResponse *response([source response]); | |
4940 | NSURL *url([response URL]); | |
b8f1a18a JF |
4941 | NSString *scheme([[url scheme] lowercaseString]); |
4942 | ||
4943 | bool bridged(false); | |
8804004f | 4944 | |
48f1762f | 4945 | @synchronized (HostConfig_) { |
b8f1a18a JF |
4946 | if ([scheme isEqualToString:@"file"]) |
4947 | bridged = true; | |
4948 | else if ([scheme isEqualToString:@"https"]) | |
48f1762f | 4949 | if ([BridgedHosts_ containsObject:[url host]]) |
b8f1a18a | 4950 | bridged = true; |
48f1762f | 4951 | } |
b8f1a18a JF |
4952 | |
4953 | if (bridged) | |
9e130bc2 | 4954 | [window setValue:cydia forKey:@"cydia"]; |
43f3d7f6 JF |
4955 | } |
4956 | ||
22485d93 JF |
4957 | - (void) _setupMail:(MFMailComposeViewController *)controller { |
4958 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/cydia.log"] mimeType:@"text/plain" fileName:@"cydia.log"]; | |
4959 | ||
4960 | system("/usr/bin/dpkg -l >/tmp/dpkgl.log"); | |
4961 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/dpkgl.log"] mimeType:@"text/plain" fileName:@"dpkgl.log"]; | |
4962 | } | |
4963 | ||
f9b36dae JF |
4964 | - (NSURL *) URLWithURL:(NSURL *)url { |
4965 | return [Diversion divertURL:url]; | |
4966 | } | |
4967 | ||
9d1bf666 | 4968 | - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
85ae5f42 JF |
4969 | return [CydiaWebViewController requestWithHeaders:[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; |
4970 | } | |
4971 | ||
92de61aa JF |
4972 | - (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
4973 | return [CydiaWebViewController requestWithHeaders:[super webThreadWebView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; | |
4974 | } | |
4975 | ||
85ae5f42 | 4976 | + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request { |
0c4fe0f4 | 4977 | NSMutableURLRequest *copy([[request mutableCopy] autorelease]); |
9d1bf666 | 4978 | |
bc1cffbe | 4979 | NSURL *url([copy URL]); |
daa21f8e | 4980 | NSString *href([url absoluteString]); |
bc1cffbe JF |
4981 | NSString *host([url host]); |
4982 | ||
daa21f8e JF |
4983 | if ([href hasPrefix:@"https://cydia.saurik.com/TSS/"]) { |
4984 | if (NSString *agent = [copy valueForHTTPHeaderField:@"X-User-Agent"]) { | |
4985 | [copy setValue:agent forHTTPHeaderField:@"User-Agent"]; | |
4986 | [copy setValue:nil forHTTPHeaderField:@"X-User-Agent"]; | |
4987 | } | |
4988 | ||
4989 | [copy setValue:nil forHTTPHeaderField:@"Referer"]; | |
4990 | [copy setValue:nil forHTTPHeaderField:@"Origin"]; | |
4991 | ||
4992 | [copy setURL:[NSURL URLWithString:[@"http://gs.apple.com/TSS/" stringByAppendingString:[href substringFromIndex:29]]]]; | |
4993 | return copy; | |
4994 | } | |
4995 | ||
6f44d712 JF |
4996 | if ([copy valueForHTTPHeaderField:@"X-Cydia-Cf"] == nil) |
4997 | [copy setValue:[NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber] forHTTPHeaderField:@"X-Cydia-Cf"]; | |
1baae086 | 4998 | if (Machine_ != NULL && [copy valueForHTTPHeaderField:@"X-Machine"] == nil) |
9d1bf666 | 4999 | [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; |
247bedb6 | 5000 | |
7826a48c | 5001 | bool bridged; @synchronized (HostConfig_) { |
e7817a6b | 5002 | bridged = [BridgedHosts_ containsObject:host]; |
247bedb6 JF |
5003 | } |
5004 | ||
7826a48c JF |
5005 | if ([url isCydiaSecure] && bridged && UniqueID_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Id"] == nil) |
5006 | [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; | |
43f3d7f6 JF |
5007 | |
5008 | return copy; | |
a9a0661e JF |
5009 | } |
5010 | ||
77801ff1 JF |
5011 | - (void) setDelegate:(id)delegate { |
5012 | [super setDelegate:delegate]; | |
5013 | [cydia_ setDelegate:delegate]; | |
5014 | } | |
5015 | ||
c6cf66c7 | 5016 | - (NSString *) applicationNameForUserAgent { |
e967efd5 | 5017 | return UserAgent_; |
c6cf66c7 | 5018 | } |
43f3d7f6 | 5019 | |
c6cf66c7 JF |
5020 | - (id) init { |
5021 | if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) { | |
5022 | cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease]; | |
43f3d7f6 JF |
5023 | } return self; |
5024 | } | |
5025 | ||
29bb09d7 JF |
5026 | @end |
5027 | ||
5028 | @interface AppCacheController : CydiaWebViewController { | |
5029 | } | |
5030 | ||
5031 | @end | |
5032 | ||
5033 | @implementation AppCacheController | |
5034 | ||
5035 | - (void) didReceiveMemoryWarning { | |
6271cb57 | 5036 | // XXX: this doesn't work |
29bb09d7 JF |
5037 | } |
5038 | ||
2713be8e JF |
5039 | - (bool) retainsNetworkActivityIndicator { |
5040 | return false; | |
5041 | } | |
5042 | ||
43f3d7f6 | 5043 | @end |
80319240 | 5044 | /* }}} */ |
43f3d7f6 | 5045 | |
b1ca831d JF |
5046 | // CydiaScript {{{ |
5047 | @interface NSObject (CydiaScript) | |
5048 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; | |
5049 | @end | |
5050 | ||
5051 | @implementation NSObject (CydiaScript) | |
5052 | ||
5053 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5054 | return self; | |
5055 | } | |
5056 | ||
5057 | @end | |
5058 | ||
5059 | @implementation NSArray (CydiaScript) | |
5060 | ||
5061 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5062 | WebScriptObject *object([context evaluateWebScript:@"[]"]); | |
5063 | for (size_t i(0), e([self count]); i != e; ++i) | |
5064 | [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; | |
5065 | return object; | |
5066 | } | |
5067 | ||
5068 | @end | |
5069 | ||
5070 | @implementation NSDictionary (CydiaScript) | |
5071 | ||
5072 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5073 | WebScriptObject *object([context evaluateWebScript:@"({})"]); | |
5074 | for (id i in self) | |
5075 | [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; | |
5076 | return object; | |
5077 | } | |
5078 | ||
5079 | @end | |
5080 | // }}} | |
5081 | ||
5829aea2 GP |
5082 | /* Confirmation Controller {{{ */ |
5083 | bool DepSubstrate(const pkgCache::VerIterator &iterator) { | |
5084 | if (!iterator.end()) | |
5085 | for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) { | |
5086 | if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends) | |
5087 | continue; | |
5088 | pkgCache::PkgIterator package(dep.TargetPkg()); | |
5089 | if (package.end()) | |
5090 | continue; | |
5091 | if (strcmp(package.Name(), "mobilesubstrate") == 0) | |
5092 | return true; | |
5093 | } | |
5094 | ||
5095 | return false; | |
5096 | } | |
5097 | ||
adb61bda | 5098 | @protocol ConfirmationControllerDelegate |
674dce72 | 5099 | - (void) cancelAndClear:(bool)clear; |
b5e7eebb | 5100 | - (void) confirmWithNavigationController:(UINavigationController *)navigation; |
dc63e78f | 5101 | - (void) queue; |
36bb2ca2 | 5102 | @end |
2367a917 | 5103 | |
a576488f | 5104 | @interface ConfirmationController : CydiaWebViewController { |
770f2a8e | 5105 | _transient Database *database_; |
6ceb0959 | 5106 | |
7b33d201 | 5107 | _H<UIAlertView> essential_; |
6ceb0959 | 5108 | |
7b33d201 JF |
5109 | _H<NSDictionary> changes_; |
5110 | _H<NSMutableArray> issues_; | |
5111 | _H<NSDictionary> sizes_; | |
6ceb0959 | 5112 | |
a9a0661e | 5113 | BOOL substrate_; |
36bb2ca2 | 5114 | } |
dc5812ec | 5115 | |
b5e7eebb | 5116 | - (id) initWithDatabase:(Database *)database; |
b4d89997 | 5117 | |
dc5812ec JF |
5118 | @end |
5119 | ||
adb61bda | 5120 | @implementation ConfirmationController |
dc5812ec | 5121 | |
ab2cfc1e JF |
5122 | - (void) complete { |
5123 | if (substrate_) | |
be860cc8 | 5124 | RestartSubstrate_ = true; |
ab2cfc1e JF |
5125 | [delegate_ confirmWithNavigationController:[self navigationController]]; |
5126 | } | |
5127 | ||
b5e7eebb | 5128 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
3272e699 | 5129 | NSString *context([alert context]); |
9bedffaa | 5130 | |
1cedb821 | 5131 | if ([context isEqualToString:@"remove"]) { |
ab2cfc1e | 5132 | if (button == [alert cancelButtonIndex]) |
02069daf | 5133 | [self _doContinue]; |
ab2cfc1e | 5134 | else if (button == [alert firstOtherButtonIndex]) { |
d69dbfc5 | 5135 | [self performSelector:@selector(complete) withObject:nil afterDelay:0]; |
9bedffaa | 5136 | } |
9bedffaa | 5137 | |
3272e699 | 5138 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 5139 | } else if ([context isEqualToString:@"unable"]) { |
b5e7eebb | 5140 | [self dismissModalViewControllerAnimated:YES]; |
3272e699 GP |
5141 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
5142 | } else { | |
b5e7eebb GP |
5143 | [super alertView:alert clickedButtonAtIndex:button]; |
5144 | } | |
36bb2ca2 JF |
5145 | } |
5146 | ||
46c46f4f | 5147 | - (void) _doContinue { |
21ea11a4 | 5148 | [delegate_ cancelAndClear:NO]; |
12d3d98d | 5149 | [self dismissModalViewControllerAnimated:YES]; |
46c46f4f | 5150 | } |
bc11cf5b | 5151 | |
46c46f4f JF |
5152 | - (id) invokeDefaultMethodWithArguments:(NSArray *)args { |
5153 | [self performSelectorOnMainThread:@selector(_doContinue) withObject:nil waitUntilDone:NO]; | |
21ea11a4 GP |
5154 | return nil; |
5155 | } | |
5156 | ||
2634b249 JF |
5157 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5158 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
6ceb0959 | 5159 | |
781001d7 | 5160 | [window setValue:[[NSDictionary dictionaryWithObjectsAndKeys: |
7b33d201 JF |
5161 | (id) changes_, @"changes", |
5162 | (id) issues_, @"issues", | |
5163 | (id) sizes_, @"sizes", | |
781001d7 JF |
5164 | self, @"queue", |
5165 | nil] Cydia$webScriptObjectInContext:window] forKey:@"cydiaConfirm"]; | |
36bb2ca2 JF |
5166 | } |
5167 | ||
b5e7eebb GP |
5168 | - (id) initWithDatabase:(Database *)database { |
5169 | if ((self = [super init]) != nil) { | |
770f2a8e JF |
5170 | database_ = database; |
5171 | ||
6ceb0959 JF |
5172 | NSMutableArray *installs([NSMutableArray arrayWithCapacity:16]); |
5173 | NSMutableArray *reinstalls([NSMutableArray arrayWithCapacity:16]); | |
5174 | NSMutableArray *upgrades([NSMutableArray arrayWithCapacity:16]); | |
5175 | NSMutableArray *downgrades([NSMutableArray arrayWithCapacity:16]); | |
5176 | NSMutableArray *removes([NSMutableArray arrayWithCapacity:16]); | |
dc5812ec | 5177 | |
36bb2ca2 | 5178 | bool remove(false); |
dc5812ec | 5179 | |
6ceb0959 JF |
5180 | pkgCacheFile &cache([database_ cache]); |
5181 | NSArray *packages([database_ packages]); | |
a9a0661e JF |
5182 | pkgDepCache::Policy *policy([database_ policy]); |
5183 | ||
7b33d201 | 5184 | issues_ = [NSMutableArray arrayWithCapacity:4]; |
6ceb0959 | 5185 | |
c4dcf2c2 | 5186 | for (Package *package in packages) { |
6ceb0959 JF |
5187 | pkgCache::PkgIterator iterator([package iterator]); |
5188 | NSString *name([package id]); | |
5189 | ||
5190 | if ([package broken]) { | |
5191 | NSMutableArray *reasons([NSMutableArray arrayWithCapacity:4]); | |
5192 | ||
5193 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5194 | name, @"package", | |
5195 | reasons, @"reasons", | |
5196 | nil]]; | |
5197 | ||
5198 | pkgCache::VerIterator ver(cache[iterator].InstVerIter(cache)); | |
5199 | if (ver.end()) | |
5200 | continue; | |
5201 | ||
5202 | for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) { | |
5203 | pkgCache::DepIterator start; | |
5204 | pkgCache::DepIterator end; | |
5205 | dep.GlobOr(start, end); // ++dep | |
5206 | ||
5207 | if (!cache->IsImportantDep(end)) | |
5208 | continue; | |
5209 | if ((cache[end] & pkgDepCache::DepGInstall) != 0) | |
5210 | continue; | |
5211 | ||
810c9763 JF |
5212 | NSMutableArray *clauses([NSMutableArray arrayWithCapacity:4]); |
5213 | ||
5214 | [reasons addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5215 | [NSString stringWithUTF8String:start.DepType()], @"relationship", | |
5216 | clauses, @"clauses", | |
5217 | nil]]; | |
5218 | ||
6ceb0959 JF |
5219 | _forever { |
5220 | NSString *reason, *installed((NSString *) [WebUndefined undefined]); | |
5221 | ||
5222 | pkgCache::PkgIterator target(start.TargetPkg()); | |
5223 | if (target->ProvidesList != 0) | |
5224 | reason = @"missing"; | |
5225 | else { | |
5226 | pkgCache::VerIterator ver(cache[target].InstVerIter(cache)); | |
5227 | if (!ver.end()) { | |
5228 | reason = @"installed"; | |
5229 | installed = [NSString stringWithUTF8String:ver.VerStr()]; | |
5230 | } else if (!cache[target].CandidateVerIter(cache).end()) | |
5231 | reason = @"uninstalled"; | |
5232 | else if (target->ProvidesList == 0) | |
5233 | reason = @"uninstallable"; | |
5234 | else | |
5235 | reason = @"virtual"; | |
5236 | } | |
5237 | ||
3e5a9f5d | 5238 | NSDictionary *version(start.TargetVer() == 0 ? (NSDictionary *) [NSNull null] : [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5239 | [NSString stringWithUTF8String:start.CompType()], @"operator", |
5240 | [NSString stringWithUTF8String:start.TargetVer()], @"value", | |
5241 | nil]); | |
5242 | ||
810c9763 | 5243 | [clauses addObject:[NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5244 | [NSString stringWithUTF8String:start.TargetPkg().Name()], @"package", |
5245 | version, @"version", | |
5246 | reason, @"reason", | |
5247 | installed, @"installed", | |
5248 | nil]]; | |
5249 | ||
5250 | // yes, seriously. (wtf?) | |
5251 | if (start == end) | |
5252 | break; | |
5253 | ++start; | |
5254 | } | |
5255 | } | |
5256 | } | |
5257 | ||
36bb2ca2 | 5258 | pkgDepCache::StateCache &state(cache[iterator]); |
dc5812ec | 5259 | |
b4fd1197 | 5260 | static RegEx special_r("(firmware|gsc\\..*|cy\\+.*)"); |
2388b078 | 5261 | |
36bb2ca2 | 5262 | if (state.NewInstall()) |
6ceb0959 | 5263 | [installs addObject:name]; |
f8d15be2 | 5264 | // XXX: else if (state.Install()) |
36bb2ca2 | 5265 | else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall) |
6ceb0959 | 5266 | [reinstalls addObject:name]; |
f8d15be2 | 5267 | // XXX: move before previous if |
36bb2ca2 | 5268 | else if (state.Upgrade()) |
6ceb0959 | 5269 | [upgrades addObject:name]; |
36bb2ca2 | 5270 | else if (state.Downgrade()) |
6ceb0959 JF |
5271 | [downgrades addObject:name]; |
5272 | else if (!state.Delete()) | |
f8d15be2 | 5273 | // XXX: _assert(state.Keep()); |
6ceb0959 | 5274 | continue; |
1916f316 JF |
5275 | else if (special_r(name)) |
5276 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5277 | [NSNull null], @"package", | |
5278 | [NSArray arrayWithObjects: | |
5279 | [NSDictionary dictionaryWithObjectsAndKeys: | |
810c9763 JF |
5280 | @"Conflicts", @"relationship", |
5281 | [NSArray arrayWithObjects: | |
5282 | [NSDictionary dictionaryWithObjectsAndKeys: | |
5283 | name, @"package", | |
5284 | [NSNull null], @"version", | |
5285 | @"installed", @"reason", | |
5286 | nil], | |
5287 | nil], @"clauses", | |
1916f316 JF |
5288 | nil], |
5289 | nil], @"reasons", | |
5290 | nil]]; | |
5291 | else { | |
2388b078 | 5292 | if ([package essential]) |
36bb2ca2 | 5293 | remove = true; |
6ceb0959 JF |
5294 | [removes addObject:name]; |
5295 | } | |
a9a0661e JF |
5296 | |
5297 | substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator)); | |
5298 | substrate_ |= DepSubstrate(iterator.CurrentVer()); | |
36bb2ca2 | 5299 | } |
ec97ef06 | 5300 | |
36bb2ca2 JF |
5301 | if (!remove) |
5302 | essential_ = nil; | |
d791dce4 | 5303 | else if (Advanced_) { |
43f3d7f6 | 5304 | NSString *parenthetical(UCLocalize("PARENTHETICAL")); |
f79a4512 | 5305 | |
7b33d201 | 5306 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 5307 | initWithTitle:UCLocalize("REMOVING_ESSENTIALS") |
b5e7eebb GP |
5308 | message:UCLocalize("REMOVING_ESSENTIALS_EX") |
5309 | delegate:self | |
5310 | cancelButtonTitle:[NSString stringWithFormat:parenthetical, UCLocalize("CANCEL_OPERATION"), UCLocalize("SAFE")] | |
1aa29546 JF |
5311 | otherButtonTitles: |
5312 | [NSString stringWithFormat:parenthetical, UCLocalize("FORCE_REMOVAL"), UCLocalize("UNSAFE")], | |
5313 | nil | |
7b33d201 | 5314 | ] autorelease]; |
04fe1349 | 5315 | |
3272e699 | 5316 | [essential_ setContext:@"remove"]; |
9aaebfec | 5317 | [essential_ setNumberOfRows:2]; |
9bedffaa | 5318 | } else { |
7b33d201 | 5319 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 5320 | initWithTitle:UCLocalize("UNABLE_TO_COMPLY") |
b5e7eebb GP |
5321 | message:UCLocalize("UNABLE_TO_COMPLY_EX") |
5322 | delegate:self | |
5323 | cancelButtonTitle:UCLocalize("OKAY") | |
5324 | otherButtonTitles:nil | |
7b33d201 | 5325 | ] autorelease]; |
ec97ef06 | 5326 | |
b5e7eebb | 5327 | [essential_ setContext:@"unable"]; |
36bb2ca2 | 5328 | } |
ec97ef06 | 5329 | |
7b33d201 | 5330 | changes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5331 | installs, @"installs", |
5332 | reinstalls, @"reinstalls", | |
5333 | upgrades, @"upgrades", | |
5334 | downgrades, @"downgrades", | |
5335 | removes, @"removes", | |
affeffc7 | 5336 | nil]; |
dc5812ec | 5337 | |
7b33d201 | 5338 | sizes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5339 | [NSNumber numberWithInteger:[database_ fetcher].FetchNeeded()], @"downloading", |
5340 | [NSNumber numberWithInteger:[database_ fetcher].PartialPresent()], @"resuming", | |
affeffc7 | 5341 | nil]; |
dc5812ec | 5342 | |
90351d93 | 5343 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/confirm/", UI_]]]; |
36bb2ca2 | 5344 | } return self; |
b4d89997 | 5345 | } |
8da60fb7 | 5346 | |
e6124cb6 JF |
5347 | - (UIBarButtonItem *) leftButton { |
5348 | return [[[UIBarButtonItem alloc] | |
5349 | initWithTitle:UCLocalize("CANCEL") | |
5350 | style:UIBarButtonItemStylePlain | |
5351 | target:self | |
5352 | action:@selector(cancelButtonClicked) | |
5353 | ] autorelease]; | |
5354 | } | |
5355 | ||
614cd4f1 | 5356 | #if !AlwaysReload |
bcde1e70 | 5357 | - (void) applyRightButton { |
6ceb0959 | 5358 | if ([issues_ count] == 0 && ![self isLoading]) |
dd9de556 JF |
5359 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
5360 | initWithTitle:UCLocalize("CONFIRM") | |
2761d574 | 5361 | style:UIBarButtonItemStyleDone |
dd9de556 JF |
5362 | target:self |
5363 | action:@selector(confirmButtonClicked) | |
5364 | ] autorelease]]; | |
5365 | else | |
bcde1e70 | 5366 | [[self navigationItem] setRightBarButtonItem:nil]; |
affeffc7 | 5367 | } |
bcde1e70 | 5368 | #endif |
affeffc7 | 5369 | |
b5e7eebb | 5370 | - (void) cancelButtonClicked { |
21ea11a4 | 5371 | [delegate_ cancelAndClear:YES]; |
05452929 | 5372 | [self dismissModalViewControllerAnimated:YES]; |
affeffc7 JF |
5373 | } |
5374 | ||
9487f027 | 5375 | #if !AlwaysReload |
b5e7eebb | 5376 | - (void) confirmButtonClicked { |
affeffc7 | 5377 | if (essential_ != nil) |
b5e7eebb | 5378 | [essential_ show]; |
ab2cfc1e JF |
5379 | else |
5380 | [self complete]; | |
affeffc7 | 5381 | } |
9487f027 | 5382 | #endif |
affeffc7 | 5383 | |
36bb2ca2 JF |
5384 | @end |
5385 | /* }}} */ | |
8da60fb7 | 5386 | |
aaae308d JF |
5387 | /* Progress Data {{{ */ |
5388 | @interface CydiaProgressData : NSObject { | |
5389 | _transient id delegate_; | |
5390 | ||
5391 | bool running_; | |
b0b11d99 | 5392 | float percent_; |
aaae308d | 5393 | |
bcbac8f7 JF |
5394 | float current_; |
5395 | float total_; | |
5396 | float speed_; | |
5397 | ||
aaae308d JF |
5398 | _H<NSMutableArray> events_; |
5399 | _H<NSString> title_; | |
5400 | ||
5401 | _H<NSString> status_; | |
5402 | _H<NSString> finish_; | |
5403 | } | |
5404 | ||
5405 | @end | |
5406 | ||
5407 | @implementation CydiaProgressData | |
5408 | ||
5409 | + (NSArray *) _attributeKeys { | |
5410 | return [NSArray arrayWithObjects: | |
bcbac8f7 | 5411 | @"current", |
aaae308d JF |
5412 | @"events", |
5413 | @"finish", | |
b0b11d99 | 5414 | @"percent", |
aaae308d | 5415 | @"running", |
bcbac8f7 | 5416 | @"speed", |
aaae308d | 5417 | @"title", |
bcbac8f7 | 5418 | @"total", |
aaae308d JF |
5419 | nil]; |
5420 | } | |
5421 | ||
5422 | - (NSArray *) attributeKeys { | |
5423 | return [[self class] _attributeKeys]; | |
5424 | } | |
5425 | ||
5426 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
5427 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
5428 | } | |
5429 | ||
5430 | - (id) init { | |
5431 | if ((self = [super init]) != nil) { | |
5432 | events_ = [NSMutableArray arrayWithCapacity:32]; | |
5433 | } return self; | |
5434 | } | |
5435 | ||
353dda5b JF |
5436 | - (id) delegate { |
5437 | return delegate_; | |
5438 | } | |
5439 | ||
aaae308d JF |
5440 | - (void) setDelegate:(id)delegate { |
5441 | delegate_ = delegate; | |
5442 | } | |
5443 | ||
b0b11d99 JF |
5444 | - (void) setPercent:(float)value { |
5445 | percent_ = value; | |
aaae308d JF |
5446 | } |
5447 | ||
b0b11d99 JF |
5448 | - (NSNumber *) percent { |
5449 | return [NSNumber numberWithFloat:percent_]; | |
aaae308d JF |
5450 | } |
5451 | ||
bcbac8f7 JF |
5452 | - (void) setCurrent:(float)value { |
5453 | current_ = value; | |
5454 | } | |
5455 | ||
5456 | - (NSNumber *) current { | |
5457 | return [NSNumber numberWithFloat:current_]; | |
5458 | } | |
5459 | ||
5460 | - (void) setTotal:(float)value { | |
5461 | total_ = value; | |
5462 | } | |
5463 | ||
5464 | - (NSNumber *) total { | |
5465 | return [NSNumber numberWithFloat:total_]; | |
5466 | } | |
5467 | ||
5468 | - (void) setSpeed:(float)value { | |
5469 | speed_ = value; | |
5470 | } | |
5471 | ||
5472 | - (NSNumber *) speed { | |
5473 | return [NSNumber numberWithFloat:speed_]; | |
5474 | } | |
5475 | ||
aaae308d JF |
5476 | - (NSArray *) events { |
5477 | return events_; | |
5478 | } | |
5479 | ||
5480 | - (void) removeAllEvents { | |
5481 | [events_ removeAllObjects]; | |
5482 | } | |
5483 | ||
5484 | - (void) addEvent:(CydiaProgressEvent *)event { | |
5485 | [events_ addObject:event]; | |
5486 | } | |
5487 | ||
5488 | - (void) setTitle:(NSString *)text { | |
5489 | title_ = text; | |
5490 | } | |
5491 | ||
5492 | - (NSString *) title { | |
5493 | return title_; | |
5494 | } | |
5495 | ||
5496 | - (void) setFinish:(NSString *)text { | |
5497 | finish_ = text; | |
5498 | } | |
5499 | ||
5500 | - (NSString *) finish { | |
5501 | return (id) finish_ ?: [NSNull null]; | |
5502 | } | |
5503 | ||
5504 | - (void) setRunning:(bool)running { | |
5505 | running_ = running; | |
5506 | } | |
5507 | ||
5508 | - (NSNumber *) running { | |
5509 | return running_ ? (NSNumber *) kCFBooleanTrue : (NSNumber *) kCFBooleanFalse; | |
5510 | } | |
5511 | ||
5512 | @end | |
5513 | /* }}} */ | |
adb61bda | 5514 | /* Progress Controller {{{ */ |
a576488f | 5515 | @interface ProgressController : CydiaWebViewController < |
36bb2ca2 JF |
5516 | ProgressDelegate |
5517 | > { | |
8b29f8e6 | 5518 | _transient Database *database_; |
bf7c998c | 5519 | _H<CydiaProgressData, 1> progress_; |
aaae308d | 5520 | unsigned cancel_; |
2367a917 JF |
5521 | } |
5522 | ||
b5e7eebb | 5523 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
2367a917 | 5524 | |
6915b806 | 5525 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title; |
2367a917 | 5526 | |
6915b806 | 5527 | - (void) setTitle:(NSString *)title; |
aaae308d | 5528 | - (void) setCancellable:(bool)cancellable; |
bd150f54 | 5529 | |
36bb2ca2 JF |
5530 | @end |
5531 | ||
adb61bda | 5532 | @implementation ProgressController |
2367a917 JF |
5533 | |
5534 | - (void) dealloc { | |
6915b806 | 5535 | [database_ setProgressDelegate:nil]; |
2367a917 JF |
5536 | [super dealloc]; |
5537 | } | |
5538 | ||
63ae52be JF |
5539 | - (UIBarButtonItem *) leftButton { |
5540 | return cancel_ == 1 ? [[[UIBarButtonItem alloc] | |
3c62d654 JF |
5541 | initWithTitle:UCLocalize("CANCEL") |
5542 | style:UIBarButtonItemStylePlain | |
5543 | target:self | |
5544 | action:@selector(cancel) | |
63ae52be JF |
5545 | ] autorelease] : nil; |
5546 | } | |
5547 | ||
5548 | - (void) updateCancel { | |
5549 | [super applyLeftButton]; | |
3c62d654 JF |
5550 | } |
5551 | ||
b5e7eebb GP |
5552 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { |
5553 | if ((self = [super init]) != nil) { | |
8b29f8e6 | 5554 | database_ = database; |
36bb2ca2 | 5555 | delegate_ = delegate; |
ec97ef06 | 5556 | |
6915b806 JF |
5557 | [database_ setProgressDelegate:self]; |
5558 | ||
aaae308d JF |
5559 | progress_ = [[[CydiaProgressData alloc] init] autorelease]; |
5560 | [progress_ setDelegate:self]; | |
3c62d654 | 5561 | |
90351d93 | 5562 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/progress/", UI_]]]; |
3c62d654 JF |
5563 | |
5564 | [scroller_ setBackgroundColor:[UIColor blackColor]]; | |
5565 | ||
5566 | [[self navigationItem] setHidesBackButton:YES]; | |
5567 | ||
5568 | [self updateCancel]; | |
36bb2ca2 | 5569 | } return self; |
2367a917 JF |
5570 | } |
5571 | ||
aaae308d JF |
5572 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5573 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
5574 | [window setValue:progress_ forKey:@"cydiaProgress"]; | |
5575 | } | |
bc11cf5b | 5576 | |
aaae308d JF |
5577 | - (void) updateProgress { |
5578 | [self dispatchEvent:@"CydiaProgressUpdate"]; | |
5579 | } | |
b5e7eebb | 5580 | |
b5e7eebb | 5581 | - (void) viewWillAppear:(BOOL)animated { |
14e4ff09 | 5582 | [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlack]; |
3c62d654 | 5583 | [super viewWillAppear:animated]; |
2367a917 JF |
5584 | } |
5585 | ||
aaae308d | 5586 | - (void) close { |
ef494bd8 RP |
5587 | UpdateExternalStatus(0); |
5588 | ||
9dd3045d JF |
5589 | if (Finish_ > 1) |
5590 | [delegate_ saveState]; | |
5591 | ||
670a0494 JF |
5592 | switch (Finish_) { |
5593 | case 0: | |
2925cbba | 5594 | [delegate_ returnToCydia]; |
670a0494 JF |
5595 | break; |
5596 | ||
5597 | case 1: | |
c4899376 JF |
5598 | [delegate_ terminateWithSuccess]; |
5599 | /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) | |
5600 | [delegate_ suspendWithAnimation:YES]; | |
5601 | else | |
5602 | [delegate_ suspend];*/ | |
670a0494 JF |
5603 | break; |
5604 | ||
5605 | case 2: | |
985d2dff | 5606 | _trace(); |
ef812071 | 5607 | goto reload; |
670a0494 JF |
5608 | |
5609 | case 3: | |
985d2dff | 5610 | _trace(); |
ef812071 JF |
5611 | goto reload; |
5612 | ||
a3755a1e JF |
5613 | reload: { |
5614 | UIProgressHUD *hud([delegate_ addProgressHUD]); | |
5615 | [hud setText:UCLocalize("LOADING")]; | |
42bbf27d | 5616 | [delegate_ performSelector:@selector(reloadSpringBoard) withObject:nil afterDelay:0.5]; |
317eb8e3 JF |
5617 | return; |
5618 | } | |
670a0494 JF |
5619 | |
5620 | case 4: | |
985d2dff | 5621 | _trace(); |
0e371502 JF |
5622 | if (void (*SBReboot)(mach_port_t) = reinterpret_cast<void (*)(mach_port_t)>(dlsym(RTLD_DEFAULT, "SBReboot"))) |
5623 | SBReboot(SBSSpringBoardServerPort()); | |
5624 | else | |
bb0fe3c9 | 5625 | reboot2(RB_AUTOBOOT); |
670a0494 | 5626 | break; |
bc8cd583 | 5627 | } |
aaae308d JF |
5628 | |
5629 | [super close]; | |
670a0494 | 5630 | } |
bd150f54 | 5631 | |
6915b806 | 5632 | - (void) setTitle:(NSString *)title { |
aaae308d JF |
5633 | [progress_ setTitle:title]; |
5634 | [self updateProgress]; | |
5635 | } | |
5636 | ||
5637 | - (UIBarButtonItem *) rightButton { | |
d53628b6 | 5638 | return [[progress_ running] boolValue] ? [super rightButton] : [[[UIBarButtonItem alloc] |
aaae308d JF |
5639 | initWithTitle:UCLocalize("CLOSE") |
5640 | style:UIBarButtonItemStylePlain | |
5641 | target:self | |
5642 | action:@selector(close) | |
5643 | ] autorelease]; | |
6915b806 JF |
5644 | } |
5645 | ||
5646 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title { | |
5647 | UpdateExternalStatus(1); | |
5648 | ||
aaae308d | 5649 | [progress_ setRunning:true]; |
6915b806 | 5650 | [self setTitle:title]; |
aaae308d | 5651 | // implicit updateProgress |
6915b806 | 5652 | |
140710ba | 5653 | SHA1SumValue notifyconf; { |
6915b806 JF |
5654 | FileFd file; |
5655 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
5656 | _error->Discard(); | |
5657 | else { | |
5658 | MMap mmap(file, MMap::ReadOnly); | |
5659 | SHA1Summation sha1; | |
5660 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5661 | notifyconf = sha1.Result(); |
6915b806 JF |
5662 | } |
5663 | } | |
5664 | ||
140710ba | 5665 | SHA1SumValue springlist; { |
6915b806 JF |
5666 | FileFd file; |
5667 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
5668 | _error->Discard(); | |
5669 | else { | |
5670 | MMap mmap(file, MMap::ReadOnly); | |
5671 | SHA1Summation sha1; | |
5672 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5673 | springlist = sha1.Result(); |
6915b806 JF |
5674 | } |
5675 | } | |
5676 | ||
5677 | if (invocation != nil) { | |
5678 | [invocation yieldToSelector:@selector(invoke)]; | |
aaae308d | 5679 | [self setTitle:@"COMPLETE"]; |
6915b806 | 5680 | } |
670a0494 | 5681 | |
22f8bed9 | 5682 | if (Finish_ < 4) { |
70d45c1e JF |
5683 | FileFd file; |
5684 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
5685 | _error->Discard(); | |
5686 | else { | |
5687 | MMap mmap(file, MMap::ReadOnly); | |
5688 | SHA1Summation sha1; | |
5689 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5690 | if (!(notifyconf == sha1.Result())) |
70d45c1e JF |
5691 | Finish_ = 4; |
5692 | } | |
22f8bed9 JF |
5693 | } |
5694 | ||
affeffc7 | 5695 | if (Finish_ < 3) { |
70d45c1e JF |
5696 | FileFd file; |
5697 | if (!file.Open(SpringBoard_, 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 (!(springlist == sha1.Result())) |
70d45c1e JF |
5704 | Finish_ = 3; |
5705 | } | |
dddbc481 JF |
5706 | } |
5707 | ||
be860cc8 JF |
5708 | if (Finish_ < 2) { |
5709 | if (RestartSubstrate_) | |
5710 | Finish_ = 2; | |
5711 | } | |
5712 | ||
5713 | RestartSubstrate_ = false; | |
5714 | ||
bc8cd583 | 5715 | switch (Finish_) { |
aaae308d JF |
5716 | case 0: [progress_ setFinish:UCLocalize("RETURN_TO_CYDIA")]; break; /* XXX: Maybe UCLocalize("DONE")? */ |
5717 | case 1: [progress_ setFinish:UCLocalize("CLOSE_CYDIA")]; break; | |
5718 | case 2: [progress_ setFinish:UCLocalize("RESTART_SPRINGBOARD")]; break; | |
5719 | case 3: [progress_ setFinish:UCLocalize("RELOAD_SPRINGBOARD")]; break; | |
5720 | case 4: [progress_ setFinish:UCLocalize("REBOOT_DEVICE")]; break; | |
bc8cd583 JF |
5721 | } |
5722 | ||
eb403f34 | 5723 | UpdateExternalStatus(Finish_ == 0 ? 0 : 2); |
ef494bd8 | 5724 | |
aaae308d JF |
5725 | [progress_ setRunning:false]; |
5726 | [self updateProgress]; | |
5727 | ||
5728 | [self applyRightButton]; | |
31f3cfff JF |
5729 | } |
5730 | ||
6915b806 | 5731 | - (void) addProgressEvent:(CydiaProgressEvent *)event { |
aaae308d JF |
5732 | [progress_ addEvent:event]; |
5733 | [self updateProgress]; | |
baf80942 JF |
5734 | } |
5735 | ||
ff2d5dcd | 5736 | - (bool) isProgressCancelled { |
aaae308d JF |
5737 | return cancel_ == 2; |
5738 | } | |
5739 | ||
5740 | - (void) cancel { | |
5741 | cancel_ = 2; | |
5742 | [self updateCancel]; | |
5743 | } | |
5744 | ||
5745 | - (void) setCancellable:(bool)cancellable { | |
5746 | unsigned cancel(cancel_); | |
5747 | ||
5748 | if (!cancellable) | |
5749 | cancel_ = 0; | |
5750 | else if (cancel_ == 0) | |
5751 | cancel_ = 1; | |
5752 | ||
5753 | if (cancel != cancel_) | |
5754 | [self updateCancel]; | |
5755 | } | |
5756 | ||
5757 | - (void) setProgressCancellable:(NSNumber *)cancellable { | |
5758 | [self setCancellable:[cancellable boolValue]]; | |
baf80942 JF |
5759 | } |
5760 | ||
d885343d | 5761 | - (void) setProgressPercent:(NSNumber *)percent { |
b0b11d99 | 5762 | [progress_ setPercent:[percent floatValue]]; |
aaae308d | 5763 | [self updateProgress]; |
49048579 JF |
5764 | } |
5765 | ||
bcbac8f7 JF |
5766 | - (void) setProgressStatus:(NSDictionary *)status { |
5767 | if (status == nil) { | |
5768 | [progress_ setCurrent:0]; | |
5769 | [progress_ setTotal:0]; | |
5770 | [progress_ setSpeed:0]; | |
5771 | } else { | |
5772 | [progress_ setPercent:[[status objectForKey:@"Percent"] floatValue]]; | |
5773 | ||
5774 | [progress_ setCurrent:[[status objectForKey:@"Current"] floatValue]]; | |
5775 | [progress_ setTotal:[[status objectForKey:@"Total"] floatValue]]; | |
5776 | [progress_ setSpeed:[[status objectForKey:@"Speed"] floatValue]]; | |
5777 | } | |
5778 | ||
5779 | [self updateProgress]; | |
5780 | } | |
5781 | ||
36bb2ca2 JF |
5782 | @end |
5783 | /* }}} */ | |
dc088e63 | 5784 | |
46aa9775 | 5785 | /* Package Cell {{{ */ |
a9311516 | 5786 | @interface PackageCell : CyteTableViewCell < |
b97fcfc6 | 5787 | CyteTableViewCellDelegate |
c21004b9 | 5788 | > { |
7b33d201 JF |
5789 | _H<UIImage> icon_; |
5790 | _H<NSString> name_; | |
5791 | _H<NSString> description_; | |
3bd1c2a2 | 5792 | bool commercial_; |
7b33d201 JF |
5793 | _H<NSString> source_; |
5794 | _H<UIImage> badge_; | |
7b33d201 | 5795 | _H<UIImage> placard_; |
59f3d290 | 5796 | bool summarized_; |
36bb2ca2 | 5797 | } |
723a0072 | 5798 | |
36bb2ca2 | 5799 | - (PackageCell *) init; |
59f3d290 | 5800 | - (void) setPackage:(Package *)package asSummary:(bool)summary; |
ec97ef06 | 5801 | |
327624b6 JF |
5802 | - (void) drawContentRect:(CGRect)rect; |
5803 | ||
5804 | @end | |
5805 | ||
36bb2ca2 JF |
5806 | @implementation PackageCell |
5807 | ||
36bb2ca2 | 5808 | - (PackageCell *) init { |
327624b6 JF |
5809 | CGRect frame(CGRectMake(0, 0, 320, 74)); |
5810 | if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) { | |
5811 | UIView *content([self contentView]); | |
5812 | CGRect bounds([content bounds]); | |
04fe1349 | 5813 | |
b97fcfc6 | 5814 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
04fe1349 JF |
5815 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5816 | [content addSubview:content_]; | |
5817 | ||
327624b6 | 5818 | [content_ setDelegate:self]; |
327624b6 | 5819 | [content_ setOpaque:YES]; |
36bb2ca2 | 5820 | } return self; |
b4d89997 | 5821 | } |
b19871dd | 5822 | |
003fc610 | 5823 | - (NSString *) accessibilityLabel { |
353dda5b | 5824 | return name_; |
003fc610 GP |
5825 | } |
5826 | ||
59f3d290 JF |
5827 | - (void) setPackage:(Package *)package asSummary:(bool)summary { |
5828 | summarized_ = summary; | |
5829 | ||
7b33d201 JF |
5830 | icon_ = nil; |
5831 | name_ = nil; | |
5832 | description_ = nil; | |
5833 | source_ = nil; | |
5834 | badge_ = nil; | |
5835 | placard_ = nil; | |
7b33d201 | 5836 | |
80132602 JF |
5837 | if (package == nil) |
5838 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
5839 | else { | |
5840 | [package parse]; | |
31f3cfff | 5841 | |
80132602 | 5842 | Source *source = [package source]; |
b19871dd | 5843 | |
80132602 | 5844 | icon_ = [package icon]; |
78de2878 | 5845 | |
80132602 JF |
5846 | if (NSString *name = [package name]) |
5847 | name_ = [NSString stringWithString:name]; | |
ef055c6c | 5848 | |
9374f6b0 | 5849 | if (NSString *description = [package shortDescription]) |
80132602 | 5850 | description_ = [NSString stringWithString:description]; |
ef055c6c | 5851 | |
80132602 | 5852 | commercial_ = [package isCommercial]; |
36bb2ca2 | 5853 | |
80132602 JF |
5854 | NSString *label = nil; |
5855 | bool trusted = false; | |
b19871dd | 5856 | |
80132602 JF |
5857 | if (source != nil) { |
5858 | label = [source label]; | |
5859 | trusted = [source trusted]; | |
5860 | } else if ([[package id] isEqualToString:@"firmware"]) | |
5861 | label = UCLocalize("APPLE"); | |
5862 | else | |
5863 | label = [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("UNKNOWN"), UCLocalize("LOCAL")]; | |
b19871dd | 5864 | |
80132602 | 5865 | NSString *from(label); |
a54b1c10 | 5866 | |
80132602 JF |
5867 | NSString *section = [package simpleSection]; |
5868 | if (section != nil && ![section isEqualToString:label]) { | |
5869 | section = [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
5870 | from = [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), from, section]; | |
5871 | } | |
a54b1c10 | 5872 | |
80132602 | 5873 | source_ = [NSString stringWithFormat:UCLocalize("FROM"), from]; |
36bb2ca2 | 5874 | |
80132602 JF |
5875 | if (NSString *purpose = [package primaryPurpose]) |
5876 | badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]; | |
c390d3ab | 5877 | |
80132602 JF |
5878 | UIColor *color; |
5879 | NSString *placard; | |
5880 | ||
5881 | if (NSString *mode = [package mode]) { | |
5882 | if ([mode isEqualToString:@"REMOVE"] || [mode isEqualToString:@"PURGE"]) { | |
5883 | color = RemovingColor_; | |
86a333c6 | 5884 | placard = @"removing"; |
80132602 JF |
5885 | } else { |
5886 | color = InstallingColor_; | |
86a333c6 | 5887 | placard = @"installing"; |
80132602 | 5888 | } |
d832908d | 5889 | } else { |
80132602 JF |
5890 | color = [UIColor whiteColor]; |
5891 | ||
5892 | if ([package installed] != nil) | |
5893 | placard = @"installed"; | |
5894 | else | |
5895 | placard = nil; | |
d832908d JF |
5896 | } |
5897 | ||
80132602 | 5898 | [content_ setBackgroundColor:color]; |
d832908d | 5899 | |
80132602 JF |
5900 | if (placard != nil) |
5901 | placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/%@.png", App_, placard]]; | |
d832908d JF |
5902 | } |
5903 | ||
d832908d | 5904 | [self setNeedsDisplay]; |
327624b6 | 5905 | [content_ setNeedsDisplay]; |
3bd1c2a2 JF |
5906 | } |
5907 | ||
59f3d290 | 5908 | - (void) drawSummaryContentRect:(CGRect)rect { |
555aaf71 | 5909 | bool highlighted(highlighted_); |
ef055c6c | 5910 | float width([self bounds].size.width); |
dc63e78f | 5911 | |
59f3d290 JF |
5912 | if (icon_ != nil) { |
5913 | CGRect rect; | |
5914 | rect.size = [(UIImage *) icon_ size]; | |
5915 | ||
25c1dafb | 5916 | while (rect.size.width > 16 || rect.size.height > 16) { |
7e1f9f6a JF |
5917 | rect.size.width /= 2; |
5918 | rect.size.height /= 2; | |
5919 | } | |
59f3d290 | 5920 | |
86a333c6 JF |
5921 | rect.origin.x = 19 - rect.size.width / 2; |
5922 | rect.origin.y = 19 - rect.size.height / 2; | |
59f3d290 | 5923 | |
8323c1b9 | 5924 | [icon_ drawInRect:Retina(rect)]; |
59f3d290 JF |
5925 | } |
5926 | ||
5927 | if (badge_ != nil) { | |
5928 | CGRect rect; | |
5929 | rect.size = [(UIImage *) badge_ size]; | |
5930 | ||
5931 | rect.size.width /= 4; | |
5932 | rect.size.height /= 4; | |
5933 | ||
86a333c6 JF |
5934 | rect.origin.x = 25 - rect.size.width / 2; |
5935 | rect.origin.y = 25 - rect.size.height / 2; | |
59f3d290 | 5936 | |
8323c1b9 | 5937 | [badge_ drawInRect:Retina(rect)]; |
59f3d290 JF |
5938 | } |
5939 | ||
5d0438dc | 5940 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
59f3d290 JF |
5941 | UISetColor(White_); |
5942 | ||
5943 | if (!highlighted) | |
5944 | UISetColor(commercial_ ? Purple_ : Black_); | |
b129e6d9 | 5945 | [name_ drawAtPoint:CGPointMake(36, 8) forWidth:(width - (placard_ == nil ? 68 : 94)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
59f3d290 JF |
5946 | |
5947 | if (placard_ != nil) | |
86a333c6 | 5948 | [placard_ drawAtPoint:CGPointMake(width - 52, 11)]; |
59f3d290 JF |
5949 | } |
5950 | ||
5951 | - (void) drawNormalContentRect:(CGRect)rect { | |
5952 | bool highlighted(highlighted_); | |
5953 | float width([self bounds].size.width); | |
b19871dd | 5954 | |
baf80942 JF |
5955 | if (icon_ != nil) { |
5956 | CGRect rect; | |
7b33d201 | 5957 | rect.size = [(UIImage *) icon_ size]; |
baf80942 | 5958 | |
25c1dafb | 5959 | while (rect.size.width > 32 || rect.size.height > 32) { |
7e1f9f6a JF |
5960 | rect.size.width /= 2; |
5961 | rect.size.height /= 2; | |
5962 | } | |
baf80942 JF |
5963 | |
5964 | rect.origin.x = 25 - rect.size.width / 2; | |
5965 | rect.origin.y = 25 - rect.size.height / 2; | |
5966 | ||
8323c1b9 | 5967 | [icon_ drawInRect:Retina(rect)]; |
baf80942 | 5968 | } |
b19871dd | 5969 | |
c390d3ab | 5970 | if (badge_ != nil) { |
f79c810d | 5971 | CGRect rect; |
7b33d201 | 5972 | rect.size = [(UIImage *) badge_ size]; |
f79c810d JF |
5973 | |
5974 | rect.size.width /= 2; | |
5975 | rect.size.height /= 2; | |
5976 | ||
5977 | rect.origin.x = 36 - rect.size.width / 2; | |
5978 | rect.origin.y = 36 - rect.size.height / 2; | |
c390d3ab | 5979 | |
8323c1b9 | 5980 | [badge_ drawInRect:Retina(rect)]; |
c390d3ab JF |
5981 | } |
5982 | ||
5d0438dc | 5983 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
f641a0e5 | 5984 | UISetColor(White_); |
b19871dd | 5985 | |
555aaf71 | 5986 | if (!highlighted) |
3bd1c2a2 | 5987 | UISetColor(commercial_ ? Purple_ : Black_); |
b129e6d9 JF |
5988 | [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
5989 | [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; | |
b19871dd | 5990 | |
555aaf71 | 5991 | if (!highlighted) |
3bd1c2a2 | 5992 | UISetColor(commercial_ ? Purplish_ : Gray_); |
b129e6d9 | 5993 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:NSLineBreakByTruncatingTail]; |
670a0494 JF |
5994 | |
5995 | if (placard_ != nil) | |
01d93940 | 5996 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; |
ec97ef06 JF |
5997 | } |
5998 | ||
59f3d290 JF |
5999 | - (void) drawContentRect:(CGRect)rect { |
6000 | if (summarized_) | |
6001 | [self drawSummaryContentRect:rect]; | |
6002 | else | |
6003 | [self drawNormalContentRect:rect]; | |
6004 | } | |
6005 | ||
8da60fb7 JF |
6006 | @end |
6007 | /* }}} */ | |
36bb2ca2 | 6008 | /* Section Cell {{{ */ |
a9311516 | 6009 | @interface SectionCell : CyteTableViewCell < |
b97fcfc6 | 6010 | CyteTableViewCellDelegate |
c21004b9 | 6011 | > { |
7b33d201 JF |
6012 | _H<NSString> basic_; |
6013 | _H<NSString> section_; | |
6014 | _H<NSString> name_; | |
6015 | _H<NSString> count_; | |
6016 | _H<UIImage> icon_; | |
6017 | _H<UISwitch> switch_; | |
6d9712c4 | 6018 | BOOL editing_; |
b4d89997 | 6019 | } |
b19871dd | 6020 | |
6d9712c4 | 6021 | - (void) setSection:(Section *)section editing:(BOOL)editing; |
36bb2ca2 | 6022 | |
8da60fb7 JF |
6023 | @end |
6024 | ||
36bb2ca2 | 6025 | @implementation SectionCell |
8da60fb7 | 6026 | |
46aa9775 GP |
6027 | - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
6028 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
8dbf3abc | 6029 | icon_ = [UIImage imageNamed:@"folder.png"]; |
6415105e | 6030 | // XXX: this initial frame is wrong, but is fixed later |
7b33d201 | 6031 | switch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)] autorelease]; |
46aa9775 GP |
6032 | [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; |
6033 | ||
6034 | UIView *content([self contentView]); | |
6035 | CGRect bounds([content bounds]); | |
6036 | ||
b97fcfc6 | 6037 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
46aa9775 GP |
6038 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6039 | [content addSubview:content_]; | |
6040 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
6041 | ||
6042 | [content_ setDelegate:self]; | |
b4d89997 | 6043 | } return self; |
b19871dd JF |
6044 | } |
6045 | ||
6d9712c4 | 6046 | - (void) onSwitch:(id)sender { |
a171abd4 | 6047 | NSMutableDictionary *metadata([Sections_ objectForKey:basic_]); |
6d9712c4 JF |
6048 | if (metadata == nil) { |
6049 | metadata = [NSMutableDictionary dictionaryWithCapacity:2]; | |
0010fa77 | 6050 | [Sections_ setObject:metadata forKey:basic_]; |
6d9712c4 JF |
6051 | } |
6052 | ||
46aa9775 | 6053 | [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; |
6d9712c4 JF |
6054 | } |
6055 | ||
6056 | - (void) setSection:(Section *)section editing:(BOOL)editing { | |
6057 | if (editing != editing_) { | |
6058 | if (editing_) | |
6059 | [switch_ removeFromSuperview]; | |
6060 | else | |
6061 | [self addSubview:switch_]; | |
6062 | editing_ = editing; | |
6063 | } | |
6064 | ||
7b33d201 JF |
6065 | basic_ = nil; |
6066 | section_ = nil; | |
6067 | name_ = nil; | |
6068 | count_ = nil; | |
6d9712c4 | 6069 | |
36bb2ca2 | 6070 | if (section == nil) { |
7b33d201 | 6071 | name_ = UCLocalize("ALL_PACKAGES"); |
f641a0e5 | 6072 | count_ = nil; |
36bb2ca2 | 6073 | } else { |
e59669fd | 6074 | basic_ = [section name]; |
677b8415 | 6075 | section_ = [section localized]; |
0010fa77 | 6076 | |
7b33d201 | 6077 | name_ = section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : (NSString *) section_; |
3e5a9f5d | 6078 | count_ = [NSString stringWithFormat:@"%zd", [section count]]; |
6d9712c4 JF |
6079 | |
6080 | if (editing_) | |
46aa9775 | 6081 | [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; |
36bb2ca2 | 6082 | } |
46aa9775 | 6083 | |
c21004b9 | 6084 | [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; |
0abb648c JF |
6085 | [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; |
6086 | ||
46aa9775 | 6087 | [content_ setNeedsDisplay]; |
f641a0e5 JF |
6088 | } |
6089 | ||
77801ff1 JF |
6090 | - (void) setFrame:(CGRect)frame { |
6091 | [super setFrame:frame]; | |
46aa9775 | 6092 | |
77801ff1 | 6093 | CGRect rect([switch_ frame]); |
6415105e | 6094 | [switch_ setFrame:CGRectMake(frame.size.width - rect.size.width - 9, 9, rect.size.width, rect.size.height)]; |
77801ff1 JF |
6095 | } |
6096 | ||
003fc610 GP |
6097 | - (NSString *) accessibilityLabel { |
6098 | return name_; | |
6099 | } | |
6100 | ||
46aa9775 | 6101 | - (void) drawContentRect:(CGRect)rect { |
8cfba088 | 6102 | bool highlighted(highlighted_ && !editing_); |
bc11cf5b | 6103 | |
86a333c6 | 6104 | [icon_ drawInRect:CGRectMake(7, 7, 32, 32)]; |
f641a0e5 | 6105 | |
5d0438dc | 6106 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
f641a0e5 JF |
6107 | UISetColor(White_); |
6108 | ||
46aa9775 | 6109 | float width(rect.size.width); |
58241d4c | 6110 | if (editing_) |
6c1ec3c7 | 6111 | width -= 9 + [switch_ frame].size.width; |
58241d4c | 6112 | |
555aaf71 JF |
6113 | if (!highlighted) |
6114 | UISetColor(Black_); | |
b129e6d9 | 6115 | [name_ drawAtPoint:CGPointMake(48, 12) forWidth:(width - 58) withFont:Font18_ lineBreakMode:NSLineBreakByTruncatingTail]; |
6d9712c4 | 6116 | |
f641a0e5 JF |
6117 | CGSize size = [count_ sizeWithFont:Font14_]; |
6118 | ||
86a333c6 | 6119 | UISetColor(Folder_); |
f641a0e5 | 6120 | if (count_ != nil) |
8323c1b9 | 6121 | [count_ drawAtPoint:CGPointMake(Retina(10 + (30 - size.width) / 2), 18) withFont:Font12Bold_]; |
b19871dd JF |
6122 | } |
6123 | ||
36bb2ca2 JF |
6124 | @end |
6125 | /* }}} */ | |
b19871dd | 6126 | |
ab398adf | 6127 | /* File Table {{{ */ |
cd79e8cf | 6128 | @interface FileTable : CyteViewController < |
c21004b9 JF |
6129 | UITableViewDataSource, |
6130 | UITableViewDelegate | |
6131 | > { | |
36bb2ca2 | 6132 | _transient Database *database_; |
7b33d201 JF |
6133 | _H<Package> package_; |
6134 | _H<NSString> name_; | |
6135 | _H<NSMutableArray> files_; | |
bf7c998c | 6136 | _H<UITableView, 2> list_; |
ab398adf | 6137 | } |
b19871dd | 6138 | |
b5e7eebb | 6139 | - (id) initWithDatabase:(Database *)database; |
ab398adf JF |
6140 | - (void) setPackage:(Package *)package; |
6141 | ||
6142 | @end | |
6143 | ||
6144 | @implementation FileTable | |
6145 | ||
eb30da80 | 6146 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
ab398adf JF |
6147 | return files_ == nil ? 0 : [files_ count]; |
6148 | } | |
6149 | ||
21ea11a4 GP |
6150 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
6151 | return 24.0f; | |
6152 | }*/ | |
ab398adf | 6153 | |
46aa9775 | 6154 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
b5e7eebb GP |
6155 | static NSString *reuseIdentifier = @"Cell"; |
6156 | ||
46aa9775 GP |
6157 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
6158 | if (cell == nil) { | |
6159 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
6160 | [cell setFont:[UIFont systemFontOfSize:16]]; | |
ab398adf | 6161 | } |
46aa9775 | 6162 | [cell setText:[files_ objectAtIndex:indexPath.row]]; |
c21004b9 | 6163 | [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
b5e7eebb | 6164 | |
46aa9775 | 6165 | return cell; |
ab398adf | 6166 | } |
b19871dd | 6167 | |
fe8e721f GP |
6168 | - (NSURL *) navigationURL { |
6169 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/files", [package_ id]]]; | |
6170 | } | |
6171 | ||
6172 | - (void) loadView { | |
e8cbebe4 | 6173 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; |
fe8e721f GP |
6174 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6175 | [list_ setRowHeight:24.0f]; | |
7b33d201 | 6176 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 6177 | [list_ setDelegate:self]; |
e8cbebe4 | 6178 | [self setView:list_]; |
fe8e721f GP |
6179 | } |
6180 | ||
6181 | - (void) viewDidLoad { | |
7d887d0b JF |
6182 | [super viewDidLoad]; |
6183 | ||
fe8e721f GP |
6184 | [[self navigationItem] setTitle:UCLocalize("INSTALLED_FILES")]; |
6185 | } | |
6186 | ||
6187 | - (void) releaseSubviews { | |
fe8e721f | 6188 | list_ = nil; |
7be3eb32 | 6189 | |
4f9acb7c JF |
6190 | package_ = nil; |
6191 | files_ = nil; | |
6192 | ||
7be3eb32 | 6193 | [super releaseSubviews]; |
fe8e721f GP |
6194 | } |
6195 | ||
b5e7eebb GP |
6196 | - (id) initWithDatabase:(Database *)database { |
6197 | if ((self = [super init]) != nil) { | |
ab398adf | 6198 | database_ = database; |
ab398adf JF |
6199 | } return self; |
6200 | } | |
6201 | ||
6202 | - (void) setPackage:(Package *)package { | |
7b33d201 JF |
6203 | package_ = nil; |
6204 | name_ = nil; | |
ab398adf | 6205 | |
4f9acb7c | 6206 | files_ = [NSMutableArray arrayWithCapacity:32]; |
ab398adf JF |
6207 | |
6208 | if (package != nil) { | |
7b33d201 JF |
6209 | package_ = package; |
6210 | name_ = [package id]; | |
ab398adf | 6211 | |
affeffc7 JF |
6212 | if (NSArray *files = [package files]) |
6213 | [files_ addObjectsFromArray:files]; | |
ab398adf | 6214 | |
9ea8d159 JF |
6215 | if ([files_ count] != 0) { |
6216 | if ([[files_ objectAtIndex:0] isEqualToString:@"/."]) | |
6217 | [files_ removeObjectAtIndex:0]; | |
a54b1c10 | 6218 | [files_ sortUsingSelector:@selector(compareByPath:)]; |
2388b078 JF |
6219 | |
6220 | NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8]; | |
6221 | [stack addObject:@"/"]; | |
6222 | ||
6223 | for (int i(0), e([files_ count]); i != e; ++i) { | |
6224 | NSString *file = [files_ objectAtIndex:i]; | |
6225 | while (![file hasPrefix:[stack lastObject]]) | |
6226 | [stack removeLastObject]; | |
6227 | NSString *directory = [stack lastObject]; | |
6228 | [stack addObject:[file stringByAppendingString:@"/"]]; | |
6229 | [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", | |
a54b1c10 | 6230 | ([stack count] - 2) * 3, "", |
2388b078 JF |
6231 | [file substringFromIndex:[directory length]] |
6232 | ]]; | |
6233 | } | |
ab398adf JF |
6234 | } |
6235 | } | |
6236 | ||
6237 | [list_ reloadData]; | |
6238 | } | |
6239 | ||
ab398adf | 6240 | - (void) reloadData { |
fe8e721f GP |
6241 | [super reloadData]; |
6242 | ||
ab398adf | 6243 | [self setPackage:[database_ packageWithName:name_]]; |
ab398adf | 6244 | } |
b4d89997 | 6245 | |
b4d89997 | 6246 | @end |
36bb2ca2 | 6247 | /* }}} */ |
adb61bda | 6248 | /* Package Controller {{{ */ |
a576488f | 6249 | @interface CYPackageController : CydiaWebViewController < |
9daa7f25 DH |
6250 | UIActionSheetDelegate |
6251 | > { | |
770f2a8e | 6252 | _transient Database *database_; |
5d79f7bf JF |
6253 | _H<Package> package_; |
6254 | _H<NSString> name_; | |
3bd1c2a2 | 6255 | bool commercial_; |
3217d35f | 6256 | std::vector<std::pair<_H<NSString>, _H<NSString>>> buttons_; |
5d79f7bf | 6257 | _H<UIBarButtonItem> button_; |
b31b87cc JF |
6258 | } |
6259 | ||
f050e4d9 | 6260 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer; |
b4d89997 | 6261 | |
36bb2ca2 | 6262 | @end |
b4d89997 | 6263 | |
f6e13561 | 6264 | @implementation CYPackageController |
b4d89997 | 6265 | |
fe8e721f | 6266 | - (NSURL *) navigationURL { |
5d79f7bf | 6267 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@", (id) name_]]; |
fe8e721f GP |
6268 | } |
6269 | ||
5a09ae08 | 6270 | - (void) _clickButtonWithName:(NSString *)name { |
3217d35f | 6271 | if ([name isEqualToString:@"CLEAR"]) |
dc63e78f | 6272 | [delegate_ clearPackage:package_]; |
3217d35f | 6273 | else if ([name isEqualToString:@"INSTALL"]) |
5a09ae08 | 6274 | [delegate_ installPackage:package_]; |
3217d35f | 6275 | else if ([name isEqualToString:@"REINSTALL"]) |
5a09ae08 | 6276 | [delegate_ installPackage:package_]; |
3217d35f | 6277 | else if ([name isEqualToString:@"REMOVE"]) |
5a09ae08 | 6278 | [delegate_ removePackage:package_]; |
3217d35f | 6279 | else if ([name isEqualToString:@"UPGRADE"]) |
5a09ae08 JF |
6280 | [delegate_ installPackage:package_]; |
6281 | else _assert(false); | |
6282 | } | |
6283 | ||
674dce72 | 6284 | - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { |
1cedb821 | 6285 | NSString *context([sheet context]); |
5a09ae08 | 6286 | |
1cedb821 | 6287 | if ([context isEqualToString:@"modify"]) { |
674dce72 | 6288 | if (button != [sheet cancelButtonIndex]) { |
60b6595c JF |
6289 | if (IsWildcat_) |
6290 | [self performSelector:@selector(_clickButtonWithName:) withObject:buttons_[button].first afterDelay:0]; | |
6291 | else | |
6292 | [self _clickButtonWithName:buttons_[button].first]; | |
674dce72 | 6293 | } |
bc11cf5b | 6294 | |
60b6595c | 6295 | [sheet dismissWithClickedButtonIndex:button animated:YES]; |
674dce72 | 6296 | } |
b4d89997 | 6297 | } |
2367a917 | 6298 | |
3e9c9e85 | 6299 | - (bool) _allowJavaScriptPanel { |
3bd1c2a2 | 6300 | return commercial_; |
3e9c9e85 JF |
6301 | } |
6302 | ||
9487f027 | 6303 | #if !AlwaysReload |
9daa7f25 | 6304 | - (void) _customButtonClicked { |
3217d35f | 6305 | size_t count(buttons_.size()); |
670a0494 JF |
6306 | if (count == 0) |
6307 | return; | |
2367a917 | 6308 | |
5a09ae08 | 6309 | if (count == 1) |
3217d35f | 6310 | [self _clickButtonWithName:buttons_[0].first]; |
5a09ae08 | 6311 | else { |
674dce72 | 6312 | NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; |
3217d35f JF |
6313 | for (const auto &button : buttons_) |
6314 | [buttons addObject:button.second]; | |
36bb2ca2 | 6315 | |
674dce72 | 6316 | UIActionSheet *sheet = [[[UIActionSheet alloc] |
9ea8d159 | 6317 | initWithTitle:nil |
36bb2ca2 | 6318 | delegate:self |
674dce72 GP |
6319 | cancelButtonTitle:nil |
6320 | destructiveButtonTitle:nil | |
6321 | otherButtonTitles:nil | |
6322 | ] autorelease]; | |
6323 | ||
6324 | for (NSString *button in buttons) [sheet addButtonWithTitle:button]; | |
6325 | if (!IsWildcat_) { | |
6326 | [sheet addButtonWithTitle:UCLocalize("CANCEL")]; | |
6327 | [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; | |
6328 | } | |
6329 | [sheet setContext:@"modify"]; | |
bc11cf5b | 6330 | |
b5e7eebb | 6331 | [delegate_ showActionSheet:sheet fromItem:[[self navigationItem] rightBarButtonItem]]; |
36bb2ca2 | 6332 | } |
b4d89997 | 6333 | } |
12b59862 | 6334 | |
2e6c1426 | 6335 | - (void) reloadButtonClicked { |
07d0e88e JF |
6336 | if (commercial_ && function_ == nil && [package_ uninstalled]) |
6337 | return; | |
6338 | [self customButtonClicked]; | |
2fad210a GP |
6339 | } |
6340 | ||
6341 | - (void) applyLoadingTitle { | |
6342 | // Don't show "Loading" as the title. Ever. | |
2e6c1426 | 6343 | } |
a5938ea5 DH |
6344 | |
6345 | - (UIBarButtonItem *) rightButton { | |
2634b249 | 6346 | return button_; |
a5938ea5 | 6347 | } |
9487f027 | 6348 | #endif |
2367a917 | 6349 | |
77259cab JF |
6350 | - (void) setPageColor:(UIColor *)color { |
6351 | return [super setPageColor:nil]; | |
6352 | } | |
6353 | ||
f050e4d9 | 6354 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer { |
b5e7eebb | 6355 | if ((self = [super init]) != nil) { |
36bb2ca2 | 6356 | database_ = database; |
5612913e | 6357 | name_ = name == nil ? @"" : [NSString stringWithString:name]; |
f050e4d9 | 6358 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]] withReferrer:referrer]; |
36bb2ca2 | 6359 | } return self; |
dc5812ec JF |
6360 | } |
6361 | ||
57e8b225 | 6362 | - (void) reloadData { |
9dac415b JF |
6363 | [super reloadData]; |
6364 | ||
57e8b225 | 6365 | package_ = [database_ packageWithName:name_]; |
36bb2ca2 | 6366 | |
3217d35f | 6367 | buttons_.clear(); |
5a09ae08 | 6368 | |
57e8b225 | 6369 | if (package_ != nil) { |
5d79f7bf | 6370 | [(Package *) package_ parse]; |
68d927e2 | 6371 | |
57e8b225 | 6372 | commercial_ = [package_ isCommercial]; |
36bb2ca2 | 6373 | |
dc63e78f | 6374 | if ([package_ mode] != nil) |
3217d35f | 6375 | buttons_.push_back(std::make_pair(@"CLEAR", UCLocalize("CLEAR"))); |
5a09ae08 | 6376 | if ([package_ source] == nil); |
31f3cfff | 6377 | else if ([package_ upgradableAndEssential:NO]) |
3217d35f | 6378 | buttons_.push_back(std::make_pair(@"UPGRADE", UCLocalize("UPGRADE"))); |
6a155117 | 6379 | else if ([package_ uninstalled]) |
3217d35f | 6380 | buttons_.push_back(std::make_pair(@"INSTALL", UCLocalize("INSTALL"))); |
5a09ae08 | 6381 | else |
3217d35f | 6382 | buttons_.push_back(std::make_pair(@"REINSTALL", UCLocalize("REINSTALL"))); |
6a155117 | 6383 | if (![package_ uninstalled]) |
3217d35f | 6384 | buttons_.push_back(std::make_pair(@"REMOVE", UCLocalize("REMOVE"))); |
2634b249 | 6385 | } |
f79a4512 | 6386 | |
2634b249 | 6387 | NSString *title; |
3217d35f | 6388 | switch (buttons_.size()) { |
2634b249 | 6389 | case 0: title = nil; break; |
3217d35f | 6390 | case 1: title = buttons_[0].second; break; |
2634b249 | 6391 | default: title = UCLocalize("MODIFY"); break; |
36bb2ca2 | 6392 | } |
2634b249 | 6393 | |
5d79f7bf | 6394 | button_ = [[[UIBarButtonItem alloc] |
2634b249 JF |
6395 | initWithTitle:title |
6396 | style:UIBarButtonItemStylePlain | |
6397 | target:self | |
6398 | action:@selector(customButtonClicked) | |
5d79f7bf | 6399 | ] autorelease]; |
b5e7eebb | 6400 | } |
f79a4512 | 6401 | |
3bd1c2a2 JF |
6402 | - (bool) isLoading { |
6403 | return commercial_ ? [super isLoading] : false; | |
9fe5e5f8 JF |
6404 | } |
6405 | ||
b4d89997 JF |
6406 | @end |
6407 | /* }}} */ | |
5829aea2 | 6408 | |
f50860ee | 6409 | /* Package List Controller {{{ */ |
cd79e8cf | 6410 | @interface PackageListController : CyteViewController < |
c21004b9 JF |
6411 | UITableViewDataSource, |
6412 | UITableViewDelegate | |
6413 | > { | |
36bb2ca2 | 6414 | _transient Database *database_; |
0175295c | 6415 | unsigned era_; |
56bf1e78 | 6416 | _H<NSArray> packages_; |
e5491e28 | 6417 | _H<NSArray> sections_; |
bf7c998c | 6418 | _H<UITableView, 2> list_; |
aeeb755b JF |
6419 | |
6420 | _H<NSArray> thumbs_; | |
6421 | std::vector<NSInteger> offset_; | |
6422 | ||
7b33d201 | 6423 | _H<NSString> title_; |
56bf1e78 | 6424 | unsigned reloading_; |
dc5812ec JF |
6425 | } |
6426 | ||
f50860ee | 6427 | - (id) initWithDatabase:(Database *)database title:(NSString *)title; |
b4d89997 | 6428 | - (void) setDelegate:(id)delegate; |
a0be02eb | 6429 | - (void) resetCursor; |
59f3d290 | 6430 | - (void) clearData; |
b4d89997 | 6431 | |
e5491e28 JF |
6432 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages; |
6433 | ||
b4d89997 JF |
6434 | @end |
6435 | ||
f50860ee | 6436 | @implementation PackageListController |
b4d89997 | 6437 | |
f050e4d9 JF |
6438 | - (NSURL *) referrerURL { |
6439 | return [self navigationURL]; | |
6440 | } | |
6441 | ||
59f3d290 JF |
6442 | - (bool) isSummarized { |
6443 | return false; | |
6444 | } | |
6445 | ||
9c5737d5 JF |
6446 | - (bool) showsSections { |
6447 | return true; | |
6448 | } | |
6449 | ||
f50860ee GP |
6450 | - (void) deselectWithAnimation:(BOOL)animated { |
6451 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
6452 | } | |
6453 | ||
bfb45dcb GP |
6454 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve { |
6455 | CGRect base = [[self view] bounds]; | |
6456 | base.size.height -= bounds.size.height; | |
6457 | base.origin = [list_ frame].origin; | |
6458 | ||
6459 | [UIView beginAnimations:nil context:NULL]; | |
6460 | [UIView setAnimationBeginsFromCurrentState:YES]; | |
6461 | [UIView setAnimationCurve:curve]; | |
6462 | [UIView setAnimationDuration:duration]; | |
6463 | [list_ setFrame:base]; | |
6464 | [UIView commitAnimations]; | |
6465 | } | |
6466 | ||
6467 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration { | |
6468 | [self resizeForKeyboardBounds:bounds duration:duration curve:UIViewAnimationCurveLinear]; | |
6469 | } | |
6470 | ||
6471 | - (void) resizeForKeyboardBounds:(CGRect)bounds { | |
6472 | [self resizeForKeyboardBounds:bounds duration:0]; | |
6473 | } | |
6474 | ||
655c7ded JF |
6475 | - (void) getKeyboardCurve:(UIViewAnimationCurve *)curve duration:(NSTimeInterval *)duration forNotification:(NSNotification *)notification { |
6476 | if (&UIKeyboardAnimationCurveUserInfoKey == NULL) | |
6477 | *curve = UIViewAnimationCurveEaseInOut; | |
6478 | else | |
6479 | [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:curve]; | |
6480 | ||
6481 | if (&UIKeyboardAnimationDurationUserInfoKey == NULL) | |
6482 | *duration = 0.3; | |
6483 | else | |
6484 | [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:duration]; | |
6485 | } | |
6486 | ||
bfb45dcb GP |
6487 | - (void) keyboardWillShow:(NSNotification *)notification { |
6488 | CGRect bounds; | |
6489 | CGPoint center; | |
bfb45dcb GP |
6490 | [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds]; |
6491 | [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:¢er]; | |
655c7ded JF |
6492 | |
6493 | NSTimeInterval duration; | |
6494 | UIViewAnimationCurve curve; | |
6495 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; | |
bfb45dcb | 6496 | |
8323c1b9 | 6497 | CGRect kbframe = CGRectMake(Retina(center.x - bounds.size.width / 2), Retina(center.y - bounds.size.height / 2), bounds.size.width, bounds.size.height); |
38991110 | 6498 | UIViewController *base = self; |
19f2d77f JF |
6499 | while ([base parentOrPresentingViewController] != nil) |
6500 | base = [base parentOrPresentingViewController]; | |
38991110 | 6501 | CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]]; |
bfb45dcb GP |
6502 | CGRect intersection = CGRectIntersection(viewframe, kbframe); |
6503 | ||
2e35f65f | 6504 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) |
0b7516cf | 6505 | intersection.size.height += CYStatusBarHeight(); |
2e35f65f | 6506 | |
bfb45dcb GP |
6507 | [self resizeForKeyboardBounds:intersection duration:duration curve:curve]; |
6508 | } | |
6509 | ||
6510 | - (void) keyboardWillHide:(NSNotification *)notification { | |
6511 | NSTimeInterval duration; | |
6512 | UIViewAnimationCurve curve; | |
655c7ded | 6513 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; |
bfb45dcb GP |
6514 | |
6515 | [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve]; | |
6516 | } | |
6517 | ||
6518 | - (void) viewWillAppear:(BOOL)animated { | |
6519 | [super viewWillAppear:animated]; | |
6520 | ||
6521 | [self resizeForKeyboardBounds:CGRectZero]; | |
6522 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
6523 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
6524 | } | |
6525 | ||
6526 | - (void) viewWillDisappear:(BOOL)animated { | |
6527 | [super viewWillDisappear:animated]; | |
6528 | ||
6529 | [self resizeForKeyboardBounds:CGRectZero]; | |
6530 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
6531 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
6532 | } | |
6533 | ||
f50860ee GP |
6534 | - (void) viewDidAppear:(BOOL)animated { |
6535 | [super viewDidAppear:animated]; | |
6536 | [self deselectWithAnimation:animated]; | |
6537 | } | |
6538 | ||
6539 | - (void) didSelectPackage:(Package *)package { | |
f050e4d9 | 6540 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id] withReferrer:[[self referrerURL] absoluteString]] autorelease]); |
f50860ee GP |
6541 | [view setDelegate:delegate_]; |
6542 | [[self navigationController] pushViewController:view animated:YES]; | |
6543 | } | |
6544 | ||
327624b6 JF |
6545 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
6546 | NSInteger count([sections_ count]); | |
6547 | return count == 0 ? 1 : count; | |
b4d89997 | 6548 | } |
2367a917 | 6549 | |
327624b6 | 6550 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
a891c345 | 6551 | if ([sections_ count] == 0 || [[sections_ objectAtIndex:section] count] == 0) |
327624b6 | 6552 | return nil; |
b4d89997 JF |
6553 | return [[sections_ objectAtIndex:section] name]; |
6554 | } | |
dc5812ec | 6555 | |
327624b6 JF |
6556 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
6557 | if ([sections_ count] == 0) | |
6558 | return 0; | |
6559 | return [[sections_ objectAtIndex:section] count]; | |
b4d89997 | 6560 | } |
dc5812ec | 6561 | |
327624b6 | 6562 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
0175295c JF |
6563 | @synchronized (database_) { |
6564 | if ([database_ era] != era_) | |
6565 | return nil; | |
6566 | ||
327624b6 JF |
6567 | Section *section([sections_ objectAtIndex:[path section]]); |
6568 | NSInteger row([path row]); | |
6569 | Package *package([packages_ objectAtIndex:([section row] + row)]); | |
0175295c JF |
6570 | return [[package retain] autorelease]; |
6571 | } } | |
dc5812ec | 6572 | |
327624b6 | 6573 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
c21004b9 | 6574 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
327624b6 JF |
6575 | if (cell == nil) |
6576 | cell = [[[PackageCell alloc] init] autorelease]; | |
60bef540 JF |
6577 | |
6578 | Package *package([database_ packageWithName:[[self packageAtIndexPath:path] id]]); | |
6579 | [cell setPackage:package asSummary:[self isSummarized]]; | |
327624b6 | 6580 | return cell; |
b4d89997 | 6581 | } |
dc5812ec | 6582 | |
f50860ee | 6583 | - (void) tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path { |
327624b6 | 6584 | Package *package([self packageAtIndexPath:path]); |
59c6ae22 | 6585 | package = [database_ packageWithName:[package id]]; |
f50860ee | 6586 | [self didSelectPackage:package]; |
327624b6 JF |
6587 | } |
6588 | ||
6589 | - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { | |
aeeb755b | 6590 | return thumbs_; |
327624b6 JF |
6591 | } |
6592 | ||
a891c345 | 6593 | - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { |
aeeb755b | 6594 | return offset_[index]; |
dc5812ec JF |
6595 | } |
6596 | ||
59f3d290 JF |
6597 | - (void) updateHeight { |
6598 | [list_ setRowHeight:([self isSummarized] ? 38 : 73)]; | |
6599 | } | |
6600 | ||
f50860ee GP |
6601 | - (id) initWithDatabase:(Database *)database title:(NSString *)title { |
6602 | if ((self = [super init]) != nil) { | |
36bb2ca2 | 6603 | database_ = database; |
f50860ee GP |
6604 | title_ = [title copy]; |
6605 | [[self navigationItem] setTitle:title_]; | |
61cc4dbc JF |
6606 | } return self; |
6607 | } | |
dc5812ec | 6608 | |
61cc4dbc | 6609 | - (void) loadView { |
b62b3788 JF |
6610 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
6611 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
6612 | [self setView:view]; | |
6613 | ||
6614 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; | |
61cc4dbc | 6615 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
b62b3788 | 6616 | [view addSubview:list_]; |
1527b095 | 6617 | |
61cc4dbc JF |
6618 | // XXX: is 20 the most optimal number here? |
6619 | [list_ setSectionIndexMinimumDisplayRowCount:20]; | |
59f3d290 | 6620 | |
61cc4dbc JF |
6621 | [(UITableView *) list_ setDataSource:self]; |
6622 | [list_ setDelegate:self]; | |
6623 | ||
6624 | [self updateHeight]; | |
6625 | } | |
6626 | ||
6627 | - (void) releaseSubviews { | |
6628 | list_ = nil; | |
7be3eb32 | 6629 | |
4f9acb7c JF |
6630 | packages_ = nil; |
6631 | sections_ = nil; | |
aeeb755b JF |
6632 | |
6633 | thumbs_ = nil; | |
6634 | offset_.clear(); | |
4f9acb7c | 6635 | |
7be3eb32 | 6636 | [super releaseSubviews]; |
dc5812ec JF |
6637 | } |
6638 | ||
6639 | - (void) setDelegate:(id)delegate { | |
2367a917 | 6640 | delegate_ = delegate; |
b4d89997 JF |
6641 | } |
6642 | ||
3025d5b4 JF |
6643 | - (bool) shouldYield { |
6644 | return false; | |
6645 | } | |
b4d89997 | 6646 | |
56bf1e78 JF |
6647 | - (bool) shouldBlock { |
6648 | return false; | |
6649 | } | |
6650 | ||
9c5737d5 | 6651 | - (NSMutableArray *) _reloadPackages { |
695fdd5c | 6652 | @synchronized (database_) { |
c28bc6f1 JF |
6653 | era_ = [database_ era]; |
6654 | NSArray *packages([database_ packages]); | |
6655 | ||
9c5737d5 | 6656 | return [NSMutableArray arrayWithArray:packages]; |
695fdd5c | 6657 | } } |
3025d5b4 JF |
6658 | |
6659 | - (void) _reloadData { | |
56bf1e78 JF |
6660 | if (reloading_ != 0) { |
6661 | reloading_ = 2; | |
6662 | return; | |
6663 | } | |
6664 | ||
e5491e28 | 6665 | NSMutableArray *packages; |
ae60e2c1 | 6666 | |
0c8f53c0 | 6667 | reload: |
3025d5b4 | 6668 | if ([self shouldYield]) { |
bec28dda JF |
6669 | do { |
6670 | UIProgressHUD *hud; | |
56bf1e78 | 6671 | |
bec28dda JF |
6672 | if (![self shouldBlock]) |
6673 | hud = nil; | |
6674 | else { | |
6675 | hud = [delegate_ addProgressHUD]; | |
6676 | [hud setText:UCLocalize("LOADING")]; | |
6677 | } | |
56bf1e78 | 6678 | |
56bf1e78 | 6679 | reloading_ = 1; |
ae60e2c1 | 6680 | packages = [self yieldToSelector:@selector(_reloadPackages)]; |
bec28dda JF |
6681 | |
6682 | if (hud != nil) | |
6683 | [delegate_ removeProgressHUD:hud]; | |
56bf1e78 | 6684 | } while (reloading_ == 2); |
3025d5b4 | 6685 | } else { |
ae60e2c1 | 6686 | packages = [self _reloadPackages]; |
3025d5b4 | 6687 | } |
36bb2ca2 | 6688 | |
0c8f53c0 JF |
6689 | @synchronized (database_) { |
6690 | if (era_ != [database_ era]) | |
6691 | goto reload; | |
6692 | reloading_ = 0; | |
6693 | ||
aeeb755b JF |
6694 | thumbs_ = nil; |
6695 | offset_.clear(); | |
6696 | ||
ae60e2c1 | 6697 | packages_ = packages; |
aeeb755b JF |
6698 | |
6699 | if ([self showsSections]) | |
6700 | sections_ = [self sectionsForPackages:packages]; | |
6701 | else { | |
6702 | Section *section([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); | |
6703 | [section setCount:[packages_ count]]; | |
6704 | sections_ = [NSArray arrayWithObject:section]; | |
6705 | } | |
ae60e2c1 | 6706 | |
e5491e28 JF |
6707 | [self updateHeight]; |
6708 | ||
6709 | _profile(PackageTable$reloadData$List) | |
6710 | [(UITableView *) list_ setDataSource:self]; | |
6711 | [list_ reloadData]; | |
6712 | _end | |
1a83afc6 JF |
6713 | } |
6714 | ||
6715 | PrintTimes(); | |
6716 | } | |
e5491e28 JF |
6717 | |
6718 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { | |
aeeb755b JF |
6719 | Section *prefix([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); |
6720 | size_t end([packages count]); | |
b4d89997 | 6721 | |
aeeb755b JF |
6722 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); |
6723 | Section *section(prefix); | |
c4dcf2c2 | 6724 | |
aeeb755b JF |
6725 | thumbs_ = CollationThumbs_; |
6726 | offset_ = CollationOffset_; | |
b4d89997 | 6727 | |
aeeb755b JF |
6728 | size_t offset(0); |
6729 | size_t offsets([CollationStarts_ count]); | |
b4d89997 | 6730 | |
aeeb755b JF |
6731 | NSString *start([CollationStarts_ objectAtIndex:offset]); |
6732 | size_t length([start length]); | |
327624b6 | 6733 | |
aeeb755b JF |
6734 | for (size_t index(0); index != end; ++index) { |
6735 | if (start != nil) { | |
6736 | Package *package([packages objectAtIndex:index]); | |
22fd24dd | 6737 | NSString *name(PackageName(package, @selector(cyname))); |
a891c345 | 6738 | |
aeeb755b | 6739 | //while ([start compare:name options:NSNumericSearch range:NSMakeRange(0, length) locale:CollationLocale_] != NSOrderedDescending) { |
22fd24dd | 6740 | while (StringNameCompare(start, name, length) != kCFCompareGreaterThan) { |
aeeb755b JF |
6741 | NSString *title([CollationTitles_ objectAtIndex:offset]); |
6742 | section = [[[Section alloc] initWithName:title row:index localize:NO] autorelease]; | |
6743 | [sections addObject:section]; | |
a891c345 | 6744 | |
aeeb755b JF |
6745 | start = ++offset == offsets ? nil : [CollationStarts_ objectAtIndex:offset]; |
6746 | if (start == nil) | |
6747 | break; | |
6748 | length = [start length]; | |
808c6eb6 | 6749 | } |
59f3d290 JF |
6750 | } |
6751 | ||
aeeb755b JF |
6752 | [section addToCount]; |
6753 | } | |
a891c345 | 6754 | |
aeeb755b JF |
6755 | for (; offset != offsets; ++offset) { |
6756 | NSString *title([CollationTitles_ objectAtIndex:offset]); | |
6757 | Section *section([[[Section alloc] initWithName:title row:end localize:NO] autorelease]); | |
6758 | [sections addObject:section]; | |
6759 | } | |
a891c345 | 6760 | |
aeeb755b JF |
6761 | if ([prefix count] != 0) { |
6762 | Section *suffix([sections lastObject]); | |
6763 | [prefix setName:[suffix name]]; | |
6764 | [suffix setName:nil]; | |
6765 | [sections insertObject:prefix atIndex:(offsets - 1)]; | |
a891c345 | 6766 | } |
b4d89997 | 6767 | |
e5491e28 JF |
6768 | return sections; |
6769 | } | |
b4d89997 | 6770 | |
3025d5b4 JF |
6771 | - (void) reloadData { |
6772 | [super reloadData]; | |
6d246265 JF |
6773 | |
6774 | if ([self shouldYield]) | |
6775 | [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; | |
6776 | else | |
6777 | [self _reloadData]; | |
3025d5b4 JF |
6778 | } |
6779 | ||
a0be02eb | 6780 | - (void) resetCursor { |
4c6a29cd | 6781 | [list_ scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; |
a0be02eb JF |
6782 | } |
6783 | ||
59f3d290 JF |
6784 | - (void) clearData { |
6785 | [self updateHeight]; | |
6786 | ||
6787 | [list_ setDataSource:nil]; | |
6788 | [list_ reloadData]; | |
6789 | ||
6790 | [self resetCursor]; | |
6791 | } | |
6792 | ||
a3328c28 JF |
6793 | @end |
6794 | /* }}} */ | |
f50860ee | 6795 | /* Filtered Package List Controller {{{ */ |
89bdef78 JF |
6796 | typedef Function<bool, Package *> PackageFilter; |
6797 | typedef Function<void, NSMutableArray *> PackageSorter; | |
f50860ee | 6798 | @interface FilteredPackageListController : PackageListController { |
89bdef78 JF |
6799 | PackageFilter filter_; |
6800 | PackageSorter sorter_; | |
a3328c28 JF |
6801 | } |
6802 | ||
89bdef78 | 6803 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter; |
3025d5b4 | 6804 | |
89bdef78 JF |
6805 | - (void) setFilter:(PackageFilter)filter; |
6806 | - (void) setSorter:(PackageSorter)sorter; | |
a3328c28 JF |
6807 | |
6808 | @end | |
6809 | ||
f50860ee | 6810 | @implementation FilteredPackageListController |
a3328c28 | 6811 | |
89bdef78 | 6812 | - (void) setFilter:(PackageFilter)filter { |
aa32d91b | 6813 | @synchronized (self) { |
01d93940 | 6814 | filter_ = filter; |
55066b9e JF |
6815 | } } |
6816 | ||
89bdef78 | 6817 | - (void) setSorter:(PackageSorter)sorter { |
55066b9e | 6818 | @synchronized (self) { |
89bdef78 | 6819 | sorter_ = sorter; |
d84597fe | 6820 | } } |
01d93940 | 6821 | |
9c5737d5 | 6822 | - (NSMutableArray *) _reloadPackages { |
aa32d91b | 6823 | @synchronized (database_) { |
c28bc6f1 | 6824 | era_ = [database_ era]; |
c28bc6f1 | 6825 | |
dd4e70dc JF |
6826 | NSArray *packages([database_ packages]); |
6827 | NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); | |
aa32d91b | 6828 | |
89bdef78 | 6829 | PackageFilter filter; |
dd4e70dc | 6830 | PackageSorter sorter; |
aa32d91b JF |
6831 | |
6832 | @synchronized (self) { | |
aa32d91b | 6833 | filter = filter_; |
89bdef78 | 6834 | sorter = sorter_; |
aa32d91b JF |
6835 | } |
6836 | ||
6837 | _profile(PackageTable$reloadData$Filter) | |
6838 | for (Package *package in packages) | |
89bdef78 | 6839 | if ([package valid] && filter(package)) |
aa32d91b | 6840 | [filtered addObject:package]; |
76933519 | 6841 | _end |
aa32d91b | 6842 | |
89bdef78 JF |
6843 | if (sorter) |
6844 | sorter(filtered); | |
aa32d91b | 6845 | return filtered; |
dd4e70dc | 6846 | } } |
55066b9e | 6847 | |
89bdef78 | 6848 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter { |
55066b9e JF |
6849 | if ((self = [super initWithDatabase:database title:title]) != nil) { |
6850 | [self setFilter:filter]; | |
a3328c28 JF |
6851 | } return self; |
6852 | } | |
6853 | ||
dc5812ec | 6854 | @end |
b5e7eebb GP |
6855 | /* }}} */ |
6856 | ||
5829aea2 | 6857 | /* Home Controller {{{ */ |
a576488f | 6858 | @interface HomeController : CydiaWebViewController { |
02f21c73 JF |
6859 | CFRunLoopRef runloop_; |
6860 | SCNetworkReachabilityRef reachability_; | |
b4d89997 | 6861 | } |
6840bff3 | 6862 | |
7b0ce2da | 6863 | @end |
b4d89997 | 6864 | |
5829aea2 | 6865 | @implementation HomeController |
46aa9775 | 6866 | |
02f21c73 JF |
6867 | static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachability, SCNetworkReachabilityFlags flags, void *info) { |
6868 | [(HomeController *) info dispatchEvent:@"CydiaReachabilityCallback"]; | |
6869 | } | |
6870 | ||
3c62d654 JF |
6871 | - (id) init { |
6872 | if ((self = [super init]) != nil) { | |
90351d93 | 6873 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/home/", UI_]]]; |
ed7bfd8c | 6874 | [self reloadData]; |
02f21c73 JF |
6875 | |
6876 | reachability_ = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "cydia.saurik.com"); | |
6877 | if (reachability_ != NULL) { | |
6878 | SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL}; | |
6879 | SCNetworkReachabilitySetCallback(reachability_, HomeControllerReachabilityCallback, &context); | |
6880 | ||
6881 | CFRunLoopRef runloop(CFRunLoopGetCurrent()); | |
6882 | if (SCNetworkReachabilityScheduleWithRunLoop(reachability_, runloop, kCFRunLoopDefaultMode)) | |
6883 | runloop_ = runloop; | |
6884 | } | |
3c62d654 JF |
6885 | } return self; |
6886 | } | |
6887 | ||
02f21c73 JF |
6888 | - (void) dealloc { |
6889 | if (reachability_ != NULL && runloop_ != NULL) | |
6890 | SCNetworkReachabilityUnscheduleFromRunLoop(reachability_, runloop_, kCFRunLoopDefaultMode); | |
6891 | [super dealloc]; | |
6892 | } | |
6893 | ||
fe8e721f GP |
6894 | - (NSURL *) navigationURL { |
6895 | return [NSURL URLWithString:@"cydia://home"]; | |
6896 | } | |
6897 | ||
5829aea2 GP |
6898 | - (void) aboutButtonClicked { |
6899 | UIAlertView *alert([[[UIAlertView alloc] init] autorelease]); | |
b4d89997 | 6900 | |
5829aea2 GP |
6901 | [alert setTitle:UCLocalize("ABOUT_CYDIA")]; |
6902 | [alert addButtonWithTitle:UCLocalize("CLOSE")]; | |
6903 | [alert setCancelButtonIndex:0]; | |
46aa9775 | 6904 | |
5829aea2 | 6905 | [alert setMessage: |
4c66fad9 | 6906 | @"Copyright \u00a9 2008-2015\n" |
7e865c1e JF |
6907 | "SaurikIT, LLC\n" |
6908 | "\n" | |
5829aea2 GP |
6909 | "Jay Freeman (saurik)\n" |
6910 | "saurik@saurik.com\n" | |
6911 | "http://www.saurik.com/" | |
6912 | ]; | |
46aa9775 | 6913 | |
5829aea2 | 6914 | [alert show]; |
a0376fc1 JF |
6915 | } |
6916 | ||
e6124cb6 JF |
6917 | - (UIBarButtonItem *) leftButton { |
6918 | return [[[UIBarButtonItem alloc] | |
35f0a3b5 GP |
6919 | initWithTitle:UCLocalize("ABOUT") |
6920 | style:UIBarButtonItemStylePlain | |
6921 | target:self | |
6922 | action:@selector(aboutButtonClicked) | |
e6124cb6 | 6923 | ] autorelease]; |
7b0ce2da JF |
6924 | } |
6925 | ||
5829aea2 GP |
6926 | @end |
6927 | /* }}} */ | |
7b0ce2da | 6928 | |
28e596e4 | 6929 | /* Cydia Navigation Controller Interface {{{ */ |
15f0d613 | 6930 | @interface UINavigationController (Cydia) |
28e596e4 JF |
6931 | |
6932 | - (NSArray *) navigationURLCollection; | |
15f0d613 | 6933 | - (void) unloadData; |
28e596e4 JF |
6934 | |
6935 | @end | |
6936 | /* }}} */ | |
7b0ce2da | 6937 | |
5829aea2 | 6938 | /* Cydia Tab Bar Controller {{{ */ |
5fe2bcc6 | 6939 | @interface CydiaTabBarController : CyteTabBarController < |
9f99f3da | 6940 | UITabBarControllerDelegate, |
21ac0ce2 | 6941 | FetchDelegate |
5829aea2 GP |
6942 | > { |
6943 | _transient Database *database_; | |
7b0ce2da | 6944 | |
e67ebdad JF |
6945 | _H<UIActivityIndicatorView> indicator_; |
6946 | ||
5829aea2 GP |
6947 | bool updating_; |
6948 | // XXX: ok, "updatedelegate_"?... | |
6949 | _transient NSObject<CydiaDelegate> *updatedelegate_; | |
7b0ce2da JF |
6950 | } |
6951 | ||
35f0a3b5 | 6952 | - (NSArray *) navigationURLCollection; |
5829aea2 | 6953 | - (void) beginUpdate; |
5829aea2 | 6954 | - (BOOL) updating; |
1cedb821 | 6955 | |
5829aea2 | 6956 | @end |
2fc76a2d | 6957 | |
5fe2bcc6 | 6958 | @implementation CydiaTabBarController |
9f99f3da | 6959 | |
35f0a3b5 | 6960 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
6961 | NSMutableArray *items([NSMutableArray array]); |
6962 | ||
35f0a3b5 | 6963 | // XXX: Should this deal with transient view controllers? |
fe8e721f | 6964 | for (id navigation in [self viewControllers]) { |
35f0a3b5 | 6965 | NSArray *stack = [navigation performSelector:@selector(navigationURLCollection)]; |
fe8e721f GP |
6966 | if (stack != nil) |
6967 | [items addObject:stack]; | |
2fc76a2d | 6968 | } |
c713af59 | 6969 | |
fe8e721f GP |
6970 | return items; |
6971 | } | |
6972 | ||
b5e7eebb GP |
6973 | - (id) initWithDatabase:(Database *)database { |
6974 | if ((self = [super init]) != nil) { | |
7b0ce2da | 6975 | database_ = database; |
9f99f3da | 6976 | [self setDelegate:self]; |
04fe1349 | 6977 | |
e67ebdad JF |
6978 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteTiny] autorelease]; |
6979 | [indicator_ setOrigin:CGPointMake(kCFCoreFoundationVersionNumber >= 800 ? 2 : 4, 2)]; | |
7b0ce2da | 6980 | |
e67ebdad | 6981 | [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b0ce2da JF |
6982 | } return self; |
6983 | } | |
6984 | ||
5829aea2 | 6985 | - (void) beginUpdate { |
20d59843 JF |
6986 | if (updating_) |
6987 | return; | |
6988 | ||
e67ebdad JF |
6989 | UIViewController *controller([[self viewControllers] objectAtIndex:1]); |
6990 | UITabBarItem *item([controller tabBarItem]); | |
6991 | ||
6992 | [item setBadgeValue:@""]; | |
6993 | UIView *badge(MSHookIvar<UIView *>([item view], "_badge")); | |
6994 | ||
6995 | [indicator_ startAnimating]; | |
6996 | [badge addSubview:indicator_]; | |
7b0ce2da | 6997 | |
5829aea2 GP |
6998 | [updatedelegate_ retainNetworkActivityIndicator]; |
6999 | updating_ = true; | |
7b0ce2da | 7000 | |
5829aea2 GP |
7001 | [NSThread |
7002 | detachNewThreadSelector:@selector(performUpdate) | |
7003 | toTarget:self | |
7004 | withObject:nil | |
7005 | ]; | |
7b0ce2da JF |
7006 | } |
7007 | ||
d13edf44 JF |
7008 | - (void) performUpdate { |
7009 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
7010 | ||
21ac0ce2 | 7011 | SourceStatus status(self, database_); |
5829aea2 GP |
7012 | [database_ updateWithStatus:status]; |
7013 | ||
7014 | [self | |
7015 | performSelectorOnMainThread:@selector(completeUpdate) | |
7016 | withObject:nil | |
7017 | waitUntilDone:NO | |
7018 | ]; | |
d13edf44 JF |
7019 | |
7020 | [pool release]; | |
f6e13561 GP |
7021 | } |
7022 | ||
5829aea2 GP |
7023 | - (void) stopUpdateWithSelector:(SEL)selector { |
7024 | updating_ = false; | |
7025 | [updatedelegate_ releaseNetworkActivityIndicator]; | |
b5e7eebb | 7026 | |
e67ebdad JF |
7027 | UIViewController *controller([[self viewControllers] objectAtIndex:1]); |
7028 | [[controller tabBarItem] setBadgeValue:nil]; | |
7029 | ||
7030 | [indicator_ removeFromSuperview]; | |
7031 | [indicator_ stopAnimating]; | |
bc11cf5b | 7032 | |
5829aea2 | 7033 | [updatedelegate_ performSelector:selector withObject:nil afterDelay:0]; |
2bdd73bd GP |
7034 | } |
7035 | ||
5829aea2 GP |
7036 | - (void) completeUpdate { |
7037 | if (!updating_) | |
7038 | return; | |
7039 | [self stopUpdateWithSelector:@selector(reloadData)]; | |
7b0ce2da JF |
7040 | } |
7041 | ||
5829aea2 | 7042 | - (void) cancelUpdate { |
383a58ac | 7043 | [self stopUpdateWithSelector:@selector(updateDataAndLoad)]; |
7b0ce2da JF |
7044 | } |
7045 | ||
5829aea2 GP |
7046 | - (void) cancelPressed { |
7047 | [self cancelUpdate]; | |
7048 | } | |
7b0ce2da | 7049 | |
5829aea2 GP |
7050 | - (BOOL) updating { |
7051 | return updating_; | |
7b0ce2da JF |
7052 | } |
7053 | ||
21ac0ce2 | 7054 | - (bool) isSourceCancelled { |
5829aea2 GP |
7055 | return !updating_; |
7056 | } | |
7b0ce2da | 7057 | |
21ac0ce2 | 7058 | - (void) startSourceFetch:(NSString *)uri { |
7b0ce2da JF |
7059 | } |
7060 | ||
21ac0ce2 | 7061 | - (void) stopSourceFetch:(NSString *)uri { |
bcbac8f7 JF |
7062 | } |
7063 | ||
5829aea2 GP |
7064 | - (void) setUpdateDelegate:(id)delegate { |
7065 | updatedelegate_ = delegate; | |
7b0ce2da JF |
7066 | } |
7067 | ||
7b0ce2da | 7068 | @end |
98228790 | 7069 | /* }}} */ |
f6e13561 | 7070 | |
28e596e4 | 7071 | /* Cydia Navigation Controller Implementation {{{ */ |
15f0d613 | 7072 | @implementation UINavigationController (Cydia) |
bc11cf5b | 7073 | |
35f0a3b5 | 7074 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
7075 | NSMutableArray *stack([NSMutableArray array]); |
7076 | ||
cd79e8cf | 7077 | for (CyteViewController *controller in [self viewControllers]) { |
fe8e721f GP |
7078 | NSString *url = [[controller navigationURL] absoluteString]; |
7079 | if (url != nil) | |
7080 | [stack addObject:url]; | |
7081 | } | |
7082 | ||
7083 | return stack; | |
7084 | } | |
7085 | ||
15f0d613 JF |
7086 | - (void) reloadData { |
7087 | [super reloadData]; | |
7088 | ||
21263088 JF |
7089 | UIViewController *visible([self visibleViewController]); |
7090 | if (visible != nil) | |
15f0d613 | 7091 | [visible reloadData]; |
21263088 JF |
7092 | |
7093 | // on the iPad, this view controller is ALSO visible. :( | |
7094 | if (IsWildcat_) | |
907ba491 JF |
7095 | if (UIViewController *modal = [self modalViewController]) |
7096 | if ([modal modalPresentationStyle] == UIModalPresentationFormSheet) | |
7097 | if (UIViewController *top = [self topViewController]) | |
7098 | if (top != visible) | |
7099 | [top reloadData]; | |
15f0d613 | 7100 | } |
6c0ba3d9 | 7101 | |
15f0d613 | 7102 | - (void) unloadData { |
cd79e8cf | 7103 | for (CyteViewController *page in [self viewControllers]) |
15f0d613 | 7104 | [page unloadData]; |
6c0ba3d9 | 7105 | |
15f0d613 | 7106 | [super unloadData]; |
dc63e78f JF |
7107 | } |
7108 | ||
5829aea2 GP |
7109 | @end |
7110 | /* }}} */ | |
7b0ce2da | 7111 | |
5829aea2 GP |
7112 | /* Cydia:// Protocol {{{ */ |
7113 | @interface CydiaURLProtocol : NSURLProtocol { | |
9fe5e5f8 JF |
7114 | } |
7115 | ||
7b0ce2da JF |
7116 | @end |
7117 | ||
5829aea2 GP |
7118 | @implementation CydiaURLProtocol |
7119 | ||
7120 | + (BOOL) canInitWithRequest:(NSURLRequest *)request { | |
7121 | NSURL *url([request URL]); | |
7122 | if (url == nil) | |
7123 | return NO; | |
b0a2900d | 7124 | |
5829aea2 | 7125 | NSString *scheme([[url scheme] lowercaseString]); |
b0a2900d JF |
7126 | if (scheme != nil && [scheme isEqualToString:@"cydia"]) |
7127 | return YES; | |
7128 | if ([[url absoluteString] hasPrefix:@"about:cydia-"]) | |
7129 | return YES; | |
7130 | ||
7131 | return NO; | |
aa5e5990 JF |
7132 | } |
7133 | ||
5829aea2 GP |
7134 | + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { |
7135 | return request; | |
7136 | } | |
aa5e5990 | 7137 | |
5829aea2 GP |
7138 | - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { |
7139 | id<NSURLProtocolClient> client([self client]); | |
7140 | if (icon == nil) | |
7141 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; | |
7142 | else { | |
7143 | NSData *data(UIImagePNGRepresentation(icon)); | |
01d93940 | 7144 | |
5829aea2 GP |
7145 | NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); |
7146 | [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; | |
7147 | [client URLProtocol:self didLoadData:data]; | |
7148 | [client URLProtocolDidFinishLoading:self]; | |
7149 | } | |
3931b718 JF |
7150 | } |
7151 | ||
5829aea2 GP |
7152 | - (void) startLoading { |
7153 | id<NSURLProtocolClient> client([self client]); | |
7154 | NSURLRequest *request([self request]); | |
bc11cf5b | 7155 | |
5829aea2 GP |
7156 | NSURL *url([request URL]); |
7157 | NSString *href([url absoluteString]); | |
b0a2900d JF |
7158 | NSString *scheme([[url scheme] lowercaseString]); |
7159 | ||
7160 | NSString *path; | |
7161 | ||
7162 | if ([scheme isEqualToString:@"cydia"]) | |
7163 | path = [href substringFromIndex:8]; | |
7164 | else if ([scheme isEqualToString:@"about"]) | |
7165 | path = [href substringFromIndex:12]; | |
7166 | else _assert(false); | |
bc11cf5b | 7167 | |
5829aea2 | 7168 | NSRange slash([path rangeOfString:@"/"]); |
bc11cf5b | 7169 | |
5829aea2 GP |
7170 | NSString *command; |
7171 | if (slash.location == NSNotFound) { | |
7172 | command = path; | |
7173 | path = nil; | |
7174 | } else { | |
7175 | command = [path substringToIndex:slash.location]; | |
7176 | path = [path substringFromIndex:(slash.location + 1)]; | |
7177 | } | |
39cda3a8 | 7178 | |
5829aea2 | 7179 | Database *database([Database sharedInstance]); |
bc11cf5b | 7180 | |
d0a5ea56 JF |
7181 | if (false); |
7182 | else if ([command isEqualToString:@"application-icon"]) { | |
7183 | if (path == nil) | |
7184 | goto fail; | |
7185 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
c7e78d5f JF |
7186 | |
7187 | UIImage *icon(nil); | |
7188 | ||
7189 | if (icon == nil && $SBSCopyIconImagePNGDataForDisplayIdentifier != NULL) { | |
7190 | NSData *data([$SBSCopyIconImagePNGDataForDisplayIdentifier(path) autorelease]); | |
d0a5ea56 | 7191 | icon = [UIImage imageWithData:data]; |
c7e78d5f JF |
7192 | } |
7193 | ||
7194 | if (icon == nil) | |
7195 | if (NSString *file = SBSCopyIconImagePathForDisplayIdentifier(path)) | |
7196 | icon = [UIImage imageAtPath:file]; | |
7197 | ||
7198 | if (icon == nil) | |
7199 | icon = [UIImage imageNamed:@"unknown.png"]; | |
7200 | ||
d0a5ea56 JF |
7201 | [self _returnPNGWithImage:icon forRequest:request]; |
7202 | } else if ([command isEqualToString:@"package-icon"]) { | |
5829aea2 GP |
7203 | if (path == nil) |
7204 | goto fail; | |
7205 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7206 | Package *package([database packageWithName:path]); | |
7207 | if (package == nil) | |
7208 | goto fail; | |
eff20a8d | 7209 | [package parse]; |
5829aea2 GP |
7210 | UIImage *icon([package icon]); |
7211 | [self _returnPNGWithImage:icon forRequest:request]; | |
5829aea2 GP |
7212 | } else if ([command isEqualToString:@"uikit-image"]) { |
7213 | if (path == nil) | |
7214 | goto fail; | |
7215 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7216 | UIImage *icon(_UIImageWithName(path)); | |
7217 | [self _returnPNGWithImage:icon forRequest:request]; | |
7218 | } else if ([command isEqualToString:@"section-icon"]) { | |
7219 | if (path == nil) | |
7220 | goto fail; | |
7221 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
611efc17 | 7222 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [path stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]); |
5829aea2 | 7223 | if (icon == nil) |
8dbf3abc | 7224 | icon = [UIImage imageNamed:@"unknown.png"]; |
5829aea2 GP |
7225 | [self _returnPNGWithImage:icon forRequest:request]; |
7226 | } else fail: { | |
7227 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; | |
7228 | } | |
39cda3a8 GP |
7229 | } |
7230 | ||
5829aea2 GP |
7231 | - (void) stopLoading { |
7232 | } | |
807ae6d7 | 7233 | |
5829aea2 GP |
7234 | @end |
7235 | /* }}} */ | |
807ae6d7 | 7236 | |
5829aea2 | 7237 | /* Section Controller {{{ */ |
f50860ee | 7238 | @interface SectionController : FilteredPackageListController { |
55066b9e | 7239 | _H<NSString> key_; |
123b380c | 7240 | _H<NSString> section_; |
5829aea2 | 7241 | } |
807ae6d7 | 7242 | |
55066b9e | 7243 | - (id) initWithDatabase:(Database *)database source:(Source *)source section:(NSString *)section; |
807ae6d7 | 7244 | |
5829aea2 | 7245 | @end |
bc11cf5b | 7246 | |
5829aea2 | 7247 | @implementation SectionController |
bc11cf5b | 7248 | |
f050e4d9 | 7249 | - (NSURL *) referrerURL { |
55066b9e JF |
7250 | NSString *name(section_); |
7251 | name = name ?: @"*"; | |
7252 | NSString *key(key_); | |
7253 | key = key ?: @"*"; | |
7254 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/sections/%@/%@", UI_, [key stringByAddingPercentEscapesIncludingReserved], [name stringByAddingPercentEscapesIncludingReserved]]]; | |
f050e4d9 JF |
7255 | } |
7256 | ||
fe8e721f | 7257 | - (NSURL *) navigationURL { |
55066b9e JF |
7258 | NSString *name(section_); |
7259 | name = name ?: @"*"; | |
7260 | NSString *key(key_); | |
7261 | key = key ?: @"*"; | |
7262 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sections/%@/%@", [key stringByAddingPercentEscapesIncludingReserved], [name stringByAddingPercentEscapesIncludingReserved]]]; | |
fe8e721f GP |
7263 | } |
7264 | ||
55066b9e | 7265 | - (id) initWithDatabase:(Database *)database source:(Source *)source section:(NSString *)section { |
5829aea2 | 7266 | NSString *title; |
55066b9e | 7267 | if (section == nil) |
5829aea2 | 7268 | title = UCLocalize("ALL_PACKAGES"); |
55066b9e JF |
7269 | else if (![section isEqual:@""]) |
7270 | title = [[NSBundle mainBundle] localizedStringForKey:Simplify(section) value:nil table:@"Sections"]; | |
3707eb56 | 7271 | else |
5829aea2 | 7272 | title = UCLocalize("NO_SECTION"); |
fe8e721f | 7273 | |
89bdef78 | 7274 | if ((self = [super initWithDatabase:database title:title]) != nil) { |
55066b9e JF |
7275 | key_ = [source key]; |
7276 | section_ = section; | |
5829aea2 | 7277 | } return self; |
b5e7eebb GP |
7278 | } |
7279 | ||
55066b9e | 7280 | - (void) reloadData { |
89bdef78 JF |
7281 | Source *source([database_ sourceWithKey:key_]); |
7282 | _H<NSString> name(section_); | |
7283 | ||
7284 | [self setFilter:[=](Package *package) { | |
7285 | NSString *section([package section]); | |
7286 | ||
7287 | return ( | |
7288 | name == nil || | |
7289 | section == nil && [name length] == 0 || | |
7290 | [name isEqualToString:section] | |
7291 | ) && ( | |
7292 | source == nil || | |
7293 | [package source] == source | |
7294 | ) && [package visible]; | |
7295 | }]; | |
7296 | ||
55066b9e JF |
7297 | [super reloadData]; |
7298 | } | |
7299 | ||
b5e7eebb GP |
7300 | @end |
7301 | /* }}} */ | |
5829aea2 | 7302 | /* Sections Controller {{{ */ |
cd79e8cf | 7303 | @interface SectionsController : CyteViewController < |
5829aea2 GP |
7304 | UITableViewDataSource, |
7305 | UITableViewDelegate | |
7585ce66 | 7306 | > { |
3931b718 | 7307 | _transient Database *database_; |
55066b9e | 7308 | _H<NSString> key_; |
7b33d201 JF |
7309 | _H<NSMutableArray> sections_; |
7310 | _H<NSMutableArray> filtered_; | |
bf7c998c | 7311 | _H<UITableView, 2> list_; |
b5e7eebb GP |
7312 | } |
7313 | ||
55066b9e | 7314 | - (id) initWithDatabase:(Database *)database source:(Source *)source; |
5829aea2 | 7315 | - (void) editButtonClicked; |
7585ce66 | 7316 | |
bc11cf5b | 7317 | @end |
b5e7eebb | 7318 | |
5829aea2 | 7319 | @implementation SectionsController |
b5e7eebb | 7320 | |
fe8e721f | 7321 | - (NSURL *) navigationURL { |
55066b9e JF |
7322 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sources/%@", [key_ stringByAddingPercentEscapesIncludingReserved]]]; |
7323 | } | |
7324 | ||
7325 | - (Source *) source { | |
7326 | if (key_ == nil) | |
7327 | return nil; | |
7328 | return [database_ sourceWithKey:key_]; | |
fe8e721f GP |
7329 | } |
7330 | ||
a784d0a4 | 7331 | - (void) updateNavigationItem { |
8e5b801a | 7332 | [[self navigationItem] setTitle:[self isEditing] ? UCLocalize("SECTION_VISIBILITY") : UCLocalize("SECTIONS")]; |
a784d0a4 GP |
7333 | if ([sections_ count] == 0) { |
7334 | [[self navigationItem] setRightBarButtonItem:nil]; | |
7335 | } else { | |
7336 | [[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] | |
8e5b801a | 7337 | initWithBarButtonSystemItem:([self isEditing] ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit) |
a784d0a4 GP |
7338 | target:self |
7339 | action:@selector(editButtonClicked) | |
7340 | ] animated:([[self navigationItem] rightBarButtonItem] != nil)]; | |
7341 | } | |
7342 | } | |
7343 | ||
8e5b801a GP |
7344 | - (void) setEditing:(BOOL)editing animated:(BOOL)animated { |
7345 | [super setEditing:editing animated:animated]; | |
35f0a3b5 | 7346 | |
8e5b801a | 7347 | if (editing) |
5829aea2 GP |
7348 | [list_ reloadData]; |
7349 | else | |
7350 | [delegate_ updateData]; | |
7585ce66 | 7351 | |
a784d0a4 | 7352 | [self updateNavigationItem]; |
b5e7eebb GP |
7353 | } |
7354 | ||
5829aea2 GP |
7355 | - (void) viewDidAppear:(BOOL)animated { |
7356 | [super viewDidAppear:animated]; | |
7357 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7585ce66 JF |
7358 | } |
7359 | ||
5829aea2 GP |
7360 | - (void) viewWillDisappear:(BOOL)animated { |
7361 | [super viewWillDisappear:animated]; | |
31eedaae | 7362 | [self setEditing:NO]; |
7585ce66 JF |
7363 | } |
7364 | ||
5829aea2 | 7365 | - (Section *) sectionAtIndexPath:(NSIndexPath *)indexPath { |
b3551da8 | 7366 | Section *section = nil; |
8e5b801a | 7367 | int index = [indexPath row]; |
b3551da8 | 7368 | if (![self isEditing]) { |
666b48ad | 7369 | index -= 1; |
b3551da8 GP |
7370 | if (index >= 0) |
7371 | section = [filtered_ objectAtIndex:index]; | |
7372 | } else { | |
7373 | section = [sections_ objectAtIndex:index]; | |
7374 | } | |
5829aea2 GP |
7375 | return section; |
7376 | } | |
7585ce66 | 7377 | |
5829aea2 | 7378 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
8e5b801a GP |
7379 | if ([self isEditing]) |
7380 | return [sections_ count]; | |
7381 | else | |
7382 | return [filtered_ count] + 1; | |
7585ce66 JF |
7383 | } |
7384 | ||
5829aea2 GP |
7385 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
7386 | return 45.0f; | |
7387 | }*/ | |
7585ce66 | 7388 | |
5829aea2 GP |
7389 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
7390 | static NSString *reuseIdentifier = @"SectionCell"; | |
7585ce66 | 7391 | |
35f0a3b5 | 7392 | SectionCell *cell = (SectionCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5829aea2 GP |
7393 | if (cell == nil) |
7394 | cell = [[[SectionCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
7585ce66 | 7395 | |
8e5b801a | 7396 | [cell setSection:[self sectionAtIndexPath:indexPath] editing:[self isEditing]]; |
54043703 | 7397 | |
5829aea2 | 7398 | return cell; |
54043703 JF |
7399 | } |
7400 | ||
5829aea2 | 7401 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
8e5b801a | 7402 | if ([self isEditing]) |
54043703 | 7403 | return; |
7585ce66 | 7404 | |
5829aea2 | 7405 | Section *section = [self sectionAtIndexPath:indexPath]; |
7585ce66 | 7406 | |
5829aea2 GP |
7407 | SectionController *controller = [[[SectionController alloc] |
7408 | initWithDatabase:database_ | |
55066b9e | 7409 | source:[self source] |
5829aea2 GP |
7410 | section:[section name] |
7411 | ] autorelease]; | |
7412 | [controller setDelegate:delegate_]; | |
7585ce66 | 7413 | |
5829aea2 | 7414 | [[self navigationController] pushViewController:controller animated:YES]; |
7585ce66 JF |
7415 | } |
7416 | ||
fe8e721f | 7417 | - (void) loadView { |
e8cbebe4 | 7418 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; |
fe8e721f | 7419 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
86a333c6 | 7420 | [list_ setRowHeight:46]; |
7b33d201 | 7421 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 7422 | [list_ setDelegate:self]; |
e8cbebe4 | 7423 | [self setView:list_]; |
fe8e721f GP |
7424 | } |
7425 | ||
7426 | - (void) viewDidLoad { | |
7d887d0b JF |
7427 | [super viewDidLoad]; |
7428 | ||
fe8e721f GP |
7429 | [[self navigationItem] setTitle:UCLocalize("SECTIONS")]; |
7430 | } | |
7431 | ||
7432 | - (void) releaseSubviews { | |
fe8e721f | 7433 | list_ = nil; |
7be3eb32 | 7434 | |
4f9acb7c JF |
7435 | sections_ = nil; |
7436 | filtered_ = nil; | |
7437 | ||
7be3eb32 | 7438 | [super releaseSubviews]; |
fe8e721f GP |
7439 | } |
7440 | ||
55066b9e | 7441 | - (id) initWithDatabase:(Database *)database source:(Source *)source { |
5829aea2 GP |
7442 | if ((self = [super init]) != nil) { |
7443 | database_ = database; | |
55066b9e | 7444 | key_ = [source key]; |
5829aea2 | 7445 | } return self; |
7585ce66 JF |
7446 | } |
7447 | ||
5829aea2 | 7448 | - (void) reloadData { |
fe8e721f GP |
7449 | [super reloadData]; |
7450 | ||
5829aea2 | 7451 | NSArray *packages = [database_ packages]; |
7585ce66 | 7452 | |
4f9acb7c JF |
7453 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
7454 | filtered_ = [NSMutableArray arrayWithCapacity:16]; | |
7585ce66 | 7455 | |
5829aea2 | 7456 | NSMutableDictionary *sections([NSMutableDictionary dictionaryWithCapacity:32]); |
7585ce66 | 7457 | |
55066b9e JF |
7458 | Source *source([self source]); |
7459 | ||
5829aea2 GP |
7460 | _trace(); |
7461 | for (Package *package in packages) { | |
55066b9e JF |
7462 | if (source != nil && [package source] != source) |
7463 | continue; | |
7464 | ||
5829aea2 GP |
7465 | NSString *name([package section]); |
7466 | NSString *key(name == nil ? @"" : name); | |
7585ce66 | 7467 | |
5829aea2 | 7468 | Section *section; |
7585ce66 | 7469 | |
5829aea2 GP |
7470 | _profile(SectionsView$reloadData$Section) |
7471 | section = [sections objectForKey:key]; | |
7472 | if (section == nil) { | |
7473 | _profile(SectionsView$reloadData$Section$Allocate) | |
729bc910 | 7474 | section = [[[Section alloc] initWithName:key localize:YES] autorelease]; |
5829aea2 GP |
7475 | [sections setObject:section forKey:key]; |
7476 | _end | |
7477 | } | |
7478 | _end | |
7585ce66 | 7479 | |
5829aea2 | 7480 | [section addToCount]; |
7585ce66 | 7481 | |
5829aea2 GP |
7482 | _profile(SectionsView$reloadData$Filter) |
7483 | if (![package valid] || ![package visible]) | |
7484 | continue; | |
7485 | _end | |
7585ce66 | 7486 | |
5829aea2 GP |
7487 | [section addToRow]; |
7488 | } | |
7489 | _trace(); | |
7585ce66 | 7490 | |
5829aea2 | 7491 | [sections_ addObjectsFromArray:[sections allValues]]; |
7585ce66 | 7492 | |
5829aea2 | 7493 | [sections_ sortUsingSelector:@selector(compareByLocalized:)]; |
7585ce66 | 7494 | |
7b33d201 | 7495 | for (Section *section in (id) sections_) { |
5829aea2 GP |
7496 | size_t count([section row]); |
7497 | if (count == 0) | |
7498 | continue; | |
7585ce66 | 7499 | |
5829aea2 GP |
7500 | section = [[[Section alloc] initWithName:[section name] localized:[section localized]] autorelease]; |
7501 | [section setCount:count]; | |
7502 | [filtered_ addObject:section]; | |
7503 | } | |
7585ce66 | 7504 | |
a784d0a4 | 7505 | [self updateNavigationItem]; |
5829aea2 GP |
7506 | [list_ reloadData]; |
7507 | _trace(); | |
7508 | } | |
7585ce66 | 7509 | |
6840bff3 | 7510 | - (void) editButtonClicked { |
8e5b801a | 7511 | [self setEditing:![self isEditing] animated:YES]; |
5829aea2 | 7512 | } |
7585ce66 | 7513 | |
5829aea2 GP |
7514 | @end |
7515 | /* }}} */ | |
7585ce66 | 7516 | |
5829aea2 | 7517 | /* Changes Controller {{{ */ |
e5491e28 | 7518 | @interface ChangesController : FilteredPackageListController { |
5829aea2 | 7519 | unsigned upgrades_; |
5829aea2 | 7520 | } |
7585ce66 | 7521 | |
ea3bb538 | 7522 | - (id) initWithDatabase:(Database *)database; |
7585ce66 | 7523 | |
5829aea2 | 7524 | @end |
7585ce66 | 7525 | |
5829aea2 | 7526 | @implementation ChangesController |
7585ce66 | 7527 | |
e5491e28 JF |
7528 | - (NSURL *) referrerURL { |
7529 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/changes/", UI_]]; | |
b5e7eebb GP |
7530 | } |
7531 | ||
e5491e28 JF |
7532 | - (NSURL *) navigationURL { |
7533 | return [NSURL URLWithString:@"cydia://changes"]; | |
5829aea2 | 7534 | } |
b5e7eebb | 7535 | |
5829aea2 GP |
7536 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
7537 | @synchronized (database_) { | |
7538 | if ([database_ era] != era_) | |
7539 | return nil; | |
b5e7eebb | 7540 | |
5829aea2 GP |
7541 | NSUInteger sectionIndex([path section]); |
7542 | if (sectionIndex >= [sections_ count]) | |
7543 | return nil; | |
7544 | Section *section([sections_ objectAtIndex:sectionIndex]); | |
7545 | NSInteger row([path row]); | |
56bf1e78 | 7546 | return [[[packages_ objectAtIndex:([section row] + row)] retain] autorelease]; |
5829aea2 | 7547 | } } |
b5e7eebb | 7548 | |
0e15b67c JF |
7549 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
7550 | NSString *context([alert context]); | |
7551 | ||
7552 | if ([context isEqualToString:@"norefresh"]) | |
7553 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
7554 | } | |
7555 | ||
e67ebdad JF |
7556 | - (void) setLeftBarButtonItem { |
7557 | if ([delegate_ updating]) | |
7558 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7559 | initWithTitle:UCLocalize("CANCEL") | |
7560 | style:UIBarButtonItemStyleDone | |
7561 | target:self | |
7562 | action:@selector(cancelButtonClicked) | |
7563 | ] autorelease] animated:YES]; | |
7564 | else | |
7565 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7566 | initWithTitle:UCLocalize("REFRESH") | |
7567 | style:UIBarButtonItemStylePlain | |
7568 | target:self | |
7569 | action:@selector(refreshButtonClicked) | |
7570 | ] autorelease] animated:YES]; | |
7571 | } | |
7572 | ||
5829aea2 | 7573 | - (void) refreshButtonClicked { |
e67ebdad JF |
7574 | if ([delegate_ requestUpdate]) |
7575 | [self setLeftBarButtonItem]; | |
7576 | } | |
0e15b67c | 7577 | |
e67ebdad JF |
7578 | - (void) cancelButtonClicked { |
7579 | [delegate_ cancelUpdate]; | |
b5e7eebb GP |
7580 | } |
7581 | ||
5829aea2 GP |
7582 | - (void) upgradeButtonClicked { |
7583 | [delegate_ distUpgrade]; | |
cb6b3a7b | 7584 | [[self navigationItem] setRightBarButtonItem:nil animated:YES]; |
b5e7eebb GP |
7585 | } |
7586 | ||
e5491e28 JF |
7587 | - (bool) shouldYield { |
7588 | return true; | |
fe8e721f GP |
7589 | } |
7590 | ||
e5491e28 JF |
7591 | - (bool) shouldBlock { |
7592 | return true; | |
fe8e721f GP |
7593 | } |
7594 | ||
e5491e28 JF |
7595 | - (void) useFilter { |
7596 | @synchronized (self) { | |
7597 | [self setFilter:[](Package *package) { | |
7598 | return [package upgradableAndEssential:YES] || [package visible]; | |
7599 | }]; | |
4f9acb7c | 7600 | |
e5491e28 JF |
7601 | [self setSorter:[](NSMutableArray *packages) { |
7602 | [packages radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackageChangesRadix) withContext:NULL]; | |
7603 | }]; | |
7604 | } } | |
5829aea2 | 7605 | |
ea3bb538 | 7606 | - (id) initWithDatabase:(Database *)database { |
e5491e28 JF |
7607 | if ((self = [super initWithDatabase:database title:UCLocalize("CHANGES")]) != nil) { |
7608 | [self useFilter]; | |
5829aea2 | 7609 | } return self; |
807ae6d7 JF |
7610 | } |
7611 | ||
946c4377 JF |
7612 | - (void) viewDidLoad { |
7613 | [super viewDidLoad]; | |
7614 | [self setLeftBarButtonItem]; | |
7615 | } | |
7616 | ||
7617 | - (void) viewWillAppear:(BOOL)animated { | |
7618 | [super viewWillAppear:animated]; | |
7619 | [self setLeftBarButtonItem]; | |
7620 | } | |
7621 | ||
e5491e28 | 7622 | - (void) reloadData { |
e67ebdad | 7623 | [self setLeftBarButtonItem]; |
e5491e28 JF |
7624 | [super reloadData]; |
7625 | } | |
e67ebdad | 7626 | |
e5491e28 JF |
7627 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { |
7628 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); | |
bdca103d | 7629 | |
5829aea2 GP |
7630 | Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; |
7631 | Section *ignored = nil; | |
7632 | Section *section = nil; | |
7633 | time_t last = 0; | |
807ae6d7 | 7634 | |
5829aea2 GP |
7635 | upgrades_ = 0; |
7636 | bool unseens = false; | |
807ae6d7 | 7637 | |
5829aea2 | 7638 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); |
807ae6d7 | 7639 | |
e5491e28 JF |
7640 | for (size_t offset = 0, count = [packages count]; offset != count; ++offset) { |
7641 | Package *package = [packages objectAtIndex:offset]; | |
807ae6d7 | 7642 | |
5829aea2 | 7643 | BOOL uae = [package upgradableAndEssential:YES]; |
807ae6d7 | 7644 | |
5829aea2 GP |
7645 | if (!uae) { |
7646 | unseens = true; | |
7647 | time_t seen([package seen]); | |
d90a4cd6 | 7648 | |
5829aea2 GP |
7649 | if (section == nil || last != seen) { |
7650 | last = seen; | |
7651 | ||
7652 | NSString *name; | |
7653 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]); | |
7654 | [name autorelease]; | |
7655 | ||
7656 | _profile(ChangesController$reloadData$Allocate) | |
7657 | name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; | |
7658 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
e5491e28 | 7659 | [sections addObject:section]; |
5829aea2 GP |
7660 | _end |
7661 | } | |
7662 | ||
7663 | [section addToCount]; | |
7664 | } else if ([package ignored]) { | |
7665 | if (ignored == nil) { | |
7666 | ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease]; | |
7667 | } | |
7668 | [ignored addToCount]; | |
7669 | } else { | |
7670 | ++upgrades_; | |
7671 | [upgradable addToCount]; | |
7672 | } | |
7673 | } | |
7674 | _trace(); | |
7675 | ||
7676 | CFRelease(formatter); | |
7677 | ||
7678 | if (unseens) { | |
e5491e28 | 7679 | Section *last = [sections lastObject]; |
5829aea2 | 7680 | size_t count = [last count]; |
e5491e28 JF |
7681 | [packages removeObjectsInRange:NSMakeRange([packages count] - count, count)]; |
7682 | [sections removeLastObject]; | |
5829aea2 GP |
7683 | } |
7684 | ||
7685 | if ([ignored count] != 0) | |
e5491e28 | 7686 | [sections insertObject:ignored atIndex:0]; |
5829aea2 | 7687 | if (upgrades_ != 0) |
e5491e28 | 7688 | [sections insertObject:upgradable atIndex:0]; |
5829aea2 GP |
7689 | |
7690 | [list_ reloadData]; | |
7691 | ||
cb6b3a7b JF |
7692 | [[self navigationItem] setRightBarButtonItem:(upgrades_ == 0 ? nil : [[[UIBarButtonItem alloc] |
7693 | initWithTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("UPGRADE"), [NSString stringWithFormat:@"%u", upgrades_]] | |
7694 | style:UIBarButtonItemStylePlain | |
7695 | target:self | |
7696 | action:@selector(upgradeButtonClicked) | |
7697 | ] autorelease]) animated:YES]; | |
5829aea2 | 7698 | |
e5491e28 | 7699 | return sections; |
a70cd4ba JF |
7700 | } |
7701 | ||
5829aea2 GP |
7702 | @end |
7703 | /* }}} */ | |
7704 | /* Search Controller {{{ */ | |
f50860ee | 7705 | @interface SearchController : FilteredPackageListController < |
5829aea2 GP |
7706 | UISearchBarDelegate |
7707 | > { | |
bf7c998c | 7708 | _H<UISearchBar, 1> search_; |
fe8e721f | 7709 | BOOL searchloaded_; |
89bdef78 | 7710 | bool summary_; |
5829aea2 GP |
7711 | } |
7712 | ||
43625891 | 7713 | - (id) initWithDatabase:(Database *)database query:(NSString *)query; |
5829aea2 | 7714 | - (void) reloadData; |
d90a4cd6 GP |
7715 | |
7716 | @end | |
7717 | ||
5829aea2 | 7718 | @implementation SearchController |
d90a4cd6 | 7719 | |
f050e4d9 JF |
7720 | - (NSURL *) referrerURL { |
7721 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/search?q=%@", UI_, [([search_ text] ?: @"") stringByAddingPercentEscapesIncludingReserved]]]; | |
7722 | } | |
7723 | ||
fe8e721f | 7724 | - (NSURL *) navigationURL { |
35f0a3b5 GP |
7725 | if ([search_ text] == nil || [[search_ text] isEqualToString:@""]) |
7726 | return [NSURL URLWithString:@"cydia://search"]; | |
7727 | else | |
b90c7892 | 7728 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://search/%@", [[search_ text] stringByAddingPercentEscapesIncludingReserved]]]; |
fe8e721f GP |
7729 | } |
7730 | ||
096e25d8 | 7731 | - (NSArray *) termsForQuery:(NSString *)query { |
945812b9 JF |
7732 | NSMutableArray *terms([NSMutableArray arrayWithCapacity:2]); |
7733 | for (NSString *component in [query componentsSeparatedByString:@" "]) | |
7734 | if ([component length] != 0) | |
7735 | [terms addObject:component]; | |
7736 | ||
7737 | return terms; | |
096e25d8 JF |
7738 | } |
7739 | ||
59f3d290 | 7740 | - (void) useSearch { |
89bdef78 JF |
7741 | _H<NSArray> query([self termsForQuery:[search_ text]]); |
7742 | summary_ = false; | |
7743 | ||
7744 | @synchronized (self) { | |
7745 | [self setFilter:[=](Package *package) { | |
7746 | if (![package unfiltered]) | |
7747 | return false; | |
7748 | if (![package matches:query]) | |
7749 | return false; | |
7750 | return true; | |
7751 | }]; | |
7752 | ||
7753 | [self setSorter:[](NSMutableArray *packages) { | |
7754 | [packages radixSortUsingSelector:@selector(rank)]; | |
7755 | }]; | |
7756 | } | |
7757 | ||
59f3d290 JF |
7758 | [self clearData]; |
7759 | [self reloadData]; | |
7760 | } | |
7761 | ||
89bdef78 JF |
7762 | - (void) usePrefix:(NSString *)prefix { |
7763 | _H<NSString> query(prefix); | |
7764 | summary_ = true; | |
7765 | ||
7766 | @synchronized (self) { | |
7767 | [self setFilter:[=](Package *package) { | |
7768 | if ([query length] == 0) | |
7769 | return false; | |
7770 | if (![package unfiltered]) | |
7771 | return false; | |
7772 | if ([[package name] compare:query options:MatchCompareOptions_ range:NSMakeRange(0, [query length])] != NSOrderedSame) | |
7773 | return false; | |
7774 | return true; | |
7775 | }]; | |
7776 | ||
7777 | [self setSorter:nullptr]; | |
7778 | } | |
7779 | ||
7780 | [self reloadData]; | |
7781 | } | |
7782 | ||
3025d5b4 | 7783 | - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar { |
59f3d290 | 7784 | [self clearData]; |
89bdef78 | 7785 | [self usePrefix:[search_ text]]; |
3025d5b4 JF |
7786 | } |
7787 | ||
7788 | - (void) searchBarButtonClicked:(UISearchBar *)searchBar { | |
5829aea2 | 7789 | [search_ resignFirstResponder]; |
59f3d290 | 7790 | [self useSearch]; |
5829aea2 GP |
7791 | } |
7792 | ||
3025d5b4 JF |
7793 | - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar { |
7794 | [search_ setText:@""]; | |
7795 | [self searchBarButtonClicked:searchBar]; | |
7796 | } | |
7797 | ||
7798 | - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { | |
7799 | [self searchBarButtonClicked:searchBar]; | |
7800 | } | |
7801 | ||
5829aea2 | 7802 | - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { |
89bdef78 | 7803 | [self usePrefix:text]; |
5829aea2 GP |
7804 | } |
7805 | ||
3025d5b4 | 7806 | - (bool) shouldYield { |
56bf1e78 JF |
7807 | return YES; |
7808 | } | |
7809 | ||
7810 | - (bool) shouldBlock { | |
89bdef78 | 7811 | return !summary_; |
3025d5b4 JF |
7812 | } |
7813 | ||
59f3d290 | 7814 | - (bool) isSummarized { |
89bdef78 | 7815 | return summary_; |
59f3d290 JF |
7816 | } |
7817 | ||
9c5737d5 JF |
7818 | - (bool) showsSections { |
7819 | return false; | |
7820 | } | |
7821 | ||
43625891 | 7822 | - (id) initWithDatabase:(Database *)database query:(NSString *)query { |
89bdef78 | 7823 | if ((self = [super initWithDatabase:database title:UCLocalize("SEARCH")])) { |
900095fd | 7824 | search_ = [[[UISearchBar alloc] init] autorelease]; |
830efb5d | 7825 | [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; |
900095fd | 7826 | [search_ setDelegate:self]; |
43625891 | 7827 | |
830efb5d JF |
7828 | UITextField *textField; |
7829 | if ([search_ respondsToSelector:@selector(searchField)]) | |
7830 | textField = [search_ searchField]; | |
7831 | else | |
7832 | textField = MSHookIvar<UITextField *>(search_, "_searchField"); | |
7833 | ||
7834 | [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
7835 | [textField setEnablesReturnKeyAutomatically:NO]; | |
7836 | [[self navigationItem] setTitleView:textField]; | |
7837 | ||
43625891 JF |
7838 | if (query != nil) |
7839 | [search_ setText:query]; | |
89bdef78 | 7840 | [self useSearch]; |
fe8e721f | 7841 | } return self; |
5829aea2 GP |
7842 | } |
7843 | ||
6840bff3 | 7844 | - (void) viewDidAppear:(BOOL)animated { |
5829aea2 | 7845 | [super viewDidAppear:animated]; |
fe8e721f GP |
7846 | |
7847 | if (!searchloaded_) { | |
7848 | searchloaded_ = YES; | |
7849 | [search_ setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; | |
5829aea2 | 7850 | [search_ layoutSubviews]; |
d90a4cd6 | 7851 | } |
058813ae GP |
7852 | |
7853 | if ([self isSummarized]) | |
7854 | [search_ becomeFirstResponder]; | |
5829aea2 | 7855 | } |
d90a4cd6 | 7856 | |
d90a4cd6 | 7857 | - (void) reloadData { |
f50860ee | 7858 | [self resetCursor]; |
9dac415b | 7859 | [super reloadData]; |
d90a4cd6 GP |
7860 | } |
7861 | ||
5829aea2 GP |
7862 | - (void) didSelectPackage:(Package *)package { |
7863 | [search_ resignFirstResponder]; | |
7864 | [super didSelectPackage:package]; | |
d90a4cd6 GP |
7865 | } |
7866 | ||
7867 | @end | |
7868 | /* }}} */ | |
5829aea2 | 7869 | /* Package Settings Controller {{{ */ |
cd79e8cf | 7870 | @interface PackageSettingsController : CyteViewController < |
c21004b9 JF |
7871 | UITableViewDataSource, |
7872 | UITableViewDelegate | |
7873 | > { | |
807ae6d7 | 7874 | _transient Database *database_; |
7b33d201 JF |
7875 | _H<NSString> name_; |
7876 | _H<Package> package_; | |
bf7c998c | 7877 | _H<UITableView, 2> table_; |
7b33d201 JF |
7878 | _H<UISwitch> subscribedSwitch_; |
7879 | _H<UISwitch> ignoredSwitch_; | |
7880 | _H<UITableViewCell> subscribedCell_; | |
7881 | _H<UITableViewCell> ignoredCell_; | |
807ae6d7 JF |
7882 | } |
7883 | ||
5829aea2 | 7884 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
c21004b9 | 7885 | |
807ae6d7 JF |
7886 | @end |
7887 | ||
5829aea2 | 7888 | @implementation PackageSettingsController |
807ae6d7 | 7889 | |
fe8e721f | 7890 | - (NSURL *) navigationURL { |
8861e953 | 7891 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/settings", (id) name_]]; |
fe8e721f GP |
7892 | } |
7893 | ||
5829aea2 GP |
7894 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7895 | if (package_ == nil) | |
7896 | return 0; | |
e954c30a | 7897 | |
2136717a DH |
7898 | if ([package_ installed] == nil) |
7899 | return 1; | |
7900 | else | |
7901 | return 2; | |
e954c30a GP |
7902 | } |
7903 | ||
5829aea2 GP |
7904 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7905 | if (package_ == nil) | |
7906 | return 0; | |
7907 | ||
2136717a DH |
7908 | // both sections contain just one item right now. |
7909 | return 1; | |
b5e7eebb GP |
7910 | } |
7911 | ||
5829aea2 | 7912 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
2136717a | 7913 | return nil; |
e954c30a GP |
7914 | } |
7915 | ||
5829aea2 | 7916 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
2136717a DH |
7917 | if (section == 0) |
7918 | return UCLocalize("SHOW_ALL_CHANGES_EX"); | |
7919 | else | |
7920 | return UCLocalize("IGNORE_UPGRADES_EX"); | |
807ae6d7 JF |
7921 | } |
7922 | ||
5829aea2 GP |
7923 | - (void) onSubscribed:(id)control { |
7924 | bool value([control isOn]); | |
7925 | if (package_ == nil) | |
7926 | return; | |
7927 | if ([package_ setSubscribed:value]) | |
7928 | [delegate_ updateData]; | |
807ae6d7 JF |
7929 | } |
7930 | ||
e5e70358 JF |
7931 | - (void) _updateIgnored { |
7932 | const char *package([name_ UTF8String]); | |
7933 | bool on([ignoredSwitch_ isOn]); | |
7934 | ||
55eb40b9 JF |
7935 | FILE *dpkg(popen("/usr/libexec/cydia/cydo --set-selections", "w")); |
7936 | fwrite(package, strlen(package), 1, dpkg); | |
e5e70358 | 7937 | |
55eb40b9 JF |
7938 | if (on) |
7939 | fwrite(" hold\n", 6, 1, dpkg); | |
7940 | else | |
7941 | fwrite(" install\n", 9, 1, dpkg); | |
e5e70358 | 7942 | |
55eb40b9 | 7943 | pclose(dpkg); |
e5e70358 JF |
7944 | } |
7945 | ||
5829aea2 | 7946 | - (void) onIgnored:(id)control { |
e5e70358 JF |
7947 | NSInvocation *invocation([NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(_updateIgnored)]]); |
7948 | [invocation setTarget:self]; | |
7949 | [invocation setSelector:@selector(_updateIgnored)]; | |
7950 | ||
7951 | [delegate_ reloadDataWithInvocation:invocation]; | |
5829aea2 | 7952 | } |
807ae6d7 | 7953 | |
46aa9775 | 7954 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
7955 | if (package_ == nil) |
7956 | return nil; | |
b5e7eebb | 7957 | |
2136717a | 7958 | switch ([indexPath section]) { |
5829aea2 GP |
7959 | case 0: return subscribedCell_; |
7960 | case 1: return ignoredCell_; | |
36fbb2aa | 7961 | |
5829aea2 GP |
7962 | _nodefault |
7963 | } | |
807ae6d7 | 7964 | |
5829aea2 | 7965 | return nil; |
807ae6d7 JF |
7966 | } |
7967 | ||
fe8e721f | 7968 | - (void) loadView { |
39470a3a JF |
7969 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
7970 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
7971 | [self setView:view]; | |
807ae6d7 | 7972 | |
7b33d201 | 7973 | table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease]; |
fe8e721f | 7974 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b33d201 | 7975 | [(UITableView *) table_ setDataSource:self]; |
fe8e721f | 7976 | [table_ setDelegate:self]; |
39470a3a | 7977 | [view addSubview:table_]; |
807ae6d7 | 7978 | |
7b33d201 | 7979 | subscribedSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7980 | [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7981 | [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7982 | |
7b33d201 | 7983 | ignoredSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7984 | [ignoredSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7985 | [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7986 | |
7b33d201 | 7987 | subscribedCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7988 | [subscribedCell_ setText:UCLocalize("SHOW_ALL_CHANGES")]; |
7989 | [subscribedCell_ setAccessoryView:subscribedSwitch_]; | |
7990 | [subscribedCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
5829aea2 | 7991 | |
7b33d201 | 7992 | ignoredCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7993 | [ignoredCell_ setText:UCLocalize("IGNORE_UPGRADES")]; |
7994 | [ignoredCell_ setAccessoryView:ignoredSwitch_]; | |
7995 | [ignoredCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
fe8e721f | 7996 | } |
5829aea2 | 7997 | |
fe8e721f | 7998 | - (void) viewDidLoad { |
7d887d0b JF |
7999 | [super viewDidLoad]; |
8000 | ||
fe8e721f GP |
8001 | [[self navigationItem] setTitle:UCLocalize("SETTINGS")]; |
8002 | } | |
5829aea2 | 8003 | |
fe8e721f | 8004 | - (void) releaseSubviews { |
fe8e721f | 8005 | ignoredCell_ = nil; |
fe8e721f | 8006 | subscribedCell_ = nil; |
fe8e721f | 8007 | table_ = nil; |
fe8e721f | 8008 | ignoredSwitch_ = nil; |
fe8e721f | 8009 | subscribedSwitch_ = nil; |
7be3eb32 JF |
8010 | |
8011 | [super releaseSubviews]; | |
fe8e721f GP |
8012 | } |
8013 | ||
8014 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { | |
6840bff3 | 8015 | if ((self = [super init]) != nil) { |
fe8e721f | 8016 | database_ = database; |
7b33d201 | 8017 | name_ = package; |
807ae6d7 JF |
8018 | } return self; |
8019 | } | |
8020 | ||
8021 | - (void) reloadData { | |
fe8e721f GP |
8022 | [super reloadData]; |
8023 | ||
5829aea2 | 8024 | package_ = [database_ packageWithName:name_]; |
f3e2c0ac | 8025 | |
5829aea2 | 8026 | if (package_ != nil) { |
5829aea2 GP |
8027 | [subscribedSwitch_ setOn:([package_ subscribed] ? 1 : 0) animated:NO]; |
8028 | [ignoredSwitch_ setOn:([package_ ignored] ? 1 : 0) animated:NO]; | |
f3e2c0ac | 8029 | } // XXX: what now, G? |
dc5812ec | 8030 | |
5829aea2 GP |
8031 | [table_ reloadData]; |
8032 | } | |
6d9712c4 | 8033 | |
dc5812ec | 8034 | @end |
2367a917 | 8035 | /* }}} */ |
d90a4cd6 | 8036 | |
5829aea2 | 8037 | /* Installed Controller {{{ */ |
f50860ee | 8038 | @interface InstalledController : FilteredPackageListController { |
821b1a0c | 8039 | bool sectioned_; |
dc5812ec JF |
8040 | } |
8041 | ||
5829aea2 | 8042 | - (id) initWithDatabase:(Database *)database; |
5829aea2 | 8043 | - (void) queueStatusDidChange; |
dc5812ec | 8044 | |
dc5812ec JF |
8045 | @end |
8046 | ||
5829aea2 | 8047 | @implementation InstalledController |
b4d89997 | 8048 | |
f050e4d9 JF |
8049 | - (NSURL *) referrerURL { |
8050 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/installed/", UI_]]; | |
8051 | } | |
8052 | ||
fe8e721f GP |
8053 | - (NSURL *) navigationURL { |
8054 | return [NSURL URLWithString:@"cydia://installed"]; | |
8055 | } | |
b5e7eebb | 8056 | |
b3906a21 | 8057 | - (void) useRecent { |
821b1a0c JF |
8058 | sectioned_ = false; |
8059 | ||
8060 | @synchronized (self) { | |
8061 | [self setFilter:[](Package *package) { | |
8062 | return ![package uninstalled] && package->role_ < 7; | |
8063 | }]; | |
8064 | ||
8065 | [self setSorter:[](NSMutableArray *packages) { | |
b3906a21 | 8066 | [packages radixSortUsingSelector:@selector(recent)]; |
821b1a0c JF |
8067 | }]; |
8068 | } } | |
8069 | ||
89bdef78 | 8070 | - (void) useFilter:(UISegmentedControl *)segmented { |
821b1a0c JF |
8071 | NSInteger selected([segmented selectedSegmentIndex]); |
8072 | if (selected == 2) | |
b3906a21 | 8073 | return [self useRecent]; |
821b1a0c JF |
8074 | bool simple(selected == 0); |
8075 | sectioned_ = true; | |
89bdef78 JF |
8076 | |
8077 | @synchronized (self) { | |
8078 | [self setFilter:[=](Package *package) { | |
8079 | return ![package uninstalled] && package->role_ <= (simple ? 1 : 3); | |
8080 | }]; | |
821b1a0c JF |
8081 | |
8082 | [self setSorter:nullptr]; | |
89bdef78 JF |
8083 | } } |
8084 | ||
98ddcefd JF |
8085 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { |
8086 | if (sectioned_) | |
8087 | return [super sectionsForPackages:packages]; | |
8088 | ||
8089 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterLongStyle, kCFDateFormatterNoStyle)); | |
8090 | ||
8091 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); | |
7108bdd2 | 8092 | Section *section(nil); |
98ddcefd JF |
8093 | time_t last(0); |
8094 | ||
8095 | for (size_t offset(0), count([packages count]); offset != count; ++offset) { | |
8096 | Package *package([packages objectAtIndex:offset]); | |
8097 | ||
b3906a21 | 8098 | time_t upgraded([package upgraded]); |
0cadb352 JF |
8099 | if (upgraded < 1168364520) |
8100 | upgraded = 0; | |
8101 | else | |
8102 | upgraded -= upgraded % (60 * 60 * 24); | |
98ddcefd | 8103 | |
b3906a21 JF |
8104 | if (section == nil || upgraded != last) { |
8105 | last = upgraded; | |
98ddcefd JF |
8106 | |
8107 | NSString *name; | |
0cadb352 JF |
8108 | if (upgraded == 0) |
8109 | continue; // XXX: name = UCLocalize("..."); | |
8110 | else { | |
8111 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:upgraded]); | |
8112 | [name autorelease]; | |
8113 | } | |
98ddcefd JF |
8114 | |
8115 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
8116 | [sections addObject:section]; | |
8117 | } | |
8118 | ||
8119 | [section addToCount]; | |
8120 | } | |
8121 | ||
8122 | CFRelease(formatter); | |
8123 | return sections; | |
8124 | } | |
8125 | ||
5829aea2 | 8126 | - (id) initWithDatabase:(Database *)database { |
89bdef78 | 8127 | if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED")]) != nil) { |
c8fbe1f4 | 8128 | UISegmentedControl *segmented([[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:UCLocalize("USER"), UCLocalize("EXPERT"), UCLocalize("RECENT"), nil]] autorelease]); |
3544397d JF |
8129 | [segmented setSelectedSegmentIndex:0]; |
8130 | [segmented setSegmentedControlStyle:UISegmentedControlStyleBar]; | |
8131 | [[self navigationItem] setTitleView:segmented]; | |
8132 | ||
8133 | [segmented addTarget:self action:@selector(modeChanged:) forEvents:UIControlEventValueChanged]; | |
89bdef78 | 8134 | [self useFilter:segmented]; |
3544397d | 8135 | |
5829aea2 GP |
8136 | [self queueStatusDidChange]; |
8137 | } return self; | |
dc5812ec JF |
8138 | } |
8139 | ||
5829aea2 GP |
8140 | #if !AlwaysReload |
8141 | - (void) queueButtonClicked { | |
8142 | [delegate_ queue]; | |
dc5812ec | 8143 | } |
5829aea2 | 8144 | #endif |
dc5812ec | 8145 | |
5829aea2 GP |
8146 | - (void) queueStatusDidChange { |
8147 | #if !AlwaysReload | |
55066b9e | 8148 | if (Queuing_) { |
7d3660da | 8149 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
55066b9e JF |
8150 | initWithTitle:UCLocalize("QUEUE") |
8151 | style:UIBarButtonItemStyleDone | |
8152 | target:self | |
8153 | action:@selector(queueButtonClicked) | |
8154 | ] autorelease]]; | |
8155 | } else { | |
7d3660da | 8156 | [[self navigationItem] setRightBarButtonItem:nil]; |
5829aea2 GP |
8157 | } |
8158 | #endif | |
dc5812ec JF |
8159 | } |
8160 | ||
3544397d | 8161 | - (void) modeChanged:(UISegmentedControl *)segmented { |
89bdef78 | 8162 | [self useFilter:segmented]; |
f50860ee | 8163 | [self reloadData]; |
9a7b04c5 JF |
8164 | } |
8165 | ||
5829aea2 GP |
8166 | @end |
8167 | /* }}} */ | |
b5e7eebb | 8168 | |
5829aea2 | 8169 | /* Source Cell {{{ */ |
a9311516 | 8170 | @interface SourceCell : CyteTableViewCell < |
21ac0ce2 JF |
8171 | CyteTableViewCellDelegate, |
8172 | SourceDelegate | |
5829aea2 | 8173 | > { |
21ac0ce2 | 8174 | _H<Source, 1> source_; |
02735663 | 8175 | _H<NSURL> url_; |
7b33d201 JF |
8176 | _H<UIImage> icon_; |
8177 | _H<NSString> origin_; | |
8178 | _H<NSString> label_; | |
21ac0ce2 | 8179 | _H<UIActivityIndicatorView> indicator_; |
5829aea2 | 8180 | } |
dc5812ec | 8181 | |
5829aea2 | 8182 | - (void) setSource:(Source *)source; |
21ac0ce2 | 8183 | - (void) setFetch:(NSNumber *)fetch; |
6da1297d | 8184 | |
5829aea2 | 8185 | @end |
dc5812ec | 8186 | |
5829aea2 | 8187 | @implementation SourceCell |
36bb2ca2 | 8188 | |
02735663 JF |
8189 | - (void) _setImage:(NSArray *)data { |
8190 | if ([url_ isEqual:[data objectAtIndex:0]]) { | |
8191 | icon_ = [data objectAtIndex:1]; | |
8192 | [content_ setNeedsDisplay]; | |
8193 | } | |
8252b666 JF |
8194 | } |
8195 | ||
7bd76e97 | 8196 | - (void) _setSource:(NSURL *) url { |
8252b666 JF |
8197 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
8198 | ||
7bd76e97 JF |
8199 | if (NSData *data = [NSURLConnection |
8200 | sendSynchronousRequest:[NSURLRequest | |
8201 | requestWithURL:url | |
de5f2673 JF |
8202 | cachePolicy:NSURLRequestUseProtocolCachePolicy |
8203 | timeoutInterval:10 | |
7bd76e97 JF |
8204 | ] |
8205 | ||
8206 | returningResponse:NULL | |
8207 | error:NULL | |
8208 | ]) | |
8209 | if (UIImage *image = [UIImage imageWithData:data]) | |
02735663 | 8210 | [self performSelectorOnMainThread:@selector(_setImage:) withObject:[NSArray arrayWithObjects:url, image, nil] waitUntilDone:NO]; |
8252b666 JF |
8211 | |
8212 | [pool release]; | |
8213 | } | |
8214 | ||
5829aea2 | 8215 | - (void) setSource:(Source *)source { |
21ac0ce2 JF |
8216 | source_ = source; |
8217 | [source_ setDelegate:self]; | |
8218 | ||
8219 | [self setFetch:[NSNumber numberWithBool:[source_ fetch]]]; | |
8220 | ||
8dbf3abc | 8221 | icon_ = [UIImage imageNamed:@"unknown.png"]; |
5829aea2 | 8222 | |
7b33d201 | 8223 | origin_ = [source name]; |
7bd76e97 | 8224 | label_ = [source rooturi]; |
5829aea2 GP |
8225 | |
8226 | [content_ setNeedsDisplay]; | |
8252b666 | 8227 | |
02735663 JF |
8228 | url_ = [source iconURL]; |
8229 | [NSThread detachNewThreadSelector:@selector(_setSource:) toTarget:self withObject:url_]; | |
77801ff1 JF |
8230 | } |
8231 | ||
55066b9e | 8232 | - (void) setAllSource { |
21ac0ce2 JF |
8233 | source_ = nil; |
8234 | [indicator_ stopAnimating]; | |
8235 | ||
8dbf3abc | 8236 | icon_ = [UIImage imageNamed:@"folder.png"]; |
55066b9e JF |
8237 | origin_ = UCLocalize("ALL_SOURCES"); |
8238 | label_ = UCLocalize("ALL_SOURCES_EX"); | |
8239 | [content_ setNeedsDisplay]; | |
8240 | } | |
8241 | ||
5829aea2 GP |
8242 | - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
8243 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
8244 | UIView *content([self contentView]); | |
8245 | CGRect bounds([content bounds]); | |
77801ff1 | 8246 | |
b97fcfc6 | 8247 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
5829aea2 GP |
8248 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8249 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
8250 | [content addSubview:content_]; | |
dc5812ec | 8251 | |
5829aea2 GP |
8252 | [content_ setDelegate:self]; |
8253 | [content_ setOpaque:YES]; | |
53db9999 | 8254 | |
21ac0ce2 JF |
8255 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGraySmall] autorelease]; |
8256 | [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin];// | UIViewAutoresizingFlexibleBottomMargin]; | |
8257 | [content addSubview:indicator_]; | |
8258 | ||
53db9999 | 8259 | [[content_ layer] setContentsGravity:kCAGravityTopLeft]; |
5829aea2 GP |
8260 | } return self; |
8261 | } | |
b4d89997 | 8262 | |
21ac0ce2 JF |
8263 | - (void) layoutSubviews { |
8264 | [super layoutSubviews]; | |
8265 | ||
8266 | UIView *content([self contentView]); | |
8267 | CGRect bounds([content bounds]); | |
8268 | ||
8269 | CGRect frame([indicator_ frame]); | |
8270 | frame.origin.x = bounds.size.width - frame.size.width; | |
8323c1b9 | 8271 | frame.origin.y = Retina((bounds.size.height - frame.size.height) / 2); |
21ac0ce2 JF |
8272 | |
8273 | if (kCFCoreFoundationVersionNumber < 800) | |
8274 | frame.origin.x -= 8; | |
8275 | [indicator_ setFrame:frame]; | |
8276 | } | |
8277 | ||
003fc610 | 8278 | - (NSString *) accessibilityLabel { |
66abff39 | 8279 | return origin_; |
003fc610 GP |
8280 | } |
8281 | ||
5829aea2 GP |
8282 | - (void) drawContentRect:(CGRect)rect { |
8283 | bool highlighted(highlighted_); | |
8284 | float width(rect.size.width); | |
36bb2ca2 | 8285 | |
25c1dafb JF |
8286 | if (icon_ != nil) { |
8287 | CGRect rect; | |
8288 | rect.size = [(UIImage *) icon_ size]; | |
8289 | ||
8290 | while (rect.size.width > 32 || rect.size.height > 32) { | |
8291 | rect.size.width /= 2; | |
8292 | rect.size.height /= 2; | |
8293 | } | |
8294 | ||
86a333c6 JF |
8295 | rect.origin.x = 26 - rect.size.width / 2; |
8296 | rect.origin.y = 26 - rect.size.height / 2; | |
25c1dafb | 8297 | |
8323c1b9 | 8298 | [icon_ drawInRect:Retina(rect)]; |
25c1dafb | 8299 | } |
36bb2ca2 | 8300 | |
5d0438dc | 8301 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
5829aea2 | 8302 | UISetColor(White_); |
dc5812ec | 8303 | |
5829aea2 GP |
8304 | if (!highlighted) |
8305 | UISetColor(Black_); | |
ffb4fe6a | 8306 | [origin_ drawAtPoint:CGPointMake(52, 8) forWidth:(width - 49) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
f79a4512 | 8307 | |
5829aea2 | 8308 | if (!highlighted) |
e93fd095 | 8309 | UISetColor(Gray_); |
ffb4fe6a | 8310 | [label_ drawAtPoint:CGPointMake(52, 29) forWidth:(width - 49) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; |
5829aea2 | 8311 | } |
dc5812ec | 8312 | |
21ac0ce2 JF |
8313 | - (void) setFetch:(NSNumber *)fetch { |
8314 | if ([fetch boolValue]) | |
8315 | [indicator_ startAnimating]; | |
8316 | else | |
8317 | [indicator_ stopAnimating]; | |
8318 | } | |
8319 | ||
3f8edf70 GP |
8320 | @end |
8321 | /* }}} */ | |
8322 | /* Sources Controller {{{ */ | |
cd79e8cf | 8323 | @interface SourcesController : CyteViewController < |
5829aea2 GP |
8324 | UITableViewDataSource, |
8325 | UITableViewDelegate | |
8326 | > { | |
8327 | _transient Database *database_; | |
c33064f1 JF |
8328 | unsigned era_; |
8329 | ||
bf7c998c | 8330 | _H<UITableView, 2> list_; |
7b33d201 | 8331 | _H<NSMutableArray> sources_; |
5829aea2 | 8332 | int offset_; |
723a0072 | 8333 | |
7b33d201 JF |
8334 | _H<NSString> href_; |
8335 | _H<UIProgressHUD> hud_; | |
8336 | _H<NSError> error_; | |
dc63e78f | 8337 | |
5829aea2 GP |
8338 | NSURLConnection *trivial_bz2_; |
8339 | NSURLConnection *trivial_gz_; | |
b4d89997 | 8340 | |
5829aea2 GP |
8341 | BOOL cydia_; |
8342 | } | |
dc5812ec | 8343 | |
5829aea2 | 8344 | - (id) initWithDatabase:(Database *)database; |
31eedaae | 8345 | - (void) updateButtonsForEditingStatusAnimated:(BOOL)animated; |
5829aea2 GP |
8346 | |
8347 | @end | |
8348 | ||
8349 | @implementation SourcesController | |
8350 | ||
8351 | - (void) _releaseConnection:(NSURLConnection *)connection { | |
8352 | if (connection != nil) { | |
8353 | [connection cancel]; | |
8354 | //[connection setDelegate:nil]; | |
8355 | [connection release]; | |
36bb2ca2 | 8356 | } |
5829aea2 | 8357 | } |
dc5812ec | 8358 | |
5829aea2 | 8359 | - (void) dealloc { |
5829aea2 GP |
8360 | [self _releaseConnection:trivial_gz_]; |
8361 | [self _releaseConnection:trivial_bz2_]; | |
3178d79b | 8362 | |
5829aea2 GP |
8363 | [super dealloc]; |
8364 | } | |
b5e7eebb | 8365 | |
fe8e721f GP |
8366 | - (NSURL *) navigationURL { |
8367 | return [NSURL URLWithString:@"cydia://sources"]; | |
8368 | } | |
8369 | ||
5829aea2 GP |
8370 | - (void) viewDidAppear:(BOOL)animated { |
8371 | [super viewDidAppear:animated]; | |
8372 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
8373 | } | |
9bb3b295 | 8374 | |
5829aea2 | 8375 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
55066b9e | 8376 | return 2; |
5829aea2 | 8377 | } |
8fe19fc1 | 8378 | |
5829aea2 | 8379 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
55066b9e JF |
8380 | if (section == 1) |
8381 | return UCLocalize("INDIVIDUAL_SOURCES"); | |
90ba4f86 | 8382 | return nil; |
36bb2ca2 | 8383 | } |
b4d89997 | 8384 | |
5829aea2 | 8385 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
55066b9e JF |
8386 | switch (section) { |
8387 | case 0: return 1; | |
8388 | case 1: return [sources_ count]; | |
8389 | default: return 0; | |
8390 | } | |
5829aea2 | 8391 | } |
3178d79b | 8392 | |
5829aea2 | 8393 | - (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath { |
c33064f1 JF |
8394 | @synchronized (database_) { |
8395 | if ([database_ era] != era_) | |
8396 | return nil; | |
55066b9e JF |
8397 | if ([indexPath section] != 1) |
8398 | return nil; | |
8dc0d35d | 8399 | NSUInteger index([indexPath row]); |
55066b9e JF |
8400 | if (index >= [sources_ count]) |
8401 | return nil; | |
8402 | return [sources_ objectAtIndex:index]; | |
c33064f1 | 8403 | } } |
b4d89997 | 8404 | |
5829aea2 GP |
8405 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
8406 | static NSString *cellIdentifier = @"SourceCell"; | |
8407 | ||
8408 | SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
55066b9e | 8409 | if (cell == nil) cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease]; |
b89fa270 | 8410 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
5829aea2 | 8411 | |
55066b9e JF |
8412 | Source *source([self sourceAtIndexPath:indexPath]); |
8413 | if (source == nil) | |
8414 | [cell setAllSource]; | |
8415 | else | |
8416 | [cell setSource:source]; | |
8417 | ||
5829aea2 | 8418 | return cell; |
f6e13561 GP |
8419 | } |
8420 | ||
5829aea2 | 8421 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e | 8422 | SectionsController *controller([[[SectionsController alloc] |
5829aea2 | 8423 | initWithDatabase:database_ |
55066b9e JF |
8424 | source:[self sourceAtIndexPath:indexPath] |
8425 | ] autorelease]); | |
5829aea2 | 8426 | |
3f8edf70 | 8427 | [controller setDelegate:delegate_]; |
3f8edf70 | 8428 | [[self navigationController] pushViewController:controller animated:YES]; |
36bb2ca2 | 8429 | } |
b4d89997 | 8430 | |
6840bff3 | 8431 | - (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e JF |
8432 | if ([indexPath section] != 1) |
8433 | return false; | |
5829aea2 GP |
8434 | Source *source = [self sourceAtIndexPath:indexPath]; |
8435 | return [source record] != nil; | |
dcb47737 RP |
8436 | } |
8437 | ||
6840bff3 | 8438 | - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e | 8439 | _assert([indexPath section] == 1); |
0e1aa02c JF |
8440 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
8441 | Source *source = [self sourceAtIndexPath:indexPath]; | |
8dc0d35d JF |
8442 | if (source == nil) return; |
8443 | ||
0e1aa02c | 8444 | [Sources_ removeObjectForKey:[source key]]; |
c38cbbec | 8445 | |
392ff7e4 | 8446 | [delegate_ _saveConfig]; |
cc3b7d31 | 8447 | [delegate_ reloadDataWithInvocation:nil]; |
0e1aa02c | 8448 | } |
5829aea2 | 8449 | } |
bcccf498 | 8450 | |
51807490 JF |
8451 | - (void) tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { |
8452 | [self updateButtonsForEditingStatusAnimated:YES]; | |
8453 | } | |
8454 | ||
5829aea2 | 8455 | - (void) complete { |
93460555 | 8456 | [delegate_ addTrivialSource:href_]; |
da3ec19c JF |
8457 | href_ = nil; |
8458 | ||
5829aea2 | 8459 | [delegate_ syncData]; |
3178d79b JF |
8460 | } |
8461 | ||
5829aea2 GP |
8462 | - (NSString *) getWarning { |
8463 | NSString *href(href_); | |
8464 | NSRange colon([href rangeOfString:@"://"]); | |
8465 | if (colon.location != NSNotFound) | |
8466 | href = [href substringFromIndex:(colon.location + 3)]; | |
8467 | href = [href stringByAddingPercentEscapes]; | |
8468 | href = [CydiaURL(@"api/repotag/") stringByAppendingString:href]; | |
5829aea2 GP |
8469 | |
8470 | NSURL *url([NSURL URLWithString:href]); | |
8471 | ||
8472 | NSStringEncoding encoding; | |
8473 | NSError *error(nil); | |
8474 | ||
8475 | if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]) | |
8476 | return [warning length] == 0 ? nil : warning; | |
8477 | return nil; | |
807ae6d7 | 8478 | } |
b4d89997 | 8479 | |
5829aea2 GP |
8480 | - (void) _endConnection:(NSURLConnection *)connection { |
8481 | // XXX: the memory management in this method is horribly awkward | |
8482 | ||
8483 | NSURLConnection **field = NULL; | |
44990507 | 8484 | if (connection == trivial_bz2_) |
5829aea2 GP |
8485 | field = &trivial_bz2_; |
8486 | else if (connection == trivial_gz_) | |
8487 | field = &trivial_gz_; | |
8488 | _assert(field != NULL); | |
8489 | [connection release]; | |
8490 | *field = nil; | |
8491 | ||
8492 | if ( | |
5829aea2 GP |
8493 | trivial_bz2_ == nil && |
8494 | trivial_gz_ == nil | |
8495 | ) { | |
da3ec19c JF |
8496 | NSString *warning(cydia_ ? [self yieldToSelector:@selector(getWarning)] : nil); |
8497 | ||
9eae15b8 JF |
8498 | [delegate_ releaseNetworkActivityIndicator]; |
8499 | ||
8500 | [delegate_ removeProgressHUD:hud_]; | |
9eae15b8 JF |
8501 | hud_ = nil; |
8502 | ||
5829aea2 | 8503 | if (cydia_) { |
da3ec19c | 8504 | if (warning != nil) { |
5829aea2 GP |
8505 | UIAlertView *alert = [[[UIAlertView alloc] |
8506 | initWithTitle:UCLocalize("SOURCE_WARNING") | |
8507 | message:warning | |
8508 | delegate:self | |
8509 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8510 | otherButtonTitles: |
8511 | UCLocalize("ADD_ANYWAY"), | |
8512 | nil | |
5829aea2 GP |
8513 | ] autorelease]; |
8514 | ||
8515 | [alert setContext:@"warning"]; | |
8516 | [alert setNumberOfRows:1]; | |
8517 | [alert show]; | |
708cf61e JF |
8518 | |
8519 | // XXX: there used to be this great mechanism called yieldToPopup... who deleted it? | |
8520 | error_ = nil; | |
8521 | return; | |
8522 | } | |
8523 | ||
8524 | [self complete]; | |
5829aea2 GP |
8525 | } else if (error_ != nil) { |
8526 | UIAlertView *alert = [[[UIAlertView alloc] | |
8527 | initWithTitle:UCLocalize("VERIFICATION_ERROR") | |
8528 | message:[error_ localizedDescription] | |
8529 | delegate:self | |
8530 | cancelButtonTitle:UCLocalize("OK") | |
8531 | otherButtonTitles:nil | |
8532 | ] autorelease]; | |
8533 | ||
8534 | [alert setContext:@"urlerror"]; | |
8535 | [alert show]; | |
da3ec19c JF |
8536 | |
8537 | href_ = nil; | |
5829aea2 GP |
8538 | } else { |
8539 | UIAlertView *alert = [[[UIAlertView alloc] | |
8540 | initWithTitle:UCLocalize("NOT_REPOSITORY") | |
8541 | message:UCLocalize("NOT_REPOSITORY_EX") | |
8542 | delegate:self | |
8543 | cancelButtonTitle:UCLocalize("OK") | |
8544 | otherButtonTitles:nil | |
8545 | ] autorelease]; | |
8546 | ||
8547 | [alert setContext:@"trivial"]; | |
8548 | [alert show]; | |
da3ec19c JF |
8549 | |
8550 | href_ = nil; | |
5829aea2 GP |
8551 | } |
8552 | ||
7b33d201 | 8553 | error_ = nil; |
5829aea2 | 8554 | } |
807ae6d7 | 8555 | } |
8fe19fc1 | 8556 | |
5829aea2 GP |
8557 | - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { |
8558 | switch ([response statusCode]) { | |
8559 | case 200: | |
8560 | cydia_ = YES; | |
8561 | } | |
8e05f686 RP |
8562 | } |
8563 | ||
5829aea2 | 8564 | - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { |
47ff9ab8 | 8565 | lprintf("connection:\"%s\" didFailWithError:\"%s\"\n", [href_ UTF8String], [[error localizedDescription] UTF8String]); |
7b33d201 | 8566 | error_ = error; |
5829aea2 | 8567 | [self _endConnection:connection]; |
807ae6d7 | 8568 | } |
b4d89997 | 8569 | |
5829aea2 GP |
8570 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
8571 | [self _endConnection:connection]; | |
8572 | } | |
b4d89997 | 8573 | |
5829aea2 | 8574 | - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method { |
2e1652a9 JF |
8575 | NSURL *url([NSURL URLWithString:href]); |
8576 | ||
5829aea2 | 8577 | NSMutableURLRequest *request = [NSMutableURLRequest |
2e1652a9 | 8578 | requestWithURL:url |
5829aea2 | 8579 | cachePolicy:NSURLRequestUseProtocolCachePolicy |
dd6f3b7b | 8580 | timeoutInterval:10 |
5829aea2 | 8581 | ]; |
bc11cf5b | 8582 | |
5829aea2 | 8583 | [request setHTTPMethod:method]; |
b4d89997 | 8584 | |
5829aea2 GP |
8585 | if (Machine_ != NULL) |
8586 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
2e1652a9 | 8587 | |
1209b7de JF |
8588 | if (UniqueID_ != nil) |
8589 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
8590 | ||
2e1652a9 | 8591 | if ([url isCydiaSecure]) { |
1209b7de | 8592 | if (UniqueID_ != nil) |
c83678e8 | 8593 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; |
2e1652a9 | 8594 | } |
b4d89997 | 8595 | |
5829aea2 | 8596 | return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; |
3178d79b JF |
8597 | } |
8598 | ||
6840bff3 | 8599 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
5829aea2 | 8600 | NSString *context([alert context]); |
dc5812ec | 8601 | |
5829aea2 GP |
8602 | if ([context isEqualToString:@"source"]) { |
8603 | switch (button) { | |
8604 | case 1: { | |
8605 | NSString *href = [[alert textField] text]; | |
8a2d167a JF |
8606 | href = VerifySource(href); |
8607 | if (href == nil) | |
2595e4c3 | 8608 | break; |
8a2d167a | 8609 | href_ = href; |
b4d89997 | 8610 | |
5829aea2 GP |
8611 | trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; |
8612 | trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; | |
b4d89997 | 8613 | |
5829aea2 | 8614 | cydia_ = false; |
8fe19fc1 | 8615 | |
5829aea2 | 8616 | // XXX: this is stupid |
7b33d201 | 8617 | hud_ = [delegate_ addProgressHUD]; |
5829aea2 | 8618 | [hud_ setText:UCLocalize("VERIFYING_URL")]; |
dcaecde2 | 8619 | [delegate_ retainNetworkActivityIndicator]; |
5829aea2 | 8620 | } break; |
770f2a8e | 8621 | |
5829aea2 GP |
8622 | case 0: |
8623 | break; | |
bc11cf5b | 8624 | |
5829aea2 GP |
8625 | _nodefault |
8626 | } | |
770f2a8e | 8627 | |
5829aea2 GP |
8628 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8629 | } else if ([context isEqualToString:@"trivial"]) | |
8630 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8631 | else if ([context isEqualToString:@"urlerror"]) | |
8632 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8633 | else if ([context isEqualToString:@"warning"]) { | |
8634 | switch (button) { | |
8635 | case 1: | |
da3ec19c | 8636 | [self performSelector:@selector(complete) withObject:nil afterDelay:0]; |
5829aea2 | 8637 | break; |
770f2a8e | 8638 | |
5829aea2 GP |
8639 | case 0: |
8640 | break; | |
b5e7eebb | 8641 | |
5829aea2 GP |
8642 | _nodefault |
8643 | } | |
770f2a8e | 8644 | |
5829aea2 GP |
8645 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8646 | } | |
8647 | } | |
770f2a8e | 8648 | |
e67ebdad JF |
8649 | - (void) updateButtonsForEditingStatusAnimated:(BOOL)animated { |
8650 | BOOL editing([list_ isEditing]); | |
8651 | ||
8652 | if (editing) | |
8653 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8654 | initWithTitle:UCLocalize("ADD") | |
8655 | style:UIBarButtonItemStylePlain | |
8656 | target:self | |
8657 | action:@selector(addButtonClicked) | |
8658 | ] autorelease] animated:animated]; | |
8659 | else if ([delegate_ updating]) | |
8660 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8661 | initWithTitle:UCLocalize("CANCEL") | |
8662 | style:UIBarButtonItemStyleDone | |
8663 | target:self | |
8664 | action:@selector(cancelButtonClicked) | |
8665 | ] autorelease] animated:animated]; | |
8666 | else | |
8667 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8668 | initWithTitle:UCLocalize("REFRESH") | |
8669 | style:UIBarButtonItemStylePlain | |
8670 | target:self | |
8671 | action:@selector(refreshButtonClicked) | |
8672 | ] autorelease] animated:animated]; | |
8673 | ||
8674 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
8675 | initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) | |
8676 | style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
8677 | target:self | |
8678 | action:@selector(editButtonClicked) | |
8679 | ] autorelease] animated:animated]; | |
8680 | } | |
8681 | ||
fe8e721f | 8682 | - (void) loadView { |
e8cbebe4 | 8683 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain] autorelease]; |
fe8e721f | 8684 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6ac6e186 | 8685 | [list_ setRowHeight:53]; |
7b33d201 | 8686 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 8687 | [list_ setDelegate:self]; |
e8cbebe4 | 8688 | [self setView:list_]; |
fe8e721f | 8689 | } |
770f2a8e | 8690 | |
fe8e721f | 8691 | - (void) viewDidLoad { |
7d887d0b JF |
8692 | [super viewDidLoad]; |
8693 | ||
fe8e721f | 8694 | [[self navigationItem] setTitle:UCLocalize("SOURCES")]; |
31eedaae JF |
8695 | [self updateButtonsForEditingStatusAnimated:NO]; |
8696 | } | |
8697 | ||
393699d7 | 8698 | - (void) viewWillAppear:(BOOL)animated { |
31eedaae JF |
8699 | [super viewWillAppear:animated]; |
8700 | ||
8701 | [list_ setEditing:NO]; | |
8702 | [self updateButtonsForEditingStatusAnimated:NO]; | |
fe8e721f | 8703 | } |
770f2a8e | 8704 | |
fe8e721f | 8705 | - (void) releaseSubviews { |
fe8e721f | 8706 | list_ = nil; |
7be3eb32 | 8707 | |
4f9acb7c JF |
8708 | sources_ = nil; |
8709 | ||
7be3eb32 | 8710 | [super releaseSubviews]; |
fe8e721f | 8711 | } |
770f2a8e | 8712 | |
fe8e721f GP |
8713 | - (id) initWithDatabase:(Database *)database { |
8714 | if ((self = [super init]) != nil) { | |
8715 | database_ = database; | |
807ae6d7 JF |
8716 | } return self; |
8717 | } | |
770f2a8e | 8718 | |
807ae6d7 | 8719 | - (void) reloadData { |
fe8e721f | 8720 | [super reloadData]; |
e67ebdad | 8721 | [self updateButtonsForEditingStatusAnimated:YES]; |
fe8e721f | 8722 | |
c33064f1 JF |
8723 | @synchronized (database_) { |
8724 | era_ = [database_ era]; | |
8725 | ||
4f9acb7c | 8726 | sources_ = [NSMutableArray arrayWithCapacity:16]; |
5829aea2 GP |
8727 | [sources_ addObjectsFromArray:[database_ sources]]; |
8728 | _trace(); | |
90ba4f86 | 8729 | [sources_ sortUsingSelector:@selector(compareByName:)]; |
5829aea2 GP |
8730 | _trace(); |
8731 | ||
8732 | int count([sources_ count]); | |
8733 | offset_ = 0; | |
8734 | for (int i = 0; i != count; i++) { | |
8735 | if ([[sources_ objectAtIndex:i] record] == nil) | |
8736 | break; | |
8737 | offset_++; | |
770f2a8e | 8738 | } |
807ae6d7 | 8739 | |
5829aea2 | 8740 | [list_ reloadData]; |
c33064f1 | 8741 | } } |
770f2a8e | 8742 | |
5829aea2 GP |
8743 | - (void) showAddSourcePrompt { |
8744 | UIAlertView *alert = [[[UIAlertView alloc] | |
8745 | initWithTitle:UCLocalize("ENTER_APT_URL") | |
8746 | message:nil | |
8747 | delegate:self | |
8748 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8749 | otherButtonTitles: |
8750 | UCLocalize("ADD_SOURCE"), | |
8751 | nil | |
5829aea2 | 8752 | ] autorelease]; |
770f2a8e | 8753 | |
5829aea2 | 8754 | [alert setContext:@"source"]; |
770f2a8e | 8755 | |
5829aea2 GP |
8756 | [alert setNumberOfRows:1]; |
8757 | [alert addTextFieldWithValue:@"http://" label:@""]; | |
770f2a8e | 8758 | |
5829aea2 GP |
8759 | UITextInputTraits *traits = [[alert textField] textInputTraits]; |
8760 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
8761 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
8762 | [traits setKeyboardType:UIKeyboardTypeURL]; | |
8763 | // XXX: UIReturnKeyDone | |
8764 | [traits setReturnKeyType:UIReturnKeyNext]; | |
770f2a8e | 8765 | |
5829aea2 | 8766 | [alert show]; |
770f2a8e JF |
8767 | } |
8768 | ||
5829aea2 GP |
8769 | - (void) addButtonClicked { |
8770 | [self showAddSourcePrompt]; | |
770f2a8e JF |
8771 | } |
8772 | ||
e67ebdad JF |
8773 | - (void) refreshButtonClicked { |
8774 | if ([delegate_ requestUpdate]) | |
8775 | [self updateButtonsForEditingStatusAnimated:YES]; | |
8776 | } | |
5829aea2 | 8777 | |
e67ebdad JF |
8778 | - (void) cancelButtonClicked { |
8779 | [delegate_ cancelUpdate]; | |
5829aea2 GP |
8780 | } |
8781 | ||
8782 | - (void) editButtonClicked { | |
8783 | [list_ setEditing:![list_ isEditing] animated:YES]; | |
31eedaae | 8784 | [self updateButtonsForEditingStatusAnimated:YES]; |
770f2a8e JF |
8785 | } |
8786 | ||
21c6da4b GP |
8787 | @end |
8788 | /* }}} */ | |
f3e11d24 | 8789 | |
f3e11d24 | 8790 | /* Stash Controller {{{ */ |
cd79e8cf | 8791 | @interface StashController : CyteViewController { |
7b33d201 JF |
8792 | _H<UIActivityIndicatorView> spinner_; |
8793 | _H<UILabel> status_; | |
8794 | _H<UILabel> caption_; | |
f3e11d24 | 8795 | } |
6840bff3 | 8796 | |
f3e11d24 GP |
8797 | @end |
8798 | ||
5829aea2 | 8799 | @implementation StashController |
f3e11d24 | 8800 | |
fe8e721f | 8801 | - (void) loadView { |
39470a3a JF |
8802 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
8803 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
8804 | [self setView:view]; | |
8805 | ||
8806 | [view setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; | |
fe8e721f | 8807 | |
7b33d201 | 8808 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; |
fe8e721f | 8809 | CGRect spinrect = [spinner_ frame]; |
8323c1b9 | 8810 | spinrect.origin.x = Retina([[self view] frame].size.width / 2 - spinrect.size.width / 2); |
fe8e721f GP |
8811 | spinrect.origin.y = [[self view] frame].size.height - 80.0f; |
8812 | [spinner_ setFrame:spinrect]; | |
8813 | [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; | |
39470a3a | 8814 | [view addSubview:spinner_]; |
fe8e721f GP |
8815 | [spinner_ startAnimating]; |
8816 | ||
8817 | CGRect captrect; | |
8818 | captrect.size.width = [[self view] frame].size.width; | |
8819 | captrect.size.height = 40.0f; | |
8820 | captrect.origin.x = 0; | |
8323c1b9 | 8821 | captrect.origin.y = Retina([[self view] frame].size.height / 2 - captrect.size.height * 2); |
7b33d201 | 8822 | caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; |
fe8e721f GP |
8823 | [caption_ setText:UCLocalize("PREPARING_FILESYSTEM")]; |
8824 | [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
8825 | [caption_ setFont:[UIFont boldSystemFontOfSize:28.0f]]; | |
8826 | [caption_ setTextColor:[UIColor whiteColor]]; | |
8827 | [caption_ setBackgroundColor:[UIColor clearColor]]; | |
8828 | [caption_ setShadowColor:[UIColor blackColor]]; | |
b129e6d9 | 8829 | [caption_ setTextAlignment:NSTextAlignmentCenter]; |
39470a3a | 8830 | [view addSubview:caption_]; |
fe8e721f GP |
8831 | |
8832 | CGRect statusrect; | |
8833 | statusrect.size.width = [[self view] frame].size.width; | |
8834 | statusrect.size.height = 30.0f; | |
8835 | statusrect.origin.x = 0; | |
8323c1b9 | 8836 | statusrect.origin.y = Retina([[self view] frame].size.height / 2 - statusrect.size.height); |
7b33d201 | 8837 | status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; |
fe8e721f GP |
8838 | [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
8839 | [status_ setText:UCLocalize("EXIT_WHEN_COMPLETE")]; | |
8840 | [status_ setFont:[UIFont systemFontOfSize:16.0f]]; | |
8841 | [status_ setTextColor:[UIColor whiteColor]]; | |
8842 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
8843 | [status_ setShadowColor:[UIColor blackColor]]; | |
b129e6d9 | 8844 | [status_ setTextAlignment:NSTextAlignmentCenter]; |
39470a3a | 8845 | [view addSubview:status_]; |
fe8e721f GP |
8846 | } |
8847 | ||
67dd58c7 JF |
8848 | - (void) releaseSubviews { |
8849 | spinner_ = nil; | |
8850 | status_ = nil; | |
8851 | caption_ = nil; | |
7be3eb32 JF |
8852 | |
8853 | [super releaseSubviews]; | |
67dd58c7 JF |
8854 | } |
8855 | ||
770f2a8e | 8856 | @end |
807ae6d7 | 8857 | /* }}} */ |
770f2a8e | 8858 | |
a06e9179 JF |
8859 | @interface CYURLCache : SDURLCache { |
8860 | } | |
8861 | ||
8862 | @end | |
8863 | ||
8864 | @implementation CYURLCache | |
8865 | ||
8866 | - (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request { | |
8867 | #if !ForRelease | |
8868 | if (false); | |
8869 | else if ([event isEqualToString:@"no-cache"]) | |
8870 | event = @"!!!"; | |
8871 | else if ([event isEqualToString:@"store"]) | |
8872 | event = @">>>"; | |
8873 | else if ([event isEqualToString:@"invalid"]) | |
8874 | event = @"???"; | |
8875 | else if ([event isEqualToString:@"memory"]) | |
8876 | event = @"mem"; | |
8877 | else if ([event isEqualToString:@"disk"]) | |
8878 | event = @"ssd"; | |
8879 | else if ([event isEqualToString:@"miss"]) | |
8880 | event = @"---"; | |
8881 | ||
8882 | NSLog(@"%@: %@", event, [[request URL] absoluteString]); | |
8883 | #endif | |
8884 | } | |
8885 | ||
5e845121 JF |
8886 | - (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request { |
8887 | if (NSURLResponse *response = [cached response]) | |
8888 | if (NSString *mime = [response MIMEType]) | |
8889 | if ([mime isEqualToString:@"text/cache-manifest"]) { | |
8890 | NSURL *url([response URL]); | |
8891 | ||
8892 | #if !ForRelease | |
8893 | NSLog(@"###: %@", [url absoluteString]); | |
8894 | #endif | |
8895 | ||
8896 | @synchronized (HostConfig_) { | |
8897 | [CachedURLs_ addObject:url]; | |
8898 | } | |
8899 | } | |
8900 | ||
8901 | [super storeCachedResponse:cached forRequest:request]; | |
8902 | } | |
8903 | ||
584daea0 JF |
8904 | - (void) createDiskCachePath { |
8905 | [super createDiskCachePath]; | |
584daea0 JF |
8906 | } |
8907 | ||
a06e9179 JF |
8908 | @end |
8909 | ||
2367a917 | 8910 | @interface Cydia : UIApplication < |
adb61bda | 8911 | ConfirmationControllerDelegate, |
6915b806 | 8912 | DatabaseDelegate, |
c88974b1 | 8913 | CydiaDelegate |
2367a917 | 8914 | > { |
7b33d201 | 8915 | _H<UIWindow> window_; |
5fe2bcc6 | 8916 | _H<CydiaTabBarController> tabbar_; |
9549563e | 8917 | _H<CyteTabBarController> emulated_; |
fedd38fe | 8918 | _H<AppCacheController> appcache_; |
3931b718 | 8919 | |
7b33d201 JF |
8920 | _H<NSMutableArray> essential_; |
8921 | _H<NSMutableArray> broken_; | |
dc5812ec JF |
8922 | |
8923 | Database *database_; | |
dc5812ec | 8924 | |
7b33d201 | 8925 | _H<NSURL> starturl_; |
54043703 | 8926 | |
235f5487 | 8927 | unsigned locked_; |
54043703 | 8928 | unsigned activity_; |
9b619239 | 8929 | |
7b33d201 | 8930 | _H<StashController> stash_; |
f3e11d24 | 8931 | |
8c02abc8 | 8932 | bool loaded_; |
dc5812ec JF |
8933 | } |
8934 | ||
fed0d010 | 8935 | - (void) loadData; |
670a0494 | 8936 | |
dc5812ec JF |
8937 | @end |
8938 | ||
8939 | @implementation Cydia | |
8940 | ||
2ef6faad | 8941 | - (void) lockSuspend { |
8a3b565c JF |
8942 | if (locked_++ == 0) { |
8943 | if ($SBSSetInterceptsMenuButtonForever != NULL) | |
8944 | (*$SBSSetInterceptsMenuButtonForever)(true); | |
26c8a4c8 JF |
8945 | |
8946 | [self setIdleTimerDisabled:YES]; | |
8a3b565c | 8947 | } |
2ef6faad JF |
8948 | } |
8949 | ||
8950 | - (void) unlockSuspend { | |
8a3b565c | 8951 | if (--locked_ == 0) { |
26c8a4c8 JF |
8952 | [self setIdleTimerDisabled:NO]; |
8953 | ||
8a3b565c JF |
8954 | if ($SBSSetInterceptsMenuButtonForever != NULL) |
8955 | (*$SBSSetInterceptsMenuButtonForever)(false); | |
8956 | } | |
2ef6faad JF |
8957 | } |
8958 | ||
b5e7eebb | 8959 | - (void) beginUpdate { |
7585ce66 | 8960 | [tabbar_ beginUpdate]; |
b5e7eebb GP |
8961 | } |
8962 | ||
e67ebdad JF |
8963 | - (void) cancelUpdate { |
8964 | [tabbar_ cancelUpdate]; | |
8965 | } | |
8966 | ||
8967 | - (bool) requestUpdate { | |
8968 | if (IsReachable("cydia.saurik.com")) { | |
8969 | [self beginUpdate]; | |
8970 | return true; | |
8971 | } else { | |
8972 | UIAlertView *alert = [[[UIAlertView alloc] | |
8973 | initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("REFRESH")] | |
8974 | message:@"Host Unreachable" // XXX: Localize | |
8975 | delegate:self | |
8976 | cancelButtonTitle:UCLocalize("OK") | |
8977 | otherButtonTitles:nil | |
8978 | ] autorelease]; | |
8979 | ||
8980 | [alert setContext:@"norefresh"]; | |
8981 | [alert show]; | |
8982 | ||
8983 | return false; | |
8984 | } | |
8985 | } | |
8986 | ||
b5e7eebb | 8987 | - (BOOL) updating { |
7585ce66 | 8988 | return [tabbar_ updating]; |
b5e7eebb GP |
8989 | } |
8990 | ||
9bedffaa JF |
8991 | - (void) _loaded { |
8992 | if ([broken_ count] != 0) { | |
8993 | int count = [broken_ count]; | |
8994 | ||
37d2b2a9 | 8995 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 8996 | initWithTitle:(count == 1 ? UCLocalize("HALFINSTALLED_PACKAGE") : [NSString stringWithFormat:UCLocalize("HALFINSTALLED_PACKAGES"), count]) |
b5e7eebb GP |
8997 | message:UCLocalize("HALFINSTALLED_PACKAGE_EX") |
8998 | delegate:self | |
1dc38e9c | 8999 | cancelButtonTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("FORCIBLY_CLEAR"), UCLocalize("UNSAFE")] |
1aa29546 JF |
9000 | otherButtonTitles: |
9001 | UCLocalize("TEMPORARY_IGNORE"), | |
9002 | nil | |
9bedffaa JF |
9003 | ] autorelease]; |
9004 | ||
37d2b2a9 | 9005 | [alert setContext:@"fixhalf"]; |
59befad5 | 9006 | [alert setNumberOfRows:2]; |
37d2b2a9 | 9007 | [alert show]; |
9bedffaa JF |
9008 | } else if (!Ignored_ && [essential_ count] != 0) { |
9009 | int count = [essential_ count]; | |
9010 | ||
37d2b2a9 | 9011 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 9012 | initWithTitle:(count == 1 ? UCLocalize("ESSENTIAL_UPGRADE") : [NSString stringWithFormat:UCLocalize("ESSENTIAL_UPGRADES"), count]) |
b5e7eebb GP |
9013 | message:UCLocalize("ESSENTIAL_UPGRADE_EX") |
9014 | delegate:self | |
9015 | cancelButtonTitle:UCLocalize("TEMPORARY_IGNORE") | |
1aa29546 JF |
9016 | otherButtonTitles: |
9017 | UCLocalize("UPGRADE_ESSENTIAL"), | |
9018 | UCLocalize("COMPLETE_UPGRADE"), | |
9019 | nil | |
9bedffaa JF |
9020 | ] autorelease]; |
9021 | ||
37d2b2a9 GP |
9022 | [alert setContext:@"upgrade"]; |
9023 | [alert show]; | |
9bedffaa JF |
9024 | } |
9025 | } | |
9026 | ||
2925cbba JF |
9027 | - (void) returnToCydia { |
9028 | [self _loaded]; | |
9029 | } | |
9030 | ||
42bbf27d JF |
9031 | - (void) reloadSpringBoard { |
9032 | if (kCFCoreFoundationVersionNumber >= 700) // XXX: iOS 6.x | |
9033 | system("/bin/launchctl stop com.apple.backboardd"); | |
9034 | else | |
9035 | system("/bin/launchctl stop com.apple.SpringBoard"); | |
9036 | sleep(15); | |
9037 | system("/usr/bin/killall backboardd SpringBoard"); | |
9038 | } | |
9039 | ||
7623f855 | 9040 | - (void) _saveConfig { |
4e479350 | 9041 | SaveConfig(database_); |
7623f855 JF |
9042 | } |
9043 | ||
89571a5b | 9044 | // Navigation controller for the queuing badge. |
15f0d613 | 9045 | - (UINavigationController *) queueNavigationController { |
89571a5b GP |
9046 | NSArray *controllers = [tabbar_ viewControllers]; |
9047 | return [controllers objectAtIndex:3]; | |
9048 | } | |
9049 | ||
302bf91c JF |
9050 | - (void) unloadData { |
9051 | [tabbar_ unloadData]; | |
9052 | } | |
9053 | ||
7623f855 JF |
9054 | - (void) _updateData { |
9055 | [self _saveConfig]; | |
302bf91c | 9056 | [self unloadData]; |
f6371a33 | 9057 | |
15f0d613 | 9058 | UINavigationController *navigation = [self queueNavigationController]; |
b5e7eebb | 9059 | |
4305896c | 9060 | id queuedelegate = nil; |
89571a5b GP |
9061 | if ([[navigation viewControllers] count] > 0) |
9062 | queuedelegate = [[navigation viewControllers] objectAtIndex:0]; | |
bc11cf5b | 9063 | |
89571a5b GP |
9064 | [queuedelegate queueStatusDidChange]; |
9065 | [[navigation tabBarItem] setBadgeValue:(Queuing_ ? UCLocalize("Q_D") : nil)]; | |
7623f855 JF |
9066 | } |
9067 | ||
6f87c61a | 9068 | - (void) _refreshIfPossible { |
8c02abc8 | 9069 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
bc11cf5b | 9070 | |
6f87c61a JF |
9071 | NSDate *update([[NSDictionary dictionaryWithContentsOfFile:@ CacheState_] objectForKey:@"LastUpdate"]); |
9072 | ||
45e21ffa | 9073 | bool recently = false; |
45e21ffa GP |
9074 | if (update != nil) { |
9075 | NSTimeInterval interval([update timeIntervalSinceNow]); | |
805ba1b0 | 9076 | if (interval > -(15*60)) |
45e21ffa GP |
9077 | recently = true; |
9078 | } | |
9079 | ||
9080 | // Don't automatic refresh if: | |
9081 | // - We already refreshed recently. | |
9082 | // - We already auto-refreshed this launch. | |
9083 | // - Auto-refresh is disabled. | |
8d3505c5 JF |
9084 | // - Cydia's server is not reachable |
9085 | if (recently || loaded_ || ManualRefresh || !IsReachable("cydia.saurik.com")) { | |
35f0a3b5 | 9086 | // If we are cancelling, we need to make sure it knows it's already loaded. |
45e21ffa | 9087 | loaded_ = true; |
d13edf44 JF |
9088 | |
9089 | [self performSelectorOnMainThread:@selector(_loaded) withObject:nil waitUntilDone:NO]; | |
45e21ffa GP |
9090 | } else { |
9091 | // We are going to load, so remember that. | |
9092 | loaded_ = true; | |
45e21ffa | 9093 | |
6f87c61a | 9094 | [tabbar_ performSelectorOnMainThread:@selector(beginUpdate) withObject:nil waitUntilDone:NO]; |
d13edf44 | 9095 | } |
9aecdc9c | 9096 | |
8c02abc8 | 9097 | [pool release]; |
9aecdc9c GP |
9098 | } |
9099 | ||
9100 | - (void) refreshIfPossible { | |
6f87c61a | 9101 | [NSThread detachNewThreadSelector:@selector(_refreshIfPossible) toTarget:self withObject:nil]; |
9aecdc9c GP |
9102 | } |
9103 | ||
e09e1589 | 9104 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
1a83afc6 | 9105 | _profile(reloadDataWithInvocation) |
e09e1589 | 9106 | @synchronized (self) { |
851f4a99 | 9107 | UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); |
1450c2b3 JF |
9108 | if (hud != nil) |
9109 | [hud setText:UCLocalize("RELOADING_DATA")]; | |
dc5812ec | 9110 | |
4ba8f30a | 9111 | [database_ yieldToSelector:@selector(reloadDataWithInvocation:) withObject:invocation]; |
d061f4ba | 9112 | |
36bb2ca2 | 9113 | size_t changes(0); |
9bedffaa | 9114 | |
a54b1c10 | 9115 | [essential_ removeAllObjects]; |
9bedffaa | 9116 | [broken_ removeAllObjects]; |
b4d89997 | 9117 | |
1a83afc6 | 9118 | _profile(reloadDataWithInvocation$Essential) |
670a0494 | 9119 | NSArray *packages([database_ packages]); |
affeffc7 | 9120 | for (Package *package in packages) { |
9bedffaa JF |
9121 | if ([package half]) |
9122 | [broken_ addObject:package]; | |
823e2d97 JF |
9123 | if ([package upgradableAndEssential:YES] && ![package ignored]) { |
9124 | if ([package essential] && [package installed] != nil) | |
a54b1c10 | 9125 | [essential_ addObject:package]; |
36bb2ca2 | 9126 | ++changes; |
a54b1c10 | 9127 | } |
36bb2ca2 | 9128 | } |
1a83afc6 | 9129 | _end |
b4d89997 | 9130 | |
89571a5b | 9131 | UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem]; |
36bb2ca2 | 9132 | if (changes != 0) { |
0e1784b4 | 9133 | _trace(); |
36bb2ca2 | 9134 | NSString *badge([[NSNumber numberWithInt:changes] stringValue]); |
45e21ffa | 9135 | [changesItem setBadgeValue:badge]; |
65a03a7a | 9136 | [changesItem setAnimatedBadge:([essential_ count] > 0)]; |
0e1784b4 | 9137 | [self setApplicationIconBadgeNumber:changes]; |
c25a610d | 9138 | } else { |
0e1784b4 | 9139 | _trace(); |
45e21ffa GP |
9140 | [changesItem setBadgeValue:nil]; |
9141 | [changesItem setAnimatedBadge:NO]; | |
0e1784b4 | 9142 | [self setApplicationIconBadgeNumber:0]; |
c25a610d | 9143 | } |
b4d89997 | 9144 | |
838ec6e3 | 9145 | Queuing_ = false; |
7623f855 | 9146 | [self _updateData]; |
be64dfbf JF |
9147 | |
9148 | if (hud != nil) | |
9149 | [self removeProgressHUD:hud]; | |
1a83afc6 JF |
9150 | } |
9151 | _end | |
9152 | ||
9153 | PrintTimes(); | |
9154 | } | |
6d9712c4 | 9155 | |
7b0ce2da | 9156 | - (void) updateData { |
7623f855 | 9157 | [self _updateData]; |
b4d89997 JF |
9158 | } |
9159 | ||
383a58ac JF |
9160 | - (void) updateDataAndLoad { |
9161 | [self _updateData]; | |
9162 | if ([database_ progressDelegate] == nil) | |
9163 | [self _loaded]; | |
9164 | } | |
9165 | ||
7b0ce2da JF |
9166 | - (void) update_ { |
9167 | [database_ update]; | |
fca2f596 | 9168 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
7b0ce2da JF |
9169 | } |
9170 | ||
2e9123cb JF |
9171 | - (void) disemulate { |
9172 | if (emulated_ == nil) | |
9173 | return; | |
9174 | ||
81628115 JF |
9175 | if ([window_ respondsToSelector:@selector(setRootViewController:)]) |
9176 | [window_ setRootViewController:tabbar_]; | |
237c3d1c JF |
9177 | else { |
9178 | [window_ addSubview:[tabbar_ view]]; | |
9179 | [[emulated_ view] removeFromSuperview]; | |
9180 | } | |
9181 | ||
2e9123cb JF |
9182 | emulated_ = nil; |
9183 | [window_ setUserInteractionEnabled:YES]; | |
9184 | } | |
9185 | ||
9186 | - (void) presentModalViewController:(UIViewController *)controller force:(BOOL)force { | |
15f0d613 | 9187 | UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:controller] autorelease]); |
2e9123cb JF |
9188 | |
9189 | UIViewController *parent; | |
9190 | if (emulated_ == nil) | |
9191 | parent = tabbar_; | |
9192 | else if (!force) | |
9193 | parent = emulated_; | |
9194 | else { | |
9195 | [self disemulate]; | |
9196 | parent = tabbar_; | |
9197 | } | |
9198 | ||
483881e4 JF |
9199 | if (IsWildcat_) |
9200 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
2e9123cb | 9201 | [parent presentModalViewController:navigation animated:YES]; |
6915b806 JF |
9202 | } |
9203 | ||
9204 | - (ProgressController *) invokeNewProgress:(NSInvocation *)invocation forController:(UINavigationController *)navigation withTitle:(NSString *)title { | |
9205 | ProgressController *progress([[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]); | |
9206 | ||
9207 | if (navigation != nil) | |
9208 | [navigation pushViewController:progress animated:YES]; | |
9209 | else | |
2e9123cb | 9210 | [self presentModalViewController:progress force:YES]; |
6915b806 JF |
9211 | |
9212 | [progress invoke:invocation withTitle:title]; | |
9213 | return progress; | |
9214 | } | |
9215 | ||
9216 | - (void) detachNewProgressSelector:(SEL)selector toTarget:(id)target forController:(UINavigationController *)navigation title:(NSString *)title { | |
9217 | [self invokeNewProgress:[NSInvocation invocationWithSelector:selector forTarget:target] forController:navigation withTitle:title]; | |
9218 | } | |
9219 | ||
9220 | - (void) repairWithInvocation:(NSInvocation *)invocation { | |
9221 | _trace(); | |
8d5bc2ad | 9222 | [self invokeNewProgress:invocation forController:nil withTitle:@"REPAIRING"]; |
6915b806 JF |
9223 | _trace(); |
9224 | } | |
9225 | ||
9226 | - (void) repairWithSelector:(SEL)selector { | |
9227 | [self performSelectorOnMainThread:@selector(repairWithInvocation:) withObject:[NSInvocation invocationWithSelector:selector forTarget:database_] waitUntilDone:YES]; | |
9228 | } | |
9229 | ||
e09e1589 JF |
9230 | - (void) reloadData { |
9231 | [self reloadDataWithInvocation:nil]; | |
2925cbba JF |
9232 | if ([database_ progressDelegate] == nil) |
9233 | [self _loaded]; | |
e09e1589 JF |
9234 | } |
9235 | ||
7b0ce2da | 9236 | - (void) syncData { |
1fe5dc43 | 9237 | [self _saveConfig]; |
33e30380 JF |
9238 | [self detachNewProgressSelector:@selector(update_) toTarget:self forController:nil title:@"UPDATING_SOURCES"]; |
9239 | } | |
1fe5dc43 | 9240 | |
33e30380 | 9241 | - (void) addSource:(NSDictionary *) source { |
25c1dafb | 9242 | CydiaAddSource(source); |
33e30380 | 9243 | } |
7b0ce2da | 9244 | |
33e30380 | 9245 | - (void) addSource:(NSString *)href withDistribution:(NSString *)distribution andSections:(NSArray *)sections { |
25c1dafb | 9246 | CydiaAddSource(href, distribution, sections); |
7b0ce2da JF |
9247 | } |
9248 | ||
8a2d167a JF |
9249 | // XXX: this method should not return anything |
9250 | - (BOOL) addTrivialSource:(NSString *)href { | |
25c1dafb | 9251 | CydiaAddSource(href, @"./"); |
8a2d167a | 9252 | return YES; |
93460555 JF |
9253 | } |
9254 | ||
b4d89997 JF |
9255 | - (void) resolve { |
9256 | pkgProblemResolver *resolver = [database_ resolver]; | |
9257 | ||
9258 | resolver->InstallProtect(); | |
9259 | if (!resolver->Resolve(true)) | |
9260 | _error->Discard(); | |
2367a917 JF |
9261 | } |
9262 | ||
670a0494 | 9263 | - (bool) perform { |
d6c371f5 JF |
9264 | // XXX: this is a really crappy way of doing this. |
9265 | // like, seriously: this state machine is still broken, and cancelling this here doesn't really /fix/ that. | |
9266 | // for one, the user can still /start/ a reloading data event while they have a queue, which is stupid | |
9267 | // for two, this just means there is a race condition between the refresh completing and the confirmation controller appearing. | |
9268 | if ([tabbar_ updating]) | |
9269 | [tabbar_ cancelUpdate]; | |
9270 | ||
670a0494 JF |
9271 | if (![database_ prepare]) |
9272 | return false; | |
49048579 | 9273 | |
adb61bda | 9274 | ConfirmationController *page([[[ConfirmationController alloc] initWithDatabase:database_] autorelease]); |
affeffc7 | 9275 | [page setDelegate:self]; |
15f0d613 | 9276 | UINavigationController *confirm_([[[UINavigationController alloc] initWithRootViewController:page] autorelease]); |
49048579 | 9277 | |
36fbb2aa JF |
9278 | if (IsWildcat_) |
9279 | [confirm_ setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 9280 | [tabbar_ presentModalViewController:confirm_ animated:YES]; |
670a0494 JF |
9281 | |
9282 | return true; | |
3178d79b JF |
9283 | } |
9284 | ||
dc63e78f JF |
9285 | - (void) queue { |
9286 | @synchronized (self) { | |
9287 | [self perform]; | |
9288 | } | |
9289 | } | |
9290 | ||
9291 | - (void) clearPackage:(Package *)package { | |
9292 | @synchronized (self) { | |
9293 | [package clear]; | |
9294 | [self resolve]; | |
9295 | [self perform]; | |
9296 | } | |
9297 | } | |
9298 | ||
77801ff1 JF |
9299 | - (void) installPackages:(NSArray *)packages { |
9300 | @synchronized (self) { | |
9301 | for (Package *package in packages) | |
9302 | [package install]; | |
9303 | [self resolve]; | |
9304 | [self perform]; | |
9305 | } | |
9306 | } | |
9307 | ||
36bb2ca2 JF |
9308 | - (void) installPackage:(Package *)package { |
9309 | @synchronized (self) { | |
9310 | [package install]; | |
9311 | [self resolve]; | |
9312 | [self perform]; | |
9313 | } | |
9314 | } | |
9315 | ||
9316 | - (void) removePackage:(Package *)package { | |
9317 | @synchronized (self) { | |
9318 | [package remove]; | |
9319 | [self resolve]; | |
9320 | [self perform]; | |
9321 | } | |
9322 | } | |
9323 | ||
9324 | - (void) distUpgrade { | |
9325 | @synchronized (self) { | |
670a0494 JF |
9326 | if (![database_ upgrade]) |
9327 | return; | |
36bb2ca2 JF |
9328 | [self perform]; |
9329 | } | |
b4d89997 JF |
9330 | } |
9331 | ||
780cdb3b JF |
9332 | - (void) _uicache { |
9333 | _trace(); | |
5685d514 | 9334 | system("/usr/bin/uicache"); |
780cdb3b JF |
9335 | _trace(); |
9336 | } | |
9337 | ||
9338 | - (void) uicache { | |
9339 | UIProgressHUD *hud([self addProgressHUD]); | |
9340 | [hud setText:UCLocalize("LOADING")]; | |
9341 | [self yieldToSelector:@selector(_uicache)]; | |
9342 | [self removeProgressHUD:hud]; | |
9343 | } | |
9344 | ||
fca2f596 JF |
9345 | - (void) perform_ { |
9346 | [database_ perform]; | |
9347 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; | |
780cdb3b | 9348 | [self performSelectorOnMainThread:@selector(uicache) withObject:nil waitUntilDone:YES]; |
fca2f596 JF |
9349 | } |
9350 | ||
b5e7eebb | 9351 | - (void) confirmWithNavigationController:(UINavigationController *)navigation { |
f6371a33 | 9352 | Queuing_ = false; |
2ef6faad | 9353 | [self lockSuspend]; |
fca2f596 | 9354 | [self detachNewProgressSelector:@selector(perform_) toTarget:self forController:navigation title:@"RUNNING"]; |
2ef6faad | 9355 | [self unlockSuspend]; |
dc5812ec JF |
9356 | } |
9357 | ||
54043703 JF |
9358 | - (void) retainNetworkActivityIndicator { |
9359 | if (activity_++ == 0) | |
9360 | [self setNetworkActivityIndicatorVisible:YES]; | |
848ed88b JF |
9361 | |
9362 | #if TraceLogging | |
9363 | NSLog(@"retainNetworkActivityIndicator->%d", activity_); | |
9364 | #endif | |
54043703 JF |
9365 | } |
9366 | ||
9367 | - (void) releaseNetworkActivityIndicator { | |
9368 | if (--activity_ == 0) | |
9369 | [self setNetworkActivityIndicatorVisible:NO]; | |
848ed88b JF |
9370 | |
9371 | #if TraceLogging | |
9372 | NSLog(@"releaseNetworkActivityIndicator->%d", activity_); | |
9373 | #endif | |
9374 | ||
54043703 JF |
9375 | } |
9376 | ||
674dce72 GP |
9377 | - (void) cancelAndClear:(bool)clear { |
9378 | @synchronized (self) { | |
9379 | if (clear) { | |
c6ca67ba | 9380 | [database_ clear]; |
b5e7eebb | 9381 | Queuing_ = false; |
674dce72 | 9382 | } else { |
b5e7eebb | 9383 | Queuing_ = true; |
6067f1b8 JF |
9384 | } |
9385 | ||
a4217bbb | 9386 | [self _updateData]; |
674dce72 | 9387 | } |
37d2b2a9 | 9388 | } |
7b0ce2da | 9389 | |
b5e7eebb GP |
9390 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
9391 | NSString *context([alert context]); | |
bc11cf5b | 9392 | |
6915b806 JF |
9393 | if ([context isEqualToString:@"conffile"]) { |
9394 | FILE *input = [database_ input]; | |
9395 | if (button == [alert cancelButtonIndex]) | |
9396 | fprintf(input, "N\n"); | |
9397 | else if (button == [alert firstOtherButtonIndex]) | |
9398 | fprintf(input, "Y\n"); | |
9399 | fflush(input); | |
9400 | ||
9401 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
9402 | } else if ([context isEqualToString:@"fixhalf"]) { | |
efa53fa9 | 9403 | if (button == [alert cancelButtonIndex]) { |
37d2b2a9 | 9404 | @synchronized (self) { |
584daea0 | 9405 | for (Package *broken in (id) broken_) { |
37d2b2a9 | 9406 | [broken remove]; |
d8b0f6e3 | 9407 | NSString *id(ShellEscape([broken id])); |
7cf0c7b3 JF |
9408 | system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/rm -f" |
9409 | " /var/lib/dpkg/info/%@.prerm" | |
9410 | " /var/lib/dpkg/info/%@.postrm" | |
9411 | " /var/lib/dpkg/info/%@.preinst" | |
9412 | " /var/lib/dpkg/info/%@.postinst" | |
9413 | " /var/lib/dpkg/info/%@.extrainst_" | |
9414 | "", id, id, id, id, id] UTF8String]); | |
37d2b2a9 | 9415 | } |
7b0ce2da | 9416 | |
37d2b2a9 GP |
9417 | [self resolve]; |
9418 | [self perform]; | |
9419 | } | |
efa53fa9 | 9420 | } else if (button == [alert firstOtherButtonIndex]) { |
37d2b2a9 GP |
9421 | [broken_ removeAllObjects]; |
9422 | [self _loaded]; | |
7b0ce2da JF |
9423 | } |
9424 | ||
37d2b2a9 | 9425 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9426 | } else if ([context isEqualToString:@"upgrade"]) { |
37d2b2a9 GP |
9427 | if (button == [alert firstOtherButtonIndex]) { |
9428 | @synchronized (self) { | |
7b33d201 | 9429 | for (Package *essential in (id) essential_) |
37d2b2a9 | 9430 | [essential install]; |
7b0ce2da | 9431 | |
37d2b2a9 GP |
9432 | [self resolve]; |
9433 | [self perform]; | |
9434 | } | |
9435 | } else if (button == [alert firstOtherButtonIndex] + 1) { | |
9436 | [self distUpgrade]; | |
9437 | } else if (button == [alert cancelButtonIndex]) { | |
9438 | Ignored_ = YES; | |
7b0ce2da JF |
9439 | } |
9440 | ||
37d2b2a9 | 9441 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9442 | } |
7b0ce2da JF |
9443 | } |
9444 | ||
d13edf44 JF |
9445 | - (void) system:(NSString *)command { |
9446 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9447 | ||
985d2dff | 9448 | _trace(); |
0c0a966b | 9449 | system([command UTF8String]); |
985d2dff | 9450 | _trace(); |
d13edf44 JF |
9451 | |
9452 | [pool release]; | |
670a0494 JF |
9453 | } |
9454 | ||
9455 | - (void) applicationWillSuspend { | |
9456 | [database_ clean]; | |
9457 | [super applicationWillSuspend]; | |
bd150f54 JF |
9458 | } |
9459 | ||
235f5487 | 9460 | - (BOOL) isSafeToSuspend { |
5927fe03 JF |
9461 | if (locked_ != 0) { |
9462 | #if !ForRelease | |
9463 | NSLog(@"isSafeToSuspend: locked_ != 0"); | |
9464 | #endif | |
9465 | return false; | |
9466 | } | |
9467 | ||
7187b5b2 JF |
9468 | if ([tabbar_ modalViewController] != nil) |
9469 | return false; | |
9470 | ||
262afe11 GP |
9471 | // Use external process status API internally. |
9472 | // This is probably a really bad idea. | |
235f5487 | 9473 | // XXX: what is the point of this? does this solve anything at all? |
262afe11 GP |
9474 | uint64_t status = 0; |
9475 | int notify_token; | |
9476 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
9477 | notify_get_state(notify_token, &status); | |
9478 | notify_cancel(notify_token); | |
9479 | } | |
9480 | ||
5927fe03 JF |
9481 | if (status != 0) { |
9482 | #if !ForRelease | |
9483 | NSLog(@"isSafeToSuspend: status != 0"); | |
9484 | #endif | |
9485 | return false; | |
9486 | } | |
9487 | ||
9488 | #if !ForRelease | |
9489 | NSLog(@"isSafeToSuspend: -> true"); | |
9490 | #endif | |
9491 | return true; | |
235f5487 JF |
9492 | } |
9493 | ||
216f3dc6 JF |
9494 | - (void) suspendReturningToLastApp:(BOOL)returning { |
9495 | if ([self isSafeToSuspend]) | |
9496 | [super suspendReturningToLastApp:returning]; | |
9497 | } | |
9498 | ||
9499 | - (void) suspend { | |
9500 | if ([self isSafeToSuspend]) | |
9501 | [super suspend]; | |
9502 | } | |
9503 | ||
9504 | - (void) applicationSuspend { | |
9505 | if ([self isSafeToSuspend]) | |
9506 | [super applicationSuspend]; | |
9507 | } | |
9508 | ||
235f5487 JF |
9509 | - (void) applicationSuspend:(__GSEvent *)event { |
9510 | if ([self isSafeToSuspend]) | |
bd150f54 | 9511 | [super applicationSuspend:event]; |
bd150f54 JF |
9512 | } |
9513 | ||
6d9712c4 | 9514 | - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 { |
235f5487 | 9515 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9516 | [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3]; |
9517 | } | |
9518 | ||
9519 | - (void) _setSuspended:(BOOL)value { | |
235f5487 | 9520 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9521 | [super _setSuspended:value]; |
9522 | } | |
9523 | ||
7b0ce2da | 9524 | - (UIProgressHUD *) addProgressHUD { |
8c9453da | 9525 | UIProgressHUD *hud([[[UIProgressHUD alloc] init] autorelease]); |
04fe1349 JF |
9526 | [hud setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
9527 | ||
d061f4ba | 9528 | [window_ setUserInteractionEnabled:NO]; |
534e31fc | 9529 | |
f36e5eac JF |
9530 | UIViewController *target(tabbar_); |
9531 | if (UIViewController *modal = [target modalViewController]) | |
9532 | target = modal; | |
9533 | ||
22b6c4b8 | 9534 | [hud showInView:[target view]]; |
534e31fc | 9535 | |
2ef6faad | 9536 | [self lockSuspend]; |
7b0ce2da JF |
9537 | return hud; |
9538 | } | |
9539 | ||
d061f4ba | 9540 | - (void) removeProgressHUD:(UIProgressHUD *)hud { |
2ef6faad | 9541 | [self unlockSuspend]; |
8c9453da | 9542 | [hud hide]; |
d061f4ba JF |
9543 | [hud removeFromSuperview]; |
9544 | [window_ setUserInteractionEnabled:YES]; | |
9545 | } | |
9546 | ||
f050e4d9 JF |
9547 | - (CyteViewController *) pageForPackage:(NSString *)name withReferrer:(NSString *)referrer { |
9548 | return [[[CYPackageController alloc] initWithDatabase:database_ forPackage:name withReferrer:referrer] autorelease]; | |
c390d3ab JF |
9549 | } |
9550 | ||
f050e4d9 | 9551 | - (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external withReferrer:(NSString *)referrer { |
e47c4742 | 9552 | NSString *scheme([[url scheme] lowercaseString]); |
f6e13561 | 9553 | if ([[url absoluteString] length] <= [scheme length] + 3) |
e47c4742 | 9554 | return nil; |
f6e13561 | 9555 | NSString *path([[url absoluteString] substringFromIndex:[scheme length] + 3]); |
3a159223 | 9556 | NSArray *components([path componentsSeparatedByString:@"/"]); |
f6e13561 | 9557 | |
4fd0c466 | 9558 | if ([scheme isEqualToString:@"apptapp"] && [components count] > 0 && [[components objectAtIndex:0] isEqualToString:@"package"]) { |
f050e4d9 | 9559 | CyteViewController *controller([self pageForPackage:[components objectAtIndex:1] withReferrer:referrer]); |
4fd0c466 JF |
9560 | if (controller != nil) |
9561 | [controller setDelegate:self]; | |
9562 | return controller; | |
9563 | } | |
f6e13561 GP |
9564 | |
9565 | if ([components count] < 1 || ![scheme isEqualToString:@"cydia"]) | |
e47c4742 | 9566 | return nil; |
f6e13561 GP |
9567 | |
9568 | NSString *base([components objectAtIndex:0]); | |
9569 | ||
cd79e8cf | 9570 | CyteViewController *controller = nil; |
f5a17517 | 9571 | |
f70ea899 | 9572 | if ([base isEqualToString:@"url"]) { |
106d645f GP |
9573 | // This kind of URL can contain slashes in the argument, so we can't parse them below. |
9574 | NSString *destination = [[url absoluteString] substringFromIndex:([scheme length] + [@"://" length] + [base length] + [@"/" length])]; | |
a576488f | 9575 | controller = [[[CydiaWebViewController alloc] initWithURL:[NSURL URLWithString:destination]] autorelease]; |
028dbd1c | 9576 | } else if (!external && [components count] == 1) { |
f6e13561 | 9577 | if ([base isEqualToString:@"sources"]) { |
f5a17517 | 9578 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9579 | } |
9580 | ||
9581 | if ([base isEqualToString:@"home"]) { | |
f5a17517 | 9582 | controller = [[[HomeController alloc] init] autorelease]; |
f6e13561 GP |
9583 | } |
9584 | ||
9585 | if ([base isEqualToString:@"sections"]) { | |
55066b9e | 9586 | controller = [[[SectionsController alloc] initWithDatabase:database_ source:nil] autorelease]; |
f6e13561 GP |
9587 | } |
9588 | ||
9589 | if ([base isEqualToString:@"search"]) { | |
43625891 | 9590 | controller = [[[SearchController alloc] initWithDatabase:database_ query:nil] autorelease]; |
f6e13561 GP |
9591 | } |
9592 | ||
9593 | if ([base isEqualToString:@"changes"]) { | |
ea3bb538 | 9594 | controller = [[[ChangesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9595 | } |
9596 | ||
9597 | if ([base isEqualToString:@"installed"]) { | |
f5a17517 | 9598 | controller = [[[InstalledController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9599 | } |
9600 | } else if ([components count] == 2) { | |
8912cff7 | 9601 | NSString *argument = [[components objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
f6e13561 GP |
9602 | |
9603 | if ([base isEqualToString:@"package"]) { | |
f050e4d9 | 9604 | controller = [self pageForPackage:argument withReferrer:referrer]; |
f6e13561 GP |
9605 | } |
9606 | ||
028dbd1c | 9607 | if (!external && [base isEqualToString:@"search"]) { |
8912cff7 | 9608 | controller = [[[SearchController alloc] initWithDatabase:database_ query:argument] autorelease]; |
f6e13561 GP |
9609 | } |
9610 | ||
028dbd1c | 9611 | if (!external && [base isEqualToString:@"sections"]) { |
55066b9e | 9612 | if ([argument isEqualToString:@"all"] || [argument isEqualToString:@"*"]) |
f6e13561 | 9613 | argument = nil; |
55066b9e | 9614 | controller = [[[SectionController alloc] initWithDatabase:database_ source:nil section:argument] autorelease]; |
f6e13561 GP |
9615 | } |
9616 | ||
75d2e426 | 9617 | if ([base isEqualToString:@"sources"]) { |
f6e13561 | 9618 | if ([argument isEqualToString:@"add"]) { |
f5a17517 GP |
9619 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
9620 | [(SourcesController *)controller showAddSourcePrompt]; | |
f6e13561 | 9621 | } else { |
8912cff7 | 9622 | Source *source([database_ sourceWithKey:argument]); |
55066b9e | 9623 | controller = [[[SectionsController alloc] initWithDatabase:database_ source:source] autorelease]; |
f6e13561 GP |
9624 | } |
9625 | } | |
9626 | ||
028dbd1c | 9627 | if (!external && [base isEqualToString:@"launch"]) { |
f6e13561 | 9628 | [self launchApplicationWithIdentifier:argument suspended:NO]; |
f5a17517 | 9629 | return nil; |
f6e13561 | 9630 | } |
028dbd1c | 9631 | } else if (!external && [components count] == 3) { |
8912cff7 JF |
9632 | NSString *arg1 = [[components objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
9633 | NSString *arg2 = [[components objectAtIndex:2] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
f6e13561 GP |
9634 | |
9635 | if ([base isEqualToString:@"package"]) { | |
9636 | if ([arg2 isEqualToString:@"settings"]) { | |
f5a17517 | 9637 | controller = [[[PackageSettingsController alloc] initWithDatabase:database_ package:arg1] autorelease]; |
f6e13561 GP |
9638 | } else if ([arg2 isEqualToString:@"files"]) { |
9639 | if (Package *package = [database_ packageWithName:arg1]) { | |
f5a17517 GP |
9640 | controller = [[[FileTable alloc] initWithDatabase:database_] autorelease]; |
9641 | [(FileTable *)controller setPackage:package]; | |
f6e13561 GP |
9642 | } |
9643 | } | |
c390d3ab | 9644 | } |
55066b9e JF |
9645 | |
9646 | if ([base isEqualToString:@"sections"]) { | |
9647 | Source *source([arg1 isEqualToString:@"*"] ? nil : [database_ sourceWithKey:arg1]); | |
9648 | NSString *section([arg2 isEqualToString:@"*"] ? nil : arg2); | |
9649 | controller = [[[SectionController alloc] initWithDatabase:database_ source:source section:section] autorelease]; | |
9650 | } | |
c390d3ab JF |
9651 | } |
9652 | ||
f5a17517 GP |
9653 | [controller setDelegate:self]; |
9654 | return controller; | |
c390d3ab JF |
9655 | } |
9656 | ||
028dbd1c | 9657 | - (BOOL) openCydiaURL:(NSURL *)url forExternal:(BOOL)external { |
f050e4d9 | 9658 | CyteViewController *page([self pageForURL:url forExternal:external withReferrer:nil]); |
40364973 | 9659 | |
e965092a JF |
9660 | if (page != nil) |
9661 | [tabbar_ setUnselectedViewController:page]; | |
40364973 | 9662 | |
f6e13561 | 9663 | return page != nil; |
40364973 GP |
9664 | } |
9665 | ||
c390d3ab JF |
9666 | - (void) applicationOpenURL:(NSURL *)url { |
9667 | [super applicationOpenURL:url]; | |
d817e4de | 9668 | |
7b33d201 JF |
9669 | if (!loaded_) |
9670 | starturl_ = url; | |
9671 | else | |
9672 | [self openCydiaURL:url forExternal:YES]; | |
c390d3ab JF |
9673 | } |
9674 | ||
bc11cf5b | 9675 | - (void) applicationWillResignActive:(UIApplication *)application { |
7eff7ea6 | 9676 | // Stop refreshing if you get a phone call or lock the device. |
7585ce66 JF |
9677 | if ([tabbar_ updating]) |
9678 | [tabbar_ cancelUpdate]; | |
bc11cf5b | 9679 | |
ca584c15 GP |
9680 | if ([[self superclass] instancesRespondToSelector:@selector(applicationWillResignActive:)]) |
9681 | [super applicationWillResignActive:application]; | |
7eff7ea6 GP |
9682 | } |
9683 | ||
9dd3045d | 9684 | - (void) saveState { |
3d1e6f42 JF |
9685 | [[NSDictionary dictionaryWithObjectsAndKeys: |
9686 | @"InterfaceState", [tabbar_ navigationURLCollection], | |
9687 | @"LastClosed", [NSDate date], | |
9688 | @"InterfaceIndex", [NSNumber numberWithInt:[tabbar_ selectedIndex]], | |
9689 | nil] writeToFile:@ SavedState_ atomically:YES]; | |
fe8e721f GP |
9690 | |
9691 | [self _saveConfig]; | |
9692 | } | |
9693 | ||
9dd3045d JF |
9694 | - (void) applicationWillTerminate:(UIApplication *)application { |
9695 | [self saveState]; | |
9696 | } | |
9697 | ||
d4011d57 | 9698 | - (void) applicationDidEnterBackground:(UIApplication *)application { |
2acc4fa4 JF |
9699 | if (kCFCoreFoundationVersionNumber < 1000 && [self isSafeToSuspend]) |
9700 | return [self terminateWithSuccess]; | |
3d1e6f42 | 9701 | Backgrounded_ = [NSDate date]; |
d4011d57 JF |
9702 | [self saveState]; |
9703 | } | |
9704 | ||
9705 | - (void) applicationWillEnterForeground:(UIApplication *)application { | |
3d1e6f42 | 9706 | if (Backgrounded_ == nil) |
95cd61f0 JF |
9707 | return; |
9708 | ||
3d1e6f42 | 9709 | NSTimeInterval interval([Backgrounded_ timeIntervalSinceNow]); |
95cd61f0 | 9710 | |
6218c58c | 9711 | if (interval <= -(30*60)) { |
888667d5 JF |
9712 | [tabbar_ setSelectedIndex:0]; |
9713 | [[[tabbar_ viewControllers] objectAtIndex:0] popToRootViewControllerAnimated:NO]; | |
9714 | } | |
95cd61f0 | 9715 | |
6218c58c | 9716 | if (interval <= -(15*60)) { |
888667d5 JF |
9717 | if (IsReachable("cydia.saurik.com")) { |
9718 | [tabbar_ beginUpdate]; | |
9719 | [appcache_ reloadURLWithCache:YES]; | |
9720 | } | |
9721 | } | |
f26c90b1 JF |
9722 | |
9723 | if ([database_ delocked]) | |
9724 | [self reloadData]; | |
d4011d57 JF |
9725 | } |
9726 | ||
6915b806 | 9727 | - (void) setConfigurationData:(NSString *)data { |
b4fd1197 | 9728 | static RegEx conffile_r("'(.*)' '(.*)' ([01]) ([01])"); |
6915b806 JF |
9729 | |
9730 | if (!conffile_r(data)) { | |
9731 | lprintf("E:invalid conffile\n"); | |
9732 | return; | |
9733 | } | |
9734 | ||
9735 | NSString *ofile = conffile_r[1]; | |
9736 | //NSString *nfile = conffile_r[2]; | |
9737 | ||
9738 | UIAlertView *alert = [[[UIAlertView alloc] | |
9739 | initWithTitle:UCLocalize("CONFIGURATION_UPGRADE") | |
9740 | message:[NSString stringWithFormat:@"%@\n\n%@", UCLocalize("CONFIGURATION_UPGRADE_EX"), ofile] | |
9741 | delegate:self | |
9742 | cancelButtonTitle:UCLocalize("KEEP_OLD_COPY") | |
9743 | otherButtonTitles: | |
9744 | UCLocalize("ACCEPT_NEW_COPY"), | |
9745 | // XXX: UCLocalize("SEE_WHAT_CHANGED"), | |
9746 | nil | |
9747 | ] autorelease]; | |
9748 | ||
9749 | [alert setContext:@"conffile"]; | |
a08145a8 | 9750 | [alert setNumberOfRows:2]; |
6915b806 JF |
9751 | [alert show]; |
9752 | } | |
9753 | ||
f3e11d24 | 9754 | - (void) addStashController { |
2ef6faad | 9755 | [self lockSuspend]; |
7b33d201 | 9756 | stash_ = [[[StashController alloc] init] autorelease]; |
f3e11d24 GP |
9757 | [window_ addSubview:[stash_ view]]; |
9758 | } | |
9759 | ||
9760 | - (void) removeStashController { | |
9761 | [[stash_ view] removeFromSuperview]; | |
7b33d201 | 9762 | stash_ = nil; |
2ef6faad | 9763 | [self unlockSuspend]; |
f3e11d24 GP |
9764 | } |
9765 | ||
9766 | - (void) stash { | |
9e1f1e91 | 9767 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; |
f3e11d24 | 9768 | UpdateExternalStatus(1); |
0c0a966b | 9769 | [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/free.sh"]; |
f3e11d24 | 9770 | UpdateExternalStatus(0); |
f3e11d24 GP |
9771 | |
9772 | [self removeStashController]; | |
42bbf27d | 9773 | [self reloadSpringBoard]; |
f3e11d24 GP |
9774 | } |
9775 | ||
f6e13561 | 9776 | - (void) setupViewControllers { |
5fe2bcc6 | 9777 | tabbar_ = [[[CydiaTabBarController alloc] initWithDatabase:database_] autorelease]; |
851f4a99 | 9778 | |
6445279b JF |
9779 | NSMutableArray *items; |
9780 | if (kCFCoreFoundationVersionNumber < 800) { | |
9781 | items = [NSMutableArray arrayWithObjects: | |
8dbf3abc JF |
9782 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage imageNamed:@"home.png"] tag:0] autorelease], |
9783 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage imageNamed:@"install.png"] tag:0] autorelease], | |
9784 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage imageNamed:@"changes.png"] tag:0] autorelease], | |
9785 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage imageNamed:@"manage.png"] tag:0] autorelease], | |
9786 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage imageNamed:@"search.png"] tag:0] autorelease], | |
6445279b | 9787 | nil]; |
851f4a99 | 9788 | } else { |
6445279b | 9789 | items = [NSMutableArray arrayWithObjects: |
8dbf3abc JF |
9790 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage imageNamed:@"home7.png"] selectedImage:[UIImage imageNamed:@"home7s.png"]] autorelease], |
9791 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage imageNamed:@"install7.png"] selectedImage:[UIImage imageNamed:@"install7s.png"]] autorelease], | |
9792 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage imageNamed:@"changes7.png"] selectedImage:[UIImage imageNamed:@"changes7s.png"]] autorelease], | |
9793 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage imageNamed:@"manage7.png"] selectedImage:[UIImage imageNamed:@"manage7s.png"]] autorelease], | |
9794 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage imageNamed:@"search7.png"] selectedImage:[UIImage imageNamed:@"search7s.png"]] autorelease], | |
6445279b | 9795 | nil]; |
851f4a99 GP |
9796 | } |
9797 | ||
9798 | NSMutableArray *controllers([NSMutableArray array]); | |
851f4a99 | 9799 | for (UITabBarItem *item in items) { |
15f0d613 | 9800 | UINavigationController *controller([[[UINavigationController alloc] init] autorelease]); |
851f4a99 GP |
9801 | [controller setTabBarItem:item]; |
9802 | [controllers addObject:controller]; | |
9803 | } | |
851f4a99 | 9804 | [tabbar_ setViewControllers:controllers]; |
4305896c | 9805 | |
f6e13561 | 9806 | [tabbar_ setUpdateDelegate:self]; |
851f4a99 GP |
9807 | } |
9808 | ||
968afbcd | 9809 | - (void) _sendMemoryWarningNotification { |
85106ebe JF |
9810 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: maybe 4_0? |
9811 | [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification" object:[UIApplication sharedApplication]]; | |
9812 | else | |
9813 | [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidReceiveMemoryWarningNotification" object:[UIApplication sharedApplication]]; | |
968afbcd JF |
9814 | } |
9815 | ||
9816 | - (void) _sendMemoryWarningNotifications { | |
9817 | while (true) { | |
9818 | [self performSelectorOnMainThread:@selector(_sendMemoryWarningNotification) withObject:nil waitUntilDone:NO]; | |
740f9f75 JF |
9819 | sleep(2); |
9820 | //usleep(2000000); | |
968afbcd JF |
9821 | } |
9822 | } | |
9823 | ||
9256a1ce JF |
9824 | - (void) applicationDidReceiveMemoryWarning:(UIApplication *)application { |
9825 | NSLog(@"--"); | |
9826 | [[NSURLCache sharedURLCache] removeAllCachedResponses]; | |
9827 | } | |
9828 | ||
bd150f54 | 9829 | - (void) applicationDidFinishLaunching:(id)unused { |
968afbcd JF |
9830 | //[NSThread detachNewThreadSelector:@selector(_sendMemoryWarningNotifications) toTarget:self withObject:nil]; |
9831 | ||
7e30ba6d | 9832 | _trace(); |
1e94d48b JF |
9833 | if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) |
9834 | [self setApplicationSupportsShakeToEdit:NO]; | |
9835 | ||
48f1762f JF |
9836 | @synchronized (HostConfig_) { |
9837 | [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]]; | |
9838 | } | |
3171f7fe | 9839 | |
4058b165 JF |
9840 | [NSURLCache setSharedURLCache:[[[CYURLCache alloc] |
9841 | initWithMemoryCapacity:524288 | |
9842 | diskCapacity:10485760 | |
b44af625 | 9843 | diskPath:Cache("SDURLCache") |
4058b165 | 9844 | ] autorelease]]; |
71cc7be1 | 9845 | |
a576488f | 9846 | [CydiaWebViewController _initialize]; |
ea173384 | 9847 | |
bfc87a4d JF |
9848 | [NSURLProtocol registerClass:[CydiaURLProtocol class]]; |
9849 | ||
793aee35 JF |
9850 | // this would disallow http{,s} URLs from accessing this data |
9851 | //[WebView registerURLSchemeAsLocal:@"cydia"]; | |
9852 | ||
7b33d201 JF |
9853 | Font12_ = [UIFont systemFontOfSize:12]; |
9854 | Font12Bold_ = [UIFont boldSystemFontOfSize:12]; | |
9855 | Font14_ = [UIFont systemFontOfSize:14]; | |
2cdc6e57 | 9856 | Font18_ = [UIFont systemFontOfSize:18]; |
7b33d201 JF |
9857 | Font18Bold_ = [UIFont boldSystemFontOfSize:18]; |
9858 | Font22Bold_ = [UIFont boldSystemFontOfSize:22]; | |
f641a0e5 | 9859 | |
7b33d201 JF |
9860 | essential_ = [NSMutableArray arrayWithCapacity:4]; |
9861 | broken_ = [NSMutableArray arrayWithCapacity:4]; | |
bd150f54 | 9862 | |
4e89e880 | 9863 | // XXX: I really need this thing... like, seriously... I'm sorry |
fedd38fe JF |
9864 | appcache_ = [[[AppCacheController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/appcache/", UI_]]] autorelease]; |
9865 | [appcache_ reloadData]; | |
4e89e880 | 9866 | |
7b33d201 | 9867 | window_ = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
f641a0e5 JF |
9868 | [window_ orderFront:self]; |
9869 | [window_ makeKey:self]; | |
c390d3ab | 9870 | [window_ setHidden:NO]; |
04fe1349 | 9871 | |
96ed699d | 9872 | if (false) stash: { |
f3e11d24 | 9873 | [self addStashController]; |
3931b718 JF |
9874 | // XXX: this would be much cleaner as a yieldToSelector: |
9875 | // that way the removeStashController could happen right here inline | |
9876 | // we also could no longer require the useless stash_ field anymore | |
f3e11d24 GP |
9877 | [self performSelector:@selector(stash) withObject:nil afterDelay:0]; |
9878 | return; | |
9879 | } | |
9880 | ||
2656fd54 JF |
9881 | struct stat root; |
9882 | int error(stat("/", &root)); | |
9883 | _assert(error != -1); | |
9884 | ||
96ed699d | 9885 | #define Stash_(path) do { \ |
2656fd54 JF |
9886 | struct stat folder; \ |
9887 | int error(lstat((path), &folder)); \ | |
9888 | if (error != -1 && ( \ | |
9889 | folder.st_dev == root.st_dev && \ | |
9890 | S_ISDIR(folder.st_mode) \ | |
9891 | ) || error == -1 && ( \ | |
9892 | errno == ENOENT || \ | |
9893 | errno == ENOTDIR \ | |
9894 | )) goto stash; \ | |
96ed699d JF |
9895 | } while (false) |
9896 | ||
40bb5513 | 9897 | Stash_("/Applications"); |
96ed699d JF |
9898 | Stash_("/Library/Ringtones"); |
9899 | Stash_("/Library/Wallpaper"); | |
9900 | //Stash_("/usr/bin"); | |
9901 | Stash_("/usr/include"); | |
96ed699d JF |
9902 | Stash_("/usr/share"); |
9903 | //Stash_("/var/lib"); | |
9904 | ||
770f2a8e | 9905 | database_ = [Database sharedInstance]; |
6915b806 | 9906 | [database_ setDelegate:self]; |
bfc87a4d | 9907 | |
89571a5b | 9908 | [window_ setUserInteractionEnabled:NO]; |
0be165c8 | 9909 | [self setupViewControllers]; |
6915b806 | 9910 | |
9549563e JF |
9911 | CydiaLoadingViewController *loading([[[CydiaLoadingViewController alloc] init] autorelease]); |
9912 | UINavigationController *navigation([[[UINavigationController alloc] init] autorelease]); | |
9913 | [navigation setViewControllers:[NSArray arrayWithObject:loading]]; | |
9914 | ||
9915 | emulated_ = [[[CyteTabBarController alloc] init] autorelease]; | |
9916 | [emulated_ setViewControllers:[NSArray arrayWithObject:navigation]]; | |
9917 | [emulated_ setSelectedIndex:0]; | |
1c28763e JF |
9918 | |
9919 | if ([emulated_ respondsToSelector:@selector(concealTabBarSelection)]) | |
9920 | [emulated_ concealTabBarSelection]; | |
9549563e | 9921 | |
81628115 JF |
9922 | if ([window_ respondsToSelector:@selector(setRootViewController:)]) |
9923 | [window_ setRootViewController:emulated_]; | |
237c3d1c JF |
9924 | else |
9925 | [window_ addSubview:[emulated_ view]]; | |
5ccb47d8 | 9926 | |
fed0d010 | 9927 | [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; |
c626a63f | 9928 | _trace(); |
fed0d010 JF |
9929 | } |
9930 | ||
d3a28a81 GP |
9931 | - (NSArray *) defaultStartPages { |
9932 | NSMutableArray *standard = [NSMutableArray array]; | |
9933 | [standard addObject:[NSArray arrayWithObject:@"cydia://home"]]; | |
55066b9e | 9934 | [standard addObject:[NSArray arrayWithObject:@"cydia://sources"]]; |
d3a28a81 | 9935 | [standard addObject:[NSArray arrayWithObject:@"cydia://changes"]]; |
55066b9e | 9936 | [standard addObject:[NSArray arrayWithObject:@"cydia://installed"]]; |
d3a28a81 GP |
9937 | [standard addObject:[NSArray arrayWithObject:@"cydia://search"]]; |
9938 | return standard; | |
9939 | } | |
9940 | ||
fed0d010 | 9941 | - (void) loadData { |
c626a63f | 9942 | _trace(); |
4121c5e0 JF |
9943 | if ([emulated_ modalViewController] != nil) |
9944 | [emulated_ dismissModalViewControllerAnimated:YES]; | |
9945 | [window_ setUserInteractionEnabled:NO]; | |
fed0d010 | 9946 | |
2925cbba JF |
9947 | [self reloadDataWithInvocation:nil]; |
9948 | [self refreshIfPossible]; | |
2e9123cb | 9949 | [self disemulate]; |
5ccb47d8 | 9950 | |
3d1e6f42 JF |
9951 | NSDictionary *state([NSDictionary dictionaryWithContentsOfFile:@ SavedState_]); |
9952 | ||
9953 | int savedIndex = [[state objectForKey:@"InterfaceIndex"] intValue]; | |
9954 | NSArray *saved = [[[state objectForKey:@"InterfaceState"] mutableCopy] autorelease]; | |
d3a28a81 GP |
9955 | int standardIndex = 0; |
9956 | NSArray *standard = [self defaultStartPages]; | |
fe8e721f | 9957 | |
d3a28a81 GP |
9958 | BOOL valid = YES; |
9959 | ||
9960 | if (saved == nil) | |
9961 | valid = NO; | |
9962 | ||
3d1e6f42 | 9963 | NSDate *closed = [state objectForKey:@"LastClosed"]; |
d3a28a81 | 9964 | if (valid && closed != nil) { |
fe8e721f | 9965 | NSTimeInterval interval([closed timeIntervalSinceNow]); |
ade2267f | 9966 | if (interval <= -(30*60)) |
d3a28a81 | 9967 | valid = NO; |
fe8e721f GP |
9968 | } |
9969 | ||
d3a28a81 GP |
9970 | if (valid && [saved count] != [standard count]) |
9971 | valid = NO; | |
efa53fa9 | 9972 | |
d3a28a81 GP |
9973 | if (valid) { |
9974 | for (unsigned int i = 0; i < [standard count]; i++) { | |
9975 | NSArray *std = [standard objectAtIndex:i], *sav = [saved objectAtIndex:i]; | |
9976 | // XXX: The "hasPrefix" sanity check here could be, in theory, fooled, | |
9977 | // but it's good enough for now. | |
9978 | if ([sav count] == 0 || ![[sav objectAtIndex:0] hasPrefix:[std objectAtIndex:0]]) { | |
9979 | valid = NO; | |
9980 | break; | |
9981 | } | |
fe8e721f | 9982 | } |
fe8e721f GP |
9983 | } |
9984 | ||
d3a28a81 GP |
9985 | NSArray *items = nil; |
9986 | if (valid) { | |
9987 | [tabbar_ setSelectedIndex:savedIndex]; | |
9988 | items = saved; | |
9989 | } else { | |
9990 | [tabbar_ setSelectedIndex:standardIndex]; | |
9991 | items = standard; | |
9992 | } | |
9993 | ||
fe8e721f GP |
9994 | for (unsigned int tab = 0; tab < [[tabbar_ viewControllers] count]; tab++) { |
9995 | NSArray *stack = [items objectAtIndex:tab]; | |
15f0d613 | 9996 | UINavigationController *navigation = [[tabbar_ viewControllers] objectAtIndex:tab]; |
fe8e721f GP |
9997 | NSMutableArray *current = [NSMutableArray array]; |
9998 | ||
9999 | for (unsigned int nav = 0; nav < [stack count]; nav++) { | |
10000 | NSString *addr = [stack objectAtIndex:nav]; | |
10001 | NSURL *url = [NSURL URLWithString:addr]; | |
f050e4d9 | 10002 | CyteViewController *page = [self pageForURL:url forExternal:NO withReferrer:nil]; |
fe8e721f GP |
10003 | if (page != nil) |
10004 | [current addObject:page]; | |
10005 | } | |
10006 | ||
10007 | [navigation setViewControllers:current]; | |
10008 | } | |
f6e13561 | 10009 | |
89571a5b | 10010 | // (Try to) show the startup URL. |
f6e13561 | 10011 | if (starturl_ != nil) { |
f14bba69 | 10012 | [self openCydiaURL:starturl_ forExternal:YES]; |
f6e13561 GP |
10013 | starturl_ = nil; |
10014 | } | |
bd150f54 JF |
10015 | } |
10016 | ||
b5e7eebb | 10017 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { |
674dce72 | 10018 | if (item != nil && IsWildcat_) { |
b5e7eebb | 10019 | [sheet showFromBarButtonItem:item animated:YES]; |
674dce72 GP |
10020 | } else { |
10021 | [sheet showInView:window_]; | |
10022 | } | |
36bb2ca2 JF |
10023 | } |
10024 | ||
6915b806 JF |
10025 | - (void) addProgressEvent:(CydiaProgressEvent *)event forTask:(NSString *)task { |
10026 | id<ProgressDelegate> progress([database_ progressDelegate] ?: [self invokeNewProgress:nil forController:nil withTitle:task]); | |
10027 | [progress setTitle:task]; | |
10028 | [progress addProgressEvent:event]; | |
10029 | } | |
10030 | ||
10031 | - (void) addProgressEventForTask:(NSArray *)data { | |
10032 | CydiaProgressEvent *event([data objectAtIndex:0]); | |
10033 | NSString *task([data count] < 2 ? nil : [data objectAtIndex:1]); | |
10034 | [self addProgressEvent:event forTask:task]; | |
10035 | } | |
10036 | ||
10037 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task { | |
10038 | [self performSelectorOnMainThread:@selector(addProgressEventForTask:) withObject:[NSArray arrayWithObjects:event, task, nil] waitUntilDone:YES]; | |
10039 | } | |
10040 | ||
dc5812ec JF |
10041 | @end |
10042 | ||
b4d89997 JF |
10043 | /*IMP alloc_; |
10044 | id Alloc_(id self, SEL selector) { | |
10045 | id object = alloc_(self, selector); | |
c390d3ab | 10046 | lprintf("[%s]A-%p\n", self->isa->name, object); |
b4d89997 JF |
10047 | return object; |
10048 | }*/ | |
10049 | ||
36bb2ca2 JF |
10050 | /*IMP dealloc_; |
10051 | id Dealloc_(id self, SEL selector) { | |
10052 | id object = dealloc_(self, selector); | |
c390d3ab | 10053 | lprintf("[%s]D-%p\n", self->isa->name, object); |
36bb2ca2 JF |
10054 | return object; |
10055 | }*/ | |
b4d89997 | 10056 | |
30c5be06 JF |
10057 | Class $NSURLConnection; |
10058 | ||
10059 | MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) { | |
0c4fe0f4 | 10060 | NSMutableURLRequest *copy([[request mutableCopy] autorelease]); |
30c5be06 JF |
10061 | |
10062 | NSURL *url([copy URL]); | |
5e845121 | 10063 | |
30c5be06 | 10064 | NSString *host([url host]); |
e4b48f2f JF |
10065 | NSString *scheme([[url scheme] lowercaseString]); |
10066 | ||
10067 | NSString *compound([NSString stringWithFormat:@"%@:%@", scheme, host]); | |
30c5be06 | 10068 | |
48f1762f JF |
10069 | @synchronized (HostConfig_) { |
10070 | if ([copy respondsToSelector:@selector(setHTTPShouldUsePipelining:)]) | |
10071 | if ([PipelinedHosts_ containsObject:host] || [PipelinedHosts_ containsObject:compound]) | |
10072 | [copy setHTTPShouldUsePipelining:YES]; | |
5e845121 JF |
10073 | |
10074 | if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"]) | |
10075 | if ([control isEqualToString:@"max-age=0"]) | |
3a23d36e | 10076 | if ([CachedURLs_ containsObject:url]) { |
5e845121 | 10077 | #if !ForRelease |
3a23d36e | 10078 | NSLog(@"~~~: %@", url); |
5e845121 JF |
10079 | #endif |
10080 | ||
10081 | [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; | |
10082 | ||
10083 | [copy setValue:nil forHTTPHeaderField:@"Cache-Control"]; | |
10084 | [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"]; | |
10085 | [copy setValue:nil forHTTPHeaderField:@"If-None-Match"]; | |
10086 | } | |
48f1762f | 10087 | } |
30c5be06 JF |
10088 | |
10089 | if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) { | |
10090 | } return self; | |
10091 | } | |
10092 | ||
b1497b56 JF |
10093 | Class $WAKWindow; |
10094 | ||
4cc9e99a | 10095 | static CGSize $WAKWindow$screenSize(WAKWindow *self, SEL _cmd) { |
b1497b56 JF |
10096 | CGSize size([[UIScreen mainScreen] bounds].size); |
10097 | /*if ([$WAKWindow respondsToSelector:@selector(hasLandscapeOrientation)]) | |
10098 | if ([$WAKWindow hasLandscapeOrientation]) | |
10099 | std::swap(size.width, size.height);*/ | |
10100 | return size; | |
438d6708 JF |
10101 | } |
10102 | ||
29cbf4e5 JF |
10103 | Class $NSUserDefaults; |
10104 | ||
10105 | MSHook(id, NSUserDefaults$objectForKey$, NSUserDefaults *self, SEL _cmd, NSString *key) { | |
10106 | if ([key respondsToSelector:@selector(isEqualToString:)] && [key isEqualToString:@"WebKitLocalStorageDatabasePathPreferenceKey"]) | |
b44af625 | 10107 | return Cache("LocalStorage"); |
29cbf4e5 JF |
10108 | return _NSUserDefaults$objectForKey$(self, _cmd, key); |
10109 | } | |
10110 | ||
9adfb865 JF |
10111 | static NSMutableDictionary *AutoreleaseDeepMutableCopyOfDictionary(CFTypeRef type) { |
10112 | if (type == NULL) | |
10113 | return nil; | |
10114 | if (CFGetTypeID(type) != CFDictionaryGetTypeID()) | |
10115 | return nil; | |
10116 | CFTypeRef copy(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, type, kCFPropertyListMutableContainers)); | |
10117 | CFRelease(type); | |
10118 | return [(NSMutableDictionary *) copy autorelease]; | |
10119 | } | |
10120 | ||
d13edf44 | 10121 | int main(int argc, char *argv[]) { |
db1e5e0d JF |
10122 | int fd(open("/tmp/cydia.log", O_WRONLY | O_APPEND | O_CREAT, 0644)); |
10123 | dup2(fd, 2); | |
10124 | close(fd); | |
10125 | ||
d13edf44 JF |
10126 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
10127 | ||
d6dad1b4 | 10128 | _trace(); |
f79a4512 | 10129 | |
9a60abe5 JF |
10130 | UpdateExternalStatus(0); |
10131 | ||
57e8b225 JF |
10132 | UIScreen *screen([UIScreen mainScreen]); |
10133 | if ([screen respondsToSelector:@selector(scale)]) | |
10134 | ScreenScale_ = [screen scale]; | |
10135 | else | |
10136 | ScreenScale_ = 1; | |
10137 | ||
c138614d | 10138 | UIDevice *device([UIDevice currentDevice]); |
08157100 | 10139 | if ([device respondsToSelector:@selector(userInterfaceIdiom)]) { |
c138614d | 10140 | UIUserInterfaceIdiom idiom([device userInterfaceIdiom]); |
08157100 JF |
10141 | if (idiom == UIUserInterfaceIdiomPad) |
10142 | IsWildcat_ = true; | |
c138614d JF |
10143 | } |
10144 | ||
08157100 JF |
10145 | Idiom_ = IsWildcat_ ? @"ipad" : @"iphone"; |
10146 | ||
b4fd1197 | 10147 | RegEx pattern("([0-9]+\\.[0-9]+).*"); |
fd825a2d | 10148 | |
9a4a4754 JF |
10149 | if (pattern([device systemVersion])) |
10150 | Firmware_ = pattern[1]; | |
fd825a2d JF |
10151 | if (pattern(Cydia_)) |
10152 | Major_ = pattern[1]; | |
9a4a4754 | 10153 | |
7b33d201 | 10154 | SessionData_ = [NSMutableDictionary dictionaryWithCapacity:4]; |
ef974f52 | 10155 | |
7b33d201 | 10156 | HostConfig_ = [[[NSObject alloc] init] autorelease]; |
48f1762f JF |
10157 | @synchronized (HostConfig_) { |
10158 | BridgedHosts_ = [NSMutableSet setWithCapacity:4]; | |
2e1652a9 | 10159 | InsecureHosts_ = [NSMutableSet setWithCapacity:4]; |
48f1762f | 10160 | PipelinedHosts_ = [NSMutableSet setWithCapacity:4]; |
5e845121 | 10161 | CachedURLs_ = [NSMutableSet setWithCapacity:32]; |
48f1762f | 10162 | } |
5df7ecfb | 10163 | |
de595d91 JF |
10164 | NSString *ui(@"ui/ios"); |
10165 | if (Idiom_ != nil) | |
10166 | ui = [ui stringByAppendingString:[NSString stringWithFormat:@"~%@", Idiom_]]; | |
fd825a2d | 10167 | ui = [ui stringByAppendingString:[NSString stringWithFormat:@"/%@", Major_]]; |
de595d91 | 10168 | UI_ = CydiaURL(ui); |
57e8b225 | 10169 | |
df213583 | 10170 | PackageName = reinterpret_cast<CYString &(*)(Package *, SEL)>(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); |
677b8415 | 10171 | |
7376b55c JF |
10172 | /* Library Hacks {{{ */ |
10173 | class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); | |
10174 | ||
b1497b56 JF |
10175 | $WAKWindow = objc_getClass("WAKWindow"); |
10176 | if ($WAKWindow != NULL) | |
438d6708 JF |
10177 | if (Method method = class_getInstanceMethod($WAKWindow, @selector(screenSize))) |
10178 | method_setImplementation(method, (IMP) &$WAKWindow$screenSize); | |
10179 | ||
30c5be06 JF |
10180 | $NSURLConnection = objc_getClass("NSURLConnection"); |
10181 | Method NSURLConnection$init$(class_getInstanceMethod($NSURLConnection, @selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:))); | |
10182 | if (NSURLConnection$init$ != NULL) { | |
10183 | _NSURLConnection$init$ = reinterpret_cast<id (*)(NSURLConnection *, SEL, NSURLRequest *, id, BOOL, int64_t, BOOL, NSDictionary *)>(method_getImplementation(NSURLConnection$init$)); | |
10184 | method_setImplementation(NSURLConnection$init$, reinterpret_cast<IMP>(&$NSURLConnection$init$)); | |
10185 | } | |
29cbf4e5 JF |
10186 | |
10187 | $NSUserDefaults = objc_getClass("NSUserDefaults"); | |
10188 | Method NSUserDefaults$objectForKey$(class_getInstanceMethod($NSUserDefaults, @selector(objectForKey:))); | |
10189 | if (NSUserDefaults$objectForKey$ != NULL) { | |
10190 | _NSUserDefaults$objectForKey$ = reinterpret_cast<id (*)(NSUserDefaults *, SEL, NSString *)>(method_getImplementation(NSUserDefaults$objectForKey$)); | |
10191 | method_setImplementation(NSUserDefaults$objectForKey$, reinterpret_cast<IMP>(&$NSUserDefaults$objectForKey$)); | |
10192 | } | |
7376b55c JF |
10193 | /* }}} */ |
10194 | /* Set Locale {{{ */ | |
f79a4512 | 10195 | Locale_ = CFLocaleCopyCurrent(); |
b1ce61ec | 10196 | Languages_ = [NSLocale preferredLanguages]; |
25fdc941 | 10197 | |
b1ce61ec | 10198 | //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); |
18876387 | 10199 | //NSLog(@"%@", [Languages_ description]); |
78430d06 | 10200 | |
b1ce61ec | 10201 | const char *lang; |
25fdc941 JF |
10202 | if (Locale_ != NULL) |
10203 | lang = [(NSString *) CFLocaleGetIdentifier(Locale_) UTF8String]; | |
3caee0a4 JF |
10204 | else if (Languages_ != nil && [Languages_ count] != 0) |
10205 | lang = [[Languages_ objectAtIndex:0] UTF8String]; | |
10206 | else | |
78430d06 | 10207 | // XXX: consider just setting to C and then falling through? |
b1ce61ec | 10208 | lang = NULL; |
3caee0a4 JF |
10209 | |
10210 | if (lang != NULL) { | |
b4fd1197 | 10211 | RegEx pattern("([a-z][a-z])(?:-[A-Za-z]*)?(_[A-Z][A-Z])?"); |
3caee0a4 | 10212 | lang = !pattern(lang) ? NULL : [pattern->*@"%1$@%2$@" UTF8String]; |
78430d06 JF |
10213 | } |
10214 | ||
b1ce61ec | 10215 | NSLog(@"Setting Language: %s", lang); |
3caee0a4 JF |
10216 | |
10217 | if (lang != NULL) { | |
10218 | setenv("LANG", lang, true); | |
10219 | std::setlocale(LC_ALL, lang); | |
10220 | } | |
7376b55c | 10221 | /* }}} */ |
aeeb755b | 10222 | /* Index Collation {{{ */ |
42e25bc5 | 10223 | if (Class $UILocalizedIndexedCollation = objc_getClass("UILocalizedIndexedCollation")) { @try { |
aeeb755b JF |
10224 | NSBundle *bundle([NSBundle bundleForClass:$UILocalizedIndexedCollation]); |
10225 | NSString *path([bundle pathForResource:@"UITableViewLocalizedSectionIndex" ofType:@"plist"]); | |
10226 | //path = @"/System/Library/Frameworks/UIKit.framework/.lproj/UITableViewLocalizedSectionIndex.plist"; | |
10227 | NSDictionary *dictionary([NSDictionary dictionaryWithContentsOfFile:path]); | |
1c28763e | 10228 | _H<UILocalizedIndexedCollation> collation([[[$UILocalizedIndexedCollation alloc] initWithDictionary:dictionary] autorelease]); |
aeeb755b JF |
10229 | |
10230 | CollationLocale_ = MSHookIvar<NSLocale *>(collation, "_locale"); | |
10231 | ||
f93f4997 JF |
10232 | if (kCFCoreFoundationVersionNumber >= 800 && [[CollationLocale_ localeIdentifier] isEqualToString:@"zh@collation=stroke"]) { |
10233 | CollationThumbs_ = [NSArray arrayWithObjects:@"1",@"•",@"4",@"•",@"7",@"•",@"10",@"•",@"13",@"•",@"16",@"•",@"19",@"A",@"•",@"E",@"•",@"I",@"•",@"M",@"•",@"R",@"•",@"V",@"•",@"Z",@"#",nil]; | |
10234 | 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}) | |
10235 | CollationOffset_.push_back(offset); | |
10236 | 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]; | |
10237 | 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]; | |
10238 | } else { | |
10239 | ||
aeeb755b JF |
10240 | CollationThumbs_ = [collation sectionIndexTitles]; |
10241 | for (size_t index(0), end([CollationThumbs_ count]); index != end; ++index) | |
10242 | CollationOffset_.push_back([collation sectionForSectionIndexTitleAtIndex:index]); | |
10243 | ||
10244 | CollationTitles_ = [collation sectionTitles]; | |
10245 | CollationStarts_ = MSHookIvar<NSArray *>(collation, "_sectionStartStrings"); | |
10246 | ||
22fd24dd JF |
10247 | NSString *&transform(MSHookIvar<NSString *>(collation, "_transform")); |
10248 | if (&transform != NULL && transform != nil) { | |
10249 | /*if ([collation respondsToSelector:@selector(transformedCollationStringForString:)]) | |
10250 | CollationModify_ = [=](NSString *value) { return [collation transformedCollationStringForString:value]; };*/ | |
10251 | const UChar *uid(reinterpret_cast<const UChar *>([transform cStringUsingEncoding:NSUnicodeStringEncoding])); | |
10252 | UErrorCode code(U_ZERO_ERROR); | |
10253 | CollationTransl_ = utrans_openU(uid, -1, UTRANS_FORWARD, NULL, 0, NULL, &code); | |
10254 | if (!U_SUCCESS(code)) | |
10255 | NSLog(@"%s", u_errorName(code)); | |
10256 | } | |
f93f4997 JF |
10257 | |
10258 | } | |
42e25bc5 JF |
10259 | } @catch (NSException *e) { |
10260 | NSLog(@"%@", e); | |
10261 | goto hard; | |
10262 | } } else hard: { | |
aeeb755b JF |
10263 | CollationLocale_ = [[[NSLocale alloc] initWithLocaleIdentifier:@"en@collation=dictionary"] autorelease]; |
10264 | ||
10265 | 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]; | |
10266 | for (NSInteger offset(0); offset != 28; ++offset) | |
10267 | CollationOffset_.push_back(offset); | |
10268 | ||
10269 | 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]; | |
10270 | 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]; | |
10271 | } | |
10272 | /* }}} */ | |
7376b55c | 10273 | /* Parse Arguments {{{ */ |
de3b1ab4 JF |
10274 | bool substrate(false); |
10275 | ||
10276 | if (argc != 0) { | |
10277 | char **args(argv); | |
10278 | int arge(1); | |
10279 | ||
10280 | for (int argi(1); argi != argc; ++argi) | |
10281 | if (strcmp(argv[argi], "--") == 0) { | |
10282 | arge = argi; | |
10283 | argv[argi] = argv[0]; | |
10284 | argv += argi; | |
10285 | argc -= argi; | |
10286 | break; | |
10287 | } | |
10288 | ||
10289 | for (int argi(1); argi != arge; ++argi) | |
d791dce4 | 10290 | if (strcmp(args[argi], "--substrate") == 0) |
de3b1ab4 JF |
10291 | substrate = true; |
10292 | else | |
10293 | fprintf(stderr, "unknown argument: %s\n", args[argi]); | |
10294 | } | |
7376b55c | 10295 | /* }}} */ |
d73cede2 | 10296 | |
7376b55c | 10297 | App_ = [[NSBundle mainBundle] bundlePath]; |
d791dce4 | 10298 | Advanced_ = YES; |
7376b55c | 10299 | |
b44af625 | 10300 | Cache_ = [[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia", @"/var/mobile"] retain]; |
d1c7f1fd | 10301 | |
b4d89997 JF |
10302 | /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); |
10303 | alloc_ = alloc->method_imp; | |
10304 | alloc->method_imp = (IMP) &Alloc_;*/ | |
10305 | ||
36bb2ca2 JF |
10306 | /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc)); |
10307 | dealloc_ = dealloc->method_imp; | |
10308 | dealloc->method_imp = (IMP) &Dealloc_;*/ | |
10309 | ||
7c80833f JF |
10310 | void *gestalt(dlopen("/usr/lib/libMobileGestalt.dylib", RTLD_GLOBAL | RTLD_LAZY)); |
10311 | $MGCopyAnswer = reinterpret_cast<CFStringRef (*)(CFStringRef)>(dlsym(gestalt, "MGCopyAnswer")); | |
10312 | ||
d791dce4 | 10313 | /* System Information {{{ */ |
3178d79b | 10314 | size_t size; |
c390d3ab JF |
10315 | |
10316 | int maxproc; | |
10317 | size = sizeof(maxproc); | |
10318 | if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) | |
10319 | perror("sysctlbyname(\"kern.maxproc\", ?)"); | |
10320 | else if (maxproc < 64) { | |
10321 | maxproc = 64; | |
10322 | if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) | |
10323 | perror("sysctlbyname(\"kern.maxproc\", #)"); | |
10324 | } | |
10325 | ||
bfc87a4d JF |
10326 | sysctlbyname("kern.osversion", NULL, &size, NULL, 0); |
10327 | char *osversion = new char[size]; | |
10328 | if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1) | |
10329 | perror("sysctlbyname(\"kern.osversion\", ?)"); | |
10330 | else | |
10331 | System_ = [NSString stringWithUTF8String:osversion]; | |
10332 | ||
3178d79b JF |
10333 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
10334 | char *machine = new char[size]; | |
c390d3ab JF |
10335 | if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1) |
10336 | perror("sysctlbyname(\"hw.machine\", ?)"); | |
10337 | else | |
10338 | Machine_ = machine; | |
3178d79b | 10339 | |
8dcc32d4 | 10340 | int64_t usermem(0); |
ce1901de JF |
10341 | size = sizeof(usermem); |
10342 | if (sysctlbyname("hw.usermem", &usermem, &size, NULL, 0) == -1) | |
10343 | usermem = 0; | |
10344 | ||
c31d7cdc JF |
10345 | SerialNumber_ = (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber"); |
10346 | ChipID_ = [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString]; | |
10347 | BBSNum_ = CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false); | |
59dbe296 | 10348 | |
7c80833f | 10349 | UniqueID_ = UniqueIdentifier(device); |
3178d79b | 10350 | |
3e9c9e85 JF |
10351 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) { |
10352 | Product_ = [info objectForKey:@"SafariProductVersion"]; | |
10353 | Safari_ = [info objectForKey:@"CFBundleVersion"]; | |
10354 | } | |
e967efd5 | 10355 | |
dd58e110 | 10356 | NSString *agent([NSString stringWithFormat:@"Cydia/%@ CyF/%.2f", Cydia_, kCFCoreFoundationVersionNumber]); |
e967efd5 | 10357 | |
b4fd1197 JF |
10358 | if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Safari_)) |
10359 | agent = [NSString stringWithFormat:@"Safari/%@ %@", match[1], agent]; | |
10360 | if (RegEx match = RegEx("([0-9]+[A-Z][0-9]+[a-z]?).*", System_)) | |
10361 | agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[1], agent]; | |
10362 | if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Product_)) | |
10363 | agent = [NSString stringWithFormat:@"Version/%@ %@", match[1], agent]; | |
e967efd5 JF |
10364 | |
10365 | UserAgent_ = agent; | |
d791dce4 | 10366 | /* }}} */ |
7376b55c | 10367 | /* Load Database {{{ */ |
4e479350 JF |
10368 | SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; |
10369 | ||
d6dad1b4 | 10370 | _trace(); |
4e479350 JF |
10371 | mkdir("/var/mobile/Library/Cydia", 0755); |
10372 | MetaFile_.Open("/var/mobile/Library/Cydia/metadata.cb0"); | |
f79a4512 | 10373 | _trace(); |
d6dad1b4 | 10374 | |
9adfb865 JF |
10375 | Values_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaValues"), CFSTR("com.saurik.Cydia"))); |
10376 | Sections_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaSections"), CFSTR("com.saurik.Cydia"))); | |
10377 | Sources_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaSources"), CFSTR("com.saurik.Cydia"))); | |
4e479350 | 10378 | Version_ = [(NSNumber *) CFPreferencesCopyAppValue(CFSTR("CydiaVersion"), CFSTR("com.saurik.Cydia")) autorelease]; |
ef055c6c | 10379 | |
4e479350 JF |
10380 | _trace(); |
10381 | NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]); | |
7b0ce2da | 10382 | |
4e479350 JF |
10383 | if (Values_ == nil) |
10384 | Values_ = [metadata objectForKey:@"Values"]; | |
10385 | if (Values_ == nil) | |
b3c8e69c | 10386 | Values_ = [[[NSMutableDictionary alloc] initWithCapacity:4] autorelease]; |
b3c8e69c | 10387 | |
4e479350 JF |
10388 | if (Sections_ == nil) |
10389 | Sections_ = [metadata objectForKey:@"Sections"]; | |
10390 | if (Sections_ == nil) | |
7b0ce2da | 10391 | Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; |
7b0ce2da | 10392 | |
4e479350 JF |
10393 | if (Sources_ == nil) |
10394 | Sources_ = [metadata objectForKey:@"Sources"]; | |
10395 | if (Sources_ == nil) | |
7b0ce2da | 10396 | Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; |
33e30380 | 10397 | |
4e479350 JF |
10398 | // XXX: this wrong, but in a way that doesn't matter :/ |
10399 | if (Version_ == nil) | |
10400 | Version_ = [metadata objectForKey:@"Version"]; | |
10401 | if (Version_ == nil) | |
33e30380 | 10402 | Version_ = [NSNumber numberWithUnsignedInt:0]; |
4e479350 JF |
10403 | |
10404 | if (NSDictionary *packages = [metadata objectForKey:@"Packages"]) { | |
10405 | bool fail(false); | |
10406 | CFDictionaryApplyFunction((CFDictionaryRef) packages, &PackageImport, &fail); | |
10407 | _trace(); | |
10408 | if (fail) | |
10409 | NSLog(@"unable to import package preferences... from 2010? oh well :/"); | |
33e30380 JF |
10410 | } |
10411 | ||
33e30380 | 10412 | if ([Version_ unsignedIntValue] == 0) { |
25c1dafb JF |
10413 | CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]); |
10414 | CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]); | |
10415 | CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]); | |
10416 | CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./"); | |
33e30380 JF |
10417 | |
10418 | Version_ = [NSNumber numberWithUnsignedInt:1]; | |
33e30380 | 10419 | |
6f87c61a JF |
10420 | if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:@ CacheState_]) { |
10421 | [cache removeObjectForKey:@"LastUpdate"]; | |
10422 | [cache writeToFile:@ CacheState_ atomically:YES]; | |
10423 | } | |
33e30380 | 10424 | } |
2595e4c3 JF |
10425 | |
10426 | _H<NSMutableArray> broken([NSMutableArray array]); | |
10427 | for (NSString *key in (id) Sources_) | |
d93e3f22 | 10428 | if ([key rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"# "]].location != NSNotFound || ![([[Sources_ objectForKey:key] objectForKey:@"URI"] ?: @"/") hasSuffix:@"/"]) |
2595e4c3 | 10429 | [broken addObject:key]; |
4e479350 | 10430 | if ([broken count] != 0) |
2595e4c3 JF |
10431 | for (NSString *key in (id) broken) |
10432 | [Sources_ removeObjectForKey:key]; | |
4e479350 | 10433 | broken = nil; |
94b0b3e5 | 10434 | |
4e479350 JF |
10435 | SaveConfig(nil); |
10436 | system("/usr/libexec/cydia/cydo /bin/rm -f /var/lib/cydia/metadata.plist"); | |
10437 | /* }}} */ | |
94b0b3e5 | 10438 | |
d791dce4 JF |
10439 | Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; |
10440 | ||
6c9fe9af | 10441 | if (kCFCoreFoundationVersionNumber > 1000) |
0c0a966b | 10442 | system("/usr/libexec/cydia/cydo /usr/libexec/cydia/setnsfpn /var/lib"); |
6c9fe9af | 10443 | |
01d93940 JF |
10444 | int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); |
10445 | ||
ad502f71 | 10446 | if (access("/User", F_OK) != 0 || version != 6) { |
d6dad1b4 | 10447 | _trace(); |
0c0a966b | 10448 | system("/usr/libexec/cydia/cydo /usr/libexec/cydia/firmware.sh"); |
d6dad1b4 JF |
10449 | _trace(); |
10450 | } | |
9e98e020 | 10451 | |
7376b55c | 10452 | if (access("/tmp/cydia.chk", F_OK) == 0) { |
b44af625 | 10453 | if (unlink([Cache("pkgcache.bin") UTF8String]) == -1) |
7376b55c | 10454 | _assert(errno == ENOENT); |
b44af625 | 10455 | if (unlink([Cache("srcpkgcache.bin") UTF8String]) == -1) |
7376b55c JF |
10456 | _assert(errno == ENOENT); |
10457 | } | |
10458 | ||
19cd1e0d JF |
10459 | system("/usr/libexec/cydia/cydo /bin/ln -sf /var/mobile/Library/Caches/com.saurik.Cydia/sources.list /etc/apt/sources.list.d/cydia.list"); |
10460 | ||
59dbe296 | 10461 | /* APT Initialization {{{ */ |
b1ce61ec JF |
10462 | _assert(pkgInitConfig(*_config)); |
10463 | _assert(pkgInitSystem(*_config, _system)); | |
10464 | ||
10465 | if (lang != NULL) | |
10466 | _config->Set("APT::Acquire::Translation", lang); | |
cb94ff21 JF |
10467 | |
10468 | // XXX: this timeout might be important :( | |
10469 | //_config->Set("Acquire::http::Timeout", 15); | |
10470 | ||
ce1901de | 10471 | _config->Set("Acquire::http::MaxParallel", usermem >= 384 * 1024 * 1024 ? 16 : 3); |
b44af625 JF |
10472 | |
10473 | mkdir([Cache_ UTF8String], 0755); | |
10474 | mkdir([Cache("archives") UTF8String], 0755); | |
10475 | mkdir([Cache("archives/partial") UTF8String], 0755); | |
10476 | _config->Set("Dir::Cache", [Cache_ UTF8String]); | |
10477 | ||
49cc457a JF |
10478 | symlink("/var/lib/apt/extended_states", [Cache("extended_states") UTF8String]); |
10479 | _config->Set("Dir::State", [Cache_ UTF8String]); | |
10480 | ||
b44af625 JF |
10481 | mkdir([Cache("lists") UTF8String], 0755); |
10482 | mkdir([Cache("lists/partial") UTF8String], 0755); | |
10483 | mkdir([Cache("periodic") UTF8String], 0755); | |
e6446ca0 | 10484 | _config->Set("Dir::State::Lists", [Cache("lists") UTF8String]); |
232b396b JF |
10485 | |
10486 | std::string logs("/var/mobile/Library/Logs/Cydia"); | |
10487 | mkdir(logs.c_str(), 0755); | |
10488 | _config->Set("Dir::Log::Terminal", logs + "/apt.log"); | |
10489 | ||
10490 | _config->Set("Dir::Bin::dpkg", "/usr/libexec/cydia/cydo"); | |
59dbe296 | 10491 | /* }}} */ |
7376b55c | 10492 | /* Color Choices {{{ */ |
36bb2ca2 JF |
10493 | space_ = CGColorSpaceCreateDeviceRGB(); |
10494 | ||
f641a0e5 | 10495 | Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0); |
77fcccaf | 10496 | Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0); |
36bb2ca2 | 10497 | Black_.Set(space_, 0.0, 0.0, 0.0, 1.0); |
86a333c6 | 10498 | Folder_.Set(space_, 0x8e/255.f, 0x8e/255.f, 0x93/255.f, 1.0); |
baf80942 | 10499 | Off_.Set(space_, 0.9, 0.9, 0.9, 1.0); |
36bb2ca2 | 10500 | White_.Set(space_, 1.0, 1.0, 1.0, 1.0); |
7b0ce2da | 10501 | Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0); |
3bd1c2a2 JF |
10502 | Green_.Set(space_, 0.0, 0.5, 0.0, 1.0); |
10503 | Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0); | |
10504 | Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0); | |
59dbe296 | 10505 | |
dc63e78f JF |
10506 | InstallingColor_ = [UIColor colorWithRed:0.88f green:1.00f blue:0.88f alpha:1.00f]; |
10507 | RemovingColor_ = [UIColor colorWithRed:1.00f green:0.88f blue:0.88f alpha:1.00f]; | |
7376b55c | 10508 | /* }}}*/ |
7376b55c | 10509 | /* UIKit Configuration {{{ */ |
600d005d JF |
10510 | // XXX: I have a feeling this was important |
10511 | //UIKeyboardDisableAutomaticAppearance(); | |
7376b55c | 10512 | /* }}} */ |
87f46a96 | 10513 | |
8a3b565c | 10514 | $SBSSetInterceptsMenuButtonForever = reinterpret_cast<void (*)(bool)>(dlsym(RTLD_DEFAULT, "SBSSetInterceptsMenuButtonForever")); |
c7e78d5f | 10515 | $SBSCopyIconImagePNGDataForDisplayIdentifier = reinterpret_cast<NSData *(*)(NSString *)>(dlsym(RTLD_DEFAULT, "SBSCopyIconImagePNGDataForDisplayIdentifier")); |
8a3b565c | 10516 | |
b37b0a4a JF |
10517 | const char *symbol(kCFCoreFoundationVersionNumber >= 800 ? "MGGetBoolAnswer" : "GSSystemHasCapability"); |
10518 | BOOL (*GSSystemHasCapability)(CFStringRef) = reinterpret_cast<BOOL (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, symbol)); | |
c73d524b JF |
10519 | bool fast = GSSystemHasCapability != NULL && GSSystemHasCapability(CFSTR("armv7")); |
10520 | ||
c73d524b | 10521 | PulseInterval_ = fast ? 50000 : 500000; |
8731fdb0 | 10522 | |
670a0494 | 10523 | Colon_ = UCLocalize("COLON_DELIMITED"); |
72fb3616 | 10524 | Elision_ = UCLocalize("ELISION"); |
670a0494 JF |
10525 | Error_ = UCLocalize("ERROR"); |
10526 | Warning_ = UCLocalize("WARNING"); | |
10527 | ||
d6dad1b4 | 10528 | _trace(); |
77df4f82 | 10529 | int value(UIApplicationMain(argc, argv, @"Cydia", @"Cydia")); |
36bb2ca2 JF |
10530 | |
10531 | CGColorSpaceRelease(space_); | |
199d0ba5 | 10532 | CFRelease(Locale_); |
36bb2ca2 | 10533 | |
d13edf44 | 10534 | [pool release]; |
36bb2ca2 | 10535 | return value; |
6d166849 | 10536 | } |