]>
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 JF |
250 | static void (*$SBSSetInterceptsMenuButtonForever)(bool); |
251 | ||
7c80833f JF |
252 | static CFStringRef (*$MGCopyAnswer)(CFStringRef); |
253 | ||
254 | static NSString *UniqueIdentifier(UIDevice *device = nil) { | |
255 | if (kCFCoreFoundationVersionNumber < 800) // iOS 7.x | |
256 | return [device ?: [UIDevice currentDevice] uniqueIdentifier]; | |
257 | else | |
258 | return [(id)$MGCopyAnswer(CFSTR("UniqueDeviceID")) autorelease]; | |
259 | } | |
260 | ||
6cbfbe28 JF |
261 | static bool IsReachable(const char *name) { |
262 | SCNetworkReachabilityFlags flags; { | |
37f1fb03 | 263 | SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, name)); |
6cbfbe28 JF |
264 | SCNetworkReachabilityGetFlags(reachability, &flags); |
265 | CFRelease(reachability); | |
266 | } | |
267 | ||
268 | // XXX: this elaborate mess is what Apple is using to determine this? :( | |
269 | // XXX: do we care if the user has to intervene? maybe that's ok? | |
270 | return | |
271 | (flags & kSCNetworkReachabilityFlagsReachable) != 0 && ( | |
272 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || ( | |
273 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 || | |
274 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0 | |
275 | ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 || | |
276 | (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0 | |
277 | ) | |
278 | ; | |
279 | } | |
280 | ||
04fe1349 JF |
281 | static const NSUInteger UIViewAutoresizingFlexibleBoth(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); |
282 | ||
670a0494 | 283 | static _finline NSString *CydiaURL(NSString *path) { |
e3d2a2f5 JF |
284 | char page[26]; |
285 | page[0] = 'h'; page[1] = 't'; page[2] = 't'; page[3] = 'p'; page[4] = 's'; | |
286 | page[5] = ':'; page[6] = '/'; page[7] = '/'; page[8] = 'c'; page[9] = 'y'; | |
287 | page[10] = 'd'; page[11] = 'i'; page[12] = 'a'; page[13] = '.'; page[14] = 's'; | |
288 | page[15] = 'a'; page[16] = 'u'; page[17] = 'r'; page[18] = 'i'; page[19] = 'k'; | |
289 | page[20] = '.'; page[21] = 'c'; page[22] = 'o'; page[23] = 'm'; page[24] = '/'; | |
290 | page[25] = '\0'; | |
670a0494 JF |
291 | return [[NSString stringWithUTF8String:page] stringByAppendingString:path]; |
292 | } | |
293 | ||
d8b0f6e3 JF |
294 | static NSString *ShellEscape(NSString *value) { |
295 | return [NSString stringWithFormat:@"'%@'", [value stringByReplacingOccurrencesOfString:@"'" withString:@"'\\''"]]; | |
296 | } | |
297 | ||
ef494bd8 RP |
298 | static _finline void UpdateExternalStatus(uint64_t newStatus) { |
299 | int notify_token; | |
300 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
301 | notify_set_state(notify_token, newStatus); | |
302 | notify_cancel(notify_token); | |
303 | } | |
304 | notify_post("com.saurik.Cydia.status"); | |
305 | } | |
306 | ||
0b7516cf | 307 | static CGFloat CYStatusBarHeight() { |
57daa971 | 308 | CGSize size([[UIApplication sharedApplication] statusBarFrame].size); |
0b7516cf | 309 | return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ? size.height : size.width; |
57daa971 JF |
310 | } |
311 | ||
68f1828e | 312 | /* NSForcedOrderingSearch doesn't work on the iPhone */ |
808c6eb6 | 313 | static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch; |
677b8415 | 314 | static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch; |
aeeb755b | 315 | static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; |
1e7a90f5 | 316 | |
eef4ccaf JF |
317 | /* Insertion Sort {{{ */ |
318 | ||
df213583 JF |
319 | CFIndex SKBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
320 | const char *ptr = (const char *)list; | |
321 | while (0 < count) { | |
322 | CFIndex half = count / 2; | |
323 | const char *probe = ptr + elementSize * half; | |
324 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 325 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
df213583 JF |
326 | ptr = (cr < 0) ? ptr : probe + elementSize; |
327 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
328 | } | |
329 | return (ptr - (const char *)list) / elementSize; | |
330 | } | |
331 | ||
eef4ccaf JF |
332 | CFIndex CFBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
333 | const char *ptr = (const char *)list; | |
334 | while (0 < count) { | |
335 | CFIndex half = count / 2; | |
336 | const char *probe = ptr + elementSize * half; | |
337 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 338 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
eef4ccaf JF |
339 | ptr = (cr < 0) ? ptr : probe + elementSize; |
340 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
341 | } | |
342 | return (ptr - (const char *)list) / elementSize; | |
343 | } | |
344 | ||
345 | void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFComparatorFunction comparator, void *context) { | |
346 | if (range.length == 0) | |
347 | return; | |
348 | const void **values(new const void *[range.length]); | |
349 | CFArrayGetValues(array, range, values); | |
350 | ||
1539387a | 351 | #if HistogramInsertionSort > 0 |
df213583 JF |
352 | uint32_t total(0), *offsets(new uint32_t[range.length]); |
353 | #endif | |
354 | ||
eef4ccaf JF |
355 | for (CFIndex index(1); index != range.length; ++index) { |
356 | const void *value(values[index]); | |
df213583 JF |
357 | //CFIndex correct(SKBSearch_(&value, sizeof(const void *), values, index, comparator, context)); |
358 | CFIndex correct(index); | |
1539387a JF |
359 | while (comparator(value, values[correct - 1], context) == kCFCompareLessThan) { |
360 | #if HistogramInsertionSort > 1 | |
361 | NSLog(@"%@ < %@", value, values[correct - 1]); | |
362 | #endif | |
df213583 JF |
363 | if (--correct == 0) |
364 | break; | |
1539387a | 365 | } |
eef4ccaf | 366 | if (correct != index) { |
df213583 JF |
367 | size_t offset(index - correct); |
368 | #if HistogramInsertionSort | |
369 | total += offset; | |
370 | ++offsets[offset]; | |
371 | if (offset > 10) | |
372 | NSLog(@"Heavy Insertion Displacement: %u = %@", offset, value); | |
373 | #endif | |
374 | memmove(values + correct + 1, values + correct, sizeof(const void *) * offset); | |
eef4ccaf JF |
375 | values[correct] = value; |
376 | } | |
377 | } | |
378 | ||
379 | CFArrayReplaceValues(array, range, values, range.length); | |
380 | delete [] values; | |
df213583 | 381 | |
1539387a | 382 | #if HistogramInsertionSort > 0 |
df213583 JF |
383 | for (CFIndex index(0); index != range.length; ++index) |
384 | if (offsets[index] != 0) | |
385 | NSLog(@"Insertion Displacement [%u]: %u", index, offsets[index]); | |
386 | NSLog(@"Average Insertion Displacement: %f", double(total) / range.length); | |
387 | delete [] offsets; | |
388 | #endif | |
eef4ccaf JF |
389 | } |
390 | ||
807ae6d7 JF |
391 | /* }}} */ |
392 | ||
bf8476c8 JF |
393 | /* Apple Bug Fixes {{{ */ |
394 | @implementation UIWebDocumentView (Cydia) | |
395 | ||
396 | - (void) _setScrollerOffset:(CGPoint)offset { | |
397 | UIScroller *scroller([self _scroller]); | |
398 | ||
399 | CGSize size([scroller contentSize]); | |
400 | CGSize bounds([scroller bounds].size); | |
401 | ||
402 | CGPoint max; | |
403 | max.x = size.width - bounds.width; | |
404 | max.y = size.height - bounds.height; | |
405 | ||
406 | // wtf Apple?! | |
407 | if (max.x < 0) | |
408 | max.x = 0; | |
409 | if (max.y < 0) | |
410 | max.y = 0; | |
411 | ||
412 | offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; | |
413 | offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; | |
414 | ||
415 | [scroller setOffset:offset]; | |
416 | } | |
417 | ||
418 | @end | |
419 | /* }}} */ | |
420 | ||
680eb135 JF |
421 | NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { |
422 | size_t length([self length] - state->state); | |
423 | if (length <= 0) | |
424 | return 0; | |
425 | else if (length > count) | |
426 | length = count; | |
427 | for (size_t i(0); i != length; ++i) | |
428 | objects[i] = [self item:state->state++]; | |
429 | state->itemsPtr = objects; | |
430 | state->mutationsPtr = (unsigned long *) self; | |
431 | return length; | |
432 | } | |
433 | ||
bfc87a4d | 434 | /* Cydia NSString Additions {{{ */ |
2388b078 | 435 | @interface NSString (Cydia) |
a54b1c10 | 436 | - (NSComparisonResult) compareByPath:(NSString *)other; |
2fc76a2d | 437 | - (NSString *) stringByAddingPercentEscapesIncludingReserved; |
2388b078 JF |
438 | @end |
439 | ||
449ef9d5 JF |
440 | @implementation NSString (Cydia) |
441 | ||
a54b1c10 JF |
442 | - (NSComparisonResult) compareByPath:(NSString *)other { |
443 | NSString *prefix = [self commonPrefixWithString:other options:0]; | |
444 | size_t length = [prefix length]; | |
445 | ||
446 | NSRange lrange = NSMakeRange(length, [self length] - length); | |
447 | NSRange rrange = NSMakeRange(length, [other length] - length); | |
448 | ||
449 | lrange = [self rangeOfString:@"/" options:0 range:lrange]; | |
450 | rrange = [other rangeOfString:@"/" options:0 range:rrange]; | |
451 | ||
452 | NSComparisonResult value; | |
453 | ||
454 | if (lrange.location == NSNotFound && rrange.location == NSNotFound) | |
455 | value = NSOrderedSame; | |
456 | else if (lrange.location == NSNotFound) | |
457 | value = NSOrderedAscending; | |
458 | else if (rrange.location == NSNotFound) | |
459 | value = NSOrderedDescending; | |
460 | else | |
461 | value = NSOrderedSame; | |
462 | ||
463 | NSString *lpath = lrange.location == NSNotFound ? [self substringFromIndex:length] : | |
464 | [self substringWithRange:NSMakeRange(length, lrange.location - length)]; | |
465 | NSString *rpath = rrange.location == NSNotFound ? [other substringFromIndex:length] : | |
466 | [other substringWithRange:NSMakeRange(length, rrange.location - length)]; | |
467 | ||
468 | NSComparisonResult result = [lpath compare:rpath]; | |
469 | return result == NSOrderedSame ? value : result; | |
470 | } | |
471 | ||
2fc76a2d JF |
472 | - (NSString *) stringByAddingPercentEscapesIncludingReserved { |
473 | return [(id)CFURLCreateStringByAddingPercentEscapes( | |
bc11cf5b | 474 | kCFAllocatorDefault, |
2fc76a2d JF |
475 | (CFStringRef) self, |
476 | NULL, | |
477 | CFSTR(";/?:@&=+$,"), | |
478 | kCFStringEncodingUTF8 | |
479 | ) autorelease]; | |
480 | } | |
481 | ||
2388b078 | 482 | @end |
bfc87a4d | 483 | /* }}} */ |
2388b078 | 484 | |
bfc87a4d | 485 | /* C++ NSString Wrapper Cache {{{ */ |
8e8fca7f JF |
486 | static _finline CFStringRef CYStringCreate(const char *data, size_t size) { |
487 | return size == 0 ? NULL : | |
488 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingUTF8, NO, kCFAllocatorNull) ?: | |
489 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingISOLatin1, NO, kCFAllocatorNull); | |
490 | } | |
491 | ||
492 | static _finline CFStringRef CYStringCreate(const char *data) { | |
493 | return CYStringCreate(data, strlen(data)); | |
494 | } | |
495 | ||
f79a4512 JF |
496 | class CYString { |
497 | private: | |
498 | char *data_; | |
499 | size_t size_; | |
500 | CFStringRef cache_; | |
501 | ||
502 | _finline void clear_() { | |
18873623 | 503 | if (cache_ != NULL) { |
f79a4512 | 504 | CFRelease(cache_); |
18873623 JF |
505 | cache_ = NULL; |
506 | } | |
f79a4512 JF |
507 | } |
508 | ||
509 | public: | |
510 | _finline bool empty() const { | |
511 | return size_ == 0; | |
512 | } | |
513 | ||
514 | _finline size_t size() const { | |
515 | return size_; | |
516 | } | |
517 | ||
518 | _finline char *data() const { | |
519 | return data_; | |
520 | } | |
521 | ||
522 | _finline void clear() { | |
523 | size_ = 0; | |
524 | clear_(); | |
525 | } | |
526 | ||
527 | _finline CYString() : | |
528 | data_(0), | |
529 | size_(0), | |
18873623 | 530 | cache_(NULL) |
f79a4512 JF |
531 | { |
532 | } | |
533 | ||
534 | _finline ~CYString() { | |
535 | clear_(); | |
536 | } | |
537 | ||
538 | void operator =(const CYString &rhs) { | |
539 | data_ = rhs.data_; | |
540 | size_ = rhs.size_; | |
541 | ||
542 | if (rhs.cache_ == nil) | |
543 | cache_ = NULL; | |
544 | else | |
545 | cache_ = reinterpret_cast<CFStringRef>(CFRetain(rhs.cache_)); | |
546 | } | |
547 | ||
64edd9df JF |
548 | void copy(CYPool *pool) { |
549 | char *temp(pool->malloc<char>(size_ + 1)); | |
97814287 JF |
550 | memcpy(temp, data_, size_); |
551 | temp[size_] = '\0'; | |
552 | data_ = temp; | |
553 | } | |
554 | ||
64edd9df | 555 | void set(CYPool *pool, const char *data, size_t size) { |
f79a4512 JF |
556 | if (size == 0) |
557 | clear(); | |
558 | else { | |
559 | clear_(); | |
560 | ||
97814287 | 561 | data_ = const_cast<char *>(data); |
f79a4512 | 562 | size_ = size; |
97814287 JF |
563 | |
564 | if (pool != NULL) | |
565 | copy(pool); | |
f79a4512 JF |
566 | } |
567 | } | |
568 | ||
64edd9df | 569 | _finline void set(CYPool *pool, const char *data) { |
f79a4512 JF |
570 | set(pool, data, data == NULL ? 0 : strlen(data)); |
571 | } | |
572 | ||
64edd9df | 573 | _finline void set(CYPool *pool, const std::string &rhs) { |
f79a4512 JF |
574 | set(pool, rhs.data(), rhs.size()); |
575 | } | |
576 | ||
577 | bool operator ==(const CYString &rhs) const { | |
578 | return size_ == rhs.size_ && memcmp(data_, rhs.data_, size_) == 0; | |
579 | } | |
580 | ||
8e8fca7f JF |
581 | _finline operator CFStringRef() { |
582 | if (cache_ == NULL) | |
583 | cache_ = CYStringCreate(data_, size_); | |
584 | return cache_; | |
df213583 JF |
585 | } |
586 | ||
587 | _finline operator id() { | |
588 | return (NSString *) static_cast<CFStringRef>(*this); | |
f79a4512 | 589 | } |
4c0ed943 JF |
590 | |
591 | _finline operator const char *() { | |
592 | return reinterpret_cast<const char *>(data_); | |
593 | } | |
f79a4512 | 594 | }; |
bfc87a4d JF |
595 | /* }}} */ |
596 | /* C++ NSString Algorithm Adapters {{{ */ | |
f79a4512 JF |
597 | extern "C" { |
598 | CF_EXPORT CFHashCode CFStringHashNSString(CFStringRef str); | |
599 | } | |
600 | ||
601 | struct NSStringMapHash : | |
602 | std::unary_function<NSString *, size_t> | |
603 | { | |
604 | _finline size_t operator ()(NSString *value) const { | |
605 | return CFStringHashNSString((CFStringRef) value); | |
606 | } | |
607 | }; | |
608 | ||
609 | struct NSStringMapLess : | |
610 | std::binary_function<NSString *, NSString *, bool> | |
611 | { | |
612 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
613 | return [lhs compare:rhs] == NSOrderedAscending; | |
614 | } | |
615 | }; | |
616 | ||
617 | struct NSStringMapEqual : | |
618 | std::binary_function<NSString *, NSString *, bool> | |
619 | { | |
620 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
621 | return CFStringCompare((CFStringRef) lhs, (CFStringRef) rhs, 0) == kCFCompareEqualTo; | |
622 | //CFEqual((CFTypeRef) lhs, (CFTypeRef) rhs); | |
623 | //[lhs isEqualToString:rhs]; | |
624 | } | |
625 | }; | |
bfc87a4d | 626 | /* }}} */ |
f79a4512 | 627 | |
5f6bff8c | 628 | /* CoreGraphics Primitives {{{ */ |
02012733 | 629 | class CYColor { |
b4d89997 JF |
630 | private: |
631 | CGColorRef color_; | |
632 | ||
6a575b5e JF |
633 | static CGColorRef Create_(CGColorSpaceRef space, float red, float green, float blue, float alpha) { |
634 | CGFloat color[] = {red, green, blue, alpha}; | |
635 | return CGColorCreate(space, color); | |
636 | } | |
637 | ||
b4d89997 | 638 | public: |
02012733 | 639 | CYColor() : |
36bb2ca2 JF |
640 | color_(NULL) |
641 | { | |
642 | } | |
643 | ||
02012733 | 644 | CYColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) : |
6a575b5e | 645 | color_(Create_(space, red, green, blue, alpha)) |
36bb2ca2 JF |
646 | { |
647 | Set(space, red, green, blue, alpha); | |
648 | } | |
649 | ||
650 | void Clear() { | |
651 | if (color_ != NULL) | |
652 | CGColorRelease(color_); | |
b4d89997 JF |
653 | } |
654 | ||
02012733 | 655 | ~CYColor() { |
36bb2ca2 JF |
656 | Clear(); |
657 | } | |
658 | ||
659 | void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) { | |
660 | Clear(); | |
6a575b5e | 661 | color_ = Create_(space, red, green, blue, alpha); |
b4d89997 JF |
662 | } |
663 | ||
664 | operator CGColorRef() { | |
665 | return color_; | |
666 | } | |
667 | }; | |
b4d89997 JF |
668 | /* }}} */ |
669 | ||
36bb2ca2 | 670 | /* Random Global Variables {{{ */ |
c73d524b | 671 | static int PulseInterval_ = 500000; |
affeffc7 | 672 | |
57e8b225 JF |
673 | static const NSString *UI_; |
674 | ||
d791dce4 | 675 | static int Finish_; |
be860cc8 | 676 | static bool RestartSubstrate_; |
d791dce4 JF |
677 | static NSArray *Finishes_; |
678 | ||
affeffc7 | 679 | #define SpringBoard_ "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist" |
22f8bed9 | 680 | #define NotifyConfig_ "/etc/notify.conf" |
2a8d9add | 681 | |
dc63e78f JF |
682 | static bool Queuing_; |
683 | ||
02012733 JF |
684 | static CYColor Blue_; |
685 | static CYColor Blueish_; | |
686 | static CYColor Black_; | |
86a333c6 | 687 | static CYColor Folder_; |
02012733 JF |
688 | static CYColor Off_; |
689 | static CYColor White_; | |
690 | static CYColor Gray_; | |
691 | static CYColor Green_; | |
692 | static CYColor Purple_; | |
693 | static CYColor Purplish_; | |
3bd1c2a2 | 694 | |
dc63e78f JF |
695 | static UIColor *InstallingColor_; |
696 | static UIColor *RemovingColor_; | |
36bb2ca2 | 697 | |
7d2ac47f | 698 | static NSString *App_; |
d73cede2 | 699 | |
2388b078 | 700 | static BOOL Advanced_; |
a54b1c10 | 701 | static BOOL Ignored_; |
2388b078 | 702 | |
7b33d201 JF |
703 | static _H<UIFont> Font12_; |
704 | static _H<UIFont> Font12Bold_; | |
705 | static _H<UIFont> Font14_; | |
2cdc6e57 | 706 | static _H<UIFont> Font18_; |
7b33d201 JF |
707 | static _H<UIFont> Font18Bold_; |
708 | static _H<UIFont> Font22Bold_; | |
f641a0e5 | 709 | |
87f46a96 | 710 | static const char *Machine_ = NULL; |
e967efd5 | 711 | static _H<NSString> System_; |
56127854 JF |
712 | static NSString *SerialNumber_ = nil; |
713 | static NSString *ChipID_ = nil; | |
6ffdaae3 | 714 | static NSString *BBSNum_ = nil; |
7c80833f | 715 | static _H<NSString> UniqueID_; |
e967efd5 JF |
716 | static _H<NSString> UserAgent_; |
717 | static _H<NSString> Product_; | |
718 | static _H<NSString> Safari_; | |
2a8d9add | 719 | |
aeeb755b JF |
720 | static _H<NSLocale> CollationLocale_; |
721 | static _H<NSArray> CollationThumbs_; | |
722 | static std::vector<NSInteger> CollationOffset_; | |
723 | static _H<NSArray> CollationTitles_; | |
724 | static _H<NSArray> CollationStarts_; | |
22fd24dd JF |
725 | static UTransliterator *CollationTransl_; |
726 | //static Function<NSString *, NSString *> CollationModify_; | |
727 | ||
728 | typedef std::basic_string<UChar> ustring; | |
729 | static ustring CollationString_; | |
730 | ||
731 | #define CUC const ustring &str(*reinterpret_cast<const ustring *>(rep)) | |
732 | #define UC ustring &str(*reinterpret_cast<ustring *>(rep)) | |
733 | static struct UReplaceableCallbacks CollationUCalls_ = { | |
734 | .length = [](const UReplaceable *rep) -> int32_t { CUC; | |
735 | return str.size(); | |
736 | }, | |
737 | ||
738 | .charAt = [](const UReplaceable *rep, int32_t offset) -> UChar { CUC; | |
739 | //fprintf(stderr, "charAt(%d) : %d\n", offset, str.size()); | |
740 | if (offset >= str.size()) | |
741 | return 0xffff; | |
742 | return str[offset]; | |
743 | }, | |
744 | ||
745 | .char32At = [](const UReplaceable *rep, int32_t offset) -> UChar32 { CUC; | |
746 | //fprintf(stderr, "char32At(%d) : %d\n", offset, str.size()); | |
747 | if (offset >= str.size()) | |
748 | return 0xffff; | |
749 | UChar32 c; | |
750 | U16_GET(str.data(), 0, offset, str.size(), c); | |
751 | return c; | |
752 | }, | |
753 | ||
754 | .replace = [](UReplaceable *rep, int32_t start, int32_t limit, const UChar *text, int32_t length) -> void { UC; | |
755 | //fprintf(stderr, "replace(%d, %d, %d) : %d\n", start, limit, length, str.size()); | |
756 | str.replace(start, limit - start, text, length); | |
757 | }, | |
758 | ||
759 | .extract = [](UReplaceable *rep, int32_t start, int32_t limit, UChar *dst) -> void { UC; | |
760 | //fprintf(stderr, "extract(%d, %d) : %d\n", start, limit, str.size()); | |
761 | str.copy(dst, limit - start, start); | |
762 | }, | |
763 | ||
764 | .copy = [](UReplaceable *rep, int32_t start, int32_t limit, int32_t dest) -> void { UC; | |
765 | //fprintf(stderr, "copy(%d, %d, %d) : %d\n", start, limit, dest, str.size()); | |
766 | str.replace(dest, 0, str, start, limit - start); | |
767 | }, | |
768 | }; | |
aeeb755b | 769 | |
d791dce4 JF |
770 | static CFLocaleRef Locale_; |
771 | static NSArray *Languages_; | |
772 | static CGColorSpaceRef space_; | |
36bb2ca2 | 773 | |
6f87c61a | 774 | #define CacheState_ "/var/mobile/Library/Caches/com.saurik.Cydia/CacheState.plist" |
3d1e6f42 JF |
775 | #define SavedState_ "/var/mobile/Library/Caches/com.saurik.Cydia/SavedState.plist" |
776 | ||
46dbfd32 | 777 | static NSDictionary *SectionMap_; |
3d1e6f42 | 778 | static _H<NSDate> Backgrounded_; |
b3c8e69c | 779 | static _transient NSMutableDictionary *Values_; |
7b0ce2da | 780 | static _transient NSMutableDictionary *Sections_; |
25c1dafb | 781 | _H<NSMutableDictionary> Sources_; |
33e30380 | 782 | static _transient NSNumber *Version_; |
31bc18a7 | 783 | static time_t now_; |
8da60fb7 | 784 | |
f333f6c5 | 785 | bool IsWildcat_; |
f98962e5 | 786 | CGFloat ScreenScale_; |
c138614d | 787 | static NSString *Idiom_; |
407564b5 | 788 | static _H<NSString> Firmware_; |
fd825a2d | 789 | static NSString *Major_; |
5df7ecfb | 790 | |
7b33d201 JF |
791 | static _H<NSMutableDictionary> SessionData_; |
792 | static _H<NSObject> HostConfig_; | |
793 | static _H<NSMutableSet> BridgedHosts_; | |
2e1652a9 | 794 | static _H<NSMutableSet> InsecureHosts_; |
7b33d201 | 795 | static _H<NSMutableSet> PipelinedHosts_; |
5e845121 | 796 | static _H<NSMutableSet> CachedURLs_; |
389133be | 797 | |
e4123ce0 JF |
798 | static NSString *kCydiaProgressEventTypeError = @"Error"; |
799 | static NSString *kCydiaProgressEventTypeInformation = @"Information"; | |
800 | static NSString *kCydiaProgressEventTypeStatus = @"Status"; | |
801 | static NSString *kCydiaProgressEventTypeWarning = @"Warning"; | |
36bb2ca2 | 802 | /* }}} */ |
d791dce4 | 803 | |
36bb2ca2 JF |
804 | /* Display Helpers {{{ */ |
805 | inline float Interpolate(float begin, float end, float fraction) { | |
806 | return (end - begin) * fraction + begin; | |
807 | } | |
2367a917 | 808 | |
8323c1b9 JF |
809 | static inline double Retina(double value) { |
810 | value *= ScreenScale_; | |
811 | value = round(value); | |
812 | value /= ScreenScale_; | |
813 | return value; | |
814 | } | |
815 | ||
816 | static inline CGRect Retina(CGRect value) { | |
817 | value.origin.x *= ScreenScale_; | |
818 | value.origin.y *= ScreenScale_; | |
819 | value.size.width *= ScreenScale_; | |
820 | value.size.height *= ScreenScale_; | |
821 | value = CGRectIntegral(value); | |
822 | value.origin.x /= ScreenScale_; | |
823 | value.origin.y /= ScreenScale_; | |
824 | value.size.width /= ScreenScale_; | |
825 | value.size.height /= ScreenScale_; | |
826 | return value; | |
827 | } | |
828 | ||
1c1dfc2d | 829 | static _finline const char *StripVersion_(const char *version) { |
6a155117 | 830 | const char *colon(strchr(version, ':')); |
673ad3c3 | 831 | return colon == NULL ? version : colon + 1; |
6a155117 JF |
832 | } |
833 | ||
f79a4512 | 834 | NSString *LocalizeSection(NSString *section) { |
b4fd1197 | 835 | static RegEx title_r("(.*?) \\((.*)\\)"); |
9fcbca29 JF |
836 | if (title_r(section)) { |
837 | NSString *parent(title_r[1]); | |
838 | NSString *child(title_r[2]); | |
839 | ||
43f3d7f6 | 840 | return [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), |
9fcbca29 JF |
841 | LocalizeSection(parent), |
842 | LocalizeSection(child) | |
b1ce61ec | 843 | ]; |
9fcbca29 | 844 | } |
b1ce61ec JF |
845 | |
846 | return [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
f79a4512 JF |
847 | } |
848 | ||
4cf4165e JF |
849 | NSString *Simplify(NSString *title) { |
850 | const char *data = [title UTF8String]; | |
393a84a1 | 851 | size_t size = [title lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
4cf4165e | 852 | |
b4fd1197 | 853 | static RegEx square_r("\\[(.*)\\]"); |
7b0ce2da JF |
854 | if (square_r(data, size)) |
855 | return Simplify(square_r[1]); | |
856 | ||
b4fd1197 | 857 | static RegEx paren_r("\\((.*)\\)"); |
7b0ce2da JF |
858 | if (paren_r(data, size)) |
859 | return Simplify(paren_r[1]); | |
860 | ||
b4fd1197 | 861 | static RegEx title_r("(.*?) \\((.*)\\)"); |
4cf4165e | 862 | if (title_r(data, size)) |
7b0ce2da JF |
863 | return Simplify(title_r[1]); |
864 | ||
865 | return title; | |
4cf4165e | 866 | } |
36bb2ca2 JF |
867 | /* }}} */ |
868 | ||
6d9712c4 | 869 | bool isSectionVisible(NSString *section) { |
45447dc3 | 870 | NSDictionary *metadata([Sections_ objectForKey:(section ?: @"")]); |
677b8415 | 871 | NSNumber *hidden(metadata == nil ? nil : [metadata objectForKey:@"Hidden"]); |
6d9712c4 JF |
872 | return hidden == nil || ![hidden boolValue]; |
873 | } | |
874 | ||
c31d7cdc | 875 | static NSObject *CYIOGetValue(const char *path, NSString *property) { |
947a8eef JF |
876 | io_registry_entry_t entry(IORegistryEntryFromPath(kIOMasterPortDefault, path)); |
877 | if (entry == MACH_PORT_NULL) | |
878 | return nil; | |
879 | ||
880 | CFTypeRef value(IORegistryEntryCreateCFProperty(entry, (CFStringRef) property, kCFAllocatorDefault, 0)); | |
881 | IOObjectRelease(entry); | |
882 | ||
883 | if (value == NULL) | |
884 | return nil; | |
885 | return [(id) value autorelease]; | |
886 | } | |
887 | ||
c31d7cdc | 888 | static NSString *CYHex(NSData *data, bool reverse = false) { |
947a8eef JF |
889 | if (data == nil) |
890 | return nil; | |
891 | ||
892 | size_t length([data length]); | |
893 | uint8_t bytes[length]; | |
894 | [data getBytes:bytes]; | |
895 | ||
896 | char string[length * 2 + 1]; | |
897 | for (size_t i(0); i != length; ++i) | |
be45a862 | 898 | sprintf(string + i * 2, "%.2x", bytes[reverse ? length - i - 1 : i]); |
947a8eef JF |
899 | |
900 | return [NSString stringWithUTF8String:string]; | |
901 | } | |
902 | ||
8a2d167a JF |
903 | static NSString *VerifySource(NSString *href) { |
904 | static RegEx href_r("(http(s?)://|file:///)[^# ]*"); | |
905 | if (!href_r(href)) { | |
906 | [[[[UIAlertView alloc] | |
907 | initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("INVALID_URL")] | |
908 | message:UCLocalize("INVALID_URL_EX") | |
909 | delegate:nil | |
910 | cancelButtonTitle:UCLocalize("OK") | |
911 | otherButtonTitles:nil | |
912 | ] autorelease] show]; | |
913 | ||
914 | return nil; | |
915 | } | |
916 | ||
917 | if (![href hasSuffix:@"/"]) | |
918 | href = [href stringByAppendingString:@"/"]; | |
919 | return href; | |
920 | } | |
921 | ||
9cb0bff2 GP |
922 | @class Cydia; |
923 | ||
d36e83a3 | 924 | /* Delegate Prototypes {{{ */ |
36bb2ca2 JF |
925 | @class Package; |
926 | @class Source; | |
6915b806 | 927 | @class CydiaProgressEvent; |
36bb2ca2 | 928 | |
6915b806 | 929 | @protocol DatabaseDelegate |
5a09ae08 | 930 | - (void) repairWithSelector:(SEL)selector; |
8b29f8e6 | 931 | - (void) setConfigurationData:(NSString *)data; |
6915b806 | 932 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task; |
8b29f8e6 JF |
933 | @end |
934 | ||
f6e13561 | 935 | @class CYPackageController; |
f79a4512 | 936 | |
21ac0ce2 JF |
937 | @protocol SourceDelegate |
938 | - (void) setFetch:(NSNumber *)fetch; | |
939 | @end | |
940 | ||
941 | @protocol FetchDelegate | |
942 | - (bool) isSourceCancelled; | |
943 | - (void) startSourceFetch:(NSString *)uri; | |
944 | - (void) stopSourceFetch:(NSString *)uri; | |
945 | @end | |
946 | ||
36bb2ca2 | 947 | @protocol CydiaDelegate |
2925cbba | 948 | - (void) returnToCydia; |
9dd3045d | 949 | - (void) saveState; |
54043703 JF |
950 | - (void) retainNetworkActivityIndicator; |
951 | - (void) releaseNetworkActivityIndicator; | |
dc63e78f | 952 | - (void) clearPackage:(Package *)package; |
36bb2ca2 | 953 | - (void) installPackage:(Package *)package; |
77801ff1 | 954 | - (void) installPackages:(NSArray *)packages; |
36bb2ca2 | 955 | - (void) removePackage:(Package *)package; |
c21004b9 JF |
956 | - (void) beginUpdate; |
957 | - (BOOL) updating; | |
e67ebdad | 958 | - (bool) requestUpdate; |
36bb2ca2 | 959 | - (void) distUpgrade; |
fed0d010 | 960 | - (void) loadData; |
6d9712c4 | 961 | - (void) updateData; |
392ff7e4 | 962 | - (void) _saveConfig; |
7b0ce2da | 963 | - (void) syncData; |
33e30380 | 964 | - (void) addSource:(NSDictionary *)source; |
8a2d167a | 965 | - (BOOL) addTrivialSource:(NSString *)href; |
7b0ce2da | 966 | - (UIProgressHUD *) addProgressHUD; |
d061f4ba | 967 | - (void) removeProgressHUD:(UIProgressHUD *)hud; |
9daa7f25 | 968 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item; |
4ba8f30a | 969 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 970 | @end |
d36e83a3 | 971 | /* }}} */ |
b4d89997 | 972 | |
21ac0ce2 JF |
973 | /* CancelStatus {{{ */ |
974 | class CancelStatus : | |
dc5812ec JF |
975 | public pkgAcquireStatus |
976 | { | |
977 | private: | |
fc470c15 | 978 | bool cancelled_; |
dc5812ec JF |
979 | |
980 | public: | |
21ac0ce2 | 981 | CancelStatus() : |
fc470c15 | 982 | cancelled_(false) |
dc5812ec JF |
983 | { |
984 | } | |
985 | ||
dc5812ec JF |
986 | virtual bool MediaChange(std::string media, std::string drive) { |
987 | return false; | |
988 | } | |
989 | ||
10387810 JF |
990 | virtual void IMSHit(pkgAcquire::ItemDesc &desc) { |
991 | Done(desc); | |
dc5812ec JF |
992 | } |
993 | ||
21ac0ce2 JF |
994 | virtual bool Pulse_(pkgAcquire *Owner) = 0; |
995 | ||
996 | virtual bool Pulse(pkgAcquire *Owner) { | |
997 | if (pkgAcquireStatus::Pulse(Owner) && Pulse_(Owner)) | |
998 | return true; | |
999 | else { | |
1000 | cancelled_ = true; | |
1001 | return false; | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | _finline bool WasCancelled() const { | |
1006 | return cancelled_; | |
1007 | } | |
1008 | }; | |
1009 | /* }}} */ | |
1010 | /* DelegateStatus {{{ */ | |
1011 | class CydiaStatus : | |
1012 | public CancelStatus | |
1013 | { | |
1014 | private: | |
1015 | _transient NSObject<ProgressDelegate> *delegate_; | |
1016 | ||
1017 | public: | |
1018 | CydiaStatus() : | |
1019 | delegate_(nil) | |
1020 | { | |
1021 | } | |
1022 | ||
1023 | void setDelegate(NSObject<ProgressDelegate> *delegate) { | |
1024 | delegate_ = delegate; | |
1025 | } | |
1026 | ||
10387810 JF |
1027 | virtual void Fetch(pkgAcquire::ItemDesc &desc) { |
1028 | NSString *name([NSString stringWithUTF8String:desc.ShortDesc.c_str()]); | |
1029 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), name] ofType:kCydiaProgressEventTypeStatus forItemDesc:desc]); | |
6915b806 | 1030 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1031 | } |
1032 | ||
10387810 JF |
1033 | virtual void Done(pkgAcquire::ItemDesc &desc) { |
1034 | NSString *name([NSString stringWithUTF8String:desc.ShortDesc.c_str()]); | |
1035 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:Colon_, UCLocalize("DONE"), name] ofType:kCydiaProgressEventTypeStatus forItemDesc:desc]); | |
d35bcbbf | 1036 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1037 | } |
1038 | ||
10387810 | 1039 | virtual void Fail(pkgAcquire::ItemDesc &desc) { |
1d80f6b9 | 1040 | if ( |
10387810 JF |
1041 | desc.Owner->Status == pkgAcquire::Item::StatIdle || |
1042 | desc.Owner->Status == pkgAcquire::Item::StatDone | |
1d80f6b9 JF |
1043 | ) |
1044 | return; | |
1045 | ||
10387810 | 1046 | std::string &error(desc.Owner->ErrorText); |
affeffc7 JF |
1047 | if (error.empty()) |
1048 | return; | |
1049 | ||
10387810 | 1050 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError forItemDesc:desc]); |
6915b806 | 1051 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
1052 | } |
1053 | ||
21ac0ce2 | 1054 | virtual bool Pulse_(pkgAcquire *Owner) { |
bcbac8f7 | 1055 | double percent( |
2367a917 JF |
1056 | double(CurrentBytes + CurrentItems) / |
1057 | double(TotalBytes + TotalItems) | |
1058 | ); | |
1059 | ||
bcbac8f7 JF |
1060 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:[NSDictionary dictionaryWithObjectsAndKeys: |
1061 | [NSNumber numberWithDouble:percent], @"Percent", | |
1062 | ||
1063 | [NSNumber numberWithDouble:CurrentBytes], @"Current", | |
1064 | [NSNumber numberWithDouble:TotalBytes], @"Total", | |
1065 | [NSNumber numberWithDouble:CurrentCPS], @"Speed", | |
1066 | nil] waitUntilDone:YES]; | |
1067 | ||
21ac0ce2 | 1068 | return ![delegate_ isProgressCancelled]; |
dc5812ec JF |
1069 | } |
1070 | ||
1071 | virtual void Start() { | |
0210c2b5 | 1072 | pkgAcquireStatus::Start(); |
aaae308d | 1073 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:YES]; |
dc5812ec JF |
1074 | } |
1075 | ||
1076 | virtual void Stop() { | |
0210c2b5 | 1077 | pkgAcquireStatus::Stop(); |
aaae308d | 1078 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:YES]; |
bcbac8f7 | 1079 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:nil waitUntilDone:YES]; |
dc5812ec JF |
1080 | } |
1081 | }; | |
1082 | /* }}} */ | |
36bb2ca2 | 1083 | /* Database Interface {{{ */ |
68d927e2 JF |
1084 | typedef std::map< unsigned long, _H<Source> > SourceMap; |
1085 | ||
36bb2ca2 | 1086 | @interface Database : NSObject { |
f79a4512 | 1087 | NSZone *zone_; |
64edd9df | 1088 | CYPool pool_; |
f79a4512 | 1089 | |
a1440b10 | 1090 | unsigned era_; |
f26c90b1 | 1091 | _H<NSDate> delock_; |
a1440b10 | 1092 | |
36bb2ca2 | 1093 | pkgCacheFile cache_; |
5a09ae08 | 1094 | pkgDepCache::Policy *policy_; |
36bb2ca2 JF |
1095 | pkgRecords *records_; |
1096 | pkgProblemResolver *resolver_; | |
1097 | pkgAcquire *fetcher_; | |
1098 | FileFd *lock_; | |
1099 | SPtr<pkgPackageManager> manager_; | |
1100 | pkgSourceList *list_; | |
5f6bff8c | 1101 | |
34f70f5d | 1102 | SourceMap sourceMap_; |
7b33d201 | 1103 | _H<NSMutableArray> sourceList_; |
34f70f5d | 1104 | |
077e9d90 | 1105 | CFMutableArrayRef packages_; |
b4d89997 | 1106 | |
6915b806 JF |
1107 | _transient NSObject<DatabaseDelegate> *delegate_; |
1108 | _transient NSObject<ProgressDelegate> *progress_; | |
1109 | ||
21ac0ce2 | 1110 | CydiaStatus status_; |
8b29f8e6 | 1111 | |
77fcccaf | 1112 | int cydiafd_; |
36bb2ca2 | 1113 | int statusfd_; |
8b29f8e6 | 1114 | FILE *input_; |
fe33a23e JF |
1115 | |
1116 | std::map<const char *, _H<NSString> > sections_; | |
dc5812ec JF |
1117 | } |
1118 | ||
770f2a8e | 1119 | + (Database *) sharedInstance; |
a1440b10 | 1120 | - (unsigned) era; |
770f2a8e | 1121 | |
77fcccaf | 1122 | - (void) _readCydia:(NSNumber *)fd; |
36bb2ca2 JF |
1123 | - (void) _readStatus:(NSNumber *)fd; |
1124 | - (void) _readOutput:(NSNumber *)fd; | |
2367a917 | 1125 | |
8b29f8e6 JF |
1126 | - (FILE *) input; |
1127 | ||
36bb2ca2 | 1128 | - (Package *) packageWithName:(NSString *)name; |
dc5812ec | 1129 | |
36bb2ca2 | 1130 | - (pkgCacheFile &) cache; |
5a09ae08 | 1131 | - (pkgDepCache::Policy *) policy; |
36bb2ca2 JF |
1132 | - (pkgRecords *) records; |
1133 | - (pkgProblemResolver *) resolver; | |
1134 | - (pkgAcquire &) fetcher; | |
a3328c28 | 1135 | - (pkgSourceList &) list; |
36bb2ca2 | 1136 | - (NSArray *) packages; |
7b0ce2da | 1137 | - (NSArray *) sources; |
d669236d | 1138 | - (Source *) sourceWithKey:(NSString *)key; |
4ba8f30a | 1139 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 1140 | |
5a09ae08 | 1141 | - (void) configure; |
670a0494 | 1142 | - (bool) prepare; |
36bb2ca2 | 1143 | - (void) perform; |
670a0494 | 1144 | - (bool) upgrade; |
36bb2ca2 JF |
1145 | - (void) update; |
1146 | ||
21ac0ce2 | 1147 | - (void) updateWithStatus:(CancelStatus &)status; |
36bb2ca2 | 1148 | |
6915b806 JF |
1149 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate; |
1150 | ||
1151 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate; | |
1152 | - (NSObject<ProgressDelegate> *) progressDelegate; | |
1153 | ||
7376b55c | 1154 | - (Source *) getSource:(pkgCache::PkgFileIterator)file; |
21ac0ce2 | 1155 | - (void) setFetch:(bool)fetch forURI:(const char *)uri; |
9ed626f1 | 1156 | - (void) resetFetch; |
fe33a23e JF |
1157 | |
1158 | - (NSString *) mappedSectionForPointer:(const char *)pointer; | |
1159 | ||
670a0494 JF |
1160 | @end |
1161 | /* }}} */ | |
21ac0ce2 JF |
1162 | /* SourceStatus {{{ */ |
1163 | class SourceStatus : | |
1164 | public CancelStatus | |
1165 | { | |
1166 | private: | |
1167 | _transient NSObject<FetchDelegate> *delegate_; | |
1168 | _transient Database *database_; | |
e6e180a4 | 1169 | std::set<std::string> fetches_; |
21ac0ce2 JF |
1170 | |
1171 | public: | |
1172 | SourceStatus(NSObject<FetchDelegate> *delegate, Database *database) : | |
1173 | delegate_(delegate), | |
1174 | database_(database) | |
1175 | { | |
1176 | } | |
1177 | ||
e6e180a4 JF |
1178 | void Set(bool fetch, const std::string &uri) { |
1179 | if (fetch) { | |
1180 | if (!fetches_.insert(uri).second) | |
1181 | return; | |
1182 | } else { | |
1183 | if (fetches_.erase(uri) == 0) | |
1184 | return; | |
1185 | } | |
1186 | ||
1187 | //printf("Set(%s, %s)\n", fetch ? "true" : "false", uri.c_str()); | |
1188 | [database_ setFetch:fetch forURI:uri.c_str()]; | |
1189 | } | |
1190 | ||
1191 | _finline void Set(bool fetch, pkgAcquire::Item *item) { | |
1192 | /*unsigned long ID(fetch ? 1 : 0); | |
1193 | if (item->ID == ID) | |
1194 | return; | |
1195 | item->ID = ID;*/ | |
1196 | Set(fetch, item->DescURI()); | |
1197 | } | |
1198 | ||
1199 | void Log(const char *tag, pkgAcquire::Item *item) { | |
1200 | //printf("%s(%s) S:%u Q:%u\n", tag, item->DescURI().c_str(), item->Status, item->QueueCounter); | |
21ac0ce2 JF |
1201 | } |
1202 | ||
1203 | virtual void Fetch(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1204 | Log("Fetch", desc.Owner); |
1205 | Set(true, desc.Owner); | |
21ac0ce2 JF |
1206 | } |
1207 | ||
1208 | virtual void Done(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1209 | Log("Done", desc.Owner); |
1210 | Set(false, desc.Owner); | |
21ac0ce2 JF |
1211 | } |
1212 | ||
1213 | virtual void Fail(pkgAcquire::ItemDesc &desc) { | |
e6e180a4 JF |
1214 | Log("Fail", desc.Owner); |
1215 | Set(false, desc.Owner); | |
21ac0ce2 JF |
1216 | } |
1217 | ||
1218 | virtual bool Pulse_(pkgAcquire *Owner) { | |
e6e180a4 JF |
1219 | std::set<std::string> fetches; |
1220 | for (pkgAcquire::ItemCIterator item(Owner->ItemsBegin()); item != Owner->ItemsEnd(); ++item) { | |
1221 | bool fetch; | |
1222 | if ((*item)->QueueCounter == 0) | |
1223 | fetch = false; | |
1224 | else switch ((*item)->Status) { | |
1225 | case pkgAcquire::Item::StatFetching: | |
1226 | fetches.insert((*item)->DescURI()); | |
1227 | fetch = true; | |
1228 | break; | |
1229 | ||
1230 | default: | |
1231 | fetch = false; | |
1232 | break; | |
1233 | } | |
1234 | ||
1235 | Log(fetch ? "Pulse<true>" : "Pulse<false>", *item); | |
1236 | Set(fetch, *item); | |
1237 | } | |
1238 | ||
1239 | std::vector<std::string> stops; | |
1240 | std::set_difference(fetches_.begin(), fetches_.end(), fetches.begin(), fetches.end(), std::back_insert_iterator<std::vector<std::string>>(stops)); | |
1241 | for (std::vector<std::string>::const_iterator stop(stops.begin()); stop != stops.end(); ++stop) { | |
1242 | //printf("Stop(%s)\n", stop->c_str()); | |
1243 | Set(false, *stop); | |
1244 | } | |
1245 | ||
21ac0ce2 JF |
1246 | return ![delegate_ isSourceCancelled]; |
1247 | } | |
9ed626f1 JF |
1248 | |
1249 | virtual void Stop() { | |
1250 | pkgAcquireStatus::Stop(); | |
1251 | [database_ resetFetch]; | |
1252 | } | |
21ac0ce2 JF |
1253 | }; |
1254 | /* }}} */ | |
6915b806 JF |
1255 | /* ProgressEvent Implementation {{{ */ |
1256 | @implementation CydiaProgressEvent | |
670a0494 | 1257 | |
6915b806 JF |
1258 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type { |
1259 | return [[[CydiaProgressEvent alloc] initWithMessage:message ofType:type] autorelease]; | |
670a0494 JF |
1260 | } |
1261 | ||
6915b806 JF |
1262 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forPackage:(NSString *)package { |
1263 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); | |
1264 | [event setPackage:package]; | |
1265 | return event; | |
670a0494 JF |
1266 | } |
1267 | ||
10387810 | 1268 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forItemDesc:(pkgAcquire::ItemDesc &)desc { |
c57867ea JF |
1269 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); |
1270 | ||
10387810 | 1271 | NSString *description([NSString stringWithUTF8String:desc.Description.c_str()]); |
c57867ea JF |
1272 | NSArray *fields([description componentsSeparatedByString:@" "]); |
1273 | [event setItem:fields]; | |
1274 | ||
1275 | if ([fields count] > 3) { | |
1276 | [event setPackage:[fields objectAtIndex:2]]; | |
1277 | [event setVersion:[fields objectAtIndex:3]]; | |
1278 | } | |
1279 | ||
10387810 | 1280 | [event setURL:[NSString stringWithUTF8String:desc.URI.c_str()]]; |
c57867ea JF |
1281 | |
1282 | return event; | |
1283 | } | |
1284 | ||
4ede7a3f JF |
1285 | + (NSArray *) _attributeKeys { |
1286 | return [NSArray arrayWithObjects: | |
c57867ea | 1287 | @"item", |
4ede7a3f JF |
1288 | @"message", |
1289 | @"package", | |
1290 | @"type", | |
c57867ea JF |
1291 | @"url", |
1292 | @"version", | |
4ede7a3f JF |
1293 | nil]; |
1294 | } | |
1295 | ||
1296 | - (NSArray *) attributeKeys { | |
1297 | return [[self class] _attributeKeys]; | |
1298 | } | |
1299 | ||
1300 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1301 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1302 | } | |
1303 | ||
6915b806 JF |
1304 | - (id) initWithMessage:(NSString *)message ofType:(NSString *)type { |
1305 | if ((self = [super init]) != nil) { | |
1306 | message_ = message; | |
1307 | type_ = type; | |
1308 | } return self; | |
670a0494 JF |
1309 | } |
1310 | ||
6915b806 JF |
1311 | - (NSString *) message { |
1312 | return message_; | |
1313 | } | |
6067f1b8 | 1314 | |
6915b806 JF |
1315 | - (NSString *) type { |
1316 | return type_; | |
1317 | } | |
1318 | ||
c57867ea JF |
1319 | - (NSArray *) item { |
1320 | return (id) item_ ?: [NSNull null]; | |
1321 | } | |
1322 | ||
1323 | - (void) setItem:(NSArray *)item { | |
1324 | item_ = item; | |
6915b806 JF |
1325 | } |
1326 | ||
c57867ea JF |
1327 | - (NSString *) package { |
1328 | return (id) package_ ?: [NSNull null]; | |
6915b806 JF |
1329 | } |
1330 | ||
1331 | - (void) setPackage:(NSString *)package { | |
1332 | package_ = package; | |
1333 | } | |
1334 | ||
c57867ea JF |
1335 | - (NSString *) url { |
1336 | return (id) url_ ?: [NSNull null]; | |
1337 | } | |
1338 | ||
1339 | - (void) setURL:(NSString *)url { | |
1340 | url_ = url; | |
1341 | } | |
1342 | ||
1343 | - (void) setVersion:(NSString *)version { | |
1344 | version_ = version; | |
1345 | } | |
1346 | ||
1347 | - (NSString *) version { | |
1348 | return (id) version_ ?: [NSNull null]; | |
1349 | } | |
1350 | ||
6915b806 JF |
1351 | - (NSString *) compound:(NSString *)value { |
1352 | if (value != nil) { | |
1353 | NSString *mode(nil); { | |
1354 | NSString *type([self type]); | |
389133be | 1355 | if ([type isEqualToString:kCydiaProgressEventTypeError]) |
6915b806 | 1356 | mode = UCLocalize("ERROR"); |
389133be | 1357 | else if ([type isEqualToString:kCydiaProgressEventTypeWarning]) |
6915b806 JF |
1358 | mode = UCLocalize("WARNING"); |
1359 | } | |
1360 | ||
1361 | if (mode != nil) | |
1362 | value = [NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), mode, value]; | |
1363 | } | |
1364 | ||
1365 | return value; | |
1366 | } | |
1367 | ||
1368 | - (NSString *) compoundMessage { | |
1369 | return [self compound:[self message]]; | |
1370 | } | |
1371 | ||
1372 | - (NSString *) compoundTitle { | |
1373 | NSString *title; | |
1374 | ||
83b78e5f | 1375 | if (package_ == nil) |
6915b806 | 1376 | title = nil; |
83b78e5f JF |
1377 | else if (Package *package = [[Database sharedInstance] packageWithName:package_]) |
1378 | title = [package name]; | |
1379 | else | |
1380 | title = package_; | |
6915b806 JF |
1381 | |
1382 | return [self compound:title]; | |
670a0494 JF |
1383 | } |
1384 | ||
dc5812ec | 1385 | @end |
36bb2ca2 | 1386 | /* }}} */ |
dc5812ec | 1387 | |
94b0b3e5 JF |
1388 | // Cytore Definitions {{{ |
1389 | struct PackageValue : | |
1390 | Cytore::Block | |
1391 | { | |
94b0b3e5 JF |
1392 | Cytore::Offset<PackageValue> next_; |
1393 | ||
1394 | uint32_t index_ : 23; | |
1395 | uint32_t subscribed_ : 1; | |
1396 | uint32_t : 8; | |
1397 | ||
1398 | int32_t first_; | |
1399 | int32_t last_; | |
1400 | ||
1401 | uint16_t vhash_; | |
1402 | uint16_t nhash_; | |
1403 | ||
1404 | char version_[8]; | |
1405 | char name_[]; | |
1406 | }; | |
1407 | ||
1408 | struct MetaValue : | |
1409 | Cytore::Block | |
1410 | { | |
9f357d11 | 1411 | uint32_t active_; |
94b0b3e5 JF |
1412 | Cytore::Offset<PackageValue> packages_[1 << 16]; |
1413 | }; | |
1414 | ||
1415 | static Cytore::File<MetaValue> MetaFile_; | |
1416 | // }}} | |
1417 | // Cytore Helper Functions {{{ | |
c65611b9 | 1418 | static PackageValue *PackageFind(const char *name, size_t length, bool *fail = NULL) { |
94b0b3e5 JF |
1419 | SplitHash nhash = { hashlittle(name, length) }; |
1420 | ||
1421 | PackageValue *metadata; | |
1422 | ||
1423 | Cytore::Offset<PackageValue> *offset(&MetaFile_->packages_[nhash.u16[0]]); | |
ac5f7cb3 | 1424 | for (;; offset = &metadata->next_) { if (offset->IsNull()) { |
94b0b3e5 JF |
1425 | *offset = MetaFile_.New<PackageValue>(length + 1); |
1426 | metadata = &MetaFile_.Get(*offset); | |
1427 | ||
c65611b9 JF |
1428 | if (metadata == NULL) { |
1429 | if (fail != NULL) | |
1430 | *fail = true; | |
1431 | ||
1432 | metadata = new PackageValue(); | |
1433 | memset(metadata, 0, sizeof(*metadata)); | |
1434 | } | |
1435 | ||
ac5f7cb3 JF |
1436 | memcpy(metadata->name_, name, length); |
1437 | metadata->name_[length] = '\0'; | |
94b0b3e5 JF |
1438 | metadata->nhash_ = nhash.u16[1]; |
1439 | } else { | |
1440 | metadata = &MetaFile_.Get(*offset); | |
ac5f7cb3 JF |
1441 | if (metadata->nhash_ != nhash.u16[1]) |
1442 | continue; | |
1443 | if (strncmp(metadata->name_, name, length) != 0) | |
1444 | continue; | |
1445 | if (metadata->name_[length] != '\0') | |
1446 | continue; | |
1447 | } break; } | |
94b0b3e5 | 1448 | |
94b0b3e5 JF |
1449 | return metadata; |
1450 | } | |
1451 | ||
1452 | static void PackageImport(const void *key, const void *value, void *context) { | |
c65611b9 JF |
1453 | bool &fail(*reinterpret_cast<bool *>(context)); |
1454 | ||
94b0b3e5 JF |
1455 | char buffer[1024]; |
1456 | if (!CFStringGetCString((CFStringRef) key, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { | |
1457 | NSLog(@"failed to import package %@", key); | |
1458 | return; | |
1459 | } | |
1460 | ||
c65611b9 | 1461 | PackageValue *metadata(PackageFind(buffer, strlen(buffer), &fail)); |
94b0b3e5 JF |
1462 | NSDictionary *package((NSDictionary *) value); |
1463 | ||
1464 | if (NSNumber *subscribed = [package objectForKey:@"IsSubscribed"]) | |
2f856365 | 1465 | if ([subscribed boolValue] && !metadata->subscribed_) |
94b0b3e5 JF |
1466 | metadata->subscribed_ = true; |
1467 | ||
1468 | if (NSDate *date = [package objectForKey:@"FirstSeen"]) { | |
1469 | time_t time([date timeIntervalSince1970]); | |
1470 | if (metadata->first_ > time || metadata->first_ == 0) | |
1471 | metadata->first_ = time; | |
1472 | } | |
1473 | ||
2f856365 JF |
1474 | NSDate *date([package objectForKey:@"LastSeen"]); |
1475 | NSString *version([package objectForKey:@"LastVersion"]); | |
5a933937 | 1476 | |
2f856365 | 1477 | if (date != nil && version != nil) { |
94b0b3e5 | 1478 | time_t time([date timeIntervalSince1970]); |
2f856365 | 1479 | if (metadata->last_ < time || metadata->last_ == 0) |
5a933937 JF |
1480 | if (CFStringGetCString((CFStringRef) version, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { |
1481 | size_t length(strlen(buffer)); | |
1482 | uint16_t vhash(hashlittle(buffer, length)); | |
94b0b3e5 | 1483 | |
5a933937 JF |
1484 | size_t capped(std::min<size_t>(8, length)); |
1485 | char *latest(buffer + length - capped); | |
1486 | ||
1487 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); | |
1488 | metadata->vhash_ = vhash; | |
2f856365 JF |
1489 | |
1490 | metadata->last_ = time; | |
5a933937 | 1491 | } |
2f856365 | 1492 | } |
94b0b3e5 JF |
1493 | } |
1494 | // }}} | |
1495 | ||
f26c90b1 JF |
1496 | static NSDate *GetStatusDate() { |
1497 | return [[[NSFileManager defaultManager] attributesOfItemAtPath:@"/var/lib/dpkg/status" error:NULL] fileModificationDate]; | |
1498 | } | |
1499 | ||
4e479350 JF |
1500 | static void SaveConfig(NSObject *lock) { |
1501 | @synchronized (lock) { | |
1502 | _trace(); | |
1503 | MetaFile_.Sync(); | |
1504 | _trace(); | |
1505 | } | |
1506 | ||
1507 | CFPreferencesSetMultiple((CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys: | |
1508 | Values_, @"CydiaValues", | |
1509 | Sections_, @"CydiaSections", | |
1510 | (id) Sources_, @"CydiaSources", | |
1511 | Version_, @"CydiaVersion", | |
1512 | nil], NULL, CFSTR("com.saurik.Cydia"), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost); | |
1513 | ||
1514 | if (!CFPreferencesAppSynchronize(CFSTR("com.saurik.Cydia"))) | |
1515 | NSLog(@"CFPreferencesAppSynchronize(com.saurik.Cydia) == false"); | |
1516 | ||
1517 | CydiaWriteSources(); | |
1518 | } | |
1519 | ||
36bb2ca2 JF |
1520 | /* Source Class {{{ */ |
1521 | @interface Source : NSObject { | |
aec64d46 JF |
1522 | unsigned era_; |
1523 | Database *database_; | |
1524 | metaIndex *index_; | |
1525 | ||
6204f56a | 1526 | CYString depiction_; |
f9f6d9e8 JF |
1527 | CYString description_; |
1528 | CYString label_; | |
1529 | CYString origin_; | |
1530 | CYString support_; | |
dc5812ec | 1531 | |
f9f6d9e8 JF |
1532 | CYString uri_; |
1533 | CYString distribution_; | |
1534 | CYString type_; | |
8252b666 | 1535 | CYString base_; |
f9f6d9e8 | 1536 | CYString version_; |
2367a917 | 1537 | |
c08c8943 JF |
1538 | _H<NSString> host_; |
1539 | _H<NSString> authority_; | |
f9f6d9e8 JF |
1540 | |
1541 | CYString defaultIcon_; | |
dc5812ec | 1542 | |
4cc9e99a | 1543 | _H<NSMutableDictionary> record_; |
36bb2ca2 | 1544 | BOOL trusted_; |
21ac0ce2 JF |
1545 | |
1546 | std::set<std::string> fetches_; | |
1547 | std::set<std::string> files_; | |
1548 | _transient NSObject<SourceDelegate> *delegate_; | |
dc5812ec JF |
1549 | } |
1550 | ||
64edd9df | 1551 | - (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool; |
dc5812ec | 1552 | |
90ba4f86 | 1553 | - (NSComparisonResult) compareByName:(Source *)source; |
7b0ce2da | 1554 | |
6204f56a | 1555 | - (NSString *) depictionForPackage:(NSString *)package; |
dc63e78f JF |
1556 | - (NSString *) supportForPackage:(NSString *)package; |
1557 | ||
0c28a403 | 1558 | - (metaIndex *) metaIndex; |
7b0ce2da | 1559 | - (NSDictionary *) record; |
36bb2ca2 | 1560 | - (BOOL) trusted; |
dc5812ec | 1561 | |
7bd76e97 | 1562 | - (NSString *) rooturi; |
36bb2ca2 JF |
1563 | - (NSString *) distribution; |
1564 | - (NSString *) type; | |
8252b666 | 1565 | |
7b0ce2da JF |
1566 | - (NSString *) key; |
1567 | - (NSString *) host; | |
36bb2ca2 | 1568 | |
7b0ce2da | 1569 | - (NSString *) name; |
8d262908 | 1570 | - (NSString *) shortDescription; |
36bb2ca2 JF |
1571 | - (NSString *) label; |
1572 | - (NSString *) origin; | |
1573 | - (NSString *) version; | |
dc5812ec | 1574 | |
36bb2ca2 | 1575 | - (NSString *) defaultIcon; |
7bd76e97 | 1576 | - (NSURL *) iconURL; |
7b0ce2da | 1577 | |
21ac0ce2 | 1578 | - (void) setFetch:(bool)fetch forURI:(const char *)uri; |
9ed626f1 | 1579 | - (void) resetFetch; |
21ac0ce2 | 1580 | |
dc5812ec | 1581 | @end |
dc5812ec | 1582 | |
36bb2ca2 | 1583 | @implementation Source |
dc5812ec | 1584 | |
33e30380 JF |
1585 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
1586 | if (false); | |
1587 | else if (selector == @selector(addSection:)) | |
1588 | return @"addSection"; | |
aec64d46 JF |
1589 | else if (selector == @selector(getField:)) |
1590 | return @"getField"; | |
33e30380 JF |
1591 | else if (selector == @selector(removeSection:)) |
1592 | return @"removeSection"; | |
1593 | else if (selector == @selector(remove)) | |
1594 | return @"remove"; | |
1595 | else | |
1596 | return nil; | |
1597 | } | |
1598 | ||
1599 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
1600 | return [self webScriptNameForSelector:selector] == nil; | |
1601 | } | |
1602 | ||
6f1a15d9 | 1603 | + (NSArray *) _attributeKeys { |
e58ff941 | 1604 | return [NSArray arrayWithObjects: |
7bd76e97 | 1605 | @"baseuri", |
e58ff941 JF |
1606 | @"distribution", |
1607 | @"host", | |
1608 | @"key", | |
7bd76e97 | 1609 | @"iconuri", |
e58ff941 JF |
1610 | @"label", |
1611 | @"name", | |
1612 | @"origin", | |
7bd76e97 | 1613 | @"rooturi", |
33e30380 | 1614 | @"sections", |
8d262908 | 1615 | @"shortDescription", |
e58ff941 JF |
1616 | @"trusted", |
1617 | @"type", | |
e58ff941 JF |
1618 | @"version", |
1619 | nil]; | |
6f1a15d9 JF |
1620 | } |
1621 | ||
1622 | - (NSArray *) attributeKeys { | |
1623 | return [[self class] _attributeKeys]; | |
1624 | } | |
1625 | ||
1626 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1627 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1628 | } | |
1629 | ||
0c28a403 JF |
1630 | - (metaIndex *) metaIndex { |
1631 | return index_; | |
1632 | } | |
1633 | ||
64edd9df | 1634 | - (void) setMetaIndex:(metaIndex *)index inPool:(CYPool *)pool { |
a3328c28 JF |
1635 | trusted_ = index->IsTrusted(); |
1636 | ||
f9f6d9e8 JF |
1637 | uri_.set(pool, index->GetURI()); |
1638 | distribution_.set(pool, index->GetDist()); | |
1639 | type_.set(pool, index->GetType()); | |
a3328c28 JF |
1640 | |
1641 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index)); | |
1642 | if (dindex != NULL) { | |
21ac0ce2 | 1643 | std::string file(dindex->MetaIndexURI("")); |
21ac0ce2 JF |
1644 | base_.set(pool, file); |
1645 | ||
3f88f205 | 1646 | pkgAcquire acquire; |
1a83afc6 | 1647 | _profile(Source$setMetaIndex$GetIndexes) |
3f88f205 | 1648 | dindex->GetIndexes(&acquire, true); |
1a83afc6 JF |
1649 | _end |
1650 | _profile(Source$setMetaIndex$DescURI) | |
3f88f205 JF |
1651 | for (pkgAcquire::ItemIterator item(acquire.ItemsBegin()); item != acquire.ItemsEnd(); item++) { |
1652 | std::string file((*item)->DescURI()); | |
1653 | files_.insert(file); | |
1654 | if (file.length() < sizeof("Packages.bz2") || file.substr(file.length() - sizeof("Packages.bz2")) != "/Packages.bz2") | |
1655 | continue; | |
1656 | file = file.substr(0, file.length() - 4); | |
21ac0ce2 JF |
1657 | files_.insert(file); |
1658 | files_.insert(file + ".gz"); | |
21ac0ce2 | 1659 | files_.insert(file + "Index"); |
3f88f205 | 1660 | } |
1a83afc6 | 1661 | _end |
8252b666 | 1662 | |
18873623 JF |
1663 | FileFd fd; |
1664 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) | |
1665 | _error->Discard(); | |
1666 | else { | |
1667 | pkgTagFile tags(&fd); | |
f9f6d9e8 | 1668 | |
18873623 JF |
1669 | pkgTagSection section; |
1670 | tags.Step(section); | |
a3328c28 | 1671 | |
18873623 JF |
1672 | struct { |
1673 | const char *name_; | |
1674 | CYString *value_; | |
1675 | } names[] = { | |
1676 | {"default-icon", &defaultIcon_}, | |
6204f56a | 1677 | {"depiction", &depiction_}, |
18873623 JF |
1678 | {"description", &description_}, |
1679 | {"label", &label_}, | |
1680 | {"origin", &origin_}, | |
1681 | {"support", &support_}, | |
1682 | {"version", &version_}, | |
1683 | }; | |
f9f6d9e8 | 1684 | |
18873623 JF |
1685 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { |
1686 | const char *start, *end; | |
1687 | ||
1688 | if (section.Find(names[i].name_, start, end)) { | |
1689 | CYString &value(*names[i].value_); | |
1690 | value.set(pool, start, end - start); | |
1691 | } | |
f9f6d9e8 | 1692 | } |
36bb2ca2 | 1693 | } |
a3328c28 | 1694 | } |
7b0ce2da | 1695 | |
a3328c28 | 1696 | record_ = [Sources_ objectForKey:[self key]]; |
f9f6d9e8 | 1697 | |
7623f855 JF |
1698 | NSURL *url([NSURL URLWithString:uri_]); |
1699 | ||
1700 | host_ = [url host]; | |
1701 | if (host_ != nil) | |
c08c8943 | 1702 | host_ = [host_ lowercaseString]; |
7623f855 JF |
1703 | |
1704 | if (host_ != nil) | |
dfdb9ae0 | 1705 | authority_ = host_; |
7623f855 JF |
1706 | else |
1707 | authority_ = [url path]; | |
a3328c28 JF |
1708 | } |
1709 | ||
64edd9df | 1710 | - (Source *) initWithMetaIndex:(metaIndex *)index forDatabase:(Database *)database inPool:(CYPool *)pool { |
a3328c28 | 1711 | if ((self = [super init]) != nil) { |
aec64d46 JF |
1712 | era_ = [database era]; |
1713 | database_ = database; | |
1714 | index_ = index; | |
1715 | ||
1a83afc6 | 1716 | _profile(Source$initWithMetaIndex$setMetaIndex) |
f9f6d9e8 | 1717 | [self setMetaIndex:index inPool:pool]; |
1a83afc6 | 1718 | _end |
36bb2ca2 | 1719 | } return self; |
2367a917 | 1720 | } |
dc5812ec | 1721 | |
aec64d46 JF |
1722 | - (NSString *) getField:(NSString *)name { |
1723 | @synchronized (database_) { | |
1724 | if ([database_ era] != era_ || index_ == NULL) | |
1725 | return nil; | |
1726 | ||
1727 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index_)); | |
1728 | if (dindex == NULL) | |
1729 | return nil; | |
1730 | ||
1731 | FileFd fd; | |
1732 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) { | |
1733 | _error->Discard(); | |
1734 | return nil; | |
1735 | } | |
1736 | ||
1737 | pkgTagFile tags(&fd); | |
1738 | ||
1739 | pkgTagSection section; | |
1740 | tags.Step(section); | |
1741 | ||
1742 | const char *start, *end; | |
1743 | if (!section.Find([name UTF8String], start, end)) | |
1744 | return (NSString *) [NSNull null]; | |
1745 | ||
1746 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; | |
1747 | } } | |
1748 | ||
90ba4f86 | 1749 | - (NSComparisonResult) compareByName:(Source *)source { |
7b0ce2da JF |
1750 | NSString *lhs = [self name]; |
1751 | NSString *rhs = [source name]; | |
1752 | ||
1753 | if ([lhs length] != 0 && [rhs length] != 0) { | |
1754 | unichar lhc = [lhs characterAtIndex:0]; | |
1755 | unichar rhc = [rhs characterAtIndex:0]; | |
1756 | ||
1757 | if (isalpha(lhc) && !isalpha(rhc)) | |
1758 | return NSOrderedAscending; | |
1759 | else if (!isalpha(lhc) && isalpha(rhc)) | |
1760 | return NSOrderedDescending; | |
1761 | } | |
1762 | ||
68f1828e | 1763 | return [lhs compare:rhs options:LaxCompareOptions_]; |
7b0ce2da JF |
1764 | } |
1765 | ||
6204f56a | 1766 | - (NSString *) depictionForPackage:(NSString *)package { |
89b0ea4a | 1767 | return depiction_.empty() ? nil : [static_cast<id>(depiction_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
6204f56a JF |
1768 | } |
1769 | ||
dc63e78f | 1770 | - (NSString *) supportForPackage:(NSString *)package { |
89b0ea4a | 1771 | return support_.empty() ? nil : [static_cast<id>(support_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
dc63e78f JF |
1772 | } |
1773 | ||
33e30380 JF |
1774 | - (NSArray *) sections { |
1775 | return record_ == nil ? (id) [NSNull null] : [record_ objectForKey:@"Sections"] ?: [NSArray array]; | |
1776 | } | |
1777 | ||
1778 | - (void) _addSection:(NSString *)section { | |
1779 | if (record_ == nil) | |
1780 | return; | |
1781 | else if (NSMutableArray *sections = [record_ objectForKey:@"Sections"]) { | |
4e479350 | 1782 | if (![sections containsObject:section]) |
33e30380 | 1783 | [sections addObject:section]; |
4e479350 | 1784 | } else |
33e30380 | 1785 | [record_ setObject:[NSMutableArray arrayWithObject:section] forKey:@"Sections"]; |
33e30380 JF |
1786 | } |
1787 | ||
1788 | - (bool) addSection:(NSString *)section { | |
1789 | if (record_ == nil) | |
1790 | return false; | |
1791 | ||
1792 | [self performSelectorOnMainThread:@selector(_addSection:) withObject:section waitUntilDone:NO]; | |
1793 | return true; | |
1794 | } | |
1795 | ||
1796 | - (void) _removeSection:(NSString *)section { | |
1797 | if (record_ == nil) | |
1798 | return; | |
1799 | ||
1800 | if (NSMutableArray *sections = [record_ objectForKey:@"Sections"]) | |
4e479350 | 1801 | if ([sections containsObject:section]) |
33e30380 | 1802 | [sections removeObject:section]; |
33e30380 JF |
1803 | } |
1804 | ||
1805 | - (bool) removeSection:(NSString *)section { | |
1806 | if (record_ == nil) | |
1807 | return false; | |
1808 | ||
1809 | [self performSelectorOnMainThread:@selector(_removeSection:) withObject:section waitUntilDone:NO]; | |
1810 | return true; | |
1811 | } | |
1812 | ||
1813 | - (void) _remove { | |
1814 | [Sources_ removeObjectForKey:[self key]]; | |
33e30380 JF |
1815 | } |
1816 | ||
1817 | - (bool) remove { | |
1818 | bool value(record_ != nil); | |
1819 | [self performSelectorOnMainThread:@selector(_remove) withObject:nil waitUntilDone:NO]; | |
1820 | return value; | |
1821 | } | |
1822 | ||
7b0ce2da JF |
1823 | - (NSDictionary *) record { |
1824 | return record_; | |
1825 | } | |
1826 | ||
36bb2ca2 JF |
1827 | - (BOOL) trusted { |
1828 | return trusted_; | |
1829 | } | |
3178d79b | 1830 | |
7bd76e97 | 1831 | - (NSString *) rooturi { |
36bb2ca2 JF |
1832 | return uri_; |
1833 | } | |
ec97ef06 | 1834 | |
36bb2ca2 JF |
1835 | - (NSString *) distribution { |
1836 | return distribution_; | |
1837 | } | |
dc5812ec | 1838 | |
36bb2ca2 JF |
1839 | - (NSString *) type { |
1840 | return type_; | |
dc5812ec JF |
1841 | } |
1842 | ||
7bd76e97 JF |
1843 | - (NSString *) baseuri { |
1844 | return base_.empty() ? nil : (id) base_; | |
1845 | } | |
1846 | ||
1847 | - (NSString *) iconuri { | |
1848 | if (NSString *base = [self baseuri]) | |
1849 | return [base stringByAppendingString:@"CydiaIcon.png"]; | |
1850 | ||
1851 | return nil; | |
1852 | } | |
1853 | ||
1854 | - (NSURL *) iconURL { | |
1855 | if (NSString *uri = [self iconuri]) | |
1856 | return [NSURL URLWithString:uri]; | |
1857 | return nil; | |
8252b666 JF |
1858 | } |
1859 | ||
7b0ce2da | 1860 | - (NSString *) key { |
f9f6d9e8 | 1861 | return [NSString stringWithFormat:@"%@:%@:%@", (NSString *) type_, (NSString *) uri_, (NSString *) distribution_]; |
7b0ce2da JF |
1862 | } |
1863 | ||
1864 | - (NSString *) host { | |
f9f6d9e8 | 1865 | return host_; |
7b0ce2da JF |
1866 | } |
1867 | ||
1868 | - (NSString *) name { | |
c08c8943 | 1869 | return origin_.empty() ? (id) authority_ : origin_; |
7b0ce2da JF |
1870 | } |
1871 | ||
8d262908 | 1872 | - (NSString *) shortDescription { |
36bb2ca2 JF |
1873 | return description_; |
1874 | } | |
b4d89997 | 1875 | |
36bb2ca2 | 1876 | - (NSString *) label { |
c08c8943 | 1877 | return label_.empty() ? (id) authority_ : label_; |
36bb2ca2 | 1878 | } |
3178d79b | 1879 | |
36bb2ca2 JF |
1880 | - (NSString *) origin { |
1881 | return origin_; | |
1882 | } | |
3178d79b | 1883 | |
36bb2ca2 JF |
1884 | - (NSString *) version { |
1885 | return version_; | |
1886 | } | |
2367a917 | 1887 | |
36bb2ca2 JF |
1888 | - (NSString *) defaultIcon { |
1889 | return defaultIcon_; | |
1890 | } | |
2367a917 | 1891 | |
21ac0ce2 JF |
1892 | - (void) setDelegate:(NSObject<SourceDelegate> *)delegate { |
1893 | delegate_ = delegate; | |
1894 | } | |
1895 | ||
1896 | - (bool) fetch { | |
1897 | return !fetches_.empty(); | |
1898 | } | |
1899 | ||
1900 | - (void) setFetch:(bool)fetch forURI:(const char *)uri { | |
1901 | if (!fetch) { | |
1902 | if (fetches_.erase(uri) == 0) | |
1903 | return; | |
1904 | } else if (files_.find(uri) == files_.end()) | |
1905 | return; | |
1906 | else if (!fetches_.insert(uri).second) | |
1907 | return; | |
1908 | ||
1909 | [delegate_ performSelectorOnMainThread:@selector(setFetch:) withObject:[NSNumber numberWithBool:[self fetch]] waitUntilDone:NO]; | |
1910 | } | |
1911 | ||
9ed626f1 JF |
1912 | - (void) resetFetch { |
1913 | fetches_.clear(); | |
1914 | [delegate_ performSelectorOnMainThread:@selector(setFetch:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:NO]; | |
1915 | } | |
1916 | ||
2388b078 JF |
1917 | @end |
1918 | /* }}} */ | |
83682c75 JF |
1919 | /* CydiaOperation Class {{{ */ |
1920 | @interface CydiaOperation : NSObject { | |
7b33d201 JF |
1921 | _H<NSString> operator_; |
1922 | _H<NSString> value_; | |
2388b078 JF |
1923 | } |
1924 | ||
83682c75 JF |
1925 | - (NSString *) operator; |
1926 | - (NSString *) value; | |
2388b078 JF |
1927 | |
1928 | @end | |
1929 | ||
83682c75 | 1930 | @implementation CydiaOperation |
2388b078 | 1931 | |
83682c75 JF |
1932 | - (id) initWithOperator:(const char *)_operator value:(const char *)value { |
1933 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
1934 | operator_ = [NSString stringWithUTF8String:_operator]; |
1935 | value_ = [NSString stringWithUTF8String:value]; | |
83682c75 JF |
1936 | } return self; |
1937 | } | |
1938 | ||
1939 | + (NSArray *) _attributeKeys { | |
1940 | return [NSArray arrayWithObjects: | |
1941 | @"operator", | |
1942 | @"value", | |
1943 | nil]; | |
2388b078 JF |
1944 | } |
1945 | ||
83682c75 JF |
1946 | - (NSArray *) attributeKeys { |
1947 | return [[self class] _attributeKeys]; | |
2388b078 JF |
1948 | } |
1949 | ||
83682c75 JF |
1950 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
1951 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1952 | } | |
1953 | ||
1954 | - (NSString *) operator { | |
1955 | return operator_; | |
1956 | } | |
1957 | ||
1958 | - (NSString *) value { | |
1959 | return value_; | |
1960 | } | |
1961 | ||
1962 | @end | |
1963 | /* }}} */ | |
810c9763 JF |
1964 | /* CydiaClause Class {{{ */ |
1965 | @interface CydiaClause : NSObject { | |
7b33d201 JF |
1966 | _H<NSString> package_; |
1967 | _H<CydiaOperation> version_; | |
83682c75 JF |
1968 | } |
1969 | ||
83682c75 JF |
1970 | - (NSString *) package; |
1971 | - (CydiaOperation *) version; | |
1972 | ||
1973 | @end | |
1974 | ||
810c9763 | 1975 | @implementation CydiaClause |
83682c75 | 1976 | |
83682c75 JF |
1977 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
1978 | if ((self = [super init]) != nil) { | |
7b33d201 | 1979 | package_ = [NSString stringWithUTF8String:dep.TargetPkg().Name()]; |
83682c75 JF |
1980 | |
1981 | if (const char *version = dep.TargetVer()) | |
7b33d201 | 1982 | version_ = [[[CydiaOperation alloc] initWithOperator:dep.CompType() value:version] autorelease]; |
83682c75 | 1983 | else |
7b33d201 | 1984 | version_ = (id) [NSNull null]; |
83682c75 JF |
1985 | } return self; |
1986 | } | |
1987 | ||
1988 | + (NSArray *) _attributeKeys { | |
1989 | return [NSArray arrayWithObjects: | |
1990 | @"package", | |
83682c75 JF |
1991 | @"version", |
1992 | nil]; | |
1993 | } | |
1994 | ||
1995 | - (NSArray *) attributeKeys { | |
1996 | return [[self class] _attributeKeys]; | |
1997 | } | |
1998 | ||
1999 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2000 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2001 | } | |
2002 | ||
83682c75 JF |
2003 | - (NSString *) package { |
2004 | return package_; | |
2005 | } | |
2006 | ||
2007 | - (CydiaOperation *) version { | |
2008 | return version_; | |
2388b078 JF |
2009 | } |
2010 | ||
810c9763 JF |
2011 | @end |
2012 | /* }}} */ | |
2013 | /* CydiaRelation Class {{{ */ | |
2014 | @interface CydiaRelation : NSObject { | |
7b33d201 JF |
2015 | _H<NSString> relationship_; |
2016 | _H<NSMutableArray> clauses_; | |
810c9763 JF |
2017 | } |
2018 | ||
2019 | - (NSString *) relationship; | |
2020 | - (NSArray *) clauses; | |
2021 | ||
2022 | @end | |
2023 | ||
2024 | @implementation CydiaRelation | |
2025 | ||
810c9763 JF |
2026 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
2027 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
2028 | relationship_ = [NSString stringWithUTF8String:dep.DepType()]; |
2029 | clauses_ = [NSMutableArray arrayWithCapacity:8]; | |
810c9763 JF |
2030 | |
2031 | pkgCache::DepIterator start; | |
2032 | pkgCache::DepIterator end; | |
2033 | dep.GlobOr(start, end); // ++dep | |
2034 | ||
2035 | _forever { | |
2036 | [clauses_ addObject:[[[CydiaClause alloc] initWithIterator:start] autorelease]]; | |
2037 | ||
2038 | // yes, seriously. (wtf?) | |
2039 | if (start == end) | |
2040 | break; | |
2041 | ++start; | |
2042 | } | |
2043 | } return self; | |
2044 | } | |
2045 | ||
2046 | + (NSArray *) _attributeKeys { | |
2047 | return [NSArray arrayWithObjects: | |
2048 | @"clauses", | |
2049 | @"relationship", | |
2050 | nil]; | |
2051 | } | |
2052 | ||
2053 | - (NSArray *) attributeKeys { | |
2054 | return [[self class] _attributeKeys]; | |
2055 | } | |
2056 | ||
2057 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2058 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2059 | } | |
2060 | ||
2061 | - (NSString *) relationship { | |
2062 | return relationship_; | |
2063 | } | |
2064 | ||
2065 | - (NSArray *) clauses { | |
2066 | return clauses_; | |
2067 | } | |
2068 | ||
2069 | - (void) addClause:(CydiaClause *)clause { | |
2070 | [clauses_ addObject:clause]; | |
2071 | } | |
2072 | ||
dc5812ec | 2073 | @end |
b4d89997 | 2074 | /* }}} */ |
36bb2ca2 | 2075 | /* Package Class {{{ */ |
12016ee5 | 2076 | struct ParsedPackage { |
974742b2 | 2077 | CYString md5sum_; |
12016ee5 JF |
2078 | CYString tagline_; |
2079 | ||
a412f357 | 2080 | CYString architecture_; |
12016ee5 JF |
2081 | CYString icon_; |
2082 | ||
2083 | CYString depiction_; | |
2084 | CYString homepage_; | |
12016ee5 JF |
2085 | CYString author_; |
2086 | ||
12016ee5 JF |
2087 | CYString support_; |
2088 | }; | |
2089 | ||
36bb2ca2 | 2090 | @interface Package : NSObject { |
13902177 | 2091 | uint32_t era_ : 25; |
89bdef78 | 2092 | @public uint32_t role_ : 3; |
aab28f8b JF |
2093 | uint32_t essential_ : 1; |
2094 | uint32_t obsolete_ : 1; | |
2095 | uint32_t ignored_ : 1; | |
13902177 | 2096 | uint32_t pooled_ : 1; |
aab28f8b | 2097 | |
64edd9df | 2098 | CYPool *pool_; |
a1440b10 | 2099 | |
9c5737d5 JF |
2100 | uint32_t rank_; |
2101 | ||
aab28f8b JF |
2102 | _transient Database *database_; |
2103 | ||
7376b55c | 2104 | pkgCache::VerIterator version_; |
36bb2ca2 | 2105 | pkgCache::PkgIterator iterator_; |
36bb2ca2 | 2106 | pkgCache::VerFileIterator file_; |
bbb879fb | 2107 | |
aab28f8b JF |
2108 | CYString id_; |
2109 | CYString name_; | |
22fd24dd | 2110 | CYString transform_; |
807ae6d7 | 2111 | |
4c0ed943 | 2112 | CYString latest_; |
6a155117 | 2113 | CYString installed_; |
b3906a21 | 2114 | time_t upgraded_; |
dc5812ec | 2115 | |
fe33a23e | 2116 | const char *section_; |
aab28f8b | 2117 | _transient NSString *section$_; |
f79a4512 | 2118 | |
7b33d201 | 2119 | _H<Source> source_; |
2388b078 | 2120 | |
bb6bb6d6 | 2121 | PackageValue *metadata_; |
aab28f8b | 2122 | ParsedPackage *parsed_; |
94b0b3e5 | 2123 | |
7b33d201 | 2124 | _H<NSMutableArray> tags_; |
b4d89997 JF |
2125 | } |
2126 | ||
64edd9df JF |
2127 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; |
2128 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database; | |
b4d89997 | 2129 | |
2388b078 | 2130 | - (pkgCache::PkgIterator) iterator; |
68d927e2 | 2131 | - (void) parse; |
2388b078 | 2132 | |
36bb2ca2 | 2133 | - (NSString *) section; |
dec6029f JF |
2134 | - (NSString *) simpleSection; |
2135 | ||
f79a4512 JF |
2136 | - (NSString *) longSection; |
2137 | - (NSString *) shortSection; | |
2138 | ||
a3328c28 JF |
2139 | - (NSString *) uri; |
2140 | ||
7aa82ca2 | 2141 | - (MIMEAddress *) maintainer; |
36bb2ca2 | 2142 | - (size_t) size; |
eef4ccaf JF |
2143 | - (NSString *) longDescription; |
2144 | - (NSString *) shortDescription; | |
808c6eb6 | 2145 | - (unichar) index; |
dc5812ec | 2146 | |
94b0b3e5 | 2147 | - (PackageValue *) metadata; |
31bc18a7 | 2148 | - (time_t) seen; |
94b0b3e5 JF |
2149 | |
2150 | - (bool) subscribed; | |
2151 | - (bool) setSubscribed:(bool)subscribed; | |
2152 | ||
807ae6d7 | 2153 | - (BOOL) ignored; |
9e98e020 | 2154 | |
36bb2ca2 JF |
2155 | - (NSString *) latest; |
2156 | - (NSString *) installed; | |
6a155117 | 2157 | - (BOOL) uninstalled; |
5a09ae08 JF |
2158 | |
2159 | - (BOOL) valid; | |
31f3cfff | 2160 | - (BOOL) upgradableAndEssential:(BOOL)essential; |
36bb2ca2 JF |
2161 | - (BOOL) essential; |
2162 | - (BOOL) broken; | |
7d2ac47f | 2163 | - (BOOL) unfiltered; |
6d9712c4 | 2164 | - (BOOL) visible; |
b4d89997 | 2165 | |
9bedffaa JF |
2166 | - (BOOL) half; |
2167 | - (BOOL) halfConfigured; | |
2168 | - (BOOL) halfInstalled; | |
2169 | - (BOOL) hasMode; | |
2170 | - (NSString *) mode; | |
2171 | ||
36bb2ca2 JF |
2172 | - (NSString *) id; |
2173 | - (NSString *) name; | |
770f2a8e | 2174 | - (UIImage *) icon; |
6f1a15d9 | 2175 | - (NSString *) homepage; |
25a2158d | 2176 | - (NSString *) depiction; |
7aa82ca2 | 2177 | - (MIMEAddress *) author; |
b4d89997 | 2178 | |
dc63e78f JF |
2179 | - (NSString *) support; |
2180 | ||
affeffc7 | 2181 | - (NSArray *) files; |
affeffc7 JF |
2182 | - (NSArray *) warnings; |
2183 | - (NSArray *) applications; | |
2388b078 | 2184 | |
36bb2ca2 | 2185 | - (Source *) source; |
b4d89997 | 2186 | |
9c5737d5 | 2187 | - (uint32_t) rank; |
d84597fe | 2188 | - (BOOL) matches:(NSArray *)query; |
b4d89997 | 2189 | |
6d9712c4 | 2190 | - (BOOL) hasTag:(NSString *)tag; |
c390d3ab | 2191 | - (NSString *) primaryPurpose; |
770f2a8e | 2192 | - (NSArray *) purposes; |
3bd1c2a2 | 2193 | - (bool) isCommercial; |
6d9712c4 | 2194 | |
cd95e390 JF |
2195 | - (void) setIndex:(size_t)index; |
2196 | ||
df213583 JF |
2197 | - (CYString &) cyname; |
2198 | ||
f79a4512 | 2199 | - (uint32_t) compareBySection:(NSArray *)sections; |
807ae6d7 | 2200 | |
36bb2ca2 JF |
2201 | - (void) install; |
2202 | - (void) remove; | |
06aa974d | 2203 | |
36bb2ca2 | 2204 | @end |
b4d89997 | 2205 | |
f79a4512 JF |
2206 | uint32_t PackageChangesRadix(Package *self, void *) { |
2207 | union { | |
2208 | uint32_t key; | |
2209 | ||
2210 | struct { | |
2211 | uint32_t timestamp : 30; | |
2212 | uint32_t ignored : 1; | |
2213 | uint32_t upgradable : 1; | |
2214 | } bits; | |
2215 | } value; | |
2216 | ||
2217 | bool upgradable([self upgradableAndEssential:YES]); | |
2218 | value.bits.upgradable = upgradable ? 1 : 0; | |
2219 | ||
2220 | if (upgradable) { | |
2221 | value.bits.timestamp = 0; | |
2222 | value.bits.ignored = [self ignored] ? 0 : 1; | |
2223 | value.bits.upgradable = 1; | |
2224 | } else { | |
36047c66 | 2225 | value.bits.timestamp = [self seen] >> 2; |
f79a4512 JF |
2226 | value.bits.ignored = 0; |
2227 | value.bits.upgradable = 0; | |
2228 | } | |
2229 | ||
2230 | return _not(uint32_t) - value.key; | |
2231 | } | |
2232 | ||
aeeb755b JF |
2233 | CYString &(*PackageName)(Package *self, SEL sel); |
2234 | ||
df213583 JF |
2235 | uint32_t PackagePrefixRadix(Package *self, void *context) { |
2236 | size_t offset(reinterpret_cast<size_t>(context)); | |
aeeb755b | 2237 | CYString &name(PackageName(self, @selector(cyname))); |
677b8415 | 2238 | |
df213583 JF |
2239 | size_t size(name.size()); |
2240 | if (size == 0) | |
2241 | return 0; | |
2242 | char *text(name.data()); | |
677b8415 | 2243 | |
df213583 JF |
2244 | size_t zeros; |
2245 | if (!isdigit(text[0])) | |
2246 | zeros = 0; | |
2247 | else { | |
2248 | size_t digits(1); | |
2249 | while (size != digits && isdigit(text[digits])) | |
2250 | if (++digits == 4) | |
2251 | break; | |
2252 | zeros = 4 - digits; | |
2253 | } | |
677b8415 | 2254 | |
df213583 JF |
2255 | uint8_t data[4]; |
2256 | ||
df213583 JF |
2257 | if (offset == 0 && zeros != 0) { |
2258 | memset(data, '0', zeros); | |
2259 | memcpy(data + zeros, text, 4 - zeros); | |
2260 | } else { | |
2261 | /* XXX: there's some danger here if you request a non-zero offset < 4 and it gets zero padded */ | |
2262 | if (size <= offset - zeros) | |
2263 | return 0; | |
2264 | ||
2265 | text += offset - zeros; | |
2266 | size -= offset - zeros; | |
2267 | ||
2268 | if (size >= 4) | |
2269 | memcpy(data, text, 4); | |
2270 | else { | |
2271 | memcpy(data, text, size); | |
2272 | memset(data + size, 0, 4 - size); | |
2273 | } | |
2274 | ||
2275 | for (size_t i(0); i != 4; ++i) | |
2276 | if (isalpha(data[i])) | |
a7a59ee1 | 2277 | data[i] |= 0x20; |
df213583 JF |
2278 | } |
2279 | ||
2280 | if (offset == 0) | |
a7a59ee1 JF |
2281 | if (data[0] == '@') |
2282 | data[0] = 0x7f; | |
2283 | else | |
2284 | data[0] = (data[0] & 0x1f) | "\x80\x00\xc0\x40"[data[0] >> 6]; | |
df213583 JF |
2285 | |
2286 | /* XXX: ntohl may be more honest */ | |
2287 | return OSSwapInt32(*reinterpret_cast<uint32_t *>(data)); | |
2288 | } | |
2289 | ||
22fd24dd | 2290 | CFComparisonResult StringNameCompare(CFStringRef lhn, CFStringRef rhn, size_t length) { |
df213583 | 2291 | _profile(PackageNameCompare) |
5358f56f | 2292 | if (lhn == NULL) |
b129e6d9 | 2293 | return rhn == NULL ? kCFCompareEqualTo : kCFCompareLessThan; |
5358f56f | 2294 | else if (rhn == NULL) |
b129e6d9 | 2295 | return kCFCompareGreaterThan; |
5358f56f | 2296 | |
22fd24dd | 2297 | CFIndex length(CFStringGetLength(lhn)); |
aeeb755b | 2298 | |
677b8415 | 2299 | _profile(PackageNameCompare$NumbersLast) |
22fd24dd | 2300 | if (length != 0 && CFStringGetLength(rhn) != 0) { |
677b8415 JF |
2301 | UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); |
2302 | UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); | |
2303 | bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); | |
2304 | if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) | |
b129e6d9 | 2305 | return lha ? kCFCompareLessThan : kCFCompareGreaterThan; |
677b8415 JF |
2306 | } |
2307 | _end | |
2308 | ||
2309 | _profile(PackageNameCompare$Compare) | |
22fd24dd | 2310 | return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, (CFLocaleRef) (id) CollationLocale_); |
677b8415 JF |
2311 | _end |
2312 | _end | |
2313 | } | |
2314 | ||
22fd24dd JF |
2315 | _finline CFComparisonResult StringNameCompare(NSString *lhn, NSString*rhn, size_t length) { |
2316 | return StringNameCompare((CFStringRef) lhn, (CFStringRef) rhn, length); | |
2317 | } | |
2318 | ||
aeeb755b | 2319 | CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { |
22fd24dd JF |
2320 | CYString &lhn(PackageName(lhs, @selector(cyname))); |
2321 | NSString *rhn(PackageName(rhs, @selector(cyname))); | |
2322 | return StringNameCompare(lhn, rhn, lhn.size()); | |
aeeb755b JF |
2323 | } |
2324 | ||
2325 | CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *arg) { | |
2326 | return PackageNameCompare(*lhs, *rhs, arg); | |
677b8415 JF |
2327 | } |
2328 | ||
2329 | struct PackageNameOrdering : | |
2330 | std::binary_function<Package *, Package *, bool> | |
2331 | { | |
2332 | _finline bool operator ()(Package *lhs, Package *rhs) const { | |
b129e6d9 | 2333 | return PackageNameCompare(lhs, rhs, NULL) == kCFCompareLessThan; |
677b8415 JF |
2334 | } |
2335 | }; | |
2336 | ||
36bb2ca2 | 2337 | @implementation Package |
b4d89997 | 2338 | |
df213583 JF |
2339 | - (NSString *) description { |
2340 | return [NSString stringWithFormat:@"<Package:%@>", static_cast<NSString *>(name_)]; | |
2341 | } | |
2342 | ||
36bb2ca2 | 2343 | - (void) dealloc { |
13902177 | 2344 | if (!pooled_) |
64edd9df | 2345 | delete pool_; |
12016ee5 JF |
2346 | if (parsed_ != NULL) |
2347 | delete parsed_; | |
36bb2ca2 | 2348 | [super dealloc]; |
b4d89997 JF |
2349 | } |
2350 | ||
3bd1c2a2 | 2351 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 2352 | if (false); |
10d63f9e JF |
2353 | else if (selector == @selector(clear)) |
2354 | return @"clear"; | |
2cd1afd9 JF |
2355 | else if (selector == @selector(getField:)) |
2356 | return @"getField"; | |
8fb7c7a1 JF |
2357 | else if (selector == @selector(getRecord)) |
2358 | return @"getRecord"; | |
e58ff941 | 2359 | else if (selector == @selector(hasTag:)) |
3bd1c2a2 | 2360 | return @"hasTag"; |
10d63f9e JF |
2361 | else if (selector == @selector(install)) |
2362 | return @"install"; | |
2363 | else if (selector == @selector(remove)) | |
2364 | return @"remove"; | |
3bd1c2a2 JF |
2365 | else |
2366 | return nil; | |
2367 | } | |
2368 | ||
2369 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
2370 | return [self webScriptNameForSelector:selector] == nil; | |
2371 | } | |
2372 | ||
6f1a15d9 | 2373 | + (NSArray *) _attributeKeys { |
e58ff941 JF |
2374 | return [NSArray arrayWithObjects: |
2375 | @"applications", | |
a412f357 | 2376 | @"architecture", |
e58ff941 JF |
2377 | @"author", |
2378 | @"depiction", | |
2379 | @"essential", | |
2380 | @"homepage", | |
2381 | @"icon", | |
2382 | @"id", | |
2383 | @"installed", | |
2384 | @"latest", | |
2385 | @"longDescription", | |
2386 | @"longSection", | |
2387 | @"maintainer", | |
974742b2 | 2388 | @"md5sum", |
e58ff941 JF |
2389 | @"mode", |
2390 | @"name", | |
2391 | @"purposes", | |
83682c75 | 2392 | @"relations", |
e58ff941 | 2393 | @"section", |
de1ace71 | 2394 | @"selection", |
e58ff941 JF |
2395 | @"shortDescription", |
2396 | @"shortSection", | |
2397 | @"simpleSection", | |
2398 | @"size", | |
2399 | @"source", | |
5959b596 | 2400 | @"state", |
e58ff941 | 2401 | @"support", |
82aa2434 | 2402 | @"tags", |
1342f808 | 2403 | @"upgraded", |
e58ff941 JF |
2404 | @"warnings", |
2405 | nil]; | |
6f1a15d9 JF |
2406 | } |
2407 | ||
2408 | - (NSArray *) attributeKeys { | |
2409 | return [[self class] _attributeKeys]; | |
2410 | } | |
2411 | ||
2412 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2413 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2414 | } | |
2415 | ||
83682c75 JF |
2416 | - (NSArray *) relations { |
2417 | @synchronized (database_) { | |
2418 | NSMutableArray *relations([NSMutableArray arrayWithCapacity:16]); | |
810c9763 JF |
2419 | for (pkgCache::DepIterator dep(version_.DependsList()); !dep.end(); ++dep) |
2420 | [relations addObject:[[[CydiaRelation alloc] initWithIterator:dep] autorelease]]; | |
83682c75 JF |
2421 | return relations; |
2422 | } } | |
2423 | ||
a412f357 JF |
2424 | - (NSString *) architecture { |
2425 | [self parse]; | |
2426 | @synchronized (database_) { | |
2427 | return parsed_->architecture_.empty() ? [NSNull null] : (id) parsed_->architecture_; | |
2428 | } } | |
2429 | ||
2cd1afd9 JF |
2430 | - (NSString *) getField:(NSString *)name { |
2431 | @synchronized (database_) { | |
2432 | if ([database_ era] != era_ || file_.end()) | |
2433 | return nil; | |
2434 | ||
2435 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2436 | ||
2437 | const char *start, *end; | |
2438 | if (!parser.Find([name UTF8String], start, end)) | |
2439 | return (NSString *) [NSNull null]; | |
2440 | ||
b6f9e52a | 2441 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; |
2cd1afd9 JF |
2442 | } } |
2443 | ||
8fb7c7a1 JF |
2444 | - (NSString *) getRecord { |
2445 | @synchronized (database_) { | |
2446 | if ([database_ era] != era_ || file_.end()) | |
2447 | return nil; | |
2448 | ||
2449 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2450 | ||
2451 | const char *start, *end; | |
2452 | parser.GetRec(start, end); | |
2453 | ||
2454 | return [NSString stringWithString:[(NSString *) CYStringCreate(start, end - start) autorelease]]; | |
2455 | } } | |
2456 | ||
68d927e2 | 2457 | - (void) parse { |
12016ee5 | 2458 | if (parsed_ != NULL) |
68d927e2 | 2459 | return; |
12016ee5 JF |
2460 | @synchronized (database_) { |
2461 | if ([database_ era] != era_ || file_.end()) | |
68d927e2 JF |
2462 | return; |
2463 | ||
12016ee5 JF |
2464 | ParsedPackage *parsed(new ParsedPackage); |
2465 | parsed_ = parsed; | |
2466 | ||
68d927e2 JF |
2467 | _profile(Package$parse) |
2468 | pkgRecords::Parser *parser; | |
2469 | ||
2470 | _profile(Package$parse$Lookup) | |
2471 | parser = &[database_ records]->Lookup(file_); | |
2472 | _end | |
2473 | ||
00067a67 | 2474 | CYString bugs; |
68d927e2 JF |
2475 | CYString website; |
2476 | ||
2477 | _profile(Package$parse$Find) | |
2478 | struct { | |
2479 | const char *name_; | |
2480 | CYString *value_; | |
2481 | } names[] = { | |
a412f357 | 2482 | {"architecture", &parsed->architecture_}, |
12016ee5 JF |
2483 | {"icon", &parsed->icon_}, |
2484 | {"depiction", &parsed->depiction_}, | |
2485 | {"homepage", &parsed->homepage_}, | |
68d927e2 | 2486 | {"website", &website}, |
00067a67 | 2487 | {"bugs", &bugs}, |
12016ee5 | 2488 | {"support", &parsed->support_}, |
12016ee5 | 2489 | {"author", &parsed->author_}, |
974742b2 | 2490 | {"md5sum", &parsed->md5sum_}, |
68d927e2 JF |
2491 | }; |
2492 | ||
2493 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { | |
2494 | const char *start, *end; | |
2495 | ||
2496 | if (parser->Find(names[i].name_, start, end)) { | |
2497 | CYString &value(*names[i].value_); | |
2498 | _profile(Package$parse$Value) | |
2499 | value.set(pool_, start, end - start); | |
2500 | _end | |
2501 | } | |
2502 | } | |
2503 | _end | |
2504 | ||
2505 | _profile(Package$parse$Tagline) | |
2506 | const char *start, *end; | |
0dd0c302 | 2507 | if (parser->ShortDesc(start, end)) { |
68d927e2 JF |
2508 | const char *stop(reinterpret_cast<const char *>(memchr(start, '\n', end - start))); |
2509 | if (stop == NULL) | |
2510 | stop = end; | |
2511 | while (stop != start && stop[-1] == '\r') | |
2512 | --stop; | |
12016ee5 | 2513 | parsed->tagline_.set(pool_, start, stop - start); |
68d927e2 JF |
2514 | } |
2515 | _end | |
2516 | ||
2517 | _profile(Package$parse$Retain) | |
12016ee5 JF |
2518 | if (parsed->homepage_.empty()) |
2519 | parsed->homepage_ = website; | |
2520 | if (parsed->homepage_ == parsed->depiction_) | |
2521 | parsed->homepage_.clear(); | |
00067a67 JF |
2522 | if (parsed->support_.empty()) |
2523 | parsed->support_ = bugs; | |
68d927e2 JF |
2524 | _end |
2525 | _end | |
12016ee5 | 2526 | } } |
68d927e2 | 2527 | |
64edd9df | 2528 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { |
a1440b10 | 2529 | if ((self = [super init]) != nil) { |
7376b55c | 2530 | _profile(Package$initWithVersion) |
13902177 | 2531 | if (pool == NULL) |
64edd9df | 2532 | pool_ = new CYPool(); |
13902177 JF |
2533 | else { |
2534 | pool_ = pool; | |
2535 | pooled_ = true; | |
2536 | } | |
a1440b10 | 2537 | |
36bb2ca2 | 2538 | database_ = database; |
aab28f8b | 2539 | era_ = [database era]; |
b4d89997 | 2540 | |
aab28f8b | 2541 | version_ = version; |
2083b866 | 2542 | |
aab28f8b JF |
2543 | pkgCache::PkgIterator iterator(version.ParentPkg()); |
2544 | iterator_ = iterator; | |
06aa974d | 2545 | |
aab28f8b | 2546 | _profile(Package$initWithVersion$Version) |
68d927e2 JF |
2547 | if (!version_.end()) |
2548 | file_ = version_.FileList(); | |
2549 | else { | |
2550 | pkgCache &cache([database_ cache]); | |
2551 | file_ = pkgCache::VerFileIterator(cache, cache.VerFileP); | |
2552 | } | |
2553 | _end | |
808c6eb6 | 2554 | |
aab28f8b | 2555 | _profile(Package$initWithVersion$Cache) |
aab28f8b JF |
2556 | name_.set(NULL, iterator.Display()); |
2557 | ||
2558 | latest_.set(NULL, StripVersion_(version_.VerStr())); | |
2559 | ||
2560 | pkgCache::VerIterator current(iterator.CurrentVer()); | |
2561 | if (!current.end()) | |
2562 | installed_.set(NULL, StripVersion_(current.VerStr())); | |
808c6eb6 JF |
2563 | _end |
2564 | ||
22fd24dd JF |
2565 | _profile(Package$initWithVersion$Transliterate) do { |
2566 | if (CollationTransl_ == NULL) | |
2567 | break; | |
2568 | if (name_.empty()) | |
2569 | break; | |
2570 | ||
2571 | _profile(Package$initWithVersion$Transliterate$utf8) | |
2572 | const uint8_t *data(reinterpret_cast<const uint8_t *>(name_.data())); | |
2573 | for (size_t i(0), e(name_.size()); i != e; ++i) | |
2574 | if (data[i] >= 0x80) | |
2575 | goto extended; | |
2576 | break; extended:; | |
2577 | _end | |
2578 | ||
2579 | UErrorCode code(U_ZERO_ERROR); | |
2580 | int32_t length; | |
2581 | ||
2582 | _profile(Package$initWithVersion$Transliterate$u_strFromUTF8WithSub) | |
2583 | CollationString_.resize(name_.size()); | |
2584 | u_strFromUTF8WithSub(&CollationString_[0], CollationString_.size(), &length, name_.data(), name_.size(), 0xfffd, NULL, &code); | |
2585 | if (!U_SUCCESS(code)) | |
2586 | break; | |
2587 | CollationString_.resize(length); | |
2588 | _end | |
2589 | ||
2590 | _profile(Package$initWithVersion$Transliterate$utrans_trans) | |
2591 | length = CollationString_.size(); | |
2592 | utrans_trans(CollationTransl_, reinterpret_cast<UReplaceable *>(&CollationString_), &CollationUCalls_, 0, &length, &code); | |
2593 | if (!U_SUCCESS(code)) | |
2594 | break; | |
2595 | _assert(CollationString_.size() == length); | |
2596 | _end | |
2597 | ||
2598 | _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$preflight) | |
2599 | u_strToUTF8WithSub(NULL, 0, &length, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); | |
2600 | if (code == U_BUFFER_OVERFLOW_ERROR) | |
2601 | code = U_ZERO_ERROR; | |
2602 | else if (!U_SUCCESS(code)) | |
2603 | break; | |
2604 | _end | |
2605 | ||
2606 | char *transform; | |
2607 | _profile(Package$initWithVersion$Transliterate$apr_palloc) | |
64edd9df | 2608 | transform = pool_->malloc<char>(length); |
22fd24dd JF |
2609 | _end |
2610 | _profile(Package$initWithVersion$Transliterate$u_strToUTF8WithSub$transform) | |
2611 | u_strToUTF8WithSub(transform, length, NULL, CollationString_.data(), CollationString_.size(), 0xfffd, NULL, &code); | |
2612 | if (!U_SUCCESS(code)) | |
2613 | break; | |
2614 | _end | |
2615 | ||
2616 | transform_.set(NULL, transform, length); | |
2617 | } while (false); _end | |
2618 | ||
7376b55c | 2619 | _profile(Package$initWithVersion$Tags) |
aab28f8b | 2620 | pkgCache::TagIterator tag(iterator.TagList()); |
0dd0c302 | 2621 | if (!tag.end()) { |
7b33d201 | 2622 | tags_ = [NSMutableArray arrayWithCapacity:8]; |
5b625a2e JF |
2623 | |
2624 | goto tag; for (; !tag.end(); ++tag) tag: { | |
0dd0c302 | 2625 | const char *name(tag.Name()); |
5b625a2e JF |
2626 | NSString *string((NSString *) CYStringCreate(name)); |
2627 | if (string == nil) | |
2628 | continue; | |
2629 | ||
2630 | [tags_ addObject:[string autorelease]]; | |
0a377825 | 2631 | |
97a3d89e JF |
2632 | if (role_ == 0 && strncmp(name, "role::", 6) == 0 /*&& strcmp(name, "role::leaper") != 0*/) { |
2633 | if (strcmp(name + 6, "enduser") == 0) | |
2634 | role_ = 1; | |
2635 | else if (strcmp(name + 6, "hacker") == 0) | |
2636 | role_ = 2; | |
2637 | else if (strcmp(name + 6, "developer") == 0) | |
2638 | role_ = 3; | |
2639 | else if (strcmp(name + 6, "cydia") == 0) | |
2640 | role_ = 7; | |
2641 | else | |
2642 | role_ = 4; | |
2643 | } | |
0a377825 JF |
2644 | |
2645 | if (strncmp(name, "cydia::", 7) == 0) { | |
2646 | if (strcmp(name + 7, "essential") == 0) | |
2647 | essential_ = true; | |
2648 | else if (strcmp(name + 7, "obsolete") == 0) | |
2649 | obsolete_ = true; | |
2650 | } | |
5b625a2e | 2651 | } |
0dd0c302 | 2652 | } |
808c6eb6 | 2653 | _end |
7b0ce2da | 2654 | |
7376b55c | 2655 | _profile(Package$initWithVersion$Metadata) |
029c8b74 JF |
2656 | const char *mixed(iterator.Name()); |
2657 | size_t size(strlen(mixed)); | |
81bf4684 JF |
2658 | static const size_t prefix(sizeof("/var/lib/dpkg/info/") - 1); |
2659 | char lower[prefix + size + 5 + 1]; | |
029c8b74 JF |
2660 | |
2661 | for (size_t i(0); i != size; ++i) | |
81bf4684 JF |
2662 | lower[prefix + i] = mixed[i] | 0x20; |
2663 | ||
2664 | if (!installed_.empty()) { | |
2665 | memcpy(lower, "/var/lib/dpkg/info/", prefix); | |
2666 | memcpy(lower + prefix + size, ".list", 6); | |
2667 | struct stat info; | |
2668 | if (stat(lower, &info) != -1) | |
b3906a21 | 2669 | upgraded_ = info.st_birthtime; |
81bf4684 | 2670 | } |
029c8b74 | 2671 | |
81bf4684 | 2672 | PackageValue *metadata(PackageFind(lower + prefix, size)); |
bb6bb6d6 | 2673 | metadata_ = metadata; |
677b8415 | 2674 | |
029c8b74 JF |
2675 | id_.set(NULL, metadata->name_, size); |
2676 | ||
94b0b3e5 JF |
2677 | const char *latest(version_.VerStr()); |
2678 | size_t length(strlen(latest)); | |
a4b0ec52 | 2679 | |
94b0b3e5 | 2680 | uint16_t vhash(hashlittle(latest, length)); |
677b8415 | 2681 | |
94b0b3e5 JF |
2682 | size_t capped(std::min<size_t>(8, length)); |
2683 | latest = latest + length - capped; | |
677b8415 | 2684 | |
94b0b3e5 JF |
2685 | if (metadata->first_ == 0) |
2686 | metadata->first_ = now_; | |
808c6eb6 | 2687 | |
94b0b3e5 | 2688 | if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) { |
94b0b3e5 JF |
2689 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); |
2690 | metadata->vhash_ = vhash; | |
2f856365 JF |
2691 | metadata->last_ = now_; |
2692 | } else if (metadata->last_ == 0) | |
2693 | metadata->last_ = metadata->first_; | |
808c6eb6 | 2694 | _end |
a1440b10 | 2695 | |
7376b55c | 2696 | _profile(Package$initWithVersion$Section) |
0bc841de | 2697 | section_ = version_.Section(); |
f79a4512 | 2698 | _end |
a1440b10 | 2699 | |
aab28f8b JF |
2700 | _profile(Package$initWithVersion$Flags) |
2701 | essential_ |= ((iterator->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES); | |
2702 | ignored_ = iterator->SelectedState == pkgCache::State::Hold; | |
4fa77608 | 2703 | _end |
3dd53516 | 2704 | _end } return self; |
dc5812ec JF |
2705 | } |
2706 | ||
64edd9df | 2707 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(CYPool *)pool database:(Database *)database { |
b1ce61ec JF |
2708 | pkgCache::VerIterator version; |
2709 | ||
2710 | _profile(Package$packageWithIterator$GetCandidateVer) | |
2711 | version = [database policy]->GetCandidateVer(iterator); | |
2712 | _end | |
2713 | ||
7376b55c JF |
2714 | if (version.end()) |
2715 | return nil; | |
b1ce61ec | 2716 | |
8564efc1 JF |
2717 | Package *package; |
2718 | ||
2719 | _profile(Package$packageWithIterator$Allocate) | |
2720 | package = [Package allocWithZone:zone]; | |
2721 | _end | |
2722 | ||
2723 | _profile(Package$packageWithIterator$Initialize) | |
2724 | package = [package | |
2725 | initWithVersion:version | |
2726 | withZone:zone | |
2727 | inPool:pool | |
2728 | database:database | |
2729 | ]; | |
2730 | _end | |
2731 | ||
2732 | _profile(Package$packageWithIterator$Autorelease) | |
2733 | package = [package autorelease]; | |
2734 | _end | |
2735 | ||
2736 | return package; | |
3dd53516 | 2737 | } |
dc5812ec | 2738 | |
2388b078 JF |
2739 | - (pkgCache::PkgIterator) iterator { |
2740 | return iterator_; | |
2741 | } | |
2742 | ||
36bb2ca2 | 2743 | - (NSString *) section { |
f79a4512 | 2744 | if (section$_ == nil) { |
fe33a23e | 2745 | if (section_ == NULL) |
f79a4512 JF |
2746 | return nil; |
2747 | ||
fe33a23e JF |
2748 | _profile(Package$section$mappedSectionForPointer) |
2749 | section$_ = [database_ mappedSectionForPointer:section_]; | |
e4f3d6e9 | 2750 | _end |
f79a4512 | 2751 | } return section$_; |
dc5812ec JF |
2752 | } |
2753 | ||
dec6029f JF |
2754 | - (NSString *) simpleSection { |
2755 | if (NSString *section = [self section]) | |
2756 | return Simplify(section); | |
2757 | else | |
2758 | return nil; | |
a3328c28 | 2759 | } |
dec6029f | 2760 | |
f79a4512 | 2761 | - (NSString *) longSection { |
18873623 | 2762 | return LocalizeSection([self section]); |
f79a4512 JF |
2763 | } |
2764 | ||
2765 | - (NSString *) shortSection { | |
2766 | return [[NSBundle mainBundle] localizedStringForKey:[self simpleSection] value:nil table:@"Sections"]; | |
2767 | } | |
2768 | ||
a3328c28 JF |
2769 | - (NSString *) uri { |
2770 | return nil; | |
2771 | #if 0 | |
2772 | pkgIndexFile *index; | |
2773 | pkgCache::PkgFileIterator file(file_.File()); | |
2774 | if (![database_ list].FindIndex(file, index)) | |
2775 | return nil; | |
2776 | return [NSString stringWithUTF8String:iterator_->Path]; | |
2777 | //return [NSString stringWithUTF8String:file.Site()]; | |
2778 | //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()]; | |
2779 | #endif | |
dec6029f JF |
2780 | } |
2781 | ||
7aa82ca2 | 2782 | - (MIMEAddress *) maintainer { |
884171d6 JF |
2783 | @synchronized (database_) { |
2784 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2785 | return nil; |
884171d6 | 2786 | |
36bb2ca2 | 2787 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
00e2109e | 2788 | const std::string &maintainer(parser->Maintainer()); |
7aa82ca2 | 2789 | return maintainer.empty() ? nil : [MIMEAddress addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]]; |
884171d6 | 2790 | } } |
8fe19fc1 | 2791 | |
974742b2 JF |
2792 | - (NSString *) md5sum { |
2793 | return parsed_ == NULL ? nil : (id) parsed_->md5sum_; | |
2794 | } | |
2795 | ||
36bb2ca2 | 2796 | - (size_t) size { |
884171d6 JF |
2797 | @synchronized (database_) { |
2798 | if ([database_ era] != era_ || version_.end()) | |
2799 | return 0; | |
2800 | ||
2801 | return version_->InstalledSize; | |
2802 | } } | |
dc5812ec | 2803 | |
eef4ccaf | 2804 | - (NSString *) longDescription { |
3dd53516 JF |
2805 | @synchronized (database_) { |
2806 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2807 | return nil; |
3dd53516 | 2808 | |
36bb2ca2 | 2809 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
2388b078 | 2810 | NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]); |
8fe19fc1 | 2811 | |
36bb2ca2 JF |
2812 | NSArray *lines = [description componentsSeparatedByString:@"\n"]; |
2813 | NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; | |
2814 | if ([lines count] < 2) | |
2815 | return nil; | |
3178d79b | 2816 | |
36bb2ca2 | 2817 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; |
c4dcf2c2 | 2818 | for (size_t i(1), e([lines count]); i != e; ++i) { |
36bb2ca2 JF |
2819 | NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; |
2820 | [trimmed addObject:trim]; | |
2821 | } | |
3178d79b | 2822 | |
36bb2ca2 | 2823 | return [trimmed componentsJoinedByString:@"\n"]; |
3dd53516 | 2824 | } } |
dc5812ec | 2825 | |
eef4ccaf | 2826 | - (NSString *) shortDescription { |
60bef540 JF |
2827 | if (parsed_ != NULL) |
2828 | return static_cast<NSString *>(parsed_->tagline_); | |
2829 | ||
2830 | @synchronized (database_) { | |
2831 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2832 | ||
2833 | const char *start, *end; | |
2834 | if (!parser.ShortDesc(start, end)) | |
2835 | return nil; | |
2836 | ||
abd93900 JF |
2837 | if (end - start > 200) |
2838 | end = start + 200; | |
60bef540 JF |
2839 | |
2840 | /* | |
2841 | if (const char *stop = reinterpret_cast<const char *>(memchr(start, '\n', end - start))) | |
2842 | end = stop; | |
2843 | ||
2844 | while (end != start && end[-1] == '\r') | |
2845 | --end; | |
2846 | */ | |
2847 | ||
2848 | return [(id) CYStringCreate(start, end - start) autorelease]; | |
2849 | } } | |
eef4ccaf | 2850 | |
808c6eb6 JF |
2851 | - (unichar) index { |
2852 | _profile(Package$index) | |
677b8415 JF |
2853 | CFStringRef name((CFStringRef) [self name]); |
2854 | if (CFStringGetLength(name) == 0) | |
808c6eb6 | 2855 | return '#'; |
677b8415 JF |
2856 | UniChar character(CFStringGetCharacterAtIndex(name, 0)); |
2857 | if (!CFUniCharIsMemberOf(character, kCFUniCharLetterCharacterSet)) | |
808c6eb6 | 2858 | return '#'; |
447db19d | 2859 | return toupper(character); |
808c6eb6 | 2860 | _end |
36bb2ca2 | 2861 | } |
3178d79b | 2862 | |
94b0b3e5 | 2863 | - (PackageValue *) metadata { |
bb6bb6d6 | 2864 | return metadata_; |
807ae6d7 JF |
2865 | } |
2866 | ||
31bc18a7 | 2867 | - (time_t) seen { |
94b0b3e5 JF |
2868 | PackageValue *metadata([self metadata]); |
2869 | return metadata->subscribed_ ? metadata->last_ : metadata->first_; | |
3178d79b JF |
2870 | } |
2871 | ||
94b0b3e5 JF |
2872 | - (bool) subscribed { |
2873 | return [self metadata]->subscribed_; | |
2874 | } | |
2875 | ||
2876 | - (bool) setSubscribed:(bool)subscribed { | |
2877 | PackageValue *metadata([self metadata]); | |
2878 | if (metadata->subscribed_ == subscribed) | |
2879 | return false; | |
2880 | metadata->subscribed_ = subscribed; | |
2881 | return true; | |
807ae6d7 JF |
2882 | } |
2883 | ||
2884 | - (BOOL) ignored { | |
1b18f026 | 2885 | return ignored_; |
807ae6d7 JF |
2886 | } |
2887 | ||
36bb2ca2 JF |
2888 | - (NSString *) latest { |
2889 | return latest_; | |
8fe19fc1 JF |
2890 | } |
2891 | ||
36bb2ca2 JF |
2892 | - (NSString *) installed { |
2893 | return installed_; | |
3178d79b JF |
2894 | } |
2895 | ||
6a155117 JF |
2896 | - (BOOL) uninstalled { |
2897 | return installed_.empty(); | |
2898 | } | |
2899 | ||
5a09ae08 JF |
2900 | - (BOOL) valid { |
2901 | return !version_.end(); | |
2902 | } | |
2903 | ||
31f3cfff | 2904 | - (BOOL) upgradableAndEssential:(BOOL)essential { |
677b8415 JF |
2905 | _profile(Package$upgradableAndEssential) |
2906 | pkgCache::VerIterator current(iterator_.CurrentVer()); | |
2907 | if (current.end()) | |
e4f3d6e9 | 2908 | return essential && essential_; |
677b8415 | 2909 | else |
e4f3d6e9 | 2910 | return !version_.end() && version_ != current; |
677b8415 | 2911 | _end |
36bb2ca2 | 2912 | } |
3178d79b | 2913 | |
36bb2ca2 | 2914 | - (BOOL) essential { |
a1440b10 | 2915 | return essential_; |
3178d79b JF |
2916 | } |
2917 | ||
36bb2ca2 | 2918 | - (BOOL) broken { |
9bedffaa JF |
2919 | return [database_ cache][iterator_].InstBroken(); |
2920 | } | |
2921 | ||
7d2ac47f | 2922 | - (BOOL) unfiltered { |
4fa77608 | 2923 | _profile(Package$unfiltered$obsolete) |
cf48f656 | 2924 | if (_unlikely(obsolete_)) |
4fa77608 JF |
2925 | return false; |
2926 | _end | |
2927 | ||
4121c5e0 JF |
2928 | _profile(Package$unfiltered$role) |
2929 | if (_unlikely(role_ > 3)) | |
4fa77608 JF |
2930 | return false; |
2931 | _end | |
2932 | ||
e4f3d6e9 JF |
2933 | return true; |
2934 | } | |
4fa77608 | 2935 | |
e4f3d6e9 JF |
2936 | - (BOOL) visible { |
2937 | if (![self unfiltered]) | |
2938 | return false; | |
2939 | ||
fe33a23e JF |
2940 | NSString *section; |
2941 | ||
2942 | _profile(Package$visible$section) | |
2943 | section = [self section]; | |
2944 | _end | |
4fa77608 | 2945 | |
e4f3d6e9 | 2946 | _profile(Package$visible$isSectionVisible) |
45447dc3 | 2947 | if (!isSectionVisible(section)) |
4fa77608 JF |
2948 | return false; |
2949 | _end | |
2950 | ||
2951 | return true; | |
7d2ac47f JF |
2952 | } |
2953 | ||
9bedffaa | 2954 | - (BOOL) half { |
677b8415 | 2955 | unsigned char current(iterator_->CurrentState); |
9bedffaa JF |
2956 | return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled; |
2957 | } | |
2958 | ||
2959 | - (BOOL) halfConfigured { | |
2960 | return iterator_->CurrentState == pkgCache::State::HalfConfigured; | |
2961 | } | |
2962 | ||
2963 | - (BOOL) halfInstalled { | |
2964 | return iterator_->CurrentState == pkgCache::State::HalfInstalled; | |
2965 | } | |
2966 | ||
2967 | - (BOOL) hasMode { | |
f18b4a97 JF |
2968 | @synchronized (database_) { |
2969 | if ([database_ era] != era_ || iterator_.end()) | |
b129e6d9 | 2970 | return NO; |
f18b4a97 | 2971 | |
9bedffaa JF |
2972 | pkgDepCache::StateCache &state([database_ cache][iterator_]); |
2973 | return state.Mode != pkgDepCache::ModeKeep; | |
f18b4a97 | 2974 | } } |
9bedffaa JF |
2975 | |
2976 | - (NSString *) mode { | |
f18b4a97 JF |
2977 | @synchronized (database_) { |
2978 | if ([database_ era] != era_ || iterator_.end()) | |
2979 | return nil; | |
2980 | ||
9bedffaa JF |
2981 | pkgDepCache::StateCache &state([database_ cache][iterator_]); |
2982 | ||
2983 | switch (state.Mode) { | |
2984 | case pkgDepCache::ModeDelete: | |
2985 | if ((state.iFlags & pkgDepCache::Purge) != 0) | |
f79a4512 | 2986 | return @"PURGE"; |
9bedffaa | 2987 | else |
f79a4512 | 2988 | return @"REMOVE"; |
9bedffaa | 2989 | case pkgDepCache::ModeKeep: |
dc63e78f | 2990 | if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2991 | return @"REINSTALL"; |
dc63e78f JF |
2992 | /*else if ((state.iFlags & pkgDepCache::AutoKept) != 0) |
2993 | return nil;*/ | |
9bedffaa JF |
2994 | else |
2995 | return nil; | |
9bedffaa | 2996 | case pkgDepCache::ModeInstall: |
dc63e78f | 2997 | /*if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2998 | return @"REINSTALL"; |
dc63e78f | 2999 | else*/ switch (state.Status) { |
9bedffaa | 3000 | case -1: |
f79a4512 | 3001 | return @"DOWNGRADE"; |
9bedffaa | 3002 | case 0: |
f79a4512 | 3003 | return @"INSTALL"; |
9bedffaa | 3004 | case 1: |
f79a4512 | 3005 | return @"UPGRADE"; |
9bedffaa | 3006 | case 2: |
f79a4512 | 3007 | return @"NEW_INSTALL"; |
670a0494 | 3008 | _nodefault |
9bedffaa | 3009 | } |
670a0494 | 3010 | _nodefault |
9bedffaa | 3011 | } |
f18b4a97 | 3012 | } } |
8fe19fc1 | 3013 | |
36bb2ca2 JF |
3014 | - (NSString *) id { |
3015 | return id_; | |
8fe19fc1 JF |
3016 | } |
3017 | ||
36bb2ca2 | 3018 | - (NSString *) name { |
9fcbca29 | 3019 | return name_.empty() ? id_ : name_; |
36bb2ca2 | 3020 | } |
8fe19fc1 | 3021 | |
770f2a8e | 3022 | - (UIImage *) icon { |
dec6029f | 3023 | NSString *section = [self simpleSection]; |
770f2a8e JF |
3024 | |
3025 | UIImage *icon(nil); | |
12016ee5 JF |
3026 | if (parsed_ != NULL) |
3027 | if (NSString *href = parsed_->icon_) | |
3028 | if ([href hasPrefix:@"file:///"]) | |
84851d87 | 3029 | icon = [UIImage imageAtPath:[[href substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; |
770f2a8e | 3030 | if (icon == nil) if (section != nil) |
84851d87 | 3031 | icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [section stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]; |
b9956841 | 3032 | if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon]) |
189a73d0 | 3033 | if ([dicon hasPrefix:@"file:///"]) |
84851d87 | 3034 | icon = [UIImage imageAtPath:[[dicon substringFromIndex:7] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; |
770f2a8e | 3035 | if (icon == nil) |
8dbf3abc | 3036 | icon = [UIImage imageNamed:@"unknown.png"]; |
770f2a8e | 3037 | return icon; |
36bb2ca2 | 3038 | } |
8fe19fc1 | 3039 | |
6f1a15d9 | 3040 | - (NSString *) homepage { |
12016ee5 | 3041 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->homepage_); |
8fe19fc1 JF |
3042 | } |
3043 | ||
25a2158d | 3044 | - (NSString *) depiction { |
12016ee5 | 3045 | return parsed_ != NULL && !parsed_->depiction_.empty() ? parsed_->depiction_ : [[self source] depictionForPackage:id_]; |
25a2158d JF |
3046 | } |
3047 | ||
7aa82ca2 JF |
3048 | - (MIMEAddress *) author { |
3049 | return parsed_ == NULL || parsed_->author_.empty() ? nil : [MIMEAddress addressWithString:parsed_->author_]; | |
77fcccaf JF |
3050 | } |
3051 | ||
dc63e78f | 3052 | - (NSString *) support { |
00067a67 | 3053 | return parsed_ != NULL && !parsed_->support_.empty() ? parsed_->support_ : [[self source] supportForPackage:id_]; |
dc63e78f JF |
3054 | } |
3055 | ||
affeffc7 | 3056 | - (NSArray *) files { |
9fcbca29 | 3057 | NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", static_cast<NSString *>(id_)]; |
affeffc7 JF |
3058 | NSMutableArray *files = [NSMutableArray arrayWithCapacity:128]; |
3059 | ||
3060 | std::ifstream fin; | |
3061 | fin.open([path UTF8String]); | |
3062 | if (!fin.is_open()) | |
3063 | return nil; | |
3064 | ||
3065 | std::string line; | |
3066 | while (std::getline(fin, line)) | |
3067 | [files addObject:[NSString stringWithUTF8String:line.c_str()]]; | |
3068 | ||
3069 | return files; | |
3070 | } | |
3071 | ||
5959b596 JF |
3072 | - (NSString *) state { |
3073 | @synchronized (database_) { | |
3074 | if ([database_ era] != era_ || file_.end()) | |
3075 | return nil; | |
3076 | ||
3077 | switch (iterator_->CurrentState) { | |
3078 | case pkgCache::State::NotInstalled: | |
3079 | return @"NotInstalled"; | |
3080 | case pkgCache::State::UnPacked: | |
3081 | return @"UnPacked"; | |
3082 | case pkgCache::State::HalfConfigured: | |
3083 | return @"HalfConfigured"; | |
3084 | case pkgCache::State::HalfInstalled: | |
3085 | return @"HalfInstalled"; | |
3086 | case pkgCache::State::ConfigFiles: | |
3087 | return @"ConfigFiles"; | |
3088 | case pkgCache::State::Installed: | |
3089 | return @"Installed"; | |
3090 | case pkgCache::State::TriggersAwaited: | |
3091 | return @"TriggersAwaited"; | |
3092 | case pkgCache::State::TriggersPending: | |
3093 | return @"TriggersPending"; | |
3094 | } | |
3095 | ||
3096 | return (NSString *) [NSNull null]; | |
3097 | } } | |
3098 | ||
de1ace71 JF |
3099 | - (NSString *) selection { |
3100 | @synchronized (database_) { | |
3101 | if ([database_ era] != era_ || file_.end()) | |
3102 | return nil; | |
3103 | ||
3104 | switch (iterator_->SelectedState) { | |
3105 | case pkgCache::State::Unknown: | |
3106 | return @"Unknown"; | |
3107 | case pkgCache::State::Install: | |
3108 | return @"Install"; | |
3109 | case pkgCache::State::Hold: | |
3110 | return @"Hold"; | |
3111 | case pkgCache::State::DeInstall: | |
3112 | return @"DeInstall"; | |
3113 | case pkgCache::State::Purge: | |
3114 | return @"Purge"; | |
3115 | } | |
3116 | ||
3117 | return (NSString *) [NSNull null]; | |
3118 | } } | |
3119 | ||
affeffc7 | 3120 | - (NSArray *) warnings { |
d212b02f JF |
3121 | @synchronized (database_) { |
3122 | if ([database_ era] != era_ || file_.end()) | |
3123 | return nil; | |
3124 | ||
affeffc7 JF |
3125 | NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); |
3126 | const char *name(iterator_.Name()); | |
3127 | ||
3128 | size_t length(strlen(name)); | |
3129 | if (length < 2) invalid: | |
43f3d7f6 | 3130 | [warnings addObject:UCLocalize("ILLEGAL_PACKAGE_IDENTIFIER")]; |
affeffc7 JF |
3131 | else for (size_t i(0); i != length; ++i) |
3132 | if ( | |
9dd60d81 JF |
3133 | /* XXX: technically this is not allowed */ |
3134 | (name[i] < 'A' || name[i] > 'Z') && | |
affeffc7 JF |
3135 | (name[i] < 'a' || name[i] > 'z') && |
3136 | (name[i] < '0' || name[i] > '9') && | |
3137 | (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.') | |
3138 | ) goto invalid; | |
3139 | ||
3140 | if (strcmp(name, "cydia") != 0) { | |
3141 | bool cydia = false; | |
7623f855 | 3142 | bool user = false; |
9dd60d81 | 3143 | bool _private = false; |
affeffc7 | 3144 | bool stash = false; |
bcc58607 | 3145 | bool dbstash = false; |
1db5920d | 3146 | bool dsstore = false; |
affeffc7 | 3147 | |
9dd60d81 JF |
3148 | bool repository = [[self section] isEqualToString:@"Repositories"]; |
3149 | ||
affeffc7 JF |
3150 | if (NSArray *files = [self files]) |
3151 | for (NSString *file in files) | |
3152 | if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"]) | |
3153 | cydia = true; | |
7623f855 JF |
3154 | else if (!user && [file isEqualToString:@"/User"]) |
3155 | user = true; | |
9dd60d81 JF |
3156 | else if (!_private && [file isEqualToString:@"/private"]) |
3157 | _private = true; | |
affeffc7 JF |
3158 | else if (!stash && [file isEqualToString:@"/var/stash"]) |
3159 | stash = true; | |
bcc58607 JF |
3160 | else if (!dbstash && [file isEqualToString:@"/var/db/stash"]) |
3161 | dbstash = true; | |
1db5920d JF |
3162 | else if (!dsstore && [file hasSuffix:@"/.DS_Store"]) |
3163 | dsstore = true; | |
affeffc7 | 3164 | |
9dd60d81 JF |
3165 | /* XXX: this is not sensitive enough. only some folders are valid. */ |
3166 | if (cydia && !repository) | |
43f3d7f6 | 3167 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"Cydia.app"]]; |
7623f855 JF |
3168 | if (user) |
3169 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/User"]]; | |
9dd60d81 | 3170 | if (_private) |
43f3d7f6 | 3171 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; |
affeffc7 | 3172 | if (stash) |
43f3d7f6 | 3173 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; |
bcc58607 JF |
3174 | if (dbstash) |
3175 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/db/stash"]]; | |
1db5920d JF |
3176 | if (dsstore) |
3177 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @".DS_Store"]]; | |
affeffc7 JF |
3178 | } |
3179 | ||
3180 | return [warnings count] == 0 ? nil : warnings; | |
d212b02f | 3181 | } } |
affeffc7 JF |
3182 | |
3183 | - (NSArray *) applications { | |
3184 | NSString *me([[NSBundle mainBundle] bundleIdentifier]); | |
3185 | ||
3186 | NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); | |
3187 | ||
b4fd1197 | 3188 | static RegEx application_r("/Applications/(.*)\\.app/Info.plist"); |
affeffc7 JF |
3189 | if (NSArray *files = [self files]) |
3190 | for (NSString *file in files) | |
3191 | if (application_r(file)) { | |
3192 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); | |
75d2e426 JF |
3193 | if (info == nil) |
3194 | continue; | |
affeffc7 | 3195 | NSString *id([info objectForKey:@"CFBundleIdentifier"]); |
75d2e426 | 3196 | if (id == nil || [id isEqualToString:me]) |
affeffc7 JF |
3197 | continue; |
3198 | ||
3199 | NSString *display([info objectForKey:@"CFBundleDisplayName"]); | |
3200 | if (display == nil) | |
3201 | display = application_r[1]; | |
3202 | ||
3203 | NSString *bundle([file stringByDeletingLastPathComponent]); | |
3204 | NSString *icon([info objectForKey:@"CFBundleIconFile"]); | |
5e17a734 JF |
3205 | // XXX: maybe this should check if this is really a string, not just for length |
3206 | if (icon == nil || ![icon respondsToSelector:@selector(length)] || [icon length] == 0) | |
affeffc7 JF |
3207 | icon = @"icon.png"; |
3208 | NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]); | |
3209 | ||
3210 | NSMutableArray *application([NSMutableArray arrayWithCapacity:2]); | |
3211 | [applications addObject:application]; | |
3212 | ||
3213 | [application addObject:id]; | |
3214 | [application addObject:display]; | |
3215 | [application addObject:url]; | |
3216 | } | |
3217 | ||
3218 | return [applications count] == 0 ? nil : applications; | |
3219 | } | |
3220 | ||
36bb2ca2 | 3221 | - (Source *) source { |
5699667a | 3222 | if (source_ == nil) { |
a1440b10 JF |
3223 | @synchronized (database_) { |
3224 | if ([database_ era] != era_ || file_.end()) | |
5699667a JF |
3225 | source_ = (Source *) [NSNull null]; |
3226 | else | |
7b33d201 | 3227 | source_ = [database_ getSource:file_.File()] ?: (Source *) [NSNull null]; |
a1440b10 | 3228 | } |
bbb879fb JF |
3229 | } |
3230 | ||
5699667a | 3231 | return source_ == (Source *) [NSNull null] ? nil : source_; |
8fe19fc1 JF |
3232 | } |
3233 | ||
b3906a21 JF |
3234 | - (time_t) upgraded { |
3235 | return upgraded_; | |
98ddcefd JF |
3236 | } |
3237 | ||
b3906a21 JF |
3238 | - (uint32_t) recent { |
3239 | return std::numeric_limits<uint32_t>::max() - upgraded_; | |
821b1a0c JF |
3240 | } |
3241 | ||
9c5737d5 JF |
3242 | - (uint32_t) rank { |
3243 | return rank_; | |
3244 | } | |
3245 | ||
d84597fe JF |
3246 | - (BOOL) matches:(NSArray *)query { |
3247 | if (query == nil || [query count] == 0) | |
36bb2ca2 | 3248 | return NO; |
8fe19fc1 | 3249 | |
9c5737d5 JF |
3250 | rank_ = 0; |
3251 | ||
3252 | NSString *string; | |
36bb2ca2 | 3253 | NSRange range; |
9c5737d5 | 3254 | NSUInteger length; |
8fe19fc1 | 3255 | |
718c267c | 3256 | string = [self name]; |
d84597fe JF |
3257 | length = [string length]; |
3258 | ||
5af08e16 | 3259 | if (length != 0) |
d84597fe JF |
3260 | for (NSString *term in query) { |
3261 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
3262 | if (range.location != NSNotFound) | |
718c267c | 3263 | rank_ -= 6 * 1000000 / length; |
d84597fe | 3264 | } |
8fe19fc1 | 3265 | |
718c267c JF |
3266 | if (rank_ == 0) { |
3267 | string = [self id]; | |
3268 | length = [string length]; | |
8fe19fc1 | 3269 | |
5af08e16 | 3270 | if (length != 0) |
718c267c JF |
3271 | for (NSString *term in query) { |
3272 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
3273 | if (range.location != NSNotFound) | |
3274 | rank_ -= 6 * 1000000 / length; | |
3275 | } | |
d84597fe | 3276 | } |
c4b530a7 | 3277 | |
9c5737d5 JF |
3278 | string = [self shortDescription]; |
3279 | length = [string length]; | |
abd93900 | 3280 | NSUInteger stop(std::min<NSUInteger>(length, 200)); |
df289c5a | 3281 | |
5af08e16 | 3282 | if (length != 0) |
d84597fe JF |
3283 | for (NSString *term in query) { |
3284 | range = [string rangeOfString:term options:MatchCompareOptions_ range:NSMakeRange(0, stop)]; | |
3285 | if (range.location != NSNotFound) | |
718c267c | 3286 | rank_ -= 2 * 100000; |
d84597fe | 3287 | } |
8fe19fc1 | 3288 | |
9c5737d5 | 3289 | return rank_ != 0; |
36bb2ca2 | 3290 | } |
8fe19fc1 | 3291 | |
82aa2434 JF |
3292 | - (NSArray *) tags { |
3293 | return tags_; | |
3294 | } | |
3295 | ||
6d9712c4 JF |
3296 | - (BOOL) hasTag:(NSString *)tag { |
3297 | return tags_ == nil ? NO : [tags_ containsObject:tag]; | |
3298 | } | |
3299 | ||
c390d3ab | 3300 | - (NSString *) primaryPurpose { |
7b33d201 | 3301 | for (NSString *tag in (NSArray *) tags_) |
c390d3ab JF |
3302 | if ([tag hasPrefix:@"purpose::"]) |
3303 | return [tag substringFromIndex:9]; | |
3304 | return nil; | |
3305 | } | |
3306 | ||
770f2a8e JF |
3307 | - (NSArray *) purposes { |
3308 | NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]); | |
7b33d201 | 3309 | for (NSString *tag in (NSArray *) tags_) |
770f2a8e JF |
3310 | if ([tag hasPrefix:@"purpose::"]) |
3311 | [purposes addObject:[tag substringFromIndex:9]]; | |
3312 | return [purposes count] == 0 ? nil : purposes; | |
3313 | } | |
3314 | ||
3bd1c2a2 JF |
3315 | - (bool) isCommercial { |
3316 | return [self hasTag:@"cydia::commercial"]; | |
3317 | } | |
3318 | ||
cd95e390 JF |
3319 | - (void) setIndex:(size_t)index { |
3320 | if (metadata_->index_ != index) | |
3321 | metadata_->index_ = index; | |
3322 | } | |
3323 | ||
df213583 | 3324 | - (CYString &) cyname { |
22fd24dd | 3325 | return !transform_.empty() ? transform_ : !name_.empty() ? name_ : id_; |
f79a4512 JF |
3326 | } |
3327 | ||
f79a4512 JF |
3328 | - (uint32_t) compareBySection:(NSArray *)sections { |
3329 | NSString *section([self section]); | |
3330 | for (size_t i(0), e([sections count]); i != e; ++i) { | |
3331 | if ([section isEqualToString:[[sections objectAtIndex:i] name]]) | |
3332 | return i; | |
36bb2ca2 | 3333 | } |
3178d79b | 3334 | |
f79a4512 | 3335 | return _not(uint32_t); |
36bb2ca2 | 3336 | } |
3178d79b | 3337 | |
dc63e78f | 3338 | - (void) clear { |
c6ca67ba | 3339 | @synchronized (database_) { |
dc63e78f JF |
3340 | pkgProblemResolver *resolver = [database_ resolver]; |
3341 | resolver->Clear(iterator_); | |
c6ca67ba JF |
3342 | |
3343 | pkgCacheFile &cache([database_ cache]); | |
3344 | cache->SetReInstall(iterator_, false); | |
3345 | cache->MarkKeep(iterator_, false); | |
3346 | } } | |
dc63e78f | 3347 | |
36bb2ca2 | 3348 | - (void) install { |
c6ca67ba | 3349 | @synchronized (database_) { |
36bb2ca2 JF |
3350 | pkgProblemResolver *resolver = [database_ resolver]; |
3351 | resolver->Clear(iterator_); | |
3352 | resolver->Protect(iterator_); | |
c6ca67ba | 3353 | |
36bb2ca2 | 3354 | pkgCacheFile &cache([database_ cache]); |
c6ca67ba | 3355 | cache->SetReInstall(iterator_, false); |
36bb2ca2 | 3356 | cache->MarkInstall(iterator_, false); |
c6ca67ba | 3357 | |
36bb2ca2 JF |
3358 | pkgDepCache::StateCache &state((*cache)[iterator_]); |
3359 | if (!state.Install()) | |
3360 | cache->SetReInstall(iterator_, true); | |
c6ca67ba | 3361 | } } |
68a238ec | 3362 | |
36bb2ca2 | 3363 | - (void) remove { |
c6ca67ba | 3364 | @synchronized (database_) { |
36bb2ca2 JF |
3365 | pkgProblemResolver *resolver = [database_ resolver]; |
3366 | resolver->Clear(iterator_); | |
36bb2ca2 | 3367 | resolver->Remove(iterator_); |
c6ca67ba JF |
3368 | resolver->Protect(iterator_); |
3369 | ||
3370 | pkgCacheFile &cache([database_ cache]); | |
3371 | cache->SetReInstall(iterator_, false); | |
3372 | cache->MarkDelete(iterator_, true); | |
3373 | } } | |
8fe19fc1 | 3374 | |
36bb2ca2 JF |
3375 | @end |
3376 | /* }}} */ | |
3377 | /* Section Class {{{ */ | |
3378 | @interface Section : NSObject { | |
7b33d201 | 3379 | _H<NSString> name_; |
36bb2ca2 JF |
3380 | size_t row_; |
3381 | size_t count_; | |
7b33d201 | 3382 | _H<NSString> localized_; |
36bb2ca2 | 3383 | } |
3178d79b | 3384 | |
677b8415 | 3385 | - (NSComparisonResult) compareByLocalized:(Section *)section; |
9fcbca29 JF |
3386 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; |
3387 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; | |
3388 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; | |
aeeb755b | 3389 | |
36bb2ca2 | 3390 | - (NSString *) name; |
aeeb755b | 3391 | - (void) setName:(NSString *)name; |
f79a4512 | 3392 | |
36bb2ca2 JF |
3393 | - (size_t) row; |
3394 | - (size_t) count; | |
f79a4512 JF |
3395 | |
3396 | - (void) addToRow; | |
36bb2ca2 | 3397 | - (void) addToCount; |
b19871dd | 3398 | |
f79a4512 | 3399 | - (void) setCount:(size_t)count; |
677b8415 | 3400 | - (NSString *) localized; |
f79a4512 | 3401 | |
36bb2ca2 | 3402 | @end |
b19871dd | 3403 | |
36bb2ca2 | 3404 | @implementation Section |
b19871dd | 3405 | |
677b8415 | 3406 | - (NSComparisonResult) compareByLocalized:(Section *)section { |
9fcbca29 JF |
3407 | NSString *lhs(localized_); |
3408 | NSString *rhs([section localized]); | |
6d9712c4 | 3409 | |
9fcbca29 | 3410 | /*if ([lhs length] != 0 && [rhs length] != 0) { |
6d9712c4 JF |
3411 | unichar lhc = [lhs characterAtIndex:0]; |
3412 | unichar rhc = [rhs characterAtIndex:0]; | |
3413 | ||
3414 | if (isalpha(lhc) && !isalpha(rhc)) | |
3415 | return NSOrderedAscending; | |
3416 | else if (!isalpha(lhc) && isalpha(rhc)) | |
3417 | return NSOrderedDescending; | |
9fcbca29 | 3418 | }*/ |
6d9712c4 | 3419 | |
68f1828e | 3420 | return [lhs compare:rhs options:LaxCompareOptions_]; |
6d9712c4 JF |
3421 | } |
3422 | ||
9fcbca29 JF |
3423 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized { |
3424 | if ((self = [self initWithName:name localize:NO]) != nil) { | |
3425 | if (localized != nil) | |
7b33d201 | 3426 | localized_ = localized; |
9fcbca29 JF |
3427 | } return self; |
3428 | } | |
3429 | ||
3430 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize { | |
3431 | return [self initWithName:name row:0 localize:localize]; | |
6d9712c4 JF |
3432 | } |
3433 | ||
9fcbca29 | 3434 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { |
36bb2ca2 | 3435 | if ((self = [super init]) != nil) { |
7b33d201 | 3436 | name_ = name; |
808c6eb6 | 3437 | row_ = row; |
9fcbca29 | 3438 | if (localize) |
7b33d201 | 3439 | localized_ = LocalizeSection(name_); |
808c6eb6 JF |
3440 | } return self; |
3441 | } | |
3442 | ||
36bb2ca2 JF |
3443 | - (NSString *) name { |
3444 | return name_; | |
3445 | } | |
3446 | ||
aeeb755b JF |
3447 | - (void) setName:(NSString *)name { |
3448 | name_ = name; | |
808c6eb6 JF |
3449 | } |
3450 | ||
36bb2ca2 JF |
3451 | - (size_t) row { |
3452 | return row_; | |
3453 | } | |
3454 | ||
3455 | - (size_t) count { | |
3456 | return count_; | |
3457 | } | |
3458 | ||
f79a4512 JF |
3459 | - (void) addToRow { |
3460 | ++row_; | |
3461 | } | |
3462 | ||
36bb2ca2 JF |
3463 | - (void) addToCount { |
3464 | ++count_; | |
3465 | } | |
3466 | ||
f79a4512 JF |
3467 | - (void) setCount:(size_t)count { |
3468 | count_ = count; | |
3469 | } | |
3470 | ||
3471 | - (NSString *) localized { | |
3472 | return localized_; | |
3473 | } | |
3474 | ||
b19871dd JF |
3475 | @end |
3476 | /* }}} */ | |
3477 | ||
9f9ae81c JF |
3478 | class CydiaLogCleaner : |
3479 | public pkgArchiveCleaner | |
3480 | { | |
3481 | protected: | |
3482 | virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) { | |
3483 | unlink(File); | |
3484 | } | |
3485 | }; | |
3486 | ||
36bb2ca2 JF |
3487 | /* Database Implementation {{{ */ |
3488 | @implementation Database | |
ec97ef06 | 3489 | |
770f2a8e | 3490 | + (Database *) sharedInstance { |
7b33d201 | 3491 | static _H<Database> instance; |
770f2a8e | 3492 | if (instance == nil) |
7b33d201 | 3493 | instance = [[[Database alloc] init] autorelease]; |
770f2a8e JF |
3494 | return instance; |
3495 | } | |
3496 | ||
a1440b10 JF |
3497 | - (unsigned) era { |
3498 | return era_; | |
3499 | } | |
3500 | ||
0944377b JF |
3501 | - (void) releasePackages { |
3502 | CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL); | |
3503 | CFArrayRemoveAllValues(packages_); | |
3504 | } | |
3505 | ||
36bb2ca2 | 3506 | - (void) dealloc { |
3931b718 | 3507 | // XXX: actually implement this thing |
36bb2ca2 | 3508 | _assert(false); |
0944377b | 3509 | [self releasePackages]; |
0944377b | 3510 | NSRecycleZone(zone_); |
36bb2ca2 JF |
3511 | [super dealloc]; |
3512 | } | |
ec97ef06 | 3513 | |
d13edf44 | 3514 | - (void) _readCydia:(NSNumber *)fd { |
77fcccaf JF |
3515 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3516 | std::istream is(&ib); | |
3517 | std::string line; | |
3518 | ||
b4fd1197 | 3519 | static RegEx finish_r("finish:([^:]*)"); |
bc8cd583 | 3520 | |
77fcccaf | 3521 | while (std::getline(is, line)) { |
d13edf44 JF |
3522 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3523 | ||
77fcccaf | 3524 | const char *data(line.c_str()); |
bc8cd583 | 3525 | size_t size = line.size(); |
c390d3ab | 3526 | lprintf("C:%s\n", data); |
bc8cd583 JF |
3527 | |
3528 | if (finish_r(data, size)) { | |
3529 | NSString *finish = finish_r[1]; | |
3530 | int index = [Finishes_ indexOfObject:finish]; | |
3531 | if (index != INT_MAX && index > Finish_) | |
3532 | Finish_ = index; | |
3533 | } | |
d13edf44 JF |
3534 | |
3535 | [pool release]; | |
77fcccaf JF |
3536 | } |
3537 | ||
670a0494 | 3538 | _assume(false); |
77fcccaf JF |
3539 | } |
3540 | ||
d13edf44 | 3541 | - (void) _readStatus:(NSNumber *)fd { |
36bb2ca2 JF |
3542 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3543 | std::istream is(&ib); | |
3544 | std::string line; | |
ec97ef06 | 3545 | |
b4fd1197 JF |
3546 | static RegEx conffile_r("status: [^ ]* : conffile-prompt : (.*?) *"); |
3547 | static RegEx pmstatus_r("([^:]*):([^:]*):([^:]*):(.*)"); | |
ec97ef06 | 3548 | |
36bb2ca2 | 3549 | while (std::getline(is, line)) { |
d13edf44 JF |
3550 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3551 | ||
36bb2ca2 | 3552 | const char *data(line.c_str()); |
670a0494 | 3553 | size_t size(line.size()); |
c390d3ab | 3554 | lprintf("S:%s\n", data); |
ec97ef06 | 3555 | |
a08145a8 JF |
3556 | if (conffile_r(data, size)) { |
3557 | // status: /fail : conffile-prompt : '/fail' '/fail.dpkg-new' 1 1 | |
4187453f | 3558 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:conffile_r[1] waitUntilDone:YES]; |
a08145a8 JF |
3559 | } else if (strncmp(data, "status: ", 8) == 0) { |
3560 | // status: <package>: {unpacked,half-configured,installed} | |
389133be | 3561 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 8)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 JF |
3562 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3563 | } else if (strncmp(data, "processing: ", 12) == 0) { | |
3564 | // processing: configure: config-test | |
389133be | 3565 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 12)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 | 3566 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
6915b806 | 3567 | } else if (pmstatus_r(data, size)) { |
31f3cfff | 3568 | std::string type([pmstatus_r[1] UTF8String]); |
4a2dc82e | 3569 | |
6915b806 | 3570 | NSString *package = pmstatus_r[2]; |
4a2dc82e JF |
3571 | if ([package isEqualToString:@"dpkg-exec"]) |
3572 | package = nil; | |
31f3cfff | 3573 | |
5a09ae08 | 3574 | float percent([pmstatus_r[3] floatValue]); |
6915b806 | 3575 | [progress_ performSelectorOnMainThread:@selector(setProgressPercent:) withObject:[NSNumber numberWithFloat:(percent / 100)] waitUntilDone:YES]; |
5a09ae08 JF |
3576 | |
3577 | NSString *string = pmstatus_r[4]; | |
5a09ae08 | 3578 | |
6915b806 | 3579 | if (type == "pmerror") { |
389133be | 3580 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeError forPackage:package]); |
6915b806 JF |
3581 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3582 | } else if (type == "pmstatus") { | |
389133be | 3583 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeStatus forPackage:package]); |
6915b806 JF |
3584 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3585 | } else if (type == "pmconffile") | |
4187453f | 3586 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:string waitUntilDone:YES]; |
670a0494 JF |
3587 | else |
3588 | lprintf("E:unknown pmstatus\n"); | |
3589 | } else | |
3590 | lprintf("E:unknown status\n"); | |
d13edf44 JF |
3591 | |
3592 | [pool release]; | |
36bb2ca2 | 3593 | } |
ec97ef06 | 3594 | |
670a0494 | 3595 | _assume(false); |
36bb2ca2 | 3596 | } |
ec97ef06 | 3597 | |
d13edf44 | 3598 | - (void) _readOutput:(NSNumber *)fd { |
36bb2ca2 JF |
3599 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3600 | std::istream is(&ib); | |
3601 | std::string line; | |
3602 | ||
5a09ae08 | 3603 | while (std::getline(is, line)) { |
d13edf44 JF |
3604 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3605 | ||
c390d3ab | 3606 | lprintf("O:%s\n", line.c_str()); |
27024935 | 3607 | |
389133be | 3608 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:line.c_str()] ofType:kCydiaProgressEventTypeInformation]); |
6915b806 | 3609 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
d13edf44 JF |
3610 | |
3611 | [pool release]; | |
5a09ae08 | 3612 | } |
36bb2ca2 | 3613 | |
670a0494 | 3614 | _assume(false); |
ec97ef06 JF |
3615 | } |
3616 | ||
8b29f8e6 JF |
3617 | - (FILE *) input { |
3618 | return input_; | |
3619 | } | |
3620 | ||
36bb2ca2 | 3621 | - (Package *) packageWithName:(NSString *)name { |
c5f1a937 JF |
3622 | if (name == nil) |
3623 | return nil; | |
3dd53516 | 3624 | @synchronized (self) { |
5a09ae08 JF |
3625 | if (static_cast<pkgDepCache *>(cache_) == NULL) |
3626 | return nil; | |
36bb2ca2 | 3627 | pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); |
13902177 | 3628 | return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:NULL database:self]; |
28ce8704 | 3629 | } } |
36bb2ca2 | 3630 | |
eb30da80 | 3631 | - (id) init { |
ec97ef06 | 3632 | if ((self = [super init]) != nil) { |
5a09ae08 | 3633 | policy_ = NULL; |
36bb2ca2 JF |
3634 | records_ = NULL; |
3635 | resolver_ = NULL; | |
3636 | fetcher_ = NULL; | |
3637 | lock_ = NULL; | |
ec97ef06 | 3638 | |
f79a4512 | 3639 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3640 | |
9f357d11 JF |
3641 | size_t capacity(MetaFile_->active_); |
3642 | if (capacity == 0) | |
3643 | capacity = 16384; | |
3644 | else | |
3645 | capacity += 1024; | |
3646 | ||
3647 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, capacity, NULL); | |
7b33d201 | 3648 | sourceList_ = [NSMutableArray arrayWithCapacity:16]; |
ec97ef06 | 3649 | |
36bb2ca2 | 3650 | int fds[2]; |
ec97ef06 | 3651 | |
77fcccaf JF |
3652 | _assert(pipe(fds) != -1); |
3653 | cydiafd_ = fds[1]; | |
3654 | ||
3655 | _config->Set("APT::Keep-Fds::", cydiafd_); | |
bc8cd583 | 3656 | setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int)); |
77fcccaf JF |
3657 | |
3658 | [NSThread | |
3659 | detachNewThreadSelector:@selector(_readCydia:) | |
3660 | toTarget:self | |
3931b718 | 3661 | withObject:[NSNumber numberWithInt:fds[0]] |
77fcccaf JF |
3662 | ]; |
3663 | ||
36bb2ca2 JF |
3664 | _assert(pipe(fds) != -1); |
3665 | statusfd_ = fds[1]; | |
ec97ef06 | 3666 | |
36bb2ca2 JF |
3667 | [NSThread |
3668 | detachNewThreadSelector:@selector(_readStatus:) | |
3669 | toTarget:self | |
3931b718 | 3670 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3671 | ]; |
ec97ef06 | 3672 | |
8b29f8e6 JF |
3673 | _assert(pipe(fds) != -1); |
3674 | _assert(dup2(fds[0], 0) != -1); | |
3675 | _assert(close(fds[0]) != -1); | |
3676 | ||
3677 | input_ = fdopen(fds[1], "a"); | |
3678 | ||
36bb2ca2 JF |
3679 | _assert(pipe(fds) != -1); |
3680 | _assert(dup2(fds[1], 1) != -1); | |
3681 | _assert(close(fds[1]) != -1); | |
3682 | ||
3683 | [NSThread | |
3684 | detachNewThreadSelector:@selector(_readOutput:) | |
3685 | toTarget:self | |
3931b718 | 3686 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3687 | ]; |
ec97ef06 JF |
3688 | } return self; |
3689 | } | |
3690 | ||
36bb2ca2 JF |
3691 | - (pkgCacheFile &) cache { |
3692 | return cache_; | |
ec97ef06 JF |
3693 | } |
3694 | ||
5a09ae08 JF |
3695 | - (pkgDepCache::Policy *) policy { |
3696 | return policy_; | |
3697 | } | |
3698 | ||
36bb2ca2 JF |
3699 | - (pkgRecords *) records { |
3700 | return records_; | |
ec97ef06 JF |
3701 | } |
3702 | ||
36bb2ca2 JF |
3703 | - (pkgProblemResolver *) resolver { |
3704 | return resolver_; | |
ec97ef06 JF |
3705 | } |
3706 | ||
36bb2ca2 JF |
3707 | - (pkgAcquire &) fetcher { |
3708 | return *fetcher_; | |
ec97ef06 JF |
3709 | } |
3710 | ||
a3328c28 JF |
3711 | - (pkgSourceList &) list { |
3712 | return *list_; | |
3713 | } | |
3714 | ||
36bb2ca2 | 3715 | - (NSArray *) packages { |
077e9d90 | 3716 | return (NSArray *) packages_; |
ec97ef06 JF |
3717 | } |
3718 | ||
7b0ce2da | 3719 | - (NSArray *) sources { |
34f70f5d | 3720 | return sourceList_; |
7b0ce2da JF |
3721 | } |
3722 | ||
d669236d GP |
3723 | - (Source *) sourceWithKey:(NSString *)key { |
3724 | for (Source *source in [self sources]) { | |
3725 | if ([[source key] isEqualToString:key]) | |
3726 | return source; | |
3727 | } return nil; | |
3728 | } | |
3729 | ||
670a0494 JF |
3730 | - (bool) popErrorWithTitle:(NSString *)title { |
3731 | bool fatal(false); | |
670a0494 JF |
3732 | |
3733 | while (!_error->empty()) { | |
3734 | std::string error; | |
3735 | bool warning(!_error->PopMessage(error)); | |
3736 | if (!warning) | |
3737 | fatal = true; | |
cb6e2ccf | 3738 | |
670a0494 JF |
3739 | for (;;) { |
3740 | size_t size(error.size()); | |
3741 | if (size == 0 || error[size - 1] != '\n') | |
3742 | break; | |
3743 | error.resize(size - 1); | |
3744 | } | |
cb6e2ccf | 3745 | |
670a0494 JF |
3746 | lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); |
3747 | ||
b4fd1197 | 3748 | static RegEx no_pubkey("GPG error:.* NO_PUBKEY .*"); |
c8ce71c7 JF |
3749 | if (warning && no_pubkey(error.c_str())) |
3750 | continue; | |
3751 | ||
389133be | 3752 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
670a0494 JF |
3753 | } |
3754 | ||
670a0494 JF |
3755 | return fatal; |
3756 | } | |
3757 | ||
3758 | - (bool) popErrorWithTitle:(NSString *)title forOperation:(bool)success { | |
3759 | return [self popErrorWithTitle:title] || !success; | |
3760 | } | |
3761 | ||
7bc0d825 | 3762 | - (bool) popErrorWithTitle:(NSString *)title forReadList:(pkgSourceList &)list { |
19cd1e0d JF |
3763 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3764 | return true; | |
3765 | return false; | |
3766 | ||
57cc2170 JF |
3767 | list.Reset(); |
3768 | ||
3769 | bool error(false); | |
3770 | ||
3771 | if (access("/etc/apt/sources.list", F_OK) == 0) | |
3772 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend("/etc/apt/sources.list")]; | |
3773 | ||
3774 | std::string base("/etc/apt/sources.list.d"); | |
3775 | if (DIR *sources = opendir(base.c_str())) { | |
3776 | while (dirent *source = readdir(sources)) | |
3777 | 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) | |
3778 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend((base + "/" + source->d_name).c_str())]; | |
3779 | closedir(sources); | |
3780 | } | |
3781 | ||
3782 | error |= [self popErrorWithTitle:title forOperation:list.ReadAppend(SOURCES_LIST)]; | |
3783 | ||
3784 | return error; | |
7bc0d825 JF |
3785 | } |
3786 | ||
d13edf44 | 3787 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
3dd53516 JF |
3788 | @synchronized (self) { |
3789 | ++era_; | |
a1440b10 | 3790 | |
0944377b | 3791 | [self releasePackages]; |
ab3f6a01 | 3792 | |
34f70f5d JF |
3793 | sourceMap_.clear(); |
3794 | [sourceList_ removeAllObjects]; | |
843e75b8 | 3795 | |
36bb2ca2 | 3796 | _error->Discard(); |
5a09ae08 | 3797 | |
36bb2ca2 | 3798 | delete list_; |
5a09ae08 | 3799 | list_ = NULL; |
36bb2ca2 JF |
3800 | manager_ = NULL; |
3801 | delete lock_; | |
5a09ae08 | 3802 | lock_ = NULL; |
36bb2ca2 | 3803 | delete fetcher_; |
5a09ae08 | 3804 | fetcher_ = NULL; |
36bb2ca2 | 3805 | delete resolver_; |
5a09ae08 | 3806 | resolver_ = NULL; |
36bb2ca2 | 3807 | delete records_; |
5a09ae08 JF |
3808 | records_ = NULL; |
3809 | delete policy_; | |
3810 | policy_ = NULL; | |
36bb2ca2 | 3811 | |
5a09ae08 | 3812 | cache_.Close(); |
8b29f8e6 | 3813 | |
64edd9df JF |
3814 | pool_.~CYPool(); |
3815 | new (&pool_) CYPool(); | |
a020a50e | 3816 | |
f79a4512 | 3817 | NSRecycleZone(zone_); |
a020a50e | 3818 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3819 | |
7376b55c JF |
3820 | int chk(creat("/tmp/cydia.chk", 0644)); |
3821 | if (chk != -1) | |
3822 | close(chk); | |
3823 | ||
4ba8f30a JF |
3824 | if (invocation != nil) |
3825 | [invocation invoke]; | |
3826 | ||
670a0494 JF |
3827 | NSString *title(UCLocalize("DATABASE")); |
3828 | ||
0c28a403 | 3829 | list_ = new pkgSourceList(); |
1a83afc6 | 3830 | _profile(reloadDataWithInvocation$ReadMainList) |
7bc0d825 | 3831 | if ([self popErrorWithTitle:title forReadList:*list_]) |
0c28a403 | 3832 | return; |
1a83afc6 | 3833 | _end |
0c28a403 | 3834 | |
1a83afc6 | 3835 | _profile(reloadDataWithInvocation$Source$initWithMetaIndex) |
0c28a403 | 3836 | for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { |
64edd9df | 3837 | Source *object([[[Source alloc] initWithMetaIndex:*source forDatabase:self inPool:&pool_] autorelease]); |
0c28a403 JF |
3838 | [sourceList_ addObject:object]; |
3839 | } | |
1a83afc6 | 3840 | _end |
0c28a403 | 3841 | |
9487f027 | 3842 | _trace(); |
124cea03 | 3843 | OpProgress progress; |
1a83afc6 | 3844 | bool opened; |
2b49cff9 | 3845 | open: |
5cf267f4 | 3846 | delock_ = GetStatusDate(); |
1a83afc6 | 3847 | _profile(reloadDataWithInvocation$pkgCacheFile) |
b44af625 | 3848 | opened = cache_.Open(progress, false); |
1a83afc6 JF |
3849 | _end |
3850 | if (!opened) { | |
2b49cff9 JF |
3851 | // XXX: what if there are errors, but Open() == true? this should be merged with popError: |
3852 | while (!_error->empty()) { | |
3853 | std::string error; | |
3854 | bool warning(!_error->PopMessage(error)); | |
3855 | ||
3856 | lprintf("cache_.Open():[%s]\n", error.c_str()); | |
3857 | ||
389133be | 3858 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
2b49cff9 JF |
3859 | |
3860 | SEL repair(NULL); | |
3861 | if (false); | |
3862 | else if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ") | |
3863 | repair = @selector(configure); | |
3864 | //else if (error == "The package lists or status file could not be parsed or opened.") | |
3865 | // repair = @selector(update); | |
3866 | // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)") | |
3867 | // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)") | |
3868 | // else if (error == "Malformed Status line") | |
3869 | // else if (error == "The list of sources could not be read.") | |
3870 | ||
3871 | if (repair != NULL) { | |
3872 | _error->Discard(); | |
3873 | [delegate_ repairWithSelector:repair]; | |
3874 | goto open; | |
3875 | } | |
efa53fa9 | 3876 | } |
5a09ae08 | 3877 | |
2b49cff9 | 3878 | return; |
36bb2ca2 | 3879 | } |
9487f027 | 3880 | _trace(); |
36bb2ca2 | 3881 | |
7376b55c JF |
3882 | unlink("/tmp/cydia.chk"); |
3883 | ||
31bc18a7 | 3884 | now_ = [[NSDate date] timeIntervalSince1970]; |
36bb2ca2 | 3885 | |
5a09ae08 | 3886 | policy_ = new pkgDepCache::Policy(); |
36bb2ca2 JF |
3887 | records_ = new pkgRecords(cache_); |
3888 | resolver_ = new pkgProblemResolver(cache_); | |
3889 | fetcher_ = new pkgAcquire(&status_); | |
3890 | lock_ = NULL; | |
3891 | ||
670a0494 | 3892 | if (cache_->DelCount() != 0 || cache_->InstCount() != 0) { |
389133be | 3893 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("COUNTS_NONZERO_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3894 | return; |
3895 | } | |
36bb2ca2 | 3896 | |
1a83afc6 | 3897 | _profile(reloadDataWithInvocation$pkgApplyStatus) |
670a0494 JF |
3898 | if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) |
3899 | return; | |
1a83afc6 | 3900 | _end |
9bedffaa JF |
3901 | |
3902 | if (cache_->BrokenCount() != 0) { | |
1a83afc6 | 3903 | _profile(pkgApplyStatus$pkgFixBroken) |
670a0494 JF |
3904 | if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) |
3905 | return; | |
1a83afc6 | 3906 | _end |
670a0494 JF |
3907 | |
3908 | if (cache_->BrokenCount() != 0) { | |
389133be | 3909 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("STILL_BROKEN_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3910 | return; |
3911 | } | |
3912 | ||
1a83afc6 | 3913 | _profile(pkgApplyStatus$pkgMinimizeUpgrade) |
670a0494 JF |
3914 | if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) |
3915 | return; | |
1a83afc6 | 3916 | _end |
9bedffaa JF |
3917 | } |
3918 | ||
0c28a403 JF |
3919 | for (Source *object in (id) sourceList_) { |
3920 | metaIndex *source([object metaIndex]); | |
3921 | std::vector<pkgIndexFile *> *indices = source->GetIndexFiles(); | |
68d927e2 JF |
3922 | for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index) |
3923 | // XXX: this could be more intelligent | |
3924 | if (dynamic_cast<debPackagesIndex *>(*index) != NULL) { | |
3925 | pkgCache::PkgFileIterator cached((*index)->FindInCache(cache_)); | |
34f70f5d JF |
3926 | if (!cached.end()) |
3927 | sourceMap_[cached->ID] = object; | |
68d927e2 | 3928 | } |
36bb2ca2 | 3929 | } |
68d927e2 | 3930 | |
677b8415 | 3931 | { |
9fcbca29 | 3932 | /*std::vector<Package *> packages; |
677b8415 | 3933 | packages.reserve(std::max(10000U, [packages_ count] + 1000)); |
9fcbca29 JF |
3934 | packages_ = nil;*/ |
3935 | ||
1a83afc6 | 3936 | _profile(reloadDataWithInvocation$packageWithIterator) |
677b8415 | 3937 | for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) |
64edd9df | 3938 | if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:&pool_ database:self]) |
9fcbca29 | 3939 | //packages.push_back(package); |
7b33d201 | 3940 | CFArrayAppendValue(packages_, CFRetain(package)); |
1a83afc6 | 3941 | _end |
677b8415 | 3942 | |
677b8415 | 3943 | |
9fcbca29 | 3944 | /*if (packages.empty()) |
677b8415 JF |
3945 | packages_ = [[NSArray alloc] init]; |
3946 | else | |
3947 | packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; | |
9fcbca29 JF |
3948 | _trace();*/ |
3949 | ||
1a83afc6 | 3950 | _profile(reloadDataWithInvocation$radix$8) |
9f0facbc | 3951 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(8)]; |
1a83afc6 JF |
3952 | _end |
3953 | ||
3954 | _profile(reloadDataWithInvocation$radix$4) | |
de42680b | 3955 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(4)]; |
1a83afc6 | 3956 | _end |
9fcbca29 | 3957 | |
1a83afc6 JF |
3958 | _profile(reloadDataWithInvocation$radix$0) |
3959 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(0)]; | |
3960 | _end | |
9fcbca29 | 3961 | |
1a83afc6 JF |
3962 | _profile(reloadDataWithInvocation$insertion) |
3963 | CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); | |
3964 | _end | |
9fcbca29 | 3965 | |
1a83afc6 JF |
3966 | /*_profile(reloadDataWithInvocation$CFQSortArray) |
3967 | CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare_), NULL); | |
3968 | _end*/ | |
9fcbca29 | 3969 | |
1a83afc6 JF |
3970 | /*_profile(reloadDataWithInvocation$stdsort) |
3971 | std::sort(packages.begin(), packages.end(), PackageNameOrdering()); | |
3972 | _end*/ | |
eef4ccaf | 3973 | |
1a83afc6 JF |
3974 | /*_profile(reloadDataWithInvocation$CFArraySortValues) |
3975 | CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); | |
3976 | _end*/ | |
9fcbca29 | 3977 | |
1a83afc6 JF |
3978 | /*_profile(reloadDataWithInvocation$sortUsingFunction) |
3979 | [packages_ sortUsingFunction:reinterpret_cast<NSComparisonResult (*)(id, id, void *)>(&PackageNameCompare) context:NULL]; | |
3980 | _end*/ | |
677b8415 | 3981 | |
cd95e390 JF |
3982 | |
3983 | size_t count(CFArrayGetCount(packages_)); | |
9f357d11 | 3984 | MetaFile_->active_ = count; |
cd95e390 JF |
3985 | for (size_t index(0); index != count; ++index) |
3986 | [(Package *) CFArrayGetValueAtIndex(packages_, index) setIndex:index]; | |
677b8415 | 3987 | } |
d13edf44 | 3988 | } } |
ec97ef06 | 3989 | |
c6ca67ba JF |
3990 | - (void) clear { |
3991 | @synchronized (self) { | |
3992 | delete resolver_; | |
3993 | resolver_ = new pkgProblemResolver(cache_); | |
3994 | ||
50c1653e JF |
3995 | for (pkgCache::PkgIterator iterator(cache_->PkgBegin()); !iterator.end(); ++iterator) |
3996 | if (!cache_[iterator].Keep()) | |
c6ca67ba | 3997 | cache_->MarkKeep(iterator, false); |
50c1653e | 3998 | else if ((cache_[iterator].iFlags & pkgDepCache::ReInstall) != 0) |
c6ca67ba | 3999 | cache_->SetReInstall(iterator, false); |
c6ca67ba JF |
4000 | } } |
4001 | ||
5a09ae08 | 4002 | - (void) configure { |
232b396b | 4003 | NSString *dpkg = [NSString stringWithFormat:@"/usr/libexec/cydo --configure -a --status-fd %u", statusfd_]; |
985d2dff | 4004 | _trace(); |
232b396b | 4005 | system([dpkg UTF8String]); |
985d2dff | 4006 | _trace(); |
5a09ae08 JF |
4007 | } |
4008 | ||
670a0494 | 4009 | - (bool) clean { |
0517651f | 4010 | @synchronized (self) { |
670a0494 | 4011 | // XXX: I don't remember this condition |
77fcccaf | 4012 | if (lock_ != NULL) |
670a0494 | 4013 | return false; |
77fcccaf JF |
4014 | |
4015 | FileFd Lock; | |
4016 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
4017 | |
4018 | NSString *title(UCLocalize("CLEAN_ARCHIVES")); | |
4019 | ||
4020 | if ([self popErrorWithTitle:title]) | |
4021 | return false; | |
77fcccaf JF |
4022 | |
4023 | pkgAcquire fetcher; | |
4024 | fetcher.Clean(_config->FindDir("Dir::Cache::Archives")); | |
4025 | ||
9f9ae81c | 4026 | CydiaLogCleaner cleaner; |
670a0494 JF |
4027 | if ([self popErrorWithTitle:title forOperation:cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)]) |
4028 | return false; | |
4029 | ||
4030 | return true; | |
0517651f | 4031 | } } |
77fcccaf | 4032 | |
670a0494 | 4033 | - (bool) prepare { |
744f398e JF |
4034 | fetcher_->Shutdown(); |
4035 | ||
36bb2ca2 JF |
4036 | pkgRecords records(cache_); |
4037 | ||
4038 | lock_ = new FileFd(); | |
4039 | lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
4040 | |
4041 | NSString *title(UCLocalize("PREPARE_ARCHIVES")); | |
4042 | ||
4043 | if ([self popErrorWithTitle:title]) | |
4044 | return false; | |
36bb2ca2 JF |
4045 | |
4046 | pkgSourceList list; | |
7bc0d825 | 4047 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4048 | return false; |
36bb2ca2 JF |
4049 | |
4050 | manager_ = (_system->CreatePM(cache_)); | |
670a0494 JF |
4051 | if ([self popErrorWithTitle:title forOperation:manager_->GetArchives(fetcher_, &list, &records)]) |
4052 | return false; | |
4053 | ||
4054 | return true; | |
ec97ef06 JF |
4055 | } |
4056 | ||
36bb2ca2 | 4057 | - (void) perform { |
be860cc8 JF |
4058 | bool substrate(RestartSubstrate_); |
4059 | RestartSubstrate_ = false; | |
4060 | ||
670a0494 JF |
4061 | NSString *title(UCLocalize("PERFORM_SELECTIONS")); |
4062 | ||
a72074b2 JF |
4063 | NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { |
4064 | pkgSourceList list; | |
7bc0d825 | 4065 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4066 | return; |
a72074b2 JF |
4067 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
4068 | [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
4069 | } | |
a0be02eb | 4070 | |
dcaecde2 | 4071 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
7ffd70fd | 4072 | |
eeb9b112 JF |
4073 | if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) { |
4074 | _trace(); | |
6e90508f | 4075 | [self popErrorWithTitle:title]; |
36bb2ca2 | 4076 | return; |
eeb9b112 JF |
4077 | } |
4078 | ||
4079 | bool failed = false; | |
4080 | for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) { | |
4081 | if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete) | |
4082 | continue; | |
6204f56a JF |
4083 | if ((*item)->Status == pkgAcquire::Item::StatIdle) |
4084 | continue; | |
eeb9b112 | 4085 | |
680a3c3c JF |
4086 | std::string uri = (*item)->DescURI(); |
4087 | std::string error = (*item)->ErrorText; | |
4088 | ||
4089 | lprintf("pAf:%s:%s\n", uri.c_str(), error.c_str()); | |
eeb9b112 | 4090 | failed = true; |
680a3c3c JF |
4091 | |
4092 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError]); | |
4093 | [delegate_ addProgressEventOnMainThread:event forTask:title]; | |
eeb9b112 JF |
4094 | } |
4095 | ||
dcaecde2 | 4096 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
54043703 | 4097 | |
eeb9b112 JF |
4098 | if (failed) { |
4099 | _trace(); | |
4100 | return; | |
4101 | } | |
36bb2ca2 | 4102 | |
be860cc8 JF |
4103 | if (substrate) |
4104 | RestartSubstrate_ = true; | |
4105 | ||
f26c90b1 JF |
4106 | if (![delock_ isEqual:GetStatusDate()]) { |
4107 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("DPKG_LOCKED") ofType:kCydiaProgressEventTypeError] forTask:title]; | |
4108 | return; | |
4109 | } | |
4110 | ||
4111 | delock_ = nil; | |
4112 | ||
d78cda50 JF |
4113 | pkgPackageManager::OrderResult result(manager_->DoInstall(statusfd_)); |
4114 | ||
49cc457a JF |
4115 | NSString *oextended(@"/var/lib/apt/extended_states"); |
4116 | NSString *nextended(Cache("extended_states")); | |
d78cda50 JF |
4117 | |
4118 | struct stat info; | |
4119 | if (stat([nextended UTF8String], &info) != -1 && (info.st_mode & S_IFMT) == S_IFREG) { | |
d8b0f6e3 JF |
4120 | system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/mv -f %@ %@", ShellEscape(nextended), ShellEscape(oextended)] UTF8String]); |
4121 | system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/chown 0:0 %@", ShellEscape(oextended)] UTF8String]); | |
d78cda50 JF |
4122 | } |
4123 | ||
49cc457a JF |
4124 | unlink([nextended UTF8String]); |
4125 | symlink([oextended UTF8String], [nextended UTF8String]); | |
4126 | ||
471683a3 | 4127 | if ([self popErrorWithTitle:title]) |
36bb2ca2 | 4128 | return; |
eeb9b112 JF |
4129 | |
4130 | if (result == pkgPackageManager::Failed) { | |
4131 | _trace(); | |
36bb2ca2 | 4132 | return; |
eeb9b112 JF |
4133 | } |
4134 | ||
4135 | if (result != pkgPackageManager::Completed) { | |
4136 | _trace(); | |
36bb2ca2 | 4137 | return; |
eeb9b112 | 4138 | } |
a0be02eb | 4139 | |
a72074b2 JF |
4140 | NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { |
4141 | pkgSourceList list; | |
7bc0d825 | 4142 | if ([self popErrorWithTitle:title forReadList:list]) |
670a0494 | 4143 | return; |
a72074b2 JF |
4144 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
4145 | [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
4146 | } | |
4147 | ||
4148 | if (![before isEqualToArray:after]) | |
4149 | [self update]; | |
ec97ef06 JF |
4150 | } |
4151 | ||
f26c90b1 JF |
4152 | - (bool) delocked { |
4153 | return ![delock_ isEqual:GetStatusDate()]; | |
4154 | } | |
4155 | ||
670a0494 JF |
4156 | - (bool) upgrade { |
4157 | NSString *title(UCLocalize("UPGRADE")); | |
4158 | if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) | |
4159 | return false; | |
4160 | return true; | |
c7c6384e JF |
4161 | } |
4162 | ||
36bb2ca2 JF |
4163 | - (void) update { |
4164 | [self updateWithStatus:status_]; | |
4165 | } | |
b4d89997 | 4166 | |
21ac0ce2 | 4167 | - (void) updateWithStatus:(CancelStatus &)status { |
670a0494 JF |
4168 | NSString *title(UCLocalize("REFRESHING_DATA")); |
4169 | ||
36bb2ca2 | 4170 | pkgSourceList list; |
7bc0d825 | 4171 | if ([self popErrorWithTitle:title forReadList:list]) |
53ca7fdd | 4172 | return; |
b4d89997 | 4173 | |
36bb2ca2 JF |
4174 | FileFd lock; |
4175 | lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
670a0494 JF |
4176 | if ([self popErrorWithTitle:title]) |
4177 | return; | |
18873623 | 4178 | |
aaae308d JF |
4179 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
4180 | ||
fc470c15 JF |
4181 | bool success(ListUpdate(status, list, PulseInterval_)); |
4182 | if (status.WasCancelled()) | |
4183 | _error->Discard(); | |
aa42c612 | 4184 | else { |
fc470c15 | 4185 | [self popErrorWithTitle:title forOperation:success]; |
6f87c61a JF |
4186 | |
4187 | [[NSDictionary dictionaryWithObjectsAndKeys: | |
4188 | [NSDate date], @"LastUpdate", | |
4189 | nil] writeToFile:@ CacheState_ atomically:YES]; | |
aa42c612 | 4190 | } |
36bb2ca2 | 4191 | |
aaae308d | 4192 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
b4d89997 JF |
4193 | } |
4194 | ||
6915b806 | 4195 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate { |
36bb2ca2 | 4196 | delegate_ = delegate; |
6915b806 JF |
4197 | } |
4198 | ||
4199 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate { | |
4200 | progress_ = delegate; | |
36bb2ca2 | 4201 | status_.setDelegate(delegate); |
36bb2ca2 | 4202 | } |
b4d89997 | 4203 | |
6915b806 JF |
4204 | - (NSObject<ProgressDelegate> *) progressDelegate { |
4205 | return progress_; | |
4206 | } | |
4207 | ||
7376b55c | 4208 | - (Source *) getSource:(pkgCache::PkgFileIterator)file { |
34f70f5d JF |
4209 | SourceMap::const_iterator i(sourceMap_.find(file->ID)); |
4210 | return i == sourceMap_.end() ? nil : i->second; | |
b19871dd JF |
4211 | } |
4212 | ||
21ac0ce2 JF |
4213 | - (void) setFetch:(bool)fetch forURI:(const char *)uri { |
4214 | for (Source *source in (id) sourceList_) | |
4215 | [source setFetch:fetch forURI:uri]; | |
4216 | } | |
4217 | ||
9ed626f1 JF |
4218 | - (void) resetFetch { |
4219 | for (Source *source in (id) sourceList_) | |
4220 | [source resetFetch]; | |
4221 | } | |
4222 | ||
fe33a23e | 4223 | - (NSString *) mappedSectionForPointer:(const char *)section { |
4905df00 | 4224 | _H<NSString> *mapped; |
fe33a23e | 4225 | |
4905df00 JF |
4226 | _profile(Database$mappedSectionForPointer$Cache) |
4227 | mapped = §ions_[section]; | |
4228 | _end | |
4229 | ||
4230 | if (*mapped == NULL) { | |
fe33a23e JF |
4231 | size_t length(strlen(section)); |
4232 | char spaced[length + 1]; | |
4233 | ||
4234 | _profile(Database$mappedSectionForPointer$Replace) | |
4235 | for (size_t index(0); index != length; ++index) | |
4236 | spaced[index] = section[index] == '_' ? ' ' : section[index]; | |
4237 | spaced[length] = '\0'; | |
4238 | _end | |
4239 | ||
4240 | NSString *string; | |
4241 | ||
4242 | _profile(Database$mappedSectionForPointer$stringWithUTF8String) | |
4243 | string = [NSString stringWithUTF8String:spaced]; | |
4244 | _end | |
4245 | ||
4246 | _profile(Database$mappedSectionForPointer$Map) | |
4905df00 | 4247 | string = [SectionMap_ objectForKey:string] ?: string; |
fe33a23e | 4248 | _end |
4905df00 JF |
4249 | |
4250 | *mapped = string; | |
4251 | } return *mapped; | |
fe33a23e JF |
4252 | } |
4253 | ||
36bb2ca2 JF |
4254 | @end |
4255 | /* }}} */ | |
b4d89997 | 4256 | |
7b33d201 | 4257 | static _H<NSMutableSet> Diversions_; |
7256476b | 4258 | |
775deead | 4259 | @interface Diversion : NSObject { |
b4fd1197 | 4260 | RegEx pattern_; |
775deead JF |
4261 | _H<NSString> key_; |
4262 | _H<NSString> format_; | |
4263 | } | |
4264 | ||
4265 | @end | |
4266 | ||
4267 | @implementation Diversion | |
4268 | ||
4269 | - (id) initWithFrom:(NSString *)from to:(NSString *)to { | |
4270 | if ((self = [super init]) != nil) { | |
4271 | pattern_ = [from UTF8String]; | |
4272 | key_ = from; | |
4273 | format_ = to; | |
4274 | } return self; | |
4275 | } | |
4276 | ||
4277 | - (NSString *) divert:(NSString *)url { | |
8ea598c0 | 4278 | return !pattern_(url) ? nil : pattern_->*format_; |
775deead JF |
4279 | } |
4280 | ||
7256476b JF |
4281 | + (NSURL *) divertURL:(NSURL *)url { |
4282 | divert: | |
4283 | NSString *href([url absoluteString]); | |
4284 | ||
7b33d201 | 4285 | for (Diversion *diversion in (id) Diversions_) |
7256476b | 4286 | if (NSString *diverted = [diversion divert:href]) { |
85d5d452 JF |
4287 | #if !ForRelease |
4288 | NSLog(@"div: %@", diverted); | |
4289 | #endif | |
7256476b JF |
4290 | url = [NSURL URLWithString:diverted]; |
4291 | goto divert; | |
4292 | } | |
4293 | ||
4294 | return url; | |
4295 | } | |
4296 | ||
775deead JF |
4297 | - (NSString *) key { |
4298 | return key_; | |
4299 | } | |
4300 | ||
4301 | - (NSUInteger) hash { | |
4302 | return [key_ hash]; | |
4303 | } | |
4304 | ||
4305 | - (BOOL) isEqual:(Diversion *)object { | |
4306 | return self == object || [self class] == [object class] && [key_ isEqual:[object key]]; | |
4307 | } | |
4308 | ||
4309 | @end | |
4310 | ||
43f3d7f6 | 4311 | @interface CydiaObject : NSObject { |
4cc9e99a | 4312 | _H<CyteWebViewController> indirect_; |
3931b718 | 4313 | _transient id delegate_; |
43f3d7f6 | 4314 | } |
c390d3ab | 4315 | |
43f3d7f6 | 4316 | - (id) initWithDelegate:(IndirectDelegate *)indirect; |
6840bff3 | 4317 | |
c390d3ab JF |
4318 | @end |
4319 | ||
9e130bc2 JF |
4320 | @class CydiaObject; |
4321 | ||
09e89a8a | 4322 | @interface CydiaWebViewController : CyteWebViewController { |
7b33d201 | 4323 | _H<CydiaObject> cydia_; |
775deead JF |
4324 | } |
4325 | ||
4326 | + (void) addDiversion:(Diversion *)diversion; | |
85ae5f42 | 4327 | + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request; |
9e130bc2 JF |
4328 | + (void) didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame withCydia:(CydiaObject *)cydia; |
4329 | - (void) setDelegate:(id)delegate; | |
775deead JF |
4330 | |
4331 | @end | |
4332 | ||
775deead | 4333 | /* Web Scripting {{{ */ |
43f3d7f6 | 4334 | @implementation CydiaObject |
c390d3ab | 4335 | |
43f3d7f6 JF |
4336 | - (id) initWithDelegate:(IndirectDelegate *)indirect { |
4337 | if ((self = [super init]) != nil) { | |
4cc9e99a | 4338 | indirect_ = (CyteWebViewController *) indirect; |
43f3d7f6 JF |
4339 | } return self; |
4340 | } | |
4341 | ||
77801ff1 JF |
4342 | - (void) setDelegate:(id)delegate { |
4343 | delegate_ = delegate; | |
4344 | } | |
4345 | ||
43f3d7f6 | 4346 | + (NSArray *) _attributeKeys { |
e58ff941 | 4347 | return [NSArray arrayWithObjects: |
6ffdaae3 | 4348 | @"bbsnum", |
e967efd5 JF |
4349 | @"build", |
4350 | @"coreFoundationVersionNumber", | |
e58ff941 | 4351 | @"device", |
56296da0 | 4352 | @"ecid", |
93d6d318 JF |
4353 | @"firmware", |
4354 | @"hostname", | |
4355 | @"idiom", | |
f87cac81 JF |
4356 | @"mcc", |
4357 | @"mnc", | |
56296da0 | 4358 | @"model", |
2656c992 | 4359 | @"operator", |
56296da0 | 4360 | @"role", |
e58ff941 | 4361 | @"serial", |
bf1d5e69 | 4362 | @"version", |
e58ff941 | 4363 | nil]; |
43f3d7f6 JF |
4364 | } |
4365 | ||
4366 | - (NSArray *) attributeKeys { | |
4367 | return [[self class] _attributeKeys]; | |
c390d3ab JF |
4368 | } |
4369 | ||
43f3d7f6 JF |
4370 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
4371 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
c390d3ab | 4372 | } |
43f3d7f6 | 4373 | |
bf1d5e69 | 4374 | - (NSString *) version { |
3d45bad1 | 4375 | return Cydia_; |
bf1d5e69 JF |
4376 | } |
4377 | ||
e967efd5 JF |
4378 | - (NSString *) build { |
4379 | return System_; | |
4380 | } | |
4381 | ||
4382 | - (NSString *) coreFoundationVersionNumber { | |
4383 | return [NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber]; | |
4384 | } | |
4385 | ||
43f3d7f6 | 4386 | - (NSString *) device { |
7c80833f | 4387 | return UniqueIdentifier(); |
c390d3ab JF |
4388 | } |
4389 | ||
93d6d318 JF |
4390 | - (NSString *) firmware { |
4391 | return [[UIDevice currentDevice] systemVersion]; | |
4392 | } | |
4393 | ||
4394 | - (NSString *) hostname { | |
4395 | return [[UIDevice currentDevice] name]; | |
4396 | } | |
4397 | ||
4398 | - (NSString *) idiom { | |
c138614d | 4399 | return (id) Idiom_ ?: [NSNull null]; |
93d6d318 JF |
4400 | } |
4401 | ||
f87cac81 JF |
4402 | - (NSString *) mcc { |
4403 | if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode"))) | |
4404 | return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault) autorelease]; | |
4405 | return nil; | |
4406 | } | |
4407 | ||
4408 | - (NSString *) mnc { | |
4409 | if (CFStringRef (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberNetworkCode"))) | |
4410 | return [(NSString *) (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(kCFAllocatorDefault) autorelease]; | |
4411 | return nil; | |
56296da0 JF |
4412 | } |
4413 | ||
2656c992 JF |
4414 | - (NSString *) operator { |
4415 | if (CFStringRef (*$CTRegistrationCopyOperatorName)(CFAllocatorRef) = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTRegistrationCopyOperatorName"))) | |
4416 | return [(NSString *) (*$CTRegistrationCopyOperatorName)(kCFAllocatorDefault) autorelease]; | |
4417 | return nil; | |
4418 | } | |
4419 | ||
6ffdaae3 JF |
4420 | - (NSString *) bbsnum { |
4421 | return (id) BBSNum_ ?: [NSNull null]; | |
4422 | } | |
4423 | ||
56296da0 | 4424 | - (NSString *) ecid { |
849cd6bf | 4425 | return (id) ChipID_ ?: [NSNull null]; |
affeffc7 JF |
4426 | } |
4427 | ||
43f3d7f6 | 4428 | - (NSString *) serial { |
56296da0 | 4429 | return SerialNumber_; |
43f3d7f6 | 4430 | } |
86316a91 | 4431 | |
56296da0 | 4432 | - (NSString *) role { |
4121c5e0 | 4433 | return (id) [NSNull null]; |
affeffc7 JF |
4434 | } |
4435 | ||
56296da0 JF |
4436 | - (NSString *) model { |
4437 | return [NSString stringWithUTF8String:Machine_]; | |
43f3d7f6 | 4438 | } |
43f3d7f6 JF |
4439 | |
4440 | + (NSString *) webScriptNameForSelector:(SEL)selector { | |
e58ff941 | 4441 | if (false); |
cfc7b442 JF |
4442 | else if (selector == @selector(addBridgedHost:)) |
4443 | return @"addBridgedHost"; | |
2e1652a9 JF |
4444 | else if (selector == @selector(addInsecureHost:)) |
4445 | return @"addInsecureHost"; | |
3f428e4e JF |
4446 | else if (selector == @selector(addInternalRedirect::)) |
4447 | return @"addInternalRedirect"; | |
e4b48f2f | 4448 | else if (selector == @selector(addPipelinedHost:scheme:)) |
834c18a4 | 4449 | return @"addPipelinedHost"; |
33e30380 JF |
4450 | else if (selector == @selector(addSource:::)) |
4451 | return @"addSource"; | |
b088c0cd JF |
4452 | else if (selector == @selector(addTrivialSource:)) |
4453 | return @"addTrivialSource"; | |
e58ff941 | 4454 | else if (selector == @selector(close)) |
43f3d7f6 | 4455 | return @"close"; |
e58ff941 JF |
4456 | else if (selector == @selector(du:)) |
4457 | return @"du"; | |
4458 | else if (selector == @selector(stringWithFormat:arguments:)) | |
4459 | return @"format"; | |
5c32f89e | 4460 | else if (selector == @selector(getAllSources)) |
caf0475e | 4461 | return @"getAllSources"; |
8c5b623f JF |
4462 | else if (selector == @selector(getApplicationInfo:value:)) |
4463 | return @"getApplicationInfoValue"; | |
375a4481 JF |
4464 | else if (selector == @selector(getDisplayIdentifiers)) |
4465 | return @"getDisplayIdentifiers"; | |
4466 | else if (selector == @selector(getLocalizedNameForDisplayIdentifier:)) | |
4467 | return @"getLocalizedNameForDisplayIdentifier"; | |
5bc1277a JF |
4468 | else if (selector == @selector(getKernelNumber:)) |
4469 | return @"getKernelNumber"; | |
4470 | else if (selector == @selector(getKernelString:)) | |
4471 | return @"getKernelString"; | |
8cc8eb1c JF |
4472 | else if (selector == @selector(getInstalledPackages)) |
4473 | return @"getInstalledPackages"; | |
c31d7cdc JF |
4474 | else if (selector == @selector(getIORegistryEntry::)) |
4475 | return @"getIORegistryEntry"; | |
948db680 JF |
4476 | else if (selector == @selector(getLocaleIdentifier)) |
4477 | return @"getLocaleIdentifier"; | |
4478 | else if (selector == @selector(getPreferredLanguages)) | |
4479 | return @"getPreferredLanguages"; | |
43f3d7f6 JF |
4480 | else if (selector == @selector(getPackageById:)) |
4481 | return @"getPackageById"; | |
37fa9338 JF |
4482 | else if (selector == @selector(getMetadataKeys)) |
4483 | return @"getMetadataKeys"; | |
b3c8e69c JF |
4484 | else if (selector == @selector(getMetadataValue:)) |
4485 | return @"getMetadataValue"; | |
ef974f52 JF |
4486 | else if (selector == @selector(getSessionValue:)) |
4487 | return @"getSessionValue"; | |
77801ff1 JF |
4488 | else if (selector == @selector(installPackages:)) |
4489 | return @"installPackages"; | |
518a552a JF |
4490 | else if (selector == @selector(isReachable:)) |
4491 | return @"isReachable"; | |
e58ff941 JF |
4492 | else if (selector == @selector(localizedStringForKey:value:table:)) |
4493 | return @"localize"; | |
8d497e2a JF |
4494 | else if (selector == @selector(popViewController:)) |
4495 | return @"popViewController"; | |
b088c0cd JF |
4496 | else if (selector == @selector(refreshSources)) |
4497 | return @"refreshSources"; | |
aa1e1906 JF |
4498 | else if (selector == @selector(registerFrame:)) |
4499 | return @"registerFrame"; | |
ed5566c7 JF |
4500 | else if (selector == @selector(removeButton)) |
4501 | return @"removeButton"; | |
33e30380 JF |
4502 | else if (selector == @selector(saveConfig)) |
4503 | return @"saveConfig"; | |
b3c8e69c JF |
4504 | else if (selector == @selector(setMetadataValue::)) |
4505 | return @"setMetadataValue"; | |
ef974f52 JF |
4506 | else if (selector == @selector(setSessionValue::)) |
4507 | return @"setSessionValue"; | |
8a126074 JF |
4508 | else if (selector == @selector(substitutePackageNames:)) |
4509 | return @"substitutePackageNames"; | |
8e3b68d4 JF |
4510 | else if (selector == @selector(scrollToBottom:)) |
4511 | return @"scrollToBottom"; | |
8366df5e JF |
4512 | else if (selector == @selector(setAllowsNavigationAction:)) |
4513 | return @"setAllowsNavigationAction"; | |
c31c825d JF |
4514 | else if (selector == @selector(setBadgeValue:)) |
4515 | return @"setBadgeValue"; | |
43f3d7f6 JF |
4516 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) |
4517 | return @"setButtonImage"; | |
4518 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
4519 | return @"setButtonTitle"; | |
b8a5d89d JF |
4520 | else if (selector == @selector(setHidesBackButton:)) |
4521 | return @"setHidesBackButton"; | |
5cdfcd6f JF |
4522 | else if (selector == @selector(setHidesNavigationBar:)) |
4523 | return @"setHidesNavigationBar"; | |
82406217 JF |
4524 | else if (selector == @selector(setNavigationBarStyle:)) |
4525 | return @"setNavigationBarStyle"; | |
00984204 JF |
4526 | else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:)) |
4527 | return @"setNavigationBarTintColor"; | |
36a20e14 JF |
4528 | else if (selector == @selector(setPasteboardString:)) |
4529 | return @"setPasteboardString"; | |
4530 | else if (selector == @selector(setPasteboardURL:)) | |
4531 | return @"setPasteboardURL"; | |
db698f42 JF |
4532 | else if (selector == @selector(setScrollAlwaysBounceVertical:)) |
4533 | return @"setScrollAlwaysBounceVertical"; | |
4886cc81 JF |
4534 | else if (selector == @selector(setScrollIndicatorStyle:)) |
4535 | return @"setScrollIndicatorStyle"; | |
ef055c6c JF |
4536 | else if (selector == @selector(setToken:)) |
4537 | return @"setToken"; | |
43f3d7f6 JF |
4538 | else if (selector == @selector(setViewportWidth:)) |
4539 | return @"setViewportWidth"; | |
43f3d7f6 JF |
4540 | else if (selector == @selector(statfs:)) |
4541 | return @"statfs"; | |
e58ff941 JF |
4542 | else if (selector == @selector(supports:)) |
4543 | return @"supports"; | |
7c218781 JF |
4544 | else if (selector == @selector(unload)) |
4545 | return @"unload"; | |
c390d3ab | 4546 | else |
43f3d7f6 JF |
4547 | return nil; |
4548 | } | |
4549 | ||
4550 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
4551 | return [self webScriptNameForSelector:selector] == nil; | |
c390d3ab JF |
4552 | } |
4553 | ||
43f3d7f6 JF |
4554 | - (BOOL) supports:(NSString *)feature { |
4555 | return [feature isEqualToString:@"window.open"]; | |
4556 | } | |
c390d3ab | 4557 | |
7c218781 JF |
4558 | - (void) unload { |
4559 | [delegate_ performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO]; | |
4560 | } | |
4561 | ||
db698f42 JF |
4562 | - (void) setScrollAlwaysBounceVertical:(NSNumber *)value { |
4563 | [indirect_ performSelectorOnMainThread:@selector(setScrollAlwaysBounceVerticalNumber:) withObject:value waitUntilDone:NO]; | |
4564 | } | |
4565 | ||
4886cc81 JF |
4566 | - (void) setScrollIndicatorStyle:(NSString *)style { |
4567 | [indirect_ performSelectorOnMainThread:@selector(setScrollIndicatorStyleWithName:) withObject:style waitUntilDone:NO]; | |
4568 | } | |
4569 | ||
3f428e4e | 4570 | - (void) addInternalRedirect:(NSString *)from :(NSString *)to { |
a576488f | 4571 | [CydiaWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO]; |
775deead JF |
4572 | } |
4573 | ||
8c5b623f JF |
4574 | - (NSDictionary *) getApplicationInfo:(NSString *)display value:(NSString *)key { |
4575 | char path[1024]; | |
4576 | if (SBBundlePathForDisplayIdentifier(SBSSpringBoardServerPort(), [display UTF8String], path) != 0) | |
4577 | return (id) [NSNull null]; | |
4578 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:[[NSString stringWithUTF8String:path] stringByAppendingString:@"/Info.plist"]]); | |
4579 | if (info == nil) | |
4580 | return (id) [NSNull null]; | |
4581 | return [info objectForKey:key]; | |
4582 | } | |
4583 | ||
375a4481 JF |
4584 | - (NSArray *) getDisplayIdentifiers { |
4585 | NSSet *set([SBSCopyDisplayIdentifiers() autorelease]); | |
4586 | if (set == nil || ![set isKindOfClass:[NSSet class]]) | |
4587 | return [NSArray array]; | |
4588 | return [set allObjects]; | |
4589 | } | |
4590 | ||
4591 | - (NSString *) getLocalizedNameForDisplayIdentifier:(NSString *)identifier { | |
4592 | return [SBSCopyLocalizedApplicationNameForDisplayIdentifier(identifier) autorelease] ?: (id) [NSNull null]; | |
4593 | } | |
4594 | ||
5bc1277a JF |
4595 | - (NSNumber *) getKernelNumber:(NSString *)name { |
4596 | const char *string([name UTF8String]); | |
4597 | ||
4598 | size_t size; | |
4599 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
4600 | return (id) [NSNull null]; | |
4601 | ||
4602 | if (size != sizeof(int)) | |
4603 | return (id) [NSNull null]; | |
4604 | ||
4605 | int value; | |
4606 | if (sysctlbyname(string, &value, &size, NULL, 0) == -1) | |
4607 | return (id) [NSNull null]; | |
4608 | ||
4609 | return [NSNumber numberWithInt:value]; | |
4610 | } | |
4611 | ||
4612 | - (NSString *) getKernelString:(NSString *)name { | |
4613 | const char *string([name UTF8String]); | |
4614 | ||
4615 | size_t size; | |
4616 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
4617 | return (id) [NSNull null]; | |
4618 | ||
4619 | char value[size + 1]; | |
4620 | if (sysctlbyname(string, value, &size, NULL, 0) == -1) | |
4621 | return (id) [NSNull null]; | |
4622 | ||
4623 | // XXX: just in case you request something ludicrous | |
4624 | value[size] = '\0'; | |
4625 | ||
4626 | return [NSString stringWithCString:value]; | |
4627 | } | |
4628 | ||
c31d7cdc JF |
4629 | - (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry { |
4630 | NSObject *value(CYIOGetValue([path UTF8String], entry)); | |
4631 | ||
4632 | if (value != nil) | |
4633 | if ([value isKindOfClass:[NSData class]]) | |
4634 | value = CYHex((NSData *) value); | |
4635 | ||
4636 | return value; | |
4637 | } | |
4638 | ||
37fa9338 JF |
4639 | - (NSArray *) getMetadataKeys { |
4640 | @synchronized (Values_) { | |
4641 | return [Values_ allKeys]; | |
4642 | } } | |
4643 | ||
aa1e1906 JF |
4644 | - (void) registerFrame:(DOMHTMLIFrameElement *)iframe { |
4645 | WebFrame *frame([iframe contentFrame]); | |
4646 | [indirect_ registerFrame:frame]; | |
4647 | } | |
4648 | ||
b3c8e69c JF |
4649 | - (id) getMetadataValue:(NSString *)key { |
4650 | @synchronized (Values_) { | |
4651 | return [Values_ objectForKey:key]; | |
4652 | } } | |
4653 | ||
4654 | - (void) setMetadataValue:(NSString *)key :(NSString *)value { | |
4655 | @synchronized (Values_) { | |
33e30380 | 4656 | if (value == nil || value == (id) [WebUndefined undefined] || value == (id) [NSNull null]) |
b3c8e69c JF |
4657 | [Values_ removeObjectForKey:key]; |
4658 | else | |
4659 | [Values_ setObject:value forKey:key]; | |
b3c8e69c JF |
4660 | } } |
4661 | ||
ef974f52 JF |
4662 | - (id) getSessionValue:(NSString *)key { |
4663 | @synchronized (SessionData_) { | |
4664 | return [SessionData_ objectForKey:key]; | |
4665 | } } | |
4666 | ||
4667 | - (void) setSessionValue:(NSString *)key :(NSString *)value { | |
4668 | @synchronized (SessionData_) { | |
4669 | if (value == (id) [WebUndefined undefined]) | |
4670 | [SessionData_ removeObjectForKey:key]; | |
4671 | else | |
4672 | [SessionData_ setObject:value forKey:key]; | |
4673 | } } | |
4674 | ||
cfc7b442 | 4675 | - (void) addBridgedHost:(NSString *)host { |
48f1762f JF |
4676 | @synchronized (HostConfig_) { |
4677 | [BridgedHosts_ addObject:host]; | |
4678 | } } | |
5df7ecfb | 4679 | |
2e1652a9 JF |
4680 | - (void) addInsecureHost:(NSString *)host { |
4681 | @synchronized (HostConfig_) { | |
4682 | [InsecureHosts_ addObject:host]; | |
4683 | } } | |
4684 | ||
e4b48f2f | 4685 | - (void) addPipelinedHost:(NSString *)host scheme:(NSString *)scheme { |
48f1762f | 4686 | @synchronized (HostConfig_) { |
e4b48f2f JF |
4687 | if (scheme != (id) [WebUndefined undefined]) |
4688 | host = [NSString stringWithFormat:@"%@:%@", [scheme lowercaseString], host]; | |
4689 | ||
48f1762f JF |
4690 | [PipelinedHosts_ addObject:host]; |
4691 | } } | |
834c18a4 | 4692 | |
8d497e2a JF |
4693 | - (void) popViewController:(NSNumber *)value { |
4694 | if (value == (id) [WebUndefined undefined]) | |
4695 | value = [NSNumber numberWithBool:YES]; | |
4696 | [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO]; | |
4697 | } | |
4698 | ||
33e30380 JF |
4699 | - (void) addSource:(NSString *)href :(NSString *)distribution :(WebScriptObject *)sections { |
4700 | NSMutableArray *array([NSMutableArray arrayWithCapacity:[sections count]]); | |
4701 | ||
4702 | for (NSString *section in sections) | |
4703 | [array addObject:section]; | |
4704 | ||
4705 | [delegate_ performSelectorOnMainThread:@selector(addSource:) withObject:[NSMutableDictionary dictionaryWithObjectsAndKeys: | |
4706 | @"deb", @"Type", | |
4707 | href, @"URI", | |
4708 | distribution, @"Distribution", | |
4709 | array, @"Sections", | |
4710 | nil] waitUntilDone:NO]; | |
4711 | } | |
4712 | ||
8a2d167a JF |
4713 | - (BOOL) addTrivialSource:(NSString *)href { |
4714 | href = VerifySource(href); | |
4715 | if (href == nil) | |
4716 | return NO; | |
a1d85d42 | 4717 | [delegate_ performSelectorOnMainThread:@selector(addTrivialSource:) withObject:href waitUntilDone:NO]; |
8a2d167a | 4718 | return YES; |
b088c0cd JF |
4719 | } |
4720 | ||
4721 | - (void) refreshSources { | |
4722 | [delegate_ performSelectorOnMainThread:@selector(syncData) withObject:nil waitUntilDone:NO]; | |
4723 | } | |
4724 | ||
33e30380 JF |
4725 | - (void) saveConfig { |
4726 | [delegate_ performSelectorOnMainThread:@selector(_saveConfig) withObject:nil waitUntilDone:NO]; | |
4727 | } | |
4728 | ||
5c32f89e JF |
4729 | - (NSArray *) getAllSources { |
4730 | return [[Database sharedInstance] sources]; | |
4731 | } | |
4732 | ||
8cc8eb1c | 4733 | - (NSArray *) getInstalledPackages { |
26e6829b JF |
4734 | Database *database([Database sharedInstance]); |
4735 | @synchronized (database) { | |
4736 | NSArray *packages([database packages]); | |
f4db946e | 4737 | NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]); |
77801ff1 | 4738 | for (Package *package in packages) |
26e6829b | 4739 | if (![package uninstalled]) |
8cc8eb1c JF |
4740 | [installed addObject:package]; |
4741 | return installed; | |
26e6829b | 4742 | } } |
8cc8eb1c | 4743 | |
43f3d7f6 | 4744 | - (Package *) getPackageById:(NSString *)id { |
62cab237 JF |
4745 | if (Package *package = [[Database sharedInstance] packageWithName:id]) { |
4746 | [package parse]; | |
4747 | return package; | |
4748 | } else | |
4749 | return (Package *) [NSNull null]; | |
43f3d7f6 JF |
4750 | } |
4751 | ||
948db680 JF |
4752 | - (NSString *) getLocaleIdentifier { |
4753 | return Locale_ == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(Locale_); | |
4754 | } | |
4755 | ||
4756 | - (NSArray *) getPreferredLanguages { | |
4757 | return Languages_; | |
4758 | } | |
4759 | ||
43f3d7f6 JF |
4760 | - (NSArray *) statfs:(NSString *)path { |
4761 | struct statfs stat; | |
4762 | ||
4763 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
4764 | return nil; | |
4765 | ||
4766 | return [NSArray arrayWithObjects: | |
4767 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
4768 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
4769 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
4770 | nil]; | |
4771 | } | |
4772 | ||
e40620ab | 4773 | - (NSNumber *) du:(NSString *)path { |
d59938e3 JF |
4774 | NSNumber *value(nil); |
4775 | ||
d8b0f6e3 | 4776 | FILE *du(popen([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/du -ks %@", ShellEscape(path)] UTF8String], "r")); |
d59938e3 JF |
4777 | if (du != NULL) { |
4778 | char line[1024]; | |
4779 | while (fgets(line, sizeof(line), du) != NULL) { | |
4780 | size_t length(strlen(line)); | |
4781 | while (length != 0 && line[length - 1] == '\n') | |
4782 | line[--length] = '\0'; | |
4783 | if (char *tab = strchr(line, '\t')) { | |
4784 | *tab = '\0'; | |
4785 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
4786 | } | |
4787 | } | |
4788 | pclose(du); | |
4789 | } | |
4790 | ||
4791 | return value; | |
43f3d7f6 JF |
4792 | } |
4793 | ||
4794 | - (void) close { | |
e6417cea | 4795 | [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO]; |
43f3d7f6 JF |
4796 | } |
4797 | ||
518a552a JF |
4798 | - (NSNumber *) isReachable:(NSString *)name { |
4799 | return [NSNumber numberWithBool:IsReachable([name UTF8String])]; | |
4800 | } | |
4801 | ||
77801ff1 JF |
4802 | - (void) installPackages:(NSArray *)packages { |
4803 | [delegate_ performSelectorOnMainThread:@selector(installPackages:) withObject:packages waitUntilDone:NO]; | |
4804 | } | |
4805 | ||
8a126074 | 4806 | - (NSString *) substitutePackageNames:(NSString *)message { |
0c4fe0f4 | 4807 | NSMutableArray *words([[[message componentsSeparatedByString:@" "] mutableCopy] autorelease]); |
8a126074 JF |
4808 | for (size_t i(0), e([words count]); i != e; ++i) { |
4809 | NSString *word([words objectAtIndex:i]); | |
4810 | if (Package *package = [[Database sharedInstance] packageWithName:word]) | |
4811 | [words replaceObjectAtIndex:i withObject:[package name]]; | |
4812 | } | |
4813 | ||
4814 | return [words componentsJoinedByString:@" "]; | |
4815 | } | |
4816 | ||
ed5566c7 JF |
4817 | - (void) removeButton { |
4818 | [indirect_ removeButton]; | |
4819 | } | |
4820 | ||
43f3d7f6 JF |
4821 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
4822 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
4823 | } | |
4824 | ||
4825 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
4826 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
4827 | } | |
4828 | ||
c31c825d JF |
4829 | - (void) setBadgeValue:(id)value { |
4830 | [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO]; | |
4831 | } | |
4832 | ||
8366df5e JF |
4833 | - (void) setAllowsNavigationAction:(NSString *)value { |
4834 | [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO]; | |
4835 | } | |
4836 | ||
b8a5d89d JF |
4837 | - (void) setHidesBackButton:(NSString *)value { |
4838 | [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO]; | |
4839 | } | |
4840 | ||
5cdfcd6f JF |
4841 | - (void) setHidesNavigationBar:(NSString *)value { |
4842 | [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO]; | |
4843 | } | |
4844 | ||
82406217 JF |
4845 | - (void) setNavigationBarStyle:(NSString *)value { |
4846 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO]; | |
4847 | } | |
4848 | ||
00984204 JF |
4849 | - (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha { |
4850 | float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]); | |
4851 | UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]); | |
4852 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO]; | |
4853 | } | |
4854 | ||
36a20e14 JF |
4855 | - (void) setPasteboardString:(NSString *)value { |
4856 | [[objc_getClass("UIPasteboard") generalPasteboard] setString:value]; | |
4857 | } | |
4858 | ||
4859 | - (void) setPasteboardURL:(NSString *)value { | |
4860 | [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]]; | |
4861 | } | |
4862 | ||
673a6e1a | 4863 | - (void) setToken:(NSString *)token { |
7826a48c | 4864 | // XXX: the website expects this :/ |
673a6e1a JF |
4865 | } |
4866 | ||
8e3b68d4 JF |
4867 | - (void) scrollToBottom:(NSNumber *)animated { |
4868 | [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO]; | |
4869 | } | |
4870 | ||
43f3d7f6 | 4871 | - (void) setViewportWidth:(float)width { |
8dbdaafa | 4872 | [indirect_ setViewportWidthOnMainThread:width]; |
43f3d7f6 JF |
4873 | } |
4874 | ||
4875 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
4876 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
4877 | unsigned count([arguments count]); | |
4878 | id values[count]; | |
4879 | for (unsigned i(0); i != count; ++i) | |
4880 | values[i] = [arguments objectAtIndex:i]; | |
673e8fa3 | 4881 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; |
43f3d7f6 JF |
4882 | } |
4883 | ||
4884 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
a95e0405 JF |
4885 | if (reinterpret_cast<id>(value) == [WebUndefined undefined]) |
4886 | value = nil; | |
43f3d7f6 JF |
4887 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) |
4888 | table = nil; | |
4889 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
c390d3ab JF |
4890 | } |
4891 | ||
4892 | @end | |
4893 | /* }}} */ | |
f79a4512 | 4894 | |
2e1652a9 JF |
4895 | @interface NSURL (CydiaSecure) |
4896 | @end | |
4897 | ||
4898 | @implementation NSURL (CydiaSecure) | |
4899 | ||
4900 | - (bool) isCydiaSecure { | |
4901 | if ([[[self scheme] lowercaseString] isEqualToString:@"https"]) | |
4902 | return true; | |
4903 | ||
4904 | @synchronized (HostConfig_) { | |
4905 | if ([InsecureHosts_ containsObject:[self host]]) | |
4906 | return true; | |
4907 | } | |
4908 | ||
4909 | return false; | |
4910 | } | |
4911 | ||
4912 | @end | |
dd5f8161 | 4913 | |
80319240 | 4914 | /* Cydia Browser Controller {{{ */ |
a576488f | 4915 | @implementation CydiaWebViewController |
43f3d7f6 | 4916 | |
fe8e721f | 4917 | - (NSURL *) navigationURL { |
d323285e | 4918 | return request_ == nil ? nil : [NSURL URLWithString:[NSString stringWithFormat:@"cydia://url/%@", [[request_ URL] absoluteString]]]; |
fe8e721f GP |
4919 | } |
4920 | ||
3f9ab807 JF |
4921 | + (void) _initialize { |
4922 | [super _initialize]; | |
4923 | ||
7b33d201 | 4924 | Diversions_ = [NSMutableSet setWithCapacity:0]; |
775deead JF |
4925 | } |
4926 | ||
4927 | + (void) addDiversion:(Diversion *)diversion { | |
4928 | [Diversions_ addObject:diversion]; | |
4929 | } | |
4930 | ||
2634b249 JF |
4931 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4932 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
9e130bc2 JF |
4933 | [CydiaWebViewController didClearWindowObject:window forFrame:frame withCydia:cydia_]; |
4934 | } | |
01d93940 | 4935 | |
9e130bc2 | 4936 | + (void) didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame withCydia:(CydiaObject *)cydia { |
01d93940 JF |
4937 | WebDataSource *source([frame dataSource]); |
4938 | NSURLResponse *response([source response]); | |
4939 | NSURL *url([response URL]); | |
b8f1a18a JF |
4940 | NSString *scheme([[url scheme] lowercaseString]); |
4941 | ||
4942 | bool bridged(false); | |
8804004f | 4943 | |
48f1762f | 4944 | @synchronized (HostConfig_) { |
b8f1a18a JF |
4945 | if ([scheme isEqualToString:@"file"]) |
4946 | bridged = true; | |
4947 | else if ([scheme isEqualToString:@"https"]) | |
48f1762f | 4948 | if ([BridgedHosts_ containsObject:[url host]]) |
b8f1a18a | 4949 | bridged = true; |
48f1762f | 4950 | } |
b8f1a18a JF |
4951 | |
4952 | if (bridged) | |
9e130bc2 | 4953 | [window setValue:cydia forKey:@"cydia"]; |
43f3d7f6 JF |
4954 | } |
4955 | ||
22485d93 JF |
4956 | - (void) _setupMail:(MFMailComposeViewController *)controller { |
4957 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/cydia.log"] mimeType:@"text/plain" fileName:@"cydia.log"]; | |
4958 | ||
4959 | system("/usr/bin/dpkg -l >/tmp/dpkgl.log"); | |
4960 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/dpkgl.log"] mimeType:@"text/plain" fileName:@"dpkgl.log"]; | |
4961 | } | |
4962 | ||
f9b36dae JF |
4963 | - (NSURL *) URLWithURL:(NSURL *)url { |
4964 | return [Diversion divertURL:url]; | |
4965 | } | |
4966 | ||
9d1bf666 | 4967 | - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
85ae5f42 JF |
4968 | return [CydiaWebViewController requestWithHeaders:[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; |
4969 | } | |
4970 | ||
92de61aa JF |
4971 | - (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
4972 | return [CydiaWebViewController requestWithHeaders:[super webThreadWebView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source]]; | |
4973 | } | |
4974 | ||
85ae5f42 | 4975 | + (NSURLRequest *) requestWithHeaders:(NSURLRequest *)request { |
0c4fe0f4 | 4976 | NSMutableURLRequest *copy([[request mutableCopy] autorelease]); |
9d1bf666 | 4977 | |
bc1cffbe | 4978 | NSURL *url([copy URL]); |
daa21f8e | 4979 | NSString *href([url absoluteString]); |
bc1cffbe JF |
4980 | NSString *host([url host]); |
4981 | ||
daa21f8e JF |
4982 | if ([href hasPrefix:@"https://cydia.saurik.com/TSS/"]) { |
4983 | if (NSString *agent = [copy valueForHTTPHeaderField:@"X-User-Agent"]) { | |
4984 | [copy setValue:agent forHTTPHeaderField:@"User-Agent"]; | |
4985 | [copy setValue:nil forHTTPHeaderField:@"X-User-Agent"]; | |
4986 | } | |
4987 | ||
4988 | [copy setValue:nil forHTTPHeaderField:@"Referer"]; | |
4989 | [copy setValue:nil forHTTPHeaderField:@"Origin"]; | |
4990 | ||
4991 | [copy setURL:[NSURL URLWithString:[@"http://gs.apple.com/TSS/" stringByAppendingString:[href substringFromIndex:29]]]]; | |
4992 | return copy; | |
4993 | } | |
4994 | ||
6f44d712 JF |
4995 | if ([copy valueForHTTPHeaderField:@"X-Cydia-Cf"] == nil) |
4996 | [copy setValue:[NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber] forHTTPHeaderField:@"X-Cydia-Cf"]; | |
1baae086 | 4997 | if (Machine_ != NULL && [copy valueForHTTPHeaderField:@"X-Machine"] == nil) |
9d1bf666 | 4998 | [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; |
247bedb6 | 4999 | |
7826a48c | 5000 | bool bridged; @synchronized (HostConfig_) { |
e7817a6b | 5001 | bridged = [BridgedHosts_ containsObject:host]; |
247bedb6 JF |
5002 | } |
5003 | ||
7826a48c JF |
5004 | if ([url isCydiaSecure] && bridged && UniqueID_ != nil && [copy valueForHTTPHeaderField:@"X-Cydia-Id"] == nil) |
5005 | [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; | |
43f3d7f6 JF |
5006 | |
5007 | return copy; | |
a9a0661e JF |
5008 | } |
5009 | ||
77801ff1 JF |
5010 | - (void) setDelegate:(id)delegate { |
5011 | [super setDelegate:delegate]; | |
5012 | [cydia_ setDelegate:delegate]; | |
5013 | } | |
5014 | ||
c6cf66c7 | 5015 | - (NSString *) applicationNameForUserAgent { |
e967efd5 | 5016 | return UserAgent_; |
c6cf66c7 | 5017 | } |
43f3d7f6 | 5018 | |
c6cf66c7 JF |
5019 | - (id) init { |
5020 | if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) { | |
5021 | cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease]; | |
43f3d7f6 JF |
5022 | } return self; |
5023 | } | |
5024 | ||
29bb09d7 JF |
5025 | @end |
5026 | ||
5027 | @interface AppCacheController : CydiaWebViewController { | |
5028 | } | |
5029 | ||
5030 | @end | |
5031 | ||
5032 | @implementation AppCacheController | |
5033 | ||
5034 | - (void) didReceiveMemoryWarning { | |
6271cb57 | 5035 | // XXX: this doesn't work |
29bb09d7 JF |
5036 | } |
5037 | ||
2713be8e JF |
5038 | - (bool) retainsNetworkActivityIndicator { |
5039 | return false; | |
5040 | } | |
5041 | ||
43f3d7f6 | 5042 | @end |
80319240 | 5043 | /* }}} */ |
43f3d7f6 | 5044 | |
b1ca831d JF |
5045 | // CydiaScript {{{ |
5046 | @interface NSObject (CydiaScript) | |
5047 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; | |
5048 | @end | |
5049 | ||
5050 | @implementation NSObject (CydiaScript) | |
5051 | ||
5052 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5053 | return self; | |
5054 | } | |
5055 | ||
5056 | @end | |
5057 | ||
5058 | @implementation NSArray (CydiaScript) | |
5059 | ||
5060 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5061 | WebScriptObject *object([context evaluateWebScript:@"[]"]); | |
5062 | for (size_t i(0), e([self count]); i != e; ++i) | |
5063 | [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; | |
5064 | return object; | |
5065 | } | |
5066 | ||
5067 | @end | |
5068 | ||
5069 | @implementation NSDictionary (CydiaScript) | |
5070 | ||
5071 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
5072 | WebScriptObject *object([context evaluateWebScript:@"({})"]); | |
5073 | for (id i in self) | |
5074 | [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; | |
5075 | return object; | |
5076 | } | |
5077 | ||
5078 | @end | |
5079 | // }}} | |
5080 | ||
5829aea2 GP |
5081 | /* Confirmation Controller {{{ */ |
5082 | bool DepSubstrate(const pkgCache::VerIterator &iterator) { | |
5083 | if (!iterator.end()) | |
5084 | for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) { | |
5085 | if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends) | |
5086 | continue; | |
5087 | pkgCache::PkgIterator package(dep.TargetPkg()); | |
5088 | if (package.end()) | |
5089 | continue; | |
5090 | if (strcmp(package.Name(), "mobilesubstrate") == 0) | |
5091 | return true; | |
5092 | } | |
5093 | ||
5094 | return false; | |
5095 | } | |
5096 | ||
adb61bda | 5097 | @protocol ConfirmationControllerDelegate |
674dce72 | 5098 | - (void) cancelAndClear:(bool)clear; |
b5e7eebb | 5099 | - (void) confirmWithNavigationController:(UINavigationController *)navigation; |
dc63e78f | 5100 | - (void) queue; |
36bb2ca2 | 5101 | @end |
2367a917 | 5102 | |
a576488f | 5103 | @interface ConfirmationController : CydiaWebViewController { |
770f2a8e | 5104 | _transient Database *database_; |
6ceb0959 | 5105 | |
7b33d201 | 5106 | _H<UIAlertView> essential_; |
6ceb0959 | 5107 | |
7b33d201 JF |
5108 | _H<NSDictionary> changes_; |
5109 | _H<NSMutableArray> issues_; | |
5110 | _H<NSDictionary> sizes_; | |
6ceb0959 | 5111 | |
a9a0661e | 5112 | BOOL substrate_; |
36bb2ca2 | 5113 | } |
dc5812ec | 5114 | |
b5e7eebb | 5115 | - (id) initWithDatabase:(Database *)database; |
b4d89997 | 5116 | |
dc5812ec JF |
5117 | @end |
5118 | ||
adb61bda | 5119 | @implementation ConfirmationController |
dc5812ec | 5120 | |
ab2cfc1e JF |
5121 | - (void) complete { |
5122 | if (substrate_) | |
be860cc8 | 5123 | RestartSubstrate_ = true; |
ab2cfc1e JF |
5124 | [delegate_ confirmWithNavigationController:[self navigationController]]; |
5125 | } | |
5126 | ||
b5e7eebb | 5127 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
3272e699 | 5128 | NSString *context([alert context]); |
9bedffaa | 5129 | |
1cedb821 | 5130 | if ([context isEqualToString:@"remove"]) { |
ab2cfc1e | 5131 | if (button == [alert cancelButtonIndex]) |
02069daf | 5132 | [self _doContinue]; |
ab2cfc1e | 5133 | else if (button == [alert firstOtherButtonIndex]) { |
d69dbfc5 | 5134 | [self performSelector:@selector(complete) withObject:nil afterDelay:0]; |
9bedffaa | 5135 | } |
9bedffaa | 5136 | |
3272e699 | 5137 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 5138 | } else if ([context isEqualToString:@"unable"]) { |
b5e7eebb | 5139 | [self dismissModalViewControllerAnimated:YES]; |
3272e699 GP |
5140 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
5141 | } else { | |
b5e7eebb GP |
5142 | [super alertView:alert clickedButtonAtIndex:button]; |
5143 | } | |
36bb2ca2 JF |
5144 | } |
5145 | ||
46c46f4f | 5146 | - (void) _doContinue { |
21ea11a4 | 5147 | [delegate_ cancelAndClear:NO]; |
12d3d98d | 5148 | [self dismissModalViewControllerAnimated:YES]; |
46c46f4f | 5149 | } |
bc11cf5b | 5150 | |
46c46f4f JF |
5151 | - (id) invokeDefaultMethodWithArguments:(NSArray *)args { |
5152 | [self performSelectorOnMainThread:@selector(_doContinue) withObject:nil waitUntilDone:NO]; | |
21ea11a4 GP |
5153 | return nil; |
5154 | } | |
5155 | ||
2634b249 JF |
5156 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5157 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
6ceb0959 | 5158 | |
781001d7 | 5159 | [window setValue:[[NSDictionary dictionaryWithObjectsAndKeys: |
7b33d201 JF |
5160 | (id) changes_, @"changes", |
5161 | (id) issues_, @"issues", | |
5162 | (id) sizes_, @"sizes", | |
781001d7 JF |
5163 | self, @"queue", |
5164 | nil] Cydia$webScriptObjectInContext:window] forKey:@"cydiaConfirm"]; | |
36bb2ca2 JF |
5165 | } |
5166 | ||
b5e7eebb GP |
5167 | - (id) initWithDatabase:(Database *)database { |
5168 | if ((self = [super init]) != nil) { | |
770f2a8e JF |
5169 | database_ = database; |
5170 | ||
6ceb0959 JF |
5171 | NSMutableArray *installs([NSMutableArray arrayWithCapacity:16]); |
5172 | NSMutableArray *reinstalls([NSMutableArray arrayWithCapacity:16]); | |
5173 | NSMutableArray *upgrades([NSMutableArray arrayWithCapacity:16]); | |
5174 | NSMutableArray *downgrades([NSMutableArray arrayWithCapacity:16]); | |
5175 | NSMutableArray *removes([NSMutableArray arrayWithCapacity:16]); | |
dc5812ec | 5176 | |
36bb2ca2 | 5177 | bool remove(false); |
dc5812ec | 5178 | |
6ceb0959 JF |
5179 | pkgCacheFile &cache([database_ cache]); |
5180 | NSArray *packages([database_ packages]); | |
a9a0661e JF |
5181 | pkgDepCache::Policy *policy([database_ policy]); |
5182 | ||
7b33d201 | 5183 | issues_ = [NSMutableArray arrayWithCapacity:4]; |
6ceb0959 | 5184 | |
c4dcf2c2 | 5185 | for (Package *package in packages) { |
6ceb0959 JF |
5186 | pkgCache::PkgIterator iterator([package iterator]); |
5187 | NSString *name([package id]); | |
5188 | ||
5189 | if ([package broken]) { | |
5190 | NSMutableArray *reasons([NSMutableArray arrayWithCapacity:4]); | |
5191 | ||
5192 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5193 | name, @"package", | |
5194 | reasons, @"reasons", | |
5195 | nil]]; | |
5196 | ||
5197 | pkgCache::VerIterator ver(cache[iterator].InstVerIter(cache)); | |
5198 | if (ver.end()) | |
5199 | continue; | |
5200 | ||
5201 | for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) { | |
5202 | pkgCache::DepIterator start; | |
5203 | pkgCache::DepIterator end; | |
5204 | dep.GlobOr(start, end); // ++dep | |
5205 | ||
5206 | if (!cache->IsImportantDep(end)) | |
5207 | continue; | |
5208 | if ((cache[end] & pkgDepCache::DepGInstall) != 0) | |
5209 | continue; | |
5210 | ||
810c9763 JF |
5211 | NSMutableArray *clauses([NSMutableArray arrayWithCapacity:4]); |
5212 | ||
5213 | [reasons addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5214 | [NSString stringWithUTF8String:start.DepType()], @"relationship", | |
5215 | clauses, @"clauses", | |
5216 | nil]]; | |
5217 | ||
6ceb0959 JF |
5218 | _forever { |
5219 | NSString *reason, *installed((NSString *) [WebUndefined undefined]); | |
5220 | ||
5221 | pkgCache::PkgIterator target(start.TargetPkg()); | |
5222 | if (target->ProvidesList != 0) | |
5223 | reason = @"missing"; | |
5224 | else { | |
5225 | pkgCache::VerIterator ver(cache[target].InstVerIter(cache)); | |
5226 | if (!ver.end()) { | |
5227 | reason = @"installed"; | |
5228 | installed = [NSString stringWithUTF8String:ver.VerStr()]; | |
5229 | } else if (!cache[target].CandidateVerIter(cache).end()) | |
5230 | reason = @"uninstalled"; | |
5231 | else if (target->ProvidesList == 0) | |
5232 | reason = @"uninstallable"; | |
5233 | else | |
5234 | reason = @"virtual"; | |
5235 | } | |
5236 | ||
3e5a9f5d | 5237 | NSDictionary *version(start.TargetVer() == 0 ? (NSDictionary *) [NSNull null] : [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5238 | [NSString stringWithUTF8String:start.CompType()], @"operator", |
5239 | [NSString stringWithUTF8String:start.TargetVer()], @"value", | |
5240 | nil]); | |
5241 | ||
810c9763 | 5242 | [clauses addObject:[NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5243 | [NSString stringWithUTF8String:start.TargetPkg().Name()], @"package", |
5244 | version, @"version", | |
5245 | reason, @"reason", | |
5246 | installed, @"installed", | |
5247 | nil]]; | |
5248 | ||
5249 | // yes, seriously. (wtf?) | |
5250 | if (start == end) | |
5251 | break; | |
5252 | ++start; | |
5253 | } | |
5254 | } | |
5255 | } | |
5256 | ||
36bb2ca2 | 5257 | pkgDepCache::StateCache &state(cache[iterator]); |
dc5812ec | 5258 | |
b4fd1197 | 5259 | static RegEx special_r("(firmware|gsc\\..*|cy\\+.*)"); |
2388b078 | 5260 | |
36bb2ca2 | 5261 | if (state.NewInstall()) |
6ceb0959 | 5262 | [installs addObject:name]; |
f8d15be2 | 5263 | // XXX: else if (state.Install()) |
36bb2ca2 | 5264 | else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall) |
6ceb0959 | 5265 | [reinstalls addObject:name]; |
f8d15be2 | 5266 | // XXX: move before previous if |
36bb2ca2 | 5267 | else if (state.Upgrade()) |
6ceb0959 | 5268 | [upgrades addObject:name]; |
36bb2ca2 | 5269 | else if (state.Downgrade()) |
6ceb0959 JF |
5270 | [downgrades addObject:name]; |
5271 | else if (!state.Delete()) | |
f8d15be2 | 5272 | // XXX: _assert(state.Keep()); |
6ceb0959 | 5273 | continue; |
1916f316 JF |
5274 | else if (special_r(name)) |
5275 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5276 | [NSNull null], @"package", | |
5277 | [NSArray arrayWithObjects: | |
5278 | [NSDictionary dictionaryWithObjectsAndKeys: | |
810c9763 JF |
5279 | @"Conflicts", @"relationship", |
5280 | [NSArray arrayWithObjects: | |
5281 | [NSDictionary dictionaryWithObjectsAndKeys: | |
5282 | name, @"package", | |
5283 | [NSNull null], @"version", | |
5284 | @"installed", @"reason", | |
5285 | nil], | |
5286 | nil], @"clauses", | |
1916f316 JF |
5287 | nil], |
5288 | nil], @"reasons", | |
5289 | nil]]; | |
5290 | else { | |
2388b078 | 5291 | if ([package essential]) |
36bb2ca2 | 5292 | remove = true; |
6ceb0959 JF |
5293 | [removes addObject:name]; |
5294 | } | |
a9a0661e JF |
5295 | |
5296 | substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator)); | |
5297 | substrate_ |= DepSubstrate(iterator.CurrentVer()); | |
36bb2ca2 | 5298 | } |
ec97ef06 | 5299 | |
36bb2ca2 JF |
5300 | if (!remove) |
5301 | essential_ = nil; | |
d791dce4 | 5302 | else if (Advanced_) { |
43f3d7f6 | 5303 | NSString *parenthetical(UCLocalize("PARENTHETICAL")); |
f79a4512 | 5304 | |
7b33d201 | 5305 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 5306 | initWithTitle:UCLocalize("REMOVING_ESSENTIALS") |
b5e7eebb GP |
5307 | message:UCLocalize("REMOVING_ESSENTIALS_EX") |
5308 | delegate:self | |
5309 | cancelButtonTitle:[NSString stringWithFormat:parenthetical, UCLocalize("CANCEL_OPERATION"), UCLocalize("SAFE")] | |
1aa29546 JF |
5310 | otherButtonTitles: |
5311 | [NSString stringWithFormat:parenthetical, UCLocalize("FORCE_REMOVAL"), UCLocalize("UNSAFE")], | |
5312 | nil | |
7b33d201 | 5313 | ] autorelease]; |
04fe1349 | 5314 | |
3272e699 | 5315 | [essential_ setContext:@"remove"]; |
9aaebfec | 5316 | [essential_ setNumberOfRows:2]; |
9bedffaa | 5317 | } else { |
7b33d201 | 5318 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 5319 | initWithTitle:UCLocalize("UNABLE_TO_COMPLY") |
b5e7eebb GP |
5320 | message:UCLocalize("UNABLE_TO_COMPLY_EX") |
5321 | delegate:self | |
5322 | cancelButtonTitle:UCLocalize("OKAY") | |
5323 | otherButtonTitles:nil | |
7b33d201 | 5324 | ] autorelease]; |
ec97ef06 | 5325 | |
b5e7eebb | 5326 | [essential_ setContext:@"unable"]; |
36bb2ca2 | 5327 | } |
ec97ef06 | 5328 | |
7b33d201 | 5329 | changes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5330 | installs, @"installs", |
5331 | reinstalls, @"reinstalls", | |
5332 | upgrades, @"upgrades", | |
5333 | downgrades, @"downgrades", | |
5334 | removes, @"removes", | |
affeffc7 | 5335 | nil]; |
dc5812ec | 5336 | |
7b33d201 | 5337 | sizes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
5338 | [NSNumber numberWithInteger:[database_ fetcher].FetchNeeded()], @"downloading", |
5339 | [NSNumber numberWithInteger:[database_ fetcher].PartialPresent()], @"resuming", | |
affeffc7 | 5340 | nil]; |
dc5812ec | 5341 | |
90351d93 | 5342 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/confirm/", UI_]]]; |
36bb2ca2 | 5343 | } return self; |
b4d89997 | 5344 | } |
8da60fb7 | 5345 | |
e6124cb6 JF |
5346 | - (UIBarButtonItem *) leftButton { |
5347 | return [[[UIBarButtonItem alloc] | |
5348 | initWithTitle:UCLocalize("CANCEL") | |
5349 | style:UIBarButtonItemStylePlain | |
5350 | target:self | |
5351 | action:@selector(cancelButtonClicked) | |
5352 | ] autorelease]; | |
5353 | } | |
5354 | ||
614cd4f1 | 5355 | #if !AlwaysReload |
bcde1e70 | 5356 | - (void) applyRightButton { |
6ceb0959 | 5357 | if ([issues_ count] == 0 && ![self isLoading]) |
dd9de556 JF |
5358 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
5359 | initWithTitle:UCLocalize("CONFIRM") | |
2761d574 | 5360 | style:UIBarButtonItemStyleDone |
dd9de556 JF |
5361 | target:self |
5362 | action:@selector(confirmButtonClicked) | |
5363 | ] autorelease]]; | |
5364 | else | |
bcde1e70 | 5365 | [[self navigationItem] setRightBarButtonItem:nil]; |
affeffc7 | 5366 | } |
bcde1e70 | 5367 | #endif |
affeffc7 | 5368 | |
b5e7eebb | 5369 | - (void) cancelButtonClicked { |
21ea11a4 | 5370 | [delegate_ cancelAndClear:YES]; |
05452929 | 5371 | [self dismissModalViewControllerAnimated:YES]; |
affeffc7 JF |
5372 | } |
5373 | ||
9487f027 | 5374 | #if !AlwaysReload |
b5e7eebb | 5375 | - (void) confirmButtonClicked { |
affeffc7 | 5376 | if (essential_ != nil) |
b5e7eebb | 5377 | [essential_ show]; |
ab2cfc1e JF |
5378 | else |
5379 | [self complete]; | |
affeffc7 | 5380 | } |
9487f027 | 5381 | #endif |
affeffc7 | 5382 | |
36bb2ca2 JF |
5383 | @end |
5384 | /* }}} */ | |
8da60fb7 | 5385 | |
aaae308d JF |
5386 | /* Progress Data {{{ */ |
5387 | @interface CydiaProgressData : NSObject { | |
5388 | _transient id delegate_; | |
5389 | ||
5390 | bool running_; | |
b0b11d99 | 5391 | float percent_; |
aaae308d | 5392 | |
bcbac8f7 JF |
5393 | float current_; |
5394 | float total_; | |
5395 | float speed_; | |
5396 | ||
aaae308d JF |
5397 | _H<NSMutableArray> events_; |
5398 | _H<NSString> title_; | |
5399 | ||
5400 | _H<NSString> status_; | |
5401 | _H<NSString> finish_; | |
5402 | } | |
5403 | ||
5404 | @end | |
5405 | ||
5406 | @implementation CydiaProgressData | |
5407 | ||
5408 | + (NSArray *) _attributeKeys { | |
5409 | return [NSArray arrayWithObjects: | |
bcbac8f7 | 5410 | @"current", |
aaae308d JF |
5411 | @"events", |
5412 | @"finish", | |
b0b11d99 | 5413 | @"percent", |
aaae308d | 5414 | @"running", |
bcbac8f7 | 5415 | @"speed", |
aaae308d | 5416 | @"title", |
bcbac8f7 | 5417 | @"total", |
aaae308d JF |
5418 | nil]; |
5419 | } | |
5420 | ||
5421 | - (NSArray *) attributeKeys { | |
5422 | return [[self class] _attributeKeys]; | |
5423 | } | |
5424 | ||
5425 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
5426 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
5427 | } | |
5428 | ||
5429 | - (id) init { | |
5430 | if ((self = [super init]) != nil) { | |
5431 | events_ = [NSMutableArray arrayWithCapacity:32]; | |
5432 | } return self; | |
5433 | } | |
5434 | ||
353dda5b JF |
5435 | - (id) delegate { |
5436 | return delegate_; | |
5437 | } | |
5438 | ||
aaae308d JF |
5439 | - (void) setDelegate:(id)delegate { |
5440 | delegate_ = delegate; | |
5441 | } | |
5442 | ||
b0b11d99 JF |
5443 | - (void) setPercent:(float)value { |
5444 | percent_ = value; | |
aaae308d JF |
5445 | } |
5446 | ||
b0b11d99 JF |
5447 | - (NSNumber *) percent { |
5448 | return [NSNumber numberWithFloat:percent_]; | |
aaae308d JF |
5449 | } |
5450 | ||
bcbac8f7 JF |
5451 | - (void) setCurrent:(float)value { |
5452 | current_ = value; | |
5453 | } | |
5454 | ||
5455 | - (NSNumber *) current { | |
5456 | return [NSNumber numberWithFloat:current_]; | |
5457 | } | |
5458 | ||
5459 | - (void) setTotal:(float)value { | |
5460 | total_ = value; | |
5461 | } | |
5462 | ||
5463 | - (NSNumber *) total { | |
5464 | return [NSNumber numberWithFloat:total_]; | |
5465 | } | |
5466 | ||
5467 | - (void) setSpeed:(float)value { | |
5468 | speed_ = value; | |
5469 | } | |
5470 | ||
5471 | - (NSNumber *) speed { | |
5472 | return [NSNumber numberWithFloat:speed_]; | |
5473 | } | |
5474 | ||
aaae308d JF |
5475 | - (NSArray *) events { |
5476 | return events_; | |
5477 | } | |
5478 | ||
5479 | - (void) removeAllEvents { | |
5480 | [events_ removeAllObjects]; | |
5481 | } | |
5482 | ||
5483 | - (void) addEvent:(CydiaProgressEvent *)event { | |
5484 | [events_ addObject:event]; | |
5485 | } | |
5486 | ||
5487 | - (void) setTitle:(NSString *)text { | |
5488 | title_ = text; | |
5489 | } | |
5490 | ||
5491 | - (NSString *) title { | |
5492 | return title_; | |
5493 | } | |
5494 | ||
5495 | - (void) setFinish:(NSString *)text { | |
5496 | finish_ = text; | |
5497 | } | |
5498 | ||
5499 | - (NSString *) finish { | |
5500 | return (id) finish_ ?: [NSNull null]; | |
5501 | } | |
5502 | ||
5503 | - (void) setRunning:(bool)running { | |
5504 | running_ = running; | |
5505 | } | |
5506 | ||
5507 | - (NSNumber *) running { | |
5508 | return running_ ? (NSNumber *) kCFBooleanTrue : (NSNumber *) kCFBooleanFalse; | |
5509 | } | |
5510 | ||
5511 | @end | |
5512 | /* }}} */ | |
adb61bda | 5513 | /* Progress Controller {{{ */ |
a576488f | 5514 | @interface ProgressController : CydiaWebViewController < |
36bb2ca2 JF |
5515 | ProgressDelegate |
5516 | > { | |
8b29f8e6 | 5517 | _transient Database *database_; |
bf7c998c | 5518 | _H<CydiaProgressData, 1> progress_; |
aaae308d | 5519 | unsigned cancel_; |
2367a917 JF |
5520 | } |
5521 | ||
b5e7eebb | 5522 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
2367a917 | 5523 | |
6915b806 | 5524 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title; |
2367a917 | 5525 | |
6915b806 | 5526 | - (void) setTitle:(NSString *)title; |
aaae308d | 5527 | - (void) setCancellable:(bool)cancellable; |
bd150f54 | 5528 | |
36bb2ca2 JF |
5529 | @end |
5530 | ||
adb61bda | 5531 | @implementation ProgressController |
2367a917 JF |
5532 | |
5533 | - (void) dealloc { | |
6915b806 | 5534 | [database_ setProgressDelegate:nil]; |
2367a917 JF |
5535 | [super dealloc]; |
5536 | } | |
5537 | ||
63ae52be JF |
5538 | - (UIBarButtonItem *) leftButton { |
5539 | return cancel_ == 1 ? [[[UIBarButtonItem alloc] | |
3c62d654 JF |
5540 | initWithTitle:UCLocalize("CANCEL") |
5541 | style:UIBarButtonItemStylePlain | |
5542 | target:self | |
5543 | action:@selector(cancel) | |
63ae52be JF |
5544 | ] autorelease] : nil; |
5545 | } | |
5546 | ||
5547 | - (void) updateCancel { | |
5548 | [super applyLeftButton]; | |
3c62d654 JF |
5549 | } |
5550 | ||
b5e7eebb GP |
5551 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { |
5552 | if ((self = [super init]) != nil) { | |
8b29f8e6 | 5553 | database_ = database; |
36bb2ca2 | 5554 | delegate_ = delegate; |
ec97ef06 | 5555 | |
6915b806 JF |
5556 | [database_ setProgressDelegate:self]; |
5557 | ||
aaae308d JF |
5558 | progress_ = [[[CydiaProgressData alloc] init] autorelease]; |
5559 | [progress_ setDelegate:self]; | |
3c62d654 | 5560 | |
90351d93 | 5561 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/progress/", UI_]]]; |
3c62d654 JF |
5562 | |
5563 | [scroller_ setBackgroundColor:[UIColor blackColor]]; | |
5564 | ||
5565 | [[self navigationItem] setHidesBackButton:YES]; | |
5566 | ||
5567 | [self updateCancel]; | |
36bb2ca2 | 5568 | } return self; |
2367a917 JF |
5569 | } |
5570 | ||
aaae308d JF |
5571 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5572 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
5573 | [window setValue:progress_ forKey:@"cydiaProgress"]; | |
5574 | } | |
bc11cf5b | 5575 | |
aaae308d JF |
5576 | - (void) updateProgress { |
5577 | [self dispatchEvent:@"CydiaProgressUpdate"]; | |
5578 | } | |
b5e7eebb | 5579 | |
b5e7eebb | 5580 | - (void) viewWillAppear:(BOOL)animated { |
14e4ff09 | 5581 | [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlack]; |
3c62d654 | 5582 | [super viewWillAppear:animated]; |
2367a917 JF |
5583 | } |
5584 | ||
aaae308d | 5585 | - (void) close { |
ef494bd8 RP |
5586 | UpdateExternalStatus(0); |
5587 | ||
9dd3045d JF |
5588 | if (Finish_ > 1) |
5589 | [delegate_ saveState]; | |
5590 | ||
670a0494 JF |
5591 | switch (Finish_) { |
5592 | case 0: | |
2925cbba | 5593 | [delegate_ returnToCydia]; |
670a0494 JF |
5594 | break; |
5595 | ||
5596 | case 1: | |
c4899376 JF |
5597 | [delegate_ terminateWithSuccess]; |
5598 | /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) | |
5599 | [delegate_ suspendWithAnimation:YES]; | |
5600 | else | |
5601 | [delegate_ suspend];*/ | |
670a0494 JF |
5602 | break; |
5603 | ||
5604 | case 2: | |
985d2dff | 5605 | _trace(); |
ef812071 | 5606 | goto reload; |
670a0494 JF |
5607 | |
5608 | case 3: | |
985d2dff | 5609 | _trace(); |
ef812071 JF |
5610 | goto reload; |
5611 | ||
a3755a1e JF |
5612 | reload: { |
5613 | UIProgressHUD *hud([delegate_ addProgressHUD]); | |
5614 | [hud setText:UCLocalize("LOADING")]; | |
42bbf27d | 5615 | [delegate_ performSelector:@selector(reloadSpringBoard) withObject:nil afterDelay:0.5]; |
317eb8e3 JF |
5616 | return; |
5617 | } | |
670a0494 JF |
5618 | |
5619 | case 4: | |
985d2dff | 5620 | _trace(); |
0e371502 JF |
5621 | if (void (*SBReboot)(mach_port_t) = reinterpret_cast<void (*)(mach_port_t)>(dlsym(RTLD_DEFAULT, "SBReboot"))) |
5622 | SBReboot(SBSSpringBoardServerPort()); | |
5623 | else | |
bb0fe3c9 | 5624 | reboot2(RB_AUTOBOOT); |
670a0494 | 5625 | break; |
bc8cd583 | 5626 | } |
aaae308d JF |
5627 | |
5628 | [super close]; | |
670a0494 | 5629 | } |
bd150f54 | 5630 | |
6915b806 | 5631 | - (void) setTitle:(NSString *)title { |
aaae308d JF |
5632 | [progress_ setTitle:title]; |
5633 | [self updateProgress]; | |
5634 | } | |
5635 | ||
5636 | - (UIBarButtonItem *) rightButton { | |
d53628b6 | 5637 | return [[progress_ running] boolValue] ? [super rightButton] : [[[UIBarButtonItem alloc] |
aaae308d JF |
5638 | initWithTitle:UCLocalize("CLOSE") |
5639 | style:UIBarButtonItemStylePlain | |
5640 | target:self | |
5641 | action:@selector(close) | |
5642 | ] autorelease]; | |
6915b806 JF |
5643 | } |
5644 | ||
5645 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title { | |
5646 | UpdateExternalStatus(1); | |
5647 | ||
aaae308d | 5648 | [progress_ setRunning:true]; |
6915b806 | 5649 | [self setTitle:title]; |
aaae308d | 5650 | // implicit updateProgress |
6915b806 | 5651 | |
140710ba | 5652 | SHA1SumValue notifyconf; { |
6915b806 JF |
5653 | FileFd file; |
5654 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
5655 | _error->Discard(); | |
5656 | else { | |
5657 | MMap mmap(file, MMap::ReadOnly); | |
5658 | SHA1Summation sha1; | |
5659 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5660 | notifyconf = sha1.Result(); |
6915b806 JF |
5661 | } |
5662 | } | |
5663 | ||
140710ba | 5664 | SHA1SumValue springlist; { |
6915b806 JF |
5665 | FileFd file; |
5666 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
5667 | _error->Discard(); | |
5668 | else { | |
5669 | MMap mmap(file, MMap::ReadOnly); | |
5670 | SHA1Summation sha1; | |
5671 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5672 | springlist = sha1.Result(); |
6915b806 JF |
5673 | } |
5674 | } | |
5675 | ||
5676 | if (invocation != nil) { | |
5677 | [invocation yieldToSelector:@selector(invoke)]; | |
aaae308d | 5678 | [self setTitle:@"COMPLETE"]; |
6915b806 | 5679 | } |
670a0494 | 5680 | |
22f8bed9 | 5681 | if (Finish_ < 4) { |
70d45c1e JF |
5682 | FileFd file; |
5683 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
5684 | _error->Discard(); | |
5685 | else { | |
5686 | MMap mmap(file, MMap::ReadOnly); | |
5687 | SHA1Summation sha1; | |
5688 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5689 | if (!(notifyconf == sha1.Result())) |
70d45c1e JF |
5690 | Finish_ = 4; |
5691 | } | |
22f8bed9 JF |
5692 | } |
5693 | ||
affeffc7 | 5694 | if (Finish_ < 3) { |
70d45c1e JF |
5695 | FileFd file; |
5696 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
5697 | _error->Discard(); | |
5698 | else { | |
5699 | MMap mmap(file, MMap::ReadOnly); | |
5700 | SHA1Summation sha1; | |
5701 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 5702 | if (!(springlist == sha1.Result())) |
70d45c1e JF |
5703 | Finish_ = 3; |
5704 | } | |
dddbc481 JF |
5705 | } |
5706 | ||
be860cc8 JF |
5707 | if (Finish_ < 2) { |
5708 | if (RestartSubstrate_) | |
5709 | Finish_ = 2; | |
5710 | } | |
5711 | ||
5712 | RestartSubstrate_ = false; | |
5713 | ||
bc8cd583 | 5714 | switch (Finish_) { |
aaae308d JF |
5715 | case 0: [progress_ setFinish:UCLocalize("RETURN_TO_CYDIA")]; break; /* XXX: Maybe UCLocalize("DONE")? */ |
5716 | case 1: [progress_ setFinish:UCLocalize("CLOSE_CYDIA")]; break; | |
5717 | case 2: [progress_ setFinish:UCLocalize("RESTART_SPRINGBOARD")]; break; | |
5718 | case 3: [progress_ setFinish:UCLocalize("RELOAD_SPRINGBOARD")]; break; | |
5719 | case 4: [progress_ setFinish:UCLocalize("REBOOT_DEVICE")]; break; | |
bc8cd583 JF |
5720 | } |
5721 | ||
eb403f34 | 5722 | UpdateExternalStatus(Finish_ == 0 ? 0 : 2); |
ef494bd8 | 5723 | |
aaae308d JF |
5724 | [progress_ setRunning:false]; |
5725 | [self updateProgress]; | |
5726 | ||
5727 | [self applyRightButton]; | |
31f3cfff JF |
5728 | } |
5729 | ||
6915b806 | 5730 | - (void) addProgressEvent:(CydiaProgressEvent *)event { |
aaae308d JF |
5731 | [progress_ addEvent:event]; |
5732 | [self updateProgress]; | |
baf80942 JF |
5733 | } |
5734 | ||
ff2d5dcd | 5735 | - (bool) isProgressCancelled { |
aaae308d JF |
5736 | return cancel_ == 2; |
5737 | } | |
5738 | ||
5739 | - (void) cancel { | |
5740 | cancel_ = 2; | |
5741 | [self updateCancel]; | |
5742 | } | |
5743 | ||
5744 | - (void) setCancellable:(bool)cancellable { | |
5745 | unsigned cancel(cancel_); | |
5746 | ||
5747 | if (!cancellable) | |
5748 | cancel_ = 0; | |
5749 | else if (cancel_ == 0) | |
5750 | cancel_ = 1; | |
5751 | ||
5752 | if (cancel != cancel_) | |
5753 | [self updateCancel]; | |
5754 | } | |
5755 | ||
5756 | - (void) setProgressCancellable:(NSNumber *)cancellable { | |
5757 | [self setCancellable:[cancellable boolValue]]; | |
baf80942 JF |
5758 | } |
5759 | ||
d885343d | 5760 | - (void) setProgressPercent:(NSNumber *)percent { |
b0b11d99 | 5761 | [progress_ setPercent:[percent floatValue]]; |
aaae308d | 5762 | [self updateProgress]; |
49048579 JF |
5763 | } |
5764 | ||
bcbac8f7 JF |
5765 | - (void) setProgressStatus:(NSDictionary *)status { |
5766 | if (status == nil) { | |
5767 | [progress_ setCurrent:0]; | |
5768 | [progress_ setTotal:0]; | |
5769 | [progress_ setSpeed:0]; | |
5770 | } else { | |
5771 | [progress_ setPercent:[[status objectForKey:@"Percent"] floatValue]]; | |
5772 | ||
5773 | [progress_ setCurrent:[[status objectForKey:@"Current"] floatValue]]; | |
5774 | [progress_ setTotal:[[status objectForKey:@"Total"] floatValue]]; | |
5775 | [progress_ setSpeed:[[status objectForKey:@"Speed"] floatValue]]; | |
5776 | } | |
5777 | ||
5778 | [self updateProgress]; | |
5779 | } | |
5780 | ||
36bb2ca2 JF |
5781 | @end |
5782 | /* }}} */ | |
dc088e63 | 5783 | |
46aa9775 | 5784 | /* Package Cell {{{ */ |
a9311516 | 5785 | @interface PackageCell : CyteTableViewCell < |
b97fcfc6 | 5786 | CyteTableViewCellDelegate |
c21004b9 | 5787 | > { |
7b33d201 JF |
5788 | _H<UIImage> icon_; |
5789 | _H<NSString> name_; | |
5790 | _H<NSString> description_; | |
3bd1c2a2 | 5791 | bool commercial_; |
7b33d201 JF |
5792 | _H<NSString> source_; |
5793 | _H<UIImage> badge_; | |
7b33d201 | 5794 | _H<UIImage> placard_; |
59f3d290 | 5795 | bool summarized_; |
36bb2ca2 | 5796 | } |
723a0072 | 5797 | |
36bb2ca2 | 5798 | - (PackageCell *) init; |
59f3d290 | 5799 | - (void) setPackage:(Package *)package asSummary:(bool)summary; |
ec97ef06 | 5800 | |
327624b6 JF |
5801 | - (void) drawContentRect:(CGRect)rect; |
5802 | ||
5803 | @end | |
5804 | ||
36bb2ca2 JF |
5805 | @implementation PackageCell |
5806 | ||
36bb2ca2 | 5807 | - (PackageCell *) init { |
327624b6 JF |
5808 | CGRect frame(CGRectMake(0, 0, 320, 74)); |
5809 | if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) { | |
5810 | UIView *content([self contentView]); | |
5811 | CGRect bounds([content bounds]); | |
04fe1349 | 5812 | |
b97fcfc6 | 5813 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
04fe1349 JF |
5814 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5815 | [content addSubview:content_]; | |
5816 | ||
327624b6 | 5817 | [content_ setDelegate:self]; |
327624b6 | 5818 | [content_ setOpaque:YES]; |
36bb2ca2 | 5819 | } return self; |
b4d89997 | 5820 | } |
b19871dd | 5821 | |
003fc610 | 5822 | - (NSString *) accessibilityLabel { |
353dda5b | 5823 | return name_; |
003fc610 GP |
5824 | } |
5825 | ||
59f3d290 JF |
5826 | - (void) setPackage:(Package *)package asSummary:(bool)summary { |
5827 | summarized_ = summary; | |
5828 | ||
7b33d201 JF |
5829 | icon_ = nil; |
5830 | name_ = nil; | |
5831 | description_ = nil; | |
5832 | source_ = nil; | |
5833 | badge_ = nil; | |
5834 | placard_ = nil; | |
7b33d201 | 5835 | |
80132602 JF |
5836 | if (package == nil) |
5837 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
5838 | else { | |
5839 | [package parse]; | |
31f3cfff | 5840 | |
80132602 | 5841 | Source *source = [package source]; |
b19871dd | 5842 | |
80132602 | 5843 | icon_ = [package icon]; |
78de2878 | 5844 | |
80132602 JF |
5845 | if (NSString *name = [package name]) |
5846 | name_ = [NSString stringWithString:name]; | |
ef055c6c | 5847 | |
9374f6b0 | 5848 | if (NSString *description = [package shortDescription]) |
80132602 | 5849 | description_ = [NSString stringWithString:description]; |
ef055c6c | 5850 | |
80132602 | 5851 | commercial_ = [package isCommercial]; |
36bb2ca2 | 5852 | |
80132602 JF |
5853 | NSString *label = nil; |
5854 | bool trusted = false; | |
b19871dd | 5855 | |
80132602 JF |
5856 | if (source != nil) { |
5857 | label = [source label]; | |
5858 | trusted = [source trusted]; | |
5859 | } else if ([[package id] isEqualToString:@"firmware"]) | |
5860 | label = UCLocalize("APPLE"); | |
5861 | else | |
5862 | label = [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("UNKNOWN"), UCLocalize("LOCAL")]; | |
b19871dd | 5863 | |
80132602 | 5864 | NSString *from(label); |
a54b1c10 | 5865 | |
80132602 JF |
5866 | NSString *section = [package simpleSection]; |
5867 | if (section != nil && ![section isEqualToString:label]) { | |
5868 | section = [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
5869 | from = [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), from, section]; | |
5870 | } | |
a54b1c10 | 5871 | |
80132602 | 5872 | source_ = [NSString stringWithFormat:UCLocalize("FROM"), from]; |
36bb2ca2 | 5873 | |
80132602 JF |
5874 | if (NSString *purpose = [package primaryPurpose]) |
5875 | badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]; | |
c390d3ab | 5876 | |
80132602 JF |
5877 | UIColor *color; |
5878 | NSString *placard; | |
5879 | ||
5880 | if (NSString *mode = [package mode]) { | |
5881 | if ([mode isEqualToString:@"REMOVE"] || [mode isEqualToString:@"PURGE"]) { | |
5882 | color = RemovingColor_; | |
86a333c6 | 5883 | placard = @"removing"; |
80132602 JF |
5884 | } else { |
5885 | color = InstallingColor_; | |
86a333c6 | 5886 | placard = @"installing"; |
80132602 | 5887 | } |
d832908d | 5888 | } else { |
80132602 JF |
5889 | color = [UIColor whiteColor]; |
5890 | ||
5891 | if ([package installed] != nil) | |
5892 | placard = @"installed"; | |
5893 | else | |
5894 | placard = nil; | |
d832908d JF |
5895 | } |
5896 | ||
80132602 | 5897 | [content_ setBackgroundColor:color]; |
d832908d | 5898 | |
80132602 JF |
5899 | if (placard != nil) |
5900 | placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/%@.png", App_, placard]]; | |
d832908d JF |
5901 | } |
5902 | ||
d832908d | 5903 | [self setNeedsDisplay]; |
327624b6 | 5904 | [content_ setNeedsDisplay]; |
3bd1c2a2 JF |
5905 | } |
5906 | ||
59f3d290 | 5907 | - (void) drawSummaryContentRect:(CGRect)rect { |
555aaf71 | 5908 | bool highlighted(highlighted_); |
ef055c6c | 5909 | float width([self bounds].size.width); |
dc63e78f | 5910 | |
59f3d290 JF |
5911 | if (icon_ != nil) { |
5912 | CGRect rect; | |
5913 | rect.size = [(UIImage *) icon_ size]; | |
5914 | ||
25c1dafb | 5915 | while (rect.size.width > 16 || rect.size.height > 16) { |
7e1f9f6a JF |
5916 | rect.size.width /= 2; |
5917 | rect.size.height /= 2; | |
5918 | } | |
59f3d290 | 5919 | |
86a333c6 JF |
5920 | rect.origin.x = 19 - rect.size.width / 2; |
5921 | rect.origin.y = 19 - rect.size.height / 2; | |
59f3d290 | 5922 | |
8323c1b9 | 5923 | [icon_ drawInRect:Retina(rect)]; |
59f3d290 JF |
5924 | } |
5925 | ||
5926 | if (badge_ != nil) { | |
5927 | CGRect rect; | |
5928 | rect.size = [(UIImage *) badge_ size]; | |
5929 | ||
5930 | rect.size.width /= 4; | |
5931 | rect.size.height /= 4; | |
5932 | ||
86a333c6 JF |
5933 | rect.origin.x = 25 - rect.size.width / 2; |
5934 | rect.origin.y = 25 - rect.size.height / 2; | |
59f3d290 | 5935 | |
8323c1b9 | 5936 | [badge_ drawInRect:Retina(rect)]; |
59f3d290 JF |
5937 | } |
5938 | ||
5d0438dc | 5939 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
59f3d290 JF |
5940 | UISetColor(White_); |
5941 | ||
5942 | if (!highlighted) | |
5943 | UISetColor(commercial_ ? Purple_ : Black_); | |
b129e6d9 | 5944 | [name_ drawAtPoint:CGPointMake(36, 8) forWidth:(width - (placard_ == nil ? 68 : 94)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
59f3d290 JF |
5945 | |
5946 | if (placard_ != nil) | |
86a333c6 | 5947 | [placard_ drawAtPoint:CGPointMake(width - 52, 11)]; |
59f3d290 JF |
5948 | } |
5949 | ||
5950 | - (void) drawNormalContentRect:(CGRect)rect { | |
5951 | bool highlighted(highlighted_); | |
5952 | float width([self bounds].size.width); | |
b19871dd | 5953 | |
baf80942 JF |
5954 | if (icon_ != nil) { |
5955 | CGRect rect; | |
7b33d201 | 5956 | rect.size = [(UIImage *) icon_ size]; |
baf80942 | 5957 | |
25c1dafb | 5958 | while (rect.size.width > 32 || rect.size.height > 32) { |
7e1f9f6a JF |
5959 | rect.size.width /= 2; |
5960 | rect.size.height /= 2; | |
5961 | } | |
baf80942 JF |
5962 | |
5963 | rect.origin.x = 25 - rect.size.width / 2; | |
5964 | rect.origin.y = 25 - rect.size.height / 2; | |
5965 | ||
8323c1b9 | 5966 | [icon_ drawInRect:Retina(rect)]; |
baf80942 | 5967 | } |
b19871dd | 5968 | |
c390d3ab | 5969 | if (badge_ != nil) { |
f79c810d | 5970 | CGRect rect; |
7b33d201 | 5971 | rect.size = [(UIImage *) badge_ size]; |
f79c810d JF |
5972 | |
5973 | rect.size.width /= 2; | |
5974 | rect.size.height /= 2; | |
5975 | ||
5976 | rect.origin.x = 36 - rect.size.width / 2; | |
5977 | rect.origin.y = 36 - rect.size.height / 2; | |
c390d3ab | 5978 | |
8323c1b9 | 5979 | [badge_ drawInRect:Retina(rect)]; |
c390d3ab JF |
5980 | } |
5981 | ||
5d0438dc | 5982 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
f641a0e5 | 5983 | UISetColor(White_); |
b19871dd | 5984 | |
555aaf71 | 5985 | if (!highlighted) |
3bd1c2a2 | 5986 | UISetColor(commercial_ ? Purple_ : Black_); |
b129e6d9 JF |
5987 | [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
5988 | [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; | |
b19871dd | 5989 | |
555aaf71 | 5990 | if (!highlighted) |
3bd1c2a2 | 5991 | UISetColor(commercial_ ? Purplish_ : Gray_); |
b129e6d9 | 5992 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:NSLineBreakByTruncatingTail]; |
670a0494 JF |
5993 | |
5994 | if (placard_ != nil) | |
01d93940 | 5995 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; |
ec97ef06 JF |
5996 | } |
5997 | ||
59f3d290 JF |
5998 | - (void) drawContentRect:(CGRect)rect { |
5999 | if (summarized_) | |
6000 | [self drawSummaryContentRect:rect]; | |
6001 | else | |
6002 | [self drawNormalContentRect:rect]; | |
6003 | } | |
6004 | ||
8da60fb7 JF |
6005 | @end |
6006 | /* }}} */ | |
36bb2ca2 | 6007 | /* Section Cell {{{ */ |
a9311516 | 6008 | @interface SectionCell : CyteTableViewCell < |
b97fcfc6 | 6009 | CyteTableViewCellDelegate |
c21004b9 | 6010 | > { |
7b33d201 JF |
6011 | _H<NSString> basic_; |
6012 | _H<NSString> section_; | |
6013 | _H<NSString> name_; | |
6014 | _H<NSString> count_; | |
6015 | _H<UIImage> icon_; | |
6016 | _H<UISwitch> switch_; | |
6d9712c4 | 6017 | BOOL editing_; |
b4d89997 | 6018 | } |
b19871dd | 6019 | |
6d9712c4 | 6020 | - (void) setSection:(Section *)section editing:(BOOL)editing; |
36bb2ca2 | 6021 | |
8da60fb7 JF |
6022 | @end |
6023 | ||
36bb2ca2 | 6024 | @implementation SectionCell |
8da60fb7 | 6025 | |
46aa9775 GP |
6026 | - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
6027 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
8dbf3abc | 6028 | icon_ = [UIImage imageNamed:@"folder.png"]; |
6415105e | 6029 | // XXX: this initial frame is wrong, but is fixed later |
7b33d201 | 6030 | switch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)] autorelease]; |
46aa9775 GP |
6031 | [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; |
6032 | ||
6033 | UIView *content([self contentView]); | |
6034 | CGRect bounds([content bounds]); | |
6035 | ||
b97fcfc6 | 6036 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
46aa9775 GP |
6037 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6038 | [content addSubview:content_]; | |
6039 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
6040 | ||
6041 | [content_ setDelegate:self]; | |
b4d89997 | 6042 | } return self; |
b19871dd JF |
6043 | } |
6044 | ||
6d9712c4 | 6045 | - (void) onSwitch:(id)sender { |
a171abd4 | 6046 | NSMutableDictionary *metadata([Sections_ objectForKey:basic_]); |
6d9712c4 JF |
6047 | if (metadata == nil) { |
6048 | metadata = [NSMutableDictionary dictionaryWithCapacity:2]; | |
0010fa77 | 6049 | [Sections_ setObject:metadata forKey:basic_]; |
6d9712c4 JF |
6050 | } |
6051 | ||
46aa9775 | 6052 | [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; |
6d9712c4 JF |
6053 | } |
6054 | ||
6055 | - (void) setSection:(Section *)section editing:(BOOL)editing { | |
6056 | if (editing != editing_) { | |
6057 | if (editing_) | |
6058 | [switch_ removeFromSuperview]; | |
6059 | else | |
6060 | [self addSubview:switch_]; | |
6061 | editing_ = editing; | |
6062 | } | |
6063 | ||
7b33d201 JF |
6064 | basic_ = nil; |
6065 | section_ = nil; | |
6066 | name_ = nil; | |
6067 | count_ = nil; | |
6d9712c4 | 6068 | |
36bb2ca2 | 6069 | if (section == nil) { |
7b33d201 | 6070 | name_ = UCLocalize("ALL_PACKAGES"); |
f641a0e5 | 6071 | count_ = nil; |
36bb2ca2 | 6072 | } else { |
e59669fd | 6073 | basic_ = [section name]; |
677b8415 | 6074 | section_ = [section localized]; |
0010fa77 | 6075 | |
7b33d201 | 6076 | name_ = section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : (NSString *) section_; |
3e5a9f5d | 6077 | count_ = [NSString stringWithFormat:@"%zd", [section count]]; |
6d9712c4 JF |
6078 | |
6079 | if (editing_) | |
46aa9775 | 6080 | [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; |
36bb2ca2 | 6081 | } |
46aa9775 | 6082 | |
c21004b9 | 6083 | [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; |
0abb648c JF |
6084 | [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; |
6085 | ||
46aa9775 | 6086 | [content_ setNeedsDisplay]; |
f641a0e5 JF |
6087 | } |
6088 | ||
77801ff1 JF |
6089 | - (void) setFrame:(CGRect)frame { |
6090 | [super setFrame:frame]; | |
46aa9775 | 6091 | |
77801ff1 | 6092 | CGRect rect([switch_ frame]); |
6415105e | 6093 | [switch_ setFrame:CGRectMake(frame.size.width - rect.size.width - 9, 9, rect.size.width, rect.size.height)]; |
77801ff1 JF |
6094 | } |
6095 | ||
003fc610 GP |
6096 | - (NSString *) accessibilityLabel { |
6097 | return name_; | |
6098 | } | |
6099 | ||
46aa9775 | 6100 | - (void) drawContentRect:(CGRect)rect { |
8cfba088 | 6101 | bool highlighted(highlighted_ && !editing_); |
bc11cf5b | 6102 | |
86a333c6 | 6103 | [icon_ drawInRect:CGRectMake(7, 7, 32, 32)]; |
f641a0e5 | 6104 | |
5d0438dc | 6105 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
f641a0e5 JF |
6106 | UISetColor(White_); |
6107 | ||
46aa9775 | 6108 | float width(rect.size.width); |
58241d4c | 6109 | if (editing_) |
6c1ec3c7 | 6110 | width -= 9 + [switch_ frame].size.width; |
58241d4c | 6111 | |
555aaf71 JF |
6112 | if (!highlighted) |
6113 | UISetColor(Black_); | |
b129e6d9 | 6114 | [name_ drawAtPoint:CGPointMake(48, 12) forWidth:(width - 58) withFont:Font18_ lineBreakMode:NSLineBreakByTruncatingTail]; |
6d9712c4 | 6115 | |
f641a0e5 JF |
6116 | CGSize size = [count_ sizeWithFont:Font14_]; |
6117 | ||
86a333c6 | 6118 | UISetColor(Folder_); |
f641a0e5 | 6119 | if (count_ != nil) |
8323c1b9 | 6120 | [count_ drawAtPoint:CGPointMake(Retina(10 + (30 - size.width) / 2), 18) withFont:Font12Bold_]; |
b19871dd JF |
6121 | } |
6122 | ||
36bb2ca2 JF |
6123 | @end |
6124 | /* }}} */ | |
b19871dd | 6125 | |
ab398adf | 6126 | /* File Table {{{ */ |
cd79e8cf | 6127 | @interface FileTable : CyteViewController < |
c21004b9 JF |
6128 | UITableViewDataSource, |
6129 | UITableViewDelegate | |
6130 | > { | |
36bb2ca2 | 6131 | _transient Database *database_; |
7b33d201 JF |
6132 | _H<Package> package_; |
6133 | _H<NSString> name_; | |
6134 | _H<NSMutableArray> files_; | |
bf7c998c | 6135 | _H<UITableView, 2> list_; |
ab398adf | 6136 | } |
b19871dd | 6137 | |
b5e7eebb | 6138 | - (id) initWithDatabase:(Database *)database; |
ab398adf JF |
6139 | - (void) setPackage:(Package *)package; |
6140 | ||
6141 | @end | |
6142 | ||
6143 | @implementation FileTable | |
6144 | ||
eb30da80 | 6145 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
ab398adf JF |
6146 | return files_ == nil ? 0 : [files_ count]; |
6147 | } | |
6148 | ||
21ea11a4 GP |
6149 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
6150 | return 24.0f; | |
6151 | }*/ | |
ab398adf | 6152 | |
46aa9775 | 6153 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
b5e7eebb GP |
6154 | static NSString *reuseIdentifier = @"Cell"; |
6155 | ||
46aa9775 GP |
6156 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
6157 | if (cell == nil) { | |
6158 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
6159 | [cell setFont:[UIFont systemFontOfSize:16]]; | |
ab398adf | 6160 | } |
46aa9775 | 6161 | [cell setText:[files_ objectAtIndex:indexPath.row]]; |
c21004b9 | 6162 | [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
b5e7eebb | 6163 | |
46aa9775 | 6164 | return cell; |
ab398adf | 6165 | } |
b19871dd | 6166 | |
fe8e721f GP |
6167 | - (NSURL *) navigationURL { |
6168 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/files", [package_ id]]]; | |
6169 | } | |
6170 | ||
6171 | - (void) loadView { | |
e8cbebe4 | 6172 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; |
fe8e721f GP |
6173 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6174 | [list_ setRowHeight:24.0f]; | |
7b33d201 | 6175 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 6176 | [list_ setDelegate:self]; |
e8cbebe4 | 6177 | [self setView:list_]; |
fe8e721f GP |
6178 | } |
6179 | ||
6180 | - (void) viewDidLoad { | |
7d887d0b JF |
6181 | [super viewDidLoad]; |
6182 | ||
fe8e721f GP |
6183 | [[self navigationItem] setTitle:UCLocalize("INSTALLED_FILES")]; |
6184 | } | |
6185 | ||
6186 | - (void) releaseSubviews { | |
fe8e721f | 6187 | list_ = nil; |
7be3eb32 | 6188 | |
4f9acb7c JF |
6189 | package_ = nil; |
6190 | files_ = nil; | |
6191 | ||
7be3eb32 | 6192 | [super releaseSubviews]; |
fe8e721f GP |
6193 | } |
6194 | ||
b5e7eebb GP |
6195 | - (id) initWithDatabase:(Database *)database { |
6196 | if ((self = [super init]) != nil) { | |
ab398adf | 6197 | database_ = database; |
ab398adf JF |
6198 | } return self; |
6199 | } | |
6200 | ||
6201 | - (void) setPackage:(Package *)package { | |
7b33d201 JF |
6202 | package_ = nil; |
6203 | name_ = nil; | |
ab398adf | 6204 | |
4f9acb7c | 6205 | files_ = [NSMutableArray arrayWithCapacity:32]; |
ab398adf JF |
6206 | |
6207 | if (package != nil) { | |
7b33d201 JF |
6208 | package_ = package; |
6209 | name_ = [package id]; | |
ab398adf | 6210 | |
affeffc7 JF |
6211 | if (NSArray *files = [package files]) |
6212 | [files_ addObjectsFromArray:files]; | |
ab398adf | 6213 | |
9ea8d159 JF |
6214 | if ([files_ count] != 0) { |
6215 | if ([[files_ objectAtIndex:0] isEqualToString:@"/."]) | |
6216 | [files_ removeObjectAtIndex:0]; | |
a54b1c10 | 6217 | [files_ sortUsingSelector:@selector(compareByPath:)]; |
2388b078 JF |
6218 | |
6219 | NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8]; | |
6220 | [stack addObject:@"/"]; | |
6221 | ||
6222 | for (int i(0), e([files_ count]); i != e; ++i) { | |
6223 | NSString *file = [files_ objectAtIndex:i]; | |
6224 | while (![file hasPrefix:[stack lastObject]]) | |
6225 | [stack removeLastObject]; | |
6226 | NSString *directory = [stack lastObject]; | |
6227 | [stack addObject:[file stringByAppendingString:@"/"]]; | |
6228 | [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", | |
a54b1c10 | 6229 | ([stack count] - 2) * 3, "", |
2388b078 JF |
6230 | [file substringFromIndex:[directory length]] |
6231 | ]]; | |
6232 | } | |
ab398adf JF |
6233 | } |
6234 | } | |
6235 | ||
6236 | [list_ reloadData]; | |
6237 | } | |
6238 | ||
ab398adf | 6239 | - (void) reloadData { |
fe8e721f GP |
6240 | [super reloadData]; |
6241 | ||
ab398adf | 6242 | [self setPackage:[database_ packageWithName:name_]]; |
ab398adf | 6243 | } |
b4d89997 | 6244 | |
b4d89997 | 6245 | @end |
36bb2ca2 | 6246 | /* }}} */ |
adb61bda | 6247 | /* Package Controller {{{ */ |
a576488f | 6248 | @interface CYPackageController : CydiaWebViewController < |
9daa7f25 DH |
6249 | UIActionSheetDelegate |
6250 | > { | |
770f2a8e | 6251 | _transient Database *database_; |
5d79f7bf JF |
6252 | _H<Package> package_; |
6253 | _H<NSString> name_; | |
3bd1c2a2 | 6254 | bool commercial_; |
3217d35f | 6255 | std::vector<std::pair<_H<NSString>, _H<NSString>>> buttons_; |
5d79f7bf | 6256 | _H<UIBarButtonItem> button_; |
b31b87cc JF |
6257 | } |
6258 | ||
f050e4d9 | 6259 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer; |
b4d89997 | 6260 | |
36bb2ca2 | 6261 | @end |
b4d89997 | 6262 | |
f6e13561 | 6263 | @implementation CYPackageController |
b4d89997 | 6264 | |
fe8e721f | 6265 | - (NSURL *) navigationURL { |
5d79f7bf | 6266 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@", (id) name_]]; |
fe8e721f GP |
6267 | } |
6268 | ||
5a09ae08 | 6269 | - (void) _clickButtonWithName:(NSString *)name { |
3217d35f | 6270 | if ([name isEqualToString:@"CLEAR"]) |
dc63e78f | 6271 | [delegate_ clearPackage:package_]; |
3217d35f | 6272 | else if ([name isEqualToString:@"INSTALL"]) |
5a09ae08 | 6273 | [delegate_ installPackage:package_]; |
3217d35f | 6274 | else if ([name isEqualToString:@"REINSTALL"]) |
5a09ae08 | 6275 | [delegate_ installPackage:package_]; |
3217d35f | 6276 | else if ([name isEqualToString:@"REMOVE"]) |
5a09ae08 | 6277 | [delegate_ removePackage:package_]; |
3217d35f | 6278 | else if ([name isEqualToString:@"UPGRADE"]) |
5a09ae08 JF |
6279 | [delegate_ installPackage:package_]; |
6280 | else _assert(false); | |
6281 | } | |
6282 | ||
674dce72 | 6283 | - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { |
1cedb821 | 6284 | NSString *context([sheet context]); |
5a09ae08 | 6285 | |
1cedb821 | 6286 | if ([context isEqualToString:@"modify"]) { |
674dce72 | 6287 | if (button != [sheet cancelButtonIndex]) { |
60b6595c JF |
6288 | if (IsWildcat_) |
6289 | [self performSelector:@selector(_clickButtonWithName:) withObject:buttons_[button].first afterDelay:0]; | |
6290 | else | |
6291 | [self _clickButtonWithName:buttons_[button].first]; | |
674dce72 | 6292 | } |
bc11cf5b | 6293 | |
60b6595c | 6294 | [sheet dismissWithClickedButtonIndex:button animated:YES]; |
674dce72 | 6295 | } |
b4d89997 | 6296 | } |
2367a917 | 6297 | |
3e9c9e85 | 6298 | - (bool) _allowJavaScriptPanel { |
3bd1c2a2 | 6299 | return commercial_; |
3e9c9e85 JF |
6300 | } |
6301 | ||
9487f027 | 6302 | #if !AlwaysReload |
9daa7f25 | 6303 | - (void) _customButtonClicked { |
3217d35f | 6304 | size_t count(buttons_.size()); |
670a0494 JF |
6305 | if (count == 0) |
6306 | return; | |
2367a917 | 6307 | |
5a09ae08 | 6308 | if (count == 1) |
3217d35f | 6309 | [self _clickButtonWithName:buttons_[0].first]; |
5a09ae08 | 6310 | else { |
674dce72 | 6311 | NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; |
3217d35f JF |
6312 | for (const auto &button : buttons_) |
6313 | [buttons addObject:button.second]; | |
36bb2ca2 | 6314 | |
674dce72 | 6315 | UIActionSheet *sheet = [[[UIActionSheet alloc] |
9ea8d159 | 6316 | initWithTitle:nil |
36bb2ca2 | 6317 | delegate:self |
674dce72 GP |
6318 | cancelButtonTitle:nil |
6319 | destructiveButtonTitle:nil | |
6320 | otherButtonTitles:nil | |
6321 | ] autorelease]; | |
6322 | ||
6323 | for (NSString *button in buttons) [sheet addButtonWithTitle:button]; | |
6324 | if (!IsWildcat_) { | |
6325 | [sheet addButtonWithTitle:UCLocalize("CANCEL")]; | |
6326 | [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; | |
6327 | } | |
6328 | [sheet setContext:@"modify"]; | |
bc11cf5b | 6329 | |
b5e7eebb | 6330 | [delegate_ showActionSheet:sheet fromItem:[[self navigationItem] rightBarButtonItem]]; |
36bb2ca2 | 6331 | } |
b4d89997 | 6332 | } |
12b59862 | 6333 | |
2e6c1426 | 6334 | - (void) reloadButtonClicked { |
07d0e88e JF |
6335 | if (commercial_ && function_ == nil && [package_ uninstalled]) |
6336 | return; | |
6337 | [self customButtonClicked]; | |
2fad210a GP |
6338 | } |
6339 | ||
6340 | - (void) applyLoadingTitle { | |
6341 | // Don't show "Loading" as the title. Ever. | |
2e6c1426 | 6342 | } |
a5938ea5 DH |
6343 | |
6344 | - (UIBarButtonItem *) rightButton { | |
2634b249 | 6345 | return button_; |
a5938ea5 | 6346 | } |
9487f027 | 6347 | #endif |
2367a917 | 6348 | |
77259cab JF |
6349 | - (void) setPageColor:(UIColor *)color { |
6350 | return [super setPageColor:nil]; | |
6351 | } | |
6352 | ||
f050e4d9 | 6353 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name withReferrer:(NSString *)referrer { |
b5e7eebb | 6354 | if ((self = [super init]) != nil) { |
36bb2ca2 | 6355 | database_ = database; |
5612913e | 6356 | name_ = name == nil ? @"" : [NSString stringWithString:name]; |
f050e4d9 | 6357 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]] withReferrer:referrer]; |
36bb2ca2 | 6358 | } return self; |
dc5812ec JF |
6359 | } |
6360 | ||
57e8b225 | 6361 | - (void) reloadData { |
9dac415b JF |
6362 | [super reloadData]; |
6363 | ||
57e8b225 | 6364 | package_ = [database_ packageWithName:name_]; |
36bb2ca2 | 6365 | |
3217d35f | 6366 | buttons_.clear(); |
5a09ae08 | 6367 | |
57e8b225 | 6368 | if (package_ != nil) { |
5d79f7bf | 6369 | [(Package *) package_ parse]; |
68d927e2 | 6370 | |
57e8b225 | 6371 | commercial_ = [package_ isCommercial]; |
36bb2ca2 | 6372 | |
dc63e78f | 6373 | if ([package_ mode] != nil) |
3217d35f | 6374 | buttons_.push_back(std::make_pair(@"CLEAR", UCLocalize("CLEAR"))); |
5a09ae08 | 6375 | if ([package_ source] == nil); |
31f3cfff | 6376 | else if ([package_ upgradableAndEssential:NO]) |
3217d35f | 6377 | buttons_.push_back(std::make_pair(@"UPGRADE", UCLocalize("UPGRADE"))); |
6a155117 | 6378 | else if ([package_ uninstalled]) |
3217d35f | 6379 | buttons_.push_back(std::make_pair(@"INSTALL", UCLocalize("INSTALL"))); |
5a09ae08 | 6380 | else |
3217d35f | 6381 | buttons_.push_back(std::make_pair(@"REINSTALL", UCLocalize("REINSTALL"))); |
6a155117 | 6382 | if (![package_ uninstalled]) |
3217d35f | 6383 | buttons_.push_back(std::make_pair(@"REMOVE", UCLocalize("REMOVE"))); |
2634b249 | 6384 | } |
f79a4512 | 6385 | |
2634b249 | 6386 | NSString *title; |
3217d35f | 6387 | switch (buttons_.size()) { |
2634b249 | 6388 | case 0: title = nil; break; |
3217d35f | 6389 | case 1: title = buttons_[0].second; break; |
2634b249 | 6390 | default: title = UCLocalize("MODIFY"); break; |
36bb2ca2 | 6391 | } |
2634b249 | 6392 | |
5d79f7bf | 6393 | button_ = [[[UIBarButtonItem alloc] |
2634b249 JF |
6394 | initWithTitle:title |
6395 | style:UIBarButtonItemStylePlain | |
6396 | target:self | |
6397 | action:@selector(customButtonClicked) | |
5d79f7bf | 6398 | ] autorelease]; |
b5e7eebb | 6399 | } |
f79a4512 | 6400 | |
3bd1c2a2 JF |
6401 | - (bool) isLoading { |
6402 | return commercial_ ? [super isLoading] : false; | |
9fe5e5f8 JF |
6403 | } |
6404 | ||
b4d89997 JF |
6405 | @end |
6406 | /* }}} */ | |
5829aea2 | 6407 | |
f50860ee | 6408 | /* Package List Controller {{{ */ |
cd79e8cf | 6409 | @interface PackageListController : CyteViewController < |
c21004b9 JF |
6410 | UITableViewDataSource, |
6411 | UITableViewDelegate | |
6412 | > { | |
36bb2ca2 | 6413 | _transient Database *database_; |
0175295c | 6414 | unsigned era_; |
56bf1e78 | 6415 | _H<NSArray> packages_; |
e5491e28 | 6416 | _H<NSArray> sections_; |
bf7c998c | 6417 | _H<UITableView, 2> list_; |
aeeb755b JF |
6418 | |
6419 | _H<NSArray> thumbs_; | |
6420 | std::vector<NSInteger> offset_; | |
6421 | ||
7b33d201 | 6422 | _H<NSString> title_; |
56bf1e78 | 6423 | unsigned reloading_; |
dc5812ec JF |
6424 | } |
6425 | ||
f50860ee | 6426 | - (id) initWithDatabase:(Database *)database title:(NSString *)title; |
b4d89997 | 6427 | - (void) setDelegate:(id)delegate; |
a0be02eb | 6428 | - (void) resetCursor; |
59f3d290 | 6429 | - (void) clearData; |
b4d89997 | 6430 | |
e5491e28 JF |
6431 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages; |
6432 | ||
b4d89997 JF |
6433 | @end |
6434 | ||
f50860ee | 6435 | @implementation PackageListController |
b4d89997 | 6436 | |
f050e4d9 JF |
6437 | - (NSURL *) referrerURL { |
6438 | return [self navigationURL]; | |
6439 | } | |
6440 | ||
59f3d290 JF |
6441 | - (bool) isSummarized { |
6442 | return false; | |
6443 | } | |
6444 | ||
9c5737d5 JF |
6445 | - (bool) showsSections { |
6446 | return true; | |
6447 | } | |
6448 | ||
f50860ee GP |
6449 | - (void) deselectWithAnimation:(BOOL)animated { |
6450 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
6451 | } | |
6452 | ||
bfb45dcb GP |
6453 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve { |
6454 | CGRect base = [[self view] bounds]; | |
6455 | base.size.height -= bounds.size.height; | |
6456 | base.origin = [list_ frame].origin; | |
6457 | ||
6458 | [UIView beginAnimations:nil context:NULL]; | |
6459 | [UIView setAnimationBeginsFromCurrentState:YES]; | |
6460 | [UIView setAnimationCurve:curve]; | |
6461 | [UIView setAnimationDuration:duration]; | |
6462 | [list_ setFrame:base]; | |
6463 | [UIView commitAnimations]; | |
6464 | } | |
6465 | ||
6466 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration { | |
6467 | [self resizeForKeyboardBounds:bounds duration:duration curve:UIViewAnimationCurveLinear]; | |
6468 | } | |
6469 | ||
6470 | - (void) resizeForKeyboardBounds:(CGRect)bounds { | |
6471 | [self resizeForKeyboardBounds:bounds duration:0]; | |
6472 | } | |
6473 | ||
655c7ded JF |
6474 | - (void) getKeyboardCurve:(UIViewAnimationCurve *)curve duration:(NSTimeInterval *)duration forNotification:(NSNotification *)notification { |
6475 | if (&UIKeyboardAnimationCurveUserInfoKey == NULL) | |
6476 | *curve = UIViewAnimationCurveEaseInOut; | |
6477 | else | |
6478 | [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:curve]; | |
6479 | ||
6480 | if (&UIKeyboardAnimationDurationUserInfoKey == NULL) | |
6481 | *duration = 0.3; | |
6482 | else | |
6483 | [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:duration]; | |
6484 | } | |
6485 | ||
bfb45dcb GP |
6486 | - (void) keyboardWillShow:(NSNotification *)notification { |
6487 | CGRect bounds; | |
6488 | CGPoint center; | |
bfb45dcb GP |
6489 | [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds]; |
6490 | [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:¢er]; | |
655c7ded JF |
6491 | |
6492 | NSTimeInterval duration; | |
6493 | UIViewAnimationCurve curve; | |
6494 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; | |
bfb45dcb | 6495 | |
8323c1b9 | 6496 | CGRect kbframe = CGRectMake(Retina(center.x - bounds.size.width / 2), Retina(center.y - bounds.size.height / 2), bounds.size.width, bounds.size.height); |
38991110 | 6497 | UIViewController *base = self; |
19f2d77f JF |
6498 | while ([base parentOrPresentingViewController] != nil) |
6499 | base = [base parentOrPresentingViewController]; | |
38991110 | 6500 | CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]]; |
bfb45dcb GP |
6501 | CGRect intersection = CGRectIntersection(viewframe, kbframe); |
6502 | ||
2e35f65f | 6503 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) |
0b7516cf | 6504 | intersection.size.height += CYStatusBarHeight(); |
2e35f65f | 6505 | |
bfb45dcb GP |
6506 | [self resizeForKeyboardBounds:intersection duration:duration curve:curve]; |
6507 | } | |
6508 | ||
6509 | - (void) keyboardWillHide:(NSNotification *)notification { | |
6510 | NSTimeInterval duration; | |
6511 | UIViewAnimationCurve curve; | |
655c7ded | 6512 | [self getKeyboardCurve:&curve duration:&duration forNotification:notification]; |
bfb45dcb GP |
6513 | |
6514 | [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve]; | |
6515 | } | |
6516 | ||
6517 | - (void) viewWillAppear:(BOOL)animated { | |
6518 | [super viewWillAppear:animated]; | |
6519 | ||
6520 | [self resizeForKeyboardBounds:CGRectZero]; | |
6521 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
6522 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
6523 | } | |
6524 | ||
6525 | - (void) viewWillDisappear:(BOOL)animated { | |
6526 | [super viewWillDisappear:animated]; | |
6527 | ||
6528 | [self resizeForKeyboardBounds:CGRectZero]; | |
6529 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
6530 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
6531 | } | |
6532 | ||
f50860ee GP |
6533 | - (void) viewDidAppear:(BOOL)animated { |
6534 | [super viewDidAppear:animated]; | |
6535 | [self deselectWithAnimation:animated]; | |
6536 | } | |
6537 | ||
6538 | - (void) didSelectPackage:(Package *)package { | |
f050e4d9 | 6539 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id] withReferrer:[[self referrerURL] absoluteString]] autorelease]); |
f50860ee GP |
6540 | [view setDelegate:delegate_]; |
6541 | [[self navigationController] pushViewController:view animated:YES]; | |
6542 | } | |
6543 | ||
327624b6 JF |
6544 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
6545 | NSInteger count([sections_ count]); | |
6546 | return count == 0 ? 1 : count; | |
b4d89997 | 6547 | } |
2367a917 | 6548 | |
327624b6 | 6549 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
a891c345 | 6550 | if ([sections_ count] == 0 || [[sections_ objectAtIndex:section] count] == 0) |
327624b6 | 6551 | return nil; |
b4d89997 JF |
6552 | return [[sections_ objectAtIndex:section] name]; |
6553 | } | |
dc5812ec | 6554 | |
327624b6 JF |
6555 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
6556 | if ([sections_ count] == 0) | |
6557 | return 0; | |
6558 | return [[sections_ objectAtIndex:section] count]; | |
b4d89997 | 6559 | } |
dc5812ec | 6560 | |
327624b6 | 6561 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
0175295c JF |
6562 | @synchronized (database_) { |
6563 | if ([database_ era] != era_) | |
6564 | return nil; | |
6565 | ||
327624b6 JF |
6566 | Section *section([sections_ objectAtIndex:[path section]]); |
6567 | NSInteger row([path row]); | |
6568 | Package *package([packages_ objectAtIndex:([section row] + row)]); | |
0175295c JF |
6569 | return [[package retain] autorelease]; |
6570 | } } | |
dc5812ec | 6571 | |
327624b6 | 6572 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
c21004b9 | 6573 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
327624b6 JF |
6574 | if (cell == nil) |
6575 | cell = [[[PackageCell alloc] init] autorelease]; | |
60bef540 JF |
6576 | |
6577 | Package *package([database_ packageWithName:[[self packageAtIndexPath:path] id]]); | |
6578 | [cell setPackage:package asSummary:[self isSummarized]]; | |
327624b6 | 6579 | return cell; |
b4d89997 | 6580 | } |
dc5812ec | 6581 | |
f50860ee | 6582 | - (void) tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path { |
327624b6 | 6583 | Package *package([self packageAtIndexPath:path]); |
59c6ae22 | 6584 | package = [database_ packageWithName:[package id]]; |
f50860ee | 6585 | [self didSelectPackage:package]; |
327624b6 JF |
6586 | } |
6587 | ||
6588 | - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { | |
aeeb755b | 6589 | return thumbs_; |
327624b6 JF |
6590 | } |
6591 | ||
a891c345 | 6592 | - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { |
aeeb755b | 6593 | return offset_[index]; |
dc5812ec JF |
6594 | } |
6595 | ||
59f3d290 JF |
6596 | - (void) updateHeight { |
6597 | [list_ setRowHeight:([self isSummarized] ? 38 : 73)]; | |
6598 | } | |
6599 | ||
f50860ee GP |
6600 | - (id) initWithDatabase:(Database *)database title:(NSString *)title { |
6601 | if ((self = [super init]) != nil) { | |
36bb2ca2 | 6602 | database_ = database; |
f50860ee GP |
6603 | title_ = [title copy]; |
6604 | [[self navigationItem] setTitle:title_]; | |
61cc4dbc JF |
6605 | } return self; |
6606 | } | |
dc5812ec | 6607 | |
61cc4dbc | 6608 | - (void) loadView { |
b62b3788 JF |
6609 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
6610 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
6611 | [self setView:view]; | |
6612 | ||
6613 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; | |
61cc4dbc | 6614 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
b62b3788 | 6615 | [view addSubview:list_]; |
1527b095 | 6616 | |
61cc4dbc JF |
6617 | // XXX: is 20 the most optimal number here? |
6618 | [list_ setSectionIndexMinimumDisplayRowCount:20]; | |
59f3d290 | 6619 | |
61cc4dbc JF |
6620 | [(UITableView *) list_ setDataSource:self]; |
6621 | [list_ setDelegate:self]; | |
6622 | ||
6623 | [self updateHeight]; | |
6624 | } | |
6625 | ||
6626 | - (void) releaseSubviews { | |
6627 | list_ = nil; | |
7be3eb32 | 6628 | |
4f9acb7c JF |
6629 | packages_ = nil; |
6630 | sections_ = nil; | |
aeeb755b JF |
6631 | |
6632 | thumbs_ = nil; | |
6633 | offset_.clear(); | |
4f9acb7c | 6634 | |
7be3eb32 | 6635 | [super releaseSubviews]; |
dc5812ec JF |
6636 | } |
6637 | ||
6638 | - (void) setDelegate:(id)delegate { | |
2367a917 | 6639 | delegate_ = delegate; |
b4d89997 JF |
6640 | } |
6641 | ||
3025d5b4 JF |
6642 | - (bool) shouldYield { |
6643 | return false; | |
6644 | } | |
b4d89997 | 6645 | |
56bf1e78 JF |
6646 | - (bool) shouldBlock { |
6647 | return false; | |
6648 | } | |
6649 | ||
9c5737d5 | 6650 | - (NSMutableArray *) _reloadPackages { |
695fdd5c | 6651 | @synchronized (database_) { |
c28bc6f1 JF |
6652 | era_ = [database_ era]; |
6653 | NSArray *packages([database_ packages]); | |
6654 | ||
9c5737d5 | 6655 | return [NSMutableArray arrayWithArray:packages]; |
695fdd5c | 6656 | } } |
3025d5b4 JF |
6657 | |
6658 | - (void) _reloadData { | |
56bf1e78 JF |
6659 | if (reloading_ != 0) { |
6660 | reloading_ = 2; | |
6661 | return; | |
6662 | } | |
6663 | ||
e5491e28 | 6664 | NSMutableArray *packages; |
ae60e2c1 | 6665 | |
0c8f53c0 | 6666 | reload: |
3025d5b4 | 6667 | if ([self shouldYield]) { |
bec28dda JF |
6668 | do { |
6669 | UIProgressHUD *hud; | |
56bf1e78 | 6670 | |
bec28dda JF |
6671 | if (![self shouldBlock]) |
6672 | hud = nil; | |
6673 | else { | |
6674 | hud = [delegate_ addProgressHUD]; | |
6675 | [hud setText:UCLocalize("LOADING")]; | |
6676 | } | |
56bf1e78 | 6677 | |
56bf1e78 | 6678 | reloading_ = 1; |
ae60e2c1 | 6679 | packages = [self yieldToSelector:@selector(_reloadPackages)]; |
bec28dda JF |
6680 | |
6681 | if (hud != nil) | |
6682 | [delegate_ removeProgressHUD:hud]; | |
56bf1e78 | 6683 | } while (reloading_ == 2); |
3025d5b4 | 6684 | } else { |
ae60e2c1 | 6685 | packages = [self _reloadPackages]; |
3025d5b4 | 6686 | } |
36bb2ca2 | 6687 | |
0c8f53c0 JF |
6688 | @synchronized (database_) { |
6689 | if (era_ != [database_ era]) | |
6690 | goto reload; | |
6691 | reloading_ = 0; | |
6692 | ||
aeeb755b JF |
6693 | thumbs_ = nil; |
6694 | offset_.clear(); | |
6695 | ||
ae60e2c1 | 6696 | packages_ = packages; |
aeeb755b JF |
6697 | |
6698 | if ([self showsSections]) | |
6699 | sections_ = [self sectionsForPackages:packages]; | |
6700 | else { | |
6701 | Section *section([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); | |
6702 | [section setCount:[packages_ count]]; | |
6703 | sections_ = [NSArray arrayWithObject:section]; | |
6704 | } | |
ae60e2c1 | 6705 | |
e5491e28 JF |
6706 | [self updateHeight]; |
6707 | ||
6708 | _profile(PackageTable$reloadData$List) | |
6709 | [(UITableView *) list_ setDataSource:self]; | |
6710 | [list_ reloadData]; | |
6711 | _end | |
1a83afc6 JF |
6712 | } |
6713 | ||
6714 | PrintTimes(); | |
6715 | } | |
e5491e28 JF |
6716 | |
6717 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { | |
aeeb755b JF |
6718 | Section *prefix([[[Section alloc] initWithName:nil row:0 localize:NO] autorelease]); |
6719 | size_t end([packages count]); | |
b4d89997 | 6720 | |
aeeb755b JF |
6721 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); |
6722 | Section *section(prefix); | |
c4dcf2c2 | 6723 | |
aeeb755b JF |
6724 | thumbs_ = CollationThumbs_; |
6725 | offset_ = CollationOffset_; | |
b4d89997 | 6726 | |
aeeb755b JF |
6727 | size_t offset(0); |
6728 | size_t offsets([CollationStarts_ count]); | |
b4d89997 | 6729 | |
aeeb755b JF |
6730 | NSString *start([CollationStarts_ objectAtIndex:offset]); |
6731 | size_t length([start length]); | |
327624b6 | 6732 | |
aeeb755b JF |
6733 | for (size_t index(0); index != end; ++index) { |
6734 | if (start != nil) { | |
6735 | Package *package([packages objectAtIndex:index]); | |
22fd24dd | 6736 | NSString *name(PackageName(package, @selector(cyname))); |
a891c345 | 6737 | |
aeeb755b | 6738 | //while ([start compare:name options:NSNumericSearch range:NSMakeRange(0, length) locale:CollationLocale_] != NSOrderedDescending) { |
22fd24dd | 6739 | while (StringNameCompare(start, name, length) != kCFCompareGreaterThan) { |
aeeb755b JF |
6740 | NSString *title([CollationTitles_ objectAtIndex:offset]); |
6741 | section = [[[Section alloc] initWithName:title row:index localize:NO] autorelease]; | |
6742 | [sections addObject:section]; | |
a891c345 | 6743 | |
aeeb755b JF |
6744 | start = ++offset == offsets ? nil : [CollationStarts_ objectAtIndex:offset]; |
6745 | if (start == nil) | |
6746 | break; | |
6747 | length = [start length]; | |
808c6eb6 | 6748 | } |
59f3d290 JF |
6749 | } |
6750 | ||
aeeb755b JF |
6751 | [section addToCount]; |
6752 | } | |
a891c345 | 6753 | |
aeeb755b JF |
6754 | for (; offset != offsets; ++offset) { |
6755 | NSString *title([CollationTitles_ objectAtIndex:offset]); | |
6756 | Section *section([[[Section alloc] initWithName:title row:end localize:NO] autorelease]); | |
6757 | [sections addObject:section]; | |
6758 | } | |
a891c345 | 6759 | |
aeeb755b JF |
6760 | if ([prefix count] != 0) { |
6761 | Section *suffix([sections lastObject]); | |
6762 | [prefix setName:[suffix name]]; | |
6763 | [suffix setName:nil]; | |
6764 | [sections insertObject:prefix atIndex:(offsets - 1)]; | |
a891c345 | 6765 | } |
b4d89997 | 6766 | |
e5491e28 JF |
6767 | return sections; |
6768 | } | |
b4d89997 | 6769 | |
3025d5b4 JF |
6770 | - (void) reloadData { |
6771 | [super reloadData]; | |
6d246265 JF |
6772 | |
6773 | if ([self shouldYield]) | |
6774 | [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; | |
6775 | else | |
6776 | [self _reloadData]; | |
3025d5b4 JF |
6777 | } |
6778 | ||
a0be02eb | 6779 | - (void) resetCursor { |
4c6a29cd | 6780 | [list_ scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; |
a0be02eb JF |
6781 | } |
6782 | ||
59f3d290 JF |
6783 | - (void) clearData { |
6784 | [self updateHeight]; | |
6785 | ||
6786 | [list_ setDataSource:nil]; | |
6787 | [list_ reloadData]; | |
6788 | ||
6789 | [self resetCursor]; | |
6790 | } | |
6791 | ||
a3328c28 JF |
6792 | @end |
6793 | /* }}} */ | |
f50860ee | 6794 | /* Filtered Package List Controller {{{ */ |
89bdef78 JF |
6795 | typedef Function<bool, Package *> PackageFilter; |
6796 | typedef Function<void, NSMutableArray *> PackageSorter; | |
f50860ee | 6797 | @interface FilteredPackageListController : PackageListController { |
89bdef78 JF |
6798 | PackageFilter filter_; |
6799 | PackageSorter sorter_; | |
a3328c28 JF |
6800 | } |
6801 | ||
89bdef78 | 6802 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter; |
3025d5b4 | 6803 | |
89bdef78 JF |
6804 | - (void) setFilter:(PackageFilter)filter; |
6805 | - (void) setSorter:(PackageSorter)sorter; | |
a3328c28 JF |
6806 | |
6807 | @end | |
6808 | ||
f50860ee | 6809 | @implementation FilteredPackageListController |
a3328c28 | 6810 | |
89bdef78 | 6811 | - (void) setFilter:(PackageFilter)filter { |
aa32d91b | 6812 | @synchronized (self) { |
01d93940 | 6813 | filter_ = filter; |
55066b9e JF |
6814 | } } |
6815 | ||
89bdef78 | 6816 | - (void) setSorter:(PackageSorter)sorter { |
55066b9e | 6817 | @synchronized (self) { |
89bdef78 | 6818 | sorter_ = sorter; |
d84597fe | 6819 | } } |
01d93940 | 6820 | |
9c5737d5 | 6821 | - (NSMutableArray *) _reloadPackages { |
aa32d91b | 6822 | @synchronized (database_) { |
c28bc6f1 | 6823 | era_ = [database_ era]; |
c28bc6f1 | 6824 | |
dd4e70dc JF |
6825 | NSArray *packages([database_ packages]); |
6826 | NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); | |
aa32d91b | 6827 | |
89bdef78 | 6828 | PackageFilter filter; |
dd4e70dc | 6829 | PackageSorter sorter; |
aa32d91b JF |
6830 | |
6831 | @synchronized (self) { | |
aa32d91b | 6832 | filter = filter_; |
89bdef78 | 6833 | sorter = sorter_; |
aa32d91b JF |
6834 | } |
6835 | ||
6836 | _profile(PackageTable$reloadData$Filter) | |
6837 | for (Package *package in packages) | |
89bdef78 | 6838 | if ([package valid] && filter(package)) |
aa32d91b | 6839 | [filtered addObject:package]; |
76933519 | 6840 | _end |
aa32d91b | 6841 | |
89bdef78 JF |
6842 | if (sorter) |
6843 | sorter(filtered); | |
aa32d91b | 6844 | return filtered; |
dd4e70dc | 6845 | } } |
55066b9e | 6846 | |
89bdef78 | 6847 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(PackageFilter)filter { |
55066b9e JF |
6848 | if ((self = [super initWithDatabase:database title:title]) != nil) { |
6849 | [self setFilter:filter]; | |
a3328c28 JF |
6850 | } return self; |
6851 | } | |
6852 | ||
dc5812ec | 6853 | @end |
b5e7eebb GP |
6854 | /* }}} */ |
6855 | ||
5829aea2 | 6856 | /* Home Controller {{{ */ |
a576488f | 6857 | @interface HomeController : CydiaWebViewController { |
02f21c73 JF |
6858 | CFRunLoopRef runloop_; |
6859 | SCNetworkReachabilityRef reachability_; | |
b4d89997 | 6860 | } |
6840bff3 | 6861 | |
7b0ce2da | 6862 | @end |
b4d89997 | 6863 | |
5829aea2 | 6864 | @implementation HomeController |
46aa9775 | 6865 | |
02f21c73 JF |
6866 | static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachability, SCNetworkReachabilityFlags flags, void *info) { |
6867 | [(HomeController *) info dispatchEvent:@"CydiaReachabilityCallback"]; | |
6868 | } | |
6869 | ||
3c62d654 JF |
6870 | - (id) init { |
6871 | if ((self = [super init]) != nil) { | |
90351d93 | 6872 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/home/", UI_]]]; |
ed7bfd8c | 6873 | [self reloadData]; |
02f21c73 JF |
6874 | |
6875 | reachability_ = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "cydia.saurik.com"); | |
6876 | if (reachability_ != NULL) { | |
6877 | SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL}; | |
6878 | SCNetworkReachabilitySetCallback(reachability_, HomeControllerReachabilityCallback, &context); | |
6879 | ||
6880 | CFRunLoopRef runloop(CFRunLoopGetCurrent()); | |
6881 | if (SCNetworkReachabilityScheduleWithRunLoop(reachability_, runloop, kCFRunLoopDefaultMode)) | |
6882 | runloop_ = runloop; | |
6883 | } | |
3c62d654 JF |
6884 | } return self; |
6885 | } | |
6886 | ||
02f21c73 JF |
6887 | - (void) dealloc { |
6888 | if (reachability_ != NULL && runloop_ != NULL) | |
6889 | SCNetworkReachabilityUnscheduleFromRunLoop(reachability_, runloop_, kCFRunLoopDefaultMode); | |
6890 | [super dealloc]; | |
6891 | } | |
6892 | ||
fe8e721f GP |
6893 | - (NSURL *) navigationURL { |
6894 | return [NSURL URLWithString:@"cydia://home"]; | |
6895 | } | |
6896 | ||
5829aea2 GP |
6897 | - (void) aboutButtonClicked { |
6898 | UIAlertView *alert([[[UIAlertView alloc] init] autorelease]); | |
b4d89997 | 6899 | |
5829aea2 GP |
6900 | [alert setTitle:UCLocalize("ABOUT_CYDIA")]; |
6901 | [alert addButtonWithTitle:UCLocalize("CLOSE")]; | |
6902 | [alert setCancelButtonIndex:0]; | |
46aa9775 | 6903 | |
5829aea2 | 6904 | [alert setMessage: |
4c66fad9 | 6905 | @"Copyright \u00a9 2008-2015\n" |
7e865c1e JF |
6906 | "SaurikIT, LLC\n" |
6907 | "\n" | |
5829aea2 GP |
6908 | "Jay Freeman (saurik)\n" |
6909 | "saurik@saurik.com\n" | |
6910 | "http://www.saurik.com/" | |
6911 | ]; | |
46aa9775 | 6912 | |
5829aea2 | 6913 | [alert show]; |
a0376fc1 JF |
6914 | } |
6915 | ||
e6124cb6 JF |
6916 | - (UIBarButtonItem *) leftButton { |
6917 | return [[[UIBarButtonItem alloc] | |
35f0a3b5 GP |
6918 | initWithTitle:UCLocalize("ABOUT") |
6919 | style:UIBarButtonItemStylePlain | |
6920 | target:self | |
6921 | action:@selector(aboutButtonClicked) | |
e6124cb6 | 6922 | ] autorelease]; |
7b0ce2da JF |
6923 | } |
6924 | ||
5829aea2 GP |
6925 | @end |
6926 | /* }}} */ | |
7b0ce2da | 6927 | |
28e596e4 | 6928 | /* Cydia Navigation Controller Interface {{{ */ |
15f0d613 | 6929 | @interface UINavigationController (Cydia) |
28e596e4 JF |
6930 | |
6931 | - (NSArray *) navigationURLCollection; | |
15f0d613 | 6932 | - (void) unloadData; |
28e596e4 JF |
6933 | |
6934 | @end | |
6935 | /* }}} */ | |
7b0ce2da | 6936 | |
5829aea2 | 6937 | /* Cydia Tab Bar Controller {{{ */ |
5fe2bcc6 | 6938 | @interface CydiaTabBarController : CyteTabBarController < |
9f99f3da | 6939 | UITabBarControllerDelegate, |
21ac0ce2 | 6940 | FetchDelegate |
5829aea2 GP |
6941 | > { |
6942 | _transient Database *database_; | |
7b0ce2da | 6943 | |
e67ebdad JF |
6944 | _H<UIActivityIndicatorView> indicator_; |
6945 | ||
5829aea2 GP |
6946 | bool updating_; |
6947 | // XXX: ok, "updatedelegate_"?... | |
6948 | _transient NSObject<CydiaDelegate> *updatedelegate_; | |
7b0ce2da JF |
6949 | } |
6950 | ||
35f0a3b5 | 6951 | - (NSArray *) navigationURLCollection; |
5829aea2 | 6952 | - (void) beginUpdate; |
5829aea2 | 6953 | - (BOOL) updating; |
1cedb821 | 6954 | |
5829aea2 | 6955 | @end |
2fc76a2d | 6956 | |
5fe2bcc6 | 6957 | @implementation CydiaTabBarController |
9f99f3da | 6958 | |
35f0a3b5 | 6959 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
6960 | NSMutableArray *items([NSMutableArray array]); |
6961 | ||
35f0a3b5 | 6962 | // XXX: Should this deal with transient view controllers? |
fe8e721f | 6963 | for (id navigation in [self viewControllers]) { |
35f0a3b5 | 6964 | NSArray *stack = [navigation performSelector:@selector(navigationURLCollection)]; |
fe8e721f GP |
6965 | if (stack != nil) |
6966 | [items addObject:stack]; | |
2fc76a2d | 6967 | } |
c713af59 | 6968 | |
fe8e721f GP |
6969 | return items; |
6970 | } | |
6971 | ||
b5e7eebb GP |
6972 | - (id) initWithDatabase:(Database *)database { |
6973 | if ((self = [super init]) != nil) { | |
7b0ce2da | 6974 | database_ = database; |
9f99f3da | 6975 | [self setDelegate:self]; |
04fe1349 | 6976 | |
e67ebdad JF |
6977 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteTiny] autorelease]; |
6978 | [indicator_ setOrigin:CGPointMake(kCFCoreFoundationVersionNumber >= 800 ? 2 : 4, 2)]; | |
7b0ce2da | 6979 | |
e67ebdad | 6980 | [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b0ce2da JF |
6981 | } return self; |
6982 | } | |
6983 | ||
5829aea2 | 6984 | - (void) beginUpdate { |
20d59843 JF |
6985 | if (updating_) |
6986 | return; | |
6987 | ||
e67ebdad JF |
6988 | UIViewController *controller([[self viewControllers] objectAtIndex:1]); |
6989 | UITabBarItem *item([controller tabBarItem]); | |
6990 | ||
6991 | [item setBadgeValue:@""]; | |
6992 | UIView *badge(MSHookIvar<UIView *>([item view], "_badge")); | |
6993 | ||
6994 | [indicator_ startAnimating]; | |
6995 | [badge addSubview:indicator_]; | |
7b0ce2da | 6996 | |
5829aea2 GP |
6997 | [updatedelegate_ retainNetworkActivityIndicator]; |
6998 | updating_ = true; | |
7b0ce2da | 6999 | |
5829aea2 GP |
7000 | [NSThread |
7001 | detachNewThreadSelector:@selector(performUpdate) | |
7002 | toTarget:self | |
7003 | withObject:nil | |
7004 | ]; | |
7b0ce2da JF |
7005 | } |
7006 | ||
d13edf44 JF |
7007 | - (void) performUpdate { |
7008 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
7009 | ||
21ac0ce2 | 7010 | SourceStatus status(self, database_); |
5829aea2 GP |
7011 | [database_ updateWithStatus:status]; |
7012 | ||
7013 | [self | |
7014 | performSelectorOnMainThread:@selector(completeUpdate) | |
7015 | withObject:nil | |
7016 | waitUntilDone:NO | |
7017 | ]; | |
d13edf44 JF |
7018 | |
7019 | [pool release]; | |
f6e13561 GP |
7020 | } |
7021 | ||
5829aea2 GP |
7022 | - (void) stopUpdateWithSelector:(SEL)selector { |
7023 | updating_ = false; | |
7024 | [updatedelegate_ releaseNetworkActivityIndicator]; | |
b5e7eebb | 7025 | |
e67ebdad JF |
7026 | UIViewController *controller([[self viewControllers] objectAtIndex:1]); |
7027 | [[controller tabBarItem] setBadgeValue:nil]; | |
7028 | ||
7029 | [indicator_ removeFromSuperview]; | |
7030 | [indicator_ stopAnimating]; | |
bc11cf5b | 7031 | |
5829aea2 | 7032 | [updatedelegate_ performSelector:selector withObject:nil afterDelay:0]; |
2bdd73bd GP |
7033 | } |
7034 | ||
5829aea2 GP |
7035 | - (void) completeUpdate { |
7036 | if (!updating_) | |
7037 | return; | |
7038 | [self stopUpdateWithSelector:@selector(reloadData)]; | |
7b0ce2da JF |
7039 | } |
7040 | ||
5829aea2 | 7041 | - (void) cancelUpdate { |
383a58ac | 7042 | [self stopUpdateWithSelector:@selector(updateDataAndLoad)]; |
7b0ce2da JF |
7043 | } |
7044 | ||
5829aea2 GP |
7045 | - (void) cancelPressed { |
7046 | [self cancelUpdate]; | |
7047 | } | |
7b0ce2da | 7048 | |
5829aea2 GP |
7049 | - (BOOL) updating { |
7050 | return updating_; | |
7b0ce2da JF |
7051 | } |
7052 | ||
21ac0ce2 | 7053 | - (bool) isSourceCancelled { |
5829aea2 GP |
7054 | return !updating_; |
7055 | } | |
7b0ce2da | 7056 | |
21ac0ce2 | 7057 | - (void) startSourceFetch:(NSString *)uri { |
7b0ce2da JF |
7058 | } |
7059 | ||
21ac0ce2 | 7060 | - (void) stopSourceFetch:(NSString *)uri { |
bcbac8f7 JF |
7061 | } |
7062 | ||
5829aea2 GP |
7063 | - (void) setUpdateDelegate:(id)delegate { |
7064 | updatedelegate_ = delegate; | |
7b0ce2da JF |
7065 | } |
7066 | ||
7b0ce2da | 7067 | @end |
98228790 | 7068 | /* }}} */ |
f6e13561 | 7069 | |
28e596e4 | 7070 | /* Cydia Navigation Controller Implementation {{{ */ |
15f0d613 | 7071 | @implementation UINavigationController (Cydia) |
bc11cf5b | 7072 | |
35f0a3b5 | 7073 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
7074 | NSMutableArray *stack([NSMutableArray array]); |
7075 | ||
cd79e8cf | 7076 | for (CyteViewController *controller in [self viewControllers]) { |
fe8e721f GP |
7077 | NSString *url = [[controller navigationURL] absoluteString]; |
7078 | if (url != nil) | |
7079 | [stack addObject:url]; | |
7080 | } | |
7081 | ||
7082 | return stack; | |
7083 | } | |
7084 | ||
15f0d613 JF |
7085 | - (void) reloadData { |
7086 | [super reloadData]; | |
7087 | ||
21263088 JF |
7088 | UIViewController *visible([self visibleViewController]); |
7089 | if (visible != nil) | |
15f0d613 | 7090 | [visible reloadData]; |
21263088 JF |
7091 | |
7092 | // on the iPad, this view controller is ALSO visible. :( | |
7093 | if (IsWildcat_) | |
907ba491 JF |
7094 | if (UIViewController *modal = [self modalViewController]) |
7095 | if ([modal modalPresentationStyle] == UIModalPresentationFormSheet) | |
7096 | if (UIViewController *top = [self topViewController]) | |
7097 | if (top != visible) | |
7098 | [top reloadData]; | |
15f0d613 | 7099 | } |
6c0ba3d9 | 7100 | |
15f0d613 | 7101 | - (void) unloadData { |
cd79e8cf | 7102 | for (CyteViewController *page in [self viewControllers]) |
15f0d613 | 7103 | [page unloadData]; |
6c0ba3d9 | 7104 | |
15f0d613 | 7105 | [super unloadData]; |
dc63e78f JF |
7106 | } |
7107 | ||
5829aea2 GP |
7108 | @end |
7109 | /* }}} */ | |
7b0ce2da | 7110 | |
5829aea2 GP |
7111 | /* Cydia:// Protocol {{{ */ |
7112 | @interface CydiaURLProtocol : NSURLProtocol { | |
9fe5e5f8 JF |
7113 | } |
7114 | ||
7b0ce2da JF |
7115 | @end |
7116 | ||
5829aea2 GP |
7117 | @implementation CydiaURLProtocol |
7118 | ||
7119 | + (BOOL) canInitWithRequest:(NSURLRequest *)request { | |
7120 | NSURL *url([request URL]); | |
7121 | if (url == nil) | |
7122 | return NO; | |
b0a2900d | 7123 | |
5829aea2 | 7124 | NSString *scheme([[url scheme] lowercaseString]); |
b0a2900d JF |
7125 | if (scheme != nil && [scheme isEqualToString:@"cydia"]) |
7126 | return YES; | |
7127 | if ([[url absoluteString] hasPrefix:@"about:cydia-"]) | |
7128 | return YES; | |
7129 | ||
7130 | return NO; | |
aa5e5990 JF |
7131 | } |
7132 | ||
5829aea2 GP |
7133 | + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { |
7134 | return request; | |
7135 | } | |
aa5e5990 | 7136 | |
5829aea2 GP |
7137 | - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { |
7138 | id<NSURLProtocolClient> client([self client]); | |
7139 | if (icon == nil) | |
7140 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; | |
7141 | else { | |
7142 | NSData *data(UIImagePNGRepresentation(icon)); | |
01d93940 | 7143 | |
5829aea2 GP |
7144 | NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); |
7145 | [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; | |
7146 | [client URLProtocol:self didLoadData:data]; | |
7147 | [client URLProtocolDidFinishLoading:self]; | |
7148 | } | |
3931b718 JF |
7149 | } |
7150 | ||
5829aea2 GP |
7151 | - (void) startLoading { |
7152 | id<NSURLProtocolClient> client([self client]); | |
7153 | NSURLRequest *request([self request]); | |
bc11cf5b | 7154 | |
5829aea2 GP |
7155 | NSURL *url([request URL]); |
7156 | NSString *href([url absoluteString]); | |
b0a2900d JF |
7157 | NSString *scheme([[url scheme] lowercaseString]); |
7158 | ||
7159 | NSString *path; | |
7160 | ||
7161 | if ([scheme isEqualToString:@"cydia"]) | |
7162 | path = [href substringFromIndex:8]; | |
7163 | else if ([scheme isEqualToString:@"about"]) | |
7164 | path = [href substringFromIndex:12]; | |
7165 | else _assert(false); | |
bc11cf5b | 7166 | |
5829aea2 | 7167 | NSRange slash([path rangeOfString:@"/"]); |
bc11cf5b | 7168 | |
5829aea2 GP |
7169 | NSString *command; |
7170 | if (slash.location == NSNotFound) { | |
7171 | command = path; | |
7172 | path = nil; | |
7173 | } else { | |
7174 | command = [path substringToIndex:slash.location]; | |
7175 | path = [path substringFromIndex:(slash.location + 1)]; | |
7176 | } | |
39cda3a8 | 7177 | |
5829aea2 | 7178 | Database *database([Database sharedInstance]); |
bc11cf5b | 7179 | |
d0a5ea56 JF |
7180 | if (false); |
7181 | else if ([command isEqualToString:@"application-icon"]) { | |
7182 | if (path == nil) | |
7183 | goto fail; | |
7184 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7185 | NSData *data([SBSCopyIconImagePNGDataForDisplayIdentifier(path) autorelease]); | |
7186 | UIImage *icon; | |
7187 | if (data == nil) | |
7188 | icon = [UIImage imageNamed:@"unknown.png"]; | |
7189 | else | |
7190 | icon = [UIImage imageWithData:data]; | |
7191 | [self _returnPNGWithImage:icon forRequest:request]; | |
7192 | } else if ([command isEqualToString:@"package-icon"]) { | |
5829aea2 GP |
7193 | if (path == nil) |
7194 | goto fail; | |
7195 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7196 | Package *package([database packageWithName:path]); | |
7197 | if (package == nil) | |
7198 | goto fail; | |
eff20a8d | 7199 | [package parse]; |
5829aea2 GP |
7200 | UIImage *icon([package icon]); |
7201 | [self _returnPNGWithImage:icon forRequest:request]; | |
5829aea2 GP |
7202 | } else if ([command isEqualToString:@"uikit-image"]) { |
7203 | if (path == nil) | |
7204 | goto fail; | |
7205 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
7206 | UIImage *icon(_UIImageWithName(path)); | |
7207 | [self _returnPNGWithImage:icon forRequest:request]; | |
7208 | } else if ([command isEqualToString:@"section-icon"]) { | |
7209 | if (path == nil) | |
7210 | goto fail; | |
7211 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
611efc17 | 7212 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [path stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]); |
5829aea2 | 7213 | if (icon == nil) |
8dbf3abc | 7214 | icon = [UIImage imageNamed:@"unknown.png"]; |
5829aea2 GP |
7215 | [self _returnPNGWithImage:icon forRequest:request]; |
7216 | } else fail: { | |
7217 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; | |
7218 | } | |
39cda3a8 GP |
7219 | } |
7220 | ||
5829aea2 GP |
7221 | - (void) stopLoading { |
7222 | } | |
807ae6d7 | 7223 | |
5829aea2 GP |
7224 | @end |
7225 | /* }}} */ | |
807ae6d7 | 7226 | |
5829aea2 | 7227 | /* Section Controller {{{ */ |
f50860ee | 7228 | @interface SectionController : FilteredPackageListController { |
55066b9e | 7229 | _H<NSString> key_; |
123b380c | 7230 | _H<NSString> section_; |
5829aea2 | 7231 | } |
807ae6d7 | 7232 | |
55066b9e | 7233 | - (id) initWithDatabase:(Database *)database source:(Source *)source section:(NSString *)section; |
807ae6d7 | 7234 | |
5829aea2 | 7235 | @end |
bc11cf5b | 7236 | |
5829aea2 | 7237 | @implementation SectionController |
bc11cf5b | 7238 | |
f050e4d9 | 7239 | - (NSURL *) referrerURL { |
55066b9e JF |
7240 | NSString *name(section_); |
7241 | name = name ?: @"*"; | |
7242 | NSString *key(key_); | |
7243 | key = key ?: @"*"; | |
7244 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/sections/%@/%@", UI_, [key stringByAddingPercentEscapesIncludingReserved], [name stringByAddingPercentEscapesIncludingReserved]]]; | |
f050e4d9 JF |
7245 | } |
7246 | ||
fe8e721f | 7247 | - (NSURL *) navigationURL { |
55066b9e JF |
7248 | NSString *name(section_); |
7249 | name = name ?: @"*"; | |
7250 | NSString *key(key_); | |
7251 | key = key ?: @"*"; | |
7252 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sections/%@/%@", [key stringByAddingPercentEscapesIncludingReserved], [name stringByAddingPercentEscapesIncludingReserved]]]; | |
fe8e721f GP |
7253 | } |
7254 | ||
55066b9e | 7255 | - (id) initWithDatabase:(Database *)database source:(Source *)source section:(NSString *)section { |
5829aea2 | 7256 | NSString *title; |
55066b9e | 7257 | if (section == nil) |
5829aea2 | 7258 | title = UCLocalize("ALL_PACKAGES"); |
55066b9e JF |
7259 | else if (![section isEqual:@""]) |
7260 | title = [[NSBundle mainBundle] localizedStringForKey:Simplify(section) value:nil table:@"Sections"]; | |
3707eb56 | 7261 | else |
5829aea2 | 7262 | title = UCLocalize("NO_SECTION"); |
fe8e721f | 7263 | |
89bdef78 | 7264 | if ((self = [super initWithDatabase:database title:title]) != nil) { |
55066b9e JF |
7265 | key_ = [source key]; |
7266 | section_ = section; | |
5829aea2 | 7267 | } return self; |
b5e7eebb GP |
7268 | } |
7269 | ||
55066b9e | 7270 | - (void) reloadData { |
89bdef78 JF |
7271 | Source *source([database_ sourceWithKey:key_]); |
7272 | _H<NSString> name(section_); | |
7273 | ||
7274 | [self setFilter:[=](Package *package) { | |
7275 | NSString *section([package section]); | |
7276 | ||
7277 | return ( | |
7278 | name == nil || | |
7279 | section == nil && [name length] == 0 || | |
7280 | [name isEqualToString:section] | |
7281 | ) && ( | |
7282 | source == nil || | |
7283 | [package source] == source | |
7284 | ) && [package visible]; | |
7285 | }]; | |
7286 | ||
55066b9e JF |
7287 | [super reloadData]; |
7288 | } | |
7289 | ||
b5e7eebb GP |
7290 | @end |
7291 | /* }}} */ | |
5829aea2 | 7292 | /* Sections Controller {{{ */ |
cd79e8cf | 7293 | @interface SectionsController : CyteViewController < |
5829aea2 GP |
7294 | UITableViewDataSource, |
7295 | UITableViewDelegate | |
7585ce66 | 7296 | > { |
3931b718 | 7297 | _transient Database *database_; |
55066b9e | 7298 | _H<NSString> key_; |
7b33d201 JF |
7299 | _H<NSMutableArray> sections_; |
7300 | _H<NSMutableArray> filtered_; | |
bf7c998c | 7301 | _H<UITableView, 2> list_; |
b5e7eebb GP |
7302 | } |
7303 | ||
55066b9e | 7304 | - (id) initWithDatabase:(Database *)database source:(Source *)source; |
5829aea2 | 7305 | - (void) editButtonClicked; |
7585ce66 | 7306 | |
bc11cf5b | 7307 | @end |
b5e7eebb | 7308 | |
5829aea2 | 7309 | @implementation SectionsController |
b5e7eebb | 7310 | |
fe8e721f | 7311 | - (NSURL *) navigationURL { |
55066b9e JF |
7312 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sources/%@", [key_ stringByAddingPercentEscapesIncludingReserved]]]; |
7313 | } | |
7314 | ||
7315 | - (Source *) source { | |
7316 | if (key_ == nil) | |
7317 | return nil; | |
7318 | return [database_ sourceWithKey:key_]; | |
fe8e721f GP |
7319 | } |
7320 | ||
a784d0a4 | 7321 | - (void) updateNavigationItem { |
8e5b801a | 7322 | [[self navigationItem] setTitle:[self isEditing] ? UCLocalize("SECTION_VISIBILITY") : UCLocalize("SECTIONS")]; |
a784d0a4 GP |
7323 | if ([sections_ count] == 0) { |
7324 | [[self navigationItem] setRightBarButtonItem:nil]; | |
7325 | } else { | |
7326 | [[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] | |
8e5b801a | 7327 | initWithBarButtonSystemItem:([self isEditing] ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit) |
a784d0a4 GP |
7328 | target:self |
7329 | action:@selector(editButtonClicked) | |
7330 | ] animated:([[self navigationItem] rightBarButtonItem] != nil)]; | |
7331 | } | |
7332 | } | |
7333 | ||
8e5b801a GP |
7334 | - (void) setEditing:(BOOL)editing animated:(BOOL)animated { |
7335 | [super setEditing:editing animated:animated]; | |
35f0a3b5 | 7336 | |
8e5b801a | 7337 | if (editing) |
5829aea2 GP |
7338 | [list_ reloadData]; |
7339 | else | |
7340 | [delegate_ updateData]; | |
7585ce66 | 7341 | |
a784d0a4 | 7342 | [self updateNavigationItem]; |
b5e7eebb GP |
7343 | } |
7344 | ||
5829aea2 GP |
7345 | - (void) viewDidAppear:(BOOL)animated { |
7346 | [super viewDidAppear:animated]; | |
7347 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7585ce66 JF |
7348 | } |
7349 | ||
5829aea2 GP |
7350 | - (void) viewWillDisappear:(BOOL)animated { |
7351 | [super viewWillDisappear:animated]; | |
31eedaae | 7352 | [self setEditing:NO]; |
7585ce66 JF |
7353 | } |
7354 | ||
5829aea2 | 7355 | - (Section *) sectionAtIndexPath:(NSIndexPath *)indexPath { |
b3551da8 | 7356 | Section *section = nil; |
8e5b801a | 7357 | int index = [indexPath row]; |
b3551da8 | 7358 | if (![self isEditing]) { |
666b48ad | 7359 | index -= 1; |
b3551da8 GP |
7360 | if (index >= 0) |
7361 | section = [filtered_ objectAtIndex:index]; | |
7362 | } else { | |
7363 | section = [sections_ objectAtIndex:index]; | |
7364 | } | |
5829aea2 GP |
7365 | return section; |
7366 | } | |
7585ce66 | 7367 | |
5829aea2 | 7368 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
8e5b801a GP |
7369 | if ([self isEditing]) |
7370 | return [sections_ count]; | |
7371 | else | |
7372 | return [filtered_ count] + 1; | |
7585ce66 JF |
7373 | } |
7374 | ||
5829aea2 GP |
7375 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
7376 | return 45.0f; | |
7377 | }*/ | |
7585ce66 | 7378 | |
5829aea2 GP |
7379 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
7380 | static NSString *reuseIdentifier = @"SectionCell"; | |
7585ce66 | 7381 | |
35f0a3b5 | 7382 | SectionCell *cell = (SectionCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5829aea2 GP |
7383 | if (cell == nil) |
7384 | cell = [[[SectionCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
7585ce66 | 7385 | |
8e5b801a | 7386 | [cell setSection:[self sectionAtIndexPath:indexPath] editing:[self isEditing]]; |
54043703 | 7387 | |
5829aea2 | 7388 | return cell; |
54043703 JF |
7389 | } |
7390 | ||
5829aea2 | 7391 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
8e5b801a | 7392 | if ([self isEditing]) |
54043703 | 7393 | return; |
7585ce66 | 7394 | |
5829aea2 | 7395 | Section *section = [self sectionAtIndexPath:indexPath]; |
7585ce66 | 7396 | |
5829aea2 GP |
7397 | SectionController *controller = [[[SectionController alloc] |
7398 | initWithDatabase:database_ | |
55066b9e | 7399 | source:[self source] |
5829aea2 GP |
7400 | section:[section name] |
7401 | ] autorelease]; | |
7402 | [controller setDelegate:delegate_]; | |
7585ce66 | 7403 | |
5829aea2 | 7404 | [[self navigationController] pushViewController:controller animated:YES]; |
7585ce66 JF |
7405 | } |
7406 | ||
fe8e721f | 7407 | - (void) loadView { |
e8cbebe4 | 7408 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; |
fe8e721f | 7409 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
86a333c6 | 7410 | [list_ setRowHeight:46]; |
7b33d201 | 7411 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 7412 | [list_ setDelegate:self]; |
e8cbebe4 | 7413 | [self setView:list_]; |
fe8e721f GP |
7414 | } |
7415 | ||
7416 | - (void) viewDidLoad { | |
7d887d0b JF |
7417 | [super viewDidLoad]; |
7418 | ||
fe8e721f GP |
7419 | [[self navigationItem] setTitle:UCLocalize("SECTIONS")]; |
7420 | } | |
7421 | ||
7422 | - (void) releaseSubviews { | |
fe8e721f | 7423 | list_ = nil; |
7be3eb32 | 7424 | |
4f9acb7c JF |
7425 | sections_ = nil; |
7426 | filtered_ = nil; | |
7427 | ||
7be3eb32 | 7428 | [super releaseSubviews]; |
fe8e721f GP |
7429 | } |
7430 | ||
55066b9e | 7431 | - (id) initWithDatabase:(Database *)database source:(Source *)source { |
5829aea2 GP |
7432 | if ((self = [super init]) != nil) { |
7433 | database_ = database; | |
55066b9e | 7434 | key_ = [source key]; |
5829aea2 | 7435 | } return self; |
7585ce66 JF |
7436 | } |
7437 | ||
5829aea2 | 7438 | - (void) reloadData { |
fe8e721f GP |
7439 | [super reloadData]; |
7440 | ||
5829aea2 | 7441 | NSArray *packages = [database_ packages]; |
7585ce66 | 7442 | |
4f9acb7c JF |
7443 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
7444 | filtered_ = [NSMutableArray arrayWithCapacity:16]; | |
7585ce66 | 7445 | |
5829aea2 | 7446 | NSMutableDictionary *sections([NSMutableDictionary dictionaryWithCapacity:32]); |
7585ce66 | 7447 | |
55066b9e JF |
7448 | Source *source([self source]); |
7449 | ||
5829aea2 GP |
7450 | _trace(); |
7451 | for (Package *package in packages) { | |
55066b9e JF |
7452 | if (source != nil && [package source] != source) |
7453 | continue; | |
7454 | ||
5829aea2 GP |
7455 | NSString *name([package section]); |
7456 | NSString *key(name == nil ? @"" : name); | |
7585ce66 | 7457 | |
5829aea2 | 7458 | Section *section; |
7585ce66 | 7459 | |
5829aea2 GP |
7460 | _profile(SectionsView$reloadData$Section) |
7461 | section = [sections objectForKey:key]; | |
7462 | if (section == nil) { | |
7463 | _profile(SectionsView$reloadData$Section$Allocate) | |
729bc910 | 7464 | section = [[[Section alloc] initWithName:key localize:YES] autorelease]; |
5829aea2 GP |
7465 | [sections setObject:section forKey:key]; |
7466 | _end | |
7467 | } | |
7468 | _end | |
7585ce66 | 7469 | |
5829aea2 | 7470 | [section addToCount]; |
7585ce66 | 7471 | |
5829aea2 GP |
7472 | _profile(SectionsView$reloadData$Filter) |
7473 | if (![package valid] || ![package visible]) | |
7474 | continue; | |
7475 | _end | |
7585ce66 | 7476 | |
5829aea2 GP |
7477 | [section addToRow]; |
7478 | } | |
7479 | _trace(); | |
7585ce66 | 7480 | |
5829aea2 | 7481 | [sections_ addObjectsFromArray:[sections allValues]]; |
7585ce66 | 7482 | |
5829aea2 | 7483 | [sections_ sortUsingSelector:@selector(compareByLocalized:)]; |
7585ce66 | 7484 | |
7b33d201 | 7485 | for (Section *section in (id) sections_) { |
5829aea2 GP |
7486 | size_t count([section row]); |
7487 | if (count == 0) | |
7488 | continue; | |
7585ce66 | 7489 | |
5829aea2 GP |
7490 | section = [[[Section alloc] initWithName:[section name] localized:[section localized]] autorelease]; |
7491 | [section setCount:count]; | |
7492 | [filtered_ addObject:section]; | |
7493 | } | |
7585ce66 | 7494 | |
a784d0a4 | 7495 | [self updateNavigationItem]; |
5829aea2 GP |
7496 | [list_ reloadData]; |
7497 | _trace(); | |
7498 | } | |
7585ce66 | 7499 | |
6840bff3 | 7500 | - (void) editButtonClicked { |
8e5b801a | 7501 | [self setEditing:![self isEditing] animated:YES]; |
5829aea2 | 7502 | } |
7585ce66 | 7503 | |
5829aea2 GP |
7504 | @end |
7505 | /* }}} */ | |
7585ce66 | 7506 | |
5829aea2 | 7507 | /* Changes Controller {{{ */ |
e5491e28 | 7508 | @interface ChangesController : FilteredPackageListController { |
5829aea2 | 7509 | unsigned upgrades_; |
5829aea2 | 7510 | } |
7585ce66 | 7511 | |
ea3bb538 | 7512 | - (id) initWithDatabase:(Database *)database; |
7585ce66 | 7513 | |
5829aea2 | 7514 | @end |
7585ce66 | 7515 | |
5829aea2 | 7516 | @implementation ChangesController |
7585ce66 | 7517 | |
e5491e28 JF |
7518 | - (NSURL *) referrerURL { |
7519 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/changes/", UI_]]; | |
b5e7eebb GP |
7520 | } |
7521 | ||
e5491e28 JF |
7522 | - (NSURL *) navigationURL { |
7523 | return [NSURL URLWithString:@"cydia://changes"]; | |
5829aea2 | 7524 | } |
b5e7eebb | 7525 | |
5829aea2 GP |
7526 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
7527 | @synchronized (database_) { | |
7528 | if ([database_ era] != era_) | |
7529 | return nil; | |
b5e7eebb | 7530 | |
5829aea2 GP |
7531 | NSUInteger sectionIndex([path section]); |
7532 | if (sectionIndex >= [sections_ count]) | |
7533 | return nil; | |
7534 | Section *section([sections_ objectAtIndex:sectionIndex]); | |
7535 | NSInteger row([path row]); | |
56bf1e78 | 7536 | return [[[packages_ objectAtIndex:([section row] + row)] retain] autorelease]; |
5829aea2 | 7537 | } } |
b5e7eebb | 7538 | |
0e15b67c JF |
7539 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
7540 | NSString *context([alert context]); | |
7541 | ||
7542 | if ([context isEqualToString:@"norefresh"]) | |
7543 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
7544 | } | |
7545 | ||
e67ebdad JF |
7546 | - (void) setLeftBarButtonItem { |
7547 | if ([delegate_ updating]) | |
7548 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7549 | initWithTitle:UCLocalize("CANCEL") | |
7550 | style:UIBarButtonItemStyleDone | |
7551 | target:self | |
7552 | action:@selector(cancelButtonClicked) | |
7553 | ] autorelease] animated:YES]; | |
7554 | else | |
7555 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7556 | initWithTitle:UCLocalize("REFRESH") | |
7557 | style:UIBarButtonItemStylePlain | |
7558 | target:self | |
7559 | action:@selector(refreshButtonClicked) | |
7560 | ] autorelease] animated:YES]; | |
7561 | } | |
7562 | ||
5829aea2 | 7563 | - (void) refreshButtonClicked { |
e67ebdad JF |
7564 | if ([delegate_ requestUpdate]) |
7565 | [self setLeftBarButtonItem]; | |
7566 | } | |
0e15b67c | 7567 | |
e67ebdad JF |
7568 | - (void) cancelButtonClicked { |
7569 | [delegate_ cancelUpdate]; | |
b5e7eebb GP |
7570 | } |
7571 | ||
5829aea2 GP |
7572 | - (void) upgradeButtonClicked { |
7573 | [delegate_ distUpgrade]; | |
cb6b3a7b | 7574 | [[self navigationItem] setRightBarButtonItem:nil animated:YES]; |
b5e7eebb GP |
7575 | } |
7576 | ||
e5491e28 JF |
7577 | - (bool) shouldYield { |
7578 | return true; | |
fe8e721f GP |
7579 | } |
7580 | ||
e5491e28 JF |
7581 | - (bool) shouldBlock { |
7582 | return true; | |
fe8e721f GP |
7583 | } |
7584 | ||
e5491e28 JF |
7585 | - (void) useFilter { |
7586 | @synchronized (self) { | |
7587 | [self setFilter:[](Package *package) { | |
7588 | return [package upgradableAndEssential:YES] || [package visible]; | |
7589 | }]; | |
4f9acb7c | 7590 | |
e5491e28 JF |
7591 | [self setSorter:[](NSMutableArray *packages) { |
7592 | [packages radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackageChangesRadix) withContext:NULL]; | |
7593 | }]; | |
7594 | } } | |
5829aea2 | 7595 | |
ea3bb538 | 7596 | - (id) initWithDatabase:(Database *)database { |
e5491e28 JF |
7597 | if ((self = [super initWithDatabase:database title:UCLocalize("CHANGES")]) != nil) { |
7598 | [self useFilter]; | |
5829aea2 | 7599 | } return self; |
807ae6d7 JF |
7600 | } |
7601 | ||
946c4377 JF |
7602 | - (void) viewDidLoad { |
7603 | [super viewDidLoad]; | |
7604 | [self setLeftBarButtonItem]; | |
7605 | } | |
7606 | ||
7607 | - (void) viewWillAppear:(BOOL)animated { | |
7608 | [super viewWillAppear:animated]; | |
7609 | [self setLeftBarButtonItem]; | |
7610 | } | |
7611 | ||
e5491e28 | 7612 | - (void) reloadData { |
e67ebdad | 7613 | [self setLeftBarButtonItem]; |
e5491e28 JF |
7614 | [super reloadData]; |
7615 | } | |
e67ebdad | 7616 | |
e5491e28 JF |
7617 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { |
7618 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); | |
bdca103d | 7619 | |
5829aea2 GP |
7620 | Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; |
7621 | Section *ignored = nil; | |
7622 | Section *section = nil; | |
7623 | time_t last = 0; | |
807ae6d7 | 7624 | |
5829aea2 GP |
7625 | upgrades_ = 0; |
7626 | bool unseens = false; | |
807ae6d7 | 7627 | |
5829aea2 | 7628 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); |
807ae6d7 | 7629 | |
e5491e28 JF |
7630 | for (size_t offset = 0, count = [packages count]; offset != count; ++offset) { |
7631 | Package *package = [packages objectAtIndex:offset]; | |
807ae6d7 | 7632 | |
5829aea2 | 7633 | BOOL uae = [package upgradableAndEssential:YES]; |
807ae6d7 | 7634 | |
5829aea2 GP |
7635 | if (!uae) { |
7636 | unseens = true; | |
7637 | time_t seen([package seen]); | |
d90a4cd6 | 7638 | |
5829aea2 GP |
7639 | if (section == nil || last != seen) { |
7640 | last = seen; | |
7641 | ||
7642 | NSString *name; | |
7643 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]); | |
7644 | [name autorelease]; | |
7645 | ||
7646 | _profile(ChangesController$reloadData$Allocate) | |
7647 | name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; | |
7648 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
e5491e28 | 7649 | [sections addObject:section]; |
5829aea2 GP |
7650 | _end |
7651 | } | |
7652 | ||
7653 | [section addToCount]; | |
7654 | } else if ([package ignored]) { | |
7655 | if (ignored == nil) { | |
7656 | ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease]; | |
7657 | } | |
7658 | [ignored addToCount]; | |
7659 | } else { | |
7660 | ++upgrades_; | |
7661 | [upgradable addToCount]; | |
7662 | } | |
7663 | } | |
7664 | _trace(); | |
7665 | ||
7666 | CFRelease(formatter); | |
7667 | ||
7668 | if (unseens) { | |
e5491e28 | 7669 | Section *last = [sections lastObject]; |
5829aea2 | 7670 | size_t count = [last count]; |
e5491e28 JF |
7671 | [packages removeObjectsInRange:NSMakeRange([packages count] - count, count)]; |
7672 | [sections removeLastObject]; | |
5829aea2 GP |
7673 | } |
7674 | ||
7675 | if ([ignored count] != 0) | |
e5491e28 | 7676 | [sections insertObject:ignored atIndex:0]; |
5829aea2 | 7677 | if (upgrades_ != 0) |
e5491e28 | 7678 | [sections insertObject:upgradable atIndex:0]; |
5829aea2 GP |
7679 | |
7680 | [list_ reloadData]; | |
7681 | ||
cb6b3a7b JF |
7682 | [[self navigationItem] setRightBarButtonItem:(upgrades_ == 0 ? nil : [[[UIBarButtonItem alloc] |
7683 | initWithTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("UPGRADE"), [NSString stringWithFormat:@"%u", upgrades_]] | |
7684 | style:UIBarButtonItemStylePlain | |
7685 | target:self | |
7686 | action:@selector(upgradeButtonClicked) | |
7687 | ] autorelease]) animated:YES]; | |
5829aea2 | 7688 | |
e5491e28 | 7689 | return sections; |
a70cd4ba JF |
7690 | } |
7691 | ||
5829aea2 GP |
7692 | @end |
7693 | /* }}} */ | |
7694 | /* Search Controller {{{ */ | |
f50860ee | 7695 | @interface SearchController : FilteredPackageListController < |
5829aea2 GP |
7696 | UISearchBarDelegate |
7697 | > { | |
bf7c998c | 7698 | _H<UISearchBar, 1> search_; |
fe8e721f | 7699 | BOOL searchloaded_; |
89bdef78 | 7700 | bool summary_; |
5829aea2 GP |
7701 | } |
7702 | ||
43625891 | 7703 | - (id) initWithDatabase:(Database *)database query:(NSString *)query; |
5829aea2 | 7704 | - (void) reloadData; |
d90a4cd6 GP |
7705 | |
7706 | @end | |
7707 | ||
5829aea2 | 7708 | @implementation SearchController |
d90a4cd6 | 7709 | |
f050e4d9 JF |
7710 | - (NSURL *) referrerURL { |
7711 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/search?q=%@", UI_, [([search_ text] ?: @"") stringByAddingPercentEscapesIncludingReserved]]]; | |
7712 | } | |
7713 | ||
fe8e721f | 7714 | - (NSURL *) navigationURL { |
35f0a3b5 GP |
7715 | if ([search_ text] == nil || [[search_ text] isEqualToString:@""]) |
7716 | return [NSURL URLWithString:@"cydia://search"]; | |
7717 | else | |
b90c7892 | 7718 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://search/%@", [[search_ text] stringByAddingPercentEscapesIncludingReserved]]]; |
fe8e721f GP |
7719 | } |
7720 | ||
096e25d8 | 7721 | - (NSArray *) termsForQuery:(NSString *)query { |
945812b9 JF |
7722 | NSMutableArray *terms([NSMutableArray arrayWithCapacity:2]); |
7723 | for (NSString *component in [query componentsSeparatedByString:@" "]) | |
7724 | if ([component length] != 0) | |
7725 | [terms addObject:component]; | |
7726 | ||
7727 | return terms; | |
096e25d8 JF |
7728 | } |
7729 | ||
59f3d290 | 7730 | - (void) useSearch { |
89bdef78 JF |
7731 | _H<NSArray> query([self termsForQuery:[search_ text]]); |
7732 | summary_ = false; | |
7733 | ||
7734 | @synchronized (self) { | |
7735 | [self setFilter:[=](Package *package) { | |
7736 | if (![package unfiltered]) | |
7737 | return false; | |
7738 | if (![package matches:query]) | |
7739 | return false; | |
7740 | return true; | |
7741 | }]; | |
7742 | ||
7743 | [self setSorter:[](NSMutableArray *packages) { | |
7744 | [packages radixSortUsingSelector:@selector(rank)]; | |
7745 | }]; | |
7746 | } | |
7747 | ||
59f3d290 JF |
7748 | [self clearData]; |
7749 | [self reloadData]; | |
7750 | } | |
7751 | ||
89bdef78 JF |
7752 | - (void) usePrefix:(NSString *)prefix { |
7753 | _H<NSString> query(prefix); | |
7754 | summary_ = true; | |
7755 | ||
7756 | @synchronized (self) { | |
7757 | [self setFilter:[=](Package *package) { | |
7758 | if ([query length] == 0) | |
7759 | return false; | |
7760 | if (![package unfiltered]) | |
7761 | return false; | |
7762 | if ([[package name] compare:query options:MatchCompareOptions_ range:NSMakeRange(0, [query length])] != NSOrderedSame) | |
7763 | return false; | |
7764 | return true; | |
7765 | }]; | |
7766 | ||
7767 | [self setSorter:nullptr]; | |
7768 | } | |
7769 | ||
7770 | [self reloadData]; | |
7771 | } | |
7772 | ||
3025d5b4 | 7773 | - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar { |
59f3d290 | 7774 | [self clearData]; |
89bdef78 | 7775 | [self usePrefix:[search_ text]]; |
3025d5b4 JF |
7776 | } |
7777 | ||
7778 | - (void) searchBarButtonClicked:(UISearchBar *)searchBar { | |
5829aea2 | 7779 | [search_ resignFirstResponder]; |
59f3d290 | 7780 | [self useSearch]; |
5829aea2 GP |
7781 | } |
7782 | ||
3025d5b4 JF |
7783 | - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar { |
7784 | [search_ setText:@""]; | |
7785 | [self searchBarButtonClicked:searchBar]; | |
7786 | } | |
7787 | ||
7788 | - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { | |
7789 | [self searchBarButtonClicked:searchBar]; | |
7790 | } | |
7791 | ||
5829aea2 | 7792 | - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { |
89bdef78 | 7793 | [self usePrefix:text]; |
5829aea2 GP |
7794 | } |
7795 | ||
3025d5b4 | 7796 | - (bool) shouldYield { |
56bf1e78 JF |
7797 | return YES; |
7798 | } | |
7799 | ||
7800 | - (bool) shouldBlock { | |
89bdef78 | 7801 | return !summary_; |
3025d5b4 JF |
7802 | } |
7803 | ||
59f3d290 | 7804 | - (bool) isSummarized { |
89bdef78 | 7805 | return summary_; |
59f3d290 JF |
7806 | } |
7807 | ||
9c5737d5 JF |
7808 | - (bool) showsSections { |
7809 | return false; | |
7810 | } | |
7811 | ||
43625891 | 7812 | - (id) initWithDatabase:(Database *)database query:(NSString *)query { |
89bdef78 | 7813 | if ((self = [super initWithDatabase:database title:UCLocalize("SEARCH")])) { |
900095fd | 7814 | search_ = [[[UISearchBar alloc] init] autorelease]; |
830efb5d | 7815 | [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; |
900095fd | 7816 | [search_ setDelegate:self]; |
43625891 | 7817 | |
830efb5d JF |
7818 | UITextField *textField; |
7819 | if ([search_ respondsToSelector:@selector(searchField)]) | |
7820 | textField = [search_ searchField]; | |
7821 | else | |
7822 | textField = MSHookIvar<UITextField *>(search_, "_searchField"); | |
7823 | ||
7824 | [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
7825 | [textField setEnablesReturnKeyAutomatically:NO]; | |
7826 | [[self navigationItem] setTitleView:textField]; | |
7827 | ||
43625891 JF |
7828 | if (query != nil) |
7829 | [search_ setText:query]; | |
89bdef78 | 7830 | [self useSearch]; |
fe8e721f | 7831 | } return self; |
5829aea2 GP |
7832 | } |
7833 | ||
6840bff3 | 7834 | - (void) viewDidAppear:(BOOL)animated { |
5829aea2 | 7835 | [super viewDidAppear:animated]; |
fe8e721f GP |
7836 | |
7837 | if (!searchloaded_) { | |
7838 | searchloaded_ = YES; | |
7839 | [search_ setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; | |
5829aea2 | 7840 | [search_ layoutSubviews]; |
d90a4cd6 | 7841 | } |
058813ae GP |
7842 | |
7843 | if ([self isSummarized]) | |
7844 | [search_ becomeFirstResponder]; | |
5829aea2 | 7845 | } |
d90a4cd6 | 7846 | |
d90a4cd6 | 7847 | - (void) reloadData { |
f50860ee | 7848 | [self resetCursor]; |
9dac415b | 7849 | [super reloadData]; |
d90a4cd6 GP |
7850 | } |
7851 | ||
5829aea2 GP |
7852 | - (void) didSelectPackage:(Package *)package { |
7853 | [search_ resignFirstResponder]; | |
7854 | [super didSelectPackage:package]; | |
d90a4cd6 GP |
7855 | } |
7856 | ||
7857 | @end | |
7858 | /* }}} */ | |
5829aea2 | 7859 | /* Package Settings Controller {{{ */ |
cd79e8cf | 7860 | @interface PackageSettingsController : CyteViewController < |
c21004b9 JF |
7861 | UITableViewDataSource, |
7862 | UITableViewDelegate | |
7863 | > { | |
807ae6d7 | 7864 | _transient Database *database_; |
7b33d201 JF |
7865 | _H<NSString> name_; |
7866 | _H<Package> package_; | |
bf7c998c | 7867 | _H<UITableView, 2> table_; |
7b33d201 JF |
7868 | _H<UISwitch> subscribedSwitch_; |
7869 | _H<UISwitch> ignoredSwitch_; | |
7870 | _H<UITableViewCell> subscribedCell_; | |
7871 | _H<UITableViewCell> ignoredCell_; | |
807ae6d7 JF |
7872 | } |
7873 | ||
5829aea2 | 7874 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
c21004b9 | 7875 | |
807ae6d7 JF |
7876 | @end |
7877 | ||
5829aea2 | 7878 | @implementation PackageSettingsController |
807ae6d7 | 7879 | |
fe8e721f | 7880 | - (NSURL *) navigationURL { |
8861e953 | 7881 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/settings", (id) name_]]; |
fe8e721f GP |
7882 | } |
7883 | ||
5829aea2 GP |
7884 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7885 | if (package_ == nil) | |
7886 | return 0; | |
e954c30a | 7887 | |
2136717a DH |
7888 | if ([package_ installed] == nil) |
7889 | return 1; | |
7890 | else | |
7891 | return 2; | |
e954c30a GP |
7892 | } |
7893 | ||
5829aea2 GP |
7894 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7895 | if (package_ == nil) | |
7896 | return 0; | |
7897 | ||
2136717a DH |
7898 | // both sections contain just one item right now. |
7899 | return 1; | |
b5e7eebb GP |
7900 | } |
7901 | ||
5829aea2 | 7902 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
2136717a | 7903 | return nil; |
e954c30a GP |
7904 | } |
7905 | ||
5829aea2 | 7906 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
2136717a DH |
7907 | if (section == 0) |
7908 | return UCLocalize("SHOW_ALL_CHANGES_EX"); | |
7909 | else | |
7910 | return UCLocalize("IGNORE_UPGRADES_EX"); | |
807ae6d7 JF |
7911 | } |
7912 | ||
5829aea2 GP |
7913 | - (void) onSubscribed:(id)control { |
7914 | bool value([control isOn]); | |
7915 | if (package_ == nil) | |
7916 | return; | |
7917 | if ([package_ setSubscribed:value]) | |
7918 | [delegate_ updateData]; | |
807ae6d7 JF |
7919 | } |
7920 | ||
e5e70358 JF |
7921 | - (void) _updateIgnored { |
7922 | const char *package([name_ UTF8String]); | |
7923 | bool on([ignoredSwitch_ isOn]); | |
7924 | ||
55eb40b9 JF |
7925 | FILE *dpkg(popen("/usr/libexec/cydia/cydo --set-selections", "w")); |
7926 | fwrite(package, strlen(package), 1, dpkg); | |
e5e70358 | 7927 | |
55eb40b9 JF |
7928 | if (on) |
7929 | fwrite(" hold\n", 6, 1, dpkg); | |
7930 | else | |
7931 | fwrite(" install\n", 9, 1, dpkg); | |
e5e70358 | 7932 | |
55eb40b9 | 7933 | pclose(dpkg); |
e5e70358 JF |
7934 | } |
7935 | ||
5829aea2 | 7936 | - (void) onIgnored:(id)control { |
e5e70358 JF |
7937 | NSInvocation *invocation([NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(_updateIgnored)]]); |
7938 | [invocation setTarget:self]; | |
7939 | [invocation setSelector:@selector(_updateIgnored)]; | |
7940 | ||
7941 | [delegate_ reloadDataWithInvocation:invocation]; | |
5829aea2 | 7942 | } |
807ae6d7 | 7943 | |
46aa9775 | 7944 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
7945 | if (package_ == nil) |
7946 | return nil; | |
b5e7eebb | 7947 | |
2136717a | 7948 | switch ([indexPath section]) { |
5829aea2 GP |
7949 | case 0: return subscribedCell_; |
7950 | case 1: return ignoredCell_; | |
36fbb2aa | 7951 | |
5829aea2 GP |
7952 | _nodefault |
7953 | } | |
807ae6d7 | 7954 | |
5829aea2 | 7955 | return nil; |
807ae6d7 JF |
7956 | } |
7957 | ||
fe8e721f | 7958 | - (void) loadView { |
39470a3a JF |
7959 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
7960 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
7961 | [self setView:view]; | |
807ae6d7 | 7962 | |
7b33d201 | 7963 | table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease]; |
fe8e721f | 7964 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b33d201 | 7965 | [(UITableView *) table_ setDataSource:self]; |
fe8e721f | 7966 | [table_ setDelegate:self]; |
39470a3a | 7967 | [view addSubview:table_]; |
807ae6d7 | 7968 | |
7b33d201 | 7969 | subscribedSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7970 | [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7971 | [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7972 | |
7b33d201 | 7973 | ignoredSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7974 | [ignoredSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7975 | [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7976 | |
7b33d201 | 7977 | subscribedCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7978 | [subscribedCell_ setText:UCLocalize("SHOW_ALL_CHANGES")]; |
7979 | [subscribedCell_ setAccessoryView:subscribedSwitch_]; | |
7980 | [subscribedCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
5829aea2 | 7981 | |
7b33d201 | 7982 | ignoredCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7983 | [ignoredCell_ setText:UCLocalize("IGNORE_UPGRADES")]; |
7984 | [ignoredCell_ setAccessoryView:ignoredSwitch_]; | |
7985 | [ignoredCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
fe8e721f | 7986 | } |
5829aea2 | 7987 | |
fe8e721f | 7988 | - (void) viewDidLoad { |
7d887d0b JF |
7989 | [super viewDidLoad]; |
7990 | ||
fe8e721f GP |
7991 | [[self navigationItem] setTitle:UCLocalize("SETTINGS")]; |
7992 | } | |
5829aea2 | 7993 | |
fe8e721f | 7994 | - (void) releaseSubviews { |
fe8e721f | 7995 | ignoredCell_ = nil; |
fe8e721f | 7996 | subscribedCell_ = nil; |
fe8e721f | 7997 | table_ = nil; |
fe8e721f | 7998 | ignoredSwitch_ = nil; |
fe8e721f | 7999 | subscribedSwitch_ = nil; |
7be3eb32 JF |
8000 | |
8001 | [super releaseSubviews]; | |
fe8e721f GP |
8002 | } |
8003 | ||
8004 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { | |
6840bff3 | 8005 | if ((self = [super init]) != nil) { |
fe8e721f | 8006 | database_ = database; |
7b33d201 | 8007 | name_ = package; |
807ae6d7 JF |
8008 | } return self; |
8009 | } | |
8010 | ||
8011 | - (void) reloadData { | |
fe8e721f GP |
8012 | [super reloadData]; |
8013 | ||
5829aea2 | 8014 | package_ = [database_ packageWithName:name_]; |
f3e2c0ac | 8015 | |
5829aea2 | 8016 | if (package_ != nil) { |
5829aea2 GP |
8017 | [subscribedSwitch_ setOn:([package_ subscribed] ? 1 : 0) animated:NO]; |
8018 | [ignoredSwitch_ setOn:([package_ ignored] ? 1 : 0) animated:NO]; | |
f3e2c0ac | 8019 | } // XXX: what now, G? |
dc5812ec | 8020 | |
5829aea2 GP |
8021 | [table_ reloadData]; |
8022 | } | |
6d9712c4 | 8023 | |
dc5812ec | 8024 | @end |
2367a917 | 8025 | /* }}} */ |
d90a4cd6 | 8026 | |
5829aea2 | 8027 | /* Installed Controller {{{ */ |
f50860ee | 8028 | @interface InstalledController : FilteredPackageListController { |
821b1a0c | 8029 | bool sectioned_; |
dc5812ec JF |
8030 | } |
8031 | ||
5829aea2 | 8032 | - (id) initWithDatabase:(Database *)database; |
5829aea2 | 8033 | - (void) queueStatusDidChange; |
dc5812ec | 8034 | |
dc5812ec JF |
8035 | @end |
8036 | ||
5829aea2 | 8037 | @implementation InstalledController |
b4d89997 | 8038 | |
f050e4d9 JF |
8039 | - (NSURL *) referrerURL { |
8040 | return [NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/installed/", UI_]]; | |
8041 | } | |
8042 | ||
fe8e721f GP |
8043 | - (NSURL *) navigationURL { |
8044 | return [NSURL URLWithString:@"cydia://installed"]; | |
8045 | } | |
b5e7eebb | 8046 | |
b3906a21 | 8047 | - (void) useRecent { |
821b1a0c JF |
8048 | sectioned_ = false; |
8049 | ||
8050 | @synchronized (self) { | |
8051 | [self setFilter:[](Package *package) { | |
8052 | return ![package uninstalled] && package->role_ < 7; | |
8053 | }]; | |
8054 | ||
8055 | [self setSorter:[](NSMutableArray *packages) { | |
b3906a21 | 8056 | [packages radixSortUsingSelector:@selector(recent)]; |
821b1a0c JF |
8057 | }]; |
8058 | } } | |
8059 | ||
89bdef78 | 8060 | - (void) useFilter:(UISegmentedControl *)segmented { |
821b1a0c JF |
8061 | NSInteger selected([segmented selectedSegmentIndex]); |
8062 | if (selected == 2) | |
b3906a21 | 8063 | return [self useRecent]; |
821b1a0c JF |
8064 | bool simple(selected == 0); |
8065 | sectioned_ = true; | |
89bdef78 JF |
8066 | |
8067 | @synchronized (self) { | |
8068 | [self setFilter:[=](Package *package) { | |
8069 | return ![package uninstalled] && package->role_ <= (simple ? 1 : 3); | |
8070 | }]; | |
821b1a0c JF |
8071 | |
8072 | [self setSorter:nullptr]; | |
89bdef78 JF |
8073 | } } |
8074 | ||
98ddcefd JF |
8075 | - (NSArray *) sectionsForPackages:(NSMutableArray *)packages { |
8076 | if (sectioned_) | |
8077 | return [super sectionsForPackages:packages]; | |
8078 | ||
8079 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterLongStyle, kCFDateFormatterNoStyle)); | |
8080 | ||
8081 | NSMutableArray *sections([NSMutableArray arrayWithCapacity:16]); | |
7108bdd2 | 8082 | Section *section(nil); |
98ddcefd JF |
8083 | time_t last(0); |
8084 | ||
8085 | for (size_t offset(0), count([packages count]); offset != count; ++offset) { | |
8086 | Package *package([packages objectAtIndex:offset]); | |
8087 | ||
b3906a21 | 8088 | time_t upgraded([package upgraded]); |
0cadb352 JF |
8089 | if (upgraded < 1168364520) |
8090 | upgraded = 0; | |
8091 | else | |
8092 | upgraded -= upgraded % (60 * 60 * 24); | |
98ddcefd | 8093 | |
b3906a21 JF |
8094 | if (section == nil || upgraded != last) { |
8095 | last = upgraded; | |
98ddcefd JF |
8096 | |
8097 | NSString *name; | |
0cadb352 JF |
8098 | if (upgraded == 0) |
8099 | continue; // XXX: name = UCLocalize("..."); | |
8100 | else { | |
8101 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:upgraded]); | |
8102 | [name autorelease]; | |
8103 | } | |
98ddcefd JF |
8104 | |
8105 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
8106 | [sections addObject:section]; | |
8107 | } | |
8108 | ||
8109 | [section addToCount]; | |
8110 | } | |
8111 | ||
8112 | CFRelease(formatter); | |
8113 | return sections; | |
8114 | } | |
8115 | ||
5829aea2 | 8116 | - (id) initWithDatabase:(Database *)database { |
89bdef78 | 8117 | if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED")]) != nil) { |
c8fbe1f4 | 8118 | UISegmentedControl *segmented([[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:UCLocalize("USER"), UCLocalize("EXPERT"), UCLocalize("RECENT"), nil]] autorelease]); |
3544397d JF |
8119 | [segmented setSelectedSegmentIndex:0]; |
8120 | [segmented setSegmentedControlStyle:UISegmentedControlStyleBar]; | |
8121 | [[self navigationItem] setTitleView:segmented]; | |
8122 | ||
8123 | [segmented addTarget:self action:@selector(modeChanged:) forEvents:UIControlEventValueChanged]; | |
89bdef78 | 8124 | [self useFilter:segmented]; |
3544397d | 8125 | |
5829aea2 GP |
8126 | [self queueStatusDidChange]; |
8127 | } return self; | |
dc5812ec JF |
8128 | } |
8129 | ||
5829aea2 GP |
8130 | #if !AlwaysReload |
8131 | - (void) queueButtonClicked { | |
8132 | [delegate_ queue]; | |
dc5812ec | 8133 | } |
5829aea2 | 8134 | #endif |
dc5812ec | 8135 | |
5829aea2 GP |
8136 | - (void) queueStatusDidChange { |
8137 | #if !AlwaysReload | |
55066b9e | 8138 | if (Queuing_) { |
7d3660da | 8139 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
55066b9e JF |
8140 | initWithTitle:UCLocalize("QUEUE") |
8141 | style:UIBarButtonItemStyleDone | |
8142 | target:self | |
8143 | action:@selector(queueButtonClicked) | |
8144 | ] autorelease]]; | |
8145 | } else { | |
7d3660da | 8146 | [[self navigationItem] setRightBarButtonItem:nil]; |
5829aea2 GP |
8147 | } |
8148 | #endif | |
dc5812ec JF |
8149 | } |
8150 | ||
3544397d | 8151 | - (void) modeChanged:(UISegmentedControl *)segmented { |
89bdef78 | 8152 | [self useFilter:segmented]; |
f50860ee | 8153 | [self reloadData]; |
9a7b04c5 JF |
8154 | } |
8155 | ||
5829aea2 GP |
8156 | @end |
8157 | /* }}} */ | |
b5e7eebb | 8158 | |
5829aea2 | 8159 | /* Source Cell {{{ */ |
a9311516 | 8160 | @interface SourceCell : CyteTableViewCell < |
21ac0ce2 JF |
8161 | CyteTableViewCellDelegate, |
8162 | SourceDelegate | |
5829aea2 | 8163 | > { |
21ac0ce2 | 8164 | _H<Source, 1> source_; |
02735663 | 8165 | _H<NSURL> url_; |
7b33d201 JF |
8166 | _H<UIImage> icon_; |
8167 | _H<NSString> origin_; | |
8168 | _H<NSString> label_; | |
21ac0ce2 | 8169 | _H<UIActivityIndicatorView> indicator_; |
5829aea2 | 8170 | } |
dc5812ec | 8171 | |
5829aea2 | 8172 | - (void) setSource:(Source *)source; |
21ac0ce2 | 8173 | - (void) setFetch:(NSNumber *)fetch; |
6da1297d | 8174 | |
5829aea2 | 8175 | @end |
dc5812ec | 8176 | |
5829aea2 | 8177 | @implementation SourceCell |
36bb2ca2 | 8178 | |
02735663 JF |
8179 | - (void) _setImage:(NSArray *)data { |
8180 | if ([url_ isEqual:[data objectAtIndex:0]]) { | |
8181 | icon_ = [data objectAtIndex:1]; | |
8182 | [content_ setNeedsDisplay]; | |
8183 | } | |
8252b666 JF |
8184 | } |
8185 | ||
7bd76e97 | 8186 | - (void) _setSource:(NSURL *) url { |
8252b666 JF |
8187 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
8188 | ||
7bd76e97 JF |
8189 | if (NSData *data = [NSURLConnection |
8190 | sendSynchronousRequest:[NSURLRequest | |
8191 | requestWithURL:url | |
de5f2673 JF |
8192 | cachePolicy:NSURLRequestUseProtocolCachePolicy |
8193 | timeoutInterval:10 | |
7bd76e97 JF |
8194 | ] |
8195 | ||
8196 | returningResponse:NULL | |
8197 | error:NULL | |
8198 | ]) | |
8199 | if (UIImage *image = [UIImage imageWithData:data]) | |
02735663 | 8200 | [self performSelectorOnMainThread:@selector(_setImage:) withObject:[NSArray arrayWithObjects:url, image, nil] waitUntilDone:NO]; |
8252b666 JF |
8201 | |
8202 | [pool release]; | |
8203 | } | |
8204 | ||
5829aea2 | 8205 | - (void) setSource:(Source *)source { |
21ac0ce2 JF |
8206 | source_ = source; |
8207 | [source_ setDelegate:self]; | |
8208 | ||
8209 | [self setFetch:[NSNumber numberWithBool:[source_ fetch]]]; | |
8210 | ||
8dbf3abc | 8211 | icon_ = [UIImage imageNamed:@"unknown.png"]; |
5829aea2 | 8212 | |
7b33d201 | 8213 | origin_ = [source name]; |
7bd76e97 | 8214 | label_ = [source rooturi]; |
5829aea2 GP |
8215 | |
8216 | [content_ setNeedsDisplay]; | |
8252b666 | 8217 | |
02735663 JF |
8218 | url_ = [source iconURL]; |
8219 | [NSThread detachNewThreadSelector:@selector(_setSource:) toTarget:self withObject:url_]; | |
77801ff1 JF |
8220 | } |
8221 | ||
55066b9e | 8222 | - (void) setAllSource { |
21ac0ce2 JF |
8223 | source_ = nil; |
8224 | [indicator_ stopAnimating]; | |
8225 | ||
8dbf3abc | 8226 | icon_ = [UIImage imageNamed:@"folder.png"]; |
55066b9e JF |
8227 | origin_ = UCLocalize("ALL_SOURCES"); |
8228 | label_ = UCLocalize("ALL_SOURCES_EX"); | |
8229 | [content_ setNeedsDisplay]; | |
8230 | } | |
8231 | ||
5829aea2 GP |
8232 | - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
8233 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
8234 | UIView *content([self contentView]); | |
8235 | CGRect bounds([content bounds]); | |
77801ff1 | 8236 | |
b97fcfc6 | 8237 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
5829aea2 GP |
8238 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8239 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
8240 | [content addSubview:content_]; | |
dc5812ec | 8241 | |
5829aea2 GP |
8242 | [content_ setDelegate:self]; |
8243 | [content_ setOpaque:YES]; | |
53db9999 | 8244 | |
21ac0ce2 JF |
8245 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGraySmall] autorelease]; |
8246 | [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin];// | UIViewAutoresizingFlexibleBottomMargin]; | |
8247 | [content addSubview:indicator_]; | |
8248 | ||
53db9999 | 8249 | [[content_ layer] setContentsGravity:kCAGravityTopLeft]; |
5829aea2 GP |
8250 | } return self; |
8251 | } | |
b4d89997 | 8252 | |
21ac0ce2 JF |
8253 | - (void) layoutSubviews { |
8254 | [super layoutSubviews]; | |
8255 | ||
8256 | UIView *content([self contentView]); | |
8257 | CGRect bounds([content bounds]); | |
8258 | ||
8259 | CGRect frame([indicator_ frame]); | |
8260 | frame.origin.x = bounds.size.width - frame.size.width; | |
8323c1b9 | 8261 | frame.origin.y = Retina((bounds.size.height - frame.size.height) / 2); |
21ac0ce2 JF |
8262 | |
8263 | if (kCFCoreFoundationVersionNumber < 800) | |
8264 | frame.origin.x -= 8; | |
8265 | [indicator_ setFrame:frame]; | |
8266 | } | |
8267 | ||
003fc610 | 8268 | - (NSString *) accessibilityLabel { |
66abff39 | 8269 | return origin_; |
003fc610 GP |
8270 | } |
8271 | ||
5829aea2 GP |
8272 | - (void) drawContentRect:(CGRect)rect { |
8273 | bool highlighted(highlighted_); | |
8274 | float width(rect.size.width); | |
36bb2ca2 | 8275 | |
25c1dafb JF |
8276 | if (icon_ != nil) { |
8277 | CGRect rect; | |
8278 | rect.size = [(UIImage *) icon_ size]; | |
8279 | ||
8280 | while (rect.size.width > 32 || rect.size.height > 32) { | |
8281 | rect.size.width /= 2; | |
8282 | rect.size.height /= 2; | |
8283 | } | |
8284 | ||
86a333c6 JF |
8285 | rect.origin.x = 26 - rect.size.width / 2; |
8286 | rect.origin.y = 26 - rect.size.height / 2; | |
25c1dafb | 8287 | |
8323c1b9 | 8288 | [icon_ drawInRect:Retina(rect)]; |
25c1dafb | 8289 | } |
36bb2ca2 | 8290 | |
5d0438dc | 8291 | if (highlighted && kCFCoreFoundationVersionNumber < 800) |
5829aea2 | 8292 | UISetColor(White_); |
dc5812ec | 8293 | |
5829aea2 GP |
8294 | if (!highlighted) |
8295 | UISetColor(Black_); | |
ffb4fe6a | 8296 | [origin_ drawAtPoint:CGPointMake(52, 8) forWidth:(width - 49) withFont:Font18Bold_ lineBreakMode:NSLineBreakByTruncatingTail]; |
f79a4512 | 8297 | |
5829aea2 | 8298 | if (!highlighted) |
e93fd095 | 8299 | UISetColor(Gray_); |
ffb4fe6a | 8300 | [label_ drawAtPoint:CGPointMake(52, 29) forWidth:(width - 49) withFont:Font12_ lineBreakMode:NSLineBreakByTruncatingTail]; |
5829aea2 | 8301 | } |
dc5812ec | 8302 | |
21ac0ce2 JF |
8303 | - (void) setFetch:(NSNumber *)fetch { |
8304 | if ([fetch boolValue]) | |
8305 | [indicator_ startAnimating]; | |
8306 | else | |
8307 | [indicator_ stopAnimating]; | |
8308 | } | |
8309 | ||
3f8edf70 GP |
8310 | @end |
8311 | /* }}} */ | |
8312 | /* Sources Controller {{{ */ | |
cd79e8cf | 8313 | @interface SourcesController : CyteViewController < |
5829aea2 GP |
8314 | UITableViewDataSource, |
8315 | UITableViewDelegate | |
8316 | > { | |
8317 | _transient Database *database_; | |
c33064f1 JF |
8318 | unsigned era_; |
8319 | ||
bf7c998c | 8320 | _H<UITableView, 2> list_; |
7b33d201 | 8321 | _H<NSMutableArray> sources_; |
5829aea2 | 8322 | int offset_; |
723a0072 | 8323 | |
7b33d201 JF |
8324 | _H<NSString> href_; |
8325 | _H<UIProgressHUD> hud_; | |
8326 | _H<NSError> error_; | |
dc63e78f | 8327 | |
5829aea2 GP |
8328 | NSURLConnection *trivial_bz2_; |
8329 | NSURLConnection *trivial_gz_; | |
b4d89997 | 8330 | |
5829aea2 GP |
8331 | BOOL cydia_; |
8332 | } | |
dc5812ec | 8333 | |
5829aea2 | 8334 | - (id) initWithDatabase:(Database *)database; |
31eedaae | 8335 | - (void) updateButtonsForEditingStatusAnimated:(BOOL)animated; |
5829aea2 GP |
8336 | |
8337 | @end | |
8338 | ||
8339 | @implementation SourcesController | |
8340 | ||
8341 | - (void) _releaseConnection:(NSURLConnection *)connection { | |
8342 | if (connection != nil) { | |
8343 | [connection cancel]; | |
8344 | //[connection setDelegate:nil]; | |
8345 | [connection release]; | |
36bb2ca2 | 8346 | } |
5829aea2 | 8347 | } |
dc5812ec | 8348 | |
5829aea2 | 8349 | - (void) dealloc { |
5829aea2 GP |
8350 | [self _releaseConnection:trivial_gz_]; |
8351 | [self _releaseConnection:trivial_bz2_]; | |
3178d79b | 8352 | |
5829aea2 GP |
8353 | [super dealloc]; |
8354 | } | |
b5e7eebb | 8355 | |
fe8e721f GP |
8356 | - (NSURL *) navigationURL { |
8357 | return [NSURL URLWithString:@"cydia://sources"]; | |
8358 | } | |
8359 | ||
5829aea2 GP |
8360 | - (void) viewDidAppear:(BOOL)animated { |
8361 | [super viewDidAppear:animated]; | |
8362 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
8363 | } | |
9bb3b295 | 8364 | |
5829aea2 | 8365 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
55066b9e | 8366 | return 2; |
5829aea2 | 8367 | } |
8fe19fc1 | 8368 | |
5829aea2 | 8369 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
55066b9e JF |
8370 | if (section == 1) |
8371 | return UCLocalize("INDIVIDUAL_SOURCES"); | |
90ba4f86 | 8372 | return nil; |
36bb2ca2 | 8373 | } |
b4d89997 | 8374 | |
5829aea2 | 8375 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
55066b9e JF |
8376 | switch (section) { |
8377 | case 0: return 1; | |
8378 | case 1: return [sources_ count]; | |
8379 | default: return 0; | |
8380 | } | |
5829aea2 | 8381 | } |
3178d79b | 8382 | |
5829aea2 | 8383 | - (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath { |
c33064f1 JF |
8384 | @synchronized (database_) { |
8385 | if ([database_ era] != era_) | |
8386 | return nil; | |
55066b9e JF |
8387 | if ([indexPath section] != 1) |
8388 | return nil; | |
8dc0d35d | 8389 | NSUInteger index([indexPath row]); |
55066b9e JF |
8390 | if (index >= [sources_ count]) |
8391 | return nil; | |
8392 | return [sources_ objectAtIndex:index]; | |
c33064f1 | 8393 | } } |
b4d89997 | 8394 | |
5829aea2 GP |
8395 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
8396 | static NSString *cellIdentifier = @"SourceCell"; | |
8397 | ||
8398 | SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
55066b9e | 8399 | if (cell == nil) cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease]; |
b89fa270 | 8400 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
5829aea2 | 8401 | |
55066b9e JF |
8402 | Source *source([self sourceAtIndexPath:indexPath]); |
8403 | if (source == nil) | |
8404 | [cell setAllSource]; | |
8405 | else | |
8406 | [cell setSource:source]; | |
8407 | ||
5829aea2 | 8408 | return cell; |
f6e13561 GP |
8409 | } |
8410 | ||
5829aea2 | 8411 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e | 8412 | SectionsController *controller([[[SectionsController alloc] |
5829aea2 | 8413 | initWithDatabase:database_ |
55066b9e JF |
8414 | source:[self sourceAtIndexPath:indexPath] |
8415 | ] autorelease]); | |
5829aea2 | 8416 | |
3f8edf70 | 8417 | [controller setDelegate:delegate_]; |
3f8edf70 | 8418 | [[self navigationController] pushViewController:controller animated:YES]; |
36bb2ca2 | 8419 | } |
b4d89997 | 8420 | |
6840bff3 | 8421 | - (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e JF |
8422 | if ([indexPath section] != 1) |
8423 | return false; | |
5829aea2 GP |
8424 | Source *source = [self sourceAtIndexPath:indexPath]; |
8425 | return [source record] != nil; | |
dcb47737 RP |
8426 | } |
8427 | ||
6840bff3 | 8428 | - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
55066b9e | 8429 | _assert([indexPath section] == 1); |
0e1aa02c JF |
8430 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
8431 | Source *source = [self sourceAtIndexPath:indexPath]; | |
8dc0d35d JF |
8432 | if (source == nil) return; |
8433 | ||
0e1aa02c | 8434 | [Sources_ removeObjectForKey:[source key]]; |
c38cbbec | 8435 | |
392ff7e4 | 8436 | [delegate_ _saveConfig]; |
cc3b7d31 | 8437 | [delegate_ reloadDataWithInvocation:nil]; |
0e1aa02c | 8438 | } |
5829aea2 | 8439 | } |
bcccf498 | 8440 | |
51807490 JF |
8441 | - (void) tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { |
8442 | [self updateButtonsForEditingStatusAnimated:YES]; | |
8443 | } | |
8444 | ||
5829aea2 | 8445 | - (void) complete { |
93460555 | 8446 | [delegate_ addTrivialSource:href_]; |
da3ec19c JF |
8447 | href_ = nil; |
8448 | ||
5829aea2 | 8449 | [delegate_ syncData]; |
3178d79b JF |
8450 | } |
8451 | ||
5829aea2 GP |
8452 | - (NSString *) getWarning { |
8453 | NSString *href(href_); | |
8454 | NSRange colon([href rangeOfString:@"://"]); | |
8455 | if (colon.location != NSNotFound) | |
8456 | href = [href substringFromIndex:(colon.location + 3)]; | |
8457 | href = [href stringByAddingPercentEscapes]; | |
8458 | href = [CydiaURL(@"api/repotag/") stringByAppendingString:href]; | |
5829aea2 GP |
8459 | |
8460 | NSURL *url([NSURL URLWithString:href]); | |
8461 | ||
8462 | NSStringEncoding encoding; | |
8463 | NSError *error(nil); | |
8464 | ||
8465 | if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]) | |
8466 | return [warning length] == 0 ? nil : warning; | |
8467 | return nil; | |
807ae6d7 | 8468 | } |
b4d89997 | 8469 | |
5829aea2 GP |
8470 | - (void) _endConnection:(NSURLConnection *)connection { |
8471 | // XXX: the memory management in this method is horribly awkward | |
8472 | ||
8473 | NSURLConnection **field = NULL; | |
44990507 | 8474 | if (connection == trivial_bz2_) |
5829aea2 GP |
8475 | field = &trivial_bz2_; |
8476 | else if (connection == trivial_gz_) | |
8477 | field = &trivial_gz_; | |
8478 | _assert(field != NULL); | |
8479 | [connection release]; | |
8480 | *field = nil; | |
8481 | ||
8482 | if ( | |
5829aea2 GP |
8483 | trivial_bz2_ == nil && |
8484 | trivial_gz_ == nil | |
8485 | ) { | |
da3ec19c JF |
8486 | NSString *warning(cydia_ ? [self yieldToSelector:@selector(getWarning)] : nil); |
8487 | ||
9eae15b8 JF |
8488 | [delegate_ releaseNetworkActivityIndicator]; |
8489 | ||
8490 | [delegate_ removeProgressHUD:hud_]; | |
9eae15b8 JF |
8491 | hud_ = nil; |
8492 | ||
5829aea2 | 8493 | if (cydia_) { |
da3ec19c | 8494 | if (warning != nil) { |
5829aea2 GP |
8495 | UIAlertView *alert = [[[UIAlertView alloc] |
8496 | initWithTitle:UCLocalize("SOURCE_WARNING") | |
8497 | message:warning | |
8498 | delegate:self | |
8499 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8500 | otherButtonTitles: |
8501 | UCLocalize("ADD_ANYWAY"), | |
8502 | nil | |
5829aea2 GP |
8503 | ] autorelease]; |
8504 | ||
8505 | [alert setContext:@"warning"]; | |
8506 | [alert setNumberOfRows:1]; | |
8507 | [alert show]; | |
708cf61e JF |
8508 | |
8509 | // XXX: there used to be this great mechanism called yieldToPopup... who deleted it? | |
8510 | error_ = nil; | |
8511 | return; | |
8512 | } | |
8513 | ||
8514 | [self complete]; | |
5829aea2 GP |
8515 | } else if (error_ != nil) { |
8516 | UIAlertView *alert = [[[UIAlertView alloc] | |
8517 | initWithTitle:UCLocalize("VERIFICATION_ERROR") | |
8518 | message:[error_ localizedDescription] | |
8519 | delegate:self | |
8520 | cancelButtonTitle:UCLocalize("OK") | |
8521 | otherButtonTitles:nil | |
8522 | ] autorelease]; | |
8523 | ||
8524 | [alert setContext:@"urlerror"]; | |
8525 | [alert show]; | |
da3ec19c JF |
8526 | |
8527 | href_ = nil; | |
5829aea2 GP |
8528 | } else { |
8529 | UIAlertView *alert = [[[UIAlertView alloc] | |
8530 | initWithTitle:UCLocalize("NOT_REPOSITORY") | |
8531 | message:UCLocalize("NOT_REPOSITORY_EX") | |
8532 | delegate:self | |
8533 | cancelButtonTitle:UCLocalize("OK") | |
8534 | otherButtonTitles:nil | |
8535 | ] autorelease]; | |
8536 | ||
8537 | [alert setContext:@"trivial"]; | |
8538 | [alert show]; | |
da3ec19c JF |
8539 | |
8540 | href_ = nil; | |
5829aea2 GP |
8541 | } |
8542 | ||
7b33d201 | 8543 | error_ = nil; |
5829aea2 | 8544 | } |
807ae6d7 | 8545 | } |
8fe19fc1 | 8546 | |
5829aea2 GP |
8547 | - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { |
8548 | switch ([response statusCode]) { | |
8549 | case 200: | |
8550 | cydia_ = YES; | |
8551 | } | |
8e05f686 RP |
8552 | } |
8553 | ||
5829aea2 | 8554 | - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { |
47ff9ab8 | 8555 | lprintf("connection:\"%s\" didFailWithError:\"%s\"\n", [href_ UTF8String], [[error localizedDescription] UTF8String]); |
7b33d201 | 8556 | error_ = error; |
5829aea2 | 8557 | [self _endConnection:connection]; |
807ae6d7 | 8558 | } |
b4d89997 | 8559 | |
5829aea2 GP |
8560 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
8561 | [self _endConnection:connection]; | |
8562 | } | |
b4d89997 | 8563 | |
5829aea2 | 8564 | - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method { |
2e1652a9 JF |
8565 | NSURL *url([NSURL URLWithString:href]); |
8566 | ||
5829aea2 | 8567 | NSMutableURLRequest *request = [NSMutableURLRequest |
2e1652a9 | 8568 | requestWithURL:url |
5829aea2 | 8569 | cachePolicy:NSURLRequestUseProtocolCachePolicy |
dd6f3b7b | 8570 | timeoutInterval:10 |
5829aea2 | 8571 | ]; |
bc11cf5b | 8572 | |
5829aea2 | 8573 | [request setHTTPMethod:method]; |
b4d89997 | 8574 | |
5829aea2 GP |
8575 | if (Machine_ != NULL) |
8576 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
2e1652a9 | 8577 | |
1209b7de JF |
8578 | if (UniqueID_ != nil) |
8579 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
8580 | ||
2e1652a9 | 8581 | if ([url isCydiaSecure]) { |
1209b7de | 8582 | if (UniqueID_ != nil) |
c83678e8 | 8583 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Cydia-Id"]; |
2e1652a9 | 8584 | } |
b4d89997 | 8585 | |
5829aea2 | 8586 | return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; |
3178d79b JF |
8587 | } |
8588 | ||
6840bff3 | 8589 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
5829aea2 | 8590 | NSString *context([alert context]); |
dc5812ec | 8591 | |
5829aea2 GP |
8592 | if ([context isEqualToString:@"source"]) { |
8593 | switch (button) { | |
8594 | case 1: { | |
8595 | NSString *href = [[alert textField] text]; | |
8a2d167a JF |
8596 | href = VerifySource(href); |
8597 | if (href == nil) | |
2595e4c3 | 8598 | break; |
8a2d167a | 8599 | href_ = href; |
b4d89997 | 8600 | |
5829aea2 GP |
8601 | trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; |
8602 | trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; | |
b4d89997 | 8603 | |
5829aea2 | 8604 | cydia_ = false; |
8fe19fc1 | 8605 | |
5829aea2 | 8606 | // XXX: this is stupid |
7b33d201 | 8607 | hud_ = [delegate_ addProgressHUD]; |
5829aea2 | 8608 | [hud_ setText:UCLocalize("VERIFYING_URL")]; |
dcaecde2 | 8609 | [delegate_ retainNetworkActivityIndicator]; |
5829aea2 | 8610 | } break; |
770f2a8e | 8611 | |
5829aea2 GP |
8612 | case 0: |
8613 | break; | |
bc11cf5b | 8614 | |
5829aea2 GP |
8615 | _nodefault |
8616 | } | |
770f2a8e | 8617 | |
5829aea2 GP |
8618 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8619 | } else if ([context isEqualToString:@"trivial"]) | |
8620 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8621 | else if ([context isEqualToString:@"urlerror"]) | |
8622 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8623 | else if ([context isEqualToString:@"warning"]) { | |
8624 | switch (button) { | |
8625 | case 1: | |
da3ec19c | 8626 | [self performSelector:@selector(complete) withObject:nil afterDelay:0]; |
5829aea2 | 8627 | break; |
770f2a8e | 8628 | |
5829aea2 GP |
8629 | case 0: |
8630 | break; | |
b5e7eebb | 8631 | |
5829aea2 GP |
8632 | _nodefault |
8633 | } | |
770f2a8e | 8634 | |
5829aea2 GP |
8635 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8636 | } | |
8637 | } | |
770f2a8e | 8638 | |
e67ebdad JF |
8639 | - (void) updateButtonsForEditingStatusAnimated:(BOOL)animated { |
8640 | BOOL editing([list_ isEditing]); | |
8641 | ||
8642 | if (editing) | |
8643 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8644 | initWithTitle:UCLocalize("ADD") | |
8645 | style:UIBarButtonItemStylePlain | |
8646 | target:self | |
8647 | action:@selector(addButtonClicked) | |
8648 | ] autorelease] animated:animated]; | |
8649 | else if ([delegate_ updating]) | |
8650 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8651 | initWithTitle:UCLocalize("CANCEL") | |
8652 | style:UIBarButtonItemStyleDone | |
8653 | target:self | |
8654 | action:@selector(cancelButtonClicked) | |
8655 | ] autorelease] animated:animated]; | |
8656 | else | |
8657 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8658 | initWithTitle:UCLocalize("REFRESH") | |
8659 | style:UIBarButtonItemStylePlain | |
8660 | target:self | |
8661 | action:@selector(refreshButtonClicked) | |
8662 | ] autorelease] animated:animated]; | |
8663 | ||
8664 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
8665 | initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) | |
8666 | style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
8667 | target:self | |
8668 | action:@selector(editButtonClicked) | |
8669 | ] autorelease] animated:animated]; | |
8670 | } | |
8671 | ||
fe8e721f | 8672 | - (void) loadView { |
e8cbebe4 | 8673 | list_ = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain] autorelease]; |
fe8e721f | 8674 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6ac6e186 | 8675 | [list_ setRowHeight:53]; |
7b33d201 | 8676 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f | 8677 | [list_ setDelegate:self]; |
e8cbebe4 | 8678 | [self setView:list_]; |
fe8e721f | 8679 | } |
770f2a8e | 8680 | |
fe8e721f | 8681 | - (void) viewDidLoad { |
7d887d0b JF |
8682 | [super viewDidLoad]; |
8683 | ||
fe8e721f | 8684 | [[self navigationItem] setTitle:UCLocalize("SOURCES")]; |
31eedaae JF |
8685 | [self updateButtonsForEditingStatusAnimated:NO]; |
8686 | } | |
8687 | ||
393699d7 | 8688 | - (void) viewWillAppear:(BOOL)animated { |
31eedaae JF |
8689 | [super viewWillAppear:animated]; |
8690 | ||
8691 | [list_ setEditing:NO]; | |
8692 | [self updateButtonsForEditingStatusAnimated:NO]; | |
fe8e721f | 8693 | } |
770f2a8e | 8694 | |
fe8e721f | 8695 | - (void) releaseSubviews { |
fe8e721f | 8696 | list_ = nil; |
7be3eb32 | 8697 | |
4f9acb7c JF |
8698 | sources_ = nil; |
8699 | ||
7be3eb32 | 8700 | [super releaseSubviews]; |
fe8e721f | 8701 | } |
770f2a8e | 8702 | |
fe8e721f GP |
8703 | - (id) initWithDatabase:(Database *)database { |
8704 | if ((self = [super init]) != nil) { | |
8705 | database_ = database; | |
807ae6d7 JF |
8706 | } return self; |
8707 | } | |
770f2a8e | 8708 | |
807ae6d7 | 8709 | - (void) reloadData { |
fe8e721f | 8710 | [super reloadData]; |
e67ebdad | 8711 | [self updateButtonsForEditingStatusAnimated:YES]; |
fe8e721f | 8712 | |
c33064f1 JF |
8713 | @synchronized (database_) { |
8714 | era_ = [database_ era]; | |
8715 | ||
4f9acb7c | 8716 | sources_ = [NSMutableArray arrayWithCapacity:16]; |
5829aea2 GP |
8717 | [sources_ addObjectsFromArray:[database_ sources]]; |
8718 | _trace(); | |
90ba4f86 | 8719 | [sources_ sortUsingSelector:@selector(compareByName:)]; |
5829aea2 GP |
8720 | _trace(); |
8721 | ||
8722 | int count([sources_ count]); | |
8723 | offset_ = 0; | |
8724 | for (int i = 0; i != count; i++) { | |
8725 | if ([[sources_ objectAtIndex:i] record] == nil) | |
8726 | break; | |
8727 | offset_++; | |
770f2a8e | 8728 | } |
807ae6d7 | 8729 | |
5829aea2 | 8730 | [list_ reloadData]; |
c33064f1 | 8731 | } } |
770f2a8e | 8732 | |
5829aea2 GP |
8733 | - (void) showAddSourcePrompt { |
8734 | UIAlertView *alert = [[[UIAlertView alloc] | |
8735 | initWithTitle:UCLocalize("ENTER_APT_URL") | |
8736 | message:nil | |
8737 | delegate:self | |
8738 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8739 | otherButtonTitles: |
8740 | UCLocalize("ADD_SOURCE"), | |
8741 | nil | |
5829aea2 | 8742 | ] autorelease]; |
770f2a8e | 8743 | |
5829aea2 | 8744 | [alert setContext:@"source"]; |
770f2a8e | 8745 | |
5829aea2 GP |
8746 | [alert setNumberOfRows:1]; |
8747 | [alert addTextFieldWithValue:@"http://" label:@""]; | |
770f2a8e | 8748 | |
5829aea2 GP |
8749 | UITextInputTraits *traits = [[alert textField] textInputTraits]; |
8750 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
8751 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
8752 | [traits setKeyboardType:UIKeyboardTypeURL]; | |
8753 | // XXX: UIReturnKeyDone | |
8754 | [traits setReturnKeyType:UIReturnKeyNext]; | |
770f2a8e | 8755 | |
5829aea2 | 8756 | [alert show]; |
770f2a8e JF |
8757 | } |
8758 | ||
5829aea2 GP |
8759 | - (void) addButtonClicked { |
8760 | [self showAddSourcePrompt]; | |
770f2a8e JF |
8761 | } |
8762 | ||
e67ebdad JF |
8763 | - (void) refreshButtonClicked { |
8764 | if ([delegate_ requestUpdate]) | |
8765 | [self updateButtonsForEditingStatusAnimated:YES]; | |
8766 | } | |
5829aea2 | 8767 | |
e67ebdad JF |
8768 | - (void) cancelButtonClicked { |
8769 | [delegate_ cancelUpdate]; | |
5829aea2 GP |
8770 | } |
8771 | ||
8772 | - (void) editButtonClicked { | |
8773 | [list_ setEditing:![list_ isEditing] animated:YES]; | |
31eedaae | 8774 | [self updateButtonsForEditingStatusAnimated:YES]; |
770f2a8e JF |
8775 | } |
8776 | ||
21c6da4b GP |
8777 | @end |
8778 | /* }}} */ | |
f3e11d24 | 8779 | |
f3e11d24 | 8780 | /* Stash Controller {{{ */ |
cd79e8cf | 8781 | @interface StashController : CyteViewController { |
7b33d201 JF |
8782 | _H<UIActivityIndicatorView> spinner_; |
8783 | _H<UILabel> status_; | |
8784 | _H<UILabel> caption_; | |
f3e11d24 | 8785 | } |
6840bff3 | 8786 | |
f3e11d24 GP |
8787 | @end |
8788 | ||
5829aea2 | 8789 | @implementation StashController |
f3e11d24 | 8790 | |
fe8e721f | 8791 | - (void) loadView { |
39470a3a JF |
8792 | UIView *view([[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]); |
8793 | [view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
8794 | [self setView:view]; | |
8795 | ||
8796 | [view setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; | |
fe8e721f | 8797 | |
7b33d201 | 8798 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; |
fe8e721f | 8799 | CGRect spinrect = [spinner_ frame]; |
8323c1b9 | 8800 | spinrect.origin.x = Retina([[self view] frame].size.width / 2 - spinrect.size.width / 2); |
fe8e721f GP |
8801 | spinrect.origin.y = [[self view] frame].size.height - 80.0f; |
8802 | [spinner_ setFrame:spinrect]; | |
8803 | [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; | |
39470a3a | 8804 | [view addSubview:spinner_]; |
fe8e721f GP |
8805 | [spinner_ startAnimating]; |
8806 | ||
8807 | CGRect captrect; | |
8808 | captrect.size.width = [[self view] frame].size.width; | |
8809 | captrect.size.height = 40.0f; | |
8810 | captrect.origin.x = 0; | |
8323c1b9 | 8811 | captrect.origin.y = Retina([[self view] frame].size.height / 2 - captrect.size.height * 2); |
7b33d201 | 8812 | caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; |
fe8e721f GP |
8813 | [caption_ setText:UCLocalize("PREPARING_FILESYSTEM")]; |
8814 | [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
8815 | [caption_ setFont:[UIFont boldSystemFontOfSize:28.0f]]; | |
8816 | [caption_ setTextColor:[UIColor whiteColor]]; | |
8817 | [caption_ setBackgroundColor:[UIColor clearColor]]; | |
8818 | [caption_ setShadowColor:[UIColor blackColor]]; | |
b129e6d9 | 8819 | [caption_ setTextAlignment:NSTextAlignmentCenter]; |
39470a3a | 8820 | [view addSubview:caption_]; |
fe8e721f GP |
8821 | |
8822 | CGRect statusrect; | |
8823 | statusrect.size.width = [[self view] frame].size.width; | |
8824 | statusrect.size.height = 30.0f; | |
8825 | statusrect.origin.x = 0; | |
8323c1b9 | 8826 | statusrect.origin.y = Retina([[self view] frame].size.height / 2 - statusrect.size.height); |
7b33d201 | 8827 | status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; |
fe8e721f GP |
8828 | [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
8829 | [status_ setText:UCLocalize("EXIT_WHEN_COMPLETE")]; | |
8830 | [status_ setFont:[UIFont systemFontOfSize:16.0f]]; | |
8831 | [status_ setTextColor:[UIColor whiteColor]]; | |
8832 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
8833 | [status_ setShadowColor:[UIColor blackColor]]; | |
b129e6d9 | 8834 | [status_ setTextAlignment:NSTextAlignmentCenter]; |
39470a3a | 8835 | [view addSubview:status_]; |
fe8e721f GP |
8836 | } |
8837 | ||
67dd58c7 JF |
8838 | - (void) releaseSubviews { |
8839 | spinner_ = nil; | |
8840 | status_ = nil; | |
8841 | caption_ = nil; | |
7be3eb32 JF |
8842 | |
8843 | [super releaseSubviews]; | |
67dd58c7 JF |
8844 | } |
8845 | ||
770f2a8e | 8846 | @end |
807ae6d7 | 8847 | /* }}} */ |
770f2a8e | 8848 | |
a06e9179 JF |
8849 | @interface CYURLCache : SDURLCache { |
8850 | } | |
8851 | ||
8852 | @end | |
8853 | ||
8854 | @implementation CYURLCache | |
8855 | ||
8856 | - (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request { | |
8857 | #if !ForRelease | |
8858 | if (false); | |
8859 | else if ([event isEqualToString:@"no-cache"]) | |
8860 | event = @"!!!"; | |
8861 | else if ([event isEqualToString:@"store"]) | |
8862 | event = @">>>"; | |
8863 | else if ([event isEqualToString:@"invalid"]) | |
8864 | event = @"???"; | |
8865 | else if ([event isEqualToString:@"memory"]) | |
8866 | event = @"mem"; | |
8867 | else if ([event isEqualToString:@"disk"]) | |
8868 | event = @"ssd"; | |
8869 | else if ([event isEqualToString:@"miss"]) | |
8870 | event = @"---"; | |
8871 | ||
8872 | NSLog(@"%@: %@", event, [[request URL] absoluteString]); | |
8873 | #endif | |
8874 | } | |
8875 | ||
5e845121 JF |
8876 | - (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request { |
8877 | if (NSURLResponse *response = [cached response]) | |
8878 | if (NSString *mime = [response MIMEType]) | |
8879 | if ([mime isEqualToString:@"text/cache-manifest"]) { | |
8880 | NSURL *url([response URL]); | |
8881 | ||
8882 | #if !ForRelease | |
8883 | NSLog(@"###: %@", [url absoluteString]); | |
8884 | #endif | |
8885 | ||
8886 | @synchronized (HostConfig_) { | |
8887 | [CachedURLs_ addObject:url]; | |
8888 | } | |
8889 | } | |
8890 | ||
8891 | [super storeCachedResponse:cached forRequest:request]; | |
8892 | } | |
8893 | ||
584daea0 JF |
8894 | - (void) createDiskCachePath { |
8895 | [super createDiskCachePath]; | |
584daea0 JF |
8896 | } |
8897 | ||
a06e9179 JF |
8898 | @end |
8899 | ||
2367a917 | 8900 | @interface Cydia : UIApplication < |
adb61bda | 8901 | ConfirmationControllerDelegate, |
6915b806 | 8902 | DatabaseDelegate, |
c88974b1 | 8903 | CydiaDelegate |
2367a917 | 8904 | > { |
7b33d201 | 8905 | _H<UIWindow> window_; |
5fe2bcc6 | 8906 | _H<CydiaTabBarController> tabbar_; |
9549563e | 8907 | _H<CyteTabBarController> emulated_; |
fedd38fe | 8908 | _H<AppCacheController> appcache_; |
3931b718 | 8909 | |
7b33d201 JF |
8910 | _H<NSMutableArray> essential_; |
8911 | _H<NSMutableArray> broken_; | |
dc5812ec JF |
8912 | |
8913 | Database *database_; | |
dc5812ec | 8914 | |
7b33d201 | 8915 | _H<NSURL> starturl_; |
54043703 | 8916 | |
235f5487 | 8917 | unsigned locked_; |
54043703 | 8918 | unsigned activity_; |
9b619239 | 8919 | |
7b33d201 | 8920 | _H<StashController> stash_; |
f3e11d24 | 8921 | |
8c02abc8 | 8922 | bool loaded_; |
dc5812ec JF |
8923 | } |
8924 | ||
fed0d010 | 8925 | - (void) loadData; |
670a0494 | 8926 | |
dc5812ec JF |
8927 | @end |
8928 | ||
8929 | @implementation Cydia | |
8930 | ||
2ef6faad | 8931 | - (void) lockSuspend { |
8a3b565c JF |
8932 | if (locked_++ == 0) { |
8933 | if ($SBSSetInterceptsMenuButtonForever != NULL) | |
8934 | (*$SBSSetInterceptsMenuButtonForever)(true); | |
26c8a4c8 JF |
8935 | |
8936 | [self setIdleTimerDisabled:YES]; | |
8a3b565c | 8937 | } |
2ef6faad JF |
8938 | } |
8939 | ||
8940 | - (void) unlockSuspend { | |
8a3b565c | 8941 | if (--locked_ == 0) { |
26c8a4c8 JF |
8942 | [self setIdleTimerDisabled:NO]; |
8943 | ||
8a3b565c JF |
8944 | if ($SBSSetInterceptsMenuButtonForever != NULL) |
8945 | (*$SBSSetInterceptsMenuButtonForever)(false); | |
8946 | } | |
2ef6faad JF |
8947 | } |
8948 | ||
b5e7eebb | 8949 | - (void) beginUpdate { |
7585ce66 | 8950 | [tabbar_ beginUpdate]; |
b5e7eebb GP |
8951 | } |
8952 | ||
e67ebdad JF |
8953 | - (void) cancelUpdate { |
8954 | [tabbar_ cancelUpdate]; | |
8955 | } | |
8956 | ||
8957 | - (bool) requestUpdate { | |
8958 | if (IsReachable("cydia.saurik.com")) { | |
8959 | [self beginUpdate]; | |
8960 | return true; | |
8961 | } else { | |
8962 | UIAlertView *alert = [[[UIAlertView alloc] | |
8963 | initWithTitle:[NSString stringWithFormat:Colon_, Error_, UCLocalize("REFRESH")] | |
8964 | message:@"Host Unreachable" // XXX: Localize | |
8965 | delegate:self | |
8966 | cancelButtonTitle:UCLocalize("OK") | |
8967 | otherButtonTitles:nil | |
8968 | ] autorelease]; | |
8969 | ||
8970 | [alert setContext:@"norefresh"]; | |
8971 | [alert show]; | |
8972 | ||
8973 | return false; | |
8974 | } | |
8975 | } | |
8976 | ||
b5e7eebb | 8977 | - (BOOL) updating { |
7585ce66 | 8978 | return [tabbar_ updating]; |
b5e7eebb GP |
8979 | } |
8980 | ||
9bedffaa JF |
8981 | - (void) _loaded { |
8982 | if ([broken_ count] != 0) { | |
8983 | int count = [broken_ count]; | |
8984 | ||
37d2b2a9 | 8985 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 8986 | initWithTitle:(count == 1 ? UCLocalize("HALFINSTALLED_PACKAGE") : [NSString stringWithFormat:UCLocalize("HALFINSTALLED_PACKAGES"), count]) |
b5e7eebb GP |
8987 | message:UCLocalize("HALFINSTALLED_PACKAGE_EX") |
8988 | delegate:self | |
1dc38e9c | 8989 | cancelButtonTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("FORCIBLY_CLEAR"), UCLocalize("UNSAFE")] |
1aa29546 JF |
8990 | otherButtonTitles: |
8991 | UCLocalize("TEMPORARY_IGNORE"), | |
8992 | nil | |
9bedffaa JF |
8993 | ] autorelease]; |
8994 | ||
37d2b2a9 | 8995 | [alert setContext:@"fixhalf"]; |
59befad5 | 8996 | [alert setNumberOfRows:2]; |
37d2b2a9 | 8997 | [alert show]; |
9bedffaa JF |
8998 | } else if (!Ignored_ && [essential_ count] != 0) { |
8999 | int count = [essential_ count]; | |
9000 | ||
37d2b2a9 | 9001 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 9002 | initWithTitle:(count == 1 ? UCLocalize("ESSENTIAL_UPGRADE") : [NSString stringWithFormat:UCLocalize("ESSENTIAL_UPGRADES"), count]) |
b5e7eebb GP |
9003 | message:UCLocalize("ESSENTIAL_UPGRADE_EX") |
9004 | delegate:self | |
9005 | cancelButtonTitle:UCLocalize("TEMPORARY_IGNORE") | |
1aa29546 JF |
9006 | otherButtonTitles: |
9007 | UCLocalize("UPGRADE_ESSENTIAL"), | |
9008 | UCLocalize("COMPLETE_UPGRADE"), | |
9009 | nil | |
9bedffaa JF |
9010 | ] autorelease]; |
9011 | ||
37d2b2a9 GP |
9012 | [alert setContext:@"upgrade"]; |
9013 | [alert show]; | |
9bedffaa JF |
9014 | } |
9015 | } | |
9016 | ||
2925cbba JF |
9017 | - (void) returnToCydia { |
9018 | [self _loaded]; | |
9019 | } | |
9020 | ||
42bbf27d JF |
9021 | - (void) reloadSpringBoard { |
9022 | if (kCFCoreFoundationVersionNumber >= 700) // XXX: iOS 6.x | |
9023 | system("/bin/launchctl stop com.apple.backboardd"); | |
9024 | else | |
9025 | system("/bin/launchctl stop com.apple.SpringBoard"); | |
9026 | sleep(15); | |
9027 | system("/usr/bin/killall backboardd SpringBoard"); | |
9028 | } | |
9029 | ||
7623f855 | 9030 | - (void) _saveConfig { |
4e479350 | 9031 | SaveConfig(database_); |
7623f855 JF |
9032 | } |
9033 | ||
89571a5b | 9034 | // Navigation controller for the queuing badge. |
15f0d613 | 9035 | - (UINavigationController *) queueNavigationController { |
89571a5b GP |
9036 | NSArray *controllers = [tabbar_ viewControllers]; |
9037 | return [controllers objectAtIndex:3]; | |
9038 | } | |
9039 | ||
302bf91c JF |
9040 | - (void) unloadData { |
9041 | [tabbar_ unloadData]; | |
9042 | } | |
9043 | ||
7623f855 JF |
9044 | - (void) _updateData { |
9045 | [self _saveConfig]; | |
302bf91c | 9046 | [self unloadData]; |
f6371a33 | 9047 | |
15f0d613 | 9048 | UINavigationController *navigation = [self queueNavigationController]; |
b5e7eebb | 9049 | |
4305896c | 9050 | id queuedelegate = nil; |
89571a5b GP |
9051 | if ([[navigation viewControllers] count] > 0) |
9052 | queuedelegate = [[navigation viewControllers] objectAtIndex:0]; | |
bc11cf5b | 9053 | |
89571a5b GP |
9054 | [queuedelegate queueStatusDidChange]; |
9055 | [[navigation tabBarItem] setBadgeValue:(Queuing_ ? UCLocalize("Q_D") : nil)]; | |
7623f855 JF |
9056 | } |
9057 | ||
6f87c61a | 9058 | - (void) _refreshIfPossible { |
8c02abc8 | 9059 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
bc11cf5b | 9060 | |
6f87c61a JF |
9061 | NSDate *update([[NSDictionary dictionaryWithContentsOfFile:@ CacheState_] objectForKey:@"LastUpdate"]); |
9062 | ||
45e21ffa | 9063 | bool recently = false; |
45e21ffa GP |
9064 | if (update != nil) { |
9065 | NSTimeInterval interval([update timeIntervalSinceNow]); | |
805ba1b0 | 9066 | if (interval > -(15*60)) |
45e21ffa GP |
9067 | recently = true; |
9068 | } | |
9069 | ||
9070 | // Don't automatic refresh if: | |
9071 | // - We already refreshed recently. | |
9072 | // - We already auto-refreshed this launch. | |
9073 | // - Auto-refresh is disabled. | |
8d3505c5 JF |
9074 | // - Cydia's server is not reachable |
9075 | if (recently || loaded_ || ManualRefresh || !IsReachable("cydia.saurik.com")) { | |
35f0a3b5 | 9076 | // If we are cancelling, we need to make sure it knows it's already loaded. |
45e21ffa | 9077 | loaded_ = true; |
d13edf44 JF |
9078 | |
9079 | [self performSelectorOnMainThread:@selector(_loaded) withObject:nil waitUntilDone:NO]; | |
45e21ffa GP |
9080 | } else { |
9081 | // We are going to load, so remember that. | |
9082 | loaded_ = true; | |
45e21ffa | 9083 | |
6f87c61a | 9084 | [tabbar_ performSelectorOnMainThread:@selector(beginUpdate) withObject:nil waitUntilDone:NO]; |
d13edf44 | 9085 | } |
9aecdc9c | 9086 | |
8c02abc8 | 9087 | [pool release]; |
9aecdc9c GP |
9088 | } |
9089 | ||
9090 | - (void) refreshIfPossible { | |
6f87c61a | 9091 | [NSThread detachNewThreadSelector:@selector(_refreshIfPossible) toTarget:self withObject:nil]; |
9aecdc9c GP |
9092 | } |
9093 | ||
e09e1589 | 9094 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
1a83afc6 | 9095 | _profile(reloadDataWithInvocation) |
e09e1589 | 9096 | @synchronized (self) { |
851f4a99 | 9097 | UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); |
1450c2b3 JF |
9098 | if (hud != nil) |
9099 | [hud setText:UCLocalize("RELOADING_DATA")]; | |
dc5812ec | 9100 | |
4ba8f30a | 9101 | [database_ yieldToSelector:@selector(reloadDataWithInvocation:) withObject:invocation]; |
d061f4ba | 9102 | |
36bb2ca2 | 9103 | size_t changes(0); |
9bedffaa | 9104 | |
a54b1c10 | 9105 | [essential_ removeAllObjects]; |
9bedffaa | 9106 | [broken_ removeAllObjects]; |
b4d89997 | 9107 | |
1a83afc6 | 9108 | _profile(reloadDataWithInvocation$Essential) |
670a0494 | 9109 | NSArray *packages([database_ packages]); |
affeffc7 | 9110 | for (Package *package in packages) { |
9bedffaa JF |
9111 | if ([package half]) |
9112 | [broken_ addObject:package]; | |
823e2d97 JF |
9113 | if ([package upgradableAndEssential:YES] && ![package ignored]) { |
9114 | if ([package essential] && [package installed] != nil) | |
a54b1c10 | 9115 | [essential_ addObject:package]; |
36bb2ca2 | 9116 | ++changes; |
a54b1c10 | 9117 | } |
36bb2ca2 | 9118 | } |
1a83afc6 | 9119 | _end |
b4d89997 | 9120 | |
89571a5b | 9121 | UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem]; |
36bb2ca2 | 9122 | if (changes != 0) { |
0e1784b4 | 9123 | _trace(); |
36bb2ca2 | 9124 | NSString *badge([[NSNumber numberWithInt:changes] stringValue]); |
45e21ffa | 9125 | [changesItem setBadgeValue:badge]; |
65a03a7a | 9126 | [changesItem setAnimatedBadge:([essential_ count] > 0)]; |
0e1784b4 | 9127 | [self setApplicationIconBadgeNumber:changes]; |
c25a610d | 9128 | } else { |
0e1784b4 | 9129 | _trace(); |
45e21ffa GP |
9130 | [changesItem setBadgeValue:nil]; |
9131 | [changesItem setAnimatedBadge:NO]; | |
0e1784b4 | 9132 | [self setApplicationIconBadgeNumber:0]; |
c25a610d | 9133 | } |
b4d89997 | 9134 | |
838ec6e3 | 9135 | Queuing_ = false; |
7623f855 | 9136 | [self _updateData]; |
be64dfbf JF |
9137 | |
9138 | if (hud != nil) | |
9139 | [self removeProgressHUD:hud]; | |
1a83afc6 JF |
9140 | } |
9141 | _end | |
9142 | ||
9143 | PrintTimes(); | |
9144 | } | |
6d9712c4 | 9145 | |
7b0ce2da | 9146 | - (void) updateData { |
7623f855 | 9147 | [self _updateData]; |
b4d89997 JF |
9148 | } |
9149 | ||
383a58ac JF |
9150 | - (void) updateDataAndLoad { |
9151 | [self _updateData]; | |
9152 | if ([database_ progressDelegate] == nil) | |
9153 | [self _loaded]; | |
9154 | } | |
9155 | ||
7b0ce2da JF |
9156 | - (void) update_ { |
9157 | [database_ update]; | |
fca2f596 | 9158 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
7b0ce2da JF |
9159 | } |
9160 | ||
2e9123cb JF |
9161 | - (void) disemulate { |
9162 | if (emulated_ == nil) | |
9163 | return; | |
9164 | ||
81628115 JF |
9165 | if ([window_ respondsToSelector:@selector(setRootViewController:)]) |
9166 | [window_ setRootViewController:tabbar_]; | |
237c3d1c JF |
9167 | else { |
9168 | [window_ addSubview:[tabbar_ view]]; | |
9169 | [[emulated_ view] removeFromSuperview]; | |
9170 | } | |
9171 | ||
2e9123cb JF |
9172 | emulated_ = nil; |
9173 | [window_ setUserInteractionEnabled:YES]; | |
9174 | } | |
9175 | ||
9176 | - (void) presentModalViewController:(UIViewController *)controller force:(BOOL)force { | |
15f0d613 | 9177 | UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:controller] autorelease]); |
2e9123cb JF |
9178 | |
9179 | UIViewController *parent; | |
9180 | if (emulated_ == nil) | |
9181 | parent = tabbar_; | |
9182 | else if (!force) | |
9183 | parent = emulated_; | |
9184 | else { | |
9185 | [self disemulate]; | |
9186 | parent = tabbar_; | |
9187 | } | |
9188 | ||
483881e4 JF |
9189 | if (IsWildcat_) |
9190 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
2e9123cb | 9191 | [parent presentModalViewController:navigation animated:YES]; |
6915b806 JF |
9192 | } |
9193 | ||
9194 | - (ProgressController *) invokeNewProgress:(NSInvocation *)invocation forController:(UINavigationController *)navigation withTitle:(NSString *)title { | |
9195 | ProgressController *progress([[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]); | |
9196 | ||
9197 | if (navigation != nil) | |
9198 | [navigation pushViewController:progress animated:YES]; | |
9199 | else | |
2e9123cb | 9200 | [self presentModalViewController:progress force:YES]; |
6915b806 JF |
9201 | |
9202 | [progress invoke:invocation withTitle:title]; | |
9203 | return progress; | |
9204 | } | |
9205 | ||
9206 | - (void) detachNewProgressSelector:(SEL)selector toTarget:(id)target forController:(UINavigationController *)navigation title:(NSString *)title { | |
9207 | [self invokeNewProgress:[NSInvocation invocationWithSelector:selector forTarget:target] forController:navigation withTitle:title]; | |
9208 | } | |
9209 | ||
9210 | - (void) repairWithInvocation:(NSInvocation *)invocation { | |
9211 | _trace(); | |
8d5bc2ad | 9212 | [self invokeNewProgress:invocation forController:nil withTitle:@"REPAIRING"]; |
6915b806 JF |
9213 | _trace(); |
9214 | } | |
9215 | ||
9216 | - (void) repairWithSelector:(SEL)selector { | |
9217 | [self performSelectorOnMainThread:@selector(repairWithInvocation:) withObject:[NSInvocation invocationWithSelector:selector forTarget:database_] waitUntilDone:YES]; | |
9218 | } | |
9219 | ||
e09e1589 JF |
9220 | - (void) reloadData { |
9221 | [self reloadDataWithInvocation:nil]; | |
2925cbba JF |
9222 | if ([database_ progressDelegate] == nil) |
9223 | [self _loaded]; | |
e09e1589 JF |
9224 | } |
9225 | ||
7b0ce2da | 9226 | - (void) syncData { |
1fe5dc43 | 9227 | [self _saveConfig]; |
33e30380 JF |
9228 | [self detachNewProgressSelector:@selector(update_) toTarget:self forController:nil title:@"UPDATING_SOURCES"]; |
9229 | } | |
1fe5dc43 | 9230 | |
33e30380 | 9231 | - (void) addSource:(NSDictionary *) source { |
25c1dafb | 9232 | CydiaAddSource(source); |
33e30380 | 9233 | } |
7b0ce2da | 9234 | |
33e30380 | 9235 | - (void) addSource:(NSString *)href withDistribution:(NSString *)distribution andSections:(NSArray *)sections { |
25c1dafb | 9236 | CydiaAddSource(href, distribution, sections); |
7b0ce2da JF |
9237 | } |
9238 | ||
8a2d167a JF |
9239 | // XXX: this method should not return anything |
9240 | - (BOOL) addTrivialSource:(NSString *)href { | |
25c1dafb | 9241 | CydiaAddSource(href, @"./"); |
8a2d167a | 9242 | return YES; |
93460555 JF |
9243 | } |
9244 | ||
b4d89997 JF |
9245 | - (void) resolve { |
9246 | pkgProblemResolver *resolver = [database_ resolver]; | |
9247 | ||
9248 | resolver->InstallProtect(); | |
9249 | if (!resolver->Resolve(true)) | |
9250 | _error->Discard(); | |
2367a917 JF |
9251 | } |
9252 | ||
670a0494 | 9253 | - (bool) perform { |
d6c371f5 JF |
9254 | // XXX: this is a really crappy way of doing this. |
9255 | // like, seriously: this state machine is still broken, and cancelling this here doesn't really /fix/ that. | |
9256 | // for one, the user can still /start/ a reloading data event while they have a queue, which is stupid | |
9257 | // for two, this just means there is a race condition between the refresh completing and the confirmation controller appearing. | |
9258 | if ([tabbar_ updating]) | |
9259 | [tabbar_ cancelUpdate]; | |
9260 | ||
670a0494 JF |
9261 | if (![database_ prepare]) |
9262 | return false; | |
49048579 | 9263 | |
adb61bda | 9264 | ConfirmationController *page([[[ConfirmationController alloc] initWithDatabase:database_] autorelease]); |
affeffc7 | 9265 | [page setDelegate:self]; |
15f0d613 | 9266 | UINavigationController *confirm_([[[UINavigationController alloc] initWithRootViewController:page] autorelease]); |
49048579 | 9267 | |
36fbb2aa JF |
9268 | if (IsWildcat_) |
9269 | [confirm_ setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 9270 | [tabbar_ presentModalViewController:confirm_ animated:YES]; |
670a0494 JF |
9271 | |
9272 | return true; | |
3178d79b JF |
9273 | } |
9274 | ||
dc63e78f JF |
9275 | - (void) queue { |
9276 | @synchronized (self) { | |
9277 | [self perform]; | |
9278 | } | |
9279 | } | |
9280 | ||
9281 | - (void) clearPackage:(Package *)package { | |
9282 | @synchronized (self) { | |
9283 | [package clear]; | |
9284 | [self resolve]; | |
9285 | [self perform]; | |
9286 | } | |
9287 | } | |
9288 | ||
77801ff1 JF |
9289 | - (void) installPackages:(NSArray *)packages { |
9290 | @synchronized (self) { | |
9291 | for (Package *package in packages) | |
9292 | [package install]; | |
9293 | [self resolve]; | |
9294 | [self perform]; | |
9295 | } | |
9296 | } | |
9297 | ||
36bb2ca2 JF |
9298 | - (void) installPackage:(Package *)package { |
9299 | @synchronized (self) { | |
9300 | [package install]; | |
9301 | [self resolve]; | |
9302 | [self perform]; | |
9303 | } | |
9304 | } | |
9305 | ||
9306 | - (void) removePackage:(Package *)package { | |
9307 | @synchronized (self) { | |
9308 | [package remove]; | |
9309 | [self resolve]; | |
9310 | [self perform]; | |
9311 | } | |
9312 | } | |
9313 | ||
9314 | - (void) distUpgrade { | |
9315 | @synchronized (self) { | |
670a0494 JF |
9316 | if (![database_ upgrade]) |
9317 | return; | |
36bb2ca2 JF |
9318 | [self perform]; |
9319 | } | |
b4d89997 JF |
9320 | } |
9321 | ||
780cdb3b JF |
9322 | - (void) _uicache { |
9323 | _trace(); | |
5685d514 | 9324 | system("/usr/bin/uicache"); |
780cdb3b JF |
9325 | _trace(); |
9326 | } | |
9327 | ||
9328 | - (void) uicache { | |
9329 | UIProgressHUD *hud([self addProgressHUD]); | |
9330 | [hud setText:UCLocalize("LOADING")]; | |
9331 | [self yieldToSelector:@selector(_uicache)]; | |
9332 | [self removeProgressHUD:hud]; | |
9333 | } | |
9334 | ||
fca2f596 JF |
9335 | - (void) perform_ { |
9336 | [database_ perform]; | |
9337 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; | |
780cdb3b | 9338 | [self performSelectorOnMainThread:@selector(uicache) withObject:nil waitUntilDone:YES]; |
fca2f596 JF |
9339 | } |
9340 | ||
b5e7eebb | 9341 | - (void) confirmWithNavigationController:(UINavigationController *)navigation { |
f6371a33 | 9342 | Queuing_ = false; |
2ef6faad | 9343 | [self lockSuspend]; |
fca2f596 | 9344 | [self detachNewProgressSelector:@selector(perform_) toTarget:self forController:navigation title:@"RUNNING"]; |
2ef6faad | 9345 | [self unlockSuspend]; |
dc5812ec JF |
9346 | } |
9347 | ||
54043703 JF |
9348 | - (void) retainNetworkActivityIndicator { |
9349 | if (activity_++ == 0) | |
9350 | [self setNetworkActivityIndicatorVisible:YES]; | |
848ed88b JF |
9351 | |
9352 | #if TraceLogging | |
9353 | NSLog(@"retainNetworkActivityIndicator->%d", activity_); | |
9354 | #endif | |
54043703 JF |
9355 | } |
9356 | ||
9357 | - (void) releaseNetworkActivityIndicator { | |
9358 | if (--activity_ == 0) | |
9359 | [self setNetworkActivityIndicatorVisible:NO]; | |
848ed88b JF |
9360 | |
9361 | #if TraceLogging | |
9362 | NSLog(@"releaseNetworkActivityIndicator->%d", activity_); | |
9363 | #endif | |
9364 | ||
54043703 JF |
9365 | } |
9366 | ||
674dce72 GP |
9367 | - (void) cancelAndClear:(bool)clear { |
9368 | @synchronized (self) { | |
9369 | if (clear) { | |
c6ca67ba | 9370 | [database_ clear]; |
b5e7eebb | 9371 | Queuing_ = false; |
674dce72 | 9372 | } else { |
b5e7eebb | 9373 | Queuing_ = true; |
6067f1b8 JF |
9374 | } |
9375 | ||
a4217bbb | 9376 | [self _updateData]; |
674dce72 | 9377 | } |
37d2b2a9 | 9378 | } |
7b0ce2da | 9379 | |
b5e7eebb GP |
9380 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
9381 | NSString *context([alert context]); | |
bc11cf5b | 9382 | |
6915b806 JF |
9383 | if ([context isEqualToString:@"conffile"]) { |
9384 | FILE *input = [database_ input]; | |
9385 | if (button == [alert cancelButtonIndex]) | |
9386 | fprintf(input, "N\n"); | |
9387 | else if (button == [alert firstOtherButtonIndex]) | |
9388 | fprintf(input, "Y\n"); | |
9389 | fflush(input); | |
9390 | ||
9391 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
9392 | } else if ([context isEqualToString:@"fixhalf"]) { | |
efa53fa9 | 9393 | if (button == [alert cancelButtonIndex]) { |
37d2b2a9 | 9394 | @synchronized (self) { |
584daea0 | 9395 | for (Package *broken in (id) broken_) { |
37d2b2a9 | 9396 | [broken remove]; |
d8b0f6e3 | 9397 | NSString *id(ShellEscape([broken id])); |
7cf0c7b3 JF |
9398 | system([[NSString stringWithFormat:@"/usr/libexec/cydia/cydo /bin/rm -f" |
9399 | " /var/lib/dpkg/info/%@.prerm" | |
9400 | " /var/lib/dpkg/info/%@.postrm" | |
9401 | " /var/lib/dpkg/info/%@.preinst" | |
9402 | " /var/lib/dpkg/info/%@.postinst" | |
9403 | " /var/lib/dpkg/info/%@.extrainst_" | |
9404 | "", id, id, id, id, id] UTF8String]); | |
37d2b2a9 | 9405 | } |
7b0ce2da | 9406 | |
37d2b2a9 GP |
9407 | [self resolve]; |
9408 | [self perform]; | |
9409 | } | |
efa53fa9 | 9410 | } else if (button == [alert firstOtherButtonIndex]) { |
37d2b2a9 GP |
9411 | [broken_ removeAllObjects]; |
9412 | [self _loaded]; | |
7b0ce2da JF |
9413 | } |
9414 | ||
37d2b2a9 | 9415 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9416 | } else if ([context isEqualToString:@"upgrade"]) { |
37d2b2a9 GP |
9417 | if (button == [alert firstOtherButtonIndex]) { |
9418 | @synchronized (self) { | |
7b33d201 | 9419 | for (Package *essential in (id) essential_) |
37d2b2a9 | 9420 | [essential install]; |
7b0ce2da | 9421 | |
37d2b2a9 GP |
9422 | [self resolve]; |
9423 | [self perform]; | |
9424 | } | |
9425 | } else if (button == [alert firstOtherButtonIndex] + 1) { | |
9426 | [self distUpgrade]; | |
9427 | } else if (button == [alert cancelButtonIndex]) { | |
9428 | Ignored_ = YES; | |
7b0ce2da JF |
9429 | } |
9430 | ||
37d2b2a9 | 9431 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 9432 | } |
7b0ce2da JF |
9433 | } |
9434 | ||
d13edf44 JF |
9435 | - (void) system:(NSString *)command { |
9436 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9437 | ||
985d2dff | 9438 | _trace(); |
0c0a966b | 9439 | system([command UTF8String]); |
985d2dff | 9440 | _trace(); |
d13edf44 JF |
9441 | |
9442 | [pool release]; | |
670a0494 JF |
9443 | } |
9444 | ||
9445 | - (void) applicationWillSuspend { | |
9446 | [database_ clean]; | |
9447 | [super applicationWillSuspend]; | |
bd150f54 JF |
9448 | } |
9449 | ||
235f5487 | 9450 | - (BOOL) isSafeToSuspend { |
5927fe03 JF |
9451 | if (locked_ != 0) { |
9452 | #if !ForRelease | |
9453 | NSLog(@"isSafeToSuspend: locked_ != 0"); | |
9454 | #endif | |
9455 | return false; | |
9456 | } | |
9457 | ||
7187b5b2 JF |
9458 | if ([tabbar_ modalViewController] != nil) |
9459 | return false; | |
9460 | ||
262afe11 GP |
9461 | // Use external process status API internally. |
9462 | // This is probably a really bad idea. | |
235f5487 | 9463 | // XXX: what is the point of this? does this solve anything at all? |
262afe11 GP |
9464 | uint64_t status = 0; |
9465 | int notify_token; | |
9466 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
9467 | notify_get_state(notify_token, &status); | |
9468 | notify_cancel(notify_token); | |
9469 | } | |
9470 | ||
5927fe03 JF |
9471 | if (status != 0) { |
9472 | #if !ForRelease | |
9473 | NSLog(@"isSafeToSuspend: status != 0"); | |
9474 | #endif | |
9475 | return false; | |
9476 | } | |
9477 | ||
9478 | #if !ForRelease | |
9479 | NSLog(@"isSafeToSuspend: -> true"); | |
9480 | #endif | |
9481 | return true; | |
235f5487 JF |
9482 | } |
9483 | ||
216f3dc6 JF |
9484 | - (void) suspendReturningToLastApp:(BOOL)returning { |
9485 | if ([self isSafeToSuspend]) | |
9486 | [super suspendReturningToLastApp:returning]; | |
9487 | } | |
9488 | ||
9489 | - (void) suspend { | |
9490 | if ([self isSafeToSuspend]) | |
9491 | [super suspend]; | |
9492 | } | |
9493 | ||
9494 | - (void) applicationSuspend { | |
9495 | if ([self isSafeToSuspend]) | |
9496 | [super applicationSuspend]; | |
9497 | } | |
9498 | ||
235f5487 JF |
9499 | - (void) applicationSuspend:(__GSEvent *)event { |
9500 | if ([self isSafeToSuspend]) | |
bd150f54 | 9501 | [super applicationSuspend:event]; |
bd150f54 JF |
9502 | } |
9503 | ||
6d9712c4 | 9504 | - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 { |
235f5487 | 9505 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9506 | [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3]; |
9507 | } | |
9508 | ||
9509 | - (void) _setSuspended:(BOOL)value { | |
235f5487 | 9510 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9511 | [super _setSuspended:value]; |
9512 | } | |
9513 | ||
7b0ce2da | 9514 | - (UIProgressHUD *) addProgressHUD { |
8c9453da | 9515 | UIProgressHUD *hud([[[UIProgressHUD alloc] init] autorelease]); |
04fe1349 JF |
9516 | [hud setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
9517 | ||
d061f4ba | 9518 | [window_ setUserInteractionEnabled:NO]; |
534e31fc | 9519 | |
f36e5eac JF |
9520 | UIViewController *target(tabbar_); |
9521 | if (UIViewController *modal = [target modalViewController]) | |
9522 | target = modal; | |
9523 | ||
22b6c4b8 | 9524 | [hud showInView:[target view]]; |
534e31fc | 9525 | |
2ef6faad | 9526 | [self lockSuspend]; |
7b0ce2da JF |
9527 | return hud; |
9528 | } | |
9529 | ||
d061f4ba | 9530 | - (void) removeProgressHUD:(UIProgressHUD *)hud { |
2ef6faad | 9531 | [self unlockSuspend]; |
8c9453da | 9532 | [hud hide]; |
d061f4ba JF |
9533 | [hud removeFromSuperview]; |
9534 | [window_ setUserInteractionEnabled:YES]; | |
9535 | } | |
9536 | ||
f050e4d9 JF |
9537 | - (CyteViewController *) pageForPackage:(NSString *)name withReferrer:(NSString *)referrer { |
9538 | return [[[CYPackageController alloc] initWithDatabase:database_ forPackage:name withReferrer:referrer] autorelease]; | |
c390d3ab JF |
9539 | } |
9540 | ||
f050e4d9 | 9541 | - (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external withReferrer:(NSString *)referrer { |
e47c4742 | 9542 | NSString *scheme([[url scheme] lowercaseString]); |
f6e13561 | 9543 | if ([[url absoluteString] length] <= [scheme length] + 3) |
e47c4742 | 9544 | return nil; |
f6e13561 | 9545 | NSString *path([[url absoluteString] substringFromIndex:[scheme length] + 3]); |
3a159223 | 9546 | NSArray *components([path componentsSeparatedByString:@"/"]); |
f6e13561 | 9547 | |
4fd0c466 | 9548 | if ([scheme isEqualToString:@"apptapp"] && [components count] > 0 && [[components objectAtIndex:0] isEqualToString:@"package"]) { |
f050e4d9 | 9549 | CyteViewController *controller([self pageForPackage:[components objectAtIndex:1] withReferrer:referrer]); |
4fd0c466 JF |
9550 | if (controller != nil) |
9551 | [controller setDelegate:self]; | |
9552 | return controller; | |
9553 | } | |
f6e13561 GP |
9554 | |
9555 | if ([components count] < 1 || ![scheme isEqualToString:@"cydia"]) | |
e47c4742 | 9556 | return nil; |
f6e13561 GP |
9557 | |
9558 | NSString *base([components objectAtIndex:0]); | |
9559 | ||
cd79e8cf | 9560 | CyteViewController *controller = nil; |
f5a17517 | 9561 | |
f70ea899 | 9562 | if ([base isEqualToString:@"url"]) { |
106d645f GP |
9563 | // This kind of URL can contain slashes in the argument, so we can't parse them below. |
9564 | NSString *destination = [[url absoluteString] substringFromIndex:([scheme length] + [@"://" length] + [base length] + [@"/" length])]; | |
a576488f | 9565 | controller = [[[CydiaWebViewController alloc] initWithURL:[NSURL URLWithString:destination]] autorelease]; |
028dbd1c | 9566 | } else if (!external && [components count] == 1) { |
f6e13561 | 9567 | if ([base isEqualToString:@"sources"]) { |
f5a17517 | 9568 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9569 | } |
9570 | ||
9571 | if ([base isEqualToString:@"home"]) { | |
f5a17517 | 9572 | controller = [[[HomeController alloc] init] autorelease]; |
f6e13561 GP |
9573 | } |
9574 | ||
9575 | if ([base isEqualToString:@"sections"]) { | |
55066b9e | 9576 | controller = [[[SectionsController alloc] initWithDatabase:database_ source:nil] autorelease]; |
f6e13561 GP |
9577 | } |
9578 | ||
9579 | if ([base isEqualToString:@"search"]) { | |
43625891 | 9580 | controller = [[[SearchController alloc] initWithDatabase:database_ query:nil] autorelease]; |
f6e13561 GP |
9581 | } |
9582 | ||
9583 | if ([base isEqualToString:@"changes"]) { | |
ea3bb538 | 9584 | controller = [[[ChangesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9585 | } |
9586 | ||
9587 | if ([base isEqualToString:@"installed"]) { | |
f5a17517 | 9588 | controller = [[[InstalledController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9589 | } |
9590 | } else if ([components count] == 2) { | |
8912cff7 | 9591 | NSString *argument = [[components objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
f6e13561 GP |
9592 | |
9593 | if ([base isEqualToString:@"package"]) { | |
f050e4d9 | 9594 | controller = [self pageForPackage:argument withReferrer:referrer]; |
f6e13561 GP |
9595 | } |
9596 | ||
028dbd1c | 9597 | if (!external && [base isEqualToString:@"search"]) { |
8912cff7 | 9598 | controller = [[[SearchController alloc] initWithDatabase:database_ query:argument] autorelease]; |
f6e13561 GP |
9599 | } |
9600 | ||
028dbd1c | 9601 | if (!external && [base isEqualToString:@"sections"]) { |
55066b9e | 9602 | if ([argument isEqualToString:@"all"] || [argument isEqualToString:@"*"]) |
f6e13561 | 9603 | argument = nil; |
55066b9e | 9604 | controller = [[[SectionController alloc] initWithDatabase:database_ source:nil section:argument] autorelease]; |
f6e13561 GP |
9605 | } |
9606 | ||
75d2e426 | 9607 | if ([base isEqualToString:@"sources"]) { |
f6e13561 | 9608 | if ([argument isEqualToString:@"add"]) { |
f5a17517 GP |
9609 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
9610 | [(SourcesController *)controller showAddSourcePrompt]; | |
f6e13561 | 9611 | } else { |
8912cff7 | 9612 | Source *source([database_ sourceWithKey:argument]); |
55066b9e | 9613 | controller = [[[SectionsController alloc] initWithDatabase:database_ source:source] autorelease]; |
f6e13561 GP |
9614 | } |
9615 | } | |
9616 | ||
028dbd1c | 9617 | if (!external && [base isEqualToString:@"launch"]) { |
f6e13561 | 9618 | [self launchApplicationWithIdentifier:argument suspended:NO]; |
f5a17517 | 9619 | return nil; |
f6e13561 | 9620 | } |
028dbd1c | 9621 | } else if (!external && [components count] == 3) { |
8912cff7 JF |
9622 | NSString *arg1 = [[components objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
9623 | NSString *arg2 = [[components objectAtIndex:2] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
f6e13561 GP |
9624 | |
9625 | if ([base isEqualToString:@"package"]) { | |
9626 | if ([arg2 isEqualToString:@"settings"]) { | |
f5a17517 | 9627 | controller = [[[PackageSettingsController alloc] initWithDatabase:database_ package:arg1] autorelease]; |
f6e13561 GP |
9628 | } else if ([arg2 isEqualToString:@"files"]) { |
9629 | if (Package *package = [database_ packageWithName:arg1]) { | |
f5a17517 GP |
9630 | controller = [[[FileTable alloc] initWithDatabase:database_] autorelease]; |
9631 | [(FileTable *)controller setPackage:package]; | |
f6e13561 GP |
9632 | } |
9633 | } | |
c390d3ab | 9634 | } |
55066b9e JF |
9635 | |
9636 | if ([base isEqualToString:@"sections"]) { | |
9637 | Source *source([arg1 isEqualToString:@"*"] ? nil : [database_ sourceWithKey:arg1]); | |
9638 | NSString *section([arg2 isEqualToString:@"*"] ? nil : arg2); | |
9639 | controller = [[[SectionController alloc] initWithDatabase:database_ source:source section:section] autorelease]; | |
9640 | } | |
c390d3ab JF |
9641 | } |
9642 | ||
f5a17517 GP |
9643 | [controller setDelegate:self]; |
9644 | return controller; | |
c390d3ab JF |
9645 | } |
9646 | ||
028dbd1c | 9647 | - (BOOL) openCydiaURL:(NSURL *)url forExternal:(BOOL)external { |
f050e4d9 | 9648 | CyteViewController *page([self pageForURL:url forExternal:external withReferrer:nil]); |
40364973 | 9649 | |
e965092a JF |
9650 | if (page != nil) |
9651 | [tabbar_ setUnselectedViewController:page]; | |
40364973 | 9652 | |
f6e13561 | 9653 | return page != nil; |
40364973 GP |
9654 | } |
9655 | ||
c390d3ab JF |
9656 | - (void) applicationOpenURL:(NSURL *)url { |
9657 | [super applicationOpenURL:url]; | |
d817e4de | 9658 | |
7b33d201 JF |
9659 | if (!loaded_) |
9660 | starturl_ = url; | |
9661 | else | |
9662 | [self openCydiaURL:url forExternal:YES]; | |
c390d3ab JF |
9663 | } |
9664 | ||
bc11cf5b | 9665 | - (void) applicationWillResignActive:(UIApplication *)application { |
7eff7ea6 | 9666 | // Stop refreshing if you get a phone call or lock the device. |
7585ce66 JF |
9667 | if ([tabbar_ updating]) |
9668 | [tabbar_ cancelUpdate]; | |
bc11cf5b | 9669 | |
ca584c15 GP |
9670 | if ([[self superclass] instancesRespondToSelector:@selector(applicationWillResignActive:)]) |
9671 | [super applicationWillResignActive:application]; | |
7eff7ea6 GP |
9672 | } |
9673 | ||
9dd3045d | 9674 | - (void) saveState { |
3d1e6f42 JF |
9675 | [[NSDictionary dictionaryWithObjectsAndKeys: |
9676 | @"InterfaceState", [tabbar_ navigationURLCollection], | |
9677 | @"LastClosed", [NSDate date], | |
9678 | @"InterfaceIndex", [NSNumber numberWithInt:[tabbar_ selectedIndex]], | |
9679 | nil] writeToFile:@ SavedState_ atomically:YES]; | |
fe8e721f GP |
9680 | |
9681 | [self _saveConfig]; | |
9682 | } | |
9683 | ||
9dd3045d JF |
9684 | - (void) applicationWillTerminate:(UIApplication *)application { |
9685 | [self saveState]; | |
9686 | } | |
9687 | ||
d4011d57 | 9688 | - (void) applicationDidEnterBackground:(UIApplication *)application { |
2acc4fa4 JF |
9689 | if (kCFCoreFoundationVersionNumber < 1000 && [self isSafeToSuspend]) |
9690 | return [self terminateWithSuccess]; | |
3d1e6f42 | 9691 | Backgrounded_ = [NSDate date]; |
d4011d57 JF |
9692 | [self saveState]; |
9693 | } | |
9694 | ||
9695 | - (void) applicationWillEnterForeground:(UIApplication *)application { | |
3d1e6f42 | 9696 | if (Backgrounded_ == nil) |
95cd61f0 JF |
9697 | return; |
9698 | ||
3d1e6f42 | 9699 | NSTimeInterval interval([Backgrounded_ timeIntervalSinceNow]); |
95cd61f0 | 9700 | |
6218c58c | 9701 | if (interval <= -(30*60)) { |
888667d5 JF |
9702 | [tabbar_ setSelectedIndex:0]; |
9703 | [[[tabbar_ viewControllers] objectAtIndex:0] popToRootViewControllerAnimated:NO]; | |
9704 | } | |
95cd61f0 | 9705 | |
6218c58c | 9706 | if (interval <= -(15*60)) { |
888667d5 JF |
9707 | if (IsReachable("cydia.saurik.com")) { |
9708 | [tabbar_ beginUpdate]; | |
9709 | [appcache_ reloadURLWithCache:YES]; | |
9710 | } | |
9711 | } | |
f26c90b1 JF |
9712 | |
9713 | if ([database_ delocked]) | |
9714 | [self reloadData]; | |
d4011d57 JF |
9715 | } |
9716 | ||
6915b806 | 9717 | - (void) setConfigurationData:(NSString *)data { |
b4fd1197 | 9718 | static RegEx conffile_r("'(.*)' '(.*)' ([01]) ([01])"); |
6915b806 JF |
9719 | |
9720 | if (!conffile_r(data)) { | |
9721 | lprintf("E:invalid conffile\n"); | |
9722 | return; | |
9723 | } | |
9724 | ||
9725 | NSString *ofile = conffile_r[1]; | |
9726 | //NSString *nfile = conffile_r[2]; | |
9727 | ||
9728 | UIAlertView *alert = [[[UIAlertView alloc] | |
9729 | initWithTitle:UCLocalize("CONFIGURATION_UPGRADE") | |
9730 | message:[NSString stringWithFormat:@"%@\n\n%@", UCLocalize("CONFIGURATION_UPGRADE_EX"), ofile] | |
9731 | delegate:self | |
9732 | cancelButtonTitle:UCLocalize("KEEP_OLD_COPY") | |
9733 | otherButtonTitles: | |
9734 | UCLocalize("ACCEPT_NEW_COPY"), | |
9735 | // XXX: UCLocalize("SEE_WHAT_CHANGED"), | |
9736 | nil | |
9737 | ] autorelease]; | |
9738 | ||
9739 | [alert setContext:@"conffile"]; | |
a08145a8 | 9740 | [alert setNumberOfRows:2]; |
6915b806 JF |
9741 | [alert show]; |
9742 | } | |
9743 | ||
f3e11d24 | 9744 | - (void) addStashController { |
2ef6faad | 9745 | [self lockSuspend]; |
7b33d201 | 9746 | stash_ = [[[StashController alloc] init] autorelease]; |
f3e11d24 GP |
9747 | [window_ addSubview:[stash_ view]]; |
9748 | } | |
9749 | ||
9750 | - (void) removeStashController { | |
9751 | [[stash_ view] removeFromSuperview]; | |
7b33d201 | 9752 | stash_ = nil; |
2ef6faad | 9753 | [self unlockSuspend]; |
f3e11d24 GP |
9754 | } |
9755 | ||
9756 | - (void) stash { | |
9e1f1e91 | 9757 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; |
f3e11d24 | 9758 | UpdateExternalStatus(1); |
0c0a966b | 9759 | [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/cydo /usr/libexec/cydia/free.sh"]; |
f3e11d24 | 9760 | UpdateExternalStatus(0); |
f3e11d24 GP |
9761 | |
9762 | [self removeStashController]; | |
42bbf27d | 9763 | [self reloadSpringBoard]; |
f3e11d24 GP |
9764 | } |
9765 | ||
f6e13561 | 9766 | - (void) setupViewControllers { |
5fe2bcc6 | 9767 | tabbar_ = [[[CydiaTabBarController alloc] initWithDatabase:database_] autorelease]; |
851f4a99 | 9768 | |
6445279b JF |
9769 | NSMutableArray *items; |
9770 | if (kCFCoreFoundationVersionNumber < 800) { | |
9771 | items = [NSMutableArray arrayWithObjects: | |
8dbf3abc JF |
9772 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage imageNamed:@"home.png"] tag:0] autorelease], |
9773 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage imageNamed:@"install.png"] tag:0] autorelease], | |
9774 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage imageNamed:@"changes.png"] tag:0] autorelease], | |
9775 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage imageNamed:@"manage.png"] tag:0] autorelease], | |
9776 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage imageNamed:@"search.png"] tag:0] autorelease], | |
6445279b | 9777 | nil]; |
851f4a99 | 9778 | } else { |
6445279b | 9779 | items = [NSMutableArray arrayWithObjects: |
8dbf3abc JF |
9780 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage imageNamed:@"home7.png"] selectedImage:[UIImage imageNamed:@"home7s.png"]] autorelease], |
9781 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage imageNamed:@"install7.png"] selectedImage:[UIImage imageNamed:@"install7s.png"]] autorelease], | |
9782 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage imageNamed:@"changes7.png"] selectedImage:[UIImage imageNamed:@"changes7s.png"]] autorelease], | |
9783 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage imageNamed:@"manage7.png"] selectedImage:[UIImage imageNamed:@"manage7s.png"]] autorelease], | |
9784 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage imageNamed:@"search7.png"] selectedImage:[UIImage imageNamed:@"search7s.png"]] autorelease], | |
6445279b | 9785 | nil]; |
851f4a99 GP |
9786 | } |
9787 | ||
9788 | NSMutableArray *controllers([NSMutableArray array]); | |
851f4a99 | 9789 | for (UITabBarItem *item in items) { |
15f0d613 | 9790 | UINavigationController *controller([[[UINavigationController alloc] init] autorelease]); |
851f4a99 GP |
9791 | [controller setTabBarItem:item]; |
9792 | [controllers addObject:controller]; | |
9793 | } | |
851f4a99 | 9794 | [tabbar_ setViewControllers:controllers]; |
4305896c | 9795 | |
f6e13561 | 9796 | [tabbar_ setUpdateDelegate:self]; |
851f4a99 GP |
9797 | } |
9798 | ||
968afbcd | 9799 | - (void) _sendMemoryWarningNotification { |
85106ebe JF |
9800 | if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: maybe 4_0? |
9801 | [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification" object:[UIApplication sharedApplication]]; | |
9802 | else | |
9803 | [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidReceiveMemoryWarningNotification" object:[UIApplication sharedApplication]]; | |
968afbcd JF |
9804 | } |
9805 | ||
9806 | - (void) _sendMemoryWarningNotifications { | |
9807 | while (true) { | |
9808 | [self performSelectorOnMainThread:@selector(_sendMemoryWarningNotification) withObject:nil waitUntilDone:NO]; | |
740f9f75 JF |
9809 | sleep(2); |
9810 | //usleep(2000000); | |
968afbcd JF |
9811 | } |
9812 | } | |
9813 | ||
9256a1ce JF |
9814 | - (void) applicationDidReceiveMemoryWarning:(UIApplication *)application { |
9815 | NSLog(@"--"); | |
9816 | [[NSURLCache sharedURLCache] removeAllCachedResponses]; | |
9817 | } | |
9818 | ||
bd150f54 | 9819 | - (void) applicationDidFinishLaunching:(id)unused { |
968afbcd JF |
9820 | //[NSThread detachNewThreadSelector:@selector(_sendMemoryWarningNotifications) toTarget:self withObject:nil]; |
9821 | ||
7e30ba6d | 9822 | _trace(); |
1e94d48b JF |
9823 | if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) |
9824 | [self setApplicationSupportsShakeToEdit:NO]; | |
9825 | ||
48f1762f JF |
9826 | @synchronized (HostConfig_) { |
9827 | [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]]; | |
9828 | } | |
3171f7fe | 9829 | |
4058b165 JF |
9830 | [NSURLCache setSharedURLCache:[[[CYURLCache alloc] |
9831 | initWithMemoryCapacity:524288 | |
9832 | diskCapacity:10485760 | |
b44af625 | 9833 | diskPath:Cache("SDURLCache") |
4058b165 | 9834 | ] autorelease]]; |
71cc7be1 | 9835 | |
a576488f | 9836 | [CydiaWebViewController _initialize]; |
ea173384 | 9837 | |
bfc87a4d JF |
9838 | [NSURLProtocol registerClass:[CydiaURLProtocol class]]; |
9839 | ||
793aee35 JF |
9840 | // this would disallow http{,s} URLs from accessing this data |
9841 | //[WebView registerURLSchemeAsLocal:@"cydia"]; | |
9842 | ||
7b33d201 JF |
9843 | Font12_ = [UIFont systemFontOfSize:12]; |
9844 | Font12Bold_ = [UIFont boldSystemFontOfSize:12]; | |
9845 | Font14_ = [UIFont systemFontOfSize:14]; | |
2cdc6e57 | 9846 | Font18_ = [UIFont systemFontOfSize:18]; |
7b33d201 JF |
9847 | Font18Bold_ = [UIFont boldSystemFontOfSize:18]; |
9848 | Font22Bold_ = [UIFont boldSystemFontOfSize:22]; | |
f641a0e5 | 9849 | |
7b33d201 JF |
9850 | essential_ = [NSMutableArray arrayWithCapacity:4]; |
9851 | broken_ = [NSMutableArray arrayWithCapacity:4]; | |
bd150f54 | 9852 | |
4e89e880 | 9853 | // XXX: I really need this thing... like, seriously... I'm sorry |
fedd38fe JF |
9854 | appcache_ = [[[AppCacheController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/appcache/", UI_]]] autorelease]; |
9855 | [appcache_ reloadData]; | |
4e89e880 | 9856 | |
7b33d201 | 9857 | window_ = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
f641a0e5 JF |
9858 | [window_ orderFront:self]; |
9859 | [window_ makeKey:self]; | |
c390d3ab | 9860 | [window_ setHidden:NO]; |
04fe1349 | 9861 | |
96ed699d | 9862 | if (false) stash: { |
f3e11d24 | 9863 | [self addStashController]; |
3931b718 JF |
9864 | // XXX: this would be much cleaner as a yieldToSelector: |
9865 | // that way the removeStashController could happen right here inline | |
9866 | // we also could no longer require the useless stash_ field anymore | |
f3e11d24 GP |
9867 | [self performSelector:@selector(stash) withObject:nil afterDelay:0]; |
9868 | return; | |
9869 | } | |
9870 | ||
2656fd54 JF |
9871 | struct stat root; |
9872 | int error(stat("/", &root)); | |
9873 | _assert(error != -1); | |
9874 | ||
96ed699d | 9875 | #define Stash_(path) do { \ |
2656fd54 JF |
9876 | struct stat folder; \ |
9877 | int error(lstat((path), &folder)); \ | |
9878 | if (error != -1 && ( \ | |
9879 | folder.st_dev == root.st_dev && \ | |
9880 | S_ISDIR(folder.st_mode) \ | |
9881 | ) || error == -1 && ( \ | |
9882 | errno == ENOENT || \ | |
9883 | errno == ENOTDIR \ | |
9884 | )) goto stash; \ | |
96ed699d JF |
9885 | } while (false) |
9886 | ||
40bb5513 | 9887 | Stash_("/Applications"); |
96ed699d JF |
9888 | Stash_("/Library/Ringtones"); |
9889 | Stash_("/Library/Wallpaper"); | |
9890 | //Stash_("/usr/bin"); | |
9891 | Stash_("/usr/include"); | |
96ed699d JF |
9892 | Stash_("/usr/share"); |
9893 | //Stash_("/var/lib"); | |
9894 | ||
770f2a8e | 9895 | database_ = [Database sharedInstance]; |
6915b806 | 9896 | [database_ setDelegate:self]; |
bfc87a4d | 9897 | |
89571a5b | 9898 | [window_ setUserInteractionEnabled:NO]; |
0be165c8 | 9899 | [self setupViewControllers]; |
6915b806 | 9900 | |
9549563e JF |
9901 | CydiaLoadingViewController *loading([[[CydiaLoadingViewController alloc] init] autorelease]); |
9902 | UINavigationController *navigation([[[UINavigationController alloc] init] autorelease]); | |
9903 | [navigation setViewControllers:[NSArray arrayWithObject:loading]]; | |
9904 | ||
9905 | emulated_ = [[[CyteTabBarController alloc] init] autorelease]; | |
9906 | [emulated_ setViewControllers:[NSArray arrayWithObject:navigation]]; | |
9907 | [emulated_ setSelectedIndex:0]; | |
1c28763e JF |
9908 | |
9909 | if ([emulated_ respondsToSelector:@selector(concealTabBarSelection)]) | |
9910 | [emulated_ concealTabBarSelection]; | |
9549563e | 9911 | |
81628115 JF |
9912 | if ([window_ respondsToSelector:@selector(setRootViewController:)]) |
9913 | [window_ setRootViewController:emulated_]; | |
237c3d1c JF |
9914 | else |
9915 | [window_ addSubview:[emulated_ view]]; | |
5ccb47d8 | 9916 | |
fed0d010 | 9917 | [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; |
c626a63f | 9918 | _trace(); |
fed0d010 JF |
9919 | } |
9920 | ||
d3a28a81 GP |
9921 | - (NSArray *) defaultStartPages { |
9922 | NSMutableArray *standard = [NSMutableArray array]; | |
9923 | [standard addObject:[NSArray arrayWithObject:@"cydia://home"]]; | |
55066b9e | 9924 | [standard addObject:[NSArray arrayWithObject:@"cydia://sources"]]; |
d3a28a81 | 9925 | [standard addObject:[NSArray arrayWithObject:@"cydia://changes"]]; |
55066b9e | 9926 | [standard addObject:[NSArray arrayWithObject:@"cydia://installed"]]; |
d3a28a81 GP |
9927 | [standard addObject:[NSArray arrayWithObject:@"cydia://search"]]; |
9928 | return standard; | |
9929 | } | |
9930 | ||
fed0d010 | 9931 | - (void) loadData { |
c626a63f | 9932 | _trace(); |
4121c5e0 JF |
9933 | if ([emulated_ modalViewController] != nil) |
9934 | [emulated_ dismissModalViewControllerAnimated:YES]; | |
9935 | [window_ setUserInteractionEnabled:NO]; | |
fed0d010 | 9936 | |
2925cbba JF |
9937 | [self reloadDataWithInvocation:nil]; |
9938 | [self refreshIfPossible]; | |
2e9123cb | 9939 | [self disemulate]; |
5ccb47d8 | 9940 | |
3d1e6f42 JF |
9941 | NSDictionary *state([NSDictionary dictionaryWithContentsOfFile:@ SavedState_]); |
9942 | ||
9943 | int savedIndex = [[state objectForKey:@"InterfaceIndex"] intValue]; | |
9944 | NSArray *saved = [[[state objectForKey:@"InterfaceState"] mutableCopy] autorelease]; | |
d3a28a81 GP |
9945 | int standardIndex = 0; |
9946 | NSArray *standard = [self defaultStartPages]; | |
fe8e721f | 9947 | |
d3a28a81 GP |
9948 | BOOL valid = YES; |
9949 | ||
9950 | if (saved == nil) | |
9951 | valid = NO; | |
9952 | ||
3d1e6f42 | 9953 | NSDate *closed = [state objectForKey:@"LastClosed"]; |
d3a28a81 | 9954 | if (valid && closed != nil) { |
fe8e721f | 9955 | NSTimeInterval interval([closed timeIntervalSinceNow]); |
ade2267f | 9956 | if (interval <= -(30*60)) |
d3a28a81 | 9957 | valid = NO; |
fe8e721f GP |
9958 | } |
9959 | ||
d3a28a81 GP |
9960 | if (valid && [saved count] != [standard count]) |
9961 | valid = NO; | |
efa53fa9 | 9962 | |
d3a28a81 GP |
9963 | if (valid) { |
9964 | for (unsigned int i = 0; i < [standard count]; i++) { | |
9965 | NSArray *std = [standard objectAtIndex:i], *sav = [saved objectAtIndex:i]; | |
9966 | // XXX: The "hasPrefix" sanity check here could be, in theory, fooled, | |
9967 | // but it's good enough for now. | |
9968 | if ([sav count] == 0 || ![[sav objectAtIndex:0] hasPrefix:[std objectAtIndex:0]]) { | |
9969 | valid = NO; | |
9970 | break; | |
9971 | } | |
fe8e721f | 9972 | } |
fe8e721f GP |
9973 | } |
9974 | ||
d3a28a81 GP |
9975 | NSArray *items = nil; |
9976 | if (valid) { | |
9977 | [tabbar_ setSelectedIndex:savedIndex]; | |
9978 | items = saved; | |
9979 | } else { | |
9980 | [tabbar_ setSelectedIndex:standardIndex]; | |
9981 | items = standard; | |
9982 | } | |
9983 | ||
fe8e721f GP |
9984 | for (unsigned int tab = 0; tab < [[tabbar_ viewControllers] count]; tab++) { |
9985 | NSArray *stack = [items objectAtIndex:tab]; | |
15f0d613 | 9986 | UINavigationController *navigation = [[tabbar_ viewControllers] objectAtIndex:tab]; |
fe8e721f GP |
9987 | NSMutableArray *current = [NSMutableArray array]; |
9988 | ||
9989 | for (unsigned int nav = 0; nav < [stack count]; nav++) { | |
9990 | NSString *addr = [stack objectAtIndex:nav]; | |
9991 | NSURL *url = [NSURL URLWithString:addr]; | |
f050e4d9 | 9992 | CyteViewController *page = [self pageForURL:url forExternal:NO withReferrer:nil]; |
fe8e721f GP |
9993 | if (page != nil) |
9994 | [current addObject:page]; | |
9995 | } | |
9996 | ||
9997 | [navigation setViewControllers:current]; | |
9998 | } | |
f6e13561 | 9999 | |
89571a5b | 10000 | // (Try to) show the startup URL. |
f6e13561 | 10001 | if (starturl_ != nil) { |
f14bba69 | 10002 | [self openCydiaURL:starturl_ forExternal:YES]; |
f6e13561 GP |
10003 | starturl_ = nil; |
10004 | } | |
bd150f54 JF |
10005 | } |
10006 | ||
b5e7eebb | 10007 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { |
674dce72 | 10008 | if (item != nil && IsWildcat_) { |
b5e7eebb | 10009 | [sheet showFromBarButtonItem:item animated:YES]; |
674dce72 GP |
10010 | } else { |
10011 | [sheet showInView:window_]; | |
10012 | } | |
36bb2ca2 JF |
10013 | } |
10014 | ||
6915b806 JF |
10015 | - (void) addProgressEvent:(CydiaProgressEvent *)event forTask:(NSString *)task { |
10016 | id<ProgressDelegate> progress([database_ progressDelegate] ?: [self invokeNewProgress:nil forController:nil withTitle:task]); | |
10017 | [progress setTitle:task]; | |
10018 | [progress addProgressEvent:event]; | |
10019 | } | |
10020 | ||
10021 | - (void) addProgressEventForTask:(NSArray *)data { | |
10022 | CydiaProgressEvent *event([data objectAtIndex:0]); | |
10023 | NSString *task([data count] < 2 ? nil : [data objectAtIndex:1]); | |
10024 | [self addProgressEvent:event forTask:task]; | |
10025 | } | |
10026 | ||
10027 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task { | |
10028 | [self performSelectorOnMainThread:@selector(addProgressEventForTask:) withObject:[NSArray arrayWithObjects:event, task, nil] waitUntilDone:YES]; | |
10029 | } | |
10030 | ||
dc5812ec JF |
10031 | @end |
10032 | ||
b4d89997 JF |
10033 | /*IMP alloc_; |
10034 | id Alloc_(id self, SEL selector) { | |
10035 | id object = alloc_(self, selector); | |
c390d3ab | 10036 | lprintf("[%s]A-%p\n", self->isa->name, object); |
b4d89997 JF |
10037 | return object; |
10038 | }*/ | |
10039 | ||
36bb2ca2 JF |
10040 | /*IMP dealloc_; |
10041 | id Dealloc_(id self, SEL selector) { | |
10042 | id object = dealloc_(self, selector); | |
c390d3ab | 10043 | lprintf("[%s]D-%p\n", self->isa->name, object); |
36bb2ca2 JF |
10044 | return object; |
10045 | }*/ | |
b4d89997 | 10046 | |
30c5be06 JF |
10047 | Class $NSURLConnection; |
10048 | ||
10049 | MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) { | |
0c4fe0f4 | 10050 | NSMutableURLRequest *copy([[request mutableCopy] autorelease]); |
30c5be06 JF |
10051 | |
10052 | NSURL *url([copy URL]); | |
5e845121 | 10053 | |
30c5be06 | 10054 | NSString *host([url host]); |
e4b48f2f JF |
10055 | NSString *scheme([[url scheme] lowercaseString]); |
10056 | ||
10057 | NSString *compound([NSString stringWithFormat:@"%@:%@", scheme, host]); | |
30c5be06 | 10058 | |
48f1762f JF |
10059 | @synchronized (HostConfig_) { |
10060 | if ([copy respondsToSelector:@selector(setHTTPShouldUsePipelining:)]) | |
10061 | if ([PipelinedHosts_ containsObject:host] || [PipelinedHosts_ containsObject:compound]) | |
10062 | [copy setHTTPShouldUsePipelining:YES]; | |
5e845121 JF |
10063 | |
10064 | if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"]) | |
10065 | if ([control isEqualToString:@"max-age=0"]) | |
3a23d36e | 10066 | if ([CachedURLs_ containsObject:url]) { |
5e845121 | 10067 | #if !ForRelease |
3a23d36e | 10068 | NSLog(@"~~~: %@", url); |
5e845121 JF |
10069 | #endif |
10070 | ||
10071 | [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; | |
10072 | ||
10073 | [copy setValue:nil forHTTPHeaderField:@"Cache-Control"]; | |
10074 | [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"]; | |
10075 | [copy setValue:nil forHTTPHeaderField:@"If-None-Match"]; | |
10076 | } | |
48f1762f | 10077 | } |
30c5be06 JF |
10078 | |
10079 | if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) { | |
10080 | } return self; | |
10081 | } | |
10082 | ||
b1497b56 JF |
10083 | Class $WAKWindow; |
10084 | ||
4cc9e99a | 10085 | static CGSize $WAKWindow$screenSize(WAKWindow *self, SEL _cmd) { |
b1497b56 JF |
10086 | CGSize size([[UIScreen mainScreen] bounds].size); |
10087 | /*if ([$WAKWindow respondsToSelector:@selector(hasLandscapeOrientation)]) | |
10088 | if ([$WAKWindow hasLandscapeOrientation]) | |
10089 | std::swap(size.width, size.height);*/ | |
10090 | return size; | |
438d6708 JF |
10091 | } |
10092 | ||
29cbf4e5 JF |
10093 | Class $NSUserDefaults; |
10094 | ||
10095 | MSHook(id, NSUserDefaults$objectForKey$, NSUserDefaults *self, SEL _cmd, NSString *key) { | |
10096 | if ([key respondsToSelector:@selector(isEqualToString:)] && [key isEqualToString:@"WebKitLocalStorageDatabasePathPreferenceKey"]) | |
b44af625 | 10097 | return Cache("LocalStorage"); |
29cbf4e5 JF |
10098 | return _NSUserDefaults$objectForKey$(self, _cmd, key); |
10099 | } | |
10100 | ||
9adfb865 JF |
10101 | static NSMutableDictionary *AutoreleaseDeepMutableCopyOfDictionary(CFTypeRef type) { |
10102 | if (type == NULL) | |
10103 | return nil; | |
10104 | if (CFGetTypeID(type) != CFDictionaryGetTypeID()) | |
10105 | return nil; | |
10106 | CFTypeRef copy(CFPropertyListCreateDeepCopy(kCFAllocatorDefault, type, kCFPropertyListMutableContainers)); | |
10107 | CFRelease(type); | |
10108 | return [(NSMutableDictionary *) copy autorelease]; | |
10109 | } | |
10110 | ||
d13edf44 | 10111 | int main(int argc, char *argv[]) { |
db1e5e0d JF |
10112 | int fd(open("/tmp/cydia.log", O_WRONLY | O_APPEND | O_CREAT, 0644)); |
10113 | dup2(fd, 2); | |
10114 | close(fd); | |
10115 | ||
d13edf44 JF |
10116 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
10117 | ||
d6dad1b4 | 10118 | _trace(); |
f79a4512 | 10119 | |
9a60abe5 JF |
10120 | UpdateExternalStatus(0); |
10121 | ||
57e8b225 JF |
10122 | UIScreen *screen([UIScreen mainScreen]); |
10123 | if ([screen respondsToSelector:@selector(scale)]) | |
10124 | ScreenScale_ = [screen scale]; | |
10125 | else | |
10126 | ScreenScale_ = 1; | |
10127 | ||
c138614d | 10128 | UIDevice *device([UIDevice currentDevice]); |
08157100 | 10129 | if ([device respondsToSelector:@selector(userInterfaceIdiom)]) { |
c138614d | 10130 | UIUserInterfaceIdiom idiom([device userInterfaceIdiom]); |
08157100 JF |
10131 | if (idiom == UIUserInterfaceIdiomPad) |
10132 | IsWildcat_ = true; | |
c138614d JF |
10133 | } |
10134 | ||
08157100 JF |
10135 | Idiom_ = IsWildcat_ ? @"ipad" : @"iphone"; |
10136 | ||
b4fd1197 | 10137 | RegEx pattern("([0-9]+\\.[0-9]+).*"); |
fd825a2d | 10138 | |
9a4a4754 JF |
10139 | if (pattern([device systemVersion])) |
10140 | Firmware_ = pattern[1]; | |
fd825a2d JF |
10141 | if (pattern(Cydia_)) |
10142 | Major_ = pattern[1]; | |
9a4a4754 | 10143 | |
7b33d201 | 10144 | SessionData_ = [NSMutableDictionary dictionaryWithCapacity:4]; |
ef974f52 | 10145 | |
7b33d201 | 10146 | HostConfig_ = [[[NSObject alloc] init] autorelease]; |
48f1762f JF |
10147 | @synchronized (HostConfig_) { |
10148 | BridgedHosts_ = [NSMutableSet setWithCapacity:4]; | |
2e1652a9 | 10149 | InsecureHosts_ = [NSMutableSet setWithCapacity:4]; |
48f1762f | 10150 | PipelinedHosts_ = [NSMutableSet setWithCapacity:4]; |
5e845121 | 10151 | CachedURLs_ = [NSMutableSet setWithCapacity:32]; |
48f1762f | 10152 | } |
5df7ecfb | 10153 | |
de595d91 JF |
10154 | NSString *ui(@"ui/ios"); |
10155 | if (Idiom_ != nil) | |
10156 | ui = [ui stringByAppendingString:[NSString stringWithFormat:@"~%@", Idiom_]]; | |
fd825a2d | 10157 | ui = [ui stringByAppendingString:[NSString stringWithFormat:@"/%@", Major_]]; |
de595d91 | 10158 | UI_ = CydiaURL(ui); |
57e8b225 | 10159 | |
df213583 | 10160 | PackageName = reinterpret_cast<CYString &(*)(Package *, SEL)>(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); |
677b8415 | 10161 | |
7376b55c JF |
10162 | /* Library Hacks {{{ */ |
10163 | class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); | |
10164 | ||
b1497b56 JF |
10165 | $WAKWindow = objc_getClass("WAKWindow"); |
10166 | if ($WAKWindow != NULL) | |
438d6708 JF |
10167 | if (Method method = class_getInstanceMethod($WAKWindow, @selector(screenSize))) |
10168 | method_setImplementation(method, (IMP) &$WAKWindow$screenSize); | |
10169 | ||
30c5be06 JF |
10170 | $NSURLConnection = objc_getClass("NSURLConnection"); |
10171 | Method NSURLConnection$init$(class_getInstanceMethod($NSURLConnection, @selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:))); | |
10172 | if (NSURLConnection$init$ != NULL) { | |
10173 | _NSURLConnection$init$ = reinterpret_cast<id (*)(NSURLConnection *, SEL, NSURLRequest *, id, BOOL, int64_t, BOOL, NSDictionary *)>(method_getImplementation(NSURLConnection$init$)); | |
10174 | method_setImplementation(NSURLConnection$init$, reinterpret_cast<IMP>(&$NSURLConnection$init$)); | |
10175 | } | |
29cbf4e5 JF |
10176 | |
10177 | $NSUserDefaults = objc_getClass("NSUserDefaults"); | |
10178 | Method NSUserDefaults$objectForKey$(class_getInstanceMethod($NSUserDefaults, @selector(objectForKey:))); | |
10179 | if (NSUserDefaults$objectForKey$ != NULL) { | |
10180 | _NSUserDefaults$objectForKey$ = reinterpret_cast<id (*)(NSUserDefaults *, SEL, NSString *)>(method_getImplementation(NSUserDefaults$objectForKey$)); | |
10181 | method_setImplementation(NSUserDefaults$objectForKey$, reinterpret_cast<IMP>(&$NSUserDefaults$objectForKey$)); | |
10182 | } | |
7376b55c JF |
10183 | /* }}} */ |
10184 | /* Set Locale {{{ */ | |
f79a4512 | 10185 | Locale_ = CFLocaleCopyCurrent(); |
b1ce61ec | 10186 | Languages_ = [NSLocale preferredLanguages]; |
25fdc941 | 10187 | |
b1ce61ec | 10188 | //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); |
18876387 | 10189 | //NSLog(@"%@", [Languages_ description]); |
78430d06 | 10190 | |
b1ce61ec | 10191 | const char *lang; |
25fdc941 JF |
10192 | if (Locale_ != NULL) |
10193 | lang = [(NSString *) CFLocaleGetIdentifier(Locale_) UTF8String]; | |
3caee0a4 JF |
10194 | else if (Languages_ != nil && [Languages_ count] != 0) |
10195 | lang = [[Languages_ objectAtIndex:0] UTF8String]; | |
10196 | else | |
78430d06 | 10197 | // XXX: consider just setting to C and then falling through? |
b1ce61ec | 10198 | lang = NULL; |
3caee0a4 JF |
10199 | |
10200 | if (lang != NULL) { | |
b4fd1197 | 10201 | RegEx pattern("([a-z][a-z])(?:-[A-Za-z]*)?(_[A-Z][A-Z])?"); |
3caee0a4 | 10202 | lang = !pattern(lang) ? NULL : [pattern->*@"%1$@%2$@" UTF8String]; |
78430d06 JF |
10203 | } |
10204 | ||
b1ce61ec | 10205 | NSLog(@"Setting Language: %s", lang); |
3caee0a4 JF |
10206 | |
10207 | if (lang != NULL) { | |
10208 | setenv("LANG", lang, true); | |
10209 | std::setlocale(LC_ALL, lang); | |
10210 | } | |
7376b55c | 10211 | /* }}} */ |
aeeb755b | 10212 | /* Index Collation {{{ */ |
42e25bc5 | 10213 | if (Class $UILocalizedIndexedCollation = objc_getClass("UILocalizedIndexedCollation")) { @try { |
aeeb755b JF |
10214 | NSBundle *bundle([NSBundle bundleForClass:$UILocalizedIndexedCollation]); |
10215 | NSString *path([bundle pathForResource:@"UITableViewLocalizedSectionIndex" ofType:@"plist"]); | |
10216 | //path = @"/System/Library/Frameworks/UIKit.framework/.lproj/UITableViewLocalizedSectionIndex.plist"; | |
10217 | NSDictionary *dictionary([NSDictionary dictionaryWithContentsOfFile:path]); | |
1c28763e | 10218 | _H<UILocalizedIndexedCollation> collation([[[$UILocalizedIndexedCollation alloc] initWithDictionary:dictionary] autorelease]); |
aeeb755b JF |
10219 | |
10220 | CollationLocale_ = MSHookIvar<NSLocale *>(collation, "_locale"); | |
10221 | ||
f93f4997 JF |
10222 | if (kCFCoreFoundationVersionNumber >= 800 && [[CollationLocale_ localeIdentifier] isEqualToString:@"zh@collation=stroke"]) { |
10223 | CollationThumbs_ = [NSArray arrayWithObjects:@"1",@"•",@"4",@"•",@"7",@"•",@"10",@"•",@"13",@"•",@"16",@"•",@"19",@"A",@"•",@"E",@"•",@"I",@"•",@"M",@"•",@"R",@"•",@"V",@"•",@"Z",@"#",nil]; | |
10224 | 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}) | |
10225 | CollationOffset_.push_back(offset); | |
10226 | 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]; | |
10227 | 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]; | |
10228 | } else { | |
10229 | ||
aeeb755b JF |
10230 | CollationThumbs_ = [collation sectionIndexTitles]; |
10231 | for (size_t index(0), end([CollationThumbs_ count]); index != end; ++index) | |
10232 | CollationOffset_.push_back([collation sectionForSectionIndexTitleAtIndex:index]); | |
10233 | ||
10234 | CollationTitles_ = [collation sectionTitles]; | |
10235 | CollationStarts_ = MSHookIvar<NSArray *>(collation, "_sectionStartStrings"); | |
10236 | ||
22fd24dd JF |
10237 | NSString *&transform(MSHookIvar<NSString *>(collation, "_transform")); |
10238 | if (&transform != NULL && transform != nil) { | |
10239 | /*if ([collation respondsToSelector:@selector(transformedCollationStringForString:)]) | |
10240 | CollationModify_ = [=](NSString *value) { return [collation transformedCollationStringForString:value]; };*/ | |
10241 | const UChar *uid(reinterpret_cast<const UChar *>([transform cStringUsingEncoding:NSUnicodeStringEncoding])); | |
10242 | UErrorCode code(U_ZERO_ERROR); | |
10243 | CollationTransl_ = utrans_openU(uid, -1, UTRANS_FORWARD, NULL, 0, NULL, &code); | |
10244 | if (!U_SUCCESS(code)) | |
10245 | NSLog(@"%s", u_errorName(code)); | |
10246 | } | |
f93f4997 JF |
10247 | |
10248 | } | |
42e25bc5 JF |
10249 | } @catch (NSException *e) { |
10250 | NSLog(@"%@", e); | |
10251 | goto hard; | |
10252 | } } else hard: { | |
aeeb755b JF |
10253 | CollationLocale_ = [[[NSLocale alloc] initWithLocaleIdentifier:@"en@collation=dictionary"] autorelease]; |
10254 | ||
10255 | 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]; | |
10256 | for (NSInteger offset(0); offset != 28; ++offset) | |
10257 | CollationOffset_.push_back(offset); | |
10258 | ||
10259 | 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]; | |
10260 | 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]; | |
10261 | } | |
10262 | /* }}} */ | |
7376b55c | 10263 | /* Parse Arguments {{{ */ |
de3b1ab4 JF |
10264 | bool substrate(false); |
10265 | ||
10266 | if (argc != 0) { | |
10267 | char **args(argv); | |
10268 | int arge(1); | |
10269 | ||
10270 | for (int argi(1); argi != argc; ++argi) | |
10271 | if (strcmp(argv[argi], "--") == 0) { | |
10272 | arge = argi; | |
10273 | argv[argi] = argv[0]; | |
10274 | argv += argi; | |
10275 | argc -= argi; | |
10276 | break; | |
10277 | } | |
10278 | ||
10279 | for (int argi(1); argi != arge; ++argi) | |
d791dce4 | 10280 | if (strcmp(args[argi], "--substrate") == 0) |
de3b1ab4 JF |
10281 | substrate = true; |
10282 | else | |
10283 | fprintf(stderr, "unknown argument: %s\n", args[argi]); | |
10284 | } | |
7376b55c | 10285 | /* }}} */ |
d73cede2 | 10286 | |
7376b55c | 10287 | App_ = [[NSBundle mainBundle] bundlePath]; |
d791dce4 | 10288 | Advanced_ = YES; |
7376b55c | 10289 | |
b44af625 | 10290 | Cache_ = [[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia", @"/var/mobile"] retain]; |
d1c7f1fd | 10291 | |
b4d89997 JF |
10292 | /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); |
10293 | alloc_ = alloc->method_imp; | |
10294 | alloc->method_imp = (IMP) &Alloc_;*/ | |
10295 | ||
36bb2ca2 JF |
10296 | /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc)); |
10297 | dealloc_ = dealloc->method_imp; | |
10298 | dealloc->method_imp = (IMP) &Dealloc_;*/ | |
10299 | ||
7c80833f JF |
10300 | void *gestalt(dlopen("/usr/lib/libMobileGestalt.dylib", RTLD_GLOBAL | RTLD_LAZY)); |
10301 | $MGCopyAnswer = reinterpret_cast<CFStringRef (*)(CFStringRef)>(dlsym(gestalt, "MGCopyAnswer")); | |
10302 | ||
d791dce4 | 10303 | /* System Information {{{ */ |
3178d79b | 10304 | size_t size; |
c390d3ab JF |
10305 | |
10306 | int maxproc; | |
10307 | size = sizeof(maxproc); | |
10308 | if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) | |
10309 | perror("sysctlbyname(\"kern.maxproc\", ?)"); | |
10310 | else if (maxproc < 64) { | |
10311 | maxproc = 64; | |
10312 | if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) | |
10313 | perror("sysctlbyname(\"kern.maxproc\", #)"); | |
10314 | } | |
10315 | ||
bfc87a4d JF |
10316 | sysctlbyname("kern.osversion", NULL, &size, NULL, 0); |
10317 | char *osversion = new char[size]; | |
10318 | if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1) | |
10319 | perror("sysctlbyname(\"kern.osversion\", ?)"); | |
10320 | else | |
10321 | System_ = [NSString stringWithUTF8String:osversion]; | |
10322 | ||
3178d79b JF |
10323 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
10324 | char *machine = new char[size]; | |
c390d3ab JF |
10325 | if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1) |
10326 | perror("sysctlbyname(\"hw.machine\", ?)"); | |
10327 | else | |
10328 | Machine_ = machine; | |
3178d79b | 10329 | |
8dcc32d4 | 10330 | int64_t usermem(0); |
ce1901de JF |
10331 | size = sizeof(usermem); |
10332 | if (sysctlbyname("hw.usermem", &usermem, &size, NULL, 0) == -1) | |
10333 | usermem = 0; | |
10334 | ||
c31d7cdc JF |
10335 | SerialNumber_ = (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber"); |
10336 | ChipID_ = [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString]; | |
10337 | BBSNum_ = CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false); | |
59dbe296 | 10338 | |
7c80833f | 10339 | UniqueID_ = UniqueIdentifier(device); |
3178d79b | 10340 | |
3e9c9e85 JF |
10341 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) { |
10342 | Product_ = [info objectForKey:@"SafariProductVersion"]; | |
10343 | Safari_ = [info objectForKey:@"CFBundleVersion"]; | |
10344 | } | |
e967efd5 | 10345 | |
dd58e110 | 10346 | NSString *agent([NSString stringWithFormat:@"Cydia/%@ CyF/%.2f", Cydia_, kCFCoreFoundationVersionNumber]); |
e967efd5 | 10347 | |
b4fd1197 JF |
10348 | if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Safari_)) |
10349 | agent = [NSString stringWithFormat:@"Safari/%@ %@", match[1], agent]; | |
10350 | if (RegEx match = RegEx("([0-9]+[A-Z][0-9]+[a-z]?).*", System_)) | |
10351 | agent = [NSString stringWithFormat:@"Mobile/%@ %@", match[1], agent]; | |
10352 | if (RegEx match = RegEx("([0-9]+(\\.[0-9]+)+).*", Product_)) | |
10353 | agent = [NSString stringWithFormat:@"Version/%@ %@", match[1], agent]; | |
e967efd5 JF |
10354 | |
10355 | UserAgent_ = agent; | |
d791dce4 | 10356 | /* }}} */ |
7376b55c | 10357 | /* Load Database {{{ */ |
4e479350 JF |
10358 | SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; |
10359 | ||
d6dad1b4 | 10360 | _trace(); |
4e479350 JF |
10361 | mkdir("/var/mobile/Library/Cydia", 0755); |
10362 | MetaFile_.Open("/var/mobile/Library/Cydia/metadata.cb0"); | |
f79a4512 | 10363 | _trace(); |
d6dad1b4 | 10364 | |
9adfb865 JF |
10365 | Values_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaValues"), CFSTR("com.saurik.Cydia"))); |
10366 | Sections_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaSections"), CFSTR("com.saurik.Cydia"))); | |
10367 | Sources_ = AutoreleaseDeepMutableCopyOfDictionary(CFPreferencesCopyAppValue(CFSTR("CydiaSources"), CFSTR("com.saurik.Cydia"))); | |
4e479350 | 10368 | Version_ = [(NSNumber *) CFPreferencesCopyAppValue(CFSTR("CydiaVersion"), CFSTR("com.saurik.Cydia")) autorelease]; |
ef055c6c | 10369 | |
4e479350 JF |
10370 | _trace(); |
10371 | NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]); | |
7b0ce2da | 10372 | |
4e479350 JF |
10373 | if (Values_ == nil) |
10374 | Values_ = [metadata objectForKey:@"Values"]; | |
10375 | if (Values_ == nil) | |
b3c8e69c | 10376 | Values_ = [[[NSMutableDictionary alloc] initWithCapacity:4] autorelease]; |
b3c8e69c | 10377 | |
4e479350 JF |
10378 | if (Sections_ == nil) |
10379 | Sections_ = [metadata objectForKey:@"Sections"]; | |
10380 | if (Sections_ == nil) | |
7b0ce2da | 10381 | Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; |
7b0ce2da | 10382 | |
4e479350 JF |
10383 | if (Sources_ == nil) |
10384 | Sources_ = [metadata objectForKey:@"Sources"]; | |
10385 | if (Sources_ == nil) | |
7b0ce2da | 10386 | Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; |
33e30380 | 10387 | |
4e479350 JF |
10388 | // XXX: this wrong, but in a way that doesn't matter :/ |
10389 | if (Version_ == nil) | |
10390 | Version_ = [metadata objectForKey:@"Version"]; | |
10391 | if (Version_ == nil) | |
33e30380 | 10392 | Version_ = [NSNumber numberWithUnsignedInt:0]; |
4e479350 JF |
10393 | |
10394 | if (NSDictionary *packages = [metadata objectForKey:@"Packages"]) { | |
10395 | bool fail(false); | |
10396 | CFDictionaryApplyFunction((CFDictionaryRef) packages, &PackageImport, &fail); | |
10397 | _trace(); | |
10398 | if (fail) | |
10399 | NSLog(@"unable to import package preferences... from 2010? oh well :/"); | |
33e30380 JF |
10400 | } |
10401 | ||
33e30380 | 10402 | if ([Version_ unsignedIntValue] == 0) { |
25c1dafb JF |
10403 | CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]); |
10404 | CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]); | |
10405 | CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]); | |
10406 | CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./"); | |
33e30380 JF |
10407 | |
10408 | Version_ = [NSNumber numberWithUnsignedInt:1]; | |
33e30380 | 10409 | |
6f87c61a JF |
10410 | if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:@ CacheState_]) { |
10411 | [cache removeObjectForKey:@"LastUpdate"]; | |
10412 | [cache writeToFile:@ CacheState_ atomically:YES]; | |
10413 | } | |
33e30380 | 10414 | } |
2595e4c3 JF |
10415 | |
10416 | _H<NSMutableArray> broken([NSMutableArray array]); | |
10417 | for (NSString *key in (id) Sources_) | |
d93e3f22 | 10418 | if ([key rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"# "]].location != NSNotFound || ![([[Sources_ objectForKey:key] objectForKey:@"URI"] ?: @"/") hasSuffix:@"/"]) |
2595e4c3 | 10419 | [broken addObject:key]; |
4e479350 | 10420 | if ([broken count] != 0) |
2595e4c3 JF |
10421 | for (NSString *key in (id) broken) |
10422 | [Sources_ removeObjectForKey:key]; | |
4e479350 | 10423 | broken = nil; |
94b0b3e5 | 10424 | |
4e479350 JF |
10425 | SaveConfig(nil); |
10426 | system("/usr/libexec/cydia/cydo /bin/rm -f /var/lib/cydia/metadata.plist"); | |
10427 | /* }}} */ | |
94b0b3e5 | 10428 | |
d791dce4 JF |
10429 | Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; |
10430 | ||
6c9fe9af | 10431 | if (kCFCoreFoundationVersionNumber > 1000) |
0c0a966b | 10432 | system("/usr/libexec/cydia/cydo /usr/libexec/cydia/setnsfpn /var/lib"); |
6c9fe9af | 10433 | |
01d93940 JF |
10434 | int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); |
10435 | ||
ad502f71 | 10436 | if (access("/User", F_OK) != 0 || version != 6) { |
d6dad1b4 | 10437 | _trace(); |
0c0a966b | 10438 | system("/usr/libexec/cydia/cydo /usr/libexec/cydia/firmware.sh"); |
d6dad1b4 JF |
10439 | _trace(); |
10440 | } | |
9e98e020 | 10441 | |
7376b55c | 10442 | if (access("/tmp/cydia.chk", F_OK) == 0) { |
b44af625 | 10443 | if (unlink([Cache("pkgcache.bin") UTF8String]) == -1) |
7376b55c | 10444 | _assert(errno == ENOENT); |
b44af625 | 10445 | if (unlink([Cache("srcpkgcache.bin") UTF8String]) == -1) |
7376b55c JF |
10446 | _assert(errno == ENOENT); |
10447 | } | |
10448 | ||
19cd1e0d JF |
10449 | system("/usr/libexec/cydia/cydo /bin/ln -sf /var/mobile/Library/Caches/com.saurik.Cydia/sources.list /etc/apt/sources.list.d/cydia.list"); |
10450 | ||
59dbe296 | 10451 | /* APT Initialization {{{ */ |
b1ce61ec JF |
10452 | _assert(pkgInitConfig(*_config)); |
10453 | _assert(pkgInitSystem(*_config, _system)); | |
10454 | ||
10455 | if (lang != NULL) | |
10456 | _config->Set("APT::Acquire::Translation", lang); | |
cb94ff21 JF |
10457 | |
10458 | // XXX: this timeout might be important :( | |
10459 | //_config->Set("Acquire::http::Timeout", 15); | |
10460 | ||
ce1901de | 10461 | _config->Set("Acquire::http::MaxParallel", usermem >= 384 * 1024 * 1024 ? 16 : 3); |
b44af625 JF |
10462 | |
10463 | mkdir([Cache_ UTF8String], 0755); | |
10464 | mkdir([Cache("archives") UTF8String], 0755); | |
10465 | mkdir([Cache("archives/partial") UTF8String], 0755); | |
10466 | _config->Set("Dir::Cache", [Cache_ UTF8String]); | |
10467 | ||
49cc457a JF |
10468 | symlink("/var/lib/apt/extended_states", [Cache("extended_states") UTF8String]); |
10469 | _config->Set("Dir::State", [Cache_ UTF8String]); | |
10470 | ||
b44af625 JF |
10471 | mkdir([Cache("lists") UTF8String], 0755); |
10472 | mkdir([Cache("lists/partial") UTF8String], 0755); | |
10473 | mkdir([Cache("periodic") UTF8String], 0755); | |
e6446ca0 | 10474 | _config->Set("Dir::State::Lists", [Cache("lists") UTF8String]); |
232b396b JF |
10475 | |
10476 | std::string logs("/var/mobile/Library/Logs/Cydia"); | |
10477 | mkdir(logs.c_str(), 0755); | |
10478 | _config->Set("Dir::Log::Terminal", logs + "/apt.log"); | |
10479 | ||
10480 | _config->Set("Dir::Bin::dpkg", "/usr/libexec/cydia/cydo"); | |
59dbe296 | 10481 | /* }}} */ |
7376b55c | 10482 | /* Color Choices {{{ */ |
36bb2ca2 JF |
10483 | space_ = CGColorSpaceCreateDeviceRGB(); |
10484 | ||
f641a0e5 | 10485 | Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0); |
77fcccaf | 10486 | Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0); |
36bb2ca2 | 10487 | Black_.Set(space_, 0.0, 0.0, 0.0, 1.0); |
86a333c6 | 10488 | Folder_.Set(space_, 0x8e/255.f, 0x8e/255.f, 0x93/255.f, 1.0); |
baf80942 | 10489 | Off_.Set(space_, 0.9, 0.9, 0.9, 1.0); |
36bb2ca2 | 10490 | White_.Set(space_, 1.0, 1.0, 1.0, 1.0); |
7b0ce2da | 10491 | Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0); |
3bd1c2a2 JF |
10492 | Green_.Set(space_, 0.0, 0.5, 0.0, 1.0); |
10493 | Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0); | |
10494 | Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0); | |
59dbe296 | 10495 | |
dc63e78f JF |
10496 | InstallingColor_ = [UIColor colorWithRed:0.88f green:1.00f blue:0.88f alpha:1.00f]; |
10497 | RemovingColor_ = [UIColor colorWithRed:1.00f green:0.88f blue:0.88f alpha:1.00f]; | |
7376b55c | 10498 | /* }}}*/ |
7376b55c | 10499 | /* UIKit Configuration {{{ */ |
600d005d JF |
10500 | // XXX: I have a feeling this was important |
10501 | //UIKeyboardDisableAutomaticAppearance(); | |
7376b55c | 10502 | /* }}} */ |
87f46a96 | 10503 | |
8a3b565c JF |
10504 | $SBSSetInterceptsMenuButtonForever = reinterpret_cast<void (*)(bool)>(dlsym(RTLD_DEFAULT, "SBSSetInterceptsMenuButtonForever")); |
10505 | ||
b37b0a4a JF |
10506 | const char *symbol(kCFCoreFoundationVersionNumber >= 800 ? "MGGetBoolAnswer" : "GSSystemHasCapability"); |
10507 | BOOL (*GSSystemHasCapability)(CFStringRef) = reinterpret_cast<BOOL (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, symbol)); | |
c73d524b JF |
10508 | bool fast = GSSystemHasCapability != NULL && GSSystemHasCapability(CFSTR("armv7")); |
10509 | ||
c73d524b | 10510 | PulseInterval_ = fast ? 50000 : 500000; |
8731fdb0 | 10511 | |
670a0494 | 10512 | Colon_ = UCLocalize("COLON_DELIMITED"); |
72fb3616 | 10513 | Elision_ = UCLocalize("ELISION"); |
670a0494 JF |
10514 | Error_ = UCLocalize("ERROR"); |
10515 | Warning_ = UCLocalize("WARNING"); | |
10516 | ||
d6dad1b4 | 10517 | _trace(); |
77df4f82 | 10518 | int value(UIApplicationMain(argc, argv, @"Cydia", @"Cydia")); |
36bb2ca2 JF |
10519 | |
10520 | CGColorSpaceRelease(space_); | |
199d0ba5 | 10521 | CFRelease(Locale_); |
36bb2ca2 | 10522 | |
d13edf44 | 10523 | [pool release]; |
36bb2ca2 | 10524 | return value; |
6d166849 | 10525 | } |