]>
Commit | Line | Data |
---|---|---|
36bb2ca2 | 1 | /* Cydia - iPhone UIKit Front-End for Debian APT |
77801ff1 | 2 | * Copyright (C) 2008-2010 Jay Freeman (saurik) |
36bb2ca2 JF |
3 | */ |
4 | ||
bfc87a4d | 5 | /* Modified BSD License {{{ */ |
36bb2ca2 JF |
6 | /* |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the | |
12 | * above copyright notice, this list of conditions | |
13 | * and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the | |
15 | * above copyright notice, this list of conditions | |
16 | * and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. The name of the author may not be used to endorse | |
20 | * or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
25 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
36 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
bfc87a4d | 38 | /* }}} */ |
36bb2ca2 | 39 | |
b0ad8dee JF |
40 | // XXX: wtf/FastMalloc.h... wtf? |
41 | #define USE_SYSTEM_MALLOC 1 | |
42 | ||
dc5812ec | 43 | /* #include Directives {{{ */ |
c21004b9 JF |
44 | #include "UICaboodle/UCPlatform.h" |
45 | #include "UICaboodle/UCLocalize.h" | |
807ae6d7 | 46 | |
ab398adf JF |
47 | #include <objc/objc.h> |
48 | #include <objc/runtime.h> | |
49 | ||
5f6bff8c | 50 | #include <CoreGraphics/CoreGraphics.h> |
5f6bff8c | 51 | #include <Foundation/Foundation.h> |
7d2ac47f | 52 | |
eef4ccaf JF |
53 | #if 0 |
54 | #define DEPLOYMENT_TARGET_MACOSX 1 | |
55 | #define CF_BUILDING_CF 1 | |
56 | #include <CoreFoundation/CFInternal.h> | |
57 | #endif | |
58 | ||
677b8415 JF |
59 | #include <CoreFoundation/CFPriv.h> |
60 | #include <CoreFoundation/CFUniChar.h> | |
61 | ||
afb5333a JF |
62 | #include <SystemConfiguration/SystemConfiguration.h> |
63 | ||
c21004b9 JF |
64 | #include <UIKit/UIKit.h> |
65 | #include "iPhonePrivate.h" | |
66 | ||
67 | #include <IOKit/IOKitLib.h> | |
a9543575 | 68 | |
f79a4512 | 69 | #include <WebCore/WebCoreThread.h> |
7e9a36b6 | 70 | |
677b8415 | 71 | #include <algorithm> |
808c6eb6 | 72 | #include <iomanip> |
2367a917 | 73 | #include <sstream> |
a0be02eb JF |
74 | #include <string> |
75 | ||
2367a917 JF |
76 | #include <ext/stdio_filebuf.h> |
77 | ||
e59669fd JF |
78 | #undef ABS |
79 | ||
dc5812ec JF |
80 | #include <apt-pkg/acquire.h> |
81 | #include <apt-pkg/acquire-item.h> | |
82 | #include <apt-pkg/algorithms.h> | |
83 | #include <apt-pkg/cachefile.h> | |
77fcccaf | 84 | #include <apt-pkg/clean.h> |
dc5812ec | 85 | #include <apt-pkg/configuration.h> |
7376b55c | 86 | #include <apt-pkg/debindexfile.h> |
3178d79b | 87 | #include <apt-pkg/debmetaindex.h> |
dc5812ec JF |
88 | #include <apt-pkg/error.h> |
89 | #include <apt-pkg/init.h> | |
dddbc481 | 90 | #include <apt-pkg/mmap.h> |
dc5812ec | 91 | #include <apt-pkg/pkgrecords.h> |
dddbc481 | 92 | #include <apt-pkg/sha1.h> |
dc5812ec | 93 | #include <apt-pkg/sourcelist.h> |
2367a917 | 94 | #include <apt-pkg/sptr.h> |
affeffc7 | 95 | #include <apt-pkg/strutl.h> |
f9f6d9e8 | 96 | #include <apt-pkg/tagfile.h> |
dc5812ec | 97 | |
f79a4512 JF |
98 | #include <apr-1/apr_pools.h> |
99 | ||
87f46a96 JF |
100 | #include <sys/types.h> |
101 | #include <sys/stat.h> | |
3178d79b | 102 | #include <sys/sysctl.h> |
59c011d8 JF |
103 | #include <sys/param.h> |
104 | #include <sys/mount.h> | |
bb0fe3c9 | 105 | #include <sys/reboot.h> |
87f46a96 | 106 | |
c21004b9 | 107 | #include <fcntl.h> |
2a8d9add | 108 | #include <notify.h> |
dddbc481 | 109 | #include <dlfcn.h> |
3178d79b | 110 | |
b4d89997 JF |
111 | extern "C" { |
112 | #include <mach-o/nlist.h> | |
113 | } | |
114 | ||
a0be02eb JF |
115 | #include <cstdio> |
116 | #include <cstdlib> | |
117 | #include <cstring> | |
b4d89997 | 118 | |
2367a917 | 119 | #include <errno.h> |
dc5812ec | 120 | #include <pcre.h> |
a9543575 | 121 | |
94b0b3e5 JF |
122 | #include <Cytore.hpp> |
123 | ||
c21004b9 | 124 | #include "UICaboodle/BrowserView.h" |
71cc7be1 | 125 | #include "SDURLCache/SDURLCache.h" |
fd7853a6 | 126 | |
c21004b9 | 127 | #include "substrate.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) | |
76933519 | 164 | std::cerr << std::setw(5) << count_ << ", " << std::setw(7) << 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 JF |
199 | |
200 | #define _pooled _H<NSAutoreleasePool> _pool([[NSAutoreleasePool alloc] init], true); | |
201 | ||
53bed0ff JF |
202 | #define CYPoolStart() \ |
203 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
204 | do | |
205 | #define CYPoolEnd() \ | |
206 | while (false); \ | |
207 | [_pool release]; | |
208 | ||
94b0b3e5 JF |
209 | // Hash Functions/Structures {{{ |
210 | extern "C" uint32_t hashlittle(const void *key, size_t length, uint32_t initval = 0); | |
211 | ||
212 | union SplitHash { | |
213 | uint32_t u32; | |
214 | uint16_t u16[2]; | |
215 | }; | |
216 | // }}} | |
217 | ||
04fe1349 JF |
218 | static const NSUInteger UIViewAutoresizingFlexibleBoth(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); |
219 | ||
bf8476c8 JF |
220 | void NSLogPoint(const char *fix, const CGPoint &point) { |
221 | NSLog(@"%s(%g,%g)", fix, point.x, point.y); | |
222 | } | |
223 | ||
affeffc7 JF |
224 | void NSLogRect(const char *fix, const CGRect &rect) { |
225 | NSLog(@"%s(%g,%g)+(%g,%g)", fix, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); | |
1e7a90f5 JF |
226 | } |
227 | ||
670a0494 JF |
228 | static _finline NSString *CydiaURL(NSString *path) { |
229 | char page[25]; | |
230 | page[0] = 'h'; page[1] = 't'; page[2] = 't'; page[3] = 'p'; page[4] = ':'; | |
231 | page[5] = '/'; page[6] = '/'; page[7] = 'c'; page[8] = 'y'; page[9] = 'd'; | |
232 | page[10] = 'i'; page[11] = 'a'; page[12] = '.'; page[13] = 's'; page[14] = 'a'; | |
233 | page[15] = 'u'; page[16] = 'r'; page[17] = 'i'; page[18] = 'k'; page[19] = '.'; | |
234 | page[20] = 'c'; page[21] = 'o'; page[22] = 'm'; page[23] = '/'; page[24] = '\0'; | |
235 | return [[NSString stringWithUTF8String:page] stringByAppendingString:path]; | |
236 | } | |
237 | ||
ef494bd8 RP |
238 | static _finline void UpdateExternalStatus(uint64_t newStatus) { |
239 | int notify_token; | |
240 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
241 | notify_set_state(notify_token, newStatus); | |
242 | notify_cancel(notify_token); | |
243 | } | |
244 | notify_post("com.saurik.Cydia.status"); | |
245 | } | |
246 | ||
bfc87a4d | 247 | /* [NSObject yieldToSelector:(withObject:)] {{{*/ |
d061f4ba | 248 | @interface NSObject (Cydia) |
2fc76a2d JF |
249 | - (id) yieldToSelector:(SEL)selector withObject:(id)object; |
250 | - (id) yieldToSelector:(SEL)selector; | |
d061f4ba JF |
251 | @end |
252 | ||
253 | @implementation NSObject (Cydia) | |
254 | ||
255 | - (void) doNothing { | |
256 | } | |
257 | ||
2fc76a2d | 258 | - (void) _yieldToContext:(NSMutableArray *)context { _pooled |
d061f4ba JF |
259 | SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue])); |
260 | id object([[context objectAtIndex:1] nonretainedObjectValue]); | |
261 | volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue])); | |
262 | ||
2fc76a2d JF |
263 | /* XXX: deal with exceptions */ |
264 | id value([self performSelector:selector withObject:object]); | |
265 | ||
677b8415 | 266 | NSMethodSignature *signature([self methodSignatureForSelector:selector]); |
2fc76a2d | 267 | [context removeAllObjects]; |
677b8415 | 268 | if ([signature methodReturnLength] != 0 && value != nil) |
2fc76a2d | 269 | [context addObject:value]; |
d061f4ba JF |
270 | |
271 | stopped = true; | |
272 | ||
273 | [self | |
274 | performSelectorOnMainThread:@selector(doNothing) | |
275 | withObject:nil | |
276 | waitUntilDone:NO | |
277 | ]; | |
278 | } | |
279 | ||
2fc76a2d | 280 | - (id) yieldToSelector:(SEL)selector withObject:(id)object { |
d061f4ba JF |
281 | volatile bool stopped(false); |
282 | ||
2fc76a2d | 283 | NSMutableArray *context([NSMutableArray arrayWithObjects: |
d061f4ba JF |
284 | [NSValue valueWithPointer:selector], |
285 | [NSValue valueWithNonretainedObject:object], | |
286 | [NSValue valueWithPointer:const_cast<bool *>(&stopped)], | |
287 | nil]); | |
288 | ||
289 | NSThread *thread([[[NSThread alloc] | |
290 | initWithTarget:self | |
291 | selector:@selector(_yieldToContext:) | |
292 | object:context | |
293 | ] autorelease]); | |
294 | ||
295 | [thread start]; | |
296 | ||
297 | NSRunLoop *loop([NSRunLoop currentRunLoop]); | |
298 | NSDate *future([NSDate distantFuture]); | |
299 | ||
300 | while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]); | |
2fc76a2d JF |
301 | |
302 | return [context count] == 0 ? nil : [context objectAtIndex:0]; | |
303 | } | |
304 | ||
305 | - (id) yieldToSelector:(SEL)selector { | |
306 | return [self yieldToSelector:selector withObject:nil]; | |
d061f4ba JF |
307 | } |
308 | ||
309 | @end | |
bfc87a4d | 310 | /* }}} */ |
d061f4ba | 311 | |
718bf014 | 312 | /* Cydia Action Sheet {{{ */ |
38be2c4b | 313 | @interface CYActionSheet : UIAlertView { |
670a0494 JF |
314 | unsigned button_; |
315 | } | |
316 | ||
317 | - (int) yieldToPopupAlertAnimated:(BOOL)animated; | |
318 | @end | |
319 | ||
320 | @implementation CYActionSheet | |
321 | ||
322 | - (id) initWithTitle:(NSString *)title buttons:(NSArray *)buttons defaultButtonIndex:(int)index { | |
38be2c4b | 323 | if ((self = [super init])) { |
b5e7eebb GP |
324 | [self setTitle:title]; |
325 | [self setDelegate:self]; | |
326 | for (NSString *button in buttons) [self addButtonWithTitle:button]; | |
327 | [self setCancelButtonIndex:index]; | |
670a0494 JF |
328 | } return self; |
329 | } | |
330 | ||
6f6d34ec | 331 | - (void) _updateFrameForDisplay { |
31f8d96a RP |
332 | [super _updateFrameForDisplay]; |
333 | if ([self cancelButtonIndex] == -1) { | |
334 | NSArray *buttons = [self buttons]; | |
335 | if ([buttons count]) { | |
336 | UIImage *background = [[buttons objectAtIndex:0] backgroundForState:0]; | |
337 | for (UIThreePartButton *button in buttons) | |
338 | [button setBackground:background forState:0]; | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
38be2c4b GP |
343 | - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
344 | button_ = buttonIndex + 1; | |
670a0494 JF |
345 | } |
346 | ||
be2b3d7b | 347 | - (void) dismiss { |
b5e7eebb | 348 | [self dismissWithClickedButtonIndex:-1 animated:YES]; |
be2b3d7b GP |
349 | } |
350 | ||
670a0494 | 351 | - (int) yieldToPopupAlertAnimated:(BOOL)animated { |
a07b7e8b | 352 | [self setRunsModal:YES]; |
670a0494 | 353 | button_ = 0; |
be2b3d7b | 354 | [self show]; |
670a0494 JF |
355 | return button_; |
356 | } | |
357 | ||
358 | @end | |
718bf014 | 359 | /* }}} */ |
670a0494 | 360 | |
68f1828e | 361 | /* NSForcedOrderingSearch doesn't work on the iPhone */ |
808c6eb6 | 362 | static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch; |
677b8415 JF |
363 | static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch; |
364 | static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareCaseInsensitive | kCFCompareNonliteral | kCFCompareLocalized | kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; | |
1e7a90f5 | 365 | |
6e201c55 JF |
366 | /* Information Dictionaries {{{ */ |
367 | @interface NSMutableArray (Cydia) | |
368 | - (void) addInfoDictionary:(NSDictionary *)info; | |
369 | @end | |
370 | ||
371 | @implementation NSMutableArray (Cydia) | |
372 | ||
373 | - (void) addInfoDictionary:(NSDictionary *)info { | |
374 | [self addObject:info]; | |
375 | } | |
376 | ||
377 | @end | |
378 | ||
379 | @interface NSMutableDictionary (Cydia) | |
380 | - (void) addInfoDictionary:(NSDictionary *)info; | |
381 | @end | |
382 | ||
383 | @implementation NSMutableDictionary (Cydia) | |
384 | ||
385 | - (void) addInfoDictionary:(NSDictionary *)info { | |
bfc87a4d | 386 | [self setObject:info forKey:[info objectForKey:@"CFBundleIdentifier"]]; |
6e201c55 JF |
387 | } |
388 | ||
389 | @end | |
390 | /* }}} */ | |
391 | ||
c390d3ab | 392 | #define lprintf(args...) fprintf(stderr, args) |
a9543575 | 393 | |
14d60a3f | 394 | #define ForRelease 1 |
ab92ad73 | 395 | #define TraceLogging (1 && !ForRelease) |
1539387a | 396 | #define HistogramInsertionSort (!ForRelease ? 0 : 0) |
f9f6d9e8 | 397 | #define ProfileTimes (0 && !ForRelease) |
123c5344 | 398 | #define ForSaurik (0 && !ForRelease) |
670a0494 | 399 | #define LogBrowser (0 && !ForRelease) |
b1ce61ec | 400 | #define TrackResize (0 && !ForRelease) |
2634b249 | 401 | #define ManualRefresh (1 && !ForRelease) |
f79a4512 | 402 | #define ShowInternals (0 && !ForRelease) |
b0ad8dee | 403 | #define IgnoreInstall (0 && !ForRelease) |
2634b249 | 404 | #define AlwaysReload (0 && !ForRelease) |
795d26fc | 405 | |
ab92ad73 | 406 | #if !TraceLogging |
189a73d0 JF |
407 | #undef _trace |
408 | #define _trace(args...) | |
ab92ad73 JF |
409 | #endif |
410 | ||
411 | #if !ProfileTimes | |
808c6eb6 | 412 | #undef _profile |
3e9c9e85 | 413 | #define _profile(name) { |
808c6eb6 | 414 | #undef _end |
3e9c9e85 | 415 | #define _end } |
2fc76a2d | 416 | #define PrintTimes() do {} while (false) |
189a73d0 JF |
417 | #endif |
418 | ||
807ae6d7 | 419 | /* Radix Sort {{{ */ |
df213583 JF |
420 | typedef uint32_t (*SKRadixFunction)(id, void *); |
421 | ||
807ae6d7 | 422 | @interface NSMutableArray (Radix) |
df213583 | 423 | - (void) radixSortUsingFunction:(SKRadixFunction)function withContext:(void *)argument; |
807ae6d7 JF |
424 | @end |
425 | ||
f79a4512 JF |
426 | struct RadixItem_ { |
427 | size_t index; | |
428 | uint32_t key; | |
429 | }; | |
807ae6d7 | 430 | |
b0403ab1 JF |
431 | @implementation NSMutableArray (Radix) |
432 | ||
433 | - (void) radixSortUsingFunction:(SKRadixFunction)function withContext:(void *)argument { | |
434 | size_t count([self count]); | |
435 | struct RadixItem_ *swap(new RadixItem_[count * 2]); | |
436 | ||
437 | for (size_t i(0); i != count; ++i) { | |
438 | RadixItem_ &item(swap[i]); | |
439 | item.index = i; | |
440 | ||
441 | id object([self objectAtIndex:i]); | |
442 | item.key = function(object, argument); | |
443 | } | |
444 | ||
f79a4512 | 445 | struct RadixItem_ *lhs(swap), *rhs(swap + count); |
807ae6d7 | 446 | |
ec3f2f53 | 447 | static const size_t width = 32; |
807ae6d7 JF |
448 | static const size_t bits = 11; |
449 | static const size_t slots = 1 << bits; | |
ec3f2f53 | 450 | static const size_t passes = (width + (bits - 1)) / bits; |
807ae6d7 JF |
451 | |
452 | size_t *hist(new size_t[slots]); | |
453 | ||
454 | for (size_t pass(0); pass != passes; ++pass) { | |
455 | memset(hist, 0, sizeof(size_t) * slots); | |
456 | ||
457 | for (size_t i(0); i != count; ++i) { | |
458 | uint32_t key(lhs[i].key); | |
459 | key >>= pass * bits; | |
ec3f2f53 | 460 | key &= _not(uint32_t) >> width - bits; |
807ae6d7 JF |
461 | ++hist[key]; |
462 | } | |
463 | ||
464 | size_t offset(0); | |
465 | for (size_t i(0); i != slots; ++i) { | |
466 | size_t local(offset); | |
467 | offset += hist[i]; | |
468 | hist[i] = local; | |
469 | } | |
470 | ||
471 | for (size_t i(0); i != count; ++i) { | |
472 | uint32_t key(lhs[i].key); | |
473 | key >>= pass * bits; | |
ec3f2f53 | 474 | key &= _not(uint32_t) >> width - bits; |
807ae6d7 JF |
475 | rhs[hist[key]++] = lhs[i]; |
476 | } | |
477 | ||
f79a4512 | 478 | RadixItem_ *tmp(lhs); |
807ae6d7 JF |
479 | lhs = rhs; |
480 | rhs = tmp; | |
481 | } | |
482 | ||
483 | delete [] hist; | |
484 | ||
fd119f42 | 485 | const void **values(new const void *[count]); |
807ae6d7 | 486 | for (size_t i(0); i != count; ++i) |
fd119f42 JF |
487 | values[i] = [self objectAtIndex:lhs[i].index]; |
488 | CFArrayReplaceValues((CFMutableArrayRef) self, CFRangeMake(0, count), values, count); | |
489 | delete [] values; | |
807ae6d7 JF |
490 | |
491 | delete [] swap; | |
492 | } | |
493 | ||
494 | @end | |
eef4ccaf JF |
495 | /* }}} */ |
496 | /* Insertion Sort {{{ */ | |
497 | ||
df213583 JF |
498 | CFIndex SKBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
499 | const char *ptr = (const char *)list; | |
500 | while (0 < count) { | |
501 | CFIndex half = count / 2; | |
502 | const char *probe = ptr + elementSize * half; | |
503 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 504 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
df213583 JF |
505 | ptr = (cr < 0) ? ptr : probe + elementSize; |
506 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
507 | } | |
508 | return (ptr - (const char *)list) / elementSize; | |
509 | } | |
510 | ||
eef4ccaf JF |
511 | CFIndex CFBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
512 | const char *ptr = (const char *)list; | |
513 | while (0 < count) { | |
514 | CFIndex half = count / 2; | |
515 | const char *probe = ptr + elementSize * half; | |
516 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 517 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
eef4ccaf JF |
518 | ptr = (cr < 0) ? ptr : probe + elementSize; |
519 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
520 | } | |
521 | return (ptr - (const char *)list) / elementSize; | |
522 | } | |
523 | ||
524 | void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFComparatorFunction comparator, void *context) { | |
525 | if (range.length == 0) | |
526 | return; | |
527 | const void **values(new const void *[range.length]); | |
528 | CFArrayGetValues(array, range, values); | |
529 | ||
1539387a | 530 | #if HistogramInsertionSort > 0 |
df213583 JF |
531 | uint32_t total(0), *offsets(new uint32_t[range.length]); |
532 | #endif | |
533 | ||
eef4ccaf JF |
534 | for (CFIndex index(1); index != range.length; ++index) { |
535 | const void *value(values[index]); | |
df213583 JF |
536 | //CFIndex correct(SKBSearch_(&value, sizeof(const void *), values, index, comparator, context)); |
537 | CFIndex correct(index); | |
1539387a JF |
538 | while (comparator(value, values[correct - 1], context) == kCFCompareLessThan) { |
539 | #if HistogramInsertionSort > 1 | |
540 | NSLog(@"%@ < %@", value, values[correct - 1]); | |
541 | #endif | |
df213583 JF |
542 | if (--correct == 0) |
543 | break; | |
1539387a | 544 | } |
eef4ccaf | 545 | if (correct != index) { |
df213583 JF |
546 | size_t offset(index - correct); |
547 | #if HistogramInsertionSort | |
548 | total += offset; | |
549 | ++offsets[offset]; | |
550 | if (offset > 10) | |
551 | NSLog(@"Heavy Insertion Displacement: %u = %@", offset, value); | |
552 | #endif | |
553 | memmove(values + correct + 1, values + correct, sizeof(const void *) * offset); | |
eef4ccaf JF |
554 | values[correct] = value; |
555 | } | |
556 | } | |
557 | ||
558 | CFArrayReplaceValues(array, range, values, range.length); | |
559 | delete [] values; | |
df213583 | 560 | |
1539387a | 561 | #if HistogramInsertionSort > 0 |
df213583 JF |
562 | for (CFIndex index(0); index != range.length; ++index) |
563 | if (offsets[index] != 0) | |
564 | NSLog(@"Insertion Displacement [%u]: %u", index, offsets[index]); | |
565 | NSLog(@"Average Insertion Displacement: %f", double(total) / range.length); | |
566 | delete [] offsets; | |
567 | #endif | |
eef4ccaf JF |
568 | } |
569 | ||
807ae6d7 JF |
570 | /* }}} */ |
571 | ||
bf8476c8 JF |
572 | /* Apple Bug Fixes {{{ */ |
573 | @implementation UIWebDocumentView (Cydia) | |
574 | ||
575 | - (void) _setScrollerOffset:(CGPoint)offset { | |
576 | UIScroller *scroller([self _scroller]); | |
577 | ||
578 | CGSize size([scroller contentSize]); | |
579 | CGSize bounds([scroller bounds].size); | |
580 | ||
581 | CGPoint max; | |
582 | max.x = size.width - bounds.width; | |
583 | max.y = size.height - bounds.height; | |
584 | ||
585 | // wtf Apple?! | |
586 | if (max.x < 0) | |
587 | max.x = 0; | |
588 | if (max.y < 0) | |
589 | max.y = 0; | |
590 | ||
591 | offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; | |
592 | offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; | |
593 | ||
594 | [scroller setOffset:offset]; | |
595 | } | |
596 | ||
597 | @end | |
598 | /* }}} */ | |
599 | ||
3c216e5e JF |
600 | @implementation WebScriptObject (NSFastEnumeration) |
601 | ||
602 | - (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)objects count:(NSUInteger)count { | |
77801ff1 JF |
603 | size_t length([self count] - state->state); |
604 | if (length <= 0) | |
605 | return 0; | |
606 | else if (length > count) | |
607 | length = count; | |
608 | for (size_t i(0); i != length; ++i) | |
609 | objects[i] = [self objectAtIndex:state->state++]; | |
610 | state->itemsPtr = objects; | |
611 | state->mutationsPtr = (unsigned long *) self; | |
612 | return length; | |
613 | } | |
614 | ||
3c216e5e JF |
615 | @end |
616 | ||
680eb135 JF |
617 | NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { |
618 | size_t length([self length] - state->state); | |
619 | if (length <= 0) | |
620 | return 0; | |
621 | else if (length > count) | |
622 | length = count; | |
623 | for (size_t i(0); i != length; ++i) | |
624 | objects[i] = [self item:state->state++]; | |
625 | state->itemsPtr = objects; | |
626 | state->mutationsPtr = (unsigned long *) self; | |
627 | return length; | |
628 | } | |
629 | ||
bfc87a4d | 630 | /* Cydia NSString Additions {{{ */ |
2388b078 | 631 | @interface NSString (Cydia) |
808c6eb6 | 632 | + (NSString *) stringWithUTF8BytesNoCopy:(const char *)bytes length:(int)length; |
f79a4512 | 633 | + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length withZone:(NSZone *)zone inPool:(apr_pool_t *)pool; |
2388b078 | 634 | + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length; |
a54b1c10 | 635 | - (NSComparisonResult) compareByPath:(NSString *)other; |
2fc76a2d JF |
636 | - (NSString *) stringByCachingURLWithCurrentCDN; |
637 | - (NSString *) stringByAddingPercentEscapesIncludingReserved; | |
2388b078 JF |
638 | @end |
639 | ||
640 | @implementation NSString (Cydia) | |
641 | ||
808c6eb6 JF |
642 | + (NSString *) stringWithUTF8BytesNoCopy:(const char *)bytes length:(int)length { |
643 | return [[[NSString alloc] initWithBytesNoCopy:const_cast<char *>(bytes) length:length encoding:NSUTF8StringEncoding freeWhenDone:NO] autorelease]; | |
644 | } | |
645 | ||
f79a4512 JF |
646 | + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length withZone:(NSZone *)zone inPool:(apr_pool_t *)pool { |
647 | char *data(reinterpret_cast<char *>(apr_palloc(pool, length))); | |
648 | memcpy(data, bytes, length); | |
649 | return [[[NSString allocWithZone:zone] initWithBytesNoCopy:data length:length encoding:NSUTF8StringEncoding freeWhenDone:NO] autorelease]; | |
650 | } | |
651 | ||
2388b078 | 652 | + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length { |
2083b866 | 653 | return [[[NSString alloc] initWithBytes:bytes length:length encoding:NSUTF8StringEncoding] autorelease]; |
2388b078 JF |
654 | } |
655 | ||
a54b1c10 JF |
656 | - (NSComparisonResult) compareByPath:(NSString *)other { |
657 | NSString *prefix = [self commonPrefixWithString:other options:0]; | |
658 | size_t length = [prefix length]; | |
659 | ||
660 | NSRange lrange = NSMakeRange(length, [self length] - length); | |
661 | NSRange rrange = NSMakeRange(length, [other length] - length); | |
662 | ||
663 | lrange = [self rangeOfString:@"/" options:0 range:lrange]; | |
664 | rrange = [other rangeOfString:@"/" options:0 range:rrange]; | |
665 | ||
666 | NSComparisonResult value; | |
667 | ||
668 | if (lrange.location == NSNotFound && rrange.location == NSNotFound) | |
669 | value = NSOrderedSame; | |
670 | else if (lrange.location == NSNotFound) | |
671 | value = NSOrderedAscending; | |
672 | else if (rrange.location == NSNotFound) | |
673 | value = NSOrderedDescending; | |
674 | else | |
675 | value = NSOrderedSame; | |
676 | ||
677 | NSString *lpath = lrange.location == NSNotFound ? [self substringFromIndex:length] : | |
678 | [self substringWithRange:NSMakeRange(length, lrange.location - length)]; | |
679 | NSString *rpath = rrange.location == NSNotFound ? [other substringFromIndex:length] : | |
680 | [other substringWithRange:NSMakeRange(length, rrange.location - length)]; | |
681 | ||
682 | NSComparisonResult result = [lpath compare:rpath]; | |
683 | return result == NSOrderedSame ? value : result; | |
684 | } | |
685 | ||
2fc76a2d JF |
686 | - (NSString *) stringByCachingURLWithCurrentCDN { |
687 | return [self | |
65c27c56 JF |
688 | stringByReplacingOccurrencesOfString:@"://cydia.saurik.com/" |
689 | withString:@"://cache.cydia.saurik.com/" | |
2fc76a2d JF |
690 | ]; |
691 | } | |
692 | ||
693 | - (NSString *) stringByAddingPercentEscapesIncludingReserved { | |
694 | return [(id)CFURLCreateStringByAddingPercentEscapes( | |
bc11cf5b | 695 | kCFAllocatorDefault, |
2fc76a2d JF |
696 | (CFStringRef) self, |
697 | NULL, | |
698 | CFSTR(";/?:@&=+$,"), | |
699 | kCFStringEncodingUTF8 | |
700 | ) autorelease]; | |
701 | } | |
702 | ||
2388b078 | 703 | @end |
bfc87a4d | 704 | /* }}} */ |
2388b078 | 705 | |
bfc87a4d | 706 | /* C++ NSString Wrapper Cache {{{ */ |
8e8fca7f JF |
707 | static _finline CFStringRef CYStringCreate(const char *data, size_t size) { |
708 | return size == 0 ? NULL : | |
709 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingUTF8, NO, kCFAllocatorNull) ?: | |
710 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingISOLatin1, NO, kCFAllocatorNull); | |
711 | } | |
712 | ||
713 | static _finline CFStringRef CYStringCreate(const char *data) { | |
714 | return CYStringCreate(data, strlen(data)); | |
715 | } | |
716 | ||
f79a4512 JF |
717 | class CYString { |
718 | private: | |
719 | char *data_; | |
720 | size_t size_; | |
721 | CFStringRef cache_; | |
722 | ||
723 | _finline void clear_() { | |
18873623 | 724 | if (cache_ != NULL) { |
f79a4512 | 725 | CFRelease(cache_); |
18873623 JF |
726 | cache_ = NULL; |
727 | } | |
f79a4512 JF |
728 | } |
729 | ||
730 | public: | |
731 | _finline bool empty() const { | |
732 | return size_ == 0; | |
733 | } | |
734 | ||
735 | _finline size_t size() const { | |
736 | return size_; | |
737 | } | |
738 | ||
739 | _finline char *data() const { | |
740 | return data_; | |
741 | } | |
742 | ||
743 | _finline void clear() { | |
744 | size_ = 0; | |
745 | clear_(); | |
746 | } | |
747 | ||
748 | _finline CYString() : | |
749 | data_(0), | |
750 | size_(0), | |
18873623 | 751 | cache_(NULL) |
f79a4512 JF |
752 | { |
753 | } | |
754 | ||
755 | _finline ~CYString() { | |
756 | clear_(); | |
757 | } | |
758 | ||
759 | void operator =(const CYString &rhs) { | |
760 | data_ = rhs.data_; | |
761 | size_ = rhs.size_; | |
762 | ||
763 | if (rhs.cache_ == nil) | |
764 | cache_ = NULL; | |
765 | else | |
766 | cache_ = reinterpret_cast<CFStringRef>(CFRetain(rhs.cache_)); | |
767 | } | |
768 | ||
97814287 JF |
769 | void copy(apr_pool_t *pool) { |
770 | char *temp(reinterpret_cast<char *>(apr_palloc(pool, size_ + 1))); | |
771 | memcpy(temp, data_, size_); | |
772 | temp[size_] = '\0'; | |
773 | data_ = temp; | |
774 | } | |
775 | ||
f79a4512 JF |
776 | void set(apr_pool_t *pool, const char *data, size_t size) { |
777 | if (size == 0) | |
778 | clear(); | |
779 | else { | |
780 | clear_(); | |
781 | ||
97814287 | 782 | data_ = const_cast<char *>(data); |
f79a4512 | 783 | size_ = size; |
97814287 JF |
784 | |
785 | if (pool != NULL) | |
786 | copy(pool); | |
f79a4512 JF |
787 | } |
788 | } | |
789 | ||
790 | _finline void set(apr_pool_t *pool, const char *data) { | |
791 | set(pool, data, data == NULL ? 0 : strlen(data)); | |
792 | } | |
793 | ||
794 | _finline void set(apr_pool_t *pool, const std::string &rhs) { | |
795 | set(pool, rhs.data(), rhs.size()); | |
796 | } | |
797 | ||
798 | bool operator ==(const CYString &rhs) const { | |
799 | return size_ == rhs.size_ && memcmp(data_, rhs.data_, size_) == 0; | |
800 | } | |
801 | ||
8e8fca7f JF |
802 | _finline operator CFStringRef() { |
803 | if (cache_ == NULL) | |
804 | cache_ = CYStringCreate(data_, size_); | |
805 | return cache_; | |
df213583 JF |
806 | } |
807 | ||
808 | _finline operator id() { | |
809 | return (NSString *) static_cast<CFStringRef>(*this); | |
f79a4512 | 810 | } |
4c0ed943 JF |
811 | |
812 | _finline operator const char *() { | |
813 | return reinterpret_cast<const char *>(data_); | |
814 | } | |
f79a4512 | 815 | }; |
bfc87a4d JF |
816 | /* }}} */ |
817 | /* C++ NSString Algorithm Adapters {{{ */ | |
f79a4512 JF |
818 | extern "C" { |
819 | CF_EXPORT CFHashCode CFStringHashNSString(CFStringRef str); | |
820 | } | |
821 | ||
822 | struct NSStringMapHash : | |
823 | std::unary_function<NSString *, size_t> | |
824 | { | |
825 | _finline size_t operator ()(NSString *value) const { | |
826 | return CFStringHashNSString((CFStringRef) value); | |
827 | } | |
828 | }; | |
829 | ||
830 | struct NSStringMapLess : | |
831 | std::binary_function<NSString *, NSString *, bool> | |
832 | { | |
833 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
834 | return [lhs compare:rhs] == NSOrderedAscending; | |
835 | } | |
836 | }; | |
837 | ||
838 | struct NSStringMapEqual : | |
839 | std::binary_function<NSString *, NSString *, bool> | |
840 | { | |
841 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
842 | return CFStringCompare((CFStringRef) lhs, (CFStringRef) rhs, 0) == kCFCompareEqualTo; | |
843 | //CFEqual((CFTypeRef) lhs, (CFTypeRef) rhs); | |
844 | //[lhs isEqualToString:rhs]; | |
845 | } | |
846 | }; | |
bfc87a4d | 847 | /* }}} */ |
f79a4512 | 848 | |
b4d89997 JF |
849 | /* Perl-Compatible RegEx {{{ */ |
850 | class Pcre { | |
851 | private: | |
852 | pcre *code_; | |
853 | pcre_extra *study_; | |
854 | int capture_; | |
855 | int *matches_; | |
856 | const char *data_; | |
857 | ||
858 | public: | |
859 | Pcre(const char *regex) : | |
860 | study_(NULL) | |
861 | { | |
862 | const char *error; | |
863 | int offset; | |
864 | code_ = pcre_compile(regex, 0, &error, &offset, NULL); | |
865 | ||
866 | if (code_ == NULL) { | |
c390d3ab | 867 | lprintf("%d:%s\n", offset, error); |
b4d89997 JF |
868 | _assert(false); |
869 | } | |
870 | ||
871 | pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_); | |
872 | matches_ = new int[(capture_ + 1) * 3]; | |
873 | } | |
874 | ||
875 | ~Pcre() { | |
876 | pcre_free(code_); | |
877 | delete matches_; | |
878 | } | |
879 | ||
880 | NSString *operator [](size_t match) { | |
2388b078 | 881 | return [NSString stringWithUTF8Bytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2])]; |
b4d89997 JF |
882 | } |
883 | ||
8b29f8e6 JF |
884 | bool operator ()(NSString *data) { |
885 | // XXX: length is for characters, not for bytes | |
886 | return operator ()([data UTF8String], [data length]); | |
887 | } | |
888 | ||
b4d89997 JF |
889 | bool operator ()(const char *data, size_t size) { |
890 | data_ = data; | |
891 | return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0; | |
892 | } | |
893 | }; | |
894 | /* }}} */ | |
36bb2ca2 | 895 | /* Mime Addresses {{{ */ |
36bb2ca2 JF |
896 | @interface Address : NSObject { |
897 | NSString *name_; | |
6f1a15d9 | 898 | NSString *address_; |
36bb2ca2 JF |
899 | } |
900 | ||
901 | - (NSString *) name; | |
6f1a15d9 | 902 | - (NSString *) address; |
36bb2ca2 | 903 | |
dc63e78f JF |
904 | - (void) setAddress:(NSString *)address; |
905 | ||
36bb2ca2 JF |
906 | + (Address *) addressWithString:(NSString *)string; |
907 | - (Address *) initWithString:(NSString *)string; | |
908 | @end | |
909 | ||
910 | @implementation Address | |
911 | ||
912 | - (void) dealloc { | |
913 | [name_ release]; | |
6f1a15d9 JF |
914 | if (address_ != nil) |
915 | [address_ release]; | |
36bb2ca2 JF |
916 | [super dealloc]; |
917 | } | |
918 | ||
919 | - (NSString *) name { | |
920 | return name_; | |
921 | } | |
922 | ||
6f1a15d9 JF |
923 | - (NSString *) address { |
924 | return address_; | |
36bb2ca2 JF |
925 | } |
926 | ||
dc63e78f JF |
927 | - (void) setAddress:(NSString *)address { |
928 | if (address_ != nil) | |
929 | [address_ autorelease]; | |
930 | if (address == nil) | |
931 | address_ = nil; | |
932 | else | |
933 | address_ = [address retain]; | |
934 | } | |
935 | ||
36bb2ca2 JF |
936 | + (Address *) addressWithString:(NSString *)string { |
937 | return [[[Address alloc] initWithString:string] autorelease]; | |
938 | } | |
939 | ||
6f1a15d9 JF |
940 | + (NSArray *) _attributeKeys { |
941 | return [NSArray arrayWithObjects:@"address", @"name", nil]; | |
942 | } | |
943 | ||
944 | - (NSArray *) attributeKeys { | |
945 | return [[self class] _attributeKeys]; | |
946 | } | |
947 | ||
948 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
949 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
950 | } | |
951 | ||
36bb2ca2 JF |
952 | - (Address *) initWithString:(NSString *)string { |
953 | if ((self = [super init]) != nil) { | |
954 | const char *data = [string UTF8String]; | |
955 | size_t size = [string length]; | |
956 | ||
6f1a15d9 | 957 | static Pcre address_r("^\"?(.*)\"? <([^>]*)>$"); |
7b0ce2da | 958 | |
6f1a15d9 JF |
959 | if (address_r(data, size)) { |
960 | name_ = [address_r[1] retain]; | |
961 | address_ = [address_r[2] retain]; | |
36bb2ca2 | 962 | } else { |
affeffc7 | 963 | name_ = [string retain]; |
6f1a15d9 | 964 | address_ = nil; |
36bb2ca2 JF |
965 | } |
966 | } return self; | |
967 | } | |
968 | ||
969 | @end | |
970 | /* }}} */ | |
5f6bff8c | 971 | /* CoreGraphics Primitives {{{ */ |
02012733 | 972 | class CYColor { |
b4d89997 JF |
973 | private: |
974 | CGColorRef color_; | |
975 | ||
6a575b5e JF |
976 | static CGColorRef Create_(CGColorSpaceRef space, float red, float green, float blue, float alpha) { |
977 | CGFloat color[] = {red, green, blue, alpha}; | |
978 | return CGColorCreate(space, color); | |
979 | } | |
980 | ||
b4d89997 | 981 | public: |
02012733 | 982 | CYColor() : |
36bb2ca2 JF |
983 | color_(NULL) |
984 | { | |
985 | } | |
986 | ||
02012733 | 987 | CYColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) : |
6a575b5e | 988 | color_(Create_(space, red, green, blue, alpha)) |
36bb2ca2 JF |
989 | { |
990 | Set(space, red, green, blue, alpha); | |
991 | } | |
992 | ||
993 | void Clear() { | |
994 | if (color_ != NULL) | |
995 | CGColorRelease(color_); | |
b4d89997 JF |
996 | } |
997 | ||
02012733 | 998 | ~CYColor() { |
36bb2ca2 JF |
999 | Clear(); |
1000 | } | |
1001 | ||
1002 | void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) { | |
1003 | Clear(); | |
6a575b5e | 1004 | color_ = Create_(space, red, green, blue, alpha); |
b4d89997 JF |
1005 | } |
1006 | ||
1007 | operator CGColorRef() { | |
1008 | return color_; | |
1009 | } | |
1010 | }; | |
b4d89997 JF |
1011 | /* }}} */ |
1012 | ||
36bb2ca2 | 1013 | /* Random Global Variables {{{ */ |
3178d79b | 1014 | static const int PulseInterval_ = 50000; |
affeffc7 | 1015 | |
d791dce4 JF |
1016 | static int Finish_; |
1017 | static NSArray *Finishes_; | |
1018 | ||
affeffc7 | 1019 | #define SpringBoard_ "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist" |
22f8bed9 | 1020 | #define NotifyConfig_ "/etc/notify.conf" |
2a8d9add | 1021 | |
dc63e78f JF |
1022 | static bool Queuing_; |
1023 | ||
02012733 JF |
1024 | static CYColor Blue_; |
1025 | static CYColor Blueish_; | |
1026 | static CYColor Black_; | |
1027 | static CYColor Off_; | |
1028 | static CYColor White_; | |
1029 | static CYColor Gray_; | |
1030 | static CYColor Green_; | |
1031 | static CYColor Purple_; | |
1032 | static CYColor Purplish_; | |
3bd1c2a2 | 1033 | |
dc63e78f JF |
1034 | static UIColor *InstallingColor_; |
1035 | static UIColor *RemovingColor_; | |
36bb2ca2 | 1036 | |
7d2ac47f | 1037 | static NSString *App_; |
d73cede2 | 1038 | static NSString *Home_; |
d73cede2 | 1039 | |
2388b078 | 1040 | static BOOL Advanced_; |
a54b1c10 | 1041 | static BOOL Ignored_; |
2388b078 | 1042 | |
f641a0e5 JF |
1043 | static UIFont *Font12_; |
1044 | static UIFont *Font12Bold_; | |
1045 | static UIFont *Font14_; | |
1046 | static UIFont *Font18Bold_; | |
1047 | static UIFont *Font22Bold_; | |
1048 | ||
87f46a96 | 1049 | static const char *Machine_ = NULL; |
f4b1c57f | 1050 | static NSString *System_ = nil; |
56127854 JF |
1051 | static NSString *SerialNumber_ = nil; |
1052 | static NSString *ChipID_ = nil; | |
1053 | static NSString *Token_ = nil; | |
1054 | static NSString *UniqueID_ = nil; | |
567e3972 | 1055 | static NSString *PLMN_ = nil; |
56127854 JF |
1056 | static NSString *Build_ = nil; |
1057 | static NSString *Product_ = nil; | |
1058 | static NSString *Safari_ = nil; | |
2a8d9add | 1059 | |
d791dce4 JF |
1060 | static CFLocaleRef Locale_; |
1061 | static NSArray *Languages_; | |
1062 | static CGColorSpaceRef space_; | |
36bb2ca2 | 1063 | |
46dbfd32 | 1064 | static NSDictionary *SectionMap_; |
b4d89997 | 1065 | static NSMutableDictionary *Metadata_; |
7b0ce2da JF |
1066 | static _transient NSMutableDictionary *Settings_; |
1067 | static _transient NSString *Role_; | |
1068 | static _transient NSMutableDictionary *Packages_; | |
1069 | static _transient NSMutableDictionary *Sections_; | |
1070 | static _transient NSMutableDictionary *Sources_; | |
bbb879fb | 1071 | static bool Changed_; |
31bc18a7 | 1072 | static time_t now_; |
8da60fb7 | 1073 | |
01d93940 | 1074 | static bool IsWildcat_; |
36bb2ca2 | 1075 | /* }}} */ |
d791dce4 | 1076 | |
36bb2ca2 JF |
1077 | /* Display Helpers {{{ */ |
1078 | inline float Interpolate(float begin, float end, float fraction) { | |
1079 | return (end - begin) * fraction + begin; | |
1080 | } | |
2367a917 | 1081 | |
f79a4512 | 1082 | /* XXX: localize this! */ |
8fe19fc1 | 1083 | NSString *SizeString(double size) { |
affeffc7 JF |
1084 | bool negative = size < 0; |
1085 | if (negative) | |
1086 | size = -size; | |
1087 | ||
8fe19fc1 JF |
1088 | unsigned power = 0; |
1089 | while (size > 1024) { | |
1090 | size /= 1024; | |
1091 | ++power; | |
1092 | } | |
1093 | ||
1094 | static const char *powers_[] = {"B", "kB", "MB", "GB"}; | |
1095 | ||
86316a91 | 1096 | return [NSString stringWithFormat:@"%s%.1f %s", (negative ? "-" : ""), size, powers_[power]]; |
8fe19fc1 JF |
1097 | } |
1098 | ||
1c1dfc2d | 1099 | static _finline const char *StripVersion_(const char *version) { |
6a155117 | 1100 | const char *colon(strchr(version, ':')); |
673ad3c3 | 1101 | return colon == NULL ? version : colon + 1; |
6a155117 JF |
1102 | } |
1103 | ||
f79a4512 | 1104 | NSString *LocalizeSection(NSString *section) { |
b1ce61ec | 1105 | static Pcre title_r("^(.*?) \\((.*)\\)$"); |
9fcbca29 JF |
1106 | if (title_r(section)) { |
1107 | NSString *parent(title_r[1]); | |
1108 | NSString *child(title_r[2]); | |
1109 | ||
43f3d7f6 | 1110 | return [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), |
9fcbca29 JF |
1111 | LocalizeSection(parent), |
1112 | LocalizeSection(child) | |
b1ce61ec | 1113 | ]; |
9fcbca29 | 1114 | } |
b1ce61ec JF |
1115 | |
1116 | return [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
f79a4512 JF |
1117 | } |
1118 | ||
4cf4165e JF |
1119 | NSString *Simplify(NSString *title) { |
1120 | const char *data = [title UTF8String]; | |
1121 | size_t size = [title length]; | |
1122 | ||
7b0ce2da JF |
1123 | static Pcre square_r("^\\[(.*)\\]$"); |
1124 | if (square_r(data, size)) | |
1125 | return Simplify(square_r[1]); | |
1126 | ||
1127 | static Pcre paren_r("^\\((.*)\\)$"); | |
1128 | if (paren_r(data, size)) | |
1129 | return Simplify(paren_r[1]); | |
1130 | ||
b1ce61ec | 1131 | static Pcre title_r("^(.*?) \\((.*)\\)$"); |
4cf4165e | 1132 | if (title_r(data, size)) |
7b0ce2da JF |
1133 | return Simplify(title_r[1]); |
1134 | ||
1135 | return title; | |
4cf4165e | 1136 | } |
36bb2ca2 JF |
1137 | /* }}} */ |
1138 | ||
d791dce4 JF |
1139 | NSString *GetLastUpdate() { |
1140 | NSDate *update = [Metadata_ objectForKey:@"LastUpdate"]; | |
1141 | ||
1142 | if (update == nil) | |
1143 | return UCLocalize("NEVER_OR_UNKNOWN"); | |
1144 | ||
1145 | CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle); | |
1146 | CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update); | |
1147 | ||
1148 | CFRelease(formatter); | |
1149 | ||
1150 | return [(NSString *) formatted autorelease]; | |
1151 | } | |
1152 | ||
6d9712c4 | 1153 | bool isSectionVisible(NSString *section) { |
677b8415 JF |
1154 | NSDictionary *metadata([Sections_ objectForKey:section]); |
1155 | NSNumber *hidden(metadata == nil ? nil : [metadata objectForKey:@"Hidden"]); | |
6d9712c4 JF |
1156 | return hidden == nil || ![hidden boolValue]; |
1157 | } | |
1158 | ||
9cb0bff2 GP |
1159 | @class Cydia; |
1160 | ||
d36e83a3 | 1161 | /* Delegate Prototypes {{{ */ |
36bb2ca2 JF |
1162 | @class Package; |
1163 | @class Source; | |
1164 | ||
31f3cfff JF |
1165 | @interface NSObject (ProgressDelegate) |
1166 | @end | |
1167 | ||
36bb2ca2 | 1168 | @protocol ProgressDelegate |
670a0494 | 1169 | - (void) setProgressError:(NSString *)error withTitle:(NSString *)id; |
36bb2ca2 JF |
1170 | - (void) setProgressTitle:(NSString *)title; |
1171 | - (void) setProgressPercent:(float)percent; | |
baf80942 | 1172 | - (void) startProgress; |
36bb2ca2 | 1173 | - (void) addProgressOutput:(NSString *)output; |
baf80942 | 1174 | - (bool) isCancelling:(size_t)received; |
36bb2ca2 JF |
1175 | @end |
1176 | ||
8b29f8e6 | 1177 | @protocol ConfigurationDelegate |
5a09ae08 | 1178 | - (void) repairWithSelector:(SEL)selector; |
8b29f8e6 JF |
1179 | - (void) setConfigurationData:(NSString *)data; |
1180 | @end | |
1181 | ||
f6e13561 | 1182 | @class CYPackageController; |
f79a4512 | 1183 | |
36bb2ca2 | 1184 | @protocol CydiaDelegate |
54043703 JF |
1185 | - (void) retainNetworkActivityIndicator; |
1186 | - (void) releaseNetworkActivityIndicator; | |
f6e13561 | 1187 | - (void) setPackageController:(CYPackageController *)view; |
dc63e78f | 1188 | - (void) clearPackage:(Package *)package; |
36bb2ca2 | 1189 | - (void) installPackage:(Package *)package; |
77801ff1 | 1190 | - (void) installPackages:(NSArray *)packages; |
36bb2ca2 | 1191 | - (void) removePackage:(Package *)package; |
c21004b9 JF |
1192 | - (void) beginUpdate; |
1193 | - (BOOL) updating; | |
36bb2ca2 | 1194 | - (void) distUpgrade; |
fed0d010 | 1195 | - (void) loadData; |
6d9712c4 | 1196 | - (void) updateData; |
7b0ce2da | 1197 | - (void) syncData; |
21c6da4b | 1198 | - (void) showSettings; |
7b0ce2da | 1199 | - (UIProgressHUD *) addProgressHUD; |
d061f4ba | 1200 | - (void) removeProgressHUD:(UIProgressHUD *)hud; |
9b623dac | 1201 | - (CYViewController *) pageForPackage:(NSString *)name; |
9daa7f25 | 1202 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item; |
36bb2ca2 | 1203 | @end |
54043703 JF |
1204 | |
1205 | static id<CydiaDelegate> CydiaApp; | |
d36e83a3 | 1206 | /* }}} */ |
b4d89997 | 1207 | |
dc5812ec JF |
1208 | /* Status Delegation {{{ */ |
1209 | class Status : | |
1210 | public pkgAcquireStatus | |
1211 | { | |
1212 | private: | |
31f3cfff | 1213 | _transient NSObject<ProgressDelegate> *delegate_; |
dc5812ec JF |
1214 | |
1215 | public: | |
1216 | Status() : | |
1217 | delegate_(nil) | |
1218 | { | |
1219 | } | |
1220 | ||
1221 | void setDelegate(id delegate) { | |
1222 | delegate_ = delegate; | |
1223 | } | |
1224 | ||
670a0494 JF |
1225 | NSObject<ProgressDelegate> *getDelegate() const { |
1226 | return delegate_; | |
1227 | } | |
1228 | ||
dc5812ec JF |
1229 | virtual bool MediaChange(std::string media, std::string drive) { |
1230 | return false; | |
1231 | } | |
1232 | ||
1233 | virtual void IMSHit(pkgAcquire::ItemDesc &item) { | |
dc5812ec JF |
1234 | } |
1235 | ||
1236 | virtual void Fetch(pkgAcquire::ItemDesc &item) { | |
9487f027 | 1237 | //NSString *name([NSString stringWithUTF8String:item.ShortDesc.c_str()]); |
670a0494 | 1238 | [delegate_ setProgressTitle:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), [NSString stringWithUTF8String:item.ShortDesc.c_str()]]]; |
dc5812ec JF |
1239 | } |
1240 | ||
1241 | virtual void Done(pkgAcquire::ItemDesc &item) { | |
dc5812ec JF |
1242 | } |
1243 | ||
1244 | virtual void Fail(pkgAcquire::ItemDesc &item) { | |
1d80f6b9 JF |
1245 | if ( |
1246 | item.Owner->Status == pkgAcquire::Item::StatIdle || | |
1247 | item.Owner->Status == pkgAcquire::Item::StatDone | |
1248 | ) | |
1249 | return; | |
1250 | ||
affeffc7 JF |
1251 | std::string &error(item.Owner->ErrorText); |
1252 | if (error.empty()) | |
1253 | return; | |
1254 | ||
1255 | NSString *description([NSString stringWithUTF8String:item.Description.c_str()]); | |
1256 | NSArray *fields([description componentsSeparatedByString:@" "]); | |
1257 | NSString *source([fields count] == 0 ? nil : [fields objectAtIndex:0]); | |
1258 | ||
670a0494 | 1259 | [delegate_ performSelectorOnMainThread:@selector(_setProgressErrorPackage:) |
affeffc7 JF |
1260 | withObject:[NSArray arrayWithObjects: |
1261 | [NSString stringWithUTF8String:error.c_str()], | |
1262 | source, | |
1263 | nil] | |
31f3cfff JF |
1264 | waitUntilDone:YES |
1265 | ]; | |
dc5812ec JF |
1266 | } |
1267 | ||
1268 | virtual bool Pulse(pkgAcquire *Owner) { | |
2367a917 JF |
1269 | bool value = pkgAcquireStatus::Pulse(Owner); |
1270 | ||
1271 | float percent( | |
1272 | double(CurrentBytes + CurrentItems) / | |
1273 | double(TotalBytes + TotalItems) | |
1274 | ); | |
1275 | ||
36bb2ca2 | 1276 | [delegate_ setProgressPercent:percent]; |
baf80942 | 1277 | return [delegate_ isCancelling:CurrentBytes] ? false : value; |
dc5812ec JF |
1278 | } |
1279 | ||
1280 | virtual void Start() { | |
baf80942 | 1281 | [delegate_ startProgress]; |
dc5812ec JF |
1282 | } |
1283 | ||
1284 | virtual void Stop() { | |
dc5812ec JF |
1285 | } |
1286 | }; | |
1287 | /* }}} */ | |
1288 | /* Progress Delegation {{{ */ | |
1289 | class Progress : | |
1290 | public OpProgress | |
1291 | { | |
1292 | private: | |
36bb2ca2 | 1293 | _transient id<ProgressDelegate> delegate_; |
bfc87a4d | 1294 | float percent_; |
dc5812ec JF |
1295 | |
1296 | protected: | |
1297 | virtual void Update() { | |
670a0494 JF |
1298 | /*if (abs(Percent - percent_) > 2) |
1299 | //NSLog(@"%s:%s:%f", Op.c_str(), SubOp.c_str(), Percent); | |
bfc87a4d | 1300 | percent_ = Percent; |
670a0494 | 1301 | }*/ |
bfc87a4d | 1302 | |
8eed3ec6 JF |
1303 | /*[delegate_ setProgressTitle:[NSString stringWithUTF8String:Op.c_str()]]; |
1304 | [delegate_ setProgressPercent:(Percent / 100)];*/ | |
dc5812ec JF |
1305 | } |
1306 | ||
1307 | public: | |
1308 | Progress() : | |
bfc87a4d JF |
1309 | delegate_(nil), |
1310 | percent_(0) | |
dc5812ec JF |
1311 | { |
1312 | } | |
1313 | ||
1314 | void setDelegate(id delegate) { | |
1315 | delegate_ = delegate; | |
1316 | } | |
1317 | ||
670a0494 JF |
1318 | id getDelegate() const { |
1319 | return delegate_; | |
1320 | } | |
1321 | ||
dc5812ec | 1322 | virtual void Done() { |
670a0494 | 1323 | //NSLog(@"DONE"); |
8eed3ec6 | 1324 | //[delegate_ setProgressPercent:1]; |
dc5812ec JF |
1325 | } |
1326 | }; | |
1327 | /* }}} */ | |
1328 | ||
36bb2ca2 | 1329 | /* Database Interface {{{ */ |
68d927e2 JF |
1330 | typedef std::map< unsigned long, _H<Source> > SourceMap; |
1331 | ||
36bb2ca2 | 1332 | @interface Database : NSObject { |
f79a4512 JF |
1333 | NSZone *zone_; |
1334 | apr_pool_t *pool_; | |
1335 | ||
a1440b10 JF |
1336 | unsigned era_; |
1337 | ||
36bb2ca2 | 1338 | pkgCacheFile cache_; |
5a09ae08 | 1339 | pkgDepCache::Policy *policy_; |
36bb2ca2 JF |
1340 | pkgRecords *records_; |
1341 | pkgProblemResolver *resolver_; | |
1342 | pkgAcquire *fetcher_; | |
1343 | FileFd *lock_; | |
1344 | SPtr<pkgPackageManager> manager_; | |
1345 | pkgSourceList *list_; | |
5f6bff8c | 1346 | |
68d927e2 | 1347 | SourceMap sources_; |
ab3f6a01 | 1348 | CFMutableArrayRef deadSources_; |
077e9d90 | 1349 | CFMutableArrayRef packages_; |
b4d89997 | 1350 | |
31f3cfff | 1351 | _transient NSObject<ConfigurationDelegate, ProgressDelegate> *delegate_; |
36bb2ca2 JF |
1352 | Status status_; |
1353 | Progress progress_; | |
8b29f8e6 | 1354 | |
77fcccaf | 1355 | int cydiafd_; |
36bb2ca2 | 1356 | int statusfd_; |
8b29f8e6 | 1357 | FILE *input_; |
fe33a23e JF |
1358 | |
1359 | std::map<const char *, _H<NSString> > sections_; | |
dc5812ec JF |
1360 | } |
1361 | ||
770f2a8e | 1362 | + (Database *) sharedInstance; |
a1440b10 | 1363 | - (unsigned) era; |
770f2a8e | 1364 | |
77fcccaf | 1365 | - (void) _readCydia:(NSNumber *)fd; |
36bb2ca2 JF |
1366 | - (void) _readStatus:(NSNumber *)fd; |
1367 | - (void) _readOutput:(NSNumber *)fd; | |
2367a917 | 1368 | |
8b29f8e6 JF |
1369 | - (FILE *) input; |
1370 | ||
36bb2ca2 | 1371 | - (Package *) packageWithName:(NSString *)name; |
dc5812ec | 1372 | |
36bb2ca2 | 1373 | - (pkgCacheFile &) cache; |
5a09ae08 | 1374 | - (pkgDepCache::Policy *) policy; |
36bb2ca2 JF |
1375 | - (pkgRecords *) records; |
1376 | - (pkgProblemResolver *) resolver; | |
1377 | - (pkgAcquire &) fetcher; | |
a3328c28 | 1378 | - (pkgSourceList &) list; |
36bb2ca2 | 1379 | - (NSArray *) packages; |
7b0ce2da | 1380 | - (NSArray *) sources; |
36bb2ca2 JF |
1381 | - (void) reloadData; |
1382 | ||
5a09ae08 | 1383 | - (void) configure; |
670a0494 | 1384 | - (bool) prepare; |
36bb2ca2 | 1385 | - (void) perform; |
670a0494 | 1386 | - (bool) upgrade; |
36bb2ca2 JF |
1387 | - (void) update; |
1388 | ||
670a0494 | 1389 | - (void) updateWithStatus:(Status &)status; |
36bb2ca2 JF |
1390 | |
1391 | - (void) setDelegate:(id)delegate; | |
7376b55c | 1392 | - (Source *) getSource:(pkgCache::PkgFileIterator)file; |
fe33a23e JF |
1393 | |
1394 | - (NSString *) mappedSectionForPointer:(const char *)pointer; | |
1395 | ||
670a0494 JF |
1396 | @end |
1397 | /* }}} */ | |
1398 | /* Delegate Helpers {{{ */ | |
c21004b9 | 1399 | @implementation NSObject (ProgressDelegate) |
670a0494 JF |
1400 | |
1401 | - (void) _setProgressErrorPackage:(NSArray *)args { | |
1402 | [self performSelector:@selector(setProgressError:forPackage:) | |
1403 | withObject:[args objectAtIndex:0] | |
1404 | withObject:([args count] == 1 ? nil : [args objectAtIndex:1]) | |
1405 | ]; | |
1406 | } | |
1407 | ||
1408 | - (void) _setProgressErrorTitle:(NSArray *)args { | |
1409 | [self performSelector:@selector(setProgressError:withTitle:) | |
1410 | withObject:[args objectAtIndex:0] | |
1411 | withObject:([args count] == 1 ? nil : [args objectAtIndex:1]) | |
1412 | ]; | |
1413 | } | |
1414 | ||
1415 | - (void) _setProgressError:(NSString *)error withTitle:(NSString *)title { | |
1416 | [self performSelectorOnMainThread:@selector(_setProgressErrorTitle:) | |
1417 | withObject:[NSArray arrayWithObjects:error, title, nil] | |
1418 | waitUntilDone:YES | |
1419 | ]; | |
1420 | } | |
1421 | ||
1422 | - (void) setProgressError:(NSString *)error forPackage:(NSString *)id { | |
1423 | Package *package = id == nil ? nil : [[Database sharedInstance] packageWithName:id]; | |
6067f1b8 | 1424 | |
eb30da80 GP |
1425 | [self performSelector:@selector(setProgressError:withTitle:) |
1426 | withObject:error | |
1427 | withObject:(package == nil ? id : [package name]) | |
1428 | ]; | |
670a0494 JF |
1429 | } |
1430 | ||
dc5812ec | 1431 | @end |
36bb2ca2 | 1432 | /* }}} */ |
dc5812ec | 1433 | |
94b0b3e5 JF |
1434 | // Cytore Definitions {{{ |
1435 | struct PackageValue : | |
1436 | Cytore::Block | |
1437 | { | |
94b0b3e5 JF |
1438 | Cytore::Offset<PackageValue> next_; |
1439 | ||
1440 | uint32_t index_ : 23; | |
1441 | uint32_t subscribed_ : 1; | |
1442 | uint32_t : 8; | |
1443 | ||
1444 | int32_t first_; | |
1445 | int32_t last_; | |
1446 | ||
1447 | uint16_t vhash_; | |
1448 | uint16_t nhash_; | |
1449 | ||
1450 | char version_[8]; | |
1451 | char name_[]; | |
1452 | }; | |
1453 | ||
1454 | struct MetaValue : | |
1455 | Cytore::Block | |
1456 | { | |
9f357d11 | 1457 | uint32_t active_; |
94b0b3e5 JF |
1458 | Cytore::Offset<PackageValue> packages_[1 << 16]; |
1459 | }; | |
1460 | ||
1461 | static Cytore::File<MetaValue> MetaFile_; | |
1462 | // }}} | |
1463 | // Cytore Helper Functions {{{ | |
bb6bb6d6 | 1464 | static PackageValue *PackageFind(const char *name, size_t length) { |
94b0b3e5 JF |
1465 | SplitHash nhash = { hashlittle(name, length) }; |
1466 | ||
1467 | PackageValue *metadata; | |
1468 | ||
1469 | Cytore::Offset<PackageValue> *offset(&MetaFile_->packages_[nhash.u16[0]]); | |
1470 | offset: if (offset->IsNull()) { | |
1471 | *offset = MetaFile_.New<PackageValue>(length + 1); | |
1472 | metadata = &MetaFile_.Get(*offset); | |
1473 | ||
1474 | memcpy(metadata->name_, name, length + 1); | |
1475 | metadata->nhash_ = nhash.u16[1]; | |
1476 | } else { | |
1477 | metadata = &MetaFile_.Get(*offset); | |
1478 | ||
1479 | if (metadata->nhash_ != nhash.u16[1] || strncmp(metadata->name_, name, length + 1) != 0) { | |
1480 | offset = &metadata->next_; | |
1481 | goto offset; | |
1482 | } | |
1483 | } | |
1484 | ||
94b0b3e5 JF |
1485 | return metadata; |
1486 | } | |
1487 | ||
1488 | static void PackageImport(const void *key, const void *value, void *context) { | |
1489 | char buffer[1024]; | |
1490 | if (!CFStringGetCString((CFStringRef) key, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { | |
1491 | NSLog(@"failed to import package %@", key); | |
1492 | return; | |
1493 | } | |
1494 | ||
1495 | PackageValue *metadata(PackageFind(buffer, strlen(buffer))); | |
1496 | NSDictionary *package((NSDictionary *) value); | |
1497 | ||
1498 | if (NSNumber *subscribed = [package objectForKey:@"IsSubscribed"]) | |
2f856365 | 1499 | if ([subscribed boolValue] && !metadata->subscribed_) |
94b0b3e5 JF |
1500 | metadata->subscribed_ = true; |
1501 | ||
1502 | if (NSDate *date = [package objectForKey:@"FirstSeen"]) { | |
1503 | time_t time([date timeIntervalSince1970]); | |
1504 | if (metadata->first_ > time || metadata->first_ == 0) | |
1505 | metadata->first_ = time; | |
1506 | } | |
1507 | ||
2f856365 JF |
1508 | NSDate *date([package objectForKey:@"LastSeen"]); |
1509 | NSString *version([package objectForKey:@"LastVersion"]); | |
5a933937 | 1510 | |
2f856365 | 1511 | if (date != nil && version != nil) { |
94b0b3e5 | 1512 | time_t time([date timeIntervalSince1970]); |
2f856365 | 1513 | if (metadata->last_ < time || metadata->last_ == 0) |
5a933937 JF |
1514 | if (CFStringGetCString((CFStringRef) version, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { |
1515 | size_t length(strlen(buffer)); | |
1516 | uint16_t vhash(hashlittle(buffer, length)); | |
94b0b3e5 | 1517 | |
5a933937 JF |
1518 | size_t capped(std::min<size_t>(8, length)); |
1519 | char *latest(buffer + length - capped); | |
1520 | ||
1521 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); | |
1522 | metadata->vhash_ = vhash; | |
2f856365 JF |
1523 | |
1524 | metadata->last_ = time; | |
5a933937 | 1525 | } |
2f856365 | 1526 | } |
94b0b3e5 JF |
1527 | } |
1528 | // }}} | |
1529 | ||
36bb2ca2 JF |
1530 | /* Source Class {{{ */ |
1531 | @interface Source : NSObject { | |
6204f56a | 1532 | CYString depiction_; |
f9f6d9e8 JF |
1533 | CYString description_; |
1534 | CYString label_; | |
1535 | CYString origin_; | |
1536 | CYString support_; | |
dc5812ec | 1537 | |
f9f6d9e8 JF |
1538 | CYString uri_; |
1539 | CYString distribution_; | |
1540 | CYString type_; | |
1541 | CYString version_; | |
2367a917 | 1542 | |
f9f6d9e8 | 1543 | NSString *host_; |
10bf7bbb | 1544 | NSString *authority_; |
f9f6d9e8 JF |
1545 | |
1546 | CYString defaultIcon_; | |
dc5812ec | 1547 | |
7b0ce2da | 1548 | NSDictionary *record_; |
36bb2ca2 | 1549 | BOOL trusted_; |
dc5812ec JF |
1550 | } |
1551 | ||
f9f6d9e8 | 1552 | - (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool; |
dc5812ec | 1553 | |
7b0ce2da JF |
1554 | - (NSComparisonResult) compareByNameAndType:(Source *)source; |
1555 | ||
6204f56a | 1556 | - (NSString *) depictionForPackage:(NSString *)package; |
dc63e78f JF |
1557 | - (NSString *) supportForPackage:(NSString *)package; |
1558 | ||
7b0ce2da | 1559 | - (NSDictionary *) record; |
36bb2ca2 | 1560 | - (BOOL) trusted; |
dc5812ec | 1561 | |
36bb2ca2 JF |
1562 | - (NSString *) uri; |
1563 | - (NSString *) distribution; | |
1564 | - (NSString *) type; | |
7b0ce2da JF |
1565 | - (NSString *) key; |
1566 | - (NSString *) host; | |
36bb2ca2 | 1567 | |
7b0ce2da | 1568 | - (NSString *) name; |
36bb2ca2 JF |
1569 | - (NSString *) description; |
1570 | - (NSString *) label; | |
1571 | - (NSString *) origin; | |
1572 | - (NSString *) version; | |
dc5812ec | 1573 | |
36bb2ca2 | 1574 | - (NSString *) defaultIcon; |
7b0ce2da | 1575 | |
dc5812ec | 1576 | @end |
dc5812ec | 1577 | |
36bb2ca2 | 1578 | @implementation Source |
dc5812ec | 1579 | |
a3328c28 | 1580 | - (void) _clear { |
f9f6d9e8 JF |
1581 | uri_.clear(); |
1582 | distribution_.clear(); | |
1583 | type_.clear(); | |
a3328c28 | 1584 | |
f9f6d9e8 JF |
1585 | description_.clear(); |
1586 | label_.clear(); | |
1587 | origin_.clear(); | |
6204f56a | 1588 | depiction_.clear(); |
f9f6d9e8 JF |
1589 | support_.clear(); |
1590 | version_.clear(); | |
1591 | defaultIcon_.clear(); | |
1592 | ||
1593 | if (record_ != nil) { | |
1594 | [record_ release]; | |
1595 | record_ = nil; | |
1596 | } | |
1597 | ||
1598 | if (host_ != nil) { | |
1599 | [host_ release]; | |
1600 | host_ = nil; | |
1601 | } | |
7623f855 JF |
1602 | |
1603 | if (authority_ != nil) { | |
1604 | [authority_ release]; | |
1605 | authority_ = nil; | |
1606 | } | |
a3328c28 | 1607 | } |
dc5812ec | 1608 | |
a3328c28 | 1609 | - (void) dealloc { |
3931b718 | 1610 | // XXX: this is a very inefficient way to call these deconstructors |
a3328c28 | 1611 | [self _clear]; |
36bb2ca2 | 1612 | [super dealloc]; |
dc5812ec JF |
1613 | } |
1614 | ||
6f1a15d9 JF |
1615 | + (NSArray *) _attributeKeys { |
1616 | return [NSArray arrayWithObjects:@"description", @"distribution", @"host", @"key", @"label", @"name", @"origin", @"trusted", @"type", @"uri", @"version", nil]; | |
1617 | } | |
1618 | ||
1619 | - (NSArray *) attributeKeys { | |
1620 | return [[self class] _attributeKeys]; | |
1621 | } | |
1622 | ||
1623 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1624 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1625 | } | |
1626 | ||
f9f6d9e8 | 1627 | - (void) setMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { |
a3328c28 | 1628 | [self _clear]; |
dc5812ec | 1629 | |
a3328c28 JF |
1630 | trusted_ = index->IsTrusted(); |
1631 | ||
f9f6d9e8 JF |
1632 | uri_.set(pool, index->GetURI()); |
1633 | distribution_.set(pool, index->GetDist()); | |
1634 | type_.set(pool, index->GetType()); | |
a3328c28 JF |
1635 | |
1636 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index)); | |
1637 | if (dindex != NULL) { | |
18873623 JF |
1638 | FileFd fd; |
1639 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) | |
1640 | _error->Discard(); | |
1641 | else { | |
1642 | pkgTagFile tags(&fd); | |
f9f6d9e8 | 1643 | |
18873623 JF |
1644 | pkgTagSection section; |
1645 | tags.Step(section); | |
a3328c28 | 1646 | |
18873623 JF |
1647 | struct { |
1648 | const char *name_; | |
1649 | CYString *value_; | |
1650 | } names[] = { | |
1651 | {"default-icon", &defaultIcon_}, | |
6204f56a | 1652 | {"depiction", &depiction_}, |
18873623 JF |
1653 | {"description", &description_}, |
1654 | {"label", &label_}, | |
1655 | {"origin", &origin_}, | |
1656 | {"support", &support_}, | |
1657 | {"version", &version_}, | |
1658 | }; | |
f9f6d9e8 | 1659 | |
18873623 JF |
1660 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { |
1661 | const char *start, *end; | |
1662 | ||
1663 | if (section.Find(names[i].name_, start, end)) { | |
1664 | CYString &value(*names[i].value_); | |
1665 | value.set(pool, start, end - start); | |
1666 | } | |
f9f6d9e8 | 1667 | } |
36bb2ca2 | 1668 | } |
a3328c28 | 1669 | } |
7b0ce2da | 1670 | |
a3328c28 JF |
1671 | record_ = [Sources_ objectForKey:[self key]]; |
1672 | if (record_ != nil) | |
1673 | record_ = [record_ retain]; | |
f9f6d9e8 | 1674 | |
7623f855 JF |
1675 | NSURL *url([NSURL URLWithString:uri_]); |
1676 | ||
1677 | host_ = [url host]; | |
1678 | if (host_ != nil) | |
1679 | host_ = [[host_ lowercaseString] retain]; | |
1680 | ||
1681 | if (host_ != nil) | |
a6884de3 | 1682 | authority_ = host_; |
7623f855 JF |
1683 | else |
1684 | authority_ = [url path]; | |
a6884de3 JF |
1685 | |
1686 | if (authority_ != nil) | |
1687 | authority_ = [authority_ retain]; | |
a3328c28 JF |
1688 | } |
1689 | ||
f9f6d9e8 | 1690 | - (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { |
a3328c28 | 1691 | if ((self = [super init]) != nil) { |
f9f6d9e8 | 1692 | [self setMetaIndex:index inPool:pool]; |
36bb2ca2 | 1693 | } return self; |
2367a917 | 1694 | } |
dc5812ec | 1695 | |
7b0ce2da JF |
1696 | - (NSComparisonResult) compareByNameAndType:(Source *)source { |
1697 | NSDictionary *lhr = [self record]; | |
1698 | NSDictionary *rhr = [source record]; | |
1699 | ||
1700 | if (lhr != rhr) | |
1701 | return lhr == nil ? NSOrderedDescending : NSOrderedAscending; | |
1702 | ||
1703 | NSString *lhs = [self name]; | |
1704 | NSString *rhs = [source name]; | |
1705 | ||
1706 | if ([lhs length] != 0 && [rhs length] != 0) { | |
1707 | unichar lhc = [lhs characterAtIndex:0]; | |
1708 | unichar rhc = [rhs characterAtIndex:0]; | |
1709 | ||
1710 | if (isalpha(lhc) && !isalpha(rhc)) | |
1711 | return NSOrderedAscending; | |
1712 | else if (!isalpha(lhc) && isalpha(rhc)) | |
1713 | return NSOrderedDescending; | |
1714 | } | |
1715 | ||
68f1828e | 1716 | return [lhs compare:rhs options:LaxCompareOptions_]; |
7b0ce2da JF |
1717 | } |
1718 | ||
6204f56a | 1719 | - (NSString *) depictionForPackage:(NSString *)package { |
89b0ea4a | 1720 | return depiction_.empty() ? nil : [static_cast<id>(depiction_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
6204f56a JF |
1721 | } |
1722 | ||
dc63e78f | 1723 | - (NSString *) supportForPackage:(NSString *)package { |
89b0ea4a | 1724 | return support_.empty() ? nil : [static_cast<id>(support_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
dc63e78f JF |
1725 | } |
1726 | ||
7b0ce2da JF |
1727 | - (NSDictionary *) record { |
1728 | return record_; | |
1729 | } | |
1730 | ||
36bb2ca2 JF |
1731 | - (BOOL) trusted { |
1732 | return trusted_; | |
1733 | } | |
3178d79b | 1734 | |
36bb2ca2 JF |
1735 | - (NSString *) uri { |
1736 | return uri_; | |
1737 | } | |
ec97ef06 | 1738 | |
36bb2ca2 JF |
1739 | - (NSString *) distribution { |
1740 | return distribution_; | |
1741 | } | |
dc5812ec | 1742 | |
36bb2ca2 JF |
1743 | - (NSString *) type { |
1744 | return type_; | |
dc5812ec JF |
1745 | } |
1746 | ||
7b0ce2da | 1747 | - (NSString *) key { |
f9f6d9e8 | 1748 | return [NSString stringWithFormat:@"%@:%@:%@", (NSString *) type_, (NSString *) uri_, (NSString *) distribution_]; |
7b0ce2da JF |
1749 | } |
1750 | ||
1751 | - (NSString *) host { | |
f9f6d9e8 | 1752 | return host_; |
7b0ce2da JF |
1753 | } |
1754 | ||
1755 | - (NSString *) name { | |
7623f855 | 1756 | return origin_.empty() ? authority_ : origin_; |
7b0ce2da JF |
1757 | } |
1758 | ||
36bb2ca2 JF |
1759 | - (NSString *) description { |
1760 | return description_; | |
1761 | } | |
b4d89997 | 1762 | |
36bb2ca2 | 1763 | - (NSString *) label { |
7623f855 | 1764 | return label_.empty() ? authority_ : label_; |
36bb2ca2 | 1765 | } |
3178d79b | 1766 | |
36bb2ca2 JF |
1767 | - (NSString *) origin { |
1768 | return origin_; | |
1769 | } | |
3178d79b | 1770 | |
36bb2ca2 JF |
1771 | - (NSString *) version { |
1772 | return version_; | |
1773 | } | |
2367a917 | 1774 | |
36bb2ca2 JF |
1775 | - (NSString *) defaultIcon { |
1776 | return defaultIcon_; | |
1777 | } | |
2367a917 | 1778 | |
2388b078 JF |
1779 | @end |
1780 | /* }}} */ | |
1781 | /* Relationship Class {{{ */ | |
1782 | @interface Relationship : NSObject { | |
1783 | NSString *type_; | |
1784 | NSString *id_; | |
1785 | } | |
1786 | ||
1787 | - (NSString *) type; | |
1788 | - (NSString *) id; | |
1789 | - (NSString *) name; | |
1790 | ||
1791 | @end | |
1792 | ||
1793 | @implementation Relationship | |
1794 | ||
1795 | - (void) dealloc { | |
1796 | [type_ release]; | |
1797 | [id_ release]; | |
1798 | [super dealloc]; | |
1799 | } | |
1800 | ||
1801 | - (NSString *) type { | |
1802 | return type_; | |
1803 | } | |
1804 | ||
1805 | - (NSString *) id { | |
1806 | return id_; | |
1807 | } | |
1808 | ||
1809 | - (NSString *) name { | |
1810 | _assert(false); | |
1811 | return nil; | |
1812 | } | |
1813 | ||
dc5812ec | 1814 | @end |
b4d89997 | 1815 | /* }}} */ |
36bb2ca2 | 1816 | /* Package Class {{{ */ |
12016ee5 JF |
1817 | struct ParsedPackage { |
1818 | CYString tagline_; | |
1819 | ||
1820 | CYString icon_; | |
1821 | ||
1822 | CYString depiction_; | |
1823 | CYString homepage_; | |
1824 | ||
1825 | CYString sponsor_; | |
1826 | CYString author_; | |
1827 | ||
1828 | CYString bugs_; | |
1829 | CYString support_; | |
1830 | }; | |
1831 | ||
36bb2ca2 | 1832 | @interface Package : NSObject { |
97a3d89e JF |
1833 | uint32_t era_ : 26; |
1834 | uint32_t role_ : 3; | |
aab28f8b JF |
1835 | uint32_t essential_ : 1; |
1836 | uint32_t obsolete_ : 1; | |
1837 | uint32_t ignored_ : 1; | |
1838 | ||
68d927e2 | 1839 | apr_pool_t *pool_; |
a1440b10 | 1840 | |
aab28f8b JF |
1841 | _transient Database *database_; |
1842 | ||
7376b55c | 1843 | pkgCache::VerIterator version_; |
36bb2ca2 | 1844 | pkgCache::PkgIterator iterator_; |
36bb2ca2 | 1845 | pkgCache::VerFileIterator file_; |
bbb879fb | 1846 | |
aab28f8b JF |
1847 | CYString id_; |
1848 | CYString name_; | |
807ae6d7 | 1849 | |
4c0ed943 | 1850 | CYString latest_; |
6a155117 | 1851 | CYString installed_; |
dc5812ec | 1852 | |
fe33a23e | 1853 | const char *section_; |
aab28f8b | 1854 | _transient NSString *section$_; |
f79a4512 | 1855 | |
aab28f8b | 1856 | Source *source_; |
2388b078 | 1857 | |
bb6bb6d6 | 1858 | PackageValue *metadata_; |
aab28f8b | 1859 | ParsedPackage *parsed_; |
94b0b3e5 | 1860 | |
aab28f8b | 1861 | NSMutableArray *tags_; |
b4d89997 JF |
1862 | } |
1863 | ||
7376b55c | 1864 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; |
f79a4512 | 1865 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; |
b4d89997 | 1866 | |
2388b078 | 1867 | - (pkgCache::PkgIterator) iterator; |
68d927e2 | 1868 | - (void) parse; |
2388b078 | 1869 | |
36bb2ca2 | 1870 | - (NSString *) section; |
dec6029f JF |
1871 | - (NSString *) simpleSection; |
1872 | ||
f79a4512 JF |
1873 | - (NSString *) longSection; |
1874 | - (NSString *) shortSection; | |
1875 | ||
a3328c28 JF |
1876 | - (NSString *) uri; |
1877 | ||
36bb2ca2 JF |
1878 | - (Address *) maintainer; |
1879 | - (size_t) size; | |
eef4ccaf JF |
1880 | - (NSString *) longDescription; |
1881 | - (NSString *) shortDescription; | |
808c6eb6 | 1882 | - (unichar) index; |
dc5812ec | 1883 | |
94b0b3e5 | 1884 | - (PackageValue *) metadata; |
31bc18a7 | 1885 | - (time_t) seen; |
94b0b3e5 JF |
1886 | |
1887 | - (bool) subscribed; | |
1888 | - (bool) setSubscribed:(bool)subscribed; | |
1889 | ||
807ae6d7 | 1890 | - (BOOL) ignored; |
9e98e020 | 1891 | |
36bb2ca2 JF |
1892 | - (NSString *) latest; |
1893 | - (NSString *) installed; | |
6a155117 | 1894 | - (BOOL) uninstalled; |
5a09ae08 JF |
1895 | |
1896 | - (BOOL) valid; | |
31f3cfff | 1897 | - (BOOL) upgradableAndEssential:(BOOL)essential; |
36bb2ca2 JF |
1898 | - (BOOL) essential; |
1899 | - (BOOL) broken; | |
7d2ac47f | 1900 | - (BOOL) unfiltered; |
6d9712c4 | 1901 | - (BOOL) visible; |
b4d89997 | 1902 | |
9bedffaa JF |
1903 | - (BOOL) half; |
1904 | - (BOOL) halfConfigured; | |
1905 | - (BOOL) halfInstalled; | |
1906 | - (BOOL) hasMode; | |
1907 | - (NSString *) mode; | |
1908 | ||
36bb2ca2 JF |
1909 | - (NSString *) id; |
1910 | - (NSString *) name; | |
770f2a8e | 1911 | - (UIImage *) icon; |
6f1a15d9 | 1912 | - (NSString *) homepage; |
25a2158d | 1913 | - (NSString *) depiction; |
77fcccaf | 1914 | - (Address *) author; |
b4d89997 | 1915 | |
dc63e78f JF |
1916 | - (NSString *) support; |
1917 | ||
affeffc7 | 1918 | - (NSArray *) files; |
affeffc7 JF |
1919 | - (NSArray *) warnings; |
1920 | - (NSArray *) applications; | |
2388b078 | 1921 | |
36bb2ca2 | 1922 | - (Source *) source; |
b4d89997 | 1923 | |
36bb2ca2 | 1924 | - (BOOL) matches:(NSString *)text; |
b4d89997 | 1925 | |
7b0ce2da | 1926 | - (bool) hasSupportingRole; |
6d9712c4 | 1927 | - (BOOL) hasTag:(NSString *)tag; |
c390d3ab | 1928 | - (NSString *) primaryPurpose; |
770f2a8e | 1929 | - (NSArray *) purposes; |
3bd1c2a2 | 1930 | - (bool) isCommercial; |
6d9712c4 | 1931 | |
cd95e390 JF |
1932 | - (void) setIndex:(size_t)index; |
1933 | ||
df213583 JF |
1934 | - (CYString &) cyname; |
1935 | ||
f79a4512 | 1936 | - (uint32_t) compareBySection:(NSArray *)sections; |
807ae6d7 | 1937 | |
36bb2ca2 JF |
1938 | - (void) install; |
1939 | - (void) remove; | |
06aa974d | 1940 | |
0a3b45ef | 1941 | - (bool) isUnfilteredAndSearchedForBy:(NSString *)search; |
01d93940 | 1942 | - (bool) isUnfilteredAndSelectedForBy:(NSString *)search; |
e4f3d6e9 | 1943 | - (bool) isInstalledAndUnfiltered:(NSNumber *)number; |
670a0494 | 1944 | - (bool) isVisibleInSection:(NSString *)section; |
0a3b45ef | 1945 | - (bool) isVisibleInSource:(Source *)source; |
b4d89997 | 1946 | |
36bb2ca2 | 1947 | @end |
b4d89997 | 1948 | |
f79a4512 JF |
1949 | uint32_t PackageChangesRadix(Package *self, void *) { |
1950 | union { | |
1951 | uint32_t key; | |
1952 | ||
1953 | struct { | |
1954 | uint32_t timestamp : 30; | |
1955 | uint32_t ignored : 1; | |
1956 | uint32_t upgradable : 1; | |
1957 | } bits; | |
1958 | } value; | |
1959 | ||
1960 | bool upgradable([self upgradableAndEssential:YES]); | |
1961 | value.bits.upgradable = upgradable ? 1 : 0; | |
1962 | ||
1963 | if (upgradable) { | |
1964 | value.bits.timestamp = 0; | |
1965 | value.bits.ignored = [self ignored] ? 0 : 1; | |
1966 | value.bits.upgradable = 1; | |
1967 | } else { | |
36047c66 | 1968 | value.bits.timestamp = [self seen] >> 2; |
f79a4512 JF |
1969 | value.bits.ignored = 0; |
1970 | value.bits.upgradable = 0; | |
1971 | } | |
1972 | ||
1973 | return _not(uint32_t) - value.key; | |
1974 | } | |
1975 | ||
df213583 JF |
1976 | uint32_t PackagePrefixRadix(Package *self, void *context) { |
1977 | size_t offset(reinterpret_cast<size_t>(context)); | |
1978 | CYString &name([self cyname]); | |
677b8415 | 1979 | |
df213583 JF |
1980 | size_t size(name.size()); |
1981 | if (size == 0) | |
1982 | return 0; | |
1983 | char *text(name.data()); | |
677b8415 | 1984 | |
df213583 JF |
1985 | size_t zeros; |
1986 | if (!isdigit(text[0])) | |
1987 | zeros = 0; | |
1988 | else { | |
1989 | size_t digits(1); | |
1990 | while (size != digits && isdigit(text[digits])) | |
1991 | if (++digits == 4) | |
1992 | break; | |
1993 | zeros = 4 - digits; | |
1994 | } | |
677b8415 | 1995 | |
df213583 JF |
1996 | uint8_t data[4]; |
1997 | ||
df213583 JF |
1998 | if (offset == 0 && zeros != 0) { |
1999 | memset(data, '0', zeros); | |
2000 | memcpy(data + zeros, text, 4 - zeros); | |
2001 | } else { | |
2002 | /* XXX: there's some danger here if you request a non-zero offset < 4 and it gets zero padded */ | |
2003 | if (size <= offset - zeros) | |
2004 | return 0; | |
2005 | ||
2006 | text += offset - zeros; | |
2007 | size -= offset - zeros; | |
2008 | ||
2009 | if (size >= 4) | |
2010 | memcpy(data, text, 4); | |
2011 | else { | |
2012 | memcpy(data, text, size); | |
2013 | memset(data + size, 0, 4 - size); | |
2014 | } | |
2015 | ||
2016 | for (size_t i(0); i != 4; ++i) | |
2017 | if (isalpha(data[i])) | |
a7a59ee1 | 2018 | data[i] |= 0x20; |
df213583 JF |
2019 | } |
2020 | ||
2021 | if (offset == 0) | |
a7a59ee1 JF |
2022 | if (data[0] == '@') |
2023 | data[0] = 0x7f; | |
2024 | else | |
2025 | data[0] = (data[0] & 0x1f) | "\x80\x00\xc0\x40"[data[0] >> 6]; | |
df213583 JF |
2026 | |
2027 | /* XXX: ntohl may be more honest */ | |
2028 | return OSSwapInt32(*reinterpret_cast<uint32_t *>(data)); | |
2029 | } | |
2030 | ||
2031 | CYString &(*PackageName)(Package *self, SEL sel); | |
2032 | ||
2033 | CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { | |
2034 | _profile(PackageNameCompare) | |
2035 | CYString &lhi(PackageName(lhs, @selector(cyname))); | |
2036 | CYString &rhi(PackageName(rhs, @selector(cyname))); | |
2037 | CFStringRef lhn(lhi), rhn(rhi); | |
677b8415 | 2038 | |
5358f56f JF |
2039 | if (lhn == NULL) |
2040 | return rhn == NULL ? NSOrderedSame : NSOrderedAscending; | |
2041 | else if (rhn == NULL) | |
2042 | return NSOrderedDescending; | |
2043 | ||
677b8415 | 2044 | _profile(PackageNameCompare$NumbersLast) |
df213583 | 2045 | if (!lhi.empty() && !rhi.empty()) { |
677b8415 JF |
2046 | UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); |
2047 | UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); | |
2048 | bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); | |
2049 | if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) | |
2050 | return lha ? NSOrderedAscending : NSOrderedDescending; | |
2051 | } | |
2052 | _end | |
2053 | ||
df213583 JF |
2054 | CFIndex length = CFStringGetLength(lhn); |
2055 | ||
677b8415 JF |
2056 | _profile(PackageNameCompare$Compare) |
2057 | return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, Locale_); | |
2058 | _end | |
2059 | _end | |
2060 | } | |
2061 | ||
eef4ccaf JF |
2062 | CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *context) { |
2063 | return PackageNameCompare(*lhs, *rhs, context); | |
677b8415 JF |
2064 | } |
2065 | ||
2066 | struct PackageNameOrdering : | |
2067 | std::binary_function<Package *, Package *, bool> | |
2068 | { | |
2069 | _finline bool operator ()(Package *lhs, Package *rhs) const { | |
2070 | return PackageNameCompare(lhs, rhs, NULL) == NSOrderedAscending; | |
2071 | } | |
2072 | }; | |
2073 | ||
36bb2ca2 | 2074 | @implementation Package |
b4d89997 | 2075 | |
df213583 JF |
2076 | - (NSString *) description { |
2077 | return [NSString stringWithFormat:@"<Package:%@>", static_cast<NSString *>(name_)]; | |
2078 | } | |
2079 | ||
36bb2ca2 | 2080 | - (void) dealloc { |
12016ee5 JF |
2081 | if (parsed_ != NULL) |
2082 | delete parsed_; | |
bbb879fb JF |
2083 | if (source_ != nil) |
2084 | [source_ release]; | |
6d9712c4 JF |
2085 | if (tags_ != nil) |
2086 | [tags_ release]; | |
36bb2ca2 | 2087 | [super dealloc]; |
b4d89997 JF |
2088 | } |
2089 | ||
3bd1c2a2 JF |
2090 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
2091 | if (selector == @selector(hasTag:)) | |
2092 | return @"hasTag"; | |
2093 | else | |
2094 | return nil; | |
2095 | } | |
2096 | ||
2097 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
2098 | return [self webScriptNameForSelector:selector] == nil; | |
2099 | } | |
2100 | ||
6f1a15d9 | 2101 | + (NSArray *) _attributeKeys { |
eef4ccaf | 2102 | return [NSArray arrayWithObjects:@"applications", @"author", @"depiction", @"longDescription", @"essential", @"homepage", @"icon", @"id", @"installed", @"latest", @"longSection", @"maintainer", @"mode", @"name", @"purposes", @"section", @"shortDescription", @"shortSection", @"simpleSection", @"size", @"source", @"sponsor", @"support", @"warnings", nil]; |
6f1a15d9 JF |
2103 | } |
2104 | ||
2105 | - (NSArray *) attributeKeys { | |
2106 | return [[self class] _attributeKeys]; | |
2107 | } | |
2108 | ||
2109 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2110 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2111 | } | |
2112 | ||
68d927e2 | 2113 | - (void) parse { |
12016ee5 | 2114 | if (parsed_ != NULL) |
68d927e2 | 2115 | return; |
12016ee5 JF |
2116 | @synchronized (database_) { |
2117 | if ([database_ era] != era_ || file_.end()) | |
68d927e2 JF |
2118 | return; |
2119 | ||
12016ee5 JF |
2120 | ParsedPackage *parsed(new ParsedPackage); |
2121 | parsed_ = parsed; | |
2122 | ||
68d927e2 JF |
2123 | _profile(Package$parse) |
2124 | pkgRecords::Parser *parser; | |
2125 | ||
2126 | _profile(Package$parse$Lookup) | |
2127 | parser = &[database_ records]->Lookup(file_); | |
2128 | _end | |
2129 | ||
2130 | CYString website; | |
2131 | ||
2132 | _profile(Package$parse$Find) | |
2133 | struct { | |
2134 | const char *name_; | |
2135 | CYString *value_; | |
2136 | } names[] = { | |
12016ee5 JF |
2137 | {"icon", &parsed->icon_}, |
2138 | {"depiction", &parsed->depiction_}, | |
2139 | {"homepage", &parsed->homepage_}, | |
68d927e2 | 2140 | {"website", &website}, |
12016ee5 JF |
2141 | {"bugs", &parsed->bugs_}, |
2142 | {"support", &parsed->support_}, | |
2143 | {"sponsor", &parsed->sponsor_}, | |
2144 | {"author", &parsed->author_}, | |
68d927e2 JF |
2145 | }; |
2146 | ||
2147 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { | |
2148 | const char *start, *end; | |
2149 | ||
2150 | if (parser->Find(names[i].name_, start, end)) { | |
2151 | CYString &value(*names[i].value_); | |
2152 | _profile(Package$parse$Value) | |
2153 | value.set(pool_, start, end - start); | |
2154 | _end | |
2155 | } | |
2156 | } | |
2157 | _end | |
2158 | ||
2159 | _profile(Package$parse$Tagline) | |
2160 | const char *start, *end; | |
0dd0c302 | 2161 | if (parser->ShortDesc(start, end)) { |
68d927e2 JF |
2162 | const char *stop(reinterpret_cast<const char *>(memchr(start, '\n', end - start))); |
2163 | if (stop == NULL) | |
2164 | stop = end; | |
2165 | while (stop != start && stop[-1] == '\r') | |
2166 | --stop; | |
12016ee5 | 2167 | parsed->tagline_.set(pool_, start, stop - start); |
68d927e2 JF |
2168 | } |
2169 | _end | |
2170 | ||
2171 | _profile(Package$parse$Retain) | |
12016ee5 JF |
2172 | if (parsed->homepage_.empty()) |
2173 | parsed->homepage_ = website; | |
2174 | if (parsed->homepage_ == parsed->depiction_) | |
2175 | parsed->homepage_.clear(); | |
68d927e2 JF |
2176 | _end |
2177 | _end | |
12016ee5 | 2178 | } } |
68d927e2 | 2179 | |
7376b55c | 2180 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { |
a1440b10 | 2181 | if ((self = [super init]) != nil) { |
7376b55c | 2182 | _profile(Package$initWithVersion) |
68d927e2 | 2183 | pool_ = pool; |
a1440b10 | 2184 | |
36bb2ca2 | 2185 | database_ = database; |
aab28f8b | 2186 | era_ = [database era]; |
b4d89997 | 2187 | |
aab28f8b | 2188 | version_ = version; |
2083b866 | 2189 | |
aab28f8b JF |
2190 | pkgCache::PkgIterator iterator(version.ParentPkg()); |
2191 | iterator_ = iterator; | |
06aa974d | 2192 | |
aab28f8b | 2193 | _profile(Package$initWithVersion$Version) |
68d927e2 JF |
2194 | if (!version_.end()) |
2195 | file_ = version_.FileList(); | |
2196 | else { | |
2197 | pkgCache &cache([database_ cache]); | |
2198 | file_ = pkgCache::VerFileIterator(cache, cache.VerFileP); | |
2199 | } | |
2200 | _end | |
808c6eb6 | 2201 | |
aab28f8b | 2202 | _profile(Package$initWithVersion$Cache) |
aab28f8b JF |
2203 | name_.set(NULL, iterator.Display()); |
2204 | ||
2205 | latest_.set(NULL, StripVersion_(version_.VerStr())); | |
2206 | ||
2207 | pkgCache::VerIterator current(iterator.CurrentVer()); | |
2208 | if (!current.end()) | |
2209 | installed_.set(NULL, StripVersion_(current.VerStr())); | |
808c6eb6 JF |
2210 | _end |
2211 | ||
7376b55c | 2212 | _profile(Package$initWithVersion$Tags) |
aab28f8b | 2213 | pkgCache::TagIterator tag(iterator.TagList()); |
0dd0c302 JF |
2214 | if (!tag.end()) { |
2215 | tags_ = [[NSMutableArray alloc] initWithCapacity:8]; | |
2216 | do { | |
2217 | const char *name(tag.Name()); | |
3931b718 | 2218 | [tags_ addObject:[(NSString *)CYStringCreate(name) autorelease]]; |
0a377825 | 2219 | |
97a3d89e JF |
2220 | if (role_ == 0 && strncmp(name, "role::", 6) == 0 /*&& strcmp(name, "role::leaper") != 0*/) { |
2221 | if (strcmp(name + 6, "enduser") == 0) | |
2222 | role_ = 1; | |
2223 | else if (strcmp(name + 6, "hacker") == 0) | |
2224 | role_ = 2; | |
2225 | else if (strcmp(name + 6, "developer") == 0) | |
2226 | role_ = 3; | |
2227 | else if (strcmp(name + 6, "cydia") == 0) | |
2228 | role_ = 7; | |
2229 | else | |
2230 | role_ = 4; | |
2231 | } | |
0a377825 JF |
2232 | |
2233 | if (strncmp(name, "cydia::", 7) == 0) { | |
2234 | if (strcmp(name + 7, "essential") == 0) | |
2235 | essential_ = true; | |
2236 | else if (strcmp(name + 7, "obsolete") == 0) | |
2237 | obsolete_ = true; | |
2238 | } | |
2239 | ||
0dd0c302 JF |
2240 | ++tag; |
2241 | } while (!tag.end()); | |
2242 | } | |
808c6eb6 | 2243 | _end |
7b0ce2da | 2244 | |
7376b55c | 2245 | _profile(Package$initWithVersion$Metadata) |
029c8b74 JF |
2246 | const char *mixed(iterator.Name()); |
2247 | size_t size(strlen(mixed)); | |
2248 | char lower[size + 1]; | |
2249 | ||
2250 | for (size_t i(0); i != size; ++i) | |
2251 | lower[i] = mixed[i] | 0x20; | |
2252 | lower[size] = '\0'; | |
2253 | ||
2254 | PackageValue *metadata(PackageFind(lower, size)); | |
bb6bb6d6 | 2255 | metadata_ = metadata; |
677b8415 | 2256 | |
029c8b74 JF |
2257 | id_.set(NULL, metadata->name_, size); |
2258 | ||
94b0b3e5 JF |
2259 | const char *latest(version_.VerStr()); |
2260 | size_t length(strlen(latest)); | |
a4b0ec52 | 2261 | |
94b0b3e5 | 2262 | uint16_t vhash(hashlittle(latest, length)); |
677b8415 | 2263 | |
94b0b3e5 JF |
2264 | size_t capped(std::min<size_t>(8, length)); |
2265 | latest = latest + length - capped; | |
677b8415 | 2266 | |
94b0b3e5 JF |
2267 | if (metadata->first_ == 0) |
2268 | metadata->first_ = now_; | |
808c6eb6 | 2269 | |
94b0b3e5 | 2270 | if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) { |
94b0b3e5 JF |
2271 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); |
2272 | metadata->vhash_ = vhash; | |
2f856365 JF |
2273 | metadata->last_ = now_; |
2274 | } else if (metadata->last_ == 0) | |
2275 | metadata->last_ = metadata->first_; | |
808c6eb6 | 2276 | _end |
a1440b10 | 2277 | |
7376b55c | 2278 | _profile(Package$initWithVersion$Section) |
fe33a23e | 2279 | section_ = iterator.Section(); |
f79a4512 | 2280 | _end |
a1440b10 | 2281 | |
aab28f8b JF |
2282 | _profile(Package$initWithVersion$Flags) |
2283 | essential_ |= ((iterator->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES); | |
2284 | ignored_ = iterator->SelectedState == pkgCache::State::Hold; | |
4fa77608 | 2285 | _end |
3dd53516 | 2286 | _end } return self; |
dc5812ec JF |
2287 | } |
2288 | ||
f79a4512 | 2289 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { |
b1ce61ec JF |
2290 | pkgCache::VerIterator version; |
2291 | ||
2292 | _profile(Package$packageWithIterator$GetCandidateVer) | |
2293 | version = [database policy]->GetCandidateVer(iterator); | |
2294 | _end | |
2295 | ||
7376b55c JF |
2296 | if (version.end()) |
2297 | return nil; | |
b1ce61ec | 2298 | |
8564efc1 JF |
2299 | Package *package; |
2300 | ||
2301 | _profile(Package$packageWithIterator$Allocate) | |
2302 | package = [Package allocWithZone:zone]; | |
2303 | _end | |
2304 | ||
2305 | _profile(Package$packageWithIterator$Initialize) | |
2306 | package = [package | |
2307 | initWithVersion:version | |
2308 | withZone:zone | |
2309 | inPool:pool | |
2310 | database:database | |
2311 | ]; | |
2312 | _end | |
2313 | ||
2314 | _profile(Package$packageWithIterator$Autorelease) | |
2315 | package = [package autorelease]; | |
2316 | _end | |
2317 | ||
2318 | return package; | |
3dd53516 | 2319 | } |
dc5812ec | 2320 | |
2388b078 JF |
2321 | - (pkgCache::PkgIterator) iterator { |
2322 | return iterator_; | |
2323 | } | |
2324 | ||
36bb2ca2 | 2325 | - (NSString *) section { |
f79a4512 | 2326 | if (section$_ == nil) { |
fe33a23e | 2327 | if (section_ == NULL) |
f79a4512 JF |
2328 | return nil; |
2329 | ||
fe33a23e JF |
2330 | _profile(Package$section$mappedSectionForPointer) |
2331 | section$_ = [database_ mappedSectionForPointer:section_]; | |
e4f3d6e9 | 2332 | _end |
f79a4512 | 2333 | } return section$_; |
dc5812ec JF |
2334 | } |
2335 | ||
dec6029f JF |
2336 | - (NSString *) simpleSection { |
2337 | if (NSString *section = [self section]) | |
2338 | return Simplify(section); | |
2339 | else | |
2340 | return nil; | |
a3328c28 | 2341 | } |
dec6029f | 2342 | |
f79a4512 | 2343 | - (NSString *) longSection { |
18873623 | 2344 | return LocalizeSection([self section]); |
f79a4512 JF |
2345 | } |
2346 | ||
2347 | - (NSString *) shortSection { | |
2348 | return [[NSBundle mainBundle] localizedStringForKey:[self simpleSection] value:nil table:@"Sections"]; | |
2349 | } | |
2350 | ||
a3328c28 JF |
2351 | - (NSString *) uri { |
2352 | return nil; | |
2353 | #if 0 | |
2354 | pkgIndexFile *index; | |
2355 | pkgCache::PkgFileIterator file(file_.File()); | |
2356 | if (![database_ list].FindIndex(file, index)) | |
2357 | return nil; | |
2358 | return [NSString stringWithUTF8String:iterator_->Path]; | |
2359 | //return [NSString stringWithUTF8String:file.Site()]; | |
2360 | //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()]; | |
2361 | #endif | |
dec6029f JF |
2362 | } |
2363 | ||
36bb2ca2 | 2364 | - (Address *) maintainer { |
884171d6 JF |
2365 | @synchronized (database_) { |
2366 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2367 | return nil; |
884171d6 | 2368 | |
36bb2ca2 | 2369 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
00e2109e JF |
2370 | const std::string &maintainer(parser->Maintainer()); |
2371 | return maintainer.empty() ? nil : [Address addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]]; | |
884171d6 | 2372 | } } |
8fe19fc1 | 2373 | |
36bb2ca2 | 2374 | - (size_t) size { |
884171d6 JF |
2375 | @synchronized (database_) { |
2376 | if ([database_ era] != era_ || version_.end()) | |
2377 | return 0; | |
2378 | ||
2379 | return version_->InstalledSize; | |
2380 | } } | |
dc5812ec | 2381 | |
eef4ccaf | 2382 | - (NSString *) longDescription { |
3dd53516 JF |
2383 | @synchronized (database_) { |
2384 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2385 | return nil; |
3dd53516 | 2386 | |
36bb2ca2 | 2387 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
2388b078 | 2388 | NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]); |
8fe19fc1 | 2389 | |
36bb2ca2 JF |
2390 | NSArray *lines = [description componentsSeparatedByString:@"\n"]; |
2391 | NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; | |
2392 | if ([lines count] < 2) | |
2393 | return nil; | |
3178d79b | 2394 | |
36bb2ca2 | 2395 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; |
c4dcf2c2 | 2396 | for (size_t i(1), e([lines count]); i != e; ++i) { |
36bb2ca2 JF |
2397 | NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; |
2398 | [trimmed addObject:trim]; | |
2399 | } | |
3178d79b | 2400 | |
36bb2ca2 | 2401 | return [trimmed componentsJoinedByString:@"\n"]; |
3dd53516 | 2402 | } } |
dc5812ec | 2403 | |
eef4ccaf | 2404 | - (NSString *) shortDescription { |
12016ee5 | 2405 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->tagline_); |
eef4ccaf JF |
2406 | } |
2407 | ||
808c6eb6 JF |
2408 | - (unichar) index { |
2409 | _profile(Package$index) | |
677b8415 JF |
2410 | CFStringRef name((CFStringRef) [self name]); |
2411 | if (CFStringGetLength(name) == 0) | |
808c6eb6 | 2412 | return '#'; |
677b8415 JF |
2413 | UniChar character(CFStringGetCharacterAtIndex(name, 0)); |
2414 | if (!CFUniCharIsMemberOf(character, kCFUniCharLetterCharacterSet)) | |
808c6eb6 | 2415 | return '#'; |
447db19d | 2416 | return toupper(character); |
808c6eb6 | 2417 | _end |
36bb2ca2 | 2418 | } |
3178d79b | 2419 | |
94b0b3e5 | 2420 | - (PackageValue *) metadata { |
bb6bb6d6 | 2421 | return metadata_; |
807ae6d7 JF |
2422 | } |
2423 | ||
31bc18a7 | 2424 | - (time_t) seen { |
94b0b3e5 JF |
2425 | PackageValue *metadata([self metadata]); |
2426 | return metadata->subscribed_ ? metadata->last_ : metadata->first_; | |
3178d79b JF |
2427 | } |
2428 | ||
94b0b3e5 JF |
2429 | - (bool) subscribed { |
2430 | return [self metadata]->subscribed_; | |
2431 | } | |
2432 | ||
2433 | - (bool) setSubscribed:(bool)subscribed { | |
2434 | PackageValue *metadata([self metadata]); | |
2435 | if (metadata->subscribed_ == subscribed) | |
2436 | return false; | |
2437 | metadata->subscribed_ = subscribed; | |
2438 | return true; | |
807ae6d7 JF |
2439 | } |
2440 | ||
2441 | - (BOOL) ignored { | |
1b18f026 | 2442 | return ignored_; |
807ae6d7 JF |
2443 | } |
2444 | ||
36bb2ca2 JF |
2445 | - (NSString *) latest { |
2446 | return latest_; | |
8fe19fc1 JF |
2447 | } |
2448 | ||
36bb2ca2 JF |
2449 | - (NSString *) installed { |
2450 | return installed_; | |
3178d79b JF |
2451 | } |
2452 | ||
6a155117 JF |
2453 | - (BOOL) uninstalled { |
2454 | return installed_.empty(); | |
2455 | } | |
2456 | ||
5a09ae08 JF |
2457 | - (BOOL) valid { |
2458 | return !version_.end(); | |
2459 | } | |
2460 | ||
31f3cfff | 2461 | - (BOOL) upgradableAndEssential:(BOOL)essential { |
677b8415 JF |
2462 | _profile(Package$upgradableAndEssential) |
2463 | pkgCache::VerIterator current(iterator_.CurrentVer()); | |
2464 | if (current.end()) | |
e4f3d6e9 | 2465 | return essential && essential_; |
677b8415 | 2466 | else |
e4f3d6e9 | 2467 | return !version_.end() && version_ != current; |
677b8415 | 2468 | _end |
36bb2ca2 | 2469 | } |
3178d79b | 2470 | |
36bb2ca2 | 2471 | - (BOOL) essential { |
a1440b10 | 2472 | return essential_; |
3178d79b JF |
2473 | } |
2474 | ||
36bb2ca2 | 2475 | - (BOOL) broken { |
9bedffaa JF |
2476 | return [database_ cache][iterator_].InstBroken(); |
2477 | } | |
2478 | ||
7d2ac47f | 2479 | - (BOOL) unfiltered { |
4fa77608 | 2480 | _profile(Package$unfiltered$obsolete) |
cf48f656 | 2481 | if (_unlikely(obsolete_)) |
4fa77608 JF |
2482 | return false; |
2483 | _end | |
2484 | ||
2485 | _profile(Package$unfiltered$hasSupportingRole) | |
cf48f656 | 2486 | if (_unlikely(![self hasSupportingRole])) |
4fa77608 JF |
2487 | return false; |
2488 | _end | |
2489 | ||
e4f3d6e9 JF |
2490 | return true; |
2491 | } | |
4fa77608 | 2492 | |
e4f3d6e9 JF |
2493 | - (BOOL) visible { |
2494 | if (![self unfiltered]) | |
2495 | return false; | |
2496 | ||
fe33a23e JF |
2497 | NSString *section; |
2498 | ||
2499 | _profile(Package$visible$section) | |
2500 | section = [self section]; | |
2501 | _end | |
4fa77608 | 2502 | |
e4f3d6e9 | 2503 | _profile(Package$visible$isSectionVisible) |
4fa77608 JF |
2504 | if (section != nil && !isSectionVisible(section)) |
2505 | return false; | |
2506 | _end | |
2507 | ||
2508 | return true; | |
7d2ac47f JF |
2509 | } |
2510 | ||
9bedffaa | 2511 | - (BOOL) half { |
677b8415 | 2512 | unsigned char current(iterator_->CurrentState); |
9bedffaa JF |
2513 | return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled; |
2514 | } | |
2515 | ||
2516 | - (BOOL) halfConfigured { | |
2517 | return iterator_->CurrentState == pkgCache::State::HalfConfigured; | |
2518 | } | |
2519 | ||
2520 | - (BOOL) halfInstalled { | |
2521 | return iterator_->CurrentState == pkgCache::State::HalfInstalled; | |
2522 | } | |
2523 | ||
2524 | - (BOOL) hasMode { | |
2525 | pkgDepCache::StateCache &state([database_ cache][iterator_]); | |
2526 | return state.Mode != pkgDepCache::ModeKeep; | |
2527 | } | |
2528 | ||
2529 | - (NSString *) mode { | |
2530 | pkgDepCache::StateCache &state([database_ cache][iterator_]); | |
2531 | ||
2532 | switch (state.Mode) { | |
2533 | case pkgDepCache::ModeDelete: | |
2534 | if ((state.iFlags & pkgDepCache::Purge) != 0) | |
f79a4512 | 2535 | return @"PURGE"; |
9bedffaa | 2536 | else |
f79a4512 | 2537 | return @"REMOVE"; |
9bedffaa | 2538 | case pkgDepCache::ModeKeep: |
dc63e78f | 2539 | if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2540 | return @"REINSTALL"; |
dc63e78f JF |
2541 | /*else if ((state.iFlags & pkgDepCache::AutoKept) != 0) |
2542 | return nil;*/ | |
9bedffaa JF |
2543 | else |
2544 | return nil; | |
9bedffaa | 2545 | case pkgDepCache::ModeInstall: |
dc63e78f | 2546 | /*if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2547 | return @"REINSTALL"; |
dc63e78f | 2548 | else*/ switch (state.Status) { |
9bedffaa | 2549 | case -1: |
f79a4512 | 2550 | return @"DOWNGRADE"; |
9bedffaa | 2551 | case 0: |
f79a4512 | 2552 | return @"INSTALL"; |
9bedffaa | 2553 | case 1: |
f79a4512 | 2554 | return @"UPGRADE"; |
9bedffaa | 2555 | case 2: |
f79a4512 | 2556 | return @"NEW_INSTALL"; |
670a0494 | 2557 | _nodefault |
9bedffaa | 2558 | } |
670a0494 | 2559 | _nodefault |
9bedffaa | 2560 | } |
8fe19fc1 JF |
2561 | } |
2562 | ||
36bb2ca2 JF |
2563 | - (NSString *) id { |
2564 | return id_; | |
8fe19fc1 JF |
2565 | } |
2566 | ||
36bb2ca2 | 2567 | - (NSString *) name { |
9fcbca29 | 2568 | return name_.empty() ? id_ : name_; |
36bb2ca2 | 2569 | } |
8fe19fc1 | 2570 | |
770f2a8e | 2571 | - (UIImage *) icon { |
dec6029f | 2572 | NSString *section = [self simpleSection]; |
770f2a8e JF |
2573 | |
2574 | UIImage *icon(nil); | |
12016ee5 JF |
2575 | if (parsed_ != NULL) |
2576 | if (NSString *href = parsed_->icon_) | |
2577 | if ([href hasPrefix:@"file:///"]) | |
2578 | // XXX: correct escaping | |
2579 | icon = [UIImage imageAtPath:[href substringFromIndex:7]]; | |
770f2a8e JF |
2580 | if (icon == nil) if (section != nil) |
2581 | icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]]; | |
b9956841 | 2582 | if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon]) |
189a73d0 | 2583 | if ([dicon hasPrefix:@"file:///"]) |
ac308d3e | 2584 | // XXX: correct escaping |
189a73d0 | 2585 | icon = [UIImage imageAtPath:[dicon substringFromIndex:7]]; |
770f2a8e JF |
2586 | if (icon == nil) |
2587 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
2588 | return icon; | |
36bb2ca2 | 2589 | } |
8fe19fc1 | 2590 | |
6f1a15d9 | 2591 | - (NSString *) homepage { |
12016ee5 | 2592 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->homepage_); |
8fe19fc1 JF |
2593 | } |
2594 | ||
25a2158d | 2595 | - (NSString *) depiction { |
12016ee5 | 2596 | return parsed_ != NULL && !parsed_->depiction_.empty() ? parsed_->depiction_ : [[self source] depictionForPackage:id_]; |
25a2158d JF |
2597 | } |
2598 | ||
795d26fc | 2599 | - (Address *) sponsor { |
12016ee5 | 2600 | return parsed_ == NULL || parsed_->sponsor_.empty() ? nil : [Address addressWithString:parsed_->sponsor_]; |
795d26fc JF |
2601 | } |
2602 | ||
77fcccaf | 2603 | - (Address *) author { |
12016ee5 | 2604 | return parsed_ == NULL || parsed_->author_.empty() ? nil : [Address addressWithString:parsed_->author_]; |
77fcccaf JF |
2605 | } |
2606 | ||
dc63e78f | 2607 | - (NSString *) support { |
12016ee5 | 2608 | return parsed_ != NULL && !parsed_->bugs_.empty() ? parsed_->bugs_ : [[self source] supportForPackage:id_]; |
dc63e78f JF |
2609 | } |
2610 | ||
affeffc7 | 2611 | - (NSArray *) files { |
9fcbca29 | 2612 | NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", static_cast<NSString *>(id_)]; |
affeffc7 JF |
2613 | NSMutableArray *files = [NSMutableArray arrayWithCapacity:128]; |
2614 | ||
2615 | std::ifstream fin; | |
2616 | fin.open([path UTF8String]); | |
2617 | if (!fin.is_open()) | |
2618 | return nil; | |
2619 | ||
2620 | std::string line; | |
2621 | while (std::getline(fin, line)) | |
2622 | [files addObject:[NSString stringWithUTF8String:line.c_str()]]; | |
2623 | ||
2624 | return files; | |
2625 | } | |
2626 | ||
affeffc7 JF |
2627 | - (NSArray *) warnings { |
2628 | NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); | |
2629 | const char *name(iterator_.Name()); | |
2630 | ||
2631 | size_t length(strlen(name)); | |
2632 | if (length < 2) invalid: | |
43f3d7f6 | 2633 | [warnings addObject:UCLocalize("ILLEGAL_PACKAGE_IDENTIFIER")]; |
affeffc7 JF |
2634 | else for (size_t i(0); i != length; ++i) |
2635 | if ( | |
9dd60d81 JF |
2636 | /* XXX: technically this is not allowed */ |
2637 | (name[i] < 'A' || name[i] > 'Z') && | |
affeffc7 JF |
2638 | (name[i] < 'a' || name[i] > 'z') && |
2639 | (name[i] < '0' || name[i] > '9') && | |
2640 | (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.') | |
2641 | ) goto invalid; | |
2642 | ||
2643 | if (strcmp(name, "cydia") != 0) { | |
2644 | bool cydia = false; | |
7623f855 | 2645 | bool user = false; |
9dd60d81 | 2646 | bool _private = false; |
affeffc7 JF |
2647 | bool stash = false; |
2648 | ||
9dd60d81 JF |
2649 | bool repository = [[self section] isEqualToString:@"Repositories"]; |
2650 | ||
affeffc7 JF |
2651 | if (NSArray *files = [self files]) |
2652 | for (NSString *file in files) | |
2653 | if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"]) | |
2654 | cydia = true; | |
7623f855 JF |
2655 | else if (!user && [file isEqualToString:@"/User"]) |
2656 | user = true; | |
9dd60d81 JF |
2657 | else if (!_private && [file isEqualToString:@"/private"]) |
2658 | _private = true; | |
affeffc7 JF |
2659 | else if (!stash && [file isEqualToString:@"/var/stash"]) |
2660 | stash = true; | |
2661 | ||
9dd60d81 JF |
2662 | /* XXX: this is not sensitive enough. only some folders are valid. */ |
2663 | if (cydia && !repository) | |
43f3d7f6 | 2664 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"Cydia.app"]]; |
7623f855 JF |
2665 | if (user) |
2666 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/User"]]; | |
9dd60d81 | 2667 | if (_private) |
43f3d7f6 | 2668 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; |
affeffc7 | 2669 | if (stash) |
43f3d7f6 | 2670 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; |
affeffc7 JF |
2671 | } |
2672 | ||
2673 | return [warnings count] == 0 ? nil : warnings; | |
2674 | } | |
2675 | ||
2676 | - (NSArray *) applications { | |
2677 | NSString *me([[NSBundle mainBundle] bundleIdentifier]); | |
2678 | ||
2679 | NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); | |
2680 | ||
2681 | static Pcre application_r("^/Applications/(.*)\\.app/Info.plist$"); | |
2682 | if (NSArray *files = [self files]) | |
2683 | for (NSString *file in files) | |
2684 | if (application_r(file)) { | |
2685 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); | |
2686 | NSString *id([info objectForKey:@"CFBundleIdentifier"]); | |
2687 | if ([id isEqualToString:me]) | |
2688 | continue; | |
2689 | ||
2690 | NSString *display([info objectForKey:@"CFBundleDisplayName"]); | |
2691 | if (display == nil) | |
2692 | display = application_r[1]; | |
2693 | ||
2694 | NSString *bundle([file stringByDeletingLastPathComponent]); | |
2695 | NSString *icon([info objectForKey:@"CFBundleIconFile"]); | |
2696 | if (icon == nil || [icon length] == 0) | |
2697 | icon = @"icon.png"; | |
2698 | NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]); | |
2699 | ||
2700 | NSMutableArray *application([NSMutableArray arrayWithCapacity:2]); | |
2701 | [applications addObject:application]; | |
2702 | ||
2703 | [application addObject:id]; | |
2704 | [application addObject:display]; | |
2705 | [application addObject:url]; | |
2706 | } | |
2707 | ||
2708 | return [applications count] == 0 ? nil : applications; | |
2709 | } | |
2710 | ||
36bb2ca2 | 2711 | - (Source *) source { |
5699667a | 2712 | if (source_ == nil) { |
a1440b10 JF |
2713 | @synchronized (database_) { |
2714 | if ([database_ era] != era_ || file_.end()) | |
5699667a JF |
2715 | source_ = (Source *) [NSNull null]; |
2716 | else | |
2717 | source_ = [([database_ getSource:file_.File()] ?: (Source *) [NSNull null]) retain]; | |
a1440b10 | 2718 | } |
bbb879fb JF |
2719 | } |
2720 | ||
5699667a | 2721 | return source_ == (Source *) [NSNull null] ? nil : source_; |
8fe19fc1 JF |
2722 | } |
2723 | ||
36bb2ca2 JF |
2724 | - (BOOL) matches:(NSString *)text { |
2725 | if (text == nil) | |
2726 | return NO; | |
8fe19fc1 | 2727 | |
36bb2ca2 | 2728 | NSRange range; |
8fe19fc1 | 2729 | |
808c6eb6 | 2730 | range = [[self id] rangeOfString:text options:MatchCompareOptions_]; |
36bb2ca2 JF |
2731 | if (range.location != NSNotFound) |
2732 | return YES; | |
8fe19fc1 | 2733 | |
808c6eb6 | 2734 | range = [[self name] rangeOfString:text options:MatchCompareOptions_]; |
36bb2ca2 JF |
2735 | if (range.location != NSNotFound) |
2736 | return YES; | |
8fe19fc1 | 2737 | |
01d93940 | 2738 | range = [[self shortDescription] rangeOfString:text options:MatchCompareOptions_]; |
36bb2ca2 | 2739 | if (range.location != NSNotFound) |
01d93940 | 2740 | return YES; |
8fe19fc1 | 2741 | |
36bb2ca2 JF |
2742 | return NO; |
2743 | } | |
8fe19fc1 | 2744 | |
7b0ce2da | 2745 | - (bool) hasSupportingRole { |
97a3d89e | 2746 | if (role_ == 0) |
6d9712c4 | 2747 | return true; |
97a3d89e | 2748 | if (role_ == 1) |
7b0ce2da JF |
2749 | return true; |
2750 | if ([Role_ isEqualToString:@"User"]) | |
2751 | return false; | |
97a3d89e | 2752 | if (role_ == 2) |
7b0ce2da JF |
2753 | return true; |
2754 | if ([Role_ isEqualToString:@"Hacker"]) | |
2755 | return false; | |
97a3d89e | 2756 | if (role_ == 3) |
7b0ce2da JF |
2757 | return true; |
2758 | if ([Role_ isEqualToString:@"Developer"]) | |
2759 | return false; | |
2760 | _assert(false); | |
6d9712c4 JF |
2761 | } |
2762 | ||
2763 | - (BOOL) hasTag:(NSString *)tag { | |
2764 | return tags_ == nil ? NO : [tags_ containsObject:tag]; | |
2765 | } | |
2766 | ||
c390d3ab JF |
2767 | - (NSString *) primaryPurpose { |
2768 | for (NSString *tag in tags_) | |
2769 | if ([tag hasPrefix:@"purpose::"]) | |
2770 | return [tag substringFromIndex:9]; | |
2771 | return nil; | |
2772 | } | |
2773 | ||
770f2a8e JF |
2774 | - (NSArray *) purposes { |
2775 | NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]); | |
2776 | for (NSString *tag in tags_) | |
2777 | if ([tag hasPrefix:@"purpose::"]) | |
2778 | [purposes addObject:[tag substringFromIndex:9]]; | |
2779 | return [purposes count] == 0 ? nil : purposes; | |
2780 | } | |
2781 | ||
3bd1c2a2 JF |
2782 | - (bool) isCommercial { |
2783 | return [self hasTag:@"cydia::commercial"]; | |
2784 | } | |
2785 | ||
cd95e390 JF |
2786 | - (void) setIndex:(size_t)index { |
2787 | if (metadata_->index_ != index) | |
2788 | metadata_->index_ = index; | |
2789 | } | |
2790 | ||
df213583 JF |
2791 | - (CYString &) cyname { |
2792 | return name_.empty() ? id_ : name_; | |
f79a4512 JF |
2793 | } |
2794 | ||
f79a4512 JF |
2795 | - (uint32_t) compareBySection:(NSArray *)sections { |
2796 | NSString *section([self section]); | |
2797 | for (size_t i(0), e([sections count]); i != e; ++i) { | |
2798 | if ([section isEqualToString:[[sections objectAtIndex:i] name]]) | |
2799 | return i; | |
36bb2ca2 | 2800 | } |
3178d79b | 2801 | |
f79a4512 | 2802 | return _not(uint32_t); |
36bb2ca2 | 2803 | } |
3178d79b | 2804 | |
dc63e78f | 2805 | - (void) clear { |
c6ca67ba | 2806 | @synchronized (database_) { |
dc63e78f JF |
2807 | pkgProblemResolver *resolver = [database_ resolver]; |
2808 | resolver->Clear(iterator_); | |
c6ca67ba JF |
2809 | |
2810 | pkgCacheFile &cache([database_ cache]); | |
2811 | cache->SetReInstall(iterator_, false); | |
2812 | cache->MarkKeep(iterator_, false); | |
2813 | } } | |
dc63e78f | 2814 | |
36bb2ca2 | 2815 | - (void) install { |
c6ca67ba | 2816 | @synchronized (database_) { |
36bb2ca2 JF |
2817 | pkgProblemResolver *resolver = [database_ resolver]; |
2818 | resolver->Clear(iterator_); | |
2819 | resolver->Protect(iterator_); | |
c6ca67ba | 2820 | |
36bb2ca2 | 2821 | pkgCacheFile &cache([database_ cache]); |
c6ca67ba | 2822 | cache->SetReInstall(iterator_, false); |
36bb2ca2 | 2823 | cache->MarkInstall(iterator_, false); |
c6ca67ba | 2824 | |
36bb2ca2 JF |
2825 | pkgDepCache::StateCache &state((*cache)[iterator_]); |
2826 | if (!state.Install()) | |
2827 | cache->SetReInstall(iterator_, true); | |
c6ca67ba | 2828 | } } |
68a238ec | 2829 | |
36bb2ca2 | 2830 | - (void) remove { |
c6ca67ba | 2831 | @synchronized (database_) { |
36bb2ca2 JF |
2832 | pkgProblemResolver *resolver = [database_ resolver]; |
2833 | resolver->Clear(iterator_); | |
36bb2ca2 | 2834 | resolver->Remove(iterator_); |
c6ca67ba JF |
2835 | resolver->Protect(iterator_); |
2836 | ||
2837 | pkgCacheFile &cache([database_ cache]); | |
2838 | cache->SetReInstall(iterator_, false); | |
2839 | cache->MarkDelete(iterator_, true); | |
2840 | } } | |
8fe19fc1 | 2841 | |
0a3b45ef | 2842 | - (bool) isUnfilteredAndSearchedForBy:(NSString *)search { |
808c6eb6 JF |
2843 | _profile(Package$isUnfilteredAndSearchedForBy) |
2844 | bool value(true); | |
2845 | ||
2846 | _profile(Package$isUnfilteredAndSearchedForBy$Unfiltered) | |
2847 | value &= [self unfiltered]; | |
2848 | _end | |
2849 | ||
2850 | _profile(Package$isUnfilteredAndSearchedForBy$Match) | |
2851 | value &= [self matches:search]; | |
2852 | _end | |
2853 | ||
0a3b45ef | 2854 | return value; |
808c6eb6 | 2855 | _end |
36bb2ca2 | 2856 | } |
8fe19fc1 | 2857 | |
01d93940 | 2858 | - (bool) isUnfilteredAndSelectedForBy:(NSString *)search { |
ef055c6c JF |
2859 | if ([search length] == 0) |
2860 | return false; | |
2861 | ||
01d93940 JF |
2862 | _profile(Package$isUnfilteredAndSelectedForBy) |
2863 | bool value(true); | |
2864 | ||
2865 | _profile(Package$isUnfilteredAndSelectedForBy$Unfiltered) | |
2866 | value &= [self unfiltered]; | |
2867 | _end | |
2868 | ||
2869 | _profile(Package$isUnfilteredAndSelectedForBy$Match) | |
2870 | value &= [[self name] compare:search options:MatchCompareOptions_ range:NSMakeRange(0, [search length])] == NSOrderedSame; | |
2871 | _end | |
2872 | ||
2873 | return value; | |
2874 | _end | |
2875 | } | |
2876 | ||
e4f3d6e9 | 2877 | - (bool) isInstalledAndUnfiltered:(NSNumber *)number { |
97a3d89e | 2878 | return ![self uninstalled] && (![number boolValue] && role_ != 7 || [self unfiltered]); |
36bb2ca2 | 2879 | } |
8fe19fc1 | 2880 | |
670a0494 | 2881 | - (bool) isVisibleInSection:(NSString *)name { |
e4f3d6e9 | 2882 | NSString *section([self section]); |
5a09ae08 | 2883 | |
e4f3d6e9 JF |
2884 | return ( |
2885 | name == nil || | |
2886 | section == nil && [name length] == 0 || | |
2887 | [name isEqualToString:section] | |
2888 | ) && [self visible]; | |
7b0ce2da JF |
2889 | } |
2890 | ||
0a3b45ef JF |
2891 | - (bool) isVisibleInSource:(Source *)source { |
2892 | return [self source] == source && [self visible]; | |
36bb2ca2 | 2893 | } |
8fe19fc1 | 2894 | |
36bb2ca2 JF |
2895 | @end |
2896 | /* }}} */ | |
2897 | /* Section Class {{{ */ | |
2898 | @interface Section : NSObject { | |
2899 | NSString *name_; | |
808c6eb6 | 2900 | unichar index_; |
36bb2ca2 JF |
2901 | size_t row_; |
2902 | size_t count_; | |
f79a4512 | 2903 | NSString *localized_; |
36bb2ca2 | 2904 | } |
3178d79b | 2905 | |
677b8415 | 2906 | - (NSComparisonResult) compareByLocalized:(Section *)section; |
9fcbca29 JF |
2907 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; |
2908 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; | |
2909 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; | |
808c6eb6 | 2910 | - (Section *) initWithIndex:(unichar)index row:(size_t)row; |
36bb2ca2 | 2911 | - (NSString *) name; |
808c6eb6 | 2912 | - (unichar) index; |
f79a4512 | 2913 | |
36bb2ca2 JF |
2914 | - (size_t) row; |
2915 | - (size_t) count; | |
f79a4512 JF |
2916 | |
2917 | - (void) addToRow; | |
36bb2ca2 | 2918 | - (void) addToCount; |
b19871dd | 2919 | |
f79a4512 | 2920 | - (void) setCount:(size_t)count; |
677b8415 | 2921 | - (NSString *) localized; |
f79a4512 | 2922 | |
36bb2ca2 | 2923 | @end |
b19871dd | 2924 | |
36bb2ca2 | 2925 | @implementation Section |
b19871dd | 2926 | |
36bb2ca2 JF |
2927 | - (void) dealloc { |
2928 | [name_ release]; | |
f79a4512 JF |
2929 | if (localized_ != nil) |
2930 | [localized_ release]; | |
36bb2ca2 JF |
2931 | [super dealloc]; |
2932 | } | |
b19871dd | 2933 | |
677b8415 | 2934 | - (NSComparisonResult) compareByLocalized:(Section *)section { |
9fcbca29 JF |
2935 | NSString *lhs(localized_); |
2936 | NSString *rhs([section localized]); | |
6d9712c4 | 2937 | |
9fcbca29 | 2938 | /*if ([lhs length] != 0 && [rhs length] != 0) { |
6d9712c4 JF |
2939 | unichar lhc = [lhs characterAtIndex:0]; |
2940 | unichar rhc = [rhs characterAtIndex:0]; | |
2941 | ||
2942 | if (isalpha(lhc) && !isalpha(rhc)) | |
2943 | return NSOrderedAscending; | |
2944 | else if (!isalpha(lhc) && isalpha(rhc)) | |
2945 | return NSOrderedDescending; | |
9fcbca29 | 2946 | }*/ |
6d9712c4 | 2947 | |
68f1828e | 2948 | return [lhs compare:rhs options:LaxCompareOptions_]; |
6d9712c4 JF |
2949 | } |
2950 | ||
9fcbca29 JF |
2951 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized { |
2952 | if ((self = [self initWithName:name localize:NO]) != nil) { | |
2953 | if (localized != nil) | |
2954 | localized_ = [localized retain]; | |
2955 | } return self; | |
2956 | } | |
2957 | ||
2958 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize { | |
2959 | return [self initWithName:name row:0 localize:localize]; | |
6d9712c4 JF |
2960 | } |
2961 | ||
9fcbca29 | 2962 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { |
36bb2ca2 JF |
2963 | if ((self = [super init]) != nil) { |
2964 | name_ = [name retain]; | |
808c6eb6 JF |
2965 | index_ = '\0'; |
2966 | row_ = row; | |
9fcbca29 JF |
2967 | if (localize) |
2968 | localized_ = [LocalizeSection(name_) retain]; | |
808c6eb6 JF |
2969 | } return self; |
2970 | } | |
2971 | ||
f79a4512 | 2972 | /* XXX: localize the index thingees */ |
808c6eb6 JF |
2973 | - (Section *) initWithIndex:(unichar)index row:(size_t)row { |
2974 | if ((self = [super init]) != nil) { | |
327624b6 | 2975 | name_ = [[NSString stringWithCharacters:&index length:1] retain]; |
808c6eb6 | 2976 | index_ = index; |
36bb2ca2 | 2977 | row_ = row; |
b19871dd JF |
2978 | } return self; |
2979 | } | |
2980 | ||
36bb2ca2 JF |
2981 | - (NSString *) name { |
2982 | return name_; | |
2983 | } | |
2984 | ||
808c6eb6 JF |
2985 | - (unichar) index { |
2986 | return index_; | |
2987 | } | |
2988 | ||
36bb2ca2 JF |
2989 | - (size_t) row { |
2990 | return row_; | |
2991 | } | |
2992 | ||
2993 | - (size_t) count { | |
2994 | return count_; | |
2995 | } | |
2996 | ||
f79a4512 JF |
2997 | - (void) addToRow { |
2998 | ++row_; | |
2999 | } | |
3000 | ||
36bb2ca2 JF |
3001 | - (void) addToCount { |
3002 | ++count_; | |
3003 | } | |
3004 | ||
f79a4512 JF |
3005 | - (void) setCount:(size_t)count { |
3006 | count_ = count; | |
3007 | } | |
3008 | ||
3009 | - (NSString *) localized { | |
3010 | return localized_; | |
3011 | } | |
3012 | ||
b19871dd JF |
3013 | @end |
3014 | /* }}} */ | |
3015 | ||
670a0494 | 3016 | static NSString *Colon_; |
72fb3616 | 3017 | static NSString *Elision_; |
670a0494 JF |
3018 | static NSString *Error_; |
3019 | static NSString *Warning_; | |
3020 | ||
36bb2ca2 JF |
3021 | /* Database Implementation {{{ */ |
3022 | @implementation Database | |
ec97ef06 | 3023 | |
770f2a8e JF |
3024 | + (Database *) sharedInstance { |
3025 | static Database *instance; | |
3026 | if (instance == nil) | |
3027 | instance = [[Database alloc] init]; | |
3028 | return instance; | |
3029 | } | |
3030 | ||
a1440b10 JF |
3031 | - (unsigned) era { |
3032 | return era_; | |
3033 | } | |
3034 | ||
0944377b JF |
3035 | - (void) releasePackages { |
3036 | CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL); | |
3037 | CFArrayRemoveAllValues(packages_); | |
3038 | } | |
3039 | ||
36bb2ca2 | 3040 | - (void) dealloc { |
3931b718 | 3041 | // XXX: actually implement this thing |
36bb2ca2 | 3042 | _assert(false); |
ab3f6a01 RP |
3043 | if (deadSources_) |
3044 | CFRelease(deadSources_); | |
0944377b | 3045 | [self releasePackages]; |
f79a4512 | 3046 | apr_pool_destroy(pool_); |
0944377b | 3047 | NSRecycleZone(zone_); |
36bb2ca2 JF |
3048 | [super dealloc]; |
3049 | } | |
ec97ef06 | 3050 | |
affeffc7 | 3051 | - (void) _readCydia:(NSNumber *)fd { _pooled |
77fcccaf JF |
3052 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3053 | std::istream is(&ib); | |
3054 | std::string line; | |
3055 | ||
bc8cd583 JF |
3056 | static Pcre finish_r("^finish:([^:]*)$"); |
3057 | ||
77fcccaf JF |
3058 | while (std::getline(is, line)) { |
3059 | const char *data(line.c_str()); | |
bc8cd583 | 3060 | size_t size = line.size(); |
c390d3ab | 3061 | lprintf("C:%s\n", data); |
bc8cd583 JF |
3062 | |
3063 | if (finish_r(data, size)) { | |
3064 | NSString *finish = finish_r[1]; | |
3065 | int index = [Finishes_ indexOfObject:finish]; | |
3066 | if (index != INT_MAX && index > Finish_) | |
3067 | Finish_ = index; | |
3068 | } | |
77fcccaf JF |
3069 | } |
3070 | ||
670a0494 | 3071 | _assume(false); |
77fcccaf JF |
3072 | } |
3073 | ||
affeffc7 | 3074 | - (void) _readStatus:(NSNumber *)fd { _pooled |
36bb2ca2 JF |
3075 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3076 | std::istream is(&ib); | |
3077 | std::string line; | |
ec97ef06 | 3078 | |
7b0ce2da JF |
3079 | static Pcre conffile_r("^status: [^ ]* : conffile-prompt : (.*?) *$"); |
3080 | static Pcre pmstatus_r("^([^:]*):([^:]*):([^:]*):(.*)$"); | |
ec97ef06 | 3081 | |
36bb2ca2 JF |
3082 | while (std::getline(is, line)) { |
3083 | const char *data(line.c_str()); | |
670a0494 | 3084 | size_t size(line.size()); |
c390d3ab | 3085 | lprintf("S:%s\n", data); |
ec97ef06 | 3086 | |
5a09ae08 JF |
3087 | if (conffile_r(data, size)) { |
3088 | [delegate_ setConfigurationData:conffile_r[1]]; | |
3089 | } else if (strncmp(data, "status: ", 8) == 0) { | |
3090 | NSString *string = [NSString stringWithUTF8String:(data + 8)]; | |
36bb2ca2 | 3091 | [delegate_ setProgressTitle:string]; |
5a09ae08 | 3092 | } else if (pmstatus_r(data, size)) { |
31f3cfff JF |
3093 | std::string type([pmstatus_r[1] UTF8String]); |
3094 | NSString *id = pmstatus_r[2]; | |
3095 | ||
5a09ae08 JF |
3096 | float percent([pmstatus_r[3] floatValue]); |
3097 | [delegate_ setProgressPercent:(percent / 100)]; | |
3098 | ||
3099 | NSString *string = pmstatus_r[4]; | |
5a09ae08 JF |
3100 | |
3101 | if (type == "pmerror") | |
670a0494 | 3102 | [delegate_ performSelectorOnMainThread:@selector(_setProgressErrorPackage:) |
31f3cfff JF |
3103 | withObject:[NSArray arrayWithObjects:string, id, nil] |
3104 | waitUntilDone:YES | |
3105 | ]; | |
9487f027 | 3106 | else if (type == "pmstatus") { |
5a09ae08 | 3107 | [delegate_ setProgressTitle:string]; |
9487f027 | 3108 | } else if (type == "pmconffile") |
5a09ae08 | 3109 | [delegate_ setConfigurationData:string]; |
670a0494 JF |
3110 | else |
3111 | lprintf("E:unknown pmstatus\n"); | |
3112 | } else | |
3113 | lprintf("E:unknown status\n"); | |
36bb2ca2 | 3114 | } |
ec97ef06 | 3115 | |
670a0494 | 3116 | _assume(false); |
36bb2ca2 | 3117 | } |
ec97ef06 | 3118 | |
affeffc7 | 3119 | - (void) _readOutput:(NSNumber *)fd { _pooled |
36bb2ca2 JF |
3120 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3121 | std::istream is(&ib); | |
3122 | std::string line; | |
3123 | ||
5a09ae08 | 3124 | while (std::getline(is, line)) { |
c390d3ab | 3125 | lprintf("O:%s\n", line.c_str()); |
2388b078 | 3126 | [delegate_ addProgressOutput:[NSString stringWithUTF8String:line.c_str()]]; |
5a09ae08 | 3127 | } |
36bb2ca2 | 3128 | |
670a0494 | 3129 | _assume(false); |
ec97ef06 JF |
3130 | } |
3131 | ||
8b29f8e6 JF |
3132 | - (FILE *) input { |
3133 | return input_; | |
3134 | } | |
3135 | ||
36bb2ca2 | 3136 | - (Package *) packageWithName:(NSString *)name { |
3dd53516 | 3137 | @synchronized (self) { |
5a09ae08 JF |
3138 | if (static_cast<pkgDepCache *>(cache_) == NULL) |
3139 | return nil; | |
36bb2ca2 | 3140 | pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); |
f79a4512 | 3141 | return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:pool_ database:self]; |
28ce8704 | 3142 | } } |
36bb2ca2 | 3143 | |
eb30da80 | 3144 | - (id) init { |
ec97ef06 | 3145 | if ((self = [super init]) != nil) { |
5a09ae08 | 3146 | policy_ = NULL; |
36bb2ca2 JF |
3147 | records_ = NULL; |
3148 | resolver_ = NULL; | |
3149 | fetcher_ = NULL; | |
3150 | lock_ = NULL; | |
ec97ef06 | 3151 | |
f79a4512 JF |
3152 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
3153 | apr_pool_create(&pool_, NULL); | |
3154 | ||
9f357d11 JF |
3155 | size_t capacity(MetaFile_->active_); |
3156 | if (capacity == 0) | |
3157 | capacity = 16384; | |
3158 | else | |
3159 | capacity += 1024; | |
3160 | ||
3161 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, capacity, NULL); | |
ab3f6a01 | 3162 | deadSources_ = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
ec97ef06 | 3163 | |
36bb2ca2 | 3164 | int fds[2]; |
ec97ef06 | 3165 | |
77fcccaf JF |
3166 | _assert(pipe(fds) != -1); |
3167 | cydiafd_ = fds[1]; | |
3168 | ||
3169 | _config->Set("APT::Keep-Fds::", cydiafd_); | |
bc8cd583 | 3170 | setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int)); |
77fcccaf JF |
3171 | |
3172 | [NSThread | |
3173 | detachNewThreadSelector:@selector(_readCydia:) | |
3174 | toTarget:self | |
3931b718 | 3175 | withObject:[NSNumber numberWithInt:fds[0]] |
77fcccaf JF |
3176 | ]; |
3177 | ||
36bb2ca2 JF |
3178 | _assert(pipe(fds) != -1); |
3179 | statusfd_ = fds[1]; | |
ec97ef06 | 3180 | |
36bb2ca2 JF |
3181 | [NSThread |
3182 | detachNewThreadSelector:@selector(_readStatus:) | |
3183 | toTarget:self | |
3931b718 | 3184 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3185 | ]; |
ec97ef06 | 3186 | |
8b29f8e6 JF |
3187 | _assert(pipe(fds) != -1); |
3188 | _assert(dup2(fds[0], 0) != -1); | |
3189 | _assert(close(fds[0]) != -1); | |
3190 | ||
3191 | input_ = fdopen(fds[1], "a"); | |
3192 | ||
36bb2ca2 JF |
3193 | _assert(pipe(fds) != -1); |
3194 | _assert(dup2(fds[1], 1) != -1); | |
3195 | _assert(close(fds[1]) != -1); | |
3196 | ||
3197 | [NSThread | |
3198 | detachNewThreadSelector:@selector(_readOutput:) | |
3199 | toTarget:self | |
3931b718 | 3200 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3201 | ]; |
ec97ef06 JF |
3202 | } return self; |
3203 | } | |
3204 | ||
36bb2ca2 JF |
3205 | - (pkgCacheFile &) cache { |
3206 | return cache_; | |
ec97ef06 JF |
3207 | } |
3208 | ||
5a09ae08 JF |
3209 | - (pkgDepCache::Policy *) policy { |
3210 | return policy_; | |
3211 | } | |
3212 | ||
36bb2ca2 JF |
3213 | - (pkgRecords *) records { |
3214 | return records_; | |
ec97ef06 JF |
3215 | } |
3216 | ||
36bb2ca2 JF |
3217 | - (pkgProblemResolver *) resolver { |
3218 | return resolver_; | |
ec97ef06 JF |
3219 | } |
3220 | ||
36bb2ca2 JF |
3221 | - (pkgAcquire &) fetcher { |
3222 | return *fetcher_; | |
ec97ef06 JF |
3223 | } |
3224 | ||
a3328c28 JF |
3225 | - (pkgSourceList &) list { |
3226 | return *list_; | |
3227 | } | |
3228 | ||
36bb2ca2 | 3229 | - (NSArray *) packages { |
077e9d90 | 3230 | return (NSArray *) packages_; |
ec97ef06 JF |
3231 | } |
3232 | ||
7b0ce2da | 3233 | - (NSArray *) sources { |
68d927e2 JF |
3234 | NSMutableArray *sources([NSMutableArray arrayWithCapacity:sources_.size()]); |
3235 | for (SourceMap::const_iterator i(sources_.begin()); i != sources_.end(); ++i) | |
3236 | [sources addObject:i->second]; | |
ab3f6a01 | 3237 | [sources addObjectsFromArray:(NSArray *)deadSources_]; |
68d927e2 | 3238 | return sources; |
7b0ce2da JF |
3239 | } |
3240 | ||
affeffc7 JF |
3241 | - (NSArray *) issues { |
3242 | if (cache_->BrokenCount() == 0) | |
3243 | return nil; | |
3244 | ||
3245 | NSMutableArray *issues([NSMutableArray arrayWithCapacity:4]); | |
3246 | ||
077e9d90 | 3247 | for (Package *package in [self packages]) { |
affeffc7 JF |
3248 | if (![package broken]) |
3249 | continue; | |
3250 | pkgCache::PkgIterator pkg([package iterator]); | |
3251 | ||
3252 | NSMutableArray *entry([NSMutableArray arrayWithCapacity:4]); | |
3253 | [entry addObject:[package name]]; | |
3254 | [issues addObject:entry]; | |
3255 | ||
3256 | pkgCache::VerIterator ver(cache_[pkg].InstVerIter(cache_)); | |
3257 | if (ver.end()) | |
3258 | continue; | |
3259 | ||
3260 | for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) { | |
3261 | pkgCache::DepIterator start; | |
3262 | pkgCache::DepIterator end; | |
3263 | dep.GlobOr(start, end); // ++dep | |
3264 | ||
3265 | if (!cache_->IsImportantDep(end)) | |
3266 | continue; | |
3267 | if ((cache_[end] & pkgDepCache::DepGInstall) != 0) | |
3268 | continue; | |
3269 | ||
3270 | NSMutableArray *failure([NSMutableArray arrayWithCapacity:4]); | |
3271 | [entry addObject:failure]; | |
3272 | [failure addObject:[NSString stringWithUTF8String:start.DepType()]]; | |
3273 | ||
744f398e JF |
3274 | NSString *name([NSString stringWithUTF8String:start.TargetPkg().Name()]); |
3275 | if (Package *package = [self packageWithName:name]) | |
3276 | name = [package name]; | |
3277 | [failure addObject:name]; | |
affeffc7 JF |
3278 | |
3279 | pkgCache::PkgIterator target(start.TargetPkg()); | |
3280 | if (target->ProvidesList != 0) | |
3281 | [failure addObject:@"?"]; | |
3282 | else { | |
3283 | pkgCache::VerIterator ver(cache_[target].InstVerIter(cache_)); | |
3284 | if (!ver.end()) | |
3285 | [failure addObject:[NSString stringWithUTF8String:ver.VerStr()]]; | |
3286 | else if (!cache_[target].CandidateVerIter(cache_).end()) | |
3287 | [failure addObject:@"-"]; | |
3288 | else if (target->ProvidesList == 0) | |
3289 | [failure addObject:@"!"]; | |
3290 | else | |
3291 | [failure addObject:@"%"]; | |
3292 | } | |
3293 | ||
3294 | _forever { | |
3295 | if (start.TargetVer() != 0) | |
3296 | [failure addObject:[NSString stringWithFormat:@"%s %s", start.CompType(), start.TargetVer()]]; | |
3297 | if (start == end) | |
3298 | break; | |
3299 | ++start; | |
3300 | } | |
3301 | } | |
3302 | } | |
3303 | ||
3304 | return issues; | |
3305 | } | |
3306 | ||
670a0494 JF |
3307 | - (bool) popErrorWithTitle:(NSString *)title { |
3308 | bool fatal(false); | |
3309 | std::string message; | |
3310 | ||
3311 | while (!_error->empty()) { | |
3312 | std::string error; | |
3313 | bool warning(!_error->PopMessage(error)); | |
3314 | if (!warning) | |
3315 | fatal = true; | |
3316 | for (;;) { | |
3317 | size_t size(error.size()); | |
3318 | if (size == 0 || error[size - 1] != '\n') | |
3319 | break; | |
3320 | error.resize(size - 1); | |
3321 | } | |
3322 | lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); | |
3323 | ||
3324 | if (!message.empty()) | |
3325 | message += "\n\n"; | |
3326 | message += error; | |
3327 | } | |
3328 | ||
ef348024 | 3329 | if (fatal && !message.empty()) |
670a0494 JF |
3330 | [delegate_ _setProgressError:[NSString stringWithUTF8String:message.c_str()] withTitle:[NSString stringWithFormat:Colon_, fatal ? Error_ : Warning_, title]]; |
3331 | ||
3332 | return fatal; | |
3333 | } | |
3334 | ||
3335 | - (bool) popErrorWithTitle:(NSString *)title forOperation:(bool)success { | |
3336 | return [self popErrorWithTitle:title] || !success; | |
3337 | } | |
3338 | ||
53bed0ff | 3339 | - (void) reloadData { CYPoolStart() { |
3dd53516 JF |
3340 | @synchronized (self) { |
3341 | ++era_; | |
a1440b10 | 3342 | |
0944377b | 3343 | [self releasePackages]; |
ab3f6a01 | 3344 | |
843e75b8 | 3345 | sources_.clear(); |
ab3f6a01 | 3346 | CFArrayRemoveAllValues(deadSources_); |
843e75b8 | 3347 | |
36bb2ca2 | 3348 | _error->Discard(); |
5a09ae08 | 3349 | |
36bb2ca2 | 3350 | delete list_; |
5a09ae08 | 3351 | list_ = NULL; |
36bb2ca2 JF |
3352 | manager_ = NULL; |
3353 | delete lock_; | |
5a09ae08 | 3354 | lock_ = NULL; |
36bb2ca2 | 3355 | delete fetcher_; |
5a09ae08 | 3356 | fetcher_ = NULL; |
36bb2ca2 | 3357 | delete resolver_; |
5a09ae08 | 3358 | resolver_ = NULL; |
36bb2ca2 | 3359 | delete records_; |
5a09ae08 JF |
3360 | records_ = NULL; |
3361 | delete policy_; | |
3362 | policy_ = NULL; | |
36bb2ca2 | 3363 | |
5a09ae08 | 3364 | cache_.Close(); |
8b29f8e6 | 3365 | |
f79a4512 | 3366 | apr_pool_clear(pool_); |
a020a50e | 3367 | |
f79a4512 | 3368 | NSRecycleZone(zone_); |
a020a50e | 3369 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3370 | |
7376b55c JF |
3371 | int chk(creat("/tmp/cydia.chk", 0644)); |
3372 | if (chk != -1) | |
3373 | close(chk); | |
3374 | ||
670a0494 JF |
3375 | NSString *title(UCLocalize("DATABASE")); |
3376 | ||
9487f027 | 3377 | _trace(); |
670a0494 | 3378 | if (!cache_.Open(progress_, true)) { pop: |
8b29f8e6 | 3379 | std::string error; |
670a0494 | 3380 | bool warning(!_error->PopMessage(error)); |
c390d3ab | 3381 | lprintf("cache_.Open():[%s]\n", error.c_str()); |
5a09ae08 JF |
3382 | |
3383 | if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ") | |
3384 | [delegate_ repairWithSelector:@selector(configure)]; | |
3385 | else if (error == "The package lists or status file could not be parsed or opened.") | |
3386 | [delegate_ repairWithSelector:@selector(update)]; | |
9ea8d159 JF |
3387 | // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)") |
3388 | // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)") | |
3389 | // else if (error == "The list of sources could not be read.") | |
670a0494 JF |
3390 | else |
3391 | [delegate_ _setProgressError:[NSString stringWithUTF8String:error.c_str()] withTitle:[NSString stringWithFormat:Colon_, warning ? Warning_ : Error_, title]]; | |
5a09ae08 | 3392 | |
670a0494 JF |
3393 | if (warning) |
3394 | goto pop; | |
3395 | _error->Discard(); | |
5a09ae08 | 3396 | return; |
36bb2ca2 | 3397 | } |
9487f027 | 3398 | _trace(); |
36bb2ca2 | 3399 | |
7376b55c JF |
3400 | unlink("/tmp/cydia.chk"); |
3401 | ||
31bc18a7 | 3402 | now_ = [[NSDate date] timeIntervalSince1970]; |
36bb2ca2 | 3403 | |
5a09ae08 | 3404 | policy_ = new pkgDepCache::Policy(); |
36bb2ca2 JF |
3405 | records_ = new pkgRecords(cache_); |
3406 | resolver_ = new pkgProblemResolver(cache_); | |
3407 | fetcher_ = new pkgAcquire(&status_); | |
3408 | lock_ = NULL; | |
3409 | ||
3410 | list_ = new pkgSourceList(); | |
670a0494 JF |
3411 | if ([self popErrorWithTitle:title forOperation:list_->ReadMainList()]) |
3412 | return; | |
3413 | ||
3414 | if (cache_->DelCount() != 0 || cache_->InstCount() != 0) { | |
3415 | [delegate_ _setProgressError:@"COUNTS_NONZERO_EX" withTitle:title]; | |
3416 | return; | |
3417 | } | |
36bb2ca2 | 3418 | |
670a0494 JF |
3419 | if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) |
3420 | return; | |
9bedffaa JF |
3421 | |
3422 | if (cache_->BrokenCount() != 0) { | |
670a0494 JF |
3423 | if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) |
3424 | return; | |
3425 | ||
3426 | if (cache_->BrokenCount() != 0) { | |
3427 | [delegate_ _setProgressError:@"STILL_BROKEN_EX" withTitle:title]; | |
3428 | return; | |
3429 | } | |
3430 | ||
3431 | if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) | |
3432 | return; | |
9bedffaa JF |
3433 | } |
3434 | ||
68d927e2 | 3435 | for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { |
ab3f6a01 | 3436 | bool found = false; |
68d927e2 JF |
3437 | std::vector<pkgIndexFile *> *indices = (*source)->GetIndexFiles(); |
3438 | for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index) | |
3439 | // XXX: this could be more intelligent | |
3440 | if (dynamic_cast<debPackagesIndex *>(*index) != NULL) { | |
3441 | pkgCache::PkgFileIterator cached((*index)->FindInCache(cache_)); | |
ab3f6a01 | 3442 | if (!cached.end()) { |
f9f6d9e8 | 3443 | sources_[cached->ID] = [[[Source alloc] initWithMetaIndex:*source inPool:pool_] autorelease]; |
ab3f6a01 RP |
3444 | found = true; |
3445 | } | |
68d927e2 | 3446 | } |
ab3f6a01 RP |
3447 | if (!found) |
3448 | CFArrayAppendValue(deadSources_, [[[Source alloc] initWithMetaIndex:*source inPool:pool_] autorelease]); | |
36bb2ca2 | 3449 | } |
68d927e2 | 3450 | |
677b8415 | 3451 | { |
9fcbca29 | 3452 | /*std::vector<Package *> packages; |
677b8415 | 3453 | packages.reserve(std::max(10000U, [packages_ count] + 1000)); |
677b8415 | 3454 | [packages_ release]; |
9fcbca29 JF |
3455 | packages_ = nil;*/ |
3456 | ||
677b8415 JF |
3457 | _trace(); |
3458 | ||
3459 | for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) | |
3460 | if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:pool_ database:self]) | |
9fcbca29 | 3461 | //packages.push_back(package); |
077e9d90 | 3462 | CFArrayAppendValue(packages_, [package retain]); |
677b8415 JF |
3463 | |
3464 | _trace(); | |
3465 | ||
9fcbca29 | 3466 | /*if (packages.empty()) |
677b8415 JF |
3467 | packages_ = [[NSArray alloc] init]; |
3468 | else | |
3469 | packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; | |
9fcbca29 JF |
3470 | _trace();*/ |
3471 | ||
077e9d90 JF |
3472 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<SKRadixFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(16)]; |
3473 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<SKRadixFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(4)]; | |
3474 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<SKRadixFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(0)]; | |
9fcbca29 JF |
3475 | |
3476 | /*_trace(); | |
3477 | PrintTimes(); | |
3478 | _trace();*/ | |
3479 | ||
3480 | _trace(); | |
3481 | ||
3482 | /*if (!packages.empty()) | |
3483 | CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare_), NULL);*/ | |
3484 | //std::sort(packages.begin(), packages.end(), PackageNameOrdering()); | |
3485 | ||
eef4ccaf JF |
3486 | //CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); |
3487 | ||
077e9d90 | 3488 | CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); |
9fcbca29 JF |
3489 | |
3490 | //[packages_ sortUsingFunction:reinterpret_cast<NSComparisonResult (*)(id, id, void *)>(&PackageNameCompare) context:NULL]; | |
677b8415 JF |
3491 | |
3492 | _trace(); | |
cd95e390 JF |
3493 | |
3494 | size_t count(CFArrayGetCount(packages_)); | |
9f357d11 JF |
3495 | MetaFile_->active_ = count; |
3496 | ||
cd95e390 JF |
3497 | for (size_t index(0); index != count; ++index) |
3498 | [(Package *) CFArrayGetValueAtIndex(packages_, index) setIndex:index]; | |
3499 | ||
3500 | _trace(); | |
677b8415 | 3501 | } |
c626a63f | 3502 | } } CYPoolEnd() _trace(); } |
ec97ef06 | 3503 | |
c6ca67ba JF |
3504 | - (void) clear { |
3505 | @synchronized (self) { | |
3506 | delete resolver_; | |
3507 | resolver_ = new pkgProblemResolver(cache_); | |
3508 | ||
3509 | for (pkgCache::PkgIterator iterator(cache_->PkgBegin()); !iterator.end(); ++iterator) { | |
3510 | if (!cache_[iterator].Keep()) { | |
3511 | cache_->MarkKeep(iterator, false); | |
3512 | cache_->SetReInstall(iterator, false); | |
3513 | } | |
3514 | } | |
3515 | } } | |
3516 | ||
5a09ae08 JF |
3517 | - (void) configure { |
3518 | NSString *dpkg = [NSString stringWithFormat:@"dpkg --configure -a --status-fd %u", statusfd_]; | |
985d2dff | 3519 | _trace(); |
5a09ae08 | 3520 | system([dpkg UTF8String]); |
985d2dff | 3521 | _trace(); |
5a09ae08 JF |
3522 | } |
3523 | ||
670a0494 JF |
3524 | - (bool) clean { |
3525 | // XXX: I don't remember this condition | |
77fcccaf | 3526 | if (lock_ != NULL) |
670a0494 | 3527 | return false; |
77fcccaf JF |
3528 | |
3529 | FileFd Lock; | |
3530 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
3531 | |
3532 | NSString *title(UCLocalize("CLEAN_ARCHIVES")); | |
3533 | ||
3534 | if ([self popErrorWithTitle:title]) | |
3535 | return false; | |
77fcccaf JF |
3536 | |
3537 | pkgAcquire fetcher; | |
3538 | fetcher.Clean(_config->FindDir("Dir::Cache::Archives")); | |
3539 | ||
9bedffaa JF |
3540 | class LogCleaner : |
3541 | public pkgArchiveCleaner | |
3542 | { | |
77fcccaf JF |
3543 | protected: |
3544 | virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) { | |
9bedffaa | 3545 | unlink(File); |
77fcccaf JF |
3546 | } |
3547 | } cleaner; | |
3548 | ||
670a0494 JF |
3549 | if ([self popErrorWithTitle:title forOperation:cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)]) |
3550 | return false; | |
3551 | ||
3552 | return true; | |
77fcccaf JF |
3553 | } |
3554 | ||
670a0494 | 3555 | - (bool) prepare { |
744f398e JF |
3556 | fetcher_->Shutdown(); |
3557 | ||
36bb2ca2 JF |
3558 | pkgRecords records(cache_); |
3559 | ||
3560 | lock_ = new FileFd(); | |
3561 | lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
3562 | |
3563 | NSString *title(UCLocalize("PREPARE_ARCHIVES")); | |
3564 | ||
3565 | if ([self popErrorWithTitle:title]) | |
3566 | return false; | |
36bb2ca2 JF |
3567 | |
3568 | pkgSourceList list; | |
670a0494 JF |
3569 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3570 | return false; | |
36bb2ca2 JF |
3571 | |
3572 | manager_ = (_system->CreatePM(cache_)); | |
670a0494 JF |
3573 | if ([self popErrorWithTitle:title forOperation:manager_->GetArchives(fetcher_, &list, &records)]) |
3574 | return false; | |
3575 | ||
3576 | return true; | |
ec97ef06 JF |
3577 | } |
3578 | ||
36bb2ca2 | 3579 | - (void) perform { |
670a0494 JF |
3580 | NSString *title(UCLocalize("PERFORM_SELECTIONS")); |
3581 | ||
a72074b2 JF |
3582 | NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { |
3583 | pkgSourceList list; | |
670a0494 JF |
3584 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3585 | return; | |
a72074b2 JF |
3586 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
3587 | [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
3588 | } | |
a0be02eb | 3589 | |
eeb9b112 JF |
3590 | if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) { |
3591 | _trace(); | |
36bb2ca2 | 3592 | return; |
eeb9b112 JF |
3593 | } |
3594 | ||
54043703 JF |
3595 | [CydiaApp retainNetworkActivityIndicator]; |
3596 | ||
eeb9b112 JF |
3597 | bool failed = false; |
3598 | for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) { | |
3599 | if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete) | |
3600 | continue; | |
6204f56a JF |
3601 | if ((*item)->Status == pkgAcquire::Item::StatIdle) |
3602 | continue; | |
eeb9b112 JF |
3603 | |
3604 | std::string uri = (*item)->DescURI(); | |
3605 | std::string error = (*item)->ErrorText; | |
3606 | ||
c390d3ab | 3607 | lprintf("pAf:%s:%s\n", uri.c_str(), error.c_str()); |
eeb9b112 JF |
3608 | failed = true; |
3609 | ||
670a0494 | 3610 | [delegate_ performSelectorOnMainThread:@selector(_setProgressErrorPackage:) |
a3328c28 JF |
3611 | withObject:[NSArray arrayWithObjects: |
3612 | [NSString stringWithUTF8String:error.c_str()], | |
3613 | nil] | |
eeb9b112 JF |
3614 | waitUntilDone:YES |
3615 | ]; | |
3616 | } | |
3617 | ||
54043703 JF |
3618 | [CydiaApp releaseNetworkActivityIndicator]; |
3619 | ||
eeb9b112 JF |
3620 | if (failed) { |
3621 | _trace(); | |
3622 | return; | |
3623 | } | |
36bb2ca2 JF |
3624 | |
3625 | _system->UnLock(); | |
3626 | pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_); | |
3627 | ||
eeb9b112 JF |
3628 | if (_error->PendingError()) { |
3629 | _trace(); | |
36bb2ca2 | 3630 | return; |
eeb9b112 JF |
3631 | } |
3632 | ||
3633 | if (result == pkgPackageManager::Failed) { | |
3634 | _trace(); | |
36bb2ca2 | 3635 | return; |
eeb9b112 JF |
3636 | } |
3637 | ||
3638 | if (result != pkgPackageManager::Completed) { | |
3639 | _trace(); | |
36bb2ca2 | 3640 | return; |
eeb9b112 | 3641 | } |
a0be02eb | 3642 | |
a72074b2 JF |
3643 | NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { |
3644 | pkgSourceList list; | |
670a0494 JF |
3645 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3646 | return; | |
a72074b2 JF |
3647 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
3648 | [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
3649 | } | |
3650 | ||
3651 | if (![before isEqualToArray:after]) | |
3652 | [self update]; | |
ec97ef06 JF |
3653 | } |
3654 | ||
670a0494 JF |
3655 | - (bool) upgrade { |
3656 | NSString *title(UCLocalize("UPGRADE")); | |
3657 | if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) | |
3658 | return false; | |
3659 | return true; | |
c7c6384e JF |
3660 | } |
3661 | ||
36bb2ca2 JF |
3662 | - (void) update { |
3663 | [self updateWithStatus:status_]; | |
3664 | } | |
b4d89997 | 3665 | |
670a0494 JF |
3666 | - (void) updateWithStatus:(Status &)status { |
3667 | _transient NSObject<ProgressDelegate> *delegate(status.getDelegate()); | |
3668 | NSString *title(UCLocalize("REFRESHING_DATA")); | |
3669 | ||
36bb2ca2 | 3670 | pkgSourceList list; |
670a0494 JF |
3671 | if (!list.ReadMainList()) |
3672 | [delegate _setProgressError:@"Unable to read source list." withTitle:title]; | |
b4d89997 | 3673 | |
36bb2ca2 JF |
3674 | FileFd lock; |
3675 | lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
670a0494 JF |
3676 | if ([self popErrorWithTitle:title]) |
3677 | return; | |
18873623 | 3678 | |
670a0494 | 3679 | if ([self popErrorWithTitle:title forOperation:ListUpdate(status, list, PulseInterval_)]) |
eb30da80 GP |
3680 | /* XXX: ignore this because users suck and don't understand why refreshing is important: return */ |
3681 | /* XXX: why the hell is an empty if statement a clang error? */ (void) 0; | |
36bb2ca2 | 3682 | |
7623f855 JF |
3683 | [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"]; |
3684 | Changed_ = true; | |
b4d89997 JF |
3685 | } |
3686 | ||
36bb2ca2 JF |
3687 | - (void) setDelegate:(id)delegate { |
3688 | delegate_ = delegate; | |
3689 | status_.setDelegate(delegate); | |
3690 | progress_.setDelegate(delegate); | |
3691 | } | |
b4d89997 | 3692 | |
7376b55c | 3693 | - (Source *) getSource:(pkgCache::PkgFileIterator)file { |
20cb1497 JF |
3694 | SourceMap::const_iterator i(sources_.find(file->ID)); |
3695 | return i == sources_.end() ? nil : i->second; | |
b19871dd JF |
3696 | } |
3697 | ||
fe33a23e | 3698 | - (NSString *) mappedSectionForPointer:(const char *)section { |
4905df00 | 3699 | _H<NSString> *mapped; |
fe33a23e | 3700 | |
4905df00 JF |
3701 | _profile(Database$mappedSectionForPointer$Cache) |
3702 | mapped = §ions_[section]; | |
3703 | _end | |
3704 | ||
3705 | if (*mapped == NULL) { | |
fe33a23e JF |
3706 | size_t length(strlen(section)); |
3707 | char spaced[length + 1]; | |
3708 | ||
3709 | _profile(Database$mappedSectionForPointer$Replace) | |
3710 | for (size_t index(0); index != length; ++index) | |
3711 | spaced[index] = section[index] == '_' ? ' ' : section[index]; | |
3712 | spaced[length] = '\0'; | |
3713 | _end | |
3714 | ||
3715 | NSString *string; | |
3716 | ||
3717 | _profile(Database$mappedSectionForPointer$stringWithUTF8String) | |
3718 | string = [NSString stringWithUTF8String:spaced]; | |
3719 | _end | |
3720 | ||
3721 | _profile(Database$mappedSectionForPointer$Map) | |
4905df00 | 3722 | string = [SectionMap_ objectForKey:string] ?: string; |
fe33a23e | 3723 | _end |
4905df00 JF |
3724 | |
3725 | *mapped = string; | |
3726 | } return *mapped; | |
fe33a23e JF |
3727 | } |
3728 | ||
36bb2ca2 JF |
3729 | @end |
3730 | /* }}} */ | |
b4d89997 | 3731 | |
43f3d7f6 JF |
3732 | /* Web Scripting {{{ */ |
3733 | @interface CydiaObject : NSObject { | |
3734 | id indirect_; | |
3931b718 | 3735 | _transient id delegate_; |
43f3d7f6 | 3736 | } |
c390d3ab | 3737 | |
43f3d7f6 | 3738 | - (id) initWithDelegate:(IndirectDelegate *)indirect; |
c390d3ab JF |
3739 | @end |
3740 | ||
43f3d7f6 | 3741 | @implementation CydiaObject |
c390d3ab JF |
3742 | |
3743 | - (void) dealloc { | |
43f3d7f6 | 3744 | [indirect_ release]; |
c390d3ab JF |
3745 | [super dealloc]; |
3746 | } | |
3747 | ||
43f3d7f6 JF |
3748 | - (id) initWithDelegate:(IndirectDelegate *)indirect { |
3749 | if ((self = [super init]) != nil) { | |
3750 | indirect_ = [indirect retain]; | |
3751 | } return self; | |
3752 | } | |
3753 | ||
77801ff1 JF |
3754 | - (void) setDelegate:(id)delegate { |
3755 | delegate_ = delegate; | |
3756 | } | |
3757 | ||
43f3d7f6 JF |
3758 | + (NSArray *) _attributeKeys { |
3759 | return [NSArray arrayWithObjects:@"device", @"firewire", @"imei", @"mac", @"serial", nil]; | |
3760 | } | |
3761 | ||
3762 | - (NSArray *) attributeKeys { | |
3763 | return [[self class] _attributeKeys]; | |
c390d3ab JF |
3764 | } |
3765 | ||
43f3d7f6 JF |
3766 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
3767 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
c390d3ab | 3768 | } |
43f3d7f6 JF |
3769 | |
3770 | - (NSString *) device { | |
3771 | return [[UIDevice currentDevice] uniqueIdentifier]; | |
c390d3ab JF |
3772 | } |
3773 | ||
43f3d7f6 JF |
3774 | #if 0 // XXX: implement! |
3775 | - (NSString *) mac { | |
3776 | if (![indirect_ promptForSensitive:@"Mac Address"]) | |
3777 | return nil; | |
affeffc7 JF |
3778 | } |
3779 | ||
43f3d7f6 JF |
3780 | - (NSString *) serial { |
3781 | if (![indirect_ promptForSensitive:@"Serial #"]) | |
3782 | return nil; | |
3783 | } | |
86316a91 | 3784 | |
43f3d7f6 JF |
3785 | - (NSString *) firewire { |
3786 | if (![indirect_ promptForSensitive:@"Firewire GUID"]) | |
3787 | return nil; | |
affeffc7 JF |
3788 | } |
3789 | ||
43f3d7f6 JF |
3790 | - (NSString *) imei { |
3791 | if (![indirect_ promptForSensitive:@"IMEI"]) | |
3792 | return nil; | |
3793 | } | |
3794 | #endif | |
3795 | ||
3796 | + (NSString *) webScriptNameForSelector:(SEL)selector { | |
3797 | if (selector == @selector(close)) | |
3798 | return @"close"; | |
8cc8eb1c JF |
3799 | else if (selector == @selector(getInstalledPackages)) |
3800 | return @"getInstalledPackages"; | |
43f3d7f6 JF |
3801 | else if (selector == @selector(getPackageById:)) |
3802 | return @"getPackageById"; | |
77801ff1 JF |
3803 | else if (selector == @selector(installPackages:)) |
3804 | return @"installPackages"; | |
43f3d7f6 JF |
3805 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) |
3806 | return @"setButtonImage"; | |
3807 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
3808 | return @"setButtonTitle"; | |
43f3d7f6 JF |
3809 | else if (selector == @selector(setPopupHook:)) |
3810 | return @"setPopupHook"; | |
3811 | else if (selector == @selector(setSpecial:)) | |
3812 | return @"setSpecial"; | |
ef055c6c JF |
3813 | else if (selector == @selector(setToken:)) |
3814 | return @"setToken"; | |
43f3d7f6 JF |
3815 | else if (selector == @selector(setViewportWidth:)) |
3816 | return @"setViewportWidth"; | |
3817 | else if (selector == @selector(supports:)) | |
3818 | return @"supports"; | |
3819 | else if (selector == @selector(stringWithFormat:arguments:)) | |
3820 | return @"format"; | |
3821 | else if (selector == @selector(localizedStringForKey:value:table:)) | |
3822 | return @"localize"; | |
3823 | else if (selector == @selector(du:)) | |
3824 | return @"du"; | |
3825 | else if (selector == @selector(statfs:)) | |
3826 | return @"statfs"; | |
c390d3ab | 3827 | else |
43f3d7f6 JF |
3828 | return nil; |
3829 | } | |
3830 | ||
3831 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
3832 | return [self webScriptNameForSelector:selector] == nil; | |
c390d3ab JF |
3833 | } |
3834 | ||
43f3d7f6 JF |
3835 | - (BOOL) supports:(NSString *)feature { |
3836 | return [feature isEqualToString:@"window.open"]; | |
3837 | } | |
c390d3ab | 3838 | |
8cc8eb1c JF |
3839 | - (NSArray *) getInstalledPackages { |
3840 | NSArray *packages([[Database sharedInstance] packages]); | |
f4db946e | 3841 | NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]); |
77801ff1 | 3842 | for (Package *package in packages) |
8cc8eb1c JF |
3843 | if ([package installed] != nil) |
3844 | [installed addObject:package]; | |
3845 | return installed; | |
3846 | } | |
3847 | ||
43f3d7f6 | 3848 | - (Package *) getPackageById:(NSString *)id { |
043e6a2e JF |
3849 | Package *package([[Database sharedInstance] packageWithName:id]); |
3850 | [package parse]; | |
3851 | return package; | |
43f3d7f6 JF |
3852 | } |
3853 | ||
3854 | - (NSArray *) statfs:(NSString *)path { | |
3855 | struct statfs stat; | |
3856 | ||
3857 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
3858 | return nil; | |
3859 | ||
3860 | return [NSArray arrayWithObjects: | |
3861 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
3862 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
3863 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
3864 | nil]; | |
3865 | } | |
3866 | ||
3867 | - (NSNumber *) du:(NSString *)path { | |
3868 | NSNumber *value(nil); | |
3869 | ||
3870 | int fds[2]; | |
3871 | _assert(pipe(fds) != -1); | |
3872 | ||
3873 | pid_t pid(ExecFork()); | |
3874 | if (pid == 0) { | |
3875 | _assert(dup2(fds[1], 1) != -1); | |
3876 | _assert(close(fds[0]) != -1); | |
3877 | _assert(close(fds[1]) != -1); | |
3878 | /* XXX: this should probably not use du */ | |
3879 | execl("/usr/libexec/cydia/du", "du", "-s", [path UTF8String], NULL); | |
3880 | exit(1); | |
3881 | _assert(false); | |
3882 | } | |
3883 | ||
3884 | _assert(close(fds[1]) != -1); | |
3885 | ||
3886 | if (FILE *du = fdopen(fds[0], "r")) { | |
3887 | char line[1024]; | |
3888 | while (fgets(line, sizeof(line), du) != NULL) { | |
3889 | size_t length(strlen(line)); | |
3890 | while (length != 0 && line[length - 1] == '\n') | |
3891 | line[--length] = '\0'; | |
3892 | if (char *tab = strchr(line, '\t')) { | |
3893 | *tab = '\0'; | |
3894 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
3895 | } | |
3896 | } | |
3897 | ||
3898 | fclose(du); | |
3899 | } else _assert(close(fds[0])); | |
3900 | ||
3901 | int status; | |
3902 | wait: | |
3903 | if (waitpid(pid, &status, 0) == -1) | |
3904 | if (errno == EINTR) | |
3905 | goto wait; | |
3906 | else _assert(false); | |
3907 | ||
3908 | return value; | |
3909 | } | |
3910 | ||
3911 | - (void) close { | |
3912 | [indirect_ close]; | |
3913 | } | |
3914 | ||
77801ff1 JF |
3915 | - (void) installPackages:(NSArray *)packages { |
3916 | [delegate_ performSelectorOnMainThread:@selector(installPackages:) withObject:packages waitUntilDone:NO]; | |
3917 | } | |
3918 | ||
43f3d7f6 JF |
3919 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
3920 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
3921 | } | |
3922 | ||
3923 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
3924 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
3925 | } | |
3926 | ||
3927 | - (void) setSpecial:(id)function { | |
3928 | [indirect_ setSpecial:function]; | |
3929 | } | |
3930 | ||
ef055c6c JF |
3931 | - (void) setToken:(NSString *)token { |
3932 | if (Token_ != nil) | |
3933 | [Token_ release]; | |
3934 | Token_ = [token retain]; | |
3935 | ||
3936 | [Metadata_ setObject:Token_ forKey:@"Token"]; | |
3937 | Changed_ = true; | |
3938 | } | |
3939 | ||
43f3d7f6 JF |
3940 | - (void) setPopupHook:(id)function { |
3941 | [indirect_ setPopupHook:function]; | |
3942 | } | |
3943 | ||
3944 | - (void) setViewportWidth:(float)width { | |
3945 | [indirect_ setViewportWidth:width]; | |
3946 | } | |
3947 | ||
3948 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
3949 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
3950 | unsigned count([arguments count]); | |
3951 | id values[count]; | |
3952 | for (unsigned i(0); i != count; ++i) | |
3953 | values[i] = [arguments objectAtIndex:i]; | |
eb30da80 | 3954 | return [[[NSString alloc] initWithFormat:format arguments:*(reinterpret_cast<va_list *>(&values))] autorelease]; |
43f3d7f6 JF |
3955 | } |
3956 | ||
3957 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
a95e0405 JF |
3958 | if (reinterpret_cast<id>(value) == [WebUndefined undefined]) |
3959 | value = nil; | |
43f3d7f6 JF |
3960 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) |
3961 | table = nil; | |
3962 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
c390d3ab JF |
3963 | } |
3964 | ||
3965 | @end | |
3966 | /* }}} */ | |
f79a4512 | 3967 | |
dd5f8161 GP |
3968 | /* @ Loading... Indicator {{{ */ |
3969 | @interface CYLoadingIndicator : UIView { | |
3970 | UIActivityIndicatorView *spinner_; | |
3971 | UILabel *label_; | |
3972 | UIView *container_; | |
3973 | } | |
3974 | ||
3975 | @property (readonly, nonatomic) UILabel *label; | |
3976 | @property (readonly, nonatomic) UIActivityIndicatorView *activityIndicatorView; | |
3977 | ||
3978 | @end | |
3979 | ||
3980 | @implementation CYLoadingIndicator | |
3981 | ||
3982 | - (id)initWithFrame:(CGRect)frame { | |
3983 | if ((self = [super initWithFrame:frame])) { | |
3984 | container_ = [[[UIView alloc] init] autorelease]; | |
3985 | [container_ setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
3986 | ||
3987 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease]; | |
3988 | [spinner_ startAnimating]; | |
3989 | [container_ addSubview:spinner_]; | |
3990 | ||
3991 | label_ = [[[UILabel alloc] init] autorelease]; | |
3992 | [label_ setFont:[UIFont boldSystemFontOfSize:15.0f]]; | |
3993 | [label_ setBackgroundColor:[UIColor clearColor]]; | |
3994 | [label_ setTextColor:[UIColor blackColor]]; | |
3995 | [label_ setShadowColor:[UIColor whiteColor]]; | |
3996 | [label_ setShadowOffset:CGSizeMake(0, 1)]; | |
3997 | [label_ setText:[NSString stringWithFormat:Elision_, UCLocalize("LOADING"), nil]]; | |
3998 | [container_ addSubview:label_]; | |
3999 | ||
4000 | CGSize viewsize = frame.size; | |
4001 | CGSize spinnersize = [spinner_ bounds].size; | |
4002 | CGSize textsize = [[label_ text] sizeWithFont:[label_ font]]; | |
4003 | float bothwidth = spinnersize.width + textsize.width + 5.0f; | |
4004 | ||
4005 | CGRect containrect = { | |
4006 | CGPointMake(floorf((viewsize.width / 2) - (bothwidth / 2)), floorf((viewsize.height / 2) - (spinnersize.height / 2))), | |
4007 | CGSizeMake(bothwidth, spinnersize.height) | |
4008 | }; | |
4009 | CGRect textrect = { | |
4010 | CGPointMake(spinnersize.width + 5.0f, floorf((spinnersize.height / 2) - (textsize.height / 2))), | |
4011 | textsize | |
4012 | }; | |
4013 | CGRect spinrect = { | |
4014 | CGPointZero, | |
4015 | spinnersize | |
4016 | }; | |
4017 | ||
4018 | [container_ setFrame:containrect]; | |
4019 | [spinner_ setFrame:spinrect]; | |
4020 | [label_ setFrame:textrect]; | |
4021 | [self addSubview:container_]; | |
4022 | } | |
4023 | ||
4024 | return self; | |
4025 | } | |
4026 | ||
4027 | - (UILabel *)label { return label_; } | |
4028 | - (UIActivityIndicatorView *)activityIndicatorView { return spinner_; } | |
4029 | ||
4030 | @end | |
4031 | /* }}} */ | |
be45a8cf | 4032 | /* Emulated Loading Controller {{{ */ |
5e565049 | 4033 | @interface CYEmulatedLoadingController : CYViewController { |
f6e13561 GP |
4034 | CYLoadingIndicator *indicator_; |
4035 | UITabBar *tabbar_; | |
4036 | UINavigationBar *navbar_; | |
4037 | } | |
4038 | @end | |
4039 | ||
4040 | @implementation CYEmulatedLoadingController | |
4041 | ||
4042 | - (CYEmulatedLoadingController *) init { | |
4043 | if ((self = [super init])) { | |
4044 | [[self view] setBackgroundColor:[UIColor pinStripeColor]]; | |
4045 | ||
4046 | indicator_ = [[CYLoadingIndicator alloc] initWithFrame:[[self view] bounds]]; | |
4047 | [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
4048 | [[self view] addSubview:indicator_]; | |
4049 | [indicator_ release]; | |
4050 | ||
4051 | tabbar_ = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 0, 49.0f)]; | |
4052 | [tabbar_ setFrame:CGRectMake(0.0f, [[self view] bounds].size.height - [tabbar_ bounds].size.height, [[self view] bounds].size.width, [tabbar_ bounds].size.height)]; | |
4053 | [tabbar_ setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth]; | |
4054 | [[self view] addSubview:tabbar_]; | |
4055 | [tabbar_ release]; | |
4056 | ||
4057 | navbar_ = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 0, 44.0f)]; | |
4058 | [navbar_ setFrame:CGRectMake(0.0f, 0.0f, [[self view] bounds].size.width, [navbar_ bounds].size.height)]; | |
4059 | [navbar_ setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth]; | |
4060 | [[self view] addSubview:navbar_]; | |
4061 | [navbar_ release]; | |
4062 | } return self; | |
4063 | } | |
4064 | ||
f6e13561 | 4065 | @end |
dd5f8161 GP |
4066 | /* }}} */ |
4067 | ||
80319240 | 4068 | /* Cydia Browser Controller {{{ */ |
40bfa2e6 | 4069 | @interface CYBrowserController : BrowserController { |
43f3d7f6 JF |
4070 | CydiaObject *cydia_; |
4071 | } | |
a9a0661e | 4072 | |
43f3d7f6 JF |
4073 | @end |
4074 | ||
b5e7eebb | 4075 | @implementation CYBrowserController |
43f3d7f6 JF |
4076 | |
4077 | - (void) dealloc { | |
4078 | [cydia_ release]; | |
4079 | [super dealloc]; | |
4080 | } | |
4081 | ||
01d93940 JF |
4082 | - (void) setHeaders:(NSDictionary *)headers forHost:(NSString *)host { |
4083 | } | |
4084 | ||
2634b249 JF |
4085 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4086 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
01d93940 JF |
4087 | |
4088 | WebDataSource *source([frame dataSource]); | |
4089 | NSURLResponse *response([source response]); | |
4090 | NSURL *url([response URL]); | |
4091 | NSString *scheme([url scheme]); | |
4092 | ||
4093 | NSHTTPURLResponse *http; | |
4094 | if (scheme != nil && ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])) | |
4095 | http = (NSHTTPURLResponse *) response; | |
4096 | else | |
4097 | http = nil; | |
4098 | ||
4099 | NSDictionary *headers([http allHeaderFields]); | |
4100 | NSString *host([url host]); | |
4101 | [self setHeaders:headers forHost:host]; | |
4102 | ||
77801ff1 JF |
4103 | if ( |
4104 | [host isEqualToString:@"cydia.saurik.com"] || | |
4105 | [host hasSuffix:@".cydia.saurik.com"] || | |
4106 | [scheme isEqualToString:@"file"] | |
4107 | ) | |
ef055c6c | 4108 | [window setValue:cydia_ forKey:@"cydia"]; |
43f3d7f6 JF |
4109 | } |
4110 | ||
bfc87a4d JF |
4111 | - (void) _setMoreHeaders:(NSMutableURLRequest *)request { |
4112 | if (System_ != NULL) | |
4113 | [request setValue:System_ forHTTPHeaderField:@"X-System"]; | |
43f3d7f6 | 4114 | if (Machine_ != NULL) |
bfc87a4d | 4115 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; |
01d93940 JF |
4116 | if (Token_ != nil) |
4117 | [request setValue:Token_ forHTTPHeaderField:@"X-Cydia-Token"]; | |
43f3d7f6 | 4118 | if (Role_ != nil) |
bfc87a4d JF |
4119 | [request setValue:Role_ forHTTPHeaderField:@"X-Role"]; |
4120 | } | |
43f3d7f6 | 4121 | |
2634b249 JF |
4122 | - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
4123 | NSMutableURLRequest *copy([[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source] mutableCopy]); | |
bfc87a4d | 4124 | [self _setMoreHeaders:copy]; |
43f3d7f6 | 4125 | return copy; |
a9a0661e JF |
4126 | } |
4127 | ||
77801ff1 JF |
4128 | - (void) setDelegate:(id)delegate { |
4129 | [super setDelegate:delegate]; | |
4130 | [cydia_ setDelegate:delegate]; | |
4131 | } | |
4132 | ||
b5e7eebb | 4133 | - (id) init { |
6d166849 | 4134 | if ((self = [super initWithWidth:0 ofClass:[CYBrowserController class]]) != nil) { |
43f3d7f6 JF |
4135 | cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_]; |
4136 | ||
2634b249 | 4137 | WebView *webview([[webview_ _documentView] webView]); |
43f3d7f6 JF |
4138 | |
4139 | Package *package([[Database sharedInstance] packageWithName:@"cydia"]); | |
35bf217f | 4140 | |
43f3d7f6 JF |
4141 | NSString *application = package == nil ? @"Cydia" : [NSString |
4142 | stringWithFormat:@"Cydia/%@", | |
4143 | [package installed] | |
4144 | ]; | |
4145 | ||
43f3d7f6 | 4146 | if (Safari_ != nil) |
9ef18597 | 4147 | application = [NSString stringWithFormat:@"Safari/%@ %@", Safari_, application]; |
35bf217f | 4148 | if (Build_ != nil) |
9ef18597 | 4149 | application = [NSString stringWithFormat:@"Mobile/%@ %@", Build_, application]; |
35bf217f | 4150 | if (Product_ != nil) |
9ef18597 | 4151 | application = [NSString stringWithFormat:@"Version/%@ %@", Product_, application]; |
43f3d7f6 JF |
4152 | |
4153 | [webview setApplicationNameForUserAgent:application]; | |
4154 | } return self; | |
4155 | } | |
4156 | ||
4157 | @end | |
80319240 | 4158 | /* }}} */ |
43f3d7f6 | 4159 | |
5829aea2 GP |
4160 | /* Confirmation Controller {{{ */ |
4161 | bool DepSubstrate(const pkgCache::VerIterator &iterator) { | |
4162 | if (!iterator.end()) | |
4163 | for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) { | |
4164 | if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends) | |
4165 | continue; | |
4166 | pkgCache::PkgIterator package(dep.TargetPkg()); | |
4167 | if (package.end()) | |
4168 | continue; | |
4169 | if (strcmp(package.Name(), "mobilesubstrate") == 0) | |
4170 | return true; | |
4171 | } | |
4172 | ||
4173 | return false; | |
4174 | } | |
4175 | ||
adb61bda | 4176 | @protocol ConfirmationControllerDelegate |
674dce72 | 4177 | - (void) cancelAndClear:(bool)clear; |
b5e7eebb | 4178 | - (void) confirmWithNavigationController:(UINavigationController *)navigation; |
dc63e78f | 4179 | - (void) queue; |
36bb2ca2 | 4180 | @end |
2367a917 | 4181 | |
adb61bda | 4182 | @interface ConfirmationController : CYBrowserController { |
770f2a8e | 4183 | _transient Database *database_; |
3272e699 | 4184 | UIAlertView *essential_; |
affeffc7 JF |
4185 | NSArray *changes_; |
4186 | NSArray *issues_; | |
4187 | NSArray *sizes_; | |
a9a0661e | 4188 | BOOL substrate_; |
36bb2ca2 | 4189 | } |
dc5812ec | 4190 | |
b5e7eebb | 4191 | - (id) initWithDatabase:(Database *)database; |
b4d89997 | 4192 | |
dc5812ec JF |
4193 | @end |
4194 | ||
adb61bda | 4195 | @implementation ConfirmationController |
dc5812ec | 4196 | |
2367a917 | 4197 | - (void) dealloc { |
affeffc7 JF |
4198 | [changes_ release]; |
4199 | if (issues_ != nil) | |
4200 | [issues_ release]; | |
4201 | [sizes_ release]; | |
36bb2ca2 JF |
4202 | if (essential_ != nil) |
4203 | [essential_ release]; | |
2367a917 JF |
4204 | [super dealloc]; |
4205 | } | |
4206 | ||
b5e7eebb | 4207 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
3272e699 | 4208 | NSString *context([alert context]); |
9bedffaa | 4209 | |
1cedb821 | 4210 | if ([context isEqualToString:@"remove"]) { |
b5e7eebb GP |
4211 | if (button == [alert cancelButtonIndex]) { |
4212 | [self dismissModalViewControllerAnimated:YES]; | |
3272e699 | 4213 | } else if (button == [alert firstOtherButtonIndex]) { |
b5e7eebb GP |
4214 | if (substrate_) |
4215 | Finish_ = 2; | |
4216 | [delegate_ confirmWithNavigationController:[self navigationController]]; | |
9bedffaa | 4217 | } |
9bedffaa | 4218 | |
3272e699 | 4219 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 4220 | } else if ([context isEqualToString:@"unable"]) { |
b5e7eebb | 4221 | [self dismissModalViewControllerAnimated:YES]; |
3272e699 GP |
4222 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
4223 | } else { | |
b5e7eebb GP |
4224 | [super alertView:alert clickedButtonAtIndex:button]; |
4225 | } | |
36bb2ca2 JF |
4226 | } |
4227 | ||
46c46f4f | 4228 | - (void) _doContinue { |
21ea11a4 GP |
4229 | [self dismissModalViewControllerAnimated:YES]; |
4230 | [delegate_ cancelAndClear:NO]; | |
46c46f4f | 4231 | } |
bc11cf5b | 4232 | |
46c46f4f JF |
4233 | - (id) invokeDefaultMethodWithArguments:(NSArray *)args { |
4234 | [self performSelectorOnMainThread:@selector(_doContinue) withObject:nil waitUntilDone:NO]; | |
21ea11a4 GP |
4235 | return nil; |
4236 | } | |
4237 | ||
2634b249 JF |
4238 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4239 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
affeffc7 JF |
4240 | [window setValue:changes_ forKey:@"changes"]; |
4241 | [window setValue:issues_ forKey:@"issues"]; | |
4242 | [window setValue:sizes_ forKey:@"sizes"]; | |
21ea11a4 | 4243 | [window setValue:self forKey:@"queue"]; |
36bb2ca2 JF |
4244 | } |
4245 | ||
b5e7eebb GP |
4246 | - (id) initWithDatabase:(Database *)database { |
4247 | if ((self = [super init]) != nil) { | |
770f2a8e JF |
4248 | database_ = database; |
4249 | ||
b5e7eebb GP |
4250 | [[self navigationItem] setTitle:UCLocalize("CONFIRM")]; |
4251 | ||
36bb2ca2 JF |
4252 | NSMutableArray *installing = [NSMutableArray arrayWithCapacity:16]; |
4253 | NSMutableArray *reinstalling = [NSMutableArray arrayWithCapacity:16]; | |
4254 | NSMutableArray *upgrading = [NSMutableArray arrayWithCapacity:16]; | |
4255 | NSMutableArray *downgrading = [NSMutableArray arrayWithCapacity:16]; | |
4256 | NSMutableArray *removing = [NSMutableArray arrayWithCapacity:16]; | |
dc5812ec | 4257 | |
36bb2ca2 | 4258 | bool remove(false); |
dc5812ec | 4259 | |
a9a0661e JF |
4260 | pkgDepCache::Policy *policy([database_ policy]); |
4261 | ||
36bb2ca2 | 4262 | pkgCacheFile &cache([database_ cache]); |
2388b078 | 4263 | NSArray *packages = [database_ packages]; |
c4dcf2c2 | 4264 | for (Package *package in packages) { |
2388b078 | 4265 | pkgCache::PkgIterator iterator = [package iterator]; |
36bb2ca2 | 4266 | pkgDepCache::StateCache &state(cache[iterator]); |
dc5812ec | 4267 | |
2388b078 JF |
4268 | NSString *name([package name]); |
4269 | ||
36bb2ca2 JF |
4270 | if (state.NewInstall()) |
4271 | [installing addObject:name]; | |
4272 | else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall) | |
4273 | [reinstalling addObject:name]; | |
4274 | else if (state.Upgrade()) | |
4275 | [upgrading addObject:name]; | |
4276 | else if (state.Downgrade()) | |
4277 | [downgrading addObject:name]; | |
4278 | else if (state.Delete()) { | |
2388b078 | 4279 | if ([package essential]) |
36bb2ca2 JF |
4280 | remove = true; |
4281 | [removing addObject:name]; | |
a9a0661e JF |
4282 | } else continue; |
4283 | ||
4284 | substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator)); | |
4285 | substrate_ |= DepSubstrate(iterator.CurrentVer()); | |
36bb2ca2 | 4286 | } |
ec97ef06 | 4287 | |
36bb2ca2 JF |
4288 | if (!remove) |
4289 | essential_ = nil; | |
d791dce4 | 4290 | else if (Advanced_) { |
43f3d7f6 | 4291 | NSString *parenthetical(UCLocalize("PARENTHETICAL")); |
f79a4512 | 4292 | |
3272e699 | 4293 | essential_ = [[UIAlertView alloc] |
43f3d7f6 | 4294 | initWithTitle:UCLocalize("REMOVING_ESSENTIALS") |
b5e7eebb GP |
4295 | message:UCLocalize("REMOVING_ESSENTIALS_EX") |
4296 | delegate:self | |
4297 | cancelButtonTitle:[NSString stringWithFormat:parenthetical, UCLocalize("CANCEL_OPERATION"), UCLocalize("SAFE")] | |
4298 | otherButtonTitles:[NSString stringWithFormat:parenthetical, UCLocalize("FORCE_REMOVAL"), UCLocalize("UNSAFE")], nil | |
4299 | ]; | |
04fe1349 | 4300 | |
3272e699 | 4301 | [essential_ setContext:@"remove"]; |
9bedffaa | 4302 | } else { |
3272e699 | 4303 | essential_ = [[UIAlertView alloc] |
43f3d7f6 | 4304 | initWithTitle:UCLocalize("UNABLE_TO_COMPLY") |
b5e7eebb GP |
4305 | message:UCLocalize("UNABLE_TO_COMPLY_EX") |
4306 | delegate:self | |
4307 | cancelButtonTitle:UCLocalize("OKAY") | |
4308 | otherButtonTitles:nil | |
36bb2ca2 | 4309 | ]; |
ec97ef06 | 4310 | |
b5e7eebb | 4311 | [essential_ setContext:@"unable"]; |
36bb2ca2 | 4312 | } |
ec97ef06 | 4313 | |
affeffc7 JF |
4314 | changes_ = [[NSArray alloc] initWithObjects: |
4315 | installing, | |
4316 | reinstalling, | |
4317 | upgrading, | |
4318 | downgrading, | |
4319 | removing, | |
4320 | nil]; | |
dc5812ec | 4321 | |
affeffc7 JF |
4322 | issues_ = [database_ issues]; |
4323 | if (issues_ != nil) | |
4324 | issues_ = [issues_ retain]; | |
dc5812ec | 4325 | |
affeffc7 JF |
4326 | sizes_ = [[NSArray alloc] initWithObjects: |
4327 | SizeString([database_ fetcher].FetchNeeded()), | |
4328 | SizeString([database_ fetcher].PartialPresent()), | |
affeffc7 | 4329 | nil]; |
dc5812ec | 4330 | |
affeffc7 | 4331 | [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"confirm" ofType:@"html"]]]; |
04fe1349 | 4332 | |
dd9de556 | 4333 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] |
21c6da4b GP |
4334 | initWithTitle:UCLocalize("CANCEL") |
4335 | // OLD: [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("CANCEL"), UCLocalize("QUEUE")] | |
b5e7eebb GP |
4336 | style:UIBarButtonItemStylePlain |
4337 | target:self | |
4338 | action:@selector(cancelButtonClicked) | |
dd9de556 | 4339 | ] autorelease]]; |
36bb2ca2 | 4340 | } return self; |
b4d89997 | 4341 | } |
8da60fb7 | 4342 | |
40bfa2e6 | 4343 | - (void) applyRightButton { |
b5e7eebb | 4344 | #if !AlwaysReload && !IgnoreInstall |
dd9de556 JF |
4345 | if (issues_ == nil && ![self isLoading]) |
4346 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
4347 | initWithTitle:UCLocalize("CONFIRM") | |
4348 | style:UIBarButtonItemStylePlain | |
4349 | target:self | |
4350 | action:@selector(confirmButtonClicked) | |
4351 | ] autorelease]]; | |
4352 | else | |
4353 | [super applyRightButton]; | |
40bfa2e6 | 4354 | #else |
bc11cf5b | 4355 | [[self navigationItem] setRightBarButtonItem:nil]; |
b5e7eebb | 4356 | #endif |
affeffc7 JF |
4357 | } |
4358 | ||
b5e7eebb | 4359 | - (void) cancelButtonClicked { |
21ea11a4 GP |
4360 | [self dismissModalViewControllerAnimated:YES]; |
4361 | [delegate_ cancelAndClear:YES]; | |
affeffc7 JF |
4362 | } |
4363 | ||
9487f027 | 4364 | #if !AlwaysReload |
b5e7eebb | 4365 | - (void) confirmButtonClicked { |
b0ad8dee | 4366 | #if IgnoreInstall |
b5e7eebb | 4367 | return; |
b0ad8dee | 4368 | #endif |
affeffc7 | 4369 | if (essential_ != nil) |
b5e7eebb | 4370 | [essential_ show]; |
affeffc7 JF |
4371 | else { |
4372 | if (substrate_) | |
4373 | Finish_ = 2; | |
b5e7eebb | 4374 | [delegate_ confirmWithNavigationController:[self navigationController]]; |
affeffc7 JF |
4375 | } |
4376 | } | |
9487f027 | 4377 | #endif |
affeffc7 | 4378 | |
36bb2ca2 JF |
4379 | @end |
4380 | /* }}} */ | |
8da60fb7 | 4381 | |
36bb2ca2 JF |
4382 | /* Progress Data {{{ */ |
4383 | @interface ProgressData : NSObject { | |
4384 | SEL selector_; | |
3931b718 JF |
4385 | // XXX: should these really both be _transient? |
4386 | _transient id target_; | |
4387 | _transient id object_; | |
dc5812ec JF |
4388 | } |
4389 | ||
36bb2ca2 | 4390 | - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object; |
b4d89997 | 4391 | |
36bb2ca2 JF |
4392 | - (SEL) selector; |
4393 | - (id) target; | |
4394 | - (id) object; | |
4395 | @end | |
b4d89997 | 4396 | |
36bb2ca2 | 4397 | @implementation ProgressData |
b4d89997 | 4398 | |
36bb2ca2 JF |
4399 | - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object { |
4400 | if ((self = [super init]) != nil) { | |
4401 | selector_ = selector; | |
4402 | target_ = target; | |
4403 | object_ = object; | |
4404 | } return self; | |
4405 | } | |
b4d89997 | 4406 | |
36bb2ca2 JF |
4407 | - (SEL) selector { |
4408 | return selector_; | |
4409 | } | |
b4d89997 | 4410 | |
36bb2ca2 JF |
4411 | - (id) target { |
4412 | return target_; | |
8da60fb7 JF |
4413 | } |
4414 | ||
36bb2ca2 JF |
4415 | - (id) object { |
4416 | return object_; | |
dc5812ec JF |
4417 | } |
4418 | ||
2367a917 JF |
4419 | @end |
4420 | /* }}} */ | |
adb61bda GP |
4421 | /* Progress Controller {{{ */ |
4422 | @interface ProgressController : CYViewController < | |
8b29f8e6 | 4423 | ConfigurationDelegate, |
36bb2ca2 JF |
4424 | ProgressDelegate |
4425 | > { | |
8b29f8e6 | 4426 | _transient Database *database_; |
36bb2ca2 JF |
4427 | UIProgressBar *progress_; |
4428 | UITextView *output_; | |
4429 | UITextLabel *status_; | |
31f3cfff | 4430 | UIPushButton *close_; |
bd150f54 | 4431 | BOOL running_; |
dddbc481 | 4432 | SHA1SumValue springlist_; |
22f8bed9 | 4433 | SHA1SumValue notifyconf_; |
670a0494 | 4434 | NSString *title_; |
2367a917 JF |
4435 | } |
4436 | ||
b5e7eebb | 4437 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
2367a917 | 4438 | |
36bb2ca2 JF |
4439 | - (void) _retachThread; |
4440 | - (void) _detachNewThreadData:(ProgressData *)data; | |
4441 | - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title; | |
2367a917 | 4442 | |
bd150f54 JF |
4443 | - (BOOL) isRunning; |
4444 | ||
2367a917 JF |
4445 | @end |
4446 | ||
adb61bda GP |
4447 | @protocol ProgressControllerDelegate |
4448 | - (void) progressControllerIsComplete:(ProgressController *)sender; | |
36bb2ca2 JF |
4449 | @end |
4450 | ||
adb61bda | 4451 | @implementation ProgressController |
2367a917 JF |
4452 | |
4453 | - (void) dealloc { | |
b5e7eebb | 4454 | [database_ setDelegate:nil]; |
36bb2ca2 JF |
4455 | [progress_ release]; |
4456 | [output_ release]; | |
4457 | [status_ release]; | |
31f3cfff | 4458 | [close_ release]; |
670a0494 JF |
4459 | if (title_ != nil) |
4460 | [title_ release]; | |
2367a917 JF |
4461 | [super dealloc]; |
4462 | } | |
4463 | ||
b5e7eebb GP |
4464 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { |
4465 | if ((self = [super init]) != nil) { | |
8b29f8e6 | 4466 | database_ = database; |
b5e7eebb | 4467 | [database_ setDelegate:self]; |
36bb2ca2 | 4468 | delegate_ = delegate; |
ec97ef06 | 4469 | |
c21004b9 | 4470 | [[self view] setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f]]; |
2367a917 | 4471 | |
b5e7eebb | 4472 | progress_ = [[UIProgressBar alloc] init]; |
04fe1349 | 4473 | [progress_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)]; |
31f3cfff | 4474 | [progress_ setStyle:0]; |
ec97ef06 | 4475 | |
b5e7eebb | 4476 | status_ = [[UITextLabel alloc] init]; |
04fe1349 | 4477 | [status_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)]; |
baf80942 JF |
4478 | [status_ setColor:[UIColor whiteColor]]; |
4479 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
36bb2ca2 JF |
4480 | [status_ setCentersHorizontally:YES]; |
4481 | //[status_ setFont:font]; | |
ec97ef06 | 4482 | |
b5e7eebb | 4483 | output_ = [[UITextView alloc] init]; |
1d80f6b9 | 4484 | |
04fe1349 | 4485 | [output_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
36bb2ca2 | 4486 | //[output_ setTextFont:@"Courier New"]; |
d791dce4 | 4487 | [output_ setFont:[[output_ font] fontWithSize:12]]; |
baf80942 JF |
4488 | [output_ setTextColor:[UIColor whiteColor]]; |
4489 | [output_ setBackgroundColor:[UIColor clearColor]]; | |
36bb2ca2 JF |
4490 | [output_ setMarginTop:0]; |
4491 | [output_ setAllowsRubberBanding:YES]; | |
795d26fc | 4492 | [output_ setEditable:NO]; |
b5e7eebb | 4493 | [[self view] addSubview:output_]; |
b4d89997 | 4494 | |
b5e7eebb | 4495 | close_ = [[UIPushButton alloc] init]; |
04fe1349 | 4496 | [close_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)]; |
31f3cfff JF |
4497 | [close_ setAutosizesToFit:NO]; |
4498 | [close_ setDrawsShadow:YES]; | |
4499 | [close_ setStretchBackground:YES]; | |
31f3cfff | 4500 | [close_ setEnabled:YES]; |
b5e7eebb | 4501 | [close_ setTitleFont:[UIFont boldSystemFontOfSize:22]]; |
bfc87a4d | 4502 | [close_ addTarget:self action:@selector(closeButtonPushed) forEvents:UIControlEventTouchUpInside]; |
31f3cfff JF |
4503 | [close_ setBackground:[UIImage applicationImageNamed:@"green-up.png"] forState:0]; |
4504 | [close_ setBackground:[UIImage applicationImageNamed:@"green-dn.png"] forState:1]; | |
36bb2ca2 | 4505 | } return self; |
2367a917 JF |
4506 | } |
4507 | ||
b5e7eebb GP |
4508 | - (void) positionViews { |
4509 | CGRect bounds = [[self view] bounds]; | |
4510 | CGSize prgsize = [UIProgressBar defaultSize]; | |
4511 | ||
4512 | CGRect prgrect = {{ | |
4513 | (bounds.size.width - prgsize.width) / 2, | |
534e31fc | 4514 | bounds.size.height - prgsize.height - 20 |
b5e7eebb | 4515 | }, prgsize}; |
bc11cf5b | 4516 | |
36fbb2aa | 4517 | float closewidth = std::min(bounds.size.width - 20, 300.0f); |
b5e7eebb GP |
4518 | |
4519 | [progress_ setFrame:prgrect]; | |
4520 | [status_ setFrame:CGRectMake( | |
4521 | 10, | |
534e31fc | 4522 | bounds.size.height - prgsize.height - 50, |
b5e7eebb GP |
4523 | bounds.size.width - 20, |
4524 | 24 | |
4525 | )]; | |
4526 | [output_ setFrame:CGRectMake( | |
4527 | 10, | |
4528 | 20, | |
4529 | bounds.size.width - 20, | |
534e31fc | 4530 | bounds.size.height - 62 |
b5e7eebb GP |
4531 | )]; |
4532 | [close_ setFrame:CGRectMake( | |
4533 | (bounds.size.width - closewidth) / 2, | |
534e31fc | 4534 | bounds.size.height - prgsize.height - 50, |
b5e7eebb GP |
4535 | closewidth, |
4536 | 32 + prgsize.height | |
4537 | )]; | |
4538 | } | |
4539 | ||
4540 | - (void) viewWillAppear:(BOOL)animated { | |
4541 | [super viewDidAppear:animated]; | |
4542 | [[self navigationItem] setHidesBackButton:YES]; | |
c21004b9 | 4543 | [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlack]; |
bc11cf5b | 4544 | |
b5e7eebb | 4545 | [self positionViews]; |
2367a917 JF |
4546 | } |
4547 | ||
b5e7eebb GP |
4548 | - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { |
4549 | [self positionViews]; | |
2367a917 JF |
4550 | } |
4551 | ||
b5e7eebb | 4552 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
969eaf2f | 4553 | NSString *context([alert context]); |
bc8cd583 | 4554 | |
670a0494 | 4555 | if ([context isEqualToString:@"conffile"]) { |
b5e7eebb | 4556 | FILE *input = [database_ input]; |
36fbb2aa JF |
4557 | if (button == [alert cancelButtonIndex]) |
4558 | fprintf(input, "N\n"); | |
4559 | else if (button == [alert firstOtherButtonIndex]) | |
4560 | fprintf(input, "Y\n"); | |
b5e7eebb | 4561 | fflush(input); |
670a0494 JF |
4562 | } |
4563 | } | |
4564 | ||
4565 | - (void) closeButtonPushed { | |
4566 | running_ = NO; | |
4567 | ||
ef494bd8 RP |
4568 | UpdateExternalStatus(0); |
4569 | ||
670a0494 JF |
4570 | switch (Finish_) { |
4571 | case 0: | |
b5e7eebb | 4572 | [self dismissModalViewControllerAnimated:YES]; |
670a0494 JF |
4573 | break; |
4574 | ||
4575 | case 1: | |
c4899376 JF |
4576 | [delegate_ terminateWithSuccess]; |
4577 | /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) | |
4578 | [delegate_ suspendWithAnimation:YES]; | |
4579 | else | |
4580 | [delegate_ suspend];*/ | |
670a0494 JF |
4581 | break; |
4582 | ||
4583 | case 2: | |
985d2dff | 4584 | _trace(); |
ef812071 | 4585 | goto reload; |
670a0494 JF |
4586 | |
4587 | case 3: | |
985d2dff | 4588 | _trace(); |
ef812071 JF |
4589 | goto reload; |
4590 | ||
4591 | reload: | |
4592 | system("/usr/bin/sbreload"); | |
985d2dff | 4593 | _trace(); |
670a0494 JF |
4594 | break; |
4595 | ||
4596 | case 4: | |
985d2dff | 4597 | _trace(); |
0e371502 JF |
4598 | if (void (*SBReboot)(mach_port_t) = reinterpret_cast<void (*)(mach_port_t)>(dlsym(RTLD_DEFAULT, "SBReboot"))) |
4599 | SBReboot(SBSSpringBoardServerPort()); | |
4600 | else | |
bb0fe3c9 | 4601 | reboot2(RB_AUTOBOOT); |
670a0494 | 4602 | break; |
bc8cd583 | 4603 | } |
670a0494 | 4604 | } |
bd150f54 | 4605 | |
670a0494 | 4606 | - (void) _retachThread { |
b5e7eebb | 4607 | [[self navigationItem] setTitle:UCLocalize("COMPLETE")]; |
670a0494 | 4608 | |
b5e7eebb | 4609 | [[self view] addSubview:close_]; |
670a0494 JF |
4610 | [progress_ removeFromSuperview]; |
4611 | [status_ removeFromSuperview]; | |
4612 | ||
4613 | [database_ popErrorWithTitle:title_]; | |
adb61bda | 4614 | [delegate_ progressControllerIsComplete:self]; |
c390d3ab | 4615 | |
22f8bed9 | 4616 | if (Finish_ < 4) { |
70d45c1e JF |
4617 | FileFd file; |
4618 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
4619 | _error->Discard(); | |
4620 | else { | |
4621 | MMap mmap(file, MMap::ReadOnly); | |
4622 | SHA1Summation sha1; | |
4623 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
4624 | if (!(notifyconf_ == sha1.Result())) | |
4625 | Finish_ = 4; | |
4626 | } | |
22f8bed9 JF |
4627 | } |
4628 | ||
affeffc7 | 4629 | if (Finish_ < 3) { |
70d45c1e JF |
4630 | FileFd file; |
4631 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
4632 | _error->Discard(); | |
4633 | else { | |
4634 | MMap mmap(file, MMap::ReadOnly); | |
4635 | SHA1Summation sha1; | |
4636 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
4637 | if (!(springlist_ == sha1.Result())) | |
4638 | Finish_ = 3; | |
4639 | } | |
dddbc481 JF |
4640 | } |
4641 | ||
bc8cd583 | 4642 | switch (Finish_) { |
b5e7eebb | 4643 | case 0: [close_ setTitle:UCLocalize("RETURN_TO_CYDIA")]; break; /* XXX: Maybe UCLocalize("DONE")? */ |
43f3d7f6 JF |
4644 | case 1: [close_ setTitle:UCLocalize("CLOSE_CYDIA")]; break; |
4645 | case 2: [close_ setTitle:UCLocalize("RESTART_SPRINGBOARD")]; break; | |
4646 | case 3: [close_ setTitle:UCLocalize("RELOAD_SPRINGBOARD")]; break; | |
4647 | case 4: [close_ setTitle:UCLocalize("REBOOT_DEVICE")]; break; | |
bc8cd583 JF |
4648 | } |
4649 | ||
985d2dff | 4650 | _trace(); |
58241d4c | 4651 | system("su -c /usr/bin/uicache mobile"); |
985d2dff | 4652 | _trace(); |
c4ce98df | 4653 | |
ef494bd8 RP |
4654 | UpdateExternalStatus(Finish_ == 0 ? 2 : 0); |
4655 | ||
bd150f54 | 4656 | [delegate_ setStatusBarShowsProgress:NO]; |
31f3cfff JF |
4657 | } |
4658 | ||
affeffc7 | 4659 | - (void) _detachNewThreadData:(ProgressData *)data { _pooled |
36bb2ca2 | 4660 | [[data target] performSelector:[data selector] withObject:[data object]]; |
36bb2ca2 | 4661 | [self performSelectorOnMainThread:@selector(_retachThread) withObject:nil waitUntilDone:YES]; |
b19871dd JF |
4662 | } |
4663 | ||
36bb2ca2 | 4664 | - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title { |
ef494bd8 RP |
4665 | UpdateExternalStatus(1); |
4666 | ||
670a0494 JF |
4667 | if (title_ != nil) |
4668 | [title_ release]; | |
4669 | if (title == nil) | |
4670 | title_ = nil; | |
4671 | else | |
4672 | title_ = [title retain]; | |
4673 | ||
b5e7eebb | 4674 | [[self navigationItem] setTitle:title_]; |
b4d89997 | 4675 | |
36bb2ca2 JF |
4676 | [status_ setText:nil]; |
4677 | [output_ setText:@""]; | |
4678 | [progress_ setProgress:0]; | |
b19871dd | 4679 | |
31f3cfff | 4680 | [close_ removeFromSuperview]; |
b5e7eebb GP |
4681 | [[self view] addSubview:progress_]; |
4682 | [[self view] addSubview:status_]; | |
31f3cfff | 4683 | |
bd150f54 JF |
4684 | [delegate_ setStatusBarShowsProgress:YES]; |
4685 | running_ = YES; | |
4686 | ||
22f8bed9 | 4687 | { |
70d45c1e JF |
4688 | FileFd file; |
4689 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
4690 | _error->Discard(); | |
4691 | else { | |
4692 | MMap mmap(file, MMap::ReadOnly); | |
4693 | SHA1Summation sha1; | |
4694 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
4695 | notifyconf_ = sha1.Result(); | |
4696 | } | |
22f8bed9 JF |
4697 | } |
4698 | ||
affeffc7 | 4699 | { |
70d45c1e JF |
4700 | FileFd file; |
4701 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
4702 | _error->Discard(); | |
4703 | else { | |
4704 | MMap mmap(file, MMap::ReadOnly); | |
4705 | SHA1Summation sha1; | |
4706 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
4707 | springlist_ = sha1.Result(); | |
4708 | } | |
dddbc481 JF |
4709 | } |
4710 | ||
36bb2ca2 JF |
4711 | [NSThread |
4712 | detachNewThreadSelector:@selector(_detachNewThreadData:) | |
4713 | toTarget:self | |
3931b718 | 4714 | withObject:[[[ProgressData alloc] |
36bb2ca2 JF |
4715 | initWithSelector:selector |
4716 | target:target | |
4717 | object:object | |
3931b718 | 4718 | ] autorelease] |
36bb2ca2 | 4719 | ]; |
b19871dd JF |
4720 | } |
4721 | ||
5a09ae08 JF |
4722 | - (void) repairWithSelector:(SEL)selector { |
4723 | [self | |
4724 | detachNewThreadSelector:selector | |
4725 | toTarget:database_ | |
4726 | withObject:nil | |
43f3d7f6 | 4727 | title:UCLocalize("REPAIRING") |
5a09ae08 JF |
4728 | ]; |
4729 | } | |
4730 | ||
8b29f8e6 JF |
4731 | - (void) setConfigurationData:(NSString *)data { |
4732 | [self | |
4733 | performSelectorOnMainThread:@selector(_setConfigurationData:) | |
4734 | withObject:data | |
4735 | waitUntilDone:YES | |
4736 | ]; | |
4737 | } | |
4738 | ||
670a0494 JF |
4739 | - (void) setProgressError:(NSString *)error withTitle:(NSString *)title { |
4740 | CYActionSheet *sheet([[[CYActionSheet alloc] | |
4741 | initWithTitle:title | |
43f3d7f6 | 4742 | buttons:[NSArray arrayWithObjects:UCLocalize("OKAY"), nil] |
31f3cfff | 4743 | defaultButtonIndex:0 |
670a0494 | 4744 | ] autorelease]); |
31f3cfff | 4745 | |
be2b3d7b | 4746 | [sheet setMessage:error]; |
670a0494 JF |
4747 | [sheet yieldToPopupAlertAnimated:YES]; |
4748 | [sheet dismiss]; | |
b19871dd JF |
4749 | } |
4750 | ||
36bb2ca2 JF |
4751 | - (void) setProgressTitle:(NSString *)title { |
4752 | [self | |
4753 | performSelectorOnMainThread:@selector(_setProgressTitle:) | |
4754 | withObject:title | |
4755 | waitUntilDone:YES | |
4756 | ]; | |
b19871dd JF |
4757 | } |
4758 | ||
36bb2ca2 JF |
4759 | - (void) setProgressPercent:(float)percent { |
4760 | [self | |
4761 | performSelectorOnMainThread:@selector(_setProgressPercent:) | |
4762 | withObject:[NSNumber numberWithFloat:percent] | |
4763 | waitUntilDone:YES | |
4764 | ]; | |
b19871dd JF |
4765 | } |
4766 | ||
baf80942 | 4767 | - (void) startProgress { |
baf80942 JF |
4768 | } |
4769 | ||
36bb2ca2 JF |
4770 | - (void) addProgressOutput:(NSString *)output { |
4771 | [self | |
4772 | performSelectorOnMainThread:@selector(_addProgressOutput:) | |
4773 | withObject:output | |
4774 | waitUntilDone:YES | |
4775 | ]; | |
b19871dd JF |
4776 | } |
4777 | ||
baf80942 | 4778 | - (bool) isCancelling:(size_t)received { |
baf80942 JF |
4779 | return false; |
4780 | } | |
4781 | ||
8b29f8e6 | 4782 | - (void) _setConfigurationData:(NSString *)data { |
7b0ce2da JF |
4783 | static Pcre conffile_r("^'(.*)' '(.*)' ([01]) ([01])$"); |
4784 | ||
670a0494 JF |
4785 | if (!conffile_r(data)) { |
4786 | lprintf("E:invalid conffile\n"); | |
4787 | return; | |
4788 | } | |
8b29f8e6 JF |
4789 | |
4790 | NSString *ofile = conffile_r[1]; | |
4791 | //NSString *nfile = conffile_r[2]; | |
4792 | ||
969eaf2f | 4793 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 4794 | initWithTitle:UCLocalize("CONFIGURATION_UPGRADE") |
b5e7eebb GP |
4795 | message:[NSString stringWithFormat:@"%@\n\n%@", UCLocalize("CONFIGURATION_UPGRADE_EX"), ofile] |
4796 | delegate:self | |
4797 | cancelButtonTitle:UCLocalize("KEEP_OLD_COPY") | |
4798 | otherButtonTitles:UCLocalize("ACCEPT_NEW_COPY"), | |
969eaf2f | 4799 | // XXX: UCLocalize("SEE_WHAT_CHANGED"), |
b5e7eebb GP |
4800 | nil |
4801 | ] autorelease]; | |
969eaf2f | 4802 | |
b5e7eebb | 4803 | [alert setContext:@"conffile"]; |
969eaf2f | 4804 | [alert show]; |
8b29f8e6 JF |
4805 | } |
4806 | ||
36bb2ca2 | 4807 | - (void) _setProgressTitle:(NSString *)title { |
9487f027 JF |
4808 | NSMutableArray *words([[title componentsSeparatedByString:@" "] mutableCopy]); |
4809 | for (size_t i(0), e([words count]); i != e; ++i) { | |
4810 | NSString *word([words objectAtIndex:i]); | |
4811 | if (Package *package = [database_ packageWithName:word]) | |
4812 | [words replaceObjectAtIndex:i withObject:[package name]]; | |
4813 | } | |
4814 | ||
4815 | [status_ setText:[words componentsJoinedByString:@" "]]; | |
b4d89997 | 4816 | } |
b19871dd | 4817 | |
36bb2ca2 JF |
4818 | - (void) _setProgressPercent:(NSNumber *)percent { |
4819 | [progress_ setProgress:[percent floatValue]]; | |
49048579 JF |
4820 | } |
4821 | ||
36bb2ca2 JF |
4822 | - (void) _addProgressOutput:(NSString *)output { |
4823 | [output_ setText:[NSString stringWithFormat:@"%@\n%@", [output_ text], output]]; | |
4824 | CGSize size = [output_ contentSize]; | |
773cfc9f DH |
4825 | CGPoint offset = [output_ contentOffset]; |
4826 | if (size.height - offset.y < [output_ frame].size.height + 20.f) { | |
4827 | CGRect rect = {{0, size.height-1}, {size.width, 1}}; | |
4828 | [output_ scrollRectToVisible:rect animated:YES]; | |
4829 | } | |
36bb2ca2 | 4830 | } |
dc088e63 | 4831 | |
bd150f54 | 4832 | - (BOOL) isRunning { |
c4ce98df | 4833 | return running_; |
bd150f54 JF |
4834 | } |
4835 | ||
36bb2ca2 JF |
4836 | @end |
4837 | /* }}} */ | |
dc088e63 | 4838 | |
46aa9775 | 4839 | /* Cell Content View {{{ */ |
c21004b9 JF |
4840 | @protocol ContentDelegate |
4841 | - (void) drawContentRect:(CGRect)rect; | |
4842 | @end | |
4843 | ||
327624b6 | 4844 | @interface ContentView : UIView { |
c21004b9 | 4845 | _transient id<ContentDelegate> delegate_; |
327624b6 JF |
4846 | } |
4847 | ||
4848 | @end | |
4849 | ||
46aa9775 | 4850 | @implementation ContentView |
555aaf71 | 4851 | |
46aa9775 GP |
4852 | - (id) initWithFrame:(CGRect)frame { |
4853 | if ((self = [super initWithFrame:frame]) != nil) { | |
46aa9775 GP |
4854 | [self setNeedsDisplayOnBoundsChange:YES]; |
4855 | } return self; | |
4856 | } | |
4857 | ||
c21004b9 | 4858 | - (void) setDelegate:(id<ContentDelegate>)delegate { |
46aa9775 GP |
4859 | delegate_ = delegate; |
4860 | } | |
4861 | ||
4862 | - (void) drawRect:(CGRect)rect { | |
4863 | [super drawRect:rect]; | |
4864 | [delegate_ drawContentRect:rect]; | |
4865 | } | |
555aaf71 JF |
4866 | |
4867 | @end | |
4868 | /* }}} */ | |
4869 | /* Cydia TableView Cell {{{ */ | |
4870 | @interface CYTableViewCell : UITableViewCell { | |
4871 | ContentView *content_; | |
4872 | bool highlighted_; | |
4873 | } | |
4874 | ||
4875 | @end | |
4876 | ||
4877 | @implementation CYTableViewCell | |
4878 | ||
4879 | - (void) dealloc { | |
4880 | [content_ release]; | |
4881 | [super dealloc]; | |
4882 | } | |
4883 | ||
4884 | - (void) _updateHighlightColorsForView:(id)view highlighted:(BOOL)highlighted { | |
4885 | //NSLog(@"_updateHighlightColorsForView:%@ highlighted:%s [content_=%@]", view, highlighted ? "YES" : "NO", content_); | |
4886 | ||
4887 | if (view == content_) { | |
4888 | //NSLog(@"_updateHighlightColorsForView:content_ highlighted:%s", highlighted ? "YES" : "NO", content_); | |
4889 | highlighted_ = highlighted; | |
4890 | } | |
4891 | ||
4892 | [super _updateHighlightColorsForView:view highlighted:highlighted]; | |
4893 | } | |
4894 | ||
4895 | - (void) setSelected:(BOOL)selected animated:(BOOL)animated { | |
4896 | //NSLog(@"setSelected:%s animated:%s", selected ? "YES" : "NO", animated ? "YES" : "NO"); | |
4897 | highlighted_ = selected; | |
4898 | ||
4899 | [super setSelected:selected animated:animated]; | |
4900 | [content_ setNeedsDisplay]; | |
4901 | } | |
4902 | ||
46aa9775 GP |
4903 | @end |
4904 | /* }}} */ | |
5829aea2 | 4905 | |
46aa9775 | 4906 | /* Package Cell {{{ */ |
555aaf71 | 4907 | @interface PackageCell : CYTableViewCell < |
c21004b9 JF |
4908 | ContentDelegate |
4909 | > { | |
f641a0e5 JF |
4910 | UIImage *icon_; |
4911 | NSString *name_; | |
4912 | NSString *description_; | |
3bd1c2a2 | 4913 | bool commercial_; |
f641a0e5 | 4914 | NSString *source_; |
c390d3ab | 4915 | UIImage *badge_; |
dc63e78f | 4916 | Package *package_; |
670a0494 | 4917 | UIImage *placard_; |
36bb2ca2 | 4918 | } |
723a0072 | 4919 | |
36bb2ca2 JF |
4920 | - (PackageCell *) init; |
4921 | - (void) setPackage:(Package *)package; | |
ec97ef06 | 4922 | |
327624b6 JF |
4923 | - (void) drawContentRect:(CGRect)rect; |
4924 | ||
4925 | @end | |
4926 | ||
36bb2ca2 JF |
4927 | @implementation PackageCell |
4928 | ||
f641a0e5 JF |
4929 | - (void) clearPackage { |
4930 | if (icon_ != nil) { | |
4931 | [icon_ release]; | |
4932 | icon_ = nil; | |
4933 | } | |
4934 | ||
4935 | if (name_ != nil) { | |
4936 | [name_ release]; | |
4937 | name_ = nil; | |
4938 | } | |
4939 | ||
4940 | if (description_ != nil) { | |
4941 | [description_ release]; | |
4942 | description_ = nil; | |
4943 | } | |
4944 | ||
4945 | if (source_ != nil) { | |
4946 | [source_ release]; | |
4947 | source_ = nil; | |
4948 | } | |
c390d3ab JF |
4949 | |
4950 | if (badge_ != nil) { | |
4951 | [badge_ release]; | |
4952 | badge_ = nil; | |
4953 | } | |
dc63e78f | 4954 | |
670a0494 JF |
4955 | if (placard_ != nil) { |
4956 | [placard_ release]; | |
4957 | placard_ = nil; | |
4958 | } | |
4959 | ||
dc63e78f JF |
4960 | [package_ release]; |
4961 | package_ = nil; | |
f641a0e5 JF |
4962 | } |
4963 | ||
36bb2ca2 | 4964 | - (void) dealloc { |
f641a0e5 | 4965 | [self clearPackage]; |
36bb2ca2 | 4966 | [super dealloc]; |
b4d89997 | 4967 | } |
b19871dd | 4968 | |
36bb2ca2 | 4969 | - (PackageCell *) init { |
327624b6 JF |
4970 | CGRect frame(CGRectMake(0, 0, 320, 74)); |
4971 | if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) { | |
4972 | UIView *content([self contentView]); | |
4973 | CGRect bounds([content bounds]); | |
04fe1349 | 4974 | |
327624b6 | 4975 | content_ = [[ContentView alloc] initWithFrame:bounds]; |
04fe1349 JF |
4976 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
4977 | [content addSubview:content_]; | |
4978 | ||
327624b6 | 4979 | [content_ setDelegate:self]; |
327624b6 | 4980 | [content_ setOpaque:YES]; |
36bb2ca2 | 4981 | } return self; |
b4d89997 | 4982 | } |
b19871dd | 4983 | |
327624b6 JF |
4984 | - (void) _setBackgroundColor { |
4985 | UIColor *color; | |
4986 | if (NSString *mode = [package_ mode]) { | |
4987 | bool remove([mode isEqualToString:@"REMOVE"] || [mode isEqualToString:@"PURGE"]); | |
4988 | color = remove ? RemovingColor_ : InstallingColor_; | |
4989 | } else | |
4990 | color = [UIColor whiteColor]; | |
4991 | ||
4992 | [content_ setBackgroundColor:color]; | |
4993 | [self setNeedsDisplay]; | |
4994 | } | |
4995 | ||
36bb2ca2 | 4996 | - (void) setPackage:(Package *)package { |
46dbfd32 | 4997 | [self clearPackage]; |
68d927e2 | 4998 | [package parse]; |
31f3cfff | 4999 | |
36bb2ca2 | 5000 | Source *source = [package source]; |
b19871dd | 5001 | |
770f2a8e | 5002 | icon_ = [[package icon] retain]; |
f641a0e5 | 5003 | name_ = [[package name] retain]; |
ef055c6c JF |
5004 | |
5005 | if (IsWildcat_) | |
5006 | description_ = [package longDescription]; | |
5007 | if (description_ == nil) | |
5008 | description_ = [package shortDescription]; | |
5009 | if (description_ != nil) | |
5010 | description_ = [description_ retain]; | |
5011 | ||
3bd1c2a2 | 5012 | commercial_ = [package isCommercial]; |
36bb2ca2 | 5013 | |
dc63e78f JF |
5014 | package_ = [package retain]; |
5015 | ||
a54b1c10 JF |
5016 | NSString *label = nil; |
5017 | bool trusted = false; | |
b19871dd | 5018 | |
36bb2ca2 JF |
5019 | if (source != nil) { |
5020 | label = [source label]; | |
5021 | trusted = [source trusted]; | |
a54b1c10 | 5022 | } else if ([[package id] isEqualToString:@"firmware"]) |
43f3d7f6 | 5023 | label = UCLocalize("APPLE"); |
7b0ce2da | 5024 | else |
43f3d7f6 | 5025 | label = [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("UNKNOWN"), UCLocalize("LOCAL")]; |
b19871dd | 5026 | |
f79a4512 | 5027 | NSString *from(label); |
a54b1c10 | 5028 | |
f79a4512 JF |
5029 | NSString *section = [package simpleSection]; |
5030 | if (section != nil && ![section isEqualToString:label]) { | |
5031 | section = [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
43f3d7f6 | 5032 | from = [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), from, section]; |
f79a4512 | 5033 | } |
a54b1c10 | 5034 | |
43f3d7f6 | 5035 | from = [NSString stringWithFormat:UCLocalize("FROM"), from]; |
f641a0e5 | 5036 | source_ = [from retain]; |
36bb2ca2 | 5037 | |
c390d3ab JF |
5038 | if (NSString *purpose = [package primaryPurpose]) |
5039 | if ((badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]) != nil) | |
5040 | badge_ = [badge_ retain]; | |
5041 | ||
670a0494 JF |
5042 | if ([package installed] != nil) |
5043 | if ((placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/installed.png", App_]]) != nil) | |
5044 | placard_ = [placard_ retain]; | |
5045 | ||
327624b6 JF |
5046 | [self _setBackgroundColor]; |
5047 | [content_ setNeedsDisplay]; | |
3bd1c2a2 JF |
5048 | } |
5049 | ||
327624b6 | 5050 | - (void) drawContentRect:(CGRect)rect { |
555aaf71 | 5051 | bool highlighted(highlighted_); |
ef055c6c | 5052 | float width([self bounds].size.width); |
dc63e78f | 5053 | |
327624b6 JF |
5054 | #if 0 |
5055 | CGContextRef context(UIGraphicsGetCurrentContext()); | |
5056 | [([[self selectedBackgroundView] superview] != nil ? [UIColor clearColor] : [self backgroundColor]) set]; | |
5057 | CGContextFillRect(context, rect); | |
5058 | #endif | |
b19871dd | 5059 | |
baf80942 JF |
5060 | if (icon_ != nil) { |
5061 | CGRect rect; | |
5062 | rect.size = [icon_ size]; | |
5063 | ||
5064 | rect.size.width /= 2; | |
5065 | rect.size.height /= 2; | |
5066 | ||
5067 | rect.origin.x = 25 - rect.size.width / 2; | |
5068 | rect.origin.y = 25 - rect.size.height / 2; | |
5069 | ||
5070 | [icon_ drawInRect:rect]; | |
5071 | } | |
b19871dd | 5072 | |
c390d3ab | 5073 | if (badge_ != nil) { |
f79c810d JF |
5074 | CGRect rect; |
5075 | rect.size = [badge_ size]; | |
5076 | ||
5077 | rect.size.width /= 2; | |
5078 | rect.size.height /= 2; | |
5079 | ||
5080 | rect.origin.x = 36 - rect.size.width / 2; | |
5081 | rect.origin.y = 36 - rect.size.height / 2; | |
c390d3ab | 5082 | |
f79c810d | 5083 | [badge_ drawInRect:rect]; |
c390d3ab JF |
5084 | } |
5085 | ||
555aaf71 | 5086 | if (highlighted) |
f641a0e5 | 5087 | UISetColor(White_); |
b19871dd | 5088 | |
555aaf71 | 5089 | if (!highlighted) |
3bd1c2a2 | 5090 | UISetColor(commercial_ ? Purple_ : Black_); |
54b844a0 DH |
5091 | [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
5092 | [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; | |
b19871dd | 5093 | |
555aaf71 | 5094 | if (!highlighted) |
3bd1c2a2 | 5095 | UISetColor(commercial_ ? Purplish_ : Gray_); |
54b844a0 | 5096 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:UILineBreakModeTailTruncation]; |
670a0494 JF |
5097 | |
5098 | if (placard_ != nil) | |
01d93940 | 5099 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; |
ec97ef06 JF |
5100 | } |
5101 | ||
8da60fb7 JF |
5102 | @end |
5103 | /* }}} */ | |
36bb2ca2 | 5104 | /* Section Cell {{{ */ |
555aaf71 | 5105 | @interface SectionCell : CYTableViewCell < |
c21004b9 JF |
5106 | ContentDelegate |
5107 | > { | |
0010fa77 | 5108 | NSString *basic_; |
6d9712c4 | 5109 | NSString *section_; |
f641a0e5 JF |
5110 | NSString *name_; |
5111 | NSString *count_; | |
5112 | UIImage *icon_; | |
68046ccc | 5113 | UISwitch *switch_; |
6d9712c4 | 5114 | BOOL editing_; |
b4d89997 | 5115 | } |
b19871dd | 5116 | |
6d9712c4 | 5117 | - (void) setSection:(Section *)section editing:(BOOL)editing; |
36bb2ca2 | 5118 | |
8da60fb7 JF |
5119 | @end |
5120 | ||
36bb2ca2 | 5121 | @implementation SectionCell |
8da60fb7 | 5122 | |
f641a0e5 | 5123 | - (void) clearSection { |
0010fa77 | 5124 | if (basic_ != nil) { |
e59669fd | 5125 | [basic_ release]; |
0010fa77 JF |
5126 | basic_ = nil; |
5127 | } | |
5128 | ||
f641a0e5 | 5129 | if (section_ != nil) { |
6d9712c4 | 5130 | [section_ release]; |
f641a0e5 JF |
5131 | section_ = nil; |
5132 | } | |
7b0ce2da | 5133 | |
f641a0e5 JF |
5134 | if (name_ != nil) { |
5135 | [name_ release]; | |
5136 | name_ = nil; | |
5137 | } | |
7b0ce2da | 5138 | |
f641a0e5 JF |
5139 | if (count_ != nil) { |
5140 | [count_ release]; | |
5141 | count_ = nil; | |
5142 | } | |
7b0ce2da JF |
5143 | } |
5144 | ||
f641a0e5 JF |
5145 | - (void) dealloc { |
5146 | [self clearSection]; | |
5147 | [icon_ release]; | |
5148 | [switch_ release]; | |
5149 | [super dealloc]; | |
7b0ce2da JF |
5150 | } |
5151 | ||
46aa9775 GP |
5152 | - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
5153 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
f641a0e5 | 5154 | icon_ = [[UIImage applicationImageNamed:@"folder.png"] retain]; |
68046ccc | 5155 | switch_ = [[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)]; |
46aa9775 GP |
5156 | [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; |
5157 | ||
5158 | UIView *content([self contentView]); | |
5159 | CGRect bounds([content bounds]); | |
5160 | ||
5161 | content_ = [[ContentView alloc] initWithFrame:bounds]; | |
5162 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
5163 | [content addSubview:content_]; | |
5164 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
5165 | ||
5166 | [content_ setDelegate:self]; | |
b4d89997 | 5167 | } return self; |
b19871dd JF |
5168 | } |
5169 | ||
6d9712c4 | 5170 | - (void) onSwitch:(id)sender { |
a171abd4 | 5171 | NSMutableDictionary *metadata([Sections_ objectForKey:basic_]); |
6d9712c4 JF |
5172 | if (metadata == nil) { |
5173 | metadata = [NSMutableDictionary dictionaryWithCapacity:2]; | |
0010fa77 | 5174 | [Sections_ setObject:metadata forKey:basic_]; |
6d9712c4 JF |
5175 | } |
5176 | ||
46aa9775 | 5177 | [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; |
a171abd4 | 5178 | Changed_ = true; |
6d9712c4 JF |
5179 | } |
5180 | ||
5181 | - (void) setSection:(Section *)section editing:(BOOL)editing { | |
5182 | if (editing != editing_) { | |
5183 | if (editing_) | |
5184 | [switch_ removeFromSuperview]; | |
5185 | else | |
5186 | [self addSubview:switch_]; | |
5187 | editing_ = editing; | |
5188 | } | |
5189 | ||
f641a0e5 | 5190 | [self clearSection]; |
6d9712c4 | 5191 | |
36bb2ca2 | 5192 | if (section == nil) { |
43f3d7f6 | 5193 | name_ = [UCLocalize("ALL_PACKAGES") retain]; |
f641a0e5 | 5194 | count_ = nil; |
36bb2ca2 | 5195 | } else { |
e59669fd | 5196 | basic_ = [section name]; |
0010fa77 JF |
5197 | if (basic_ != nil) |
5198 | basic_ = [basic_ retain]; | |
5199 | ||
677b8415 | 5200 | section_ = [section localized]; |
6d9712c4 JF |
5201 | if (section_ != nil) |
5202 | section_ = [section_ retain]; | |
0010fa77 | 5203 | |
43f3d7f6 | 5204 | name_ = [(section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : section_) retain]; |
f641a0e5 | 5205 | count_ = [[NSString stringWithFormat:@"%d", [section count]] retain]; |
6d9712c4 JF |
5206 | |
5207 | if (editing_) | |
46aa9775 | 5208 | [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; |
36bb2ca2 | 5209 | } |
46aa9775 | 5210 | |
c21004b9 | 5211 | [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; |
0abb648c JF |
5212 | [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; |
5213 | ||
46aa9775 | 5214 | [content_ setNeedsDisplay]; |
f641a0e5 JF |
5215 | } |
5216 | ||
77801ff1 JF |
5217 | - (void) setFrame:(CGRect)frame { |
5218 | [super setFrame:frame]; | |
46aa9775 | 5219 | |
77801ff1 JF |
5220 | CGRect rect([switch_ frame]); |
5221 | [switch_ setFrame:CGRectMake(frame.size.width - 102, 9, rect.size.width, rect.size.height)]; | |
5222 | } | |
5223 | ||
46aa9775 | 5224 | - (void) drawContentRect:(CGRect)rect { |
8cfba088 | 5225 | bool highlighted(highlighted_ && !editing_); |
bc11cf5b | 5226 | |
f641a0e5 JF |
5227 | [icon_ drawInRect:CGRectMake(8, 7, 32, 32)]; |
5228 | ||
555aaf71 | 5229 | if (highlighted) |
f641a0e5 JF |
5230 | UISetColor(White_); |
5231 | ||
46aa9775 | 5232 | float width(rect.size.width); |
58241d4c | 5233 | if (editing_) |
46aa9775 | 5234 | width -= 87; |
58241d4c | 5235 | |
555aaf71 JF |
5236 | if (!highlighted) |
5237 | UISetColor(Black_); | |
54b844a0 | 5238 | [name_ drawAtPoint:CGPointMake(48, 9) forWidth:(width - 70) withFont:Font22Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
6d9712c4 | 5239 | |
f641a0e5 JF |
5240 | CGSize size = [count_ sizeWithFont:Font14_]; |
5241 | ||
5242 | UISetColor(White_); | |
5243 | if (count_ != nil) | |
c390d3ab | 5244 | [count_ drawAtPoint:CGPointMake(13 + (29 - size.width) / 2, 16) withFont:Font12Bold_]; |
b19871dd JF |
5245 | } |
5246 | ||
36bb2ca2 JF |
5247 | @end |
5248 | /* }}} */ | |
b19871dd | 5249 | |
ab398adf | 5250 | /* File Table {{{ */ |
c21004b9 JF |
5251 | @interface FileTable : CYViewController < |
5252 | UITableViewDataSource, | |
5253 | UITableViewDelegate | |
5254 | > { | |
36bb2ca2 | 5255 | _transient Database *database_; |
ab398adf JF |
5256 | Package *package_; |
5257 | NSString *name_; | |
5258 | NSMutableArray *files_; | |
46aa9775 | 5259 | UITableView *list_; |
ab398adf | 5260 | } |
b19871dd | 5261 | |
b5e7eebb | 5262 | - (id) initWithDatabase:(Database *)database; |
ab398adf JF |
5263 | - (void) setPackage:(Package *)package; |
5264 | ||
5265 | @end | |
5266 | ||
5267 | @implementation FileTable | |
5268 | ||
5269 | - (void) dealloc { | |
5270 | if (package_ != nil) | |
5271 | [package_ release]; | |
5272 | if (name_ != nil) | |
5273 | [name_ release]; | |
5274 | [files_ release]; | |
5275 | [list_ release]; | |
5276 | [super dealloc]; | |
5277 | } | |
5278 | ||
eb30da80 | 5279 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
ab398adf JF |
5280 | return files_ == nil ? 0 : [files_ count]; |
5281 | } | |
5282 | ||
21ea11a4 GP |
5283 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
5284 | return 24.0f; | |
5285 | }*/ | |
ab398adf | 5286 | |
46aa9775 | 5287 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
b5e7eebb GP |
5288 | static NSString *reuseIdentifier = @"Cell"; |
5289 | ||
46aa9775 GP |
5290 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5291 | if (cell == nil) { | |
5292 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
5293 | [cell setFont:[UIFont systemFontOfSize:16]]; | |
ab398adf | 5294 | } |
46aa9775 | 5295 | [cell setText:[files_ objectAtIndex:indexPath.row]]; |
c21004b9 | 5296 | [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
b5e7eebb | 5297 | |
46aa9775 | 5298 | return cell; |
ab398adf | 5299 | } |
b19871dd | 5300 | |
b5e7eebb GP |
5301 | - (id) initWithDatabase:(Database *)database { |
5302 | if ((self = [super init]) != nil) { | |
ab398adf | 5303 | database_ = database; |
b19871dd | 5304 | |
b5e7eebb GP |
5305 | [[self navigationItem] setTitle:UCLocalize("INSTALLED_FILES")]; |
5306 | ||
ab398adf | 5307 | files_ = [[NSMutableArray arrayWithCapacity:32] retain]; |
8da60fb7 | 5308 | |
46aa9775 | 5309 | list_ = [[UITableView alloc] initWithFrame:[[self view] bounds]]; |
21ea11a4 GP |
5310 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5311 | [list_ setRowHeight:24.0f]; | |
b5e7eebb | 5312 | [[self view] addSubview:list_]; |
ab398adf | 5313 | |
ab398adf | 5314 | [list_ setDataSource:self]; |
ab398adf | 5315 | [list_ setDelegate:self]; |
ab398adf JF |
5316 | } return self; |
5317 | } | |
5318 | ||
5319 | - (void) setPackage:(Package *)package { | |
5320 | if (package_ != nil) { | |
5321 | [package_ autorelease]; | |
5322 | package_ = nil; | |
5323 | } | |
5324 | ||
5325 | if (name_ != nil) { | |
5326 | [name_ release]; | |
5327 | name_ = nil; | |
5328 | } | |
5329 | ||
5330 | [files_ removeAllObjects]; | |
5331 | ||
5332 | if (package != nil) { | |
5333 | package_ = [package retain]; | |
5334 | name_ = [[package id] retain]; | |
5335 | ||
affeffc7 JF |
5336 | if (NSArray *files = [package files]) |
5337 | [files_ addObjectsFromArray:files]; | |
ab398adf | 5338 | |
9ea8d159 JF |
5339 | if ([files_ count] != 0) { |
5340 | if ([[files_ objectAtIndex:0] isEqualToString:@"/."]) | |
5341 | [files_ removeObjectAtIndex:0]; | |
a54b1c10 | 5342 | [files_ sortUsingSelector:@selector(compareByPath:)]; |
2388b078 JF |
5343 | |
5344 | NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8]; | |
5345 | [stack addObject:@"/"]; | |
5346 | ||
5347 | for (int i(0), e([files_ count]); i != e; ++i) { | |
5348 | NSString *file = [files_ objectAtIndex:i]; | |
5349 | while (![file hasPrefix:[stack lastObject]]) | |
5350 | [stack removeLastObject]; | |
5351 | NSString *directory = [stack lastObject]; | |
5352 | [stack addObject:[file stringByAppendingString:@"/"]]; | |
5353 | [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", | |
a54b1c10 | 5354 | ([stack count] - 2) * 3, "", |
2388b078 JF |
5355 | [file substringFromIndex:[directory length]] |
5356 | ]]; | |
5357 | } | |
ab398adf JF |
5358 | } |
5359 | } | |
5360 | ||
5361 | [list_ reloadData]; | |
5362 | } | |
5363 | ||
ab398adf JF |
5364 | - (void) reloadData { |
5365 | [self setPackage:[database_ packageWithName:name_]]; | |
ab398adf | 5366 | } |
b4d89997 | 5367 | |
b4d89997 | 5368 | @end |
36bb2ca2 | 5369 | /* }}} */ |
adb61bda | 5370 | /* Package Controller {{{ */ |
f6e13561 | 5371 | @interface CYPackageController : CYBrowserController < |
9daa7f25 DH |
5372 | UIActionSheetDelegate |
5373 | > { | |
770f2a8e | 5374 | _transient Database *database_; |
36bb2ca2 JF |
5375 | Package *package_; |
5376 | NSString *name_; | |
3bd1c2a2 | 5377 | bool commercial_; |
5a09ae08 | 5378 | NSMutableArray *buttons_; |
2634b249 | 5379 | UIBarButtonItem *button_; |
b31b87cc JF |
5380 | } |
5381 | ||
b5e7eebb | 5382 | - (id) initWithDatabase:(Database *)database; |
36bb2ca2 | 5383 | - (void) setPackage:(Package *)package; |
b4d89997 | 5384 | |
36bb2ca2 | 5385 | @end |
b4d89997 | 5386 | |
f6e13561 | 5387 | @implementation CYPackageController |
b4d89997 | 5388 | |
36bb2ca2 | 5389 | - (void) dealloc { |
36bb2ca2 JF |
5390 | if (package_ != nil) |
5391 | [package_ release]; | |
5392 | if (name_ != nil) | |
5393 | [name_ release]; | |
2634b249 | 5394 | |
5a09ae08 | 5395 | [buttons_ release]; |
2634b249 JF |
5396 | |
5397 | if (button_ != nil) | |
5398 | [button_ release]; | |
5399 | ||
36bb2ca2 JF |
5400 | [super dealloc]; |
5401 | } | |
8da60fb7 | 5402 | |
92d4a16a | 5403 | - (void) release { |
f79a4512 | 5404 | if ([self retainCount] == 1) |
adb61bda | 5405 | [delegate_ setPackageController:self]; |
f79a4512 | 5406 | [super release]; |
92d4a16a | 5407 | } |
f79a4512 JF |
5408 | |
5409 | /* XXX: this is not safe at all... localization of /fail/ */ | |
5a09ae08 | 5410 | - (void) _clickButtonWithName:(NSString *)name { |
43f3d7f6 | 5411 | if ([name isEqualToString:UCLocalize("CLEAR")]) |
dc63e78f | 5412 | [delegate_ clearPackage:package_]; |
43f3d7f6 | 5413 | else if ([name isEqualToString:UCLocalize("INSTALL")]) |
5a09ae08 | 5414 | [delegate_ installPackage:package_]; |
43f3d7f6 | 5415 | else if ([name isEqualToString:UCLocalize("REINSTALL")]) |
5a09ae08 | 5416 | [delegate_ installPackage:package_]; |
43f3d7f6 | 5417 | else if ([name isEqualToString:UCLocalize("REMOVE")]) |
5a09ae08 | 5418 | [delegate_ removePackage:package_]; |
43f3d7f6 | 5419 | else if ([name isEqualToString:UCLocalize("UPGRADE")]) |
5a09ae08 JF |
5420 | [delegate_ installPackage:package_]; |
5421 | else _assert(false); | |
5422 | } | |
5423 | ||
674dce72 | 5424 | - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { |
1cedb821 | 5425 | NSString *context([sheet context]); |
5a09ae08 | 5426 | |
1cedb821 | 5427 | if ([context isEqualToString:@"modify"]) { |
674dce72 GP |
5428 | if (button != [sheet cancelButtonIndex]) { |
5429 | NSString *buttonName = [buttons_ objectAtIndex:button]; | |
5430 | [self _clickButtonWithName:buttonName]; | |
5431 | } | |
bc11cf5b | 5432 | |
674dce72 | 5433 | [sheet dismissWithClickedButtonIndex:-1 animated:YES]; |
674dce72 | 5434 | } |
b4d89997 | 5435 | } |
2367a917 | 5436 | |
2634b249 JF |
5437 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5438 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
f79a4512 | 5439 | [window setValue:package_ forKey:@"package"]; |
6f1a15d9 JF |
5440 | } |
5441 | ||
3e9c9e85 | 5442 | - (bool) _allowJavaScriptPanel { |
3bd1c2a2 | 5443 | return commercial_; |
3e9c9e85 JF |
5444 | } |
5445 | ||
9487f027 | 5446 | #if !AlwaysReload |
9daa7f25 | 5447 | - (void) _customButtonClicked { |
670a0494 JF |
5448 | int count([buttons_ count]); |
5449 | if (count == 0) | |
5450 | return; | |
2367a917 | 5451 | |
5a09ae08 JF |
5452 | if (count == 1) |
5453 | [self _clickButtonWithName:[buttons_ objectAtIndex:0]]; | |
5454 | else { | |
674dce72 | 5455 | NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; |
5a09ae08 | 5456 | [buttons addObjectsFromArray:buttons_]; |
36bb2ca2 | 5457 | |
674dce72 | 5458 | UIActionSheet *sheet = [[[UIActionSheet alloc] |
9ea8d159 | 5459 | initWithTitle:nil |
36bb2ca2 | 5460 | delegate:self |
674dce72 GP |
5461 | cancelButtonTitle:nil |
5462 | destructiveButtonTitle:nil | |
5463 | otherButtonTitles:nil | |
5464 | ] autorelease]; | |
5465 | ||
5466 | for (NSString *button in buttons) [sheet addButtonWithTitle:button]; | |
5467 | if (!IsWildcat_) { | |
5468 | [sheet addButtonWithTitle:UCLocalize("CANCEL")]; | |
5469 | [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; | |
5470 | } | |
5471 | [sheet setContext:@"modify"]; | |
bc11cf5b | 5472 | |
b5e7eebb | 5473 | [delegate_ showActionSheet:sheet fromItem:[[self navigationItem] rightBarButtonItem]]; |
36bb2ca2 | 5474 | } |
b4d89997 | 5475 | } |
12b59862 | 5476 | |
a5938ea5 DH |
5477 | // We don't want to allow non-commercial packages to do custom things to the install button, |
5478 | // so it must call customButtonClicked with a custom commercial_ == 1 fallthrough. | |
9daa7f25 | 5479 | - (void) customButtonClicked { |
a5938ea5 DH |
5480 | if (commercial_) |
5481 | [super customButtonClicked]; | |
5482 | else | |
9daa7f25 | 5483 | [self _customButtonClicked]; |
12b59862 | 5484 | } |
2e6c1426 GP |
5485 | |
5486 | - (void) reloadButtonClicked { | |
6a8591be GP |
5487 | // Don't reload a commerical package by tapping the loading button, |
5488 | // but if it's not an Install button, we should forward it on. | |
5489 | if (![package_ uninstalled]) | |
5490 | [self _customButtonClicked]; | |
2fad210a GP |
5491 | } |
5492 | ||
5493 | - (void) applyLoadingTitle { | |
5494 | // Don't show "Loading" as the title. Ever. | |
2e6c1426 | 5495 | } |
a5938ea5 DH |
5496 | |
5497 | - (UIBarButtonItem *) rightButton { | |
2634b249 | 5498 | return button_; |
a5938ea5 | 5499 | } |
9487f027 | 5500 | #endif |
2367a917 | 5501 | |
b5e7eebb GP |
5502 | - (id) initWithDatabase:(Database *)database { |
5503 | if ((self = [super init]) != nil) { | |
36bb2ca2 | 5504 | database_ = database; |
5a09ae08 | 5505 | buttons_ = [[NSMutableArray alloc] initWithCapacity:4]; |
f79a4512 | 5506 | [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"package" ofType:@"html"]]]; |
36bb2ca2 | 5507 | } return self; |
dc5812ec JF |
5508 | } |
5509 | ||
36bb2ca2 JF |
5510 | - (void) setPackage:(Package *)package { |
5511 | if (package_ != nil) { | |
5512 | [package_ autorelease]; | |
5513 | package_ = nil; | |
5514 | } | |
5515 | ||
5516 | if (name_ != nil) { | |
5517 | [name_ release]; | |
5518 | name_ = nil; | |
5519 | } | |
5520 | ||
5a09ae08 JF |
5521 | [buttons_ removeAllObjects]; |
5522 | ||
36bb2ca2 | 5523 | if (package != nil) { |
68d927e2 JF |
5524 | [package parse]; |
5525 | ||
36bb2ca2 JF |
5526 | package_ = [package retain]; |
5527 | name_ = [[package id] retain]; | |
3bd1c2a2 | 5528 | commercial_ = [package isCommercial]; |
36bb2ca2 | 5529 | |
dc63e78f | 5530 | if ([package_ mode] != nil) |
43f3d7f6 | 5531 | [buttons_ addObject:UCLocalize("CLEAR")]; |
5a09ae08 | 5532 | if ([package_ source] == nil); |
31f3cfff | 5533 | else if ([package_ upgradableAndEssential:NO]) |
43f3d7f6 | 5534 | [buttons_ addObject:UCLocalize("UPGRADE")]; |
6a155117 | 5535 | else if ([package_ uninstalled]) |
43f3d7f6 | 5536 | [buttons_ addObject:UCLocalize("INSTALL")]; |
5a09ae08 | 5537 | else |
43f3d7f6 | 5538 | [buttons_ addObject:UCLocalize("REINSTALL")]; |
6a155117 | 5539 | if (![package_ uninstalled]) |
43f3d7f6 | 5540 | [buttons_ addObject:UCLocalize("REMOVE")]; |
2634b249 | 5541 | } |
f79a4512 | 5542 | |
2634b249 JF |
5543 | if (button_ != nil) |
5544 | [button_ release]; | |
f79a4512 | 5545 | |
2634b249 JF |
5546 | NSString *title; |
5547 | switch ([buttons_ count]) { | |
5548 | case 0: title = nil; break; | |
5549 | case 1: title = [buttons_ objectAtIndex:0]; break; | |
5550 | default: title = UCLocalize("MODIFY"); break; | |
36bb2ca2 | 5551 | } |
2634b249 JF |
5552 | |
5553 | button_ = [[UIBarButtonItem alloc] | |
5554 | initWithTitle:title | |
5555 | style:UIBarButtonItemStylePlain | |
5556 | target:self | |
5557 | action:@selector(customButtonClicked) | |
5558 | ]; | |
56e88a25 JF |
5559 | |
5560 | [self reloadURL]; | |
b5e7eebb | 5561 | } |
f79a4512 | 5562 | |
3bd1c2a2 JF |
5563 | - (bool) isLoading { |
5564 | return commercial_ ? [super isLoading] : false; | |
9fe5e5f8 JF |
5565 | } |
5566 | ||
36bb2ca2 JF |
5567 | - (void) reloadData { |
5568 | [self setPackage:[database_ packageWithName:name_]]; | |
8fe19fc1 JF |
5569 | } |
5570 | ||
b4d89997 JF |
5571 | @end |
5572 | /* }}} */ | |
5829aea2 | 5573 | |
b4d89997 | 5574 | /* Package Table {{{ */ |
c21004b9 JF |
5575 | @interface PackageTable : UIView < |
5576 | UITableViewDataSource, | |
5577 | UITableViewDelegate | |
5578 | > { | |
36bb2ca2 | 5579 | _transient Database *database_; |
0175295c | 5580 | unsigned era_; |
36bb2ca2 | 5581 | NSMutableArray *packages_; |
b4d89997 | 5582 | NSMutableArray *sections_; |
327624b6 JF |
5583 | UITableView *list_; |
5584 | NSMutableArray *index_; | |
5585 | NSMutableDictionary *indices_; | |
3931b718 JF |
5586 | // XXX: this target_ seems to be delegate_. :( |
5587 | _transient id target_; | |
b5e7eebb | 5588 | SEL action_; |
3931b718 JF |
5589 | // XXX: why do we even have this delegate_? |
5590 | _transient id delegate_; | |
dc5812ec JF |
5591 | } |
5592 | ||
b5e7eebb | 5593 | - (id) initWithFrame:(CGRect)frame database:(Database *)database target:(id)target action:(SEL)action; |
b4d89997 JF |
5594 | |
5595 | - (void) setDelegate:(id)delegate; | |
a0be02eb | 5596 | |
36bb2ca2 | 5597 | - (void) reloadData; |
a0be02eb | 5598 | - (void) resetCursor; |
b4d89997 | 5599 | |
327624b6 | 5600 | - (UITableView *) list; |
2388b078 | 5601 | |
ab398adf JF |
5602 | - (void) setShouldHideHeaderInShortLists:(BOOL)hide; |
5603 | ||
b5e7eebb GP |
5604 | - (void) deselectWithAnimation:(BOOL)animated; |
5605 | ||
b4d89997 JF |
5606 | @end |
5607 | ||
5608 | @implementation PackageTable | |
5609 | ||
5610 | - (void) dealloc { | |
36bb2ca2 | 5611 | [packages_ release]; |
b4d89997 | 5612 | [sections_ release]; |
36bb2ca2 | 5613 | [list_ release]; |
327624b6 JF |
5614 | [index_ release]; |
5615 | [indices_ release]; | |
b5e7eebb | 5616 | |
b4d89997 | 5617 | [super dealloc]; |
8fe19fc1 JF |
5618 | } |
5619 | ||
a891c345 GP |
5620 | + (BOOL) hasIndexedCollation { |
5621 | return NO; // XXX: objc_getClass("UILocalizedIndexedCollation") != nil; | |
5622 | } | |
5623 | ||
327624b6 JF |
5624 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
5625 | NSInteger count([sections_ count]); | |
5626 | return count == 0 ? 1 : count; | |
b4d89997 | 5627 | } |
2367a917 | 5628 | |
327624b6 | 5629 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
a891c345 | 5630 | if ([sections_ count] == 0 || [[sections_ objectAtIndex:section] count] == 0) |
327624b6 | 5631 | return nil; |
b4d89997 JF |
5632 | return [[sections_ objectAtIndex:section] name]; |
5633 | } | |
dc5812ec | 5634 | |
327624b6 JF |
5635 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
5636 | if ([sections_ count] == 0) | |
5637 | return 0; | |
5638 | return [[sections_ objectAtIndex:section] count]; | |
b4d89997 | 5639 | } |
dc5812ec | 5640 | |
327624b6 | 5641 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
0175295c JF |
5642 | @synchronized (database_) { |
5643 | if ([database_ era] != era_) | |
5644 | return nil; | |
5645 | ||
327624b6 JF |
5646 | Section *section([sections_ objectAtIndex:[path section]]); |
5647 | NSInteger row([path row]); | |
5648 | Package *package([packages_ objectAtIndex:([section row] + row)]); | |
0175295c JF |
5649 | return [[package retain] autorelease]; |
5650 | } } | |
dc5812ec | 5651 | |
327624b6 | 5652 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
c21004b9 | 5653 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
327624b6 JF |
5654 | if (cell == nil) |
5655 | cell = [[[PackageCell alloc] init] autorelease]; | |
5656 | [cell setPackage:[self packageAtIndexPath:path]]; | |
5657 | return cell; | |
b4d89997 | 5658 | } |
dc5812ec | 5659 | |
b5e7eebb GP |
5660 | - (void) deselectWithAnimation:(BOOL)animated { |
5661 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
5662 | } | |
5663 | ||
327624b6 JF |
5664 | - (NSIndexPath *) tableView:(UITableView *)table willSelectRowAtIndexPath:(NSIndexPath *)path { |
5665 | Package *package([self packageAtIndexPath:path]); | |
59c6ae22 | 5666 | package = [database_ packageWithName:[package id]]; |
b5e7eebb | 5667 | [target_ performSelector:action_ withObject:package]; |
327624b6 JF |
5668 | return path; |
5669 | } | |
5670 | ||
5671 | - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { | |
a891c345 | 5672 | // XXX: is 20 the most optimal number here? |
327624b6 JF |
5673 | return [packages_ count] > 20 ? index_ : nil; |
5674 | } | |
5675 | ||
a891c345 GP |
5676 | - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { |
5677 | if ([[self class] hasIndexedCollation]) { | |
5678 | return [[objc_getClass("UILocalizedIndexedCollation") currentCollation] sectionForSectionIndexTitleAtIndex:index]; | |
5679 | } | |
5680 | ||
327624b6 | 5681 | return index; |
dc5812ec JF |
5682 | } |
5683 | ||
b5e7eebb GP |
5684 | - (id) initWithFrame:(CGRect)frame database:(Database *)database target:(id)target action:(SEL)action { |
5685 | if ((self = [super initWithFrame:frame]) != nil) { | |
36bb2ca2 | 5686 | database_ = database; |
b5e7eebb GP |
5687 | |
5688 | target_ = target; | |
5689 | action_ = action; | |
36bb2ca2 | 5690 | |
a891c345 GP |
5691 | index_ = [[self class] hasIndexedCollation] |
5692 | ? [[[objc_getClass("UILocalizedIndexedCollation") currentCollation] sectionIndexTitles] retain] | |
5693 | : [[NSMutableArray alloc] initWithCapacity:32]; | |
327624b6 JF |
5694 | indices_ = [[NSMutableDictionary alloc] initWithCapacity:32]; |
5695 | ||
36bb2ca2 | 5696 | packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; |
b4d89997 | 5697 | sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; |
dc5812ec | 5698 | |
327624b6 | 5699 | list_ = [[UITableView alloc] initWithFrame:[self bounds] style:UITableViewStylePlain]; |
04fe1349 | 5700 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
d2741c0a | 5701 | [list_ setRowHeight:73]; |
04fe1349 JF |
5702 | [self addSubview:list_]; |
5703 | ||
b4d89997 | 5704 | [list_ setDataSource:self]; |
327624b6 | 5705 | [list_ setDelegate:self]; |
b4d89997 | 5706 | } return self; |
dc5812ec JF |
5707 | } |
5708 | ||
5709 | - (void) setDelegate:(id)delegate { | |
2367a917 | 5710 | delegate_ = delegate; |
b4d89997 JF |
5711 | } |
5712 | ||
a3328c28 JF |
5713 | - (bool) hasPackage:(Package *)package { |
5714 | return true; | |
a0be02eb JF |
5715 | } |
5716 | ||
36bb2ca2 | 5717 | - (void) reloadData { |
0175295c | 5718 | era_ = [database_ era]; |
36bb2ca2 | 5719 | NSArray *packages = [database_ packages]; |
b4d89997 | 5720 | |
36bb2ca2 | 5721 | [packages_ removeAllObjects]; |
b4d89997 JF |
5722 | [sections_ removeAllObjects]; |
5723 | ||
808c6eb6 | 5724 | _profile(PackageTable$reloadData$Filter) |
c4dcf2c2 | 5725 | for (Package *package in packages) |
808c6eb6 JF |
5726 | if ([self hasPackage:package]) |
5727 | [packages_ addObject:package]; | |
808c6eb6 | 5728 | _end |
36bb2ca2 | 5729 | |
327624b6 JF |
5730 | [indices_ removeAllObjects]; |
5731 | ||
b4d89997 JF |
5732 | Section *section = nil; |
5733 | ||
a891c345 GP |
5734 | if ([[self class] hasIndexedCollation]) { |
5735 | id collation = [objc_getClass("UILocalizedIndexedCollation") currentCollation]; | |
5736 | NSArray *titles = [collation sectionIndexTitles]; | |
5737 | int secidx = -1; | |
c4dcf2c2 | 5738 | |
a891c345 GP |
5739 | _profile(PackageTable$reloadData$Section) |
5740 | for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { | |
5741 | Package *package; | |
5742 | int index; | |
b4d89997 | 5743 | |
a891c345 GP |
5744 | _profile(PackageTable$reloadData$Section$Package) |
5745 | package = [packages_ objectAtIndex:offset]; | |
5746 | index = [collation sectionForObject:package collationStringSelector:@selector(name)]; | |
808c6eb6 | 5747 | _end |
b4d89997 | 5748 | |
a891c345 GP |
5749 | while (secidx < index) { |
5750 | secidx += 1; | |
327624b6 | 5751 | |
a891c345 GP |
5752 | _profile(PackageTable$reloadData$Section$Allocate) |
5753 | section = [[[Section alloc] initWithName:[titles objectAtIndex:secidx] row:offset localize:NO] autorelease]; | |
5754 | _end | |
5755 | ||
5756 | _profile(PackageTable$reloadData$Section$Add) | |
5757 | [sections_ addObject:section]; | |
5758 | _end | |
5759 | } | |
5760 | ||
5761 | [section addToCount]; | |
808c6eb6 | 5762 | } |
a891c345 GP |
5763 | _end |
5764 | } else { | |
5765 | [index_ removeAllObjects]; | |
808c6eb6 | 5766 | |
a891c345 GP |
5767 | _profile(PackageTable$reloadData$Section) |
5768 | for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { | |
5769 | Package *package; | |
5770 | unichar index; | |
5771 | ||
5772 | _profile(PackageTable$reloadData$Section$Package) | |
5773 | package = [packages_ objectAtIndex:offset]; | |
5774 | index = [package index]; | |
5775 | _end | |
5776 | ||
5777 | if (section == nil || [section index] != index) { | |
5778 | _profile(PackageTable$reloadData$Section$Allocate) | |
5779 | section = [[[Section alloc] initWithIndex:index row:offset] autorelease]; | |
5780 | _end | |
5781 | ||
5782 | [index_ addObject:[section name]]; | |
5783 | //[indices_ setObject:[NSNumber numberForInt:[sections_ count]] forKey:index]; | |
5784 | ||
5785 | _profile(PackageTable$reloadData$Section$Add) | |
5786 | [sections_ addObject:section]; | |
5787 | _end | |
5788 | } | |
5789 | ||
5790 | [section addToCount]; | |
5791 | } | |
5792 | _end | |
5793 | } | |
b4d89997 | 5794 | |
c4dcf2c2 JF |
5795 | _profile(PackageTable$reloadData$List) |
5796 | [list_ reloadData]; | |
5797 | _end | |
b4d89997 JF |
5798 | } |
5799 | ||
a0be02eb | 5800 | - (void) resetCursor { |
327624b6 | 5801 | [list_ scrollRectToVisible:CGRectMake(0, 0, 0, 0) animated:NO]; |
a0be02eb JF |
5802 | } |
5803 | ||
327624b6 | 5804 | - (UITableView *) list { |
2388b078 JF |
5805 | return list_; |
5806 | } | |
5807 | ||
ab398adf | 5808 | - (void) setShouldHideHeaderInShortLists:(BOOL)hide { |
327624b6 | 5809 | //XXX:[list_ setShouldHideHeaderInShortLists:hide]; |
ab398adf JF |
5810 | } |
5811 | ||
a3328c28 JF |
5812 | @end |
5813 | /* }}} */ | |
5814 | /* Filtered Package Table {{{ */ | |
5815 | @interface FilteredPackageTable : PackageTable { | |
5816 | SEL filter_; | |
76933519 | 5817 | IMP imp_; |
a3328c28 JF |
5818 | id object_; |
5819 | } | |
5820 | ||
5821 | - (void) setObject:(id)object; | |
01d93940 | 5822 | - (void) setObject:(id)object forFilter:(SEL)filter; |
a3328c28 | 5823 | |
b5e7eebb | 5824 | - (id) initWithFrame:(CGRect)frame database:(Database *)database target:(id)target action:(SEL)action filter:(SEL)filter with:(id)object; |
a3328c28 JF |
5825 | |
5826 | @end | |
5827 | ||
5828 | @implementation FilteredPackageTable | |
5829 | ||
5830 | - (void) dealloc { | |
5831 | if (object_ != nil) | |
5832 | [object_ release]; | |
5833 | [super dealloc]; | |
5834 | } | |
5835 | ||
01d93940 JF |
5836 | - (void) setFilter:(SEL)filter { |
5837 | filter_ = filter; | |
5838 | ||
5839 | /* XXX: this is an unsafe optimization of doomy hell */ | |
5840 | Method method(class_getInstanceMethod([Package class], filter)); | |
5841 | _assert(method != NULL); | |
5842 | imp_ = method_getImplementation(method); | |
5843 | _assert(imp_ != NULL); | |
5844 | } | |
5845 | ||
a3328c28 JF |
5846 | - (void) setObject:(id)object { |
5847 | if (object_ != nil) | |
5848 | [object_ release]; | |
5849 | if (object == nil) | |
5850 | object_ = nil; | |
5851 | else | |
5852 | object_ = [object retain]; | |
5853 | } | |
5854 | ||
01d93940 JF |
5855 | - (void) setObject:(id)object forFilter:(SEL)filter { |
5856 | [self setFilter:filter]; | |
5857 | [self setObject:object]; | |
01d93940 JF |
5858 | } |
5859 | ||
a3328c28 | 5860 | - (bool) hasPackage:(Package *)package { |
76933519 JF |
5861 | _profile(FilteredPackageTable$hasPackage) |
5862 | return [package valid] && (*reinterpret_cast<bool (*)(id, SEL, id)>(imp_))(package, filter_, object_); | |
5863 | _end | |
a3328c28 JF |
5864 | } |
5865 | ||
b5e7eebb GP |
5866 | - (id) initWithFrame:(CGRect)frame database:(Database *)database target:(id)target action:(SEL)action filter:(SEL)filter with:(id)object { |
5867 | if ((self = [super initWithFrame:frame database:database target:target action:action]) != nil) { | |
01d93940 | 5868 | [self setFilter:filter]; |
b5e7eebb | 5869 | object_ = [object retain]; |
189a73d0 | 5870 | [self reloadData]; |
a3328c28 JF |
5871 | } return self; |
5872 | } | |
5873 | ||
dc5812ec | 5874 | @end |
b4d89997 | 5875 | /* }}} */ |
adb61bda GP |
5876 | /* Filtered Package Controller {{{ */ |
5877 | @interface FilteredPackageController : CYViewController { | |
b5e7eebb GP |
5878 | _transient Database *database_; |
5879 | FilteredPackageTable *packages_; | |
5880 | NSString *title_; | |
5881 | } | |
5882 | ||
5883 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object; | |
5884 | ||
5885 | @end | |
bc11cf5b | 5886 | |
adb61bda | 5887 | @implementation FilteredPackageController |
b5e7eebb GP |
5888 | |
5889 | - (void) dealloc { | |
5890 | [packages_ release]; | |
5891 | [title_ release]; | |
bc11cf5b | 5892 | |
b5e7eebb GP |
5893 | [super dealloc]; |
5894 | } | |
5895 | ||
5896 | - (void) viewDidAppear:(BOOL)animated { | |
5897 | [super viewDidAppear:animated]; | |
5898 | [packages_ deselectWithAnimation:animated]; | |
5899 | } | |
5900 | ||
5901 | - (void) didSelectPackage:(Package *)package { | |
f6e13561 | 5902 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_] autorelease]); |
b5e7eebb GP |
5903 | [view setPackage:package]; |
5904 | [view setDelegate:delegate_]; | |
5905 | [[self navigationController] pushViewController:view animated:YES]; | |
5906 | } | |
5907 | ||
eb30da80 | 5908 | - (NSString *) title { return title_; } |
b5e7eebb GP |
5909 | |
5910 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object { | |
5911 | if ((self = [super init]) != nil) { | |
5912 | database_ = database; | |
5913 | title_ = [title copy]; | |
5914 | [[self navigationItem] setTitle:title_]; | |
5915 | ||
5916 | packages_ = [[FilteredPackageTable alloc] | |
5917 | initWithFrame:[[self view] bounds] | |
5918 | database:database | |
5919 | target:self | |
5920 | action:@selector(didSelectPackage:) | |
5921 | filter:filter | |
5922 | with:object | |
5923 | ]; | |
5924 | ||
5925 | [packages_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
5926 | [[self view] addSubview:packages_]; | |
5927 | } return self; | |
5928 | } | |
5929 | ||
5930 | - (void) reloadData { | |
5931 | [packages_ reloadData]; | |
5932 | } | |
5933 | ||
5934 | - (void) setDelegate:(id)delegate { | |
5935 | [super setDelegate:delegate]; | |
5936 | [packages_ setDelegate:delegate]; | |
5937 | } | |
5938 | ||
5939 | @end | |
bc11cf5b | 5940 | |
b5e7eebb GP |
5941 | /* }}} */ |
5942 | ||
5829aea2 GP |
5943 | /* Home Controller {{{ */ |
5944 | @interface HomeController : CYBrowserController { | |
b4d89997 | 5945 | } |
7b0ce2da | 5946 | @end |
b4d89997 | 5947 | |
5829aea2 | 5948 | @implementation HomeController |
46aa9775 | 5949 | |
5829aea2 GP |
5950 | + (BOOL)shouldHideNavigationBar { |
5951 | return NO; | |
46aa9775 GP |
5952 | } |
5953 | ||
5829aea2 GP |
5954 | - (void) _setMoreHeaders:(NSMutableURLRequest *)request { |
5955 | [super _setMoreHeaders:request]; | |
46aa9775 | 5956 | |
5829aea2 GP |
5957 | if (ChipID_ != nil) |
5958 | [request setValue:ChipID_ forHTTPHeaderField:@"X-Chip-ID"]; | |
5959 | if (UniqueID_ != nil) | |
5960 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
5961 | if (PLMN_ != nil) | |
5962 | [request setValue:PLMN_ forHTTPHeaderField:@"X-Carrier-ID"]; | |
46aa9775 GP |
5963 | } |
5964 | ||
5829aea2 GP |
5965 | - (void) aboutButtonClicked { |
5966 | UIAlertView *alert([[[UIAlertView alloc] init] autorelease]); | |
b4d89997 | 5967 | |
5829aea2 GP |
5968 | [alert setTitle:UCLocalize("ABOUT_CYDIA")]; |
5969 | [alert addButtonWithTitle:UCLocalize("CLOSE")]; | |
5970 | [alert setCancelButtonIndex:0]; | |
46aa9775 | 5971 | |
5829aea2 GP |
5972 | [alert setMessage: |
5973 | @"Copyright (C) 2008-2010\n" | |
5974 | "Jay Freeman (saurik)\n" | |
5975 | "saurik@saurik.com\n" | |
5976 | "http://www.saurik.com/" | |
5977 | ]; | |
46aa9775 | 5978 | |
5829aea2 | 5979 | [alert show]; |
a0376fc1 JF |
5980 | } |
5981 | ||
5829aea2 GP |
5982 | - (void) viewWillAppear:(BOOL)animated { |
5983 | [super viewWillAppear:animated]; | |
01d93940 | 5984 | |
5829aea2 GP |
5985 | if ([[self class] shouldHideNavigationBar]) |
5986 | [[self navigationController] setNavigationBarHidden:YES animated:animated]; | |
5987 | } | |
7b0ce2da | 5988 | |
5829aea2 GP |
5989 | - (void) viewWillDisappear:(BOOL)animated { |
5990 | [super viewWillDisappear:animated]; | |
7b0ce2da | 5991 | |
5829aea2 GP |
5992 | if ([[self class] shouldHideNavigationBar]) |
5993 | [[self navigationController] setNavigationBarHidden:NO animated:animated]; | |
5994 | } | |
7b0ce2da | 5995 | |
5829aea2 GP |
5996 | - (id) init { |
5997 | if ((self = [super init]) != nil) { | |
5998 | [self loadURL:[NSURL URLWithString:CydiaURL(@"")]]; | |
5999 | ||
6000 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
6001 | initWithTitle:UCLocalize("ABOUT") | |
6002 | style:UIBarButtonItemStylePlain | |
6003 | target:self | |
6004 | action:@selector(aboutButtonClicked) | |
6005 | ] autorelease]]; | |
6006 | } return self; | |
7b0ce2da JF |
6007 | } |
6008 | ||
6009 | @end | |
6010 | /* }}} */ | |
5829aea2 GP |
6011 | /* Manage Controller {{{ */ |
6012 | @interface ManageController : CYBrowserController { | |
7b0ce2da JF |
6013 | } |
6014 | ||
5829aea2 | 6015 | - (void) queueStatusDidChange; |
7b0ce2da JF |
6016 | @end |
6017 | ||
5829aea2 | 6018 | @implementation ManageController |
7b0ce2da | 6019 | |
5829aea2 GP |
6020 | - (id) init { |
6021 | if ((self = [super init]) != nil) { | |
6022 | [[self navigationItem] setTitle:UCLocalize("MANAGE")]; | |
7b0ce2da | 6023 | |
5829aea2 | 6024 | [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"manage" ofType:@"html"]]]; |
7b0ce2da | 6025 | |
5829aea2 GP |
6026 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] |
6027 | initWithTitle:UCLocalize("SETTINGS") | |
6028 | style:UIBarButtonItemStylePlain | |
6029 | target:self | |
6030 | action:@selector(settingsButtonClicked) | |
6031 | ] autorelease]]; | |
7b0ce2da | 6032 | |
5829aea2 GP |
6033 | [self queueStatusDidChange]; |
6034 | } return self; | |
7b0ce2da JF |
6035 | } |
6036 | ||
5829aea2 GP |
6037 | - (void) settingsButtonClicked { |
6038 | [delegate_ showSettings]; | |
b5e7eebb GP |
6039 | } |
6040 | ||
5829aea2 GP |
6041 | #if !AlwaysReload |
6042 | - (void) queueButtonClicked { | |
6043 | [delegate_ queue]; | |
7b0ce2da JF |
6044 | } |
6045 | ||
5829aea2 GP |
6046 | - (void) applyLoadingTitle { |
6047 | // No "Loading" title. | |
7b0ce2da JF |
6048 | } |
6049 | ||
5829aea2 GP |
6050 | - (void) applyRightButton { |
6051 | // No right button. | |
7b0ce2da | 6052 | } |
5829aea2 | 6053 | #endif |
7b0ce2da | 6054 | |
5829aea2 GP |
6055 | - (void) queueStatusDidChange { |
6056 | #if !AlwaysReload | |
6057 | if (!IsWildcat_ && Queuing_) { | |
6058 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
6059 | initWithTitle:UCLocalize("QUEUE") | |
6060 | style:UIBarButtonItemStyleDone | |
6061 | target:self | |
6062 | action:@selector(queueButtonClicked) | |
6063 | ] autorelease]]; | |
6064 | } else { | |
6065 | [[self navigationItem] setRightBarButtonItem:nil]; | |
46aa9775 | 6066 | } |
5829aea2 | 6067 | #endif |
7b0ce2da JF |
6068 | } |
6069 | ||
5829aea2 GP |
6070 | - (bool) isLoading { |
6071 | return false; | |
7b0ce2da JF |
6072 | } |
6073 | ||
5829aea2 GP |
6074 | @end |
6075 | /* }}} */ | |
7b0ce2da | 6076 | |
5829aea2 GP |
6077 | /* Refresh Bar {{{ */ |
6078 | @interface RefreshBar : UINavigationBar { | |
6079 | UIProgressIndicator *indicator_; | |
6080 | UITextLabel *prompt_; | |
6081 | UIProgressBar *progress_; | |
6082 | UINavigationButton *cancel_; | |
6083 | } | |
7b0ce2da | 6084 | |
5829aea2 | 6085 | @end |
7b0ce2da | 6086 | |
5829aea2 | 6087 | @implementation RefreshBar |
7b0ce2da | 6088 | |
5829aea2 GP |
6089 | - (void) dealloc { |
6090 | [indicator_ release]; | |
6091 | [prompt_ release]; | |
6092 | [progress_ release]; | |
6093 | [cancel_ release]; | |
6094 | [super dealloc]; | |
7b0ce2da JF |
6095 | } |
6096 | ||
5829aea2 GP |
6097 | - (void) positionViews { |
6098 | CGRect frame = [cancel_ frame]; | |
6099 | frame.size = [cancel_ sizeThatFits:frame.size]; | |
6100 | frame.origin.x = [self frame].size.width - frame.size.width - 5; | |
6101 | frame.origin.y = ([self frame].size.height - frame.size.height) / 2; | |
6102 | [cancel_ setFrame:frame]; | |
7b0ce2da | 6103 | |
5829aea2 GP |
6104 | CGSize prgsize = {75, 100}; |
6105 | CGRect prgrect = {{ | |
6106 | [self frame].size.width - prgsize.width - 10, | |
6107 | ([self frame].size.height - prgsize.height) / 2 | |
6108 | } , prgsize}; | |
6109 | [progress_ setFrame:prgrect]; | |
7b0ce2da | 6110 | |
5829aea2 GP |
6111 | CGSize indsize([UIProgressIndicator defaultSizeForStyle:[indicator_ activityIndicatorViewStyle]]); |
6112 | unsigned indoffset = ([self frame].size.height - indsize.height) / 2; | |
6113 | CGRect indrect = {{indoffset, indoffset}, indsize}; | |
6114 | [indicator_ setFrame:indrect]; | |
2fc76a2d | 6115 | |
5829aea2 GP |
6116 | CGSize prmsize = {215, indsize.height + 4}; |
6117 | CGRect prmrect = {{ | |
6118 | indoffset * 2 + indsize.width, | |
6119 | unsigned([self frame].size.height - prmsize.height) / 2 - 1 | |
6120 | }, prmsize}; | |
6121 | [prompt_ setFrame:prmrect]; | |
2fc76a2d JF |
6122 | } |
6123 | ||
5829aea2 GP |
6124 | - (void)setFrame:(CGRect)frame { |
6125 | [super setFrame:frame]; | |
2fc76a2d | 6126 | |
5829aea2 | 6127 | [self positionViews]; |
2fc76a2d JF |
6128 | } |
6129 | ||
5829aea2 GP |
6130 | - (id) initWithFrame:(CGRect)frame delegate:(id)delegate { |
6131 | if ((self = [super initWithFrame:frame])) { | |
6132 | [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | |
7b0ce2da | 6133 | |
5829aea2 | 6134 | [self setBarStyle:UIBarStyleBlack]; |
7b0ce2da | 6135 | |
5829aea2 GP |
6136 | UIBarStyle barstyle([self _barStyle:NO]); |
6137 | bool ugly(barstyle == UIBarStyleDefault); | |
3e9c9e85 | 6138 | |
5829aea2 GP |
6139 | UIProgressIndicatorStyle style = ugly ? |
6140 | UIProgressIndicatorStyleMediumBrown : | |
6141 | UIProgressIndicatorStyleMediumWhite; | |
2fc76a2d | 6142 | |
5829aea2 GP |
6143 | indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectZero]; |
6144 | [indicator_ setStyle:style]; | |
6145 | [indicator_ startAnimation]; | |
6146 | [self addSubview:indicator_]; | |
7b0ce2da | 6147 | |
5829aea2 GP |
6148 | prompt_ = [[UITextLabel alloc] initWithFrame:CGRectZero]; |
6149 | [prompt_ setColor:[UIColor colorWithCGColor:(ugly ? Blueish_ : Off_)]]; | |
6150 | [prompt_ setBackgroundColor:[UIColor clearColor]]; | |
6151 | [prompt_ setFont:[UIFont systemFontOfSize:15]]; | |
6152 | [self addSubview:prompt_]; | |
7b0ce2da | 6153 | |
5829aea2 GP |
6154 | progress_ = [[UIProgressBar alloc] initWithFrame:CGRectZero]; |
6155 | [progress_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin]; | |
6156 | [progress_ setStyle:0]; | |
6157 | [self addSubview:progress_]; | |
7b0ce2da | 6158 | |
5829aea2 GP |
6159 | cancel_ = [[UINavigationButton alloc] initWithTitle:UCLocalize("CANCEL") style:UINavigationButtonStyleHighlighted]; |
6160 | [cancel_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; | |
6161 | [cancel_ addTarget:delegate action:@selector(cancelPressed) forControlEvents:UIControlEventTouchUpInside]; | |
6162 | [cancel_ setBarStyle:barstyle]; | |
3e9c9e85 | 6163 | |
5829aea2 GP |
6164 | [self positionViews]; |
6165 | } return self; | |
6166 | } | |
3e9c9e85 | 6167 | |
5829aea2 GP |
6168 | - (void) cancel { |
6169 | [cancel_ removeFromSuperview]; | |
6170 | } | |
7b0ce2da | 6171 | |
5829aea2 GP |
6172 | - (void) start { |
6173 | [prompt_ setText:UCLocalize("UPDATING_DATABASE")]; | |
6174 | [progress_ setProgress:0]; | |
6175 | [self addSubview:cancel_]; | |
7b0ce2da JF |
6176 | } |
6177 | ||
5829aea2 GP |
6178 | - (void) stop { |
6179 | [cancel_ removeFromSuperview]; | |
7b0ce2da JF |
6180 | } |
6181 | ||
5829aea2 GP |
6182 | - (void) setPrompt:(NSString *)prompt { |
6183 | [prompt_ setText:prompt]; | |
7b0ce2da JF |
6184 | } |
6185 | ||
5829aea2 GP |
6186 | - (void) setProgress:(float)progress { |
6187 | [progress_ setProgress:progress]; | |
7b0ce2da JF |
6188 | } |
6189 | ||
5829aea2 GP |
6190 | @end |
6191 | /* }}} */ | |
b5e7eebb | 6192 | |
5829aea2 | 6193 | @class CYNavigationController; |
7b0ce2da | 6194 | |
5829aea2 GP |
6195 | /* Cydia Tab Bar Controller {{{ */ |
6196 | @interface CYTabBarController : UITabBarController < | |
6197 | ProgressDelegate | |
6198 | > { | |
6199 | _transient Database *database_; | |
6200 | RefreshBar *refreshbar_; | |
7b0ce2da | 6201 | |
5829aea2 GP |
6202 | bool dropped_; |
6203 | bool updating_; | |
6204 | // XXX: ok, "updatedelegate_"?... | |
6205 | _transient NSObject<CydiaDelegate> *updatedelegate_; | |
f79a4512 | 6206 | |
5829aea2 | 6207 | id root_; |
7b0ce2da JF |
6208 | } |
6209 | ||
5829aea2 GP |
6210 | - (void) dropBar:(BOOL)animated; |
6211 | - (void) beginUpdate; | |
6212 | - (void) raiseBar:(BOOL)animated; | |
6213 | - (BOOL) updating; | |
1cedb821 | 6214 | |
5829aea2 | 6215 | @end |
2fc76a2d | 6216 | |
5829aea2 | 6217 | @implementation CYTabBarController |
3e9c9e85 | 6218 | |
5829aea2 GP |
6219 | - (void) reloadData { |
6220 | size_t count([[self viewControllers] count]); | |
6221 | for (size_t i(0); i != count; ++i) { | |
6222 | CYNavigationController *page([[self viewControllers] objectAtIndex:(count - i - 1)]); | |
6223 | [page reloadData]; | |
2fc76a2d | 6224 | } |
c713af59 GP |
6225 | |
6226 | [(CYNavigationController *) [self transientViewController] reloadData]; | |
7b0ce2da JF |
6227 | } |
6228 | ||
b5e7eebb GP |
6229 | - (id) initWithDatabase:(Database *)database { |
6230 | if ((self = [super init]) != nil) { | |
7b0ce2da | 6231 | database_ = database; |
04fe1349 | 6232 | |
5829aea2 GP |
6233 | [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6234 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; | |
7b0ce2da | 6235 | |
5829aea2 | 6236 | refreshbar_ = [[RefreshBar alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, [UINavigationBar defaultSize].height) delegate:self]; |
7b0ce2da JF |
6237 | } return self; |
6238 | } | |
6239 | ||
5829aea2 GP |
6240 | - (void) setUpdate:(NSDate *)date { |
6241 | [self beginUpdate]; | |
6242 | } | |
1cedb821 | 6243 | |
5829aea2 GP |
6244 | - (void) beginUpdate { |
6245 | [refreshbar_ start]; | |
6246 | [self dropBar:YES]; | |
7b0ce2da | 6247 | |
5829aea2 GP |
6248 | [updatedelegate_ retainNetworkActivityIndicator]; |
6249 | updating_ = true; | |
7b0ce2da | 6250 | |
5829aea2 GP |
6251 | [NSThread |
6252 | detachNewThreadSelector:@selector(performUpdate) | |
6253 | toTarget:self | |
6254 | withObject:nil | |
6255 | ]; | |
7b0ce2da JF |
6256 | } |
6257 | ||
5829aea2 GP |
6258 | - (void) performUpdate { _pooled |
6259 | Status status; | |
6260 | status.setDelegate(self); | |
6261 | [database_ updateWithStatus:status]; | |
6262 | ||
6263 | [self | |
6264 | performSelectorOnMainThread:@selector(completeUpdate) | |
6265 | withObject:nil | |
6266 | waitUntilDone:NO | |
6267 | ]; | |
f6e13561 GP |
6268 | } |
6269 | ||
5829aea2 GP |
6270 | - (void) stopUpdateWithSelector:(SEL)selector { |
6271 | updating_ = false; | |
6272 | [updatedelegate_ releaseNetworkActivityIndicator]; | |
b5e7eebb | 6273 | |
5829aea2 GP |
6274 | [self raiseBar:YES]; |
6275 | [refreshbar_ stop]; | |
bc11cf5b | 6276 | |
5829aea2 | 6277 | [updatedelegate_ performSelector:selector withObject:nil afterDelay:0]; |
2bdd73bd GP |
6278 | } |
6279 | ||
5829aea2 GP |
6280 | - (void) completeUpdate { |
6281 | if (!updating_) | |
6282 | return; | |
6283 | [self stopUpdateWithSelector:@selector(reloadData)]; | |
7b0ce2da JF |
6284 | } |
6285 | ||
5829aea2 GP |
6286 | - (void) cancelUpdate { |
6287 | [self stopUpdateWithSelector:@selector(updateData)]; | |
7b0ce2da JF |
6288 | } |
6289 | ||
5829aea2 GP |
6290 | - (void) cancelPressed { |
6291 | [self cancelUpdate]; | |
6292 | } | |
7b0ce2da | 6293 | |
5829aea2 GP |
6294 | - (BOOL) updating { |
6295 | return updating_; | |
7b0ce2da JF |
6296 | } |
6297 | ||
5829aea2 GP |
6298 | - (void) setProgressError:(NSString *)error withTitle:(NSString *)title { |
6299 | [refreshbar_ setPrompt:[NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), UCLocalize("ERROR"), error]]; | |
6300 | } | |
7b0ce2da | 6301 | |
5829aea2 GP |
6302 | - (void) startProgress { |
6303 | } | |
c21004b9 | 6304 | |
5829aea2 GP |
6305 | - (void) setProgressTitle:(NSString *)title { |
6306 | [self | |
6307 | performSelectorOnMainThread:@selector(_setProgressTitle:) | |
6308 | withObject:title | |
6309 | waitUntilDone:YES | |
6310 | ]; | |
6311 | } | |
7b0ce2da | 6312 | |
5829aea2 GP |
6313 | - (bool) isCancelling:(size_t)received { |
6314 | return !updating_; | |
6315 | } | |
7b0ce2da | 6316 | |
5829aea2 GP |
6317 | - (void) setProgressPercent:(float)percent { |
6318 | [self | |
6319 | performSelectorOnMainThread:@selector(_setProgressPercent:) | |
6320 | withObject:[NSNumber numberWithFloat:percent] | |
6321 | waitUntilDone:YES | |
6322 | ]; | |
7b0ce2da JF |
6323 | } |
6324 | ||
5829aea2 GP |
6325 | - (void) addProgressOutput:(NSString *)output { |
6326 | [self | |
6327 | performSelectorOnMainThread:@selector(_addProgressOutput:) | |
6328 | withObject:output | |
6329 | waitUntilDone:YES | |
6330 | ]; | |
6331 | } | |
baf80942 | 6332 | |
5829aea2 GP |
6333 | - (void) _setProgressTitle:(NSString *)title { |
6334 | [refreshbar_ setPrompt:title]; | |
7b0ce2da JF |
6335 | } |
6336 | ||
5829aea2 GP |
6337 | - (void) _setProgressPercent:(NSNumber *)percent { |
6338 | [refreshbar_ setProgress:[percent floatValue]]; | |
7b0ce2da JF |
6339 | } |
6340 | ||
5829aea2 | 6341 | - (void) _addProgressOutput:(NSString *)output { |
f641a0e5 JF |
6342 | } |
6343 | ||
5829aea2 GP |
6344 | - (void) setUpdateDelegate:(id)delegate { |
6345 | updatedelegate_ = delegate; | |
7b0ce2da JF |
6346 | } |
6347 | ||
5829aea2 GP |
6348 | - (CGFloat) statusBarHeight { |
6349 | if (UIInterfaceOrientationIsPortrait([self interfaceOrientation])) { | |
6350 | return [[UIApplication sharedApplication] statusBarFrame].size.height; | |
6351 | } else { | |
6352 | return [[UIApplication sharedApplication] statusBarFrame].size.width; | |
6353 | } | |
7b0ce2da JF |
6354 | } |
6355 | ||
5829aea2 GP |
6356 | - (UIView *) transitionView { |
6357 | if ([self respondsToSelector:@selector(_transitionView)]) | |
6358 | return [self _transitionView]; | |
6359 | else | |
6360 | return MSHookIvar<id>(self, "_viewControllerTransitionView"); | |
f641a0e5 JF |
6361 | } |
6362 | ||
5829aea2 GP |
6363 | - (void) dropBar:(BOOL)animated { |
6364 | if (dropped_) | |
6365 | return; | |
6366 | dropped_ = true; | |
7b0ce2da | 6367 | |
5829aea2 GP |
6368 | UIView *transition([self transitionView]); |
6369 | [[self view] addSubview:refreshbar_]; | |
f6e13561 | 6370 | |
5829aea2 | 6371 | CGRect barframe([refreshbar_ frame]); |
f6e13561 | 6372 | |
a23207d2 | 6373 | if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) |
5829aea2 GP |
6374 | barframe.origin.y = [self statusBarHeight]; |
6375 | else | |
6376 | barframe.origin.y = 0; | |
f6e13561 | 6377 | |
5829aea2 | 6378 | [refreshbar_ setFrame:barframe]; |
f6e13561 | 6379 | |
5829aea2 GP |
6380 | if (animated) |
6381 | [UIView beginAnimations:nil context:NULL]; | |
f6e13561 | 6382 | |
5829aea2 GP |
6383 | CGRect viewframe = [transition frame]; |
6384 | viewframe.origin.y += barframe.size.height; | |
6385 | viewframe.size.height -= barframe.size.height; | |
6386 | [transition setFrame:viewframe]; | |
f6e13561 | 6387 | |
5829aea2 GP |
6388 | if (animated) |
6389 | [UIView commitAnimations]; | |
f6e13561 | 6390 | |
5829aea2 GP |
6391 | // Ensure bar has the proper width for our view, it might have changed |
6392 | barframe.size.width = viewframe.size.width; | |
6393 | [refreshbar_ setFrame:barframe]; | |
f6e13561 | 6394 | |
5829aea2 GP |
6395 | // XXX: fix Apple's layout bug |
6396 | [[root_ selectedViewController] _updateLayoutForStatusBarAndInterfaceOrientation]; | |
f6e13561 GP |
6397 | } |
6398 | ||
5829aea2 GP |
6399 | - (void) raiseBar:(BOOL)animated { |
6400 | if (!dropped_) | |
6401 | return; | |
6402 | dropped_ = false; | |
f6e13561 | 6403 | |
5829aea2 GP |
6404 | UIView *transition([self transitionView]); |
6405 | [refreshbar_ removeFromSuperview]; | |
7b0ce2da | 6406 | |
5829aea2 | 6407 | CGRect barframe([refreshbar_ frame]); |
7b0ce2da | 6408 | |
5829aea2 GP |
6409 | if (animated) |
6410 | [UIView beginAnimations:nil context:NULL]; | |
7b0ce2da | 6411 | |
5829aea2 GP |
6412 | CGRect viewframe = [transition frame]; |
6413 | viewframe.origin.y -= barframe.size.height; | |
6414 | viewframe.size.height += barframe.size.height; | |
6415 | [transition setFrame:viewframe]; | |
9daa0bdc | 6416 | |
5829aea2 GP |
6417 | if (animated) |
6418 | [UIView commitAnimations]; | |
526ea480 | 6419 | |
5829aea2 GP |
6420 | // XXX: fix Apple's layout bug |
6421 | // SRK [[self selectedViewController] _updateLayoutForStatusBarAndInterfaceOrientation]; | |
bfc87a4d JF |
6422 | } |
6423 | ||
5829aea2 GP |
6424 | #if 0 |
6425 | - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { | |
6426 | // XXX: fix Apple's layout bug | |
6427 | // SRK [[self selectedViewController] _updateLayoutForStatusBarAndInterfaceOrientation]; | |
6428 | } | |
6429 | #endif | |
526ea480 | 6430 | |
5829aea2 GP |
6431 | - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { |
6432 | bool dropped(dropped_); | |
7b0ce2da | 6433 | |
5829aea2 GP |
6434 | if (dropped) |
6435 | [self raiseBar:NO]; | |
7b0ce2da | 6436 | |
5829aea2 | 6437 | [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; |
7b0ce2da | 6438 | |
5829aea2 GP |
6439 | if (dropped) |
6440 | [self dropBar:NO]; | |
9daa0bdc | 6441 | |
5829aea2 GP |
6442 | // XXX: fix Apple's layout bug |
6443 | // SRK [[self selectedViewController] _updateLayoutForStatusBarAndInterfaceOrientation]; | |
b5e7eebb GP |
6444 | } |
6445 | ||
5829aea2 GP |
6446 | - (void) statusBarFrameChanged:(NSNotification *)notification { |
6447 | if (dropped_) { | |
6448 | [self raiseBar:NO]; | |
6449 | [self dropBar:NO]; | |
6450 | } | |
b5e7eebb GP |
6451 | } |
6452 | ||
5829aea2 GP |
6453 | - (void) dealloc { |
6454 | [refreshbar_ release]; | |
6455 | [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
6456 | [super dealloc]; | |
7b0ce2da JF |
6457 | } |
6458 | ||
6459 | @end | |
98228790 | 6460 | /* }}} */ |
5829aea2 GP |
6461 | /* Cydia Navigation Controller {{{ */ |
6462 | @interface CYNavigationController : UINavigationController { | |
6463 | _transient Database *database_; | |
6464 | _transient id<UINavigationControllerDelegate> delegate_; | |
7b0ce2da JF |
6465 | } |
6466 | ||
5829aea2 GP |
6467 | - (id) initWithDatabase:(Database *)database; |
6468 | - (void) reloadData; | |
7b0ce2da | 6469 | |
5829aea2 | 6470 | @end |
bc11cf5b | 6471 | |
f6e13561 | 6472 | |
5829aea2 | 6473 | @implementation CYNavigationController |
bc11cf5b | 6474 | |
5829aea2 GP |
6475 | - (void) dealloc { |
6476 | [super dealloc]; | |
7b0ce2da JF |
6477 | } |
6478 | ||
5829aea2 GP |
6479 | - (void) reloadData { |
6480 | size_t count([[self viewControllers] count]); | |
6481 | for (size_t i(0); i != count; ++i) { | |
6482 | CYViewController *page([[self viewControllers] objectAtIndex:(count - i - 1)]); | |
6483 | [page reloadData]; | |
6484 | } | |
dc63e78f JF |
6485 | } |
6486 | ||
5829aea2 GP |
6487 | - (void) setDelegate:(id<UINavigationControllerDelegate>)delegate { |
6488 | delegate_ = delegate; | |
eb97a3a8 GP |
6489 | } |
6490 | ||
5829aea2 GP |
6491 | - (id) initWithDatabase:(Database *)database { |
6492 | if ((self = [super init]) != nil) { | |
6493 | database_ = database; | |
6494 | } return self; | |
dc63e78f JF |
6495 | } |
6496 | ||
5829aea2 GP |
6497 | @end |
6498 | /* }}} */ | |
7b0ce2da | 6499 | |
5829aea2 GP |
6500 | /* Cydia:// Protocol {{{ */ |
6501 | @interface CydiaURLProtocol : NSURLProtocol { | |
9fe5e5f8 JF |
6502 | } |
6503 | ||
7b0ce2da JF |
6504 | @end |
6505 | ||
5829aea2 GP |
6506 | @implementation CydiaURLProtocol |
6507 | ||
6508 | + (BOOL) canInitWithRequest:(NSURLRequest *)request { | |
6509 | NSURL *url([request URL]); | |
6510 | if (url == nil) | |
6511 | return NO; | |
6512 | NSString *scheme([[url scheme] lowercaseString]); | |
6513 | if (scheme == nil || ![scheme isEqualToString:@"cydia"]) | |
6514 | return NO; | |
6515 | return YES; | |
aa5e5990 JF |
6516 | } |
6517 | ||
5829aea2 GP |
6518 | + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { |
6519 | return request; | |
6520 | } | |
aa5e5990 | 6521 | |
5829aea2 GP |
6522 | - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { |
6523 | id<NSURLProtocolClient> client([self client]); | |
6524 | if (icon == nil) | |
6525 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; | |
6526 | else { | |
6527 | NSData *data(UIImagePNGRepresentation(icon)); | |
01d93940 | 6528 | |
5829aea2 GP |
6529 | NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); |
6530 | [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; | |
6531 | [client URLProtocol:self didLoadData:data]; | |
6532 | [client URLProtocolDidFinishLoading:self]; | |
6533 | } | |
3931b718 JF |
6534 | } |
6535 | ||
5829aea2 GP |
6536 | - (void) startLoading { |
6537 | id<NSURLProtocolClient> client([self client]); | |
6538 | NSURLRequest *request([self request]); | |
bc11cf5b | 6539 | |
5829aea2 GP |
6540 | NSURL *url([request URL]); |
6541 | NSString *href([url absoluteString]); | |
bc11cf5b | 6542 | |
5829aea2 GP |
6543 | NSString *path([href substringFromIndex:8]); |
6544 | NSRange slash([path rangeOfString:@"/"]); | |
bc11cf5b | 6545 | |
5829aea2 GP |
6546 | NSString *command; |
6547 | if (slash.location == NSNotFound) { | |
6548 | command = path; | |
6549 | path = nil; | |
6550 | } else { | |
6551 | command = [path substringToIndex:slash.location]; | |
6552 | path = [path substringFromIndex:(slash.location + 1)]; | |
6553 | } | |
39cda3a8 | 6554 | |
5829aea2 | 6555 | Database *database([Database sharedInstance]); |
bc11cf5b | 6556 | |
5829aea2 GP |
6557 | if ([command isEqualToString:@"package-icon"]) { |
6558 | if (path == nil) | |
6559 | goto fail; | |
6560 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6561 | Package *package([database packageWithName:path]); | |
6562 | if (package == nil) | |
6563 | goto fail; | |
6564 | UIImage *icon([package icon]); | |
6565 | [self _returnPNGWithImage:icon forRequest:request]; | |
6566 | } else if ([command isEqualToString:@"source-icon"]) { | |
6567 | if (path == nil) | |
6568 | goto fail; | |
6569 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6570 | NSString *source(Simplify(path)); | |
6571 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sources/%@.png", App_, source]]); | |
6572 | if (icon == nil) | |
6573 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
6574 | [self _returnPNGWithImage:icon forRequest:request]; | |
6575 | } else if ([command isEqualToString:@"uikit-image"]) { | |
6576 | if (path == nil) | |
6577 | goto fail; | |
6578 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6579 | UIImage *icon(_UIImageWithName(path)); | |
6580 | [self _returnPNGWithImage:icon forRequest:request]; | |
6581 | } else if ([command isEqualToString:@"section-icon"]) { | |
6582 | if (path == nil) | |
6583 | goto fail; | |
6584 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6585 | NSString *section(Simplify(path)); | |
6586 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]]); | |
6587 | if (icon == nil) | |
6588 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
6589 | [self _returnPNGWithImage:icon forRequest:request]; | |
6590 | } else fail: { | |
6591 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; | |
6592 | } | |
39cda3a8 GP |
6593 | } |
6594 | ||
5829aea2 GP |
6595 | - (void) stopLoading { |
6596 | } | |
807ae6d7 | 6597 | |
5829aea2 GP |
6598 | @end |
6599 | /* }}} */ | |
807ae6d7 | 6600 | |
5829aea2 GP |
6601 | /* Section Controller {{{ */ |
6602 | @interface SectionController : FilteredPackageController { | |
6603 | } | |
807ae6d7 | 6604 | |
5829aea2 | 6605 | - (id) initWithDatabase:(Database *)database section:(NSString *)section; |
807ae6d7 | 6606 | |
5829aea2 | 6607 | @end |
bc11cf5b | 6608 | |
5829aea2 | 6609 | @implementation SectionController |
bc11cf5b | 6610 | |
5829aea2 GP |
6611 | - (void) dealloc { |
6612 | [super dealloc]; | |
807ae6d7 JF |
6613 | } |
6614 | ||
5829aea2 GP |
6615 | - (id) initWithDatabase:(Database *)database section:(NSString *)name { |
6616 | NSString *title; | |
807ae6d7 | 6617 | |
5829aea2 GP |
6618 | if (name == nil) { |
6619 | title = UCLocalize("ALL_PACKAGES"); | |
6620 | } else if (![name isEqual:@""]) { | |
6621 | title = [[NSBundle mainBundle] localizedStringForKey:Simplify(name) value:nil table:@"Sections"]; | |
6622 | } else { | |
6623 | title = UCLocalize("NO_SECTION"); | |
6624 | } | |
b5e7eebb | 6625 | |
5829aea2 GP |
6626 | if ((self = [super initWithDatabase:database title:title filter:@selector(isVisibleInSection:) with:name]) != nil) { |
6627 | } return self; | |
b5e7eebb GP |
6628 | } |
6629 | ||
5829aea2 GP |
6630 | - (void) reloadData { |
6631 | [packages_ reloadData]; | |
b5e7eebb GP |
6632 | } |
6633 | ||
5829aea2 GP |
6634 | - (void) setDelegate:(id)delegate { |
6635 | [super setDelegate:delegate]; | |
6636 | [packages_ setDelegate:delegate]; | |
b5e7eebb GP |
6637 | } |
6638 | ||
6639 | @end | |
6640 | /* }}} */ | |
5829aea2 GP |
6641 | /* Sections Controller {{{ */ |
6642 | @interface SectionsController : CYViewController < | |
6643 | UITableViewDataSource, | |
6644 | UITableViewDelegate | |
7585ce66 | 6645 | > { |
3931b718 | 6646 | _transient Database *database_; |
5829aea2 GP |
6647 | NSMutableArray *sections_; |
6648 | NSMutableArray *filtered_; | |
6649 | UITableView *list_; | |
6650 | UIView *accessory_; | |
6651 | BOOL editing_; | |
b5e7eebb GP |
6652 | } |
6653 | ||
5829aea2 GP |
6654 | - (id) initWithDatabase:(Database *)database; |
6655 | - (void) reloadData; | |
6656 | - (void) resetView; | |
6657 | ||
6658 | - (void) editButtonClicked; | |
7585ce66 | 6659 | |
bc11cf5b | 6660 | @end |
b5e7eebb | 6661 | |
5829aea2 | 6662 | @implementation SectionsController |
b5e7eebb | 6663 | |
5829aea2 GP |
6664 | - (void) dealloc { |
6665 | [list_ setDataSource:nil]; | |
6666 | [list_ setDelegate:nil]; | |
b5e7eebb | 6667 | |
5829aea2 GP |
6668 | [sections_ release]; |
6669 | [filtered_ release]; | |
6670 | [list_ release]; | |
6671 | [accessory_ release]; | |
6672 | [super dealloc]; | |
6673 | } | |
7585ce66 | 6674 | |
5829aea2 GP |
6675 | - (void) setEditing:(BOOL)editing { |
6676 | if ((editing_ = editing)) | |
6677 | [list_ reloadData]; | |
6678 | else | |
6679 | [delegate_ updateData]; | |
7585ce66 | 6680 | |
5829aea2 GP |
6681 | [[self navigationItem] setTitle:editing_ ? UCLocalize("SECTION_VISIBILITY") : UCLocalize("SECTIONS")]; |
6682 | [[[self navigationItem] rightBarButtonItem] setTitle:[sections_ count] == 0 ? nil : editing_ ? UCLocalize("DONE") : UCLocalize("EDIT")]; | |
6683 | [[[self navigationItem] rightBarButtonItem] setStyle:editing_ ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain]; | |
b5e7eebb GP |
6684 | } |
6685 | ||
5829aea2 GP |
6686 | - (void) viewDidAppear:(BOOL)animated { |
6687 | [super viewDidAppear:animated]; | |
6688 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7585ce66 JF |
6689 | } |
6690 | ||
5829aea2 GP |
6691 | - (void) viewWillDisappear:(BOOL)animated { |
6692 | [super viewWillDisappear:animated]; | |
6693 | if (editing_) [self setEditing:NO]; | |
7585ce66 JF |
6694 | } |
6695 | ||
5829aea2 GP |
6696 | - (Section *) sectionAtIndexPath:(NSIndexPath *)indexPath { |
6697 | Section *section = (editing_ ? [sections_ objectAtIndex:[indexPath row]] : ([indexPath row] == 0 ? nil : [filtered_ objectAtIndex:([indexPath row] - 1)])); | |
6698 | return section; | |
6699 | } | |
7585ce66 | 6700 | |
5829aea2 GP |
6701 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
6702 | return editing_ ? [sections_ count] : [filtered_ count] + 1; | |
7585ce66 JF |
6703 | } |
6704 | ||
5829aea2 GP |
6705 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
6706 | return 45.0f; | |
6707 | }*/ | |
7585ce66 | 6708 | |
5829aea2 GP |
6709 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
6710 | static NSString *reuseIdentifier = @"SectionCell"; | |
7585ce66 | 6711 | |
5829aea2 GP |
6712 | SectionCell *cell = (SectionCell *) [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
6713 | if (cell == nil) | |
6714 | cell = [[[SectionCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
7585ce66 | 6715 | |
5829aea2 | 6716 | [cell setSection:[self sectionAtIndexPath:indexPath] editing:editing_]; |
54043703 | 6717 | |
5829aea2 | 6718 | return cell; |
54043703 JF |
6719 | } |
6720 | ||
5829aea2 GP |
6721 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
6722 | if (editing_) | |
54043703 | 6723 | return; |
7585ce66 | 6724 | |
5829aea2 | 6725 | Section *section = [self sectionAtIndexPath:indexPath]; |
7585ce66 | 6726 | |
5829aea2 GP |
6727 | SectionController *controller = [[[SectionController alloc] |
6728 | initWithDatabase:database_ | |
6729 | section:[section name] | |
6730 | ] autorelease]; | |
6731 | [controller setDelegate:delegate_]; | |
7585ce66 | 6732 | |
5829aea2 | 6733 | [[self navigationController] pushViewController:controller animated:YES]; |
7585ce66 JF |
6734 | } |
6735 | ||
5829aea2 GP |
6736 | - (id) initWithDatabase:(Database *)database { |
6737 | if ((self = [super init]) != nil) { | |
6738 | database_ = database; | |
7585ce66 | 6739 | |
5829aea2 | 6740 | [[self navigationItem] setTitle:UCLocalize("SECTIONS")]; |
7585ce66 | 6741 | |
5829aea2 GP |
6742 | sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; |
6743 | filtered_ = [[NSMutableArray arrayWithCapacity:16] retain]; | |
7585ce66 | 6744 | |
5829aea2 GP |
6745 | list_ = [[UITableView alloc] initWithFrame:[[self view] bounds]]; |
6746 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
6747 | [list_ setRowHeight:45.0f]; | |
6748 | [[self view] addSubview:list_]; | |
7585ce66 | 6749 | |
5829aea2 GP |
6750 | [list_ setDataSource:self]; |
6751 | [list_ setDelegate:self]; | |
7585ce66 | 6752 | |
5829aea2 GP |
6753 | [self reloadData]; |
6754 | } return self; | |
7585ce66 JF |
6755 | } |
6756 | ||
5829aea2 GP |
6757 | - (void) reloadData { |
6758 | NSArray *packages = [database_ packages]; | |
7585ce66 | 6759 | |
5829aea2 GP |
6760 | [sections_ removeAllObjects]; |
6761 | [filtered_ removeAllObjects]; | |
7585ce66 | 6762 | |
5829aea2 | 6763 | NSMutableDictionary *sections([NSMutableDictionary dictionaryWithCapacity:32]); |
7585ce66 | 6764 | |
5829aea2 GP |
6765 | _trace(); |
6766 | for (Package *package in packages) { | |
6767 | NSString *name([package section]); | |
6768 | NSString *key(name == nil ? @"" : name); | |
7585ce66 | 6769 | |
5829aea2 | 6770 | Section *section; |
7585ce66 | 6771 | |
5829aea2 GP |
6772 | _profile(SectionsView$reloadData$Section) |
6773 | section = [sections objectForKey:key]; | |
6774 | if (section == nil) { | |
6775 | _profile(SectionsView$reloadData$Section$Allocate) | |
6776 | section = [[[Section alloc] initWithName:name localize:YES] autorelease]; | |
6777 | [sections setObject:section forKey:key]; | |
6778 | _end | |
6779 | } | |
6780 | _end | |
7585ce66 | 6781 | |
5829aea2 | 6782 | [section addToCount]; |
7585ce66 | 6783 | |
5829aea2 GP |
6784 | _profile(SectionsView$reloadData$Filter) |
6785 | if (![package valid] || ![package visible]) | |
6786 | continue; | |
6787 | _end | |
7585ce66 | 6788 | |
5829aea2 GP |
6789 | [section addToRow]; |
6790 | } | |
6791 | _trace(); | |
7585ce66 | 6792 | |
5829aea2 | 6793 | [sections_ addObjectsFromArray:[sections allValues]]; |
7585ce66 | 6794 | |
5829aea2 | 6795 | [sections_ sortUsingSelector:@selector(compareByLocalized:)]; |
7585ce66 | 6796 | |
5829aea2 GP |
6797 | for (Section *section in sections_) { |
6798 | size_t count([section row]); | |
6799 | if (count == 0) | |
6800 | continue; | |
7585ce66 | 6801 | |
5829aea2 GP |
6802 | section = [[[Section alloc] initWithName:[section name] localized:[section localized]] autorelease]; |
6803 | [section setCount:count]; | |
6804 | [filtered_ addObject:section]; | |
6805 | } | |
7585ce66 | 6806 | |
5829aea2 GP |
6807 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
6808 | initWithTitle:([sections_ count] == 0 ? nil : UCLocalize("EDIT")) | |
6809 | style:UIBarButtonItemStylePlain | |
6810 | target:self | |
6811 | action:@selector(editButtonClicked) | |
6812 | ] autorelease] animated:([[self navigationItem] rightBarButtonItem] != nil)]; | |
7585ce66 | 6813 | |
5829aea2 GP |
6814 | [list_ reloadData]; |
6815 | _trace(); | |
6816 | } | |
7585ce66 | 6817 | |
5829aea2 GP |
6818 | - (void) resetView { |
6819 | if (editing_) | |
6820 | [self editButtonClicked]; | |
7585ce66 JF |
6821 | } |
6822 | ||
5829aea2 GP |
6823 | - (void)editButtonClicked { |
6824 | [self setEditing:!editing_]; | |
6825 | } | |
7585ce66 | 6826 | |
5829aea2 GP |
6827 | - (UIView *) accessoryView { |
6828 | return accessory_; | |
6829 | } | |
7585ce66 | 6830 | |
5829aea2 GP |
6831 | @end |
6832 | /* }}} */ | |
7585ce66 | 6833 | |
5829aea2 GP |
6834 | /* Changes Controller {{{ */ |
6835 | @interface ChangesController : CYViewController < | |
6836 | UITableViewDataSource, | |
6837 | UITableViewDelegate | |
6838 | > { | |
6839 | _transient Database *database_; | |
6840 | unsigned era_; | |
6841 | CFMutableArrayRef packages_; | |
6842 | NSMutableArray *sections_; | |
6843 | UITableView *list_; | |
6844 | unsigned upgrades_; | |
6845 | BOOL hasSentFirstLoad_; | |
6846 | } | |
7585ce66 | 6847 | |
ea3bb538 | 6848 | - (id) initWithDatabase:(Database *)database; |
7585ce66 | 6849 | |
5829aea2 | 6850 | @end |
7585ce66 | 6851 | |
5829aea2 | 6852 | @implementation ChangesController |
7585ce66 | 6853 | |
5829aea2 GP |
6854 | - (void) dealloc { |
6855 | [list_ setDelegate:nil]; | |
6856 | [list_ setDataSource:nil]; | |
7585ce66 | 6857 | |
5829aea2 | 6858 | CFRelease(packages_); |
7585ce66 | 6859 | |
5829aea2 GP |
6860 | [sections_ release]; |
6861 | [list_ release]; | |
6862 | [super dealloc]; | |
7585ce66 JF |
6863 | } |
6864 | ||
5829aea2 GP |
6865 | - (void) viewDidAppear:(BOOL)animated { |
6866 | [super viewDidAppear:animated]; | |
6867 | if (!hasSentFirstLoad_) { | |
6868 | hasSentFirstLoad_ = YES; | |
6869 | [self performSelector:@selector(reloadData) withObject:nil afterDelay:0.0]; | |
6870 | } else { | |
6871 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7585ce66 JF |
6872 | } |
6873 | } | |
6874 | ||
5829aea2 GP |
6875 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
6876 | NSInteger count([sections_ count]); | |
6877 | return count == 0 ? 1 : count; | |
7585ce66 JF |
6878 | } |
6879 | ||
5829aea2 GP |
6880 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
6881 | if ([sections_ count] == 0) | |
6882 | return nil; | |
6883 | return [[sections_ objectAtIndex:section] name]; | |
b5e7eebb GP |
6884 | } |
6885 | ||
5829aea2 GP |
6886 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
6887 | if ([sections_ count] == 0) | |
6888 | return 0; | |
6889 | return [[sections_ objectAtIndex:section] count]; | |
6890 | } | |
b5e7eebb | 6891 | |
5829aea2 GP |
6892 | - (Package *) packageAtIndex:(NSUInteger)index { |
6893 | return (Package *) CFArrayGetValueAtIndex(packages_, index); | |
6894 | } | |
b5e7eebb | 6895 | |
5829aea2 GP |
6896 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
6897 | @synchronized (database_) { | |
6898 | if ([database_ era] != era_) | |
6899 | return nil; | |
b5e7eebb | 6900 | |
5829aea2 GP |
6901 | NSUInteger sectionIndex([path section]); |
6902 | if (sectionIndex >= [sections_ count]) | |
6903 | return nil; | |
6904 | Section *section([sections_ objectAtIndex:sectionIndex]); | |
6905 | NSInteger row([path row]); | |
6906 | return [[[self packageAtIndex:([section row] + row)] retain] autorelease]; | |
6907 | } } | |
b5e7eebb | 6908 | |
5829aea2 GP |
6909 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
6910 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); | |
6911 | if (cell == nil) | |
6912 | cell = [[[PackageCell alloc] init] autorelease]; | |
6913 | [cell setPackage:[self packageAtIndexPath:path]]; | |
6914 | return cell; | |
9cb0bff2 GP |
6915 | } |
6916 | ||
5829aea2 GP |
6917 | - (NSIndexPath *) tableView:(UITableView *)table willSelectRowAtIndexPath:(NSIndexPath *)path { |
6918 | Package *package([self packageAtIndexPath:path]); | |
6919 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_] autorelease]); | |
6920 | [view setDelegate:delegate_]; | |
6921 | [view setPackage:package]; | |
6922 | [[self navigationController] pushViewController:view animated:YES]; | |
6923 | return path; | |
b5e7eebb GP |
6924 | } |
6925 | ||
5829aea2 GP |
6926 | - (void) refreshButtonClicked { |
6927 | [delegate_ beginUpdate]; | |
6928 | [[self navigationItem] setLeftBarButtonItem:nil animated:YES]; | |
b5e7eebb GP |
6929 | } |
6930 | ||
5829aea2 GP |
6931 | - (void) upgradeButtonClicked { |
6932 | [delegate_ distUpgrade]; | |
b5e7eebb GP |
6933 | } |
6934 | ||
5829aea2 GP |
6935 | - (NSString *) title { return UCLocalize("CHANGES"); } |
6936 | ||
ea3bb538 | 6937 | - (id) initWithDatabase:(Database *)database { |
b5e7eebb GP |
6938 | if ((self = [super init]) != nil) { |
6939 | database_ = database; | |
5829aea2 | 6940 | [[self navigationItem] setTitle:UCLocalize("CHANGES")]; |
b5e7eebb | 6941 | |
5829aea2 | 6942 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL); |
5829aea2 | 6943 | sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; |
807ae6d7 | 6944 | |
5829aea2 GP |
6945 | list_ = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain]; |
6946 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
6947 | [list_ setRowHeight:73]; | |
6948 | [[self view] addSubview:list_]; | |
807ae6d7 | 6949 | |
5829aea2 GP |
6950 | [list_ setDataSource:self]; |
6951 | [list_ setDelegate:self]; | |
5829aea2 | 6952 | } return self; |
807ae6d7 JF |
6953 | } |
6954 | ||
5829aea2 GP |
6955 | - (void) _reloadPackages:(NSArray *)packages { |
6956 | CFRelease(packages_); | |
6957 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, [packages count], NULL); | |
9fe5e5f8 | 6958 | |
5829aea2 GP |
6959 | _trace(); |
6960 | _profile(ChangesController$_reloadPackages$Filter) | |
6961 | for (Package *package in packages) | |
6962 | if ([package upgradableAndEssential:YES] || [package visible]) | |
6963 | CFArrayAppendValue(packages_, package); | |
6964 | _end | |
6965 | _trace(); | |
6966 | _profile(ChangesController$_reloadPackages$radixSort) | |
6967 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<SKRadixFunction>(&PackageChangesRadix) withContext:NULL]; | |
6968 | _end | |
6969 | _trace(); | |
9fe5e5f8 JF |
6970 | } |
6971 | ||
5829aea2 GP |
6972 | - (void) reloadData { |
6973 | @synchronized (database_) { | |
6974 | era_ = [database_ era]; | |
6975 | NSArray *packages = [database_ packages]; | |
807ae6d7 | 6976 | |
5829aea2 | 6977 | [sections_ removeAllObjects]; |
807ae6d7 | 6978 | |
5829aea2 GP |
6979 | #if 1 |
6980 | UIProgressHUD *hud([delegate_ addProgressHUD]); | |
6981 | [hud setText:UCLocalize("LOADING")]; | |
6982 | //NSLog(@"HUD:%@::%@", delegate_, hud); | |
6983 | [self yieldToSelector:@selector(_reloadPackages:) withObject:packages]; | |
6984 | [delegate_ removeProgressHUD:hud]; | |
6985 | #else | |
6986 | [self _reloadPackages:packages]; | |
6987 | #endif | |
807ae6d7 | 6988 | |
5829aea2 GP |
6989 | Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; |
6990 | Section *ignored = nil; | |
6991 | Section *section = nil; | |
6992 | time_t last = 0; | |
807ae6d7 | 6993 | |
5829aea2 GP |
6994 | upgrades_ = 0; |
6995 | bool unseens = false; | |
807ae6d7 | 6996 | |
5829aea2 | 6997 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); |
807ae6d7 | 6998 | |
5829aea2 GP |
6999 | for (size_t offset = 0, count = CFArrayGetCount(packages_); offset != count; ++offset) { |
7000 | Package *package = [self packageAtIndex:offset]; | |
807ae6d7 | 7001 | |
5829aea2 | 7002 | BOOL uae = [package upgradableAndEssential:YES]; |
807ae6d7 | 7003 | |
5829aea2 GP |
7004 | if (!uae) { |
7005 | unseens = true; | |
7006 | time_t seen([package seen]); | |
d90a4cd6 | 7007 | |
5829aea2 GP |
7008 | if (section == nil || last != seen) { |
7009 | last = seen; | |
7010 | ||
7011 | NSString *name; | |
7012 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]); | |
7013 | [name autorelease]; | |
7014 | ||
7015 | _profile(ChangesController$reloadData$Allocate) | |
7016 | name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; | |
7017 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
7018 | [sections_ addObject:section]; | |
7019 | _end | |
7020 | } | |
7021 | ||
7022 | [section addToCount]; | |
7023 | } else if ([package ignored]) { | |
7024 | if (ignored == nil) { | |
7025 | ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease]; | |
7026 | } | |
7027 | [ignored addToCount]; | |
7028 | } else { | |
7029 | ++upgrades_; | |
7030 | [upgradable addToCount]; | |
7031 | } | |
7032 | } | |
7033 | _trace(); | |
7034 | ||
7035 | CFRelease(formatter); | |
7036 | ||
7037 | if (unseens) { | |
7038 | Section *last = [sections_ lastObject]; | |
7039 | size_t count = [last count]; | |
7040 | CFArrayReplaceValues(packages_, CFRangeMake(CFArrayGetCount(packages_) - count, count), NULL, 0); | |
7041 | [sections_ removeLastObject]; | |
7042 | } | |
7043 | ||
7044 | if ([ignored count] != 0) | |
7045 | [sections_ insertObject:ignored atIndex:0]; | |
7046 | if (upgrades_ != 0) | |
7047 | [sections_ insertObject:upgradable atIndex:0]; | |
7048 | ||
7049 | [list_ reloadData]; | |
7050 | ||
7051 | if (upgrades_ > 0) | |
7052 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
7053 | initWithTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("UPGRADE"), [NSString stringWithFormat:@"%u", upgrades_]] | |
7054 | style:UIBarButtonItemStylePlain | |
7055 | target:self | |
7056 | action:@selector(upgradeButtonClicked) | |
7057 | ] autorelease]]; | |
7058 | ||
7059 | if (![delegate_ updating]) | |
7060 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7061 | initWithTitle:UCLocalize("REFRESH") | |
7062 | style:UIBarButtonItemStylePlain | |
7063 | target:self | |
7064 | action:@selector(refreshButtonClicked) | |
7065 | ] autorelease]]; | |
7066 | ||
7067 | PrintTimes(); | |
7068 | } } | |
7069 | ||
7070 | @end | |
7071 | /* }}} */ | |
7072 | /* Search Controller {{{ */ | |
7073 | @interface SearchController : FilteredPackageController < | |
7074 | UISearchBarDelegate | |
7075 | > { | |
7076 | UISearchBar *search_; | |
7077 | } | |
7078 | ||
7079 | - (id) initWithDatabase:(Database *)database; | |
7080 | - (void) setSearchTerm:(NSString *)term; | |
7081 | - (void) reloadData; | |
d90a4cd6 GP |
7082 | |
7083 | @end | |
7084 | ||
5829aea2 | 7085 | @implementation SearchController |
d90a4cd6 GP |
7086 | |
7087 | - (void) dealloc { | |
5829aea2 | 7088 | [search_ release]; |
d90a4cd6 GP |
7089 | [super dealloc]; |
7090 | } | |
7091 | ||
5829aea2 GP |
7092 | - (void) setSearchTerm:(NSString *)searchTerm { |
7093 | [search_ setText:searchTerm]; | |
7094 | } | |
d90a4cd6 | 7095 | |
5829aea2 GP |
7096 | - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { |
7097 | [packages_ setObject:[search_ text] forFilter:@selector(isUnfilteredAndSearchedForBy:)]; | |
7098 | [search_ resignFirstResponder]; | |
7099 | [self reloadData]; | |
7100 | } | |
7101 | ||
7102 | - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { | |
7103 | [packages_ setObject:text forFilter:@selector(isUnfilteredAndSelectedForBy:)]; | |
7104 | [self reloadData]; | |
7105 | } | |
7106 | ||
7107 | - (id) initWithDatabase:(Database *)database { | |
7108 | return [super initWithDatabase:database title:UCLocalize("SEARCH") filter:@selector(isUnfilteredAndSearchedForBy:) with:nil]; | |
7109 | } | |
7110 | ||
7111 | - (void)viewDidAppear:(BOOL)animated { | |
7112 | [super viewDidAppear:animated]; | |
7113 | if (!search_) { | |
7114 | search_ = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; | |
7115 | [search_ layoutSubviews]; | |
7116 | [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; | |
7117 | ||
7118 | UITextField *textField; | |
7119 | if ([search_ respondsToSelector:@selector(searchField)]) | |
7120 | textField = [search_ searchField]; | |
7121 | else | |
7122 | textField = MSHookIvar<UITextField *>(search_, "_searchField"); | |
7123 | ||
7124 | [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
7125 | [search_ setDelegate:self]; | |
7126 | [textField setEnablesReturnKeyAutomatically:NO]; | |
7127 | [[self navigationItem] setTitleView:textField]; | |
d90a4cd6 | 7128 | } |
5829aea2 | 7129 | } |
d90a4cd6 | 7130 | |
5829aea2 | 7131 | - (void) _reloadData { |
d90a4cd6 GP |
7132 | } |
7133 | ||
7134 | - (void) reloadData { | |
5829aea2 GP |
7135 | _profile(SearchController$reloadData) |
7136 | [packages_ reloadData]; | |
7137 | _end | |
7138 | PrintTimes(); | |
7139 | [packages_ resetCursor]; | |
d90a4cd6 GP |
7140 | } |
7141 | ||
5829aea2 GP |
7142 | - (void) didSelectPackage:(Package *)package { |
7143 | [search_ resignFirstResponder]; | |
7144 | [super didSelectPackage:package]; | |
d90a4cd6 GP |
7145 | } |
7146 | ||
7147 | @end | |
7148 | /* }}} */ | |
5829aea2 GP |
7149 | /* Package Settings Controller {{{ */ |
7150 | @interface PackageSettingsController : CYViewController < | |
c21004b9 JF |
7151 | UITableViewDataSource, |
7152 | UITableViewDelegate | |
7153 | > { | |
807ae6d7 | 7154 | _transient Database *database_; |
5829aea2 GP |
7155 | NSString *name_; |
7156 | Package *package_; | |
7157 | UITableView *table_; | |
7158 | UISwitch *subscribedSwitch_; | |
7159 | UISwitch *ignoredSwitch_; | |
7160 | UITableViewCell *subscribedCell_; | |
7161 | UITableViewCell *ignoredCell_; | |
807ae6d7 JF |
7162 | } |
7163 | ||
5829aea2 | 7164 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
c21004b9 | 7165 | |
807ae6d7 JF |
7166 | @end |
7167 | ||
5829aea2 | 7168 | @implementation PackageSettingsController |
807ae6d7 JF |
7169 | |
7170 | - (void) dealloc { | |
5829aea2 GP |
7171 | [name_ release]; |
7172 | if (package_ != nil) | |
7173 | [package_ release]; | |
7174 | [table_ release]; | |
7175 | [subscribedSwitch_ release]; | |
7176 | [ignoredSwitch_ release]; | |
7177 | [subscribedCell_ release]; | |
7178 | [ignoredCell_ release]; | |
807ae6d7 | 7179 | |
807ae6d7 JF |
7180 | [super dealloc]; |
7181 | } | |
7182 | ||
5829aea2 GP |
7183 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7184 | if (package_ == nil) | |
7185 | return 0; | |
e954c30a | 7186 | |
5829aea2 | 7187 | return 1; |
e954c30a GP |
7188 | } |
7189 | ||
5829aea2 GP |
7190 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7191 | if (package_ == nil) | |
7192 | return 0; | |
7193 | ||
7194 | return 2; | |
b5e7eebb GP |
7195 | } |
7196 | ||
5829aea2 GP |
7197 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
7198 | return UCLocalize("CHANGE_PACKAGE_SETTINGS"); | |
e954c30a GP |
7199 | } |
7200 | ||
5829aea2 GP |
7201 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
7202 | return UCLocalize("SHOW_ALL_CHANGES_EX"); | |
807ae6d7 JF |
7203 | } |
7204 | ||
5829aea2 GP |
7205 | - (void) onSubscribed:(id)control { |
7206 | bool value([control isOn]); | |
7207 | if (package_ == nil) | |
7208 | return; | |
7209 | if ([package_ setSubscribed:value]) | |
7210 | [delegate_ updateData]; | |
807ae6d7 JF |
7211 | } |
7212 | ||
5829aea2 GP |
7213 | - (void) onIgnored:(id)control { |
7214 | // TODO: set Held state - possibly call out to dpkg, etc. | |
7215 | } | |
807ae6d7 | 7216 | |
46aa9775 | 7217 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
7218 | if (package_ == nil) |
7219 | return nil; | |
b5e7eebb | 7220 | |
5829aea2 GP |
7221 | switch ([indexPath row]) { |
7222 | case 0: return subscribedCell_; | |
7223 | case 1: return ignoredCell_; | |
36fbb2aa | 7224 | |
5829aea2 GP |
7225 | _nodefault |
7226 | } | |
807ae6d7 | 7227 | |
5829aea2 | 7228 | return nil; |
807ae6d7 JF |
7229 | } |
7230 | ||
5829aea2 | 7231 | - (NSString *) title { return UCLocalize("SETTINGS"); } |
807ae6d7 | 7232 | |
5829aea2 GP |
7233 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { |
7234 | if ((self = [super init])) { | |
807ae6d7 | 7235 | database_ = database; |
5829aea2 | 7236 | name_ = [package retain]; |
807ae6d7 | 7237 | |
5829aea2 | 7238 | [[self navigationItem] setTitle:UCLocalize("SETTINGS")]; |
807ae6d7 | 7239 | |
5829aea2 GP |
7240 | table_ = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped]; |
7241 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
7242 | [[self view] addSubview:table_]; | |
807ae6d7 | 7243 | |
5829aea2 GP |
7244 | subscribedSwitch_ = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]; |
7245 | [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; | |
7246 | [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:UIControlEventValueChanged]; | |
7247 | ||
7248 | ignoredSwitch_ = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]; | |
7249 | [ignoredSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; | |
7250 | [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:UIControlEventValueChanged]; | |
7251 | // Disable this switch, since it only reflects (not modifies) the ignored state. | |
7252 | [ignoredSwitch_ setUserInteractionEnabled:NO]; | |
7253 | ||
7254 | subscribedCell_ = [[UITableViewCell alloc] init]; | |
7255 | [subscribedCell_ setText:UCLocalize("SHOW_ALL_CHANGES")]; | |
7256 | [subscribedCell_ setAccessoryView:subscribedSwitch_]; | |
7257 | [subscribedCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
7258 | ||
7259 | ignoredCell_ = [[UITableViewCell alloc] init]; | |
7260 | [ignoredCell_ setText:UCLocalize("IGNORE_UPGRADES")]; | |
7261 | [ignoredCell_ setAccessoryView:ignoredSwitch_]; | |
7262 | [ignoredCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
7263 | // FIXME: Ignored state is not saved. | |
7264 | [ignoredCell_ setUserInteractionEnabled:NO]; | |
807ae6d7 | 7265 | |
5829aea2 GP |
7266 | [table_ setDataSource:self]; |
7267 | [table_ setDelegate:self]; | |
807ae6d7 | 7268 | [self reloadData]; |
807ae6d7 JF |
7269 | } return self; |
7270 | } | |
7271 | ||
7272 | - (void) reloadData { | |
5829aea2 GP |
7273 | if (package_ != nil) |
7274 | [package_ autorelease]; | |
7275 | package_ = [database_ packageWithName:name_]; | |
7276 | if (package_ != nil) { | |
7277 | [package_ retain]; | |
7278 | [subscribedSwitch_ setOn:([package_ subscribed] ? 1 : 0) animated:NO]; | |
7279 | [ignoredSwitch_ setOn:([package_ ignored] ? 1 : 0) animated:NO]; | |
b4d89997 | 7280 | } |
dc5812ec | 7281 | |
5829aea2 GP |
7282 | [table_ reloadData]; |
7283 | } | |
6d9712c4 | 7284 | |
5829aea2 GP |
7285 | @end |
7286 | /* }}} */ | |
7287 | /* Signature Controller {{{ */ | |
7288 | @interface SignatureController : CYBrowserController { | |
7289 | _transient Database *database_; | |
7290 | NSString *package_; | |
7291 | } | |
dc5812ec | 7292 | |
5829aea2 | 7293 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
dc5812ec | 7294 | |
5829aea2 | 7295 | @end |
2367a917 | 7296 | |
5829aea2 | 7297 | @implementation SignatureController |
b5e7eebb | 7298 | |
5829aea2 GP |
7299 | - (void) dealloc { |
7300 | [package_ release]; | |
7301 | [super dealloc]; | |
36bb2ca2 | 7302 | } |
2367a917 | 7303 | |
5829aea2 GP |
7304 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
7305 | // XXX: dude! | |
7306 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
36bb2ca2 | 7307 | } |
2367a917 | 7308 | |
5829aea2 GP |
7309 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { |
7310 | if ((self = [super init]) != nil) { | |
7311 | database_ = database; | |
7312 | package_ = [package retain]; | |
7313 | [self reloadData]; | |
7314 | } return self; | |
baf80942 JF |
7315 | } |
7316 | ||
5829aea2 GP |
7317 | - (void) reloadData { |
7318 | [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"signature" ofType:@"html"]]]; | |
a54b1c10 JF |
7319 | } |
7320 | ||
dc5812ec | 7321 | @end |
2367a917 | 7322 | /* }}} */ |
d90a4cd6 | 7323 | |
5829aea2 GP |
7324 | /* Installed Controller {{{ */ |
7325 | @interface InstalledController : FilteredPackageController { | |
7326 | BOOL expert_; | |
dc5812ec JF |
7327 | } |
7328 | ||
5829aea2 GP |
7329 | - (id) initWithDatabase:(Database *)database; |
7330 | ||
7331 | - (void) updateRoleButton; | |
7332 | - (void) queueStatusDidChange; | |
dc5812ec | 7333 | |
dc5812ec JF |
7334 | @end |
7335 | ||
5829aea2 | 7336 | @implementation InstalledController |
b4d89997 JF |
7337 | |
7338 | - (void) dealloc { | |
b4d89997 JF |
7339 | [super dealloc]; |
7340 | } | |
dc5812ec | 7341 | |
5829aea2 | 7342 | - (NSString *) title { return UCLocalize("INSTALLED"); } |
b5e7eebb | 7343 | |
5829aea2 GP |
7344 | - (id) initWithDatabase:(Database *)database { |
7345 | if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED") filter:@selector(isInstalledAndUnfiltered:) with:[NSNumber numberWithBool:YES]]) != nil) { | |
7346 | [self updateRoleButton]; | |
7347 | [self queueStatusDidChange]; | |
7348 | } return self; | |
dc5812ec JF |
7349 | } |
7350 | ||
5829aea2 GP |
7351 | #if !AlwaysReload |
7352 | - (void) queueButtonClicked { | |
7353 | [delegate_ queue]; | |
dc5812ec | 7354 | } |
5829aea2 | 7355 | #endif |
dc5812ec | 7356 | |
5829aea2 GP |
7357 | - (void) queueStatusDidChange { |
7358 | #if !AlwaysReload | |
7359 | if (IsWildcat_) { | |
7360 | if (Queuing_) { | |
7361 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7362 | initWithTitle:UCLocalize("QUEUE") | |
7363 | style:UIBarButtonItemStyleDone | |
7364 | target:self | |
7365 | action:@selector(queueButtonClicked) | |
7366 | ] autorelease]]; | |
7367 | } else { | |
7368 | [[self navigationItem] setLeftBarButtonItem:nil]; | |
7369 | } | |
7370 | } | |
7371 | #endif | |
dc5812ec JF |
7372 | } |
7373 | ||
5829aea2 GP |
7374 | - (void) reloadData { |
7375 | [packages_ reloadData]; | |
6da1297d JF |
7376 | } |
7377 | ||
5829aea2 GP |
7378 | - (void) updateRoleButton { |
7379 | if (Role_ != nil && ![Role_ isEqualToString:@"Developer"]) | |
7380 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
7381 | initWithTitle:(expert_ ? UCLocalize("EXPERT") : UCLocalize("SIMPLE")) | |
7382 | style:(expert_ ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
7383 | target:self | |
7384 | action:@selector(roleButtonClicked) | |
7385 | ] autorelease]]; | |
dc5812ec JF |
7386 | } |
7387 | ||
5829aea2 GP |
7388 | - (void) roleButtonClicked { |
7389 | [packages_ setObject:[NSNumber numberWithBool:expert_]]; | |
7390 | [packages_ reloadData]; | |
7391 | expert_ = !expert_; | |
dc5812ec | 7392 | |
5829aea2 | 7393 | [self updateRoleButton]; |
9a7b04c5 JF |
7394 | } |
7395 | ||
5829aea2 GP |
7396 | - (void) setDelegate:(id)delegate { |
7397 | [super setDelegate:delegate]; | |
7398 | [packages_ setDelegate:delegate]; | |
dc5812ec JF |
7399 | } |
7400 | ||
5829aea2 GP |
7401 | @end |
7402 | /* }}} */ | |
b5e7eebb | 7403 | |
5829aea2 GP |
7404 | /* Source Cell {{{ */ |
7405 | @interface SourceCell : CYTableViewCell < | |
7406 | ContentDelegate | |
7407 | > { | |
7408 | UIImage *icon_; | |
7409 | NSString *origin_; | |
7410 | NSString *label_; | |
7411 | } | |
dc5812ec | 7412 | |
5829aea2 | 7413 | - (void) setSource:(Source *)source; |
6da1297d | 7414 | |
5829aea2 | 7415 | @end |
dc5812ec | 7416 | |
5829aea2 | 7417 | @implementation SourceCell |
36bb2ca2 | 7418 | |
5829aea2 GP |
7419 | - (void) clearSource { |
7420 | [icon_ release]; | |
7421 | [origin_ release]; | |
7422 | [label_ release]; | |
dc5812ec | 7423 | |
5829aea2 GP |
7424 | icon_ = nil; |
7425 | origin_ = nil; | |
7426 | label_ = nil; | |
dc5812ec JF |
7427 | } |
7428 | ||
5829aea2 GP |
7429 | - (void) setSource:(Source *)source { |
7430 | [self clearSource]; | |
2367a917 | 7431 | |
5829aea2 GP |
7432 | if (icon_ == nil) |
7433 | icon_ = [UIImage applicationImageNamed:[NSString stringWithFormat:@"Sources/%@.png", [source host]]]; | |
7434 | if (icon_ == nil) | |
7435 | icon_ = [UIImage applicationImageNamed:@"unknown.png"]; | |
7436 | icon_ = [icon_ retain]; | |
7437 | ||
7438 | origin_ = [[source name] retain]; | |
7439 | label_ = [[source uri] retain]; | |
7440 | ||
7441 | [content_ setNeedsDisplay]; | |
77801ff1 JF |
7442 | } |
7443 | ||
5829aea2 GP |
7444 | - (void) dealloc { |
7445 | [self clearSource]; | |
7446 | [super dealloc]; | |
7447 | } | |
77801ff1 | 7448 | |
5829aea2 GP |
7449 | - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
7450 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
7451 | UIView *content([self contentView]); | |
7452 | CGRect bounds([content bounds]); | |
77801ff1 | 7453 | |
5829aea2 GP |
7454 | content_ = [[ContentView alloc] initWithFrame:bounds]; |
7455 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
7456 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
7457 | [content addSubview:content_]; | |
dc5812ec | 7458 | |
5829aea2 GP |
7459 | [content_ setDelegate:self]; |
7460 | [content_ setOpaque:YES]; | |
7461 | } return self; | |
7462 | } | |
b4d89997 | 7463 | |
5829aea2 GP |
7464 | - (void) drawContentRect:(CGRect)rect { |
7465 | bool highlighted(highlighted_); | |
7466 | float width(rect.size.width); | |
36bb2ca2 | 7467 | |
5829aea2 GP |
7468 | if (icon_ != nil) |
7469 | [icon_ drawInRect:CGRectMake(10, 10, 30, 30)]; | |
36bb2ca2 | 7470 | |
5829aea2 GP |
7471 | if (highlighted) |
7472 | UISetColor(White_); | |
dc5812ec | 7473 | |
5829aea2 GP |
7474 | if (!highlighted) |
7475 | UISetColor(Black_); | |
7476 | [origin_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - 80) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; | |
f79a4512 | 7477 | |
5829aea2 GP |
7478 | if (!highlighted) |
7479 | UISetColor(Blue_); | |
7480 | [label_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; | |
7481 | } | |
dc5812ec | 7482 | |
5829aea2 GP |
7483 | @end |
7484 | /* }}} */ | |
7485 | /* Source Table {{{ */ | |
7486 | @interface SourcesController : CYViewController < | |
7487 | UITableViewDataSource, | |
7488 | UITableViewDelegate | |
7489 | > { | |
7490 | _transient Database *database_; | |
7491 | UITableView *list_; | |
7492 | NSMutableArray *sources_; | |
7493 | int offset_; | |
723a0072 | 7494 | |
5829aea2 GP |
7495 | NSString *href_; |
7496 | UIProgressHUD *hud_; | |
7497 | NSError *error_; | |
dc63e78f | 7498 | |
5829aea2 GP |
7499 | //NSURLConnection *installer_; |
7500 | NSURLConnection *trivial_; | |
7501 | NSURLConnection *trivial_bz2_; | |
7502 | NSURLConnection *trivial_gz_; | |
7503 | //NSURLConnection *automatic_; | |
b4d89997 | 7504 | |
5829aea2 GP |
7505 | BOOL cydia_; |
7506 | } | |
dc5812ec | 7507 | |
5829aea2 | 7508 | - (id) initWithDatabase:(Database *)database; |
b4d89997 | 7509 | |
5829aea2 GP |
7510 | - (void) updateButtonsForEditingStatus:(BOOL)editing animated:(BOOL)animated; |
7511 | ||
7512 | @end | |
7513 | ||
7514 | @implementation SourcesController | |
7515 | ||
7516 | - (void) _releaseConnection:(NSURLConnection *)connection { | |
7517 | if (connection != nil) { | |
7518 | [connection cancel]; | |
7519 | //[connection setDelegate:nil]; | |
7520 | [connection release]; | |
36bb2ca2 | 7521 | } |
5829aea2 | 7522 | } |
dc5812ec | 7523 | |
5829aea2 GP |
7524 | - (void) dealloc { |
7525 | if (href_ != nil) | |
7526 | [href_ release]; | |
7527 | if (hud_ != nil) | |
7528 | [hud_ release]; | |
7529 | if (error_ != nil) | |
7530 | [error_ release]; | |
c25a610d | 7531 | |
5829aea2 GP |
7532 | //[self _releaseConnection:installer_]; |
7533 | [self _releaseConnection:trivial_]; | |
7534 | [self _releaseConnection:trivial_gz_]; | |
7535 | [self _releaseConnection:trivial_bz2_]; | |
7536 | //[self _releaseConnection:automatic_]; | |
3178d79b | 7537 | |
5829aea2 GP |
7538 | [sources_ release]; |
7539 | [list_ release]; | |
7540 | [super dealloc]; | |
7541 | } | |
b5e7eebb | 7542 | |
5829aea2 GP |
7543 | - (void) viewDidAppear:(BOOL)animated { |
7544 | [super viewDidAppear:animated]; | |
7545 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7546 | } | |
9bb3b295 | 7547 | |
5829aea2 GP |
7548 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7549 | return offset_ == 0 ? 1 : 2; | |
7550 | } | |
8fe19fc1 | 7551 | |
5829aea2 GP |
7552 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
7553 | switch (section + (offset_ == 0 ? 1 : 0)) { | |
7554 | case 0: return UCLocalize("ENTERED_BY_USER"); | |
7555 | case 1: return UCLocalize("INSTALLED_BY_PACKAGE"); | |
7556 | ||
7557 | _nodefault | |
7558 | } | |
36bb2ca2 | 7559 | } |
b4d89997 | 7560 | |
5829aea2 GP |
7561 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7562 | int count = [sources_ count]; | |
7563 | switch (section) { | |
7564 | case 0: return (offset_ == 0 ? count : offset_); | |
7565 | case 1: return count - offset_; | |
b4d89997 | 7566 | |
5829aea2 GP |
7567 | _nodefault |
7568 | } | |
7569 | } | |
3178d79b | 7570 | |
5829aea2 GP |
7571 | - (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath { |
7572 | unsigned idx = 0; | |
7573 | switch (indexPath.section) { | |
7574 | case 0: idx = indexPath.row; break; | |
7575 | case 1: idx = indexPath.row + offset_; break; | |
3178d79b | 7576 | |
5829aea2 GP |
7577 | _nodefault |
7578 | } | |
7579 | return [sources_ objectAtIndex:idx]; | |
b4d89997 JF |
7580 | } |
7581 | ||
5829aea2 GP |
7582 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
7583 | static NSString *cellIdentifier = @"SourceCell"; | |
7584 | ||
7585 | SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
7586 | if(cell == nil) cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease]; | |
7587 | [cell setSource:[self sourceAtIndexPath:indexPath]]; | |
b89fa270 | 7588 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
5829aea2 GP |
7589 | |
7590 | return cell; | |
f6e13561 GP |
7591 | } |
7592 | ||
5829aea2 GP |
7593 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
7594 | Source *source = [self sourceAtIndexPath:indexPath]; | |
7595 | ||
7596 | FilteredPackageController *packages = [[[FilteredPackageController alloc] | |
7597 | initWithDatabase:database_ | |
7598 | title:[source label] | |
7599 | filter:@selector(isVisibleInSource:) | |
7600 | with:source | |
7601 | ] autorelease]; | |
7602 | ||
7603 | [packages setDelegate:delegate_]; | |
7604 | ||
7605 | [[self navigationController] pushViewController:packages animated:YES]; | |
36bb2ca2 | 7606 | } |
b4d89997 | 7607 | |
5829aea2 GP |
7608 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
7609 | Source *source = [self sourceAtIndexPath:indexPath]; | |
7610 | return [source record] != nil; | |
dcb47737 RP |
7611 | } |
7612 | ||
5829aea2 GP |
7613 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
7614 | Source *source = [self sourceAtIndexPath:indexPath]; | |
7615 | [Sources_ removeObjectForKey:[source key]]; | |
7616 | [delegate_ syncData]; | |
7617 | } | |
bcccf498 | 7618 | |
5829aea2 GP |
7619 | - (void) complete { |
7620 | [Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
7621 | @"deb", @"Type", | |
7622 | href_, @"URI", | |
7623 | @"./", @"Distribution", | |
7624 | nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href_]]; | |
bcccf498 | 7625 | |
5829aea2 | 7626 | [delegate_ syncData]; |
3178d79b JF |
7627 | } |
7628 | ||
5829aea2 GP |
7629 | - (NSString *) getWarning { |
7630 | NSString *href(href_); | |
7631 | NSRange colon([href rangeOfString:@"://"]); | |
7632 | if (colon.location != NSNotFound) | |
7633 | href = [href substringFromIndex:(colon.location + 3)]; | |
7634 | href = [href stringByAddingPercentEscapes]; | |
7635 | href = [CydiaURL(@"api/repotag/") stringByAppendingString:href]; | |
7636 | href = [href stringByCachingURLWithCurrentCDN]; | |
7637 | ||
7638 | NSURL *url([NSURL URLWithString:href]); | |
7639 | ||
7640 | NSStringEncoding encoding; | |
7641 | NSError *error(nil); | |
7642 | ||
7643 | if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]) | |
7644 | return [warning length] == 0 ? nil : warning; | |
7645 | return nil; | |
807ae6d7 | 7646 | } |
b4d89997 | 7647 | |
5829aea2 GP |
7648 | - (void) _endConnection:(NSURLConnection *)connection { |
7649 | // XXX: the memory management in this method is horribly awkward | |
7650 | ||
7651 | NSURLConnection **field = NULL; | |
7652 | if (connection == trivial_) | |
7653 | field = &trivial_; | |
7654 | else if (connection == trivial_bz2_) | |
7655 | field = &trivial_bz2_; | |
7656 | else if (connection == trivial_gz_) | |
7657 | field = &trivial_gz_; | |
7658 | _assert(field != NULL); | |
7659 | [connection release]; | |
7660 | *field = nil; | |
7661 | ||
7662 | if ( | |
7663 | trivial_ == nil && | |
7664 | trivial_bz2_ == nil && | |
7665 | trivial_gz_ == nil | |
7666 | ) { | |
7667 | bool defer(false); | |
7668 | ||
7669 | if (cydia_) { | |
7670 | if (NSString *warning = [self yieldToSelector:@selector(getWarning)]) { | |
7671 | defer = true; | |
7672 | ||
7673 | UIAlertView *alert = [[[UIAlertView alloc] | |
7674 | initWithTitle:UCLocalize("SOURCE_WARNING") | |
7675 | message:warning | |
7676 | delegate:self | |
7677 | cancelButtonTitle:UCLocalize("CANCEL") | |
7678 | otherButtonTitles:UCLocalize("ADD_ANYWAY"), nil | |
7679 | ] autorelease]; | |
7680 | ||
7681 | [alert setContext:@"warning"]; | |
7682 | [alert setNumberOfRows:1]; | |
7683 | [alert show]; | |
7684 | } else | |
7685 | [self complete]; | |
7686 | } else if (error_ != nil) { | |
7687 | UIAlertView *alert = [[[UIAlertView alloc] | |
7688 | initWithTitle:UCLocalize("VERIFICATION_ERROR") | |
7689 | message:[error_ localizedDescription] | |
7690 | delegate:self | |
7691 | cancelButtonTitle:UCLocalize("OK") | |
7692 | otherButtonTitles:nil | |
7693 | ] autorelease]; | |
7694 | ||
7695 | [alert setContext:@"urlerror"]; | |
7696 | [alert show]; | |
7697 | } else { | |
7698 | UIAlertView *alert = [[[UIAlertView alloc] | |
7699 | initWithTitle:UCLocalize("NOT_REPOSITORY") | |
7700 | message:UCLocalize("NOT_REPOSITORY_EX") | |
7701 | delegate:self | |
7702 | cancelButtonTitle:UCLocalize("OK") | |
7703 | otherButtonTitles:nil | |
7704 | ] autorelease]; | |
7705 | ||
7706 | [alert setContext:@"trivial"]; | |
7707 | [alert show]; | |
7708 | } | |
7709 | ||
7710 | [delegate_ setStatusBarShowsProgress:NO]; | |
7711 | [delegate_ removeProgressHUD:hud_]; | |
7712 | ||
7713 | [hud_ autorelease]; | |
7714 | hud_ = nil; | |
7715 | ||
7716 | if (!defer) { | |
7717 | [href_ release]; | |
7718 | href_ = nil; | |
7719 | } | |
7720 | ||
7721 | if (error_ != nil) { | |
7722 | [error_ release]; | |
7723 | error_ = nil; | |
7724 | } | |
7725 | } | |
807ae6d7 | 7726 | } |
8fe19fc1 | 7727 | |
5829aea2 GP |
7728 | - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { |
7729 | switch ([response statusCode]) { | |
7730 | case 200: | |
7731 | cydia_ = YES; | |
7732 | } | |
8e05f686 RP |
7733 | } |
7734 | ||
5829aea2 GP |
7735 | - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { |
7736 | lprintf("connection:\"%s\" didFailWithError:\"%s\"", [href_ UTF8String], [[error localizedDescription] UTF8String]); | |
7737 | if (error_ != nil) | |
7738 | error_ = [error retain]; | |
7739 | [self _endConnection:connection]; | |
807ae6d7 | 7740 | } |
b4d89997 | 7741 | |
5829aea2 GP |
7742 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
7743 | [self _endConnection:connection]; | |
7744 | } | |
b4d89997 | 7745 | |
5829aea2 | 7746 | - (NSString *) title { return UCLocalize("SOURCES"); } |
b4d89997 | 7747 | |
5829aea2 GP |
7748 | - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method { |
7749 | NSMutableURLRequest *request = [NSMutableURLRequest | |
7750 | requestWithURL:[NSURL URLWithString:href] | |
7751 | cachePolicy:NSURLRequestUseProtocolCachePolicy | |
7752 | timeoutInterval:120.0 | |
7753 | ]; | |
bc11cf5b | 7754 | |
5829aea2 | 7755 | [request setHTTPMethod:method]; |
b4d89997 | 7756 | |
5829aea2 GP |
7757 | if (Machine_ != NULL) |
7758 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
7759 | if (UniqueID_ != nil) | |
7760 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
7761 | if (Role_ != nil) | |
7762 | [request setValue:Role_ forHTTPHeaderField:@"X-Role"]; | |
b4d89997 | 7763 | |
5829aea2 | 7764 | return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; |
3178d79b JF |
7765 | } |
7766 | ||
5829aea2 GP |
7767 | - (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
7768 | NSString *context([alert context]); | |
dc5812ec | 7769 | |
5829aea2 GP |
7770 | if ([context isEqualToString:@"source"]) { |
7771 | switch (button) { | |
7772 | case 1: { | |
7773 | NSString *href = [[alert textField] text]; | |
baf80942 | 7774 | |
5829aea2 | 7775 | //installer_ = [[self _requestHRef:href method:@"GET"] retain]; |
f6e13561 | 7776 | |
5829aea2 GP |
7777 | if (![href hasSuffix:@"/"]) |
7778 | href_ = [href stringByAppendingString:@"/"]; | |
7779 | else | |
7780 | href_ = href; | |
7781 | href_ = [href_ retain]; | |
b4d89997 | 7782 | |
5829aea2 GP |
7783 | trivial_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages"] method:@"HEAD"] retain]; |
7784 | trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; | |
7785 | trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; | |
7786 | //trivial_bz2_ = [[self _requestHRef:[href stringByAppendingString:@"dists/Release"] method:@"HEAD"] retain]; | |
b4d89997 | 7787 | |
5829aea2 | 7788 | cydia_ = false; |
8fe19fc1 | 7789 | |
5829aea2 GP |
7790 | // XXX: this is stupid |
7791 | hud_ = [[delegate_ addProgressHUD] retain]; | |
7792 | [hud_ setText:UCLocalize("VERIFYING_URL")]; | |
7793 | } break; | |
770f2a8e | 7794 | |
5829aea2 GP |
7795 | case 0: |
7796 | break; | |
bc11cf5b | 7797 | |
5829aea2 GP |
7798 | _nodefault |
7799 | } | |
770f2a8e | 7800 | |
5829aea2 GP |
7801 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
7802 | } else if ([context isEqualToString:@"trivial"]) | |
7803 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
7804 | else if ([context isEqualToString:@"urlerror"]) | |
7805 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
7806 | else if ([context isEqualToString:@"warning"]) { | |
7807 | switch (button) { | |
7808 | case 1: | |
7809 | [self complete]; | |
7810 | break; | |
770f2a8e | 7811 | |
5829aea2 GP |
7812 | case 0: |
7813 | break; | |
b5e7eebb | 7814 | |
5829aea2 GP |
7815 | _nodefault |
7816 | } | |
770f2a8e | 7817 | |
5829aea2 GP |
7818 | [href_ release]; |
7819 | href_ = nil; | |
b5e7eebb | 7820 | |
5829aea2 GP |
7821 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
7822 | } | |
7823 | } | |
770f2a8e | 7824 | |
5829aea2 GP |
7825 | - (id) initWithDatabase:(Database *)database { |
7826 | if ((self = [super init]) != nil) { | |
7827 | [[self navigationItem] setTitle:UCLocalize("SOURCES")]; | |
7828 | [self updateButtonsForEditingStatus:NO animated:NO]; | |
770f2a8e | 7829 | |
5829aea2 GP |
7830 | database_ = database; |
7831 | sources_ = [[NSMutableArray arrayWithCapacity:16] retain]; | |
770f2a8e | 7832 | |
5829aea2 GP |
7833 | list_ = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain]; |
7834 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
7835 | [list_ setRowHeight:56]; | |
7836 | [[self view] addSubview:list_]; | |
770f2a8e | 7837 | |
5829aea2 GP |
7838 | [list_ setDataSource:self]; |
7839 | [list_ setDelegate:self]; | |
770f2a8e | 7840 | |
807ae6d7 JF |
7841 | [self reloadData]; |
7842 | } return self; | |
7843 | } | |
770f2a8e | 7844 | |
807ae6d7 | 7845 | - (void) reloadData { |
5829aea2 GP |
7846 | pkgSourceList list; |
7847 | if (!list.ReadMainList()) | |
7848 | return; | |
7849 | ||
7850 | [sources_ removeAllObjects]; | |
7851 | [sources_ addObjectsFromArray:[database_ sources]]; | |
7852 | _trace(); | |
7853 | [sources_ sortUsingSelector:@selector(compareByNameAndType:)]; | |
7854 | _trace(); | |
7855 | ||
7856 | int count([sources_ count]); | |
7857 | offset_ = 0; | |
7858 | for (int i = 0; i != count; i++) { | |
7859 | if ([[sources_ objectAtIndex:i] record] == nil) | |
7860 | break; | |
7861 | offset_++; | |
770f2a8e | 7862 | } |
807ae6d7 | 7863 | |
5829aea2 GP |
7864 | [list_ setEditing:NO]; |
7865 | [self updateButtonsForEditingStatus:NO animated:NO]; | |
7866 | [list_ reloadData]; | |
770f2a8e JF |
7867 | } |
7868 | ||
5829aea2 GP |
7869 | - (void) showAddSourcePrompt { |
7870 | UIAlertView *alert = [[[UIAlertView alloc] | |
7871 | initWithTitle:UCLocalize("ENTER_APT_URL") | |
7872 | message:nil | |
7873 | delegate:self | |
7874 | cancelButtonTitle:UCLocalize("CANCEL") | |
7875 | otherButtonTitles:UCLocalize("ADD_SOURCE"), nil | |
7876 | ] autorelease]; | |
770f2a8e | 7877 | |
5829aea2 GP |
7878 | [alert setContext:@"source"]; |
7879 | [alert setTransform:CGAffineTransformTranslate([alert transform], 0.0, 100.0)]; | |
770f2a8e | 7880 | |
5829aea2 GP |
7881 | [alert setNumberOfRows:1]; |
7882 | [alert addTextFieldWithValue:@"http://" label:@""]; | |
770f2a8e | 7883 | |
5829aea2 GP |
7884 | UITextInputTraits *traits = [[alert textField] textInputTraits]; |
7885 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
7886 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
7887 | [traits setKeyboardType:UIKeyboardTypeURL]; | |
7888 | // XXX: UIReturnKeyDone | |
7889 | [traits setReturnKeyType:UIReturnKeyNext]; | |
770f2a8e | 7890 | |
5829aea2 | 7891 | [alert show]; |
770f2a8e JF |
7892 | } |
7893 | ||
5829aea2 GP |
7894 | - (void) addButtonClicked { |
7895 | [self showAddSourcePrompt]; | |
770f2a8e JF |
7896 | } |
7897 | ||
5829aea2 GP |
7898 | - (void) updateButtonsForEditingStatus:(BOOL)editing animated:(BOOL)animated { |
7899 | [[self navigationItem] setLeftBarButtonItem:(editing ? [[[UIBarButtonItem alloc] | |
7900 | initWithTitle:UCLocalize("ADD") | |
7901 | style:UIBarButtonItemStylePlain | |
7902 | target:self | |
7903 | action:@selector(addButtonClicked) | |
7904 | ] autorelease] : [[self navigationItem] backBarButtonItem]) animated:animated]; | |
7905 | ||
7906 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
7907 | initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) | |
7908 | style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
7909 | target:self | |
7910 | action:@selector(editButtonClicked) | |
7911 | ] autorelease] animated:animated]; | |
7912 | ||
7913 | if (IsWildcat_ && !editing) | |
7914 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7915 | initWithTitle:UCLocalize("SETTINGS") | |
7916 | style:UIBarButtonItemStylePlain | |
7917 | target:self | |
7918 | action:@selector(settingsButtonClicked) | |
7919 | ] autorelease]]; | |
770f2a8e JF |
7920 | } |
7921 | ||
5829aea2 GP |
7922 | - (void) settingsButtonClicked { |
7923 | [delegate_ showSettings]; | |
7924 | } | |
7925 | ||
7926 | - (void) editButtonClicked { | |
7927 | [list_ setEditing:![list_ isEditing] animated:YES]; | |
7928 | ||
7929 | [self updateButtonsForEditingStatus:[list_ isEditing] animated:YES]; | |
770f2a8e JF |
7930 | } |
7931 | ||
21c6da4b GP |
7932 | @end |
7933 | /* }}} */ | |
f3e11d24 | 7934 | |
5829aea2 GP |
7935 | /* Settings Controller {{{ */ |
7936 | @interface SettingsController : CYViewController < | |
c21004b9 JF |
7937 | UITableViewDataSource, |
7938 | UITableViewDelegate | |
7939 | > { | |
21c6da4b | 7940 | _transient Database *database_; |
3931b718 JF |
7941 | // XXX: ok, "roledelegate_"?... |
7942 | _transient id roledelegate_; | |
21c6da4b GP |
7943 | UITableView *table_; |
7944 | UISegmentedControl *segment_; | |
7945 | UIView *container_; | |
7946 | } | |
c21004b9 JF |
7947 | |
7948 | - (void) showDoneButton; | |
7949 | - (void) resizeSegmentedControl; | |
7950 | ||
21c6da4b GP |
7951 | @end |
7952 | ||
5829aea2 | 7953 | @implementation SettingsController |
21c6da4b GP |
7954 | - (void) dealloc { |
7955 | [table_ release]; | |
7956 | [segment_ release]; | |
7957 | [container_ release]; | |
bc11cf5b | 7958 | |
21c6da4b GP |
7959 | [super dealloc]; |
7960 | } | |
7961 | ||
7962 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { | |
7963 | if ((self = [super init])) { | |
7964 | database_ = database; | |
7965 | roledelegate_ = delegate; | |
bc11cf5b | 7966 | |
21c6da4b | 7967 | [[self navigationItem] setTitle:UCLocalize("WHO_ARE_YOU")]; |
bc11cf5b | 7968 | |
21c6da4b | 7969 | NSArray *items = [NSArray arrayWithObjects: |
bc11cf5b | 7970 | UCLocalize("USER"), |
21c6da4b GP |
7971 | UCLocalize("HACKER"), |
7972 | UCLocalize("DEVELOPER"), | |
7973 | nil]; | |
7974 | segment_ = [[UISegmentedControl alloc] initWithItems:items]; | |
7975 | container_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, 44.0f)]; | |
7976 | [container_ addSubview:segment_]; | |
bc11cf5b | 7977 | |
21c6da4b GP |
7978 | int index = -1; |
7979 | if ([Role_ isEqualToString:@"User"]) index = 0; | |
7980 | if ([Role_ isEqualToString:@"Hacker"]) index = 1; | |
7981 | if ([Role_ isEqualToString:@"Developer"]) index = 2; | |
7982 | if (index != -1) { | |
7983 | [segment_ setSelectedSegmentIndex:index]; | |
7984 | [self showDoneButton]; | |
7985 | } | |
bc11cf5b | 7986 | |
21c6da4b | 7987 | [segment_ addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged]; |
e62f29c6 | 7988 | [self resizeSegmentedControl]; |
bc11cf5b | 7989 | |
21c6da4b GP |
7990 | table_ = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped]; |
7991 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
7992 | [table_ setDelegate:self]; | |
7993 | [table_ setDataSource:self]; | |
7994 | [[self view] addSubview:table_]; | |
7995 | [table_ reloadData]; | |
7996 | } return self; | |
7997 | } | |
7998 | ||
e62f29c6 GP |
7999 | - (void) resizeSegmentedControl { |
8000 | CGFloat width = [[self view] frame].size.width; | |
8001 | [segment_ setFrame:CGRectMake(width / 32.0f, 0, width - (width / 32.0f * 2.0f), 44.0f)]; | |
8002 | } | |
8003 | ||
2bdd73bd GP |
8004 | - (void) viewWillAppear:(BOOL)animated { |
8005 | [super viewWillAppear:animated]; | |
bc11cf5b | 8006 | |
e62f29c6 GP |
8007 | [self resizeSegmentedControl]; |
8008 | } | |
8009 | ||
8010 | - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { | |
8011 | [self resizeSegmentedControl]; | |
8012 | } | |
8013 | ||
8014 | - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { | |
8015 | [self resizeSegmentedControl]; | |
2bdd73bd GP |
8016 | } |
8017 | ||
21c6da4b | 8018 | - (void) save { |
fed0d010 | 8019 | NSString *role(nil); |
bc11cf5b | 8020 | |
21c6da4b | 8021 | switch ([segment_ selectedSegmentIndex]) { |
124c6201 GP |
8022 | case 0: role = @"User"; break; |
8023 | case 1: role = @"Hacker"; break; | |
8024 | case 2: role = @"Developer"; break; | |
21c6da4b GP |
8025 | |
8026 | _nodefault | |
8027 | } | |
8028 | ||
124c6201 | 8029 | if (![role isEqualToString:Role_]) { |
fed0d010 | 8030 | bool rolling(Role_ == nil); |
124c6201 | 8031 | Role_ = role; |
bc11cf5b | 8032 | |
124c6201 GP |
8033 | Settings_ = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
8034 | Role_, @"Role", | |
8035 | nil]; | |
21c6da4b | 8036 | |
124c6201 | 8037 | [Metadata_ setObject:Settings_ forKey:@"Settings"]; |
124c6201 | 8038 | Changed_ = true; |
bc11cf5b | 8039 | |
fed0d010 JF |
8040 | if (rolling) |
8041 | [roledelegate_ loadData]; | |
8042 | else | |
8043 | [roledelegate_ updateData]; | |
124c6201 | 8044 | } |
21c6da4b GP |
8045 | } |
8046 | ||
8047 | - (void) segmentChanged:(UISegmentedControl *)control { | |
8048 | [self showDoneButton]; | |
8049 | } | |
8050 | ||
8576ab50 | 8051 | - (void) saveAndClose { |
21c6da4b | 8052 | [self save]; |
8576ab50 GP |
8053 | |
8054 | [[self navigationItem] setRightBarButtonItem:nil]; | |
21c6da4b GP |
8055 | [[self navigationController] dismissModalViewControllerAnimated:YES]; |
8056 | } | |
8057 | ||
8576ab50 GP |
8058 | - (void) doneButtonClicked { |
8059 | UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)] autorelease]; | |
8060 | [spinner startAnimating]; | |
8061 | UIBarButtonItem *spinItem = [[[UIBarButtonItem alloc] initWithCustomView:spinner] autorelease]; | |
8062 | [[self navigationItem] setRightBarButtonItem:spinItem]; | |
8063 | ||
8064 | [self performSelector:@selector(saveAndClose) withObject:nil afterDelay:0]; | |
8065 | } | |
8066 | ||
21c6da4b | 8067 | - (void) showDoneButton { |
dd9de556 | 8068 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
21c6da4b GP |
8069 | initWithTitle:UCLocalize("DONE") |
8070 | style:UIBarButtonItemStyleDone | |
8071 | target:self | |
8072 | action:@selector(doneButtonClicked) | |
dd9de556 | 8073 | ] autorelease] animated:([[self navigationItem] rightBarButtonItem] == nil)]; |
21c6da4b GP |
8074 | } |
8075 | ||
8076 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { | |
e62f29c6 GP |
8077 | // XXX: For not having a single cell in the table, this sure is a lot of sections. |
8078 | return 6; | |
21c6da4b GP |
8079 | } |
8080 | ||
8081 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
8082 | return 0; // :( | |
8083 | } | |
8084 | ||
8085 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
8086 | return nil; // This method is required by the protocol. | |
8087 | } | |
8088 | ||
8089 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { | |
bc11cf5b | 8090 | if (section == 1) |
21c6da4b | 8091 | return UCLocalize("ROLE_EX"); |
bc11cf5b | 8092 | if (section == 4) |
21c6da4b GP |
8093 | return [NSString stringWithFormat: |
8094 | @"%@: %@\n%@: %@\n%@: %@", | |
bc11cf5b JF |
8095 | UCLocalize("USER"), UCLocalize("USER_EX"), |
8096 | UCLocalize("HACKER"), UCLocalize("HACKER_EX"), | |
21c6da4b GP |
8097 | UCLocalize("DEVELOPER"), UCLocalize("DEVELOPER_EX") |
8098 | ]; | |
8099 | else return nil; | |
8100 | } | |
8101 | ||
8102 | - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { | |
36fbb2aa | 8103 | return section == 3 ? 44.0f : 0; |
21c6da4b GP |
8104 | } |
8105 | ||
8106 | - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { | |
36fbb2aa | 8107 | return section == 3 ? container_ : nil; |
21c6da4b GP |
8108 | } |
8109 | ||
f3e11d24 GP |
8110 | @end |
8111 | /* }}} */ | |
8112 | /* Stash Controller {{{ */ | |
5829aea2 | 8113 | @interface StashController : CYViewController { |
3931b718 JF |
8114 | // XXX: just delete these things |
8115 | _transient UIActivityIndicatorView *spinner_; | |
8116 | _transient UILabel *status_; | |
8117 | _transient UILabel *caption_; | |
f3e11d24 GP |
8118 | } |
8119 | @end | |
8120 | ||
5829aea2 | 8121 | @implementation StashController |
f3e11d24 GP |
8122 | - (id) init { |
8123 | if ((self = [super init])) { | |
e61a973c | 8124 | [[self view] setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; |
f3e11d24 | 8125 | |
dd9de556 | 8126 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; |
e61a973c | 8127 | CGRect spinrect = [spinner_ frame]; |
f3e11d24 GP |
8128 | spinrect.origin.x = ([[self view] frame].size.width / 2) - (spinrect.size.width / 2); |
8129 | spinrect.origin.y = [[self view] frame].size.height - 80.0f; | |
8130 | [spinner_ setFrame:spinrect]; | |
8131 | [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; | |
e61a973c | 8132 | [[self view] addSubview:spinner_]; |
e61a973c | 8133 | [spinner_ startAnimating]; |
f3e11d24 GP |
8134 | |
8135 | CGRect captrect; | |
8136 | captrect.size.width = [[self view] frame].size.width; | |
8137 | captrect.size.height = 40.0f; | |
8138 | captrect.origin.x = 0; | |
8139 | captrect.origin.y = ([[self view] frame].size.height / 2) - (captrect.size.height * 2); | |
dd9de556 | 8140 | caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; |
eab7d275 | 8141 | [caption_ setText:UCLocalize("PREPARING_FILESYSTEM")]; |
851f4a99 | 8142 | [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
f3e11d24 GP |
8143 | [caption_ setFont:[UIFont boldSystemFontOfSize:28.0f]]; |
8144 | [caption_ setTextColor:[UIColor whiteColor]]; | |
8145 | [caption_ setBackgroundColor:[UIColor clearColor]]; | |
8146 | [caption_ setShadowColor:[UIColor blackColor]]; | |
8147 | [caption_ setTextAlignment:UITextAlignmentCenter]; | |
8148 | [[self view] addSubview:caption_]; | |
f3e11d24 GP |
8149 | |
8150 | CGRect statusrect; | |
8151 | statusrect.size.width = [[self view] frame].size.width; | |
8152 | statusrect.size.height = 30.0f; | |
8153 | statusrect.origin.x = 0; | |
8154 | statusrect.origin.y = ([[self view] frame].size.height / 2) - statusrect.size.height; | |
dd9de556 | 8155 | status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; |
851f4a99 | 8156 | [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
eab7d275 | 8157 | [status_ setText:UCLocalize("EXIT_WHEN_COMPLETE")]; |
f3e11d24 GP |
8158 | [status_ setFont:[UIFont systemFontOfSize:16.0f]]; |
8159 | [status_ setTextColor:[UIColor whiteColor]]; | |
8160 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
8161 | [status_ setShadowColor:[UIColor blackColor]]; | |
8162 | [status_ setTextAlignment:UITextAlignmentCenter]; | |
8163 | [[self view] addSubview:status_]; | |
f3e11d24 GP |
8164 | } return self; |
8165 | } | |
8166 | ||
770f2a8e | 8167 | @end |
807ae6d7 | 8168 | /* }}} */ |
770f2a8e | 8169 | |
2367a917 | 8170 | @interface Cydia : UIApplication < |
adb61bda GP |
8171 | ConfirmationControllerDelegate, |
8172 | ProgressControllerDelegate, | |
eb30da80 | 8173 | CydiaDelegate, |
68046ccc JF |
8174 | UINavigationControllerDelegate, |
8175 | UITabBarControllerDelegate | |
2367a917 | 8176 | > { |
3931b718 JF |
8177 | // XXX: evaluate all fields for _transient |
8178 | ||
dc5812ec | 8179 | UIWindow *window_; |
68046ccc | 8180 | CYTabBarController *tabbar_; |
9bedffaa | 8181 | |
a54b1c10 | 8182 | NSMutableArray *essential_; |
9bedffaa | 8183 | NSMutableArray *broken_; |
dc5812ec JF |
8184 | |
8185 | Database *database_; | |
dc5812ec | 8186 | |
54043703 | 8187 | NSURL *starturl_; |
54043703 | 8188 | |
235f5487 | 8189 | unsigned locked_; |
54043703 | 8190 | unsigned activity_; |
9b619239 | 8191 | |
5829aea2 | 8192 | StashController *stash_; |
f3e11d24 | 8193 | |
8c02abc8 | 8194 | bool loaded_; |
dc5812ec JF |
8195 | } |
8196 | ||
fed0d010 | 8197 | - (void) loadData; |
670a0494 | 8198 | |
dc5812ec JF |
8199 | @end |
8200 | ||
8201 | @implementation Cydia | |
8202 | ||
b5e7eebb | 8203 | - (void) beginUpdate { |
7585ce66 | 8204 | [tabbar_ beginUpdate]; |
b5e7eebb GP |
8205 | } |
8206 | ||
8207 | - (BOOL) updating { | |
7585ce66 | 8208 | return [tabbar_ updating]; |
b5e7eebb GP |
8209 | } |
8210 | ||
9bedffaa JF |
8211 | - (void) _loaded { |
8212 | if ([broken_ count] != 0) { | |
8213 | int count = [broken_ count]; | |
8214 | ||
37d2b2a9 | 8215 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 8216 | initWithTitle:(count == 1 ? UCLocalize("HALFINSTALLED_PACKAGE") : [NSString stringWithFormat:UCLocalize("HALFINSTALLED_PACKAGES"), count]) |
b5e7eebb GP |
8217 | message:UCLocalize("HALFINSTALLED_PACKAGE_EX") |
8218 | delegate:self | |
8219 | cancelButtonTitle:UCLocalize("FORCIBLY_CLEAR") | |
8220 | otherButtonTitles:UCLocalize("TEMPORARY_IGNORE"), nil | |
9bedffaa JF |
8221 | ] autorelease]; |
8222 | ||
37d2b2a9 GP |
8223 | [alert setContext:@"fixhalf"]; |
8224 | [alert show]; | |
9bedffaa JF |
8225 | } else if (!Ignored_ && [essential_ count] != 0) { |
8226 | int count = [essential_ count]; | |
8227 | ||
37d2b2a9 | 8228 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 8229 | initWithTitle:(count == 1 ? UCLocalize("ESSENTIAL_UPGRADE") : [NSString stringWithFormat:UCLocalize("ESSENTIAL_UPGRADES"), count]) |
b5e7eebb GP |
8230 | message:UCLocalize("ESSENTIAL_UPGRADE_EX") |
8231 | delegate:self | |
8232 | cancelButtonTitle:UCLocalize("TEMPORARY_IGNORE") | |
37d2b2a9 | 8233 | otherButtonTitles:UCLocalize("UPGRADE_ESSENTIAL"), UCLocalize("COMPLETE_UPGRADE"), nil |
9bedffaa JF |
8234 | ] autorelease]; |
8235 | ||
37d2b2a9 GP |
8236 | [alert setContext:@"upgrade"]; |
8237 | [alert show]; | |
9bedffaa JF |
8238 | } |
8239 | } | |
8240 | ||
7623f855 | 8241 | - (void) _saveConfig { |
ffbb3bd5 JF |
8242 | _trace(); |
8243 | MetaFile_.Sync(); | |
8244 | _trace(); | |
8245 | ||
7623f855 | 8246 | if (Changed_) { |
7623f855 | 8247 | NSString *error(nil); |
ffbb3bd5 | 8248 | |
7623f855 JF |
8249 | if (NSData *data = [NSPropertyListSerialization dataFromPropertyList:Metadata_ format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error]) { |
8250 | _trace(); | |
8251 | NSError *error(nil); | |
8252 | if (![data writeToFile:@"/var/lib/cydia/metadata.plist" options:NSAtomicWrite error:&error]) | |
8253 | NSLog(@"failure to save metadata data: %@", error); | |
8254 | _trace(); | |
ffbb3bd5 JF |
8255 | |
8256 | Changed_ = false; | |
7623f855 JF |
8257 | } else { |
8258 | NSLog(@"failure to serialize metadata: %@", error); | |
7623f855 | 8259 | } |
7623f855 JF |
8260 | } |
8261 | } | |
8262 | ||
89571a5b GP |
8263 | // Navigation controller for the queuing badge. |
8264 | - (CYNavigationController *) queueNavigationController { | |
8265 | NSArray *controllers = [tabbar_ viewControllers]; | |
8266 | return [controllers objectAtIndex:3]; | |
8267 | } | |
8268 | ||
7623f855 JF |
8269 | - (void) _updateData { |
8270 | [self _saveConfig]; | |
8271 | ||
f6e13561 | 8272 | [tabbar_ reloadData]; |
f6371a33 | 8273 | |
89571a5b | 8274 | CYNavigationController *navigation = [self queueNavigationController]; |
b5e7eebb | 8275 | |
4305896c | 8276 | id queuedelegate = nil; |
89571a5b GP |
8277 | if ([[navigation viewControllers] count] > 0) |
8278 | queuedelegate = [[navigation viewControllers] objectAtIndex:0]; | |
bc11cf5b | 8279 | |
89571a5b GP |
8280 | [queuedelegate queueStatusDidChange]; |
8281 | [[navigation tabBarItem] setBadgeValue:(Queuing_ ? UCLocalize("Q_D") : nil)]; | |
7623f855 JF |
8282 | } |
8283 | ||
9aecdc9c | 8284 | - (void) _refreshIfPossible { |
8c02abc8 | 8285 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
bc11cf5b | 8286 | |
45e21ffa GP |
8287 | bool recently = false; |
8288 | NSDate *update([Metadata_ objectForKey:@"LastUpdate"]); | |
8289 | if (update != nil) { | |
8290 | NSTimeInterval interval([update timeIntervalSinceNow]); | |
8291 | if (interval <= 0 && interval > -(15*60)) | |
8292 | recently = true; | |
8293 | } | |
8294 | ||
8295 | // Don't automatic refresh if: | |
8296 | // - We already refreshed recently. | |
8297 | // - We already auto-refreshed this launch. | |
8298 | // - Auto-refresh is disabled. | |
8299 | if (recently || loaded_ || ManualRefresh) { | |
8300 | [self performSelectorOnMainThread:@selector(_loaded) withObject:nil waitUntilDone:NO]; | |
8301 | ||
8302 | // If we are cancelling due to ManualRefresh or a recent refresh | |
8303 | // we need to make sure it knows it's already loaded. | |
8304 | loaded_ = true; | |
8305 | return; | |
8306 | } else { | |
8307 | // We are going to load, so remember that. | |
8308 | loaded_ = true; | |
8309 | } | |
8310 | ||
afb5333a JF |
8311 | SCNetworkReachabilityFlags flags; { |
8312 | SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(NULL, "cydia.saurik.com")); | |
8313 | SCNetworkReachabilityGetFlags(reachability, &flags); | |
8314 | CFRelease(reachability); | |
8315 | } | |
8316 | ||
8317 | // XXX: this elaborate mess is what Apple is using to determine this? :( | |
8318 | // XXX: do we care if the user has to intervene? maybe that's ok? | |
8319 | bool reachable( | |
8320 | (flags & kSCNetworkReachabilityFlagsReachable) != 0 && ( | |
8321 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || ( | |
8322 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 || | |
8323 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0 | |
8324 | ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 || | |
8325 | (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0 | |
8326 | ) | |
8327 | ); | |
bc11cf5b | 8328 | |
45e21ffa GP |
8329 | // If we can reach the server, auto-refresh! |
8330 | if (reachable) | |
7585ce66 | 8331 | [tabbar_ performSelectorOnMainThread:@selector(setUpdate:) withObject:update waitUntilDone:NO]; |
9aecdc9c | 8332 | |
8c02abc8 | 8333 | [pool release]; |
9aecdc9c GP |
8334 | } |
8335 | ||
8336 | - (void) refreshIfPossible { | |
8c02abc8 | 8337 | [NSThread detachNewThreadSelector:@selector(_refreshIfPossible) toTarget:self withObject:nil]; |
9aecdc9c GP |
8338 | } |
8339 | ||
36bb2ca2 | 8340 | - (void) _reloadData { |
851f4a99 GP |
8341 | UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); |
8342 | [hud setText:UCLocalize("RELOADING_DATA")]; | |
dc5812ec | 8343 | |
d061f4ba | 8344 | [database_ yieldToSelector:@selector(reloadData) withObject:nil]; |
d061f4ba | 8345 | |
36fbb2aa JF |
8346 | if (hud != nil) |
8347 | [self removeProgressHUD:hud]; | |
c25a610d | 8348 | |
36bb2ca2 | 8349 | size_t changes(0); |
9bedffaa | 8350 | |
a54b1c10 | 8351 | [essential_ removeAllObjects]; |
9bedffaa | 8352 | [broken_ removeAllObjects]; |
b4d89997 | 8353 | |
670a0494 | 8354 | NSArray *packages([database_ packages]); |
affeffc7 | 8355 | for (Package *package in packages) { |
9bedffaa JF |
8356 | if ([package half]) |
8357 | [broken_ addObject:package]; | |
31f3cfff | 8358 | if ([package upgradableAndEssential:NO]) { |
a54b1c10 JF |
8359 | if ([package essential]) |
8360 | [essential_ addObject:package]; | |
36bb2ca2 | 8361 | ++changes; |
a54b1c10 | 8362 | } |
36bb2ca2 | 8363 | } |
b4d89997 | 8364 | |
0e1784b4 JF |
8365 | NSLog(@"changes:#%u", changes); |
8366 | ||
89571a5b | 8367 | UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem]; |
36bb2ca2 | 8368 | if (changes != 0) { |
0e1784b4 | 8369 | _trace(); |
36bb2ca2 | 8370 | NSString *badge([[NSNumber numberWithInt:changes] stringValue]); |
45e21ffa | 8371 | [changesItem setBadgeValue:badge]; |
65a03a7a | 8372 | [changesItem setAnimatedBadge:([essential_ count] > 0)]; |
0e1784b4 | 8373 | [self setApplicationIconBadgeNumber:changes]; |
c25a610d | 8374 | } else { |
0e1784b4 | 8375 | _trace(); |
45e21ffa GP |
8376 | [changesItem setBadgeValue:nil]; |
8377 | [changesItem setAnimatedBadge:NO]; | |
0e1784b4 | 8378 | [self setApplicationIconBadgeNumber:0]; |
c25a610d | 8379 | } |
b4d89997 | 8380 | |
7623f855 | 8381 | [self _updateData]; |
6d9712c4 | 8382 | |
8c02abc8 | 8383 | [self refreshIfPossible]; |
6d9712c4 JF |
8384 | } |
8385 | ||
7b0ce2da | 8386 | - (void) updateData { |
7623f855 | 8387 | [self _updateData]; |
b4d89997 JF |
8388 | } |
8389 | ||
7b0ce2da JF |
8390 | - (void) update_ { |
8391 | [database_ update]; | |
8392 | } | |
8393 | ||
8394 | - (void) syncData { | |
670a0494 | 8395 | FILE *file(fopen("/etc/apt/sources.list.d/cydia.list", "w")); |
7b0ce2da JF |
8396 | _assert(file != NULL); |
8397 | ||
670a0494 JF |
8398 | for (NSString *key in [Sources_ allKeys]) { |
8399 | NSDictionary *source([Sources_ objectForKey:key]); | |
7b0ce2da JF |
8400 | |
8401 | fprintf(file, "%s %s %s\n", | |
8402 | [[source objectForKey:@"Type"] UTF8String], | |
8403 | [[source objectForKey:@"URI"] UTF8String], | |
8404 | [[source objectForKey:@"Distribution"] UTF8String] | |
8405 | ); | |
8406 | } | |
8407 | ||
8408 | fclose(file); | |
8409 | ||
8410 | [self _saveConfig]; | |
8411 | ||
adb61bda | 8412 | ProgressController *progress = [[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]; |
eb30da80 | 8413 | CYNavigationController *navigation = [[[CYNavigationController alloc] initWithRootViewController:progress] autorelease]; |
36fbb2aa JF |
8414 | if (IsWildcat_) |
8415 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 8416 | [tabbar_ presentModalViewController:navigation animated:YES]; |
b5e7eebb GP |
8417 | |
8418 | [progress | |
7b0ce2da JF |
8419 | detachNewThreadSelector:@selector(update_) |
8420 | toTarget:self | |
8421 | withObject:nil | |
43f3d7f6 | 8422 | title:UCLocalize("UPDATING_SOURCES") |
7b0ce2da JF |
8423 | ]; |
8424 | } | |
8425 | ||
36bb2ca2 JF |
8426 | - (void) reloadData { |
8427 | @synchronized (self) { | |
b5e7eebb | 8428 | [self _reloadData]; |
36bb2ca2 | 8429 | } |
b4d89997 JF |
8430 | } |
8431 | ||
8432 | - (void) resolve { | |
8433 | pkgProblemResolver *resolver = [database_ resolver]; | |
8434 | ||
8435 | resolver->InstallProtect(); | |
8436 | if (!resolver->Resolve(true)) | |
8437 | _error->Discard(); | |
2367a917 JF |
8438 | } |
8439 | ||
670a0494 JF |
8440 | - (bool) perform { |
8441 | if (![database_ prepare]) | |
8442 | return false; | |
49048579 | 8443 | |
adb61bda | 8444 | ConfirmationController *page([[[ConfirmationController alloc] initWithDatabase:database_] autorelease]); |
affeffc7 | 8445 | [page setDelegate:self]; |
3931b718 | 8446 | CYNavigationController *confirm_([[[CYNavigationController alloc] initWithRootViewController:page] autorelease]); |
b5e7eebb | 8447 | [confirm_ setDelegate:self]; |
49048579 | 8448 | |
36fbb2aa JF |
8449 | if (IsWildcat_) |
8450 | [confirm_ setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 8451 | [tabbar_ presentModalViewController:confirm_ animated:YES]; |
670a0494 JF |
8452 | |
8453 | return true; | |
3178d79b JF |
8454 | } |
8455 | ||
dc63e78f JF |
8456 | - (void) queue { |
8457 | @synchronized (self) { | |
8458 | [self perform]; | |
8459 | } | |
8460 | } | |
8461 | ||
8462 | - (void) clearPackage:(Package *)package { | |
8463 | @synchronized (self) { | |
8464 | [package clear]; | |
8465 | [self resolve]; | |
8466 | [self perform]; | |
8467 | } | |
8468 | } | |
8469 | ||
77801ff1 JF |
8470 | - (void) installPackages:(NSArray *)packages { |
8471 | @synchronized (self) { | |
8472 | for (Package *package in packages) | |
8473 | [package install]; | |
8474 | [self resolve]; | |
8475 | [self perform]; | |
8476 | } | |
8477 | } | |
8478 | ||
36bb2ca2 JF |
8479 | - (void) installPackage:(Package *)package { |
8480 | @synchronized (self) { | |
8481 | [package install]; | |
8482 | [self resolve]; | |
8483 | [self perform]; | |
8484 | } | |
8485 | } | |
8486 | ||
8487 | - (void) removePackage:(Package *)package { | |
8488 | @synchronized (self) { | |
8489 | [package remove]; | |
8490 | [self resolve]; | |
8491 | [self perform]; | |
8492 | } | |
8493 | } | |
8494 | ||
8495 | - (void) distUpgrade { | |
8496 | @synchronized (self) { | |
670a0494 JF |
8497 | if (![database_ upgrade]) |
8498 | return; | |
36bb2ca2 JF |
8499 | [self perform]; |
8500 | } | |
b4d89997 JF |
8501 | } |
8502 | ||
dc63e78f | 8503 | - (void) complete { |
36bb2ca2 | 8504 | @synchronized (self) { |
36bb2ca2 JF |
8505 | [self _reloadData]; |
8506 | } | |
3178d79b JF |
8507 | } |
8508 | ||
b5e7eebb | 8509 | - (void) confirmWithNavigationController:(UINavigationController *)navigation { |
f6371a33 JF |
8510 | Queuing_ = false; |
8511 | ||
adb61bda | 8512 | ProgressController *progress = [[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]; |
bc11cf5b | 8513 | |
b5e7eebb GP |
8514 | if (navigation != nil) { |
8515 | [navigation pushViewController:progress animated:YES]; | |
8516 | } else { | |
9cb0bff2 | 8517 | navigation = [[[CYNavigationController alloc] initWithRootViewController:progress] autorelease]; |
36fbb2aa JF |
8518 | if (IsWildcat_) |
8519 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 8520 | [tabbar_ presentModalViewController:navigation animated:YES]; |
b5e7eebb | 8521 | } |
bc11cf5b | 8522 | |
b5e7eebb | 8523 | [progress |
2367a917 JF |
8524 | detachNewThreadSelector:@selector(perform) |
8525 | toTarget:database_ | |
8526 | withObject:nil | |
43f3d7f6 | 8527 | title:UCLocalize("RUNNING") |
2367a917 | 8528 | ]; |
235f5487 JF |
8529 | |
8530 | ++locked_; | |
2367a917 JF |
8531 | } |
8532 | ||
39cda3a8 | 8533 | - (void) progressControllerIsComplete:(ProgressController *)progress { |
235f5487 | 8534 | --locked_; |
dc63e78f | 8535 | [self complete]; |
dc5812ec JF |
8536 | } |
8537 | ||
68046ccc | 8538 | - (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { |
89571a5b GP |
8539 | CYNavigationController *controller = (CYNavigationController *) viewController; |
8540 | ||
8541 | if ([[controller viewControllers] count] == 0) { | |
8542 | int index = [tabbar_ selectedIndex]; | |
8543 | CYViewController *root = nil; | |
8544 | ||
8545 | if (index == 0) | |
8546 | root = [[[HomeController alloc] init] autorelease]; | |
8547 | else if (index == 1) | |
8548 | root = [[[SectionsController alloc] initWithDatabase:database_] autorelease]; | |
8549 | else if (index == 2) | |
ea3bb538 | 8550 | root = [[[ChangesController alloc] initWithDatabase:database_] autorelease]; |
89571a5b GP |
8551 | |
8552 | if (IsWildcat_) { | |
8553 | if (index == 3) | |
8554 | root = [[[InstalledController alloc] initWithDatabase:database_] autorelease]; | |
8555 | else if (index == 4) | |
8556 | root = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; | |
8557 | else if (index == 5) | |
8558 | root = [[[SearchController alloc] initWithDatabase:database_] autorelease]; | |
8559 | } else { | |
8560 | if (index == 3) | |
8561 | root = [[[ManageController alloc] init] autorelease]; | |
8562 | else if (index == 4) | |
8563 | root = [[[SearchController alloc] initWithDatabase:database_] autorelease]; | |
8564 | } | |
36bb2ca2 | 8565 | |
89571a5b | 8566 | [root setDelegate:self]; |
36bb2ca2 | 8567 | |
89571a5b GP |
8568 | if (root != nil) |
8569 | [controller setViewControllers:[NSArray arrayWithObject:root]]; | |
dc5812ec | 8570 | } |
06aa974d JF |
8571 | } |
8572 | ||
21c6da4b | 8573 | - (void) showSettings { |
5829aea2 | 8574 | SettingsController *role = [[[SettingsController alloc] initWithDatabase:database_ delegate:self] autorelease]; |
3931b718 | 8575 | CYNavigationController *nav = [[[CYNavigationController alloc] initWithRootViewController:role] autorelease]; |
36fbb2aa JF |
8576 | if (IsWildcat_) |
8577 | [nav setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 8578 | [tabbar_ presentModalViewController:nav animated:YES]; |
7b0ce2da JF |
8579 | } |
8580 | ||
54043703 JF |
8581 | - (void) retainNetworkActivityIndicator { |
8582 | if (activity_++ == 0) | |
8583 | [self setNetworkActivityIndicatorVisible:YES]; | |
8584 | } | |
8585 | ||
8586 | - (void) releaseNetworkActivityIndicator { | |
8587 | if (--activity_ == 0) | |
8588 | [self setNetworkActivityIndicatorVisible:NO]; | |
8589 | } | |
8590 | ||
f6e13561 | 8591 | - (void) setPackageController:(CYPackageController *)view { |
7592e053 JF |
8592 | WebThreadLock(); |
8593 | [view setPackage:nil]; | |
7592e053 JF |
8594 | WebThreadUnlock(); |
8595 | } | |
8596 | ||
674dce72 GP |
8597 | - (void) cancelAndClear:(bool)clear { |
8598 | @synchronized (self) { | |
8599 | if (clear) { | |
c6ca67ba | 8600 | [database_ clear]; |
b5e7eebb | 8601 | Queuing_ = false; |
674dce72 | 8602 | } else { |
b5e7eebb | 8603 | Queuing_ = true; |
6067f1b8 JF |
8604 | } |
8605 | ||
a4217bbb | 8606 | [self _updateData]; |
674dce72 | 8607 | } |
37d2b2a9 | 8608 | } |
7b0ce2da | 8609 | |
b5e7eebb GP |
8610 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
8611 | NSString *context([alert context]); | |
bc11cf5b | 8612 | |
b5e7eebb GP |
8613 | if ([context isEqualToString:@"fixhalf"]) { |
8614 | if (button == [alert firstOtherButtonIndex]) { | |
37d2b2a9 GP |
8615 | @synchronized (self) { |
8616 | for (Package *broken in broken_) { | |
8617 | [broken remove]; | |
8618 | ||
8619 | NSString *id = [broken id]; | |
8620 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.prerm", id] UTF8String]); | |
8621 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postrm", id] UTF8String]); | |
8622 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.preinst", id] UTF8String]); | |
8623 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postinst", id] UTF8String]); | |
8624 | } | |
7b0ce2da | 8625 | |
37d2b2a9 GP |
8626 | [self resolve]; |
8627 | [self perform]; | |
8628 | } | |
b5e7eebb | 8629 | } else if (button == [alert cancelButtonIndex]) { |
37d2b2a9 GP |
8630 | [broken_ removeAllObjects]; |
8631 | [self _loaded]; | |
7b0ce2da JF |
8632 | } |
8633 | ||
37d2b2a9 | 8634 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 8635 | } else if ([context isEqualToString:@"upgrade"]) { |
37d2b2a9 GP |
8636 | if (button == [alert firstOtherButtonIndex]) { |
8637 | @synchronized (self) { | |
8638 | for (Package *essential in essential_) | |
8639 | [essential install]; | |
7b0ce2da | 8640 | |
37d2b2a9 GP |
8641 | [self resolve]; |
8642 | [self perform]; | |
8643 | } | |
8644 | } else if (button == [alert firstOtherButtonIndex] + 1) { | |
8645 | [self distUpgrade]; | |
8646 | } else if (button == [alert cancelButtonIndex]) { | |
8647 | Ignored_ = YES; | |
7b0ce2da JF |
8648 | } |
8649 | ||
37d2b2a9 | 8650 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 8651 | } |
7b0ce2da JF |
8652 | } |
8653 | ||
670a0494 | 8654 | - (void) system:(NSString *)command { _pooled |
985d2dff | 8655 | _trace(); |
670a0494 | 8656 | system([command UTF8String]); |
985d2dff | 8657 | _trace(); |
670a0494 JF |
8658 | } |
8659 | ||
8660 | - (void) applicationWillSuspend { | |
8661 | [database_ clean]; | |
8662 | [super applicationWillSuspend]; | |
bd150f54 JF |
8663 | } |
8664 | ||
235f5487 | 8665 | - (BOOL) isSafeToSuspend { |
262afe11 GP |
8666 | // Use external process status API internally. |
8667 | // This is probably a really bad idea. | |
235f5487 | 8668 | // XXX: what is the point of this? does this solve anything at all? |
262afe11 GP |
8669 | uint64_t status = 0; |
8670 | int notify_token; | |
8671 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
8672 | notify_get_state(notify_token, &status); | |
8673 | notify_cancel(notify_token); | |
8674 | } | |
8675 | ||
235f5487 JF |
8676 | return locked_ == 0 && status == 0; |
8677 | } | |
8678 | ||
8679 | - (void) applicationSuspend:(__GSEvent *)event { | |
8680 | if ([self isSafeToSuspend]) | |
bd150f54 | 8681 | [super applicationSuspend:event]; |
bd150f54 JF |
8682 | } |
8683 | ||
6d9712c4 | 8684 | - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 { |
235f5487 | 8685 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
8686 | [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3]; |
8687 | } | |
8688 | ||
8689 | - (void) _setSuspended:(BOOL)value { | |
235f5487 | 8690 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
8691 | [super _setSuspended:value]; |
8692 | } | |
8693 | ||
7b0ce2da | 8694 | - (UIProgressHUD *) addProgressHUD { |
d061f4ba | 8695 | UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window_] autorelease]); |
04fe1349 JF |
8696 | [hud setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8697 | ||
d061f4ba | 8698 | [window_ setUserInteractionEnabled:NO]; |
7b0ce2da | 8699 | [hud show:YES]; |
534e31fc | 8700 | |
7585ce66 | 8701 | UIViewController *target = tabbar_; |
534e31fc GP |
8702 | while ([target modalViewController] != nil) target = [target modalViewController]; |
8703 | [[target view] addSubview:hud]; | |
8704 | ||
235f5487 | 8705 | ++locked_; |
7b0ce2da JF |
8706 | return hud; |
8707 | } | |
8708 | ||
d061f4ba JF |
8709 | - (void) removeProgressHUD:(UIProgressHUD *)hud { |
8710 | [hud show:NO]; | |
8711 | [hud removeFromSuperview]; | |
8712 | [window_ setUserInteractionEnabled:YES]; | |
235f5487 | 8713 | --locked_; |
d061f4ba JF |
8714 | } |
8715 | ||
9b623dac | 8716 | - (CYViewController *) pageForPackage:(NSString *)name { |
c390d3ab | 8717 | if (Package *package = [database_ packageWithName:name]) { |
f6e13561 | 8718 | CYPackageController *view = [[[CYPackageController alloc] initWithDatabase:database_] autorelease]; |
c390d3ab JF |
8719 | [view setPackage:package]; |
8720 | return view; | |
8721 | } else { | |
670a0494 JF |
8722 | NSURL *url([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"unknown" ofType:@"html"]]); |
8723 | url = [NSURL URLWithString:[[url absoluteString] stringByAppendingString:[NSString stringWithFormat:@"?%@", name]]]; | |
f6e13561 GP |
8724 | CYBrowserController *browser = [[[CYBrowserController alloc] init] autorelease]; |
8725 | [browser loadURL:url]; | |
8726 | return browser; | |
c390d3ab JF |
8727 | } |
8728 | } | |
8729 | ||
f6e13561 | 8730 | - (CYViewController *) pageForURL:(NSURL *)url { |
e47c4742 | 8731 | NSString *scheme([[url scheme] lowercaseString]); |
f6e13561 | 8732 | if ([[url absoluteString] length] <= [scheme length] + 3) |
e47c4742 | 8733 | return nil; |
f6e13561 GP |
8734 | NSString *path([[url absoluteString] substringFromIndex:[scheme length] + 3]); |
8735 | NSArray *components([path pathComponents]); | |
8736 | ||
f5a17517 | 8737 | if ([scheme isEqualToString:@"apptapp"] && [components count] > 0 && [[components objectAtIndex:0] isEqualToString:@"package"]) |
f6e13561 GP |
8738 | return [self pageForPackage:[components objectAtIndex:1]]; |
8739 | ||
8740 | if ([components count] < 1 || ![scheme isEqualToString:@"cydia"]) | |
e47c4742 | 8741 | return nil; |
f6e13561 GP |
8742 | |
8743 | NSString *base([components objectAtIndex:0]); | |
8744 | ||
f5a17517 GP |
8745 | CYViewController *controller = nil; |
8746 | ||
f70ea899 | 8747 | if ([base isEqualToString:@"url"]) { |
106d645f GP |
8748 | // This kind of URL can contain slashes in the argument, so we can't parse them below. |
8749 | NSString *destination = [[url absoluteString] substringFromIndex:([scheme length] + [@"://" length] + [base length] + [@"/" length])]; | |
f70ea899 | 8750 | controller = [[[CYBrowserController alloc] init] autorelease]; |
106d645f | 8751 | [(CYBrowserController *)controller loadURL:[NSURL URLWithString:destination]]; |
f70ea899 | 8752 | } else if ([components count] == 1) { |
f6e13561 | 8753 | if ([base isEqualToString:@"storage"]) { |
f5a17517 GP |
8754 | controller = [[[CYBrowserController alloc] init] autorelease]; |
8755 | [(CYBrowserController *)controller loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"storage" ofType:@"html"]]]; | |
f6e13561 GP |
8756 | } |
8757 | ||
8758 | if ([base isEqualToString:@"sources"]) { | |
f5a17517 | 8759 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
8760 | } |
8761 | ||
8762 | if ([base isEqualToString:@"home"]) { | |
f5a17517 | 8763 | controller = [[[HomeController alloc] init] autorelease]; |
f6e13561 GP |
8764 | } |
8765 | ||
8766 | if ([base isEqualToString:@"sections"]) { | |
f5a17517 | 8767 | controller = [[[SectionsController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
8768 | } |
8769 | ||
8770 | if ([base isEqualToString:@"search"]) { | |
f5a17517 | 8771 | controller = [[[SearchController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
8772 | } |
8773 | ||
8774 | if ([base isEqualToString:@"changes"]) { | |
ea3bb538 | 8775 | controller = [[[ChangesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
8776 | } |
8777 | ||
8778 | if ([base isEqualToString:@"installed"]) { | |
f5a17517 | 8779 | controller = [[[InstalledController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
8780 | } |
8781 | } else if ([components count] == 2) { | |
8782 | NSString *argument = [components objectAtIndex:1]; | |
8783 | ||
8784 | if ([base isEqualToString:@"package"]) { | |
f5a17517 | 8785 | controller = [self pageForPackage:argument]; |
f6e13561 GP |
8786 | } |
8787 | ||
8788 | if ([base isEqualToString:@"search"]) { | |
f5a17517 GP |
8789 | controller = [[[SearchController alloc] initWithDatabase:database_] autorelease]; |
8790 | [(SearchController *)controller setSearchTerm:argument]; | |
f6e13561 GP |
8791 | } |
8792 | ||
8793 | if ([base isEqualToString:@"sections"]) { | |
8794 | if ([argument isEqualToString:@"all"]) | |
8795 | argument = nil; | |
f5a17517 | 8796 | controller = [[[SectionController alloc] initWithDatabase:database_ section:argument] autorelease]; |
f6e13561 GP |
8797 | } |
8798 | ||
8799 | if ([base isEqualToString:@"sources"]) { | |
8800 | if ([argument isEqualToString:@"add"]) { | |
f5a17517 GP |
8801 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
8802 | [(SourcesController *)controller showAddSourcePrompt]; | |
f6e13561 GP |
8803 | } else { |
8804 | // XXX: Create page of the source specfified. | |
8805 | } | |
8806 | } | |
8807 | ||
f6e13561 GP |
8808 | if ([base isEqualToString:@"launch"]) { |
8809 | [self launchApplicationWithIdentifier:argument suspended:NO]; | |
f5a17517 | 8810 | return nil; |
f6e13561 GP |
8811 | } |
8812 | } else if ([components count] == 3) { | |
8813 | NSString *arg1 = [components objectAtIndex:1]; | |
8814 | NSString *arg2 = [components objectAtIndex:2]; | |
8815 | ||
8816 | if ([base isEqualToString:@"package"]) { | |
8817 | if ([arg2 isEqualToString:@"settings"]) { | |
f5a17517 | 8818 | controller = [[[PackageSettingsController alloc] initWithDatabase:database_ package:arg1] autorelease]; |
f6e13561 | 8819 | } else if ([arg2 isEqualToString:@"signature"]) { |
f5a17517 | 8820 | controller = [[[SignatureController alloc] initWithDatabase:database_ package:arg1] autorelease]; |
f6e13561 GP |
8821 | } else if ([arg2 isEqualToString:@"files"]) { |
8822 | if (Package *package = [database_ packageWithName:arg1]) { | |
f5a17517 GP |
8823 | controller = [[[FileTable alloc] initWithDatabase:database_] autorelease]; |
8824 | [(FileTable *)controller setPackage:package]; | |
f6e13561 GP |
8825 | } |
8826 | } | |
c390d3ab JF |
8827 | } |
8828 | } | |
8829 | ||
f5a17517 GP |
8830 | [controller setDelegate:self]; |
8831 | return controller; | |
c390d3ab JF |
8832 | } |
8833 | ||
40364973 | 8834 | - (BOOL) openCydiaURL:(NSURL *)url { |
f6e13561 | 8835 | CYViewController *page([self pageForURL:url]); |
40364973 | 8836 | |
c713af59 GP |
8837 | if (page != nil) { |
8838 | CYNavigationController *nav = [[[CYNavigationController alloc] init] autorelease]; | |
8839 | [nav setViewControllers:[NSArray arrayWithObject:page]]; | |
8840 | [tabbar_ setTransientViewController:nav]; | |
8841 | } | |
40364973 | 8842 | |
f6e13561 | 8843 | return page != nil; |
40364973 GP |
8844 | } |
8845 | ||
c390d3ab JF |
8846 | - (void) applicationOpenURL:(NSURL *)url { |
8847 | [super applicationOpenURL:url]; | |
d817e4de | 8848 | |
40364973 GP |
8849 | if (!loaded_) starturl_ = [url retain]; |
8850 | else [self openCydiaURL:url]; | |
c390d3ab JF |
8851 | } |
8852 | ||
bc11cf5b | 8853 | - (void) applicationWillResignActive:(UIApplication *)application { |
7eff7ea6 | 8854 | // Stop refreshing if you get a phone call or lock the device. |
7585ce66 JF |
8855 | if ([tabbar_ updating]) |
8856 | [tabbar_ cancelUpdate]; | |
bc11cf5b | 8857 | |
ca584c15 GP |
8858 | if ([[self superclass] instancesRespondToSelector:@selector(applicationWillResignActive:)]) |
8859 | [super applicationWillResignActive:application]; | |
7eff7ea6 GP |
8860 | } |
8861 | ||
f3e11d24 | 8862 | - (void) addStashController { |
235f5487 | 8863 | ++locked_; |
5829aea2 | 8864 | stash_ = [[StashController alloc] init]; |
f3e11d24 GP |
8865 | [window_ addSubview:[stash_ view]]; |
8866 | } | |
8867 | ||
8868 | - (void) removeStashController { | |
8869 | [[stash_ view] removeFromSuperview]; | |
8870 | [stash_ release]; | |
235f5487 | 8871 | --locked_; |
f3e11d24 GP |
8872 | } |
8873 | ||
8874 | - (void) stash { | |
8875 | [self setIdleTimerDisabled:YES]; | |
8876 | ||
9e1f1e91 | 8877 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; |
f3e11d24 GP |
8878 | [self setStatusBarShowsProgress:YES]; |
8879 | UpdateExternalStatus(1); | |
8880 | ||
8881 | [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/free.sh"]; | |
8882 | ||
8883 | UpdateExternalStatus(0); | |
8884 | [self setStatusBarShowsProgress:NO]; | |
8885 | ||
8886 | [self removeStashController]; | |
8887 | ||
8888 | if (ExecFork() == 0) { | |
8889 | execlp("launchctl", "launchctl", "stop", "com.apple.SpringBoard", NULL); | |
8890 | perror("launchctl stop"); | |
8891 | } | |
8892 | } | |
8893 | ||
f6e13561 | 8894 | - (void) setupViewControllers { |
851f4a99 GP |
8895 | tabbar_ = [[CYTabBarController alloc] initWithDatabase:database_]; |
8896 | [tabbar_ setDelegate:self]; | |
8897 | ||
8898 | NSMutableArray *items([NSMutableArray arrayWithObjects: | |
89571a5b GP |
8899 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage applicationImageNamed:@"home.png"] tag:0] autorelease], |
8900 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SECTIONS") image:[UIImage applicationImageNamed:@"install.png"] tag:0] autorelease], | |
8901 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage applicationImageNamed:@"changes.png"] tag:0] autorelease], | |
8902 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage applicationImageNamed:@"search.png"] tag:0] autorelease], | |
f6e13561 GP |
8903 | nil]); |
8904 | ||
851f4a99 | 8905 | if (IsWildcat_) { |
89571a5b GP |
8906 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage applicationImageNamed:@"source.png"] tag:0] autorelease] atIndex:3]; |
8907 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage applicationImageNamed:@"manage.png"] tag:0] autorelease] atIndex:3]; | |
851f4a99 | 8908 | } else { |
89571a5b | 8909 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("MANAGE") image:[UIImage applicationImageNamed:@"manage.png"] tag:0] autorelease] atIndex:3]; |
851f4a99 GP |
8910 | } |
8911 | ||
8912 | NSMutableArray *controllers([NSMutableArray array]); | |
851f4a99 GP |
8913 | for (UITabBarItem *item in items) { |
8914 | CYNavigationController *controller([[[CYNavigationController alloc] initWithDatabase:database_] autorelease]); | |
8915 | [controller setTabBarItem:item]; | |
8916 | [controllers addObject:controller]; | |
8917 | } | |
851f4a99 | 8918 | [tabbar_ setViewControllers:controllers]; |
4305896c | 8919 | |
f6e13561 | 8920 | [tabbar_ setUpdateDelegate:self]; |
851f4a99 GP |
8921 | } |
8922 | ||
f6e13561 GP |
8923 | - (void)showEmulatedLoadingControllerInView:(UIView *)view { |
8924 | static CYEmulatedLoadingController *fake = [[CYEmulatedLoadingController alloc] init]; | |
4fe4bdc3 | 8925 | if (view != nil) { |
f6e13561 | 8926 | [view addSubview:[fake view]]; |
4fe4bdc3 | 8927 | } else { |
f6e13561 | 8928 | [[fake view] removeFromSuperview]; |
4fe4bdc3 GP |
8929 | } |
8930 | } | |
8931 | ||
bd150f54 | 8932 | - (void) applicationDidFinishLaunching:(id)unused { |
7e30ba6d | 8933 | _trace(); |
54043703 JF |
8934 | CydiaApp = self; |
8935 | ||
1e94d48b JF |
8936 | if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) |
8937 | [self setApplicationSupportsShakeToEdit:NO]; | |
8938 | ||
71cc7be1 JF |
8939 | [NSURLCache setSharedURLCache:[[[SDURLCache alloc] |
8940 | initWithMemoryCapacity:524288 | |
8941 | diskCapacity:10485760 | |
8942 | diskPath:[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia/SDURLCache", @"/var/root"] | |
8943 | ] autorelease]]; | |
8944 | ||
b5e7eebb | 8945 | [CYBrowserController _initialize]; |
ea173384 | 8946 | |
bfc87a4d JF |
8947 | [NSURLProtocol registerClass:[CydiaURLProtocol class]]; |
8948 | ||
f641a0e5 JF |
8949 | Font12_ = [[UIFont systemFontOfSize:12] retain]; |
8950 | Font12Bold_ = [[UIFont boldSystemFontOfSize:12] retain]; | |
8951 | Font14_ = [[UIFont systemFontOfSize:14] retain]; | |
8952 | Font18Bold_ = [[UIFont boldSystemFontOfSize:18] retain]; | |
8953 | Font22Bold_ = [[UIFont boldSystemFontOfSize:22] retain]; | |
8954 | ||
bd150f54 JF |
8955 | essential_ = [[NSMutableArray alloc] initWithCapacity:4]; |
8956 | broken_ = [[NSMutableArray alloc] initWithCapacity:4]; | |
8957 | ||
4fe4bdc3 | 8958 | window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
f641a0e5 JF |
8959 | [window_ orderFront:self]; |
8960 | [window_ makeKey:self]; | |
c390d3ab | 8961 | [window_ setHidden:NO]; |
04fe1349 | 8962 | |
f3e11d24 GP |
8963 | if ( |
8964 | readlink("/Applications", NULL, 0) == -1 && errno == EINVAL || | |
8965 | readlink("/Library/Ringtones", NULL, 0) == -1 && errno == EINVAL || | |
8966 | readlink("/Library/Wallpaper", NULL, 0) == -1 && errno == EINVAL || | |
8967 | //readlink("/usr/bin", NULL, 0) == -1 && errno == EINVAL || | |
8968 | readlink("/usr/include", NULL, 0) == -1 && errno == EINVAL || | |
8969 | readlink("/usr/lib/pam", NULL, 0) == -1 && errno == EINVAL || | |
8970 | readlink("/usr/libexec", NULL, 0) == -1 && errno == EINVAL || | |
8971 | readlink("/usr/share", NULL, 0) == -1 && errno == EINVAL || | |
8972 | //readlink("/var/lib", NULL, 0) == -1 && errno == EINVAL || | |
8973 | false | |
8974 | ) { | |
8975 | [self addStashController]; | |
3931b718 JF |
8976 | // XXX: this would be much cleaner as a yieldToSelector: |
8977 | // that way the removeStashController could happen right here inline | |
8978 | // we also could no longer require the useless stash_ field anymore | |
f3e11d24 GP |
8979 | [self performSelector:@selector(stash) withObject:nil afterDelay:0]; |
8980 | return; | |
8981 | } | |
8982 | ||
770f2a8e | 8983 | database_ = [Database sharedInstance]; |
bfc87a4d | 8984 | |
89571a5b | 8985 | [window_ setUserInteractionEnabled:NO]; |
0be165c8 | 8986 | [self setupViewControllers]; |
f6e13561 | 8987 | [self showEmulatedLoadingControllerInView:window_]; |
5ccb47d8 | 8988 | |
fed0d010 | 8989 | [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; |
c626a63f | 8990 | _trace(); |
fed0d010 JF |
8991 | } |
8992 | ||
8993 | - (void) loadData { | |
c626a63f | 8994 | _trace(); |
fed0d010 JF |
8995 | if (Role_ == nil) { |
8996 | [self showSettings]; | |
8997 | return; | |
8998 | } | |
8999 | ||
851f4a99 | 9000 | [self reloadData]; |
670a0494 | 9001 | PrintTimes(); |
5ccb47d8 | 9002 | |
293c95d6 | 9003 | [window_ addSubview:[tabbar_ view]]; |
f6e13561 | 9004 | [self showEmulatedLoadingControllerInView:nil]; |
851f4a99 | 9005 | [window_ setUserInteractionEnabled:YES]; |
5ccb47d8 | 9006 | |
89571a5b GP |
9007 | // Show the home page. |
9008 | CYNavigationController *navigation = [[tabbar_ viewControllers] objectAtIndex:0]; | |
f5a17517 | 9009 | [navigation setViewControllers:[NSArray arrayWithObject:[self pageForURL:[NSURL URLWithString:@"cydia://home"]]]]; |
f6e13561 | 9010 | |
89571a5b | 9011 | // (Try to) show the startup URL. |
f6e13561 GP |
9012 | if (starturl_ != nil) { |
9013 | [self openCydiaURL:starturl_]; | |
9014 | [starturl_ release]; | |
9015 | starturl_ = nil; | |
9016 | } | |
bd150f54 JF |
9017 | } |
9018 | ||
b5e7eebb | 9019 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { |
674dce72 | 9020 | if (item != nil && IsWildcat_) { |
b5e7eebb | 9021 | [sheet showFromBarButtonItem:item animated:YES]; |
674dce72 GP |
9022 | } else { |
9023 | [sheet showInView:window_]; | |
9024 | } | |
36bb2ca2 JF |
9025 | } |
9026 | ||
dc5812ec JF |
9027 | @end |
9028 | ||
b4d89997 JF |
9029 | /*IMP alloc_; |
9030 | id Alloc_(id self, SEL selector) { | |
9031 | id object = alloc_(self, selector); | |
c390d3ab | 9032 | lprintf("[%s]A-%p\n", self->isa->name, object); |
b4d89997 JF |
9033 | return object; |
9034 | }*/ | |
9035 | ||
36bb2ca2 JF |
9036 | /*IMP dealloc_; |
9037 | id Dealloc_(id self, SEL selector) { | |
9038 | id object = dealloc_(self, selector); | |
c390d3ab | 9039 | lprintf("[%s]D-%p\n", self->isa->name, object); |
36bb2ca2 JF |
9040 | return object; |
9041 | }*/ | |
b4d89997 | 9042 | |
f79a4512 JF |
9043 | Class $WebDefaultUIKitDelegate; |
9044 | ||
d791dce4 | 9045 | MSHook(void, UIWebDocumentView$_setUIKitDelegate$, UIWebDocumentView *self, SEL _cmd, id delegate) { |
f79a4512 JF |
9046 | if (delegate == nil && $WebDefaultUIKitDelegate != nil) |
9047 | delegate = [$WebDefaultUIKitDelegate sharedUIKitDelegate]; | |
d791dce4 | 9048 | return _UIWebDocumentView$_setUIKitDelegate$(self, _cmd, delegate); |
f79a4512 JF |
9049 | } |
9050 | ||
10d65752 RP |
9051 | static NSNumber *shouldPlayKeyboardSounds; |
9052 | ||
9053 | Class $UIHardware; | |
9054 | ||
b72246b0 JF |
9055 | MSHook(void, UIHardware$_playSystemSound$, Class self, SEL _cmd, int sound) { |
9056 | switch (sound) { | |
10d65752 RP |
9057 | case 1104: // Keyboard Button Clicked |
9058 | case 1105: // Keyboard Delete Repeated | |
b72246b0 JF |
9059 | if (shouldPlayKeyboardSounds == nil) { |
9060 | NSDictionary *dict([[[NSDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.apple.preferences.sounds.plist"] autorelease]); | |
9061 | shouldPlayKeyboardSounds = [([dict objectForKey:@"keyboard"] ?: (id) kCFBooleanTrue) retain]; | |
10d65752 | 9062 | } |
b72246b0 | 9063 | |
10d65752 RP |
9064 | if (![shouldPlayKeyboardSounds boolValue]) |
9065 | break; | |
b72246b0 | 9066 | |
10d65752 | 9067 | default: |
b72246b0 | 9068 | _UIHardware$_playSystemSound$(self, _cmd, sound); |
10d65752 RP |
9069 | } |
9070 | } | |
9071 | ||
affeffc7 | 9072 | int main(int argc, char *argv[]) { _pooled |
d6dad1b4 | 9073 | _trace(); |
f79a4512 | 9074 | |
01d93940 JF |
9075 | if (Class $UIDevice = objc_getClass("UIDevice")) { |
9076 | UIDevice *device([$UIDevice currentDevice]); | |
9077 | IsWildcat_ = [device respondsToSelector:@selector(isWildcat)] && [device isWildcat]; | |
9078 | } else | |
9079 | IsWildcat_ = false; | |
9080 | ||
df213583 | 9081 | PackageName = reinterpret_cast<CYString &(*)(Package *, SEL)>(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); |
677b8415 | 9082 | |
7376b55c JF |
9083 | /* Library Hacks {{{ */ |
9084 | class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); | |
9085 | ||
9086 | $WebDefaultUIKitDelegate = objc_getClass("WebDefaultUIKitDelegate"); | |
9087 | Method UIWebDocumentView$_setUIKitDelegate$(class_getInstanceMethod([WebView class], @selector(_setUIKitDelegate:))); | |
9088 | if (UIWebDocumentView$_setUIKitDelegate$ != NULL) { | |
9089 | _UIWebDocumentView$_setUIKitDelegate$ = reinterpret_cast<void (*)(UIWebDocumentView *, SEL, id)>(method_getImplementation(UIWebDocumentView$_setUIKitDelegate$)); | |
9090 | method_setImplementation(UIWebDocumentView$_setUIKitDelegate$, reinterpret_cast<IMP>(&$UIWebDocumentView$_setUIKitDelegate$)); | |
9091 | } | |
10d65752 RP |
9092 | |
9093 | $UIHardware = objc_getClass("UIHardware"); | |
9094 | Method UIHardware$_playSystemSound$(class_getClassMethod($UIHardware, @selector(_playSystemSound:))); | |
9095 | if (UIHardware$_playSystemSound$ != NULL) { | |
9096 | _UIHardware$_playSystemSound$ = reinterpret_cast<void (*)(Class, SEL, int)>(method_getImplementation(UIHardware$_playSystemSound$)); | |
9097 | method_setImplementation(UIHardware$_playSystemSound$, reinterpret_cast<IMP>(&$UIHardware$_playSystemSound$)); | |
9098 | } | |
7376b55c JF |
9099 | /* }}} */ |
9100 | /* Set Locale {{{ */ | |
f79a4512 | 9101 | Locale_ = CFLocaleCopyCurrent(); |
b1ce61ec JF |
9102 | Languages_ = [NSLocale preferredLanguages]; |
9103 | //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); | |
18876387 | 9104 | //NSLog(@"%@", [Languages_ description]); |
78430d06 | 9105 | |
b1ce61ec JF |
9106 | const char *lang; |
9107 | if (Languages_ == nil || [Languages_ count] == 0) | |
78430d06 | 9108 | // XXX: consider just setting to C and then falling through? |
b1ce61ec | 9109 | lang = NULL; |
78430d06 | 9110 | else { |
b1ce61ec | 9111 | lang = [[Languages_ objectAtIndex:0] UTF8String]; |
78430d06 JF |
9112 | setenv("LANG", lang, true); |
9113 | } | |
9114 | ||
b1ce61ec JF |
9115 | //std::setlocale(LC_ALL, lang); |
9116 | NSLog(@"Setting Language: %s", lang); | |
7376b55c | 9117 | /* }}} */ |
f79a4512 | 9118 | |
d791dce4 | 9119 | apr_app_initialize(&argc, const_cast<const char * const **>(&argv), NULL); |
f79a4512 | 9120 | |
7376b55c | 9121 | /* Parse Arguments {{{ */ |
de3b1ab4 JF |
9122 | bool substrate(false); |
9123 | ||
9124 | if (argc != 0) { | |
9125 | char **args(argv); | |
9126 | int arge(1); | |
9127 | ||
9128 | for (int argi(1); argi != argc; ++argi) | |
9129 | if (strcmp(argv[argi], "--") == 0) { | |
9130 | arge = argi; | |
9131 | argv[argi] = argv[0]; | |
9132 | argv += argi; | |
9133 | argc -= argi; | |
9134 | break; | |
9135 | } | |
9136 | ||
9137 | for (int argi(1); argi != arge; ++argi) | |
d791dce4 | 9138 | if (strcmp(args[argi], "--substrate") == 0) |
de3b1ab4 JF |
9139 | substrate = true; |
9140 | else | |
9141 | fprintf(stderr, "unknown argument: %s\n", args[argi]); | |
9142 | } | |
7376b55c | 9143 | /* }}} */ |
d73cede2 | 9144 | |
7376b55c JF |
9145 | App_ = [[NSBundle mainBundle] bundlePath]; |
9146 | Home_ = NSHomeDirectory(); | |
d791dce4 | 9147 | Advanced_ = YES; |
7376b55c | 9148 | |
b4d89997 JF |
9149 | setuid(0); |
9150 | setgid(0); | |
9151 | ||
9152 | /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); | |
9153 | alloc_ = alloc->method_imp; | |
9154 | alloc->method_imp = (IMP) &Alloc_;*/ | |
9155 | ||
36bb2ca2 JF |
9156 | /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc)); |
9157 | dealloc_ = dealloc->method_imp; | |
9158 | dealloc->method_imp = (IMP) &Dealloc_;*/ | |
9159 | ||
d791dce4 | 9160 | /* System Information {{{ */ |
3178d79b | 9161 | size_t size; |
c390d3ab JF |
9162 | |
9163 | int maxproc; | |
9164 | size = sizeof(maxproc); | |
9165 | if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) | |
9166 | perror("sysctlbyname(\"kern.maxproc\", ?)"); | |
9167 | else if (maxproc < 64) { | |
9168 | maxproc = 64; | |
9169 | if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) | |
9170 | perror("sysctlbyname(\"kern.maxproc\", #)"); | |
9171 | } | |
9172 | ||
bfc87a4d JF |
9173 | sysctlbyname("kern.osversion", NULL, &size, NULL, 0); |
9174 | char *osversion = new char[size]; | |
9175 | if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1) | |
9176 | perror("sysctlbyname(\"kern.osversion\", ?)"); | |
9177 | else | |
9178 | System_ = [NSString stringWithUTF8String:osversion]; | |
9179 | ||
3178d79b JF |
9180 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
9181 | char *machine = new char[size]; | |
c390d3ab JF |
9182 | if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1) |
9183 | perror("sysctlbyname(\"hw.machine\", ?)"); | |
9184 | else | |
9185 | Machine_ = machine; | |
3178d79b | 9186 | |
59dbe296 JF |
9187 | if (CFMutableDictionaryRef dict = IOServiceMatching("IOPlatformExpertDevice")) { |
9188 | if (io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, dict)) { | |
9189 | if (CFTypeRef serial = IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0)) { | |
bfc87a4d | 9190 | SerialNumber_ = [NSString stringWithString:(NSString *)serial]; |
59dbe296 JF |
9191 | CFRelease(serial); |
9192 | } | |
9193 | ||
9194 | if (CFTypeRef ecid = IORegistryEntrySearchCFProperty(service, kIODeviceTreePlane, CFSTR("unique-chip-id"), kCFAllocatorDefault, kIORegistryIterateRecursively)) { | |
9195 | NSData *data((NSData *) ecid); | |
9196 | size_t length([data length]); | |
9197 | uint8_t bytes[length]; | |
9198 | [data getBytes:bytes]; | |
9199 | char string[length * 2 + 1]; | |
9200 | for (size_t i(0); i != length; ++i) | |
9201 | sprintf(string + i * 2, "%.2X", bytes[length - i - 1]); | |
bfc87a4d | 9202 | ChipID_ = [NSString stringWithUTF8String:string]; |
59dbe296 JF |
9203 | CFRelease(ecid); |
9204 | } | |
9205 | ||
9206 | IOObjectRelease(service); | |
9207 | } | |
9208 | } | |
9209 | ||
87f46a96 | 9210 | UniqueID_ = [[UIDevice currentDevice] uniqueIdentifier]; |
3178d79b | 9211 | |
567e3972 JF |
9212 | CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef); |
9213 | $CTSIMSupportCopyMobileSubscriberCountryCode = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode")); | |
9214 | CFStringRef mcc($CTSIMSupportCopyMobileSubscriberCountryCode == NULL ? NULL : (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault)); | |
9215 | ||
9216 | CFStringRef (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(CFAllocatorRef); | |
9217 | $CTSIMSupportCopyMobileSubscriberNetworkCode = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode")); | |
9218 | CFStringRef mnc($CTSIMSupportCopyMobileSubscriberNetworkCode == NULL ? NULL : (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(kCFAllocatorDefault)); | |
9219 | ||
9220 | if (mcc != NULL && mnc != NULL) | |
9221 | PLMN_ = [NSString stringWithFormat:@"%@%@", mcc, mnc]; | |
9222 | ||
9223 | if (mnc != NULL) | |
9224 | CFRelease(mnc); | |
9225 | if (mcc != NULL) | |
9226 | CFRelease(mcc); | |
9227 | ||
ad5d065e JF |
9228 | if (NSDictionary *system = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]) |
9229 | Build_ = [system objectForKey:@"ProductBuildVersion"]; | |
3e9c9e85 JF |
9230 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) { |
9231 | Product_ = [info objectForKey:@"SafariProductVersion"]; | |
9232 | Safari_ = [info objectForKey:@"CFBundleVersion"]; | |
9233 | } | |
d791dce4 | 9234 | /* }}} */ |
7376b55c | 9235 | /* Load Database {{{ */ |
d6dad1b4 | 9236 | _trace(); |
f79a4512 JF |
9237 | Metadata_ = [[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]; |
9238 | _trace(); | |
9239 | SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; | |
d6dad1b4 JF |
9240 | |
9241 | if (Metadata_ == NULL) | |
f79a4512 | 9242 | Metadata_ = [NSMutableDictionary dictionaryWithCapacity:2]; |
6d9712c4 | 9243 | else { |
2bdd73bd | 9244 | Settings_ = [Metadata_ objectForKey:@"Settings"]; |
7b0ce2da | 9245 | |
b4d89997 | 9246 | Packages_ = [Metadata_ objectForKey:@"Packages"]; |
6d9712c4 | 9247 | Sections_ = [Metadata_ objectForKey:@"Sections"]; |
7b0ce2da | 9248 | Sources_ = [Metadata_ objectForKey:@"Sources"]; |
ef055c6c JF |
9249 | |
9250 | Token_ = [Metadata_ objectForKey:@"Token"]; | |
7b0ce2da JF |
9251 | } |
9252 | ||
9253 | if (Settings_ != nil) | |
9254 | Role_ = [Settings_ objectForKey:@"Role"]; | |
9255 | ||
7b0ce2da JF |
9256 | if (Sections_ == nil) { |
9257 | Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; | |
9258 | [Metadata_ setObject:Sections_ forKey:@"Sections"]; | |
9259 | } | |
9260 | ||
9261 | if (Sources_ == nil) { | |
9262 | Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; | |
9263 | [Metadata_ setObject:Sources_ forKey:@"Sources"]; | |
6d9712c4 | 9264 | } |
7376b55c | 9265 | /* }}} */ |
b4d89997 | 9266 | |
94b0b3e5 JF |
9267 | _trace(); |
9268 | MetaFile_.Open("/var/lib/cydia/metadata.cb0"); | |
9269 | _trace(); | |
9270 | ||
9271 | if (Packages_ != nil) { | |
9272 | CFDictionaryApplyFunction((CFDictionaryRef) Packages_, &PackageImport, NULL); | |
9273 | _trace(); | |
9274 | [Metadata_ removeObjectForKey:@"Packages"]; | |
9275 | Packages_ = nil; | |
9276 | Changed_ = true; | |
9277 | } | |
9278 | ||
d791dce4 JF |
9279 | Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; |
9280 | ||
01d93940 JF |
9281 | if (substrate && access("/Library/MobileSubstrate/DynamicLibraries/SimulatedKeyEvents.dylib", F_OK) == 0) |
9282 | dlopen("/Library/MobileSubstrate/DynamicLibraries/SimulatedKeyEvents.dylib", RTLD_LAZY | RTLD_GLOBAL); | |
9dd60d81 JF |
9283 | if (substrate && access("/Applications/WinterBoard.app/WinterBoard.dylib", F_OK) == 0) |
9284 | dlopen("/Applications/WinterBoard.app/WinterBoard.dylib", RTLD_LAZY | RTLD_GLOBAL); | |
9285 | /*if (substrate && access("/Library/MobileSubstrate/MobileSubstrate.dylib", F_OK) == 0) | |
9286 | dlopen("/Library/MobileSubstrate/MobileSubstrate.dylib", RTLD_LAZY | RTLD_GLOBAL);*/ | |
dddbc481 | 9287 | |
01d93940 JF |
9288 | int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); |
9289 | ||
3b0f10b0 | 9290 | if (access("/tmp/.cydia.fw", F_OK) == 0) { |
ea173384 | 9291 | unlink("/tmp/.cydia.fw"); |
3b0f10b0 | 9292 | goto firmware; |
77801ff1 | 9293 | } else if (access("/User", F_OK) != 0 || version < 2) { |
3b0f10b0 | 9294 | firmware: |
d6dad1b4 | 9295 | _trace(); |
26c2dd8c | 9296 | system("/usr/libexec/cydia/firmware.sh"); |
d6dad1b4 JF |
9297 | _trace(); |
9298 | } | |
9e98e020 | 9299 | |
87f46a96 JF |
9300 | _assert([[NSFileManager defaultManager] |
9301 | createDirectoryAtPath:@"/var/cache/apt/archives/partial" | |
9302 | withIntermediateDirectories:YES | |
9303 | attributes:nil | |
9304 | error:NULL | |
9305 | ]); | |
9306 | ||
7376b55c JF |
9307 | if (access("/tmp/cydia.chk", F_OK) == 0) { |
9308 | if (unlink("/var/cache/apt/pkgcache.bin") == -1) | |
9309 | _assert(errno == ENOENT); | |
9310 | if (unlink("/var/cache/apt/srcpkgcache.bin") == -1) | |
9311 | _assert(errno == ENOENT); | |
9312 | } | |
9313 | ||
59dbe296 | 9314 | /* APT Initialization {{{ */ |
b1ce61ec JF |
9315 | _assert(pkgInitConfig(*_config)); |
9316 | _assert(pkgInitSystem(*_config, _system)); | |
9317 | ||
9318 | if (lang != NULL) | |
9319 | _config->Set("APT::Acquire::Translation", lang); | |
cb94ff21 JF |
9320 | |
9321 | // XXX: this timeout might be important :( | |
9322 | //_config->Set("Acquire::http::Timeout", 15); | |
9323 | ||
7623f855 | 9324 | _config->Set("Acquire::http::MaxParallel", 3); |
59dbe296 | 9325 | /* }}} */ |
7376b55c | 9326 | /* Color Choices {{{ */ |
36bb2ca2 JF |
9327 | space_ = CGColorSpaceCreateDeviceRGB(); |
9328 | ||
f641a0e5 | 9329 | Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0); |
77fcccaf | 9330 | Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0); |
36bb2ca2 | 9331 | Black_.Set(space_, 0.0, 0.0, 0.0, 1.0); |
baf80942 | 9332 | Off_.Set(space_, 0.9, 0.9, 0.9, 1.0); |
36bb2ca2 | 9333 | White_.Set(space_, 1.0, 1.0, 1.0, 1.0); |
7b0ce2da | 9334 | Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0); |
3bd1c2a2 JF |
9335 | Green_.Set(space_, 0.0, 0.5, 0.0, 1.0); |
9336 | Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0); | |
9337 | Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0); | |
59dbe296 | 9338 | |
dc63e78f JF |
9339 | InstallingColor_ = [UIColor colorWithRed:0.88f green:1.00f blue:0.88f alpha:1.00f]; |
9340 | RemovingColor_ = [UIColor colorWithRed:1.00f green:0.88f blue:0.88f alpha:1.00f]; | |
7376b55c | 9341 | /* }}}*/ |
7376b55c | 9342 | /* UIKit Configuration {{{ */ |
f79a4512 JF |
9343 | void (*$GSFontSetUseLegacyFontMetrics)(BOOL)(reinterpret_cast<void (*)(BOOL)>(dlsym(RTLD_DEFAULT, "GSFontSetUseLegacyFontMetrics"))); |
9344 | if ($GSFontSetUseLegacyFontMetrics != NULL) | |
9345 | $GSFontSetUseLegacyFontMetrics(YES); | |
7b0ce2da | 9346 | |
600d005d JF |
9347 | // XXX: I have a feeling this was important |
9348 | //UIKeyboardDisableAutomaticAppearance(); | |
7376b55c | 9349 | /* }}} */ |
87f46a96 | 9350 | |
670a0494 | 9351 | Colon_ = UCLocalize("COLON_DELIMITED"); |
72fb3616 | 9352 | Elision_ = UCLocalize("ELISION"); |
670a0494 JF |
9353 | Error_ = UCLocalize("ERROR"); |
9354 | Warning_ = UCLocalize("WARNING"); | |
9355 | ||
d6dad1b4 | 9356 | _trace(); |
77df4f82 | 9357 | int value(UIApplicationMain(argc, argv, @"Cydia", @"Cydia")); |
36bb2ca2 JF |
9358 | |
9359 | CGColorSpaceRelease(space_); | |
199d0ba5 | 9360 | CFRelease(Locale_); |
36bb2ca2 | 9361 | |
36bb2ca2 | 9362 | return value; |
6d166849 | 9363 | } |