]>
Commit | Line | Data |
---|---|---|
e057ec05 | 1 | /* Cydia - iPhone UIKit Front-End for Debian APT |
daf7f6e2 | 2 | * Copyright (C) 2008-2010 Jay Freeman (saurik) |
e057ec05 JF |
3 | */ |
4 | ||
017b2b71 | 5 | /* Modified BSD License {{{ */ |
e057ec05 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 | */ | |
017b2b71 | 38 | /* }}} */ |
e057ec05 | 39 | |
83105e6e JF |
40 | // XXX: wtf/FastMalloc.h... wtf? |
41 | #define USE_SYSTEM_MALLOC 1 | |
42 | ||
a75f53e7 | 43 | /* #include Directives {{{ */ |
1e4922b8 JF |
44 | #include "UICaboodle/UCPlatform.h" |
45 | #include "UICaboodle/UCLocalize.h" | |
f159ecd4 | 46 | |
59efd93a JF |
47 | #include <objc/objc.h> |
48 | #include <objc/runtime.h> | |
49 | ||
b0d03ade | 50 | #include <CoreGraphics/CoreGraphics.h> |
b0d03ade | 51 | #include <Foundation/Foundation.h> |
853d14d3 | 52 | |
66abcbb0 JF |
53 | #if 0 |
54 | #define DEPLOYMENT_TARGET_MACOSX 1 | |
55 | #define CF_BUILDING_CF 1 | |
56 | #include <CoreFoundation/CFInternal.h> | |
57 | #endif | |
58 | ||
43b742af JF |
59 | #include <CoreFoundation/CFPriv.h> |
60 | #include <CoreFoundation/CFUniChar.h> | |
61 | ||
d7a235a6 JF |
62 | #include <SystemConfiguration/SystemConfiguration.h> |
63 | ||
1e4922b8 JF |
64 | #include <UIKit/UIKit.h> |
65 | #include "iPhonePrivate.h" | |
66 | ||
67 | #include <IOKit/IOKitLib.h> | |
d210b85d | 68 | |
6932575e | 69 | #include <WebCore/WebCoreThread.h> |
77f175ac | 70 | |
43b742af | 71 | #include <algorithm> |
3bddda52 | 72 | #include <iomanip> |
4941f41d | 73 | #include <sstream> |
8993ad57 JF |
74 | #include <string> |
75 | ||
4941f41d JF |
76 | #include <ext/stdio_filebuf.h> |
77 | ||
7805b429 JF |
78 | #undef ABS |
79 | ||
a75f53e7 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> | |
d72d91aa | 84 | #include <apt-pkg/clean.h> |
a75f53e7 | 85 | #include <apt-pkg/configuration.h> |
3e3977a2 | 86 | #include <apt-pkg/debindexfile.h> |
2d28b35a | 87 | #include <apt-pkg/debmetaindex.h> |
a75f53e7 JF |
88 | #include <apt-pkg/error.h> |
89 | #include <apt-pkg/init.h> | |
1eb0c554 | 90 | #include <apt-pkg/mmap.h> |
a75f53e7 | 91 | #include <apt-pkg/pkgrecords.h> |
1eb0c554 | 92 | #include <apt-pkg/sha1.h> |
a75f53e7 | 93 | #include <apt-pkg/sourcelist.h> |
4941f41d | 94 | #include <apt-pkg/sptr.h> |
f464053e | 95 | #include <apt-pkg/strutl.h> |
b8b1cfd0 | 96 | #include <apt-pkg/tagfile.h> |
a75f53e7 | 97 | |
6932575e JF |
98 | #include <apr-1/apr_pools.h> |
99 | ||
2cb68ddf JF |
100 | #include <sys/types.h> |
101 | #include <sys/stat.h> | |
2d28b35a | 102 | #include <sys/sysctl.h> |
65fe894c JF |
103 | #include <sys/param.h> |
104 | #include <sys/mount.h> | |
2cb68ddf | 105 | |
1e4922b8 | 106 | #include <fcntl.h> |
fc675b93 | 107 | #include <notify.h> |
1eb0c554 | 108 | #include <dlfcn.h> |
2d28b35a | 109 | |
686e302f JF |
110 | extern "C" { |
111 | #include <mach-o/nlist.h> | |
112 | } | |
113 | ||
8993ad57 JF |
114 | #include <cstdio> |
115 | #include <cstdlib> | |
116 | #include <cstring> | |
686e302f | 117 | |
4941f41d | 118 | #include <errno.h> |
a75f53e7 | 119 | #include <pcre.h> |
d210b85d | 120 | |
7a8635c9 JF |
121 | #include <Cytore.hpp> |
122 | ||
1e4922b8 | 123 | #include "UICaboodle/BrowserView.h" |
2bfeaf6f | 124 | |
1e4922b8 | 125 | #include "substrate.h" |
9ae52960 GP |
126 | /* }}} */ |
127 | ||
017b2b71 | 128 | /* Profiler {{{ */ |
f159ecd4 JF |
129 | struct timeval _ltv; |
130 | bool _itv; | |
131 | ||
e2a207dd JF |
132 | #define _timestamp ({ \ |
133 | struct timeval tv; \ | |
134 | gettimeofday(&tv, NULL); \ | |
135 | tv.tv_sec * 1000000 + tv.tv_usec; \ | |
136 | }) | |
137 | ||
3bddda52 JF |
138 | typedef std::vector<class ProfileTime *> TimeList; |
139 | TimeList times_; | |
140 | ||
141 | class ProfileTime { | |
142 | private: | |
143 | const char *name_; | |
144 | uint64_t total_; | |
142bd2db | 145 | uint64_t count_; |
3bddda52 JF |
146 | |
147 | public: | |
148 | ProfileTime(const char *name) : | |
149 | name_(name), | |
150 | total_(0) | |
151 | { | |
152 | times_.push_back(this); | |
153 | } | |
154 | ||
155 | void AddTime(uint64_t time) { | |
156 | total_ += time; | |
142bd2db | 157 | ++count_; |
3bddda52 JF |
158 | } |
159 | ||
160 | void Print() { | |
161 | if (total_ != 0) | |
142bd2db | 162 | std::cerr << std::setw(5) << count_ << ", " << std::setw(7) << total_ << " : " << name_ << std::endl; |
3bddda52 | 163 | total_ = 0; |
142bd2db | 164 | count_ = 0; |
3bddda52 JF |
165 | } |
166 | }; | |
167 | ||
168 | class ProfileTimer { | |
169 | private: | |
170 | ProfileTime &time_; | |
171 | uint64_t start_; | |
172 | ||
173 | public: | |
174 | ProfileTimer(ProfileTime &time) : | |
175 | time_(time), | |
176 | start_(_timestamp) | |
177 | { | |
178 | } | |
179 | ||
180 | ~ProfileTimer() { | |
181 | time_.AddTime(_timestamp - start_); | |
182 | } | |
183 | }; | |
184 | ||
185 | void PrintTimes() { | |
186 | for (TimeList::const_iterator i(times_.begin()); i != times_.end(); ++i) | |
187 | (*i)->Print(); | |
188 | std::cerr << "========" << std::endl; | |
189 | } | |
190 | ||
191 | #define _profile(name) { \ | |
192 | static ProfileTime name(#name); \ | |
193 | ProfileTimer _ ## name(name); | |
194 | ||
195 | #define _end } | |
6932575e | 196 | /* }}} */ |
f464053e JF |
197 | |
198 | #define _pooled _H<NSAutoreleasePool> _pool([[NSAutoreleasePool alloc] init], true); | |
199 | ||
7831584c JF |
200 | #define CYPoolStart() \ |
201 | NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \ | |
202 | do | |
203 | #define CYPoolEnd() \ | |
204 | while (false); \ | |
205 | [_pool release]; | |
206 | ||
7a8635c9 JF |
207 | // Hash Functions/Structures {{{ |
208 | extern "C" uint32_t hashlittle(const void *key, size_t length, uint32_t initval = 0); | |
209 | ||
210 | union SplitHash { | |
211 | uint32_t u32; | |
212 | uint16_t u16[2]; | |
213 | }; | |
214 | // }}} | |
215 | ||
d3bef7bc JF |
216 | static const NSUInteger UIViewAutoresizingFlexibleBoth(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); |
217 | ||
541a556a JF |
218 | void NSLogPoint(const char *fix, const CGPoint &point) { |
219 | NSLog(@"%s(%g,%g)", fix, point.x, point.y); | |
220 | } | |
221 | ||
f464053e JF |
222 | void NSLogRect(const char *fix, const CGRect &rect) { |
223 | NSLog(@"%s(%g,%g)+(%g,%g)", fix, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); | |
c045fc11 JF |
224 | } |
225 | ||
6981ccdf JF |
226 | static _finline NSString *CydiaURL(NSString *path) { |
227 | char page[25]; | |
228 | page[0] = 'h'; page[1] = 't'; page[2] = 't'; page[3] = 'p'; page[4] = ':'; | |
229 | page[5] = '/'; page[6] = '/'; page[7] = 'c'; page[8] = 'y'; page[9] = 'd'; | |
230 | page[10] = 'i'; page[11] = 'a'; page[12] = '.'; page[13] = 's'; page[14] = 'a'; | |
231 | page[15] = 'u'; page[16] = 'r'; page[17] = 'i'; page[18] = 'k'; page[19] = '.'; | |
232 | page[20] = 'c'; page[21] = 'o'; page[22] = 'm'; page[23] = '/'; page[24] = '\0'; | |
233 | return [[NSString stringWithUTF8String:page] stringByAppendingString:path]; | |
234 | } | |
235 | ||
864da84b RP |
236 | static _finline void UpdateExternalStatus(uint64_t newStatus) { |
237 | int notify_token; | |
238 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
239 | notify_set_state(notify_token, newStatus); | |
240 | notify_cancel(notify_token); | |
241 | } | |
242 | notify_post("com.saurik.Cydia.status"); | |
243 | } | |
244 | ||
017b2b71 | 245 | /* [NSObject yieldToSelector:(withObject:)] {{{*/ |
7398a389 | 246 | @interface NSObject (Cydia) |
b4dff19a JF |
247 | - (id) yieldToSelector:(SEL)selector withObject:(id)object; |
248 | - (id) yieldToSelector:(SEL)selector; | |
7398a389 JF |
249 | @end |
250 | ||
251 | @implementation NSObject (Cydia) | |
252 | ||
253 | - (void) doNothing { | |
254 | } | |
255 | ||
b4dff19a | 256 | - (void) _yieldToContext:(NSMutableArray *)context { _pooled |
7398a389 JF |
257 | SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue])); |
258 | id object([[context objectAtIndex:1] nonretainedObjectValue]); | |
259 | volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue])); | |
260 | ||
b4dff19a JF |
261 | /* XXX: deal with exceptions */ |
262 | id value([self performSelector:selector withObject:object]); | |
263 | ||
43b742af | 264 | NSMethodSignature *signature([self methodSignatureForSelector:selector]); |
b4dff19a | 265 | [context removeAllObjects]; |
43b742af | 266 | if ([signature methodReturnLength] != 0 && value != nil) |
b4dff19a | 267 | [context addObject:value]; |
7398a389 JF |
268 | |
269 | stopped = true; | |
270 | ||
271 | [self | |
272 | performSelectorOnMainThread:@selector(doNothing) | |
273 | withObject:nil | |
274 | waitUntilDone:NO | |
275 | ]; | |
276 | } | |
277 | ||
b4dff19a | 278 | - (id) yieldToSelector:(SEL)selector withObject:(id)object { |
7b00c562 | 279 | /*return [self performSelector:selector withObject:object];*/ |
3bddda52 | 280 | |
7398a389 JF |
281 | volatile bool stopped(false); |
282 | ||
b4dff19a | 283 | NSMutableArray *context([NSMutableArray arrayWithObjects: |
7398a389 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]); | |
b4dff19a 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]; | |
7398a389 JF |
307 | } |
308 | ||
309 | @end | |
017b2b71 | 310 | /* }}} */ |
7398a389 | 311 | |
6744338a | 312 | /* Cydia Action Sheet {{{ */ |
19723386 | 313 | @interface CYActionSheet : UIAlertView { |
6981ccdf 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 { | |
19723386 | 323 | if ((self = [super init])) { |
9ae52960 GP |
324 | [self setTitle:title]; |
325 | [self setDelegate:self]; | |
326 | for (NSString *button in buttons) [self addButtonWithTitle:button]; | |
327 | [self setCancelButtonIndex:index]; | |
6981ccdf JF |
328 | } return self; |
329 | } | |
330 | ||
d5653f96 | 331 | - (void) _updateFrameForDisplay { |
8b6bb0fd 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 | ||
19723386 GP |
343 | - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
344 | button_ = buttonIndex + 1; | |
6981ccdf JF |
345 | } |
346 | ||
9e639c5a | 347 | - (void) dismiss { |
9ae52960 | 348 | [self dismissWithClickedButtonIndex:-1 animated:YES]; |
9e639c5a GP |
349 | } |
350 | ||
6981ccdf | 351 | - (int) yieldToPopupAlertAnimated:(BOOL)animated { |
05636a2c | 352 | [self setRunsModal:YES]; |
6981ccdf | 353 | button_ = 0; |
9e639c5a | 354 | [self show]; |
6981ccdf JF |
355 | return button_; |
356 | } | |
357 | ||
358 | @end | |
6744338a | 359 | /* }}} */ |
6981ccdf | 360 | |
9c4e0cbe | 361 | /* NSForcedOrderingSearch doesn't work on the iPhone */ |
3bddda52 | 362 | static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch; |
43b742af JF |
363 | static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch; |
364 | static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareCaseInsensitive | kCFCompareNonliteral | kCFCompareLocalized | kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; | |
c045fc11 | 365 | |
7f9c29fa 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 { | |
017b2b71 | 386 | [self setObject:info forKey:[info objectForKey:@"CFBundleIdentifier"]]; |
7f9c29fa JF |
387 | } |
388 | ||
389 | @end | |
390 | /* }}} */ | |
391 | ||
cb9c2100 | 392 | #define lprintf(args...) fprintf(stderr, args) |
d210b85d | 393 | |
982de8f1 | 394 | #define ForRelease 1 |
1ff58dbb | 395 | #define TraceLogging (1 && !ForRelease) |
2df365d8 | 396 | #define HistogramInsertionSort (!ForRelease ? 0 : 0) |
b8b1cfd0 | 397 | #define ProfileTimes (0 && !ForRelease) |
fa5cb337 | 398 | #define ForSaurik (0 && !ForRelease) |
6981ccdf | 399 | #define LogBrowser (0 && !ForRelease) |
bb9edf8b | 400 | #define TrackResize (0 && !ForRelease) |
c2292b80 | 401 | #define ManualRefresh (1 && !ForRelease) |
6932575e | 402 | #define ShowInternals (0 && !ForRelease) |
83105e6e | 403 | #define IgnoreInstall (0 && !ForRelease) |
c2292b80 | 404 | #define AlwaysReload (0 && !ForRelease) |
81ab76dc | 405 | |
1ff58dbb | 406 | #if !TraceLogging |
6e673d99 JF |
407 | #undef _trace |
408 | #define _trace(args...) | |
1ff58dbb JF |
409 | #endif |
410 | ||
411 | #if !ProfileTimes | |
3bddda52 | 412 | #undef _profile |
7b00c562 | 413 | #define _profile(name) { |
3bddda52 | 414 | #undef _end |
7b00c562 | 415 | #define _end } |
b4dff19a | 416 | #define PrintTimes() do {} while (false) |
6e673d99 JF |
417 | #endif |
418 | ||
f159ecd4 | 419 | /* Radix Sort {{{ */ |
dd9390c5 JF |
420 | typedef uint32_t (*SKRadixFunction)(id, void *); |
421 | ||
f159ecd4 | 422 | @interface NSMutableArray (Radix) |
dd9390c5 | 423 | - (void) radixSortUsingFunction:(SKRadixFunction)function withContext:(void *)argument; |
f159ecd4 JF |
424 | @end |
425 | ||
6932575e JF |
426 | struct RadixItem_ { |
427 | size_t index; | |
428 | uint32_t key; | |
429 | }; | |
f159ecd4 | 430 | |
ac11599c 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 | ||
6932575e | 445 | struct RadixItem_ *lhs(swap), *rhs(swap + count); |
f159ecd4 | 446 | |
890c1d38 | 447 | static const size_t width = 32; |
f159ecd4 JF |
448 | static const size_t bits = 11; |
449 | static const size_t slots = 1 << bits; | |
890c1d38 | 450 | static const size_t passes = (width + (bits - 1)) / bits; |
f159ecd4 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; | |
890c1d38 | 460 | key &= _not(uint32_t) >> width - bits; |
f159ecd4 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; | |
890c1d38 | 474 | key &= _not(uint32_t) >> width - bits; |
f159ecd4 JF |
475 | rhs[hist[key]++] = lhs[i]; |
476 | } | |
477 | ||
6932575e | 478 | RadixItem_ *tmp(lhs); |
f159ecd4 JF |
479 | lhs = rhs; |
480 | rhs = tmp; | |
481 | } | |
482 | ||
483 | delete [] hist; | |
484 | ||
2ec90f7b | 485 | const void **values(new const void *[count]); |
f159ecd4 | 486 | for (size_t i(0); i != count; ++i) |
2ec90f7b JF |
487 | values[i] = [self objectAtIndex:lhs[i].index]; |
488 | CFArrayReplaceValues((CFMutableArrayRef) self, CFRangeMake(0, count), values, count); | |
489 | delete [] values; | |
f159ecd4 JF |
490 | |
491 | delete [] swap; | |
492 | } | |
493 | ||
494 | @end | |
66abcbb0 JF |
495 | /* }}} */ |
496 | /* Insertion Sort {{{ */ | |
497 | ||
dd9390c5 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); | |
9ae52960 | 504 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
dd9390c5 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 | ||
66abcbb0 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); | |
9ae52960 | 517 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
66abcbb0 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 | ||
2df365d8 | 530 | #if HistogramInsertionSort > 0 |
dd9390c5 JF |
531 | uint32_t total(0), *offsets(new uint32_t[range.length]); |
532 | #endif | |
533 | ||
66abcbb0 JF |
534 | for (CFIndex index(1); index != range.length; ++index) { |
535 | const void *value(values[index]); | |
dd9390c5 JF |
536 | //CFIndex correct(SKBSearch_(&value, sizeof(const void *), values, index, comparator, context)); |
537 | CFIndex correct(index); | |
2df365d8 JF |
538 | while (comparator(value, values[correct - 1], context) == kCFCompareLessThan) { |
539 | #if HistogramInsertionSort > 1 | |
540 | NSLog(@"%@ < %@", value, values[correct - 1]); | |
541 | #endif | |
dd9390c5 JF |
542 | if (--correct == 0) |
543 | break; | |
2df365d8 | 544 | } |
66abcbb0 | 545 | if (correct != index) { |
dd9390c5 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); | |
66abcbb0 JF |
554 | values[correct] = value; |
555 | } | |
556 | } | |
557 | ||
558 | CFArrayReplaceValues(array, range, values, range.length); | |
559 | delete [] values; | |
dd9390c5 | 560 | |
2df365d8 | 561 | #if HistogramInsertionSort > 0 |
dd9390c5 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 | |
66abcbb0 JF |
568 | } |
569 | ||
f159ecd4 JF |
570 | /* }}} */ |
571 | ||
541a556a 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 | ||
8a4106c2 JF |
600 | @implementation WebScriptObject (NSFastEnumeration) |
601 | ||
602 | - (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)objects count:(NSUInteger)count { | |
daf7f6e2 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 | ||
8a4106c2 JF |
615 | @end |
616 | ||
cd8bec2f 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 | ||
017b2b71 | 630 | /* Cydia NSString Additions {{{ */ |
2a987aa5 | 631 | @interface NSString (Cydia) |
3bddda52 | 632 | + (NSString *) stringWithUTF8BytesNoCopy:(const char *)bytes length:(int)length; |
6932575e | 633 | + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length withZone:(NSZone *)zone inPool:(apr_pool_t *)pool; |
2a987aa5 | 634 | + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length; |
9e07091a | 635 | - (NSComparisonResult) compareByPath:(NSString *)other; |
b4dff19a JF |
636 | - (NSString *) stringByCachingURLWithCurrentCDN; |
637 | - (NSString *) stringByAddingPercentEscapesIncludingReserved; | |
2a987aa5 JF |
638 | @end |
639 | ||
640 | @implementation NSString (Cydia) | |
641 | ||
3bddda52 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 | ||
6932575e 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 | ||
2a987aa5 | 652 | + (NSString *) stringWithUTF8Bytes:(const char *)bytes length:(int)length { |
e2a207dd | 653 | return [[[NSString alloc] initWithBytes:bytes length:length encoding:NSUTF8StringEncoding] autorelease]; |
2a987aa5 JF |
654 | } |
655 | ||
9e07091a 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 | ||
b4dff19a JF |
686 | - (NSString *) stringByCachingURLWithCurrentCDN { |
687 | return [self | |
99dc9e91 JF |
688 | stringByReplacingOccurrencesOfString:@"://cydia.saurik.com/" |
689 | withString:@"://cache.cydia.saurik.com/" | |
b4dff19a JF |
690 | ]; |
691 | } | |
692 | ||
693 | - (NSString *) stringByAddingPercentEscapesIncludingReserved { | |
694 | return [(id)CFURLCreateStringByAddingPercentEscapes( | |
f99f86e2 | 695 | kCFAllocatorDefault, |
b4dff19a JF |
696 | (CFStringRef) self, |
697 | NULL, | |
698 | CFSTR(";/?:@&=+$,"), | |
699 | kCFStringEncodingUTF8 | |
700 | ) autorelease]; | |
701 | } | |
702 | ||
2a987aa5 | 703 | @end |
017b2b71 | 704 | /* }}} */ |
2a987aa5 | 705 | |
017b2b71 | 706 | /* C++ NSString Wrapper Cache {{{ */ |
2a0a85d0 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 | ||
6932575e JF |
717 | class CYString { |
718 | private: | |
719 | char *data_; | |
720 | size_t size_; | |
721 | CFStringRef cache_; | |
722 | ||
723 | _finline void clear_() { | |
f30eaf83 | 724 | if (cache_ != NULL) { |
6932575e | 725 | CFRelease(cache_); |
f30eaf83 JF |
726 | cache_ = NULL; |
727 | } | |
6932575e 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), | |
f30eaf83 | 751 | cache_(NULL) |
6932575e 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 | ||
aba7dd7d 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 | ||
6932575e 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 | ||
aba7dd7d | 782 | data_ = const_cast<char *>(data); |
6932575e | 783 | size_ = size; |
aba7dd7d JF |
784 | |
785 | if (pool != NULL) | |
786 | copy(pool); | |
6932575e 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 | ||
2a0a85d0 JF |
802 | _finline operator CFStringRef() { |
803 | if (cache_ == NULL) | |
804 | cache_ = CYStringCreate(data_, size_); | |
805 | return cache_; | |
dd9390c5 JF |
806 | } |
807 | ||
808 | _finline operator id() { | |
809 | return (NSString *) static_cast<CFStringRef>(*this); | |
6932575e | 810 | } |
c7715e92 JF |
811 | |
812 | _finline operator const char *() { | |
813 | return reinterpret_cast<const char *>(data_); | |
814 | } | |
6932575e | 815 | }; |
017b2b71 JF |
816 | /* }}} */ |
817 | /* C++ NSString Algorithm Adapters {{{ */ | |
6932575e 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 | }; | |
017b2b71 | 847 | /* }}} */ |
6932575e | 848 | |
686e302f 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) { | |
cb9c2100 | 867 | lprintf("%d:%s\n", offset, error); |
686e302f 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) { | |
2a987aa5 | 881 | return [NSString stringWithUTF8Bytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2])]; |
686e302f JF |
882 | } |
883 | ||
7600bd69 JF |
884 | bool operator ()(NSString *data) { |
885 | // XXX: length is for characters, not for bytes | |
886 | return operator ()([data UTF8String], [data length]); | |
887 | } | |
888 | ||
686e302f 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 | /* }}} */ | |
e057ec05 | 895 | /* Mime Addresses {{{ */ |
e057ec05 JF |
896 | @interface Address : NSObject { |
897 | NSString *name_; | |
ad554f10 | 898 | NSString *address_; |
e057ec05 JF |
899 | } |
900 | ||
901 | - (NSString *) name; | |
ad554f10 | 902 | - (NSString *) address; |
e057ec05 | 903 | |
3ff1504e JF |
904 | - (void) setAddress:(NSString *)address; |
905 | ||
e057ec05 JF |
906 | + (Address *) addressWithString:(NSString *)string; |
907 | - (Address *) initWithString:(NSString *)string; | |
908 | @end | |
909 | ||
910 | @implementation Address | |
911 | ||
912 | - (void) dealloc { | |
913 | [name_ release]; | |
ad554f10 JF |
914 | if (address_ != nil) |
915 | [address_ release]; | |
e057ec05 JF |
916 | [super dealloc]; |
917 | } | |
918 | ||
919 | - (NSString *) name { | |
920 | return name_; | |
921 | } | |
922 | ||
ad554f10 JF |
923 | - (NSString *) address { |
924 | return address_; | |
e057ec05 JF |
925 | } |
926 | ||
3ff1504e 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 | ||
e057ec05 JF |
936 | + (Address *) addressWithString:(NSString *)string { |
937 | return [[[Address alloc] initWithString:string] autorelease]; | |
938 | } | |
939 | ||
ad554f10 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 | ||
e057ec05 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 | ||
ad554f10 | 957 | static Pcre address_r("^\"?(.*)\"? <([^>]*)>$"); |
faf4eb4f | 958 | |
ad554f10 JF |
959 | if (address_r(data, size)) { |
960 | name_ = [address_r[1] retain]; | |
961 | address_ = [address_r[2] retain]; | |
e057ec05 | 962 | } else { |
f464053e | 963 | name_ = [string retain]; |
ad554f10 | 964 | address_ = nil; |
e057ec05 JF |
965 | } |
966 | } return self; | |
967 | } | |
968 | ||
969 | @end | |
970 | /* }}} */ | |
b0d03ade | 971 | /* CoreGraphics Primitives {{{ */ |
eb6d9c93 | 972 | class CYColor { |
686e302f JF |
973 | private: |
974 | CGColorRef color_; | |
975 | ||
a1382843 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 | ||
686e302f | 981 | public: |
eb6d9c93 | 982 | CYColor() : |
e057ec05 JF |
983 | color_(NULL) |
984 | { | |
985 | } | |
986 | ||
eb6d9c93 | 987 | CYColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) : |
a1382843 | 988 | color_(Create_(space, red, green, blue, alpha)) |
e057ec05 JF |
989 | { |
990 | Set(space, red, green, blue, alpha); | |
991 | } | |
992 | ||
993 | void Clear() { | |
994 | if (color_ != NULL) | |
995 | CGColorRelease(color_); | |
686e302f JF |
996 | } |
997 | ||
eb6d9c93 | 998 | ~CYColor() { |
e057ec05 JF |
999 | Clear(); |
1000 | } | |
1001 | ||
1002 | void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) { | |
1003 | Clear(); | |
a1382843 | 1004 | color_ = Create_(space, red, green, blue, alpha); |
686e302f JF |
1005 | } |
1006 | ||
1007 | operator CGColorRef() { | |
1008 | return color_; | |
1009 | } | |
1010 | }; | |
686e302f JF |
1011 | /* }}} */ |
1012 | ||
e057ec05 | 1013 | /* Random Global Variables {{{ */ |
2d28b35a | 1014 | static const int PulseInterval_ = 50000; |
575ffd3c | 1015 | static const int ButtonBarWidth_ = 60; |
2a987aa5 | 1016 | static const int ButtonBarHeight_ = 48; |
fa7bb92f | 1017 | static const float KeyboardTime_ = 0.3f; |
f464053e | 1018 | |
c59881cd JF |
1019 | static int Finish_; |
1020 | static NSArray *Finishes_; | |
1021 | ||
f464053e | 1022 | #define SpringBoard_ "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist" |
bde2d79b | 1023 | #define NotifyConfig_ "/etc/notify.conf" |
fc675b93 | 1024 | |
3ff1504e JF |
1025 | static bool Queuing_; |
1026 | ||
eb6d9c93 JF |
1027 | static CYColor Blue_; |
1028 | static CYColor Blueish_; | |
1029 | static CYColor Black_; | |
1030 | static CYColor Off_; | |
1031 | static CYColor White_; | |
1032 | static CYColor Gray_; | |
1033 | static CYColor Green_; | |
1034 | static CYColor Purple_; | |
1035 | static CYColor Purplish_; | |
d8d9a65c | 1036 | |
3ff1504e JF |
1037 | static UIColor *InstallingColor_; |
1038 | static UIColor *RemovingColor_; | |
e057ec05 | 1039 | |
853d14d3 | 1040 | static NSString *App_; |
0039464f | 1041 | static NSString *Home_; |
0039464f | 1042 | |
2a987aa5 | 1043 | static BOOL Advanced_; |
9e07091a | 1044 | static BOOL Ignored_; |
2a987aa5 | 1045 | |
5e563e79 JF |
1046 | static UIFont *Font12_; |
1047 | static UIFont *Font12Bold_; | |
1048 | static UIFont *Font14_; | |
1049 | static UIFont *Font18Bold_; | |
1050 | static UIFont *Font22Bold_; | |
1051 | ||
2cb68ddf | 1052 | static const char *Machine_ = NULL; |
78f1a54b | 1053 | static NSString *System_ = nil; |
3bdadc1e JF |
1054 | static NSString *SerialNumber_ = nil; |
1055 | static NSString *ChipID_ = nil; | |
1056 | static NSString *Token_ = nil; | |
1057 | static NSString *UniqueID_ = nil; | |
3074466a | 1058 | static NSString *PLMN_ = nil; |
3bdadc1e JF |
1059 | static NSString *Build_ = nil; |
1060 | static NSString *Product_ = nil; | |
1061 | static NSString *Safari_ = nil; | |
fc675b93 | 1062 | |
c59881cd JF |
1063 | static CFLocaleRef Locale_; |
1064 | static NSArray *Languages_; | |
1065 | static CGColorSpaceRef space_; | |
e057ec05 | 1066 | |
d1494d2c | 1067 | static NSDictionary *SectionMap_; |
686e302f | 1068 | static NSMutableDictionary *Metadata_; |
faf4eb4f JF |
1069 | static _transient NSMutableDictionary *Settings_; |
1070 | static _transient NSString *Role_; | |
1071 | static _transient NSMutableDictionary *Packages_; | |
1072 | static _transient NSMutableDictionary *Sections_; | |
1073 | static _transient NSMutableDictionary *Sources_; | |
3d37fc0d | 1074 | static bool Changed_; |
8032d797 | 1075 | static time_t now_; |
20dd7407 | 1076 | |
5ec44e34 | 1077 | static bool IsWildcat_; |
e057ec05 | 1078 | /* }}} */ |
c59881cd | 1079 | |
e057ec05 JF |
1080 | /* Display Helpers {{{ */ |
1081 | inline float Interpolate(float begin, float end, float fraction) { | |
1082 | return (end - begin) * fraction + begin; | |
1083 | } | |
4941f41d | 1084 | |
6932575e | 1085 | /* XXX: localize this! */ |
b6ffa083 | 1086 | NSString *SizeString(double size) { |
f464053e JF |
1087 | bool negative = size < 0; |
1088 | if (negative) | |
1089 | size = -size; | |
1090 | ||
b6ffa083 JF |
1091 | unsigned power = 0; |
1092 | while (size > 1024) { | |
1093 | size /= 1024; | |
1094 | ++power; | |
1095 | } | |
1096 | ||
1097 | static const char *powers_[] = {"B", "kB", "MB", "GB"}; | |
1098 | ||
56e10908 | 1099 | return [NSString stringWithFormat:@"%s%.1f %s", (negative ? "-" : ""), size, powers_[power]]; |
b6ffa083 JF |
1100 | } |
1101 | ||
5bb2842a | 1102 | static _finline const char *StripVersion_(const char *version) { |
b4c4fac4 JF |
1103 | const char *colon(strchr(version, ':')); |
1104 | if (colon != NULL) | |
1105 | version = colon + 1; | |
1106 | return version; | |
1107 | } | |
1108 | ||
6932575e | 1109 | NSString *LocalizeSection(NSString *section) { |
bb9edf8b | 1110 | static Pcre title_r("^(.*?) \\((.*)\\)$"); |
9ee296df JF |
1111 | if (title_r(section)) { |
1112 | NSString *parent(title_r[1]); | |
1113 | NSString *child(title_r[2]); | |
1114 | ||
61b13cae | 1115 | return [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), |
9ee296df JF |
1116 | LocalizeSection(parent), |
1117 | LocalizeSection(child) | |
bb9edf8b | 1118 | ]; |
9ee296df | 1119 | } |
bb9edf8b JF |
1120 | |
1121 | return [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
6932575e JF |
1122 | } |
1123 | ||
49525fb2 JF |
1124 | NSString *Simplify(NSString *title) { |
1125 | const char *data = [title UTF8String]; | |
1126 | size_t size = [title length]; | |
1127 | ||
faf4eb4f JF |
1128 | static Pcre square_r("^\\[(.*)\\]$"); |
1129 | if (square_r(data, size)) | |
1130 | return Simplify(square_r[1]); | |
1131 | ||
1132 | static Pcre paren_r("^\\((.*)\\)$"); | |
1133 | if (paren_r(data, size)) | |
1134 | return Simplify(paren_r[1]); | |
1135 | ||
bb9edf8b | 1136 | static Pcre title_r("^(.*?) \\((.*)\\)$"); |
49525fb2 | 1137 | if (title_r(data, size)) |
faf4eb4f JF |
1138 | return Simplify(title_r[1]); |
1139 | ||
1140 | return title; | |
49525fb2 | 1141 | } |
e057ec05 JF |
1142 | /* }}} */ |
1143 | ||
c59881cd JF |
1144 | NSString *GetLastUpdate() { |
1145 | NSDate *update = [Metadata_ objectForKey:@"LastUpdate"]; | |
1146 | ||
1147 | if (update == nil) | |
1148 | return UCLocalize("NEVER_OR_UNKNOWN"); | |
1149 | ||
1150 | CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle); | |
1151 | CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update); | |
1152 | ||
1153 | CFRelease(formatter); | |
1154 | ||
1155 | return [(NSString *) formatted autorelease]; | |
1156 | } | |
1157 | ||
fa7bb92f | 1158 | bool isSectionVisible(NSString *section) { |
43b742af JF |
1159 | NSDictionary *metadata([Sections_ objectForKey:section]); |
1160 | NSNumber *hidden(metadata == nil ? nil : [metadata objectForKey:@"Hidden"]); | |
fa7bb92f JF |
1161 | return hidden == nil || ![hidden boolValue]; |
1162 | } | |
1163 | ||
15b41c77 GP |
1164 | @class Cydia; |
1165 | ||
a0e1b888 | 1166 | /* Delegate Prototypes {{{ */ |
e057ec05 JF |
1167 | @class Package; |
1168 | @class Source; | |
1169 | ||
238b07ce JF |
1170 | @interface NSObject (ProgressDelegate) |
1171 | @end | |
1172 | ||
e057ec05 | 1173 | @protocol ProgressDelegate |
6981ccdf | 1174 | - (void) setProgressError:(NSString *)error withTitle:(NSString *)id; |
e057ec05 JF |
1175 | - (void) setProgressTitle:(NSString *)title; |
1176 | - (void) setProgressPercent:(float)percent; | |
87c76914 | 1177 | - (void) startProgress; |
e057ec05 | 1178 | - (void) addProgressOutput:(NSString *)output; |
87c76914 | 1179 | - (bool) isCancelling:(size_t)received; |
e057ec05 JF |
1180 | @end |
1181 | ||
7600bd69 | 1182 | @protocol ConfigurationDelegate |
965edd52 | 1183 | - (void) repairWithSelector:(SEL)selector; |
7600bd69 JF |
1184 | - (void) setConfigurationData:(NSString *)data; |
1185 | @end | |
1186 | ||
f441e717 | 1187 | @class PackageController; |
6932575e | 1188 | |
e057ec05 | 1189 | @protocol CydiaDelegate |
f441e717 | 1190 | - (void) setPackageController:(PackageController *)view; |
3ff1504e | 1191 | - (void) clearPackage:(Package *)package; |
e057ec05 | 1192 | - (void) installPackage:(Package *)package; |
daf7f6e2 | 1193 | - (void) installPackages:(NSArray *)packages; |
e057ec05 | 1194 | - (void) removePackage:(Package *)package; |
1e4922b8 JF |
1195 | - (void) beginUpdate; |
1196 | - (BOOL) updating; | |
e057ec05 | 1197 | - (void) distUpgrade; |
90e9a238 | 1198 | - (void) loadData; |
fa7bb92f | 1199 | - (void) updateData; |
faf4eb4f | 1200 | - (void) syncData; |
c1edf105 | 1201 | - (void) showSettings; |
faf4eb4f | 1202 | - (UIProgressHUD *) addProgressHUD; |
832c0799 | 1203 | - (BOOL) hudIsShowing; |
7398a389 | 1204 | - (void) removeProgressHUD:(UIProgressHUD *)hud; |
881fe77f | 1205 | - (CYViewController *) pageForPackage:(NSString *)name; |
f441e717 | 1206 | - (PackageController *) packageController; |
48861ec9 | 1207 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item; |
e057ec05 | 1208 | @end |
a0e1b888 | 1209 | /* }}} */ |
686e302f | 1210 | |
a75f53e7 JF |
1211 | /* Status Delegation {{{ */ |
1212 | class Status : | |
1213 | public pkgAcquireStatus | |
1214 | { | |
1215 | private: | |
238b07ce | 1216 | _transient NSObject<ProgressDelegate> *delegate_; |
a75f53e7 JF |
1217 | |
1218 | public: | |
1219 | Status() : | |
1220 | delegate_(nil) | |
1221 | { | |
1222 | } | |
1223 | ||
1224 | void setDelegate(id delegate) { | |
1225 | delegate_ = delegate; | |
1226 | } | |
1227 | ||
6981ccdf JF |
1228 | NSObject<ProgressDelegate> *getDelegate() const { |
1229 | return delegate_; | |
1230 | } | |
1231 | ||
a75f53e7 JF |
1232 | virtual bool MediaChange(std::string media, std::string drive) { |
1233 | return false; | |
1234 | } | |
1235 | ||
1236 | virtual void IMSHit(pkgAcquire::ItemDesc &item) { | |
a75f53e7 JF |
1237 | } |
1238 | ||
1239 | virtual void Fetch(pkgAcquire::ItemDesc &item) { | |
907a35d6 | 1240 | //NSString *name([NSString stringWithUTF8String:item.ShortDesc.c_str()]); |
6981ccdf | 1241 | [delegate_ setProgressTitle:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), [NSString stringWithUTF8String:item.ShortDesc.c_str()]]]; |
a75f53e7 JF |
1242 | } |
1243 | ||
1244 | virtual void Done(pkgAcquire::ItemDesc &item) { | |
a75f53e7 JF |
1245 | } |
1246 | ||
1247 | virtual void Fail(pkgAcquire::ItemDesc &item) { | |
3325a005 JF |
1248 | if ( |
1249 | item.Owner->Status == pkgAcquire::Item::StatIdle || | |
1250 | item.Owner->Status == pkgAcquire::Item::StatDone | |
1251 | ) | |
1252 | return; | |
1253 | ||
f464053e JF |
1254 | std::string &error(item.Owner->ErrorText); |
1255 | if (error.empty()) | |
1256 | return; | |
1257 | ||
1258 | NSString *description([NSString stringWithUTF8String:item.Description.c_str()]); | |
1259 | NSArray *fields([description componentsSeparatedByString:@" "]); | |
1260 | NSString *source([fields count] == 0 ? nil : [fields objectAtIndex:0]); | |
1261 | ||
6981ccdf | 1262 | [delegate_ performSelectorOnMainThread:@selector(_setProgressErrorPackage:) |
f464053e JF |
1263 | withObject:[NSArray arrayWithObjects: |
1264 | [NSString stringWithUTF8String:error.c_str()], | |
1265 | source, | |
1266 | nil] | |
238b07ce JF |
1267 | waitUntilDone:YES |
1268 | ]; | |
a75f53e7 JF |
1269 | } |
1270 | ||
1271 | virtual bool Pulse(pkgAcquire *Owner) { | |
4941f41d JF |
1272 | bool value = pkgAcquireStatus::Pulse(Owner); |
1273 | ||
1274 | float percent( | |
1275 | double(CurrentBytes + CurrentItems) / | |
1276 | double(TotalBytes + TotalItems) | |
1277 | ); | |
1278 | ||
e057ec05 | 1279 | [delegate_ setProgressPercent:percent]; |
87c76914 | 1280 | return [delegate_ isCancelling:CurrentBytes] ? false : value; |
a75f53e7 JF |
1281 | } |
1282 | ||
1283 | virtual void Start() { | |
87c76914 | 1284 | [delegate_ startProgress]; |
a75f53e7 JF |
1285 | } |
1286 | ||
1287 | virtual void Stop() { | |
a75f53e7 JF |
1288 | } |
1289 | }; | |
1290 | /* }}} */ | |
1291 | /* Progress Delegation {{{ */ | |
1292 | class Progress : | |
1293 | public OpProgress | |
1294 | { | |
1295 | private: | |
e057ec05 | 1296 | _transient id<ProgressDelegate> delegate_; |
017b2b71 | 1297 | float percent_; |
a75f53e7 JF |
1298 | |
1299 | protected: | |
1300 | virtual void Update() { | |
6981ccdf JF |
1301 | /*if (abs(Percent - percent_) > 2) |
1302 | //NSLog(@"%s:%s:%f", Op.c_str(), SubOp.c_str(), Percent); | |
017b2b71 | 1303 | percent_ = Percent; |
6981ccdf | 1304 | }*/ |
017b2b71 | 1305 | |
b7ad9c68 JF |
1306 | /*[delegate_ setProgressTitle:[NSString stringWithUTF8String:Op.c_str()]]; |
1307 | [delegate_ setProgressPercent:(Percent / 100)];*/ | |
a75f53e7 JF |
1308 | } |
1309 | ||
1310 | public: | |
1311 | Progress() : | |
017b2b71 JF |
1312 | delegate_(nil), |
1313 | percent_(0) | |
a75f53e7 JF |
1314 | { |
1315 | } | |
1316 | ||
1317 | void setDelegate(id delegate) { | |
1318 | delegate_ = delegate; | |
1319 | } | |
1320 | ||
6981ccdf JF |
1321 | id getDelegate() const { |
1322 | return delegate_; | |
1323 | } | |
1324 | ||
a75f53e7 | 1325 | virtual void Done() { |
6981ccdf | 1326 | //NSLog(@"DONE"); |
b7ad9c68 | 1327 | //[delegate_ setProgressPercent:1]; |
a75f53e7 JF |
1328 | } |
1329 | }; | |
1330 | /* }}} */ | |
1331 | ||
e057ec05 | 1332 | /* Database Interface {{{ */ |
631a0a1e JF |
1333 | typedef std::map< unsigned long, _H<Source> > SourceMap; |
1334 | ||
e057ec05 | 1335 | @interface Database : NSObject { |
6932575e JF |
1336 | NSZone *zone_; |
1337 | apr_pool_t *pool_; | |
1338 | ||
a70cf746 JF |
1339 | unsigned era_; |
1340 | ||
e057ec05 | 1341 | pkgCacheFile cache_; |
965edd52 | 1342 | pkgDepCache::Policy *policy_; |
e057ec05 JF |
1343 | pkgRecords *records_; |
1344 | pkgProblemResolver *resolver_; | |
1345 | pkgAcquire *fetcher_; | |
1346 | FileFd *lock_; | |
1347 | SPtr<pkgPackageManager> manager_; | |
1348 | pkgSourceList *list_; | |
b0d03ade | 1349 | |
631a0a1e | 1350 | SourceMap sources_; |
777744da | 1351 | CFMutableArrayRef packages_; |
686e302f | 1352 | |
238b07ce | 1353 | _transient NSObject<ConfigurationDelegate, ProgressDelegate> *delegate_; |
e057ec05 JF |
1354 | Status status_; |
1355 | Progress progress_; | |
7600bd69 | 1356 | |
d72d91aa | 1357 | int cydiafd_; |
e057ec05 | 1358 | int statusfd_; |
7600bd69 | 1359 | FILE *input_; |
a75f53e7 JF |
1360 | } |
1361 | ||
dbe0f181 | 1362 | + (Database *) sharedInstance; |
a70cf746 | 1363 | - (unsigned) era; |
dbe0f181 | 1364 | |
d72d91aa | 1365 | - (void) _readCydia:(NSNumber *)fd; |
e057ec05 JF |
1366 | - (void) _readStatus:(NSNumber *)fd; |
1367 | - (void) _readOutput:(NSNumber *)fd; | |
4941f41d | 1368 | |
7600bd69 JF |
1369 | - (FILE *) input; |
1370 | ||
e057ec05 | 1371 | - (Package *) packageWithName:(NSString *)name; |
a75f53e7 | 1372 | |
e057ec05 | 1373 | - (pkgCacheFile &) cache; |
965edd52 | 1374 | - (pkgDepCache::Policy *) policy; |
e057ec05 JF |
1375 | - (pkgRecords *) records; |
1376 | - (pkgProblemResolver *) resolver; | |
1377 | - (pkgAcquire &) fetcher; | |
ce09fc27 | 1378 | - (pkgSourceList &) list; |
e057ec05 | 1379 | - (NSArray *) packages; |
faf4eb4f | 1380 | - (NSArray *) sources; |
e057ec05 JF |
1381 | - (void) reloadData; |
1382 | ||
965edd52 | 1383 | - (void) configure; |
6981ccdf | 1384 | - (bool) prepare; |
e057ec05 | 1385 | - (void) perform; |
6981ccdf | 1386 | - (bool) upgrade; |
e057ec05 JF |
1387 | - (void) update; |
1388 | ||
6981ccdf | 1389 | - (void) updateWithStatus:(Status &)status; |
e057ec05 JF |
1390 | |
1391 | - (void) setDelegate:(id)delegate; | |
3e3977a2 | 1392 | - (Source *) getSource:(pkgCache::PkgFileIterator)file; |
6981ccdf JF |
1393 | @end |
1394 | /* }}} */ | |
1395 | /* Delegate Helpers {{{ */ | |
1e4922b8 | 1396 | @implementation NSObject (ProgressDelegate) |
6981ccdf JF |
1397 | |
1398 | - (void) _setProgressErrorPackage:(NSArray *)args { | |
1399 | [self performSelector:@selector(setProgressError:forPackage:) | |
1400 | withObject:[args objectAtIndex:0] | |
1401 | withObject:([args count] == 1 ? nil : [args objectAtIndex:1]) | |
1402 | ]; | |
1403 | } | |
1404 | ||
1405 | - (void) _setProgressErrorTitle:(NSArray *)args { | |
1406 | [self performSelector:@selector(setProgressError:withTitle:) | |
1407 | withObject:[args objectAtIndex:0] | |
1408 | withObject:([args count] == 1 ? nil : [args objectAtIndex:1]) | |
1409 | ]; | |
1410 | } | |
1411 | ||
1412 | - (void) _setProgressError:(NSString *)error withTitle:(NSString *)title { | |
1413 | [self performSelectorOnMainThread:@selector(_setProgressErrorTitle:) | |
1414 | withObject:[NSArray arrayWithObjects:error, title, nil] | |
1415 | waitUntilDone:YES | |
1416 | ]; | |
1417 | } | |
1418 | ||
1419 | - (void) setProgressError:(NSString *)error forPackage:(NSString *)id { | |
1420 | Package *package = id == nil ? nil : [[Database sharedInstance] packageWithName:id]; | |
283fc596 | 1421 | |
9b62701b GP |
1422 | [self performSelector:@selector(setProgressError:withTitle:) |
1423 | withObject:error | |
1424 | withObject:(package == nil ? id : [package name]) | |
1425 | ]; | |
6981ccdf JF |
1426 | } |
1427 | ||
a75f53e7 | 1428 | @end |
e057ec05 | 1429 | /* }}} */ |
a75f53e7 | 1430 | |
7a8635c9 JF |
1431 | // Cytore Definitions {{{ |
1432 | struct PackageValue : | |
1433 | Cytore::Block | |
1434 | { | |
1435 | Cytore::Offset<void> reserved_; | |
1436 | Cytore::Offset<PackageValue> next_; | |
1437 | ||
1438 | uint32_t index_ : 23; | |
1439 | uint32_t subscribed_ : 1; | |
1440 | uint32_t : 8; | |
1441 | ||
1442 | int32_t first_; | |
1443 | int32_t last_; | |
1444 | ||
1445 | uint16_t vhash_; | |
1446 | uint16_t nhash_; | |
1447 | ||
1448 | char version_[8]; | |
1449 | char name_[]; | |
1450 | }; | |
1451 | ||
1452 | struct MetaValue : | |
1453 | Cytore::Block | |
1454 | { | |
1455 | Cytore::Offset<void> reserved_; | |
1456 | Cytore::Offset<PackageValue> packages_[1 << 16]; | |
1457 | }; | |
1458 | ||
1459 | static Cytore::File<MetaValue> MetaFile_; | |
1460 | // }}} | |
1461 | // Cytore Helper Functions {{{ | |
8ead5016 | 1462 | static PackageValue *PackageFind(const char *name, size_t length) { |
7a8635c9 JF |
1463 | SplitHash nhash = { hashlittle(name, length) }; |
1464 | ||
1465 | PackageValue *metadata; | |
1466 | ||
1467 | Cytore::Offset<PackageValue> *offset(&MetaFile_->packages_[nhash.u16[0]]); | |
1468 | offset: if (offset->IsNull()) { | |
1469 | *offset = MetaFile_.New<PackageValue>(length + 1); | |
1470 | metadata = &MetaFile_.Get(*offset); | |
1471 | ||
1472 | memcpy(metadata->name_, name, length + 1); | |
1473 | metadata->nhash_ = nhash.u16[1]; | |
1474 | } else { | |
1475 | metadata = &MetaFile_.Get(*offset); | |
1476 | ||
1477 | if (metadata->nhash_ != nhash.u16[1] || strncmp(metadata->name_, name, length + 1) != 0) { | |
1478 | offset = &metadata->next_; | |
1479 | goto offset; | |
1480 | } | |
1481 | } | |
1482 | ||
7a8635c9 JF |
1483 | return metadata; |
1484 | } | |
1485 | ||
1486 | static void PackageImport(const void *key, const void *value, void *context) { | |
1487 | char buffer[1024]; | |
1488 | if (!CFStringGetCString((CFStringRef) key, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { | |
1489 | NSLog(@"failed to import package %@", key); | |
1490 | return; | |
1491 | } | |
1492 | ||
1493 | PackageValue *metadata(PackageFind(buffer, strlen(buffer))); | |
1494 | NSDictionary *package((NSDictionary *) value); | |
1495 | ||
1496 | if (NSNumber *subscribed = [package objectForKey:@"IsSubscribed"]) | |
1497 | if ([subscribed boolValue]) | |
1498 | metadata->subscribed_ = true; | |
1499 | ||
1500 | if (NSDate *date = [package objectForKey:@"FirstSeen"]) { | |
1501 | time_t time([date timeIntervalSince1970]); | |
1502 | if (metadata->first_ > time || metadata->first_ == 0) | |
1503 | metadata->first_ = time; | |
1504 | } | |
1505 | ||
1506 | if (NSDate *date = [package objectForKey:@"LastSeen"]) { | |
1507 | time_t time([date timeIntervalSince1970]); | |
1508 | if (metadata->last_ < time || metadata->last_ == 0) { | |
1509 | metadata->last_ = time; | |
1510 | goto last; | |
1511 | } | |
1512 | } else if (metadata->last_ == 0) last: { | |
1513 | NSString *version([package objectForKey:@"LastVersion"]); | |
1514 | if (CFStringGetCString((CFStringRef) version, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { | |
1515 | size_t length(strlen(buffer)); | |
1516 | uint16_t vhash(hashlittle(buffer, length)); | |
1517 | ||
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; | |
1523 | } | |
1524 | } | |
1525 | } | |
1526 | // }}} | |
1527 | ||
e057ec05 JF |
1528 | /* Source Class {{{ */ |
1529 | @interface Source : NSObject { | |
9050015e | 1530 | CYString depiction_; |
b8b1cfd0 JF |
1531 | CYString description_; |
1532 | CYString label_; | |
1533 | CYString origin_; | |
1534 | CYString support_; | |
a75f53e7 | 1535 | |
b8b1cfd0 JF |
1536 | CYString uri_; |
1537 | CYString distribution_; | |
1538 | CYString type_; | |
1539 | CYString version_; | |
4941f41d | 1540 | |
b8b1cfd0 | 1541 | NSString *host_; |
6b8ef53e | 1542 | NSString *authority_; |
b8b1cfd0 JF |
1543 | |
1544 | CYString defaultIcon_; | |
a75f53e7 | 1545 | |
faf4eb4f | 1546 | NSDictionary *record_; |
e057ec05 | 1547 | BOOL trusted_; |
a75f53e7 JF |
1548 | } |
1549 | ||
b8b1cfd0 | 1550 | - (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool; |
a75f53e7 | 1551 | |
faf4eb4f JF |
1552 | - (NSComparisonResult) compareByNameAndType:(Source *)source; |
1553 | ||
9050015e | 1554 | - (NSString *) depictionForPackage:(NSString *)package; |
3ff1504e JF |
1555 | - (NSString *) supportForPackage:(NSString *)package; |
1556 | ||
faf4eb4f | 1557 | - (NSDictionary *) record; |
e057ec05 | 1558 | - (BOOL) trusted; |
a75f53e7 | 1559 | |
e057ec05 JF |
1560 | - (NSString *) uri; |
1561 | - (NSString *) distribution; | |
1562 | - (NSString *) type; | |
faf4eb4f JF |
1563 | - (NSString *) key; |
1564 | - (NSString *) host; | |
e057ec05 | 1565 | |
faf4eb4f | 1566 | - (NSString *) name; |
e057ec05 JF |
1567 | - (NSString *) description; |
1568 | - (NSString *) label; | |
1569 | - (NSString *) origin; | |
1570 | - (NSString *) version; | |
a75f53e7 | 1571 | |
e057ec05 | 1572 | - (NSString *) defaultIcon; |
faf4eb4f | 1573 | |
a75f53e7 | 1574 | @end |
a75f53e7 | 1575 | |
e057ec05 | 1576 | @implementation Source |
a75f53e7 | 1577 | |
ce09fc27 | 1578 | - (void) _clear { |
b8b1cfd0 JF |
1579 | uri_.clear(); |
1580 | distribution_.clear(); | |
1581 | type_.clear(); | |
ce09fc27 | 1582 | |
b8b1cfd0 JF |
1583 | description_.clear(); |
1584 | label_.clear(); | |
1585 | origin_.clear(); | |
9050015e | 1586 | depiction_.clear(); |
b8b1cfd0 JF |
1587 | support_.clear(); |
1588 | version_.clear(); | |
1589 | defaultIcon_.clear(); | |
1590 | ||
1591 | if (record_ != nil) { | |
1592 | [record_ release]; | |
1593 | record_ = nil; | |
1594 | } | |
1595 | ||
1596 | if (host_ != nil) { | |
1597 | [host_ release]; | |
1598 | host_ = nil; | |
1599 | } | |
419a9efd JF |
1600 | |
1601 | if (authority_ != nil) { | |
1602 | [authority_ release]; | |
1603 | authority_ = nil; | |
1604 | } | |
ce09fc27 | 1605 | } |
a75f53e7 | 1606 | |
ce09fc27 | 1607 | - (void) dealloc { |
a591888a | 1608 | // XXX: this is a very inefficient way to call these deconstructors |
ce09fc27 | 1609 | [self _clear]; |
e057ec05 | 1610 | [super dealloc]; |
a75f53e7 JF |
1611 | } |
1612 | ||
ad554f10 JF |
1613 | + (NSArray *) _attributeKeys { |
1614 | return [NSArray arrayWithObjects:@"description", @"distribution", @"host", @"key", @"label", @"name", @"origin", @"trusted", @"type", @"uri", @"version", nil]; | |
1615 | } | |
1616 | ||
1617 | - (NSArray *) attributeKeys { | |
1618 | return [[self class] _attributeKeys]; | |
1619 | } | |
1620 | ||
1621 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1622 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1623 | } | |
1624 | ||
b8b1cfd0 | 1625 | - (void) setMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { |
ce09fc27 | 1626 | [self _clear]; |
a75f53e7 | 1627 | |
ce09fc27 JF |
1628 | trusted_ = index->IsTrusted(); |
1629 | ||
b8b1cfd0 JF |
1630 | uri_.set(pool, index->GetURI()); |
1631 | distribution_.set(pool, index->GetDist()); | |
1632 | type_.set(pool, index->GetType()); | |
ce09fc27 JF |
1633 | |
1634 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index)); | |
1635 | if (dindex != NULL) { | |
f30eaf83 JF |
1636 | FileFd fd; |
1637 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) | |
1638 | _error->Discard(); | |
1639 | else { | |
1640 | pkgTagFile tags(&fd); | |
b8b1cfd0 | 1641 | |
f30eaf83 JF |
1642 | pkgTagSection section; |
1643 | tags.Step(section); | |
ce09fc27 | 1644 | |
f30eaf83 JF |
1645 | struct { |
1646 | const char *name_; | |
1647 | CYString *value_; | |
1648 | } names[] = { | |
1649 | {"default-icon", &defaultIcon_}, | |
9050015e | 1650 | {"depiction", &depiction_}, |
f30eaf83 JF |
1651 | {"description", &description_}, |
1652 | {"label", &label_}, | |
1653 | {"origin", &origin_}, | |
1654 | {"support", &support_}, | |
1655 | {"version", &version_}, | |
1656 | }; | |
b8b1cfd0 | 1657 | |
f30eaf83 JF |
1658 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { |
1659 | const char *start, *end; | |
1660 | ||
1661 | if (section.Find(names[i].name_, start, end)) { | |
1662 | CYString &value(*names[i].value_); | |
1663 | value.set(pool, start, end - start); | |
1664 | } | |
b8b1cfd0 | 1665 | } |
e057ec05 | 1666 | } |
ce09fc27 | 1667 | } |
faf4eb4f | 1668 | |
ce09fc27 JF |
1669 | record_ = [Sources_ objectForKey:[self key]]; |
1670 | if (record_ != nil) | |
1671 | record_ = [record_ retain]; | |
b8b1cfd0 | 1672 | |
419a9efd JF |
1673 | NSURL *url([NSURL URLWithString:uri_]); |
1674 | ||
1675 | host_ = [url host]; | |
1676 | if (host_ != nil) | |
1677 | host_ = [[host_ lowercaseString] retain]; | |
1678 | ||
1679 | if (host_ != nil) | |
12b7669a | 1680 | authority_ = host_; |
419a9efd JF |
1681 | else |
1682 | authority_ = [url path]; | |
12b7669a JF |
1683 | |
1684 | if (authority_ != nil) | |
1685 | authority_ = [authority_ retain]; | |
ce09fc27 JF |
1686 | } |
1687 | ||
b8b1cfd0 | 1688 | - (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { |
ce09fc27 | 1689 | if ((self = [super init]) != nil) { |
b8b1cfd0 | 1690 | [self setMetaIndex:index inPool:pool]; |
e057ec05 | 1691 | } return self; |
4941f41d | 1692 | } |
a75f53e7 | 1693 | |
faf4eb4f JF |
1694 | - (NSComparisonResult) compareByNameAndType:(Source *)source { |
1695 | NSDictionary *lhr = [self record]; | |
1696 | NSDictionary *rhr = [source record]; | |
1697 | ||
1698 | if (lhr != rhr) | |
1699 | return lhr == nil ? NSOrderedDescending : NSOrderedAscending; | |
1700 | ||
1701 | NSString *lhs = [self name]; | |
1702 | NSString *rhs = [source name]; | |
1703 | ||
1704 | if ([lhs length] != 0 && [rhs length] != 0) { | |
1705 | unichar lhc = [lhs characterAtIndex:0]; | |
1706 | unichar rhc = [rhs characterAtIndex:0]; | |
1707 | ||
1708 | if (isalpha(lhc) && !isalpha(rhc)) | |
1709 | return NSOrderedAscending; | |
1710 | else if (!isalpha(lhc) && isalpha(rhc)) | |
1711 | return NSOrderedDescending; | |
1712 | } | |
1713 | ||
9c4e0cbe | 1714 | return [lhs compare:rhs options:LaxCompareOptions_]; |
faf4eb4f JF |
1715 | } |
1716 | ||
9050015e | 1717 | - (NSString *) depictionForPackage:(NSString *)package { |
cbc40330 | 1718 | return depiction_.empty() ? nil : [static_cast<id>(depiction_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
9050015e JF |
1719 | } |
1720 | ||
3ff1504e | 1721 | - (NSString *) supportForPackage:(NSString *)package { |
cbc40330 | 1722 | return support_.empty() ? nil : [static_cast<id>(support_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
3ff1504e JF |
1723 | } |
1724 | ||
faf4eb4f JF |
1725 | - (NSDictionary *) record { |
1726 | return record_; | |
1727 | } | |
1728 | ||
e057ec05 JF |
1729 | - (BOOL) trusted { |
1730 | return trusted_; | |
1731 | } | |
2d28b35a | 1732 | |
e057ec05 JF |
1733 | - (NSString *) uri { |
1734 | return uri_; | |
1735 | } | |
1cb11c5f | 1736 | |
e057ec05 JF |
1737 | - (NSString *) distribution { |
1738 | return distribution_; | |
1739 | } | |
a75f53e7 | 1740 | |
e057ec05 JF |
1741 | - (NSString *) type { |
1742 | return type_; | |
a75f53e7 JF |
1743 | } |
1744 | ||
faf4eb4f | 1745 | - (NSString *) key { |
b8b1cfd0 | 1746 | return [NSString stringWithFormat:@"%@:%@:%@", (NSString *) type_, (NSString *) uri_, (NSString *) distribution_]; |
faf4eb4f JF |
1747 | } |
1748 | ||
1749 | - (NSString *) host { | |
b8b1cfd0 | 1750 | return host_; |
faf4eb4f JF |
1751 | } |
1752 | ||
1753 | - (NSString *) name { | |
419a9efd | 1754 | return origin_.empty() ? authority_ : origin_; |
faf4eb4f JF |
1755 | } |
1756 | ||
e057ec05 JF |
1757 | - (NSString *) description { |
1758 | return description_; | |
1759 | } | |
686e302f | 1760 | |
e057ec05 | 1761 | - (NSString *) label { |
419a9efd | 1762 | return label_.empty() ? authority_ : label_; |
e057ec05 | 1763 | } |
2d28b35a | 1764 | |
e057ec05 JF |
1765 | - (NSString *) origin { |
1766 | return origin_; | |
1767 | } | |
2d28b35a | 1768 | |
e057ec05 JF |
1769 | - (NSString *) version { |
1770 | return version_; | |
1771 | } | |
4941f41d | 1772 | |
e057ec05 JF |
1773 | - (NSString *) defaultIcon { |
1774 | return defaultIcon_; | |
1775 | } | |
4941f41d | 1776 | |
2a987aa5 JF |
1777 | @end |
1778 | /* }}} */ | |
1779 | /* Relationship Class {{{ */ | |
1780 | @interface Relationship : NSObject { | |
1781 | NSString *type_; | |
1782 | NSString *id_; | |
1783 | } | |
1784 | ||
1785 | - (NSString *) type; | |
1786 | - (NSString *) id; | |
1787 | - (NSString *) name; | |
1788 | ||
1789 | @end | |
1790 | ||
1791 | @implementation Relationship | |
1792 | ||
1793 | - (void) dealloc { | |
1794 | [type_ release]; | |
1795 | [id_ release]; | |
1796 | [super dealloc]; | |
1797 | } | |
1798 | ||
1799 | - (NSString *) type { | |
1800 | return type_; | |
1801 | } | |
1802 | ||
1803 | - (NSString *) id { | |
1804 | return id_; | |
1805 | } | |
1806 | ||
1807 | - (NSString *) name { | |
1808 | _assert(false); | |
1809 | return nil; | |
1810 | } | |
1811 | ||
a75f53e7 | 1812 | @end |
686e302f | 1813 | /* }}} */ |
e057ec05 | 1814 | /* Package Class {{{ */ |
69a5bcac JF |
1815 | struct ParsedPackage { |
1816 | CYString tagline_; | |
1817 | ||
1818 | CYString icon_; | |
1819 | ||
1820 | CYString depiction_; | |
1821 | CYString homepage_; | |
1822 | ||
1823 | CYString sponsor_; | |
1824 | CYString author_; | |
1825 | ||
1826 | CYString bugs_; | |
1827 | CYString support_; | |
1828 | }; | |
1829 | ||
e057ec05 | 1830 | @interface Package : NSObject { |
a70cf746 | 1831 | unsigned era_; |
631a0a1e | 1832 | apr_pool_t *pool_; |
a70cf746 | 1833 | |
3e3977a2 | 1834 | pkgCache::VerIterator version_; |
e057ec05 JF |
1835 | pkgCache::PkgIterator iterator_; |
1836 | _transient Database *database_; | |
e057ec05 | 1837 | pkgCache::VerFileIterator file_; |
3d37fc0d | 1838 | |
e057ec05 | 1839 | Source *source_; |
69a5bcac | 1840 | ParsedPackage *parsed_; |
a75f53e7 | 1841 | |
6932575e | 1842 | CYString section_; |
a591888a | 1843 | _transient NSString *section$_; |
a70cf746 | 1844 | bool essential_; |
5ec44e34 | 1845 | bool obsolete_; |
f159ecd4 | 1846 | |
c7715e92 | 1847 | CYString latest_; |
b4c4fac4 | 1848 | CYString installed_; |
a75f53e7 | 1849 | |
9ee296df | 1850 | CYString id_; |
6932575e | 1851 | CYString name_; |
6932575e | 1852 | |
fbe40361 | 1853 | NSMutableArray *tags_; |
faf4eb4f | 1854 | NSString *role_; |
2a987aa5 | 1855 | |
8ead5016 | 1856 | PackageValue *metadata_; |
7a8635c9 | 1857 | |
1f9a3349 | 1858 | bool ignored_; |
686e302f JF |
1859 | } |
1860 | ||
3e3977a2 | 1861 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; |
6932575e | 1862 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; |
686e302f | 1863 | |
2a987aa5 | 1864 | - (pkgCache::PkgIterator) iterator; |
631a0a1e | 1865 | - (void) parse; |
2a987aa5 | 1866 | |
e057ec05 | 1867 | - (NSString *) section; |
6b4b3bee JF |
1868 | - (NSString *) simpleSection; |
1869 | ||
6932575e JF |
1870 | - (NSString *) longSection; |
1871 | - (NSString *) shortSection; | |
1872 | ||
ce09fc27 JF |
1873 | - (NSString *) uri; |
1874 | ||
e057ec05 JF |
1875 | - (Address *) maintainer; |
1876 | - (size_t) size; | |
66abcbb0 JF |
1877 | - (NSString *) longDescription; |
1878 | - (NSString *) shortDescription; | |
3bddda52 | 1879 | - (unichar) index; |
a75f53e7 | 1880 | |
7a8635c9 | 1881 | - (PackageValue *) metadata; |
8032d797 | 1882 | - (time_t) seen; |
7a8635c9 JF |
1883 | |
1884 | - (bool) subscribed; | |
1885 | - (bool) setSubscribed:(bool)subscribed; | |
1886 | ||
f159ecd4 | 1887 | - (BOOL) ignored; |
63a1e4b8 | 1888 | |
e057ec05 JF |
1889 | - (NSString *) latest; |
1890 | - (NSString *) installed; | |
b4c4fac4 | 1891 | - (BOOL) uninstalled; |
965edd52 JF |
1892 | |
1893 | - (BOOL) valid; | |
238b07ce | 1894 | - (BOOL) upgradableAndEssential:(BOOL)essential; |
e057ec05 JF |
1895 | - (BOOL) essential; |
1896 | - (BOOL) broken; | |
853d14d3 | 1897 | - (BOOL) unfiltered; |
fa7bb92f | 1898 | - (BOOL) visible; |
686e302f | 1899 | |
3319715b JF |
1900 | - (BOOL) half; |
1901 | - (BOOL) halfConfigured; | |
1902 | - (BOOL) halfInstalled; | |
1903 | - (BOOL) hasMode; | |
1904 | - (NSString *) mode; | |
1905 | ||
e057ec05 JF |
1906 | - (NSString *) id; |
1907 | - (NSString *) name; | |
dbe0f181 | 1908 | - (UIImage *) icon; |
ad554f10 | 1909 | - (NSString *) homepage; |
0235116c | 1910 | - (NSString *) depiction; |
d72d91aa | 1911 | - (Address *) author; |
686e302f | 1912 | |
3ff1504e JF |
1913 | - (NSString *) support; |
1914 | ||
f464053e | 1915 | - (NSArray *) files; |
f464053e JF |
1916 | - (NSArray *) warnings; |
1917 | - (NSArray *) applications; | |
2a987aa5 | 1918 | |
e057ec05 | 1919 | - (Source *) source; |
faf4eb4f | 1920 | - (NSString *) role; |
686e302f | 1921 | |
e057ec05 | 1922 | - (BOOL) matches:(NSString *)text; |
686e302f | 1923 | |
faf4eb4f | 1924 | - (bool) hasSupportingRole; |
fa7bb92f | 1925 | - (BOOL) hasTag:(NSString *)tag; |
cb9c2100 | 1926 | - (NSString *) primaryPurpose; |
dbe0f181 | 1927 | - (NSArray *) purposes; |
d8d9a65c | 1928 | - (bool) isCommercial; |
fa7bb92f | 1929 | |
dd9390c5 JF |
1930 | - (CYString &) cyname; |
1931 | ||
6932575e | 1932 | - (uint32_t) compareBySection:(NSArray *)sections; |
f159ecd4 | 1933 | |
e057ec05 JF |
1934 | - (void) install; |
1935 | - (void) remove; | |
7e986211 | 1936 | |
7cf54836 | 1937 | - (bool) isUnfilteredAndSearchedForBy:(NSString *)search; |
5ec44e34 | 1938 | - (bool) isUnfilteredAndSelectedForBy:(NSString *)search; |
1c220bde | 1939 | - (bool) isInstalledAndUnfiltered:(NSNumber *)number; |
6981ccdf | 1940 | - (bool) isVisibleInSection:(NSString *)section; |
7cf54836 | 1941 | - (bool) isVisibleInSource:(Source *)source; |
686e302f | 1942 | |
e057ec05 | 1943 | @end |
686e302f | 1944 | |
6932575e JF |
1945 | uint32_t PackageChangesRadix(Package *self, void *) { |
1946 | union { | |
1947 | uint32_t key; | |
1948 | ||
1949 | struct { | |
1950 | uint32_t timestamp : 30; | |
1951 | uint32_t ignored : 1; | |
1952 | uint32_t upgradable : 1; | |
1953 | } bits; | |
1954 | } value; | |
1955 | ||
1956 | bool upgradable([self upgradableAndEssential:YES]); | |
1957 | value.bits.upgradable = upgradable ? 1 : 0; | |
1958 | ||
1959 | if (upgradable) { | |
1960 | value.bits.timestamp = 0; | |
1961 | value.bits.ignored = [self ignored] ? 0 : 1; | |
1962 | value.bits.upgradable = 1; | |
1963 | } else { | |
05467b1e | 1964 | value.bits.timestamp = [self seen] >> 2; |
6932575e JF |
1965 | value.bits.ignored = 0; |
1966 | value.bits.upgradable = 0; | |
1967 | } | |
1968 | ||
1969 | return _not(uint32_t) - value.key; | |
1970 | } | |
1971 | ||
dd9390c5 JF |
1972 | _finline static void Stifle(uint8_t &value) { |
1973 | } | |
43b742af | 1974 | |
dd9390c5 JF |
1975 | uint32_t PackagePrefixRadix(Package *self, void *context) { |
1976 | size_t offset(reinterpret_cast<size_t>(context)); | |
1977 | CYString &name([self cyname]); | |
43b742af | 1978 | |
dd9390c5 JF |
1979 | size_t size(name.size()); |
1980 | if (size == 0) | |
1981 | return 0; | |
1982 | char *text(name.data()); | |
43b742af | 1983 | |
dd9390c5 JF |
1984 | size_t zeros; |
1985 | if (!isdigit(text[0])) | |
1986 | zeros = 0; | |
1987 | else { | |
1988 | size_t digits(1); | |
1989 | while (size != digits && isdigit(text[digits])) | |
1990 | if (++digits == 4) | |
1991 | break; | |
1992 | zeros = 4 - digits; | |
1993 | } | |
43b742af | 1994 | |
dd9390c5 JF |
1995 | uint8_t data[4]; |
1996 | ||
1997 | // 0.607997 | |
1998 | ||
1999 | if (offset == 0 && zeros != 0) { | |
2000 | memset(data, '0', zeros); | |
2001 | memcpy(data + zeros, text, 4 - zeros); | |
2002 | } else { | |
2003 | /* XXX: there's some danger here if you request a non-zero offset < 4 and it gets zero padded */ | |
2004 | if (size <= offset - zeros) | |
2005 | return 0; | |
2006 | ||
2007 | text += offset - zeros; | |
2008 | size -= offset - zeros; | |
2009 | ||
2010 | if (size >= 4) | |
2011 | memcpy(data, text, 4); | |
2012 | else { | |
2013 | memcpy(data, text, size); | |
2014 | memset(data + size, 0, 4 - size); | |
2015 | } | |
2016 | ||
2017 | for (size_t i(0); i != 4; ++i) | |
2018 | if (isalpha(data[i])) | |
440a8f9a | 2019 | data[i] |= 0x20; |
dd9390c5 JF |
2020 | } |
2021 | ||
2022 | if (offset == 0) | |
440a8f9a JF |
2023 | if (data[0] == '@') |
2024 | data[0] = 0x7f; | |
2025 | else | |
2026 | data[0] = (data[0] & 0x1f) | "\x80\x00\xc0\x40"[data[0] >> 6]; | |
dd9390c5 JF |
2027 | |
2028 | /* XXX: ntohl may be more honest */ | |
2029 | return OSSwapInt32(*reinterpret_cast<uint32_t *>(data)); | |
2030 | } | |
2031 | ||
2032 | CYString &(*PackageName)(Package *self, SEL sel); | |
2033 | ||
2034 | CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { | |
2035 | _profile(PackageNameCompare) | |
2036 | CYString &lhi(PackageName(lhs, @selector(cyname))); | |
2037 | CYString &rhi(PackageName(rhs, @selector(cyname))); | |
2038 | CFStringRef lhn(lhi), rhn(rhi); | |
43b742af | 2039 | |
08383255 JF |
2040 | if (lhn == NULL) |
2041 | return rhn == NULL ? NSOrderedSame : NSOrderedAscending; | |
2042 | else if (rhn == NULL) | |
2043 | return NSOrderedDescending; | |
2044 | ||
43b742af | 2045 | _profile(PackageNameCompare$NumbersLast) |
dd9390c5 | 2046 | if (!lhi.empty() && !rhi.empty()) { |
43b742af JF |
2047 | UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); |
2048 | UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); | |
2049 | bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); | |
2050 | if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) | |
2051 | return lha ? NSOrderedAscending : NSOrderedDescending; | |
2052 | } | |
2053 | _end | |
2054 | ||
dd9390c5 JF |
2055 | CFIndex length = CFStringGetLength(lhn); |
2056 | ||
43b742af JF |
2057 | _profile(PackageNameCompare$Compare) |
2058 | return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, Locale_); | |
2059 | _end | |
2060 | _end | |
2061 | } | |
2062 | ||
66abcbb0 JF |
2063 | CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *context) { |
2064 | return PackageNameCompare(*lhs, *rhs, context); | |
43b742af JF |
2065 | } |
2066 | ||
2067 | struct PackageNameOrdering : | |
2068 | std::binary_function<Package *, Package *, bool> | |
2069 | { | |
2070 | _finline bool operator ()(Package *lhs, Package *rhs) const { | |
2071 | return PackageNameCompare(lhs, rhs, NULL) == NSOrderedAscending; | |
2072 | } | |
2073 | }; | |
2074 | ||
e057ec05 | 2075 | @implementation Package |
686e302f | 2076 | |
dd9390c5 JF |
2077 | - (NSString *) description { |
2078 | return [NSString stringWithFormat:@"<Package:%@>", static_cast<NSString *>(name_)]; | |
2079 | } | |
2080 | ||
e057ec05 | 2081 | - (void) dealloc { |
69a5bcac JF |
2082 | if (parsed_ != NULL) |
2083 | delete parsed_; | |
2084 | ||
3d37fc0d JF |
2085 | if (source_ != nil) |
2086 | [source_ release]; | |
f159ecd4 | 2087 | |
fa7bb92f JF |
2088 | if (tags_ != nil) |
2089 | [tags_ release]; | |
faf4eb4f JF |
2090 | if (role_ != nil) |
2091 | [role_ release]; | |
63a1e4b8 | 2092 | |
e057ec05 | 2093 | [super dealloc]; |
686e302f JF |
2094 | } |
2095 | ||
d8d9a65c JF |
2096 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
2097 | if (selector == @selector(hasTag:)) | |
2098 | return @"hasTag"; | |
2099 | else | |
2100 | return nil; | |
2101 | } | |
2102 | ||
2103 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
2104 | return [self webScriptNameForSelector:selector] == nil; | |
2105 | } | |
2106 | ||
ad554f10 | 2107 | + (NSArray *) _attributeKeys { |
66abcbb0 | 2108 | 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]; |
ad554f10 JF |
2109 | } |
2110 | ||
2111 | - (NSArray *) attributeKeys { | |
2112 | return [[self class] _attributeKeys]; | |
2113 | } | |
2114 | ||
2115 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
2116 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
2117 | } | |
2118 | ||
631a0a1e | 2119 | - (void) parse { |
69a5bcac | 2120 | if (parsed_ != NULL) |
631a0a1e | 2121 | return; |
69a5bcac JF |
2122 | @synchronized (database_) { |
2123 | if ([database_ era] != era_ || file_.end()) | |
631a0a1e JF |
2124 | return; |
2125 | ||
69a5bcac JF |
2126 | ParsedPackage *parsed(new ParsedPackage); |
2127 | parsed_ = parsed; | |
2128 | ||
631a0a1e JF |
2129 | _profile(Package$parse) |
2130 | pkgRecords::Parser *parser; | |
2131 | ||
2132 | _profile(Package$parse$Lookup) | |
2133 | parser = &[database_ records]->Lookup(file_); | |
2134 | _end | |
2135 | ||
2136 | CYString website; | |
2137 | ||
2138 | _profile(Package$parse$Find) | |
2139 | struct { | |
2140 | const char *name_; | |
2141 | CYString *value_; | |
2142 | } names[] = { | |
69a5bcac JF |
2143 | {"icon", &parsed->icon_}, |
2144 | {"depiction", &parsed->depiction_}, | |
2145 | {"homepage", &parsed->homepage_}, | |
631a0a1e | 2146 | {"website", &website}, |
69a5bcac JF |
2147 | {"bugs", &parsed->bugs_}, |
2148 | {"support", &parsed->support_}, | |
2149 | {"sponsor", &parsed->sponsor_}, | |
2150 | {"author", &parsed->author_}, | |
631a0a1e JF |
2151 | }; |
2152 | ||
2153 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { | |
2154 | const char *start, *end; | |
2155 | ||
2156 | if (parser->Find(names[i].name_, start, end)) { | |
2157 | CYString &value(*names[i].value_); | |
2158 | _profile(Package$parse$Value) | |
2159 | value.set(pool_, start, end - start); | |
2160 | _end | |
2161 | } | |
2162 | } | |
2163 | _end | |
2164 | ||
2165 | _profile(Package$parse$Tagline) | |
2166 | const char *start, *end; | |
fbe40361 | 2167 | if (parser->ShortDesc(start, end)) { |
631a0a1e JF |
2168 | const char *stop(reinterpret_cast<const char *>(memchr(start, '\n', end - start))); |
2169 | if (stop == NULL) | |
2170 | stop = end; | |
2171 | while (stop != start && stop[-1] == '\r') | |
2172 | --stop; | |
69a5bcac | 2173 | parsed->tagline_.set(pool_, start, stop - start); |
631a0a1e JF |
2174 | } |
2175 | _end | |
2176 | ||
2177 | _profile(Package$parse$Retain) | |
69a5bcac JF |
2178 | if (parsed->homepage_.empty()) |
2179 | parsed->homepage_ = website; | |
2180 | if (parsed->homepage_ == parsed->depiction_) | |
2181 | parsed->homepage_.clear(); | |
631a0a1e JF |
2182 | _end |
2183 | _end | |
69a5bcac | 2184 | } } |
631a0a1e | 2185 | |
3e3977a2 | 2186 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { |
a70cf746 | 2187 | if ((self = [super init]) != nil) { |
3e3977a2 | 2188 | _profile(Package$initWithVersion) |
a70cf746 | 2189 | era_ = [database era]; |
631a0a1e | 2190 | pool_ = pool; |
a70cf746 | 2191 | |
3e3977a2 | 2192 | version_ = version; |
9c8417b0 JF |
2193 | |
2194 | _profile(Package$initWithVersion$ParentPkg) | |
2195 | iterator_ = version.ParentPkg(); | |
2196 | _end | |
2197 | ||
e057ec05 | 2198 | database_ = database; |
686e302f | 2199 | |
3e3977a2 | 2200 | _profile(Package$initWithVersion$Latest) |
e23a6e8c | 2201 | latest_.set(NULL, StripVersion_(version_.VerStr())); |
3bddda52 | 2202 | _end |
e2a207dd | 2203 | |
631a0a1e JF |
2204 | pkgCache::VerIterator current; |
2205 | _profile(Package$initWithVersion$Versions) | |
2206 | current = iterator_.CurrentVer(); | |
2207 | if (!current.end()) | |
e23a6e8c | 2208 | installed_.set(NULL, StripVersion_(current.VerStr())); |
7e986211 | 2209 | |
631a0a1e JF |
2210 | if (!version_.end()) |
2211 | file_ = version_.FileList(); | |
2212 | else { | |
2213 | pkgCache &cache([database_ cache]); | |
2214 | file_ = pkgCache::VerFileIterator(cache, cache.VerFileP); | |
2215 | } | |
2216 | _end | |
3bddda52 | 2217 | |
3e3977a2 | 2218 | _profile(Package$initWithVersion$Name) |
aba7dd7d | 2219 | id_.set(NULL, iterator_.Name()); |
e23a6e8c | 2220 | name_.set(NULL, iterator_.Display()); |
3bddda52 JF |
2221 | _end |
2222 | ||
9c8417b0 | 2223 | _profile(Package$initWithVersion$lowercaseString) |
aba7dd7d | 2224 | // XXX: do not use tolower() as this is not locale-specific? :( |
c400e87f JF |
2225 | char *data(id_.data()); |
2226 | for (size_t i(0), e(id_.size()); i != e; ++i) | |
aba7dd7d JF |
2227 | if ((data[i] & 0x20) == 0) { |
2228 | id_.copy(pool); | |
2229 | data = id_.data(); | |
2230 | for (; i != e; ++i) | |
2231 | data[i] |= 0x20; | |
2232 | break; | |
2233 | } | |
c400e87f JF |
2234 | _end |
2235 | ||
3e3977a2 | 2236 | _profile(Package$initWithVersion$Tags) |
fbe40361 JF |
2237 | pkgCache::TagIterator tag(iterator_.TagList()); |
2238 | if (!tag.end()) { | |
2239 | tags_ = [[NSMutableArray alloc] initWithCapacity:8]; | |
2240 | do { | |
2241 | const char *name(tag.Name()); | |
a591888a | 2242 | [tags_ addObject:[(NSString *)CYStringCreate(name) autorelease]]; |
7c401ac1 | 2243 | |
bd8e54e1 | 2244 | if (role_ == nil && strncmp(name, "role::", 6) == 0 /*&& strcmp(name, "role::leaper") != 0*/) |
2a0a85d0 | 2245 | role_ = (NSString *) CYStringCreate(name + 6); |
7c401ac1 JF |
2246 | |
2247 | if (strncmp(name, "cydia::", 7) == 0) { | |
2248 | if (strcmp(name + 7, "essential") == 0) | |
2249 | essential_ = true; | |
2250 | else if (strcmp(name + 7, "obsolete") == 0) | |
2251 | obsolete_ = true; | |
2252 | } | |
2253 | ||
fbe40361 JF |
2254 | ++tag; |
2255 | } while (!tag.end()); | |
2256 | } | |
3bddda52 | 2257 | _end |
faf4eb4f | 2258 | |
3e3977a2 | 2259 | _profile(Package$initWithVersion$Metadata) |
8ead5016 JF |
2260 | PackageValue *metadata(PackageFind(id_.data(), id_.size())); |
2261 | metadata_ = metadata; | |
43b742af | 2262 | |
7a8635c9 JF |
2263 | const char *latest(version_.VerStr()); |
2264 | size_t length(strlen(latest)); | |
6b92acab | 2265 | |
7a8635c9 | 2266 | uint16_t vhash(hashlittle(latest, length)); |
43b742af | 2267 | |
7a8635c9 JF |
2268 | size_t capped(std::min<size_t>(8, length)); |
2269 | latest = latest + length - capped; | |
43b742af | 2270 | |
7a8635c9 JF |
2271 | if (metadata->first_ == 0) |
2272 | metadata->first_ = now_; | |
3bddda52 | 2273 | |
7a8635c9 JF |
2274 | if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) { |
2275 | metadata->last_ = now_; | |
2276 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); | |
2277 | metadata->vhash_ = vhash; | |
2278 | } else if (metadata->last_ == 0) | |
2279 | metadata->last_ = metadata->first_; | |
3bddda52 | 2280 | _end |
a70cf746 | 2281 | |
3e3977a2 | 2282 | _profile(Package$initWithVersion$Section) |
e23a6e8c | 2283 | section_.set(NULL, iterator_.Section()); |
6932575e | 2284 | _end |
a70cf746 | 2285 | |
9c8417b0 | 2286 | _profile(Package$initWithVersion$hasTag) |
7c401ac1 | 2287 | essential_ |= ((iterator_->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES); |
9c8417b0 | 2288 | _end |
1f9a3349 DH |
2289 | |
2290 | ignored_ = iterator_->SelectedState == pkgCache::State::Hold; | |
f3d8816a | 2291 | _end } return self; |
a75f53e7 JF |
2292 | } |
2293 | ||
6932575e | 2294 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { |
bb9edf8b JF |
2295 | pkgCache::VerIterator version; |
2296 | ||
2297 | _profile(Package$packageWithIterator$GetCandidateVer) | |
2298 | version = [database policy]->GetCandidateVer(iterator); | |
2299 | _end | |
2300 | ||
3e3977a2 JF |
2301 | if (version.end()) |
2302 | return nil; | |
bb9edf8b | 2303 | |
965edd52 | 2304 | return [[[Package alloc] |
3e3977a2 | 2305 | initWithVersion:version |
6932575e JF |
2306 | withZone:zone |
2307 | inPool:pool | |
965edd52 JF |
2308 | database:database |
2309 | ] autorelease]; | |
f3d8816a | 2310 | } |
a75f53e7 | 2311 | |
2a987aa5 JF |
2312 | - (pkgCache::PkgIterator) iterator { |
2313 | return iterator_; | |
2314 | } | |
2315 | ||
e057ec05 | 2316 | - (NSString *) section { |
6932575e JF |
2317 | if (section$_ == nil) { |
2318 | if (section_.empty()) | |
2319 | return nil; | |
2320 | ||
1c220bde JF |
2321 | _profile(Package$section) |
2322 | std::replace(section_.data(), section_.data() + section_.size(), '_', ' '); | |
2323 | NSString *name(section_); | |
4cc22db3 | 2324 | section$_ = [SectionMap_ objectForKey:name] ?: name; |
1c220bde | 2325 | _end |
6932575e | 2326 | } return section$_; |
a75f53e7 JF |
2327 | } |
2328 | ||
6b4b3bee JF |
2329 | - (NSString *) simpleSection { |
2330 | if (NSString *section = [self section]) | |
2331 | return Simplify(section); | |
2332 | else | |
2333 | return nil; | |
ce09fc27 | 2334 | } |
6b4b3bee | 2335 | |
6932575e | 2336 | - (NSString *) longSection { |
f30eaf83 | 2337 | return LocalizeSection([self section]); |
6932575e JF |
2338 | } |
2339 | ||
2340 | - (NSString *) shortSection { | |
2341 | return [[NSBundle mainBundle] localizedStringForKey:[self simpleSection] value:nil table:@"Sections"]; | |
2342 | } | |
2343 | ||
ce09fc27 JF |
2344 | - (NSString *) uri { |
2345 | return nil; | |
2346 | #if 0 | |
2347 | pkgIndexFile *index; | |
2348 | pkgCache::PkgFileIterator file(file_.File()); | |
2349 | if (![database_ list].FindIndex(file, index)) | |
2350 | return nil; | |
2351 | return [NSString stringWithUTF8String:iterator_->Path]; | |
2352 | //return [NSString stringWithUTF8String:file.Site()]; | |
2353 | //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()]; | |
2354 | #endif | |
6b4b3bee JF |
2355 | } |
2356 | ||
e057ec05 | 2357 | - (Address *) maintainer { |
ff694a50 JF |
2358 | @synchronized (database_) { |
2359 | if ([database_ era] != era_ || file_.end()) | |
965edd52 | 2360 | return nil; |
ff694a50 | 2361 | |
e057ec05 | 2362 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
0a7e5478 JF |
2363 | const std::string &maintainer(parser->Maintainer()); |
2364 | return maintainer.empty() ? nil : [Address addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]]; | |
ff694a50 | 2365 | } } |
b6ffa083 | 2366 | |
e057ec05 | 2367 | - (size_t) size { |
ff694a50 JF |
2368 | @synchronized (database_) { |
2369 | if ([database_ era] != era_ || version_.end()) | |
2370 | return 0; | |
2371 | ||
2372 | return version_->InstalledSize; | |
2373 | } } | |
a75f53e7 | 2374 | |
66abcbb0 | 2375 | - (NSString *) longDescription { |
f3d8816a JF |
2376 | @synchronized (database_) { |
2377 | if ([database_ era] != era_ || file_.end()) | |
965edd52 | 2378 | return nil; |
f3d8816a | 2379 | |
e057ec05 | 2380 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
2a987aa5 | 2381 | NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]); |
b6ffa083 | 2382 | |
e057ec05 JF |
2383 | NSArray *lines = [description componentsSeparatedByString:@"\n"]; |
2384 | NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; | |
2385 | if ([lines count] < 2) | |
2386 | return nil; | |
2d28b35a | 2387 | |
e057ec05 | 2388 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; |
9fdd37d0 | 2389 | for (size_t i(1), e([lines count]); i != e; ++i) { |
e057ec05 JF |
2390 | NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; |
2391 | [trimmed addObject:trim]; | |
2392 | } | |
2d28b35a | 2393 | |
e057ec05 | 2394 | return [trimmed componentsJoinedByString:@"\n"]; |
f3d8816a | 2395 | } } |
a75f53e7 | 2396 | |
66abcbb0 | 2397 | - (NSString *) shortDescription { |
69a5bcac | 2398 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->tagline_); |
66abcbb0 JF |
2399 | } |
2400 | ||
3bddda52 JF |
2401 | - (unichar) index { |
2402 | _profile(Package$index) | |
43b742af JF |
2403 | CFStringRef name((CFStringRef) [self name]); |
2404 | if (CFStringGetLength(name) == 0) | |
3bddda52 | 2405 | return '#'; |
43b742af JF |
2406 | UniChar character(CFStringGetCharacterAtIndex(name, 0)); |
2407 | if (!CFUniCharIsMemberOf(character, kCFUniCharLetterCharacterSet)) | |
3bddda52 | 2408 | return '#'; |
c46df204 | 2409 | return toupper(character); |
3bddda52 | 2410 | _end |
e057ec05 | 2411 | } |
2d28b35a | 2412 | |
7a8635c9 | 2413 | - (PackageValue *) metadata { |
8ead5016 | 2414 | return metadata_; |
f159ecd4 JF |
2415 | } |
2416 | ||
8032d797 | 2417 | - (time_t) seen { |
7a8635c9 JF |
2418 | PackageValue *metadata([self metadata]); |
2419 | return metadata->subscribed_ ? metadata->last_ : metadata->first_; | |
2d28b35a JF |
2420 | } |
2421 | ||
7a8635c9 JF |
2422 | - (bool) subscribed { |
2423 | return [self metadata]->subscribed_; | |
2424 | } | |
2425 | ||
2426 | - (bool) setSubscribed:(bool)subscribed { | |
2427 | PackageValue *metadata([self metadata]); | |
2428 | if (metadata->subscribed_ == subscribed) | |
2429 | return false; | |
2430 | metadata->subscribed_ = subscribed; | |
2431 | return true; | |
f159ecd4 JF |
2432 | } |
2433 | ||
2434 | - (BOOL) ignored { | |
1f9a3349 | 2435 | return ignored_; |
f159ecd4 JF |
2436 | } |
2437 | ||
e057ec05 JF |
2438 | - (NSString *) latest { |
2439 | return latest_; | |
b6ffa083 JF |
2440 | } |
2441 | ||
e057ec05 JF |
2442 | - (NSString *) installed { |
2443 | return installed_; | |
2d28b35a JF |
2444 | } |
2445 | ||
b4c4fac4 JF |
2446 | - (BOOL) uninstalled { |
2447 | return installed_.empty(); | |
2448 | } | |
2449 | ||
965edd52 JF |
2450 | - (BOOL) valid { |
2451 | return !version_.end(); | |
2452 | } | |
2453 | ||
238b07ce | 2454 | - (BOOL) upgradableAndEssential:(BOOL)essential { |
43b742af JF |
2455 | _profile(Package$upgradableAndEssential) |
2456 | pkgCache::VerIterator current(iterator_.CurrentVer()); | |
2457 | if (current.end()) | |
1c220bde | 2458 | return essential && essential_; |
43b742af | 2459 | else |
1c220bde | 2460 | return !version_.end() && version_ != current; |
43b742af | 2461 | _end |
e057ec05 | 2462 | } |
2d28b35a | 2463 | |
e057ec05 | 2464 | - (BOOL) essential { |
a70cf746 | 2465 | return essential_; |
2d28b35a JF |
2466 | } |
2467 | ||
e057ec05 | 2468 | - (BOOL) broken { |
3319715b JF |
2469 | return [database_ cache][iterator_].InstBroken(); |
2470 | } | |
2471 | ||
853d14d3 | 2472 | - (BOOL) unfiltered { |
9c8417b0 JF |
2473 | _profile(Package$unfiltered$obsolete) |
2474 | if (obsolete_) | |
2475 | return false; | |
2476 | _end | |
2477 | ||
2478 | _profile(Package$unfiltered$hasSupportingRole) | |
2479 | if (![self hasSupportingRole]) | |
2480 | return false; | |
2481 | _end | |
2482 | ||
1c220bde JF |
2483 | return true; |
2484 | } | |
9c8417b0 | 2485 | |
1c220bde JF |
2486 | - (BOOL) visible { |
2487 | if (![self unfiltered]) | |
2488 | return false; | |
2489 | ||
2490 | NSString *section([self section]); | |
9c8417b0 | 2491 | |
1c220bde | 2492 | _profile(Package$visible$isSectionVisible) |
9c8417b0 JF |
2493 | if (section != nil && !isSectionVisible(section)) |
2494 | return false; | |
2495 | _end | |
2496 | ||
2497 | return true; | |
853d14d3 JF |
2498 | } |
2499 | ||
3319715b | 2500 | - (BOOL) half { |
43b742af | 2501 | unsigned char current(iterator_->CurrentState); |
3319715b JF |
2502 | return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled; |
2503 | } | |
2504 | ||
2505 | - (BOOL) halfConfigured { | |
2506 | return iterator_->CurrentState == pkgCache::State::HalfConfigured; | |
2507 | } | |
2508 | ||
2509 | - (BOOL) halfInstalled { | |
2510 | return iterator_->CurrentState == pkgCache::State::HalfInstalled; | |
2511 | } | |
2512 | ||
2513 | - (BOOL) hasMode { | |
2514 | pkgDepCache::StateCache &state([database_ cache][iterator_]); | |
2515 | return state.Mode != pkgDepCache::ModeKeep; | |
2516 | } | |
2517 | ||
2518 | - (NSString *) mode { | |
2519 | pkgDepCache::StateCache &state([database_ cache][iterator_]); | |
2520 | ||
2521 | switch (state.Mode) { | |
2522 | case pkgDepCache::ModeDelete: | |
2523 | if ((state.iFlags & pkgDepCache::Purge) != 0) | |
6932575e | 2524 | return @"PURGE"; |
3319715b | 2525 | else |
6932575e | 2526 | return @"REMOVE"; |
3319715b | 2527 | case pkgDepCache::ModeKeep: |
3ff1504e | 2528 | if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
6932575e | 2529 | return @"REINSTALL"; |
3ff1504e JF |
2530 | /*else if ((state.iFlags & pkgDepCache::AutoKept) != 0) |
2531 | return nil;*/ | |
3319715b JF |
2532 | else |
2533 | return nil; | |
3319715b | 2534 | case pkgDepCache::ModeInstall: |
3ff1504e | 2535 | /*if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
6932575e | 2536 | return @"REINSTALL"; |
3ff1504e | 2537 | else*/ switch (state.Status) { |
3319715b | 2538 | case -1: |
6932575e | 2539 | return @"DOWNGRADE"; |
3319715b | 2540 | case 0: |
6932575e | 2541 | return @"INSTALL"; |
3319715b | 2542 | case 1: |
6932575e | 2543 | return @"UPGRADE"; |
3319715b | 2544 | case 2: |
6932575e | 2545 | return @"NEW_INSTALL"; |
6981ccdf | 2546 | _nodefault |
3319715b | 2547 | } |
6981ccdf | 2548 | _nodefault |
3319715b | 2549 | } |
b6ffa083 JF |
2550 | } |
2551 | ||
e057ec05 JF |
2552 | - (NSString *) id { |
2553 | return id_; | |
b6ffa083 JF |
2554 | } |
2555 | ||
e057ec05 | 2556 | - (NSString *) name { |
9ee296df | 2557 | return name_.empty() ? id_ : name_; |
e057ec05 | 2558 | } |
b6ffa083 | 2559 | |
dbe0f181 | 2560 | - (UIImage *) icon { |
6b4b3bee | 2561 | NSString *section = [self simpleSection]; |
dbe0f181 JF |
2562 | |
2563 | UIImage *icon(nil); | |
69a5bcac JF |
2564 | if (parsed_ != NULL) |
2565 | if (NSString *href = parsed_->icon_) | |
2566 | if ([href hasPrefix:@"file:///"]) | |
2567 | // XXX: correct escaping | |
2568 | icon = [UIImage imageAtPath:[href substringFromIndex:7]]; | |
dbe0f181 JF |
2569 | if (icon == nil) if (section != nil) |
2570 | icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]]; | |
a3eb1499 | 2571 | if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon]) |
6e673d99 | 2572 | if ([dicon hasPrefix:@"file:///"]) |
69a8c80e | 2573 | // XXX: correct escaping |
6e673d99 | 2574 | icon = [UIImage imageAtPath:[dicon substringFromIndex:7]]; |
dbe0f181 JF |
2575 | if (icon == nil) |
2576 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
2577 | return icon; | |
e057ec05 | 2578 | } |
b6ffa083 | 2579 | |
ad554f10 | 2580 | - (NSString *) homepage { |
69a5bcac | 2581 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->homepage_); |
b6ffa083 JF |
2582 | } |
2583 | ||
0235116c | 2584 | - (NSString *) depiction { |
69a5bcac | 2585 | return parsed_ != NULL && !parsed_->depiction_.empty() ? parsed_->depiction_ : [[self source] depictionForPackage:id_]; |
0235116c JF |
2586 | } |
2587 | ||
81ab76dc | 2588 | - (Address *) sponsor { |
69a5bcac | 2589 | return parsed_ == NULL || parsed_->sponsor_.empty() ? nil : [Address addressWithString:parsed_->sponsor_]; |
81ab76dc JF |
2590 | } |
2591 | ||
d72d91aa | 2592 | - (Address *) author { |
69a5bcac | 2593 | return parsed_ == NULL || parsed_->author_.empty() ? nil : [Address addressWithString:parsed_->author_]; |
d72d91aa JF |
2594 | } |
2595 | ||
3ff1504e | 2596 | - (NSString *) support { |
69a5bcac | 2597 | return parsed_ != NULL && !parsed_->bugs_.empty() ? parsed_->bugs_ : [[self source] supportForPackage:id_]; |
3ff1504e JF |
2598 | } |
2599 | ||
f464053e | 2600 | - (NSArray *) files { |
9ee296df | 2601 | NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", static_cast<NSString *>(id_)]; |
f464053e JF |
2602 | NSMutableArray *files = [NSMutableArray arrayWithCapacity:128]; |
2603 | ||
2604 | std::ifstream fin; | |
2605 | fin.open([path UTF8String]); | |
2606 | if (!fin.is_open()) | |
2607 | return nil; | |
2608 | ||
2609 | std::string line; | |
2610 | while (std::getline(fin, line)) | |
2611 | [files addObject:[NSString stringWithUTF8String:line.c_str()]]; | |
2612 | ||
2613 | return files; | |
2614 | } | |
2615 | ||
f464053e JF |
2616 | - (NSArray *) warnings { |
2617 | NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); | |
2618 | const char *name(iterator_.Name()); | |
2619 | ||
2620 | size_t length(strlen(name)); | |
2621 | if (length < 2) invalid: | |
61b13cae | 2622 | [warnings addObject:UCLocalize("ILLEGAL_PACKAGE_IDENTIFIER")]; |
f464053e JF |
2623 | else for (size_t i(0); i != length; ++i) |
2624 | if ( | |
8944281a JF |
2625 | /* XXX: technically this is not allowed */ |
2626 | (name[i] < 'A' || name[i] > 'Z') && | |
f464053e JF |
2627 | (name[i] < 'a' || name[i] > 'z') && |
2628 | (name[i] < '0' || name[i] > '9') && | |
2629 | (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.') | |
2630 | ) goto invalid; | |
2631 | ||
2632 | if (strcmp(name, "cydia") != 0) { | |
2633 | bool cydia = false; | |
419a9efd | 2634 | bool user = false; |
8944281a | 2635 | bool _private = false; |
f464053e JF |
2636 | bool stash = false; |
2637 | ||
8944281a JF |
2638 | bool repository = [[self section] isEqualToString:@"Repositories"]; |
2639 | ||
f464053e JF |
2640 | if (NSArray *files = [self files]) |
2641 | for (NSString *file in files) | |
2642 | if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"]) | |
2643 | cydia = true; | |
419a9efd JF |
2644 | else if (!user && [file isEqualToString:@"/User"]) |
2645 | user = true; | |
8944281a JF |
2646 | else if (!_private && [file isEqualToString:@"/private"]) |
2647 | _private = true; | |
f464053e JF |
2648 | else if (!stash && [file isEqualToString:@"/var/stash"]) |
2649 | stash = true; | |
2650 | ||
8944281a JF |
2651 | /* XXX: this is not sensitive enough. only some folders are valid. */ |
2652 | if (cydia && !repository) | |
61b13cae | 2653 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"Cydia.app"]]; |
419a9efd JF |
2654 | if (user) |
2655 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/User"]]; | |
8944281a | 2656 | if (_private) |
61b13cae | 2657 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; |
f464053e | 2658 | if (stash) |
61b13cae | 2659 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; |
f464053e JF |
2660 | } |
2661 | ||
2662 | return [warnings count] == 0 ? nil : warnings; | |
2663 | } | |
2664 | ||
2665 | - (NSArray *) applications { | |
2666 | NSString *me([[NSBundle mainBundle] bundleIdentifier]); | |
2667 | ||
2668 | NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); | |
2669 | ||
2670 | static Pcre application_r("^/Applications/(.*)\\.app/Info.plist$"); | |
2671 | if (NSArray *files = [self files]) | |
2672 | for (NSString *file in files) | |
2673 | if (application_r(file)) { | |
2674 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); | |
2675 | NSString *id([info objectForKey:@"CFBundleIdentifier"]); | |
2676 | if ([id isEqualToString:me]) | |
2677 | continue; | |
2678 | ||
2679 | NSString *display([info objectForKey:@"CFBundleDisplayName"]); | |
2680 | if (display == nil) | |
2681 | display = application_r[1]; | |
2682 | ||
2683 | NSString *bundle([file stringByDeletingLastPathComponent]); | |
2684 | NSString *icon([info objectForKey:@"CFBundleIconFile"]); | |
2685 | if (icon == nil || [icon length] == 0) | |
2686 | icon = @"icon.png"; | |
2687 | NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]); | |
2688 | ||
2689 | NSMutableArray *application([NSMutableArray arrayWithCapacity:2]); | |
2690 | [applications addObject:application]; | |
2691 | ||
2692 | [application addObject:id]; | |
2693 | [application addObject:display]; | |
2694 | [application addObject:url]; | |
2695 | } | |
2696 | ||
2697 | return [applications count] == 0 ? nil : applications; | |
2698 | } | |
2699 | ||
e057ec05 | 2700 | - (Source *) source { |
678b0973 | 2701 | if (source_ == nil) { |
a70cf746 JF |
2702 | @synchronized (database_) { |
2703 | if ([database_ era] != era_ || file_.end()) | |
678b0973 JF |
2704 | source_ = (Source *) [NSNull null]; |
2705 | else | |
2706 | source_ = [([database_ getSource:file_.File()] ?: (Source *) [NSNull null]) retain]; | |
a70cf746 | 2707 | } |
3d37fc0d JF |
2708 | } |
2709 | ||
678b0973 | 2710 | return source_ == (Source *) [NSNull null] ? nil : source_; |
b6ffa083 JF |
2711 | } |
2712 | ||
faf4eb4f JF |
2713 | - (NSString *) role { |
2714 | return role_; | |
2715 | } | |
2716 | ||
e057ec05 JF |
2717 | - (BOOL) matches:(NSString *)text { |
2718 | if (text == nil) | |
2719 | return NO; | |
b6ffa083 | 2720 | |
e057ec05 | 2721 | NSRange range; |
b6ffa083 | 2722 | |
3bddda52 | 2723 | range = [[self id] rangeOfString:text options:MatchCompareOptions_]; |
e057ec05 JF |
2724 | if (range.location != NSNotFound) |
2725 | return YES; | |
b6ffa083 | 2726 | |
3bddda52 | 2727 | range = [[self name] rangeOfString:text options:MatchCompareOptions_]; |
e057ec05 JF |
2728 | if (range.location != NSNotFound) |
2729 | return YES; | |
b6ffa083 | 2730 | |
5ec44e34 | 2731 | range = [[self shortDescription] rangeOfString:text options:MatchCompareOptions_]; |
e057ec05 | 2732 | if (range.location != NSNotFound) |
5ec44e34 | 2733 | return YES; |
b6ffa083 | 2734 | |
e057ec05 JF |
2735 | return NO; |
2736 | } | |
b6ffa083 | 2737 | |
faf4eb4f JF |
2738 | - (bool) hasSupportingRole { |
2739 | if (role_ == nil) | |
fa7bb92f | 2740 | return true; |
faf4eb4f JF |
2741 | if ([role_ isEqualToString:@"enduser"]) |
2742 | return true; | |
2743 | if ([Role_ isEqualToString:@"User"]) | |
2744 | return false; | |
2745 | if ([role_ isEqualToString:@"hacker"]) | |
2746 | return true; | |
2747 | if ([Role_ isEqualToString:@"Hacker"]) | |
2748 | return false; | |
2749 | if ([role_ isEqualToString:@"developer"]) | |
2750 | return true; | |
2751 | if ([Role_ isEqualToString:@"Developer"]) | |
2752 | return false; | |
2753 | _assert(false); | |
fa7bb92f JF |
2754 | } |
2755 | ||
2756 | - (BOOL) hasTag:(NSString *)tag { | |
2757 | return tags_ == nil ? NO : [tags_ containsObject:tag]; | |
2758 | } | |
2759 | ||
cb9c2100 JF |
2760 | - (NSString *) primaryPurpose { |
2761 | for (NSString *tag in tags_) | |
2762 | if ([tag hasPrefix:@"purpose::"]) | |
2763 | return [tag substringFromIndex:9]; | |
2764 | return nil; | |
2765 | } | |
2766 | ||
dbe0f181 JF |
2767 | - (NSArray *) purposes { |
2768 | NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]); | |
2769 | for (NSString *tag in tags_) | |
2770 | if ([tag hasPrefix:@"purpose::"]) | |
2771 | [purposes addObject:[tag substringFromIndex:9]]; | |
2772 | return [purposes count] == 0 ? nil : purposes; | |
2773 | } | |
2774 | ||
d8d9a65c JF |
2775 | - (bool) isCommercial { |
2776 | return [self hasTag:@"cydia::commercial"]; | |
2777 | } | |
2778 | ||
dd9390c5 JF |
2779 | - (CYString &) cyname { |
2780 | return name_.empty() ? id_ : name_; | |
6932575e JF |
2781 | } |
2782 | ||
6932575e JF |
2783 | - (uint32_t) compareBySection:(NSArray *)sections { |
2784 | NSString *section([self section]); | |
2785 | for (size_t i(0), e([sections count]); i != e; ++i) { | |
2786 | if ([section isEqualToString:[[sections objectAtIndex:i] name]]) | |
2787 | return i; | |
e057ec05 | 2788 | } |
2d28b35a | 2789 | |
6932575e | 2790 | return _not(uint32_t); |
e057ec05 | 2791 | } |
2d28b35a | 2792 | |
3ff1504e | 2793 | - (void) clear { |
30746bd9 | 2794 | @synchronized (database_) { |
3ff1504e JF |
2795 | pkgProblemResolver *resolver = [database_ resolver]; |
2796 | resolver->Clear(iterator_); | |
30746bd9 JF |
2797 | |
2798 | pkgCacheFile &cache([database_ cache]); | |
2799 | cache->SetReInstall(iterator_, false); | |
2800 | cache->MarkKeep(iterator_, false); | |
2801 | } } | |
3ff1504e | 2802 | |
e057ec05 | 2803 | - (void) install { |
30746bd9 | 2804 | @synchronized (database_) { |
e057ec05 JF |
2805 | pkgProblemResolver *resolver = [database_ resolver]; |
2806 | resolver->Clear(iterator_); | |
2807 | resolver->Protect(iterator_); | |
30746bd9 | 2808 | |
e057ec05 | 2809 | pkgCacheFile &cache([database_ cache]); |
30746bd9 | 2810 | cache->SetReInstall(iterator_, false); |
e057ec05 | 2811 | cache->MarkInstall(iterator_, false); |
30746bd9 | 2812 | |
e057ec05 JF |
2813 | pkgDepCache::StateCache &state((*cache)[iterator_]); |
2814 | if (!state.Install()) | |
2815 | cache->SetReInstall(iterator_, true); | |
30746bd9 | 2816 | } } |
0f25fa58 | 2817 | |
e057ec05 | 2818 | - (void) remove { |
30746bd9 | 2819 | @synchronized (database_) { |
e057ec05 JF |
2820 | pkgProblemResolver *resolver = [database_ resolver]; |
2821 | resolver->Clear(iterator_); | |
e057ec05 | 2822 | resolver->Remove(iterator_); |
30746bd9 JF |
2823 | resolver->Protect(iterator_); |
2824 | ||
2825 | pkgCacheFile &cache([database_ cache]); | |
2826 | cache->SetReInstall(iterator_, false); | |
2827 | cache->MarkDelete(iterator_, true); | |
2828 | } } | |
b6ffa083 | 2829 | |
7cf54836 | 2830 | - (bool) isUnfilteredAndSearchedForBy:(NSString *)search { |
3bddda52 JF |
2831 | _profile(Package$isUnfilteredAndSearchedForBy) |
2832 | bool value(true); | |
2833 | ||
2834 | _profile(Package$isUnfilteredAndSearchedForBy$Unfiltered) | |
2835 | value &= [self unfiltered]; | |
2836 | _end | |
2837 | ||
2838 | _profile(Package$isUnfilteredAndSearchedForBy$Match) | |
2839 | value &= [self matches:search]; | |
2840 | _end | |
2841 | ||
7cf54836 | 2842 | return value; |
3bddda52 | 2843 | _end |
e057ec05 | 2844 | } |
b6ffa083 | 2845 | |
5ec44e34 | 2846 | - (bool) isUnfilteredAndSelectedForBy:(NSString *)search { |
37455cf8 JF |
2847 | if ([search length] == 0) |
2848 | return false; | |
2849 | ||
5ec44e34 JF |
2850 | _profile(Package$isUnfilteredAndSelectedForBy) |
2851 | bool value(true); | |
2852 | ||
2853 | _profile(Package$isUnfilteredAndSelectedForBy$Unfiltered) | |
2854 | value &= [self unfiltered]; | |
2855 | _end | |
2856 | ||
2857 | _profile(Package$isUnfilteredAndSelectedForBy$Match) | |
2858 | value &= [[self name] compare:search options:MatchCompareOptions_ range:NSMakeRange(0, [search length])] == NSOrderedSame; | |
2859 | _end | |
2860 | ||
2861 | return value; | |
2862 | _end | |
2863 | } | |
2864 | ||
1c220bde JF |
2865 | - (bool) isInstalledAndUnfiltered:(NSNumber *)number { |
2866 | return ![self uninstalled] && (![number boolValue] && ![role_ isEqualToString:@"cydia"] || [self unfiltered]); | |
e057ec05 | 2867 | } |
b6ffa083 | 2868 | |
6981ccdf | 2869 | - (bool) isVisibleInSection:(NSString *)name { |
1c220bde | 2870 | NSString *section([self section]); |
965edd52 | 2871 | |
1c220bde JF |
2872 | return ( |
2873 | name == nil || | |
2874 | section == nil && [name length] == 0 || | |
2875 | [name isEqualToString:section] | |
2876 | ) && [self visible]; | |
faf4eb4f JF |
2877 | } |
2878 | ||
7cf54836 JF |
2879 | - (bool) isVisibleInSource:(Source *)source { |
2880 | return [self source] == source && [self visible]; | |
e057ec05 | 2881 | } |
b6ffa083 | 2882 | |
e057ec05 JF |
2883 | @end |
2884 | /* }}} */ | |
2885 | /* Section Class {{{ */ | |
2886 | @interface Section : NSObject { | |
2887 | NSString *name_; | |
3bddda52 | 2888 | unichar index_; |
e057ec05 JF |
2889 | size_t row_; |
2890 | size_t count_; | |
6932575e | 2891 | NSString *localized_; |
e057ec05 | 2892 | } |
2d28b35a | 2893 | |
43b742af | 2894 | - (NSComparisonResult) compareByLocalized:(Section *)section; |
9ee296df JF |
2895 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; |
2896 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; | |
2897 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; | |
3bddda52 | 2898 | - (Section *) initWithIndex:(unichar)index row:(size_t)row; |
e057ec05 | 2899 | - (NSString *) name; |
3bddda52 | 2900 | - (unichar) index; |
6932575e | 2901 | |
e057ec05 JF |
2902 | - (size_t) row; |
2903 | - (size_t) count; | |
6932575e JF |
2904 | |
2905 | - (void) addToRow; | |
e057ec05 | 2906 | - (void) addToCount; |
a933cee2 | 2907 | |
6932575e | 2908 | - (void) setCount:(size_t)count; |
43b742af | 2909 | - (NSString *) localized; |
6932575e | 2910 | |
e057ec05 | 2911 | @end |
a933cee2 | 2912 | |
e057ec05 | 2913 | @implementation Section |
a933cee2 | 2914 | |
e057ec05 JF |
2915 | - (void) dealloc { |
2916 | [name_ release]; | |
6932575e JF |
2917 | if (localized_ != nil) |
2918 | [localized_ release]; | |
e057ec05 JF |
2919 | [super dealloc]; |
2920 | } | |
a933cee2 | 2921 | |
43b742af | 2922 | - (NSComparisonResult) compareByLocalized:(Section *)section { |
9ee296df JF |
2923 | NSString *lhs(localized_); |
2924 | NSString *rhs([section localized]); | |
fa7bb92f | 2925 | |
9ee296df | 2926 | /*if ([lhs length] != 0 && [rhs length] != 0) { |
fa7bb92f JF |
2927 | unichar lhc = [lhs characterAtIndex:0]; |
2928 | unichar rhc = [rhs characterAtIndex:0]; | |
2929 | ||
2930 | if (isalpha(lhc) && !isalpha(rhc)) | |
2931 | return NSOrderedAscending; | |
2932 | else if (!isalpha(lhc) && isalpha(rhc)) | |
2933 | return NSOrderedDescending; | |
9ee296df | 2934 | }*/ |
fa7bb92f | 2935 | |
9c4e0cbe | 2936 | return [lhs compare:rhs options:LaxCompareOptions_]; |
fa7bb92f JF |
2937 | } |
2938 | ||
9ee296df JF |
2939 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized { |
2940 | if ((self = [self initWithName:name localize:NO]) != nil) { | |
2941 | if (localized != nil) | |
2942 | localized_ = [localized retain]; | |
2943 | } return self; | |
2944 | } | |
2945 | ||
2946 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize { | |
2947 | return [self initWithName:name row:0 localize:localize]; | |
fa7bb92f JF |
2948 | } |
2949 | ||
9ee296df | 2950 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { |
e057ec05 JF |
2951 | if ((self = [super init]) != nil) { |
2952 | name_ = [name retain]; | |
3bddda52 JF |
2953 | index_ = '\0'; |
2954 | row_ = row; | |
9ee296df JF |
2955 | if (localize) |
2956 | localized_ = [LocalizeSection(name_) retain]; | |
3bddda52 JF |
2957 | } return self; |
2958 | } | |
2959 | ||
6932575e | 2960 | /* XXX: localize the index thingees */ |
3bddda52 JF |
2961 | - (Section *) initWithIndex:(unichar)index row:(size_t)row { |
2962 | if ((self = [super init]) != nil) { | |
5d8f1006 | 2963 | name_ = [[NSString stringWithCharacters:&index length:1] retain]; |
3bddda52 | 2964 | index_ = index; |
e057ec05 | 2965 | row_ = row; |
a933cee2 JF |
2966 | } return self; |
2967 | } | |
2968 | ||
e057ec05 JF |
2969 | - (NSString *) name { |
2970 | return name_; | |
2971 | } | |
2972 | ||
3bddda52 JF |
2973 | - (unichar) index { |
2974 | return index_; | |
2975 | } | |
2976 | ||
e057ec05 JF |
2977 | - (size_t) row { |
2978 | return row_; | |
2979 | } | |
2980 | ||
2981 | - (size_t) count { | |
2982 | return count_; | |
2983 | } | |
2984 | ||
6932575e JF |
2985 | - (void) addToRow { |
2986 | ++row_; | |
2987 | } | |
2988 | ||
e057ec05 JF |
2989 | - (void) addToCount { |
2990 | ++count_; | |
2991 | } | |
2992 | ||
6932575e JF |
2993 | - (void) setCount:(size_t)count { |
2994 | count_ = count; | |
2995 | } | |
2996 | ||
2997 | - (NSString *) localized { | |
2998 | return localized_; | |
2999 | } | |
3000 | ||
a933cee2 JF |
3001 | @end |
3002 | /* }}} */ | |
3003 | ||
6981ccdf | 3004 | static NSString *Colon_; |
835b451e | 3005 | static NSString *Elision_; |
6981ccdf JF |
3006 | static NSString *Error_; |
3007 | static NSString *Warning_; | |
3008 | ||
e057ec05 JF |
3009 | /* Database Implementation {{{ */ |
3010 | @implementation Database | |
1cb11c5f | 3011 | |
dbe0f181 JF |
3012 | + (Database *) sharedInstance { |
3013 | static Database *instance; | |
3014 | if (instance == nil) | |
3015 | instance = [[Database alloc] init]; | |
3016 | return instance; | |
3017 | } | |
3018 | ||
a70cf746 JF |
3019 | - (unsigned) era { |
3020 | return era_; | |
3021 | } | |
3022 | ||
e733869b JF |
3023 | - (void) releasePackages { |
3024 | CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL); | |
3025 | CFArrayRemoveAllValues(packages_); | |
3026 | } | |
3027 | ||
e057ec05 | 3028 | - (void) dealloc { |
a591888a | 3029 | // XXX: actually implement this thing |
e057ec05 | 3030 | _assert(false); |
e733869b | 3031 | [self releasePackages]; |
6932575e | 3032 | apr_pool_destroy(pool_); |
e733869b | 3033 | NSRecycleZone(zone_); |
e057ec05 JF |
3034 | [super dealloc]; |
3035 | } | |
1cb11c5f | 3036 | |
f464053e | 3037 | - (void) _readCydia:(NSNumber *)fd { _pooled |
d72d91aa JF |
3038 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3039 | std::istream is(&ib); | |
3040 | std::string line; | |
3041 | ||
03d01f0e JF |
3042 | static Pcre finish_r("^finish:([^:]*)$"); |
3043 | ||
d72d91aa JF |
3044 | while (std::getline(is, line)) { |
3045 | const char *data(line.c_str()); | |
03d01f0e | 3046 | size_t size = line.size(); |
cb9c2100 | 3047 | lprintf("C:%s\n", data); |
03d01f0e JF |
3048 | |
3049 | if (finish_r(data, size)) { | |
3050 | NSString *finish = finish_r[1]; | |
3051 | int index = [Finishes_ indexOfObject:finish]; | |
3052 | if (index != INT_MAX && index > Finish_) | |
3053 | Finish_ = index; | |
3054 | } | |
d72d91aa JF |
3055 | } |
3056 | ||
6981ccdf | 3057 | _assume(false); |
d72d91aa JF |
3058 | } |
3059 | ||
f464053e | 3060 | - (void) _readStatus:(NSNumber *)fd { _pooled |
e057ec05 JF |
3061 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3062 | std::istream is(&ib); | |
3063 | std::string line; | |
1cb11c5f | 3064 | |
faf4eb4f JF |
3065 | static Pcre conffile_r("^status: [^ ]* : conffile-prompt : (.*?) *$"); |
3066 | static Pcre pmstatus_r("^([^:]*):([^:]*):([^:]*):(.*)$"); | |
1cb11c5f | 3067 | |
e057ec05 JF |
3068 | while (std::getline(is, line)) { |
3069 | const char *data(line.c_str()); | |
6981ccdf | 3070 | size_t size(line.size()); |
cb9c2100 | 3071 | lprintf("S:%s\n", data); |
1cb11c5f | 3072 | |
965edd52 JF |
3073 | if (conffile_r(data, size)) { |
3074 | [delegate_ setConfigurationData:conffile_r[1]]; | |
3075 | } else if (strncmp(data, "status: ", 8) == 0) { | |
3076 | NSString *string = [NSString stringWithUTF8String:(data + 8)]; | |
e057ec05 | 3077 | [delegate_ setProgressTitle:string]; |
965edd52 | 3078 | } else if (pmstatus_r(data, size)) { |
238b07ce JF |
3079 | std::string type([pmstatus_r[1] UTF8String]); |
3080 | NSString *id = pmstatus_r[2]; | |
3081 | ||
965edd52 JF |
3082 | float percent([pmstatus_r[3] floatValue]); |
3083 | [delegate_ setProgressPercent:(percent / 100)]; | |
3084 | ||
3085 | NSString *string = pmstatus_r[4]; | |
965edd52 JF |
3086 | |
3087 | if (type == "pmerror") | |
6981ccdf | 3088 | [delegate_ performSelectorOnMainThread:@selector(_setProgressErrorPackage:) |
238b07ce JF |
3089 | withObject:[NSArray arrayWithObjects:string, id, nil] |
3090 | waitUntilDone:YES | |
3091 | ]; | |
907a35d6 | 3092 | else if (type == "pmstatus") { |
965edd52 | 3093 | [delegate_ setProgressTitle:string]; |
907a35d6 | 3094 | } else if (type == "pmconffile") |
965edd52 | 3095 | [delegate_ setConfigurationData:string]; |
6981ccdf JF |
3096 | else |
3097 | lprintf("E:unknown pmstatus\n"); | |
3098 | } else | |
3099 | lprintf("E:unknown status\n"); | |
e057ec05 | 3100 | } |
1cb11c5f | 3101 | |
6981ccdf | 3102 | _assume(false); |
e057ec05 | 3103 | } |
1cb11c5f | 3104 | |
f464053e | 3105 | - (void) _readOutput:(NSNumber *)fd { _pooled |
e057ec05 JF |
3106 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3107 | std::istream is(&ib); | |
3108 | std::string line; | |
3109 | ||
965edd52 | 3110 | while (std::getline(is, line)) { |
cb9c2100 | 3111 | lprintf("O:%s\n", line.c_str()); |
2a987aa5 | 3112 | [delegate_ addProgressOutput:[NSString stringWithUTF8String:line.c_str()]]; |
965edd52 | 3113 | } |
e057ec05 | 3114 | |
6981ccdf | 3115 | _assume(false); |
1cb11c5f JF |
3116 | } |
3117 | ||
7600bd69 JF |
3118 | - (FILE *) input { |
3119 | return input_; | |
3120 | } | |
3121 | ||
e057ec05 | 3122 | - (Package *) packageWithName:(NSString *)name { |
f3d8816a | 3123 | @synchronized (self) { |
965edd52 JF |
3124 | if (static_cast<pkgDepCache *>(cache_) == NULL) |
3125 | return nil; | |
e057ec05 | 3126 | pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); |
6932575e | 3127 | return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:pool_ database:self]; |
ca06bb0e | 3128 | } } |
e057ec05 | 3129 | |
9b62701b | 3130 | - (id) init { |
1cb11c5f | 3131 | if ((self = [super init]) != nil) { |
965edd52 | 3132 | policy_ = NULL; |
e057ec05 JF |
3133 | records_ = NULL; |
3134 | resolver_ = NULL; | |
3135 | fetcher_ = NULL; | |
3136 | lock_ = NULL; | |
1cb11c5f | 3137 | |
6932575e JF |
3138 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
3139 | apr_pool_create(&pool_, NULL); | |
3140 | ||
777744da | 3141 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL); |
1cb11c5f | 3142 | |
e057ec05 | 3143 | int fds[2]; |
1cb11c5f | 3144 | |
d72d91aa JF |
3145 | _assert(pipe(fds) != -1); |
3146 | cydiafd_ = fds[1]; | |
3147 | ||
3148 | _config->Set("APT::Keep-Fds::", cydiafd_); | |
03d01f0e | 3149 | setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int)); |
d72d91aa JF |
3150 | |
3151 | [NSThread | |
3152 | detachNewThreadSelector:@selector(_readCydia:) | |
3153 | toTarget:self | |
a591888a | 3154 | withObject:[NSNumber numberWithInt:fds[0]] |
d72d91aa JF |
3155 | ]; |
3156 | ||
e057ec05 JF |
3157 | _assert(pipe(fds) != -1); |
3158 | statusfd_ = fds[1]; | |
1cb11c5f | 3159 | |
e057ec05 JF |
3160 | [NSThread |
3161 | detachNewThreadSelector:@selector(_readStatus:) | |
3162 | toTarget:self | |
a591888a | 3163 | withObject:[NSNumber numberWithInt:fds[0]] |
e057ec05 | 3164 | ]; |
1cb11c5f | 3165 | |
7600bd69 JF |
3166 | _assert(pipe(fds) != -1); |
3167 | _assert(dup2(fds[0], 0) != -1); | |
3168 | _assert(close(fds[0]) != -1); | |
3169 | ||
3170 | input_ = fdopen(fds[1], "a"); | |
3171 | ||
e057ec05 JF |
3172 | _assert(pipe(fds) != -1); |
3173 | _assert(dup2(fds[1], 1) != -1); | |
3174 | _assert(close(fds[1]) != -1); | |
3175 | ||
3176 | [NSThread | |
3177 | detachNewThreadSelector:@selector(_readOutput:) | |
3178 | toTarget:self | |
a591888a | 3179 | withObject:[NSNumber numberWithInt:fds[0]] |
e057ec05 | 3180 | ]; |
1cb11c5f JF |
3181 | } return self; |
3182 | } | |
3183 | ||
e057ec05 JF |
3184 | - (pkgCacheFile &) cache { |
3185 | return cache_; | |
1cb11c5f JF |
3186 | } |
3187 | ||
965edd52 JF |
3188 | - (pkgDepCache::Policy *) policy { |
3189 | return policy_; | |
3190 | } | |
3191 | ||
e057ec05 JF |
3192 | - (pkgRecords *) records { |
3193 | return records_; | |
1cb11c5f JF |
3194 | } |
3195 | ||
e057ec05 JF |
3196 | - (pkgProblemResolver *) resolver { |
3197 | return resolver_; | |
1cb11c5f JF |
3198 | } |
3199 | ||
e057ec05 JF |
3200 | - (pkgAcquire &) fetcher { |
3201 | return *fetcher_; | |
1cb11c5f JF |
3202 | } |
3203 | ||
ce09fc27 JF |
3204 | - (pkgSourceList &) list { |
3205 | return *list_; | |
3206 | } | |
3207 | ||
e057ec05 | 3208 | - (NSArray *) packages { |
777744da | 3209 | return (NSArray *) packages_; |
1cb11c5f JF |
3210 | } |
3211 | ||
faf4eb4f | 3212 | - (NSArray *) sources { |
631a0a1e JF |
3213 | NSMutableArray *sources([NSMutableArray arrayWithCapacity:sources_.size()]); |
3214 | for (SourceMap::const_iterator i(sources_.begin()); i != sources_.end(); ++i) | |
3215 | [sources addObject:i->second]; | |
3216 | return sources; | |
faf4eb4f JF |
3217 | } |
3218 | ||
f464053e JF |
3219 | - (NSArray *) issues { |
3220 | if (cache_->BrokenCount() == 0) | |
3221 | return nil; | |
3222 | ||
3223 | NSMutableArray *issues([NSMutableArray arrayWithCapacity:4]); | |
3224 | ||
777744da | 3225 | for (Package *package in [self packages]) { |
f464053e JF |
3226 | if (![package broken]) |
3227 | continue; | |
3228 | pkgCache::PkgIterator pkg([package iterator]); | |
3229 | ||
3230 | NSMutableArray *entry([NSMutableArray arrayWithCapacity:4]); | |
3231 | [entry addObject:[package name]]; | |
3232 | [issues addObject:entry]; | |
3233 | ||
3234 | pkgCache::VerIterator ver(cache_[pkg].InstVerIter(cache_)); | |
3235 | if (ver.end()) | |
3236 | continue; | |
3237 | ||
3238 | for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) { | |
3239 | pkgCache::DepIterator start; | |
3240 | pkgCache::DepIterator end; | |
3241 | dep.GlobOr(start, end); // ++dep | |
3242 | ||
3243 | if (!cache_->IsImportantDep(end)) | |
3244 | continue; | |
3245 | if ((cache_[end] & pkgDepCache::DepGInstall) != 0) | |
3246 | continue; | |
3247 | ||
3248 | NSMutableArray *failure([NSMutableArray arrayWithCapacity:4]); | |
3249 | [entry addObject:failure]; | |
3250 | [failure addObject:[NSString stringWithUTF8String:start.DepType()]]; | |
3251 | ||
e4765783 JF |
3252 | NSString *name([NSString stringWithUTF8String:start.TargetPkg().Name()]); |
3253 | if (Package *package = [self packageWithName:name]) | |
3254 | name = [package name]; | |
3255 | [failure addObject:name]; | |
f464053e JF |
3256 | |
3257 | pkgCache::PkgIterator target(start.TargetPkg()); | |
3258 | if (target->ProvidesList != 0) | |
3259 | [failure addObject:@"?"]; | |
3260 | else { | |
3261 | pkgCache::VerIterator ver(cache_[target].InstVerIter(cache_)); | |
3262 | if (!ver.end()) | |
3263 | [failure addObject:[NSString stringWithUTF8String:ver.VerStr()]]; | |
3264 | else if (!cache_[target].CandidateVerIter(cache_).end()) | |
3265 | [failure addObject:@"-"]; | |
3266 | else if (target->ProvidesList == 0) | |
3267 | [failure addObject:@"!"]; | |
3268 | else | |
3269 | [failure addObject:@"%"]; | |
3270 | } | |
3271 | ||
3272 | _forever { | |
3273 | if (start.TargetVer() != 0) | |
3274 | [failure addObject:[NSString stringWithFormat:@"%s %s", start.CompType(), start.TargetVer()]]; | |
3275 | if (start == end) | |
3276 | break; | |
3277 | ++start; | |
3278 | } | |
3279 | } | |
3280 | } | |
3281 | ||
3282 | return issues; | |
3283 | } | |
3284 | ||
6981ccdf JF |
3285 | - (bool) popErrorWithTitle:(NSString *)title { |
3286 | bool fatal(false); | |
3287 | std::string message; | |
3288 | ||
3289 | while (!_error->empty()) { | |
3290 | std::string error; | |
3291 | bool warning(!_error->PopMessage(error)); | |
3292 | if (!warning) | |
3293 | fatal = true; | |
3294 | for (;;) { | |
3295 | size_t size(error.size()); | |
3296 | if (size == 0 || error[size - 1] != '\n') | |
3297 | break; | |
3298 | error.resize(size - 1); | |
3299 | } | |
3300 | lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); | |
3301 | ||
3302 | if (!message.empty()) | |
3303 | message += "\n\n"; | |
3304 | message += error; | |
3305 | } | |
3306 | ||
281f523c | 3307 | if (fatal && !message.empty()) |
6981ccdf JF |
3308 | [delegate_ _setProgressError:[NSString stringWithUTF8String:message.c_str()] withTitle:[NSString stringWithFormat:Colon_, fatal ? Error_ : Warning_, title]]; |
3309 | ||
3310 | return fatal; | |
3311 | } | |
3312 | ||
3313 | - (bool) popErrorWithTitle:(NSString *)title forOperation:(bool)success { | |
3314 | return [self popErrorWithTitle:title] || !success; | |
3315 | } | |
3316 | ||
7831584c | 3317 | - (void) reloadData { CYPoolStart() { |
f3d8816a JF |
3318 | @synchronized (self) { |
3319 | ++era_; | |
a70cf746 | 3320 | |
e733869b | 3321 | [self releasePackages]; |
98fb9119 JF |
3322 | sources_.clear(); |
3323 | ||
e057ec05 | 3324 | _error->Discard(); |
965edd52 | 3325 | |
e057ec05 | 3326 | delete list_; |
965edd52 | 3327 | list_ = NULL; |
e057ec05 JF |
3328 | manager_ = NULL; |
3329 | delete lock_; | |
965edd52 | 3330 | lock_ = NULL; |
e057ec05 | 3331 | delete fetcher_; |
965edd52 | 3332 | fetcher_ = NULL; |
e057ec05 | 3333 | delete resolver_; |
965edd52 | 3334 | resolver_ = NULL; |
e057ec05 | 3335 | delete records_; |
965edd52 JF |
3336 | records_ = NULL; |
3337 | delete policy_; | |
3338 | policy_ = NULL; | |
e057ec05 | 3339 | |
965edd52 | 3340 | cache_.Close(); |
7600bd69 | 3341 | |
6932575e JF |
3342 | apr_pool_clear(pool_); |
3343 | NSRecycleZone(zone_); | |
3344 | ||
3e3977a2 JF |
3345 | int chk(creat("/tmp/cydia.chk", 0644)); |
3346 | if (chk != -1) | |
3347 | close(chk); | |
3348 | ||
6981ccdf JF |
3349 | NSString *title(UCLocalize("DATABASE")); |
3350 | ||
907a35d6 | 3351 | _trace(); |
6981ccdf | 3352 | if (!cache_.Open(progress_, true)) { pop: |
7600bd69 | 3353 | std::string error; |
6981ccdf | 3354 | bool warning(!_error->PopMessage(error)); |
cb9c2100 | 3355 | lprintf("cache_.Open():[%s]\n", error.c_str()); |
965edd52 JF |
3356 | |
3357 | if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ") | |
3358 | [delegate_ repairWithSelector:@selector(configure)]; | |
3359 | else if (error == "The package lists or status file could not be parsed or opened.") | |
3360 | [delegate_ repairWithSelector:@selector(update)]; | |
fc19e583 JF |
3361 | // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)") |
3362 | // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)") | |
3363 | // else if (error == "The list of sources could not be read.") | |
6981ccdf JF |
3364 | else |
3365 | [delegate_ _setProgressError:[NSString stringWithUTF8String:error.c_str()] withTitle:[NSString stringWithFormat:Colon_, warning ? Warning_ : Error_, title]]; | |
965edd52 | 3366 | |
6981ccdf JF |
3367 | if (warning) |
3368 | goto pop; | |
3369 | _error->Discard(); | |
965edd52 | 3370 | return; |
e057ec05 | 3371 | } |
907a35d6 | 3372 | _trace(); |
e057ec05 | 3373 | |
3e3977a2 JF |
3374 | unlink("/tmp/cydia.chk"); |
3375 | ||
8032d797 | 3376 | now_ = [[NSDate date] timeIntervalSince1970]; |
e057ec05 | 3377 | |
965edd52 | 3378 | policy_ = new pkgDepCache::Policy(); |
e057ec05 JF |
3379 | records_ = new pkgRecords(cache_); |
3380 | resolver_ = new pkgProblemResolver(cache_); | |
3381 | fetcher_ = new pkgAcquire(&status_); | |
3382 | lock_ = NULL; | |
3383 | ||
3384 | list_ = new pkgSourceList(); | |
6981ccdf JF |
3385 | if ([self popErrorWithTitle:title forOperation:list_->ReadMainList()]) |
3386 | return; | |
3387 | ||
3388 | if (cache_->DelCount() != 0 || cache_->InstCount() != 0) { | |
3389 | [delegate_ _setProgressError:@"COUNTS_NONZERO_EX" withTitle:title]; | |
3390 | return; | |
3391 | } | |
e057ec05 | 3392 | |
6981ccdf JF |
3393 | if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) |
3394 | return; | |
3319715b JF |
3395 | |
3396 | if (cache_->BrokenCount() != 0) { | |
6981ccdf JF |
3397 | if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) |
3398 | return; | |
3399 | ||
3400 | if (cache_->BrokenCount() != 0) { | |
3401 | [delegate_ _setProgressError:@"STILL_BROKEN_EX" withTitle:title]; | |
3402 | return; | |
3403 | } | |
3404 | ||
3405 | if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) | |
3406 | return; | |
3319715b JF |
3407 | } |
3408 | ||
631a0a1e JF |
3409 | for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { |
3410 | std::vector<pkgIndexFile *> *indices = (*source)->GetIndexFiles(); | |
3411 | for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index) | |
3412 | // XXX: this could be more intelligent | |
3413 | if (dynamic_cast<debPackagesIndex *>(*index) != NULL) { | |
3414 | pkgCache::PkgFileIterator cached((*index)->FindInCache(cache_)); | |
3415 | if (!cached.end()) | |
b8b1cfd0 | 3416 | sources_[cached->ID] = [[[Source alloc] initWithMetaIndex:*source inPool:pool_] autorelease]; |
631a0a1e | 3417 | } |
e057ec05 | 3418 | } |
631a0a1e | 3419 | |
43b742af | 3420 | { |
9ee296df | 3421 | /*std::vector<Package *> packages; |
43b742af | 3422 | packages.reserve(std::max(10000U, [packages_ count] + 1000)); |
43b742af | 3423 | [packages_ release]; |
9ee296df JF |
3424 | packages_ = nil;*/ |
3425 | ||
43b742af JF |
3426 | _trace(); |
3427 | ||
3428 | for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) | |
3429 | if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:pool_ database:self]) | |
9ee296df | 3430 | //packages.push_back(package); |
777744da | 3431 | CFArrayAppendValue(packages_, [package retain]); |
43b742af JF |
3432 | |
3433 | _trace(); | |
3434 | ||
9ee296df | 3435 | /*if (packages.empty()) |
43b742af JF |
3436 | packages_ = [[NSArray alloc] init]; |
3437 | else | |
3438 | packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; | |
9ee296df JF |
3439 | _trace();*/ |
3440 | ||
777744da JF |
3441 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<SKRadixFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(16)]; |
3442 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<SKRadixFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(4)]; | |
3443 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<SKRadixFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(0)]; | |
9ee296df JF |
3444 | |
3445 | /*_trace(); | |
3446 | PrintTimes(); | |
3447 | _trace();*/ | |
3448 | ||
3449 | _trace(); | |
3450 | ||
3451 | /*if (!packages.empty()) | |
3452 | CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare_), NULL);*/ | |
3453 | //std::sort(packages.begin(), packages.end(), PackageNameOrdering()); | |
3454 | ||
66abcbb0 JF |
3455 | //CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); |
3456 | ||
777744da | 3457 | CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); |
9ee296df JF |
3458 | |
3459 | //[packages_ sortUsingFunction:reinterpret_cast<NSComparisonResult (*)(id, id, void *)>(&PackageNameCompare) context:NULL]; | |
43b742af JF |
3460 | |
3461 | _trace(); | |
3462 | } | |
d11754e5 | 3463 | } } CYPoolEnd() _trace(); } |
1cb11c5f | 3464 | |
30746bd9 JF |
3465 | - (void) clear { |
3466 | @synchronized (self) { | |
3467 | delete resolver_; | |
3468 | resolver_ = new pkgProblemResolver(cache_); | |
3469 | ||
3470 | for (pkgCache::PkgIterator iterator(cache_->PkgBegin()); !iterator.end(); ++iterator) { | |
3471 | if (!cache_[iterator].Keep()) { | |
3472 | cache_->MarkKeep(iterator, false); | |
3473 | cache_->SetReInstall(iterator, false); | |
3474 | } | |
3475 | } | |
3476 | } } | |
3477 | ||
965edd52 JF |
3478 | - (void) configure { |
3479 | NSString *dpkg = [NSString stringWithFormat:@"dpkg --configure -a --status-fd %u", statusfd_]; | |
3480 | system([dpkg UTF8String]); | |
3481 | } | |
3482 | ||
6981ccdf JF |
3483 | - (bool) clean { |
3484 | // XXX: I don't remember this condition | |
d72d91aa | 3485 | if (lock_ != NULL) |
6981ccdf | 3486 | return false; |
d72d91aa JF |
3487 | |
3488 | FileFd Lock; | |
3489 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
6981ccdf JF |
3490 | |
3491 | NSString *title(UCLocalize("CLEAN_ARCHIVES")); | |
3492 | ||
3493 | if ([self popErrorWithTitle:title]) | |
3494 | return false; | |
d72d91aa JF |
3495 | |
3496 | pkgAcquire fetcher; | |
3497 | fetcher.Clean(_config->FindDir("Dir::Cache::Archives")); | |
3498 | ||
3319715b JF |
3499 | class LogCleaner : |
3500 | public pkgArchiveCleaner | |
3501 | { | |
d72d91aa JF |
3502 | protected: |
3503 | virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) { | |
3319715b | 3504 | unlink(File); |
d72d91aa JF |
3505 | } |
3506 | } cleaner; | |
3507 | ||
6981ccdf JF |
3508 | if ([self popErrorWithTitle:title forOperation:cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)]) |
3509 | return false; | |
3510 | ||
3511 | return true; | |
d72d91aa JF |
3512 | } |
3513 | ||
6981ccdf | 3514 | - (bool) prepare { |
e4765783 JF |
3515 | fetcher_->Shutdown(); |
3516 | ||
e057ec05 JF |
3517 | pkgRecords records(cache_); |
3518 | ||
3519 | lock_ = new FileFd(); | |
3520 | lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
6981ccdf JF |
3521 | |
3522 | NSString *title(UCLocalize("PREPARE_ARCHIVES")); | |
3523 | ||
3524 | if ([self popErrorWithTitle:title]) | |
3525 | return false; | |
e057ec05 JF |
3526 | |
3527 | pkgSourceList list; | |
6981ccdf JF |
3528 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3529 | return false; | |
e057ec05 JF |
3530 | |
3531 | manager_ = (_system->CreatePM(cache_)); | |
6981ccdf JF |
3532 | if ([self popErrorWithTitle:title forOperation:manager_->GetArchives(fetcher_, &list, &records)]) |
3533 | return false; | |
3534 | ||
3535 | return true; | |
1cb11c5f JF |
3536 | } |
3537 | ||
e057ec05 | 3538 | - (void) perform { |
6981ccdf JF |
3539 | NSString *title(UCLocalize("PERFORM_SELECTIONS")); |
3540 | ||
113c9b62 JF |
3541 | NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { |
3542 | pkgSourceList list; | |
6981ccdf JF |
3543 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3544 | return; | |
113c9b62 JF |
3545 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
3546 | [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
3547 | } | |
8993ad57 | 3548 | |
2d7f7dea JF |
3549 | if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) { |
3550 | _trace(); | |
e057ec05 | 3551 | return; |
2d7f7dea JF |
3552 | } |
3553 | ||
3554 | bool failed = false; | |
3555 | for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) { | |
3556 | if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete) | |
3557 | continue; | |
9050015e JF |
3558 | if ((*item)->Status == pkgAcquire::Item::StatIdle) |
3559 | continue; | |
2d7f7dea JF |
3560 | |
3561 | std::string uri = (*item)->DescURI(); | |
3562 | std::string error = (*item)->ErrorText; | |
3563 | ||
cb9c2100 | 3564 | lprintf("pAf:%s:%s\n", uri.c_str(), error.c_str()); |
2d7f7dea JF |
3565 | failed = true; |
3566 | ||
6981ccdf | 3567 | [delegate_ performSelectorOnMainThread:@selector(_setProgressErrorPackage:) |
ce09fc27 JF |
3568 | withObject:[NSArray arrayWithObjects: |
3569 | [NSString stringWithUTF8String:error.c_str()], | |
3570 | nil] | |
2d7f7dea JF |
3571 | waitUntilDone:YES |
3572 | ]; | |
3573 | } | |
3574 | ||
3575 | if (failed) { | |
3576 | _trace(); | |
3577 | return; | |
3578 | } | |
e057ec05 JF |
3579 | |
3580 | _system->UnLock(); | |
3581 | pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_); | |
3582 | ||
2d7f7dea JF |
3583 | if (_error->PendingError()) { |
3584 | _trace(); | |
e057ec05 | 3585 | return; |
2d7f7dea JF |
3586 | } |
3587 | ||
3588 | if (result == pkgPackageManager::Failed) { | |
3589 | _trace(); | |
e057ec05 | 3590 | return; |
2d7f7dea JF |
3591 | } |
3592 | ||
3593 | if (result != pkgPackageManager::Completed) { | |
3594 | _trace(); | |
e057ec05 | 3595 | return; |
2d7f7dea | 3596 | } |
8993ad57 | 3597 | |
113c9b62 JF |
3598 | NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { |
3599 | pkgSourceList list; | |
6981ccdf JF |
3600 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3601 | return; | |
113c9b62 JF |
3602 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
3603 | [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
3604 | } | |
3605 | ||
3606 | if (![before isEqualToArray:after]) | |
3607 | [self update]; | |
1cb11c5f JF |
3608 | } |
3609 | ||
6981ccdf JF |
3610 | - (bool) upgrade { |
3611 | NSString *title(UCLocalize("UPGRADE")); | |
3612 | if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) | |
3613 | return false; | |
3614 | return true; | |
1951a333 JF |
3615 | } |
3616 | ||
e057ec05 JF |
3617 | - (void) update { |
3618 | [self updateWithStatus:status_]; | |
3619 | } | |
686e302f | 3620 | |
6981ccdf JF |
3621 | - (void) updateWithStatus:(Status &)status { |
3622 | _transient NSObject<ProgressDelegate> *delegate(status.getDelegate()); | |
3623 | NSString *title(UCLocalize("REFRESHING_DATA")); | |
3624 | ||
e057ec05 | 3625 | pkgSourceList list; |
6981ccdf JF |
3626 | if (!list.ReadMainList()) |
3627 | [delegate _setProgressError:@"Unable to read source list." withTitle:title]; | |
686e302f | 3628 | |
e057ec05 JF |
3629 | FileFd lock; |
3630 | lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
6981ccdf JF |
3631 | if ([self popErrorWithTitle:title]) |
3632 | return; | |
f30eaf83 | 3633 | |
6981ccdf | 3634 | if ([self popErrorWithTitle:title forOperation:ListUpdate(status, list, PulseInterval_)]) |
9b62701b GP |
3635 | /* XXX: ignore this because users suck and don't understand why refreshing is important: return */ |
3636 | /* XXX: why the hell is an empty if statement a clang error? */ (void) 0; | |
e057ec05 | 3637 | |
419a9efd JF |
3638 | [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"]; |
3639 | Changed_ = true; | |
686e302f JF |
3640 | } |
3641 | ||
e057ec05 JF |
3642 | - (void) setDelegate:(id)delegate { |
3643 | delegate_ = delegate; | |
3644 | status_.setDelegate(delegate); | |
3645 | progress_.setDelegate(delegate); | |
3646 | } | |
686e302f | 3647 | |
3e3977a2 | 3648 | - (Source *) getSource:(pkgCache::PkgFileIterator)file { |
1636cf29 JF |
3649 | SourceMap::const_iterator i(sources_.find(file->ID)); |
3650 | return i == sources_.end() ? nil : i->second; | |
a933cee2 JF |
3651 | } |
3652 | ||
e057ec05 JF |
3653 | @end |
3654 | /* }}} */ | |
686e302f | 3655 | |
f441e717 | 3656 | /* Confirmation Controller {{{ */ |
61b13cae JF |
3657 | bool DepSubstrate(const pkgCache::VerIterator &iterator) { |
3658 | if (!iterator.end()) | |
3659 | for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) { | |
3660 | if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends) | |
3661 | continue; | |
3662 | pkgCache::PkgIterator package(dep.TargetPkg()); | |
3663 | if (package.end()) | |
3664 | continue; | |
3665 | if (strcmp(package.Name(), "mobilesubstrate") == 0) | |
3666 | return true; | |
3667 | } | |
3668 | ||
3669 | return false; | |
cb9c2100 | 3670 | } |
9ae52960 | 3671 | /* }}} */ |
cb9c2100 | 3672 | |
61b13cae JF |
3673 | /* Web Scripting {{{ */ |
3674 | @interface CydiaObject : NSObject { | |
3675 | id indirect_; | |
a591888a | 3676 | _transient id delegate_; |
61b13cae | 3677 | } |
cb9c2100 | 3678 | |
61b13cae | 3679 | - (id) initWithDelegate:(IndirectDelegate *)indirect; |
cb9c2100 JF |
3680 | @end |
3681 | ||
61b13cae | 3682 | @implementation CydiaObject |
cb9c2100 JF |
3683 | |
3684 | - (void) dealloc { | |
61b13cae | 3685 | [indirect_ release]; |
cb9c2100 JF |
3686 | [super dealloc]; |
3687 | } | |
3688 | ||
61b13cae JF |
3689 | - (id) initWithDelegate:(IndirectDelegate *)indirect { |
3690 | if ((self = [super init]) != nil) { | |
3691 | indirect_ = [indirect retain]; | |
3692 | } return self; | |
3693 | } | |
3694 | ||
daf7f6e2 JF |
3695 | - (void) setDelegate:(id)delegate { |
3696 | delegate_ = delegate; | |
3697 | } | |
3698 | ||
61b13cae JF |
3699 | + (NSArray *) _attributeKeys { |
3700 | return [NSArray arrayWithObjects:@"device", @"firewire", @"imei", @"mac", @"serial", nil]; | |
3701 | } | |
3702 | ||
3703 | - (NSArray *) attributeKeys { | |
3704 | return [[self class] _attributeKeys]; | |
cb9c2100 JF |
3705 | } |
3706 | ||
61b13cae JF |
3707 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
3708 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
cb9c2100 | 3709 | } |
61b13cae JF |
3710 | |
3711 | - (NSString *) device { | |
3712 | return [[UIDevice currentDevice] uniqueIdentifier]; | |
cb9c2100 JF |
3713 | } |
3714 | ||
61b13cae JF |
3715 | #if 0 // XXX: implement! |
3716 | - (NSString *) mac { | |
3717 | if (![indirect_ promptForSensitive:@"Mac Address"]) | |
3718 | return nil; | |
f464053e JF |
3719 | } |
3720 | ||
61b13cae JF |
3721 | - (NSString *) serial { |
3722 | if (![indirect_ promptForSensitive:@"Serial #"]) | |
3723 | return nil; | |
3724 | } | |
56e10908 | 3725 | |
61b13cae JF |
3726 | - (NSString *) firewire { |
3727 | if (![indirect_ promptForSensitive:@"Firewire GUID"]) | |
3728 | return nil; | |
f464053e JF |
3729 | } |
3730 | ||
61b13cae JF |
3731 | - (NSString *) imei { |
3732 | if (![indirect_ promptForSensitive:@"IMEI"]) | |
3733 | return nil; | |
3734 | } | |
3735 | #endif | |
3736 | ||
3737 | + (NSString *) webScriptNameForSelector:(SEL)selector { | |
3738 | if (selector == @selector(close)) | |
3739 | return @"close"; | |
22b21e43 JF |
3740 | else if (selector == @selector(getInstalledPackages)) |
3741 | return @"getInstalledPackages"; | |
61b13cae JF |
3742 | else if (selector == @selector(getPackageById:)) |
3743 | return @"getPackageById"; | |
daf7f6e2 JF |
3744 | else if (selector == @selector(installPackages:)) |
3745 | return @"installPackages"; | |
61b13cae JF |
3746 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) |
3747 | return @"setButtonImage"; | |
3748 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
3749 | return @"setButtonTitle"; | |
61b13cae JF |
3750 | else if (selector == @selector(setPopupHook:)) |
3751 | return @"setPopupHook"; | |
3752 | else if (selector == @selector(setSpecial:)) | |
3753 | return @"setSpecial"; | |
37455cf8 JF |
3754 | else if (selector == @selector(setToken:)) |
3755 | return @"setToken"; | |
61b13cae JF |
3756 | else if (selector == @selector(setViewportWidth:)) |
3757 | return @"setViewportWidth"; | |
3758 | else if (selector == @selector(supports:)) | |
3759 | return @"supports"; | |
3760 | else if (selector == @selector(stringWithFormat:arguments:)) | |
3761 | return @"format"; | |
3762 | else if (selector == @selector(localizedStringForKey:value:table:)) | |
3763 | return @"localize"; | |
3764 | else if (selector == @selector(du:)) | |
3765 | return @"du"; | |
3766 | else if (selector == @selector(statfs:)) | |
3767 | return @"statfs"; | |
cb9c2100 | 3768 | else |
61b13cae JF |
3769 | return nil; |
3770 | } | |
3771 | ||
3772 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
3773 | return [self webScriptNameForSelector:selector] == nil; | |
cb9c2100 JF |
3774 | } |
3775 | ||
61b13cae JF |
3776 | - (BOOL) supports:(NSString *)feature { |
3777 | return [feature isEqualToString:@"window.open"]; | |
3778 | } | |
cb9c2100 | 3779 | |
22b21e43 JF |
3780 | - (NSArray *) getInstalledPackages { |
3781 | NSArray *packages([[Database sharedInstance] packages]); | |
a4f13922 | 3782 | NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]); |
daf7f6e2 | 3783 | for (Package *package in packages) |
22b21e43 JF |
3784 | if ([package installed] != nil) |
3785 | [installed addObject:package]; | |
3786 | return installed; | |
3787 | } | |
3788 | ||
61b13cae | 3789 | - (Package *) getPackageById:(NSString *)id { |
e32f0fcb JF |
3790 | Package *package([[Database sharedInstance] packageWithName:id]); |
3791 | [package parse]; | |
3792 | return package; | |
61b13cae JF |
3793 | } |
3794 | ||
3795 | - (NSArray *) statfs:(NSString *)path { | |
3796 | struct statfs stat; | |
3797 | ||
3798 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
3799 | return nil; | |
3800 | ||
3801 | return [NSArray arrayWithObjects: | |
3802 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
3803 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
3804 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
3805 | nil]; | |
3806 | } | |
3807 | ||
3808 | - (NSNumber *) du:(NSString *)path { | |
3809 | NSNumber *value(nil); | |
3810 | ||
3811 | int fds[2]; | |
3812 | _assert(pipe(fds) != -1); | |
3813 | ||
3814 | pid_t pid(ExecFork()); | |
3815 | if (pid == 0) { | |
3816 | _assert(dup2(fds[1], 1) != -1); | |
3817 | _assert(close(fds[0]) != -1); | |
3818 | _assert(close(fds[1]) != -1); | |
3819 | /* XXX: this should probably not use du */ | |
3820 | execl("/usr/libexec/cydia/du", "du", "-s", [path UTF8String], NULL); | |
3821 | exit(1); | |
3822 | _assert(false); | |
3823 | } | |
3824 | ||
3825 | _assert(close(fds[1]) != -1); | |
3826 | ||
3827 | if (FILE *du = fdopen(fds[0], "r")) { | |
3828 | char line[1024]; | |
3829 | while (fgets(line, sizeof(line), du) != NULL) { | |
3830 | size_t length(strlen(line)); | |
3831 | while (length != 0 && line[length - 1] == '\n') | |
3832 | line[--length] = '\0'; | |
3833 | if (char *tab = strchr(line, '\t')) { | |
3834 | *tab = '\0'; | |
3835 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
3836 | } | |
3837 | } | |
3838 | ||
3839 | fclose(du); | |
3840 | } else _assert(close(fds[0])); | |
3841 | ||
3842 | int status; | |
3843 | wait: | |
3844 | if (waitpid(pid, &status, 0) == -1) | |
3845 | if (errno == EINTR) | |
3846 | goto wait; | |
3847 | else _assert(false); | |
3848 | ||
3849 | return value; | |
3850 | } | |
3851 | ||
3852 | - (void) close { | |
3853 | [indirect_ close]; | |
3854 | } | |
3855 | ||
daf7f6e2 JF |
3856 | - (void) installPackages:(NSArray *)packages { |
3857 | [delegate_ performSelectorOnMainThread:@selector(installPackages:) withObject:packages waitUntilDone:NO]; | |
3858 | } | |
3859 | ||
61b13cae JF |
3860 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
3861 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
3862 | } | |
3863 | ||
3864 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
3865 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
3866 | } | |
3867 | ||
3868 | - (void) setSpecial:(id)function { | |
3869 | [indirect_ setSpecial:function]; | |
3870 | } | |
3871 | ||
37455cf8 JF |
3872 | - (void) setToken:(NSString *)token { |
3873 | if (Token_ != nil) | |
3874 | [Token_ release]; | |
3875 | Token_ = [token retain]; | |
3876 | ||
3877 | [Metadata_ setObject:Token_ forKey:@"Token"]; | |
3878 | Changed_ = true; | |
3879 | } | |
3880 | ||
61b13cae JF |
3881 | - (void) setPopupHook:(id)function { |
3882 | [indirect_ setPopupHook:function]; | |
3883 | } | |
3884 | ||
3885 | - (void) setViewportWidth:(float)width { | |
3886 | [indirect_ setViewportWidth:width]; | |
3887 | } | |
3888 | ||
3889 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
3890 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
3891 | unsigned count([arguments count]); | |
3892 | id values[count]; | |
3893 | for (unsigned i(0); i != count; ++i) | |
3894 | values[i] = [arguments objectAtIndex:i]; | |
9b62701b | 3895 | return [[[NSString alloc] initWithFormat:format arguments:*(reinterpret_cast<va_list *>(&values))] autorelease]; |
61b13cae JF |
3896 | } |
3897 | ||
3898 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
e1316e10 JF |
3899 | if (reinterpret_cast<id>(value) == [WebUndefined undefined]) |
3900 | value = nil; | |
61b13cae JF |
3901 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) |
3902 | table = nil; | |
3903 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
cb9c2100 JF |
3904 | } |
3905 | ||
3906 | @end | |
3907 | /* }}} */ | |
6932575e | 3908 | |
7d6ecbef | 3909 | /* Cydia Browser Controller {{{ */ |
ed349d9a | 3910 | @interface CYBrowserController : BrowserController { |
61b13cae JF |
3911 | CydiaObject *cydia_; |
3912 | } | |
e00439f7 | 3913 | |
61b13cae JF |
3914 | @end |
3915 | ||
9ae52960 | 3916 | @implementation CYBrowserController |
61b13cae JF |
3917 | |
3918 | - (void) dealloc { | |
3919 | [cydia_ release]; | |
3920 | [super dealloc]; | |
3921 | } | |
3922 | ||
5ec44e34 JF |
3923 | - (void) setHeaders:(NSDictionary *)headers forHost:(NSString *)host { |
3924 | } | |
3925 | ||
c2292b80 JF |
3926 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
3927 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
5ec44e34 JF |
3928 | |
3929 | WebDataSource *source([frame dataSource]); | |
3930 | NSURLResponse *response([source response]); | |
3931 | NSURL *url([response URL]); | |
3932 | NSString *scheme([url scheme]); | |
3933 | ||
3934 | NSHTTPURLResponse *http; | |
3935 | if (scheme != nil && ([scheme isEqualToString:@"http"] || [scheme isEqualToString:@"https"])) | |
3936 | http = (NSHTTPURLResponse *) response; | |
3937 | else | |
3938 | http = nil; | |
3939 | ||
3940 | NSDictionary *headers([http allHeaderFields]); | |
3941 | NSString *host([url host]); | |
3942 | [self setHeaders:headers forHost:host]; | |
3943 | ||
daf7f6e2 JF |
3944 | if ( |
3945 | [host isEqualToString:@"cydia.saurik.com"] || | |
3946 | [host hasSuffix:@".cydia.saurik.com"] || | |
3947 | [scheme isEqualToString:@"file"] | |
3948 | ) | |
37455cf8 | 3949 | [window setValue:cydia_ forKey:@"cydia"]; |
61b13cae JF |
3950 | } |
3951 | ||
017b2b71 JF |
3952 | - (void) _setMoreHeaders:(NSMutableURLRequest *)request { |
3953 | if (System_ != NULL) | |
3954 | [request setValue:System_ forHTTPHeaderField:@"X-System"]; | |
61b13cae | 3955 | if (Machine_ != NULL) |
017b2b71 | 3956 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; |
5ec44e34 JF |
3957 | if (Token_ != nil) |
3958 | [request setValue:Token_ forHTTPHeaderField:@"X-Cydia-Token"]; | |
61b13cae | 3959 | if (Role_ != nil) |
017b2b71 JF |
3960 | [request setValue:Role_ forHTTPHeaderField:@"X-Role"]; |
3961 | } | |
61b13cae | 3962 | |
c2292b80 JF |
3963 | - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
3964 | NSMutableURLRequest *copy([[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source] mutableCopy]); | |
017b2b71 | 3965 | [self _setMoreHeaders:copy]; |
61b13cae | 3966 | return copy; |
e00439f7 JF |
3967 | } |
3968 | ||
daf7f6e2 JF |
3969 | - (void) setDelegate:(id)delegate { |
3970 | [super setDelegate:delegate]; | |
3971 | [cydia_ setDelegate:delegate]; | |
3972 | } | |
3973 | ||
9ae52960 | 3974 | - (id) init { |
2938b930 | 3975 | if ((self = [super initWithWidth:0 ofClass:[CYBrowserController class]]) != nil) { |
61b13cae JF |
3976 | cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_]; |
3977 | ||
c2292b80 | 3978 | WebView *webview([[webview_ _documentView] webView]); |
61b13cae JF |
3979 | |
3980 | Package *package([[Database sharedInstance] packageWithName:@"cydia"]); | |
0a523b5a | 3981 | |
61b13cae JF |
3982 | NSString *application = package == nil ? @"Cydia" : [NSString |
3983 | stringWithFormat:@"Cydia/%@", | |
3984 | [package installed] | |
3985 | ]; | |
3986 | ||
61b13cae | 3987 | if (Safari_ != nil) |
f26c1a7f | 3988 | application = [NSString stringWithFormat:@"Safari/%@ %@", Safari_, application]; |
0a523b5a | 3989 | if (Build_ != nil) |
f26c1a7f | 3990 | application = [NSString stringWithFormat:@"Mobile/%@ %@", Build_, application]; |
0a523b5a | 3991 | if (Product_ != nil) |
f26c1a7f | 3992 | application = [NSString stringWithFormat:@"Version/%@ %@", Product_, application]; |
61b13cae JF |
3993 | |
3994 | [webview setApplicationNameForUserAgent:application]; | |
3995 | } return self; | |
3996 | } | |
3997 | ||
3998 | @end | |
7d6ecbef | 3999 | /* }}} */ |
61b13cae | 4000 | |
7d6ecbef | 4001 | /* Confirmation {{{ */ |
f441e717 | 4002 | @protocol ConfirmationControllerDelegate |
1ca35d78 | 4003 | - (void) cancelAndClear:(bool)clear; |
9ae52960 | 4004 | - (void) confirmWithNavigationController:(UINavigationController *)navigation; |
3ff1504e | 4005 | - (void) queue; |
e057ec05 | 4006 | @end |
4941f41d | 4007 | |
f441e717 | 4008 | @interface ConfirmationController : CYBrowserController { |
dbe0f181 | 4009 | _transient Database *database_; |
9c421310 | 4010 | UIAlertView *essential_; |
f464053e JF |
4011 | NSArray *changes_; |
4012 | NSArray *issues_; | |
4013 | NSArray *sizes_; | |
e00439f7 | 4014 | BOOL substrate_; |
e057ec05 | 4015 | } |
a75f53e7 | 4016 | |
9ae52960 | 4017 | - (id) initWithDatabase:(Database *)database; |
686e302f | 4018 | |
a75f53e7 JF |
4019 | @end |
4020 | ||
f441e717 | 4021 | @implementation ConfirmationController |
a75f53e7 | 4022 | |
4941f41d | 4023 | - (void) dealloc { |
f464053e JF |
4024 | [changes_ release]; |
4025 | if (issues_ != nil) | |
4026 | [issues_ release]; | |
4027 | [sizes_ release]; | |
e057ec05 JF |
4028 | if (essential_ != nil) |
4029 | [essential_ release]; | |
4941f41d JF |
4030 | [super dealloc]; |
4031 | } | |
4032 | ||
9ae52960 | 4033 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
9c421310 | 4034 | NSString *context([alert context]); |
3319715b | 4035 | |
a5dd312c | 4036 | if ([context isEqualToString:@"remove"]) { |
9ae52960 GP |
4037 | if (button == [alert cancelButtonIndex]) { |
4038 | [self dismissModalViewControllerAnimated:YES]; | |
9c421310 | 4039 | } else if (button == [alert firstOtherButtonIndex]) { |
9ae52960 GP |
4040 | if (substrate_) |
4041 | Finish_ = 2; | |
4042 | [delegate_ confirmWithNavigationController:[self navigationController]]; | |
3319715b | 4043 | } |
3319715b | 4044 | |
9c421310 | 4045 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
a5dd312c | 4046 | } else if ([context isEqualToString:@"unable"]) { |
9ae52960 | 4047 | [self dismissModalViewControllerAnimated:YES]; |
9c421310 GP |
4048 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
4049 | } else { | |
9ae52960 GP |
4050 | [super alertView:alert clickedButtonAtIndex:button]; |
4051 | } | |
e057ec05 JF |
4052 | } |
4053 | ||
fa2cd4b3 | 4054 | - (void) _doContinue { |
2064d251 GP |
4055 | [self dismissModalViewControllerAnimated:YES]; |
4056 | [delegate_ cancelAndClear:NO]; | |
fa2cd4b3 | 4057 | } |
f99f86e2 | 4058 | |
fa2cd4b3 JF |
4059 | - (id) invokeDefaultMethodWithArguments:(NSArray *)args { |
4060 | [self performSelectorOnMainThread:@selector(_doContinue) withObject:nil waitUntilDone:NO]; | |
2064d251 GP |
4061 | return nil; |
4062 | } | |
4063 | ||
c2292b80 JF |
4064 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4065 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
f464053e JF |
4066 | [window setValue:changes_ forKey:@"changes"]; |
4067 | [window setValue:issues_ forKey:@"issues"]; | |
4068 | [window setValue:sizes_ forKey:@"sizes"]; | |
2064d251 | 4069 | [window setValue:self forKey:@"queue"]; |
e057ec05 JF |
4070 | } |
4071 | ||
9ae52960 GP |
4072 | - (id) initWithDatabase:(Database *)database { |
4073 | if ((self = [super init]) != nil) { | |
dbe0f181 JF |
4074 | database_ = database; |
4075 | ||
9ae52960 GP |
4076 | [[self navigationItem] setTitle:UCLocalize("CONFIRM")]; |
4077 | ||
e057ec05 JF |
4078 | NSMutableArray *installing = [NSMutableArray arrayWithCapacity:16]; |
4079 | NSMutableArray *reinstalling = [NSMutableArray arrayWithCapacity:16]; | |
4080 | NSMutableArray *upgrading = [NSMutableArray arrayWithCapacity:16]; | |
4081 | NSMutableArray *downgrading = [NSMutableArray arrayWithCapacity:16]; | |
4082 | NSMutableArray *removing = [NSMutableArray arrayWithCapacity:16]; | |
a75f53e7 | 4083 | |
e057ec05 | 4084 | bool remove(false); |
a75f53e7 | 4085 | |
e00439f7 JF |
4086 | pkgDepCache::Policy *policy([database_ policy]); |
4087 | ||
e057ec05 | 4088 | pkgCacheFile &cache([database_ cache]); |
2a987aa5 | 4089 | NSArray *packages = [database_ packages]; |
9fdd37d0 | 4090 | for (Package *package in packages) { |
2a987aa5 | 4091 | pkgCache::PkgIterator iterator = [package iterator]; |
e057ec05 | 4092 | pkgDepCache::StateCache &state(cache[iterator]); |
a75f53e7 | 4093 | |
2a987aa5 JF |
4094 | NSString *name([package name]); |
4095 | ||
e057ec05 JF |
4096 | if (state.NewInstall()) |
4097 | [installing addObject:name]; | |
4098 | else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall) | |
4099 | [reinstalling addObject:name]; | |
4100 | else if (state.Upgrade()) | |
4101 | [upgrading addObject:name]; | |
4102 | else if (state.Downgrade()) | |
4103 | [downgrading addObject:name]; | |
4104 | else if (state.Delete()) { | |
2a987aa5 | 4105 | if ([package essential]) |
e057ec05 JF |
4106 | remove = true; |
4107 | [removing addObject:name]; | |
e00439f7 JF |
4108 | } else continue; |
4109 | ||
4110 | substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator)); | |
4111 | substrate_ |= DepSubstrate(iterator.CurrentVer()); | |
e057ec05 | 4112 | } |
1cb11c5f | 4113 | |
e057ec05 JF |
4114 | if (!remove) |
4115 | essential_ = nil; | |
c59881cd | 4116 | else if (Advanced_) { |
61b13cae | 4117 | NSString *parenthetical(UCLocalize("PARENTHETICAL")); |
6932575e | 4118 | |
9c421310 | 4119 | essential_ = [[UIAlertView alloc] |
61b13cae | 4120 | initWithTitle:UCLocalize("REMOVING_ESSENTIALS") |
9ae52960 GP |
4121 | message:UCLocalize("REMOVING_ESSENTIALS_EX") |
4122 | delegate:self | |
4123 | cancelButtonTitle:[NSString stringWithFormat:parenthetical, UCLocalize("CANCEL_OPERATION"), UCLocalize("SAFE")] | |
4124 | otherButtonTitles:[NSString stringWithFormat:parenthetical, UCLocalize("FORCE_REMOVAL"), UCLocalize("UNSAFE")], nil | |
4125 | ]; | |
d3bef7bc | 4126 | |
9c421310 | 4127 | [essential_ setContext:@"remove"]; |
3319715b | 4128 | } else { |
9c421310 | 4129 | essential_ = [[UIAlertView alloc] |
61b13cae | 4130 | initWithTitle:UCLocalize("UNABLE_TO_COMPLY") |
9ae52960 GP |
4131 | message:UCLocalize("UNABLE_TO_COMPLY_EX") |
4132 | delegate:self | |
4133 | cancelButtonTitle:UCLocalize("OKAY") | |
4134 | otherButtonTitles:nil | |
e057ec05 | 4135 | ]; |
1cb11c5f | 4136 | |
9ae52960 | 4137 | [essential_ setContext:@"unable"]; |
e057ec05 | 4138 | } |
1cb11c5f | 4139 | |
f464053e JF |
4140 | changes_ = [[NSArray alloc] initWithObjects: |
4141 | installing, | |
4142 | reinstalling, | |
4143 | upgrading, | |
4144 | downgrading, | |
4145 | removing, | |
4146 | nil]; | |
a75f53e7 | 4147 | |
f464053e JF |
4148 | issues_ = [database_ issues]; |
4149 | if (issues_ != nil) | |
4150 | issues_ = [issues_ retain]; | |
a75f53e7 | 4151 | |
f464053e JF |
4152 | sizes_ = [[NSArray alloc] initWithObjects: |
4153 | SizeString([database_ fetcher].FetchNeeded()), | |
4154 | SizeString([database_ fetcher].PartialPresent()), | |
f464053e | 4155 | nil]; |
a75f53e7 | 4156 | |
f464053e | 4157 | [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"confirm" ofType:@"html"]]]; |
d3bef7bc | 4158 | |
11f75d4f | 4159 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] |
c1edf105 GP |
4160 | initWithTitle:UCLocalize("CANCEL") |
4161 | // OLD: [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("CANCEL"), UCLocalize("QUEUE")] | |
9ae52960 GP |
4162 | style:UIBarButtonItemStylePlain |
4163 | target:self | |
4164 | action:@selector(cancelButtonClicked) | |
11f75d4f | 4165 | ] autorelease]]; |
e057ec05 | 4166 | } return self; |
686e302f | 4167 | } |
20dd7407 | 4168 | |
ed349d9a | 4169 | - (void) applyRightButton { |
9ae52960 | 4170 | #if !AlwaysReload && !IgnoreInstall |
11f75d4f JF |
4171 | if (issues_ == nil && ![self isLoading]) |
4172 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
4173 | initWithTitle:UCLocalize("CONFIRM") | |
4174 | style:UIBarButtonItemStylePlain | |
4175 | target:self | |
4176 | action:@selector(confirmButtonClicked) | |
4177 | ] autorelease]]; | |
4178 | else | |
4179 | [super applyRightButton]; | |
ed349d9a | 4180 | #else |
f99f86e2 | 4181 | [[self navigationItem] setRightBarButtonItem:nil]; |
9ae52960 | 4182 | #endif |
f464053e JF |
4183 | } |
4184 | ||
9ae52960 | 4185 | - (void) cancelButtonClicked { |
2064d251 GP |
4186 | [self dismissModalViewControllerAnimated:YES]; |
4187 | [delegate_ cancelAndClear:YES]; | |
f464053e JF |
4188 | } |
4189 | ||
907a35d6 | 4190 | #if !AlwaysReload |
9ae52960 | 4191 | - (void) confirmButtonClicked { |
83105e6e | 4192 | #if IgnoreInstall |
9ae52960 | 4193 | return; |
83105e6e | 4194 | #endif |
f464053e | 4195 | if (essential_ != nil) |
9ae52960 | 4196 | [essential_ show]; |
f464053e JF |
4197 | else { |
4198 | if (substrate_) | |
4199 | Finish_ = 2; | |
9ae52960 | 4200 | [delegate_ confirmWithNavigationController:[self navigationController]]; |
f464053e JF |
4201 | } |
4202 | } | |
907a35d6 | 4203 | #endif |
f464053e | 4204 | |
e057ec05 JF |
4205 | @end |
4206 | /* }}} */ | |
20dd7407 | 4207 | |
e057ec05 JF |
4208 | /* Progress Data {{{ */ |
4209 | @interface ProgressData : NSObject { | |
4210 | SEL selector_; | |
a591888a JF |
4211 | // XXX: should these really both be _transient? |
4212 | _transient id target_; | |
4213 | _transient id object_; | |
a75f53e7 JF |
4214 | } |
4215 | ||
e057ec05 | 4216 | - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object; |
686e302f | 4217 | |
e057ec05 JF |
4218 | - (SEL) selector; |
4219 | - (id) target; | |
4220 | - (id) object; | |
4221 | @end | |
686e302f | 4222 | |
e057ec05 | 4223 | @implementation ProgressData |
686e302f | 4224 | |
e057ec05 JF |
4225 | - (ProgressData *) initWithSelector:(SEL)selector target:(id)target object:(id)object { |
4226 | if ((self = [super init]) != nil) { | |
4227 | selector_ = selector; | |
4228 | target_ = target; | |
4229 | object_ = object; | |
4230 | } return self; | |
4231 | } | |
686e302f | 4232 | |
e057ec05 JF |
4233 | - (SEL) selector { |
4234 | return selector_; | |
4235 | } | |
686e302f | 4236 | |
e057ec05 JF |
4237 | - (id) target { |
4238 | return target_; | |
20dd7407 JF |
4239 | } |
4240 | ||
e057ec05 JF |
4241 | - (id) object { |
4242 | return object_; | |
a75f53e7 JF |
4243 | } |
4244 | ||
4941f41d JF |
4245 | @end |
4246 | /* }}} */ | |
f441e717 GP |
4247 | /* Progress Controller {{{ */ |
4248 | @interface ProgressController : CYViewController < | |
7600bd69 | 4249 | ConfigurationDelegate, |
e057ec05 JF |
4250 | ProgressDelegate |
4251 | > { | |
7600bd69 | 4252 | _transient Database *database_; |
e057ec05 JF |
4253 | UIProgressBar *progress_; |
4254 | UITextView *output_; | |
4255 | UITextLabel *status_; | |
238b07ce | 4256 | UIPushButton *close_; |
b26eb97d | 4257 | BOOL running_; |
1eb0c554 | 4258 | SHA1SumValue springlist_; |
bde2d79b | 4259 | SHA1SumValue notifyconf_; |
6981ccdf | 4260 | NSString *title_; |
4941f41d JF |
4261 | } |
4262 | ||
9ae52960 | 4263 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
4941f41d | 4264 | |
e057ec05 JF |
4265 | - (void) _retachThread; |
4266 | - (void) _detachNewThreadData:(ProgressData *)data; | |
4267 | - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title; | |
4941f41d | 4268 | |
b26eb97d JF |
4269 | - (BOOL) isRunning; |
4270 | ||
4941f41d JF |
4271 | @end |
4272 | ||
f441e717 GP |
4273 | @protocol ProgressControllerDelegate |
4274 | - (void) progressControllerIsComplete:(ProgressController *)sender; | |
e057ec05 JF |
4275 | @end |
4276 | ||
f441e717 | 4277 | @implementation ProgressController |
4941f41d JF |
4278 | |
4279 | - (void) dealloc { | |
9ae52960 | 4280 | [database_ setDelegate:nil]; |
e057ec05 JF |
4281 | [progress_ release]; |
4282 | [output_ release]; | |
4283 | [status_ release]; | |
238b07ce | 4284 | [close_ release]; |
6981ccdf JF |
4285 | if (title_ != nil) |
4286 | [title_ release]; | |
4941f41d JF |
4287 | [super dealloc]; |
4288 | } | |
4289 | ||
9ae52960 GP |
4290 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { |
4291 | if ((self = [super init]) != nil) { | |
7600bd69 | 4292 | database_ = database; |
9ae52960 | 4293 | [database_ setDelegate:self]; |
e057ec05 | 4294 | delegate_ = delegate; |
1cb11c5f | 4295 | |
1e4922b8 | 4296 | [[self view] setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f]]; |
4941f41d | 4297 | |
9ae52960 | 4298 | progress_ = [[UIProgressBar alloc] init]; |
d3bef7bc | 4299 | [progress_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)]; |
238b07ce | 4300 | [progress_ setStyle:0]; |
1cb11c5f | 4301 | |
9ae52960 | 4302 | status_ = [[UITextLabel alloc] init]; |
d3bef7bc | 4303 | [status_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)]; |
87c76914 JF |
4304 | [status_ setColor:[UIColor whiteColor]]; |
4305 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
e057ec05 JF |
4306 | [status_ setCentersHorizontally:YES]; |
4307 | //[status_ setFont:font]; | |
1cb11c5f | 4308 | |
9ae52960 | 4309 | output_ = [[UITextView alloc] init]; |
3325a005 | 4310 | |
d3bef7bc | 4311 | [output_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
e057ec05 | 4312 | //[output_ setTextFont:@"Courier New"]; |
c59881cd | 4313 | [output_ setFont:[[output_ font] fontWithSize:12]]; |
87c76914 JF |
4314 | [output_ setTextColor:[UIColor whiteColor]]; |
4315 | [output_ setBackgroundColor:[UIColor clearColor]]; | |
e057ec05 JF |
4316 | [output_ setMarginTop:0]; |
4317 | [output_ setAllowsRubberBanding:YES]; | |
81ab76dc | 4318 | [output_ setEditable:NO]; |
9ae52960 | 4319 | [[self view] addSubview:output_]; |
686e302f | 4320 | |
9ae52960 | 4321 | close_ = [[UIPushButton alloc] init]; |
d3bef7bc | 4322 | [close_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)]; |
238b07ce JF |
4323 | [close_ setAutosizesToFit:NO]; |
4324 | [close_ setDrawsShadow:YES]; | |
4325 | [close_ setStretchBackground:YES]; | |
238b07ce | 4326 | [close_ setEnabled:YES]; |
9ae52960 | 4327 | [close_ setTitleFont:[UIFont boldSystemFontOfSize:22]]; |
017b2b71 | 4328 | [close_ addTarget:self action:@selector(closeButtonPushed) forEvents:UIControlEventTouchUpInside]; |
238b07ce JF |
4329 | [close_ setBackground:[UIImage applicationImageNamed:@"green-up.png"] forState:0]; |
4330 | [close_ setBackground:[UIImage applicationImageNamed:@"green-dn.png"] forState:1]; | |
e057ec05 | 4331 | } return self; |
4941f41d JF |
4332 | } |
4333 | ||
9ae52960 GP |
4334 | - (void) positionViews { |
4335 | CGRect bounds = [[self view] bounds]; | |
4336 | CGSize prgsize = [UIProgressBar defaultSize]; | |
4337 | ||
4338 | CGRect prgrect = {{ | |
4339 | (bounds.size.width - prgsize.width) / 2, | |
12e0ef8f | 4340 | bounds.size.height - prgsize.height - 20 |
9ae52960 | 4341 | }, prgsize}; |
f99f86e2 | 4342 | |
96291f72 | 4343 | float closewidth = std::min(bounds.size.width - 20, 300.0f); |
9ae52960 GP |
4344 | |
4345 | [progress_ setFrame:prgrect]; | |
4346 | [status_ setFrame:CGRectMake( | |
4347 | 10, | |
12e0ef8f | 4348 | bounds.size.height - prgsize.height - 50, |
9ae52960 GP |
4349 | bounds.size.width - 20, |
4350 | 24 | |
4351 | )]; | |
4352 | [output_ setFrame:CGRectMake( | |
4353 | 10, | |
4354 | 20, | |
4355 | bounds.size.width - 20, | |
12e0ef8f | 4356 | bounds.size.height - 62 |
9ae52960 GP |
4357 | )]; |
4358 | [close_ setFrame:CGRectMake( | |
4359 | (bounds.size.width - closewidth) / 2, | |
12e0ef8f | 4360 | bounds.size.height - prgsize.height - 50, |
9ae52960 GP |
4361 | closewidth, |
4362 | 32 + prgsize.height | |
4363 | )]; | |
4364 | } | |
4365 | ||
4366 | - (void) viewWillAppear:(BOOL)animated { | |
4367 | [super viewDidAppear:animated]; | |
4368 | [[self navigationItem] setHidesBackButton:YES]; | |
1e4922b8 | 4369 | [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlack]; |
f99f86e2 | 4370 | |
9ae52960 | 4371 | [self positionViews]; |
4941f41d JF |
4372 | } |
4373 | ||
9ae52960 GP |
4374 | - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { |
4375 | [self positionViews]; | |
4941f41d JF |
4376 | } |
4377 | ||
9ae52960 | 4378 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
3d257c70 | 4379 | NSString *context([alert context]); |
03d01f0e | 4380 | |
6981ccdf | 4381 | if ([context isEqualToString:@"conffile"]) { |
9ae52960 | 4382 | FILE *input = [database_ input]; |
96291f72 JF |
4383 | if (button == [alert cancelButtonIndex]) |
4384 | fprintf(input, "N\n"); | |
4385 | else if (button == [alert firstOtherButtonIndex]) | |
4386 | fprintf(input, "Y\n"); | |
9ae52960 | 4387 | fflush(input); |
6981ccdf JF |
4388 | } |
4389 | } | |
4390 | ||
4391 | - (void) closeButtonPushed { | |
4392 | running_ = NO; | |
4393 | ||
864da84b RP |
4394 | UpdateExternalStatus(0); |
4395 | ||
6981ccdf JF |
4396 | switch (Finish_) { |
4397 | case 0: | |
9ae52960 | 4398 | [self dismissModalViewControllerAnimated:YES]; |
6981ccdf JF |
4399 | break; |
4400 | ||
4401 | case 1: | |
74a834de JF |
4402 | [delegate_ terminateWithSuccess]; |
4403 | /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) | |
4404 | [delegate_ suspendWithAnimation:YES]; | |
4405 | else | |
4406 | [delegate_ suspend];*/ | |
6981ccdf JF |
4407 | break; |
4408 | ||
4409 | case 2: | |
4410 | system("launchctl stop com.apple.SpringBoard"); | |
4411 | break; | |
4412 | ||
4413 | case 3: | |
4414 | system("launchctl unload "SpringBoard_"; launchctl load "SpringBoard_); | |
4415 | break; | |
4416 | ||
4417 | case 4: | |
4418 | system("reboot"); | |
4419 | break; | |
03d01f0e | 4420 | } |
6981ccdf | 4421 | } |
b26eb97d | 4422 | |
6981ccdf | 4423 | - (void) _retachThread { |
9ae52960 | 4424 | [[self navigationItem] setTitle:UCLocalize("COMPLETE")]; |
6981ccdf | 4425 | |
9ae52960 | 4426 | [[self view] addSubview:close_]; |
6981ccdf JF |
4427 | [progress_ removeFromSuperview]; |
4428 | [status_ removeFromSuperview]; | |
4429 | ||
4430 | [database_ popErrorWithTitle:title_]; | |
f441e717 | 4431 | [delegate_ progressControllerIsComplete:self]; |
cb9c2100 | 4432 | |
bde2d79b | 4433 | if (Finish_ < 4) { |
bd8e54e1 JF |
4434 | FileFd file; |
4435 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
4436 | _error->Discard(); | |
4437 | else { | |
4438 | MMap mmap(file, MMap::ReadOnly); | |
4439 | SHA1Summation sha1; | |
4440 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
4441 | if (!(notifyconf_ == sha1.Result())) | |
4442 | Finish_ = 4; | |
4443 | } | |
bde2d79b JF |
4444 | } |
4445 | ||
f464053e | 4446 | if (Finish_ < 3) { |
bd8e54e1 JF |
4447 | FileFd file; |
4448 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
4449 | _error->Discard(); | |
4450 | else { | |
4451 | MMap mmap(file, MMap::ReadOnly); | |
4452 | SHA1Summation sha1; | |
4453 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
4454 | if (!(springlist_ == sha1.Result())) | |
4455 | Finish_ = 3; | |
4456 | } | |
1eb0c554 JF |
4457 | } |
4458 | ||
03d01f0e | 4459 | switch (Finish_) { |
9ae52960 | 4460 | case 0: [close_ setTitle:UCLocalize("RETURN_TO_CYDIA")]; break; /* XXX: Maybe UCLocalize("DONE")? */ |
61b13cae JF |
4461 | case 1: [close_ setTitle:UCLocalize("CLOSE_CYDIA")]; break; |
4462 | case 2: [close_ setTitle:UCLocalize("RESTART_SPRINGBOARD")]; break; | |
4463 | case 3: [close_ setTitle:UCLocalize("RELOAD_SPRINGBOARD")]; break; | |
4464 | case 4: [close_ setTitle:UCLocalize("REBOOT_DEVICE")]; break; | |
03d01f0e JF |
4465 | } |
4466 | ||
575ffd3c | 4467 | system("su -c /usr/bin/uicache mobile"); |
5f54c108 | 4468 | |
864da84b RP |
4469 | UpdateExternalStatus(Finish_ == 0 ? 2 : 0); |
4470 | ||
b26eb97d | 4471 | [delegate_ setStatusBarShowsProgress:NO]; |
238b07ce JF |
4472 | } |
4473 | ||
f464053e | 4474 | - (void) _detachNewThreadData:(ProgressData *)data { _pooled |
e057ec05 | 4475 | [[data target] performSelector:[data selector] withObject:[data object]]; |
e057ec05 | 4476 | [self performSelectorOnMainThread:@selector(_retachThread) withObject:nil waitUntilDone:YES]; |
a933cee2 JF |
4477 | } |
4478 | ||
e057ec05 | 4479 | - (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)object title:(NSString *)title { |
864da84b RP |
4480 | UpdateExternalStatus(1); |
4481 | ||
6981ccdf JF |
4482 | if (title_ != nil) |
4483 | [title_ release]; | |
4484 | if (title == nil) | |
4485 | title_ = nil; | |
4486 | else | |
4487 | title_ = [title retain]; | |
4488 | ||
9ae52960 | 4489 | [[self navigationItem] setTitle:title_]; |
686e302f | 4490 | |
e057ec05 JF |
4491 | [status_ setText:nil]; |
4492 | [output_ setText:@""]; | |
4493 | [progress_ setProgress:0]; | |
a933cee2 | 4494 | |
238b07ce | 4495 | [close_ removeFromSuperview]; |
9ae52960 GP |
4496 | [[self view] addSubview:progress_]; |
4497 | [[self view] addSubview:status_]; | |
238b07ce | 4498 | |
b26eb97d JF |
4499 | [delegate_ setStatusBarShowsProgress:YES]; |
4500 | running_ = YES; | |
4501 | ||
bde2d79b | 4502 | { |
bd8e54e1 JF |
4503 | FileFd file; |
4504 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
4505 | _error->Discard(); | |
4506 | else { | |
4507 | MMap mmap(file, MMap::ReadOnly); | |
4508 | SHA1Summation sha1; | |
4509 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
4510 | notifyconf_ = sha1.Result(); | |
4511 | } | |
bde2d79b JF |
4512 | } |
4513 | ||
f464053e | 4514 | { |
bd8e54e1 JF |
4515 | FileFd file; |
4516 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
4517 | _error->Discard(); | |
4518 | else { | |
4519 | MMap mmap(file, MMap::ReadOnly); | |
4520 | SHA1Summation sha1; | |
4521 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
4522 | springlist_ = sha1.Result(); | |
4523 | } | |
1eb0c554 JF |
4524 | } |
4525 | ||
e057ec05 JF |
4526 | [NSThread |
4527 | detachNewThreadSelector:@selector(_detachNewThreadData:) | |
4528 | toTarget:self | |
a591888a | 4529 | withObject:[[[ProgressData alloc] |
e057ec05 JF |
4530 | initWithSelector:selector |
4531 | target:target | |
4532 | object:object | |
a591888a | 4533 | ] autorelease] |
e057ec05 | 4534 | ]; |
a933cee2 JF |
4535 | } |
4536 | ||
965edd52 JF |
4537 | - (void) repairWithSelector:(SEL)selector { |
4538 | [self | |
4539 | detachNewThreadSelector:selector | |
4540 | toTarget:database_ | |
4541 | withObject:nil | |
61b13cae | 4542 | title:UCLocalize("REPAIRING") |
965edd52 JF |
4543 | ]; |
4544 | } | |
4545 | ||
7600bd69 JF |
4546 | - (void) setConfigurationData:(NSString *)data { |
4547 | [self | |
4548 | performSelectorOnMainThread:@selector(_setConfigurationData:) | |
4549 | withObject:data | |
4550 | waitUntilDone:YES | |
4551 | ]; | |
4552 | } | |
4553 | ||
6981ccdf JF |
4554 | - (void) setProgressError:(NSString *)error withTitle:(NSString *)title { |
4555 | CYActionSheet *sheet([[[CYActionSheet alloc] | |
4556 | initWithTitle:title | |
61b13cae | 4557 | buttons:[NSArray arrayWithObjects:UCLocalize("OKAY"), nil] |
238b07ce | 4558 | defaultButtonIndex:0 |
6981ccdf | 4559 | ] autorelease]); |
238b07ce | 4560 | |
9e639c5a | 4561 | [sheet setMessage:error]; |
6981ccdf JF |
4562 | [sheet yieldToPopupAlertAnimated:YES]; |
4563 | [sheet dismiss]; | |
a933cee2 JF |
4564 | } |
4565 | ||
e057ec05 JF |
4566 | - (void) setProgressTitle:(NSString *)title { |
4567 | [self | |
4568 | performSelectorOnMainThread:@selector(_setProgressTitle:) | |
4569 | withObject:title | |
4570 | waitUntilDone:YES | |
4571 | ]; | |
a933cee2 JF |
4572 | } |
4573 | ||
e057ec05 JF |
4574 | - (void) setProgressPercent:(float)percent { |
4575 | [self | |
4576 | performSelectorOnMainThread:@selector(_setProgressPercent:) | |
4577 | withObject:[NSNumber numberWithFloat:percent] | |
4578 | waitUntilDone:YES | |
4579 | ]; | |
a933cee2 JF |
4580 | } |
4581 | ||
87c76914 | 4582 | - (void) startProgress { |
87c76914 JF |
4583 | } |
4584 | ||
e057ec05 JF |
4585 | - (void) addProgressOutput:(NSString *)output { |
4586 | [self | |
4587 | performSelectorOnMainThread:@selector(_addProgressOutput:) | |
4588 | withObject:output | |
4589 | waitUntilDone:YES | |
4590 | ]; | |
a933cee2 JF |
4591 | } |
4592 | ||
87c76914 | 4593 | - (bool) isCancelling:(size_t)received { |
87c76914 JF |
4594 | return false; |
4595 | } | |
4596 | ||
7600bd69 | 4597 | - (void) _setConfigurationData:(NSString *)data { |
faf4eb4f JF |
4598 | static Pcre conffile_r("^'(.*)' '(.*)' ([01]) ([01])$"); |
4599 | ||
6981ccdf JF |
4600 | if (!conffile_r(data)) { |
4601 | lprintf("E:invalid conffile\n"); | |
4602 | return; | |
4603 | } | |
7600bd69 JF |
4604 | |
4605 | NSString *ofile = conffile_r[1]; | |
4606 | //NSString *nfile = conffile_r[2]; | |
4607 | ||
3d257c70 | 4608 | UIAlertView *alert = [[[UIAlertView alloc] |
61b13cae | 4609 | initWithTitle:UCLocalize("CONFIGURATION_UPGRADE") |
9ae52960 GP |
4610 | message:[NSString stringWithFormat:@"%@\n\n%@", UCLocalize("CONFIGURATION_UPGRADE_EX"), ofile] |
4611 | delegate:self | |
4612 | cancelButtonTitle:UCLocalize("KEEP_OLD_COPY") | |
4613 | otherButtonTitles:UCLocalize("ACCEPT_NEW_COPY"), | |
3d257c70 | 4614 | // XXX: UCLocalize("SEE_WHAT_CHANGED"), |
9ae52960 GP |
4615 | nil |
4616 | ] autorelease]; | |
3d257c70 | 4617 | |
9ae52960 | 4618 | [alert setContext:@"conffile"]; |
3d257c70 | 4619 | [alert show]; |
7600bd69 JF |
4620 | } |
4621 | ||
e057ec05 | 4622 | - (void) _setProgressTitle:(NSString *)title { |
907a35d6 JF |
4623 | NSMutableArray *words([[title componentsSeparatedByString:@" "] mutableCopy]); |
4624 | for (size_t i(0), e([words count]); i != e; ++i) { | |
4625 | NSString *word([words objectAtIndex:i]); | |
4626 | if (Package *package = [database_ packageWithName:word]) | |
4627 | [words replaceObjectAtIndex:i withObject:[package name]]; | |
4628 | } | |
4629 | ||
4630 | [status_ setText:[words componentsJoinedByString:@" "]]; | |
686e302f | 4631 | } |
a933cee2 | 4632 | |
e057ec05 JF |
4633 | - (void) _setProgressPercent:(NSNumber *)percent { |
4634 | [progress_ setProgress:[percent floatValue]]; | |
242bcc6d JF |
4635 | } |
4636 | ||
e057ec05 JF |
4637 | - (void) _addProgressOutput:(NSString *)output { |
4638 | [output_ setText:[NSString stringWithFormat:@"%@\n%@", [output_ text], output]]; | |
4639 | CGSize size = [output_ contentSize]; | |
4640 | CGRect rect = {{0, size.height}, {size.width, 0}}; | |
4641 | [output_ scrollRectToVisible:rect animated:YES]; | |
4642 | } | |
3957dd75 | 4643 | |
b26eb97d | 4644 | - (BOOL) isRunning { |
5f54c108 | 4645 | return running_; |
b26eb97d JF |
4646 | } |
4647 | ||
e057ec05 JF |
4648 | @end |
4649 | /* }}} */ | |
3957dd75 | 4650 | |
d4a9ec10 | 4651 | /* Cell Content View {{{ */ |
1e4922b8 JF |
4652 | @protocol ContentDelegate |
4653 | - (void) drawContentRect:(CGRect)rect; | |
4654 | @end | |
4655 | ||
5d8f1006 | 4656 | @interface ContentView : UIView { |
1e4922b8 | 4657 | _transient id<ContentDelegate> delegate_; |
5d8f1006 JF |
4658 | } |
4659 | ||
4660 | @end | |
4661 | ||
d4a9ec10 | 4662 | @implementation ContentView |
1b242491 | 4663 | |
d4a9ec10 GP |
4664 | - (id) initWithFrame:(CGRect)frame { |
4665 | if ((self = [super initWithFrame:frame]) != nil) { | |
d4a9ec10 GP |
4666 | [self setNeedsDisplayOnBoundsChange:YES]; |
4667 | } return self; | |
4668 | } | |
4669 | ||
1e4922b8 | 4670 | - (void) setDelegate:(id<ContentDelegate>)delegate { |
d4a9ec10 GP |
4671 | delegate_ = delegate; |
4672 | } | |
4673 | ||
4674 | - (void) drawRect:(CGRect)rect { | |
4675 | [super drawRect:rect]; | |
4676 | [delegate_ drawContentRect:rect]; | |
4677 | } | |
1b242491 JF |
4678 | |
4679 | @end | |
4680 | /* }}} */ | |
4681 | /* Cydia TableView Cell {{{ */ | |
4682 | @interface CYTableViewCell : UITableViewCell { | |
4683 | ContentView *content_; | |
4684 | bool highlighted_; | |
4685 | } | |
4686 | ||
4687 | @end | |
4688 | ||
4689 | @implementation CYTableViewCell | |
4690 | ||
4691 | - (void) dealloc { | |
4692 | [content_ release]; | |
4693 | [super dealloc]; | |
4694 | } | |
4695 | ||
4696 | - (void) _updateHighlightColorsForView:(id)view highlighted:(BOOL)highlighted { | |
4697 | //NSLog(@"_updateHighlightColorsForView:%@ highlighted:%s [content_=%@]", view, highlighted ? "YES" : "NO", content_); | |
4698 | ||
4699 | if (view == content_) { | |
4700 | //NSLog(@"_updateHighlightColorsForView:content_ highlighted:%s", highlighted ? "YES" : "NO", content_); | |
4701 | highlighted_ = highlighted; | |
4702 | } | |
4703 | ||
4704 | [super _updateHighlightColorsForView:view highlighted:highlighted]; | |
4705 | } | |
4706 | ||
4707 | - (void) setSelected:(BOOL)selected animated:(BOOL)animated { | |
4708 | //NSLog(@"setSelected:%s animated:%s", selected ? "YES" : "NO", animated ? "YES" : "NO"); | |
4709 | highlighted_ = selected; | |
4710 | ||
4711 | [super setSelected:selected animated:animated]; | |
4712 | [content_ setNeedsDisplay]; | |
4713 | } | |
4714 | ||
d4a9ec10 GP |
4715 | @end |
4716 | /* }}} */ | |
4717 | /* Package Cell {{{ */ | |
1b242491 | 4718 | @interface PackageCell : CYTableViewCell < |
1e4922b8 JF |
4719 | ContentDelegate |
4720 | > { | |
5e563e79 JF |
4721 | UIImage *icon_; |
4722 | NSString *name_; | |
4723 | NSString *description_; | |
d8d9a65c | 4724 | bool commercial_; |
5e563e79 | 4725 | NSString *source_; |
cb9c2100 | 4726 | UIImage *badge_; |
3ff1504e | 4727 | Package *package_; |
6981ccdf | 4728 | UIImage *placard_; |
e057ec05 | 4729 | } |
138ae18d | 4730 | |
e057ec05 JF |
4731 | - (PackageCell *) init; |
4732 | - (void) setPackage:(Package *)package; | |
1cb11c5f | 4733 | |
3319715b | 4734 | + (int) heightForPackage:(Package *)package; |
5d8f1006 JF |
4735 | - (void) drawContentRect:(CGRect)rect; |
4736 | ||
4737 | @end | |
4738 | ||
e057ec05 JF |
4739 | @implementation PackageCell |
4740 | ||
5e563e79 JF |
4741 | - (void) clearPackage { |
4742 | if (icon_ != nil) { | |
4743 | [icon_ release]; | |
4744 | icon_ = nil; | |
4745 | } | |
4746 | ||
4747 | if (name_ != nil) { | |
4748 | [name_ release]; | |
4749 | name_ = nil; | |
4750 | } | |
4751 | ||
4752 | if (description_ != nil) { | |
4753 | [description_ release]; | |
4754 | description_ = nil; | |
4755 | } | |
4756 | ||
4757 | if (source_ != nil) { | |
4758 | [source_ release]; | |
4759 | source_ = nil; | |
4760 | } | |
cb9c2100 JF |
4761 | |
4762 | if (badge_ != nil) { | |
4763 | [badge_ release]; | |
4764 | badge_ = nil; | |
4765 | } | |
3ff1504e | 4766 | |
6981ccdf JF |
4767 | if (placard_ != nil) { |
4768 | [placard_ release]; | |
4769 | placard_ = nil; | |
4770 | } | |
4771 | ||
3ff1504e JF |
4772 | [package_ release]; |
4773 | package_ = nil; | |
5e563e79 JF |
4774 | } |
4775 | ||
e057ec05 | 4776 | - (void) dealloc { |
5e563e79 | 4777 | [self clearPackage]; |
e057ec05 | 4778 | [super dealloc]; |
686e302f | 4779 | } |
a933cee2 | 4780 | |
e057ec05 | 4781 | - (PackageCell *) init { |
5d8f1006 JF |
4782 | CGRect frame(CGRectMake(0, 0, 320, 74)); |
4783 | if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) { | |
4784 | UIView *content([self contentView]); | |
4785 | CGRect bounds([content bounds]); | |
d3bef7bc | 4786 | |
5d8f1006 | 4787 | content_ = [[ContentView alloc] initWithFrame:bounds]; |
d3bef7bc JF |
4788 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
4789 | [content addSubview:content_]; | |
4790 | ||
5d8f1006 | 4791 | [content_ setDelegate:self]; |
5d8f1006 | 4792 | [content_ setOpaque:YES]; |
e057ec05 | 4793 | } return self; |
686e302f | 4794 | } |
a933cee2 | 4795 | |
5d8f1006 JF |
4796 | - (void) _setBackgroundColor { |
4797 | UIColor *color; | |
4798 | if (NSString *mode = [package_ mode]) { | |
4799 | bool remove([mode isEqualToString:@"REMOVE"] || [mode isEqualToString:@"PURGE"]); | |
4800 | color = remove ? RemovingColor_ : InstallingColor_; | |
4801 | } else | |
4802 | color = [UIColor whiteColor]; | |
4803 | ||
4804 | [content_ setBackgroundColor:color]; | |
4805 | [self setNeedsDisplay]; | |
4806 | } | |
4807 | ||
e057ec05 | 4808 | - (void) setPackage:(Package *)package { |
d1494d2c | 4809 | [self clearPackage]; |
631a0a1e | 4810 | [package parse]; |
238b07ce | 4811 | |
e057ec05 | 4812 | Source *source = [package source]; |
a933cee2 | 4813 | |
dbe0f181 | 4814 | icon_ = [[package icon] retain]; |
5e563e79 | 4815 | name_ = [[package name] retain]; |
37455cf8 JF |
4816 | |
4817 | if (IsWildcat_) | |
4818 | description_ = [package longDescription]; | |
4819 | if (description_ == nil) | |
4820 | description_ = [package shortDescription]; | |
4821 | if (description_ != nil) | |
4822 | description_ = [description_ retain]; | |
4823 | ||
d8d9a65c | 4824 | commercial_ = [package isCommercial]; |
e057ec05 | 4825 | |
3ff1504e JF |
4826 | package_ = [package retain]; |
4827 | ||
9e07091a JF |
4828 | NSString *label = nil; |
4829 | bool trusted = false; | |
a933cee2 | 4830 | |
e057ec05 JF |
4831 | if (source != nil) { |
4832 | label = [source label]; | |
4833 | trusted = [source trusted]; | |
9e07091a | 4834 | } else if ([[package id] isEqualToString:@"firmware"]) |
61b13cae | 4835 | label = UCLocalize("APPLE"); |
faf4eb4f | 4836 | else |
61b13cae | 4837 | label = [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("UNKNOWN"), UCLocalize("LOCAL")]; |
a933cee2 | 4838 | |
6932575e | 4839 | NSString *from(label); |
9e07091a | 4840 | |
6932575e JF |
4841 | NSString *section = [package simpleSection]; |
4842 | if (section != nil && ![section isEqualToString:label]) { | |
4843 | section = [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
61b13cae | 4844 | from = [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), from, section]; |
6932575e | 4845 | } |
9e07091a | 4846 | |
61b13cae | 4847 | from = [NSString stringWithFormat:UCLocalize("FROM"), from]; |
5e563e79 | 4848 | source_ = [from retain]; |
e057ec05 | 4849 | |
cb9c2100 JF |
4850 | if (NSString *purpose = [package primaryPurpose]) |
4851 | if ((badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]) != nil) | |
4852 | badge_ = [badge_ retain]; | |
4853 | ||
6981ccdf JF |
4854 | if ([package installed] != nil) |
4855 | if ((placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/installed.png", App_]]) != nil) | |
4856 | placard_ = [placard_ retain]; | |
4857 | ||
5d8f1006 JF |
4858 | [self _setBackgroundColor]; |
4859 | [content_ setNeedsDisplay]; | |
d8d9a65c JF |
4860 | } |
4861 | ||
5d8f1006 | 4862 | - (void) drawContentRect:(CGRect)rect { |
1b242491 | 4863 | bool highlighted(highlighted_); |
37455cf8 | 4864 | float width([self bounds].size.width); |
3ff1504e | 4865 | |
5d8f1006 JF |
4866 | #if 0 |
4867 | CGContextRef context(UIGraphicsGetCurrentContext()); | |
4868 | [([[self selectedBackgroundView] superview] != nil ? [UIColor clearColor] : [self backgroundColor]) set]; | |
4869 | CGContextFillRect(context, rect); | |
4870 | #endif | |
a933cee2 | 4871 | |
87c76914 JF |
4872 | if (icon_ != nil) { |
4873 | CGRect rect; | |
4874 | rect.size = [icon_ size]; | |
4875 | ||
4876 | rect.size.width /= 2; | |
4877 | rect.size.height /= 2; | |
4878 | ||
4879 | rect.origin.x = 25 - rect.size.width / 2; | |
4880 | rect.origin.y = 25 - rect.size.height / 2; | |
4881 | ||
4882 | [icon_ drawInRect:rect]; | |
4883 | } | |
a933cee2 | 4884 | |
cb9c2100 JF |
4885 | if (badge_ != nil) { |
4886 | CGSize size = [badge_ size]; | |
4887 | ||
4888 | [badge_ drawAtPoint:CGPointMake( | |
4889 | 36 - size.width / 2, | |
4890 | 36 - size.height / 2 | |
4891 | )]; | |
4892 | } | |
4893 | ||
1b242491 | 4894 | if (highlighted) |
5e563e79 | 4895 | UISetColor(White_); |
a933cee2 | 4896 | |
1b242491 | 4897 | if (!highlighted) |
d8d9a65c | 4898 | UISetColor(commercial_ ? Purple_ : Black_); |
a6c1718e DH |
4899 | [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
4900 | [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; | |
a933cee2 | 4901 | |
1b242491 | 4902 | if (!highlighted) |
d8d9a65c | 4903 | UISetColor(commercial_ ? Purplish_ : Gray_); |
a6c1718e | 4904 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:UILineBreakModeTailTruncation]; |
6981ccdf JF |
4905 | |
4906 | if (placard_ != nil) | |
5ec44e34 | 4907 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; |
1cb11c5f JF |
4908 | } |
4909 | ||
3319715b | 4910 | + (int) heightForPackage:(Package *)package { |
631a0a1e | 4911 | return 73; |
3319715b JF |
4912 | } |
4913 | ||
20dd7407 JF |
4914 | @end |
4915 | /* }}} */ | |
e057ec05 | 4916 | /* Section Cell {{{ */ |
1b242491 | 4917 | @interface SectionCell : CYTableViewCell < |
1e4922b8 JF |
4918 | ContentDelegate |
4919 | > { | |
4923fbd7 | 4920 | NSString *basic_; |
fa7bb92f | 4921 | NSString *section_; |
5e563e79 JF |
4922 | NSString *name_; |
4923 | NSString *count_; | |
4924 | UIImage *icon_; | |
18884e36 | 4925 | UISwitch *switch_; |
fa7bb92f | 4926 | BOOL editing_; |
686e302f | 4927 | } |
a933cee2 | 4928 | |
fa7bb92f | 4929 | - (void) setSection:(Section *)section editing:(BOOL)editing; |
e057ec05 | 4930 | |
20dd7407 JF |
4931 | @end |
4932 | ||
e057ec05 | 4933 | @implementation SectionCell |
20dd7407 | 4934 | |
5e563e79 | 4935 | - (void) clearSection { |
4923fbd7 | 4936 | if (basic_ != nil) { |
7805b429 | 4937 | [basic_ release]; |
4923fbd7 JF |
4938 | basic_ = nil; |
4939 | } | |
4940 | ||
5e563e79 | 4941 | if (section_ != nil) { |
fa7bb92f | 4942 | [section_ release]; |
5e563e79 JF |
4943 | section_ = nil; |
4944 | } | |
faf4eb4f | 4945 | |
5e563e79 JF |
4946 | if (name_ != nil) { |
4947 | [name_ release]; | |
4948 | name_ = nil; | |
4949 | } | |
faf4eb4f | 4950 | |
5e563e79 JF |
4951 | if (count_ != nil) { |
4952 | [count_ release]; | |
4953 | count_ = nil; | |
4954 | } | |
faf4eb4f JF |
4955 | } |
4956 | ||
5e563e79 JF |
4957 | - (void) dealloc { |
4958 | [self clearSection]; | |
4959 | [icon_ release]; | |
4960 | [switch_ release]; | |
4961 | [super dealloc]; | |
faf4eb4f JF |
4962 | } |
4963 | ||
d4a9ec10 GP |
4964 | - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
4965 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
5e563e79 | 4966 | icon_ = [[UIImage applicationImageNamed:@"folder.png"] retain]; |
18884e36 | 4967 | switch_ = [[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)]; |
d4a9ec10 GP |
4968 | [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; |
4969 | ||
4970 | UIView *content([self contentView]); | |
4971 | CGRect bounds([content bounds]); | |
4972 | ||
4973 | content_ = [[ContentView alloc] initWithFrame:bounds]; | |
4974 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
4975 | [content addSubview:content_]; | |
4976 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
4977 | ||
4978 | [content_ setDelegate:self]; | |
686e302f | 4979 | } return self; |
a933cee2 JF |
4980 | } |
4981 | ||
fa7bb92f | 4982 | - (void) onSwitch:(id)sender { |
4923fbd7 | 4983 | NSMutableDictionary *metadata = [Sections_ objectForKey:basic_]; |
fa7bb92f JF |
4984 | if (metadata == nil) { |
4985 | metadata = [NSMutableDictionary dictionaryWithCapacity:2]; | |
4923fbd7 | 4986 | [Sections_ setObject:metadata forKey:basic_]; |
fa7bb92f JF |
4987 | } |
4988 | ||
4989 | Changed_ = true; | |
d4a9ec10 | 4990 | [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; |
fa7bb92f JF |
4991 | } |
4992 | ||
4993 | - (void) setSection:(Section *)section editing:(BOOL)editing { | |
4994 | if (editing != editing_) { | |
4995 | if (editing_) | |
4996 | [switch_ removeFromSuperview]; | |
4997 | else | |
4998 | [self addSubview:switch_]; | |
4999 | editing_ = editing; | |
5000 | } | |
5001 | ||
5e563e79 | 5002 | [self clearSection]; |
fa7bb92f | 5003 | |
e057ec05 | 5004 | if (section == nil) { |
61b13cae | 5005 | name_ = [UCLocalize("ALL_PACKAGES") retain]; |
5e563e79 | 5006 | count_ = nil; |
e057ec05 | 5007 | } else { |
7805b429 | 5008 | basic_ = [section name]; |
4923fbd7 JF |
5009 | if (basic_ != nil) |
5010 | basic_ = [basic_ retain]; | |
5011 | ||
43b742af | 5012 | section_ = [section localized]; |
fa7bb92f JF |
5013 | if (section_ != nil) |
5014 | section_ = [section_ retain]; | |
4923fbd7 | 5015 | |
61b13cae | 5016 | name_ = [(section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : section_) retain]; |
5e563e79 | 5017 | count_ = [[NSString stringWithFormat:@"%d", [section count]] retain]; |
fa7bb92f JF |
5018 | |
5019 | if (editing_) | |
d4a9ec10 | 5020 | [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; |
e057ec05 | 5021 | } |
d4a9ec10 | 5022 | |
1e4922b8 | 5023 | [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; |
864db79b JF |
5024 | [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; |
5025 | ||
d4a9ec10 | 5026 | [content_ setNeedsDisplay]; |
5e563e79 JF |
5027 | } |
5028 | ||
daf7f6e2 JF |
5029 | - (void) setFrame:(CGRect)frame { |
5030 | [super setFrame:frame]; | |
d4a9ec10 | 5031 | |
daf7f6e2 JF |
5032 | CGRect rect([switch_ frame]); |
5033 | [switch_ setFrame:CGRectMake(frame.size.width - 102, 9, rect.size.width, rect.size.height)]; | |
5034 | } | |
5035 | ||
d4a9ec10 | 5036 | - (void) drawContentRect:(CGRect)rect { |
1b242491 | 5037 | bool highlighted(highlighted_); |
f99f86e2 | 5038 | |
5e563e79 JF |
5039 | [icon_ drawInRect:CGRectMake(8, 7, 32, 32)]; |
5040 | ||
1b242491 | 5041 | if (highlighted) |
5e563e79 JF |
5042 | UISetColor(White_); |
5043 | ||
d4a9ec10 | 5044 | float width(rect.size.width); |
575ffd3c | 5045 | if (editing_) |
d4a9ec10 | 5046 | width -= 87; |
575ffd3c | 5047 | |
1b242491 JF |
5048 | if (!highlighted) |
5049 | UISetColor(Black_); | |
a6c1718e | 5050 | [name_ drawAtPoint:CGPointMake(48, 9) forWidth:(width - 70) withFont:Font22Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
fa7bb92f | 5051 | |
5e563e79 JF |
5052 | CGSize size = [count_ sizeWithFont:Font14_]; |
5053 | ||
5054 | UISetColor(White_); | |
5055 | if (count_ != nil) | |
cb9c2100 | 5056 | [count_ drawAtPoint:CGPointMake(13 + (29 - size.width) / 2, 16) withFont:Font12Bold_]; |
a933cee2 JF |
5057 | } |
5058 | ||
e057ec05 JF |
5059 | @end |
5060 | /* }}} */ | |
a933cee2 | 5061 | |
59efd93a | 5062 | /* File Table {{{ */ |
1e4922b8 JF |
5063 | @interface FileTable : CYViewController < |
5064 | UITableViewDataSource, | |
5065 | UITableViewDelegate | |
5066 | > { | |
e057ec05 | 5067 | _transient Database *database_; |
59efd93a JF |
5068 | Package *package_; |
5069 | NSString *name_; | |
5070 | NSMutableArray *files_; | |
d4a9ec10 | 5071 | UITableView *list_; |
59efd93a | 5072 | } |
a933cee2 | 5073 | |
9ae52960 | 5074 | - (id) initWithDatabase:(Database *)database; |
59efd93a JF |
5075 | - (void) setPackage:(Package *)package; |
5076 | ||
5077 | @end | |
5078 | ||
5079 | @implementation FileTable | |
5080 | ||
5081 | - (void) dealloc { | |
5082 | if (package_ != nil) | |
5083 | [package_ release]; | |
5084 | if (name_ != nil) | |
5085 | [name_ release]; | |
5086 | [files_ release]; | |
5087 | [list_ release]; | |
5088 | [super dealloc]; | |
5089 | } | |
5090 | ||
9b62701b | 5091 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
59efd93a JF |
5092 | return files_ == nil ? 0 : [files_ count]; |
5093 | } | |
5094 | ||
2064d251 GP |
5095 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
5096 | return 24.0f; | |
5097 | }*/ | |
59efd93a | 5098 | |
d4a9ec10 | 5099 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
9ae52960 GP |
5100 | static NSString *reuseIdentifier = @"Cell"; |
5101 | ||
d4a9ec10 GP |
5102 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5103 | if (cell == nil) { | |
5104 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
5105 | [cell setFont:[UIFont systemFontOfSize:16]]; | |
59efd93a | 5106 | } |
d4a9ec10 | 5107 | [cell setText:[files_ objectAtIndex:indexPath.row]]; |
1e4922b8 | 5108 | [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
9ae52960 | 5109 | |
d4a9ec10 | 5110 | return cell; |
59efd93a | 5111 | } |
a933cee2 | 5112 | |
9ae52960 GP |
5113 | - (id) initWithDatabase:(Database *)database { |
5114 | if ((self = [super init]) != nil) { | |
59efd93a | 5115 | database_ = database; |
a933cee2 | 5116 | |
9ae52960 GP |
5117 | [[self navigationItem] setTitle:UCLocalize("INSTALLED_FILES")]; |
5118 | ||
59efd93a | 5119 | files_ = [[NSMutableArray arrayWithCapacity:32] retain]; |
20dd7407 | 5120 | |
d4a9ec10 | 5121 | list_ = [[UITableView alloc] initWithFrame:[[self view] bounds]]; |
2064d251 GP |
5122 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5123 | [list_ setRowHeight:24.0f]; | |
9ae52960 | 5124 | [[self view] addSubview:list_]; |
59efd93a | 5125 | |
59efd93a | 5126 | [list_ setDataSource:self]; |
59efd93a | 5127 | [list_ setDelegate:self]; |
59efd93a JF |
5128 | } return self; |
5129 | } | |
5130 | ||
5131 | - (void) setPackage:(Package *)package { | |
5132 | if (package_ != nil) { | |
5133 | [package_ autorelease]; | |
5134 | package_ = nil; | |
5135 | } | |
5136 | ||
5137 | if (name_ != nil) { | |
5138 | [name_ release]; | |
5139 | name_ = nil; | |
5140 | } | |
5141 | ||
5142 | [files_ removeAllObjects]; | |
5143 | ||
5144 | if (package != nil) { | |
5145 | package_ = [package retain]; | |
5146 | name_ = [[package id] retain]; | |
5147 | ||
f464053e JF |
5148 | if (NSArray *files = [package files]) |
5149 | [files_ addObjectsFromArray:files]; | |
59efd93a | 5150 | |
fc19e583 JF |
5151 | if ([files_ count] != 0) { |
5152 | if ([[files_ objectAtIndex:0] isEqualToString:@"/."]) | |
5153 | [files_ removeObjectAtIndex:0]; | |
9e07091a | 5154 | [files_ sortUsingSelector:@selector(compareByPath:)]; |
2a987aa5 JF |
5155 | |
5156 | NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8]; | |
5157 | [stack addObject:@"/"]; | |
5158 | ||
5159 | for (int i(0), e([files_ count]); i != e; ++i) { | |
5160 | NSString *file = [files_ objectAtIndex:i]; | |
5161 | while (![file hasPrefix:[stack lastObject]]) | |
5162 | [stack removeLastObject]; | |
5163 | NSString *directory = [stack lastObject]; | |
5164 | [stack addObject:[file stringByAppendingString:@"/"]]; | |
5165 | [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", | |
9e07091a | 5166 | ([stack count] - 2) * 3, "", |
2a987aa5 JF |
5167 | [file substringFromIndex:[directory length]] |
5168 | ]]; | |
5169 | } | |
59efd93a JF |
5170 | } |
5171 | } | |
5172 | ||
5173 | [list_ reloadData]; | |
5174 | } | |
5175 | ||
59efd93a JF |
5176 | - (void) reloadData { |
5177 | [self setPackage:[database_ packageWithName:name_]]; | |
59efd93a | 5178 | } |
686e302f | 5179 | |
686e302f | 5180 | @end |
e057ec05 | 5181 | /* }}} */ |
f441e717 | 5182 | /* Package Controller {{{ */ |
48861ec9 DH |
5183 | @interface PackageController : CYBrowserController < |
5184 | UIActionSheetDelegate | |
5185 | > { | |
dbe0f181 | 5186 | _transient Database *database_; |
e057ec05 JF |
5187 | Package *package_; |
5188 | NSString *name_; | |
d8d9a65c | 5189 | bool commercial_; |
965edd52 | 5190 | NSMutableArray *buttons_; |
c2292b80 | 5191 | UIBarButtonItem *button_; |
df5a7529 JF |
5192 | } |
5193 | ||
9ae52960 | 5194 | - (id) initWithDatabase:(Database *)database; |
e057ec05 | 5195 | - (void) setPackage:(Package *)package; |
686e302f | 5196 | |
e057ec05 | 5197 | @end |
686e302f | 5198 | |
f441e717 | 5199 | @implementation PackageController |
686e302f | 5200 | |
e057ec05 | 5201 | - (void) dealloc { |
e057ec05 JF |
5202 | if (package_ != nil) |
5203 | [package_ release]; | |
5204 | if (name_ != nil) | |
5205 | [name_ release]; | |
c2292b80 | 5206 | |
965edd52 | 5207 | [buttons_ release]; |
c2292b80 JF |
5208 | |
5209 | if (button_ != nil) | |
5210 | [button_ release]; | |
5211 | ||
e057ec05 JF |
5212 | [super dealloc]; |
5213 | } | |
20dd7407 | 5214 | |
d287ae9a | 5215 | - (void) release { |
6932575e | 5216 | if ([self retainCount] == 1) |
f441e717 | 5217 | [delegate_ setPackageController:self]; |
6932575e | 5218 | [super release]; |
d287ae9a | 5219 | } |
6932575e JF |
5220 | |
5221 | /* XXX: this is not safe at all... localization of /fail/ */ | |
965edd52 | 5222 | - (void) _clickButtonWithName:(NSString *)name { |
61b13cae | 5223 | if ([name isEqualToString:UCLocalize("CLEAR")]) |
3ff1504e | 5224 | [delegate_ clearPackage:package_]; |
61b13cae | 5225 | else if ([name isEqualToString:UCLocalize("INSTALL")]) |
965edd52 | 5226 | [delegate_ installPackage:package_]; |
61b13cae | 5227 | else if ([name isEqualToString:UCLocalize("REINSTALL")]) |
965edd52 | 5228 | [delegate_ installPackage:package_]; |
61b13cae | 5229 | else if ([name isEqualToString:UCLocalize("REMOVE")]) |
965edd52 | 5230 | [delegate_ removePackage:package_]; |
61b13cae | 5231 | else if ([name isEqualToString:UCLocalize("UPGRADE")]) |
965edd52 JF |
5232 | [delegate_ installPackage:package_]; |
5233 | else _assert(false); | |
5234 | } | |
5235 | ||
1ca35d78 | 5236 | - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { |
a5dd312c | 5237 | NSString *context([sheet context]); |
965edd52 | 5238 | |
a5dd312c | 5239 | if ([context isEqualToString:@"modify"]) { |
1ca35d78 GP |
5240 | if (button != [sheet cancelButtonIndex]) { |
5241 | NSString *buttonName = [buttons_ objectAtIndex:button]; | |
5242 | [self _clickButtonWithName:buttonName]; | |
5243 | } | |
f99f86e2 | 5244 | |
1ca35d78 | 5245 | [sheet dismissWithClickedButtonIndex:-1 animated:YES]; |
1ca35d78 | 5246 | } |
686e302f | 5247 | } |
4941f41d | 5248 | |
c2292b80 JF |
5249 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
5250 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
6932575e | 5251 | [window setValue:package_ forKey:@"package"]; |
ad554f10 JF |
5252 | } |
5253 | ||
7b00c562 | 5254 | - (bool) _allowJavaScriptPanel { |
d8d9a65c | 5255 | return commercial_; |
7b00c562 JF |
5256 | } |
5257 | ||
907a35d6 | 5258 | #if !AlwaysReload |
48861ec9 | 5259 | - (void) _customButtonClicked { |
6981ccdf JF |
5260 | int count([buttons_ count]); |
5261 | if (count == 0) | |
5262 | return; | |
4941f41d | 5263 | |
965edd52 JF |
5264 | if (count == 1) |
5265 | [self _clickButtonWithName:[buttons_ objectAtIndex:0]]; | |
5266 | else { | |
1ca35d78 | 5267 | NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; |
965edd52 | 5268 | [buttons addObjectsFromArray:buttons_]; |
e057ec05 | 5269 | |
1ca35d78 | 5270 | UIActionSheet *sheet = [[[UIActionSheet alloc] |
fc19e583 | 5271 | initWithTitle:nil |
e057ec05 | 5272 | delegate:self |
1ca35d78 GP |
5273 | cancelButtonTitle:nil |
5274 | destructiveButtonTitle:nil | |
5275 | otherButtonTitles:nil | |
5276 | ] autorelease]; | |
5277 | ||
5278 | for (NSString *button in buttons) [sheet addButtonWithTitle:button]; | |
5279 | if (!IsWildcat_) { | |
5280 | [sheet addButtonWithTitle:UCLocalize("CANCEL")]; | |
5281 | [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; | |
5282 | } | |
5283 | [sheet setContext:@"modify"]; | |
f99f86e2 | 5284 | |
9ae52960 | 5285 | [delegate_ showActionSheet:sheet fromItem:[[self navigationItem] rightBarButtonItem]]; |
e057ec05 | 5286 | } |
686e302f | 5287 | } |
1ce016d4 | 5288 | |
3db9f43c DH |
5289 | // We don't want to allow non-commercial packages to do custom things to the install button, |
5290 | // so it must call customButtonClicked with a custom commercial_ == 1 fallthrough. | |
48861ec9 | 5291 | - (void) customButtonClicked { |
3db9f43c DH |
5292 | if (commercial_) |
5293 | [super customButtonClicked]; | |
5294 | else | |
48861ec9 | 5295 | [self _customButtonClicked]; |
1ce016d4 | 5296 | } |
c8e3b4b3 GP |
5297 | |
5298 | - (void) reloadButtonClicked { | |
b36becfc GP |
5299 | // Don't reload a package view by clicking the button. |
5300 | } | |
5301 | ||
5302 | - (void) applyLoadingTitle { | |
5303 | // Don't show "Loading" as the title. Ever. | |
c8e3b4b3 | 5304 | } |
3db9f43c DH |
5305 | |
5306 | - (UIBarButtonItem *) rightButton { | |
c2292b80 | 5307 | return button_; |
3db9f43c | 5308 | } |
907a35d6 | 5309 | #endif |
4941f41d | 5310 | |
9ae52960 GP |
5311 | - (id) initWithDatabase:(Database *)database { |
5312 | if ((self = [super init]) != nil) { | |
e057ec05 | 5313 | database_ = database; |
965edd52 | 5314 | buttons_ = [[NSMutableArray alloc] initWithCapacity:4]; |
6932575e | 5315 | [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"package" ofType:@"html"]]]; |
e057ec05 | 5316 | } return self; |
a75f53e7 JF |
5317 | } |
5318 | ||
e057ec05 JF |
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 | ||
965edd52 JF |
5330 | [buttons_ removeAllObjects]; |
5331 | ||
e057ec05 | 5332 | if (package != nil) { |
631a0a1e JF |
5333 | [package parse]; |
5334 | ||
e057ec05 JF |
5335 | package_ = [package retain]; |
5336 | name_ = [[package id] retain]; | |
d8d9a65c | 5337 | commercial_ = [package isCommercial]; |
e057ec05 | 5338 | |
3ff1504e | 5339 | if ([package_ mode] != nil) |
61b13cae | 5340 | [buttons_ addObject:UCLocalize("CLEAR")]; |
965edd52 | 5341 | if ([package_ source] == nil); |
238b07ce | 5342 | else if ([package_ upgradableAndEssential:NO]) |
61b13cae | 5343 | [buttons_ addObject:UCLocalize("UPGRADE")]; |
b4c4fac4 | 5344 | else if ([package_ uninstalled]) |
61b13cae | 5345 | [buttons_ addObject:UCLocalize("INSTALL")]; |
965edd52 | 5346 | else |
61b13cae | 5347 | [buttons_ addObject:UCLocalize("REINSTALL")]; |
b4c4fac4 | 5348 | if (![package_ uninstalled]) |
61b13cae | 5349 | [buttons_ addObject:UCLocalize("REMOVE")]; |
c2292b80 | 5350 | } |
6932575e | 5351 | |
c2292b80 JF |
5352 | if (button_ != nil) |
5353 | [button_ release]; | |
6932575e | 5354 | |
c2292b80 JF |
5355 | NSString *title; |
5356 | switch ([buttons_ count]) { | |
5357 | case 0: title = nil; break; | |
5358 | case 1: title = [buttons_ objectAtIndex:0]; break; | |
5359 | default: title = UCLocalize("MODIFY"); break; | |
e057ec05 | 5360 | } |
c2292b80 JF |
5361 | |
5362 | button_ = [[UIBarButtonItem alloc] | |
5363 | initWithTitle:title | |
5364 | style:UIBarButtonItemStylePlain | |
5365 | target:self | |
5366 | action:@selector(customButtonClicked) | |
5367 | ]; | |
3a3ed4dc JF |
5368 | |
5369 | [self reloadURL]; | |
9ae52960 | 5370 | } |
6932575e | 5371 | |
d8d9a65c JF |
5372 | - (bool) isLoading { |
5373 | return commercial_ ? [super isLoading] : false; | |
a99d2808 JF |
5374 | } |
5375 | ||
e057ec05 JF |
5376 | - (void) reloadData { |
5377 | [self setPackage:[database_ packageWithName:name_]]; | |
b6ffa083 JF |
5378 | } |
5379 | ||
686e302f JF |
5380 | @end |
5381 | /* }}} */ | |
686e302f | 5382 | /* Package Table {{{ */ |
1e4922b8 JF |
5383 | @interface PackageTable : UIView < |
5384 | UITableViewDataSource, | |
5385 | UITableViewDelegate | |
5386 | > { | |
e057ec05 | 5387 | _transient Database *database_; |
e057ec05 | 5388 | NSMutableArray *packages_; |
686e302f | 5389 | NSMutableArray *sections_; |
5d8f1006 JF |
5390 | UITableView *list_; |
5391 | NSMutableArray *index_; | |
5392 | NSMutableDictionary *indices_; | |
a591888a JF |
5393 | // XXX: this target_ seems to be delegate_. :( |
5394 | _transient id target_; | |
9ae52960 | 5395 | SEL action_; |
a591888a JF |
5396 | // XXX: why do we even have this delegate_? |
5397 | _transient id delegate_; | |
a75f53e7 JF |
5398 | } |
5399 | ||
9ae52960 | 5400 | - (id) initWithFrame:(CGRect)frame database:(Database *)database target:(id)target action:(SEL)action; |
686e302f JF |
5401 | |
5402 | - (void) setDelegate:(id)delegate; | |
8993ad57 | 5403 | |
e057ec05 | 5404 | - (void) reloadData; |
8993ad57 | 5405 | - (void) resetCursor; |
686e302f | 5406 | |
5d8f1006 | 5407 | - (UITableView *) list; |
2a987aa5 | 5408 | |
59efd93a JF |
5409 | - (void) setShouldHideHeaderInShortLists:(BOOL)hide; |
5410 | ||
9ae52960 GP |
5411 | - (void) deselectWithAnimation:(BOOL)animated; |
5412 | ||
686e302f JF |
5413 | @end |
5414 | ||
5415 | @implementation PackageTable | |
5416 | ||
5417 | - (void) dealloc { | |
e057ec05 | 5418 | [packages_ release]; |
686e302f | 5419 | [sections_ release]; |
e057ec05 | 5420 | [list_ release]; |
5d8f1006 JF |
5421 | [index_ release]; |
5422 | [indices_ release]; | |
9ae52960 | 5423 | |
686e302f | 5424 | [super dealloc]; |
b6ffa083 JF |
5425 | } |
5426 | ||
5d8f1006 JF |
5427 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
5428 | NSInteger count([sections_ count]); | |
5429 | return count == 0 ? 1 : count; | |
686e302f | 5430 | } |
4941f41d | 5431 | |
5d8f1006 JF |
5432 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
5433 | if ([sections_ count] == 0) | |
5434 | return nil; | |
686e302f JF |
5435 | return [[sections_ objectAtIndex:section] name]; |
5436 | } | |
a75f53e7 | 5437 | |
5d8f1006 JF |
5438 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
5439 | if ([sections_ count] == 0) | |
5440 | return 0; | |
5441 | return [[sections_ objectAtIndex:section] count]; | |
686e302f | 5442 | } |
a75f53e7 | 5443 | |
5d8f1006 JF |
5444 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
5445 | Section *section([sections_ objectAtIndex:[path section]]); | |
5446 | NSInteger row([path row]); | |
5447 | Package *package([packages_ objectAtIndex:([section row] + row)]); | |
5448 | return package; | |
686e302f | 5449 | } |
a75f53e7 | 5450 | |
5d8f1006 | 5451 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
1e4922b8 | 5452 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
5d8f1006 JF |
5453 | if (cell == nil) |
5454 | cell = [[[PackageCell alloc] init] autorelease]; | |
5455 | [cell setPackage:[self packageAtIndexPath:path]]; | |
5456 | return cell; | |
686e302f | 5457 | } |
a75f53e7 | 5458 | |
9ae52960 GP |
5459 | - (void) deselectWithAnimation:(BOOL)animated { |
5460 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
5461 | } | |
5462 | ||
2064d251 | 5463 | /*- (CGFloat) tableView:(UITableView *)table heightForRowAtIndexPath:(NSIndexPath *)path { |
5d8f1006 | 5464 | return [PackageCell heightForPackage:[self packageAtIndexPath:path]]; |
2064d251 | 5465 | }*/ |
a75f53e7 | 5466 | |
5d8f1006 JF |
5467 | - (NSIndexPath *) tableView:(UITableView *)table willSelectRowAtIndexPath:(NSIndexPath *)path { |
5468 | Package *package([self packageAtIndexPath:path]); | |
c3f582af | 5469 | package = [database_ packageWithName:[package id]]; |
9ae52960 | 5470 | [target_ performSelector:action_ withObject:package]; |
5d8f1006 JF |
5471 | return path; |
5472 | } | |
5473 | ||
5474 | - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { | |
5475 | return [packages_ count] > 20 ? index_ : nil; | |
5476 | } | |
5477 | ||
5478 | - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { | |
5479 | return index; | |
a75f53e7 JF |
5480 | } |
5481 | ||
9ae52960 GP |
5482 | - (id) initWithFrame:(CGRect)frame database:(Database *)database target:(id)target action:(SEL)action { |
5483 | if ((self = [super initWithFrame:frame]) != nil) { | |
e057ec05 | 5484 | database_ = database; |
9ae52960 GP |
5485 | |
5486 | target_ = target; | |
5487 | action_ = action; | |
e057ec05 | 5488 | |
5d8f1006 JF |
5489 | index_ = [[NSMutableArray alloc] initWithCapacity:32]; |
5490 | indices_ = [[NSMutableDictionary alloc] initWithCapacity:32]; | |
5491 | ||
e057ec05 | 5492 | packages_ = [[NSMutableArray arrayWithCapacity:16] retain]; |
686e302f | 5493 | sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; |
a75f53e7 | 5494 | |
5d8f1006 | 5495 | list_ = [[UITableView alloc] initWithFrame:[self bounds] style:UITableViewStylePlain]; |
d3bef7bc | 5496 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
2064d251 | 5497 | [list_ setRowHeight:73.0f]; |
d3bef7bc JF |
5498 | [self addSubview:list_]; |
5499 | ||
686e302f | 5500 | [list_ setDataSource:self]; |
5d8f1006 | 5501 | [list_ setDelegate:self]; |
686e302f | 5502 | } return self; |
a75f53e7 JF |
5503 | } |
5504 | ||
5505 | - (void) setDelegate:(id)delegate { | |
4941f41d | 5506 | delegate_ = delegate; |
686e302f JF |
5507 | } |
5508 | ||
ce09fc27 JF |
5509 | - (bool) hasPackage:(Package *)package { |
5510 | return true; | |
8993ad57 JF |
5511 | } |
5512 | ||
e057ec05 JF |
5513 | - (void) reloadData { |
5514 | NSArray *packages = [database_ packages]; | |
686e302f | 5515 | |
e057ec05 | 5516 | [packages_ removeAllObjects]; |
686e302f JF |
5517 | [sections_ removeAllObjects]; |
5518 | ||
3bddda52 | 5519 | _profile(PackageTable$reloadData$Filter) |
9fdd37d0 | 5520 | for (Package *package in packages) |
3bddda52 JF |
5521 | if ([self hasPackage:package]) |
5522 | [packages_ addObject:package]; | |
3bddda52 | 5523 | _end |
e057ec05 | 5524 | |
5d8f1006 JF |
5525 | [index_ removeAllObjects]; |
5526 | [indices_ removeAllObjects]; | |
5527 | ||
686e302f JF |
5528 | Section *section = nil; |
5529 | ||
3bddda52 | 5530 | _profile(PackageTable$reloadData$Section) |
9fdd37d0 JF |
5531 | for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { |
5532 | Package *package; | |
5533 | unichar index; | |
5534 | ||
5535 | _profile(PackageTable$reloadData$Section$Package) | |
5536 | package = [packages_ objectAtIndex:offset]; | |
5537 | index = [package index]; | |
5538 | _end | |
686e302f | 5539 | |
3bddda52 JF |
5540 | if (section == nil || [section index] != index) { |
5541 | _profile(PackageTable$reloadData$Section$Allocate) | |
5542 | section = [[[Section alloc] initWithIndex:index row:offset] autorelease]; | |
5543 | _end | |
686e302f | 5544 | |
5d8f1006 JF |
5545 | [index_ addObject:[section name]]; |
5546 | //[indices_ setObject:[NSNumber numberForInt:[sections_ count]] forKey:index]; | |
5547 | ||
9fdd37d0 JF |
5548 | _profile(PackageTable$reloadData$Section$Add) |
5549 | [sections_ addObject:section]; | |
5550 | _end | |
3bddda52 JF |
5551 | } |
5552 | ||
5553 | [section addToCount]; | |
5554 | } | |
5555 | _end | |
686e302f | 5556 | |
9fdd37d0 JF |
5557 | _profile(PackageTable$reloadData$List) |
5558 | [list_ reloadData]; | |
5559 | _end | |
686e302f JF |
5560 | } |
5561 | ||
8993ad57 | 5562 | - (void) resetCursor { |
5d8f1006 | 5563 | [list_ scrollRectToVisible:CGRectMake(0, 0, 0, 0) animated:NO]; |
8993ad57 JF |
5564 | } |
5565 | ||
5d8f1006 | 5566 | - (UITableView *) list { |
2a987aa5 JF |
5567 | return list_; |
5568 | } | |
5569 | ||
59efd93a | 5570 | - (void) setShouldHideHeaderInShortLists:(BOOL)hide { |
5d8f1006 | 5571 | //XXX:[list_ setShouldHideHeaderInShortLists:hide]; |
59efd93a JF |
5572 | } |
5573 | ||
ce09fc27 JF |
5574 | @end |
5575 | /* }}} */ | |
5576 | /* Filtered Package Table {{{ */ | |
5577 | @interface FilteredPackageTable : PackageTable { | |
5578 | SEL filter_; | |
142bd2db | 5579 | IMP imp_; |
ce09fc27 JF |
5580 | id object_; |
5581 | } | |
5582 | ||
5583 | - (void) setObject:(id)object; | |
5ec44e34 | 5584 | - (void) setObject:(id)object forFilter:(SEL)filter; |
ce09fc27 | 5585 | |
9ae52960 | 5586 | - (id) initWithFrame:(CGRect)frame database:(Database *)database target:(id)target action:(SEL)action filter:(SEL)filter with:(id)object; |
ce09fc27 JF |
5587 | |
5588 | @end | |
5589 | ||
5590 | @implementation FilteredPackageTable | |
5591 | ||
5592 | - (void) dealloc { | |
5593 | if (object_ != nil) | |
5594 | [object_ release]; | |
5595 | [super dealloc]; | |
5596 | } | |
5597 | ||
5ec44e34 JF |
5598 | - (void) setFilter:(SEL)filter { |
5599 | filter_ = filter; | |
5600 | ||
5601 | /* XXX: this is an unsafe optimization of doomy hell */ | |
5602 | Method method(class_getInstanceMethod([Package class], filter)); | |
5603 | _assert(method != NULL); | |
5604 | imp_ = method_getImplementation(method); | |
5605 | _assert(imp_ != NULL); | |
5606 | } | |
5607 | ||
ce09fc27 JF |
5608 | - (void) setObject:(id)object { |
5609 | if (object_ != nil) | |
5610 | [object_ release]; | |
5611 | if (object == nil) | |
5612 | object_ = nil; | |
5613 | else | |
5614 | object_ = [object retain]; | |
5615 | } | |
5616 | ||
5ec44e34 JF |
5617 | - (void) setObject:(id)object forFilter:(SEL)filter { |
5618 | [self setFilter:filter]; | |
5619 | [self setObject:object]; | |
5ec44e34 JF |
5620 | } |
5621 | ||
ce09fc27 | 5622 | - (bool) hasPackage:(Package *)package { |
142bd2db JF |
5623 | _profile(FilteredPackageTable$hasPackage) |
5624 | return [package valid] && (*reinterpret_cast<bool (*)(id, SEL, id)>(imp_))(package, filter_, object_); | |
5625 | _end | |
ce09fc27 JF |
5626 | } |
5627 | ||
9ae52960 GP |
5628 | - (id) initWithFrame:(CGRect)frame database:(Database *)database target:(id)target action:(SEL)action filter:(SEL)filter with:(id)object { |
5629 | if ((self = [super initWithFrame:frame database:database target:target action:action]) != nil) { | |
5ec44e34 | 5630 | [self setFilter:filter]; |
9ae52960 | 5631 | object_ = [object retain]; |
6e673d99 | 5632 | [self reloadData]; |
ce09fc27 JF |
5633 | } return self; |
5634 | } | |
5635 | ||
a75f53e7 | 5636 | @end |
686e302f | 5637 | /* }}} */ |
a75f53e7 | 5638 | |
f441e717 GP |
5639 | /* Filtered Package Controller {{{ */ |
5640 | @interface FilteredPackageController : CYViewController { | |
9ae52960 GP |
5641 | _transient Database *database_; |
5642 | FilteredPackageTable *packages_; | |
5643 | NSString *title_; | |
5644 | } | |
5645 | ||
5646 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object; | |
5647 | ||
5648 | @end | |
f99f86e2 | 5649 | |
f441e717 | 5650 | @implementation FilteredPackageController |
9ae52960 GP |
5651 | |
5652 | - (void) dealloc { | |
5653 | [packages_ release]; | |
5654 | [title_ release]; | |
f99f86e2 | 5655 | |
9ae52960 GP |
5656 | [super dealloc]; |
5657 | } | |
5658 | ||
5659 | - (void) viewDidAppear:(BOOL)animated { | |
5660 | [super viewDidAppear:animated]; | |
5661 | [packages_ deselectWithAnimation:animated]; | |
5662 | } | |
5663 | ||
5664 | - (void) didSelectPackage:(Package *)package { | |
f441e717 | 5665 | PackageController *view([delegate_ packageController]); |
9ae52960 GP |
5666 | [view setPackage:package]; |
5667 | [view setDelegate:delegate_]; | |
5668 | [[self navigationController] pushViewController:view animated:YES]; | |
5669 | } | |
5670 | ||
9b62701b | 5671 | - (NSString *) title { return title_; } |
9ae52960 GP |
5672 | |
5673 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object { | |
5674 | if ((self = [super init]) != nil) { | |
5675 | database_ = database; | |
5676 | title_ = [title copy]; | |
5677 | [[self navigationItem] setTitle:title_]; | |
5678 | ||
5679 | packages_ = [[FilteredPackageTable alloc] | |
5680 | initWithFrame:[[self view] bounds] | |
5681 | database:database | |
5682 | target:self | |
5683 | action:@selector(didSelectPackage:) | |
5684 | filter:filter | |
5685 | with:object | |
5686 | ]; | |
5687 | ||
5688 | [packages_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
5689 | [[self view] addSubview:packages_]; | |
5690 | } return self; | |
5691 | } | |
5692 | ||
5693 | - (void) reloadData { | |
5694 | [packages_ reloadData]; | |
5695 | } | |
5696 | ||
5697 | - (void) setDelegate:(id)delegate { | |
5698 | [super setDelegate:delegate]; | |
5699 | [packages_ setDelegate:delegate]; | |
5700 | } | |
5701 | ||
5702 | @end | |
f99f86e2 | 5703 | |
9ae52960 GP |
5704 | /* }}} */ |
5705 | ||
f441e717 GP |
5706 | /* Add Source Controller {{{ */ |
5707 | @interface AddSourceController : CYViewController { | |
faf4eb4f | 5708 | _transient Database *database_; |
a75f53e7 JF |
5709 | } |
5710 | ||
9ae52960 | 5711 | - (id) initWithDatabase:(Database *)database; |
686e302f | 5712 | |
faf4eb4f | 5713 | @end |
a75f53e7 | 5714 | |
f441e717 | 5715 | @implementation AddSourceController |
a75f53e7 | 5716 | |
9ae52960 GP |
5717 | - (id) initWithDatabase:(Database *)database { |
5718 | if ((self = [super init]) != nil) { | |
faf4eb4f JF |
5719 | database_ = database; |
5720 | } return self; | |
e057ec05 | 5721 | } |
a75f53e7 | 5722 | |
faf4eb4f JF |
5723 | @end |
5724 | /* }}} */ | |
5725 | /* Source Cell {{{ */ | |
1b242491 | 5726 | @interface SourceCell : CYTableViewCell < |
1e4922b8 JF |
5727 | ContentDelegate |
5728 | > { | |
5e563e79 JF |
5729 | UIImage *icon_; |
5730 | NSString *origin_; | |
5731 | NSString *description_; | |
5732 | NSString *label_; | |
686e302f JF |
5733 | } |
5734 | ||
d4a9ec10 | 5735 | - (void) setSource:(Source *)source; |
686e302f | 5736 | |
faf4eb4f | 5737 | @end |
686e302f | 5738 | |
faf4eb4f | 5739 | @implementation SourceCell |
686e302f | 5740 | |
d4a9ec10 | 5741 | - (void) clearSource { |
5e563e79 | 5742 | [icon_ release]; |
faf4eb4f JF |
5743 | [origin_ release]; |
5744 | [description_ release]; | |
5745 | [label_ release]; | |
d4a9ec10 GP |
5746 | |
5747 | icon_ = nil; | |
5748 | origin_ = nil; | |
5749 | description_ = nil; | |
5750 | label_ = nil; | |
5751 | } | |
5752 | ||
5753 | - (void) setSource:(Source *)source { | |
5754 | [self clearSource]; | |
f99f86e2 | 5755 | |
d4a9ec10 GP |
5756 | if (icon_ == nil) |
5757 | icon_ = [UIImage applicationImageNamed:[NSString stringWithFormat:@"Sources/%@.png", [source host]]]; | |
5758 | if (icon_ == nil) | |
5759 | icon_ = [UIImage applicationImageNamed:@"unknown.png"]; | |
5760 | icon_ = [icon_ retain]; | |
5761 | ||
5762 | origin_ = [[source name] retain]; | |
5763 | label_ = [[source uri] retain]; | |
5764 | description_ = [[source description] retain]; | |
5765 | ||
5766 | [content_ setNeedsDisplay]; | |
5767 | } | |
5768 | ||
5769 | - (void) dealloc { | |
5770 | [self clearSource]; | |
faf4eb4f | 5771 | [super dealloc]; |
e057ec05 | 5772 | } |
686e302f | 5773 | |
d4a9ec10 GP |
5774 | - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
5775 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
5776 | UIView *content([self contentView]); | |
5777 | CGRect bounds([content bounds]); | |
5778 | ||
5779 | content_ = [[ContentView alloc] initWithFrame:bounds]; | |
5780 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
5781 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
5782 | [content addSubview:content_]; | |
5783 | ||
5784 | [content_ setDelegate:self]; | |
5785 | [content_ setOpaque:YES]; | |
faf4eb4f | 5786 | } return self; |
48c0461e JF |
5787 | } |
5788 | ||
d4a9ec10 | 5789 | - (void) drawContentRect:(CGRect)rect { |
1b242491 | 5790 | bool highlighted(highlighted_); |
5ec44e34 JF |
5791 | float width(rect.size.width); |
5792 | ||
5e563e79 JF |
5793 | if (icon_ != nil) |
5794 | [icon_ drawInRect:CGRectMake(10, 10, 30, 30)]; | |
faf4eb4f | 5795 | |
1b242491 | 5796 | if (highlighted) |
5e563e79 | 5797 | UISetColor(White_); |
faf4eb4f | 5798 | |
1b242491 | 5799 | if (!highlighted) |
5e563e79 | 5800 | UISetColor(Black_); |
a6c1718e | 5801 | [origin_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - 80) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
faf4eb4f | 5802 | |
1b242491 | 5803 | if (!highlighted) |
5e563e79 | 5804 | UISetColor(Blue_); |
a6c1718e | 5805 | [label_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; |
faf4eb4f | 5806 | |
1b242491 | 5807 | if (!highlighted) |
5e563e79 | 5808 | UISetColor(Gray_); |
a6c1718e | 5809 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 40) withFont:Font14_ lineBreakMode:UILineBreakModeTailTruncation]; |
faf4eb4f JF |
5810 | } |
5811 | ||
5812 | @end | |
5813 | /* }}} */ | |
5814 | /* Source Table {{{ */ | |
1e4922b8 JF |
5815 | @interface SourceTable : CYViewController < |
5816 | UITableViewDataSource, | |
5817 | UITableViewDelegate | |
5818 | > { | |
faf4eb4f | 5819 | _transient Database *database_; |
d4a9ec10 | 5820 | UITableView *list_; |
faf4eb4f | 5821 | NSMutableArray *sources_; |
faf4eb4f JF |
5822 | int offset_; |
5823 | ||
5824 | NSString *href_; | |
5825 | UIProgressHUD *hud_; | |
5826 | NSError *error_; | |
5827 | ||
5828 | //NSURLConnection *installer_; | |
3d3f4666 | 5829 | NSURLConnection *trivial_; |
faf4eb4f JF |
5830 | NSURLConnection *trivial_bz2_; |
5831 | NSURLConnection *trivial_gz_; | |
5832 | //NSURLConnection *automatic_; | |
5833 | ||
3d3f4666 | 5834 | BOOL cydia_; |
faf4eb4f JF |
5835 | } |
5836 | ||
9ae52960 | 5837 | - (id) initWithDatabase:(Database *)database; |
faf4eb4f | 5838 | |
1e4922b8 JF |
5839 | - (void) updateButtonsForEditingStatus:(BOOL)editing animated:(BOOL)animated; |
5840 | ||
faf4eb4f JF |
5841 | @end |
5842 | ||
5843 | @implementation SourceTable | |
5844 | ||
a591888a | 5845 | - (void) _releaseConnection:(NSURLConnection *)connection { |
faf4eb4f JF |
5846 | if (connection != nil) { |
5847 | [connection cancel]; | |
5848 | //[connection setDelegate:nil]; | |
5849 | [connection release]; | |
5850 | } | |
5851 | } | |
5852 | ||
5853 | - (void) dealloc { | |
faf4eb4f JF |
5854 | if (href_ != nil) |
5855 | [href_ release]; | |
5856 | if (hud_ != nil) | |
5857 | [hud_ release]; | |
5858 | if (error_ != nil) | |
5859 | [error_ release]; | |
5860 | ||
a591888a JF |
5861 | //[self _releaseConnection:installer_]; |
5862 | [self _releaseConnection:trivial_]; | |
5863 | [self _releaseConnection:trivial_gz_]; | |
5864 | [self _releaseConnection:trivial_bz2_]; | |
5865 | //[self _releaseConnection:automatic_]; | |
faf4eb4f JF |
5866 | |
5867 | [sources_ release]; | |
5868 | [list_ release]; | |
5869 | [super dealloc]; | |
5870 | } | |
5871 | ||
9ae52960 GP |
5872 | - (void) viewDidAppear:(BOOL)animated { |
5873 | [super viewDidAppear:animated]; | |
5874 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
5875 | } | |
5876 | ||
9b62701b | 5877 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
faf4eb4f JF |
5878 | return offset_ == 0 ? 1 : 2; |
5879 | } | |
5880 | ||
9b62701b | 5881 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
faf4eb4f | 5882 | switch (section + (offset_ == 0 ? 1 : 0)) { |
61b13cae JF |
5883 | case 0: return UCLocalize("ENTERED_BY_USER"); |
5884 | case 1: return UCLocalize("INSTALLED_BY_PACKAGE"); | |
faf4eb4f | 5885 | |
6981ccdf | 5886 | _nodefault |
faf4eb4f JF |
5887 | } |
5888 | } | |
5889 | ||
9b62701b | 5890 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
d4a9ec10 GP |
5891 | int count = [sources_ count]; |
5892 | switch (section) { | |
5893 | case 0: return (offset_ == 0 ? count : offset_); | |
5894 | case 1: return count - offset_; | |
faf4eb4f | 5895 | |
9ae52960 | 5896 | _nodefault |
faf4eb4f JF |
5897 | } |
5898 | } | |
5899 | ||
d4a9ec10 GP |
5900 | - (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath { |
5901 | unsigned idx = 0; | |
5902 | switch (indexPath.section) { | |
5903 | case 0: idx = indexPath.row; break; | |
5904 | case 1: idx = indexPath.row + offset_; break; | |
5905 | ||
9ae52960 | 5906 | _nodefault |
d4a9ec10 GP |
5907 | } |
5908 | return [sources_ objectAtIndex:idx]; | |
faf4eb4f JF |
5909 | } |
5910 | ||
d4a9ec10 GP |
5911 | - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
5912 | Source *source = [self sourceAtIndexPath:indexPath]; | |
5e563e79 | 5913 | return [source description] == nil ? 56 : 73; |
faf4eb4f JF |
5914 | } |
5915 | ||
d4a9ec10 | 5916 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
9ae52960 | 5917 | static NSString *cellIdentifier = @"SourceCell"; |
faf4eb4f | 5918 | |
d4a9ec10 | 5919 | SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; |
9ae52960 GP |
5920 | if(cell == nil) cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease]; |
5921 | [cell setSource:[self sourceAtIndexPath:indexPath]]; | |
f99f86e2 | 5922 | |
d4a9ec10 | 5923 | return cell; |
faf4eb4f JF |
5924 | } |
5925 | ||
1e4922b8 JF |
5926 | - (UITableViewCellAccessoryType) tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath { |
5927 | return UITableViewCellAccessoryDisclosureIndicator; | |
faf4eb4f JF |
5928 | } |
5929 | ||
d4a9ec10 GP |
5930 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
5931 | Source *source = [self sourceAtIndexPath:indexPath]; | |
faf4eb4f | 5932 | |
f441e717 | 5933 | FilteredPackageController *packages = [[[FilteredPackageController alloc] |
9ae52960 | 5934 | initWithDatabase:database_ |
faf4eb4f JF |
5935 | title:[source label] |
5936 | filter:@selector(isVisibleInSource:) | |
5937 | with:source | |
5938 | ] autorelease]; | |
5939 | ||
5940 | [packages setDelegate:delegate_]; | |
5941 | ||
9ae52960 | 5942 | [[self navigationController] pushViewController:packages animated:YES]; |
faf4eb4f JF |
5943 | } |
5944 | ||
d4a9ec10 GP |
5945 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
5946 | Source *source = [self sourceAtIndexPath:indexPath]; | |
faf4eb4f JF |
5947 | return [source record] != nil; |
5948 | } | |
5949 | ||
d4a9ec10 GP |
5950 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
5951 | Source *source = [self sourceAtIndexPath:indexPath]; | |
faf4eb4f JF |
5952 | [Sources_ removeObjectForKey:[source key]]; |
5953 | [delegate_ syncData]; | |
5954 | } | |
5955 | ||
b4dff19a JF |
5956 | - (void) complete { |
5957 | [Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
5958 | @"deb", @"Type", | |
5959 | href_, @"URI", | |
5960 | @"./", @"Distribution", | |
5961 | nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href_]]; | |
5962 | ||
5963 | [delegate_ syncData]; | |
5964 | } | |
5965 | ||
5966 | - (NSString *) getWarning { | |
7b00c562 JF |
5967 | NSString *href(href_); |
5968 | NSRange colon([href rangeOfString:@"://"]); | |
5969 | if (colon.location != NSNotFound) | |
5970 | href = [href substringFromIndex:(colon.location + 3)]; | |
5971 | href = [href stringByAddingPercentEscapes]; | |
6981ccdf | 5972 | href = [CydiaURL(@"api/repotag/") stringByAppendingString:href]; |
b4dff19a JF |
5973 | href = [href stringByCachingURLWithCurrentCDN]; |
5974 | ||
5975 | NSURL *url([NSURL URLWithString:href]); | |
5976 | ||
5977 | NSStringEncoding encoding; | |
5978 | NSError *error(nil); | |
5979 | ||
5980 | if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]) | |
5981 | return [warning length] == 0 ? nil : warning; | |
5982 | return nil; | |
5983 | } | |
5984 | ||
faf4eb4f | 5985 | - (void) _endConnection:(NSURLConnection *)connection { |
a591888a JF |
5986 | // XXX: the memory management in this method is horribly awkward |
5987 | ||
faf4eb4f | 5988 | NSURLConnection **field = NULL; |
3d3f4666 JF |
5989 | if (connection == trivial_) |
5990 | field = &trivial_; | |
5991 | else if (connection == trivial_bz2_) | |
faf4eb4f JF |
5992 | field = &trivial_bz2_; |
5993 | else if (connection == trivial_gz_) | |
5994 | field = &trivial_gz_; | |
5995 | _assert(field != NULL); | |
5996 | [connection release]; | |
5997 | *field = nil; | |
5998 | ||
5999 | if ( | |
3d3f4666 | 6000 | trivial_ == nil && |
faf4eb4f JF |
6001 | trivial_bz2_ == nil && |
6002 | trivial_gz_ == nil | |
6003 | ) { | |
7b00c562 | 6004 | bool defer(false); |
faf4eb4f | 6005 | |
3d3f4666 | 6006 | if (cydia_) { |
b4dff19a | 6007 | if (NSString *warning = [self yieldToSelector:@selector(getWarning)]) { |
7b00c562 JF |
6008 | defer = true; |
6009 | ||
1ba930a4 | 6010 | UIAlertView *alert = [[[UIAlertView alloc] |
61b13cae | 6011 | initWithTitle:UCLocalize("SOURCE_WARNING") |
9ae52960 GP |
6012 | message:warning |
6013 | delegate:self | |
6014 | cancelButtonTitle:UCLocalize("CANCEL") | |
1ba930a4 | 6015 | otherButtonTitles:UCLocalize("ADD_ANYWAY"), nil |
b4dff19a JF |
6016 | ] autorelease]; |
6017 | ||
9ae52960 | 6018 | [alert setContext:@"warning"]; |
1ba930a4 GP |
6019 | [alert setNumberOfRows:1]; |
6020 | [alert show]; | |
b4dff19a JF |
6021 | } else |
6022 | [self complete]; | |
faf4eb4f | 6023 | } else if (error_ != nil) { |
1ba930a4 | 6024 | UIAlertView *alert = [[[UIAlertView alloc] |
61b13cae | 6025 | initWithTitle:UCLocalize("VERIFICATION_ERROR") |
9ae52960 GP |
6026 | message:[error_ localizedDescription] |
6027 | delegate:self | |
6028 | cancelButtonTitle:UCLocalize("OK") | |
1ba930a4 | 6029 | otherButtonTitles:nil |
faf4eb4f JF |
6030 | ] autorelease]; |
6031 | ||
9ae52960 | 6032 | [alert setContext:@"urlerror"]; |
1ba930a4 | 6033 | [alert show]; |
faf4eb4f | 6034 | } else { |
1ba930a4 | 6035 | UIAlertView *alert = [[[UIAlertView alloc] |
61b13cae | 6036 | initWithTitle:UCLocalize("NOT_REPOSITORY") |
9ae52960 GP |
6037 | message:UCLocalize("NOT_REPOSITORY_EX") |
6038 | delegate:self | |
6039 | cancelButtonTitle:UCLocalize("OK") | |
1ba930a4 | 6040 | otherButtonTitles:nil |
faf4eb4f JF |
6041 | ] autorelease]; |
6042 | ||
9ae52960 | 6043 | [alert setContext:@"trivial"]; |
1ba930a4 | 6044 | [alert show]; |
faf4eb4f JF |
6045 | } |
6046 | ||
7b00c562 JF |
6047 | [delegate_ setStatusBarShowsProgress:NO]; |
6048 | [delegate_ removeProgressHUD:hud_]; | |
6049 | ||
6050 | [hud_ autorelease]; | |
6051 | hud_ = nil; | |
6052 | ||
6053 | if (!defer) { | |
6054 | [href_ release]; | |
6055 | href_ = nil; | |
6056 | } | |
faf4eb4f JF |
6057 | |
6058 | if (error_ != nil) { | |
6059 | [error_ release]; | |
6060 | error_ = nil; | |
6061 | } | |
6062 | } | |
6063 | } | |
6064 | ||
6065 | - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { | |
6066 | switch ([response statusCode]) { | |
6067 | case 200: | |
3d3f4666 | 6068 | cydia_ = YES; |
faf4eb4f JF |
6069 | } |
6070 | } | |
6071 | ||
6072 | - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { | |
cb9c2100 | 6073 | lprintf("connection:\"%s\" didFailWithError:\"%s\"", [href_ UTF8String], [[error localizedDescription] UTF8String]); |
faf4eb4f JF |
6074 | if (error_ != nil) |
6075 | error_ = [error retain]; | |
6076 | [self _endConnection:connection]; | |
6077 | } | |
6078 | ||
6079 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { | |
6080 | [self _endConnection:connection]; | |
6081 | } | |
6082 | ||
9b62701b | 6083 | - (NSString *) title { return UCLocalize("SOURCES"); } |
9ae52960 | 6084 | |
faf4eb4f JF |
6085 | - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method { |
6086 | NSMutableURLRequest *request = [NSMutableURLRequest | |
6087 | requestWithURL:[NSURL URLWithString:href] | |
6088 | cachePolicy:NSURLRequestUseProtocolCachePolicy | |
daf7f6e2 | 6089 | timeoutInterval:120.0 |
faf4eb4f JF |
6090 | ]; |
6091 | ||
6092 | [request setHTTPMethod:method]; | |
6093 | ||
6932575e JF |
6094 | if (Machine_ != NULL) |
6095 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
daf7f6e2 JF |
6096 | if (UniqueID_ != nil) |
6097 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
6932575e JF |
6098 | if (Role_ != nil) |
6099 | [request setValue:Role_ forHTTPHeaderField:@"X-Role"]; | |
6100 | ||
faf4eb4f JF |
6101 | return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; |
6102 | } | |
6103 | ||
1ba930a4 GP |
6104 | - (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
6105 | NSString *context([alert context]); | |
a5dd312c JF |
6106 | |
6107 | if ([context isEqualToString:@"source"]) { | |
faf4eb4f JF |
6108 | switch (button) { |
6109 | case 1: { | |
1ba930a4 | 6110 | NSString *href = [[alert textField] text]; |
faf4eb4f JF |
6111 | |
6112 | //installer_ = [[self _requestHRef:href method:@"GET"] retain]; | |
6113 | ||
6114 | if (![href hasSuffix:@"/"]) | |
6115 | href_ = [href stringByAppendingString:@"/"]; | |
6116 | else | |
6117 | href_ = href; | |
6118 | href_ = [href_ retain]; | |
6119 | ||
3d3f4666 | 6120 | trivial_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages"] method:@"HEAD"] retain]; |
faf4eb4f JF |
6121 | trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; |
6122 | trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; | |
6123 | //trivial_bz2_ = [[self _requestHRef:[href stringByAppendingString:@"dists/Release"] method:@"HEAD"] retain]; | |
6124 | ||
3d3f4666 | 6125 | cydia_ = false; |
faf4eb4f | 6126 | |
a591888a | 6127 | // XXX: this is stupid |
7398a389 | 6128 | hud_ = [[delegate_ addProgressHUD] retain]; |
61b13cae | 6129 | [hud_ setText:UCLocalize("VERIFYING_URL")]; |
faf4eb4f JF |
6130 | } break; |
6131 | ||
1ba930a4 | 6132 | case 0: |
faf4eb4f JF |
6133 | break; |
6134 | ||
6981ccdf | 6135 | _nodefault |
faf4eb4f JF |
6136 | } |
6137 | ||
1ba930a4 | 6138 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
b7adefda | 6139 | } else if ([context isEqualToString:@"trivial"]) |
1ba930a4 | 6140 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
b7adefda | 6141 | else if ([context isEqualToString:@"urlerror"]) |
1ba930a4 | 6142 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
b4dff19a JF |
6143 | else if ([context isEqualToString:@"warning"]) { |
6144 | switch (button) { | |
6145 | case 1: | |
6146 | [self complete]; | |
6147 | break; | |
6148 | ||
1ba930a4 | 6149 | case 0: |
b4dff19a JF |
6150 | break; |
6151 | ||
6981ccdf | 6152 | _nodefault |
b4dff19a JF |
6153 | } |
6154 | ||
7b00c562 JF |
6155 | [href_ release]; |
6156 | href_ = nil; | |
6157 | ||
1ba930a4 | 6158 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
b4dff19a | 6159 | } |
faf4eb4f JF |
6160 | } |
6161 | ||
9ae52960 GP |
6162 | - (id) initWithDatabase:(Database *)database { |
6163 | if ((self = [super init]) != nil) { | |
6164 | [[self navigationItem] setTitle:UCLocalize("SOURCES")]; | |
6165 | [self updateButtonsForEditingStatus:NO animated:NO]; | |
f99f86e2 | 6166 | |
faf4eb4f JF |
6167 | database_ = database; |
6168 | sources_ = [[NSMutableArray arrayWithCapacity:16] retain]; | |
6169 | ||
9ae52960 | 6170 | list_ = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain]; |
d3bef7bc | 6171 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
9ae52960 | 6172 | [[self view] addSubview:list_]; |
d3bef7bc | 6173 | |
faf4eb4f | 6174 | [list_ setDataSource:self]; |
d4a9ec10 | 6175 | [list_ setDelegate:self]; |
faf4eb4f JF |
6176 | |
6177 | [self reloadData]; | |
6178 | } return self; | |
6179 | } | |
6180 | ||
6181 | - (void) reloadData { | |
6182 | pkgSourceList list; | |
6981ccdf JF |
6183 | if (!list.ReadMainList()) |
6184 | return; | |
faf4eb4f JF |
6185 | |
6186 | [sources_ removeAllObjects]; | |
6187 | [sources_ addObjectsFromArray:[database_ sources]]; | |
f159ecd4 | 6188 | _trace(); |
faf4eb4f | 6189 | [sources_ sortUsingSelector:@selector(compareByNameAndType:)]; |
f159ecd4 | 6190 | _trace(); |
faf4eb4f | 6191 | |
96291f72 | 6192 | int count([sources_ count]); |
d4a9ec10 GP |
6193 | offset_ = 0; |
6194 | for (int i = 0; i != count; i++) { | |
96291f72 JF |
6195 | if ([[sources_ objectAtIndex:i] record] == nil) |
6196 | break; | |
6197 | offset_++; | |
faf4eb4f JF |
6198 | } |
6199 | ||
d4a9ec10 | 6200 | [list_ setEditing:NO]; |
9ae52960 | 6201 | [self updateButtonsForEditingStatus:NO animated:NO]; |
faf4eb4f JF |
6202 | [list_ reloadData]; |
6203 | } | |
6204 | ||
9ae52960 | 6205 | - (void) addButtonClicked { |
f441e717 | 6206 | /*[book_ pushPage:[[[AddSourceController alloc] |
faf4eb4f JF |
6207 | initWithBook:book_ |
6208 | database:database_ | |
6209 | ] autorelease]];*/ | |
6210 | ||
1ba930a4 | 6211 | UIAlertView *alert = [[[UIAlertView alloc] |
61b13cae | 6212 | initWithTitle:UCLocalize("ENTER_APT_URL") |
9ae52960 GP |
6213 | message:nil |
6214 | delegate:self | |
6215 | cancelButtonTitle:UCLocalize("CANCEL") | |
6216 | otherButtonTitles:UCLocalize("ADD_SOURCE"), nil | |
faf4eb4f JF |
6217 | ] autorelease]; |
6218 | ||
9ae52960 GP |
6219 | [alert setContext:@"source"]; |
6220 | [alert setTransform:CGAffineTransformTranslate([alert transform], 0.0, 100.0)]; | |
a5dd312c | 6221 | |
1ba930a4 GP |
6222 | [alert setNumberOfRows:1]; |
6223 | [alert addTextFieldWithValue:@"http://" label:@""]; | |
faf4eb4f | 6224 | |
1ba930a4 | 6225 | UITextInputTraits *traits = [[alert textField] textInputTraits]; |
b7adefda JF |
6226 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; |
6227 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
a5dd312c | 6228 | [traits setKeyboardType:UIKeyboardTypeURL]; |
b7adefda JF |
6229 | // XXX: UIReturnKeyDone |
6230 | [traits setReturnKeyType:UIReturnKeyNext]; | |
faf4eb4f | 6231 | |
1ba930a4 | 6232 | [alert show]; |
faf4eb4f JF |
6233 | } |
6234 | ||
9ae52960 | 6235 | - (void) updateButtonsForEditingStatus:(BOOL)editing animated:(BOOL)animated { |
11f75d4f | 6236 | [[self navigationItem] setLeftBarButtonItem:(editing ? [[[UIBarButtonItem alloc] |
9ae52960 GP |
6237 | initWithTitle:UCLocalize("ADD") |
6238 | style:UIBarButtonItemStylePlain | |
6239 | target:self | |
6240 | action:@selector(addButtonClicked) | |
11f75d4f | 6241 | ] autorelease] : [[self navigationItem] backBarButtonItem]) animated:animated]; |
9ae52960 | 6242 | |
11f75d4f JF |
6243 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
6244 | initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) | |
6245 | style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
9ae52960 GP |
6246 | target:self |
6247 | action:@selector(editButtonClicked) | |
11f75d4f | 6248 | ] autorelease] animated:animated]; |
f99f86e2 | 6249 | |
11f75d4f JF |
6250 | if (IsWildcat_ && !editing) |
6251 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
ca45de84 GP |
6252 | initWithTitle:UCLocalize("SETTINGS") |
6253 | style:UIBarButtonItemStylePlain | |
6254 | target:self | |
6255 | action:@selector(settingsButtonClicked) | |
11f75d4f | 6256 | ] autorelease]]; |
ca45de84 GP |
6257 | } |
6258 | ||
6259 | - (void) settingsButtonClicked { | |
6260 | [delegate_ showSettings]; | |
faf4eb4f JF |
6261 | } |
6262 | ||
9ae52960 GP |
6263 | - (void) editButtonClicked { |
6264 | [list_ setEditing:![list_ isEditing] animated:YES]; | |
f99f86e2 | 6265 | |
9ae52960 | 6266 | [self updateButtonsForEditingStatus:[list_ isEditing] animated:YES]; |
faf4eb4f JF |
6267 | } |
6268 | ||
6269 | @end | |
6270 | /* }}} */ | |
6271 | ||
f441e717 GP |
6272 | /* Installed Controller {{{ */ |
6273 | @interface InstalledController : FilteredPackageController { | |
5e563e79 | 6274 | BOOL expert_; |
faf4eb4f JF |
6275 | } |
6276 | ||
9ae52960 | 6277 | - (id) initWithDatabase:(Database *)database; |
faf4eb4f | 6278 | |
1e4922b8 JF |
6279 | - (void) updateRoleButton; |
6280 | - (void) queueStatusDidChange; | |
6281 | ||
faf4eb4f JF |
6282 | @end |
6283 | ||
f441e717 | 6284 | @implementation InstalledController |
faf4eb4f JF |
6285 | |
6286 | - (void) dealloc { | |
faf4eb4f JF |
6287 | [super dealloc]; |
6288 | } | |
6289 | ||
9b62701b | 6290 | - (NSString *) title { return UCLocalize("INSTALLED"); } |
87c76914 | 6291 | |
9ae52960 | 6292 | - (id) initWithDatabase:(Database *)database { |
1c220bde | 6293 | if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED") filter:@selector(isInstalledAndUnfiltered:) with:[NSNumber numberWithBool:YES]]) != nil) { |
9ae52960 GP |
6294 | [self updateRoleButton]; |
6295 | [self queueStatusDidChange]; | |
faf4eb4f JF |
6296 | } return self; |
6297 | } | |
6298 | ||
9ae52960 GP |
6299 | #if !AlwaysReload |
6300 | - (void) queueButtonClicked { | |
6301 | [delegate_ queue]; | |
faf4eb4f | 6302 | } |
9ae52960 | 6303 | #endif |
faf4eb4f | 6304 | |
9ae52960 GP |
6305 | - (void) queueStatusDidChange { |
6306 | #if !AlwaysReload | |
6307 | if (IsWildcat_) { | |
11f75d4f JF |
6308 | if (Queuing_) { |
6309 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
6310 | initWithTitle:UCLocalize("QUEUE") | |
6311 | style:UIBarButtonItemStyleDone | |
6312 | target:self | |
6313 | action:@selector(queueButtonClicked) | |
6314 | ] autorelease]]; | |
6315 | } else { | |
6316 | [[self navigationItem] setLeftBarButtonItem:nil]; | |
6317 | } | |
9ae52960 GP |
6318 | } |
6319 | #endif | |
5e563e79 JF |
6320 | } |
6321 | ||
9ae52960 GP |
6322 | - (void) reloadData { |
6323 | [packages_ reloadData]; | |
faf4eb4f JF |
6324 | } |
6325 | ||
9ae52960 | 6326 | - (void) updateRoleButton { |
11f75d4f JF |
6327 | if (Role_ != nil && ![Role_ isEqualToString:@"Developer"]) |
6328 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
6329 | initWithTitle:(expert_ ? UCLocalize("EXPERT") : UCLocalize("SIMPLE")) | |
6330 | style:(expert_ ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
6331 | target:self | |
6332 | action:@selector(roleButtonClicked) | |
6333 | ] autorelease]]; | |
faf4eb4f JF |
6334 | } |
6335 | ||
9ae52960 GP |
6336 | - (void) roleButtonClicked { |
6337 | [packages_ setObject:[NSNumber numberWithBool:expert_]]; | |
6338 | [packages_ reloadData]; | |
6339 | expert_ = !expert_; | |
5e563e79 | 6340 | |
9ae52960 | 6341 | [self updateRoleButton]; |
5e563e79 JF |
6342 | } |
6343 | ||
faf4eb4f JF |
6344 | - (void) setDelegate:(id)delegate { |
6345 | [super setDelegate:delegate]; | |
6346 | [packages_ setDelegate:delegate]; | |
6347 | } | |
6348 | ||
6349 | @end | |
6350 | /* }}} */ | |
6351 | ||
f441e717 GP |
6352 | /* Home Controller {{{ */ |
6353 | @interface HomeController : CYBrowserController { | |
faf4eb4f JF |
6354 | } |
6355 | ||
6356 | @end | |
6357 | ||
f441e717 | 6358 | @implementation HomeController |
faf4eb4f | 6359 | |
017b2b71 JF |
6360 | - (void) _setMoreHeaders:(NSMutableURLRequest *)request { |
6361 | [super _setMoreHeaders:request]; | |
ba87b4de | 6362 | |
017b2b71 JF |
6363 | if (ChipID_ != nil) |
6364 | [request setValue:ChipID_ forHTTPHeaderField:@"X-Chip-ID"]; | |
5ec44e34 JF |
6365 | if (UniqueID_ != nil) |
6366 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
3074466a JF |
6367 | if (PLMN_ != nil) |
6368 | [request setValue:PLMN_ forHTTPHeaderField:@"X-Carrier-ID"]; | |
017b2b71 JF |
6369 | } |
6370 | ||
9ae52960 | 6371 | - (void) aboutButtonClicked { |
ba87b4de JF |
6372 | UIAlertView *alert([[[UIAlertView alloc] init] autorelease]); |
6373 | ||
99dc9e91 JF |
6374 | [alert setTitle:UCLocalize("ABOUT_CYDIA")]; |
6375 | [alert addButtonWithTitle:UCLocalize("CLOSE")]; | |
6376 | [alert setCancelButtonIndex:0]; | |
faf4eb4f | 6377 | |
99dc9e91 | 6378 | [alert setMessage: |
daf7f6e2 | 6379 | @"Copyright (C) 2008-2010\n" |
faf4eb4f JF |
6380 | "Jay Freeman (saurik)\n" |
6381 | "saurik@saurik.com\n" | |
991507f3 | 6382 | "http://www.saurik.com/" |
faf4eb4f JF |
6383 | ]; |
6384 | ||
99dc9e91 | 6385 | [alert show]; |
faf4eb4f JF |
6386 | } |
6387 | ||
9ae52960 GP |
6388 | - (void) viewWillAppear:(BOOL)animated { |
6389 | [super viewWillAppear:animated]; | |
2ecf1e7f | 6390 | //[[self navigationController] setNavigationBarHidden:YES animated:animated]; |
9ae52960 GP |
6391 | } |
6392 | ||
6393 | - (void) viewWillDisappear:(BOOL)animated { | |
6394 | [super viewWillDisappear:animated]; | |
2ecf1e7f | 6395 | //[[self navigationController] setNavigationBarHidden:NO animated:animated]; |
9ae52960 GP |
6396 | } |
6397 | ||
6398 | - (id) init { | |
6399 | if ((self = [super init]) != nil) { | |
ba87b4de | 6400 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] |
9ae52960 GP |
6401 | initWithTitle:UCLocalize("ABOUT") |
6402 | style:UIBarButtonItemStylePlain | |
6403 | target:self | |
6404 | action:@selector(aboutButtonClicked) | |
ba87b4de | 6405 | ] autorelease]]; |
9ae52960 | 6406 | } return self; |
faf4eb4f JF |
6407 | } |
6408 | ||
6409 | @end | |
1bb0d66c | 6410 | /* }}} */ |
f441e717 GP |
6411 | /* Manage Controller {{{ */ |
6412 | @interface ManageController : CYBrowserController { | |
faf4eb4f JF |
6413 | } |
6414 | ||
1e4922b8 | 6415 | - (void) queueStatusDidChange; |
faf4eb4f JF |
6416 | @end |
6417 | ||
f441e717 | 6418 | @implementation ManageController |
faf4eb4f | 6419 | |
9ae52960 GP |
6420 | - (id) init { |
6421 | if ((self = [super init]) != nil) { | |
6422 | [[self navigationItem] setTitle:UCLocalize("MANAGE")]; | |
f99f86e2 | 6423 | |
11f75d4f | 6424 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] |
9ae52960 GP |
6425 | initWithTitle:UCLocalize("SETTINGS") |
6426 | style:UIBarButtonItemStylePlain | |
6427 | target:self | |
6428 | action:@selector(settingsButtonClicked) | |
11f75d4f | 6429 | ] autorelease]]; |
f99f86e2 | 6430 | |
9ae52960 GP |
6431 | [self queueStatusDidChange]; |
6432 | } return self; | |
faf4eb4f JF |
6433 | } |
6434 | ||
9ae52960 | 6435 | - (void) settingsButtonClicked { |
c1edf105 | 6436 | [delegate_ showSettings]; |
faf4eb4f JF |
6437 | } |
6438 | ||
541a556a | 6439 | #if !AlwaysReload |
9ae52960 GP |
6440 | - (void) queueButtonClicked { |
6441 | [delegate_ queue]; | |
3ff1504e JF |
6442 | } |
6443 | ||
b38613d7 GP |
6444 | - (void) applyLoadingTitle { |
6445 | // No "Loading" title. | |
6446 | } | |
6447 | ||
6448 | - (void) applyRightButton { | |
6449 | // No right button. | |
3ff1504e | 6450 | } |
b38613d7 | 6451 | #endif |
3ff1504e | 6452 | |
9ae52960 GP |
6453 | - (void) queueStatusDidChange { |
6454 | #if !AlwaysReload | |
6455 | if (!IsWildcat_ && Queuing_) { | |
11f75d4f | 6456 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
9ae52960 GP |
6457 | initWithTitle:UCLocalize("QUEUE") |
6458 | style:UIBarButtonItemStyleDone | |
6459 | target:self | |
6460 | action:@selector(queueButtonClicked) | |
11f75d4f | 6461 | ] autorelease]]; |
9ae52960 GP |
6462 | } else { |
6463 | [[self navigationItem] setRightBarButtonItem:nil]; | |
6464 | } | |
541a556a | 6465 | #endif |
9ae52960 | 6466 | } |
faf4eb4f | 6467 | |
d8d9a65c | 6468 | - (bool) isLoading { |
a99d2808 JF |
6469 | return false; |
6470 | } | |
6471 | ||
faf4eb4f | 6472 | @end |
1bb0d66c | 6473 | /* }}} */ |
faf4eb4f | 6474 | |
9ae52960 GP |
6475 | /* Refresh Bar {{{ */ |
6476 | @interface RefreshBar : UINavigationBar { | |
faf4eb4f JF |
6477 | UIProgressIndicator *indicator_; |
6478 | UITextLabel *prompt_; | |
6479 | UIProgressBar *progress_; | |
87c76914 | 6480 | UINavigationButton *cancel_; |
30d83bfe JF |
6481 | } |
6482 | ||
30d83bfe | 6483 | @end |
30d83bfe | 6484 | |
9ae52960 | 6485 | @implementation RefreshBar |
5ec44e34 | 6486 | |
a591888a JF |
6487 | - (void) dealloc { |
6488 | [indicator_ release]; | |
6489 | [prompt_ release]; | |
6490 | [progress_ release]; | |
6491 | [cancel_ release]; | |
6492 | [super dealloc]; | |
6493 | } | |
6494 | ||
ba6cbb36 GP |
6495 | - (void) positionViews { |
6496 | CGRect frame = [cancel_ frame]; | |
6497 | frame.origin.x = [self frame].size.width - frame.size.width - 5; | |
6498 | frame.origin.y = ([self frame].size.height - frame.size.height) / 2; | |
6499 | [cancel_ setFrame:frame]; | |
f99f86e2 | 6500 | |
ba6cbb36 GP |
6501 | CGSize prgsize = {75, 100}; |
6502 | CGRect prgrect = {{ | |
6503 | [self frame].size.width - prgsize.width - 10, | |
6504 | ([self frame].size.height - prgsize.height) / 2 | |
6505 | } , prgsize}; | |
6506 | [progress_ setFrame:prgrect]; | |
f99f86e2 | 6507 | |
ba6cbb36 GP |
6508 | CGSize indsize([UIProgressIndicator defaultSizeForStyle:[indicator_ activityIndicatorViewStyle]]); |
6509 | unsigned indoffset = ([self frame].size.height - indsize.height) / 2; | |
6510 | CGRect indrect = {{indoffset, indoffset}, indsize}; | |
6511 | [indicator_ setFrame:indrect]; | |
f99f86e2 | 6512 | |
ba6cbb36 GP |
6513 | CGSize prmsize = {215, indsize.height + 4}; |
6514 | CGRect prmrect = {{ | |
6515 | indoffset * 2 + indsize.width, | |
6516 | unsigned([self frame].size.height - prmsize.height) / 2 - 1 | |
6517 | }, prmsize}; | |
6518 | [prompt_ setFrame:prmrect]; | |
6519 | } | |
6520 | ||
6521 | - (void)setFrame:(CGRect)frame { | |
6522 | [super setFrame:frame]; | |
f99f86e2 | 6523 | |
ba6cbb36 GP |
6524 | [self positionViews]; |
6525 | } | |
6526 | ||
9ae52960 GP |
6527 | - (id) initWithFrame:(CGRect)frame delegate:(id)delegate { |
6528 | if ((self = [super initWithFrame:frame])) { | |
6529 | [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | |
5ec44e34 | 6530 | |
9ae52960 | 6531 | [self setTintColor:[UIColor colorWithRed:0.23 green:0.23 blue:0.23 alpha:1]]; |
1e4922b8 | 6532 | [self setBarStyle:UIBarStyleBlack]; |
5ec44e34 | 6533 | |
1e4922b8 JF |
6534 | UIBarStyle barstyle([self _barStyle:NO]); |
6535 | bool ugly(barstyle == UIBarStyleDefault); | |
f159ecd4 JF |
6536 | |
6537 | UIProgressIndicatorStyle style = ugly ? | |
6538 | UIProgressIndicatorStyleMediumBrown : | |
6539 | UIProgressIndicatorStyleMediumWhite; | |
6540 | ||
ba6cbb36 | 6541 | indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectZero]; |
f159ecd4 | 6542 | [indicator_ setStyle:style]; |
ba6cbb36 | 6543 | [indicator_ startAnimation]; |
9ae52960 | 6544 | [self addSubview:indicator_]; |
f159ecd4 | 6545 | |
ba6cbb36 | 6546 | prompt_ = [[UITextLabel alloc] initWithFrame:CGRectZero]; |
f159ecd4 JF |
6547 | [prompt_ setColor:[UIColor colorWithCGColor:(ugly ? Blueish_ : Off_)]]; |
6548 | [prompt_ setBackgroundColor:[UIColor clearColor]]; | |
ba6cbb36 | 6549 | [prompt_ setFont:[UIFont systemFontOfSize:15]]; |
9ae52960 | 6550 | [self addSubview:prompt_]; |
f159ecd4 | 6551 | |
ba6cbb36 GP |
6552 | progress_ = [[UIProgressBar alloc] initWithFrame:CGRectZero]; |
6553 | [progress_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin]; | |
d3bef7bc | 6554 | [progress_ setStyle:0]; |
ba6cbb36 | 6555 | [self addSubview:progress_]; |
f99f86e2 | 6556 | |
61b13cae | 6557 | cancel_ = [[UINavigationButton alloc] initWithTitle:UCLocalize("CANCEL") style:UINavigationButtonStyleHighlighted]; |
e7a88a8c | 6558 | [cancel_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
9ae52960 | 6559 | [cancel_ addTarget:delegate action:@selector(cancelPressed) forControlEvents:UIControlEventTouchUpInside]; |
ba6cbb36 | 6560 | [cancel_ setBarStyle:barstyle]; |
f99f86e2 | 6561 | |
ba6cbb36 | 6562 | [self positionViews]; |
f159ecd4 JF |
6563 | } return self; |
6564 | } | |
6565 | ||
9ae52960 | 6566 | - (void) cancel { |
f159ecd4 JF |
6567 | [cancel_ removeFromSuperview]; |
6568 | } | |
6569 | ||
9ae52960 GP |
6570 | - (void) start { |
6571 | [prompt_ setText:UCLocalize("UPDATING_DATABASE")]; | |
6572 | [progress_ setProgress:0]; | |
6573 | [self addSubview:cancel_]; | |
6574 | } | |
6575 | ||
6576 | - (void) stop { | |
6577 | [cancel_ removeFromSuperview]; | |
6578 | } | |
6579 | ||
6580 | - (void) setPrompt:(NSString *)prompt { | |
6581 | [prompt_ setText:prompt]; | |
6582 | } | |
6583 | ||
6584 | - (void) setProgress:(float)progress { | |
6585 | [progress_ setProgress:progress]; | |
6586 | } | |
6587 | ||
6588 | @end | |
6589 | /* }}} */ | |
6590 | ||
1e4922b8 JF |
6591 | @class CYNavigationController; |
6592 | ||
9ae52960 | 6593 | /* Cydia Tab Bar Controller {{{ */ |
e7a88a8c | 6594 | @interface CYTabBarController : UITabBarController { |
a591888a | 6595 | _transient Database *database_; |
9ae52960 GP |
6596 | } |
6597 | ||
f99f86e2 | 6598 | @end |
9ae52960 GP |
6599 | |
6600 | @implementation CYTabBarController | |
6601 | ||
e7a88a8c GP |
6602 | /* XXX: some logic should probably go here related to |
6603 | freeing the view controllers on tab change */ | |
f159ecd4 | 6604 | |
9ae52960 GP |
6605 | - (void) reloadData { |
6606 | size_t count([[self viewControllers] count]); | |
6607 | for (size_t i(0); i != count; ++i) { | |
1e4922b8 | 6608 | CYNavigationController *page([[self viewControllers] objectAtIndex:(count - i - 1)]); |
9ae52960 GP |
6609 | [page reloadData]; |
6610 | } | |
6611 | } | |
6612 | ||
982de8f1 | 6613 | - (id) initWithDatabase:(Database *)database { |
9ae52960 GP |
6614 | if ((self = [super init]) != nil) { |
6615 | database_ = database; | |
9ae52960 GP |
6616 | } return self; |
6617 | } | |
6618 | ||
6619 | @end | |
6620 | /* }}} */ | |
6621 | ||
6622 | /* Cydia Navigation Controller {{{ */ | |
1e4922b8 | 6623 | @interface CYNavigationController : UINavigationController { |
9ae52960 | 6624 | _transient Database *database_; |
a591888a | 6625 | _transient id<UINavigationControllerDelegate> delegate_; |
9ae52960 GP |
6626 | } |
6627 | ||
6628 | - (id) initWithDatabase:(Database *)database; | |
6629 | - (void) reloadData; | |
6630 | ||
6631 | @end | |
6632 | ||
6633 | ||
6634 | @implementation CYNavigationController | |
6635 | ||
15b41c77 GP |
6636 | - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { |
6637 | // Inherit autorotation settings for modal parents. | |
6638 | if ([self parentViewController] && [[self parentViewController] modalViewController] == self) { | |
6639 | return [[self parentViewController] shouldAutorotateToInterfaceOrientation:orientation]; | |
6640 | } else { | |
6641 | return [super shouldAutorotateToInterfaceOrientation:orientation]; | |
6642 | } | |
6643 | } | |
6644 | ||
9ae52960 GP |
6645 | - (void) dealloc { |
6646 | [super dealloc]; | |
6647 | } | |
6648 | ||
6649 | - (void) reloadData { | |
6650 | size_t count([[self viewControllers] count]); | |
6651 | for (size_t i(0); i != count; ++i) { | |
1e4922b8 | 6652 | CYViewController *page([[self viewControllers] objectAtIndex:(count - i - 1)]); |
9ae52960 GP |
6653 | [page reloadData]; |
6654 | } | |
6655 | } | |
6656 | ||
9b62701b | 6657 | - (void) setDelegate:(id<UINavigationControllerDelegate>)delegate { |
9ae52960 GP |
6658 | delegate_ = delegate; |
6659 | } | |
6660 | ||
6661 | - (id) initWithDatabase:(Database *)database { | |
6662 | if ((self = [super init]) != nil) { | |
6663 | database_ = database; | |
6664 | } return self; | |
6665 | } | |
6666 | ||
f159ecd4 JF |
6667 | @end |
6668 | /* }}} */ | |
6669 | /* Cydia:// Protocol {{{ */ | |
6670 | @interface CydiaURLProtocol : NSURLProtocol { | |
6671 | } | |
6672 | ||
6673 | @end | |
6674 | ||
6675 | @implementation CydiaURLProtocol | |
6676 | ||
6677 | + (BOOL) canInitWithRequest:(NSURLRequest *)request { | |
6678 | NSURL *url([request URL]); | |
6679 | if (url == nil) | |
6680 | return NO; | |
6681 | NSString *scheme([[url scheme] lowercaseString]); | |
6682 | if (scheme == nil || ![scheme isEqualToString:@"cydia"]) | |
6683 | return NO; | |
6684 | return YES; | |
6685 | } | |
6686 | ||
6687 | + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { | |
6688 | return request; | |
6689 | } | |
6690 | ||
a99d2808 JF |
6691 | - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { |
6692 | id<NSURLProtocolClient> client([self client]); | |
6e673d99 JF |
6693 | if (icon == nil) |
6694 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; | |
6695 | else { | |
6696 | NSData *data(UIImagePNGRepresentation(icon)); | |
a99d2808 | 6697 | |
6e673d99 JF |
6698 | NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); |
6699 | [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; | |
6700 | [client URLProtocol:self didLoadData:data]; | |
6701 | [client URLProtocolDidFinishLoading:self]; | |
6702 | } | |
a99d2808 JF |
6703 | } |
6704 | ||
f159ecd4 JF |
6705 | - (void) startLoading { |
6706 | id<NSURLProtocolClient> client([self client]); | |
6707 | NSURLRequest *request([self request]); | |
6708 | ||
6709 | NSURL *url([request URL]); | |
6710 | NSString *href([url absoluteString]); | |
6711 | ||
6712 | NSString *path([href substringFromIndex:8]); | |
6713 | NSRange slash([path rangeOfString:@"/"]); | |
6714 | ||
6715 | NSString *command; | |
6716 | if (slash.location == NSNotFound) { | |
6717 | command = path; | |
6718 | path = nil; | |
6719 | } else { | |
6720 | command = [path substringToIndex:slash.location]; | |
6721 | path = [path substringFromIndex:(slash.location + 1)]; | |
6722 | } | |
6723 | ||
6724 | Database *database([Database sharedInstance]); | |
6725 | ||
6726 | if ([command isEqualToString:@"package-icon"]) { | |
6727 | if (path == nil) | |
6728 | goto fail; | |
6b4b3bee | 6729 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
f159ecd4 JF |
6730 | Package *package([database packageWithName:path]); |
6731 | if (package == nil) | |
6732 | goto fail; | |
6b4b3bee | 6733 | UIImage *icon([package icon]); |
a99d2808 | 6734 | [self _returnPNGWithImage:icon forRequest:request]; |
18159e09 JF |
6735 | } else if ([command isEqualToString:@"source-icon"]) { |
6736 | if (path == nil) | |
6737 | goto fail; | |
6738 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6739 | NSString *source(Simplify(path)); | |
18159e09 JF |
6740 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sources/%@.png", App_, source]]); |
6741 | if (icon == nil) | |
6742 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
a99d2808 JF |
6743 | [self _returnPNGWithImage:icon forRequest:request]; |
6744 | } else if ([command isEqualToString:@"uikit-image"]) { | |
6745 | if (path == nil) | |
6746 | goto fail; | |
6747 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6748 | UIImage *icon(_UIImageWithName(path)); | |
6749 | [self _returnPNGWithImage:icon forRequest:request]; | |
6b4b3bee JF |
6750 | } else if ([command isEqualToString:@"section-icon"]) { |
6751 | if (path == nil) | |
6752 | goto fail; | |
6753 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6754 | NSString *section(Simplify(path)); | |
6b4b3bee JF |
6755 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]]); |
6756 | if (icon == nil) | |
6757 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
a99d2808 | 6758 | [self _returnPNGWithImage:icon forRequest:request]; |
f159ecd4 JF |
6759 | } else fail: { |
6760 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; | |
6761 | } | |
6762 | } | |
6763 | ||
6764 | - (void) stopLoading { | |
6765 | } | |
6766 | ||
6767 | @end | |
6768 | /* }}} */ | |
6769 | ||
f441e717 | 6770 | /* Sections Controller {{{ */ |
1e4922b8 JF |
6771 | @interface SectionsController : CYViewController < |
6772 | UITableViewDataSource, | |
6773 | UITableViewDelegate | |
6774 | > { | |
f159ecd4 JF |
6775 | _transient Database *database_; |
6776 | NSMutableArray *sections_; | |
6777 | NSMutableArray *filtered_; | |
d4a9ec10 | 6778 | UITableView *list_; |
f159ecd4 JF |
6779 | UIView *accessory_; |
6780 | BOOL editing_; | |
6781 | } | |
6782 | ||
9ae52960 | 6783 | - (id) initWithDatabase:(Database *)database; |
f159ecd4 JF |
6784 | - (void) reloadData; |
6785 | - (void) resetView; | |
6786 | ||
1e4922b8 JF |
6787 | - (void) editButtonClicked; |
6788 | ||
f159ecd4 JF |
6789 | @end |
6790 | ||
f441e717 | 6791 | @implementation SectionsController |
f159ecd4 JF |
6792 | |
6793 | - (void) dealloc { | |
6794 | [list_ setDataSource:nil]; | |
6795 | [list_ setDelegate:nil]; | |
6796 | ||
6797 | [sections_ release]; | |
6798 | [filtered_ release]; | |
f159ecd4 JF |
6799 | [list_ release]; |
6800 | [accessory_ release]; | |
6801 | [super dealloc]; | |
6802 | } | |
6803 | ||
9ae52960 GP |
6804 | - (void) viewDidAppear:(BOOL)animated { |
6805 | [super viewDidAppear:animated]; | |
6806 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
6807 | } | |
6808 | ||
d4a9ec10 GP |
6809 | - (Section *) sectionAtIndexPath:(NSIndexPath *)indexPath { |
6810 | Section *section = (editing_ ? [sections_ objectAtIndex:[indexPath row]] : ([indexPath row] == 0 ? nil : [filtered_ objectAtIndex:([indexPath row] - 1)])); | |
6811 | return section; | |
f159ecd4 JF |
6812 | } |
6813 | ||
9b62701b | 6814 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
d4a9ec10 | 6815 | return editing_ ? [sections_ count] : [filtered_ count] + 1; |
f159ecd4 JF |
6816 | } |
6817 | ||
2064d251 GP |
6818 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
6819 | return 45.0f; | |
6820 | }*/ | |
f159ecd4 | 6821 | |
d4a9ec10 | 6822 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
9ae52960 GP |
6823 | static NSString *reuseIdentifier = @"SectionCell"; |
6824 | ||
6825 | SectionCell *cell = (SectionCell *) [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; | |
96291f72 JF |
6826 | if (cell == nil) |
6827 | cell = [[[SectionCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
6828 | ||
d4a9ec10 | 6829 | [cell setSection:[self sectionAtIndexPath:indexPath] editing:editing_]; |
f159ecd4 | 6830 | |
d4a9ec10 | 6831 | return cell; |
f159ecd4 JF |
6832 | } |
6833 | ||
d4a9ec10 | 6834 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
864db79b JF |
6835 | if (editing_) |
6836 | return; | |
d1e1aaab | 6837 | |
864db79b | 6838 | Section *section = [self sectionAtIndexPath:indexPath]; |
d4a9ec10 | 6839 | NSString *name = [section name]; |
f159ecd4 JF |
6840 | NSString *title; |
6841 | ||
d4a9ec10 | 6842 | if ([indexPath row] == 0) { |
f159ecd4 JF |
6843 | section = nil; |
6844 | name = nil; | |
61b13cae | 6845 | title = UCLocalize("ALL_PACKAGES"); |
f159ecd4 | 6846 | } else { |
ca06bb0e JF |
6847 | if (name != nil) { |
6848 | name = [NSString stringWithString:name]; | |
6932575e | 6849 | title = [[NSBundle mainBundle] localizedStringForKey:Simplify(name) value:nil table:@"Sections"]; |
ca06bb0e | 6850 | } else { |
f159ecd4 | 6851 | name = @""; |
61b13cae | 6852 | title = UCLocalize("NO_SECTION"); |
f159ecd4 JF |
6853 | } |
6854 | } | |
6855 | ||
f441e717 | 6856 | FilteredPackageController *table = [[[FilteredPackageController alloc] |
9ae52960 | 6857 | initWithDatabase:database_ |
f159ecd4 | 6858 | title:title |
6981ccdf | 6859 | filter:@selector(isVisibleInSection:) |
f159ecd4 JF |
6860 | with:name |
6861 | ] autorelease]; | |
6862 | ||
6863 | [table setDelegate:delegate_]; | |
6864 | ||
9ae52960 | 6865 | [[self navigationController] pushViewController:table animated:YES]; |
f159ecd4 JF |
6866 | } |
6867 | ||
9b62701b | 6868 | - (NSString *) title { return UCLocalize("SECTIONS"); } |
9ae52960 GP |
6869 | |
6870 | - (id) initWithDatabase:(Database *)database { | |
6871 | if ((self = [super init]) != nil) { | |
f159ecd4 JF |
6872 | database_ = database; |
6873 | ||
9ae52960 GP |
6874 | [[self navigationItem] setTitle:UCLocalize("SECTIONS")]; |
6875 | ||
f159ecd4 JF |
6876 | sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; |
6877 | filtered_ = [[NSMutableArray arrayWithCapacity:16] retain]; | |
6878 | ||
9ae52960 | 6879 | list_ = [[UITableView alloc] initWithFrame:[[self view] bounds]]; |
d3bef7bc | 6880 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
2064d251 | 6881 | [list_ setRowHeight:45.0f]; |
9ae52960 | 6882 | [[self view] addSubview:list_]; |
f159ecd4 | 6883 | |
d4a9ec10 | 6884 | [list_ setDataSource:self]; |
9ae52960 | 6885 | [list_ setDelegate:self]; |
f159ecd4 JF |
6886 | |
6887 | [self reloadData]; | |
f159ecd4 JF |
6888 | } return self; |
6889 | } | |
6890 | ||
6891 | - (void) reloadData { | |
6892 | NSArray *packages = [database_ packages]; | |
6893 | ||
6894 | [sections_ removeAllObjects]; | |
6895 | [filtered_ removeAllObjects]; | |
6896 | ||
6932575e | 6897 | NSMutableDictionary *sections([NSMutableDictionary dictionaryWithCapacity:32]); |
f159ecd4 JF |
6898 | |
6899 | _trace(); | |
9fdd37d0 | 6900 | for (Package *package in packages) { |
f159ecd4 | 6901 | NSString *name([package section]); |
6932575e | 6902 | NSString *key(name == nil ? @"" : name); |
f159ecd4 | 6903 | |
6932575e JF |
6904 | Section *section; |
6905 | ||
6906 | _profile(SectionsView$reloadData$Section) | |
6907 | section = [sections objectForKey:key]; | |
f159ecd4 | 6908 | if (section == nil) { |
6932575e | 6909 | _profile(SectionsView$reloadData$Section$Allocate) |
9ee296df | 6910 | section = [[[Section alloc] initWithName:name localize:YES] autorelease]; |
6932575e JF |
6911 | [sections setObject:section forKey:key]; |
6912 | _end | |
f159ecd4 | 6913 | } |
6932575e JF |
6914 | _end |
6915 | ||
6916 | [section addToCount]; | |
fa7bb92f | 6917 | |
6932575e | 6918 | _profile(SectionsView$reloadData$Filter) |
6981ccdf | 6919 | if (![package valid] || ![package visible]) |
6932575e JF |
6920 | continue; |
6921 | _end | |
6922 | ||
6923 | [section addToRow]; | |
686e302f | 6924 | } |
f159ecd4 | 6925 | _trace(); |
a75f53e7 | 6926 | |
fa7bb92f | 6927 | [sections_ addObjectsFromArray:[sections allValues]]; |
fa7bb92f | 6928 | |
43b742af | 6929 | [sections_ sortUsingSelector:@selector(compareByLocalized:)]; |
a75f53e7 | 6930 | |
6932575e JF |
6931 | for (Section *section in sections_) { |
6932 | size_t count([section row]); | |
43b742af | 6933 | if (count == 0) |
6932575e | 6934 | continue; |
a75f53e7 | 6935 | |
9ee296df | 6936 | section = [[[Section alloc] initWithName:[section name] localized:[section localized]] autorelease]; |
6932575e JF |
6937 | [section setCount:count]; |
6938 | [filtered_ addObject:section]; | |
686e302f | 6939 | } |
4941f41d | 6940 | |
11f75d4f JF |
6941 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
6942 | initWithTitle:([sections_ count] == 0 ? nil : UCLocalize("EDIT")) | |
9ae52960 GP |
6943 | style:UIBarButtonItemStylePlain |
6944 | target:self | |
6945 | action:@selector(editButtonClicked) | |
11f75d4f | 6946 | ] autorelease] animated:([[self navigationItem] rightBarButtonItem] != nil)]; |
9ae52960 | 6947 | |
686e302f | 6948 | [list_ reloadData]; |
f159ecd4 | 6949 | _trace(); |
e057ec05 | 6950 | } |
4941f41d | 6951 | |
fa7bb92f JF |
6952 | - (void) resetView { |
6953 | if (editing_) | |
9ae52960 | 6954 | [self editButtonClicked]; |
e057ec05 | 6955 | } |
4941f41d | 6956 | |
9ae52960 | 6957 | - (void) editButtonClicked { |
fa7bb92f JF |
6958 | if ((editing_ = !editing_)) |
6959 | [list_ reloadData]; | |
f159ecd4 | 6960 | else |
fa7bb92f | 6961 | [delegate_ updateData]; |
f99f86e2 | 6962 | |
9ae52960 GP |
6963 | [[self navigationItem] setTitle:editing_ ? UCLocalize("SECTION_VISIBILITY") : UCLocalize("SECTIONS")]; |
6964 | [[[self navigationItem] rightBarButtonItem] setTitle:[sections_ count] == 0 ? nil : editing_ ? UCLocalize("DONE") : UCLocalize("EDIT")]; | |
6965 | [[[self navigationItem] rightBarButtonItem] setStyle:editing_ ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain]; | |
87c76914 JF |
6966 | } |
6967 | ||
9e07091a JF |
6968 | - (UIView *) accessoryView { |
6969 | return accessory_; | |
6970 | } | |
6971 | ||
a75f53e7 | 6972 | @end |
4941f41d | 6973 | /* }}} */ |
f441e717 | 6974 | /* Changes Controller {{{ */ |
1e4922b8 JF |
6975 | @interface ChangesController : CYViewController < |
6976 | UITableViewDataSource, | |
6977 | UITableViewDelegate | |
6978 | > { | |
e057ec05 | 6979 | _transient Database *database_; |
2148c5ca | 6980 | CFMutableArrayRef packages_; |
a75f53e7 | 6981 | NSMutableArray *sections_; |
5d8f1006 | 6982 | UITableView *list_; |
e057ec05 | 6983 | unsigned upgrades_; |
77a4e323 | 6984 | BOOL hasSentFirstLoad_; |
a75f53e7 JF |
6985 | } |
6986 | ||
9ae52960 | 6987 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
e057ec05 | 6988 | - (void) reloadData; |
a75f53e7 | 6989 | |
a75f53e7 JF |
6990 | @end |
6991 | ||
f441e717 | 6992 | @implementation ChangesController |
686e302f JF |
6993 | |
6994 | - (void) dealloc { | |
5d8f1006 | 6995 | [list_ setDelegate:nil]; |
e057ec05 JF |
6996 | [list_ setDataSource:nil]; |
6997 | ||
2148c5ca JF |
6998 | CFRelease(packages_); |
6999 | ||
686e302f | 7000 | [sections_ release]; |
e057ec05 | 7001 | [list_ release]; |
686e302f JF |
7002 | [super dealloc]; |
7003 | } | |
a75f53e7 | 7004 | |
9ae52960 GP |
7005 | - (void) viewDidAppear:(BOOL)animated { |
7006 | [super viewDidAppear:animated]; | |
77a4e323 RP |
7007 | if (!hasSentFirstLoad_) { |
7008 | hasSentFirstLoad_ = YES; | |
7009 | [self performSelector:@selector(reloadData) withObject:nil afterDelay:0.0]; | |
7010 | } else { | |
7011 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7012 | } | |
9ae52960 GP |
7013 | } |
7014 | ||
5d8f1006 JF |
7015 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
7016 | NSInteger count([sections_ count]); | |
7017 | return count == 0 ? 1 : count; | |
a75f53e7 JF |
7018 | } |
7019 | ||
5d8f1006 JF |
7020 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
7021 | if ([sections_ count] == 0) | |
7022 | return nil; | |
a75f53e7 JF |
7023 | return [[sections_ objectAtIndex:section] name]; |
7024 | } | |
7025 | ||
5d8f1006 JF |
7026 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
7027 | if ([sections_ count] == 0) | |
7028 | return 0; | |
7029 | return [[sections_ objectAtIndex:section] count]; | |
a75f53e7 JF |
7030 | } |
7031 | ||
2148c5ca JF |
7032 | - (Package *) packageAtIndex:(NSUInteger)index { |
7033 | return (Package *) CFArrayGetValueAtIndex(packages_, index); | |
7034 | } | |
7035 | ||
5d8f1006 JF |
7036 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
7037 | Section *section([sections_ objectAtIndex:[path section]]); | |
7038 | NSInteger row([path row]); | |
2148c5ca | 7039 | return [self packageAtIndex:([section row] + row)]; |
a75f53e7 JF |
7040 | } |
7041 | ||
5d8f1006 | 7042 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
1e4922b8 | 7043 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
5d8f1006 JF |
7044 | if (cell == nil) |
7045 | cell = [[[PackageCell alloc] init] autorelease]; | |
7046 | [cell setPackage:[self packageAtIndexPath:path]]; | |
7047 | return cell; | |
a75f53e7 JF |
7048 | } |
7049 | ||
2064d251 | 7050 | /*- (CGFloat) tableView:(UITableView *)table heightForRowAtIndexPath:(NSIndexPath *)path { |
5d8f1006 | 7051 | return [PackageCell heightForPackage:[self packageAtIndexPath:path]]; |
2064d251 | 7052 | }*/ |
a75f53e7 | 7053 | |
5d8f1006 JF |
7054 | - (NSIndexPath *) tableView:(UITableView *)table willSelectRowAtIndexPath:(NSIndexPath *)path { |
7055 | Package *package([self packageAtIndexPath:path]); | |
f441e717 | 7056 | PackageController *view([delegate_ packageController]); |
e057ec05 JF |
7057 | [view setDelegate:delegate_]; |
7058 | [view setPackage:package]; | |
9ae52960 | 7059 | [[self navigationController] pushViewController:view animated:YES]; |
5d8f1006 | 7060 | return path; |
a75f53e7 JF |
7061 | } |
7062 | ||
9ae52960 | 7063 | - (void) refreshButtonClicked { |
1e4922b8 | 7064 | [delegate_ beginUpdate]; |
24966c5d | 7065 | [[self navigationItem] setLeftBarButtonItem:nil animated:YES]; |
b7eb9e84 JF |
7066 | } |
7067 | ||
9ae52960 | 7068 | - (void) upgradeButtonClicked { |
e057ec05 | 7069 | [delegate_ distUpgrade]; |
a75f53e7 JF |
7070 | } |
7071 | ||
9b62701b | 7072 | - (NSString *) title { return UCLocalize("CHANGES"); } |
9ae52960 GP |
7073 | |
7074 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { | |
7075 | if ((self = [super init]) != nil) { | |
e057ec05 | 7076 | database_ = database; |
9ae52960 | 7077 | [[self navigationItem] setTitle:UCLocalize("CHANGES")]; |
a75f53e7 | 7078 | |
2148c5ca JF |
7079 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL); |
7080 | ||
686e302f | 7081 | sections_ = [[NSMutableArray arrayWithCapacity:16] retain]; |
a75f53e7 | 7082 | |
9ae52960 | 7083 | list_ = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain]; |
d3bef7bc | 7084 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
2064d251 | 7085 | [list_ setRowHeight:73.0f]; |
9ae52960 | 7086 | [[self view] addSubview:list_]; |
e057ec05 | 7087 | |
686e302f | 7088 | [list_ setDataSource:self]; |
5d8f1006 | 7089 | [list_ setDelegate:self]; |
a75f53e7 | 7090 | |
daf7f6e2 | 7091 | delegate_ = delegate; |
a75f53e7 JF |
7092 | } return self; |
7093 | } | |
7094 | ||
daf7f6e2 | 7095 | - (void) _reloadPackages:(NSArray *)packages { |
f159ecd4 | 7096 | _trace(); |
9fdd37d0 | 7097 | for (Package *package in packages) |
1c220bde | 7098 | if ([package upgradableAndEssential:YES] || [package visible]) |
2148c5ca | 7099 | CFArrayAppendValue(packages_, package); |
4941f41d | 7100 | |
f159ecd4 | 7101 | _trace(); |
2148c5ca | 7102 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<SKRadixFunction>(&PackageChangesRadix) withContext:NULL]; |
f159ecd4 | 7103 | _trace(); |
daf7f6e2 JF |
7104 | } |
7105 | ||
7106 | - (void) reloadData { | |
7107 | NSArray *packages = [database_ packages]; | |
7108 | ||
2148c5ca JF |
7109 | CFArrayRemoveAllValues(packages_); |
7110 | ||
daf7f6e2 JF |
7111 | [sections_ removeAllObjects]; |
7112 | ||
939f8030 | 7113 | #if 1 |
daf7f6e2 | 7114 | UIProgressHUD *hud([delegate_ addProgressHUD]); |
939f8030 | 7115 | [hud setText:UCLocalize("LOADING")]; |
12e0ef8f | 7116 | //NSLog(@"HUD:%@::%@", delegate_, hud); |
daf7f6e2 JF |
7117 | [self yieldToSelector:@selector(_reloadPackages:) withObject:packages]; |
7118 | [delegate_ removeProgressHUD:hud]; | |
dbae8548 JF |
7119 | #else |
7120 | [self _reloadPackages:packages]; | |
7121 | #endif | |
a75f53e7 | 7122 | |
61b13cae | 7123 | Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; |
1f9a3349 | 7124 | Section *ignored = nil; |
a75f53e7 | 7125 | Section *section = nil; |
8032d797 | 7126 | time_t last = 0; |
686e302f | 7127 | |
e057ec05 JF |
7128 | upgrades_ = 0; |
7129 | bool unseens = false; | |
7130 | ||
43b742af | 7131 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); |
e057ec05 | 7132 | |
2148c5ca JF |
7133 | for (size_t offset = 0, count = CFArrayGetCount(packages_); offset != count; ++offset) { |
7134 | Package *package = [self packageAtIndex:offset]; | |
a75f53e7 | 7135 | |
43b742af | 7136 | BOOL uae = [package upgradableAndEssential:YES]; |
6932575e JF |
7137 | |
7138 | if (!uae) { | |
e057ec05 | 7139 | unseens = true; |
8032d797 | 7140 | time_t seen([package seen]); |
a75f53e7 | 7141 | |
8032d797 | 7142 | if (section == nil || last != seen) { |
f159ecd4 | 7143 | last = seen; |
138ae18d | 7144 | |
3ff1504e | 7145 | NSString *name; |
8032d797 JF |
7146 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]); |
7147 | [name autorelease]; | |
3ff1504e | 7148 | |
f441e717 | 7149 | _profile(ChangesController$reloadData$Allocate) |
61b13cae | 7150 | name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; |
9ee296df | 7151 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; |
6932575e JF |
7152 | [sections_ addObject:section]; |
7153 | _end | |
686e302f JF |
7154 | } |
7155 | ||
e057ec05 | 7156 | [section addToCount]; |
1f9a3349 DH |
7157 | } else if ([package ignored]) { |
7158 | if (ignored == nil) { | |
7159 | ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease]; | |
7160 | } | |
f159ecd4 | 7161 | [ignored addToCount]; |
1f9a3349 | 7162 | } else { |
f159ecd4 JF |
7163 | ++upgrades_; |
7164 | [upgradable addToCount]; | |
686e302f | 7165 | } |
a75f53e7 | 7166 | } |
f159ecd4 | 7167 | _trace(); |
a75f53e7 | 7168 | |
e057ec05 | 7169 | CFRelease(formatter); |
686e302f | 7170 | |
e057ec05 JF |
7171 | if (unseens) { |
7172 | Section *last = [sections_ lastObject]; | |
7173 | size_t count = [last count]; | |
2148c5ca | 7174 | CFArrayReplaceValues(packages_, CFRangeMake(CFArrayGetCount(packages_) - count, count), NULL, 0); |
e057ec05 JF |
7175 | [sections_ removeLastObject]; |
7176 | } | |
a75f53e7 | 7177 | |
f159ecd4 JF |
7178 | if ([ignored count] != 0) |
7179 | [sections_ insertObject:ignored atIndex:0]; | |
e057ec05 JF |
7180 | if (upgrades_ != 0) |
7181 | [sections_ insertObject:upgradable atIndex:0]; | |
d12c6e70 | 7182 | |
e057ec05 | 7183 | [list_ reloadData]; |
2d28b35a | 7184 | |
11f75d4f JF |
7185 | if (upgrades_ > 0) |
7186 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
7187 | initWithTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("UPGRADE"), [NSString stringWithFormat:@"%u", upgrades_]] | |
7188 | style:UIBarButtonItemStylePlain | |
7189 | target:self | |
7190 | action:@selector(upgradeButtonClicked) | |
7191 | ] autorelease]]; | |
9ae52960 | 7192 | |
11f75d4f JF |
7193 | if (![delegate_ updating]) |
7194 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7195 | initWithTitle:UCLocalize("REFRESH") | |
7196 | style:UIBarButtonItemStylePlain | |
7197 | target:self | |
7198 | action:@selector(refreshButtonClicked) | |
7199 | ] autorelease]]; | |
b6ffa083 JF |
7200 | } |
7201 | ||
2d28b35a JF |
7202 | @end |
7203 | /* }}} */ | |
f441e717 | 7204 | /* Search Controller {{{ */ |
1e4922b8 JF |
7205 | @interface SearchController : FilteredPackageController < |
7206 | UISearchBarDelegate | |
7207 | > { | |
7208 | UISearchBar *search_; | |
e057ec05 | 7209 | } |
686e302f | 7210 | |
9ae52960 | 7211 | - (id) initWithDatabase:(Database *)database; |
e057ec05 | 7212 | - (void) reloadData; |
686e302f | 7213 | |
2d28b35a JF |
7214 | @end |
7215 | ||
f441e717 | 7216 | @implementation SearchController |
2d28b35a | 7217 | |
686e302f | 7218 | - (void) dealloc { |
9ae52960 | 7219 | [search_ release]; |
e057ec05 | 7220 | [super dealloc]; |
686e302f JF |
7221 | } |
7222 | ||
9b62701b | 7223 | - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { |
9ae52960 GP |
7224 | [packages_ setObject:[search_ text] forFilter:@selector(isUnfilteredAndSearchedForBy:)]; |
7225 | [search_ resignFirstResponder]; | |
5ec44e34 | 7226 | [self reloadData]; |
e057ec05 | 7227 | } |
686e302f | 7228 | |
9b62701b | 7229 | - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { |
9ae52960 | 7230 | [packages_ setObject:text forFilter:@selector(isUnfilteredAndSelectedForBy:)]; |
5ec44e34 | 7231 | [self reloadData]; |
e057ec05 | 7232 | } |
686e302f | 7233 | |
9b62701b | 7234 | - (NSString *) title { return nil; } |
59efd93a | 7235 | |
9ae52960 | 7236 | - (id) initWithDatabase:(Database *)database { |
2c4fc6f1 RP |
7237 | return [super initWithDatabase:database title:UCLocalize("SEARCH") filter:@selector(isUnfilteredAndSearchedForBy:) with:nil]; |
7238 | } | |
7239 | ||
7240 | - (void)viewDidAppear:(BOOL)animated { | |
f99f86e2 JF |
7241 | [super viewDidAppear:animated]; |
7242 | if (!search_) { | |
1e4922b8 | 7243 | search_ = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; |
2c4fc6f1 | 7244 | [search_ layoutSubviews]; |
9ae52960 | 7245 | [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; |
2c4fc6f1 RP |
7246 | UITextField *textField = [search_ searchField]; |
7247 | [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
9ae52960 | 7248 | [search_ setDelegate:self]; |
2c4fc6f1 RP |
7249 | [textField setEnablesReturnKeyAutomatically:NO]; |
7250 | [[self navigationItem] setTitleView:textField]; | |
7251 | } | |
2d28b35a JF |
7252 | } |
7253 | ||
f159ecd4 JF |
7254 | - (void) _reloadData { |
7255 | } | |
686e302f | 7256 | |
f159ecd4 | 7257 | - (void) reloadData { |
f441e717 | 7258 | _profile(SearchController$reloadData) |
9ae52960 | 7259 | [packages_ reloadData]; |
3bddda52 JF |
7260 | _end |
7261 | PrintTimes(); | |
9ae52960 | 7262 | [packages_ resetCursor]; |
f159ecd4 | 7263 | } |
b6ffa083 | 7264 | |
583b5809 RP |
7265 | - (void) didSelectPackage:(Package *)package { |
7266 | [search_ resignFirstResponder]; | |
7267 | [super didSelectPackage:package]; | |
7268 | } | |
7269 | ||
f159ecd4 JF |
7270 | @end |
7271 | /* }}} */ | |
f441e717 | 7272 | /* Settings Controller {{{ */ |
1e4922b8 JF |
7273 | @interface SettingsController : CYViewController < |
7274 | UITableViewDataSource, | |
7275 | UITableViewDelegate | |
7276 | > { | |
f159ecd4 JF |
7277 | _transient Database *database_; |
7278 | NSString *name_; | |
7279 | Package *package_; | |
31a8e05a | 7280 | UITableView *table_; |
18884e36 JF |
7281 | UISwitch *subscribedSwitch_; |
7282 | UISwitch *ignoredSwitch_; | |
31a8e05a GP |
7283 | UITableViewCell *subscribedCell_; |
7284 | UITableViewCell *ignoredCell_; | |
f159ecd4 | 7285 | } |
686e302f | 7286 | |
9ae52960 | 7287 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
686e302f | 7288 | |
f159ecd4 | 7289 | @end |
686e302f | 7290 | |
f441e717 | 7291 | @implementation SettingsController |
686e302f | 7292 | |
f159ecd4 | 7293 | - (void) dealloc { |
f159ecd4 JF |
7294 | [name_ release]; |
7295 | if (package_ != nil) | |
7296 | [package_ release]; | |
7297 | [table_ release]; | |
7298 | [subscribedSwitch_ release]; | |
7299 | [ignoredSwitch_ release]; | |
7300 | [subscribedCell_ release]; | |
7301 | [ignoredCell_ release]; | |
f99f86e2 | 7302 | |
f159ecd4 JF |
7303 | [super dealloc]; |
7304 | } | |
686e302f | 7305 | |
31a8e05a | 7306 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
f159ecd4 JF |
7307 | if (package_ == nil) |
7308 | return 0; | |
686e302f | 7309 | |
31a8e05a | 7310 | return 1; |
2d28b35a JF |
7311 | } |
7312 | ||
31a8e05a | 7313 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
f159ecd4 JF |
7314 | if (package_ == nil) |
7315 | return 0; | |
a75f53e7 | 7316 | |
7a8635c9 | 7317 | return 2; |
31a8e05a | 7318 | } |
87c76914 | 7319 | |
31a8e05a GP |
7320 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
7321 | return UCLocalize("SHOW_ALL_CHANGES_EX"); | |
e057ec05 | 7322 | } |
686e302f | 7323 | |
7a8635c9 JF |
7324 | - (void) onSubscribed:(id)control { |
7325 | bool value([control isOn]); | |
f159ecd4 JF |
7326 | if (package_ == nil) |
7327 | return; | |
7a8635c9 | 7328 | if ([package_ setSubscribed:value]) |
f159ecd4 | 7329 | [delegate_ updateData]; |
e057ec05 | 7330 | } |
686e302f | 7331 | |
31a8e05a | 7332 | - (void) onIgnored:(id)control { |
1f9a3349 | 7333 | // TODO: set Held state - possibly call out to dpkg, etc. |
b6ffa083 JF |
7334 | } |
7335 | ||
31a8e05a | 7336 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
f159ecd4 JF |
7337 | if (package_ == nil) |
7338 | return nil; | |
dbe0f181 | 7339 | |
31a8e05a GP |
7340 | switch ([indexPath row]) { |
7341 | case 0: return subscribedCell_; | |
7342 | case 1: return ignoredCell_; | |
f99f86e2 | 7343 | |
6981ccdf | 7344 | _nodefault |
f159ecd4 | 7345 | } |
dbe0f181 | 7346 | |
f159ecd4 | 7347 | return nil; |
dbe0f181 JF |
7348 | } |
7349 | ||
9b62701b | 7350 | - (NSString *) title { return UCLocalize("SETTINGS"); } |
9ae52960 GP |
7351 | |
7352 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { | |
7353 | if ((self = [super init])) { | |
f159ecd4 JF |
7354 | database_ = database; |
7355 | name_ = [package retain]; | |
dbe0f181 | 7356 | |
9ae52960 GP |
7357 | [[self navigationItem] setTitle:UCLocalize("SETTINGS")]; |
7358 | ||
31a8e05a GP |
7359 | table_ = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped]; |
7360 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
9ae52960 | 7361 | [[self view] addSubview:table_]; |
dbe0f181 | 7362 | |
18884e36 | 7363 | subscribedSwitch_ = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]; |
31a8e05a GP |
7364 | [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7365 | [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:UIControlEventValueChanged]; | |
dbe0f181 | 7366 | |
18884e36 | 7367 | ignoredSwitch_ = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]; |
31a8e05a GP |
7368 | [ignoredSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7369 | [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:UIControlEventValueChanged]; | |
dbe0f181 | 7370 | |
31a8e05a GP |
7371 | subscribedCell_ = [[UITableViewCell alloc] init]; |
7372 | [subscribedCell_ setText:UCLocalize("SHOW_ALL_CHANGES")]; | |
7373 | [subscribedCell_ setAccessoryView:subscribedSwitch_]; | |
ced7fc60 | 7374 | [subscribedCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; |
dbe0f181 | 7375 | |
31a8e05a GP |
7376 | ignoredCell_ = [[UITableViewCell alloc] init]; |
7377 | [ignoredCell_ setText:UCLocalize("IGNORE_UPGRADES")]; | |
7378 | [ignoredCell_ setAccessoryView:ignoredSwitch_]; | |
ced7fc60 | 7379 | [ignoredCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; |
dbe0f181 | 7380 | |
f159ecd4 | 7381 | [table_ setDataSource:self]; |
31a8e05a | 7382 | [table_ setDelegate:self]; |
f159ecd4 JF |
7383 | [self reloadData]; |
7384 | } return self; | |
7385 | } | |
dbe0f181 | 7386 | |
f159ecd4 JF |
7387 | - (void) reloadData { |
7388 | if (package_ != nil) | |
7389 | [package_ autorelease]; | |
7390 | package_ = [database_ packageWithName:name_]; | |
7391 | if (package_ != nil) { | |
7392 | [package_ retain]; | |
31a8e05a GP |
7393 | [subscribedSwitch_ setOn:([package_ subscribed] ? 1 : 0) animated:NO]; |
7394 | [ignoredSwitch_ setOn:([package_ ignored] ? 1 : 0) animated:NO]; | |
dbe0f181 | 7395 | } |
f159ecd4 JF |
7396 | |
7397 | [table_ reloadData]; | |
dbe0f181 JF |
7398 | } |
7399 | ||
dbe0f181 | 7400 | @end |
017b2b71 | 7401 | /* }}} */ |
f441e717 GP |
7402 | /* Signature Controller {{{ */ |
7403 | @interface SignatureController : CYBrowserController { | |
dbe0f181 JF |
7404 | _transient Database *database_; |
7405 | NSString *package_; | |
7406 | } | |
7407 | ||
9ae52960 | 7408 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
dbe0f181 JF |
7409 | |
7410 | @end | |
7411 | ||
f441e717 | 7412 | @implementation SignatureController |
dbe0f181 JF |
7413 | |
7414 | - (void) dealloc { | |
7415 | [package_ release]; | |
7416 | [super dealloc]; | |
7417 | } | |
7418 | ||
c2292b80 | 7419 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
dbe0f181 | 7420 | // XXX: dude! |
c2292b80 | 7421 | [super webView:view didClearWindowObject:window forFrame:frame]; |
dbe0f181 JF |
7422 | } |
7423 | ||
9ae52960 GP |
7424 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { |
7425 | if ((self = [super init]) != nil) { | |
dbe0f181 JF |
7426 | database_ = database; |
7427 | package_ = [package retain]; | |
7428 | [self reloadData]; | |
7429 | } return self; | |
7430 | } | |
7431 | ||
7432 | - (void) reloadData { | |
7433 | [self loadURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"signature" ofType:@"html"]]]; | |
7434 | } | |
7435 | ||
c1edf105 GP |
7436 | @end |
7437 | /* }}} */ | |
4cd1145e | 7438 | |
c1edf105 | 7439 | /* Role Controller {{{ */ |
1e4922b8 JF |
7440 | @interface RoleController : CYViewController < |
7441 | UITableViewDataSource, | |
7442 | UITableViewDelegate | |
7443 | > { | |
c1edf105 | 7444 | _transient Database *database_; |
a591888a JF |
7445 | // XXX: ok, "roledelegate_"?... |
7446 | _transient id roledelegate_; | |
c1edf105 GP |
7447 | UITableView *table_; |
7448 | UISegmentedControl *segment_; | |
7449 | UIView *container_; | |
7450 | } | |
1e4922b8 JF |
7451 | |
7452 | - (void) showDoneButton; | |
7453 | - (void) resizeSegmentedControl; | |
7454 | ||
c1edf105 GP |
7455 | @end |
7456 | ||
7457 | @implementation RoleController | |
7458 | - (void) dealloc { | |
7459 | [table_ release]; | |
7460 | [segment_ release]; | |
7461 | [container_ release]; | |
f99f86e2 | 7462 | |
c1edf105 GP |
7463 | [super dealloc]; |
7464 | } | |
7465 | ||
7466 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { | |
7467 | if ((self = [super init])) { | |
7468 | database_ = database; | |
7469 | roledelegate_ = delegate; | |
f99f86e2 | 7470 | |
c1edf105 | 7471 | [[self navigationItem] setTitle:UCLocalize("WHO_ARE_YOU")]; |
f99f86e2 | 7472 | |
c1edf105 | 7473 | NSArray *items = [NSArray arrayWithObjects: |
f99f86e2 | 7474 | UCLocalize("USER"), |
c1edf105 GP |
7475 | UCLocalize("HACKER"), |
7476 | UCLocalize("DEVELOPER"), | |
7477 | nil]; | |
7478 | segment_ = [[UISegmentedControl alloc] initWithItems:items]; | |
7479 | container_ = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, 44.0f)]; | |
7480 | [container_ addSubview:segment_]; | |
f99f86e2 | 7481 | |
c1edf105 GP |
7482 | int index = -1; |
7483 | if ([Role_ isEqualToString:@"User"]) index = 0; | |
7484 | if ([Role_ isEqualToString:@"Hacker"]) index = 1; | |
7485 | if ([Role_ isEqualToString:@"Developer"]) index = 2; | |
7486 | if (index != -1) { | |
7487 | [segment_ setSelectedSegmentIndex:index]; | |
7488 | [self showDoneButton]; | |
7489 | } | |
f99f86e2 | 7490 | |
c1edf105 | 7491 | [segment_ addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged]; |
62d55799 | 7492 | [self resizeSegmentedControl]; |
f99f86e2 | 7493 | |
c1edf105 GP |
7494 | table_ = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped]; |
7495 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; | |
7496 | [table_ setDelegate:self]; | |
7497 | [table_ setDataSource:self]; | |
7498 | [[self view] addSubview:table_]; | |
7499 | [table_ reloadData]; | |
7500 | } return self; | |
7501 | } | |
7502 | ||
62d55799 GP |
7503 | - (void) resizeSegmentedControl { |
7504 | CGFloat width = [[self view] frame].size.width; | |
7505 | [segment_ setFrame:CGRectMake(width / 32.0f, 0, width - (width / 32.0f * 2.0f), 44.0f)]; | |
7506 | } | |
7507 | ||
ca45de84 GP |
7508 | - (void) viewWillAppear:(BOOL)animated { |
7509 | [super viewWillAppear:animated]; | |
f99f86e2 | 7510 | |
62d55799 GP |
7511 | [self resizeSegmentedControl]; |
7512 | } | |
7513 | ||
7514 | - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { | |
7515 | [self resizeSegmentedControl]; | |
7516 | } | |
7517 | ||
7518 | - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { | |
7519 | [self resizeSegmentedControl]; | |
ca45de84 GP |
7520 | } |
7521 | ||
c1edf105 | 7522 | - (void) save { |
90e9a238 | 7523 | NSString *role(nil); |
f99f86e2 | 7524 | |
c1edf105 | 7525 | switch ([segment_ selectedSegmentIndex]) { |
fabc4a01 GP |
7526 | case 0: role = @"User"; break; |
7527 | case 1: role = @"Hacker"; break; | |
7528 | case 2: role = @"Developer"; break; | |
c1edf105 GP |
7529 | |
7530 | _nodefault | |
7531 | } | |
7532 | ||
fabc4a01 | 7533 | if (![role isEqualToString:Role_]) { |
90e9a238 | 7534 | bool rolling(Role_ == nil); |
fabc4a01 | 7535 | Role_ = role; |
f99f86e2 | 7536 | |
fabc4a01 GP |
7537 | Settings_ = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
7538 | Role_, @"Role", | |
7539 | nil]; | |
c1edf105 | 7540 | |
fabc4a01 | 7541 | [Metadata_ setObject:Settings_ forKey:@"Settings"]; |
c1edf105 | 7542 | |
fabc4a01 | 7543 | Changed_ = true; |
f99f86e2 | 7544 | |
90e9a238 JF |
7545 | if (rolling) |
7546 | [roledelegate_ loadData]; | |
7547 | else | |
7548 | [roledelegate_ updateData]; | |
fabc4a01 | 7549 | } |
c1edf105 GP |
7550 | } |
7551 | ||
7552 | - (void) segmentChanged:(UISegmentedControl *)control { | |
7553 | [self showDoneButton]; | |
7554 | } | |
7555 | ||
109d42fa | 7556 | - (void) saveAndClose { |
c1edf105 | 7557 | [self save]; |
109d42fa GP |
7558 | |
7559 | [[self navigationItem] setRightBarButtonItem:nil]; | |
c1edf105 GP |
7560 | [[self navigationController] dismissModalViewControllerAnimated:YES]; |
7561 | } | |
7562 | ||
109d42fa GP |
7563 | - (void) doneButtonClicked { |
7564 | UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)] autorelease]; | |
7565 | [spinner startAnimating]; | |
7566 | UIBarButtonItem *spinItem = [[[UIBarButtonItem alloc] initWithCustomView:spinner] autorelease]; | |
7567 | [[self navigationItem] setRightBarButtonItem:spinItem]; | |
7568 | ||
7569 | [self performSelector:@selector(saveAndClose) withObject:nil afterDelay:0]; | |
7570 | } | |
7571 | ||
c1edf105 | 7572 | - (void) showDoneButton { |
11f75d4f | 7573 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
c1edf105 GP |
7574 | initWithTitle:UCLocalize("DONE") |
7575 | style:UIBarButtonItemStyleDone | |
7576 | target:self | |
7577 | action:@selector(doneButtonClicked) | |
11f75d4f | 7578 | ] autorelease] animated:([[self navigationItem] rightBarButtonItem] == nil)]; |
c1edf105 GP |
7579 | } |
7580 | ||
7581 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { | |
62d55799 GP |
7582 | // XXX: For not having a single cell in the table, this sure is a lot of sections. |
7583 | return 6; | |
c1edf105 GP |
7584 | } |
7585 | ||
7586 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
7587 | return 0; // :( | |
7588 | } | |
7589 | ||
7590 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
7591 | return nil; // This method is required by the protocol. | |
7592 | } | |
7593 | ||
7594 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { | |
f99f86e2 | 7595 | if (section == 1) |
c1edf105 | 7596 | return UCLocalize("ROLE_EX"); |
f99f86e2 | 7597 | if (section == 4) |
c1edf105 GP |
7598 | return [NSString stringWithFormat: |
7599 | @"%@: %@\n%@: %@\n%@: %@", | |
f99f86e2 JF |
7600 | UCLocalize("USER"), UCLocalize("USER_EX"), |
7601 | UCLocalize("HACKER"), UCLocalize("HACKER_EX"), | |
c1edf105 GP |
7602 | UCLocalize("DEVELOPER"), UCLocalize("DEVELOPER_EX") |
7603 | ]; | |
7604 | else return nil; | |
7605 | } | |
7606 | ||
7607 | - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { | |
96291f72 | 7608 | return section == 3 ? 44.0f : 0; |
c1edf105 GP |
7609 | } |
7610 | ||
7611 | - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { | |
96291f72 | 7612 | return section == 3 ? container_ : nil; |
c1edf105 GP |
7613 | } |
7614 | ||
4cd1145e GP |
7615 | @end |
7616 | /* }}} */ | |
7617 | /* Stash Controller {{{ */ | |
7618 | @interface CYStashController : CYViewController { | |
a591888a JF |
7619 | // XXX: just delete these things |
7620 | _transient UIActivityIndicatorView *spinner_; | |
7621 | _transient UILabel *status_; | |
7622 | _transient UILabel *caption_; | |
4cd1145e GP |
7623 | } |
7624 | @end | |
7625 | ||
7626 | @implementation CYStashController | |
7627 | - (id) init { | |
7628 | if ((self = [super init])) { | |
83aff0db | 7629 | [[self view] setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; |
4cd1145e | 7630 | |
11f75d4f | 7631 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; |
83aff0db | 7632 | CGRect spinrect = [spinner_ frame]; |
4cd1145e GP |
7633 | spinrect.origin.x = ([[self view] frame].size.width / 2) - (spinrect.size.width / 2); |
7634 | spinrect.origin.y = [[self view] frame].size.height - 80.0f; | |
7635 | [spinner_ setFrame:spinrect]; | |
7636 | [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; | |
83aff0db | 7637 | [[self view] addSubview:spinner_]; |
83aff0db | 7638 | [spinner_ startAnimating]; |
4cd1145e GP |
7639 | |
7640 | CGRect captrect; | |
7641 | captrect.size.width = [[self view] frame].size.width; | |
7642 | captrect.size.height = 40.0f; | |
7643 | captrect.origin.x = 0; | |
7644 | captrect.origin.y = ([[self view] frame].size.height / 2) - (captrect.size.height * 2); | |
11f75d4f | 7645 | caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; |
4cd1145e | 7646 | [caption_ setText:@"Initializing Filesystem"]; |
7fc6909e | 7647 | [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
4cd1145e GP |
7648 | [caption_ setFont:[UIFont boldSystemFontOfSize:28.0f]]; |
7649 | [caption_ setTextColor:[UIColor whiteColor]]; | |
7650 | [caption_ setBackgroundColor:[UIColor clearColor]]; | |
7651 | [caption_ setShadowColor:[UIColor blackColor]]; | |
7652 | [caption_ setTextAlignment:UITextAlignmentCenter]; | |
7653 | [[self view] addSubview:caption_]; | |
4cd1145e GP |
7654 | |
7655 | CGRect statusrect; | |
7656 | statusrect.size.width = [[self view] frame].size.width; | |
7657 | statusrect.size.height = 30.0f; | |
7658 | statusrect.origin.x = 0; | |
7659 | statusrect.origin.y = ([[self view] frame].size.height / 2) - statusrect.size.height; | |
11f75d4f | 7660 | status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; |
7fc6909e | 7661 | [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
4cd1145e GP |
7662 | [status_ setText:@"(Cydia will exit when complete.)"]; |
7663 | [status_ setFont:[UIFont systemFontOfSize:16.0f]]; | |
7664 | [status_ setTextColor:[UIColor whiteColor]]; | |
7665 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
7666 | [status_ setShadowColor:[UIColor blackColor]]; | |
7667 | [status_ setTextAlignment:UITextAlignmentCenter]; | |
7668 | [[self view] addSubview:status_]; | |
4cd1145e GP |
7669 | } return self; |
7670 | } | |
7671 | ||
7672 | - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { | |
7673 | return IsWildcat_ || orientation == UIInterfaceOrientationPortrait; | |
7674 | } | |
dbe0f181 | 7675 | @end |
f159ecd4 | 7676 | /* }}} */ |
dbe0f181 | 7677 | |
e7a88a8c GP |
7678 | /* Cydia Container {{{ */ |
7679 | @interface CYContainer : UIViewController <ProgressDelegate> { | |
7680 | _transient Database *database_; | |
7681 | RefreshBar *refreshbar_; | |
7682 | ||
7683 | bool dropped_; | |
7684 | bool updating_; | |
a591888a JF |
7685 | // XXX: ok, "updatedelegate_"?... |
7686 | _transient NSObject<CydiaDelegate> *updatedelegate_; | |
7687 | // XXX: can't we query for this variable when we need it? | |
7688 | _transient UITabBarController *root_; | |
e7a88a8c GP |
7689 | } |
7690 | ||
1e4922b8 JF |
7691 | - (void) setTabBarController:(UITabBarController *)controller; |
7692 | ||
7693 | - (void) dropBar:(BOOL)animated; | |
7694 | - (void) beginUpdate; | |
7695 | - (void) raiseBar:(BOOL)animated; | |
57fde81f | 7696 | - (BOOL) updating; |
1e4922b8 | 7697 | |
e7a88a8c GP |
7698 | @end |
7699 | ||
7700 | @implementation CYContainer | |
7701 | ||
57fde81f GP |
7702 | - (BOOL) _reallyWantsFullScreenLayout { |
7703 | return YES; | |
7704 | } | |
7705 | ||
15b41c77 GP |
7706 | // NOTE: UIWindow only sends the top controller these messages, |
7707 | // So we have to forward them on. | |
7708 | ||
7709 | - (void) viewDidAppear:(BOOL)animated { | |
7710 | [super viewDidAppear:animated]; | |
7711 | [root_ viewDidAppear:animated]; | |
7712 | } | |
7713 | ||
7714 | - (void) viewWillAppear:(BOOL)animated { | |
7715 | [super viewWillAppear:animated]; | |
7716 | [root_ viewWillAppear:animated]; | |
7717 | } | |
7718 | ||
7719 | - (void) viewDidDisappear:(BOOL)animated { | |
7720 | [super viewDidDisappear:animated]; | |
7721 | [root_ viewDidDisappear:animated]; | |
7722 | } | |
7723 | ||
7724 | - (void) viewWillDisappear:(BOOL)animated { | |
7725 | [super viewWillDisappear:animated]; | |
7726 | [root_ viewWillDisappear:animated]; | |
7727 | } | |
7728 | ||
e7a88a8c | 7729 | - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { |
832c0799 | 7730 | return ![updatedelegate_ hudIsShowing] && (IsWildcat_ || orientation == UIInterfaceOrientationPortrait); |
e7a88a8c GP |
7731 | } |
7732 | ||
1e4922b8 | 7733 | - (void) setTabBarController:(UITabBarController *)controller { |
e7a88a8c GP |
7734 | root_ = controller; |
7735 | [[self view] addSubview:[root_ view]]; | |
7736 | } | |
7737 | ||
7738 | - (void) setUpdate:(NSDate *)date { | |
7739 | [self beginUpdate]; | |
7740 | } | |
7741 | ||
7742 | - (void) beginUpdate { | |
7743 | [self dropBar:YES]; | |
7744 | [refreshbar_ start]; | |
f99f86e2 | 7745 | |
e7a88a8c | 7746 | updating_ = true; |
f99f86e2 | 7747 | |
e7a88a8c GP |
7748 | [NSThread |
7749 | detachNewThreadSelector:@selector(performUpdate) | |
7750 | toTarget:self | |
7751 | withObject:nil | |
7752 | ]; | |
7753 | } | |
7754 | ||
7755 | - (void) performUpdate { _pooled | |
7756 | Status status; | |
7757 | status.setDelegate(self); | |
7758 | [database_ updateWithStatus:status]; | |
7759 | ||
7760 | [self | |
7761 | performSelectorOnMainThread:@selector(completeUpdate) | |
7762 | withObject:nil | |
7763 | waitUntilDone:NO | |
7764 | ]; | |
7765 | } | |
7766 | ||
7767 | - (void) completeUpdate { | |
96291f72 JF |
7768 | if (!updating_) |
7769 | return; | |
e7a88a8c | 7770 | updating_ = false; |
f99f86e2 | 7771 | |
e7a88a8c GP |
7772 | [self raiseBar:YES]; |
7773 | [refreshbar_ stop]; | |
7774 | [updatedelegate_ performSelector:@selector(reloadData) withObject:nil afterDelay:0]; | |
7775 | } | |
7776 | ||
7777 | - (void) cancelUpdate { | |
24966c5d GP |
7778 | updating_ = false; |
7779 | [self raiseBar:YES]; | |
7780 | [refreshbar_ stop]; | |
7781 | [updatedelegate_ performSelector:@selector(updateData) withObject:nil afterDelay:0]; | |
e7a88a8c GP |
7782 | } |
7783 | ||
7784 | - (void) cancelPressed { | |
7785 | [self cancelUpdate]; | |
7786 | } | |
7787 | ||
7788 | - (BOOL) updating { | |
7789 | return updating_; | |
7790 | } | |
7791 | ||
7792 | - (void) setProgressError:(NSString *)error withTitle:(NSString *)title { | |
7793 | [refreshbar_ setPrompt:[NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), UCLocalize("ERROR"), error]]; | |
7794 | } | |
7795 | ||
7796 | - (void) startProgress { | |
7797 | } | |
7798 | ||
7799 | - (void) setProgressTitle:(NSString *)title { | |
7800 | [self | |
7801 | performSelectorOnMainThread:@selector(_setProgressTitle:) | |
7802 | withObject:title | |
7803 | waitUntilDone:YES | |
7804 | ]; | |
7805 | } | |
7806 | ||
7807 | - (bool) isCancelling:(size_t)received { | |
7808 | return !updating_; | |
7809 | } | |
7810 | ||
7811 | - (void) setProgressPercent:(float)percent { | |
7812 | [self | |
7813 | performSelectorOnMainThread:@selector(_setProgressPercent:) | |
7814 | withObject:[NSNumber numberWithFloat:percent] | |
7815 | waitUntilDone:YES | |
7816 | ]; | |
7817 | } | |
7818 | ||
7819 | - (void) addProgressOutput:(NSString *)output { | |
7820 | [self | |
7821 | performSelectorOnMainThread:@selector(_addProgressOutput:) | |
7822 | withObject:output | |
7823 | waitUntilDone:YES | |
7824 | ]; | |
7825 | } | |
7826 | ||
7827 | - (void) _setProgressTitle:(NSString *)title { | |
7828 | [refreshbar_ setPrompt:title]; | |
7829 | } | |
7830 | ||
7831 | - (void) _setProgressPercent:(NSNumber *)percent { | |
7832 | [refreshbar_ setProgress:[percent floatValue]]; | |
7833 | } | |
7834 | ||
7835 | - (void) _addProgressOutput:(NSString *)output { | |
7836 | } | |
7837 | ||
7838 | - (void) setUpdateDelegate:(id)delegate { | |
7839 | updatedelegate_ = delegate; | |
7840 | } | |
7841 | ||
149ed89e GP |
7842 | - (CGFloat) statusBarHeight { |
7843 | if (UIInterfaceOrientationIsPortrait([self interfaceOrientation])) { | |
7844 | return [[UIApplication sharedApplication] statusBarFrame].size.height; | |
7845 | } else { | |
7846 | return [[UIApplication sharedApplication] statusBarFrame].size.width; | |
7847 | } | |
7848 | } | |
7849 | ||
e7a88a8c | 7850 | - (void) dropBar:(BOOL)animated { |
96291f72 JF |
7851 | if (dropped_) |
7852 | return; | |
e7a88a8c GP |
7853 | dropped_ = true; |
7854 | ||
7855 | [[self view] addSubview:refreshbar_]; | |
7856 | ||
149ed89e | 7857 | CGFloat sboffset = [self statusBarHeight]; |
57fde81f | 7858 | |
283fc596 | 7859 | CGRect barframe = [refreshbar_ frame]; |
57fde81f GP |
7860 | barframe.origin.y = sboffset; |
7861 | [refreshbar_ setFrame:barframe]; | |
7862 | ||
96291f72 JF |
7863 | if (animated) |
7864 | [UIView beginAnimations:nil context:NULL]; | |
e7a88a8c | 7865 | CGRect viewframe = [[root_ view] frame]; |
57fde81f GP |
7866 | viewframe.origin.y += barframe.size.height + sboffset; |
7867 | viewframe.size.height -= barframe.size.height + sboffset; | |
e7a88a8c | 7868 | [[root_ view] setFrame:viewframe]; |
96291f72 JF |
7869 | if (animated) |
7870 | [UIView commitAnimations]; | |
e7a88a8c | 7871 | |
15b41c77 GP |
7872 | // Ensure bar has the proper width for our view, it might have changed |
7873 | barframe.size.width = viewframe.size.width; | |
7874 | [refreshbar_ setFrame:barframe]; | |
7875 | ||
e7a88a8c GP |
7876 | // XXX: fix Apple's layout bug |
7877 | [[root_ selectedViewController] _updateLayoutForStatusBarAndInterfaceOrientation]; | |
7878 | } | |
7879 | ||
7880 | - (void) raiseBar:(BOOL)animated { | |
96291f72 JF |
7881 | if (!dropped_) |
7882 | return; | |
e7a88a8c GP |
7883 | dropped_ = false; |
7884 | ||
7885 | [refreshbar_ removeFromSuperview]; | |
7886 | ||
283fc596 | 7887 | CGFloat sboffset = [self statusBarHeight]; |
57fde81f | 7888 | |
96291f72 JF |
7889 | if (animated) |
7890 | [UIView beginAnimations:nil context:NULL]; | |
e7a88a8c GP |
7891 | CGRect barframe = [refreshbar_ frame]; |
7892 | CGRect viewframe = [[root_ view] frame]; | |
57fde81f GP |
7893 | viewframe.origin.y -= barframe.size.height + sboffset; |
7894 | viewframe.size.height += barframe.size.height + sboffset; | |
e7a88a8c | 7895 | [[root_ view] setFrame:viewframe]; |
96291f72 JF |
7896 | if (animated) |
7897 | [UIView commitAnimations]; | |
e7a88a8c GP |
7898 | |
7899 | // XXX: fix Apple's layout bug | |
7900 | [[root_ selectedViewController] _updateLayoutForStatusBarAndInterfaceOrientation]; | |
7901 | } | |
7902 | ||
62d55799 | 7903 | - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { |
d6011e10 RP |
7904 | // XXX: fix Apple's layout bug |
7905 | [[root_ selectedViewController] _updateLayoutForStatusBarAndInterfaceOrientation]; | |
7906 | } | |
7907 | ||
15b41c77 | 7908 | - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { |
e7a88a8c GP |
7909 | if (dropped_) { |
7910 | [self raiseBar:NO]; | |
7911 | [self dropBar:NO]; | |
7912 | } | |
f99f86e2 | 7913 | |
e7a88a8c GP |
7914 | // XXX: fix Apple's layout bug |
7915 | [[root_ selectedViewController] _updateLayoutForStatusBarAndInterfaceOrientation]; | |
7916 | } | |
7917 | ||
57fde81f GP |
7918 | - (void) statusBarFrameChanged:(NSNotification *)notification { |
7919 | if (dropped_) { | |
7920 | [self raiseBar:NO]; | |
7921 | [self dropBar:NO]; | |
7922 | } | |
7923 | } | |
7924 | ||
e7a88a8c GP |
7925 | - (void) dealloc { |
7926 | [refreshbar_ release]; | |
57fde81f | 7927 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
e7a88a8c GP |
7928 | [super dealloc]; |
7929 | } | |
7930 | ||
d5653f96 | 7931 | - (id) initWithDatabase:(Database *)database { |
e7a88a8c GP |
7932 | if ((self = [super init]) != nil) { |
7933 | database_ = database; | |
7934 | ||
15b41c77 | 7935 | [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
57fde81f | 7936 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; |
15b41c77 GP |
7937 | |
7938 | refreshbar_ = [[RefreshBar alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, [UINavigationBar defaultSize].height) delegate:self]; | |
e7a88a8c GP |
7939 | } return self; |
7940 | } | |
7941 | ||
7942 | @end | |
7943 | /* }}} */ | |
7944 | ||
9ae52960 GP |
7945 | typedef enum { |
7946 | kCydiaTag = 0, | |
7947 | kSectionsTag = 1, | |
7948 | kChangesTag = 2, | |
7949 | kManageTag = 3, | |
7950 | kInstalledTag = 4, | |
7951 | kSourcesTag = 5, | |
7952 | kSearchTag = 6 | |
7953 | } CYTabTag; | |
d3bef7bc | 7954 | |
4941f41d | 7955 | @interface Cydia : UIApplication < |
f441e717 GP |
7956 | ConfirmationControllerDelegate, |
7957 | ProgressControllerDelegate, | |
9b62701b | 7958 | CydiaDelegate, |
18884e36 JF |
7959 | UINavigationControllerDelegate, |
7960 | UITabBarControllerDelegate | |
4941f41d | 7961 | > { |
a591888a JF |
7962 | // XXX: evaluate all fields for _transient |
7963 | ||
a75f53e7 | 7964 | UIWindow *window_; |
e7a88a8c | 7965 | CYContainer *container_; |
18884e36 | 7966 | CYTabBarController *tabbar_; |
3319715b | 7967 | |
9e07091a | 7968 | NSMutableArray *essential_; |
3319715b | 7969 | NSMutableArray *broken_; |
a75f53e7 JF |
7970 | |
7971 | Database *database_; | |
a75f53e7 | 7972 | |
84848968 | 7973 | int tag_; |
114ac994 | 7974 | int hudcount_; |
4a3efc29 | 7975 | NSURL *starturl_; |
e25e221f | 7976 | |
f441e717 GP |
7977 | SectionsController *sections_; |
7978 | ChangesController *changes_; | |
7979 | ManageController *manage_; | |
7980 | SearchController *search_; | |
9ae52960 | 7981 | SourceTable *sources_; |
f441e717 | 7982 | InstalledController *installed_; |
9ae52960 | 7983 | id queueDelegate_; |
6932575e | 7984 | |
4cd1145e GP |
7985 | CYStashController *stash_; |
7986 | ||
e7a88a8c | 7987 | bool loaded_; |
a75f53e7 JF |
7988 | } |
7989 | ||
881fe77f JF |
7990 | - (CYViewController *) _pageForURL:(NSURL *)url withClass:(Class)_class; |
7991 | - (void) setPage:(CYViewController *)page; | |
90e9a238 | 7992 | - (void) loadData; |
6981ccdf | 7993 | |
a75f53e7 JF |
7994 | @end |
7995 | ||
6981ccdf | 7996 | static _finline void _setHomePage(Cydia *self) { |
f441e717 | 7997 | [self setPage:[self _pageForURL:[NSURL URLWithString:CydiaURL(@"")] withClass:[HomeController class]]]; |
6981ccdf JF |
7998 | } |
7999 | ||
a75f53e7 JF |
8000 | @implementation Cydia |
8001 | ||
9ae52960 | 8002 | - (void) beginUpdate { |
e7a88a8c | 8003 | [container_ beginUpdate]; |
9ae52960 GP |
8004 | } |
8005 | ||
8006 | - (BOOL) updating { | |
e7a88a8c | 8007 | return [container_ updating]; |
9ae52960 GP |
8008 | } |
8009 | ||
5ec44e34 JF |
8010 | - (UIView *) rotatingContentViewForWindow:(UIWindow *)window { |
8011 | return window_; | |
8012 | } | |
8013 | ||
3319715b JF |
8014 | - (void) _loaded { |
8015 | if ([broken_ count] != 0) { | |
8016 | int count = [broken_ count]; | |
8017 | ||
7d3e75f4 | 8018 | UIAlertView *alert = [[[UIAlertView alloc] |
61b13cae | 8019 | initWithTitle:(count == 1 ? UCLocalize("HALFINSTALLED_PACKAGE") : [NSString stringWithFormat:UCLocalize("HALFINSTALLED_PACKAGES"), count]) |
9ae52960 GP |
8020 | message:UCLocalize("HALFINSTALLED_PACKAGE_EX") |
8021 | delegate:self | |
8022 | cancelButtonTitle:UCLocalize("FORCIBLY_CLEAR") | |
8023 | otherButtonTitles:UCLocalize("TEMPORARY_IGNORE"), nil | |
3319715b JF |
8024 | ] autorelease]; |
8025 | ||
7d3e75f4 GP |
8026 | [alert setContext:@"fixhalf"]; |
8027 | [alert show]; | |
3319715b JF |
8028 | } else if (!Ignored_ && [essential_ count] != 0) { |
8029 | int count = [essential_ count]; | |
8030 | ||
7d3e75f4 | 8031 | UIAlertView *alert = [[[UIAlertView alloc] |
61b13cae | 8032 | initWithTitle:(count == 1 ? UCLocalize("ESSENTIAL_UPGRADE") : [NSString stringWithFormat:UCLocalize("ESSENTIAL_UPGRADES"), count]) |
9ae52960 GP |
8033 | message:UCLocalize("ESSENTIAL_UPGRADE_EX") |
8034 | delegate:self | |
8035 | cancelButtonTitle:UCLocalize("TEMPORARY_IGNORE") | |
7d3e75f4 | 8036 | otherButtonTitles:UCLocalize("UPGRADE_ESSENTIAL"), UCLocalize("COMPLETE_UPGRADE"), nil |
3319715b JF |
8037 | ] autorelease]; |
8038 | ||
7d3e75f4 GP |
8039 | [alert setContext:@"upgrade"]; |
8040 | [alert show]; | |
3319715b JF |
8041 | } |
8042 | } | |
8043 | ||
419a9efd JF |
8044 | - (void) _saveConfig { |
8045 | if (Changed_) { | |
8046 | _trace(); | |
8047 | NSString *error(nil); | |
8048 | if (NSData *data = [NSPropertyListSerialization dataFromPropertyList:Metadata_ format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error]) { | |
8049 | _trace(); | |
8050 | NSError *error(nil); | |
8051 | if (![data writeToFile:@"/var/lib/cydia/metadata.plist" options:NSAtomicWrite error:&error]) | |
8052 | NSLog(@"failure to save metadata data: %@", error); | |
8053 | _trace(); | |
8054 | } else { | |
8055 | NSLog(@"failure to serialize metadata: %@", error); | |
8056 | return; | |
8057 | } | |
8058 | ||
8059 | Changed_ = false; | |
8060 | } | |
8061 | } | |
8062 | ||
8063 | - (void) _updateData { | |
8064 | [self _saveConfig]; | |
8065 | ||
8066 | /* XXX: this is just stupid */ | |
84848968 | 8067 | if (tag_ != 1 && sections_ != nil) |
419a9efd | 8068 | [sections_ reloadData]; |
84848968 | 8069 | if (tag_ != 2 && changes_ != nil) |
419a9efd | 8070 | [changes_ reloadData]; |
84848968 | 8071 | if (tag_ != 4 && search_ != nil) |
419a9efd JF |
8072 | [search_ reloadData]; |
8073 | ||
1e4922b8 | 8074 | [(CYNavigationController *)[tabbar_ selectedViewController] reloadData]; |
9ae52960 GP |
8075 | } |
8076 | ||
8077 | - (int)indexOfTabWithTag:(int)tag { | |
8078 | int i = 0; | |
8079 | for (UINavigationController *controller in [tabbar_ viewControllers]) { | |
96291f72 JF |
8080 | if ([[controller tabBarItem] tag] == tag) |
8081 | return i; | |
9ae52960 GP |
8082 | i += 1; |
8083 | } | |
f99f86e2 | 8084 | |
9ae52960 | 8085 | return -1; |
419a9efd JF |
8086 | } |
8087 | ||
d56a0c9b | 8088 | - (void) _refreshIfPossible { |
e7a88a8c | 8089 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
f99f86e2 | 8090 | |
24966c5d GP |
8091 | bool recently = false; |
8092 | NSDate *update([Metadata_ objectForKey:@"LastUpdate"]); | |
8093 | if (update != nil) { | |
8094 | NSTimeInterval interval([update timeIntervalSinceNow]); | |
8095 | if (interval <= 0 && interval > -(15*60)) | |
8096 | recently = true; | |
8097 | } | |
8098 | ||
8099 | // Don't automatic refresh if: | |
8100 | // - We already refreshed recently. | |
8101 | // - We already auto-refreshed this launch. | |
8102 | // - Auto-refresh is disabled. | |
8103 | if (recently || loaded_ || ManualRefresh) { | |
8104 | [self performSelectorOnMainThread:@selector(_loaded) withObject:nil waitUntilDone:NO]; | |
8105 | ||
8106 | // If we are cancelling due to ManualRefresh or a recent refresh | |
8107 | // we need to make sure it knows it's already loaded. | |
8108 | loaded_ = true; | |
8109 | return; | |
8110 | } else { | |
8111 | // We are going to load, so remember that. | |
8112 | loaded_ = true; | |
8113 | } | |
8114 | ||
d7a235a6 JF |
8115 | SCNetworkReachabilityFlags flags; { |
8116 | SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(NULL, "cydia.saurik.com")); | |
8117 | SCNetworkReachabilityGetFlags(reachability, &flags); | |
8118 | CFRelease(reachability); | |
8119 | } | |
8120 | ||
8121 | // XXX: this elaborate mess is what Apple is using to determine this? :( | |
8122 | // XXX: do we care if the user has to intervene? maybe that's ok? | |
8123 | bool reachable( | |
8124 | (flags & kSCNetworkReachabilityFlagsReachable) != 0 && ( | |
8125 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || ( | |
8126 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 || | |
8127 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0 | |
8128 | ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 || | |
8129 | (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0 | |
8130 | ) | |
8131 | ); | |
f99f86e2 | 8132 | |
24966c5d GP |
8133 | // If we can reach the server, auto-refresh! |
8134 | if (reachable) | |
e7a88a8c | 8135 | [container_ performSelectorOnMainThread:@selector(setUpdate:) withObject:update waitUntilDone:NO]; |
d56a0c9b | 8136 | |
e7a88a8c | 8137 | [pool release]; |
d56a0c9b GP |
8138 | } |
8139 | ||
8140 | - (void) refreshIfPossible { | |
e7a88a8c | 8141 | [NSThread detachNewThreadSelector:@selector(_refreshIfPossible) toTarget:self withObject:nil]; |
d56a0c9b GP |
8142 | } |
8143 | ||
e057ec05 | 8144 | - (void) _reloadData { |
7fc6909e GP |
8145 | UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); |
8146 | [hud setText:UCLocalize("RELOADING_DATA")]; | |
a75f53e7 | 8147 | |
7398a389 | 8148 | [database_ yieldToSelector:@selector(reloadData) withObject:nil]; |
7398a389 | 8149 | |
96291f72 JF |
8150 | if (hud != nil) |
8151 | [self removeProgressHUD:hud]; | |
d12c6e70 | 8152 | |
e057ec05 | 8153 | size_t changes(0); |
3319715b | 8154 | |
9e07091a | 8155 | [essential_ removeAllObjects]; |
3319715b | 8156 | [broken_ removeAllObjects]; |
686e302f | 8157 | |
6981ccdf | 8158 | NSArray *packages([database_ packages]); |
f464053e | 8159 | for (Package *package in packages) { |
3319715b JF |
8160 | if ([package half]) |
8161 | [broken_ addObject:package]; | |
238b07ce | 8162 | if ([package upgradableAndEssential:NO]) { |
9e07091a JF |
8163 | if ([package essential]) |
8164 | [essential_ addObject:package]; | |
e057ec05 | 8165 | ++changes; |
9e07091a | 8166 | } |
e057ec05 | 8167 | } |
686e302f | 8168 | |
24966c5d | 8169 | UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:[self indexOfTabWithTag:kChangesTag]] tabBarItem]; |
e057ec05 JF |
8170 | if (changes != 0) { |
8171 | NSString *badge([[NSNumber numberWithInt:changes] stringValue]); | |
24966c5d | 8172 | [changesItem setBadgeValue:badge]; |
3203e099 | 8173 | [changesItem setAnimatedBadge:([essential_ count] > 0)]; |
f99f86e2 | 8174 | |
6932575e JF |
8175 | if ([self respondsToSelector:@selector(setApplicationBadge:)]) |
8176 | [self setApplicationBadge:badge]; | |
8177 | else | |
8178 | [self setApplicationBadgeString:badge]; | |
d12c6e70 | 8179 | } else { |
24966c5d GP |
8180 | [changesItem setBadgeValue:nil]; |
8181 | [changesItem setAnimatedBadge:NO]; | |
f99f86e2 | 8182 | |
6932575e JF |
8183 | if ([self respondsToSelector:@selector(removeApplicationBadge)]) |
8184 | [self removeApplicationBadge]; | |
8185 | else // XXX: maybe use setApplicationBadgeString also? | |
8186 | [self setApplicationIconBadgeNumber:0]; | |
d12c6e70 | 8187 | } |
686e302f | 8188 | |
419a9efd | 8189 | [self _updateData]; |
fa7bb92f | 8190 | |
e7a88a8c | 8191 | [self refreshIfPossible]; |
fa7bb92f JF |
8192 | } |
8193 | ||
faf4eb4f | 8194 | - (void) updateData { |
419a9efd | 8195 | [self _updateData]; |
686e302f JF |
8196 | } |
8197 | ||
faf4eb4f JF |
8198 | - (void) update_ { |
8199 | [database_ update]; | |
8200 | } | |
8201 | ||
8202 | - (void) syncData { | |
6981ccdf | 8203 | FILE *file(fopen("/etc/apt/sources.list.d/cydia.list", "w")); |
faf4eb4f JF |
8204 | _assert(file != NULL); |
8205 | ||
6981ccdf JF |
8206 | for (NSString *key in [Sources_ allKeys]) { |
8207 | NSDictionary *source([Sources_ objectForKey:key]); | |
faf4eb4f JF |
8208 | |
8209 | fprintf(file, "%s %s %s\n", | |
8210 | [[source objectForKey:@"Type"] UTF8String], | |
8211 | [[source objectForKey:@"URI"] UTF8String], | |
8212 | [[source objectForKey:@"Distribution"] UTF8String] | |
8213 | ); | |
8214 | } | |
8215 | ||
8216 | fclose(file); | |
8217 | ||
8218 | [self _saveConfig]; | |
8219 | ||
f441e717 | 8220 | ProgressController *progress = [[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]; |
9b62701b | 8221 | CYNavigationController *navigation = [[[CYNavigationController alloc] initWithRootViewController:progress] autorelease]; |
96291f72 JF |
8222 | if (IsWildcat_) |
8223 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
e7a88a8c | 8224 | [container_ presentModalViewController:navigation animated:YES]; |
9ae52960 GP |
8225 | |
8226 | [progress | |
faf4eb4f JF |
8227 | detachNewThreadSelector:@selector(update_) |
8228 | toTarget:self | |
8229 | withObject:nil | |
61b13cae | 8230 | title:UCLocalize("UPDATING_SOURCES") |
faf4eb4f JF |
8231 | ]; |
8232 | } | |
8233 | ||
e057ec05 JF |
8234 | - (void) reloadData { |
8235 | @synchronized (self) { | |
9ae52960 | 8236 | [self _reloadData]; |
e057ec05 | 8237 | } |
686e302f JF |
8238 | } |
8239 | ||
8240 | - (void) resolve { | |
8241 | pkgProblemResolver *resolver = [database_ resolver]; | |
8242 | ||
8243 | resolver->InstallProtect(); | |
8244 | if (!resolver->Resolve(true)) | |
8245 | _error->Discard(); | |
4941f41d JF |
8246 | } |
8247 | ||
04700693 | 8248 | - (CGRect) popUpBounds { |
9ae52960 | 8249 | return [[tabbar_ view] bounds]; |
04700693 JF |
8250 | } |
8251 | ||
6981ccdf JF |
8252 | - (bool) perform { |
8253 | if (![database_ prepare]) | |
8254 | return false; | |
242bcc6d | 8255 | |
f441e717 | 8256 | ConfirmationController *page([[[ConfirmationController alloc] initWithDatabase:database_] autorelease]); |
f464053e | 8257 | [page setDelegate:self]; |
a591888a | 8258 | CYNavigationController *confirm_([[[CYNavigationController alloc] initWithRootViewController:page] autorelease]); |
9ae52960 | 8259 | [confirm_ setDelegate:self]; |
242bcc6d | 8260 | |
96291f72 JF |
8261 | if (IsWildcat_) |
8262 | [confirm_ setModalPresentationStyle:UIModalPresentationFormSheet]; | |
e7a88a8c | 8263 | [container_ presentModalViewController:confirm_ animated:YES]; |
6981ccdf JF |
8264 | |
8265 | return true; | |
2d28b35a JF |
8266 | } |
8267 | ||
3ff1504e JF |
8268 | - (void) queue { |
8269 | @synchronized (self) { | |
8270 | [self perform]; | |
8271 | } | |
8272 | } | |
8273 | ||
8274 | - (void) clearPackage:(Package *)package { | |
8275 | @synchronized (self) { | |
8276 | [package clear]; | |
8277 | [self resolve]; | |
8278 | [self perform]; | |
8279 | } | |
8280 | } | |
8281 | ||
daf7f6e2 JF |
8282 | - (void) installPackages:(NSArray *)packages { |
8283 | @synchronized (self) { | |
8284 | for (Package *package in packages) | |
8285 | [package install]; | |
8286 | [self resolve]; | |
8287 | [self perform]; | |
8288 | } | |
8289 | } | |
8290 | ||
e057ec05 JF |
8291 | - (void) installPackage:(Package *)package { |
8292 | @synchronized (self) { | |
8293 | [package install]; | |
8294 | [self resolve]; | |
8295 | [self perform]; | |
8296 | } | |
8297 | } | |
8298 | ||
8299 | - (void) removePackage:(Package *)package { | |
8300 | @synchronized (self) { | |
8301 | [package remove]; | |
8302 | [self resolve]; | |
8303 | [self perform]; | |
8304 | } | |
8305 | } | |
8306 | ||
8307 | - (void) distUpgrade { | |
8308 | @synchronized (self) { | |
6981ccdf JF |
8309 | if (![database_ upgrade]) |
8310 | return; | |
e057ec05 JF |
8311 | [self perform]; |
8312 | } | |
686e302f JF |
8313 | } |
8314 | ||
3ff1504e | 8315 | - (void) complete { |
e057ec05 | 8316 | @synchronized (self) { |
e057ec05 JF |
8317 | [self _reloadData]; |
8318 | } | |
2d28b35a JF |
8319 | } |
8320 | ||
9ae52960 | 8321 | - (void) confirmWithNavigationController:(UINavigationController *)navigation { |
f441e717 | 8322 | ProgressController *progress = [[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]; |
f99f86e2 | 8323 | |
9ae52960 GP |
8324 | if (navigation != nil) { |
8325 | [navigation pushViewController:progress animated:YES]; | |
8326 | } else { | |
15b41c77 | 8327 | navigation = [[[CYNavigationController alloc] initWithRootViewController:progress] autorelease]; |
96291f72 JF |
8328 | if (IsWildcat_) |
8329 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
e7a88a8c | 8330 | [container_ presentModalViewController:navigation animated:YES]; |
9ae52960 | 8331 | } |
f99f86e2 | 8332 | |
9ae52960 | 8333 | [progress |
4941f41d JF |
8334 | detachNewThreadSelector:@selector(perform) |
8335 | toTarget:database_ | |
8336 | withObject:nil | |
61b13cae | 8337 | title:UCLocalize("RUNNING") |
4941f41d JF |
8338 | ]; |
8339 | } | |
8340 | ||
ba6cbb36 | 8341 | - (void) progressControllerIsComplete:(ProgressController *)progress { |
3ff1504e | 8342 | [self complete]; |
a75f53e7 JF |
8343 | } |
8344 | ||
881fe77f | 8345 | - (void) setPage:(CYViewController *)page { |
e057ec05 | 8346 | [page setDelegate:self]; |
9ae52960 | 8347 | |
1e4922b8 | 8348 | CYNavigationController *navController = (CYNavigationController *) [tabbar_ selectedViewController]; |
eceab8e6 | 8349 | [navController setViewControllers:[NSArray arrayWithObject:page]]; |
96291f72 JF |
8350 | for (CYNavigationController *page in [tabbar_ viewControllers]) |
8351 | if (page != navController) | |
8352 | [page setViewControllers:nil]; | |
a75f53e7 JF |
8353 | } |
8354 | ||
881fe77f | 8355 | - (CYViewController *) _pageForURL:(NSURL *)url withClass:(Class)_class { |
9ae52960 | 8356 | CYBrowserController *browser = [[[_class alloc] init] autorelease]; |
faf4eb4f | 8357 | [browser loadURL:url]; |
e057ec05 | 8358 | return browser; |
4941f41d JF |
8359 | } |
8360 | ||
f441e717 | 8361 | - (SectionsController *) sectionsController { |
6932575e | 8362 | if (sections_ == nil) |
f441e717 | 8363 | sections_ = [[SectionsController alloc] initWithDatabase:database_]; |
6932575e JF |
8364 | return sections_; |
8365 | } | |
8366 | ||
f441e717 | 8367 | - (ChangesController *) changesController { |
5ec44e34 | 8368 | if (changes_ == nil) |
f441e717 | 8369 | changes_ = [[ChangesController alloc] initWithDatabase:database_ delegate:self]; |
5ec44e34 JF |
8370 | return changes_; |
8371 | } | |
8372 | ||
f441e717 | 8373 | - (ManageController *) manageController { |
9ae52960 | 8374 | if (manage_ == nil) { |
f441e717 | 8375 | manage_ = (ManageController *) [[self |
5ec44e34 | 8376 | _pageForURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"manage" ofType:@"html"]] |
f441e717 | 8377 | withClass:[ManageController class] |
5ec44e34 | 8378 | ] retain]; |
96291f72 JF |
8379 | if (!IsWildcat_) |
8380 | queueDelegate_ = manage_; | |
9ae52960 | 8381 | } |
5ec44e34 JF |
8382 | return manage_; |
8383 | } | |
8384 | ||
f441e717 | 8385 | - (SearchController *) searchController { |
5ec44e34 | 8386 | if (search_ == nil) |
f441e717 | 8387 | search_ = [[SearchController alloc] initWithDatabase:database_]; |
5ec44e34 JF |
8388 | return search_; |
8389 | } | |
8390 | ||
f441e717 | 8391 | - (SourceTable *) sourcesController { |
9ae52960 GP |
8392 | if (sources_ == nil) |
8393 | sources_ = [[SourceTable alloc] initWithDatabase:database_]; | |
8394 | return sources_; | |
8395 | } | |
8396 | ||
f441e717 | 8397 | - (InstalledController *) installedController { |
9ae52960 | 8398 | if (installed_ == nil) { |
f441e717 | 8399 | installed_ = [[InstalledController alloc] initWithDatabase:database_]; |
96291f72 JF |
8400 | if (IsWildcat_) |
8401 | queueDelegate_ = installed_; | |
9ae52960 GP |
8402 | } |
8403 | return installed_; | |
8404 | } | |
8405 | ||
18884e36 | 8406 | - (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { |
9ae52960 | 8407 | int tag = [[viewController tabBarItem] tag]; |
97959670 | 8408 | if (tag == tag_) { |
1e4922b8 | 8409 | [(CYNavigationController *)[tabbar_ selectedViewController] popToRootViewControllerAnimated:YES]; |
97959670 | 8410 | return; |
9ae52960 | 8411 | } else if (tag_ == 1) { |
f441e717 | 8412 | [[self sectionsController] resetView]; |
9ae52960 | 8413 | } |
a75f53e7 | 8414 | |
7e986211 | 8415 | switch (tag) { |
9ae52960 | 8416 | case kCydiaTag: _setHomePage(self); break; |
e057ec05 | 8417 | |
f441e717 GP |
8418 | case kSectionsTag: [self setPage:[self sectionsController]]; break; |
8419 | case kChangesTag: [self setPage:[self changesController]]; break; | |
8420 | case kManageTag: [self setPage:[self manageController]]; break; | |
8421 | case kInstalledTag: [self setPage:[self installedController]]; break; | |
8422 | case kSourcesTag: [self setPage:[self sourcesController]]; break; | |
8423 | case kSearchTag: [self setPage:[self searchController]]; break; | |
e057ec05 | 8424 | |
6981ccdf | 8425 | _nodefault |
a75f53e7 JF |
8426 | } |
8427 | ||
7e986211 | 8428 | tag_ = tag; |
7e986211 JF |
8429 | } |
8430 | ||
c1edf105 | 8431 | - (void) showSettings { |
a591888a JF |
8432 | RoleController *role = [[[RoleController alloc] initWithDatabase:database_ delegate:self] autorelease]; |
8433 | CYNavigationController *nav = [[[CYNavigationController alloc] initWithRootViewController:role] autorelease]; | |
96291f72 JF |
8434 | if (IsWildcat_) |
8435 | [nav setModalPresentationStyle:UIModalPresentationFormSheet]; | |
c1edf105 | 8436 | [container_ presentModalViewController:nav animated:YES]; |
faf4eb4f JF |
8437 | } |
8438 | ||
f441e717 | 8439 | - (void) setPackageController:(PackageController *)view { |
b452841e JF |
8440 | WebThreadLock(); |
8441 | [view setPackage:nil]; | |
b452841e JF |
8442 | WebThreadUnlock(); |
8443 | } | |
8444 | ||
f441e717 GP |
8445 | - (PackageController *) _packageController { |
8446 | return [[[PackageController alloc] initWithDatabase:database_] autorelease]; | |
6932575e JF |
8447 | } |
8448 | ||
f441e717 | 8449 | - (PackageController *) packageController { |
f441e717 | 8450 | return [self _packageController]; |
6932575e JF |
8451 | } |
8452 | ||
19044e4f GP |
8453 | // Returns the navigation controller for the queuing badge. |
8454 | - (id) queueBadgeController { | |
8455 | int index = [self indexOfTabWithTag:kManageTag]; | |
96291f72 JF |
8456 | if (index == -1) |
8457 | index = [self indexOfTabWithTag:kInstalledTag]; | |
283fc596 | 8458 | |
19044e4f GP |
8459 | return [[tabbar_ viewControllers] objectAtIndex:index]; |
8460 | } | |
8461 | ||
1ca35d78 GP |
8462 | - (void) cancelAndClear:(bool)clear { |
8463 | @synchronized (self) { | |
8464 | if (clear) { | |
30746bd9 | 8465 | [database_ clear]; |
9ae52960 | 8466 | |
283fc596 | 8467 | // Stop queuing. |
9ae52960 | 8468 | Queuing_ = false; |
19044e4f | 8469 | [[[self queueBadgeController] tabBarItem] setBadgeValue:nil]; |
1ca35d78 | 8470 | } else { |
283fc596 | 8471 | // Start queuing. |
9ae52960 | 8472 | Queuing_ = true; |
19044e4f | 8473 | [[[self queueBadgeController] tabBarItem] setBadgeValue:UCLocalize("Q_D")]; |
283fc596 JF |
8474 | } |
8475 | ||
41c6e6d1 | 8476 | [self _updateData]; |
19044e4f | 8477 | [queueDelegate_ queueStatusDidChange]; |
1ca35d78 | 8478 | } |
7d3e75f4 | 8479 | } |
faf4eb4f | 8480 | |
9ae52960 GP |
8481 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
8482 | NSString *context([alert context]); | |
f99f86e2 | 8483 | |
9ae52960 GP |
8484 | if ([context isEqualToString:@"fixhalf"]) { |
8485 | if (button == [alert firstOtherButtonIndex]) { | |
7d3e75f4 GP |
8486 | @synchronized (self) { |
8487 | for (Package *broken in broken_) { | |
8488 | [broken remove]; | |
8489 | ||
8490 | NSString *id = [broken id]; | |
8491 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.prerm", id] UTF8String]); | |
8492 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postrm", id] UTF8String]); | |
8493 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.preinst", id] UTF8String]); | |
8494 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postinst", id] UTF8String]); | |
8495 | } | |
faf4eb4f | 8496 | |
7d3e75f4 GP |
8497 | [self resolve]; |
8498 | [self perform]; | |
8499 | } | |
9ae52960 | 8500 | } else if (button == [alert cancelButtonIndex]) { |
7d3e75f4 GP |
8501 | [broken_ removeAllObjects]; |
8502 | [self _loaded]; | |
faf4eb4f JF |
8503 | } |
8504 | ||
7d3e75f4 | 8505 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
a5dd312c | 8506 | } else if ([context isEqualToString:@"upgrade"]) { |
7d3e75f4 GP |
8507 | if (button == [alert firstOtherButtonIndex]) { |
8508 | @synchronized (self) { | |
8509 | for (Package *essential in essential_) | |
8510 | [essential install]; | |
faf4eb4f | 8511 | |
7d3e75f4 GP |
8512 | [self resolve]; |
8513 | [self perform]; | |
8514 | } | |
8515 | } else if (button == [alert firstOtherButtonIndex] + 1) { | |
8516 | [self distUpgrade]; | |
8517 | } else if (button == [alert cancelButtonIndex]) { | |
8518 | Ignored_ = YES; | |
faf4eb4f JF |
8519 | } |
8520 | ||
7d3e75f4 | 8521 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
a5dd312c | 8522 | } |
faf4eb4f JF |
8523 | } |
8524 | ||
6981ccdf JF |
8525 | - (void) system:(NSString *)command { _pooled |
8526 | system([command UTF8String]); | |
8527 | } | |
8528 | ||
8529 | - (void) applicationWillSuspend { | |
8530 | [database_ clean]; | |
8531 | [super applicationWillSuspend]; | |
b26eb97d JF |
8532 | } |
8533 | ||
832c0799 | 8534 | - (BOOL) hudIsShowing { |
114ac994 | 8535 | return (hudcount_ > 0); |
832c0799 GP |
8536 | } |
8537 | ||
b26eb97d | 8538 | - (void) applicationSuspend:(__GSEvent *)event { |
ea0e9eb1 GP |
8539 | // Use external process status API internally. |
8540 | // This is probably a really bad idea. | |
8541 | uint64_t status = 0; | |
8542 | int notify_token; | |
8543 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
8544 | notify_get_state(notify_token, &status); | |
8545 | notify_cancel(notify_token); | |
8546 | } | |
8547 | ||
832c0799 | 8548 | if (![self hudIsShowing] && status == 0) |
b26eb97d | 8549 | [super applicationSuspend:event]; |
b26eb97d JF |
8550 | } |
8551 | ||
fa7bb92f | 8552 | - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 { |
832c0799 | 8553 | if (![self hudIsShowing]) |
fa7bb92f JF |
8554 | [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3]; |
8555 | } | |
8556 | ||
8557 | - (void) _setSuspended:(BOOL)value { | |
832c0799 | 8558 | if (![self hudIsShowing]) |
fa7bb92f JF |
8559 | [super _setSuspended:value]; |
8560 | } | |
8561 | ||
faf4eb4f | 8562 | - (UIProgressHUD *) addProgressHUD { |
7398a389 | 8563 | UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window_] autorelease]); |
d3bef7bc JF |
8564 | [hud setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8565 | ||
7398a389 | 8566 | [window_ setUserInteractionEnabled:NO]; |
faf4eb4f | 8567 | [hud show:YES]; |
12e0ef8f GP |
8568 | |
8569 | UIViewController *target = container_; | |
8570 | while ([target modalViewController] != nil) target = [target modalViewController]; | |
8571 | [[target view] addSubview:hud]; | |
8572 | ||
114ac994 | 8573 | hudcount_++; |
faf4eb4f JF |
8574 | return hud; |
8575 | } | |
8576 | ||
7398a389 JF |
8577 | - (void) removeProgressHUD:(UIProgressHUD *)hud { |
8578 | [hud show:NO]; | |
8579 | [hud removeFromSuperview]; | |
8580 | [window_ setUserInteractionEnabled:YES]; | |
114ac994 | 8581 | hudcount_--; |
7398a389 JF |
8582 | } |
8583 | ||
881fe77f | 8584 | - (CYViewController *) pageForPackage:(NSString *)name { |
cb9c2100 | 8585 | if (Package *package = [database_ packageWithName:name]) { |
f441e717 | 8586 | PackageController *view([self packageController]); |
cb9c2100 JF |
8587 | [view setPackage:package]; |
8588 | return view; | |
8589 | } else { | |
6981ccdf JF |
8590 | NSURL *url([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"unknown" ofType:@"html"]]); |
8591 | url = [NSURL URLWithString:[[url absoluteString] stringByAppendingString:[NSString stringWithFormat:@"?%@", name]]]; | |
9ae52960 | 8592 | return [self _pageForURL:url withClass:[CYBrowserController class]]; |
cb9c2100 JF |
8593 | } |
8594 | } | |
8595 | ||
881fe77f | 8596 | - (CYViewController *) pageForURL:(NSURL *)url hasTag:(int *)tag { |
cb9c2100 | 8597 | if (tag != NULL) |
84848968 | 8598 | *tag = -1; |
cb9c2100 | 8599 | |
61b13cae JF |
8600 | NSString *href([url absoluteString]); |
8601 | if ([href hasPrefix:@"apptapp://package/"]) | |
8602 | return [self pageForPackage:[href substringFromIndex:18]]; | |
8603 | ||
2dfc46f7 JF |
8604 | NSString *scheme([[url scheme] lowercaseString]); |
8605 | if (![scheme isEqualToString:@"cydia"]) | |
8606 | return nil; | |
8607 | NSString *path([url absoluteString]); | |
8608 | if ([path length] < 8) | |
8609 | return nil; | |
8610 | path = [path substringFromIndex:8]; | |
8611 | if (![path hasPrefix:@"/"]) | |
8612 | path = [@"/" stringByAppendingString:path]; | |
8613 | ||
8614 | if ([path isEqualToString:@"/add-source"]) | |
f441e717 | 8615 | return [[[AddSourceController alloc] initWithDatabase:database_] autorelease]; |
2dfc46f7 | 8616 | else if ([path isEqualToString:@"/storage"]) |
9ae52960 | 8617 | return [self _pageForURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"storage" ofType:@"html"]] withClass:[CYBrowserController class]]; |
2dfc46f7 | 8618 | else if ([path isEqualToString:@"/sources"]) |
9ae52960 | 8619 | return [[[SourceTable alloc] initWithDatabase:database_] autorelease]; |
2dfc46f7 | 8620 | else if ([path isEqualToString:@"/packages"]) |
f441e717 | 8621 | return [[[InstalledController alloc] initWithDatabase:database_] autorelease]; |
2dfc46f7 | 8622 | else if ([path hasPrefix:@"/url/"]) |
9ae52960 | 8623 | return [self _pageForURL:[NSURL URLWithString:[path substringFromIndex:5]] withClass:[CYBrowserController class]]; |
2dfc46f7 JF |
8624 | else if ([path hasPrefix:@"/launch/"]) |
8625 | [self launchApplicationWithIdentifier:[path substringFromIndex:8] suspended:NO]; | |
8626 | else if ([path hasPrefix:@"/package-settings/"]) | |
f441e717 | 8627 | return [[[SettingsController alloc] initWithDatabase:database_ package:[path substringFromIndex:18]] autorelease]; |
2dfc46f7 | 8628 | else if ([path hasPrefix:@"/package-signature/"]) |
f441e717 | 8629 | return [[[SignatureController alloc] initWithDatabase:database_ package:[path substringFromIndex:19]] autorelease]; |
2dfc46f7 JF |
8630 | else if ([path hasPrefix:@"/package/"]) |
8631 | return [self pageForPackage:[path substringFromIndex:9]]; | |
8632 | else if ([path hasPrefix:@"/files/"]) { | |
8633 | NSString *name = [path substringFromIndex:7]; | |
cb9c2100 JF |
8634 | |
8635 | if (Package *package = [database_ packageWithName:name]) { | |
9ae52960 | 8636 | FileTable *files = [[[FileTable alloc] initWithDatabase:database_] autorelease]; |
cb9c2100 JF |
8637 | [files setPackage:package]; |
8638 | return files; | |
8639 | } | |
8640 | } | |
8641 | ||
8642 | return nil; | |
8643 | } | |
8644 | ||
114ac994 GP |
8645 | - (BOOL) openCydiaURL:(NSURL *)url { |
8646 | CYViewController *page = nil; | |
8647 | int tag = 0; | |
8648 | ||
a8d90389 GP |
8649 | NSLog(@"open url: %@", url); |
8650 | ||
8651 | if ((page = [self pageForURL:url hasTag:&tag])) { | |
114ac994 GP |
8652 | [self setPage:page]; |
8653 | tag_ = tag; | |
8654 | [tabbar_ setSelectedViewController:(tag_ == -1 ? nil : [[tabbar_ viewControllers] objectAtIndex:tag_])]; | |
8655 | } | |
8656 | ||
8657 | return !!page; | |
8658 | } | |
8659 | ||
cb9c2100 JF |
8660 | - (void) applicationOpenURL:(NSURL *)url { |
8661 | [super applicationOpenURL:url]; | |
a8d90389 | 8662 | NSLog(@"first: %@", url); |
114ac994 GP |
8663 | if (!loaded_) starturl_ = [url retain]; |
8664 | else [self openCydiaURL:url]; | |
cb9c2100 JF |
8665 | } |
8666 | ||
f99f86e2 | 8667 | - (void) applicationWillResignActive:(UIApplication *)application { |
57e5156d | 8668 | // Stop refreshing if you get a phone call or lock the device. |
96291f72 JF |
8669 | if ([container_ updating]) |
8670 | [container_ cancelUpdate]; | |
f99f86e2 | 8671 | |
0a6531c7 GP |
8672 | if ([[self superclass] instancesRespondToSelector:@selector(applicationWillResignActive:)]) |
8673 | [super applicationWillResignActive:application]; | |
57e5156d GP |
8674 | } |
8675 | ||
4cd1145e GP |
8676 | - (void) addStashController { |
8677 | stash_ = [[CYStashController alloc] init]; | |
8678 | [window_ addSubview:[stash_ view]]; | |
8679 | } | |
8680 | ||
8681 | - (void) removeStashController { | |
8682 | [[stash_ view] removeFromSuperview]; | |
8683 | [stash_ release]; | |
8684 | } | |
8685 | ||
8686 | - (void) stash { | |
8687 | [self setIdleTimerDisabled:YES]; | |
8688 | ||
a8f3b357 | 8689 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; |
4cd1145e GP |
8690 | [self setStatusBarShowsProgress:YES]; |
8691 | UpdateExternalStatus(1); | |
8692 | ||
8693 | [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/free.sh"]; | |
8694 | ||
8695 | UpdateExternalStatus(0); | |
8696 | [self setStatusBarShowsProgress:NO]; | |
8697 | ||
8698 | [self removeStashController]; | |
8699 | ||
8700 | if (ExecFork() == 0) { | |
8701 | execlp("launchctl", "launchctl", "stop", "com.apple.SpringBoard", NULL); | |
8702 | perror("launchctl stop"); | |
8703 | } | |
8704 | } | |
8705 | ||
7fc6909e GP |
8706 | - (void) setupTabBarController { |
8707 | tabbar_ = [[CYTabBarController alloc] initWithDatabase:database_]; | |
8708 | [tabbar_ setDelegate:self]; | |
8709 | ||
8710 | NSMutableArray *items([NSMutableArray arrayWithObjects: | |
8711 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage applicationImageNamed:@"home.png"] tag:kCydiaTag] autorelease], | |
8712 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SECTIONS") image:[UIImage applicationImageNamed:@"install.png"] tag:kSectionsTag] autorelease], | |
8713 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage applicationImageNamed:@"changes.png"] tag:kChangesTag] autorelease], | |
8714 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage applicationImageNamed:@"search.png"] tag:kSearchTag] autorelease], | |
8715 | nil]); | |
8716 | ||
8717 | if (IsWildcat_) { | |
8718 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage applicationImageNamed:@"source.png"] tag:kSourcesTag] autorelease] atIndex:3]; | |
8719 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage applicationImageNamed:@"manage.png"] tag:kInstalledTag] autorelease] atIndex:3]; | |
8720 | } else { | |
8721 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("MANAGE") image:[UIImage applicationImageNamed:@"manage.png"] tag:kManageTag] autorelease] atIndex:3]; | |
8722 | } | |
8723 | ||
8724 | NSMutableArray *controllers([NSMutableArray array]); | |
8725 | ||
8726 | for (UITabBarItem *item in items) { | |
8727 | CYNavigationController *controller([[[CYNavigationController alloc] initWithDatabase:database_] autorelease]); | |
8728 | [controller setTabBarItem:item]; | |
8729 | [controllers addObject:controller]; | |
8730 | } | |
8731 | ||
8732 | [tabbar_ setViewControllers:controllers]; | |
7fc6909e GP |
8733 | } |
8734 | ||
b26eb97d | 8735 | - (void) applicationDidFinishLaunching:(id)unused { |
9ae52960 | 8736 | [CYBrowserController _initialize]; |
4825688a | 8737 | |
017b2b71 JF |
8738 | [NSURLProtocol registerClass:[CydiaURLProtocol class]]; |
8739 | ||
5e563e79 JF |
8740 | Font12_ = [[UIFont systemFontOfSize:12] retain]; |
8741 | Font12Bold_ = [[UIFont boldSystemFontOfSize:12] retain]; | |
8742 | Font14_ = [[UIFont systemFontOfSize:14] retain]; | |
8743 | Font18Bold_ = [[UIFont boldSystemFontOfSize:18] retain]; | |
8744 | Font22Bold_ = [[UIFont boldSystemFontOfSize:22] retain]; | |
8745 | ||
84848968 | 8746 | tag_ = 0; |
b26eb97d JF |
8747 | |
8748 | essential_ = [[NSMutableArray alloc] initWithCapacity:4]; | |
8749 | broken_ = [[NSMutableArray alloc] initWithCapacity:4]; | |
8750 | ||
d3bef7bc JF |
8751 | UIScreen *screen([UIScreen mainScreen]); |
8752 | ||
8753 | window_ = [[UIWindow alloc] initWithFrame:[screen bounds]]; | |
5e563e79 JF |
8754 | [window_ orderFront:self]; |
8755 | [window_ makeKey:self]; | |
cb9c2100 | 8756 | [window_ setHidden:NO]; |
d3bef7bc | 8757 | |
4cd1145e GP |
8758 | if ( |
8759 | readlink("/Applications", NULL, 0) == -1 && errno == EINVAL || | |
8760 | readlink("/Library/Ringtones", NULL, 0) == -1 && errno == EINVAL || | |
8761 | readlink("/Library/Wallpaper", NULL, 0) == -1 && errno == EINVAL || | |
8762 | //readlink("/usr/bin", NULL, 0) == -1 && errno == EINVAL || | |
8763 | readlink("/usr/include", NULL, 0) == -1 && errno == EINVAL || | |
8764 | readlink("/usr/lib/pam", NULL, 0) == -1 && errno == EINVAL || | |
8765 | readlink("/usr/libexec", NULL, 0) == -1 && errno == EINVAL || | |
8766 | readlink("/usr/share", NULL, 0) == -1 && errno == EINVAL || | |
8767 | //readlink("/var/lib", NULL, 0) == -1 && errno == EINVAL || | |
8768 | false | |
8769 | ) { | |
8770 | [self addStashController]; | |
a591888a JF |
8771 | // XXX: this would be much cleaner as a yieldToSelector: |
8772 | // that way the removeStashController could happen right here inline | |
8773 | // we also could no longer require the useless stash_ field anymore | |
4cd1145e GP |
8774 | [self performSelector:@selector(stash) withObject:nil afterDelay:0]; |
8775 | return; | |
8776 | } | |
8777 | ||
dbe0f181 | 8778 | database_ = [Database sharedInstance]; |
017b2b71 | 8779 | |
7fc6909e | 8780 | [self setupTabBarController]; |
e7a88a8c GP |
8781 | |
8782 | container_ = [[CYContainer alloc] initWithDatabase:database_]; | |
e7a88a8c | 8783 | [container_ setUpdateDelegate:self]; |
1e4922b8 | 8784 | [container_ setTabBarController:tabbar_]; |
e7a88a8c | 8785 | [window_ addSubview:[container_ view]]; |
6981ccdf | 8786 | |
525bc190 | 8787 | // Show pinstripes while loading data. |
c961857b | 8788 | [[container_ view] setBackgroundColor:[UIColor pinStripeColor]]; |
525bc190 | 8789 | |
90e9a238 | 8790 | [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; |
d11754e5 | 8791 | _trace(); |
90e9a238 JF |
8792 | } |
8793 | ||
8794 | - (void) loadData { | |
d11754e5 | 8795 | _trace(); |
90e9a238 JF |
8796 | if (Role_ == nil) { |
8797 | [self showSettings]; | |
8798 | return; | |
8799 | } | |
8800 | ||
7fc6909e | 8801 | [window_ setUserInteractionEnabled:NO]; |
6981ccdf | 8802 | |
11f75d4f | 8803 | UIView *container = [[[UIView alloc] init] autorelease]; |
7fc6909e GP |
8804 | [container setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin]; |
8805 | ||
11f75d4f | 8806 | UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease]; |
7fc6909e GP |
8807 | [spinner startAnimating]; |
8808 | [container addSubview:spinner]; | |
7fc6909e | 8809 | |
11f75d4f | 8810 | UILabel *label = [[[UILabel alloc] init] autorelease]; |
7fc6909e GP |
8811 | [label setFont:[UIFont boldSystemFontOfSize:15.0f]]; |
8812 | [label setBackgroundColor:[UIColor clearColor]]; | |
8813 | [label setTextColor:[UIColor blackColor]]; | |
8814 | [label setShadowColor:[UIColor whiteColor]]; | |
8815 | [label setShadowOffset:CGSizeMake(0, 1)]; | |
835b451e | 8816 | [label setText:[NSString stringWithFormat:Elision_, UCLocalize("LOADING"), nil]]; |
7fc6909e | 8817 | [container addSubview:label]; |
7fc6909e GP |
8818 | |
8819 | CGSize viewsize = [[tabbar_ view] frame].size; | |
8820 | CGSize spinnersize = [spinner bounds].size; | |
8821 | CGSize textsize = [[label text] sizeWithFont:[label font]]; | |
8822 | float bothwidth = spinnersize.width + textsize.width + 5.0f; | |
8823 | ||
8824 | CGRect containrect = { | |
8825 | CGPointMake(floorf((viewsize.width / 2) - (bothwidth / 2)), floorf((viewsize.height / 2) - (spinnersize.height / 2))), | |
8826 | CGSizeMake(bothwidth, spinnersize.height) | |
8827 | }; | |
8828 | CGRect textrect = { | |
8829 | CGPointMake(spinnersize.width + 5.0f, floorf((spinnersize.height / 2) - (textsize.height / 2))), | |
8830 | textsize | |
8831 | }; | |
8832 | CGRect spinrect = { | |
8833 | CGPointZero, | |
8834 | spinnersize | |
8835 | }; | |
8836 | ||
8837 | [container setFrame:containrect]; | |
8838 | [spinner setFrame:spinrect]; | |
8839 | [label setFrame:textrect]; | |
8840 | [[container_ view] addSubview:container]; | |
7fc6909e GP |
8841 | |
8842 | [self reloadData]; | |
6981ccdf JF |
8843 | PrintTimes(); |
8844 | ||
4a3efc29 | 8845 | // Show the initial page |
114ac994 | 8846 | if (starturl_ == nil || ![self openCydiaURL:starturl_]) { |
4a3efc29 GP |
8847 | [tabbar_ setSelectedIndex:0]; |
8848 | _setHomePage(self); | |
8849 | } | |
8850 | ||
a8d90389 GP |
8851 | [starturl_ release]; |
8852 | starturl_ = nil; | |
8853 | ||
7fc6909e | 8854 | [window_ setUserInteractionEnabled:YES]; |
525bc190 GP |
8855 | |
8856 | // XXX: does this actually slow anything down? | |
8857 | [[container_ view] setBackgroundColor:[UIColor clearColor]]; | |
7fc6909e | 8858 | [container removeFromSuperview]; |
b26eb97d JF |
8859 | } |
8860 | ||
9ae52960 | 8861 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { |
1ca35d78 | 8862 | if (item != nil && IsWildcat_) { |
9ae52960 | 8863 | [sheet showFromBarButtonItem:item animated:YES]; |
1ca35d78 GP |
8864 | } else { |
8865 | [sheet showInView:window_]; | |
8866 | } | |
e057ec05 JF |
8867 | } |
8868 | ||
a75f53e7 JF |
8869 | @end |
8870 | ||
686e302f JF |
8871 | /*IMP alloc_; |
8872 | id Alloc_(id self, SEL selector) { | |
8873 | id object = alloc_(self, selector); | |
cb9c2100 | 8874 | lprintf("[%s]A-%p\n", self->isa->name, object); |
686e302f JF |
8875 | return object; |
8876 | }*/ | |
8877 | ||
e057ec05 JF |
8878 | /*IMP dealloc_; |
8879 | id Dealloc_(id self, SEL selector) { | |
8880 | id object = dealloc_(self, selector); | |
cb9c2100 | 8881 | lprintf("[%s]D-%p\n", self->isa->name, object); |
e057ec05 JF |
8882 | return object; |
8883 | }*/ | |
686e302f | 8884 | |
6932575e JF |
8885 | Class $WebDefaultUIKitDelegate; |
8886 | ||
c59881cd | 8887 | MSHook(void, UIWebDocumentView$_setUIKitDelegate$, UIWebDocumentView *self, SEL _cmd, id delegate) { |
6932575e JF |
8888 | if (delegate == nil && $WebDefaultUIKitDelegate != nil) |
8889 | delegate = [$WebDefaultUIKitDelegate sharedUIKitDelegate]; | |
c59881cd | 8890 | return _UIWebDocumentView$_setUIKitDelegate$(self, _cmd, delegate); |
6932575e JF |
8891 | } |
8892 | ||
aaab313d RP |
8893 | static NSNumber *shouldPlayKeyboardSounds; |
8894 | ||
8895 | Class $UIHardware; | |
8896 | ||
d5f224e7 JF |
8897 | MSHook(void, UIHardware$_playSystemSound$, Class self, SEL _cmd, int sound) { |
8898 | switch (sound) { | |
aaab313d RP |
8899 | case 1104: // Keyboard Button Clicked |
8900 | case 1105: // Keyboard Delete Repeated | |
d5f224e7 JF |
8901 | if (shouldPlayKeyboardSounds == nil) { |
8902 | NSDictionary *dict([[[NSDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.apple.preferences.sounds.plist"] autorelease]); | |
8903 | shouldPlayKeyboardSounds = [([dict objectForKey:@"keyboard"] ?: (id) kCFBooleanTrue) retain]; | |
aaab313d | 8904 | } |
d5f224e7 | 8905 | |
aaab313d RP |
8906 | if (![shouldPlayKeyboardSounds boolValue]) |
8907 | break; | |
d5f224e7 | 8908 | |
aaab313d | 8909 | default: |
d5f224e7 | 8910 | _UIHardware$_playSystemSound$(self, _cmd, sound); |
aaab313d RP |
8911 | } |
8912 | } | |
8913 | ||
f464053e | 8914 | int main(int argc, char *argv[]) { _pooled |
7ec29c77 | 8915 | _trace(); |
6932575e | 8916 | |
5ec44e34 JF |
8917 | if (Class $UIDevice = objc_getClass("UIDevice")) { |
8918 | UIDevice *device([$UIDevice currentDevice]); | |
8919 | IsWildcat_ = [device respondsToSelector:@selector(isWildcat)] && [device isWildcat]; | |
8920 | } else | |
8921 | IsWildcat_ = false; | |
8922 | ||
dd9390c5 | 8923 | PackageName = reinterpret_cast<CYString &(*)(Package *, SEL)>(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); |
43b742af | 8924 | |
3e3977a2 JF |
8925 | /* Library Hacks {{{ */ |
8926 | class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); | |
8927 | ||
8928 | $WebDefaultUIKitDelegate = objc_getClass("WebDefaultUIKitDelegate"); | |
8929 | Method UIWebDocumentView$_setUIKitDelegate$(class_getInstanceMethod([WebView class], @selector(_setUIKitDelegate:))); | |
8930 | if (UIWebDocumentView$_setUIKitDelegate$ != NULL) { | |
8931 | _UIWebDocumentView$_setUIKitDelegate$ = reinterpret_cast<void (*)(UIWebDocumentView *, SEL, id)>(method_getImplementation(UIWebDocumentView$_setUIKitDelegate$)); | |
8932 | method_setImplementation(UIWebDocumentView$_setUIKitDelegate$, reinterpret_cast<IMP>(&$UIWebDocumentView$_setUIKitDelegate$)); | |
8933 | } | |
aaab313d RP |
8934 | |
8935 | $UIHardware = objc_getClass("UIHardware"); | |
8936 | Method UIHardware$_playSystemSound$(class_getClassMethod($UIHardware, @selector(_playSystemSound:))); | |
8937 | if (UIHardware$_playSystemSound$ != NULL) { | |
8938 | _UIHardware$_playSystemSound$ = reinterpret_cast<void (*)(Class, SEL, int)>(method_getImplementation(UIHardware$_playSystemSound$)); | |
8939 | method_setImplementation(UIHardware$_playSystemSound$, reinterpret_cast<IMP>(&$UIHardware$_playSystemSound$)); | |
8940 | } | |
3e3977a2 JF |
8941 | /* }}} */ |
8942 | /* Set Locale {{{ */ | |
6932575e | 8943 | Locale_ = CFLocaleCopyCurrent(); |
bb9edf8b JF |
8944 | Languages_ = [NSLocale preferredLanguages]; |
8945 | //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); | |
f7ee98cb | 8946 | //NSLog(@"%@", [Languages_ description]); |
85e0f0f0 | 8947 | |
bb9edf8b JF |
8948 | const char *lang; |
8949 | if (Languages_ == nil || [Languages_ count] == 0) | |
85e0f0f0 | 8950 | // XXX: consider just setting to C and then falling through? |
bb9edf8b | 8951 | lang = NULL; |
85e0f0f0 | 8952 | else { |
bb9edf8b | 8953 | lang = [[Languages_ objectAtIndex:0] UTF8String]; |
85e0f0f0 JF |
8954 | setenv("LANG", lang, true); |
8955 | } | |
8956 | ||
bb9edf8b JF |
8957 | //std::setlocale(LC_ALL, lang); |
8958 | NSLog(@"Setting Language: %s", lang); | |
3e3977a2 | 8959 | /* }}} */ |
6932575e | 8960 | |
c59881cd | 8961 | apr_app_initialize(&argc, const_cast<const char * const **>(&argv), NULL); |
6932575e | 8962 | |
3e3977a2 | 8963 | /* Parse Arguments {{{ */ |
64da7a2f JF |
8964 | bool substrate(false); |
8965 | ||
8966 | if (argc != 0) { | |
8967 | char **args(argv); | |
8968 | int arge(1); | |
8969 | ||
8970 | for (int argi(1); argi != argc; ++argi) | |
8971 | if (strcmp(argv[argi], "--") == 0) { | |
8972 | arge = argi; | |
8973 | argv[argi] = argv[0]; | |
8974 | argv += argi; | |
8975 | argc -= argi; | |
8976 | break; | |
8977 | } | |
8978 | ||
8979 | for (int argi(1); argi != arge; ++argi) | |
c59881cd | 8980 | if (strcmp(args[argi], "--substrate") == 0) |
64da7a2f JF |
8981 | substrate = true; |
8982 | else | |
8983 | fprintf(stderr, "unknown argument: %s\n", args[argi]); | |
8984 | } | |
3e3977a2 | 8985 | /* }}} */ |
0039464f | 8986 | |
3e3977a2 JF |
8987 | App_ = [[NSBundle mainBundle] bundlePath]; |
8988 | Home_ = NSHomeDirectory(); | |
c59881cd | 8989 | Advanced_ = YES; |
3e3977a2 | 8990 | |
686e302f JF |
8991 | setuid(0); |
8992 | setgid(0); | |
8993 | ||
8994 | /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); | |
8995 | alloc_ = alloc->method_imp; | |
8996 | alloc->method_imp = (IMP) &Alloc_;*/ | |
8997 | ||
e057ec05 JF |
8998 | /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc)); |
8999 | dealloc_ = dealloc->method_imp; | |
9000 | dealloc->method_imp = (IMP) &Dealloc_;*/ | |
9001 | ||
c59881cd | 9002 | /* System Information {{{ */ |
2d28b35a | 9003 | size_t size; |
cb9c2100 JF |
9004 | |
9005 | int maxproc; | |
9006 | size = sizeof(maxproc); | |
9007 | if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) | |
9008 | perror("sysctlbyname(\"kern.maxproc\", ?)"); | |
9009 | else if (maxproc < 64) { | |
9010 | maxproc = 64; | |
9011 | if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) | |
9012 | perror("sysctlbyname(\"kern.maxproc\", #)"); | |
9013 | } | |
9014 | ||
017b2b71 JF |
9015 | sysctlbyname("kern.osversion", NULL, &size, NULL, 0); |
9016 | char *osversion = new char[size]; | |
9017 | if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1) | |
9018 | perror("sysctlbyname(\"kern.osversion\", ?)"); | |
9019 | else | |
9020 | System_ = [NSString stringWithUTF8String:osversion]; | |
9021 | ||
2d28b35a JF |
9022 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
9023 | char *machine = new char[size]; | |
cb9c2100 JF |
9024 | if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1) |
9025 | perror("sysctlbyname(\"hw.machine\", ?)"); | |
9026 | else | |
9027 | Machine_ = machine; | |
2d28b35a | 9028 | |
6188cfdd JF |
9029 | if (CFMutableDictionaryRef dict = IOServiceMatching("IOPlatformExpertDevice")) { |
9030 | if (io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, dict)) { | |
9031 | if (CFTypeRef serial = IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0)) { | |
017b2b71 | 9032 | SerialNumber_ = [NSString stringWithString:(NSString *)serial]; |
6188cfdd JF |
9033 | CFRelease(serial); |
9034 | } | |
9035 | ||
9036 | if (CFTypeRef ecid = IORegistryEntrySearchCFProperty(service, kIODeviceTreePlane, CFSTR("unique-chip-id"), kCFAllocatorDefault, kIORegistryIterateRecursively)) { | |
9037 | NSData *data((NSData *) ecid); | |
9038 | size_t length([data length]); | |
9039 | uint8_t bytes[length]; | |
9040 | [data getBytes:bytes]; | |
9041 | char string[length * 2 + 1]; | |
9042 | for (size_t i(0); i != length; ++i) | |
9043 | sprintf(string + i * 2, "%.2X", bytes[length - i - 1]); | |
017b2b71 | 9044 | ChipID_ = [NSString stringWithUTF8String:string]; |
6188cfdd JF |
9045 | CFRelease(ecid); |
9046 | } | |
9047 | ||
9048 | IOObjectRelease(service); | |
9049 | } | |
9050 | } | |
9051 | ||
2cb68ddf | 9052 | UniqueID_ = [[UIDevice currentDevice] uniqueIdentifier]; |
2d28b35a | 9053 | |
3074466a JF |
9054 | CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef); |
9055 | $CTSIMSupportCopyMobileSubscriberCountryCode = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode")); | |
9056 | CFStringRef mcc($CTSIMSupportCopyMobileSubscriberCountryCode == NULL ? NULL : (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault)); | |
9057 | ||
9058 | CFStringRef (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(CFAllocatorRef); | |
9059 | $CTSIMSupportCopyMobileSubscriberNetworkCode = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode")); | |
9060 | CFStringRef mnc($CTSIMSupportCopyMobileSubscriberNetworkCode == NULL ? NULL : (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(kCFAllocatorDefault)); | |
9061 | ||
9062 | if (mcc != NULL && mnc != NULL) | |
9063 | PLMN_ = [NSString stringWithFormat:@"%@%@", mcc, mnc]; | |
9064 | ||
9065 | if (mnc != NULL) | |
9066 | CFRelease(mnc); | |
9067 | if (mcc != NULL) | |
9068 | CFRelease(mcc); | |
9069 | ||
68c05606 JF |
9070 | if (NSDictionary *system = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]) |
9071 | Build_ = [system objectForKey:@"ProductBuildVersion"]; | |
7b00c562 JF |
9072 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) { |
9073 | Product_ = [info objectForKey:@"SafariProductVersion"]; | |
9074 | Safari_ = [info objectForKey:@"CFBundleVersion"]; | |
9075 | } | |
c59881cd | 9076 | /* }}} */ |
3e3977a2 | 9077 | /* Load Database {{{ */ |
7ec29c77 | 9078 | _trace(); |
6932575e JF |
9079 | Metadata_ = [[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]; |
9080 | _trace(); | |
9081 | SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; | |
7ec29c77 JF |
9082 | |
9083 | if (Metadata_ == NULL) | |
6932575e | 9084 | Metadata_ = [NSMutableDictionary dictionaryWithCapacity:2]; |
fa7bb92f | 9085 | else { |
ca45de84 | 9086 | Settings_ = [Metadata_ objectForKey:@"Settings"]; |
faf4eb4f | 9087 | |
686e302f | 9088 | Packages_ = [Metadata_ objectForKey:@"Packages"]; |
fa7bb92f | 9089 | Sections_ = [Metadata_ objectForKey:@"Sections"]; |
faf4eb4f | 9090 | Sources_ = [Metadata_ objectForKey:@"Sources"]; |
37455cf8 JF |
9091 | |
9092 | Token_ = [Metadata_ objectForKey:@"Token"]; | |
faf4eb4f JF |
9093 | } |
9094 | ||
9095 | if (Settings_ != nil) | |
9096 | Role_ = [Settings_ objectForKey:@"Role"]; | |
9097 | ||
faf4eb4f JF |
9098 | if (Sections_ == nil) { |
9099 | Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; | |
9100 | [Metadata_ setObject:Sections_ forKey:@"Sections"]; | |
9101 | } | |
9102 | ||
9103 | if (Sources_ == nil) { | |
9104 | Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; | |
9105 | [Metadata_ setObject:Sources_ forKey:@"Sources"]; | |
fa7bb92f | 9106 | } |
3e3977a2 | 9107 | /* }}} */ |
686e302f | 9108 | |
7a8635c9 JF |
9109 | _trace(); |
9110 | MetaFile_.Open("/var/lib/cydia/metadata.cb0"); | |
9111 | _trace(); | |
9112 | ||
9113 | if (Packages_ != nil) { | |
9114 | CFDictionaryApplyFunction((CFDictionaryRef) Packages_, &PackageImport, NULL); | |
9115 | _trace(); | |
9116 | [Metadata_ removeObjectForKey:@"Packages"]; | |
9117 | Packages_ = nil; | |
9118 | Changed_ = true; | |
9119 | } | |
9120 | ||
c59881cd JF |
9121 | Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; |
9122 | ||
5ec44e34 JF |
9123 | if (substrate && access("/Library/MobileSubstrate/DynamicLibraries/SimulatedKeyEvents.dylib", F_OK) == 0) |
9124 | dlopen("/Library/MobileSubstrate/DynamicLibraries/SimulatedKeyEvents.dylib", RTLD_LAZY | RTLD_GLOBAL); | |
8944281a JF |
9125 | if (substrate && access("/Applications/WinterBoard.app/WinterBoard.dylib", F_OK) == 0) |
9126 | dlopen("/Applications/WinterBoard.app/WinterBoard.dylib", RTLD_LAZY | RTLD_GLOBAL); | |
9127 | /*if (substrate && access("/Library/MobileSubstrate/MobileSubstrate.dylib", F_OK) == 0) | |
9128 | dlopen("/Library/MobileSubstrate/MobileSubstrate.dylib", RTLD_LAZY | RTLD_GLOBAL);*/ | |
1eb0c554 | 9129 | |
5ec44e34 JF |
9130 | int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); |
9131 | ||
17f91215 | 9132 | if (access("/tmp/.cydia.fw", F_OK) == 0) { |
4825688a | 9133 | unlink("/tmp/.cydia.fw"); |
17f91215 | 9134 | goto firmware; |
daf7f6e2 | 9135 | } else if (access("/User", F_OK) != 0 || version < 2) { |
17f91215 | 9136 | firmware: |
7ec29c77 | 9137 | _trace(); |
6a0db335 | 9138 | system("/usr/libexec/cydia/firmware.sh"); |
7ec29c77 JF |
9139 | _trace(); |
9140 | } | |
63a1e4b8 | 9141 | |
2cb68ddf JF |
9142 | _assert([[NSFileManager defaultManager] |
9143 | createDirectoryAtPath:@"/var/cache/apt/archives/partial" | |
9144 | withIntermediateDirectories:YES | |
9145 | attributes:nil | |
9146 | error:NULL | |
9147 | ]); | |
9148 | ||
3e3977a2 JF |
9149 | if (access("/tmp/cydia.chk", F_OK) == 0) { |
9150 | if (unlink("/var/cache/apt/pkgcache.bin") == -1) | |
9151 | _assert(errno == ENOENT); | |
9152 | if (unlink("/var/cache/apt/srcpkgcache.bin") == -1) | |
9153 | _assert(errno == ENOENT); | |
9154 | } | |
9155 | ||
6188cfdd | 9156 | /* APT Initialization {{{ */ |
bb9edf8b JF |
9157 | _assert(pkgInitConfig(*_config)); |
9158 | _assert(pkgInitSystem(*_config, _system)); | |
9159 | ||
9160 | if (lang != NULL) | |
9161 | _config->Set("APT::Acquire::Translation", lang); | |
69caeecb JF |
9162 | |
9163 | // XXX: this timeout might be important :( | |
9164 | //_config->Set("Acquire::http::Timeout", 15); | |
9165 | ||
419a9efd | 9166 | _config->Set("Acquire::http::MaxParallel", 3); |
6188cfdd | 9167 | /* }}} */ |
3e3977a2 | 9168 | /* Color Choices {{{ */ |
e057ec05 JF |
9169 | space_ = CGColorSpaceCreateDeviceRGB(); |
9170 | ||
5e563e79 | 9171 | Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0); |
d72d91aa | 9172 | Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0); |
e057ec05 | 9173 | Black_.Set(space_, 0.0, 0.0, 0.0, 1.0); |
87c76914 | 9174 | Off_.Set(space_, 0.9, 0.9, 0.9, 1.0); |
e057ec05 | 9175 | White_.Set(space_, 1.0, 1.0, 1.0, 1.0); |
faf4eb4f | 9176 | Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0); |
d8d9a65c JF |
9177 | Green_.Set(space_, 0.0, 0.5, 0.0, 1.0); |
9178 | Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0); | |
9179 | Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0); | |
6188cfdd | 9180 | |
3ff1504e JF |
9181 | InstallingColor_ = [UIColor colorWithRed:0.88f green:1.00f blue:0.88f alpha:1.00f]; |
9182 | RemovingColor_ = [UIColor colorWithRed:1.00f green:0.88f blue:0.88f alpha:1.00f]; | |
3e3977a2 | 9183 | /* }}}*/ |
3e3977a2 | 9184 | /* UIKit Configuration {{{ */ |
6932575e JF |
9185 | void (*$GSFontSetUseLegacyFontMetrics)(BOOL)(reinterpret_cast<void (*)(BOOL)>(dlsym(RTLD_DEFAULT, "GSFontSetUseLegacyFontMetrics"))); |
9186 | if ($GSFontSetUseLegacyFontMetrics != NULL) | |
9187 | $GSFontSetUseLegacyFontMetrics(YES); | |
faf4eb4f | 9188 | |
d4f84362 JF |
9189 | // XXX: I have a feeling this was important |
9190 | //UIKeyboardDisableAutomaticAppearance(); | |
3e3977a2 | 9191 | /* }}} */ |
2cb68ddf | 9192 | |
6981ccdf | 9193 | Colon_ = UCLocalize("COLON_DELIMITED"); |
835b451e | 9194 | Elision_ = UCLocalize("ELISION"); |
6981ccdf JF |
9195 | Error_ = UCLocalize("ERROR"); |
9196 | Warning_ = UCLocalize("WARNING"); | |
9197 | ||
7ec29c77 | 9198 | _trace(); |
bf50bdfa | 9199 | int value(UIApplicationMain(argc, argv, @"Cydia", @"Cydia")); |
e057ec05 JF |
9200 | |
9201 | CGColorSpaceRelease(space_); | |
b27f0a94 | 9202 | CFRelease(Locale_); |
e057ec05 | 9203 | |
e057ec05 | 9204 | return value; |
2938b930 | 9205 | } |