]>
Commit | Line | Data |
---|---|---|
36bb2ca2 | 1 | /* Cydia - iPhone UIKit Front-End for Debian APT |
9d67f1f4 | 2 | * Copyright (C) 2008-2011 Jay Freeman (saurik) |
36bb2ca2 JF |
3 | */ |
4 | ||
bfc87a4d | 5 | /* Modified BSD License {{{ */ |
36bb2ca2 JF |
6 | /* |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the | |
12 | * above copyright notice, this list of conditions | |
13 | * and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the | |
15 | * above copyright notice, this list of conditions | |
16 | * and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. The name of the author may not be used to endorse | |
20 | * or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
25 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
36 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
bfc87a4d | 38 | /* }}} */ |
36bb2ca2 | 39 | |
b0ad8dee JF |
40 | // XXX: wtf/FastMalloc.h... wtf? |
41 | #define USE_SYSTEM_MALLOC 1 | |
42 | ||
dc5812ec | 43 | /* #include Directives {{{ */ |
819a0ab1 | 44 | #include "CyteKit/UCPlatform.h" |
d458596e | 45 | #include "CyteKit/Localize.h" |
807ae6d7 | 46 | |
ab398adf JF |
47 | #include <objc/objc.h> |
48 | #include <objc/runtime.h> | |
49 | ||
5f6bff8c | 50 | #include <CoreGraphics/CoreGraphics.h> |
5f6bff8c | 51 | #include <Foundation/Foundation.h> |
7d2ac47f | 52 | |
eef4ccaf JF |
53 | #if 0 |
54 | #define DEPLOYMENT_TARGET_MACOSX 1 | |
55 | #define CF_BUILDING_CF 1 | |
56 | #include <CoreFoundation/CFInternal.h> | |
57 | #endif | |
58 | ||
677b8415 JF |
59 | #include <CoreFoundation/CFPriv.h> |
60 | #include <CoreFoundation/CFUniChar.h> | |
61 | ||
afb5333a JF |
62 | #include <SystemConfiguration/SystemConfiguration.h> |
63 | ||
c21004b9 JF |
64 | #include <UIKit/UIKit.h> |
65 | #include "iPhonePrivate.h" | |
66 | ||
67 | #include <IOKit/IOKitLib.h> | |
a9543575 | 68 | |
f79a4512 | 69 | #include <WebCore/WebCoreThread.h> |
7e9a36b6 | 70 | |
677b8415 | 71 | #include <algorithm> |
808c6eb6 | 72 | #include <iomanip> |
2367a917 | 73 | #include <sstream> |
a0be02eb JF |
74 | #include <string> |
75 | ||
2367a917 JF |
76 | #include <ext/stdio_filebuf.h> |
77 | ||
e59669fd JF |
78 | #undef ABS |
79 | ||
dc5812ec JF |
80 | #include <apt-pkg/acquire.h> |
81 | #include <apt-pkg/acquire-item.h> | |
82 | #include <apt-pkg/algorithms.h> | |
83 | #include <apt-pkg/cachefile.h> | |
77fcccaf | 84 | #include <apt-pkg/clean.h> |
dc5812ec | 85 | #include <apt-pkg/configuration.h> |
7376b55c | 86 | #include <apt-pkg/debindexfile.h> |
3178d79b | 87 | #include <apt-pkg/debmetaindex.h> |
dc5812ec JF |
88 | #include <apt-pkg/error.h> |
89 | #include <apt-pkg/init.h> | |
dddbc481 | 90 | #include <apt-pkg/mmap.h> |
dc5812ec | 91 | #include <apt-pkg/pkgrecords.h> |
dddbc481 | 92 | #include <apt-pkg/sha1.h> |
dc5812ec | 93 | #include <apt-pkg/sourcelist.h> |
2367a917 | 94 | #include <apt-pkg/sptr.h> |
affeffc7 | 95 | #include <apt-pkg/strutl.h> |
f9f6d9e8 | 96 | #include <apt-pkg/tagfile.h> |
dc5812ec | 97 | |
f79a4512 JF |
98 | #include <apr-1/apr_pools.h> |
99 | ||
87f46a96 JF |
100 | #include <sys/types.h> |
101 | #include <sys/stat.h> | |
3178d79b | 102 | #include <sys/sysctl.h> |
59c011d8 JF |
103 | #include <sys/param.h> |
104 | #include <sys/mount.h> | |
bb0fe3c9 | 105 | #include <sys/reboot.h> |
87f46a96 | 106 | |
c21004b9 | 107 | #include <fcntl.h> |
2a8d9add | 108 | #include <notify.h> |
dddbc481 | 109 | #include <dlfcn.h> |
3178d79b | 110 | |
b4d89997 JF |
111 | extern "C" { |
112 | #include <mach-o/nlist.h> | |
113 | } | |
114 | ||
a0be02eb JF |
115 | #include <cstdio> |
116 | #include <cstdlib> | |
117 | #include <cstring> | |
b4d89997 | 118 | |
2367a917 | 119 | #include <errno.h> |
a9543575 | 120 | |
94b0b3e5 JF |
121 | #include <Cytore.hpp> |
122 | ||
86cf87e2 | 123 | #include <CydiaSubstrate/CydiaSubstrate.h> |
cb218676 JF |
124 | #include "Menes/Menes.h" |
125 | ||
819a0ab1 | 126 | #include "CyteKit/PerlCompatibleRegEx.hpp" |
b97fcfc6 | 127 | #include "CyteKit/TableViewCell.h" |
f172aa8f | 128 | #include "CyteKit/WebScriptObject-Cyte.h" |
d458596e JF |
129 | #include "CyteKit/WebViewController.h" |
130 | #include "CyteKit/stringWithUTF8Bytes.h" | |
449ef9d5 | 131 | |
7aa82ca2 | 132 | #include "Cydia/MIMEAddress.h" |
70750ab3 | 133 | #include "Cydia/LoadingViewController.h" |
cb218676 | 134 | #include "Cydia/ProgressEvent.h" |
28b8b687 | 135 | |
71cc7be1 | 136 | #include "SDURLCache/SDURLCache.h" |
b5e7eebb GP |
137 | /* }}} */ |
138 | ||
bfc87a4d | 139 | /* Profiler {{{ */ |
807ae6d7 JF |
140 | struct timeval _ltv; |
141 | bool _itv; | |
142 | ||
2083b866 JF |
143 | #define _timestamp ({ \ |
144 | struct timeval tv; \ | |
145 | gettimeofday(&tv, NULL); \ | |
146 | tv.tv_sec * 1000000 + tv.tv_usec; \ | |
147 | }) | |
148 | ||
808c6eb6 JF |
149 | typedef std::vector<class ProfileTime *> TimeList; |
150 | TimeList times_; | |
151 | ||
152 | class ProfileTime { | |
153 | private: | |
154 | const char *name_; | |
155 | uint64_t total_; | |
76933519 | 156 | uint64_t count_; |
808c6eb6 JF |
157 | |
158 | public: | |
159 | ProfileTime(const char *name) : | |
160 | name_(name), | |
161 | total_(0) | |
162 | { | |
163 | times_.push_back(this); | |
164 | } | |
165 | ||
166 | void AddTime(uint64_t time) { | |
167 | total_ += time; | |
76933519 | 168 | ++count_; |
808c6eb6 JF |
169 | } |
170 | ||
171 | void Print() { | |
172 | if (total_ != 0) | |
76933519 | 173 | std::cerr << std::setw(5) << count_ << ", " << std::setw(7) << total_ << " : " << name_ << std::endl; |
808c6eb6 | 174 | total_ = 0; |
76933519 | 175 | count_ = 0; |
808c6eb6 JF |
176 | } |
177 | }; | |
178 | ||
179 | class ProfileTimer { | |
180 | private: | |
181 | ProfileTime &time_; | |
182 | uint64_t start_; | |
183 | ||
184 | public: | |
185 | ProfileTimer(ProfileTime &time) : | |
186 | time_(time), | |
187 | start_(_timestamp) | |
188 | { | |
189 | } | |
190 | ||
191 | ~ProfileTimer() { | |
192 | time_.AddTime(_timestamp - start_); | |
193 | } | |
194 | }; | |
195 | ||
196 | void PrintTimes() { | |
197 | for (TimeList::const_iterator i(times_.begin()); i != times_.end(); ++i) | |
198 | (*i)->Print(); | |
199 | std::cerr << "========" << std::endl; | |
200 | } | |
201 | ||
202 | #define _profile(name) { \ | |
203 | static ProfileTime name(#name); \ | |
204 | ProfileTimer _ ## name(name); | |
205 | ||
206 | #define _end } | |
f79a4512 | 207 | /* }}} */ |
affeffc7 | 208 | |
f9ba524a JF |
209 | #define Cydia_ CYDIA_VERSION |
210 | ||
43c5d1cb JF |
211 | #define lprintf(args...) fprintf(stderr, args) |
212 | ||
213 | #define ForRelease 1 | |
214 | #define TraceLogging (1 && !ForRelease) | |
215 | #define HistogramInsertionSort (!ForRelease ? 0 : 0) | |
216 | #define ProfileTimes (0 && !ForRelease) | |
217 | #define ForSaurik (0 && !ForRelease) | |
218 | #define LogBrowser (0 && !ForRelease) | |
219 | #define TrackResize (0 && !ForRelease) | |
220 | #define ManualRefresh (1 && !ForRelease) | |
221 | #define ShowInternals (0 && !ForRelease) | |
222 | #define AlwaysReload (0 && !ForRelease) | |
223 | #define TryIndexedCollation (0 && !ForRelease) | |
224 | ||
225 | #if !TraceLogging | |
226 | #undef _trace | |
227 | #define _trace(args...) | |
228 | #endif | |
229 | ||
230 | #if !ProfileTimes | |
231 | #undef _profile | |
232 | #define _profile(name) { | |
233 | #undef _end | |
234 | #define _end } | |
235 | #define PrintTimes() do {} while (false) | |
236 | #endif | |
237 | ||
94b0b3e5 JF |
238 | // Hash Functions/Structures {{{ |
239 | extern "C" uint32_t hashlittle(const void *key, size_t length, uint32_t initval = 0); | |
240 | ||
241 | union SplitHash { | |
242 | uint32_t u32; | |
243 | uint16_t u16[2]; | |
244 | }; | |
245 | // }}} | |
246 | ||
04fe1349 JF |
247 | static const NSUInteger UIViewAutoresizingFlexibleBoth(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); |
248 | ||
670a0494 | 249 | static _finline NSString *CydiaURL(NSString *path) { |
e3d2a2f5 JF |
250 | char page[26]; |
251 | page[0] = 'h'; page[1] = 't'; page[2] = 't'; page[3] = 'p'; page[4] = 's'; | |
252 | page[5] = ':'; page[6] = '/'; page[7] = '/'; page[8] = 'c'; page[9] = 'y'; | |
253 | page[10] = 'd'; page[11] = 'i'; page[12] = 'a'; page[13] = '.'; page[14] = 's'; | |
254 | page[15] = 'a'; page[16] = 'u'; page[17] = 'r'; page[18] = 'i'; page[19] = 'k'; | |
255 | page[20] = '.'; page[21] = 'c'; page[22] = 'o'; page[23] = 'm'; page[24] = '/'; | |
256 | page[25] = '\0'; | |
670a0494 JF |
257 | return [[NSString stringWithUTF8String:page] stringByAppendingString:path]; |
258 | } | |
259 | ||
ef494bd8 RP |
260 | static _finline void UpdateExternalStatus(uint64_t newStatus) { |
261 | int notify_token; | |
262 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
263 | notify_set_state(notify_token, newStatus); | |
264 | notify_cancel(notify_token); | |
265 | } | |
266 | notify_post("com.saurik.Cydia.status"); | |
267 | } | |
268 | ||
68f1828e | 269 | /* NSForcedOrderingSearch doesn't work on the iPhone */ |
808c6eb6 | 270 | static const NSStringCompareOptions MatchCompareOptions_ = NSLiteralSearch | NSCaseInsensitiveSearch; |
677b8415 JF |
271 | static const NSStringCompareOptions LaxCompareOptions_ = NSNumericSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch | NSCaseInsensitiveSearch; |
272 | static const CFStringCompareFlags LaxCompareFlags_ = kCFCompareCaseInsensitive | kCFCompareNonliteral | kCFCompareLocalized | kCFCompareNumerically | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; | |
1e7a90f5 | 273 | |
eef4ccaf JF |
274 | /* Insertion Sort {{{ */ |
275 | ||
df213583 JF |
276 | CFIndex SKBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
277 | const char *ptr = (const char *)list; | |
278 | while (0 < count) { | |
279 | CFIndex half = count / 2; | |
280 | const char *probe = ptr + elementSize * half; | |
281 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 282 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
df213583 JF |
283 | ptr = (cr < 0) ? ptr : probe + elementSize; |
284 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
285 | } | |
286 | return (ptr - (const char *)list) / elementSize; | |
287 | } | |
288 | ||
eef4ccaf JF |
289 | CFIndex CFBSearch_(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context) { |
290 | const char *ptr = (const char *)list; | |
291 | while (0 < count) { | |
292 | CFIndex half = count / 2; | |
293 | const char *probe = ptr + elementSize * half; | |
294 | CFComparisonResult cr = comparator(element, probe, context); | |
b5e7eebb | 295 | if (0 == cr) return (probe - (const char *)list) / elementSize; |
eef4ccaf JF |
296 | ptr = (cr < 0) ? ptr : probe + elementSize; |
297 | count = (cr < 0) ? half : (half + (count & 1) - 1); | |
298 | } | |
299 | return (ptr - (const char *)list) / elementSize; | |
300 | } | |
301 | ||
302 | void CFArrayInsertionSortValues(CFMutableArrayRef array, CFRange range, CFComparatorFunction comparator, void *context) { | |
303 | if (range.length == 0) | |
304 | return; | |
305 | const void **values(new const void *[range.length]); | |
306 | CFArrayGetValues(array, range, values); | |
307 | ||
1539387a | 308 | #if HistogramInsertionSort > 0 |
df213583 JF |
309 | uint32_t total(0), *offsets(new uint32_t[range.length]); |
310 | #endif | |
311 | ||
eef4ccaf JF |
312 | for (CFIndex index(1); index != range.length; ++index) { |
313 | const void *value(values[index]); | |
df213583 JF |
314 | //CFIndex correct(SKBSearch_(&value, sizeof(const void *), values, index, comparator, context)); |
315 | CFIndex correct(index); | |
1539387a JF |
316 | while (comparator(value, values[correct - 1], context) == kCFCompareLessThan) { |
317 | #if HistogramInsertionSort > 1 | |
318 | NSLog(@"%@ < %@", value, values[correct - 1]); | |
319 | #endif | |
df213583 JF |
320 | if (--correct == 0) |
321 | break; | |
1539387a | 322 | } |
eef4ccaf | 323 | if (correct != index) { |
df213583 JF |
324 | size_t offset(index - correct); |
325 | #if HistogramInsertionSort | |
326 | total += offset; | |
327 | ++offsets[offset]; | |
328 | if (offset > 10) | |
329 | NSLog(@"Heavy Insertion Displacement: %u = %@", offset, value); | |
330 | #endif | |
331 | memmove(values + correct + 1, values + correct, sizeof(const void *) * offset); | |
eef4ccaf JF |
332 | values[correct] = value; |
333 | } | |
334 | } | |
335 | ||
336 | CFArrayReplaceValues(array, range, values, range.length); | |
337 | delete [] values; | |
df213583 | 338 | |
1539387a | 339 | #if HistogramInsertionSort > 0 |
df213583 JF |
340 | for (CFIndex index(0); index != range.length; ++index) |
341 | if (offsets[index] != 0) | |
342 | NSLog(@"Insertion Displacement [%u]: %u", index, offsets[index]); | |
343 | NSLog(@"Average Insertion Displacement: %f", double(total) / range.length); | |
344 | delete [] offsets; | |
345 | #endif | |
eef4ccaf JF |
346 | } |
347 | ||
807ae6d7 JF |
348 | /* }}} */ |
349 | ||
bf8476c8 JF |
350 | /* Apple Bug Fixes {{{ */ |
351 | @implementation UIWebDocumentView (Cydia) | |
352 | ||
353 | - (void) _setScrollerOffset:(CGPoint)offset { | |
354 | UIScroller *scroller([self _scroller]); | |
355 | ||
356 | CGSize size([scroller contentSize]); | |
357 | CGSize bounds([scroller bounds].size); | |
358 | ||
359 | CGPoint max; | |
360 | max.x = size.width - bounds.width; | |
361 | max.y = size.height - bounds.height; | |
362 | ||
363 | // wtf Apple?! | |
364 | if (max.x < 0) | |
365 | max.x = 0; | |
366 | if (max.y < 0) | |
367 | max.y = 0; | |
368 | ||
369 | offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x; | |
370 | offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y; | |
371 | ||
372 | [scroller setOffset:offset]; | |
373 | } | |
374 | ||
375 | @end | |
376 | /* }}} */ | |
377 | ||
680eb135 JF |
378 | NSUInteger DOMNodeList$countByEnumeratingWithState$objects$count$(DOMNodeList *self, SEL sel, NSFastEnumerationState *state, id *objects, NSUInteger count) { |
379 | size_t length([self length] - state->state); | |
380 | if (length <= 0) | |
381 | return 0; | |
382 | else if (length > count) | |
383 | length = count; | |
384 | for (size_t i(0); i != length; ++i) | |
385 | objects[i] = [self item:state->state++]; | |
386 | state->itemsPtr = objects; | |
387 | state->mutationsPtr = (unsigned long *) self; | |
388 | return length; | |
389 | } | |
390 | ||
bfc87a4d | 391 | /* Cydia NSString Additions {{{ */ |
2388b078 | 392 | @interface NSString (Cydia) |
a54b1c10 | 393 | - (NSComparisonResult) compareByPath:(NSString *)other; |
2fc76a2d JF |
394 | - (NSString *) stringByCachingURLWithCurrentCDN; |
395 | - (NSString *) stringByAddingPercentEscapesIncludingReserved; | |
2388b078 JF |
396 | @end |
397 | ||
449ef9d5 JF |
398 | @implementation NSString (Cydia) |
399 | ||
a54b1c10 JF |
400 | - (NSComparisonResult) compareByPath:(NSString *)other { |
401 | NSString *prefix = [self commonPrefixWithString:other options:0]; | |
402 | size_t length = [prefix length]; | |
403 | ||
404 | NSRange lrange = NSMakeRange(length, [self length] - length); | |
405 | NSRange rrange = NSMakeRange(length, [other length] - length); | |
406 | ||
407 | lrange = [self rangeOfString:@"/" options:0 range:lrange]; | |
408 | rrange = [other rangeOfString:@"/" options:0 range:rrange]; | |
409 | ||
410 | NSComparisonResult value; | |
411 | ||
412 | if (lrange.location == NSNotFound && rrange.location == NSNotFound) | |
413 | value = NSOrderedSame; | |
414 | else if (lrange.location == NSNotFound) | |
415 | value = NSOrderedAscending; | |
416 | else if (rrange.location == NSNotFound) | |
417 | value = NSOrderedDescending; | |
418 | else | |
419 | value = NSOrderedSame; | |
420 | ||
421 | NSString *lpath = lrange.location == NSNotFound ? [self substringFromIndex:length] : | |
422 | [self substringWithRange:NSMakeRange(length, lrange.location - length)]; | |
423 | NSString *rpath = rrange.location == NSNotFound ? [other substringFromIndex:length] : | |
424 | [other substringWithRange:NSMakeRange(length, rrange.location - length)]; | |
425 | ||
426 | NSComparisonResult result = [lpath compare:rpath]; | |
427 | return result == NSOrderedSame ? value : result; | |
428 | } | |
429 | ||
2fc76a2d JF |
430 | - (NSString *) stringByCachingURLWithCurrentCDN { |
431 | return [self | |
65c27c56 JF |
432 | stringByReplacingOccurrencesOfString:@"://cydia.saurik.com/" |
433 | withString:@"://cache.cydia.saurik.com/" | |
2fc76a2d JF |
434 | ]; |
435 | } | |
436 | ||
437 | - (NSString *) stringByAddingPercentEscapesIncludingReserved { | |
438 | return [(id)CFURLCreateStringByAddingPercentEscapes( | |
bc11cf5b | 439 | kCFAllocatorDefault, |
2fc76a2d JF |
440 | (CFStringRef) self, |
441 | NULL, | |
442 | CFSTR(";/?:@&=+$,"), | |
443 | kCFStringEncodingUTF8 | |
444 | ) autorelease]; | |
445 | } | |
446 | ||
2388b078 | 447 | @end |
bfc87a4d | 448 | /* }}} */ |
2388b078 | 449 | |
bfc87a4d | 450 | /* C++ NSString Wrapper Cache {{{ */ |
8e8fca7f JF |
451 | static _finline CFStringRef CYStringCreate(const char *data, size_t size) { |
452 | return size == 0 ? NULL : | |
453 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingUTF8, NO, kCFAllocatorNull) ?: | |
454 | CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const uint8_t *>(data), size, kCFStringEncodingISOLatin1, NO, kCFAllocatorNull); | |
455 | } | |
456 | ||
457 | static _finline CFStringRef CYStringCreate(const char *data) { | |
458 | return CYStringCreate(data, strlen(data)); | |
459 | } | |
460 | ||
f79a4512 JF |
461 | class CYString { |
462 | private: | |
463 | char *data_; | |
464 | size_t size_; | |
465 | CFStringRef cache_; | |
466 | ||
467 | _finline void clear_() { | |
18873623 | 468 | if (cache_ != NULL) { |
f79a4512 | 469 | CFRelease(cache_); |
18873623 JF |
470 | cache_ = NULL; |
471 | } | |
f79a4512 JF |
472 | } |
473 | ||
474 | public: | |
475 | _finline bool empty() const { | |
476 | return size_ == 0; | |
477 | } | |
478 | ||
479 | _finline size_t size() const { | |
480 | return size_; | |
481 | } | |
482 | ||
483 | _finline char *data() const { | |
484 | return data_; | |
485 | } | |
486 | ||
487 | _finline void clear() { | |
488 | size_ = 0; | |
489 | clear_(); | |
490 | } | |
491 | ||
492 | _finline CYString() : | |
493 | data_(0), | |
494 | size_(0), | |
18873623 | 495 | cache_(NULL) |
f79a4512 JF |
496 | { |
497 | } | |
498 | ||
499 | _finline ~CYString() { | |
500 | clear_(); | |
501 | } | |
502 | ||
503 | void operator =(const CYString &rhs) { | |
504 | data_ = rhs.data_; | |
505 | size_ = rhs.size_; | |
506 | ||
507 | if (rhs.cache_ == nil) | |
508 | cache_ = NULL; | |
509 | else | |
510 | cache_ = reinterpret_cast<CFStringRef>(CFRetain(rhs.cache_)); | |
511 | } | |
512 | ||
97814287 JF |
513 | void copy(apr_pool_t *pool) { |
514 | char *temp(reinterpret_cast<char *>(apr_palloc(pool, size_ + 1))); | |
515 | memcpy(temp, data_, size_); | |
516 | temp[size_] = '\0'; | |
517 | data_ = temp; | |
518 | } | |
519 | ||
f79a4512 JF |
520 | void set(apr_pool_t *pool, const char *data, size_t size) { |
521 | if (size == 0) | |
522 | clear(); | |
523 | else { | |
524 | clear_(); | |
525 | ||
97814287 | 526 | data_ = const_cast<char *>(data); |
f79a4512 | 527 | size_ = size; |
97814287 JF |
528 | |
529 | if (pool != NULL) | |
530 | copy(pool); | |
f79a4512 JF |
531 | } |
532 | } | |
533 | ||
534 | _finline void set(apr_pool_t *pool, const char *data) { | |
535 | set(pool, data, data == NULL ? 0 : strlen(data)); | |
536 | } | |
537 | ||
538 | _finline void set(apr_pool_t *pool, const std::string &rhs) { | |
539 | set(pool, rhs.data(), rhs.size()); | |
540 | } | |
541 | ||
542 | bool operator ==(const CYString &rhs) const { | |
543 | return size_ == rhs.size_ && memcmp(data_, rhs.data_, size_) == 0; | |
544 | } | |
545 | ||
8e8fca7f JF |
546 | _finline operator CFStringRef() { |
547 | if (cache_ == NULL) | |
548 | cache_ = CYStringCreate(data_, size_); | |
549 | return cache_; | |
df213583 JF |
550 | } |
551 | ||
552 | _finline operator id() { | |
553 | return (NSString *) static_cast<CFStringRef>(*this); | |
f79a4512 | 554 | } |
4c0ed943 JF |
555 | |
556 | _finline operator const char *() { | |
557 | return reinterpret_cast<const char *>(data_); | |
558 | } | |
f79a4512 | 559 | }; |
bfc87a4d JF |
560 | /* }}} */ |
561 | /* C++ NSString Algorithm Adapters {{{ */ | |
f79a4512 JF |
562 | extern "C" { |
563 | CF_EXPORT CFHashCode CFStringHashNSString(CFStringRef str); | |
564 | } | |
565 | ||
566 | struct NSStringMapHash : | |
567 | std::unary_function<NSString *, size_t> | |
568 | { | |
569 | _finline size_t operator ()(NSString *value) const { | |
570 | return CFStringHashNSString((CFStringRef) value); | |
571 | } | |
572 | }; | |
573 | ||
574 | struct NSStringMapLess : | |
575 | std::binary_function<NSString *, NSString *, bool> | |
576 | { | |
577 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
578 | return [lhs compare:rhs] == NSOrderedAscending; | |
579 | } | |
580 | }; | |
581 | ||
582 | struct NSStringMapEqual : | |
583 | std::binary_function<NSString *, NSString *, bool> | |
584 | { | |
585 | _finline bool operator ()(NSString *lhs, NSString *rhs) const { | |
586 | return CFStringCompare((CFStringRef) lhs, (CFStringRef) rhs, 0) == kCFCompareEqualTo; | |
587 | //CFEqual((CFTypeRef) lhs, (CFTypeRef) rhs); | |
588 | //[lhs isEqualToString:rhs]; | |
589 | } | |
590 | }; | |
bfc87a4d | 591 | /* }}} */ |
f79a4512 | 592 | |
5f6bff8c | 593 | /* CoreGraphics Primitives {{{ */ |
02012733 | 594 | class CYColor { |
b4d89997 JF |
595 | private: |
596 | CGColorRef color_; | |
597 | ||
6a575b5e JF |
598 | static CGColorRef Create_(CGColorSpaceRef space, float red, float green, float blue, float alpha) { |
599 | CGFloat color[] = {red, green, blue, alpha}; | |
600 | return CGColorCreate(space, color); | |
601 | } | |
602 | ||
b4d89997 | 603 | public: |
02012733 | 604 | CYColor() : |
36bb2ca2 JF |
605 | color_(NULL) |
606 | { | |
607 | } | |
608 | ||
02012733 | 609 | CYColor(CGColorSpaceRef space, float red, float green, float blue, float alpha) : |
6a575b5e | 610 | color_(Create_(space, red, green, blue, alpha)) |
36bb2ca2 JF |
611 | { |
612 | Set(space, red, green, blue, alpha); | |
613 | } | |
614 | ||
615 | void Clear() { | |
616 | if (color_ != NULL) | |
617 | CGColorRelease(color_); | |
b4d89997 JF |
618 | } |
619 | ||
02012733 | 620 | ~CYColor() { |
36bb2ca2 JF |
621 | Clear(); |
622 | } | |
623 | ||
624 | void Set(CGColorSpaceRef space, float red, float green, float blue, float alpha) { | |
625 | Clear(); | |
6a575b5e | 626 | color_ = Create_(space, red, green, blue, alpha); |
b4d89997 JF |
627 | } |
628 | ||
629 | operator CGColorRef() { | |
630 | return color_; | |
631 | } | |
632 | }; | |
b4d89997 JF |
633 | /* }}} */ |
634 | ||
36bb2ca2 | 635 | /* Random Global Variables {{{ */ |
3178d79b | 636 | static const int PulseInterval_ = 50000; |
affeffc7 | 637 | |
57e8b225 JF |
638 | static const NSString *UI_; |
639 | ||
d791dce4 | 640 | static int Finish_; |
be860cc8 | 641 | static bool RestartSubstrate_; |
d791dce4 JF |
642 | static NSArray *Finishes_; |
643 | ||
affeffc7 | 644 | #define SpringBoard_ "/System/Library/LaunchDaemons/com.apple.SpringBoard.plist" |
22f8bed9 | 645 | #define NotifyConfig_ "/etc/notify.conf" |
2a8d9add | 646 | |
dc63e78f JF |
647 | static bool Queuing_; |
648 | ||
02012733 JF |
649 | static CYColor Blue_; |
650 | static CYColor Blueish_; | |
651 | static CYColor Black_; | |
652 | static CYColor Off_; | |
653 | static CYColor White_; | |
654 | static CYColor Gray_; | |
655 | static CYColor Green_; | |
656 | static CYColor Purple_; | |
657 | static CYColor Purplish_; | |
3bd1c2a2 | 658 | |
dc63e78f JF |
659 | static UIColor *InstallingColor_; |
660 | static UIColor *RemovingColor_; | |
36bb2ca2 | 661 | |
7d2ac47f | 662 | static NSString *App_; |
d73cede2 | 663 | |
2388b078 | 664 | static BOOL Advanced_; |
a54b1c10 | 665 | static BOOL Ignored_; |
2388b078 | 666 | |
7b33d201 JF |
667 | static _H<UIFont> Font12_; |
668 | static _H<UIFont> Font12Bold_; | |
669 | static _H<UIFont> Font14_; | |
670 | static _H<UIFont> Font18Bold_; | |
671 | static _H<UIFont> Font22Bold_; | |
f641a0e5 | 672 | |
87f46a96 | 673 | static const char *Machine_ = NULL; |
f4b1c57f | 674 | static NSString *System_ = nil; |
56127854 JF |
675 | static NSString *SerialNumber_ = nil; |
676 | static NSString *ChipID_ = nil; | |
6ffdaae3 | 677 | static NSString *BBSNum_ = nil; |
9737d93e | 678 | static _H<NSString> Token_; |
56127854 | 679 | static NSString *UniqueID_ = nil; |
567e3972 | 680 | static NSString *PLMN_ = nil; |
56127854 JF |
681 | static NSString *Build_ = nil; |
682 | static NSString *Product_ = nil; | |
683 | static NSString *Safari_ = nil; | |
2a8d9add | 684 | |
d791dce4 JF |
685 | static CFLocaleRef Locale_; |
686 | static NSArray *Languages_; | |
687 | static CGColorSpaceRef space_; | |
36bb2ca2 | 688 | |
46dbfd32 | 689 | static NSDictionary *SectionMap_; |
b4d89997 | 690 | static NSMutableDictionary *Metadata_; |
7b0ce2da JF |
691 | static _transient NSMutableDictionary *Settings_; |
692 | static _transient NSString *Role_; | |
693 | static _transient NSMutableDictionary *Packages_; | |
694 | static _transient NSMutableDictionary *Sections_; | |
695 | static _transient NSMutableDictionary *Sources_; | |
bbb879fb | 696 | static bool Changed_; |
31bc18a7 | 697 | static time_t now_; |
8da60fb7 | 698 | |
f333f6c5 | 699 | bool IsWildcat_; |
57e8b225 | 700 | static CGFloat ScreenScale_; |
c138614d | 701 | static NSString *Idiom_; |
5df7ecfb | 702 | |
7b33d201 JF |
703 | static _H<NSMutableDictionary> SessionData_; |
704 | static _H<NSObject> HostConfig_; | |
705 | static _H<NSMutableSet> BridgedHosts_; | |
706 | static _H<NSMutableSet> PipelinedHosts_; | |
5e845121 | 707 | static _H<NSMutableSet> CachedURLs_; |
389133be | 708 | |
e4123ce0 JF |
709 | static NSString *kCydiaProgressEventTypeError = @"Error"; |
710 | static NSString *kCydiaProgressEventTypeInformation = @"Information"; | |
711 | static NSString *kCydiaProgressEventTypeStatus = @"Status"; | |
712 | static NSString *kCydiaProgressEventTypeWarning = @"Warning"; | |
36bb2ca2 | 713 | /* }}} */ |
d791dce4 | 714 | |
36bb2ca2 JF |
715 | /* Display Helpers {{{ */ |
716 | inline float Interpolate(float begin, float end, float fraction) { | |
717 | return (end - begin) * fraction + begin; | |
718 | } | |
2367a917 | 719 | |
1c1dfc2d | 720 | static _finline const char *StripVersion_(const char *version) { |
6a155117 | 721 | const char *colon(strchr(version, ':')); |
673ad3c3 | 722 | return colon == NULL ? version : colon + 1; |
6a155117 JF |
723 | } |
724 | ||
f79a4512 | 725 | NSString *LocalizeSection(NSString *section) { |
b1ce61ec | 726 | static Pcre title_r("^(.*?) \\((.*)\\)$"); |
9fcbca29 JF |
727 | if (title_r(section)) { |
728 | NSString *parent(title_r[1]); | |
729 | NSString *child(title_r[2]); | |
730 | ||
43f3d7f6 | 731 | return [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), |
9fcbca29 JF |
732 | LocalizeSection(parent), |
733 | LocalizeSection(child) | |
b1ce61ec | 734 | ]; |
9fcbca29 | 735 | } |
b1ce61ec JF |
736 | |
737 | return [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
f79a4512 JF |
738 | } |
739 | ||
4cf4165e JF |
740 | NSString *Simplify(NSString *title) { |
741 | const char *data = [title UTF8String]; | |
742 | size_t size = [title length]; | |
743 | ||
7b0ce2da JF |
744 | static Pcre square_r("^\\[(.*)\\]$"); |
745 | if (square_r(data, size)) | |
746 | return Simplify(square_r[1]); | |
747 | ||
748 | static Pcre paren_r("^\\((.*)\\)$"); | |
749 | if (paren_r(data, size)) | |
750 | return Simplify(paren_r[1]); | |
751 | ||
b1ce61ec | 752 | static Pcre title_r("^(.*?) \\((.*)\\)$"); |
4cf4165e | 753 | if (title_r(data, size)) |
7b0ce2da JF |
754 | return Simplify(title_r[1]); |
755 | ||
756 | return title; | |
4cf4165e | 757 | } |
36bb2ca2 JF |
758 | /* }}} */ |
759 | ||
d791dce4 JF |
760 | NSString *GetLastUpdate() { |
761 | NSDate *update = [Metadata_ objectForKey:@"LastUpdate"]; | |
762 | ||
763 | if (update == nil) | |
764 | return UCLocalize("NEVER_OR_UNKNOWN"); | |
765 | ||
766 | CFDateFormatterRef formatter = CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle); | |
767 | CFStringRef formatted = CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) update); | |
768 | ||
769 | CFRelease(formatter); | |
770 | ||
771 | return [(NSString *) formatted autorelease]; | |
772 | } | |
773 | ||
6d9712c4 | 774 | bool isSectionVisible(NSString *section) { |
45447dc3 | 775 | NSDictionary *metadata([Sections_ objectForKey:(section ?: @"")]); |
677b8415 | 776 | NSNumber *hidden(metadata == nil ? nil : [metadata objectForKey:@"Hidden"]); |
6d9712c4 JF |
777 | return hidden == nil || ![hidden boolValue]; |
778 | } | |
779 | ||
c31d7cdc | 780 | static NSObject *CYIOGetValue(const char *path, NSString *property) { |
947a8eef JF |
781 | io_registry_entry_t entry(IORegistryEntryFromPath(kIOMasterPortDefault, path)); |
782 | if (entry == MACH_PORT_NULL) | |
783 | return nil; | |
784 | ||
785 | CFTypeRef value(IORegistryEntryCreateCFProperty(entry, (CFStringRef) property, kCFAllocatorDefault, 0)); | |
786 | IOObjectRelease(entry); | |
787 | ||
788 | if (value == NULL) | |
789 | return nil; | |
790 | return [(id) value autorelease]; | |
791 | } | |
792 | ||
c31d7cdc | 793 | static NSString *CYHex(NSData *data, bool reverse = false) { |
947a8eef JF |
794 | if (data == nil) |
795 | return nil; | |
796 | ||
797 | size_t length([data length]); | |
798 | uint8_t bytes[length]; | |
799 | [data getBytes:bytes]; | |
800 | ||
801 | char string[length * 2 + 1]; | |
802 | for (size_t i(0); i != length; ++i) | |
be45a862 | 803 | sprintf(string + i * 2, "%.2x", bytes[reverse ? length - i - 1 : i]); |
947a8eef JF |
804 | |
805 | return [NSString stringWithUTF8String:string]; | |
806 | } | |
807 | ||
9cb0bff2 GP |
808 | @class Cydia; |
809 | ||
d36e83a3 | 810 | /* Delegate Prototypes {{{ */ |
36bb2ca2 JF |
811 | @class Package; |
812 | @class Source; | |
6915b806 | 813 | @class CydiaProgressEvent; |
36bb2ca2 | 814 | |
6915b806 | 815 | @protocol DatabaseDelegate |
5a09ae08 | 816 | - (void) repairWithSelector:(SEL)selector; |
8b29f8e6 | 817 | - (void) setConfigurationData:(NSString *)data; |
6915b806 | 818 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task; |
8b29f8e6 JF |
819 | @end |
820 | ||
f6e13561 | 821 | @class CYPackageController; |
f79a4512 | 822 | |
36bb2ca2 | 823 | @protocol CydiaDelegate |
54043703 JF |
824 | - (void) retainNetworkActivityIndicator; |
825 | - (void) releaseNetworkActivityIndicator; | |
dc63e78f | 826 | - (void) clearPackage:(Package *)package; |
36bb2ca2 | 827 | - (void) installPackage:(Package *)package; |
77801ff1 | 828 | - (void) installPackages:(NSArray *)packages; |
36bb2ca2 | 829 | - (void) removePackage:(Package *)package; |
c21004b9 JF |
830 | - (void) beginUpdate; |
831 | - (BOOL) updating; | |
36bb2ca2 | 832 | - (void) distUpgrade; |
fed0d010 | 833 | - (void) loadData; |
6d9712c4 | 834 | - (void) updateData; |
7b0ce2da | 835 | - (void) syncData; |
93460555 | 836 | - (void) addTrivialSource:(NSString *)href; |
21c6da4b | 837 | - (void) showSettings; |
7b0ce2da | 838 | - (UIProgressHUD *) addProgressHUD; |
d061f4ba | 839 | - (void) removeProgressHUD:(UIProgressHUD *)hud; |
cd79e8cf | 840 | - (CyteViewController *) pageForPackage:(NSString *)name; |
9daa7f25 | 841 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item; |
4ba8f30a | 842 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 843 | @end |
d36e83a3 | 844 | /* }}} */ |
b4d89997 | 845 | |
dc5812ec JF |
846 | /* Status Delegation {{{ */ |
847 | class Status : | |
848 | public pkgAcquireStatus | |
849 | { | |
850 | private: | |
31f3cfff | 851 | _transient NSObject<ProgressDelegate> *delegate_; |
fc470c15 | 852 | bool cancelled_; |
dc5812ec JF |
853 | |
854 | public: | |
855 | Status() : | |
fc470c15 JF |
856 | delegate_(nil), |
857 | cancelled_(false) | |
dc5812ec JF |
858 | { |
859 | } | |
860 | ||
6915b806 | 861 | void setDelegate(NSObject<ProgressDelegate> *delegate) { |
dc5812ec JF |
862 | delegate_ = delegate; |
863 | } | |
864 | ||
670a0494 JF |
865 | NSObject<ProgressDelegate> *getDelegate() const { |
866 | return delegate_; | |
867 | } | |
868 | ||
dc5812ec JF |
869 | virtual bool MediaChange(std::string media, std::string drive) { |
870 | return false; | |
871 | } | |
872 | ||
873 | virtual void IMSHit(pkgAcquire::ItemDesc &item) { | |
dc5812ec JF |
874 | } |
875 | ||
876 | virtual void Fetch(pkgAcquire::ItemDesc &item) { | |
6915b806 | 877 | NSString *name([NSString stringWithUTF8String:item.ShortDesc.c_str()]); |
389133be | 878 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithFormat:UCLocalize("DOWNLOADING_"), name] ofType:kCydiaProgressEventTypeStatus forItem:item]); |
6915b806 | 879 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
880 | } |
881 | ||
882 | virtual void Done(pkgAcquire::ItemDesc &item) { | |
dc5812ec JF |
883 | } |
884 | ||
885 | virtual void Fail(pkgAcquire::ItemDesc &item) { | |
1d80f6b9 JF |
886 | if ( |
887 | item.Owner->Status == pkgAcquire::Item::StatIdle || | |
888 | item.Owner->Status == pkgAcquire::Item::StatDone | |
889 | ) | |
890 | return; | |
891 | ||
affeffc7 JF |
892 | std::string &error(item.Owner->ErrorText); |
893 | if (error.empty()) | |
894 | return; | |
895 | ||
389133be | 896 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:kCydiaProgressEventTypeError forItem:item]); |
6915b806 | 897 | [delegate_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
dc5812ec JF |
898 | } |
899 | ||
900 | virtual bool Pulse(pkgAcquire *Owner) { | |
2367a917 JF |
901 | bool value = pkgAcquireStatus::Pulse(Owner); |
902 | ||
bcbac8f7 | 903 | double percent( |
2367a917 JF |
904 | double(CurrentBytes + CurrentItems) / |
905 | double(TotalBytes + TotalItems) | |
906 | ); | |
907 | ||
bcbac8f7 JF |
908 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:[NSDictionary dictionaryWithObjectsAndKeys: |
909 | [NSNumber numberWithDouble:percent], @"Percent", | |
910 | ||
911 | [NSNumber numberWithDouble:CurrentBytes], @"Current", | |
912 | [NSNumber numberWithDouble:TotalBytes], @"Total", | |
913 | [NSNumber numberWithDouble:CurrentCPS], @"Speed", | |
914 | nil] waitUntilDone:YES]; | |
915 | ||
fc470c15 JF |
916 | if (value && ![delegate_ isProgressCancelled]) |
917 | return true; | |
918 | else { | |
919 | cancelled_ = true; | |
920 | return false; | |
921 | } | |
922 | } | |
923 | ||
924 | _finline bool WasCancelled() const { | |
925 | return cancelled_; | |
dc5812ec JF |
926 | } |
927 | ||
928 | virtual void Start() { | |
0210c2b5 | 929 | pkgAcquireStatus::Start(); |
aaae308d | 930 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:YES] waitUntilDone:YES]; |
dc5812ec JF |
931 | } |
932 | ||
933 | virtual void Stop() { | |
0210c2b5 | 934 | pkgAcquireStatus::Stop(); |
aaae308d | 935 | [delegate_ performSelectorOnMainThread:@selector(setProgressCancellable:) withObject:[NSNumber numberWithBool:NO] waitUntilDone:YES]; |
bcbac8f7 | 936 | [delegate_ performSelectorOnMainThread:@selector(setProgressStatus:) withObject:nil waitUntilDone:YES]; |
dc5812ec JF |
937 | } |
938 | }; | |
939 | /* }}} */ | |
36bb2ca2 | 940 | /* Database Interface {{{ */ |
68d927e2 JF |
941 | typedef std::map< unsigned long, _H<Source> > SourceMap; |
942 | ||
36bb2ca2 | 943 | @interface Database : NSObject { |
f79a4512 JF |
944 | NSZone *zone_; |
945 | apr_pool_t *pool_; | |
946 | ||
a1440b10 JF |
947 | unsigned era_; |
948 | ||
36bb2ca2 | 949 | pkgCacheFile cache_; |
5a09ae08 | 950 | pkgDepCache::Policy *policy_; |
36bb2ca2 JF |
951 | pkgRecords *records_; |
952 | pkgProblemResolver *resolver_; | |
953 | pkgAcquire *fetcher_; | |
954 | FileFd *lock_; | |
955 | SPtr<pkgPackageManager> manager_; | |
956 | pkgSourceList *list_; | |
5f6bff8c | 957 | |
34f70f5d | 958 | SourceMap sourceMap_; |
7b33d201 | 959 | _H<NSMutableArray> sourceList_; |
34f70f5d | 960 | |
077e9d90 | 961 | CFMutableArrayRef packages_; |
b4d89997 | 962 | |
6915b806 JF |
963 | _transient NSObject<DatabaseDelegate> *delegate_; |
964 | _transient NSObject<ProgressDelegate> *progress_; | |
965 | ||
36bb2ca2 | 966 | Status status_; |
8b29f8e6 | 967 | |
77fcccaf | 968 | int cydiafd_; |
36bb2ca2 | 969 | int statusfd_; |
8b29f8e6 | 970 | FILE *input_; |
fe33a23e JF |
971 | |
972 | std::map<const char *, _H<NSString> > sections_; | |
dc5812ec JF |
973 | } |
974 | ||
770f2a8e | 975 | + (Database *) sharedInstance; |
a1440b10 | 976 | - (unsigned) era; |
770f2a8e | 977 | |
77fcccaf | 978 | - (void) _readCydia:(NSNumber *)fd; |
36bb2ca2 JF |
979 | - (void) _readStatus:(NSNumber *)fd; |
980 | - (void) _readOutput:(NSNumber *)fd; | |
2367a917 | 981 | |
8b29f8e6 JF |
982 | - (FILE *) input; |
983 | ||
36bb2ca2 | 984 | - (Package *) packageWithName:(NSString *)name; |
dc5812ec | 985 | |
36bb2ca2 | 986 | - (pkgCacheFile &) cache; |
5a09ae08 | 987 | - (pkgDepCache::Policy *) policy; |
36bb2ca2 JF |
988 | - (pkgRecords *) records; |
989 | - (pkgProblemResolver *) resolver; | |
990 | - (pkgAcquire &) fetcher; | |
a3328c28 | 991 | - (pkgSourceList &) list; |
36bb2ca2 | 992 | - (NSArray *) packages; |
7b0ce2da | 993 | - (NSArray *) sources; |
d669236d | 994 | - (Source *) sourceWithKey:(NSString *)key; |
4ba8f30a | 995 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation; |
36bb2ca2 | 996 | |
5a09ae08 | 997 | - (void) configure; |
670a0494 | 998 | - (bool) prepare; |
36bb2ca2 | 999 | - (void) perform; |
670a0494 | 1000 | - (bool) upgrade; |
36bb2ca2 JF |
1001 | - (void) update; |
1002 | ||
670a0494 | 1003 | - (void) updateWithStatus:(Status &)status; |
36bb2ca2 | 1004 | |
6915b806 JF |
1005 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate; |
1006 | ||
1007 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate; | |
1008 | - (NSObject<ProgressDelegate> *) progressDelegate; | |
1009 | ||
7376b55c | 1010 | - (Source *) getSource:(pkgCache::PkgFileIterator)file; |
fe33a23e JF |
1011 | |
1012 | - (NSString *) mappedSectionForPointer:(const char *)pointer; | |
1013 | ||
670a0494 JF |
1014 | @end |
1015 | /* }}} */ | |
6915b806 JF |
1016 | /* ProgressEvent Implementation {{{ */ |
1017 | @implementation CydiaProgressEvent | |
670a0494 | 1018 | |
6915b806 JF |
1019 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type { |
1020 | return [[[CydiaProgressEvent alloc] initWithMessage:message ofType:type] autorelease]; | |
670a0494 JF |
1021 | } |
1022 | ||
6915b806 JF |
1023 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forPackage:(NSString *)package { |
1024 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); | |
1025 | [event setPackage:package]; | |
1026 | return event; | |
670a0494 JF |
1027 | } |
1028 | ||
c57867ea JF |
1029 | + (CydiaProgressEvent *) eventWithMessage:(NSString *)message ofType:(NSString *)type forItem:(pkgAcquire::ItemDesc &)item { |
1030 | CydiaProgressEvent *event([self eventWithMessage:message ofType:type]); | |
1031 | ||
1032 | NSString *description([NSString stringWithUTF8String:item.Description.c_str()]); | |
1033 | NSArray *fields([description componentsSeparatedByString:@" "]); | |
1034 | [event setItem:fields]; | |
1035 | ||
1036 | if ([fields count] > 3) { | |
1037 | [event setPackage:[fields objectAtIndex:2]]; | |
1038 | [event setVersion:[fields objectAtIndex:3]]; | |
1039 | } | |
1040 | ||
1041 | [event setURL:[NSString stringWithUTF8String:item.URI.c_str()]]; | |
1042 | ||
1043 | return event; | |
1044 | } | |
1045 | ||
4ede7a3f JF |
1046 | + (NSArray *) _attributeKeys { |
1047 | return [NSArray arrayWithObjects: | |
c57867ea | 1048 | @"item", |
4ede7a3f JF |
1049 | @"message", |
1050 | @"package", | |
1051 | @"type", | |
c57867ea JF |
1052 | @"url", |
1053 | @"version", | |
4ede7a3f JF |
1054 | nil]; |
1055 | } | |
1056 | ||
1057 | - (NSArray *) attributeKeys { | |
1058 | return [[self class] _attributeKeys]; | |
1059 | } | |
1060 | ||
1061 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1062 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1063 | } | |
1064 | ||
6915b806 JF |
1065 | - (id) initWithMessage:(NSString *)message ofType:(NSString *)type { |
1066 | if ((self = [super init]) != nil) { | |
1067 | message_ = message; | |
1068 | type_ = type; | |
1069 | } return self; | |
670a0494 JF |
1070 | } |
1071 | ||
6915b806 JF |
1072 | - (NSString *) message { |
1073 | return message_; | |
1074 | } | |
6067f1b8 | 1075 | |
6915b806 JF |
1076 | - (NSString *) type { |
1077 | return type_; | |
1078 | } | |
1079 | ||
c57867ea JF |
1080 | - (NSArray *) item { |
1081 | return (id) item_ ?: [NSNull null]; | |
1082 | } | |
1083 | ||
1084 | - (void) setItem:(NSArray *)item { | |
1085 | item_ = item; | |
6915b806 JF |
1086 | } |
1087 | ||
c57867ea JF |
1088 | - (NSString *) package { |
1089 | return (id) package_ ?: [NSNull null]; | |
6915b806 JF |
1090 | } |
1091 | ||
1092 | - (void) setPackage:(NSString *)package { | |
1093 | package_ = package; | |
1094 | } | |
1095 | ||
c57867ea JF |
1096 | - (NSString *) url { |
1097 | return (id) url_ ?: [NSNull null]; | |
1098 | } | |
1099 | ||
1100 | - (void) setURL:(NSString *)url { | |
1101 | url_ = url; | |
1102 | } | |
1103 | ||
1104 | - (void) setVersion:(NSString *)version { | |
1105 | version_ = version; | |
1106 | } | |
1107 | ||
1108 | - (NSString *) version { | |
1109 | return (id) version_ ?: [NSNull null]; | |
1110 | } | |
1111 | ||
6915b806 JF |
1112 | - (NSString *) compound:(NSString *)value { |
1113 | if (value != nil) { | |
1114 | NSString *mode(nil); { | |
1115 | NSString *type([self type]); | |
389133be | 1116 | if ([type isEqualToString:kCydiaProgressEventTypeError]) |
6915b806 | 1117 | mode = UCLocalize("ERROR"); |
389133be | 1118 | else if ([type isEqualToString:kCydiaProgressEventTypeWarning]) |
6915b806 JF |
1119 | mode = UCLocalize("WARNING"); |
1120 | } | |
1121 | ||
1122 | if (mode != nil) | |
1123 | value = [NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), mode, value]; | |
1124 | } | |
1125 | ||
1126 | return value; | |
1127 | } | |
1128 | ||
1129 | - (NSString *) compoundMessage { | |
1130 | return [self compound:[self message]]; | |
1131 | } | |
1132 | ||
1133 | - (NSString *) compoundTitle { | |
1134 | NSString *title; | |
1135 | ||
83b78e5f | 1136 | if (package_ == nil) |
6915b806 | 1137 | title = nil; |
83b78e5f JF |
1138 | else if (Package *package = [[Database sharedInstance] packageWithName:package_]) |
1139 | title = [package name]; | |
1140 | else | |
1141 | title = package_; | |
6915b806 JF |
1142 | |
1143 | return [self compound:title]; | |
670a0494 JF |
1144 | } |
1145 | ||
dc5812ec | 1146 | @end |
36bb2ca2 | 1147 | /* }}} */ |
dc5812ec | 1148 | |
94b0b3e5 JF |
1149 | // Cytore Definitions {{{ |
1150 | struct PackageValue : | |
1151 | Cytore::Block | |
1152 | { | |
94b0b3e5 JF |
1153 | Cytore::Offset<PackageValue> next_; |
1154 | ||
1155 | uint32_t index_ : 23; | |
1156 | uint32_t subscribed_ : 1; | |
1157 | uint32_t : 8; | |
1158 | ||
1159 | int32_t first_; | |
1160 | int32_t last_; | |
1161 | ||
1162 | uint16_t vhash_; | |
1163 | uint16_t nhash_; | |
1164 | ||
1165 | char version_[8]; | |
1166 | char name_[]; | |
1167 | }; | |
1168 | ||
1169 | struct MetaValue : | |
1170 | Cytore::Block | |
1171 | { | |
9f357d11 | 1172 | uint32_t active_; |
94b0b3e5 JF |
1173 | Cytore::Offset<PackageValue> packages_[1 << 16]; |
1174 | }; | |
1175 | ||
1176 | static Cytore::File<MetaValue> MetaFile_; | |
1177 | // }}} | |
1178 | // Cytore Helper Functions {{{ | |
c65611b9 | 1179 | static PackageValue *PackageFind(const char *name, size_t length, bool *fail = NULL) { |
94b0b3e5 JF |
1180 | SplitHash nhash = { hashlittle(name, length) }; |
1181 | ||
1182 | PackageValue *metadata; | |
1183 | ||
1184 | Cytore::Offset<PackageValue> *offset(&MetaFile_->packages_[nhash.u16[0]]); | |
1185 | offset: if (offset->IsNull()) { | |
1186 | *offset = MetaFile_.New<PackageValue>(length + 1); | |
1187 | metadata = &MetaFile_.Get(*offset); | |
1188 | ||
c65611b9 JF |
1189 | if (metadata == NULL) { |
1190 | if (fail != NULL) | |
1191 | *fail = true; | |
1192 | ||
1193 | metadata = new PackageValue(); | |
1194 | memset(metadata, 0, sizeof(*metadata)); | |
1195 | } | |
1196 | ||
94b0b3e5 JF |
1197 | memcpy(metadata->name_, name, length + 1); |
1198 | metadata->nhash_ = nhash.u16[1]; | |
1199 | } else { | |
1200 | metadata = &MetaFile_.Get(*offset); | |
1201 | ||
1202 | if (metadata->nhash_ != nhash.u16[1] || strncmp(metadata->name_, name, length + 1) != 0) { | |
1203 | offset = &metadata->next_; | |
1204 | goto offset; | |
1205 | } | |
1206 | } | |
1207 | ||
94b0b3e5 JF |
1208 | return metadata; |
1209 | } | |
1210 | ||
1211 | static void PackageImport(const void *key, const void *value, void *context) { | |
c65611b9 JF |
1212 | bool &fail(*reinterpret_cast<bool *>(context)); |
1213 | ||
94b0b3e5 JF |
1214 | char buffer[1024]; |
1215 | if (!CFStringGetCString((CFStringRef) key, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { | |
1216 | NSLog(@"failed to import package %@", key); | |
1217 | return; | |
1218 | } | |
1219 | ||
c65611b9 | 1220 | PackageValue *metadata(PackageFind(buffer, strlen(buffer), &fail)); |
94b0b3e5 JF |
1221 | NSDictionary *package((NSDictionary *) value); |
1222 | ||
1223 | if (NSNumber *subscribed = [package objectForKey:@"IsSubscribed"]) | |
2f856365 | 1224 | if ([subscribed boolValue] && !metadata->subscribed_) |
94b0b3e5 JF |
1225 | metadata->subscribed_ = true; |
1226 | ||
1227 | if (NSDate *date = [package objectForKey:@"FirstSeen"]) { | |
1228 | time_t time([date timeIntervalSince1970]); | |
1229 | if (metadata->first_ > time || metadata->first_ == 0) | |
1230 | metadata->first_ = time; | |
1231 | } | |
1232 | ||
2f856365 JF |
1233 | NSDate *date([package objectForKey:@"LastSeen"]); |
1234 | NSString *version([package objectForKey:@"LastVersion"]); | |
5a933937 | 1235 | |
2f856365 | 1236 | if (date != nil && version != nil) { |
94b0b3e5 | 1237 | time_t time([date timeIntervalSince1970]); |
2f856365 | 1238 | if (metadata->last_ < time || metadata->last_ == 0) |
5a933937 JF |
1239 | if (CFStringGetCString((CFStringRef) version, buffer, sizeof(buffer), kCFStringEncodingUTF8)) { |
1240 | size_t length(strlen(buffer)); | |
1241 | uint16_t vhash(hashlittle(buffer, length)); | |
94b0b3e5 | 1242 | |
5a933937 JF |
1243 | size_t capped(std::min<size_t>(8, length)); |
1244 | char *latest(buffer + length - capped); | |
1245 | ||
1246 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); | |
1247 | metadata->vhash_ = vhash; | |
2f856365 JF |
1248 | |
1249 | metadata->last_ = time; | |
5a933937 | 1250 | } |
2f856365 | 1251 | } |
94b0b3e5 JF |
1252 | } |
1253 | // }}} | |
1254 | ||
36bb2ca2 JF |
1255 | /* Source Class {{{ */ |
1256 | @interface Source : NSObject { | |
6204f56a | 1257 | CYString depiction_; |
f9f6d9e8 JF |
1258 | CYString description_; |
1259 | CYString label_; | |
1260 | CYString origin_; | |
1261 | CYString support_; | |
dc5812ec | 1262 | |
f9f6d9e8 JF |
1263 | CYString uri_; |
1264 | CYString distribution_; | |
1265 | CYString type_; | |
8252b666 | 1266 | CYString base_; |
f9f6d9e8 | 1267 | CYString version_; |
2367a917 | 1268 | |
c08c8943 JF |
1269 | _H<NSString> host_; |
1270 | _H<NSString> authority_; | |
f9f6d9e8 JF |
1271 | |
1272 | CYString defaultIcon_; | |
dc5812ec | 1273 | |
c08c8943 | 1274 | _H<NSDictionary> record_; |
36bb2ca2 | 1275 | BOOL trusted_; |
dc5812ec JF |
1276 | } |
1277 | ||
f9f6d9e8 | 1278 | - (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool; |
dc5812ec | 1279 | |
7b0ce2da JF |
1280 | - (NSComparisonResult) compareByNameAndType:(Source *)source; |
1281 | ||
6204f56a | 1282 | - (NSString *) depictionForPackage:(NSString *)package; |
dc63e78f JF |
1283 | - (NSString *) supportForPackage:(NSString *)package; |
1284 | ||
7b0ce2da | 1285 | - (NSDictionary *) record; |
36bb2ca2 | 1286 | - (BOOL) trusted; |
dc5812ec | 1287 | |
36bb2ca2 JF |
1288 | - (NSString *) uri; |
1289 | - (NSString *) distribution; | |
1290 | - (NSString *) type; | |
8252b666 JF |
1291 | |
1292 | - (NSString *) base; | |
1293 | ||
7b0ce2da JF |
1294 | - (NSString *) key; |
1295 | - (NSString *) host; | |
36bb2ca2 | 1296 | |
7b0ce2da | 1297 | - (NSString *) name; |
8d262908 | 1298 | - (NSString *) shortDescription; |
36bb2ca2 JF |
1299 | - (NSString *) label; |
1300 | - (NSString *) origin; | |
1301 | - (NSString *) version; | |
dc5812ec | 1302 | |
36bb2ca2 | 1303 | - (NSString *) defaultIcon; |
7b0ce2da | 1304 | |
dc5812ec | 1305 | @end |
dc5812ec | 1306 | |
36bb2ca2 | 1307 | @implementation Source |
dc5812ec | 1308 | |
a3328c28 | 1309 | - (void) _clear { |
f9f6d9e8 JF |
1310 | uri_.clear(); |
1311 | distribution_.clear(); | |
1312 | type_.clear(); | |
a3328c28 | 1313 | |
8252b666 JF |
1314 | base_.clear(); |
1315 | ||
f9f6d9e8 JF |
1316 | description_.clear(); |
1317 | label_.clear(); | |
1318 | origin_.clear(); | |
6204f56a | 1319 | depiction_.clear(); |
f9f6d9e8 JF |
1320 | support_.clear(); |
1321 | version_.clear(); | |
1322 | defaultIcon_.clear(); | |
1323 | ||
c08c8943 JF |
1324 | record_ = nil; |
1325 | host_ = nil; | |
1326 | authority_ = nil; | |
a3328c28 | 1327 | } |
dc5812ec | 1328 | |
6f1a15d9 | 1329 | + (NSArray *) _attributeKeys { |
e58ff941 | 1330 | return [NSArray arrayWithObjects: |
e58ff941 JF |
1331 | @"distribution", |
1332 | @"host", | |
1333 | @"key", | |
1334 | @"label", | |
1335 | @"name", | |
1336 | @"origin", | |
8d262908 | 1337 | @"shortDescription", |
e58ff941 JF |
1338 | @"trusted", |
1339 | @"type", | |
1340 | @"uri", | |
1341 | @"version", | |
1342 | nil]; | |
6f1a15d9 JF |
1343 | } |
1344 | ||
1345 | - (NSArray *) attributeKeys { | |
1346 | return [[self class] _attributeKeys]; | |
1347 | } | |
1348 | ||
1349 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1350 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1351 | } | |
1352 | ||
f9f6d9e8 | 1353 | - (void) setMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { |
a3328c28 | 1354 | [self _clear]; |
dc5812ec | 1355 | |
a3328c28 JF |
1356 | trusted_ = index->IsTrusted(); |
1357 | ||
f9f6d9e8 JF |
1358 | uri_.set(pool, index->GetURI()); |
1359 | distribution_.set(pool, index->GetDist()); | |
1360 | type_.set(pool, index->GetType()); | |
a3328c28 JF |
1361 | |
1362 | debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index)); | |
1363 | if (dindex != NULL) { | |
8252b666 JF |
1364 | base_.set(pool, dindex->MetaIndexURI("")); |
1365 | ||
18873623 JF |
1366 | FileFd fd; |
1367 | if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly)) | |
1368 | _error->Discard(); | |
1369 | else { | |
1370 | pkgTagFile tags(&fd); | |
f9f6d9e8 | 1371 | |
18873623 JF |
1372 | pkgTagSection section; |
1373 | tags.Step(section); | |
a3328c28 | 1374 | |
18873623 JF |
1375 | struct { |
1376 | const char *name_; | |
1377 | CYString *value_; | |
1378 | } names[] = { | |
1379 | {"default-icon", &defaultIcon_}, | |
6204f56a | 1380 | {"depiction", &depiction_}, |
18873623 JF |
1381 | {"description", &description_}, |
1382 | {"label", &label_}, | |
1383 | {"origin", &origin_}, | |
1384 | {"support", &support_}, | |
1385 | {"version", &version_}, | |
1386 | }; | |
f9f6d9e8 | 1387 | |
18873623 JF |
1388 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { |
1389 | const char *start, *end; | |
1390 | ||
1391 | if (section.Find(names[i].name_, start, end)) { | |
1392 | CYString &value(*names[i].value_); | |
1393 | value.set(pool, start, end - start); | |
1394 | } | |
f9f6d9e8 | 1395 | } |
36bb2ca2 | 1396 | } |
a3328c28 | 1397 | } |
7b0ce2da | 1398 | |
a3328c28 | 1399 | record_ = [Sources_ objectForKey:[self key]]; |
f9f6d9e8 | 1400 | |
7623f855 JF |
1401 | NSURL *url([NSURL URLWithString:uri_]); |
1402 | ||
1403 | host_ = [url host]; | |
1404 | if (host_ != nil) | |
c08c8943 | 1405 | host_ = [host_ lowercaseString]; |
7623f855 JF |
1406 | |
1407 | if (host_ != nil) | |
dfdb9ae0 | 1408 | authority_ = host_; |
7623f855 JF |
1409 | else |
1410 | authority_ = [url path]; | |
a3328c28 JF |
1411 | } |
1412 | ||
f9f6d9e8 | 1413 | - (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool { |
a3328c28 | 1414 | if ((self = [super init]) != nil) { |
f9f6d9e8 | 1415 | [self setMetaIndex:index inPool:pool]; |
36bb2ca2 | 1416 | } return self; |
2367a917 | 1417 | } |
dc5812ec | 1418 | |
7b0ce2da JF |
1419 | - (NSComparisonResult) compareByNameAndType:(Source *)source { |
1420 | NSDictionary *lhr = [self record]; | |
1421 | NSDictionary *rhr = [source record]; | |
1422 | ||
1423 | if (lhr != rhr) | |
1424 | return lhr == nil ? NSOrderedDescending : NSOrderedAscending; | |
1425 | ||
1426 | NSString *lhs = [self name]; | |
1427 | NSString *rhs = [source name]; | |
1428 | ||
1429 | if ([lhs length] != 0 && [rhs length] != 0) { | |
1430 | unichar lhc = [lhs characterAtIndex:0]; | |
1431 | unichar rhc = [rhs characterAtIndex:0]; | |
1432 | ||
1433 | if (isalpha(lhc) && !isalpha(rhc)) | |
1434 | return NSOrderedAscending; | |
1435 | else if (!isalpha(lhc) && isalpha(rhc)) | |
1436 | return NSOrderedDescending; | |
1437 | } | |
1438 | ||
68f1828e | 1439 | return [lhs compare:rhs options:LaxCompareOptions_]; |
7b0ce2da JF |
1440 | } |
1441 | ||
6204f56a | 1442 | - (NSString *) depictionForPackage:(NSString *)package { |
89b0ea4a | 1443 | return depiction_.empty() ? nil : [static_cast<id>(depiction_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
6204f56a JF |
1444 | } |
1445 | ||
dc63e78f | 1446 | - (NSString *) supportForPackage:(NSString *)package { |
89b0ea4a | 1447 | return support_.empty() ? nil : [static_cast<id>(support_) stringByReplacingOccurrencesOfString:@"*" withString:package]; |
dc63e78f JF |
1448 | } |
1449 | ||
7b0ce2da JF |
1450 | - (NSDictionary *) record { |
1451 | return record_; | |
1452 | } | |
1453 | ||
36bb2ca2 JF |
1454 | - (BOOL) trusted { |
1455 | return trusted_; | |
1456 | } | |
3178d79b | 1457 | |
36bb2ca2 JF |
1458 | - (NSString *) uri { |
1459 | return uri_; | |
1460 | } | |
ec97ef06 | 1461 | |
36bb2ca2 JF |
1462 | - (NSString *) distribution { |
1463 | return distribution_; | |
1464 | } | |
dc5812ec | 1465 | |
36bb2ca2 JF |
1466 | - (NSString *) type { |
1467 | return type_; | |
dc5812ec JF |
1468 | } |
1469 | ||
8252b666 JF |
1470 | - (NSString *) base { |
1471 | return base_; | |
1472 | } | |
1473 | ||
7b0ce2da | 1474 | - (NSString *) key { |
f9f6d9e8 | 1475 | return [NSString stringWithFormat:@"%@:%@:%@", (NSString *) type_, (NSString *) uri_, (NSString *) distribution_]; |
7b0ce2da JF |
1476 | } |
1477 | ||
1478 | - (NSString *) host { | |
f9f6d9e8 | 1479 | return host_; |
7b0ce2da JF |
1480 | } |
1481 | ||
1482 | - (NSString *) name { | |
c08c8943 | 1483 | return origin_.empty() ? (id) authority_ : origin_; |
7b0ce2da JF |
1484 | } |
1485 | ||
8d262908 | 1486 | - (NSString *) shortDescription { |
36bb2ca2 JF |
1487 | return description_; |
1488 | } | |
b4d89997 | 1489 | |
36bb2ca2 | 1490 | - (NSString *) label { |
c08c8943 | 1491 | return label_.empty() ? (id) authority_ : label_; |
36bb2ca2 | 1492 | } |
3178d79b | 1493 | |
36bb2ca2 JF |
1494 | - (NSString *) origin { |
1495 | return origin_; | |
1496 | } | |
3178d79b | 1497 | |
36bb2ca2 JF |
1498 | - (NSString *) version { |
1499 | return version_; | |
1500 | } | |
2367a917 | 1501 | |
36bb2ca2 JF |
1502 | - (NSString *) defaultIcon { |
1503 | return defaultIcon_; | |
1504 | } | |
2367a917 | 1505 | |
2388b078 JF |
1506 | @end |
1507 | /* }}} */ | |
83682c75 JF |
1508 | /* CydiaOperation Class {{{ */ |
1509 | @interface CydiaOperation : NSObject { | |
7b33d201 JF |
1510 | _H<NSString> operator_; |
1511 | _H<NSString> value_; | |
2388b078 JF |
1512 | } |
1513 | ||
83682c75 JF |
1514 | - (NSString *) operator; |
1515 | - (NSString *) value; | |
2388b078 JF |
1516 | |
1517 | @end | |
1518 | ||
83682c75 | 1519 | @implementation CydiaOperation |
2388b078 | 1520 | |
83682c75 JF |
1521 | - (id) initWithOperator:(const char *)_operator value:(const char *)value { |
1522 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
1523 | operator_ = [NSString stringWithUTF8String:_operator]; |
1524 | value_ = [NSString stringWithUTF8String:value]; | |
83682c75 JF |
1525 | } return self; |
1526 | } | |
1527 | ||
1528 | + (NSArray *) _attributeKeys { | |
1529 | return [NSArray arrayWithObjects: | |
1530 | @"operator", | |
1531 | @"value", | |
1532 | nil]; | |
2388b078 JF |
1533 | } |
1534 | ||
83682c75 JF |
1535 | - (NSArray *) attributeKeys { |
1536 | return [[self class] _attributeKeys]; | |
2388b078 JF |
1537 | } |
1538 | ||
83682c75 JF |
1539 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
1540 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1541 | } | |
1542 | ||
1543 | - (NSString *) operator { | |
1544 | return operator_; | |
1545 | } | |
1546 | ||
1547 | - (NSString *) value { | |
1548 | return value_; | |
1549 | } | |
1550 | ||
1551 | @end | |
1552 | /* }}} */ | |
810c9763 JF |
1553 | /* CydiaClause Class {{{ */ |
1554 | @interface CydiaClause : NSObject { | |
7b33d201 JF |
1555 | _H<NSString> package_; |
1556 | _H<CydiaOperation> version_; | |
83682c75 JF |
1557 | } |
1558 | ||
83682c75 JF |
1559 | - (NSString *) package; |
1560 | - (CydiaOperation *) version; | |
1561 | ||
1562 | @end | |
1563 | ||
810c9763 | 1564 | @implementation CydiaClause |
83682c75 | 1565 | |
83682c75 JF |
1566 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
1567 | if ((self = [super init]) != nil) { | |
7b33d201 | 1568 | package_ = [NSString stringWithUTF8String:dep.TargetPkg().Name()]; |
83682c75 JF |
1569 | |
1570 | if (const char *version = dep.TargetVer()) | |
7b33d201 | 1571 | version_ = [[[CydiaOperation alloc] initWithOperator:dep.CompType() value:version] autorelease]; |
83682c75 | 1572 | else |
7b33d201 | 1573 | version_ = (id) [NSNull null]; |
83682c75 JF |
1574 | } return self; |
1575 | } | |
1576 | ||
1577 | + (NSArray *) _attributeKeys { | |
1578 | return [NSArray arrayWithObjects: | |
1579 | @"package", | |
83682c75 JF |
1580 | @"version", |
1581 | nil]; | |
1582 | } | |
1583 | ||
1584 | - (NSArray *) attributeKeys { | |
1585 | return [[self class] _attributeKeys]; | |
1586 | } | |
1587 | ||
1588 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1589 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1590 | } | |
1591 | ||
83682c75 JF |
1592 | - (NSString *) package { |
1593 | return package_; | |
1594 | } | |
1595 | ||
1596 | - (CydiaOperation *) version { | |
1597 | return version_; | |
2388b078 JF |
1598 | } |
1599 | ||
810c9763 JF |
1600 | @end |
1601 | /* }}} */ | |
1602 | /* CydiaRelation Class {{{ */ | |
1603 | @interface CydiaRelation : NSObject { | |
7b33d201 JF |
1604 | _H<NSString> relationship_; |
1605 | _H<NSMutableArray> clauses_; | |
810c9763 JF |
1606 | } |
1607 | ||
1608 | - (NSString *) relationship; | |
1609 | - (NSArray *) clauses; | |
1610 | ||
1611 | @end | |
1612 | ||
1613 | @implementation CydiaRelation | |
1614 | ||
810c9763 JF |
1615 | - (id) initWithIterator:(pkgCache::DepIterator &)dep { |
1616 | if ((self = [super init]) != nil) { | |
7b33d201 JF |
1617 | relationship_ = [NSString stringWithUTF8String:dep.DepType()]; |
1618 | clauses_ = [NSMutableArray arrayWithCapacity:8]; | |
810c9763 JF |
1619 | |
1620 | pkgCache::DepIterator start; | |
1621 | pkgCache::DepIterator end; | |
1622 | dep.GlobOr(start, end); // ++dep | |
1623 | ||
1624 | _forever { | |
1625 | [clauses_ addObject:[[[CydiaClause alloc] initWithIterator:start] autorelease]]; | |
1626 | ||
1627 | // yes, seriously. (wtf?) | |
1628 | if (start == end) | |
1629 | break; | |
1630 | ++start; | |
1631 | } | |
1632 | } return self; | |
1633 | } | |
1634 | ||
1635 | + (NSArray *) _attributeKeys { | |
1636 | return [NSArray arrayWithObjects: | |
1637 | @"clauses", | |
1638 | @"relationship", | |
1639 | nil]; | |
1640 | } | |
1641 | ||
1642 | - (NSArray *) attributeKeys { | |
1643 | return [[self class] _attributeKeys]; | |
1644 | } | |
1645 | ||
1646 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1647 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1648 | } | |
1649 | ||
1650 | - (NSString *) relationship { | |
1651 | return relationship_; | |
1652 | } | |
1653 | ||
1654 | - (NSArray *) clauses { | |
1655 | return clauses_; | |
1656 | } | |
1657 | ||
1658 | - (void) addClause:(CydiaClause *)clause { | |
1659 | [clauses_ addObject:clause]; | |
1660 | } | |
1661 | ||
dc5812ec | 1662 | @end |
b4d89997 | 1663 | /* }}} */ |
36bb2ca2 | 1664 | /* Package Class {{{ */ |
12016ee5 JF |
1665 | struct ParsedPackage { |
1666 | CYString tagline_; | |
1667 | ||
1668 | CYString icon_; | |
1669 | ||
1670 | CYString depiction_; | |
1671 | CYString homepage_; | |
1672 | ||
1673 | CYString sponsor_; | |
1674 | CYString author_; | |
1675 | ||
1676 | CYString bugs_; | |
1677 | CYString support_; | |
1678 | }; | |
1679 | ||
36bb2ca2 | 1680 | @interface Package : NSObject { |
97a3d89e JF |
1681 | uint32_t era_ : 26; |
1682 | uint32_t role_ : 3; | |
aab28f8b JF |
1683 | uint32_t essential_ : 1; |
1684 | uint32_t obsolete_ : 1; | |
1685 | uint32_t ignored_ : 1; | |
1686 | ||
68d927e2 | 1687 | apr_pool_t *pool_; |
a1440b10 | 1688 | |
9c5737d5 JF |
1689 | uint32_t rank_; |
1690 | ||
aab28f8b JF |
1691 | _transient Database *database_; |
1692 | ||
7376b55c | 1693 | pkgCache::VerIterator version_; |
36bb2ca2 | 1694 | pkgCache::PkgIterator iterator_; |
36bb2ca2 | 1695 | pkgCache::VerFileIterator file_; |
bbb879fb | 1696 | |
aab28f8b JF |
1697 | CYString id_; |
1698 | CYString name_; | |
807ae6d7 | 1699 | |
4c0ed943 | 1700 | CYString latest_; |
6a155117 | 1701 | CYString installed_; |
dc5812ec | 1702 | |
fe33a23e | 1703 | const char *section_; |
aab28f8b | 1704 | _transient NSString *section$_; |
f79a4512 | 1705 | |
7b33d201 | 1706 | _H<Source> source_; |
2388b078 | 1707 | |
bb6bb6d6 | 1708 | PackageValue *metadata_; |
aab28f8b | 1709 | ParsedPackage *parsed_; |
94b0b3e5 | 1710 | |
7b33d201 | 1711 | _H<NSMutableArray> tags_; |
b4d89997 JF |
1712 | } |
1713 | ||
7376b55c | 1714 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; |
f79a4512 | 1715 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database; |
b4d89997 | 1716 | |
2388b078 | 1717 | - (pkgCache::PkgIterator) iterator; |
68d927e2 | 1718 | - (void) parse; |
2388b078 | 1719 | |
36bb2ca2 | 1720 | - (NSString *) section; |
dec6029f JF |
1721 | - (NSString *) simpleSection; |
1722 | ||
f79a4512 JF |
1723 | - (NSString *) longSection; |
1724 | - (NSString *) shortSection; | |
1725 | ||
a3328c28 JF |
1726 | - (NSString *) uri; |
1727 | ||
7aa82ca2 | 1728 | - (MIMEAddress *) maintainer; |
36bb2ca2 | 1729 | - (size_t) size; |
eef4ccaf JF |
1730 | - (NSString *) longDescription; |
1731 | - (NSString *) shortDescription; | |
808c6eb6 | 1732 | - (unichar) index; |
dc5812ec | 1733 | |
94b0b3e5 | 1734 | - (PackageValue *) metadata; |
31bc18a7 | 1735 | - (time_t) seen; |
94b0b3e5 JF |
1736 | |
1737 | - (bool) subscribed; | |
1738 | - (bool) setSubscribed:(bool)subscribed; | |
1739 | ||
807ae6d7 | 1740 | - (BOOL) ignored; |
9e98e020 | 1741 | |
36bb2ca2 JF |
1742 | - (NSString *) latest; |
1743 | - (NSString *) installed; | |
6a155117 | 1744 | - (BOOL) uninstalled; |
5a09ae08 JF |
1745 | |
1746 | - (BOOL) valid; | |
31f3cfff | 1747 | - (BOOL) upgradableAndEssential:(BOOL)essential; |
36bb2ca2 JF |
1748 | - (BOOL) essential; |
1749 | - (BOOL) broken; | |
7d2ac47f | 1750 | - (BOOL) unfiltered; |
6d9712c4 | 1751 | - (BOOL) visible; |
b4d89997 | 1752 | |
9bedffaa JF |
1753 | - (BOOL) half; |
1754 | - (BOOL) halfConfigured; | |
1755 | - (BOOL) halfInstalled; | |
1756 | - (BOOL) hasMode; | |
1757 | - (NSString *) mode; | |
1758 | ||
36bb2ca2 JF |
1759 | - (NSString *) id; |
1760 | - (NSString *) name; | |
770f2a8e | 1761 | - (UIImage *) icon; |
6f1a15d9 | 1762 | - (NSString *) homepage; |
25a2158d | 1763 | - (NSString *) depiction; |
7aa82ca2 | 1764 | - (MIMEAddress *) author; |
b4d89997 | 1765 | |
dc63e78f JF |
1766 | - (NSString *) support; |
1767 | ||
affeffc7 | 1768 | - (NSArray *) files; |
affeffc7 JF |
1769 | - (NSArray *) warnings; |
1770 | - (NSArray *) applications; | |
2388b078 | 1771 | |
36bb2ca2 | 1772 | - (Source *) source; |
b4d89997 | 1773 | |
9c5737d5 | 1774 | - (uint32_t) rank; |
d84597fe | 1775 | - (BOOL) matches:(NSArray *)query; |
b4d89997 | 1776 | |
7b0ce2da | 1777 | - (bool) hasSupportingRole; |
6d9712c4 | 1778 | - (BOOL) hasTag:(NSString *)tag; |
c390d3ab | 1779 | - (NSString *) primaryPurpose; |
770f2a8e | 1780 | - (NSArray *) purposes; |
3bd1c2a2 | 1781 | - (bool) isCommercial; |
6d9712c4 | 1782 | |
cd95e390 JF |
1783 | - (void) setIndex:(size_t)index; |
1784 | ||
df213583 JF |
1785 | - (CYString &) cyname; |
1786 | ||
f79a4512 | 1787 | - (uint32_t) compareBySection:(NSArray *)sections; |
807ae6d7 | 1788 | |
36bb2ca2 JF |
1789 | - (void) install; |
1790 | - (void) remove; | |
06aa974d | 1791 | |
d84597fe | 1792 | - (bool) isUnfilteredAndSearchedForBy:(NSArray *)query; |
01d93940 | 1793 | - (bool) isUnfilteredAndSelectedForBy:(NSString *)search; |
e4f3d6e9 | 1794 | - (bool) isInstalledAndUnfiltered:(NSNumber *)number; |
670a0494 | 1795 | - (bool) isVisibleInSection:(NSString *)section; |
0a3b45ef | 1796 | - (bool) isVisibleInSource:(Source *)source; |
b4d89997 | 1797 | |
36bb2ca2 | 1798 | @end |
b4d89997 | 1799 | |
f79a4512 JF |
1800 | uint32_t PackageChangesRadix(Package *self, void *) { |
1801 | union { | |
1802 | uint32_t key; | |
1803 | ||
1804 | struct { | |
1805 | uint32_t timestamp : 30; | |
1806 | uint32_t ignored : 1; | |
1807 | uint32_t upgradable : 1; | |
1808 | } bits; | |
1809 | } value; | |
1810 | ||
1811 | bool upgradable([self upgradableAndEssential:YES]); | |
1812 | value.bits.upgradable = upgradable ? 1 : 0; | |
1813 | ||
1814 | if (upgradable) { | |
1815 | value.bits.timestamp = 0; | |
1816 | value.bits.ignored = [self ignored] ? 0 : 1; | |
1817 | value.bits.upgradable = 1; | |
1818 | } else { | |
36047c66 | 1819 | value.bits.timestamp = [self seen] >> 2; |
f79a4512 JF |
1820 | value.bits.ignored = 0; |
1821 | value.bits.upgradable = 0; | |
1822 | } | |
1823 | ||
1824 | return _not(uint32_t) - value.key; | |
1825 | } | |
1826 | ||
df213583 JF |
1827 | uint32_t PackagePrefixRadix(Package *self, void *context) { |
1828 | size_t offset(reinterpret_cast<size_t>(context)); | |
1829 | CYString &name([self cyname]); | |
677b8415 | 1830 | |
df213583 JF |
1831 | size_t size(name.size()); |
1832 | if (size == 0) | |
1833 | return 0; | |
1834 | char *text(name.data()); | |
677b8415 | 1835 | |
df213583 JF |
1836 | size_t zeros; |
1837 | if (!isdigit(text[0])) | |
1838 | zeros = 0; | |
1839 | else { | |
1840 | size_t digits(1); | |
1841 | while (size != digits && isdigit(text[digits])) | |
1842 | if (++digits == 4) | |
1843 | break; | |
1844 | zeros = 4 - digits; | |
1845 | } | |
677b8415 | 1846 | |
df213583 JF |
1847 | uint8_t data[4]; |
1848 | ||
df213583 JF |
1849 | if (offset == 0 && zeros != 0) { |
1850 | memset(data, '0', zeros); | |
1851 | memcpy(data + zeros, text, 4 - zeros); | |
1852 | } else { | |
1853 | /* XXX: there's some danger here if you request a non-zero offset < 4 and it gets zero padded */ | |
1854 | if (size <= offset - zeros) | |
1855 | return 0; | |
1856 | ||
1857 | text += offset - zeros; | |
1858 | size -= offset - zeros; | |
1859 | ||
1860 | if (size >= 4) | |
1861 | memcpy(data, text, 4); | |
1862 | else { | |
1863 | memcpy(data, text, size); | |
1864 | memset(data + size, 0, 4 - size); | |
1865 | } | |
1866 | ||
1867 | for (size_t i(0); i != 4; ++i) | |
1868 | if (isalpha(data[i])) | |
a7a59ee1 | 1869 | data[i] |= 0x20; |
df213583 JF |
1870 | } |
1871 | ||
1872 | if (offset == 0) | |
a7a59ee1 JF |
1873 | if (data[0] == '@') |
1874 | data[0] = 0x7f; | |
1875 | else | |
1876 | data[0] = (data[0] & 0x1f) | "\x80\x00\xc0\x40"[data[0] >> 6]; | |
df213583 JF |
1877 | |
1878 | /* XXX: ntohl may be more honest */ | |
1879 | return OSSwapInt32(*reinterpret_cast<uint32_t *>(data)); | |
1880 | } | |
1881 | ||
1882 | CYString &(*PackageName)(Package *self, SEL sel); | |
1883 | ||
1884 | CFComparisonResult PackageNameCompare(Package *lhs, Package *rhs, void *arg) { | |
1885 | _profile(PackageNameCompare) | |
1886 | CYString &lhi(PackageName(lhs, @selector(cyname))); | |
1887 | CYString &rhi(PackageName(rhs, @selector(cyname))); | |
1888 | CFStringRef lhn(lhi), rhn(rhi); | |
677b8415 | 1889 | |
5358f56f JF |
1890 | if (lhn == NULL) |
1891 | return rhn == NULL ? NSOrderedSame : NSOrderedAscending; | |
1892 | else if (rhn == NULL) | |
1893 | return NSOrderedDescending; | |
1894 | ||
677b8415 | 1895 | _profile(PackageNameCompare$NumbersLast) |
df213583 | 1896 | if (!lhi.empty() && !rhi.empty()) { |
677b8415 JF |
1897 | UniChar lhc(CFStringGetCharacterAtIndex(lhn, 0)); |
1898 | UniChar rhc(CFStringGetCharacterAtIndex(rhn, 0)); | |
1899 | bool lha(CFUniCharIsMemberOf(lhc, kCFUniCharLetterCharacterSet)); | |
1900 | if (lha != CFUniCharIsMemberOf(rhc, kCFUniCharLetterCharacterSet)) | |
1901 | return lha ? NSOrderedAscending : NSOrderedDescending; | |
1902 | } | |
1903 | _end | |
1904 | ||
df213583 JF |
1905 | CFIndex length = CFStringGetLength(lhn); |
1906 | ||
677b8415 JF |
1907 | _profile(PackageNameCompare$Compare) |
1908 | return CFStringCompareWithOptionsAndLocale(lhn, rhn, CFRangeMake(0, length), LaxCompareFlags_, Locale_); | |
1909 | _end | |
1910 | _end | |
1911 | } | |
1912 | ||
eef4ccaf JF |
1913 | CFComparisonResult PackageNameCompare_(Package **lhs, Package **rhs, void *context) { |
1914 | return PackageNameCompare(*lhs, *rhs, context); | |
677b8415 JF |
1915 | } |
1916 | ||
1917 | struct PackageNameOrdering : | |
1918 | std::binary_function<Package *, Package *, bool> | |
1919 | { | |
1920 | _finline bool operator ()(Package *lhs, Package *rhs) const { | |
1921 | return PackageNameCompare(lhs, rhs, NULL) == NSOrderedAscending; | |
1922 | } | |
1923 | }; | |
1924 | ||
36bb2ca2 | 1925 | @implementation Package |
b4d89997 | 1926 | |
df213583 JF |
1927 | - (NSString *) description { |
1928 | return [NSString stringWithFormat:@"<Package:%@>", static_cast<NSString *>(name_)]; | |
1929 | } | |
1930 | ||
36bb2ca2 | 1931 | - (void) dealloc { |
12016ee5 JF |
1932 | if (parsed_ != NULL) |
1933 | delete parsed_; | |
36bb2ca2 | 1934 | [super dealloc]; |
b4d89997 JF |
1935 | } |
1936 | ||
3bd1c2a2 | 1937 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 1938 | if (false); |
10d63f9e JF |
1939 | else if (selector == @selector(clear)) |
1940 | return @"clear"; | |
2cd1afd9 JF |
1941 | else if (selector == @selector(getField:)) |
1942 | return @"getField"; | |
e58ff941 | 1943 | else if (selector == @selector(hasTag:)) |
3bd1c2a2 | 1944 | return @"hasTag"; |
10d63f9e JF |
1945 | else if (selector == @selector(install)) |
1946 | return @"install"; | |
1947 | else if (selector == @selector(remove)) | |
1948 | return @"remove"; | |
3bd1c2a2 JF |
1949 | else |
1950 | return nil; | |
1951 | } | |
1952 | ||
1953 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
1954 | return [self webScriptNameForSelector:selector] == nil; | |
1955 | } | |
1956 | ||
6f1a15d9 | 1957 | + (NSArray *) _attributeKeys { |
e58ff941 JF |
1958 | return [NSArray arrayWithObjects: |
1959 | @"applications", | |
1960 | @"author", | |
1961 | @"depiction", | |
1962 | @"essential", | |
1963 | @"homepage", | |
1964 | @"icon", | |
1965 | @"id", | |
1966 | @"installed", | |
1967 | @"latest", | |
1968 | @"longDescription", | |
1969 | @"longSection", | |
1970 | @"maintainer", | |
1971 | @"mode", | |
1972 | @"name", | |
1973 | @"purposes", | |
83682c75 | 1974 | @"relations", |
e58ff941 | 1975 | @"section", |
de1ace71 | 1976 | @"selection", |
e58ff941 JF |
1977 | @"shortDescription", |
1978 | @"shortSection", | |
1979 | @"simpleSection", | |
1980 | @"size", | |
1981 | @"source", | |
1982 | @"sponsor", | |
5959b596 | 1983 | @"state", |
e58ff941 | 1984 | @"support", |
82aa2434 | 1985 | @"tags", |
e58ff941 JF |
1986 | @"warnings", |
1987 | nil]; | |
6f1a15d9 JF |
1988 | } |
1989 | ||
1990 | - (NSArray *) attributeKeys { | |
1991 | return [[self class] _attributeKeys]; | |
1992 | } | |
1993 | ||
1994 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
1995 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
1996 | } | |
1997 | ||
83682c75 JF |
1998 | - (NSArray *) relations { |
1999 | @synchronized (database_) { | |
2000 | NSMutableArray *relations([NSMutableArray arrayWithCapacity:16]); | |
810c9763 JF |
2001 | for (pkgCache::DepIterator dep(version_.DependsList()); !dep.end(); ++dep) |
2002 | [relations addObject:[[[CydiaRelation alloc] initWithIterator:dep] autorelease]]; | |
83682c75 JF |
2003 | return relations; |
2004 | } } | |
2005 | ||
2cd1afd9 JF |
2006 | - (NSString *) getField:(NSString *)name { |
2007 | @synchronized (database_) { | |
2008 | if ([database_ era] != era_ || file_.end()) | |
2009 | return nil; | |
2010 | ||
2011 | pkgRecords::Parser &parser([database_ records]->Lookup(file_)); | |
2012 | ||
2013 | const char *start, *end; | |
2014 | if (!parser.Find([name UTF8String], start, end)) | |
2015 | return (NSString *) [NSNull null]; | |
2016 | ||
2017 | return [(NSString *) CYStringCreate(start, end - start) autorelease]; | |
2018 | } } | |
2019 | ||
68d927e2 | 2020 | - (void) parse { |
12016ee5 | 2021 | if (parsed_ != NULL) |
68d927e2 | 2022 | return; |
12016ee5 JF |
2023 | @synchronized (database_) { |
2024 | if ([database_ era] != era_ || file_.end()) | |
68d927e2 JF |
2025 | return; |
2026 | ||
12016ee5 JF |
2027 | ParsedPackage *parsed(new ParsedPackage); |
2028 | parsed_ = parsed; | |
2029 | ||
68d927e2 JF |
2030 | _profile(Package$parse) |
2031 | pkgRecords::Parser *parser; | |
2032 | ||
2033 | _profile(Package$parse$Lookup) | |
2034 | parser = &[database_ records]->Lookup(file_); | |
2035 | _end | |
2036 | ||
2037 | CYString website; | |
2038 | ||
2039 | _profile(Package$parse$Find) | |
2040 | struct { | |
2041 | const char *name_; | |
2042 | CYString *value_; | |
2043 | } names[] = { | |
12016ee5 JF |
2044 | {"icon", &parsed->icon_}, |
2045 | {"depiction", &parsed->depiction_}, | |
2046 | {"homepage", &parsed->homepage_}, | |
68d927e2 | 2047 | {"website", &website}, |
12016ee5 JF |
2048 | {"bugs", &parsed->bugs_}, |
2049 | {"support", &parsed->support_}, | |
2050 | {"sponsor", &parsed->sponsor_}, | |
2051 | {"author", &parsed->author_}, | |
68d927e2 JF |
2052 | }; |
2053 | ||
2054 | for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) { | |
2055 | const char *start, *end; | |
2056 | ||
2057 | if (parser->Find(names[i].name_, start, end)) { | |
2058 | CYString &value(*names[i].value_); | |
2059 | _profile(Package$parse$Value) | |
2060 | value.set(pool_, start, end - start); | |
2061 | _end | |
2062 | } | |
2063 | } | |
2064 | _end | |
2065 | ||
2066 | _profile(Package$parse$Tagline) | |
2067 | const char *start, *end; | |
0dd0c302 | 2068 | if (parser->ShortDesc(start, end)) { |
68d927e2 JF |
2069 | const char *stop(reinterpret_cast<const char *>(memchr(start, '\n', end - start))); |
2070 | if (stop == NULL) | |
2071 | stop = end; | |
2072 | while (stop != start && stop[-1] == '\r') | |
2073 | --stop; | |
12016ee5 | 2074 | parsed->tagline_.set(pool_, start, stop - start); |
68d927e2 JF |
2075 | } |
2076 | _end | |
2077 | ||
2078 | _profile(Package$parse$Retain) | |
12016ee5 JF |
2079 | if (parsed->homepage_.empty()) |
2080 | parsed->homepage_ = website; | |
2081 | if (parsed->homepage_ == parsed->depiction_) | |
2082 | parsed->homepage_.clear(); | |
68d927e2 JF |
2083 | _end |
2084 | _end | |
12016ee5 | 2085 | } } |
68d927e2 | 2086 | |
7376b55c | 2087 | - (Package *) initWithVersion:(pkgCache::VerIterator)version withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { |
a1440b10 | 2088 | if ((self = [super init]) != nil) { |
7376b55c | 2089 | _profile(Package$initWithVersion) |
68d927e2 | 2090 | pool_ = pool; |
a1440b10 | 2091 | |
36bb2ca2 | 2092 | database_ = database; |
aab28f8b | 2093 | era_ = [database era]; |
b4d89997 | 2094 | |
aab28f8b | 2095 | version_ = version; |
2083b866 | 2096 | |
aab28f8b JF |
2097 | pkgCache::PkgIterator iterator(version.ParentPkg()); |
2098 | iterator_ = iterator; | |
06aa974d | 2099 | |
aab28f8b | 2100 | _profile(Package$initWithVersion$Version) |
68d927e2 JF |
2101 | if (!version_.end()) |
2102 | file_ = version_.FileList(); | |
2103 | else { | |
2104 | pkgCache &cache([database_ cache]); | |
2105 | file_ = pkgCache::VerFileIterator(cache, cache.VerFileP); | |
2106 | } | |
2107 | _end | |
808c6eb6 | 2108 | |
aab28f8b | 2109 | _profile(Package$initWithVersion$Cache) |
aab28f8b JF |
2110 | name_.set(NULL, iterator.Display()); |
2111 | ||
2112 | latest_.set(NULL, StripVersion_(version_.VerStr())); | |
2113 | ||
2114 | pkgCache::VerIterator current(iterator.CurrentVer()); | |
2115 | if (!current.end()) | |
2116 | installed_.set(NULL, StripVersion_(current.VerStr())); | |
808c6eb6 JF |
2117 | _end |
2118 | ||
7376b55c | 2119 | _profile(Package$initWithVersion$Tags) |
aab28f8b | 2120 | pkgCache::TagIterator tag(iterator.TagList()); |
0dd0c302 | 2121 | if (!tag.end()) { |
7b33d201 | 2122 | tags_ = [NSMutableArray arrayWithCapacity:8]; |
0dd0c302 JF |
2123 | do { |
2124 | const char *name(tag.Name()); | |
3931b718 | 2125 | [tags_ addObject:[(NSString *)CYStringCreate(name) autorelease]]; |
0a377825 | 2126 | |
97a3d89e JF |
2127 | if (role_ == 0 && strncmp(name, "role::", 6) == 0 /*&& strcmp(name, "role::leaper") != 0*/) { |
2128 | if (strcmp(name + 6, "enduser") == 0) | |
2129 | role_ = 1; | |
2130 | else if (strcmp(name + 6, "hacker") == 0) | |
2131 | role_ = 2; | |
2132 | else if (strcmp(name + 6, "developer") == 0) | |
2133 | role_ = 3; | |
2134 | else if (strcmp(name + 6, "cydia") == 0) | |
2135 | role_ = 7; | |
2136 | else | |
2137 | role_ = 4; | |
2138 | } | |
0a377825 JF |
2139 | |
2140 | if (strncmp(name, "cydia::", 7) == 0) { | |
2141 | if (strcmp(name + 7, "essential") == 0) | |
2142 | essential_ = true; | |
2143 | else if (strcmp(name + 7, "obsolete") == 0) | |
2144 | obsolete_ = true; | |
2145 | } | |
2146 | ||
0dd0c302 JF |
2147 | ++tag; |
2148 | } while (!tag.end()); | |
2149 | } | |
808c6eb6 | 2150 | _end |
7b0ce2da | 2151 | |
7376b55c | 2152 | _profile(Package$initWithVersion$Metadata) |
029c8b74 JF |
2153 | const char *mixed(iterator.Name()); |
2154 | size_t size(strlen(mixed)); | |
2155 | char lower[size + 1]; | |
2156 | ||
2157 | for (size_t i(0); i != size; ++i) | |
2158 | lower[i] = mixed[i] | 0x20; | |
2159 | lower[size] = '\0'; | |
2160 | ||
2161 | PackageValue *metadata(PackageFind(lower, size)); | |
bb6bb6d6 | 2162 | metadata_ = metadata; |
677b8415 | 2163 | |
029c8b74 JF |
2164 | id_.set(NULL, metadata->name_, size); |
2165 | ||
94b0b3e5 JF |
2166 | const char *latest(version_.VerStr()); |
2167 | size_t length(strlen(latest)); | |
a4b0ec52 | 2168 | |
94b0b3e5 | 2169 | uint16_t vhash(hashlittle(latest, length)); |
677b8415 | 2170 | |
94b0b3e5 JF |
2171 | size_t capped(std::min<size_t>(8, length)); |
2172 | latest = latest + length - capped; | |
677b8415 | 2173 | |
94b0b3e5 JF |
2174 | if (metadata->first_ == 0) |
2175 | metadata->first_ = now_; | |
808c6eb6 | 2176 | |
94b0b3e5 | 2177 | if (metadata->vhash_ != vhash || strncmp(metadata->version_, latest, sizeof(metadata->version_)) != 0) { |
94b0b3e5 JF |
2178 | strncpy(metadata->version_, latest, sizeof(metadata->version_)); |
2179 | metadata->vhash_ = vhash; | |
2f856365 JF |
2180 | metadata->last_ = now_; |
2181 | } else if (metadata->last_ == 0) | |
2182 | metadata->last_ = metadata->first_; | |
808c6eb6 | 2183 | _end |
a1440b10 | 2184 | |
7376b55c | 2185 | _profile(Package$initWithVersion$Section) |
fe33a23e | 2186 | section_ = iterator.Section(); |
f79a4512 | 2187 | _end |
a1440b10 | 2188 | |
aab28f8b JF |
2189 | _profile(Package$initWithVersion$Flags) |
2190 | essential_ |= ((iterator->Flags & pkgCache::Flag::Essential) == 0 ? NO : YES); | |
2191 | ignored_ = iterator->SelectedState == pkgCache::State::Hold; | |
4fa77608 | 2192 | _end |
3dd53516 | 2193 | _end } return self; |
dc5812ec JF |
2194 | } |
2195 | ||
f79a4512 | 2196 | + (Package *) packageWithIterator:(pkgCache::PkgIterator)iterator withZone:(NSZone *)zone inPool:(apr_pool_t *)pool database:(Database *)database { |
b1ce61ec JF |
2197 | pkgCache::VerIterator version; |
2198 | ||
2199 | _profile(Package$packageWithIterator$GetCandidateVer) | |
2200 | version = [database policy]->GetCandidateVer(iterator); | |
2201 | _end | |
2202 | ||
7376b55c JF |
2203 | if (version.end()) |
2204 | return nil; | |
b1ce61ec | 2205 | |
8564efc1 JF |
2206 | Package *package; |
2207 | ||
2208 | _profile(Package$packageWithIterator$Allocate) | |
2209 | package = [Package allocWithZone:zone]; | |
2210 | _end | |
2211 | ||
2212 | _profile(Package$packageWithIterator$Initialize) | |
2213 | package = [package | |
2214 | initWithVersion:version | |
2215 | withZone:zone | |
2216 | inPool:pool | |
2217 | database:database | |
2218 | ]; | |
2219 | _end | |
2220 | ||
2221 | _profile(Package$packageWithIterator$Autorelease) | |
2222 | package = [package autorelease]; | |
2223 | _end | |
2224 | ||
2225 | return package; | |
3dd53516 | 2226 | } |
dc5812ec | 2227 | |
2388b078 JF |
2228 | - (pkgCache::PkgIterator) iterator { |
2229 | return iterator_; | |
2230 | } | |
2231 | ||
36bb2ca2 | 2232 | - (NSString *) section { |
f79a4512 | 2233 | if (section$_ == nil) { |
fe33a23e | 2234 | if (section_ == NULL) |
f79a4512 JF |
2235 | return nil; |
2236 | ||
fe33a23e JF |
2237 | _profile(Package$section$mappedSectionForPointer) |
2238 | section$_ = [database_ mappedSectionForPointer:section_]; | |
e4f3d6e9 | 2239 | _end |
f79a4512 | 2240 | } return section$_; |
dc5812ec JF |
2241 | } |
2242 | ||
dec6029f JF |
2243 | - (NSString *) simpleSection { |
2244 | if (NSString *section = [self section]) | |
2245 | return Simplify(section); | |
2246 | else | |
2247 | return nil; | |
a3328c28 | 2248 | } |
dec6029f | 2249 | |
f79a4512 | 2250 | - (NSString *) longSection { |
18873623 | 2251 | return LocalizeSection([self section]); |
f79a4512 JF |
2252 | } |
2253 | ||
2254 | - (NSString *) shortSection { | |
2255 | return [[NSBundle mainBundle] localizedStringForKey:[self simpleSection] value:nil table:@"Sections"]; | |
2256 | } | |
2257 | ||
a3328c28 JF |
2258 | - (NSString *) uri { |
2259 | return nil; | |
2260 | #if 0 | |
2261 | pkgIndexFile *index; | |
2262 | pkgCache::PkgFileIterator file(file_.File()); | |
2263 | if (![database_ list].FindIndex(file, index)) | |
2264 | return nil; | |
2265 | return [NSString stringWithUTF8String:iterator_->Path]; | |
2266 | //return [NSString stringWithUTF8String:file.Site()]; | |
2267 | //return [NSString stringWithUTF8String:index->ArchiveURI(file.FileName()).c_str()]; | |
2268 | #endif | |
dec6029f JF |
2269 | } |
2270 | ||
7aa82ca2 | 2271 | - (MIMEAddress *) maintainer { |
884171d6 JF |
2272 | @synchronized (database_) { |
2273 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2274 | return nil; |
884171d6 | 2275 | |
36bb2ca2 | 2276 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
00e2109e | 2277 | const std::string &maintainer(parser->Maintainer()); |
7aa82ca2 | 2278 | return maintainer.empty() ? nil : [MIMEAddress addressWithString:[NSString stringWithUTF8String:maintainer.c_str()]]; |
884171d6 | 2279 | } } |
8fe19fc1 | 2280 | |
36bb2ca2 | 2281 | - (size_t) size { |
884171d6 JF |
2282 | @synchronized (database_) { |
2283 | if ([database_ era] != era_ || version_.end()) | |
2284 | return 0; | |
2285 | ||
2286 | return version_->InstalledSize; | |
2287 | } } | |
dc5812ec | 2288 | |
eef4ccaf | 2289 | - (NSString *) longDescription { |
3dd53516 JF |
2290 | @synchronized (database_) { |
2291 | if ([database_ era] != era_ || file_.end()) | |
5a09ae08 | 2292 | return nil; |
3dd53516 | 2293 | |
36bb2ca2 | 2294 | pkgRecords::Parser *parser = &[database_ records]->Lookup(file_); |
2388b078 | 2295 | NSString *description([NSString stringWithUTF8String:parser->LongDesc().c_str()]); |
8fe19fc1 | 2296 | |
36bb2ca2 JF |
2297 | NSArray *lines = [description componentsSeparatedByString:@"\n"]; |
2298 | NSMutableArray *trimmed = [NSMutableArray arrayWithCapacity:([lines count] - 1)]; | |
2299 | if ([lines count] < 2) | |
2300 | return nil; | |
3178d79b | 2301 | |
36bb2ca2 | 2302 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet]; |
c4dcf2c2 | 2303 | for (size_t i(1), e([lines count]); i != e; ++i) { |
36bb2ca2 JF |
2304 | NSString *trim = [[lines objectAtIndex:i] stringByTrimmingCharactersInSet:whitespace]; |
2305 | [trimmed addObject:trim]; | |
2306 | } | |
3178d79b | 2307 | |
36bb2ca2 | 2308 | return [trimmed componentsJoinedByString:@"\n"]; |
3dd53516 | 2309 | } } |
dc5812ec | 2310 | |
eef4ccaf | 2311 | - (NSString *) shortDescription { |
12016ee5 | 2312 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->tagline_); |
eef4ccaf JF |
2313 | } |
2314 | ||
808c6eb6 JF |
2315 | - (unichar) index { |
2316 | _profile(Package$index) | |
677b8415 JF |
2317 | CFStringRef name((CFStringRef) [self name]); |
2318 | if (CFStringGetLength(name) == 0) | |
808c6eb6 | 2319 | return '#'; |
677b8415 JF |
2320 | UniChar character(CFStringGetCharacterAtIndex(name, 0)); |
2321 | if (!CFUniCharIsMemberOf(character, kCFUniCharLetterCharacterSet)) | |
808c6eb6 | 2322 | return '#'; |
447db19d | 2323 | return toupper(character); |
808c6eb6 | 2324 | _end |
36bb2ca2 | 2325 | } |
3178d79b | 2326 | |
94b0b3e5 | 2327 | - (PackageValue *) metadata { |
bb6bb6d6 | 2328 | return metadata_; |
807ae6d7 JF |
2329 | } |
2330 | ||
31bc18a7 | 2331 | - (time_t) seen { |
94b0b3e5 JF |
2332 | PackageValue *metadata([self metadata]); |
2333 | return metadata->subscribed_ ? metadata->last_ : metadata->first_; | |
3178d79b JF |
2334 | } |
2335 | ||
94b0b3e5 JF |
2336 | - (bool) subscribed { |
2337 | return [self metadata]->subscribed_; | |
2338 | } | |
2339 | ||
2340 | - (bool) setSubscribed:(bool)subscribed { | |
2341 | PackageValue *metadata([self metadata]); | |
2342 | if (metadata->subscribed_ == subscribed) | |
2343 | return false; | |
2344 | metadata->subscribed_ = subscribed; | |
2345 | return true; | |
807ae6d7 JF |
2346 | } |
2347 | ||
2348 | - (BOOL) ignored { | |
1b18f026 | 2349 | return ignored_; |
807ae6d7 JF |
2350 | } |
2351 | ||
36bb2ca2 JF |
2352 | - (NSString *) latest { |
2353 | return latest_; | |
8fe19fc1 JF |
2354 | } |
2355 | ||
36bb2ca2 JF |
2356 | - (NSString *) installed { |
2357 | return installed_; | |
3178d79b JF |
2358 | } |
2359 | ||
6a155117 JF |
2360 | - (BOOL) uninstalled { |
2361 | return installed_.empty(); | |
2362 | } | |
2363 | ||
5a09ae08 JF |
2364 | - (BOOL) valid { |
2365 | return !version_.end(); | |
2366 | } | |
2367 | ||
31f3cfff | 2368 | - (BOOL) upgradableAndEssential:(BOOL)essential { |
677b8415 JF |
2369 | _profile(Package$upgradableAndEssential) |
2370 | pkgCache::VerIterator current(iterator_.CurrentVer()); | |
2371 | if (current.end()) | |
e4f3d6e9 | 2372 | return essential && essential_; |
677b8415 | 2373 | else |
e4f3d6e9 | 2374 | return !version_.end() && version_ != current; |
677b8415 | 2375 | _end |
36bb2ca2 | 2376 | } |
3178d79b | 2377 | |
36bb2ca2 | 2378 | - (BOOL) essential { |
a1440b10 | 2379 | return essential_; |
3178d79b JF |
2380 | } |
2381 | ||
36bb2ca2 | 2382 | - (BOOL) broken { |
9bedffaa JF |
2383 | return [database_ cache][iterator_].InstBroken(); |
2384 | } | |
2385 | ||
7d2ac47f | 2386 | - (BOOL) unfiltered { |
4fa77608 | 2387 | _profile(Package$unfiltered$obsolete) |
cf48f656 | 2388 | if (_unlikely(obsolete_)) |
4fa77608 JF |
2389 | return false; |
2390 | _end | |
2391 | ||
2392 | _profile(Package$unfiltered$hasSupportingRole) | |
cf48f656 | 2393 | if (_unlikely(![self hasSupportingRole])) |
4fa77608 JF |
2394 | return false; |
2395 | _end | |
2396 | ||
e4f3d6e9 JF |
2397 | return true; |
2398 | } | |
4fa77608 | 2399 | |
e4f3d6e9 JF |
2400 | - (BOOL) visible { |
2401 | if (![self unfiltered]) | |
2402 | return false; | |
2403 | ||
fe33a23e JF |
2404 | NSString *section; |
2405 | ||
2406 | _profile(Package$visible$section) | |
2407 | section = [self section]; | |
2408 | _end | |
4fa77608 | 2409 | |
e4f3d6e9 | 2410 | _profile(Package$visible$isSectionVisible) |
45447dc3 | 2411 | if (!isSectionVisible(section)) |
4fa77608 JF |
2412 | return false; |
2413 | _end | |
2414 | ||
2415 | return true; | |
7d2ac47f JF |
2416 | } |
2417 | ||
9bedffaa | 2418 | - (BOOL) half { |
677b8415 | 2419 | unsigned char current(iterator_->CurrentState); |
9bedffaa JF |
2420 | return current == pkgCache::State::HalfConfigured || current == pkgCache::State::HalfInstalled; |
2421 | } | |
2422 | ||
2423 | - (BOOL) halfConfigured { | |
2424 | return iterator_->CurrentState == pkgCache::State::HalfConfigured; | |
2425 | } | |
2426 | ||
2427 | - (BOOL) halfInstalled { | |
2428 | return iterator_->CurrentState == pkgCache::State::HalfInstalled; | |
2429 | } | |
2430 | ||
2431 | - (BOOL) hasMode { | |
2432 | pkgDepCache::StateCache &state([database_ cache][iterator_]); | |
2433 | return state.Mode != pkgDepCache::ModeKeep; | |
2434 | } | |
2435 | ||
2436 | - (NSString *) mode { | |
2437 | pkgDepCache::StateCache &state([database_ cache][iterator_]); | |
2438 | ||
2439 | switch (state.Mode) { | |
2440 | case pkgDepCache::ModeDelete: | |
2441 | if ((state.iFlags & pkgDepCache::Purge) != 0) | |
f79a4512 | 2442 | return @"PURGE"; |
9bedffaa | 2443 | else |
f79a4512 | 2444 | return @"REMOVE"; |
9bedffaa | 2445 | case pkgDepCache::ModeKeep: |
dc63e78f | 2446 | if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2447 | return @"REINSTALL"; |
dc63e78f JF |
2448 | /*else if ((state.iFlags & pkgDepCache::AutoKept) != 0) |
2449 | return nil;*/ | |
9bedffaa JF |
2450 | else |
2451 | return nil; | |
9bedffaa | 2452 | case pkgDepCache::ModeInstall: |
dc63e78f | 2453 | /*if ((state.iFlags & pkgDepCache::ReInstall) != 0) |
f79a4512 | 2454 | return @"REINSTALL"; |
dc63e78f | 2455 | else*/ switch (state.Status) { |
9bedffaa | 2456 | case -1: |
f79a4512 | 2457 | return @"DOWNGRADE"; |
9bedffaa | 2458 | case 0: |
f79a4512 | 2459 | return @"INSTALL"; |
9bedffaa | 2460 | case 1: |
f79a4512 | 2461 | return @"UPGRADE"; |
9bedffaa | 2462 | case 2: |
f79a4512 | 2463 | return @"NEW_INSTALL"; |
670a0494 | 2464 | _nodefault |
9bedffaa | 2465 | } |
670a0494 | 2466 | _nodefault |
9bedffaa | 2467 | } |
8fe19fc1 JF |
2468 | } |
2469 | ||
36bb2ca2 JF |
2470 | - (NSString *) id { |
2471 | return id_; | |
8fe19fc1 JF |
2472 | } |
2473 | ||
36bb2ca2 | 2474 | - (NSString *) name { |
9fcbca29 | 2475 | return name_.empty() ? id_ : name_; |
36bb2ca2 | 2476 | } |
8fe19fc1 | 2477 | |
770f2a8e | 2478 | - (UIImage *) icon { |
dec6029f | 2479 | NSString *section = [self simpleSection]; |
770f2a8e JF |
2480 | |
2481 | UIImage *icon(nil); | |
12016ee5 JF |
2482 | if (parsed_ != NULL) |
2483 | if (NSString *href = parsed_->icon_) | |
2484 | if ([href hasPrefix:@"file:///"]) | |
2485 | // XXX: correct escaping | |
2486 | icon = [UIImage imageAtPath:[href substringFromIndex:7]]; | |
770f2a8e JF |
2487 | if (icon == nil) if (section != nil) |
2488 | icon = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, section]]; | |
b9956841 | 2489 | if (icon == nil) if (Source *source = [self source]) if (NSString *dicon = [source defaultIcon]) |
189a73d0 | 2490 | if ([dicon hasPrefix:@"file:///"]) |
ac308d3e | 2491 | // XXX: correct escaping |
189a73d0 | 2492 | icon = [UIImage imageAtPath:[dicon substringFromIndex:7]]; |
770f2a8e JF |
2493 | if (icon == nil) |
2494 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
2495 | return icon; | |
36bb2ca2 | 2496 | } |
8fe19fc1 | 2497 | |
6f1a15d9 | 2498 | - (NSString *) homepage { |
12016ee5 | 2499 | return parsed_ == NULL ? nil : static_cast<NSString *>(parsed_->homepage_); |
8fe19fc1 JF |
2500 | } |
2501 | ||
25a2158d | 2502 | - (NSString *) depiction { |
12016ee5 | 2503 | return parsed_ != NULL && !parsed_->depiction_.empty() ? parsed_->depiction_ : [[self source] depictionForPackage:id_]; |
25a2158d JF |
2504 | } |
2505 | ||
7aa82ca2 JF |
2506 | - (MIMEAddress *) sponsor { |
2507 | return parsed_ == NULL || parsed_->sponsor_.empty() ? nil : [MIMEAddress addressWithString:parsed_->sponsor_]; | |
795d26fc JF |
2508 | } |
2509 | ||
7aa82ca2 JF |
2510 | - (MIMEAddress *) author { |
2511 | return parsed_ == NULL || parsed_->author_.empty() ? nil : [MIMEAddress addressWithString:parsed_->author_]; | |
77fcccaf JF |
2512 | } |
2513 | ||
dc63e78f | 2514 | - (NSString *) support { |
12016ee5 | 2515 | return parsed_ != NULL && !parsed_->bugs_.empty() ? parsed_->bugs_ : [[self source] supportForPackage:id_]; |
dc63e78f JF |
2516 | } |
2517 | ||
affeffc7 | 2518 | - (NSArray *) files { |
9fcbca29 | 2519 | NSString *path = [NSString stringWithFormat:@"/var/lib/dpkg/info/%@.list", static_cast<NSString *>(id_)]; |
affeffc7 JF |
2520 | NSMutableArray *files = [NSMutableArray arrayWithCapacity:128]; |
2521 | ||
2522 | std::ifstream fin; | |
2523 | fin.open([path UTF8String]); | |
2524 | if (!fin.is_open()) | |
2525 | return nil; | |
2526 | ||
2527 | std::string line; | |
2528 | while (std::getline(fin, line)) | |
2529 | [files addObject:[NSString stringWithUTF8String:line.c_str()]]; | |
2530 | ||
2531 | return files; | |
2532 | } | |
2533 | ||
5959b596 JF |
2534 | - (NSString *) state { |
2535 | @synchronized (database_) { | |
2536 | if ([database_ era] != era_ || file_.end()) | |
2537 | return nil; | |
2538 | ||
2539 | switch (iterator_->CurrentState) { | |
2540 | case pkgCache::State::NotInstalled: | |
2541 | return @"NotInstalled"; | |
2542 | case pkgCache::State::UnPacked: | |
2543 | return @"UnPacked"; | |
2544 | case pkgCache::State::HalfConfigured: | |
2545 | return @"HalfConfigured"; | |
2546 | case pkgCache::State::HalfInstalled: | |
2547 | return @"HalfInstalled"; | |
2548 | case pkgCache::State::ConfigFiles: | |
2549 | return @"ConfigFiles"; | |
2550 | case pkgCache::State::Installed: | |
2551 | return @"Installed"; | |
2552 | case pkgCache::State::TriggersAwaited: | |
2553 | return @"TriggersAwaited"; | |
2554 | case pkgCache::State::TriggersPending: | |
2555 | return @"TriggersPending"; | |
2556 | } | |
2557 | ||
2558 | return (NSString *) [NSNull null]; | |
2559 | } } | |
2560 | ||
de1ace71 JF |
2561 | - (NSString *) selection { |
2562 | @synchronized (database_) { | |
2563 | if ([database_ era] != era_ || file_.end()) | |
2564 | return nil; | |
2565 | ||
2566 | switch (iterator_->SelectedState) { | |
2567 | case pkgCache::State::Unknown: | |
2568 | return @"Unknown"; | |
2569 | case pkgCache::State::Install: | |
2570 | return @"Install"; | |
2571 | case pkgCache::State::Hold: | |
2572 | return @"Hold"; | |
2573 | case pkgCache::State::DeInstall: | |
2574 | return @"DeInstall"; | |
2575 | case pkgCache::State::Purge: | |
2576 | return @"Purge"; | |
2577 | } | |
2578 | ||
2579 | return (NSString *) [NSNull null]; | |
2580 | } } | |
2581 | ||
affeffc7 JF |
2582 | - (NSArray *) warnings { |
2583 | NSMutableArray *warnings([NSMutableArray arrayWithCapacity:4]); | |
2584 | const char *name(iterator_.Name()); | |
2585 | ||
2586 | size_t length(strlen(name)); | |
2587 | if (length < 2) invalid: | |
43f3d7f6 | 2588 | [warnings addObject:UCLocalize("ILLEGAL_PACKAGE_IDENTIFIER")]; |
affeffc7 JF |
2589 | else for (size_t i(0); i != length; ++i) |
2590 | if ( | |
9dd60d81 JF |
2591 | /* XXX: technically this is not allowed */ |
2592 | (name[i] < 'A' || name[i] > 'Z') && | |
affeffc7 JF |
2593 | (name[i] < 'a' || name[i] > 'z') && |
2594 | (name[i] < '0' || name[i] > '9') && | |
2595 | (i == 0 || name[i] != '+' && name[i] != '-' && name[i] != '.') | |
2596 | ) goto invalid; | |
2597 | ||
2598 | if (strcmp(name, "cydia") != 0) { | |
2599 | bool cydia = false; | |
7623f855 | 2600 | bool user = false; |
9dd60d81 | 2601 | bool _private = false; |
affeffc7 JF |
2602 | bool stash = false; |
2603 | ||
9dd60d81 JF |
2604 | bool repository = [[self section] isEqualToString:@"Repositories"]; |
2605 | ||
affeffc7 JF |
2606 | if (NSArray *files = [self files]) |
2607 | for (NSString *file in files) | |
2608 | if (!cydia && [file isEqualToString:@"/Applications/Cydia.app"]) | |
2609 | cydia = true; | |
7623f855 JF |
2610 | else if (!user && [file isEqualToString:@"/User"]) |
2611 | user = true; | |
9dd60d81 JF |
2612 | else if (!_private && [file isEqualToString:@"/private"]) |
2613 | _private = true; | |
affeffc7 JF |
2614 | else if (!stash && [file isEqualToString:@"/var/stash"]) |
2615 | stash = true; | |
2616 | ||
9dd60d81 JF |
2617 | /* XXX: this is not sensitive enough. only some folders are valid. */ |
2618 | if (cydia && !repository) | |
43f3d7f6 | 2619 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"Cydia.app"]]; |
7623f855 JF |
2620 | if (user) |
2621 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/User"]]; | |
9dd60d81 | 2622 | if (_private) |
43f3d7f6 | 2623 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/private"]]; |
affeffc7 | 2624 | if (stash) |
43f3d7f6 | 2625 | [warnings addObject:[NSString stringWithFormat:UCLocalize("FILES_INSTALLED_TO"), @"/var/stash"]]; |
affeffc7 JF |
2626 | } |
2627 | ||
2628 | return [warnings count] == 0 ? nil : warnings; | |
2629 | } | |
2630 | ||
2631 | - (NSArray *) applications { | |
2632 | NSString *me([[NSBundle mainBundle] bundleIdentifier]); | |
2633 | ||
2634 | NSMutableArray *applications([NSMutableArray arrayWithCapacity:2]); | |
2635 | ||
2636 | static Pcre application_r("^/Applications/(.*)\\.app/Info.plist$"); | |
2637 | if (NSArray *files = [self files]) | |
2638 | for (NSString *file in files) | |
2639 | if (application_r(file)) { | |
2640 | NSDictionary *info([NSDictionary dictionaryWithContentsOfFile:file]); | |
2641 | NSString *id([info objectForKey:@"CFBundleIdentifier"]); | |
2642 | if ([id isEqualToString:me]) | |
2643 | continue; | |
2644 | ||
2645 | NSString *display([info objectForKey:@"CFBundleDisplayName"]); | |
2646 | if (display == nil) | |
2647 | display = application_r[1]; | |
2648 | ||
2649 | NSString *bundle([file stringByDeletingLastPathComponent]); | |
2650 | NSString *icon([info objectForKey:@"CFBundleIconFile"]); | |
2651 | if (icon == nil || [icon length] == 0) | |
2652 | icon = @"icon.png"; | |
2653 | NSURL *url([NSURL fileURLWithPath:[bundle stringByAppendingPathComponent:icon]]); | |
2654 | ||
2655 | NSMutableArray *application([NSMutableArray arrayWithCapacity:2]); | |
2656 | [applications addObject:application]; | |
2657 | ||
2658 | [application addObject:id]; | |
2659 | [application addObject:display]; | |
2660 | [application addObject:url]; | |
2661 | } | |
2662 | ||
2663 | return [applications count] == 0 ? nil : applications; | |
2664 | } | |
2665 | ||
36bb2ca2 | 2666 | - (Source *) source { |
5699667a | 2667 | if (source_ == nil) { |
a1440b10 JF |
2668 | @synchronized (database_) { |
2669 | if ([database_ era] != era_ || file_.end()) | |
5699667a JF |
2670 | source_ = (Source *) [NSNull null]; |
2671 | else | |
7b33d201 | 2672 | source_ = [database_ getSource:file_.File()] ?: (Source *) [NSNull null]; |
a1440b10 | 2673 | } |
bbb879fb JF |
2674 | } |
2675 | ||
5699667a | 2676 | return source_ == (Source *) [NSNull null] ? nil : source_; |
8fe19fc1 JF |
2677 | } |
2678 | ||
9c5737d5 JF |
2679 | - (uint32_t) rank { |
2680 | return rank_; | |
2681 | } | |
2682 | ||
d84597fe JF |
2683 | - (BOOL) matches:(NSArray *)query { |
2684 | if (query == nil || [query count] == 0) | |
36bb2ca2 | 2685 | return NO; |
8fe19fc1 | 2686 | |
9c5737d5 JF |
2687 | rank_ = 0; |
2688 | ||
2689 | NSString *string; | |
36bb2ca2 | 2690 | NSRange range; |
9c5737d5 | 2691 | NSUInteger length; |
8fe19fc1 | 2692 | |
d84597fe JF |
2693 | [self parse]; |
2694 | ||
9c5737d5 | 2695 | string = [self id]; |
d84597fe JF |
2696 | length = [string length]; |
2697 | ||
2698 | for (NSString *term in query) { | |
2699 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
2700 | if (range.location != NSNotFound) | |
2701 | rank_ -= 10 * 100000 / length; | |
2702 | } | |
8fe19fc1 | 2703 | |
9c5737d5 | 2704 | string = [self name]; |
8fe19fc1 | 2705 | |
d84597fe JF |
2706 | for (NSString *term in query) { |
2707 | range = [string rangeOfString:term options:MatchCompareOptions_]; | |
2708 | if (range.location != NSNotFound) | |
2709 | rank_ -= 6 * 100000 / length; | |
2710 | } | |
c4b530a7 | 2711 | |
9c5737d5 JF |
2712 | string = [self shortDescription]; |
2713 | length = [string length]; | |
d84597fe | 2714 | NSUInteger stop(std::min<NSUInteger>(length, 100)); |
df289c5a | 2715 | |
d84597fe JF |
2716 | for (NSString *term in query) { |
2717 | range = [string rangeOfString:term options:MatchCompareOptions_ range:NSMakeRange(0, stop)]; | |
2718 | if (range.location != NSNotFound) | |
2719 | rank_ -= 2 * 100000 / length; | |
2720 | } | |
8fe19fc1 | 2721 | |
9c5737d5 | 2722 | return rank_ != 0; |
36bb2ca2 | 2723 | } |
8fe19fc1 | 2724 | |
7b0ce2da | 2725 | - (bool) hasSupportingRole { |
97a3d89e | 2726 | if (role_ == 0) |
6d9712c4 | 2727 | return true; |
97a3d89e | 2728 | if (role_ == 1) |
7b0ce2da JF |
2729 | return true; |
2730 | if ([Role_ isEqualToString:@"User"]) | |
2731 | return false; | |
97a3d89e | 2732 | if (role_ == 2) |
7b0ce2da JF |
2733 | return true; |
2734 | if ([Role_ isEqualToString:@"Hacker"]) | |
2735 | return false; | |
97a3d89e | 2736 | if (role_ == 3) |
7b0ce2da JF |
2737 | return true; |
2738 | if ([Role_ isEqualToString:@"Developer"]) | |
2739 | return false; | |
2740 | _assert(false); | |
6d9712c4 JF |
2741 | } |
2742 | ||
82aa2434 JF |
2743 | - (NSArray *) tags { |
2744 | return tags_; | |
2745 | } | |
2746 | ||
6d9712c4 JF |
2747 | - (BOOL) hasTag:(NSString *)tag { |
2748 | return tags_ == nil ? NO : [tags_ containsObject:tag]; | |
2749 | } | |
2750 | ||
c390d3ab | 2751 | - (NSString *) primaryPurpose { |
7b33d201 | 2752 | for (NSString *tag in (NSArray *) tags_) |
c390d3ab JF |
2753 | if ([tag hasPrefix:@"purpose::"]) |
2754 | return [tag substringFromIndex:9]; | |
2755 | return nil; | |
2756 | } | |
2757 | ||
770f2a8e JF |
2758 | - (NSArray *) purposes { |
2759 | NSMutableArray *purposes([NSMutableArray arrayWithCapacity:2]); | |
7b33d201 | 2760 | for (NSString *tag in (NSArray *) tags_) |
770f2a8e JF |
2761 | if ([tag hasPrefix:@"purpose::"]) |
2762 | [purposes addObject:[tag substringFromIndex:9]]; | |
2763 | return [purposes count] == 0 ? nil : purposes; | |
2764 | } | |
2765 | ||
3bd1c2a2 JF |
2766 | - (bool) isCommercial { |
2767 | return [self hasTag:@"cydia::commercial"]; | |
2768 | } | |
2769 | ||
cd95e390 JF |
2770 | - (void) setIndex:(size_t)index { |
2771 | if (metadata_->index_ != index) | |
2772 | metadata_->index_ = index; | |
2773 | } | |
2774 | ||
df213583 JF |
2775 | - (CYString &) cyname { |
2776 | return name_.empty() ? id_ : name_; | |
f79a4512 JF |
2777 | } |
2778 | ||
f79a4512 JF |
2779 | - (uint32_t) compareBySection:(NSArray *)sections { |
2780 | NSString *section([self section]); | |
2781 | for (size_t i(0), e([sections count]); i != e; ++i) { | |
2782 | if ([section isEqualToString:[[sections objectAtIndex:i] name]]) | |
2783 | return i; | |
36bb2ca2 | 2784 | } |
3178d79b | 2785 | |
f79a4512 | 2786 | return _not(uint32_t); |
36bb2ca2 | 2787 | } |
3178d79b | 2788 | |
dc63e78f | 2789 | - (void) clear { |
c6ca67ba | 2790 | @synchronized (database_) { |
dc63e78f JF |
2791 | pkgProblemResolver *resolver = [database_ resolver]; |
2792 | resolver->Clear(iterator_); | |
c6ca67ba JF |
2793 | |
2794 | pkgCacheFile &cache([database_ cache]); | |
2795 | cache->SetReInstall(iterator_, false); | |
2796 | cache->MarkKeep(iterator_, false); | |
2797 | } } | |
dc63e78f | 2798 | |
36bb2ca2 | 2799 | - (void) install { |
c6ca67ba | 2800 | @synchronized (database_) { |
36bb2ca2 JF |
2801 | pkgProblemResolver *resolver = [database_ resolver]; |
2802 | resolver->Clear(iterator_); | |
2803 | resolver->Protect(iterator_); | |
c6ca67ba | 2804 | |
36bb2ca2 | 2805 | pkgCacheFile &cache([database_ cache]); |
c6ca67ba | 2806 | cache->SetReInstall(iterator_, false); |
36bb2ca2 | 2807 | cache->MarkInstall(iterator_, false); |
c6ca67ba | 2808 | |
36bb2ca2 JF |
2809 | pkgDepCache::StateCache &state((*cache)[iterator_]); |
2810 | if (!state.Install()) | |
2811 | cache->SetReInstall(iterator_, true); | |
c6ca67ba | 2812 | } } |
68a238ec | 2813 | |
36bb2ca2 | 2814 | - (void) remove { |
c6ca67ba | 2815 | @synchronized (database_) { |
36bb2ca2 JF |
2816 | pkgProblemResolver *resolver = [database_ resolver]; |
2817 | resolver->Clear(iterator_); | |
36bb2ca2 | 2818 | resolver->Remove(iterator_); |
c6ca67ba JF |
2819 | resolver->Protect(iterator_); |
2820 | ||
2821 | pkgCacheFile &cache([database_ cache]); | |
2822 | cache->SetReInstall(iterator_, false); | |
2823 | cache->MarkDelete(iterator_, true); | |
2824 | } } | |
8fe19fc1 | 2825 | |
d84597fe | 2826 | - (bool) isUnfilteredAndSearchedForBy:(NSArray *)query { |
808c6eb6 JF |
2827 | _profile(Package$isUnfilteredAndSearchedForBy) |
2828 | bool value(true); | |
2829 | ||
2830 | _profile(Package$isUnfilteredAndSearchedForBy$Unfiltered) | |
2831 | value &= [self unfiltered]; | |
2832 | _end | |
2833 | ||
2834 | _profile(Package$isUnfilteredAndSearchedForBy$Match) | |
d84597fe | 2835 | value &= [self matches:query]; |
808c6eb6 JF |
2836 | _end |
2837 | ||
0a3b45ef | 2838 | return value; |
808c6eb6 | 2839 | _end |
36bb2ca2 | 2840 | } |
8fe19fc1 | 2841 | |
01d93940 | 2842 | - (bool) isUnfilteredAndSelectedForBy:(NSString *)search { |
ef055c6c JF |
2843 | if ([search length] == 0) |
2844 | return false; | |
2845 | ||
01d93940 JF |
2846 | _profile(Package$isUnfilteredAndSelectedForBy) |
2847 | bool value(true); | |
2848 | ||
2849 | _profile(Package$isUnfilteredAndSelectedForBy$Unfiltered) | |
2850 | value &= [self unfiltered]; | |
2851 | _end | |
2852 | ||
2853 | _profile(Package$isUnfilteredAndSelectedForBy$Match) | |
2854 | value &= [[self name] compare:search options:MatchCompareOptions_ range:NSMakeRange(0, [search length])] == NSOrderedSame; | |
2855 | _end | |
2856 | ||
2857 | return value; | |
2858 | _end | |
2859 | } | |
2860 | ||
e4f3d6e9 | 2861 | - (bool) isInstalledAndUnfiltered:(NSNumber *)number { |
97a3d89e | 2862 | return ![self uninstalled] && (![number boolValue] && role_ != 7 || [self unfiltered]); |
36bb2ca2 | 2863 | } |
8fe19fc1 | 2864 | |
670a0494 | 2865 | - (bool) isVisibleInSection:(NSString *)name { |
e4f3d6e9 | 2866 | NSString *section([self section]); |
5a09ae08 | 2867 | |
e4f3d6e9 JF |
2868 | return ( |
2869 | name == nil || | |
2870 | section == nil && [name length] == 0 || | |
2871 | [name isEqualToString:section] | |
2872 | ) && [self visible]; | |
7b0ce2da JF |
2873 | } |
2874 | ||
0a3b45ef JF |
2875 | - (bool) isVisibleInSource:(Source *)source { |
2876 | return [self source] == source && [self visible]; | |
36bb2ca2 | 2877 | } |
8fe19fc1 | 2878 | |
36bb2ca2 JF |
2879 | @end |
2880 | /* }}} */ | |
2881 | /* Section Class {{{ */ | |
2882 | @interface Section : NSObject { | |
7b33d201 | 2883 | _H<NSString> name_; |
808c6eb6 | 2884 | unichar index_; |
36bb2ca2 JF |
2885 | size_t row_; |
2886 | size_t count_; | |
7b33d201 | 2887 | _H<NSString> localized_; |
36bb2ca2 | 2888 | } |
3178d79b | 2889 | |
677b8415 | 2890 | - (NSComparisonResult) compareByLocalized:(Section *)section; |
9fcbca29 JF |
2891 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized; |
2892 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize; | |
2893 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize; | |
808c6eb6 | 2894 | - (Section *) initWithIndex:(unichar)index row:(size_t)row; |
36bb2ca2 | 2895 | - (NSString *) name; |
808c6eb6 | 2896 | - (unichar) index; |
f79a4512 | 2897 | |
36bb2ca2 JF |
2898 | - (size_t) row; |
2899 | - (size_t) count; | |
f79a4512 JF |
2900 | |
2901 | - (void) addToRow; | |
36bb2ca2 | 2902 | - (void) addToCount; |
b19871dd | 2903 | |
f79a4512 | 2904 | - (void) setCount:(size_t)count; |
677b8415 | 2905 | - (NSString *) localized; |
f79a4512 | 2906 | |
36bb2ca2 | 2907 | @end |
b19871dd | 2908 | |
36bb2ca2 | 2909 | @implementation Section |
b19871dd | 2910 | |
677b8415 | 2911 | - (NSComparisonResult) compareByLocalized:(Section *)section { |
9fcbca29 JF |
2912 | NSString *lhs(localized_); |
2913 | NSString *rhs([section localized]); | |
6d9712c4 | 2914 | |
9fcbca29 | 2915 | /*if ([lhs length] != 0 && [rhs length] != 0) { |
6d9712c4 JF |
2916 | unichar lhc = [lhs characterAtIndex:0]; |
2917 | unichar rhc = [rhs characterAtIndex:0]; | |
2918 | ||
2919 | if (isalpha(lhc) && !isalpha(rhc)) | |
2920 | return NSOrderedAscending; | |
2921 | else if (!isalpha(lhc) && isalpha(rhc)) | |
2922 | return NSOrderedDescending; | |
9fcbca29 | 2923 | }*/ |
6d9712c4 | 2924 | |
68f1828e | 2925 | return [lhs compare:rhs options:LaxCompareOptions_]; |
6d9712c4 JF |
2926 | } |
2927 | ||
9fcbca29 JF |
2928 | - (Section *) initWithName:(NSString *)name localized:(NSString *)localized { |
2929 | if ((self = [self initWithName:name localize:NO]) != nil) { | |
2930 | if (localized != nil) | |
7b33d201 | 2931 | localized_ = localized; |
9fcbca29 JF |
2932 | } return self; |
2933 | } | |
2934 | ||
2935 | - (Section *) initWithName:(NSString *)name localize:(BOOL)localize { | |
2936 | return [self initWithName:name row:0 localize:localize]; | |
6d9712c4 JF |
2937 | } |
2938 | ||
9fcbca29 | 2939 | - (Section *) initWithName:(NSString *)name row:(size_t)row localize:(BOOL)localize { |
36bb2ca2 | 2940 | if ((self = [super init]) != nil) { |
7b33d201 | 2941 | name_ = name; |
808c6eb6 JF |
2942 | index_ = '\0'; |
2943 | row_ = row; | |
9fcbca29 | 2944 | if (localize) |
7b33d201 | 2945 | localized_ = LocalizeSection(name_); |
808c6eb6 JF |
2946 | } return self; |
2947 | } | |
2948 | ||
f79a4512 | 2949 | /* XXX: localize the index thingees */ |
808c6eb6 JF |
2950 | - (Section *) initWithIndex:(unichar)index row:(size_t)row { |
2951 | if ((self = [super init]) != nil) { | |
7b33d201 | 2952 | name_ = [NSString stringWithCharacters:&index length:1]; |
808c6eb6 | 2953 | index_ = index; |
36bb2ca2 | 2954 | row_ = row; |
b19871dd JF |
2955 | } return self; |
2956 | } | |
2957 | ||
36bb2ca2 JF |
2958 | - (NSString *) name { |
2959 | return name_; | |
2960 | } | |
2961 | ||
808c6eb6 JF |
2962 | - (unichar) index { |
2963 | return index_; | |
2964 | } | |
2965 | ||
36bb2ca2 JF |
2966 | - (size_t) row { |
2967 | return row_; | |
2968 | } | |
2969 | ||
2970 | - (size_t) count { | |
2971 | return count_; | |
2972 | } | |
2973 | ||
f79a4512 JF |
2974 | - (void) addToRow { |
2975 | ++row_; | |
2976 | } | |
2977 | ||
36bb2ca2 JF |
2978 | - (void) addToCount { |
2979 | ++count_; | |
2980 | } | |
2981 | ||
f79a4512 JF |
2982 | - (void) setCount:(size_t)count { |
2983 | count_ = count; | |
2984 | } | |
2985 | ||
2986 | - (NSString *) localized { | |
2987 | return localized_; | |
2988 | } | |
2989 | ||
b19871dd JF |
2990 | @end |
2991 | /* }}} */ | |
2992 | ||
670a0494 | 2993 | static NSString *Colon_; |
70750ab3 | 2994 | NSString *Elision_; |
670a0494 JF |
2995 | static NSString *Error_; |
2996 | static NSString *Warning_; | |
2997 | ||
9f9ae81c JF |
2998 | class CydiaLogCleaner : |
2999 | public pkgArchiveCleaner | |
3000 | { | |
3001 | protected: | |
3002 | virtual void Erase(const char *File, std::string Pkg, std::string Ver, struct stat &St) { | |
3003 | unlink(File); | |
3004 | } | |
3005 | }; | |
3006 | ||
36bb2ca2 JF |
3007 | /* Database Implementation {{{ */ |
3008 | @implementation Database | |
ec97ef06 | 3009 | |
770f2a8e | 3010 | + (Database *) sharedInstance { |
7b33d201 | 3011 | static _H<Database> instance; |
770f2a8e | 3012 | if (instance == nil) |
7b33d201 | 3013 | instance = [[[Database alloc] init] autorelease]; |
770f2a8e JF |
3014 | return instance; |
3015 | } | |
3016 | ||
a1440b10 JF |
3017 | - (unsigned) era { |
3018 | return era_; | |
3019 | } | |
3020 | ||
0944377b JF |
3021 | - (void) releasePackages { |
3022 | CFArrayApplyFunction(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFArrayApplierFunction>(&CFRelease), NULL); | |
3023 | CFArrayRemoveAllValues(packages_); | |
3024 | } | |
3025 | ||
36bb2ca2 | 3026 | - (void) dealloc { |
3931b718 | 3027 | // XXX: actually implement this thing |
36bb2ca2 | 3028 | _assert(false); |
0944377b | 3029 | [self releasePackages]; |
f79a4512 | 3030 | apr_pool_destroy(pool_); |
0944377b | 3031 | NSRecycleZone(zone_); |
36bb2ca2 JF |
3032 | [super dealloc]; |
3033 | } | |
ec97ef06 | 3034 | |
d13edf44 | 3035 | - (void) _readCydia:(NSNumber *)fd { |
77fcccaf JF |
3036 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3037 | std::istream is(&ib); | |
3038 | std::string line; | |
3039 | ||
bc8cd583 JF |
3040 | static Pcre finish_r("^finish:([^:]*)$"); |
3041 | ||
77fcccaf | 3042 | while (std::getline(is, line)) { |
d13edf44 JF |
3043 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3044 | ||
77fcccaf | 3045 | const char *data(line.c_str()); |
bc8cd583 | 3046 | size_t size = line.size(); |
c390d3ab | 3047 | lprintf("C:%s\n", data); |
bc8cd583 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 | } | |
d13edf44 JF |
3055 | |
3056 | [pool release]; | |
77fcccaf JF |
3057 | } |
3058 | ||
670a0494 | 3059 | _assume(false); |
77fcccaf JF |
3060 | } |
3061 | ||
d13edf44 | 3062 | - (void) _readStatus:(NSNumber *)fd { |
36bb2ca2 JF |
3063 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3064 | std::istream is(&ib); | |
3065 | std::string line; | |
ec97ef06 | 3066 | |
7b0ce2da JF |
3067 | static Pcre conffile_r("^status: [^ ]* : conffile-prompt : (.*?) *$"); |
3068 | static Pcre pmstatus_r("^([^:]*):([^:]*):([^:]*):(.*)$"); | |
ec97ef06 | 3069 | |
36bb2ca2 | 3070 | while (std::getline(is, line)) { |
d13edf44 JF |
3071 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3072 | ||
36bb2ca2 | 3073 | const char *data(line.c_str()); |
670a0494 | 3074 | size_t size(line.size()); |
c390d3ab | 3075 | lprintf("S:%s\n", data); |
ec97ef06 | 3076 | |
a08145a8 JF |
3077 | if (conffile_r(data, size)) { |
3078 | // status: /fail : conffile-prompt : '/fail' '/fail.dpkg-new' 1 1 | |
4187453f | 3079 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:conffile_r[1] waitUntilDone:YES]; |
a08145a8 JF |
3080 | } else if (strncmp(data, "status: ", 8) == 0) { |
3081 | // status: <package>: {unpacked,half-configured,installed} | |
389133be | 3082 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 8)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 JF |
3083 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3084 | } else if (strncmp(data, "processing: ", 12) == 0) { | |
3085 | // processing: configure: config-test | |
389133be | 3086 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:(data + 12)] ofType:kCydiaProgressEventTypeStatus]); |
a08145a8 | 3087 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
6915b806 | 3088 | } else if (pmstatus_r(data, size)) { |
31f3cfff | 3089 | std::string type([pmstatus_r[1] UTF8String]); |
4a2dc82e | 3090 | |
6915b806 | 3091 | NSString *package = pmstatus_r[2]; |
4a2dc82e JF |
3092 | if ([package isEqualToString:@"dpkg-exec"]) |
3093 | package = nil; | |
31f3cfff | 3094 | |
5a09ae08 | 3095 | float percent([pmstatus_r[3] floatValue]); |
6915b806 | 3096 | [progress_ performSelectorOnMainThread:@selector(setProgressPercent:) withObject:[NSNumber numberWithFloat:(percent / 100)] waitUntilDone:YES]; |
5a09ae08 JF |
3097 | |
3098 | NSString *string = pmstatus_r[4]; | |
5a09ae08 | 3099 | |
6915b806 | 3100 | if (type == "pmerror") { |
389133be | 3101 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeError forPackage:package]); |
6915b806 JF |
3102 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3103 | } else if (type == "pmstatus") { | |
389133be | 3104 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:string ofType:kCydiaProgressEventTypeStatus forPackage:package]); |
6915b806 JF |
3105 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
3106 | } else if (type == "pmconffile") | |
4187453f | 3107 | [delegate_ performSelectorOnMainThread:@selector(setConfigurationData:) withObject:string waitUntilDone:YES]; |
670a0494 JF |
3108 | else |
3109 | lprintf("E:unknown pmstatus\n"); | |
3110 | } else | |
3111 | lprintf("E:unknown status\n"); | |
d13edf44 JF |
3112 | |
3113 | [pool release]; | |
36bb2ca2 | 3114 | } |
ec97ef06 | 3115 | |
670a0494 | 3116 | _assume(false); |
36bb2ca2 | 3117 | } |
ec97ef06 | 3118 | |
d13edf44 | 3119 | - (void) _readOutput:(NSNumber *)fd { |
36bb2ca2 JF |
3120 | __gnu_cxx::stdio_filebuf<char> ib([fd intValue], std::ios::in); |
3121 | std::istream is(&ib); | |
3122 | std::string line; | |
3123 | ||
5a09ae08 | 3124 | while (std::getline(is, line)) { |
d13edf44 JF |
3125 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
3126 | ||
c390d3ab | 3127 | lprintf("O:%s\n", line.c_str()); |
27024935 | 3128 | |
389133be | 3129 | CydiaProgressEvent *event([CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:line.c_str()] ofType:kCydiaProgressEventTypeInformation]); |
6915b806 | 3130 | [progress_ performSelectorOnMainThread:@selector(addProgressEvent:) withObject:event waitUntilDone:YES]; |
d13edf44 JF |
3131 | |
3132 | [pool release]; | |
5a09ae08 | 3133 | } |
36bb2ca2 | 3134 | |
670a0494 | 3135 | _assume(false); |
ec97ef06 JF |
3136 | } |
3137 | ||
8b29f8e6 JF |
3138 | - (FILE *) input { |
3139 | return input_; | |
3140 | } | |
3141 | ||
36bb2ca2 | 3142 | - (Package *) packageWithName:(NSString *)name { |
3dd53516 | 3143 | @synchronized (self) { |
5a09ae08 JF |
3144 | if (static_cast<pkgDepCache *>(cache_) == NULL) |
3145 | return nil; | |
36bb2ca2 | 3146 | pkgCache::PkgIterator iterator(cache_->FindPkg([name UTF8String])); |
f79a4512 | 3147 | return iterator.end() ? nil : [Package packageWithIterator:iterator withZone:NULL inPool:pool_ database:self]; |
28ce8704 | 3148 | } } |
36bb2ca2 | 3149 | |
eb30da80 | 3150 | - (id) init { |
ec97ef06 | 3151 | if ((self = [super init]) != nil) { |
5a09ae08 | 3152 | policy_ = NULL; |
36bb2ca2 JF |
3153 | records_ = NULL; |
3154 | resolver_ = NULL; | |
3155 | fetcher_ = NULL; | |
3156 | lock_ = NULL; | |
ec97ef06 | 3157 | |
f79a4512 JF |
3158 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
3159 | apr_pool_create(&pool_, NULL); | |
3160 | ||
9f357d11 JF |
3161 | size_t capacity(MetaFile_->active_); |
3162 | if (capacity == 0) | |
3163 | capacity = 16384; | |
3164 | else | |
3165 | capacity += 1024; | |
3166 | ||
3167 | packages_ = CFArrayCreateMutable(kCFAllocatorDefault, capacity, NULL); | |
7b33d201 | 3168 | sourceList_ = [NSMutableArray arrayWithCapacity:16]; |
ec97ef06 | 3169 | |
36bb2ca2 | 3170 | int fds[2]; |
ec97ef06 | 3171 | |
77fcccaf JF |
3172 | _assert(pipe(fds) != -1); |
3173 | cydiafd_ = fds[1]; | |
3174 | ||
3175 | _config->Set("APT::Keep-Fds::", cydiafd_); | |
bc8cd583 | 3176 | setenv("CYDIA", [[[[NSNumber numberWithInt:cydiafd_] stringValue] stringByAppendingString:@" 1"] UTF8String], _not(int)); |
77fcccaf JF |
3177 | |
3178 | [NSThread | |
3179 | detachNewThreadSelector:@selector(_readCydia:) | |
3180 | toTarget:self | |
3931b718 | 3181 | withObject:[NSNumber numberWithInt:fds[0]] |
77fcccaf JF |
3182 | ]; |
3183 | ||
36bb2ca2 JF |
3184 | _assert(pipe(fds) != -1); |
3185 | statusfd_ = fds[1]; | |
ec97ef06 | 3186 | |
36bb2ca2 JF |
3187 | [NSThread |
3188 | detachNewThreadSelector:@selector(_readStatus:) | |
3189 | toTarget:self | |
3931b718 | 3190 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3191 | ]; |
ec97ef06 | 3192 | |
8b29f8e6 JF |
3193 | _assert(pipe(fds) != -1); |
3194 | _assert(dup2(fds[0], 0) != -1); | |
3195 | _assert(close(fds[0]) != -1); | |
3196 | ||
3197 | input_ = fdopen(fds[1], "a"); | |
3198 | ||
36bb2ca2 JF |
3199 | _assert(pipe(fds) != -1); |
3200 | _assert(dup2(fds[1], 1) != -1); | |
3201 | _assert(close(fds[1]) != -1); | |
3202 | ||
3203 | [NSThread | |
3204 | detachNewThreadSelector:@selector(_readOutput:) | |
3205 | toTarget:self | |
3931b718 | 3206 | withObject:[NSNumber numberWithInt:fds[0]] |
36bb2ca2 | 3207 | ]; |
ec97ef06 JF |
3208 | } return self; |
3209 | } | |
3210 | ||
36bb2ca2 JF |
3211 | - (pkgCacheFile &) cache { |
3212 | return cache_; | |
ec97ef06 JF |
3213 | } |
3214 | ||
5a09ae08 JF |
3215 | - (pkgDepCache::Policy *) policy { |
3216 | return policy_; | |
3217 | } | |
3218 | ||
36bb2ca2 JF |
3219 | - (pkgRecords *) records { |
3220 | return records_; | |
ec97ef06 JF |
3221 | } |
3222 | ||
36bb2ca2 JF |
3223 | - (pkgProblemResolver *) resolver { |
3224 | return resolver_; | |
ec97ef06 JF |
3225 | } |
3226 | ||
36bb2ca2 JF |
3227 | - (pkgAcquire &) fetcher { |
3228 | return *fetcher_; | |
ec97ef06 JF |
3229 | } |
3230 | ||
a3328c28 JF |
3231 | - (pkgSourceList &) list { |
3232 | return *list_; | |
3233 | } | |
3234 | ||
36bb2ca2 | 3235 | - (NSArray *) packages { |
077e9d90 | 3236 | return (NSArray *) packages_; |
ec97ef06 JF |
3237 | } |
3238 | ||
7b0ce2da | 3239 | - (NSArray *) sources { |
34f70f5d | 3240 | return sourceList_; |
7b0ce2da JF |
3241 | } |
3242 | ||
d669236d GP |
3243 | - (Source *) sourceWithKey:(NSString *)key { |
3244 | for (Source *source in [self sources]) { | |
3245 | if ([[source key] isEqualToString:key]) | |
3246 | return source; | |
3247 | } return nil; | |
3248 | } | |
3249 | ||
670a0494 JF |
3250 | - (bool) popErrorWithTitle:(NSString *)title { |
3251 | bool fatal(false); | |
670a0494 JF |
3252 | |
3253 | while (!_error->empty()) { | |
3254 | std::string error; | |
3255 | bool warning(!_error->PopMessage(error)); | |
3256 | if (!warning) | |
3257 | fatal = true; | |
cb6e2ccf | 3258 | |
670a0494 JF |
3259 | for (;;) { |
3260 | size_t size(error.size()); | |
3261 | if (size == 0 || error[size - 1] != '\n') | |
3262 | break; | |
3263 | error.resize(size - 1); | |
3264 | } | |
cb6e2ccf | 3265 | |
670a0494 JF |
3266 | lprintf("%c:[%s]\n", warning ? 'W' : 'E', error.c_str()); |
3267 | ||
389133be | 3268 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
670a0494 JF |
3269 | } |
3270 | ||
670a0494 JF |
3271 | return fatal; |
3272 | } | |
3273 | ||
3274 | - (bool) popErrorWithTitle:(NSString *)title forOperation:(bool)success { | |
3275 | return [self popErrorWithTitle:title] || !success; | |
3276 | } | |
3277 | ||
d13edf44 | 3278 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
3dd53516 JF |
3279 | @synchronized (self) { |
3280 | ++era_; | |
a1440b10 | 3281 | |
0944377b | 3282 | [self releasePackages]; |
ab3f6a01 | 3283 | |
34f70f5d JF |
3284 | sourceMap_.clear(); |
3285 | [sourceList_ removeAllObjects]; | |
843e75b8 | 3286 | |
36bb2ca2 | 3287 | _error->Discard(); |
5a09ae08 | 3288 | |
36bb2ca2 | 3289 | delete list_; |
5a09ae08 | 3290 | list_ = NULL; |
36bb2ca2 JF |
3291 | manager_ = NULL; |
3292 | delete lock_; | |
5a09ae08 | 3293 | lock_ = NULL; |
36bb2ca2 | 3294 | delete fetcher_; |
5a09ae08 | 3295 | fetcher_ = NULL; |
36bb2ca2 | 3296 | delete resolver_; |
5a09ae08 | 3297 | resolver_ = NULL; |
36bb2ca2 | 3298 | delete records_; |
5a09ae08 JF |
3299 | records_ = NULL; |
3300 | delete policy_; | |
3301 | policy_ = NULL; | |
36bb2ca2 | 3302 | |
5a09ae08 | 3303 | cache_.Close(); |
8b29f8e6 | 3304 | |
f79a4512 | 3305 | apr_pool_clear(pool_); |
a020a50e | 3306 | |
f79a4512 | 3307 | NSRecycleZone(zone_); |
a020a50e | 3308 | zone_ = NSCreateZone(1024 * 1024, 256 * 1024, NO); |
f79a4512 | 3309 | |
7376b55c JF |
3310 | int chk(creat("/tmp/cydia.chk", 0644)); |
3311 | if (chk != -1) | |
3312 | close(chk); | |
3313 | ||
4ba8f30a JF |
3314 | if (invocation != nil) |
3315 | [invocation invoke]; | |
3316 | ||
670a0494 JF |
3317 | NSString *title(UCLocalize("DATABASE")); |
3318 | ||
9487f027 | 3319 | _trace(); |
124cea03 JF |
3320 | OpProgress progress; |
3321 | while (!cache_.Open(progress, true)) { pop: | |
8b29f8e6 | 3322 | std::string error; |
670a0494 | 3323 | bool warning(!_error->PopMessage(error)); |
c390d3ab | 3324 | lprintf("cache_.Open():[%s]\n", error.c_str()); |
5a09ae08 JF |
3325 | |
3326 | if (error == "dpkg was interrupted, you must manually run 'dpkg --configure -a' to correct the problem. ") | |
3327 | [delegate_ repairWithSelector:@selector(configure)]; | |
3328 | else if (error == "The package lists or status file could not be parsed or opened.") | |
3329 | [delegate_ repairWithSelector:@selector(update)]; | |
37bf1e1b | 3330 | // else if (error == "Could not get lock /var/lib/dpkg/lock - open (35 Resource temporarily unavailable)") |
f549c55a JF |
3331 | // else if (error == "Could not open lock file /var/lib/dpkg/lock - open (13 Permission denied)") |
3332 | // else if (error == "Malformed Status line") | |
9ea8d159 | 3333 | // else if (error == "The list of sources could not be read.") |
efa53fa9 | 3334 | else { |
389133be | 3335 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:[NSString stringWithUTF8String:error.c_str()] ofType:(warning ? kCydiaProgressEventTypeWarning : kCydiaProgressEventTypeError)] forTask:title]; |
efa53fa9 GP |
3336 | return; |
3337 | } | |
5a09ae08 | 3338 | |
670a0494 JF |
3339 | if (warning) |
3340 | goto pop; | |
3341 | _error->Discard(); | |
36bb2ca2 | 3342 | } |
9487f027 | 3343 | _trace(); |
36bb2ca2 | 3344 | |
7376b55c JF |
3345 | unlink("/tmp/cydia.chk"); |
3346 | ||
31bc18a7 | 3347 | now_ = [[NSDate date] timeIntervalSince1970]; |
36bb2ca2 | 3348 | |
5a09ae08 | 3349 | policy_ = new pkgDepCache::Policy(); |
36bb2ca2 JF |
3350 | records_ = new pkgRecords(cache_); |
3351 | resolver_ = new pkgProblemResolver(cache_); | |
3352 | fetcher_ = new pkgAcquire(&status_); | |
3353 | lock_ = NULL; | |
3354 | ||
3355 | list_ = new pkgSourceList(); | |
670a0494 JF |
3356 | if ([self popErrorWithTitle:title forOperation:list_->ReadMainList()]) |
3357 | return; | |
3358 | ||
3359 | if (cache_->DelCount() != 0 || cache_->InstCount() != 0) { | |
389133be | 3360 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("COUNTS_NONZERO_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3361 | return; |
3362 | } | |
36bb2ca2 | 3363 | |
670a0494 JF |
3364 | if ([self popErrorWithTitle:title forOperation:pkgApplyStatus(cache_)]) |
3365 | return; | |
9bedffaa JF |
3366 | |
3367 | if (cache_->BrokenCount() != 0) { | |
670a0494 JF |
3368 | if ([self popErrorWithTitle:title forOperation:pkgFixBroken(cache_)]) |
3369 | return; | |
3370 | ||
3371 | if (cache_->BrokenCount() != 0) { | |
389133be | 3372 | [delegate_ addProgressEventOnMainThread:[CydiaProgressEvent eventWithMessage:UCLocalize("STILL_BROKEN_EX") ofType:kCydiaProgressEventTypeError] forTask:title]; |
670a0494 JF |
3373 | return; |
3374 | } | |
3375 | ||
3376 | if ([self popErrorWithTitle:title forOperation:pkgMinimizeUpgrade(cache_)]) | |
3377 | return; | |
9bedffaa JF |
3378 | } |
3379 | ||
68d927e2 | 3380 | for (pkgSourceList::const_iterator source = list_->begin(); source != list_->end(); ++source) { |
34f70f5d JF |
3381 | Source *object([[[Source alloc] initWithMetaIndex:*source inPool:pool_] autorelease]); |
3382 | [sourceList_ addObject:object]; | |
3383 | ||
68d927e2 JF |
3384 | std::vector<pkgIndexFile *> *indices = (*source)->GetIndexFiles(); |
3385 | for (std::vector<pkgIndexFile *>::const_iterator index = indices->begin(); index != indices->end(); ++index) | |
3386 | // XXX: this could be more intelligent | |
3387 | if (dynamic_cast<debPackagesIndex *>(*index) != NULL) { | |
3388 | pkgCache::PkgFileIterator cached((*index)->FindInCache(cache_)); | |
34f70f5d JF |
3389 | if (!cached.end()) |
3390 | sourceMap_[cached->ID] = object; | |
68d927e2 | 3391 | } |
36bb2ca2 | 3392 | } |
68d927e2 | 3393 | |
677b8415 | 3394 | { |
9fcbca29 | 3395 | /*std::vector<Package *> packages; |
677b8415 | 3396 | packages.reserve(std::max(10000U, [packages_ count] + 1000)); |
9fcbca29 JF |
3397 | packages_ = nil;*/ |
3398 | ||
677b8415 JF |
3399 | _trace(); |
3400 | ||
3401 | for (pkgCache::PkgIterator iterator = cache_->PkgBegin(); !iterator.end(); ++iterator) | |
3402 | if (Package *package = [Package packageWithIterator:iterator withZone:zone_ inPool:pool_ database:self]) | |
9fcbca29 | 3403 | //packages.push_back(package); |
7b33d201 | 3404 | CFArrayAppendValue(packages_, CFRetain(package)); |
677b8415 JF |
3405 | |
3406 | _trace(); | |
3407 | ||
9fcbca29 | 3408 | /*if (packages.empty()) |
677b8415 JF |
3409 | packages_ = [[NSArray alloc] init]; |
3410 | else | |
3411 | packages_ = [[NSArray alloc] initWithObjects:&packages.front() count:packages.size()]; | |
9fcbca29 JF |
3412 | _trace();*/ |
3413 | ||
de42680b JF |
3414 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(16)]; |
3415 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(4)]; | |
3416 | [(NSMutableArray *) packages_ radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackagePrefixRadix) withContext:reinterpret_cast<void *>(0)]; | |
9fcbca29 JF |
3417 | |
3418 | /*_trace(); | |
3419 | PrintTimes(); | |
3420 | _trace();*/ | |
3421 | ||
3422 | _trace(); | |
3423 | ||
3424 | /*if (!packages.empty()) | |
3425 | CFQSortArray(&packages.front(), packages.size(), sizeof(packages.front()), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare_), NULL);*/ | |
3426 | //std::sort(packages.begin(), packages.end(), PackageNameOrdering()); | |
3427 | ||
eef4ccaf JF |
3428 | //CFArraySortValues((CFMutableArrayRef) packages_, CFRangeMake(0, [packages_ count]), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); |
3429 | ||
077e9d90 | 3430 | CFArrayInsertionSortValues(packages_, CFRangeMake(0, CFArrayGetCount(packages_)), reinterpret_cast<CFComparatorFunction>(&PackageNameCompare), NULL); |
9fcbca29 JF |
3431 | |
3432 | //[packages_ sortUsingFunction:reinterpret_cast<NSComparisonResult (*)(id, id, void *)>(&PackageNameCompare) context:NULL]; | |
677b8415 JF |
3433 | |
3434 | _trace(); | |
cd95e390 JF |
3435 | |
3436 | size_t count(CFArrayGetCount(packages_)); | |
9f357d11 JF |
3437 | MetaFile_->active_ = count; |
3438 | ||
cd95e390 JF |
3439 | for (size_t index(0); index != count; ++index) |
3440 | [(Package *) CFArrayGetValueAtIndex(packages_, index) setIndex:index]; | |
3441 | ||
3442 | _trace(); | |
677b8415 | 3443 | } |
d13edf44 | 3444 | } } |
ec97ef06 | 3445 | |
c6ca67ba JF |
3446 | - (void) clear { |
3447 | @synchronized (self) { | |
3448 | delete resolver_; | |
3449 | resolver_ = new pkgProblemResolver(cache_); | |
3450 | ||
50c1653e JF |
3451 | for (pkgCache::PkgIterator iterator(cache_->PkgBegin()); !iterator.end(); ++iterator) |
3452 | if (!cache_[iterator].Keep()) | |
c6ca67ba | 3453 | cache_->MarkKeep(iterator, false); |
50c1653e | 3454 | else if ((cache_[iterator].iFlags & pkgDepCache::ReInstall) != 0) |
c6ca67ba | 3455 | cache_->SetReInstall(iterator, false); |
c6ca67ba JF |
3456 | } } |
3457 | ||
5a09ae08 JF |
3458 | - (void) configure { |
3459 | NSString *dpkg = [NSString stringWithFormat:@"dpkg --configure -a --status-fd %u", statusfd_]; | |
985d2dff | 3460 | _trace(); |
5a09ae08 | 3461 | system([dpkg UTF8String]); |
985d2dff | 3462 | _trace(); |
5a09ae08 JF |
3463 | } |
3464 | ||
670a0494 JF |
3465 | - (bool) clean { |
3466 | // XXX: I don't remember this condition | |
77fcccaf | 3467 | if (lock_ != NULL) |
670a0494 | 3468 | return false; |
77fcccaf JF |
3469 | |
3470 | FileFd Lock; | |
3471 | Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
3472 | |
3473 | NSString *title(UCLocalize("CLEAN_ARCHIVES")); | |
3474 | ||
3475 | if ([self popErrorWithTitle:title]) | |
3476 | return false; | |
77fcccaf JF |
3477 | |
3478 | pkgAcquire fetcher; | |
3479 | fetcher.Clean(_config->FindDir("Dir::Cache::Archives")); | |
3480 | ||
9f9ae81c | 3481 | CydiaLogCleaner cleaner; |
670a0494 JF |
3482 | if ([self popErrorWithTitle:title forOperation:cleaner.Go(_config->FindDir("Dir::Cache::Archives") + "partial/", cache_)]) |
3483 | return false; | |
3484 | ||
3485 | return true; | |
77fcccaf JF |
3486 | } |
3487 | ||
670a0494 | 3488 | - (bool) prepare { |
744f398e JF |
3489 | fetcher_->Shutdown(); |
3490 | ||
36bb2ca2 JF |
3491 | pkgRecords records(cache_); |
3492 | ||
3493 | lock_ = new FileFd(); | |
3494 | lock_->Fd(GetLock(_config->FindDir("Dir::Cache::Archives") + "lock")); | |
670a0494 JF |
3495 | |
3496 | NSString *title(UCLocalize("PREPARE_ARCHIVES")); | |
3497 | ||
3498 | if ([self popErrorWithTitle:title]) | |
3499 | return false; | |
36bb2ca2 JF |
3500 | |
3501 | pkgSourceList list; | |
670a0494 JF |
3502 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3503 | return false; | |
36bb2ca2 JF |
3504 | |
3505 | manager_ = (_system->CreatePM(cache_)); | |
670a0494 JF |
3506 | if ([self popErrorWithTitle:title forOperation:manager_->GetArchives(fetcher_, &list, &records)]) |
3507 | return false; | |
3508 | ||
3509 | return true; | |
ec97ef06 JF |
3510 | } |
3511 | ||
36bb2ca2 | 3512 | - (void) perform { |
be860cc8 JF |
3513 | bool substrate(RestartSubstrate_); |
3514 | RestartSubstrate_ = false; | |
3515 | ||
670a0494 JF |
3516 | NSString *title(UCLocalize("PERFORM_SELECTIONS")); |
3517 | ||
a72074b2 JF |
3518 | NSMutableArray *before = [NSMutableArray arrayWithCapacity:16]; { |
3519 | pkgSourceList list; | |
670a0494 JF |
3520 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3521 | return; | |
a72074b2 JF |
3522 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
3523 | [before addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
3524 | } | |
a0be02eb | 3525 | |
dcaecde2 | 3526 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
7ffd70fd | 3527 | |
eeb9b112 JF |
3528 | if (fetcher_->Run(PulseInterval_) != pkgAcquire::Continue) { |
3529 | _trace(); | |
6e90508f | 3530 | [self popErrorWithTitle:title]; |
36bb2ca2 | 3531 | return; |
eeb9b112 JF |
3532 | } |
3533 | ||
3534 | bool failed = false; | |
3535 | for (pkgAcquire::ItemIterator item = fetcher_->ItemsBegin(); item != fetcher_->ItemsEnd(); item++) { | |
3536 | if ((*item)->Status == pkgAcquire::Item::StatDone && (*item)->Complete) | |
3537 | continue; | |
6204f56a JF |
3538 | if ((*item)->Status == pkgAcquire::Item::StatIdle) |
3539 | continue; | |
eeb9b112 | 3540 | |
eeb9b112 | 3541 | failed = true; |
eeb9b112 JF |
3542 | } |
3543 | ||
dcaecde2 | 3544 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
54043703 | 3545 | |
eeb9b112 JF |
3546 | if (failed) { |
3547 | _trace(); | |
3548 | return; | |
3549 | } | |
36bb2ca2 | 3550 | |
be860cc8 JF |
3551 | if (substrate) |
3552 | RestartSubstrate_ = true; | |
3553 | ||
36bb2ca2 JF |
3554 | _system->UnLock(); |
3555 | pkgPackageManager::OrderResult result = manager_->DoInstall(statusfd_); | |
3556 | ||
eeb9b112 JF |
3557 | if (_error->PendingError()) { |
3558 | _trace(); | |
36bb2ca2 | 3559 | return; |
eeb9b112 JF |
3560 | } |
3561 | ||
3562 | if (result == pkgPackageManager::Failed) { | |
3563 | _trace(); | |
36bb2ca2 | 3564 | return; |
eeb9b112 JF |
3565 | } |
3566 | ||
3567 | if (result != pkgPackageManager::Completed) { | |
3568 | _trace(); | |
36bb2ca2 | 3569 | return; |
eeb9b112 | 3570 | } |
a0be02eb | 3571 | |
a72074b2 JF |
3572 | NSMutableArray *after = [NSMutableArray arrayWithCapacity:16]; { |
3573 | pkgSourceList list; | |
670a0494 JF |
3574 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3575 | return; | |
a72074b2 JF |
3576 | for (pkgSourceList::const_iterator source = list.begin(); source != list.end(); ++source) |
3577 | [after addObject:[NSString stringWithUTF8String:(*source)->GetURI().c_str()]]; | |
3578 | } | |
3579 | ||
3580 | if (![before isEqualToArray:after]) | |
3581 | [self update]; | |
ec97ef06 JF |
3582 | } |
3583 | ||
670a0494 JF |
3584 | - (bool) upgrade { |
3585 | NSString *title(UCLocalize("UPGRADE")); | |
3586 | if ([self popErrorWithTitle:title forOperation:pkgDistUpgrade(cache_)]) | |
3587 | return false; | |
3588 | return true; | |
c7c6384e JF |
3589 | } |
3590 | ||
36bb2ca2 JF |
3591 | - (void) update { |
3592 | [self updateWithStatus:status_]; | |
3593 | } | |
b4d89997 | 3594 | |
670a0494 | 3595 | - (void) updateWithStatus:(Status &)status { |
670a0494 JF |
3596 | NSString *title(UCLocalize("REFRESHING_DATA")); |
3597 | ||
36bb2ca2 | 3598 | pkgSourceList list; |
53ca7fdd JF |
3599 | if ([self popErrorWithTitle:title forOperation:list.ReadMainList()]) |
3600 | return; | |
b4d89997 | 3601 | |
36bb2ca2 JF |
3602 | FileFd lock; |
3603 | lock.Fd(GetLock(_config->FindDir("Dir::State::Lists") + "lock")); | |
670a0494 JF |
3604 | if ([self popErrorWithTitle:title]) |
3605 | return; | |
18873623 | 3606 | |
aaae308d JF |
3607 | [delegate_ performSelectorOnMainThread:@selector(retainNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
3608 | ||
fc470c15 JF |
3609 | bool success(ListUpdate(status, list, PulseInterval_)); |
3610 | if (status.WasCancelled()) | |
3611 | _error->Discard(); | |
aa42c612 | 3612 | else { |
fc470c15 | 3613 | [self popErrorWithTitle:title forOperation:success]; |
aa42c612 JF |
3614 | [Metadata_ setObject:[NSDate date] forKey:@"LastUpdate"]; |
3615 | Changed_ = true; | |
3616 | } | |
36bb2ca2 | 3617 | |
aaae308d | 3618 | [delegate_ performSelectorOnMainThread:@selector(releaseNetworkActivityIndicator) withObject:nil waitUntilDone:YES]; |
b4d89997 JF |
3619 | } |
3620 | ||
6915b806 | 3621 | - (void) setDelegate:(NSObject<DatabaseDelegate> *)delegate { |
36bb2ca2 | 3622 | delegate_ = delegate; |
6915b806 JF |
3623 | } |
3624 | ||
3625 | - (void) setProgressDelegate:(NSObject<ProgressDelegate> *)delegate { | |
3626 | progress_ = delegate; | |
36bb2ca2 | 3627 | status_.setDelegate(delegate); |
36bb2ca2 | 3628 | } |
b4d89997 | 3629 | |
6915b806 JF |
3630 | - (NSObject<ProgressDelegate> *) progressDelegate { |
3631 | return progress_; | |
3632 | } | |
3633 | ||
7376b55c | 3634 | - (Source *) getSource:(pkgCache::PkgFileIterator)file { |
34f70f5d JF |
3635 | SourceMap::const_iterator i(sourceMap_.find(file->ID)); |
3636 | return i == sourceMap_.end() ? nil : i->second; | |
b19871dd JF |
3637 | } |
3638 | ||
fe33a23e | 3639 | - (NSString *) mappedSectionForPointer:(const char *)section { |
4905df00 | 3640 | _H<NSString> *mapped; |
fe33a23e | 3641 | |
4905df00 JF |
3642 | _profile(Database$mappedSectionForPointer$Cache) |
3643 | mapped = §ions_[section]; | |
3644 | _end | |
3645 | ||
3646 | if (*mapped == NULL) { | |
fe33a23e JF |
3647 | size_t length(strlen(section)); |
3648 | char spaced[length + 1]; | |
3649 | ||
3650 | _profile(Database$mappedSectionForPointer$Replace) | |
3651 | for (size_t index(0); index != length; ++index) | |
3652 | spaced[index] = section[index] == '_' ? ' ' : section[index]; | |
3653 | spaced[length] = '\0'; | |
3654 | _end | |
3655 | ||
3656 | NSString *string; | |
3657 | ||
3658 | _profile(Database$mappedSectionForPointer$stringWithUTF8String) | |
3659 | string = [NSString stringWithUTF8String:spaced]; | |
3660 | _end | |
3661 | ||
3662 | _profile(Database$mappedSectionForPointer$Map) | |
4905df00 | 3663 | string = [SectionMap_ objectForKey:string] ?: string; |
fe33a23e | 3664 | _end |
4905df00 JF |
3665 | |
3666 | *mapped = string; | |
3667 | } return *mapped; | |
fe33a23e JF |
3668 | } |
3669 | ||
36bb2ca2 JF |
3670 | @end |
3671 | /* }}} */ | |
b4d89997 | 3672 | |
7b33d201 | 3673 | static _H<NSMutableSet> Diversions_; |
7256476b | 3674 | |
775deead JF |
3675 | @interface Diversion : NSObject { |
3676 | Pcre pattern_; | |
3677 | _H<NSString> key_; | |
3678 | _H<NSString> format_; | |
3679 | } | |
3680 | ||
3681 | @end | |
3682 | ||
3683 | @implementation Diversion | |
3684 | ||
3685 | - (id) initWithFrom:(NSString *)from to:(NSString *)to { | |
3686 | if ((self = [super init]) != nil) { | |
3687 | pattern_ = [from UTF8String]; | |
3688 | key_ = from; | |
3689 | format_ = to; | |
3690 | } return self; | |
3691 | } | |
3692 | ||
3693 | - (NSString *) divert:(NSString *)url { | |
8ea598c0 | 3694 | return !pattern_(url) ? nil : pattern_->*format_; |
775deead JF |
3695 | } |
3696 | ||
7256476b JF |
3697 | + (NSURL *) divertURL:(NSURL *)url { |
3698 | divert: | |
3699 | NSString *href([url absoluteString]); | |
3700 | ||
7b33d201 | 3701 | for (Diversion *diversion in (id) Diversions_) |
7256476b | 3702 | if (NSString *diverted = [diversion divert:href]) { |
85d5d452 JF |
3703 | #if !ForRelease |
3704 | NSLog(@"div: %@", diverted); | |
3705 | #endif | |
7256476b JF |
3706 | url = [NSURL URLWithString:diverted]; |
3707 | goto divert; | |
3708 | } | |
3709 | ||
3710 | return url; | |
3711 | } | |
3712 | ||
775deead JF |
3713 | - (NSString *) key { |
3714 | return key_; | |
3715 | } | |
3716 | ||
3717 | - (NSUInteger) hash { | |
3718 | return [key_ hash]; | |
3719 | } | |
3720 | ||
3721 | - (BOOL) isEqual:(Diversion *)object { | |
3722 | return self == object || [self class] == [object class] && [key_ isEqual:[object key]]; | |
3723 | } | |
3724 | ||
3725 | @end | |
3726 | ||
43f3d7f6 | 3727 | @interface CydiaObject : NSObject { |
7b33d201 | 3728 | _H<IndirectDelegate> indirect_; |
3931b718 | 3729 | _transient id delegate_; |
43f3d7f6 | 3730 | } |
c390d3ab | 3731 | |
43f3d7f6 | 3732 | - (id) initWithDelegate:(IndirectDelegate *)indirect; |
6840bff3 | 3733 | |
c390d3ab JF |
3734 | @end |
3735 | ||
09e89a8a | 3736 | @interface CydiaWebViewController : CyteWebViewController { |
7b33d201 | 3737 | _H<CydiaObject> cydia_; |
775deead JF |
3738 | } |
3739 | ||
3740 | + (void) addDiversion:(Diversion *)diversion; | |
3741 | ||
3742 | @end | |
3743 | ||
775deead | 3744 | /* Web Scripting {{{ */ |
43f3d7f6 | 3745 | @implementation CydiaObject |
c390d3ab | 3746 | |
43f3d7f6 JF |
3747 | - (id) initWithDelegate:(IndirectDelegate *)indirect { |
3748 | if ((self = [super init]) != nil) { | |
7b33d201 | 3749 | indirect_ = indirect; |
43f3d7f6 JF |
3750 | } return self; |
3751 | } | |
3752 | ||
77801ff1 JF |
3753 | - (void) setDelegate:(id)delegate { |
3754 | delegate_ = delegate; | |
3755 | } | |
3756 | ||
43f3d7f6 | 3757 | + (NSArray *) _attributeKeys { |
e58ff941 | 3758 | return [NSArray arrayWithObjects: |
6ffdaae3 | 3759 | @"bbsnum", |
e58ff941 | 3760 | @"device", |
56296da0 | 3761 | @"ecid", |
93d6d318 JF |
3762 | @"firmware", |
3763 | @"hostname", | |
3764 | @"idiom", | |
56296da0 JF |
3765 | @"model", |
3766 | @"plmn", | |
3767 | @"role", | |
e58ff941 | 3768 | @"serial", |
3ea82d91 | 3769 | @"token", |
bf1d5e69 | 3770 | @"version", |
e58ff941 | 3771 | nil]; |
43f3d7f6 JF |
3772 | } |
3773 | ||
3774 | - (NSArray *) attributeKeys { | |
3775 | return [[self class] _attributeKeys]; | |
c390d3ab JF |
3776 | } |
3777 | ||
43f3d7f6 JF |
3778 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { |
3779 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
c390d3ab | 3780 | } |
43f3d7f6 | 3781 | |
bf1d5e69 JF |
3782 | - (NSString *) version { |
3783 | return @ Cydia_; | |
3784 | } | |
3785 | ||
43f3d7f6 JF |
3786 | - (NSString *) device { |
3787 | return [[UIDevice currentDevice] uniqueIdentifier]; | |
c390d3ab JF |
3788 | } |
3789 | ||
93d6d318 JF |
3790 | - (NSString *) firmware { |
3791 | return [[UIDevice currentDevice] systemVersion]; | |
3792 | } | |
3793 | ||
3794 | - (NSString *) hostname { | |
3795 | return [[UIDevice currentDevice] name]; | |
3796 | } | |
3797 | ||
3798 | - (NSString *) idiom { | |
c138614d | 3799 | return (id) Idiom_ ?: [NSNull null]; |
93d6d318 JF |
3800 | } |
3801 | ||
56296da0 | 3802 | - (NSString *) plmn { |
849cd6bf | 3803 | return (id) PLMN_ ?: [NSNull null]; |
56296da0 JF |
3804 | } |
3805 | ||
6ffdaae3 JF |
3806 | - (NSString *) bbsnum { |
3807 | return (id) BBSNum_ ?: [NSNull null]; | |
3808 | } | |
3809 | ||
56296da0 | 3810 | - (NSString *) ecid { |
849cd6bf | 3811 | return (id) ChipID_ ?: [NSNull null]; |
affeffc7 JF |
3812 | } |
3813 | ||
43f3d7f6 | 3814 | - (NSString *) serial { |
56296da0 | 3815 | return SerialNumber_; |
43f3d7f6 | 3816 | } |
86316a91 | 3817 | |
56296da0 | 3818 | - (NSString *) role { |
849cd6bf | 3819 | return (id) Role_ ?: [NSNull null]; |
affeffc7 JF |
3820 | } |
3821 | ||
56296da0 JF |
3822 | - (NSString *) model { |
3823 | return [NSString stringWithUTF8String:Machine_]; | |
43f3d7f6 | 3824 | } |
43f3d7f6 | 3825 | |
3ea82d91 JF |
3826 | - (NSString *) token { |
3827 | return (id) Token_ ?: [NSNull null]; | |
3828 | } | |
3829 | ||
43f3d7f6 | 3830 | + (NSString *) webScriptNameForSelector:(SEL)selector { |
e58ff941 | 3831 | if (false); |
cfc7b442 JF |
3832 | else if (selector == @selector(addBridgedHost:)) |
3833 | return @"addBridgedHost"; | |
3f428e4e JF |
3834 | else if (selector == @selector(addInternalRedirect::)) |
3835 | return @"addInternalRedirect"; | |
e4b48f2f | 3836 | else if (selector == @selector(addPipelinedHost:scheme:)) |
834c18a4 | 3837 | return @"addPipelinedHost"; |
b088c0cd JF |
3838 | else if (selector == @selector(addTrivialSource:)) |
3839 | return @"addTrivialSource"; | |
e58ff941 | 3840 | else if (selector == @selector(close)) |
43f3d7f6 | 3841 | return @"close"; |
e58ff941 JF |
3842 | else if (selector == @selector(du:)) |
3843 | return @"du"; | |
3844 | else if (selector == @selector(stringWithFormat:arguments:)) | |
3845 | return @"format"; | |
5c32f89e JF |
3846 | else if (selector == @selector(getAllSources)) |
3847 | return @"getAllSourcs"; | |
5bc1277a JF |
3848 | else if (selector == @selector(getKernelNumber:)) |
3849 | return @"getKernelNumber"; | |
3850 | else if (selector == @selector(getKernelString:)) | |
3851 | return @"getKernelString"; | |
8cc8eb1c JF |
3852 | else if (selector == @selector(getInstalledPackages)) |
3853 | return @"getInstalledPackages"; | |
c31d7cdc JF |
3854 | else if (selector == @selector(getIORegistryEntry::)) |
3855 | return @"getIORegistryEntry"; | |
948db680 JF |
3856 | else if (selector == @selector(getLocaleIdentifier)) |
3857 | return @"getLocaleIdentifier"; | |
3858 | else if (selector == @selector(getPreferredLanguages)) | |
3859 | return @"getPreferredLanguages"; | |
43f3d7f6 JF |
3860 | else if (selector == @selector(getPackageById:)) |
3861 | return @"getPackageById"; | |
ef974f52 JF |
3862 | else if (selector == @selector(getSessionValue:)) |
3863 | return @"getSessionValue"; | |
77801ff1 JF |
3864 | else if (selector == @selector(installPackages:)) |
3865 | return @"installPackages"; | |
e58ff941 JF |
3866 | else if (selector == @selector(localizedStringForKey:value:table:)) |
3867 | return @"localize"; | |
8d497e2a JF |
3868 | else if (selector == @selector(popViewController:)) |
3869 | return @"popViewController"; | |
b088c0cd JF |
3870 | else if (selector == @selector(refreshSources)) |
3871 | return @"refreshSources"; | |
ed5566c7 JF |
3872 | else if (selector == @selector(removeButton)) |
3873 | return @"removeButton"; | |
ef974f52 JF |
3874 | else if (selector == @selector(setSessionValue::)) |
3875 | return @"setSessionValue"; | |
8a126074 JF |
3876 | else if (selector == @selector(substitutePackageNames:)) |
3877 | return @"substitutePackageNames"; | |
8e3b68d4 JF |
3878 | else if (selector == @selector(scrollToBottom:)) |
3879 | return @"scrollToBottom"; | |
8366df5e JF |
3880 | else if (selector == @selector(setAllowsNavigationAction:)) |
3881 | return @"setAllowsNavigationAction"; | |
c31c825d JF |
3882 | else if (selector == @selector(setBadgeValue:)) |
3883 | return @"setBadgeValue"; | |
43f3d7f6 JF |
3884 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) |
3885 | return @"setButtonImage"; | |
3886 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
3887 | return @"setButtonTitle"; | |
b8a5d89d JF |
3888 | else if (selector == @selector(setHidesBackButton:)) |
3889 | return @"setHidesBackButton"; | |
5cdfcd6f JF |
3890 | else if (selector == @selector(setHidesNavigationBar:)) |
3891 | return @"setHidesNavigationBar"; | |
82406217 JF |
3892 | else if (selector == @selector(setNavigationBarStyle:)) |
3893 | return @"setNavigationBarStyle"; | |
00984204 JF |
3894 | else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:)) |
3895 | return @"setNavigationBarTintColor"; | |
36a20e14 JF |
3896 | else if (selector == @selector(setPasteboardString:)) |
3897 | return @"setPasteboardString"; | |
3898 | else if (selector == @selector(setPasteboardURL:)) | |
3899 | return @"setPasteboardURL"; | |
ef055c6c JF |
3900 | else if (selector == @selector(setToken:)) |
3901 | return @"setToken"; | |
43f3d7f6 JF |
3902 | else if (selector == @selector(setViewportWidth:)) |
3903 | return @"setViewportWidth"; | |
43f3d7f6 JF |
3904 | else if (selector == @selector(statfs:)) |
3905 | return @"statfs"; | |
e58ff941 JF |
3906 | else if (selector == @selector(supports:)) |
3907 | return @"supports"; | |
7c218781 JF |
3908 | else if (selector == @selector(unload)) |
3909 | return @"unload"; | |
c390d3ab | 3910 | else |
43f3d7f6 JF |
3911 | return nil; |
3912 | } | |
3913 | ||
3914 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
3915 | return [self webScriptNameForSelector:selector] == nil; | |
c390d3ab JF |
3916 | } |
3917 | ||
43f3d7f6 JF |
3918 | - (BOOL) supports:(NSString *)feature { |
3919 | return [feature isEqualToString:@"window.open"]; | |
3920 | } | |
c390d3ab | 3921 | |
7c218781 JF |
3922 | - (void) unload { |
3923 | [delegate_ performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO]; | |
3924 | } | |
3925 | ||
3f428e4e | 3926 | - (void) addInternalRedirect:(NSString *)from :(NSString *)to { |
a576488f | 3927 | [CydiaWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO]; |
775deead JF |
3928 | } |
3929 | ||
5bc1277a JF |
3930 | - (NSNumber *) getKernelNumber:(NSString *)name { |
3931 | const char *string([name UTF8String]); | |
3932 | ||
3933 | size_t size; | |
3934 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
3935 | return (id) [NSNull null]; | |
3936 | ||
3937 | if (size != sizeof(int)) | |
3938 | return (id) [NSNull null]; | |
3939 | ||
3940 | int value; | |
3941 | if (sysctlbyname(string, &value, &size, NULL, 0) == -1) | |
3942 | return (id) [NSNull null]; | |
3943 | ||
3944 | return [NSNumber numberWithInt:value]; | |
3945 | } | |
3946 | ||
3947 | - (NSString *) getKernelString:(NSString *)name { | |
3948 | const char *string([name UTF8String]); | |
3949 | ||
3950 | size_t size; | |
3951 | if (sysctlbyname(string, NULL, &size, NULL, 0) == -1) | |
3952 | return (id) [NSNull null]; | |
3953 | ||
3954 | char value[size + 1]; | |
3955 | if (sysctlbyname(string, value, &size, NULL, 0) == -1) | |
3956 | return (id) [NSNull null]; | |
3957 | ||
3958 | // XXX: just in case you request something ludicrous | |
3959 | value[size] = '\0'; | |
3960 | ||
3961 | return [NSString stringWithCString:value]; | |
3962 | } | |
3963 | ||
c31d7cdc JF |
3964 | - (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry { |
3965 | NSObject *value(CYIOGetValue([path UTF8String], entry)); | |
3966 | ||
3967 | if (value != nil) | |
3968 | if ([value isKindOfClass:[NSData class]]) | |
3969 | value = CYHex((NSData *) value); | |
3970 | ||
3971 | return value; | |
3972 | } | |
3973 | ||
ef974f52 JF |
3974 | - (id) getSessionValue:(NSString *)key { |
3975 | @synchronized (SessionData_) { | |
3976 | return [SessionData_ objectForKey:key]; | |
3977 | } } | |
3978 | ||
3979 | - (void) setSessionValue:(NSString *)key :(NSString *)value { | |
3980 | @synchronized (SessionData_) { | |
3981 | if (value == (id) [WebUndefined undefined]) | |
3982 | [SessionData_ removeObjectForKey:key]; | |
3983 | else | |
3984 | [SessionData_ setObject:value forKey:key]; | |
3985 | } } | |
3986 | ||
cfc7b442 | 3987 | - (void) addBridgedHost:(NSString *)host { |
48f1762f JF |
3988 | @synchronized (HostConfig_) { |
3989 | [BridgedHosts_ addObject:host]; | |
3990 | } } | |
5df7ecfb | 3991 | |
e4b48f2f | 3992 | - (void) addPipelinedHost:(NSString *)host scheme:(NSString *)scheme { |
48f1762f | 3993 | @synchronized (HostConfig_) { |
e4b48f2f JF |
3994 | if (scheme != (id) [WebUndefined undefined]) |
3995 | host = [NSString stringWithFormat:@"%@:%@", [scheme lowercaseString], host]; | |
3996 | ||
48f1762f JF |
3997 | [PipelinedHosts_ addObject:host]; |
3998 | } } | |
834c18a4 | 3999 | |
8d497e2a JF |
4000 | - (void) popViewController:(NSNumber *)value { |
4001 | if (value == (id) [WebUndefined undefined]) | |
4002 | value = [NSNumber numberWithBool:YES]; | |
4003 | [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO]; | |
4004 | } | |
4005 | ||
b088c0cd | 4006 | - (void) addTrivialSource:(NSString *)href { |
a1d85d42 | 4007 | [delegate_ performSelectorOnMainThread:@selector(addTrivialSource:) withObject:href waitUntilDone:NO]; |
b088c0cd JF |
4008 | } |
4009 | ||
4010 | - (void) refreshSources { | |
4011 | [delegate_ performSelectorOnMainThread:@selector(syncData) withObject:nil waitUntilDone:NO]; | |
4012 | } | |
4013 | ||
5c32f89e JF |
4014 | - (NSArray *) getAllSources { |
4015 | return [[Database sharedInstance] sources]; | |
4016 | } | |
4017 | ||
8cc8eb1c | 4018 | - (NSArray *) getInstalledPackages { |
26e6829b JF |
4019 | Database *database([Database sharedInstance]); |
4020 | @synchronized (database) { | |
4021 | NSArray *packages([database packages]); | |
f4db946e | 4022 | NSMutableArray *installed([NSMutableArray arrayWithCapacity:1024]); |
77801ff1 | 4023 | for (Package *package in packages) |
26e6829b | 4024 | if (![package uninstalled]) |
8cc8eb1c JF |
4025 | [installed addObject:package]; |
4026 | return installed; | |
26e6829b | 4027 | } } |
8cc8eb1c | 4028 | |
43f3d7f6 | 4029 | - (Package *) getPackageById:(NSString *)id { |
62cab237 JF |
4030 | if (Package *package = [[Database sharedInstance] packageWithName:id]) { |
4031 | [package parse]; | |
4032 | return package; | |
4033 | } else | |
4034 | return (Package *) [NSNull null]; | |
43f3d7f6 JF |
4035 | } |
4036 | ||
948db680 JF |
4037 | - (NSString *) getLocaleIdentifier { |
4038 | return Locale_ == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(Locale_); | |
4039 | } | |
4040 | ||
4041 | - (NSArray *) getPreferredLanguages { | |
4042 | return Languages_; | |
4043 | } | |
4044 | ||
43f3d7f6 JF |
4045 | - (NSArray *) statfs:(NSString *)path { |
4046 | struct statfs stat; | |
4047 | ||
4048 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
4049 | return nil; | |
4050 | ||
4051 | return [NSArray arrayWithObjects: | |
4052 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
4053 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
4054 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
4055 | nil]; | |
4056 | } | |
4057 | ||
4058 | - (NSNumber *) du:(NSString *)path { | |
4059 | NSNumber *value(nil); | |
4060 | ||
4061 | int fds[2]; | |
4062 | _assert(pipe(fds) != -1); | |
4063 | ||
4064 | pid_t pid(ExecFork()); | |
4065 | if (pid == 0) { | |
4066 | _assert(dup2(fds[1], 1) != -1); | |
4067 | _assert(close(fds[0]) != -1); | |
4068 | _assert(close(fds[1]) != -1); | |
4069 | /* XXX: this should probably not use du */ | |
4070 | execl("/usr/libexec/cydia/du", "du", "-s", [path UTF8String], NULL); | |
4071 | exit(1); | |
4072 | _assert(false); | |
4073 | } | |
4074 | ||
4075 | _assert(close(fds[1]) != -1); | |
4076 | ||
4077 | if (FILE *du = fdopen(fds[0], "r")) { | |
4078 | char line[1024]; | |
4079 | while (fgets(line, sizeof(line), du) != NULL) { | |
4080 | size_t length(strlen(line)); | |
4081 | while (length != 0 && line[length - 1] == '\n') | |
4082 | line[--length] = '\0'; | |
4083 | if (char *tab = strchr(line, '\t')) { | |
4084 | *tab = '\0'; | |
4085 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
4086 | } | |
4087 | } | |
4088 | ||
4089 | fclose(du); | |
4090 | } else _assert(close(fds[0])); | |
4091 | ||
4092 | int status; | |
4093 | wait: | |
4094 | if (waitpid(pid, &status, 0) == -1) | |
4095 | if (errno == EINTR) | |
4096 | goto wait; | |
4097 | else _assert(false); | |
4098 | ||
4099 | return value; | |
4100 | } | |
4101 | ||
4102 | - (void) close { | |
e6417cea | 4103 | [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO]; |
43f3d7f6 JF |
4104 | } |
4105 | ||
77801ff1 JF |
4106 | - (void) installPackages:(NSArray *)packages { |
4107 | [delegate_ performSelectorOnMainThread:@selector(installPackages:) withObject:packages waitUntilDone:NO]; | |
4108 | } | |
4109 | ||
8a126074 JF |
4110 | - (NSString *) substitutePackageNames:(NSString *)message { |
4111 | NSMutableArray *words([[message componentsSeparatedByString:@" "] mutableCopy]); | |
4112 | for (size_t i(0), e([words count]); i != e; ++i) { | |
4113 | NSString *word([words objectAtIndex:i]); | |
4114 | if (Package *package = [[Database sharedInstance] packageWithName:word]) | |
4115 | [words replaceObjectAtIndex:i withObject:[package name]]; | |
4116 | } | |
4117 | ||
4118 | return [words componentsJoinedByString:@" "]; | |
4119 | } | |
4120 | ||
ed5566c7 JF |
4121 | - (void) removeButton { |
4122 | [indirect_ removeButton]; | |
4123 | } | |
4124 | ||
43f3d7f6 JF |
4125 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
4126 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
4127 | } | |
4128 | ||
4129 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
4130 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
4131 | } | |
4132 | ||
c31c825d JF |
4133 | - (void) setBadgeValue:(id)value { |
4134 | [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO]; | |
4135 | } | |
4136 | ||
8366df5e JF |
4137 | - (void) setAllowsNavigationAction:(NSString *)value { |
4138 | [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO]; | |
4139 | } | |
4140 | ||
b8a5d89d JF |
4141 | - (void) setHidesBackButton:(NSString *)value { |
4142 | [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO]; | |
4143 | } | |
4144 | ||
5cdfcd6f JF |
4145 | - (void) setHidesNavigationBar:(NSString *)value { |
4146 | [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO]; | |
4147 | } | |
4148 | ||
82406217 JF |
4149 | - (void) setNavigationBarStyle:(NSString *)value { |
4150 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO]; | |
4151 | } | |
4152 | ||
00984204 JF |
4153 | - (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha { |
4154 | float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]); | |
4155 | UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]); | |
4156 | [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO]; | |
4157 | } | |
4158 | ||
36a20e14 JF |
4159 | - (void) setPasteboardString:(NSString *)value { |
4160 | [[objc_getClass("UIPasteboard") generalPasteboard] setString:value]; | |
4161 | } | |
4162 | ||
4163 | - (void) setPasteboardURL:(NSString *)value { | |
4164 | [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]]; | |
4165 | } | |
4166 | ||
673a6e1a | 4167 | - (void) _setToken:(NSString *)token { |
9737d93e JF |
4168 | Token_ = token; |
4169 | ||
4170 | if (token == nil) | |
4171 | [Metadata_ removeObjectForKey:@"Token"]; | |
4172 | else | |
4173 | [Metadata_ setObject:Token_ forKey:@"Token"]; | |
ef055c6c | 4174 | |
ef055c6c JF |
4175 | Changed_ = true; |
4176 | } | |
4177 | ||
673a6e1a JF |
4178 | - (void) setToken:(NSString *)token { |
4179 | [self performSelectorOnMainThread:@selector(_setToken:) withObject:token waitUntilDone:NO]; | |
4180 | } | |
4181 | ||
8e3b68d4 JF |
4182 | - (void) scrollToBottom:(NSNumber *)animated { |
4183 | [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO]; | |
4184 | } | |
4185 | ||
43f3d7f6 | 4186 | - (void) setViewportWidth:(float)width { |
8dbdaafa | 4187 | [indirect_ setViewportWidthOnMainThread:width]; |
43f3d7f6 JF |
4188 | } |
4189 | ||
4190 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
4191 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
4192 | unsigned count([arguments count]); | |
4193 | id values[count]; | |
4194 | for (unsigned i(0); i != count; ++i) | |
4195 | values[i] = [arguments objectAtIndex:i]; | |
673e8fa3 | 4196 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; |
43f3d7f6 JF |
4197 | } |
4198 | ||
4199 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
a95e0405 JF |
4200 | if (reinterpret_cast<id>(value) == [WebUndefined undefined]) |
4201 | value = nil; | |
43f3d7f6 JF |
4202 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) |
4203 | table = nil; | |
4204 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
c390d3ab JF |
4205 | } |
4206 | ||
4207 | @end | |
4208 | /* }}} */ | |
f79a4512 | 4209 | |
dd5f8161 | 4210 | |
80319240 | 4211 | /* Cydia Browser Controller {{{ */ |
a576488f | 4212 | @implementation CydiaWebViewController |
43f3d7f6 | 4213 | |
fe8e721f | 4214 | - (NSURL *) navigationURL { |
d323285e | 4215 | return request_ == nil ? nil : [NSURL URLWithString:[NSString stringWithFormat:@"cydia://url/%@", [[request_ URL] absoluteString]]]; |
fe8e721f GP |
4216 | } |
4217 | ||
775deead | 4218 | + (void) initialize { |
7b33d201 | 4219 | Diversions_ = [NSMutableSet setWithCapacity:0]; |
775deead JF |
4220 | } |
4221 | ||
4222 | + (void) addDiversion:(Diversion *)diversion { | |
4223 | [Diversions_ addObject:diversion]; | |
4224 | } | |
4225 | ||
2634b249 JF |
4226 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4227 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
01d93940 JF |
4228 | |
4229 | WebDataSource *source([frame dataSource]); | |
4230 | NSURLResponse *response([source response]); | |
4231 | NSURL *url([response URL]); | |
b8f1a18a JF |
4232 | NSString *scheme([[url scheme] lowercaseString]); |
4233 | ||
4234 | bool bridged(false); | |
8804004f | 4235 | |
48f1762f | 4236 | @synchronized (HostConfig_) { |
b8f1a18a JF |
4237 | if ([scheme isEqualToString:@"file"]) |
4238 | bridged = true; | |
4239 | else if ([scheme isEqualToString:@"https"]) | |
48f1762f | 4240 | if ([BridgedHosts_ containsObject:[url host]]) |
b8f1a18a | 4241 | bridged = true; |
48f1762f | 4242 | } |
b8f1a18a JF |
4243 | |
4244 | if (bridged) | |
4245 | [window setValue:cydia_ forKey:@"cydia"]; | |
43f3d7f6 JF |
4246 | } |
4247 | ||
22485d93 JF |
4248 | - (void) _setupMail:(MFMailComposeViewController *)controller { |
4249 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/cydia.log"] mimeType:@"text/plain" fileName:@"cydia.log"]; | |
4250 | ||
4251 | system("/usr/bin/dpkg -l >/tmp/dpkgl.log"); | |
4252 | [controller addAttachmentData:[NSData dataWithContentsOfFile:@"/tmp/dpkgl.log"] mimeType:@"text/plain" fileName:@"dpkgl.log"]; | |
4253 | } | |
4254 | ||
f9b36dae JF |
4255 | - (NSURL *) URLWithURL:(NSURL *)url { |
4256 | return [Diversion divertURL:url]; | |
4257 | } | |
4258 | ||
9d1bf666 JF |
4259 | - (NSURLRequest *) webView:(WebView *)view resource:(id)resource willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
4260 | NSMutableURLRequest *copy([[super webView:view resource:resource willSendRequest:request redirectResponse:response fromDataSource:source] mutableCopy]); | |
4261 | ||
bfc87a4d | 4262 | if (System_ != NULL) |
9d1bf666 | 4263 | [copy setValue:System_ forHTTPHeaderField:@"X-System"]; |
43f3d7f6 | 4264 | if (Machine_ != NULL) |
9d1bf666 | 4265 | [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; |
01d93940 | 4266 | if (Token_ != nil) |
9d1bf666 | 4267 | [copy setValue:Token_ forHTTPHeaderField:@"X-Cydia-Token"]; |
43f3d7f6 JF |
4268 | |
4269 | return copy; | |
a9a0661e JF |
4270 | } |
4271 | ||
77801ff1 JF |
4272 | - (void) setDelegate:(id)delegate { |
4273 | [super setDelegate:delegate]; | |
4274 | [cydia_ setDelegate:delegate]; | |
4275 | } | |
4276 | ||
b5e7eebb | 4277 | - (id) init { |
a576488f | 4278 | if ((self = [super initWithWidth:0 ofClass:[CydiaWebViewController class]]) != nil) { |
7b33d201 | 4279 | cydia_ = [[[CydiaObject alloc] initWithDelegate:indirect_] autorelease]; |
43f3d7f6 | 4280 | |
2634b249 | 4281 | WebView *webview([[webview_ _documentView] webView]); |
43f3d7f6 | 4282 | |
44c1771c | 4283 | NSString *application([NSString stringWithFormat:@"Cydia/%@", @ Cydia_]); |
43f3d7f6 | 4284 | |
43f3d7f6 | 4285 | if (Safari_ != nil) |
9ef18597 | 4286 | application = [NSString stringWithFormat:@"Safari/%@ %@", Safari_, application]; |
35bf217f | 4287 | if (Build_ != nil) |
9ef18597 | 4288 | application = [NSString stringWithFormat:@"Mobile/%@ %@", Build_, application]; |
35bf217f | 4289 | if (Product_ != nil) |
9ef18597 | 4290 | application = [NSString stringWithFormat:@"Version/%@ %@", Product_, application]; |
43f3d7f6 JF |
4291 | |
4292 | [webview setApplicationNameForUserAgent:application]; | |
4293 | } return self; | |
4294 | } | |
4295 | ||
4296 | @end | |
80319240 | 4297 | /* }}} */ |
43f3d7f6 | 4298 | |
b1ca831d JF |
4299 | // CydiaScript {{{ |
4300 | @interface NSObject (CydiaScript) | |
4301 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context; | |
4302 | @end | |
4303 | ||
4304 | @implementation NSObject (CydiaScript) | |
4305 | ||
4306 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
4307 | return self; | |
4308 | } | |
4309 | ||
4310 | @end | |
4311 | ||
4312 | @implementation NSArray (CydiaScript) | |
4313 | ||
4314 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
4315 | WebScriptObject *object([context evaluateWebScript:@"[]"]); | |
4316 | for (size_t i(0), e([self count]); i != e; ++i) | |
4317 | [object setWebScriptValueAtIndex:i value:[[self objectAtIndex:i] Cydia$webScriptObjectInContext:context]]; | |
4318 | return object; | |
4319 | } | |
4320 | ||
4321 | @end | |
4322 | ||
4323 | @implementation NSDictionary (CydiaScript) | |
4324 | ||
4325 | - (id) Cydia$webScriptObjectInContext:(WebScriptObject *)context { | |
4326 | WebScriptObject *object([context evaluateWebScript:@"({})"]); | |
4327 | for (id i in self) | |
4328 | [object setValue:[[self objectForKey:i] Cydia$webScriptObjectInContext:context] forKey:i]; | |
4329 | return object; | |
4330 | } | |
4331 | ||
4332 | @end | |
4333 | // }}} | |
4334 | ||
5829aea2 GP |
4335 | /* Confirmation Controller {{{ */ |
4336 | bool DepSubstrate(const pkgCache::VerIterator &iterator) { | |
4337 | if (!iterator.end()) | |
4338 | for (pkgCache::DepIterator dep(iterator.DependsList()); !dep.end(); ++dep) { | |
4339 | if (dep->Type != pkgCache::Dep::Depends && dep->Type != pkgCache::Dep::PreDepends) | |
4340 | continue; | |
4341 | pkgCache::PkgIterator package(dep.TargetPkg()); | |
4342 | if (package.end()) | |
4343 | continue; | |
4344 | if (strcmp(package.Name(), "mobilesubstrate") == 0) | |
4345 | return true; | |
4346 | } | |
4347 | ||
4348 | return false; | |
4349 | } | |
4350 | ||
adb61bda | 4351 | @protocol ConfirmationControllerDelegate |
674dce72 | 4352 | - (void) cancelAndClear:(bool)clear; |
b5e7eebb | 4353 | - (void) confirmWithNavigationController:(UINavigationController *)navigation; |
dc63e78f | 4354 | - (void) queue; |
36bb2ca2 | 4355 | @end |
2367a917 | 4356 | |
a576488f | 4357 | @interface ConfirmationController : CydiaWebViewController { |
770f2a8e | 4358 | _transient Database *database_; |
6ceb0959 | 4359 | |
7b33d201 | 4360 | _H<UIAlertView> essential_; |
6ceb0959 | 4361 | |
7b33d201 JF |
4362 | _H<NSDictionary> changes_; |
4363 | _H<NSMutableArray> issues_; | |
4364 | _H<NSDictionary> sizes_; | |
6ceb0959 | 4365 | |
a9a0661e | 4366 | BOOL substrate_; |
36bb2ca2 | 4367 | } |
dc5812ec | 4368 | |
b5e7eebb | 4369 | - (id) initWithDatabase:(Database *)database; |
b4d89997 | 4370 | |
dc5812ec JF |
4371 | @end |
4372 | ||
adb61bda | 4373 | @implementation ConfirmationController |
dc5812ec | 4374 | |
ab2cfc1e JF |
4375 | - (void) complete { |
4376 | if (substrate_) | |
be860cc8 | 4377 | RestartSubstrate_ = true; |
ab2cfc1e JF |
4378 | [delegate_ confirmWithNavigationController:[self navigationController]]; |
4379 | } | |
4380 | ||
b5e7eebb | 4381 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
3272e699 | 4382 | NSString *context([alert context]); |
9bedffaa | 4383 | |
1cedb821 | 4384 | if ([context isEqualToString:@"remove"]) { |
ab2cfc1e | 4385 | if (button == [alert cancelButtonIndex]) |
b5e7eebb | 4386 | [self dismissModalViewControllerAnimated:YES]; |
ab2cfc1e JF |
4387 | else if (button == [alert firstOtherButtonIndex]) { |
4388 | [self complete]; | |
9bedffaa | 4389 | } |
9bedffaa | 4390 | |
3272e699 | 4391 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 4392 | } else if ([context isEqualToString:@"unable"]) { |
b5e7eebb | 4393 | [self dismissModalViewControllerAnimated:YES]; |
3272e699 GP |
4394 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
4395 | } else { | |
b5e7eebb GP |
4396 | [super alertView:alert clickedButtonAtIndex:button]; |
4397 | } | |
36bb2ca2 JF |
4398 | } |
4399 | ||
46c46f4f | 4400 | - (void) _doContinue { |
21ea11a4 GP |
4401 | [self dismissModalViewControllerAnimated:YES]; |
4402 | [delegate_ cancelAndClear:NO]; | |
46c46f4f | 4403 | } |
bc11cf5b | 4404 | |
46c46f4f JF |
4405 | - (id) invokeDefaultMethodWithArguments:(NSArray *)args { |
4406 | [self performSelectorOnMainThread:@selector(_doContinue) withObject:nil waitUntilDone:NO]; | |
21ea11a4 GP |
4407 | return nil; |
4408 | } | |
4409 | ||
2634b249 JF |
4410 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4411 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
6ceb0959 | 4412 | |
781001d7 | 4413 | [window setValue:[[NSDictionary dictionaryWithObjectsAndKeys: |
7b33d201 JF |
4414 | (id) changes_, @"changes", |
4415 | (id) issues_, @"issues", | |
4416 | (id) sizes_, @"sizes", | |
781001d7 JF |
4417 | self, @"queue", |
4418 | nil] Cydia$webScriptObjectInContext:window] forKey:@"cydiaConfirm"]; | |
36bb2ca2 JF |
4419 | } |
4420 | ||
b5e7eebb GP |
4421 | - (id) initWithDatabase:(Database *)database { |
4422 | if ((self = [super init]) != nil) { | |
770f2a8e JF |
4423 | database_ = database; |
4424 | ||
6ceb0959 JF |
4425 | NSMutableArray *installs([NSMutableArray arrayWithCapacity:16]); |
4426 | NSMutableArray *reinstalls([NSMutableArray arrayWithCapacity:16]); | |
4427 | NSMutableArray *upgrades([NSMutableArray arrayWithCapacity:16]); | |
4428 | NSMutableArray *downgrades([NSMutableArray arrayWithCapacity:16]); | |
4429 | NSMutableArray *removes([NSMutableArray arrayWithCapacity:16]); | |
dc5812ec | 4430 | |
36bb2ca2 | 4431 | bool remove(false); |
dc5812ec | 4432 | |
6ceb0959 JF |
4433 | pkgCacheFile &cache([database_ cache]); |
4434 | NSArray *packages([database_ packages]); | |
a9a0661e JF |
4435 | pkgDepCache::Policy *policy([database_ policy]); |
4436 | ||
7b33d201 | 4437 | issues_ = [NSMutableArray arrayWithCapacity:4]; |
6ceb0959 | 4438 | |
c4dcf2c2 | 4439 | for (Package *package in packages) { |
6ceb0959 JF |
4440 | pkgCache::PkgIterator iterator([package iterator]); |
4441 | NSString *name([package id]); | |
4442 | ||
4443 | if ([package broken]) { | |
4444 | NSMutableArray *reasons([NSMutableArray arrayWithCapacity:4]); | |
4445 | ||
4446 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
4447 | name, @"package", | |
4448 | reasons, @"reasons", | |
4449 | nil]]; | |
4450 | ||
4451 | pkgCache::VerIterator ver(cache[iterator].InstVerIter(cache)); | |
4452 | if (ver.end()) | |
4453 | continue; | |
4454 | ||
4455 | for (pkgCache::DepIterator dep(ver.DependsList()); !dep.end(); ) { | |
4456 | pkgCache::DepIterator start; | |
4457 | pkgCache::DepIterator end; | |
4458 | dep.GlobOr(start, end); // ++dep | |
4459 | ||
4460 | if (!cache->IsImportantDep(end)) | |
4461 | continue; | |
4462 | if ((cache[end] & pkgDepCache::DepGInstall) != 0) | |
4463 | continue; | |
4464 | ||
810c9763 JF |
4465 | NSMutableArray *clauses([NSMutableArray arrayWithCapacity:4]); |
4466 | ||
4467 | [reasons addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
4468 | [NSString stringWithUTF8String:start.DepType()], @"relationship", | |
4469 | clauses, @"clauses", | |
4470 | nil]]; | |
4471 | ||
6ceb0959 JF |
4472 | _forever { |
4473 | NSString *reason, *installed((NSString *) [WebUndefined undefined]); | |
4474 | ||
4475 | pkgCache::PkgIterator target(start.TargetPkg()); | |
4476 | if (target->ProvidesList != 0) | |
4477 | reason = @"missing"; | |
4478 | else { | |
4479 | pkgCache::VerIterator ver(cache[target].InstVerIter(cache)); | |
4480 | if (!ver.end()) { | |
4481 | reason = @"installed"; | |
4482 | installed = [NSString stringWithUTF8String:ver.VerStr()]; | |
4483 | } else if (!cache[target].CandidateVerIter(cache).end()) | |
4484 | reason = @"uninstalled"; | |
4485 | else if (target->ProvidesList == 0) | |
4486 | reason = @"uninstallable"; | |
4487 | else | |
4488 | reason = @"virtual"; | |
4489 | } | |
4490 | ||
4491 | NSDictionary *version(start.TargetVer() == 0 ? [NSNull null] : [NSDictionary dictionaryWithObjectsAndKeys: | |
4492 | [NSString stringWithUTF8String:start.CompType()], @"operator", | |
4493 | [NSString stringWithUTF8String:start.TargetVer()], @"value", | |
4494 | nil]); | |
4495 | ||
810c9763 | 4496 | [clauses addObject:[NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
4497 | [NSString stringWithUTF8String:start.TargetPkg().Name()], @"package", |
4498 | version, @"version", | |
4499 | reason, @"reason", | |
4500 | installed, @"installed", | |
4501 | nil]]; | |
4502 | ||
4503 | // yes, seriously. (wtf?) | |
4504 | if (start == end) | |
4505 | break; | |
4506 | ++start; | |
4507 | } | |
4508 | } | |
4509 | } | |
4510 | ||
36bb2ca2 | 4511 | pkgDepCache::StateCache &state(cache[iterator]); |
dc5812ec | 4512 | |
6ceb0959 | 4513 | static Pcre special_r("^(firmware$|gsc\\.|cy\\+)"); |
2388b078 | 4514 | |
36bb2ca2 | 4515 | if (state.NewInstall()) |
6ceb0959 | 4516 | [installs addObject:name]; |
f8d15be2 | 4517 | // XXX: else if (state.Install()) |
36bb2ca2 | 4518 | else if (!state.Delete() && (state.iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall) |
6ceb0959 | 4519 | [reinstalls addObject:name]; |
f8d15be2 | 4520 | // XXX: move before previous if |
36bb2ca2 | 4521 | else if (state.Upgrade()) |
6ceb0959 | 4522 | [upgrades addObject:name]; |
36bb2ca2 | 4523 | else if (state.Downgrade()) |
6ceb0959 JF |
4524 | [downgrades addObject:name]; |
4525 | else if (!state.Delete()) | |
f8d15be2 | 4526 | // XXX: _assert(state.Keep()); |
6ceb0959 | 4527 | continue; |
1916f316 JF |
4528 | else if (special_r(name)) |
4529 | [issues_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
4530 | [NSNull null], @"package", | |
4531 | [NSArray arrayWithObjects: | |
4532 | [NSDictionary dictionaryWithObjectsAndKeys: | |
810c9763 JF |
4533 | @"Conflicts", @"relationship", |
4534 | [NSArray arrayWithObjects: | |
4535 | [NSDictionary dictionaryWithObjectsAndKeys: | |
4536 | name, @"package", | |
4537 | [NSNull null], @"version", | |
4538 | @"installed", @"reason", | |
4539 | nil], | |
4540 | nil], @"clauses", | |
1916f316 JF |
4541 | nil], |
4542 | nil], @"reasons", | |
4543 | nil]]; | |
4544 | else { | |
2388b078 | 4545 | if ([package essential]) |
36bb2ca2 | 4546 | remove = true; |
6ceb0959 JF |
4547 | [removes addObject:name]; |
4548 | } | |
a9a0661e JF |
4549 | |
4550 | substrate_ |= DepSubstrate(policy->GetCandidateVer(iterator)); | |
4551 | substrate_ |= DepSubstrate(iterator.CurrentVer()); | |
36bb2ca2 | 4552 | } |
ec97ef06 | 4553 | |
36bb2ca2 JF |
4554 | if (!remove) |
4555 | essential_ = nil; | |
d791dce4 | 4556 | else if (Advanced_) { |
43f3d7f6 | 4557 | NSString *parenthetical(UCLocalize("PARENTHETICAL")); |
f79a4512 | 4558 | |
7b33d201 | 4559 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 4560 | initWithTitle:UCLocalize("REMOVING_ESSENTIALS") |
b5e7eebb GP |
4561 | message:UCLocalize("REMOVING_ESSENTIALS_EX") |
4562 | delegate:self | |
4563 | cancelButtonTitle:[NSString stringWithFormat:parenthetical, UCLocalize("CANCEL_OPERATION"), UCLocalize("SAFE")] | |
1aa29546 JF |
4564 | otherButtonTitles: |
4565 | [NSString stringWithFormat:parenthetical, UCLocalize("FORCE_REMOVAL"), UCLocalize("UNSAFE")], | |
4566 | nil | |
7b33d201 | 4567 | ] autorelease]; |
04fe1349 | 4568 | |
3272e699 | 4569 | [essential_ setContext:@"remove"]; |
9bedffaa | 4570 | } else { |
7b33d201 | 4571 | essential_ = [[[UIAlertView alloc] |
43f3d7f6 | 4572 | initWithTitle:UCLocalize("UNABLE_TO_COMPLY") |
b5e7eebb GP |
4573 | message:UCLocalize("UNABLE_TO_COMPLY_EX") |
4574 | delegate:self | |
4575 | cancelButtonTitle:UCLocalize("OKAY") | |
4576 | otherButtonTitles:nil | |
7b33d201 | 4577 | ] autorelease]; |
ec97ef06 | 4578 | |
b5e7eebb | 4579 | [essential_ setContext:@"unable"]; |
36bb2ca2 | 4580 | } |
ec97ef06 | 4581 | |
7b33d201 | 4582 | changes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
4583 | installs, @"installs", |
4584 | reinstalls, @"reinstalls", | |
4585 | upgrades, @"upgrades", | |
4586 | downgrades, @"downgrades", | |
4587 | removes, @"removes", | |
affeffc7 | 4588 | nil]; |
dc5812ec | 4589 | |
7b33d201 | 4590 | sizes_ = [NSDictionary dictionaryWithObjectsAndKeys: |
6ceb0959 JF |
4591 | [NSNumber numberWithInteger:[database_ fetcher].FetchNeeded()], @"downloading", |
4592 | [NSNumber numberWithInteger:[database_ fetcher].PartialPresent()], @"resuming", | |
affeffc7 | 4593 | nil]; |
dc5812ec | 4594 | |
90351d93 | 4595 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/confirm/", UI_]]]; |
36bb2ca2 | 4596 | } return self; |
b4d89997 | 4597 | } |
8da60fb7 | 4598 | |
e6124cb6 JF |
4599 | - (UIBarButtonItem *) leftButton { |
4600 | return [[[UIBarButtonItem alloc] | |
4601 | initWithTitle:UCLocalize("CANCEL") | |
4602 | style:UIBarButtonItemStylePlain | |
4603 | target:self | |
4604 | action:@selector(cancelButtonClicked) | |
4605 | ] autorelease]; | |
4606 | } | |
4607 | ||
614cd4f1 | 4608 | #if !AlwaysReload |
bcde1e70 | 4609 | - (void) applyRightButton { |
6ceb0959 | 4610 | if ([issues_ count] == 0 && ![self isLoading]) |
dd9de556 JF |
4611 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
4612 | initWithTitle:UCLocalize("CONFIRM") | |
2761d574 | 4613 | style:UIBarButtonItemStyleDone |
dd9de556 JF |
4614 | target:self |
4615 | action:@selector(confirmButtonClicked) | |
4616 | ] autorelease]]; | |
4617 | else | |
bcde1e70 | 4618 | [[self navigationItem] setRightBarButtonItem:nil]; |
affeffc7 | 4619 | } |
bcde1e70 | 4620 | #endif |
affeffc7 | 4621 | |
b5e7eebb | 4622 | - (void) cancelButtonClicked { |
21ea11a4 GP |
4623 | [self dismissModalViewControllerAnimated:YES]; |
4624 | [delegate_ cancelAndClear:YES]; | |
affeffc7 JF |
4625 | } |
4626 | ||
9487f027 | 4627 | #if !AlwaysReload |
b5e7eebb | 4628 | - (void) confirmButtonClicked { |
affeffc7 | 4629 | if (essential_ != nil) |
b5e7eebb | 4630 | [essential_ show]; |
ab2cfc1e JF |
4631 | else |
4632 | [self complete]; | |
affeffc7 | 4633 | } |
9487f027 | 4634 | #endif |
affeffc7 | 4635 | |
36bb2ca2 JF |
4636 | @end |
4637 | /* }}} */ | |
8da60fb7 | 4638 | |
aaae308d JF |
4639 | /* Progress Data {{{ */ |
4640 | @interface CydiaProgressData : NSObject { | |
4641 | _transient id delegate_; | |
4642 | ||
4643 | bool running_; | |
b0b11d99 | 4644 | float percent_; |
aaae308d | 4645 | |
bcbac8f7 JF |
4646 | float current_; |
4647 | float total_; | |
4648 | float speed_; | |
4649 | ||
aaae308d JF |
4650 | _H<NSMutableArray> events_; |
4651 | _H<NSString> title_; | |
4652 | ||
4653 | _H<NSString> status_; | |
4654 | _H<NSString> finish_; | |
4655 | } | |
4656 | ||
4657 | @end | |
4658 | ||
4659 | @implementation CydiaProgressData | |
4660 | ||
4661 | + (NSArray *) _attributeKeys { | |
4662 | return [NSArray arrayWithObjects: | |
bcbac8f7 | 4663 | @"current", |
aaae308d JF |
4664 | @"events", |
4665 | @"finish", | |
b0b11d99 | 4666 | @"percent", |
aaae308d | 4667 | @"running", |
bcbac8f7 | 4668 | @"speed", |
aaae308d | 4669 | @"title", |
bcbac8f7 | 4670 | @"total", |
aaae308d JF |
4671 | nil]; |
4672 | } | |
4673 | ||
4674 | - (NSArray *) attributeKeys { | |
4675 | return [[self class] _attributeKeys]; | |
4676 | } | |
4677 | ||
4678 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
4679 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
4680 | } | |
4681 | ||
4682 | - (id) init { | |
4683 | if ((self = [super init]) != nil) { | |
4684 | events_ = [NSMutableArray arrayWithCapacity:32]; | |
4685 | } return self; | |
4686 | } | |
4687 | ||
4688 | - (void) setDelegate:(id)delegate { | |
4689 | delegate_ = delegate; | |
4690 | } | |
4691 | ||
b0b11d99 JF |
4692 | - (void) setPercent:(float)value { |
4693 | percent_ = value; | |
aaae308d JF |
4694 | } |
4695 | ||
b0b11d99 JF |
4696 | - (NSNumber *) percent { |
4697 | return [NSNumber numberWithFloat:percent_]; | |
aaae308d JF |
4698 | } |
4699 | ||
bcbac8f7 JF |
4700 | - (void) setCurrent:(float)value { |
4701 | current_ = value; | |
4702 | } | |
4703 | ||
4704 | - (NSNumber *) current { | |
4705 | return [NSNumber numberWithFloat:current_]; | |
4706 | } | |
4707 | ||
4708 | - (void) setTotal:(float)value { | |
4709 | total_ = value; | |
4710 | } | |
4711 | ||
4712 | - (NSNumber *) total { | |
4713 | return [NSNumber numberWithFloat:total_]; | |
4714 | } | |
4715 | ||
4716 | - (void) setSpeed:(float)value { | |
4717 | speed_ = value; | |
4718 | } | |
4719 | ||
4720 | - (NSNumber *) speed { | |
4721 | return [NSNumber numberWithFloat:speed_]; | |
4722 | } | |
4723 | ||
aaae308d JF |
4724 | - (NSArray *) events { |
4725 | return events_; | |
4726 | } | |
4727 | ||
4728 | - (void) removeAllEvents { | |
4729 | [events_ removeAllObjects]; | |
4730 | } | |
4731 | ||
4732 | - (void) addEvent:(CydiaProgressEvent *)event { | |
4733 | [events_ addObject:event]; | |
4734 | } | |
4735 | ||
4736 | - (void) setTitle:(NSString *)text { | |
4737 | title_ = text; | |
4738 | } | |
4739 | ||
4740 | - (NSString *) title { | |
4741 | return title_; | |
4742 | } | |
4743 | ||
4744 | - (void) setFinish:(NSString *)text { | |
4745 | finish_ = text; | |
4746 | } | |
4747 | ||
4748 | - (NSString *) finish { | |
4749 | return (id) finish_ ?: [NSNull null]; | |
4750 | } | |
4751 | ||
4752 | - (void) setRunning:(bool)running { | |
4753 | running_ = running; | |
4754 | } | |
4755 | ||
4756 | - (NSNumber *) running { | |
4757 | return running_ ? (NSNumber *) kCFBooleanTrue : (NSNumber *) kCFBooleanFalse; | |
4758 | } | |
4759 | ||
4760 | @end | |
4761 | /* }}} */ | |
adb61bda | 4762 | /* Progress Controller {{{ */ |
a576488f | 4763 | @interface ProgressController : CydiaWebViewController < |
36bb2ca2 JF |
4764 | ProgressDelegate |
4765 | > { | |
8b29f8e6 | 4766 | _transient Database *database_; |
bf7c998c | 4767 | _H<CydiaProgressData, 1> progress_; |
aaae308d | 4768 | unsigned cancel_; |
2367a917 JF |
4769 | } |
4770 | ||
b5e7eebb | 4771 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate; |
2367a917 | 4772 | |
6915b806 | 4773 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title; |
2367a917 | 4774 | |
6915b806 | 4775 | - (void) setTitle:(NSString *)title; |
aaae308d | 4776 | - (void) setCancellable:(bool)cancellable; |
bd150f54 | 4777 | |
36bb2ca2 JF |
4778 | @end |
4779 | ||
adb61bda | 4780 | @implementation ProgressController |
2367a917 JF |
4781 | |
4782 | - (void) dealloc { | |
6915b806 | 4783 | [database_ setProgressDelegate:nil]; |
2367a917 JF |
4784 | [super dealloc]; |
4785 | } | |
4786 | ||
63ae52be JF |
4787 | - (UIBarButtonItem *) leftButton { |
4788 | return cancel_ == 1 ? [[[UIBarButtonItem alloc] | |
3c62d654 JF |
4789 | initWithTitle:UCLocalize("CANCEL") |
4790 | style:UIBarButtonItemStylePlain | |
4791 | target:self | |
4792 | action:@selector(cancel) | |
63ae52be JF |
4793 | ] autorelease] : nil; |
4794 | } | |
4795 | ||
4796 | - (void) updateCancel { | |
4797 | [super applyLeftButton]; | |
3c62d654 JF |
4798 | } |
4799 | ||
b5e7eebb GP |
4800 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { |
4801 | if ((self = [super init]) != nil) { | |
8b29f8e6 | 4802 | database_ = database; |
36bb2ca2 | 4803 | delegate_ = delegate; |
ec97ef06 | 4804 | |
6915b806 JF |
4805 | [database_ setProgressDelegate:self]; |
4806 | ||
aaae308d JF |
4807 | progress_ = [[[CydiaProgressData alloc] init] autorelease]; |
4808 | [progress_ setDelegate:self]; | |
3c62d654 | 4809 | |
90351d93 | 4810 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/progress/", UI_]]]; |
3c62d654 JF |
4811 | |
4812 | [scroller_ setBackgroundColor:[UIColor blackColor]]; | |
4813 | ||
4814 | [[self navigationItem] setHidesBackButton:YES]; | |
4815 | ||
4816 | [self updateCancel]; | |
36bb2ca2 | 4817 | } return self; |
2367a917 JF |
4818 | } |
4819 | ||
aaae308d JF |
4820 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
4821 | [super webView:view didClearWindowObject:window forFrame:frame]; | |
4822 | [window setValue:progress_ forKey:@"cydiaProgress"]; | |
4823 | } | |
bc11cf5b | 4824 | |
aaae308d JF |
4825 | - (void) updateProgress { |
4826 | [self dispatchEvent:@"CydiaProgressUpdate"]; | |
4827 | } | |
b5e7eebb | 4828 | |
b5e7eebb | 4829 | - (void) viewWillAppear:(BOOL)animated { |
14e4ff09 | 4830 | [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlack]; |
3c62d654 | 4831 | [super viewWillAppear:animated]; |
2367a917 JF |
4832 | } |
4833 | ||
aaae308d | 4834 | - (void) close { |
ef494bd8 RP |
4835 | UpdateExternalStatus(0); |
4836 | ||
670a0494 JF |
4837 | switch (Finish_) { |
4838 | case 0: | |
670a0494 JF |
4839 | break; |
4840 | ||
4841 | case 1: | |
c4899376 JF |
4842 | [delegate_ terminateWithSuccess]; |
4843 | /*if ([delegate_ respondsToSelector:@selector(suspendWithAnimation:)]) | |
4844 | [delegate_ suspendWithAnimation:YES]; | |
4845 | else | |
4846 | [delegate_ suspend];*/ | |
670a0494 JF |
4847 | break; |
4848 | ||
4849 | case 2: | |
985d2dff | 4850 | _trace(); |
ef812071 | 4851 | goto reload; |
670a0494 JF |
4852 | |
4853 | case 3: | |
985d2dff | 4854 | _trace(); |
ef812071 JF |
4855 | goto reload; |
4856 | ||
4857 | reload: | |
4858 | system("/usr/bin/sbreload"); | |
985d2dff | 4859 | _trace(); |
670a0494 JF |
4860 | break; |
4861 | ||
4862 | case 4: | |
985d2dff | 4863 | _trace(); |
0e371502 JF |
4864 | if (void (*SBReboot)(mach_port_t) = reinterpret_cast<void (*)(mach_port_t)>(dlsym(RTLD_DEFAULT, "SBReboot"))) |
4865 | SBReboot(SBSSpringBoardServerPort()); | |
4866 | else | |
bb0fe3c9 | 4867 | reboot2(RB_AUTOBOOT); |
670a0494 | 4868 | break; |
bc8cd583 | 4869 | } |
aaae308d JF |
4870 | |
4871 | [super close]; | |
670a0494 | 4872 | } |
bd150f54 | 4873 | |
6915b806 | 4874 | - (void) setTitle:(NSString *)title { |
aaae308d JF |
4875 | [progress_ setTitle:title]; |
4876 | [self updateProgress]; | |
4877 | } | |
4878 | ||
4879 | - (UIBarButtonItem *) rightButton { | |
d53628b6 | 4880 | return [[progress_ running] boolValue] ? [super rightButton] : [[[UIBarButtonItem alloc] |
aaae308d JF |
4881 | initWithTitle:UCLocalize("CLOSE") |
4882 | style:UIBarButtonItemStylePlain | |
4883 | target:self | |
4884 | action:@selector(close) | |
4885 | ] autorelease]; | |
6915b806 JF |
4886 | } |
4887 | ||
4888 | - (void) invoke:(NSInvocation *)invocation withTitle:(NSString *)title { | |
4889 | UpdateExternalStatus(1); | |
4890 | ||
aaae308d | 4891 | [progress_ setRunning:true]; |
6915b806 | 4892 | [self setTitle:title]; |
aaae308d | 4893 | // implicit updateProgress |
6915b806 | 4894 | |
140710ba | 4895 | SHA1SumValue notifyconf; { |
6915b806 JF |
4896 | FileFd file; |
4897 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
4898 | _error->Discard(); | |
4899 | else { | |
4900 | MMap mmap(file, MMap::ReadOnly); | |
4901 | SHA1Summation sha1; | |
4902 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 4903 | notifyconf = sha1.Result(); |
6915b806 JF |
4904 | } |
4905 | } | |
4906 | ||
140710ba | 4907 | SHA1SumValue springlist; { |
6915b806 JF |
4908 | FileFd file; |
4909 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
4910 | _error->Discard(); | |
4911 | else { | |
4912 | MMap mmap(file, MMap::ReadOnly); | |
4913 | SHA1Summation sha1; | |
4914 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 4915 | springlist = sha1.Result(); |
6915b806 JF |
4916 | } |
4917 | } | |
4918 | ||
4919 | if (invocation != nil) { | |
4920 | [invocation yieldToSelector:@selector(invoke)]; | |
aaae308d | 4921 | [self setTitle:@"COMPLETE"]; |
6915b806 | 4922 | } |
670a0494 | 4923 | |
22f8bed9 | 4924 | if (Finish_ < 4) { |
70d45c1e JF |
4925 | FileFd file; |
4926 | if (!file.Open(NotifyConfig_, FileFd::ReadOnly)) | |
4927 | _error->Discard(); | |
4928 | else { | |
4929 | MMap mmap(file, MMap::ReadOnly); | |
4930 | SHA1Summation sha1; | |
4931 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 4932 | if (!(notifyconf == sha1.Result())) |
70d45c1e JF |
4933 | Finish_ = 4; |
4934 | } | |
22f8bed9 JF |
4935 | } |
4936 | ||
affeffc7 | 4937 | if (Finish_ < 3) { |
70d45c1e JF |
4938 | FileFd file; |
4939 | if (!file.Open(SpringBoard_, FileFd::ReadOnly)) | |
4940 | _error->Discard(); | |
4941 | else { | |
4942 | MMap mmap(file, MMap::ReadOnly); | |
4943 | SHA1Summation sha1; | |
4944 | sha1.Add(reinterpret_cast<uint8_t *>(mmap.Data()), mmap.Size()); | |
140710ba | 4945 | if (!(springlist == sha1.Result())) |
70d45c1e JF |
4946 | Finish_ = 3; |
4947 | } | |
dddbc481 JF |
4948 | } |
4949 | ||
be860cc8 JF |
4950 | if (Finish_ < 2) { |
4951 | if (RestartSubstrate_) | |
4952 | Finish_ = 2; | |
4953 | } | |
4954 | ||
4955 | RestartSubstrate_ = false; | |
4956 | ||
bc8cd583 | 4957 | switch (Finish_) { |
aaae308d JF |
4958 | case 0: [progress_ setFinish:UCLocalize("RETURN_TO_CYDIA")]; break; /* XXX: Maybe UCLocalize("DONE")? */ |
4959 | case 1: [progress_ setFinish:UCLocalize("CLOSE_CYDIA")]; break; | |
4960 | case 2: [progress_ setFinish:UCLocalize("RESTART_SPRINGBOARD")]; break; | |
4961 | case 3: [progress_ setFinish:UCLocalize("RELOAD_SPRINGBOARD")]; break; | |
4962 | case 4: [progress_ setFinish:UCLocalize("REBOOT_DEVICE")]; break; | |
bc8cd583 JF |
4963 | } |
4964 | ||
985d2dff | 4965 | _trace(); |
58241d4c | 4966 | system("su -c /usr/bin/uicache mobile"); |
985d2dff | 4967 | _trace(); |
c4ce98df | 4968 | |
eb403f34 | 4969 | UpdateExternalStatus(Finish_ == 0 ? 0 : 2); |
ef494bd8 | 4970 | |
aaae308d JF |
4971 | [progress_ setRunning:false]; |
4972 | [self updateProgress]; | |
4973 | ||
4974 | [self applyRightButton]; | |
31f3cfff JF |
4975 | } |
4976 | ||
6915b806 | 4977 | - (void) addProgressEvent:(CydiaProgressEvent *)event { |
aaae308d JF |
4978 | [progress_ addEvent:event]; |
4979 | [self updateProgress]; | |
baf80942 JF |
4980 | } |
4981 | ||
ff2d5dcd | 4982 | - (bool) isProgressCancelled { |
aaae308d JF |
4983 | return cancel_ == 2; |
4984 | } | |
4985 | ||
4986 | - (void) cancel { | |
4987 | cancel_ = 2; | |
4988 | [self updateCancel]; | |
4989 | } | |
4990 | ||
4991 | - (void) setCancellable:(bool)cancellable { | |
4992 | unsigned cancel(cancel_); | |
4993 | ||
4994 | if (!cancellable) | |
4995 | cancel_ = 0; | |
4996 | else if (cancel_ == 0) | |
4997 | cancel_ = 1; | |
4998 | ||
4999 | if (cancel != cancel_) | |
5000 | [self updateCancel]; | |
5001 | } | |
5002 | ||
5003 | - (void) setProgressCancellable:(NSNumber *)cancellable { | |
5004 | [self setCancellable:[cancellable boolValue]]; | |
baf80942 JF |
5005 | } |
5006 | ||
d885343d | 5007 | - (void) setProgressPercent:(NSNumber *)percent { |
b0b11d99 | 5008 | [progress_ setPercent:[percent floatValue]]; |
aaae308d | 5009 | [self updateProgress]; |
49048579 JF |
5010 | } |
5011 | ||
bcbac8f7 JF |
5012 | - (void) setProgressStatus:(NSDictionary *)status { |
5013 | if (status == nil) { | |
5014 | [progress_ setCurrent:0]; | |
5015 | [progress_ setTotal:0]; | |
5016 | [progress_ setSpeed:0]; | |
5017 | } else { | |
5018 | [progress_ setPercent:[[status objectForKey:@"Percent"] floatValue]]; | |
5019 | ||
5020 | [progress_ setCurrent:[[status objectForKey:@"Current"] floatValue]]; | |
5021 | [progress_ setTotal:[[status objectForKey:@"Total"] floatValue]]; | |
5022 | [progress_ setSpeed:[[status objectForKey:@"Speed"] floatValue]]; | |
5023 | } | |
5024 | ||
5025 | [self updateProgress]; | |
5026 | } | |
5027 | ||
36bb2ca2 JF |
5028 | @end |
5029 | /* }}} */ | |
dc088e63 | 5030 | |
46aa9775 | 5031 | /* Package Cell {{{ */ |
a9311516 | 5032 | @interface PackageCell : CyteTableViewCell < |
b97fcfc6 | 5033 | CyteTableViewCellDelegate |
c21004b9 | 5034 | > { |
7b33d201 JF |
5035 | _H<UIImage> icon_; |
5036 | _H<NSString> name_; | |
5037 | _H<NSString> description_; | |
3bd1c2a2 | 5038 | bool commercial_; |
7b33d201 JF |
5039 | _H<NSString> source_; |
5040 | _H<UIImage> badge_; | |
5041 | _H<Package> package_; | |
5042 | _H<UIImage> placard_; | |
59f3d290 | 5043 | bool summarized_; |
36bb2ca2 | 5044 | } |
723a0072 | 5045 | |
36bb2ca2 | 5046 | - (PackageCell *) init; |
59f3d290 | 5047 | - (void) setPackage:(Package *)package asSummary:(bool)summary; |
ec97ef06 | 5048 | |
327624b6 JF |
5049 | - (void) drawContentRect:(CGRect)rect; |
5050 | ||
5051 | @end | |
5052 | ||
36bb2ca2 JF |
5053 | @implementation PackageCell |
5054 | ||
36bb2ca2 | 5055 | - (PackageCell *) init { |
327624b6 JF |
5056 | CGRect frame(CGRectMake(0, 0, 320, 74)); |
5057 | if ((self = [super initWithFrame:frame reuseIdentifier:@"Package"]) != nil) { | |
5058 | UIView *content([self contentView]); | |
5059 | CGRect bounds([content bounds]); | |
04fe1349 | 5060 | |
b97fcfc6 | 5061 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
04fe1349 JF |
5062 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5063 | [content addSubview:content_]; | |
5064 | ||
327624b6 | 5065 | [content_ setDelegate:self]; |
327624b6 | 5066 | [content_ setOpaque:YES]; |
36bb2ca2 | 5067 | } return self; |
b4d89997 | 5068 | } |
b19871dd | 5069 | |
003fc610 | 5070 | - (NSString *) accessibilityLabel { |
7b33d201 | 5071 | return [NSString stringWithFormat:UCLocalize("COLON_DELIMITED"), (id) name_, (id) description_]; |
003fc610 GP |
5072 | } |
5073 | ||
59f3d290 JF |
5074 | - (void) setPackage:(Package *)package asSummary:(bool)summary { |
5075 | summarized_ = summary; | |
5076 | ||
7b33d201 JF |
5077 | icon_ = nil; |
5078 | name_ = nil; | |
5079 | description_ = nil; | |
5080 | source_ = nil; | |
5081 | badge_ = nil; | |
5082 | placard_ = nil; | |
5083 | package_ = nil; | |
5084 | ||
68d927e2 | 5085 | [package parse]; |
31f3cfff | 5086 | |
36bb2ca2 | 5087 | Source *source = [package source]; |
b19871dd | 5088 | |
7b33d201 JF |
5089 | icon_ = [package icon]; |
5090 | name_ = [package name]; | |
ef055c6c JF |
5091 | |
5092 | if (IsWildcat_) | |
5093 | description_ = [package longDescription]; | |
5094 | if (description_ == nil) | |
5095 | description_ = [package shortDescription]; | |
ef055c6c | 5096 | |
3bd1c2a2 | 5097 | commercial_ = [package isCommercial]; |
36bb2ca2 | 5098 | |
7b33d201 | 5099 | package_ = package; |
dc63e78f | 5100 | |
a54b1c10 JF |
5101 | NSString *label = nil; |
5102 | bool trusted = false; | |
b19871dd | 5103 | |
36bb2ca2 JF |
5104 | if (source != nil) { |
5105 | label = [source label]; | |
5106 | trusted = [source trusted]; | |
a54b1c10 | 5107 | } else if ([[package id] isEqualToString:@"firmware"]) |
43f3d7f6 | 5108 | label = UCLocalize("APPLE"); |
7b0ce2da | 5109 | else |
43f3d7f6 | 5110 | label = [NSString stringWithFormat:UCLocalize("SLASH_DELIMITED"), UCLocalize("UNKNOWN"), UCLocalize("LOCAL")]; |
b19871dd | 5111 | |
f79a4512 | 5112 | NSString *from(label); |
a54b1c10 | 5113 | |
f79a4512 JF |
5114 | NSString *section = [package simpleSection]; |
5115 | if (section != nil && ![section isEqualToString:label]) { | |
5116 | section = [[NSBundle mainBundle] localizedStringForKey:section value:nil table:@"Sections"]; | |
43f3d7f6 | 5117 | from = [NSString stringWithFormat:UCLocalize("PARENTHETICAL"), from, section]; |
f79a4512 | 5118 | } |
a54b1c10 | 5119 | |
7b33d201 | 5120 | source_ = [NSString stringWithFormat:UCLocalize("FROM"), from]; |
36bb2ca2 | 5121 | |
c390d3ab | 5122 | if (NSString *purpose = [package primaryPurpose]) |
7b33d201 | 5123 | badge_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/Purposes/%@.png", App_, purpose]]; |
c390d3ab | 5124 | |
d832908d JF |
5125 | UIColor *color; |
5126 | NSString *placard; | |
5127 | ||
5128 | if (NSString *mode = [package_ mode]) { | |
5129 | if ([mode isEqualToString:@"REMOVE"] || [mode isEqualToString:@"PURGE"]) { | |
5130 | color = RemovingColor_; | |
5131 | //placard = @"removing"; | |
5132 | } else { | |
5133 | color = InstallingColor_; | |
5134 | //placard = @"installing"; | |
5135 | } | |
5136 | ||
5137 | // XXX: the removing/installing placards are not @2x | |
5138 | placard = nil; | |
5139 | } else { | |
5140 | color = [UIColor whiteColor]; | |
5141 | ||
5142 | if ([package installed] != nil) | |
5143 | placard = @"installed"; | |
5144 | else | |
5145 | placard = nil; | |
5146 | } | |
5147 | ||
5148 | [content_ setBackgroundColor:color]; | |
5149 | ||
5150 | if (placard != nil) | |
7b33d201 | 5151 | placard_ = [UIImage imageAtPath:[NSString stringWithFormat:@"%@/%@.png", App_, placard]]; |
670a0494 | 5152 | |
d832908d | 5153 | [self setNeedsDisplay]; |
327624b6 | 5154 | [content_ setNeedsDisplay]; |
3bd1c2a2 JF |
5155 | } |
5156 | ||
59f3d290 | 5157 | - (void) drawSummaryContentRect:(CGRect)rect { |
555aaf71 | 5158 | bool highlighted(highlighted_); |
ef055c6c | 5159 | float width([self bounds].size.width); |
dc63e78f | 5160 | |
59f3d290 JF |
5161 | if (icon_ != nil) { |
5162 | CGRect rect; | |
5163 | rect.size = [(UIImage *) icon_ size]; | |
5164 | ||
5165 | rect.size.width /= 4; | |
5166 | rect.size.height /= 4; | |
5167 | ||
5168 | rect.origin.x = 14 - rect.size.width / 4; | |
5169 | rect.origin.y = 14 - rect.size.height / 4; | |
5170 | ||
5171 | [icon_ drawInRect:rect]; | |
5172 | } | |
5173 | ||
5174 | if (badge_ != nil) { | |
5175 | CGRect rect; | |
5176 | rect.size = [(UIImage *) badge_ size]; | |
5177 | ||
5178 | rect.size.width /= 4; | |
5179 | rect.size.height /= 4; | |
5180 | ||
5181 | rect.origin.x = 20 - rect.size.width / 4; | |
5182 | rect.origin.y = 20 - rect.size.height / 4; | |
5183 | ||
5184 | [badge_ drawInRect:rect]; | |
5185 | } | |
5186 | ||
5187 | if (highlighted) | |
5188 | UISetColor(White_); | |
5189 | ||
5190 | if (!highlighted) | |
5191 | UISetColor(commercial_ ? Purple_ : Black_); | |
5192 | [name_ drawAtPoint:CGPointMake(36, 8) forWidth:(width - (placard_ == nil ? 68 : 94)) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; | |
5193 | ||
5194 | if (placard_ != nil) | |
5195 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; | |
5196 | } | |
5197 | ||
5198 | - (void) drawNormalContentRect:(CGRect)rect { | |
5199 | bool highlighted(highlighted_); | |
5200 | float width([self bounds].size.width); | |
b19871dd | 5201 | |
baf80942 JF |
5202 | if (icon_ != nil) { |
5203 | CGRect rect; | |
7b33d201 | 5204 | rect.size = [(UIImage *) icon_ size]; |
baf80942 JF |
5205 | |
5206 | rect.size.width /= 2; | |
5207 | rect.size.height /= 2; | |
5208 | ||
5209 | rect.origin.x = 25 - rect.size.width / 2; | |
5210 | rect.origin.y = 25 - rect.size.height / 2; | |
5211 | ||
5212 | [icon_ drawInRect:rect]; | |
5213 | } | |
b19871dd | 5214 | |
c390d3ab | 5215 | if (badge_ != nil) { |
f79c810d | 5216 | CGRect rect; |
7b33d201 | 5217 | rect.size = [(UIImage *) badge_ size]; |
f79c810d JF |
5218 | |
5219 | rect.size.width /= 2; | |
5220 | rect.size.height /= 2; | |
5221 | ||
5222 | rect.origin.x = 36 - rect.size.width / 2; | |
5223 | rect.origin.y = 36 - rect.size.height / 2; | |
c390d3ab | 5224 | |
f79c810d | 5225 | [badge_ drawInRect:rect]; |
c390d3ab JF |
5226 | } |
5227 | ||
555aaf71 | 5228 | if (highlighted) |
f641a0e5 | 5229 | UISetColor(White_); |
b19871dd | 5230 | |
555aaf71 | 5231 | if (!highlighted) |
3bd1c2a2 | 5232 | UISetColor(commercial_ ? Purple_ : Black_); |
54b844a0 DH |
5233 | [name_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - (placard_ == nil ? 80 : 106)) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
5234 | [source_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; | |
b19871dd | 5235 | |
555aaf71 | 5236 | if (!highlighted) |
3bd1c2a2 | 5237 | UISetColor(commercial_ ? Purplish_ : Gray_); |
54b844a0 | 5238 | [description_ drawAtPoint:CGPointMake(12, 46) forWidth:(width - 46) withFont:Font14_ lineBreakMode:UILineBreakModeTailTruncation]; |
670a0494 JF |
5239 | |
5240 | if (placard_ != nil) | |
01d93940 | 5241 | [placard_ drawAtPoint:CGPointMake(width - 52, 9)]; |
ec97ef06 JF |
5242 | } |
5243 | ||
59f3d290 JF |
5244 | - (void) drawContentRect:(CGRect)rect { |
5245 | if (summarized_) | |
5246 | [self drawSummaryContentRect:rect]; | |
5247 | else | |
5248 | [self drawNormalContentRect:rect]; | |
5249 | } | |
5250 | ||
8da60fb7 JF |
5251 | @end |
5252 | /* }}} */ | |
36bb2ca2 | 5253 | /* Section Cell {{{ */ |
a9311516 | 5254 | @interface SectionCell : CyteTableViewCell < |
b97fcfc6 | 5255 | CyteTableViewCellDelegate |
c21004b9 | 5256 | > { |
7b33d201 JF |
5257 | _H<NSString> basic_; |
5258 | _H<NSString> section_; | |
5259 | _H<NSString> name_; | |
5260 | _H<NSString> count_; | |
5261 | _H<UIImage> icon_; | |
5262 | _H<UISwitch> switch_; | |
6d9712c4 | 5263 | BOOL editing_; |
b4d89997 | 5264 | } |
b19871dd | 5265 | |
6d9712c4 | 5266 | - (void) setSection:(Section *)section editing:(BOOL)editing; |
36bb2ca2 | 5267 | |
8da60fb7 JF |
5268 | @end |
5269 | ||
36bb2ca2 | 5270 | @implementation SectionCell |
8da60fb7 | 5271 | |
46aa9775 GP |
5272 | - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
5273 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
7b33d201 JF |
5274 | icon_ = [UIImage applicationImageNamed:@"folder.png"]; |
5275 | switch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(218, 9, 60, 25)] autorelease]; | |
46aa9775 GP |
5276 | [switch_ addTarget:self action:@selector(onSwitch:) forEvents:UIControlEventValueChanged]; |
5277 | ||
5278 | UIView *content([self contentView]); | |
5279 | CGRect bounds([content bounds]); | |
5280 | ||
b97fcfc6 | 5281 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
46aa9775 GP |
5282 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5283 | [content addSubview:content_]; | |
5284 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
5285 | ||
5286 | [content_ setDelegate:self]; | |
b4d89997 | 5287 | } return self; |
b19871dd JF |
5288 | } |
5289 | ||
6d9712c4 | 5290 | - (void) onSwitch:(id)sender { |
a171abd4 | 5291 | NSMutableDictionary *metadata([Sections_ objectForKey:basic_]); |
6d9712c4 JF |
5292 | if (metadata == nil) { |
5293 | metadata = [NSMutableDictionary dictionaryWithCapacity:2]; | |
0010fa77 | 5294 | [Sections_ setObject:metadata forKey:basic_]; |
6d9712c4 JF |
5295 | } |
5296 | ||
46aa9775 | 5297 | [metadata setObject:[NSNumber numberWithBool:([switch_ isOn] == NO)] forKey:@"Hidden"]; |
a171abd4 | 5298 | Changed_ = true; |
6d9712c4 JF |
5299 | } |
5300 | ||
5301 | - (void) setSection:(Section *)section editing:(BOOL)editing { | |
5302 | if (editing != editing_) { | |
5303 | if (editing_) | |
5304 | [switch_ removeFromSuperview]; | |
5305 | else | |
5306 | [self addSubview:switch_]; | |
5307 | editing_ = editing; | |
5308 | } | |
5309 | ||
7b33d201 JF |
5310 | basic_ = nil; |
5311 | section_ = nil; | |
5312 | name_ = nil; | |
5313 | count_ = nil; | |
6d9712c4 | 5314 | |
36bb2ca2 | 5315 | if (section == nil) { |
7b33d201 | 5316 | name_ = UCLocalize("ALL_PACKAGES"); |
f641a0e5 | 5317 | count_ = nil; |
36bb2ca2 | 5318 | } else { |
e59669fd | 5319 | basic_ = [section name]; |
677b8415 | 5320 | section_ = [section localized]; |
0010fa77 | 5321 | |
7b33d201 JF |
5322 | name_ = section_ == nil || [section_ length] == 0 ? UCLocalize("NO_SECTION") : (NSString *) section_; |
5323 | count_ = [NSString stringWithFormat:@"%d", [section count]]; | |
6d9712c4 JF |
5324 | |
5325 | if (editing_) | |
46aa9775 | 5326 | [switch_ setOn:(isSectionVisible(basic_) ? 1 : 0) animated:NO]; |
36bb2ca2 | 5327 | } |
46aa9775 | 5328 | |
c21004b9 | 5329 | [self setAccessoryType:editing ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator]; |
0abb648c JF |
5330 | [self setSelectionStyle:editing ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue]; |
5331 | ||
46aa9775 | 5332 | [content_ setNeedsDisplay]; |
f641a0e5 JF |
5333 | } |
5334 | ||
77801ff1 JF |
5335 | - (void) setFrame:(CGRect)frame { |
5336 | [super setFrame:frame]; | |
46aa9775 | 5337 | |
77801ff1 JF |
5338 | CGRect rect([switch_ frame]); |
5339 | [switch_ setFrame:CGRectMake(frame.size.width - 102, 9, rect.size.width, rect.size.height)]; | |
5340 | } | |
5341 | ||
003fc610 GP |
5342 | - (NSString *) accessibilityLabel { |
5343 | return name_; | |
5344 | } | |
5345 | ||
46aa9775 | 5346 | - (void) drawContentRect:(CGRect)rect { |
8cfba088 | 5347 | bool highlighted(highlighted_ && !editing_); |
bc11cf5b | 5348 | |
f641a0e5 JF |
5349 | [icon_ drawInRect:CGRectMake(8, 7, 32, 32)]; |
5350 | ||
555aaf71 | 5351 | if (highlighted) |
f641a0e5 JF |
5352 | UISetColor(White_); |
5353 | ||
46aa9775 | 5354 | float width(rect.size.width); |
58241d4c | 5355 | if (editing_) |
46aa9775 | 5356 | width -= 87; |
58241d4c | 5357 | |
555aaf71 JF |
5358 | if (!highlighted) |
5359 | UISetColor(Black_); | |
54b844a0 | 5360 | [name_ drawAtPoint:CGPointMake(48, 9) forWidth:(width - 70) withFont:Font22Bold_ lineBreakMode:UILineBreakModeTailTruncation]; |
6d9712c4 | 5361 | |
f641a0e5 JF |
5362 | CGSize size = [count_ sizeWithFont:Font14_]; |
5363 | ||
5364 | UISetColor(White_); | |
5365 | if (count_ != nil) | |
c390d3ab | 5366 | [count_ drawAtPoint:CGPointMake(13 + (29 - size.width) / 2, 16) withFont:Font12Bold_]; |
b19871dd JF |
5367 | } |
5368 | ||
36bb2ca2 JF |
5369 | @end |
5370 | /* }}} */ | |
b19871dd | 5371 | |
ab398adf | 5372 | /* File Table {{{ */ |
cd79e8cf | 5373 | @interface FileTable : CyteViewController < |
c21004b9 JF |
5374 | UITableViewDataSource, |
5375 | UITableViewDelegate | |
5376 | > { | |
36bb2ca2 | 5377 | _transient Database *database_; |
7b33d201 JF |
5378 | _H<Package> package_; |
5379 | _H<NSString> name_; | |
5380 | _H<NSMutableArray> files_; | |
bf7c998c | 5381 | _H<UITableView, 2> list_; |
ab398adf | 5382 | } |
b19871dd | 5383 | |
b5e7eebb | 5384 | - (id) initWithDatabase:(Database *)database; |
ab398adf JF |
5385 | - (void) setPackage:(Package *)package; |
5386 | ||
5387 | @end | |
5388 | ||
5389 | @implementation FileTable | |
5390 | ||
eb30da80 | 5391 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
ab398adf JF |
5392 | return files_ == nil ? 0 : [files_ count]; |
5393 | } | |
5394 | ||
21ea11a4 GP |
5395 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
5396 | return 24.0f; | |
5397 | }*/ | |
ab398adf | 5398 | |
46aa9775 | 5399 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
b5e7eebb GP |
5400 | static NSString *reuseIdentifier = @"Cell"; |
5401 | ||
46aa9775 GP |
5402 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5403 | if (cell == nil) { | |
5404 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
5405 | [cell setFont:[UIFont systemFontOfSize:16]]; | |
ab398adf | 5406 | } |
46aa9775 | 5407 | [cell setText:[files_ objectAtIndex:indexPath.row]]; |
c21004b9 | 5408 | [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; |
b5e7eebb | 5409 | |
46aa9775 | 5410 | return cell; |
ab398adf | 5411 | } |
b19871dd | 5412 | |
fe8e721f GP |
5413 | - (NSURL *) navigationURL { |
5414 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/files", [package_ id]]]; | |
5415 | } | |
5416 | ||
5417 | - (void) loadView { | |
5418 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
5419 | ||
7b33d201 | 5420 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds]] autorelease]; |
fe8e721f GP |
5421 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
5422 | [list_ setRowHeight:24.0f]; | |
7b33d201 | 5423 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f GP |
5424 | [list_ setDelegate:self]; |
5425 | [[self view] addSubview:list_]; | |
5426 | } | |
5427 | ||
5428 | - (void) viewDidLoad { | |
7d887d0b JF |
5429 | [super viewDidLoad]; |
5430 | ||
fe8e721f GP |
5431 | [[self navigationItem] setTitle:UCLocalize("INSTALLED_FILES")]; |
5432 | } | |
5433 | ||
5434 | - (void) releaseSubviews { | |
fe8e721f GP |
5435 | list_ = nil; |
5436 | } | |
5437 | ||
b5e7eebb GP |
5438 | - (id) initWithDatabase:(Database *)database { |
5439 | if ((self = [super init]) != nil) { | |
ab398adf | 5440 | database_ = database; |
b19871dd | 5441 | |
7b33d201 | 5442 | files_ = [NSMutableArray arrayWithCapacity:32]; |
ab398adf JF |
5443 | } return self; |
5444 | } | |
5445 | ||
5446 | - (void) setPackage:(Package *)package { | |
7b33d201 JF |
5447 | package_ = nil; |
5448 | name_ = nil; | |
ab398adf JF |
5449 | |
5450 | [files_ removeAllObjects]; | |
5451 | ||
5452 | if (package != nil) { | |
7b33d201 JF |
5453 | package_ = package; |
5454 | name_ = [package id]; | |
ab398adf | 5455 | |
affeffc7 JF |
5456 | if (NSArray *files = [package files]) |
5457 | [files_ addObjectsFromArray:files]; | |
ab398adf | 5458 | |
9ea8d159 JF |
5459 | if ([files_ count] != 0) { |
5460 | if ([[files_ objectAtIndex:0] isEqualToString:@"/."]) | |
5461 | [files_ removeObjectAtIndex:0]; | |
a54b1c10 | 5462 | [files_ sortUsingSelector:@selector(compareByPath:)]; |
2388b078 JF |
5463 | |
5464 | NSMutableArray *stack = [NSMutableArray arrayWithCapacity:8]; | |
5465 | [stack addObject:@"/"]; | |
5466 | ||
5467 | for (int i(0), e([files_ count]); i != e; ++i) { | |
5468 | NSString *file = [files_ objectAtIndex:i]; | |
5469 | while (![file hasPrefix:[stack lastObject]]) | |
5470 | [stack removeLastObject]; | |
5471 | NSString *directory = [stack lastObject]; | |
5472 | [stack addObject:[file stringByAppendingString:@"/"]]; | |
5473 | [files_ replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%*s%@", | |
a54b1c10 | 5474 | ([stack count] - 2) * 3, "", |
2388b078 JF |
5475 | [file substringFromIndex:[directory length]] |
5476 | ]]; | |
5477 | } | |
ab398adf JF |
5478 | } |
5479 | } | |
5480 | ||
5481 | [list_ reloadData]; | |
5482 | } | |
5483 | ||
ab398adf | 5484 | - (void) reloadData { |
fe8e721f GP |
5485 | [super reloadData]; |
5486 | ||
ab398adf | 5487 | [self setPackage:[database_ packageWithName:name_]]; |
ab398adf | 5488 | } |
b4d89997 | 5489 | |
b4d89997 | 5490 | @end |
36bb2ca2 | 5491 | /* }}} */ |
adb61bda | 5492 | /* Package Controller {{{ */ |
a576488f | 5493 | @interface CYPackageController : CydiaWebViewController < |
9daa7f25 DH |
5494 | UIActionSheetDelegate |
5495 | > { | |
770f2a8e | 5496 | _transient Database *database_; |
5d79f7bf JF |
5497 | _H<Package> package_; |
5498 | _H<NSString> name_; | |
3bd1c2a2 | 5499 | bool commercial_; |
5d79f7bf JF |
5500 | _H<NSMutableArray> buttons_; |
5501 | _H<UIBarButtonItem> button_; | |
b31b87cc JF |
5502 | } |
5503 | ||
57e8b225 | 5504 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name; |
b4d89997 | 5505 | |
36bb2ca2 | 5506 | @end |
b4d89997 | 5507 | |
f6e13561 | 5508 | @implementation CYPackageController |
b4d89997 | 5509 | |
fe8e721f | 5510 | - (NSURL *) navigationURL { |
5d79f7bf | 5511 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@", (id) name_]]; |
fe8e721f GP |
5512 | } |
5513 | ||
f79a4512 | 5514 | /* XXX: this is not safe at all... localization of /fail/ */ |
5a09ae08 | 5515 | - (void) _clickButtonWithName:(NSString *)name { |
43f3d7f6 | 5516 | if ([name isEqualToString:UCLocalize("CLEAR")]) |
dc63e78f | 5517 | [delegate_ clearPackage:package_]; |
43f3d7f6 | 5518 | else if ([name isEqualToString:UCLocalize("INSTALL")]) |
5a09ae08 | 5519 | [delegate_ installPackage:package_]; |
43f3d7f6 | 5520 | else if ([name isEqualToString:UCLocalize("REINSTALL")]) |
5a09ae08 | 5521 | [delegate_ installPackage:package_]; |
43f3d7f6 | 5522 | else if ([name isEqualToString:UCLocalize("REMOVE")]) |
5a09ae08 | 5523 | [delegate_ removePackage:package_]; |
43f3d7f6 | 5524 | else if ([name isEqualToString:UCLocalize("UPGRADE")]) |
5a09ae08 JF |
5525 | [delegate_ installPackage:package_]; |
5526 | else _assert(false); | |
5527 | } | |
5528 | ||
674dce72 | 5529 | - (void) actionSheet:(UIActionSheet *)sheet clickedButtonAtIndex:(NSInteger)button { |
1cedb821 | 5530 | NSString *context([sheet context]); |
5a09ae08 | 5531 | |
1cedb821 | 5532 | if ([context isEqualToString:@"modify"]) { |
674dce72 GP |
5533 | if (button != [sheet cancelButtonIndex]) { |
5534 | NSString *buttonName = [buttons_ objectAtIndex:button]; | |
5535 | [self _clickButtonWithName:buttonName]; | |
5536 | } | |
bc11cf5b | 5537 | |
674dce72 | 5538 | [sheet dismissWithClickedButtonIndex:-1 animated:YES]; |
674dce72 | 5539 | } |
b4d89997 | 5540 | } |
2367a917 | 5541 | |
3e9c9e85 | 5542 | - (bool) _allowJavaScriptPanel { |
3bd1c2a2 | 5543 | return commercial_; |
3e9c9e85 JF |
5544 | } |
5545 | ||
9487f027 | 5546 | #if !AlwaysReload |
9daa7f25 | 5547 | - (void) _customButtonClicked { |
670a0494 JF |
5548 | int count([buttons_ count]); |
5549 | if (count == 0) | |
5550 | return; | |
2367a917 | 5551 | |
5a09ae08 JF |
5552 | if (count == 1) |
5553 | [self _clickButtonWithName:[buttons_ objectAtIndex:0]]; | |
5554 | else { | |
674dce72 | 5555 | NSMutableArray *buttons = [NSMutableArray arrayWithCapacity:count]; |
5a09ae08 | 5556 | [buttons addObjectsFromArray:buttons_]; |
36bb2ca2 | 5557 | |
674dce72 | 5558 | UIActionSheet *sheet = [[[UIActionSheet alloc] |
9ea8d159 | 5559 | initWithTitle:nil |
36bb2ca2 | 5560 | delegate:self |
674dce72 GP |
5561 | cancelButtonTitle:nil |
5562 | destructiveButtonTitle:nil | |
5563 | otherButtonTitles:nil | |
5564 | ] autorelease]; | |
5565 | ||
5566 | for (NSString *button in buttons) [sheet addButtonWithTitle:button]; | |
5567 | if (!IsWildcat_) { | |
5568 | [sheet addButtonWithTitle:UCLocalize("CANCEL")]; | |
5569 | [sheet setCancelButtonIndex:[sheet numberOfButtons] - 1]; | |
5570 | } | |
5571 | [sheet setContext:@"modify"]; | |
bc11cf5b | 5572 | |
b5e7eebb | 5573 | [delegate_ showActionSheet:sheet fromItem:[[self navigationItem] rightBarButtonItem]]; |
36bb2ca2 | 5574 | } |
b4d89997 | 5575 | } |
12b59862 | 5576 | |
a5938ea5 DH |
5577 | // We don't want to allow non-commercial packages to do custom things to the install button, |
5578 | // so it must call customButtonClicked with a custom commercial_ == 1 fallthrough. | |
9daa7f25 | 5579 | - (void) customButtonClicked { |
a5938ea5 DH |
5580 | if (commercial_) |
5581 | [super customButtonClicked]; | |
5582 | else | |
9daa7f25 | 5583 | [self _customButtonClicked]; |
12b59862 | 5584 | } |
2e6c1426 GP |
5585 | |
5586 | - (void) reloadButtonClicked { | |
6a8591be GP |
5587 | // Don't reload a commerical package by tapping the loading button, |
5588 | // but if it's not an Install button, we should forward it on. | |
5589 | if (![package_ uninstalled]) | |
5590 | [self _customButtonClicked]; | |
2fad210a GP |
5591 | } |
5592 | ||
5593 | - (void) applyLoadingTitle { | |
5594 | // Don't show "Loading" as the title. Ever. | |
2e6c1426 | 5595 | } |
a5938ea5 DH |
5596 | |
5597 | - (UIBarButtonItem *) rightButton { | |
2634b249 | 5598 | return button_; |
a5938ea5 | 5599 | } |
9487f027 | 5600 | #endif |
2367a917 | 5601 | |
57e8b225 | 5602 | - (id) initWithDatabase:(Database *)database forPackage:(NSString *)name { |
b5e7eebb | 5603 | if ((self = [super init]) != nil) { |
36bb2ca2 | 5604 | database_ = database; |
5d79f7bf JF |
5605 | buttons_ = [NSMutableArray arrayWithCapacity:4]; |
5606 | name_ = [NSString stringWithString:name]; | |
5607 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/package/%@", UI_, (id) name_]]]; | |
36bb2ca2 | 5608 | } return self; |
dc5812ec JF |
5609 | } |
5610 | ||
57e8b225 | 5611 | - (void) reloadData { |
9dac415b JF |
5612 | [super reloadData]; |
5613 | ||
57e8b225 | 5614 | package_ = [database_ packageWithName:name_]; |
36bb2ca2 | 5615 | |
5a09ae08 JF |
5616 | [buttons_ removeAllObjects]; |
5617 | ||
57e8b225 | 5618 | if (package_ != nil) { |
5d79f7bf | 5619 | [(Package *) package_ parse]; |
68d927e2 | 5620 | |
57e8b225 | 5621 | commercial_ = [package_ isCommercial]; |
36bb2ca2 | 5622 | |
dc63e78f | 5623 | if ([package_ mode] != nil) |
43f3d7f6 | 5624 | [buttons_ addObject:UCLocalize("CLEAR")]; |
5a09ae08 | 5625 | if ([package_ source] == nil); |
31f3cfff | 5626 | else if ([package_ upgradableAndEssential:NO]) |
43f3d7f6 | 5627 | [buttons_ addObject:UCLocalize("UPGRADE")]; |
6a155117 | 5628 | else if ([package_ uninstalled]) |
43f3d7f6 | 5629 | [buttons_ addObject:UCLocalize("INSTALL")]; |
5a09ae08 | 5630 | else |
43f3d7f6 | 5631 | [buttons_ addObject:UCLocalize("REINSTALL")]; |
6a155117 | 5632 | if (![package_ uninstalled]) |
43f3d7f6 | 5633 | [buttons_ addObject:UCLocalize("REMOVE")]; |
2634b249 | 5634 | } |
f79a4512 | 5635 | |
2634b249 JF |
5636 | NSString *title; |
5637 | switch ([buttons_ count]) { | |
5638 | case 0: title = nil; break; | |
5639 | case 1: title = [buttons_ objectAtIndex:0]; break; | |
5640 | default: title = UCLocalize("MODIFY"); break; | |
36bb2ca2 | 5641 | } |
2634b249 | 5642 | |
5d79f7bf | 5643 | button_ = [[[UIBarButtonItem alloc] |
2634b249 JF |
5644 | initWithTitle:title |
5645 | style:UIBarButtonItemStylePlain | |
5646 | target:self | |
5647 | action:@selector(customButtonClicked) | |
5d79f7bf | 5648 | ] autorelease]; |
b5e7eebb | 5649 | } |
f79a4512 | 5650 | |
3bd1c2a2 JF |
5651 | - (bool) isLoading { |
5652 | return commercial_ ? [super isLoading] : false; | |
9fe5e5f8 JF |
5653 | } |
5654 | ||
b4d89997 JF |
5655 | @end |
5656 | /* }}} */ | |
5829aea2 | 5657 | |
f50860ee | 5658 | /* Package List Controller {{{ */ |
cd79e8cf | 5659 | @interface PackageListController : CyteViewController < |
c21004b9 JF |
5660 | UITableViewDataSource, |
5661 | UITableViewDelegate | |
5662 | > { | |
36bb2ca2 | 5663 | _transient Database *database_; |
0175295c | 5664 | unsigned era_; |
56bf1e78 | 5665 | _H<NSArray> packages_; |
7b33d201 | 5666 | _H<NSMutableArray> sections_; |
bf7c998c | 5667 | _H<UITableView, 2> list_; |
7b33d201 JF |
5668 | _H<NSMutableArray> index_; |
5669 | _H<NSMutableDictionary> indices_; | |
5670 | _H<NSString> title_; | |
56bf1e78 | 5671 | unsigned reloading_; |
dc5812ec JF |
5672 | } |
5673 | ||
f50860ee | 5674 | - (id) initWithDatabase:(Database *)database title:(NSString *)title; |
b4d89997 | 5675 | - (void) setDelegate:(id)delegate; |
a0be02eb | 5676 | - (void) resetCursor; |
59f3d290 | 5677 | - (void) clearData; |
b4d89997 | 5678 | |
b4d89997 JF |
5679 | @end |
5680 | ||
f50860ee | 5681 | @implementation PackageListController |
b4d89997 | 5682 | |
59f3d290 JF |
5683 | - (bool) isSummarized { |
5684 | return false; | |
5685 | } | |
5686 | ||
9c5737d5 JF |
5687 | - (bool) showsSections { |
5688 | return true; | |
5689 | } | |
5690 | ||
f50860ee GP |
5691 | - (void) deselectWithAnimation:(BOOL)animated { |
5692 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
5693 | } | |
5694 | ||
bfb45dcb GP |
5695 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration curve:(UIViewAnimationCurve)curve { |
5696 | CGRect base = [[self view] bounds]; | |
5697 | base.size.height -= bounds.size.height; | |
5698 | base.origin = [list_ frame].origin; | |
5699 | ||
5700 | [UIView beginAnimations:nil context:NULL]; | |
5701 | [UIView setAnimationBeginsFromCurrentState:YES]; | |
5702 | [UIView setAnimationCurve:curve]; | |
5703 | [UIView setAnimationDuration:duration]; | |
5704 | [list_ setFrame:base]; | |
5705 | [UIView commitAnimations]; | |
5706 | } | |
5707 | ||
5708 | - (void) resizeForKeyboardBounds:(CGRect)bounds duration:(NSTimeInterval)duration { | |
5709 | [self resizeForKeyboardBounds:bounds duration:duration curve:UIViewAnimationCurveLinear]; | |
5710 | } | |
5711 | ||
5712 | - (void) resizeForKeyboardBounds:(CGRect)bounds { | |
5713 | [self resizeForKeyboardBounds:bounds duration:0]; | |
5714 | } | |
5715 | ||
5716 | - (void) keyboardWillShow:(NSNotification *)notification { | |
5717 | CGRect bounds; | |
5718 | CGPoint center; | |
5719 | NSTimeInterval duration; | |
5720 | UIViewAnimationCurve curve; | |
5721 | [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&bounds]; | |
5722 | [[[notification userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] getValue:¢er]; | |
5723 | [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&curve]; | |
5724 | [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&duration]; | |
5725 | ||
5726 | CGRect kbframe = CGRectMake(round(center.x - bounds.size.width / 2.0), round(center.y - bounds.size.height / 2.0), bounds.size.width, bounds.size.height); | |
38991110 GP |
5727 | UIViewController *base = self; |
5728 | while ([base parentViewController] != nil) | |
5729 | base = [base parentViewController]; | |
5730 | CGRect viewframe = [[base view] convertRect:[list_ frame] fromView:[list_ superview]]; | |
bfb45dcb GP |
5731 | CGRect intersection = CGRectIntersection(viewframe, kbframe); |
5732 | ||
5733 | [self resizeForKeyboardBounds:intersection duration:duration curve:curve]; | |
5734 | } | |
5735 | ||
5736 | - (void) keyboardWillHide:(NSNotification *)notification { | |
5737 | NSTimeInterval duration; | |
5738 | UIViewAnimationCurve curve; | |
5739 | [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&curve]; | |
5740 | [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&duration]; | |
5741 | ||
5742 | [self resizeForKeyboardBounds:CGRectZero duration:duration curve:curve]; | |
5743 | } | |
5744 | ||
5745 | - (void) viewWillAppear:(BOOL)animated { | |
5746 | [super viewWillAppear:animated]; | |
5747 | ||
5748 | [self resizeForKeyboardBounds:CGRectZero]; | |
5749 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
5750 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
5751 | } | |
5752 | ||
5753 | - (void) viewWillDisappear:(BOOL)animated { | |
5754 | [super viewWillDisappear:animated]; | |
5755 | ||
5756 | [self resizeForKeyboardBounds:CGRectZero]; | |
5757 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; | |
5758 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; | |
5759 | } | |
5760 | ||
f50860ee GP |
5761 | - (void) viewDidAppear:(BOOL)animated { |
5762 | [super viewDidAppear:animated]; | |
5763 | [self deselectWithAnimation:animated]; | |
5764 | } | |
5765 | ||
5766 | - (void) didSelectPackage:(Package *)package { | |
57e8b225 | 5767 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id]] autorelease]); |
f50860ee GP |
5768 | [view setDelegate:delegate_]; |
5769 | [[self navigationController] pushViewController:view animated:YES]; | |
5770 | } | |
5771 | ||
d03d7492 | 5772 | #if TryIndexedCollation |
a891c345 GP |
5773 | + (BOOL) hasIndexedCollation { |
5774 | return NO; // XXX: objc_getClass("UILocalizedIndexedCollation") != nil; | |
5775 | } | |
d03d7492 | 5776 | #endif |
a891c345 | 5777 | |
327624b6 JF |
5778 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
5779 | NSInteger count([sections_ count]); | |
5780 | return count == 0 ? 1 : count; | |
b4d89997 | 5781 | } |
2367a917 | 5782 | |
327624b6 | 5783 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
a891c345 | 5784 | if ([sections_ count] == 0 || [[sections_ objectAtIndex:section] count] == 0) |
327624b6 | 5785 | return nil; |
b4d89997 JF |
5786 | return [[sections_ objectAtIndex:section] name]; |
5787 | } | |
dc5812ec | 5788 | |
327624b6 JF |
5789 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
5790 | if ([sections_ count] == 0) | |
5791 | return 0; | |
5792 | return [[sections_ objectAtIndex:section] count]; | |
b4d89997 | 5793 | } |
dc5812ec | 5794 | |
327624b6 | 5795 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
0175295c JF |
5796 | @synchronized (database_) { |
5797 | if ([database_ era] != era_) | |
5798 | return nil; | |
5799 | ||
327624b6 JF |
5800 | Section *section([sections_ objectAtIndex:[path section]]); |
5801 | NSInteger row([path row]); | |
5802 | Package *package([packages_ objectAtIndex:([section row] + row)]); | |
0175295c JF |
5803 | return [[package retain] autorelease]; |
5804 | } } | |
dc5812ec | 5805 | |
327624b6 | 5806 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
c21004b9 | 5807 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); |
327624b6 JF |
5808 | if (cell == nil) |
5809 | cell = [[[PackageCell alloc] init] autorelease]; | |
59f3d290 | 5810 | [cell setPackage:[self packageAtIndexPath:path] asSummary:[self isSummarized]]; |
327624b6 | 5811 | return cell; |
b4d89997 | 5812 | } |
dc5812ec | 5813 | |
f50860ee | 5814 | - (void) tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path { |
327624b6 | 5815 | Package *package([self packageAtIndexPath:path]); |
59c6ae22 | 5816 | package = [database_ packageWithName:[package id]]; |
f50860ee | 5817 | [self didSelectPackage:package]; |
327624b6 JF |
5818 | } |
5819 | ||
5820 | - (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView { | |
9c5737d5 | 5821 | if ([self showsSections]) |
59f3d290 JF |
5822 | return nil; |
5823 | ||
1527b095 | 5824 | return index_; |
327624b6 JF |
5825 | } |
5826 | ||
a891c345 | 5827 | - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { |
d03d7492 | 5828 | #if TryIndexedCollation |
a891c345 GP |
5829 | if ([[self class] hasIndexedCollation]) { |
5830 | return [[objc_getClass("UILocalizedIndexedCollation") currentCollation] sectionForSectionIndexTitleAtIndex:index]; | |
5831 | } | |
d03d7492 | 5832 | #endif |
a891c345 | 5833 | |
327624b6 | 5834 | return index; |
dc5812ec JF |
5835 | } |
5836 | ||
59f3d290 JF |
5837 | - (void) updateHeight { |
5838 | [list_ setRowHeight:([self isSummarized] ? 38 : 73)]; | |
5839 | } | |
5840 | ||
f50860ee GP |
5841 | - (id) initWithDatabase:(Database *)database title:(NSString *)title { |
5842 | if ((self = [super init]) != nil) { | |
36bb2ca2 | 5843 | database_ = database; |
f50860ee GP |
5844 | title_ = [title copy]; |
5845 | [[self navigationItem] setTitle:title_]; | |
36bb2ca2 | 5846 | |
d03d7492 JF |
5847 | #if TryIndexedCollation |
5848 | if ([[self class] hasIndexedCollation]) | |
7b33d201 | 5849 | index_ = [[objc_getClass("UILocalizedIndexedCollation") currentCollation] sectionIndexTitles]; |
d03d7492 JF |
5850 | else |
5851 | #endif | |
7b33d201 | 5852 | index_ = [NSMutableArray arrayWithCapacity:32]; |
d03d7492 | 5853 | |
7b33d201 | 5854 | indices_ = [NSMutableDictionary dictionaryWithCapacity:32]; |
327624b6 | 5855 | |
56bf1e78 | 5856 | packages_ = [NSArray array]; |
7b33d201 | 5857 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
dc5812ec | 5858 | |
7b33d201 | 5859 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; |
04fe1349 | 5860 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
f50860ee | 5861 | [[self view] addSubview:list_]; |
04fe1349 | 5862 | |
1527b095 JF |
5863 | // XXX: is 20 the most optimal number here? |
5864 | [list_ setSectionIndexMinimumDisplayRowCount:20]; | |
5865 | ||
7b33d201 | 5866 | [(UITableView *) list_ setDataSource:self]; |
327624b6 | 5867 | [list_ setDelegate:self]; |
59f3d290 JF |
5868 | |
5869 | [self updateHeight]; | |
b4d89997 | 5870 | } return self; |
dc5812ec JF |
5871 | } |
5872 | ||
5873 | - (void) setDelegate:(id)delegate { | |
2367a917 | 5874 | delegate_ = delegate; |
b4d89997 JF |
5875 | } |
5876 | ||
3025d5b4 JF |
5877 | - (bool) shouldYield { |
5878 | return false; | |
5879 | } | |
b4d89997 | 5880 | |
56bf1e78 JF |
5881 | - (bool) shouldBlock { |
5882 | return false; | |
5883 | } | |
5884 | ||
9c5737d5 | 5885 | - (NSMutableArray *) _reloadPackages { |
695fdd5c | 5886 | @synchronized (database_) { |
c28bc6f1 JF |
5887 | era_ = [database_ era]; |
5888 | NSArray *packages([database_ packages]); | |
5889 | ||
9c5737d5 | 5890 | return [NSMutableArray arrayWithArray:packages]; |
695fdd5c | 5891 | } } |
3025d5b4 JF |
5892 | |
5893 | - (void) _reloadData { | |
56bf1e78 JF |
5894 | if (reloading_ != 0) { |
5895 | reloading_ = 2; | |
5896 | return; | |
5897 | } | |
5898 | ||
ae60e2c1 JF |
5899 | NSArray *packages; |
5900 | ||
3025d5b4 | 5901 | if ([self shouldYield]) { |
bec28dda JF |
5902 | do { |
5903 | UIProgressHUD *hud; | |
56bf1e78 | 5904 | |
bec28dda JF |
5905 | if (![self shouldBlock]) |
5906 | hud = nil; | |
5907 | else { | |
5908 | hud = [delegate_ addProgressHUD]; | |
5909 | [hud setText:UCLocalize("LOADING")]; | |
5910 | } | |
56bf1e78 | 5911 | |
56bf1e78 | 5912 | reloading_ = 1; |
ae60e2c1 | 5913 | packages = [self yieldToSelector:@selector(_reloadPackages)]; |
bec28dda JF |
5914 | |
5915 | if (hud != nil) | |
5916 | [delegate_ removeProgressHUD:hud]; | |
56bf1e78 JF |
5917 | } while (reloading_ == 2); |
5918 | ||
5919 | reloading_ = 0; | |
3025d5b4 | 5920 | } else { |
ae60e2c1 | 5921 | packages = [self _reloadPackages]; |
3025d5b4 | 5922 | } |
36bb2ca2 | 5923 | |
ae60e2c1 JF |
5924 | packages_ = packages; |
5925 | ||
327624b6 | 5926 | [indices_ removeAllObjects]; |
56bf1e78 | 5927 | [sections_ removeAllObjects]; |
327624b6 | 5928 | |
b4d89997 JF |
5929 | Section *section = nil; |
5930 | ||
d03d7492 | 5931 | #if TryIndexedCollation |
a891c345 GP |
5932 | if ([[self class] hasIndexedCollation]) { |
5933 | id collation = [objc_getClass("UILocalizedIndexedCollation") currentCollation]; | |
5934 | NSArray *titles = [collation sectionIndexTitles]; | |
5935 | int secidx = -1; | |
c4dcf2c2 | 5936 | |
a891c345 GP |
5937 | _profile(PackageTable$reloadData$Section) |
5938 | for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { | |
5939 | Package *package; | |
5940 | int index; | |
b4d89997 | 5941 | |
a891c345 GP |
5942 | _profile(PackageTable$reloadData$Section$Package) |
5943 | package = [packages_ objectAtIndex:offset]; | |
5944 | index = [collation sectionForObject:package collationStringSelector:@selector(name)]; | |
808c6eb6 | 5945 | _end |
b4d89997 | 5946 | |
a891c345 GP |
5947 | while (secidx < index) { |
5948 | secidx += 1; | |
327624b6 | 5949 | |
a891c345 GP |
5950 | _profile(PackageTable$reloadData$Section$Allocate) |
5951 | section = [[[Section alloc] initWithName:[titles objectAtIndex:secidx] row:offset localize:NO] autorelease]; | |
5952 | _end | |
5953 | ||
5954 | _profile(PackageTable$reloadData$Section$Add) | |
5955 | [sections_ addObject:section]; | |
5956 | _end | |
5957 | } | |
5958 | ||
5959 | [section addToCount]; | |
808c6eb6 | 5960 | } |
a891c345 | 5961 | _end |
d03d7492 JF |
5962 | } else |
5963 | #endif | |
5964 | { | |
a891c345 | 5965 | [index_ removeAllObjects]; |
808c6eb6 | 5966 | |
9c5737d5 JF |
5967 | bool sectioned([self showsSections]); |
5968 | if (!sectioned) { | |
59f3d290 JF |
5969 | section = [[[Section alloc] initWithName:nil localize:false] autorelease]; |
5970 | [sections_ addObject:section]; | |
5971 | } | |
5972 | ||
a891c345 GP |
5973 | _profile(PackageTable$reloadData$Section) |
5974 | for (size_t offset(0), end([packages_ count]); offset != end; ++offset) { | |
5975 | Package *package; | |
5976 | unichar index; | |
5977 | ||
5978 | _profile(PackageTable$reloadData$Section$Package) | |
5979 | package = [packages_ objectAtIndex:offset]; | |
5980 | index = [package index]; | |
5981 | _end | |
5982 | ||
9c5737d5 | 5983 | if (sectioned && (section == nil || [section index] != index)) { |
a891c345 GP |
5984 | _profile(PackageTable$reloadData$Section$Allocate) |
5985 | section = [[[Section alloc] initWithIndex:index row:offset] autorelease]; | |
5986 | _end | |
5987 | ||
5988 | [index_ addObject:[section name]]; | |
5989 | //[indices_ setObject:[NSNumber numberForInt:[sections_ count]] forKey:index]; | |
5990 | ||
5991 | _profile(PackageTable$reloadData$Section$Add) | |
5992 | [sections_ addObject:section]; | |
5993 | _end | |
5994 | } | |
5995 | ||
5996 | [section addToCount]; | |
5997 | } | |
5998 | _end | |
5999 | } | |
b4d89997 | 6000 | |
59f3d290 JF |
6001 | [self updateHeight]; |
6002 | ||
c4dcf2c2 | 6003 | _profile(PackageTable$reloadData$List) |
59f3d290 | 6004 | [(UITableView *) list_ setDataSource:self]; |
c4dcf2c2 JF |
6005 | [list_ reloadData]; |
6006 | _end | |
b4d89997 JF |
6007 | } |
6008 | ||
3025d5b4 JF |
6009 | - (void) reloadData { |
6010 | [super reloadData]; | |
6011 | [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; | |
6012 | } | |
6013 | ||
a0be02eb | 6014 | - (void) resetCursor { |
4c6a29cd | 6015 | [list_ scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; |
a0be02eb JF |
6016 | } |
6017 | ||
59f3d290 JF |
6018 | - (void) clearData { |
6019 | [self updateHeight]; | |
6020 | ||
6021 | [list_ setDataSource:nil]; | |
6022 | [list_ reloadData]; | |
6023 | ||
6024 | [self resetCursor]; | |
6025 | } | |
6026 | ||
a3328c28 JF |
6027 | @end |
6028 | /* }}} */ | |
f50860ee GP |
6029 | /* Filtered Package List Controller {{{ */ |
6030 | @interface FilteredPackageListController : PackageListController { | |
a3328c28 | 6031 | SEL filter_; |
76933519 | 6032 | IMP imp_; |
7b33d201 | 6033 | _H<NSObject> object_; |
a3328c28 JF |
6034 | } |
6035 | ||
6036 | - (void) setObject:(id)object; | |
01d93940 | 6037 | - (void) setObject:(id)object forFilter:(SEL)filter; |
a3328c28 | 6038 | |
3025d5b4 JF |
6039 | - (SEL) filter; |
6040 | - (void) setFilter:(SEL)filter; | |
6041 | ||
f50860ee | 6042 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object; |
a3328c28 JF |
6043 | |
6044 | @end | |
6045 | ||
f50860ee | 6046 | @implementation FilteredPackageListController |
a3328c28 | 6047 | |
3025d5b4 JF |
6048 | - (SEL) filter { |
6049 | return filter_; | |
6050 | } | |
6051 | ||
01d93940 | 6052 | - (void) setFilter:(SEL)filter { |
aa32d91b | 6053 | @synchronized (self) { |
01d93940 JF |
6054 | filter_ = filter; |
6055 | ||
6056 | /* XXX: this is an unsafe optimization of doomy hell */ | |
6057 | Method method(class_getInstanceMethod([Package class], filter)); | |
6058 | _assert(method != NULL); | |
6059 | imp_ = method_getImplementation(method); | |
6060 | _assert(imp_ != NULL); | |
aa32d91b | 6061 | } } |
01d93940 | 6062 | |
a3328c28 | 6063 | - (void) setObject:(id)object { |
aa32d91b | 6064 | @synchronized (self) { |
7b33d201 | 6065 | object_ = object; |
aa32d91b | 6066 | } } |
a3328c28 | 6067 | |
01d93940 | 6068 | - (void) setObject:(id)object forFilter:(SEL)filter { |
d84597fe | 6069 | @synchronized (self) { |
01d93940 JF |
6070 | [self setFilter:filter]; |
6071 | [self setObject:object]; | |
d84597fe | 6072 | } } |
01d93940 | 6073 | |
9c5737d5 | 6074 | - (NSMutableArray *) _reloadPackages { |
aa32d91b | 6075 | @synchronized (database_) { |
c28bc6f1 JF |
6076 | era_ = [database_ era]; |
6077 | NSArray *packages([database_ packages]); | |
6078 | ||
aa32d91b JF |
6079 | NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); |
6080 | ||
6081 | IMP imp; | |
6082 | SEL filter; | |
6083 | _H<NSObject> object; | |
6084 | ||
6085 | @synchronized (self) { | |
6086 | imp = imp_; | |
6087 | filter = filter_; | |
6088 | object = object_; | |
6089 | } | |
6090 | ||
6091 | _profile(PackageTable$reloadData$Filter) | |
6092 | for (Package *package in packages) | |
6093 | if ([package valid] && (*reinterpret_cast<bool (*)(id, SEL, id)>(imp))(package, filter, object)) | |
6094 | [filtered addObject:package]; | |
76933519 | 6095 | _end |
aa32d91b JF |
6096 | |
6097 | return filtered; | |
6098 | } } | |
a3328c28 | 6099 | |
f50860ee GP |
6100 | - (id) initWithDatabase:(Database *)database title:(NSString *)title filter:(SEL)filter with:(id)object { |
6101 | if ((self = [super initWithDatabase:database title:title]) != nil) { | |
01d93940 | 6102 | [self setFilter:filter]; |
f50860ee | 6103 | [self setObject:object]; |
a3328c28 JF |
6104 | } return self; |
6105 | } | |
6106 | ||
dc5812ec | 6107 | @end |
b5e7eebb GP |
6108 | /* }}} */ |
6109 | ||
5829aea2 | 6110 | /* Home Controller {{{ */ |
a576488f | 6111 | @interface HomeController : CydiaWebViewController { |
b4d89997 | 6112 | } |
6840bff3 | 6113 | |
7b0ce2da | 6114 | @end |
b4d89997 | 6115 | |
5829aea2 | 6116 | @implementation HomeController |
46aa9775 | 6117 | |
3c62d654 JF |
6118 | - (id) init { |
6119 | if ((self = [super init]) != nil) { | |
90351d93 | 6120 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/home/", UI_]]]; |
ed7bfd8c | 6121 | [self reloadData]; |
3c62d654 JF |
6122 | } return self; |
6123 | } | |
6124 | ||
fe8e721f GP |
6125 | - (NSURL *) navigationURL { |
6126 | return [NSURL URLWithString:@"cydia://home"]; | |
6127 | } | |
6128 | ||
5829aea2 GP |
6129 | - (void) aboutButtonClicked { |
6130 | UIAlertView *alert([[[UIAlertView alloc] init] autorelease]); | |
b4d89997 | 6131 | |
5829aea2 GP |
6132 | [alert setTitle:UCLocalize("ABOUT_CYDIA")]; |
6133 | [alert addButtonWithTitle:UCLocalize("CLOSE")]; | |
6134 | [alert setCancelButtonIndex:0]; | |
46aa9775 | 6135 | |
5829aea2 | 6136 | [alert setMessage: |
d53628b6 | 6137 | @"Copyright \u00a9 2008-2011\n" |
7e865c1e JF |
6138 | "SaurikIT, LLC\n" |
6139 | "\n" | |
5829aea2 GP |
6140 | "Jay Freeman (saurik)\n" |
6141 | "saurik@saurik.com\n" | |
6142 | "http://www.saurik.com/" | |
6143 | ]; | |
46aa9775 | 6144 | |
5829aea2 | 6145 | [alert show]; |
a0376fc1 JF |
6146 | } |
6147 | ||
e6124cb6 JF |
6148 | - (UIBarButtonItem *) leftButton { |
6149 | return [[[UIBarButtonItem alloc] | |
35f0a3b5 GP |
6150 | initWithTitle:UCLocalize("ABOUT") |
6151 | style:UIBarButtonItemStylePlain | |
6152 | target:self | |
6153 | action:@selector(aboutButtonClicked) | |
e6124cb6 | 6154 | ] autorelease]; |
7b0ce2da JF |
6155 | } |
6156 | ||
6157 | @end | |
6158 | /* }}} */ | |
5829aea2 | 6159 | /* Manage Controller {{{ */ |
a576488f | 6160 | @interface ManageController : CydiaWebViewController { |
7b0ce2da JF |
6161 | } |
6162 | ||
5829aea2 | 6163 | - (void) queueStatusDidChange; |
6840bff3 | 6164 | |
7b0ce2da JF |
6165 | @end |
6166 | ||
5829aea2 | 6167 | @implementation ManageController |
7b0ce2da | 6168 | |
3c62d654 JF |
6169 | - (id) init { |
6170 | if ((self = [super init]) != nil) { | |
90351d93 | 6171 | [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/#!/manage/", UI_]]]; |
3c62d654 | 6172 | } return self; |
fe8e721f | 6173 | } |
7b0ce2da | 6174 | |
3c62d654 JF |
6175 | - (NSURL *) navigationURL { |
6176 | return [NSURL URLWithString:@"cydia://manage"]; | |
35f0a3b5 | 6177 | } |
7b0ce2da | 6178 | |
e6124cb6 JF |
6179 | - (UIBarButtonItem *) leftButton { |
6180 | return [[[UIBarButtonItem alloc] | |
35f0a3b5 GP |
6181 | initWithTitle:UCLocalize("SETTINGS") |
6182 | style:UIBarButtonItemStylePlain | |
6183 | target:self | |
6184 | action:@selector(settingsButtonClicked) | |
e6124cb6 | 6185 | ] autorelease]; |
7b0ce2da JF |
6186 | } |
6187 | ||
5829aea2 GP |
6188 | - (void) settingsButtonClicked { |
6189 | [delegate_ showSettings]; | |
b5e7eebb GP |
6190 | } |
6191 | ||
5829aea2 GP |
6192 | - (void) queueButtonClicked { |
6193 | [delegate_ queue]; | |
7b0ce2da JF |
6194 | } |
6195 | ||
29756674 JF |
6196 | - (UIBarButtonItem *) customButton { |
6197 | return Queuing_ ? [[[UIBarButtonItem alloc] | |
6198 | initWithTitle:UCLocalize("QUEUE") | |
6199 | style:UIBarButtonItemStyleDone | |
6200 | target:self | |
6201 | action:@selector(queueButtonClicked) | |
6202 | ] autorelease] : [super customButton]; | |
7b0ce2da JF |
6203 | } |
6204 | ||
5829aea2 | 6205 | - (void) queueStatusDidChange { |
29756674 | 6206 | [self applyRightButton]; |
7b0ce2da JF |
6207 | } |
6208 | ||
5829aea2 | 6209 | - (bool) isLoading { |
29756674 | 6210 | return !Queuing_ && [super isLoading]; |
7b0ce2da JF |
6211 | } |
6212 | ||
5829aea2 GP |
6213 | @end |
6214 | /* }}} */ | |
7b0ce2da | 6215 | |
5829aea2 GP |
6216 | /* Refresh Bar {{{ */ |
6217 | @interface RefreshBar : UINavigationBar { | |
7b33d201 JF |
6218 | _H<UIProgressIndicator> indicator_; |
6219 | _H<UITextLabel> prompt_; | |
6220 | _H<UIProgressBar> progress_; | |
6221 | _H<UINavigationButton> cancel_; | |
5829aea2 | 6222 | } |
7b0ce2da | 6223 | |
5829aea2 | 6224 | @end |
7b0ce2da | 6225 | |
5829aea2 | 6226 | @implementation RefreshBar |
7b0ce2da | 6227 | |
5829aea2 GP |
6228 | - (void) positionViews { |
6229 | CGRect frame = [cancel_ frame]; | |
6230 | frame.size = [cancel_ sizeThatFits:frame.size]; | |
6231 | frame.origin.x = [self frame].size.width - frame.size.width - 5; | |
6232 | frame.origin.y = ([self frame].size.height - frame.size.height) / 2; | |
6233 | [cancel_ setFrame:frame]; | |
7b0ce2da | 6234 | |
5829aea2 GP |
6235 | CGSize prgsize = {75, 100}; |
6236 | CGRect prgrect = {{ | |
6237 | [self frame].size.width - prgsize.width - 10, | |
6238 | ([self frame].size.height - prgsize.height) / 2 | |
6239 | } , prgsize}; | |
6240 | [progress_ setFrame:prgrect]; | |
7b0ce2da | 6241 | |
5829aea2 GP |
6242 | CGSize indsize([UIProgressIndicator defaultSizeForStyle:[indicator_ activityIndicatorViewStyle]]); |
6243 | unsigned indoffset = ([self frame].size.height - indsize.height) / 2; | |
6244 | CGRect indrect = {{indoffset, indoffset}, indsize}; | |
6245 | [indicator_ setFrame:indrect]; | |
2fc76a2d | 6246 | |
5829aea2 GP |
6247 | CGSize prmsize = {215, indsize.height + 4}; |
6248 | CGRect prmrect = {{ | |
6249 | indoffset * 2 + indsize.width, | |
6250 | unsigned([self frame].size.height - prmsize.height) / 2 - 1 | |
6251 | }, prmsize}; | |
6252 | [prompt_ setFrame:prmrect]; | |
2fc76a2d JF |
6253 | } |
6254 | ||
6840bff3 | 6255 | - (void) setFrame:(CGRect)frame { |
5829aea2 | 6256 | [super setFrame:frame]; |
5829aea2 | 6257 | [self positionViews]; |
2fc76a2d JF |
6258 | } |
6259 | ||
5829aea2 | 6260 | - (id) initWithFrame:(CGRect)frame delegate:(id)delegate { |
6840bff3 | 6261 | if ((self = [super initWithFrame:frame]) != nil) { |
5829aea2 | 6262 | [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
7b0ce2da | 6263 | |
5829aea2 | 6264 | [self setBarStyle:UIBarStyleBlack]; |
7b0ce2da | 6265 | |
5829aea2 GP |
6266 | UIBarStyle barstyle([self _barStyle:NO]); |
6267 | bool ugly(barstyle == UIBarStyleDefault); | |
3e9c9e85 | 6268 | |
5829aea2 GP |
6269 | UIProgressIndicatorStyle style = ugly ? |
6270 | UIProgressIndicatorStyleMediumBrown : | |
6271 | UIProgressIndicatorStyleMediumWhite; | |
2fc76a2d | 6272 | |
7b33d201 JF |
6273 | indicator_ = [[[UIProgressIndicator alloc] initWithFrame:CGRectZero] autorelease]; |
6274 | [(UIProgressIndicator *) indicator_ setStyle:style]; | |
5829aea2 GP |
6275 | [indicator_ startAnimation]; |
6276 | [self addSubview:indicator_]; | |
7b0ce2da | 6277 | |
7b33d201 | 6278 | prompt_ = [[[UITextLabel alloc] initWithFrame:CGRectZero] autorelease]; |
5829aea2 GP |
6279 | [prompt_ setColor:[UIColor colorWithCGColor:(ugly ? Blueish_ : Off_)]]; |
6280 | [prompt_ setBackgroundColor:[UIColor clearColor]]; | |
6281 | [prompt_ setFont:[UIFont systemFontOfSize:15]]; | |
6282 | [self addSubview:prompt_]; | |
7b0ce2da | 6283 | |
7b33d201 | 6284 | progress_ = [[[UIProgressBar alloc] initWithFrame:CGRectZero] autorelease]; |
5829aea2 | 6285 | [progress_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin]; |
7b33d201 | 6286 | [(UIProgressBar *) progress_ setStyle:0]; |
5829aea2 | 6287 | [self addSubview:progress_]; |
7b0ce2da | 6288 | |
7b33d201 | 6289 | cancel_ = [[[UINavigationButton alloc] initWithTitle:UCLocalize("CANCEL") style:UINavigationButtonStyleHighlighted] autorelease]; |
5829aea2 GP |
6290 | [cancel_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
6291 | [cancel_ addTarget:delegate action:@selector(cancelPressed) forControlEvents:UIControlEventTouchUpInside]; | |
6292 | [cancel_ setBarStyle:barstyle]; | |
3e9c9e85 | 6293 | |
5829aea2 GP |
6294 | [self positionViews]; |
6295 | } return self; | |
6296 | } | |
3e9c9e85 | 6297 | |
aaae308d JF |
6298 | - (void) setCancellable:(bool)cancellable { |
6299 | if (cancellable) | |
6300 | [self addSubview:cancel_]; | |
6301 | else | |
6302 | [cancel_ removeFromSuperview]; | |
5829aea2 | 6303 | } |
7b0ce2da | 6304 | |
5829aea2 GP |
6305 | - (void) start { |
6306 | [prompt_ setText:UCLocalize("UPDATING_DATABASE")]; | |
6307 | [progress_ setProgress:0]; | |
7b0ce2da JF |
6308 | } |
6309 | ||
5829aea2 | 6310 | - (void) stop { |
aaae308d | 6311 | [self setCancellable:NO]; |
7b0ce2da JF |
6312 | } |
6313 | ||
5829aea2 GP |
6314 | - (void) setPrompt:(NSString *)prompt { |
6315 | [prompt_ setText:prompt]; | |
7b0ce2da JF |
6316 | } |
6317 | ||
5829aea2 GP |
6318 | - (void) setProgress:(float)progress { |
6319 | [progress_ setProgress:progress]; | |
7b0ce2da JF |
6320 | } |
6321 | ||
5829aea2 GP |
6322 | @end |
6323 | /* }}} */ | |
b5e7eebb | 6324 | |
28e596e4 | 6325 | /* Cydia Navigation Controller Interface {{{ */ |
15f0d613 | 6326 | @interface UINavigationController (Cydia) |
28e596e4 JF |
6327 | |
6328 | - (NSArray *) navigationURLCollection; | |
15f0d613 | 6329 | - (void) unloadData; |
28e596e4 JF |
6330 | |
6331 | @end | |
6332 | /* }}} */ | |
7b0ce2da | 6333 | |
5829aea2 GP |
6334 | /* Cydia Tab Bar Controller {{{ */ |
6335 | @interface CYTabBarController : UITabBarController < | |
9f99f3da | 6336 | UITabBarControllerDelegate, |
5829aea2 GP |
6337 | ProgressDelegate |
6338 | > { | |
6339 | _transient Database *database_; | |
bf7c998c | 6340 | _H<RefreshBar, 1> refreshbar_; |
7b0ce2da | 6341 | |
5829aea2 GP |
6342 | bool dropped_; |
6343 | bool updating_; | |
6344 | // XXX: ok, "updatedelegate_"?... | |
6345 | _transient NSObject<CydiaDelegate> *updatedelegate_; | |
f79a4512 | 6346 | |
7b33d201 | 6347 | _H<UIViewController> remembered_; |
9f99f3da | 6348 | _transient UIViewController *transient_; |
7b0ce2da JF |
6349 | } |
6350 | ||
35f0a3b5 | 6351 | - (NSArray *) navigationURLCollection; |
5829aea2 GP |
6352 | - (void) dropBar:(BOOL)animated; |
6353 | - (void) beginUpdate; | |
6354 | - (void) raiseBar:(BOOL)animated; | |
6355 | - (BOOL) updating; | |
6c0ba3d9 | 6356 | - (void) unloadData; |
1cedb821 | 6357 | |
5829aea2 | 6358 | @end |
2fc76a2d | 6359 | |
5829aea2 | 6360 | @implementation CYTabBarController |
3e9c9e85 | 6361 | |
9f99f3da GP |
6362 | - (void) setUnselectedViewController:(UIViewController *)transient { |
6363 | NSMutableArray *controllers = [[self viewControllers] mutableCopy]; | |
6364 | if (transient != nil) { | |
6365 | if (transient_ == nil) | |
7b33d201 | 6366 | remembered_ = [controllers objectAtIndex:0]; |
9f99f3da GP |
6367 | transient_ = transient; |
6368 | [transient_ setTabBarItem:[remembered_ tabBarItem]]; | |
6369 | [controllers replaceObjectAtIndex:0 withObject:transient_]; | |
6370 | [self setSelectedIndex:0]; | |
6371 | [self setViewControllers:controllers]; | |
6372 | [self concealTabBarSelection]; | |
6373 | } else if (remembered_ != nil) { | |
6374 | [remembered_ setTabBarItem:[transient_ tabBarItem]]; | |
6375 | transient_ = transient; | |
6376 | [controllers replaceObjectAtIndex:0 withObject:remembered_]; | |
9f99f3da GP |
6377 | remembered_ = nil; |
6378 | [self setViewControllers:controllers]; | |
6379 | [self revealTabBarSelection]; | |
6380 | } | |
6381 | } | |
6382 | ||
6383 | - (UIViewController *) unselectedViewController { | |
6384 | return transient_; | |
6385 | } | |
6386 | ||
6387 | - (void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { | |
6388 | if ([self unselectedViewController]) | |
6389 | [self setUnselectedViewController:nil]; | |
6390 | } | |
6391 | ||
35f0a3b5 | 6392 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
6393 | NSMutableArray *items([NSMutableArray array]); |
6394 | ||
35f0a3b5 | 6395 | // XXX: Should this deal with transient view controllers? |
fe8e721f | 6396 | for (id navigation in [self viewControllers]) { |
35f0a3b5 | 6397 | NSArray *stack = [navigation performSelector:@selector(navigationURLCollection)]; |
fe8e721f GP |
6398 | if (stack != nil) |
6399 | [items addObject:stack]; | |
2fc76a2d | 6400 | } |
c713af59 | 6401 | |
fe8e721f GP |
6402 | return items; |
6403 | } | |
6404 | ||
6c0ba3d9 | 6405 | - (void) unloadData { |
29640f4e JF |
6406 | [super unloadData]; |
6407 | ||
15f0d613 JF |
6408 | for (UINavigationController *controller in [self viewControllers]) |
6409 | [controller unloadData]; | |
6410 | ||
29640f4e JF |
6411 | if (UIViewController *selected = [self selectedViewController]) |
6412 | [selected reloadData]; | |
c713af59 | 6413 | |
29640f4e JF |
6414 | if (UIViewController *unselected = [self unselectedViewController]) { |
6415 | [unselected unloadData]; | |
15f0d613 | 6416 | [unselected reloadData]; |
29640f4e | 6417 | } |
7b0ce2da JF |
6418 | } |
6419 | ||
35f0a3b5 | 6420 | - (void) dealloc { |
35f0a3b5 GP |
6421 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
6422 | ||
6423 | [super dealloc]; | |
6424 | } | |
6425 | ||
b5e7eebb GP |
6426 | - (id) initWithDatabase:(Database *)database { |
6427 | if ((self = [super init]) != nil) { | |
7b0ce2da | 6428 | database_ = database; |
9f99f3da | 6429 | [self setDelegate:self]; |
04fe1349 | 6430 | |
5829aea2 GP |
6431 | [[self view] setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6432 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; | |
7b0ce2da | 6433 | |
7b33d201 | 6434 | refreshbar_ = [[[RefreshBar alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, [UINavigationBar defaultSize].height) delegate:self] autorelease]; |
7b0ce2da JF |
6435 | } return self; |
6436 | } | |
6437 | ||
5829aea2 GP |
6438 | - (void) setUpdate:(NSDate *)date { |
6439 | [self beginUpdate]; | |
6440 | } | |
1cedb821 | 6441 | |
5829aea2 | 6442 | - (void) beginUpdate { |
7b33d201 | 6443 | [(RefreshBar *) refreshbar_ start]; |
5829aea2 | 6444 | [self dropBar:YES]; |
7b0ce2da | 6445 | |
5829aea2 GP |
6446 | [updatedelegate_ retainNetworkActivityIndicator]; |
6447 | updating_ = true; | |
7b0ce2da | 6448 | |
5829aea2 GP |
6449 | [NSThread |
6450 | detachNewThreadSelector:@selector(performUpdate) | |
6451 | toTarget:self | |
6452 | withObject:nil | |
6453 | ]; | |
7b0ce2da JF |
6454 | } |
6455 | ||
d13edf44 JF |
6456 | - (void) performUpdate { |
6457 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
6458 | ||
5829aea2 GP |
6459 | Status status; |
6460 | status.setDelegate(self); | |
6461 | [database_ updateWithStatus:status]; | |
6462 | ||
6463 | [self | |
6464 | performSelectorOnMainThread:@selector(completeUpdate) | |
6465 | withObject:nil | |
6466 | waitUntilDone:NO | |
6467 | ]; | |
d13edf44 JF |
6468 | |
6469 | [pool release]; | |
f6e13561 GP |
6470 | } |
6471 | ||
5829aea2 GP |
6472 | - (void) stopUpdateWithSelector:(SEL)selector { |
6473 | updating_ = false; | |
6474 | [updatedelegate_ releaseNetworkActivityIndicator]; | |
b5e7eebb | 6475 | |
5829aea2 GP |
6476 | [self raiseBar:YES]; |
6477 | [refreshbar_ stop]; | |
bc11cf5b | 6478 | |
5829aea2 | 6479 | [updatedelegate_ performSelector:selector withObject:nil afterDelay:0]; |
2bdd73bd GP |
6480 | } |
6481 | ||
5829aea2 GP |
6482 | - (void) completeUpdate { |
6483 | if (!updating_) | |
6484 | return; | |
6485 | [self stopUpdateWithSelector:@selector(reloadData)]; | |
7b0ce2da JF |
6486 | } |
6487 | ||
5829aea2 GP |
6488 | - (void) cancelUpdate { |
6489 | [self stopUpdateWithSelector:@selector(updateData)]; | |
7b0ce2da JF |
6490 | } |
6491 | ||
5829aea2 GP |
6492 | - (void) cancelPressed { |
6493 | [self cancelUpdate]; | |
6494 | } | |
7b0ce2da | 6495 | |
5829aea2 GP |
6496 | - (BOOL) updating { |
6497 | return updating_; | |
7b0ce2da JF |
6498 | } |
6499 | ||
6915b806 JF |
6500 | - (void) addProgressEvent:(CydiaProgressEvent *)event { |
6501 | [refreshbar_ setPrompt:[event compoundMessage]]; | |
5829aea2 | 6502 | } |
c21004b9 | 6503 | |
ff2d5dcd | 6504 | - (bool) isProgressCancelled { |
5829aea2 GP |
6505 | return !updating_; |
6506 | } | |
7b0ce2da | 6507 | |
aaae308d JF |
6508 | - (void) setProgressCancellable:(NSNumber *)cancellable { |
6509 | [refreshbar_ setCancellable:(updating_ && [cancellable boolValue])]; | |
6510 | } | |
6511 | ||
d885343d | 6512 | - (void) setProgressPercent:(NSNumber *)percent { |
5829aea2 | 6513 | [refreshbar_ setProgress:[percent floatValue]]; |
7b0ce2da JF |
6514 | } |
6515 | ||
bcbac8f7 JF |
6516 | - (void) setProgressStatus:(NSDictionary *)status { |
6517 | if (status != nil) | |
6518 | [self setProgressPercent:[status objectForKey:@"Percent"]]; | |
6519 | } | |
6520 | ||
5829aea2 GP |
6521 | - (void) setUpdateDelegate:(id)delegate { |
6522 | updatedelegate_ = delegate; | |
7b0ce2da JF |
6523 | } |
6524 | ||
5829aea2 GP |
6525 | - (CGFloat) statusBarHeight { |
6526 | if (UIInterfaceOrientationIsPortrait([self interfaceOrientation])) { | |
6527 | return [[UIApplication sharedApplication] statusBarFrame].size.height; | |
6528 | } else { | |
6529 | return [[UIApplication sharedApplication] statusBarFrame].size.width; | |
6530 | } | |
7b0ce2da JF |
6531 | } |
6532 | ||
5829aea2 GP |
6533 | - (UIView *) transitionView { |
6534 | if ([self respondsToSelector:@selector(_transitionView)]) | |
6535 | return [self _transitionView]; | |
6536 | else | |
6537 | return MSHookIvar<id>(self, "_viewControllerTransitionView"); | |
f641a0e5 JF |
6538 | } |
6539 | ||
5829aea2 GP |
6540 | - (void) dropBar:(BOOL)animated { |
6541 | if (dropped_) | |
6542 | return; | |
6543 | dropped_ = true; | |
7b0ce2da | 6544 | |
5829aea2 GP |
6545 | UIView *transition([self transitionView]); |
6546 | [[self view] addSubview:refreshbar_]; | |
f6e13561 | 6547 | |
5829aea2 | 6548 | CGRect barframe([refreshbar_ frame]); |
f6e13561 | 6549 | |
a23207d2 | 6550 | if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_0) // XXX: _UIApplicationLinkedOnOrAfter(4) |
5829aea2 GP |
6551 | barframe.origin.y = [self statusBarHeight]; |
6552 | else | |
6553 | barframe.origin.y = 0; | |
f6e13561 | 6554 | |
5829aea2 | 6555 | [refreshbar_ setFrame:barframe]; |
f6e13561 | 6556 | |
5829aea2 GP |
6557 | if (animated) |
6558 | [UIView beginAnimations:nil context:NULL]; | |
f6e13561 | 6559 | |
5829aea2 GP |
6560 | CGRect viewframe = [transition frame]; |
6561 | viewframe.origin.y += barframe.size.height; | |
6562 | viewframe.size.height -= barframe.size.height; | |
6563 | [transition setFrame:viewframe]; | |
f6e13561 | 6564 | |
5829aea2 GP |
6565 | if (animated) |
6566 | [UIView commitAnimations]; | |
f6e13561 | 6567 | |
5829aea2 GP |
6568 | // Ensure bar has the proper width for our view, it might have changed |
6569 | barframe.size.width = viewframe.size.width; | |
6570 | [refreshbar_ setFrame:barframe]; | |
f6e13561 GP |
6571 | } |
6572 | ||
5829aea2 GP |
6573 | - (void) raiseBar:(BOOL)animated { |
6574 | if (!dropped_) | |
6575 | return; | |
6576 | dropped_ = false; | |
f6e13561 | 6577 | |
5829aea2 GP |
6578 | UIView *transition([self transitionView]); |
6579 | [refreshbar_ removeFromSuperview]; | |
7b0ce2da | 6580 | |
5829aea2 | 6581 | CGRect barframe([refreshbar_ frame]); |
7b0ce2da | 6582 | |
5829aea2 GP |
6583 | if (animated) |
6584 | [UIView beginAnimations:nil context:NULL]; | |
7b0ce2da | 6585 | |
5829aea2 GP |
6586 | CGRect viewframe = [transition frame]; |
6587 | viewframe.origin.y -= barframe.size.height; | |
6588 | viewframe.size.height += barframe.size.height; | |
6589 | [transition setFrame:viewframe]; | |
9daa0bdc | 6590 | |
5829aea2 GP |
6591 | if (animated) |
6592 | [UIView commitAnimations]; | |
bfc87a4d JF |
6593 | } |
6594 | ||
5829aea2 GP |
6595 | - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { |
6596 | bool dropped(dropped_); | |
7b0ce2da | 6597 | |
5829aea2 GP |
6598 | if (dropped) |
6599 | [self raiseBar:NO]; | |
7b0ce2da | 6600 | |
5829aea2 | 6601 | [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; |
7b0ce2da | 6602 | |
5829aea2 GP |
6603 | if (dropped) |
6604 | [self dropBar:NO]; | |
b5e7eebb GP |
6605 | } |
6606 | ||
5829aea2 GP |
6607 | - (void) statusBarFrameChanged:(NSNotification *)notification { |
6608 | if (dropped_) { | |
6609 | [self raiseBar:NO]; | |
6610 | [self dropBar:NO]; | |
6611 | } | |
b5e7eebb GP |
6612 | } |
6613 | ||
7b0ce2da | 6614 | @end |
98228790 | 6615 | /* }}} */ |
f6e13561 | 6616 | |
28e596e4 | 6617 | /* Cydia Navigation Controller Implementation {{{ */ |
15f0d613 | 6618 | @implementation UINavigationController (Cydia) |
bc11cf5b | 6619 | |
35f0a3b5 | 6620 | - (NSArray *) navigationURLCollection { |
fe8e721f GP |
6621 | NSMutableArray *stack([NSMutableArray array]); |
6622 | ||
cd79e8cf | 6623 | for (CyteViewController *controller in [self viewControllers]) { |
fe8e721f GP |
6624 | NSString *url = [[controller navigationURL] absoluteString]; |
6625 | if (url != nil) | |
6626 | [stack addObject:url]; | |
6627 | } | |
6628 | ||
6629 | return stack; | |
6630 | } | |
6631 | ||
15f0d613 JF |
6632 | - (void) reloadData { |
6633 | [super reloadData]; | |
6634 | ||
6635 | if (UIViewController *visible = [self visibleViewController]) | |
6636 | [visible reloadData]; | |
6637 | } | |
6c0ba3d9 | 6638 | |
15f0d613 | 6639 | - (void) unloadData { |
cd79e8cf | 6640 | for (CyteViewController *page in [self viewControllers]) |
15f0d613 | 6641 | [page unloadData]; |
6c0ba3d9 | 6642 | |
15f0d613 | 6643 | [super unloadData]; |
dc63e78f JF |
6644 | } |
6645 | ||
5829aea2 GP |
6646 | @end |
6647 | /* }}} */ | |
7b0ce2da | 6648 | |
5829aea2 GP |
6649 | /* Cydia:// Protocol {{{ */ |
6650 | @interface CydiaURLProtocol : NSURLProtocol { | |
9fe5e5f8 JF |
6651 | } |
6652 | ||
7b0ce2da JF |
6653 | @end |
6654 | ||
5829aea2 GP |
6655 | @implementation CydiaURLProtocol |
6656 | ||
6657 | + (BOOL) canInitWithRequest:(NSURLRequest *)request { | |
6658 | NSURL *url([request URL]); | |
6659 | if (url == nil) | |
6660 | return NO; | |
b0a2900d | 6661 | |
5829aea2 | 6662 | NSString *scheme([[url scheme] lowercaseString]); |
b0a2900d JF |
6663 | if (scheme != nil && [scheme isEqualToString:@"cydia"]) |
6664 | return YES; | |
6665 | if ([[url absoluteString] hasPrefix:@"about:cydia-"]) | |
6666 | return YES; | |
6667 | ||
6668 | return NO; | |
aa5e5990 JF |
6669 | } |
6670 | ||
5829aea2 GP |
6671 | + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { |
6672 | return request; | |
6673 | } | |
aa5e5990 | 6674 | |
5829aea2 GP |
6675 | - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { |
6676 | id<NSURLProtocolClient> client([self client]); | |
6677 | if (icon == nil) | |
6678 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; | |
6679 | else { | |
6680 | NSData *data(UIImagePNGRepresentation(icon)); | |
01d93940 | 6681 | |
5829aea2 GP |
6682 | NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); |
6683 | [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; | |
6684 | [client URLProtocol:self didLoadData:data]; | |
6685 | [client URLProtocolDidFinishLoading:self]; | |
6686 | } | |
3931b718 JF |
6687 | } |
6688 | ||
5829aea2 GP |
6689 | - (void) startLoading { |
6690 | id<NSURLProtocolClient> client([self client]); | |
6691 | NSURLRequest *request([self request]); | |
bc11cf5b | 6692 | |
5829aea2 GP |
6693 | NSURL *url([request URL]); |
6694 | NSString *href([url absoluteString]); | |
b0a2900d JF |
6695 | NSString *scheme([[url scheme] lowercaseString]); |
6696 | ||
6697 | NSString *path; | |
6698 | ||
6699 | if ([scheme isEqualToString:@"cydia"]) | |
6700 | path = [href substringFromIndex:8]; | |
6701 | else if ([scheme isEqualToString:@"about"]) | |
6702 | path = [href substringFromIndex:12]; | |
6703 | else _assert(false); | |
bc11cf5b | 6704 | |
5829aea2 | 6705 | NSRange slash([path rangeOfString:@"/"]); |
bc11cf5b | 6706 | |
5829aea2 GP |
6707 | NSString *command; |
6708 | if (slash.location == NSNotFound) { | |
6709 | command = path; | |
6710 | path = nil; | |
6711 | } else { | |
6712 | command = [path substringToIndex:slash.location]; | |
6713 | path = [path substringFromIndex:(slash.location + 1)]; | |
6714 | } | |
39cda3a8 | 6715 | |
5829aea2 | 6716 | Database *database([Database sharedInstance]); |
bc11cf5b | 6717 | |
5829aea2 GP |
6718 | if ([command isEqualToString:@"package-icon"]) { |
6719 | if (path == nil) | |
6720 | goto fail; | |
6721 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6722 | Package *package([database packageWithName:path]); | |
6723 | if (package == nil) | |
6724 | goto fail; | |
eff20a8d | 6725 | [package parse]; |
5829aea2 GP |
6726 | UIImage *icon([package icon]); |
6727 | [self _returnPNGWithImage:icon forRequest:request]; | |
6728 | } else if ([command isEqualToString:@"source-icon"]) { | |
6729 | if (path == nil) | |
6730 | goto fail; | |
6731 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6732 | NSString *source(Simplify(path)); | |
6733 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sources/%@.png", App_, source]]); | |
6734 | if (icon == nil) | |
6735 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
6736 | [self _returnPNGWithImage:icon forRequest:request]; | |
6737 | } else if ([command isEqualToString:@"uikit-image"]) { | |
6738 | if (path == nil) | |
6739 | goto fail; | |
6740 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6741 | UIImage *icon(_UIImageWithName(path)); | |
6742 | [self _returnPNGWithImage:icon forRequest:request]; | |
6743 | } else if ([command isEqualToString:@"section-icon"]) { | |
6744 | if (path == nil) | |
6745 | goto fail; | |
6746 | path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
6747 | NSString *section(Simplify(path)); | |
34841cd7 | 6748 | UIImage *icon([UIImage imageAtPath:[NSString stringWithFormat:@"%@/Sections/%@.png", App_, [section stringByReplacingOccurrencesOfString:@" " withString:@"_"]]]); |
5829aea2 GP |
6749 | if (icon == nil) |
6750 | icon = [UIImage applicationImageNamed:@"unknown.png"]; | |
6751 | [self _returnPNGWithImage:icon forRequest:request]; | |
6752 | } else fail: { | |
6753 | [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; | |
6754 | } | |
39cda3a8 GP |
6755 | } |
6756 | ||
5829aea2 GP |
6757 | - (void) stopLoading { |
6758 | } | |
807ae6d7 | 6759 | |
5829aea2 GP |
6760 | @end |
6761 | /* }}} */ | |
807ae6d7 | 6762 | |
5829aea2 | 6763 | /* Section Controller {{{ */ |
f50860ee | 6764 | @interface SectionController : FilteredPackageListController { |
123b380c | 6765 | _H<NSString> section_; |
5829aea2 | 6766 | } |
807ae6d7 | 6767 | |
5829aea2 | 6768 | - (id) initWithDatabase:(Database *)database section:(NSString *)section; |
807ae6d7 | 6769 | |
5829aea2 | 6770 | @end |
bc11cf5b | 6771 | |
5829aea2 | 6772 | @implementation SectionController |
bc11cf5b | 6773 | |
fe8e721f GP |
6774 | - (NSURL *) navigationURL { |
6775 | NSString *name = section_; | |
6776 | if (name == nil) | |
6777 | name = @"all"; | |
6778 | ||
6779 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sections/%@", name]]; | |
6780 | } | |
6781 | ||
5829aea2 GP |
6782 | - (id) initWithDatabase:(Database *)database section:(NSString *)name { |
6783 | NSString *title; | |
3707eb56 | 6784 | if (name == nil) |
5829aea2 | 6785 | title = UCLocalize("ALL_PACKAGES"); |
3707eb56 | 6786 | else if (![name isEqual:@""]) |
5829aea2 | 6787 | title = [[NSBundle mainBundle] localizedStringForKey:Simplify(name) value:nil table:@"Sections"]; |
3707eb56 | 6788 | else |
5829aea2 | 6789 | title = UCLocalize("NO_SECTION"); |
fe8e721f | 6790 | |
5829aea2 | 6791 | if ((self = [super initWithDatabase:database title:title filter:@selector(isVisibleInSection:) with:name]) != nil) { |
3707eb56 | 6792 | section_ = name; |
5829aea2 | 6793 | } return self; |
b5e7eebb GP |
6794 | } |
6795 | ||
b5e7eebb GP |
6796 | @end |
6797 | /* }}} */ | |
5829aea2 | 6798 | /* Sections Controller {{{ */ |
cd79e8cf | 6799 | @interface SectionsController : CyteViewController < |
5829aea2 GP |
6800 | UITableViewDataSource, |
6801 | UITableViewDelegate | |
7585ce66 | 6802 | > { |
3931b718 | 6803 | _transient Database *database_; |
7b33d201 JF |
6804 | _H<NSMutableArray> sections_; |
6805 | _H<NSMutableArray> filtered_; | |
bf7c998c | 6806 | _H<UITableView, 2> list_; |
b5e7eebb GP |
6807 | } |
6808 | ||
5829aea2 | 6809 | - (id) initWithDatabase:(Database *)database; |
5829aea2 | 6810 | - (void) editButtonClicked; |
7585ce66 | 6811 | |
bc11cf5b | 6812 | @end |
b5e7eebb | 6813 | |
5829aea2 | 6814 | @implementation SectionsController |
b5e7eebb | 6815 | |
fe8e721f GP |
6816 | - (NSURL *) navigationURL { |
6817 | return [NSURL URLWithString:@"cydia://sections"]; | |
6818 | } | |
6819 | ||
a784d0a4 | 6820 | - (void) updateNavigationItem { |
8e5b801a | 6821 | [[self navigationItem] setTitle:[self isEditing] ? UCLocalize("SECTION_VISIBILITY") : UCLocalize("SECTIONS")]; |
a784d0a4 GP |
6822 | if ([sections_ count] == 0) { |
6823 | [[self navigationItem] setRightBarButtonItem:nil]; | |
6824 | } else { | |
6825 | [[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] | |
8e5b801a | 6826 | initWithBarButtonSystemItem:([self isEditing] ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit) |
a784d0a4 GP |
6827 | target:self |
6828 | action:@selector(editButtonClicked) | |
6829 | ] animated:([[self navigationItem] rightBarButtonItem] != nil)]; | |
6830 | } | |
6831 | } | |
6832 | ||
8e5b801a GP |
6833 | - (void) setEditing:(BOOL)editing animated:(BOOL)animated { |
6834 | [super setEditing:editing animated:animated]; | |
35f0a3b5 | 6835 | |
8e5b801a | 6836 | if (editing) |
5829aea2 GP |
6837 | [list_ reloadData]; |
6838 | else | |
6839 | [delegate_ updateData]; | |
7585ce66 | 6840 | |
a784d0a4 | 6841 | [self updateNavigationItem]; |
b5e7eebb GP |
6842 | } |
6843 | ||
5829aea2 GP |
6844 | - (void) viewDidAppear:(BOOL)animated { |
6845 | [super viewDidAppear:animated]; | |
6846 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7585ce66 JF |
6847 | } |
6848 | ||
5829aea2 GP |
6849 | - (void) viewWillDisappear:(BOOL)animated { |
6850 | [super viewWillDisappear:animated]; | |
8e5b801a | 6851 | if ([self isEditing]) [self setEditing:NO]; |
7585ce66 JF |
6852 | } |
6853 | ||
5829aea2 | 6854 | - (Section *) sectionAtIndexPath:(NSIndexPath *)indexPath { |
b3551da8 | 6855 | Section *section = nil; |
8e5b801a | 6856 | int index = [indexPath row]; |
b3551da8 GP |
6857 | if (![self isEditing]) { |
6858 | index -= 1; | |
6859 | if (index >= 0) | |
6860 | section = [filtered_ objectAtIndex:index]; | |
6861 | } else { | |
6862 | section = [sections_ objectAtIndex:index]; | |
6863 | } | |
5829aea2 GP |
6864 | return section; |
6865 | } | |
7585ce66 | 6866 | |
5829aea2 | 6867 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
8e5b801a GP |
6868 | if ([self isEditing]) |
6869 | return [sections_ count]; | |
6870 | else | |
6871 | return [filtered_ count] + 1; | |
7585ce66 JF |
6872 | } |
6873 | ||
5829aea2 GP |
6874 | /*- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
6875 | return 45.0f; | |
6876 | }*/ | |
7585ce66 | 6877 | |
5829aea2 GP |
6878 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
6879 | static NSString *reuseIdentifier = @"SectionCell"; | |
7585ce66 | 6880 | |
35f0a3b5 | 6881 | SectionCell *cell = (SectionCell *)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; |
5829aea2 GP |
6882 | if (cell == nil) |
6883 | cell = [[[SectionCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease]; | |
7585ce66 | 6884 | |
8e5b801a | 6885 | [cell setSection:[self sectionAtIndexPath:indexPath] editing:[self isEditing]]; |
54043703 | 6886 | |
5829aea2 | 6887 | return cell; |
54043703 JF |
6888 | } |
6889 | ||
5829aea2 | 6890 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
8e5b801a | 6891 | if ([self isEditing]) |
54043703 | 6892 | return; |
7585ce66 | 6893 | |
5829aea2 | 6894 | Section *section = [self sectionAtIndexPath:indexPath]; |
7585ce66 | 6895 | |
5829aea2 GP |
6896 | SectionController *controller = [[[SectionController alloc] |
6897 | initWithDatabase:database_ | |
6898 | section:[section name] | |
6899 | ] autorelease]; | |
6900 | [controller setDelegate:delegate_]; | |
7585ce66 | 6901 | |
5829aea2 | 6902 | [[self navigationController] pushViewController:controller animated:YES]; |
7585ce66 JF |
6903 | } |
6904 | ||
fe8e721f GP |
6905 | - (void) loadView { |
6906 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
6907 | ||
7b33d201 | 6908 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds]] autorelease]; |
fe8e721f GP |
6909 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
6910 | [list_ setRowHeight:45.0f]; | |
7b33d201 | 6911 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f GP |
6912 | [list_ setDelegate:self]; |
6913 | [[self view] addSubview:list_]; | |
6914 | } | |
6915 | ||
6916 | - (void) viewDidLoad { | |
7d887d0b JF |
6917 | [super viewDidLoad]; |
6918 | ||
fe8e721f GP |
6919 | [[self navigationItem] setTitle:UCLocalize("SECTIONS")]; |
6920 | } | |
6921 | ||
6922 | - (void) releaseSubviews { | |
fe8e721f GP |
6923 | list_ = nil; |
6924 | } | |
6925 | ||
5829aea2 GP |
6926 | - (id) initWithDatabase:(Database *)database { |
6927 | if ((self = [super init]) != nil) { | |
6928 | database_ = database; | |
7585ce66 | 6929 | |
7b33d201 JF |
6930 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
6931 | filtered_ = [NSMutableArray arrayWithCapacity:16]; | |
5829aea2 | 6932 | } return self; |
7585ce66 JF |
6933 | } |
6934 | ||
5829aea2 | 6935 | - (void) reloadData { |
fe8e721f GP |
6936 | [super reloadData]; |
6937 | ||
5829aea2 | 6938 | NSArray *packages = [database_ packages]; |
7585ce66 | 6939 | |
5829aea2 GP |
6940 | [sections_ removeAllObjects]; |
6941 | [filtered_ removeAllObjects]; | |
7585ce66 | 6942 | |
5829aea2 | 6943 | NSMutableDictionary *sections([NSMutableDictionary dictionaryWithCapacity:32]); |
7585ce66 | 6944 | |
5829aea2 GP |
6945 | _trace(); |
6946 | for (Package *package in packages) { | |
6947 | NSString *name([package section]); | |
6948 | NSString *key(name == nil ? @"" : name); | |
7585ce66 | 6949 | |
5829aea2 | 6950 | Section *section; |
7585ce66 | 6951 | |
5829aea2 GP |
6952 | _profile(SectionsView$reloadData$Section) |
6953 | section = [sections objectForKey:key]; | |
6954 | if (section == nil) { | |
6955 | _profile(SectionsView$reloadData$Section$Allocate) | |
729bc910 | 6956 | section = [[[Section alloc] initWithName:key localize:YES] autorelease]; |
5829aea2 GP |
6957 | [sections setObject:section forKey:key]; |
6958 | _end | |
6959 | } | |
6960 | _end | |
7585ce66 | 6961 | |
5829aea2 | 6962 | [section addToCount]; |
7585ce66 | 6963 | |
5829aea2 GP |
6964 | _profile(SectionsView$reloadData$Filter) |
6965 | if (![package valid] || ![package visible]) | |
6966 | continue; | |
6967 | _end | |
7585ce66 | 6968 | |
5829aea2 GP |
6969 | [section addToRow]; |
6970 | } | |
6971 | _trace(); | |
7585ce66 | 6972 | |
5829aea2 | 6973 | [sections_ addObjectsFromArray:[sections allValues]]; |
7585ce66 | 6974 | |
5829aea2 | 6975 | [sections_ sortUsingSelector:@selector(compareByLocalized:)]; |
7585ce66 | 6976 | |
7b33d201 | 6977 | for (Section *section in (id) sections_) { |
5829aea2 GP |
6978 | size_t count([section row]); |
6979 | if (count == 0) | |
6980 | continue; | |
7585ce66 | 6981 | |
5829aea2 GP |
6982 | section = [[[Section alloc] initWithName:[section name] localized:[section localized]] autorelease]; |
6983 | [section setCount:count]; | |
6984 | [filtered_ addObject:section]; | |
6985 | } | |
7585ce66 | 6986 | |
a784d0a4 | 6987 | [self updateNavigationItem]; |
5829aea2 GP |
6988 | [list_ reloadData]; |
6989 | _trace(); | |
6990 | } | |
7585ce66 | 6991 | |
6840bff3 | 6992 | - (void) editButtonClicked { |
8e5b801a | 6993 | [self setEditing:![self isEditing] animated:YES]; |
5829aea2 | 6994 | } |
7585ce66 | 6995 | |
5829aea2 GP |
6996 | @end |
6997 | /* }}} */ | |
7585ce66 | 6998 | |
5829aea2 | 6999 | /* Changes Controller {{{ */ |
cd79e8cf | 7000 | @interface ChangesController : CyteViewController < |
5829aea2 GP |
7001 | UITableViewDataSource, |
7002 | UITableViewDelegate | |
7003 | > { | |
7004 | _transient Database *database_; | |
7005 | unsigned era_; | |
56bf1e78 | 7006 | _H<NSArray> packages_; |
7b33d201 | 7007 | _H<NSMutableArray> sections_; |
bf7c998c | 7008 | _H<UITableView, 2> list_; |
5829aea2 | 7009 | unsigned upgrades_; |
5829aea2 | 7010 | } |
7585ce66 | 7011 | |
ea3bb538 | 7012 | - (id) initWithDatabase:(Database *)database; |
7585ce66 | 7013 | |
5829aea2 | 7014 | @end |
7585ce66 | 7015 | |
5829aea2 | 7016 | @implementation ChangesController |
7585ce66 | 7017 | |
fe8e721f GP |
7018 | - (NSURL *) navigationURL { |
7019 | return [NSURL URLWithString:@"cydia://changes"]; | |
7020 | } | |
7021 | ||
5829aea2 GP |
7022 | - (void) viewDidAppear:(BOOL)animated { |
7023 | [super viewDidAppear:animated]; | |
a70cd4ba | 7024 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; |
7585ce66 JF |
7025 | } |
7026 | ||
5829aea2 GP |
7027 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)list { |
7028 | NSInteger count([sections_ count]); | |
7029 | return count == 0 ? 1 : count; | |
7585ce66 JF |
7030 | } |
7031 | ||
5829aea2 GP |
7032 | - (NSString *) tableView:(UITableView *)list titleForHeaderInSection:(NSInteger)section { |
7033 | if ([sections_ count] == 0) | |
7034 | return nil; | |
7035 | return [[sections_ objectAtIndex:section] name]; | |
b5e7eebb GP |
7036 | } |
7037 | ||
5829aea2 GP |
7038 | - (NSInteger) tableView:(UITableView *)list numberOfRowsInSection:(NSInteger)section { |
7039 | if ([sections_ count] == 0) | |
7040 | return 0; | |
7041 | return [[sections_ objectAtIndex:section] count]; | |
7042 | } | |
b5e7eebb | 7043 | |
5829aea2 GP |
7044 | - (Package *) packageAtIndexPath:(NSIndexPath *)path { |
7045 | @synchronized (database_) { | |
7046 | if ([database_ era] != era_) | |
7047 | return nil; | |
b5e7eebb | 7048 | |
5829aea2 GP |
7049 | NSUInteger sectionIndex([path section]); |
7050 | if (sectionIndex >= [sections_ count]) | |
7051 | return nil; | |
7052 | Section *section([sections_ objectAtIndex:sectionIndex]); | |
7053 | NSInteger row([path row]); | |
56bf1e78 | 7054 | return [[[packages_ objectAtIndex:([section row] + row)] retain] autorelease]; |
5829aea2 | 7055 | } } |
b5e7eebb | 7056 | |
5829aea2 GP |
7057 | - (UITableViewCell *) tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path { |
7058 | PackageCell *cell((PackageCell *) [table dequeueReusableCellWithIdentifier:@"Package"]); | |
7059 | if (cell == nil) | |
7060 | cell = [[[PackageCell alloc] init] autorelease]; | |
59f3d290 | 7061 | [cell setPackage:[self packageAtIndexPath:path] asSummary:false]; |
5829aea2 | 7062 | return cell; |
9cb0bff2 GP |
7063 | } |
7064 | ||
5829aea2 GP |
7065 | - (NSIndexPath *) tableView:(UITableView *)table willSelectRowAtIndexPath:(NSIndexPath *)path { |
7066 | Package *package([self packageAtIndexPath:path]); | |
57e8b225 | 7067 | CYPackageController *view([[[CYPackageController alloc] initWithDatabase:database_ forPackage:[package id]] autorelease]); |
5829aea2 | 7068 | [view setDelegate:delegate_]; |
5829aea2 GP |
7069 | [[self navigationController] pushViewController:view animated:YES]; |
7070 | return path; | |
b5e7eebb GP |
7071 | } |
7072 | ||
5829aea2 GP |
7073 | - (void) refreshButtonClicked { |
7074 | [delegate_ beginUpdate]; | |
7075 | [[self navigationItem] setLeftBarButtonItem:nil animated:YES]; | |
b5e7eebb GP |
7076 | } |
7077 | ||
5829aea2 GP |
7078 | - (void) upgradeButtonClicked { |
7079 | [delegate_ distUpgrade]; | |
b5e7eebb GP |
7080 | } |
7081 | ||
fe8e721f GP |
7082 | - (void) loadView { |
7083 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
7084 | ||
7b33d201 | 7085 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; |
fe8e721f GP |
7086 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7087 | [list_ setRowHeight:73]; | |
7b33d201 | 7088 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f GP |
7089 | [list_ setDelegate:self]; |
7090 | [[self view] addSubview:list_]; | |
7091 | } | |
7092 | ||
7093 | - (void) viewDidLoad { | |
7d887d0b JF |
7094 | [super viewDidLoad]; |
7095 | ||
fe8e721f GP |
7096 | [[self navigationItem] setTitle:UCLocalize("CHANGES")]; |
7097 | } | |
7098 | ||
7099 | - (void) releaseSubviews { | |
fe8e721f GP |
7100 | list_ = nil; |
7101 | } | |
5829aea2 | 7102 | |
ea3bb538 | 7103 | - (id) initWithDatabase:(Database *)database { |
b5e7eebb GP |
7104 | if ((self = [super init]) != nil) { |
7105 | database_ = database; | |
b5e7eebb | 7106 | |
56bf1e78 | 7107 | packages_ = [NSArray array]; |
7b33d201 | 7108 | sections_ = [NSMutableArray arrayWithCapacity:16]; |
5829aea2 | 7109 | } return self; |
807ae6d7 JF |
7110 | } |
7111 | ||
9c5737d5 | 7112 | - (NSMutableArray *) _reloadPackages { |
c28bc6f1 JF |
7113 | @synchronized (database_) { |
7114 | era_ = [database_ era]; | |
7115 | NSArray *packages([database_ packages]); | |
7116 | ||
56bf1e78 | 7117 | NSMutableArray *filtered([NSMutableArray arrayWithCapacity:[packages count]]); |
9fe5e5f8 | 7118 | |
5829aea2 GP |
7119 | _trace(); |
7120 | _profile(ChangesController$_reloadPackages$Filter) | |
7121 | for (Package *package in packages) | |
7122 | if ([package upgradableAndEssential:YES] || [package visible]) | |
56bf1e78 | 7123 | CFArrayAppendValue((CFMutableArrayRef) filtered, package); |
5829aea2 GP |
7124 | _end |
7125 | _trace(); | |
7126 | _profile(ChangesController$_reloadPackages$radixSort) | |
56bf1e78 | 7127 | [filtered radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(&PackageChangesRadix) withContext:NULL]; |
5829aea2 GP |
7128 | _end |
7129 | _trace(); | |
56bf1e78 JF |
7130 | |
7131 | return filtered; | |
c28bc6f1 | 7132 | } } |
9fe5e5f8 | 7133 | |
a70cd4ba | 7134 | - (void) _reloadData { |
ae60e2c1 JF |
7135 | NSArray *packages; |
7136 | ||
c28bc6f1 JF |
7137 | reload: |
7138 | if (true) { | |
7139 | UIProgressHUD *hud([delegate_ addProgressHUD]); | |
7140 | [hud setText:UCLocalize("LOADING")]; | |
7141 | //NSLog(@"HUD:%@::%@", delegate_, hud); | |
ae60e2c1 | 7142 | packages = [self yieldToSelector:@selector(_reloadPackages)]; |
c28bc6f1 JF |
7143 | [delegate_ removeProgressHUD:hud]; |
7144 | } else { | |
ae60e2c1 | 7145 | packages = [self _reloadPackages]; |
c28bc6f1 | 7146 | } |
807ae6d7 | 7147 | |
c28bc6f1 JF |
7148 | @synchronized (database_) { |
7149 | if (era_ != [database_ era]) | |
7150 | goto reload; | |
807ae6d7 | 7151 | |
ae60e2c1 | 7152 | packages_ = packages; |
bdca103d JF |
7153 | [sections_ removeAllObjects]; |
7154 | ||
5829aea2 GP |
7155 | Section *upgradable = [[[Section alloc] initWithName:UCLocalize("AVAILABLE_UPGRADES") localize:NO] autorelease]; |
7156 | Section *ignored = nil; | |
7157 | Section *section = nil; | |
7158 | time_t last = 0; | |
807ae6d7 | 7159 | |
5829aea2 GP |
7160 | upgrades_ = 0; |
7161 | bool unseens = false; | |
807ae6d7 | 7162 | |
5829aea2 | 7163 | CFDateFormatterRef formatter(CFDateFormatterCreate(NULL, Locale_, kCFDateFormatterMediumStyle, kCFDateFormatterMediumStyle)); |
807ae6d7 | 7164 | |
56bf1e78 JF |
7165 | for (size_t offset = 0, count = [packages_ count]; offset != count; ++offset) { |
7166 | Package *package = [packages_ objectAtIndex:offset]; | |
807ae6d7 | 7167 | |
5829aea2 | 7168 | BOOL uae = [package upgradableAndEssential:YES]; |
807ae6d7 | 7169 | |
5829aea2 GP |
7170 | if (!uae) { |
7171 | unseens = true; | |
7172 | time_t seen([package seen]); | |
d90a4cd6 | 7173 | |
5829aea2 GP |
7174 | if (section == nil || last != seen) { |
7175 | last = seen; | |
7176 | ||
7177 | NSString *name; | |
7178 | name = (NSString *) CFDateFormatterCreateStringWithDate(NULL, formatter, (CFDateRef) [NSDate dateWithTimeIntervalSince1970:seen]); | |
7179 | [name autorelease]; | |
7180 | ||
7181 | _profile(ChangesController$reloadData$Allocate) | |
7182 | name = [NSString stringWithFormat:UCLocalize("NEW_AT"), name]; | |
7183 | section = [[[Section alloc] initWithName:name row:offset localize:NO] autorelease]; | |
7184 | [sections_ addObject:section]; | |
7185 | _end | |
7186 | } | |
7187 | ||
7188 | [section addToCount]; | |
7189 | } else if ([package ignored]) { | |
7190 | if (ignored == nil) { | |
7191 | ignored = [[[Section alloc] initWithName:UCLocalize("IGNORED_UPGRADES") row:offset localize:NO] autorelease]; | |
7192 | } | |
7193 | [ignored addToCount]; | |
7194 | } else { | |
7195 | ++upgrades_; | |
7196 | [upgradable addToCount]; | |
7197 | } | |
7198 | } | |
7199 | _trace(); | |
7200 | ||
7201 | CFRelease(formatter); | |
7202 | ||
7203 | if (unseens) { | |
7204 | Section *last = [sections_ lastObject]; | |
7205 | size_t count = [last count]; | |
56bf1e78 | 7206 | [packages_ removeObjectsInRange:NSMakeRange([packages_ count] - count, count)]; |
5829aea2 GP |
7207 | [sections_ removeLastObject]; |
7208 | } | |
7209 | ||
7210 | if ([ignored count] != 0) | |
7211 | [sections_ insertObject:ignored atIndex:0]; | |
7212 | if (upgrades_ != 0) | |
7213 | [sections_ insertObject:upgradable atIndex:0]; | |
7214 | ||
7215 | [list_ reloadData]; | |
7216 | ||
7217 | if (upgrades_ > 0) | |
7218 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
7219 | initWithTitle:[NSString stringWithFormat:UCLocalize("PARENTHETICAL"), UCLocalize("UPGRADE"), [NSString stringWithFormat:@"%u", upgrades_]] | |
7220 | style:UIBarButtonItemStylePlain | |
7221 | target:self | |
7222 | action:@selector(upgradeButtonClicked) | |
7223 | ] autorelease]]; | |
7224 | ||
7225 | if (![delegate_ updating]) | |
7226 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7227 | initWithTitle:UCLocalize("REFRESH") | |
7228 | style:UIBarButtonItemStylePlain | |
7229 | target:self | |
7230 | action:@selector(refreshButtonClicked) | |
7231 | ] autorelease]]; | |
7232 | ||
7233 | PrintTimes(); | |
7234 | } } | |
7235 | ||
a70cd4ba JF |
7236 | - (void) reloadData { |
7237 | [super reloadData]; | |
7238 | [self performSelector:@selector(_reloadData) withObject:nil afterDelay:0]; | |
7239 | } | |
7240 | ||
5829aea2 GP |
7241 | @end |
7242 | /* }}} */ | |
7243 | /* Search Controller {{{ */ | |
f50860ee | 7244 | @interface SearchController : FilteredPackageListController < |
5829aea2 GP |
7245 | UISearchBarDelegate |
7246 | > { | |
bf7c998c | 7247 | _H<UISearchBar, 1> search_; |
fe8e721f | 7248 | BOOL searchloaded_; |
5829aea2 GP |
7249 | } |
7250 | ||
43625891 | 7251 | - (id) initWithDatabase:(Database *)database query:(NSString *)query; |
5829aea2 | 7252 | - (void) reloadData; |
d90a4cd6 GP |
7253 | |
7254 | @end | |
7255 | ||
5829aea2 | 7256 | @implementation SearchController |
d90a4cd6 | 7257 | |
fe8e721f | 7258 | - (NSURL *) navigationURL { |
35f0a3b5 GP |
7259 | if ([search_ text] == nil || [[search_ text] isEqualToString:@""]) |
7260 | return [NSURL URLWithString:@"cydia://search"]; | |
7261 | else | |
7262 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://search/%@", [search_ text]]]; | |
fe8e721f GP |
7263 | } |
7264 | ||
59f3d290 | 7265 | - (void) useSearch { |
d84597fe | 7266 | [self setObject:[[search_ text] componentsSeparatedByString:@" "] forFilter:@selector(isUnfilteredAndSearchedForBy:)]; |
59f3d290 JF |
7267 | [self clearData]; |
7268 | [self reloadData]; | |
7269 | } | |
7270 | ||
7271 | - (void) viewWillAppear:(BOOL)animated { | |
7272 | [super viewWillAppear:animated]; | |
7273 | ||
7274 | if ([self filter] == @selector(isUnfilteredAndSelectedForBy:)) | |
7275 | [self useSearch]; | |
7276 | } | |
7277 | ||
3025d5b4 JF |
7278 | - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar { |
7279 | [self setObject:[search_ text] forFilter:@selector(isUnfilteredAndSelectedForBy:)]; | |
59f3d290 JF |
7280 | [self clearData]; |
7281 | [self reloadData]; | |
3025d5b4 JF |
7282 | } |
7283 | ||
7284 | - (void) searchBarButtonClicked:(UISearchBar *)searchBar { | |
5829aea2 | 7285 | [search_ resignFirstResponder]; |
59f3d290 | 7286 | [self useSearch]; |
5829aea2 GP |
7287 | } |
7288 | ||
3025d5b4 JF |
7289 | - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar { |
7290 | [search_ setText:@""]; | |
7291 | [self searchBarButtonClicked:searchBar]; | |
7292 | } | |
7293 | ||
7294 | - (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar { | |
7295 | [self searchBarButtonClicked:searchBar]; | |
7296 | } | |
7297 | ||
5829aea2 | 7298 | - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)text { |
f50860ee | 7299 | [self setObject:text forFilter:@selector(isUnfilteredAndSelectedForBy:)]; |
5829aea2 GP |
7300 | [self reloadData]; |
7301 | } | |
7302 | ||
3025d5b4 | 7303 | - (bool) shouldYield { |
56bf1e78 JF |
7304 | return YES; |
7305 | } | |
7306 | ||
7307 | - (bool) shouldBlock { | |
3025d5b4 JF |
7308 | return [self filter] == @selector(isUnfilteredAndSearchedForBy:); |
7309 | } | |
7310 | ||
59f3d290 JF |
7311 | - (bool) isSummarized { |
7312 | return [self filter] == @selector(isUnfilteredAndSelectedForBy:); | |
7313 | } | |
7314 | ||
9c5737d5 JF |
7315 | - (bool) showsSections { |
7316 | return false; | |
7317 | } | |
7318 | ||
7319 | - (NSMutableArray *) _reloadPackages { | |
7320 | NSMutableArray *packages([super _reloadPackages]); | |
7321 | if ([self filter] == @selector(isUnfilteredAndSearchedForBy:)) | |
7322 | [packages radixSortUsingSelector:@selector(rank)]; | |
7323 | return packages; | |
7324 | } | |
7325 | ||
43625891 | 7326 | - (id) initWithDatabase:(Database *)database query:(NSString *)query { |
d84597fe | 7327 | if ((self = [super initWithDatabase:database title:UCLocalize("SEARCH") filter:@selector(isUnfilteredAndSearchedForBy:) with:[query componentsSeparatedByString:@" "]])) { |
900095fd JF |
7328 | search_ = [[[UISearchBar alloc] init] autorelease]; |
7329 | [search_ setDelegate:self]; | |
43625891 JF |
7330 | |
7331 | if (query != nil) | |
7332 | [search_ setText:query]; | |
fe8e721f | 7333 | } return self; |
5829aea2 GP |
7334 | } |
7335 | ||
6840bff3 | 7336 | - (void) viewDidAppear:(BOOL)animated { |
5829aea2 | 7337 | [super viewDidAppear:animated]; |
fe8e721f GP |
7338 | |
7339 | if (!searchloaded_) { | |
7340 | searchloaded_ = YES; | |
7341 | [search_ setFrame:CGRectMake(0, 0, [[self view] bounds].size.width, 44.0f)]; | |
5829aea2 GP |
7342 | [search_ layoutSubviews]; |
7343 | [search_ setPlaceholder:UCLocalize("SEARCH_EX")]; | |
7344 | ||
7345 | UITextField *textField; | |
7346 | if ([search_ respondsToSelector:@selector(searchField)]) | |
7347 | textField = [search_ searchField]; | |
7348 | else | |
7349 | textField = MSHookIvar<UITextField *>(search_, "_searchField"); | |
7350 | ||
7351 | [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
5829aea2 GP |
7352 | [textField setEnablesReturnKeyAutomatically:NO]; |
7353 | [[self navigationItem] setTitleView:textField]; | |
d90a4cd6 | 7354 | } |
5829aea2 | 7355 | } |
d90a4cd6 | 7356 | |
d90a4cd6 | 7357 | - (void) reloadData { |
d84597fe JF |
7358 | id object([search_ text]); |
7359 | if ([self filter] == @selector(isUnfilteredAndSearchedForBy:)) | |
7360 | object = [object componentsSeparatedByString:@" "]; | |
7361 | ||
7362 | [self setObject:object]; | |
f50860ee | 7363 | [self resetCursor]; |
9dac415b JF |
7364 | |
7365 | [super reloadData]; | |
d90a4cd6 GP |
7366 | } |
7367 | ||
5829aea2 GP |
7368 | - (void) didSelectPackage:(Package *)package { |
7369 | [search_ resignFirstResponder]; | |
7370 | [super didSelectPackage:package]; | |
d90a4cd6 GP |
7371 | } |
7372 | ||
7373 | @end | |
7374 | /* }}} */ | |
5829aea2 | 7375 | /* Package Settings Controller {{{ */ |
cd79e8cf | 7376 | @interface PackageSettingsController : CyteViewController < |
c21004b9 JF |
7377 | UITableViewDataSource, |
7378 | UITableViewDelegate | |
7379 | > { | |
807ae6d7 | 7380 | _transient Database *database_; |
7b33d201 JF |
7381 | _H<NSString> name_; |
7382 | _H<Package> package_; | |
bf7c998c | 7383 | _H<UITableView, 2> table_; |
7b33d201 JF |
7384 | _H<UISwitch> subscribedSwitch_; |
7385 | _H<UISwitch> ignoredSwitch_; | |
7386 | _H<UITableViewCell> subscribedCell_; | |
7387 | _H<UITableViewCell> ignoredCell_; | |
807ae6d7 JF |
7388 | } |
7389 | ||
5829aea2 | 7390 | - (id) initWithDatabase:(Database *)database package:(NSString *)package; |
c21004b9 | 7391 | |
807ae6d7 JF |
7392 | @end |
7393 | ||
5829aea2 | 7394 | @implementation PackageSettingsController |
807ae6d7 | 7395 | |
fe8e721f GP |
7396 | - (NSURL *) navigationURL { |
7397 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://package/%@/settings", [package_ id]]]; | |
7398 | } | |
7399 | ||
5829aea2 GP |
7400 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7401 | if (package_ == nil) | |
7402 | return 0; | |
e954c30a | 7403 | |
2136717a DH |
7404 | if ([package_ installed] == nil) |
7405 | return 1; | |
7406 | else | |
7407 | return 2; | |
e954c30a GP |
7408 | } |
7409 | ||
5829aea2 GP |
7410 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7411 | if (package_ == nil) | |
7412 | return 0; | |
7413 | ||
2136717a DH |
7414 | // both sections contain just one item right now. |
7415 | return 1; | |
b5e7eebb GP |
7416 | } |
7417 | ||
5829aea2 | 7418 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
2136717a | 7419 | return nil; |
e954c30a GP |
7420 | } |
7421 | ||
5829aea2 | 7422 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { |
2136717a DH |
7423 | if (section == 0) |
7424 | return UCLocalize("SHOW_ALL_CHANGES_EX"); | |
7425 | else | |
7426 | return UCLocalize("IGNORE_UPGRADES_EX"); | |
807ae6d7 JF |
7427 | } |
7428 | ||
5829aea2 GP |
7429 | - (void) onSubscribed:(id)control { |
7430 | bool value([control isOn]); | |
7431 | if (package_ == nil) | |
7432 | return; | |
7433 | if ([package_ setSubscribed:value]) | |
7434 | [delegate_ updateData]; | |
807ae6d7 JF |
7435 | } |
7436 | ||
e5e70358 JF |
7437 | - (void) _updateIgnored { |
7438 | const char *package([name_ UTF8String]); | |
7439 | bool on([ignoredSwitch_ isOn]); | |
7440 | ||
7441 | pid_t pid(ExecFork()); | |
7442 | if (pid == 0) { | |
7443 | FILE *dpkg(popen("dpkg --set-selections", "w")); | |
7444 | fwrite(package, strlen(package), 1, dpkg); | |
7445 | ||
7446 | if (on) | |
7447 | fwrite(" hold\n", 6, 1, dpkg); | |
7448 | else | |
7449 | fwrite(" install\n", 9, 1, dpkg); | |
7450 | ||
7451 | pclose(dpkg); | |
7452 | ||
7453 | exit(0); | |
7454 | _assert(false); | |
7455 | } | |
7456 | ||
7457 | _forever { | |
7458 | int status; | |
7459 | int result(waitpid(pid, &status, 0)); | |
7460 | ||
7461 | if (result != -1) { | |
7462 | _assert(result == pid); | |
7463 | break; | |
7464 | } | |
7465 | } | |
7466 | } | |
7467 | ||
5829aea2 | 7468 | - (void) onIgnored:(id)control { |
e5e70358 JF |
7469 | NSInvocation *invocation([NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(_updateIgnored)]]); |
7470 | [invocation setTarget:self]; | |
7471 | [invocation setSelector:@selector(_updateIgnored)]; | |
7472 | ||
7473 | [delegate_ reloadDataWithInvocation:invocation]; | |
5829aea2 | 7474 | } |
807ae6d7 | 7475 | |
46aa9775 | 7476 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
7477 | if (package_ == nil) |
7478 | return nil; | |
b5e7eebb | 7479 | |
2136717a | 7480 | switch ([indexPath section]) { |
5829aea2 GP |
7481 | case 0: return subscribedCell_; |
7482 | case 1: return ignoredCell_; | |
36fbb2aa | 7483 | |
5829aea2 GP |
7484 | _nodefault |
7485 | } | |
807ae6d7 | 7486 | |
5829aea2 | 7487 | return nil; |
807ae6d7 JF |
7488 | } |
7489 | ||
fe8e721f GP |
7490 | - (void) loadView { |
7491 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
807ae6d7 | 7492 | |
7b33d201 | 7493 | table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease]; |
fe8e721f | 7494 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7b33d201 | 7495 | [(UITableView *) table_ setDataSource:self]; |
fe8e721f GP |
7496 | [table_ setDelegate:self]; |
7497 | [[self view] addSubview:table_]; | |
807ae6d7 | 7498 | |
7b33d201 | 7499 | subscribedSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7500 | [subscribedSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7501 | [subscribedSwitch_ addTarget:self action:@selector(onSubscribed:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7502 | |
7b33d201 | 7503 | ignoredSwitch_ = [[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 50, 20)] autorelease]; |
fe8e721f GP |
7504 | [ignoredSwitch_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
7505 | [ignoredSwitch_ addTarget:self action:@selector(onIgnored:) forEvents:UIControlEventValueChanged]; | |
807ae6d7 | 7506 | |
7b33d201 | 7507 | subscribedCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7508 | [subscribedCell_ setText:UCLocalize("SHOW_ALL_CHANGES")]; |
7509 | [subscribedCell_ setAccessoryView:subscribedSwitch_]; | |
7510 | [subscribedCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
5829aea2 | 7511 | |
7b33d201 | 7512 | ignoredCell_ = [[[UITableViewCell alloc] init] autorelease]; |
fe8e721f GP |
7513 | [ignoredCell_ setText:UCLocalize("IGNORE_UPGRADES")]; |
7514 | [ignoredCell_ setAccessoryView:ignoredSwitch_]; | |
7515 | [ignoredCell_ setSelectionStyle:UITableViewCellSelectionStyleNone]; | |
fe8e721f | 7516 | } |
5829aea2 | 7517 | |
fe8e721f | 7518 | - (void) viewDidLoad { |
7d887d0b JF |
7519 | [super viewDidLoad]; |
7520 | ||
fe8e721f GP |
7521 | [[self navigationItem] setTitle:UCLocalize("SETTINGS")]; |
7522 | } | |
5829aea2 | 7523 | |
fe8e721f | 7524 | - (void) releaseSubviews { |
fe8e721f | 7525 | ignoredCell_ = nil; |
fe8e721f | 7526 | subscribedCell_ = nil; |
fe8e721f | 7527 | table_ = nil; |
fe8e721f | 7528 | ignoredSwitch_ = nil; |
fe8e721f GP |
7529 | subscribedSwitch_ = nil; |
7530 | } | |
7531 | ||
7532 | - (id) initWithDatabase:(Database *)database package:(NSString *)package { | |
6840bff3 | 7533 | if ((self = [super init]) != nil) { |
fe8e721f | 7534 | database_ = database; |
7b33d201 | 7535 | name_ = package; |
807ae6d7 JF |
7536 | } return self; |
7537 | } | |
7538 | ||
7539 | - (void) reloadData { | |
fe8e721f GP |
7540 | [super reloadData]; |
7541 | ||
5829aea2 | 7542 | package_ = [database_ packageWithName:name_]; |
f3e2c0ac | 7543 | |
5829aea2 | 7544 | if (package_ != nil) { |
5829aea2 GP |
7545 | [subscribedSwitch_ setOn:([package_ subscribed] ? 1 : 0) animated:NO]; |
7546 | [ignoredSwitch_ setOn:([package_ ignored] ? 1 : 0) animated:NO]; | |
f3e2c0ac | 7547 | } // XXX: what now, G? |
dc5812ec | 7548 | |
5829aea2 GP |
7549 | [table_ reloadData]; |
7550 | } | |
6d9712c4 | 7551 | |
dc5812ec | 7552 | @end |
2367a917 | 7553 | /* }}} */ |
d90a4cd6 | 7554 | |
5829aea2 | 7555 | /* Installed Controller {{{ */ |
f50860ee | 7556 | @interface InstalledController : FilteredPackageListController { |
5829aea2 | 7557 | BOOL expert_; |
dc5812ec JF |
7558 | } |
7559 | ||
5829aea2 GP |
7560 | - (id) initWithDatabase:(Database *)database; |
7561 | ||
7562 | - (void) updateRoleButton; | |
7563 | - (void) queueStatusDidChange; | |
dc5812ec | 7564 | |
dc5812ec JF |
7565 | @end |
7566 | ||
5829aea2 | 7567 | @implementation InstalledController |
b4d89997 | 7568 | |
fe8e721f GP |
7569 | - (NSURL *) navigationURL { |
7570 | return [NSURL URLWithString:@"cydia://installed"]; | |
7571 | } | |
b5e7eebb | 7572 | |
5829aea2 GP |
7573 | - (id) initWithDatabase:(Database *)database { |
7574 | if ((self = [super initWithDatabase:database title:UCLocalize("INSTALLED") filter:@selector(isInstalledAndUnfiltered:) with:[NSNumber numberWithBool:YES]]) != nil) { | |
7575 | [self updateRoleButton]; | |
7576 | [self queueStatusDidChange]; | |
7577 | } return self; | |
dc5812ec JF |
7578 | } |
7579 | ||
5829aea2 GP |
7580 | #if !AlwaysReload |
7581 | - (void) queueButtonClicked { | |
7582 | [delegate_ queue]; | |
dc5812ec | 7583 | } |
5829aea2 | 7584 | #endif |
dc5812ec | 7585 | |
5829aea2 GP |
7586 | - (void) queueStatusDidChange { |
7587 | #if !AlwaysReload | |
7588 | if (IsWildcat_) { | |
7589 | if (Queuing_) { | |
7590 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
7591 | initWithTitle:UCLocalize("QUEUE") | |
7592 | style:UIBarButtonItemStyleDone | |
7593 | target:self | |
7594 | action:@selector(queueButtonClicked) | |
7595 | ] autorelease]]; | |
7596 | } else { | |
7597 | [[self navigationItem] setLeftBarButtonItem:nil]; | |
7598 | } | |
7599 | } | |
7600 | #endif | |
dc5812ec JF |
7601 | } |
7602 | ||
5829aea2 GP |
7603 | - (void) updateRoleButton { |
7604 | if (Role_ != nil && ![Role_ isEqualToString:@"Developer"]) | |
7605 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
7606 | initWithTitle:(expert_ ? UCLocalize("EXPERT") : UCLocalize("SIMPLE")) | |
7607 | style:(expert_ ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
7608 | target:self | |
7609 | action:@selector(roleButtonClicked) | |
7610 | ] autorelease]]; | |
dc5812ec JF |
7611 | } |
7612 | ||
5829aea2 | 7613 | - (void) roleButtonClicked { |
f50860ee GP |
7614 | [self setObject:[NSNumber numberWithBool:expert_]]; |
7615 | [self reloadData]; | |
5829aea2 | 7616 | expert_ = !expert_; |
dc5812ec | 7617 | |
5829aea2 | 7618 | [self updateRoleButton]; |
9a7b04c5 JF |
7619 | } |
7620 | ||
5829aea2 GP |
7621 | @end |
7622 | /* }}} */ | |
b5e7eebb | 7623 | |
5829aea2 | 7624 | /* Source Cell {{{ */ |
a9311516 | 7625 | @interface SourceCell : CyteTableViewCell < |
b97fcfc6 | 7626 | CyteTableViewCellDelegate |
5829aea2 | 7627 | > { |
7b33d201 JF |
7628 | _H<UIImage> icon_; |
7629 | _H<NSString> origin_; | |
7630 | _H<NSString> label_; | |
5829aea2 | 7631 | } |
dc5812ec | 7632 | |
5829aea2 | 7633 | - (void) setSource:(Source *)source; |
6da1297d | 7634 | |
5829aea2 | 7635 | @end |
dc5812ec | 7636 | |
5829aea2 | 7637 | @implementation SourceCell |
36bb2ca2 | 7638 | |
8252b666 JF |
7639 | - (void) _setImage:(UIImage *)image { |
7640 | icon_ = image; | |
7641 | [content_ setNeedsDisplay]; | |
7642 | } | |
7643 | ||
7644 | - (void) _setSource:(Source *)source { | |
7645 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
7646 | ||
7647 | if (NSString *base = [source base]) | |
7648 | if ([base length] != 0) { | |
7649 | NSURL *url([NSURL URLWithString:[base stringByAppendingString:@"CydiaIcon.png"]]); | |
7650 | ||
e2add7b8 JF |
7651 | if (NSData *data = [NSURLConnection |
7652 | sendSynchronousRequest:[NSURLRequest | |
7653 | requestWithURL:url | |
7654 | //cachePolicy:NSURLRequestUseProtocolCachePolicy | |
7655 | //timeoutInterval:5 | |
7656 | ] | |
7657 | ||
7658 | returningResponse:NULL | |
7659 | error:NULL | |
7660 | ]) | |
8252b666 JF |
7661 | if (UIImage *image = [UIImage imageWithData:data]) |
7662 | [self performSelectorOnMainThread:@selector(_setImage:) withObject:image waitUntilDone:NO]; | |
7663 | } | |
7664 | ||
7665 | [pool release]; | |
7666 | } | |
7667 | ||
5829aea2 | 7668 | - (void) setSource:(Source *)source { |
8252b666 | 7669 | icon_ = [UIImage applicationImageNamed:@"unknown.png"]; |
5829aea2 | 7670 | |
7b33d201 JF |
7671 | origin_ = [source name]; |
7672 | label_ = [source uri]; | |
5829aea2 GP |
7673 | |
7674 | [content_ setNeedsDisplay]; | |
8252b666 JF |
7675 | |
7676 | [NSThread detachNewThreadSelector:@selector(_setSource:) toTarget:self withObject:source]; | |
77801ff1 JF |
7677 | } |
7678 | ||
5829aea2 GP |
7679 | - (SourceCell *) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { |
7680 | if ((self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) != nil) { | |
7681 | UIView *content([self contentView]); | |
7682 | CGRect bounds([content bounds]); | |
77801ff1 | 7683 | |
b97fcfc6 | 7684 | content_ = [[[CyteTableViewCellContentView alloc] initWithFrame:bounds] autorelease]; |
5829aea2 GP |
7685 | [content_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
7686 | [content_ setBackgroundColor:[UIColor whiteColor]]; | |
7687 | [content addSubview:content_]; | |
dc5812ec | 7688 | |
5829aea2 GP |
7689 | [content_ setDelegate:self]; |
7690 | [content_ setOpaque:YES]; | |
7691 | } return self; | |
7692 | } | |
b4d89997 | 7693 | |
003fc610 GP |
7694 | - (NSString *) accessibilityLabel { |
7695 | return label_; | |
7696 | } | |
7697 | ||
5829aea2 GP |
7698 | - (void) drawContentRect:(CGRect)rect { |
7699 | bool highlighted(highlighted_); | |
7700 | float width(rect.size.width); | |
36bb2ca2 | 7701 | |
5829aea2 GP |
7702 | if (icon_ != nil) |
7703 | [icon_ drawInRect:CGRectMake(10, 10, 30, 30)]; | |
36bb2ca2 | 7704 | |
5829aea2 GP |
7705 | if (highlighted) |
7706 | UISetColor(White_); | |
dc5812ec | 7707 | |
5829aea2 GP |
7708 | if (!highlighted) |
7709 | UISetColor(Black_); | |
7710 | [origin_ drawAtPoint:CGPointMake(48, 8) forWidth:(width - 80) withFont:Font18Bold_ lineBreakMode:UILineBreakModeTailTruncation]; | |
f79a4512 | 7711 | |
5829aea2 GP |
7712 | if (!highlighted) |
7713 | UISetColor(Blue_); | |
7714 | [label_ drawAtPoint:CGPointMake(58, 29) forWidth:(width - 95) withFont:Font12_ lineBreakMode:UILineBreakModeTailTruncation]; | |
7715 | } | |
dc5812ec | 7716 | |
5829aea2 GP |
7717 | @end |
7718 | /* }}} */ | |
3f8edf70 | 7719 | /* Source Controller {{{ */ |
f50860ee | 7720 | @interface SourceController : FilteredPackageListController { |
2d5372fc | 7721 | _transient Source *source_; |
7b33d201 | 7722 | _H<NSString> key_; |
3f8edf70 GP |
7723 | } |
7724 | ||
7725 | - (id) initWithDatabase:(Database *)database source:(Source *)source; | |
7726 | ||
7727 | @end | |
7728 | ||
7729 | @implementation SourceController | |
7730 | ||
fe8e721f GP |
7731 | - (NSURL *) navigationURL { |
7732 | return [NSURL URLWithString:[NSString stringWithFormat:@"cydia://sources/%@", [source_ name]]]; | |
7733 | } | |
7734 | ||
3f8edf70 | 7735 | - (id) initWithDatabase:(Database *)database source:(Source *)source { |
7c642bd6 JF |
7736 | if ((self = [super initWithDatabase:database title:[source label] filter:@selector(isVisibleInSource:) with:source]) != nil) { |
7737 | source_ = source; | |
7b33d201 | 7738 | key_ = [source key]; |
3f8edf70 GP |
7739 | } return self; |
7740 | } | |
7741 | ||
2d5372fc | 7742 | - (void) reloadData { |
d669236d | 7743 | source_ = [database_ sourceWithKey:key_]; |
7b33d201 | 7744 | key_ = [source_ key]; |
2d5372fc | 7745 | [self setObject:source_]; |
9dac415b | 7746 | |
b6494e70 | 7747 | [[self navigationItem] setTitle:[source_ label]]; |
2d5372fc GP |
7748 | |
7749 | [super reloadData]; | |
7750 | } | |
7751 | ||
3f8edf70 GP |
7752 | @end |
7753 | /* }}} */ | |
7754 | /* Sources Controller {{{ */ | |
cd79e8cf | 7755 | @interface SourcesController : CyteViewController < |
5829aea2 GP |
7756 | UITableViewDataSource, |
7757 | UITableViewDelegate | |
7758 | > { | |
7759 | _transient Database *database_; | |
bf7c998c | 7760 | _H<UITableView, 2> list_; |
7b33d201 | 7761 | _H<NSMutableArray> sources_; |
5829aea2 | 7762 | int offset_; |
723a0072 | 7763 | |
7b33d201 JF |
7764 | _H<NSString> href_; |
7765 | _H<UIProgressHUD> hud_; | |
7766 | _H<NSError> error_; | |
dc63e78f | 7767 | |
5829aea2 GP |
7768 | //NSURLConnection *installer_; |
7769 | NSURLConnection *trivial_; | |
7770 | NSURLConnection *trivial_bz2_; | |
7771 | NSURLConnection *trivial_gz_; | |
7772 | //NSURLConnection *automatic_; | |
b4d89997 | 7773 | |
5829aea2 GP |
7774 | BOOL cydia_; |
7775 | } | |
dc5812ec | 7776 | |
5829aea2 | 7777 | - (id) initWithDatabase:(Database *)database; |
5829aea2 GP |
7778 | - (void) updateButtonsForEditingStatus:(BOOL)editing animated:(BOOL)animated; |
7779 | ||
7780 | @end | |
7781 | ||
7782 | @implementation SourcesController | |
7783 | ||
7784 | - (void) _releaseConnection:(NSURLConnection *)connection { | |
7785 | if (connection != nil) { | |
7786 | [connection cancel]; | |
7787 | //[connection setDelegate:nil]; | |
7788 | [connection release]; | |
36bb2ca2 | 7789 | } |
5829aea2 | 7790 | } |
dc5812ec | 7791 | |
5829aea2 | 7792 | - (void) dealloc { |
5829aea2 GP |
7793 | //[self _releaseConnection:installer_]; |
7794 | [self _releaseConnection:trivial_]; | |
7795 | [self _releaseConnection:trivial_gz_]; | |
7796 | [self _releaseConnection:trivial_bz2_]; | |
7797 | //[self _releaseConnection:automatic_]; | |
3178d79b | 7798 | |
5829aea2 GP |
7799 | [super dealloc]; |
7800 | } | |
b5e7eebb | 7801 | |
fe8e721f GP |
7802 | - (NSURL *) navigationURL { |
7803 | return [NSURL URLWithString:@"cydia://sources"]; | |
7804 | } | |
7805 | ||
5829aea2 GP |
7806 | - (void) viewDidAppear:(BOOL)animated { |
7807 | [super viewDidAppear:animated]; | |
7808 | [list_ deselectRowAtIndexPath:[list_ indexPathForSelectedRow] animated:animated]; | |
7809 | } | |
9bb3b295 | 7810 | |
5829aea2 GP |
7811 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
7812 | return offset_ == 0 ? 1 : 2; | |
7813 | } | |
8fe19fc1 | 7814 | |
5829aea2 GP |
7815 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
7816 | switch (section + (offset_ == 0 ? 1 : 0)) { | |
7817 | case 0: return UCLocalize("ENTERED_BY_USER"); | |
7818 | case 1: return UCLocalize("INSTALLED_BY_PACKAGE"); | |
7819 | ||
7820 | _nodefault | |
7821 | } | |
36bb2ca2 | 7822 | } |
b4d89997 | 7823 | |
5829aea2 GP |
7824 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
7825 | int count = [sources_ count]; | |
7826 | switch (section) { | |
7827 | case 0: return (offset_ == 0 ? count : offset_); | |
7828 | case 1: return count - offset_; | |
b4d89997 | 7829 | |
5829aea2 GP |
7830 | _nodefault |
7831 | } | |
7832 | } | |
3178d79b | 7833 | |
5829aea2 GP |
7834 | - (Source *) sourceAtIndexPath:(NSIndexPath *)indexPath { |
7835 | unsigned idx = 0; | |
7836 | switch (indexPath.section) { | |
7837 | case 0: idx = indexPath.row; break; | |
7838 | case 1: idx = indexPath.row + offset_; break; | |
3178d79b | 7839 | |
5829aea2 GP |
7840 | _nodefault |
7841 | } | |
7842 | return [sources_ objectAtIndex:idx]; | |
b4d89997 JF |
7843 | } |
7844 | ||
5829aea2 GP |
7845 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
7846 | static NSString *cellIdentifier = @"SourceCell"; | |
7847 | ||
7848 | SourceCell *cell = (SourceCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; | |
7849 | if(cell == nil) cell = [[[SourceCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease]; | |
7850 | [cell setSource:[self sourceAtIndexPath:indexPath]]; | |
b89fa270 | 7851 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
5829aea2 GP |
7852 | |
7853 | return cell; | |
f6e13561 GP |
7854 | } |
7855 | ||
5829aea2 GP |
7856 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
7857 | Source *source = [self sourceAtIndexPath:indexPath]; | |
7858 | ||
3f8edf70 | 7859 | SourceController *controller = [[[SourceController alloc] |
5829aea2 | 7860 | initWithDatabase:database_ |
3f8edf70 | 7861 | source:source |
5829aea2 GP |
7862 | ] autorelease]; |
7863 | ||
3f8edf70 | 7864 | [controller setDelegate:delegate_]; |
5829aea2 | 7865 | |
3f8edf70 | 7866 | [[self navigationController] pushViewController:controller animated:YES]; |
36bb2ca2 | 7867 | } |
b4d89997 | 7868 | |
6840bff3 | 7869 | - (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
5829aea2 GP |
7870 | Source *source = [self sourceAtIndexPath:indexPath]; |
7871 | return [source record] != nil; | |
dcb47737 RP |
7872 | } |
7873 | ||
6840bff3 | 7874 | - (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
0e1aa02c JF |
7875 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
7876 | Source *source = [self sourceAtIndexPath:indexPath]; | |
7877 | [Sources_ removeObjectForKey:[source key]]; | |
7878 | [delegate_ syncData]; | |
7879 | } | |
5829aea2 | 7880 | } |
bcccf498 | 7881 | |
5829aea2 | 7882 | - (void) complete { |
93460555 | 7883 | [delegate_ addTrivialSource:href_]; |
5829aea2 | 7884 | [delegate_ syncData]; |
3178d79b JF |
7885 | } |
7886 | ||
5829aea2 GP |
7887 | - (NSString *) getWarning { |
7888 | NSString *href(href_); | |
7889 | NSRange colon([href rangeOfString:@"://"]); | |
7890 | if (colon.location != NSNotFound) | |
7891 | href = [href substringFromIndex:(colon.location + 3)]; | |
7892 | href = [href stringByAddingPercentEscapes]; | |
7893 | href = [CydiaURL(@"api/repotag/") stringByAppendingString:href]; | |
7894 | href = [href stringByCachingURLWithCurrentCDN]; | |
7895 | ||
7896 | NSURL *url([NSURL URLWithString:href]); | |
7897 | ||
7898 | NSStringEncoding encoding; | |
7899 | NSError *error(nil); | |
7900 | ||
7901 | if (NSString *warning = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error]) | |
7902 | return [warning length] == 0 ? nil : warning; | |
7903 | return nil; | |
807ae6d7 | 7904 | } |
b4d89997 | 7905 | |
5829aea2 GP |
7906 | - (void) _endConnection:(NSURLConnection *)connection { |
7907 | // XXX: the memory management in this method is horribly awkward | |
7908 | ||
7909 | NSURLConnection **field = NULL; | |
7910 | if (connection == trivial_) | |
7911 | field = &trivial_; | |
7912 | else if (connection == trivial_bz2_) | |
7913 | field = &trivial_bz2_; | |
7914 | else if (connection == trivial_gz_) | |
7915 | field = &trivial_gz_; | |
7916 | _assert(field != NULL); | |
7917 | [connection release]; | |
7918 | *field = nil; | |
7919 | ||
7920 | if ( | |
7921 | trivial_ == nil && | |
7922 | trivial_bz2_ == nil && | |
7923 | trivial_gz_ == nil | |
7924 | ) { | |
9eae15b8 JF |
7925 | [delegate_ releaseNetworkActivityIndicator]; |
7926 | ||
7927 | [delegate_ removeProgressHUD:hud_]; | |
9eae15b8 JF |
7928 | hud_ = nil; |
7929 | ||
5829aea2 GP |
7930 | bool defer(false); |
7931 | ||
7932 | if (cydia_) { | |
7933 | if (NSString *warning = [self yieldToSelector:@selector(getWarning)]) { | |
7934 | defer = true; | |
7935 | ||
7936 | UIAlertView *alert = [[[UIAlertView alloc] | |
7937 | initWithTitle:UCLocalize("SOURCE_WARNING") | |
7938 | message:warning | |
7939 | delegate:self | |
7940 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
7941 | otherButtonTitles: |
7942 | UCLocalize("ADD_ANYWAY"), | |
7943 | nil | |
5829aea2 GP |
7944 | ] autorelease]; |
7945 | ||
7946 | [alert setContext:@"warning"]; | |
7947 | [alert setNumberOfRows:1]; | |
7948 | [alert show]; | |
7949 | } else | |
7950 | [self complete]; | |
7951 | } else if (error_ != nil) { | |
7952 | UIAlertView *alert = [[[UIAlertView alloc] | |
7953 | initWithTitle:UCLocalize("VERIFICATION_ERROR") | |
7954 | message:[error_ localizedDescription] | |
7955 | delegate:self | |
7956 | cancelButtonTitle:UCLocalize("OK") | |
7957 | otherButtonTitles:nil | |
7958 | ] autorelease]; | |
7959 | ||
7960 | [alert setContext:@"urlerror"]; | |
7961 | [alert show]; | |
7962 | } else { | |
7963 | UIAlertView *alert = [[[UIAlertView alloc] | |
7964 | initWithTitle:UCLocalize("NOT_REPOSITORY") | |
7965 | message:UCLocalize("NOT_REPOSITORY_EX") | |
7966 | delegate:self | |
7967 | cancelButtonTitle:UCLocalize("OK") | |
7968 | otherButtonTitles:nil | |
7969 | ] autorelease]; | |
7970 | ||
7971 | [alert setContext:@"trivial"]; | |
7972 | [alert show]; | |
7973 | } | |
7974 | ||
7b33d201 JF |
7975 | href_ = nil; |
7976 | error_ = nil; | |
5829aea2 | 7977 | } |
807ae6d7 | 7978 | } |
8fe19fc1 | 7979 | |
5829aea2 GP |
7980 | - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { |
7981 | switch ([response statusCode]) { | |
7982 | case 200: | |
7983 | cydia_ = YES; | |
7984 | } | |
8e05f686 RP |
7985 | } |
7986 | ||
5829aea2 GP |
7987 | - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { |
7988 | lprintf("connection:\"%s\" didFailWithError:\"%s\"", [href_ UTF8String], [[error localizedDescription] UTF8String]); | |
7b33d201 | 7989 | error_ = error; |
5829aea2 | 7990 | [self _endConnection:connection]; |
807ae6d7 | 7991 | } |
b4d89997 | 7992 | |
5829aea2 GP |
7993 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
7994 | [self _endConnection:connection]; | |
7995 | } | |
b4d89997 | 7996 | |
5829aea2 GP |
7997 | - (NSURLConnection *) _requestHRef:(NSString *)href method:(NSString *)method { |
7998 | NSMutableURLRequest *request = [NSMutableURLRequest | |
7999 | requestWithURL:[NSURL URLWithString:href] | |
8000 | cachePolicy:NSURLRequestUseProtocolCachePolicy | |
8001 | timeoutInterval:120.0 | |
8002 | ]; | |
bc11cf5b | 8003 | |
5829aea2 | 8004 | [request setHTTPMethod:method]; |
b4d89997 | 8005 | |
5829aea2 GP |
8006 | if (Machine_ != NULL) |
8007 | [request setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
8008 | if (UniqueID_ != nil) | |
8009 | [request setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
b4d89997 | 8010 | |
5829aea2 | 8011 | return [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; |
3178d79b JF |
8012 | } |
8013 | ||
6840bff3 | 8014 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
5829aea2 | 8015 | NSString *context([alert context]); |
dc5812ec | 8016 | |
5829aea2 GP |
8017 | if ([context isEqualToString:@"source"]) { |
8018 | switch (button) { | |
8019 | case 1: { | |
8020 | NSString *href = [[alert textField] text]; | |
baf80942 | 8021 | |
5829aea2 | 8022 | //installer_ = [[self _requestHRef:href method:@"GET"] retain]; |
f6e13561 | 8023 | |
5829aea2 GP |
8024 | if (![href hasSuffix:@"/"]) |
8025 | href_ = [href stringByAppendingString:@"/"]; | |
8026 | else | |
8027 | href_ = href; | |
b4d89997 | 8028 | |
5829aea2 GP |
8029 | trivial_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages"] method:@"HEAD"] retain]; |
8030 | trivial_bz2_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.bz2"] method:@"HEAD"] retain]; | |
8031 | trivial_gz_ = [[self _requestHRef:[href_ stringByAppendingString:@"Packages.gz"] method:@"HEAD"] retain]; | |
8032 | //trivial_bz2_ = [[self _requestHRef:[href stringByAppendingString:@"dists/Release"] method:@"HEAD"] retain]; | |
b4d89997 | 8033 | |
5829aea2 | 8034 | cydia_ = false; |
8fe19fc1 | 8035 | |
5829aea2 | 8036 | // XXX: this is stupid |
7b33d201 | 8037 | hud_ = [delegate_ addProgressHUD]; |
5829aea2 | 8038 | [hud_ setText:UCLocalize("VERIFYING_URL")]; |
dcaecde2 | 8039 | [delegate_ retainNetworkActivityIndicator]; |
5829aea2 | 8040 | } break; |
770f2a8e | 8041 | |
5829aea2 GP |
8042 | case 0: |
8043 | break; | |
bc11cf5b | 8044 | |
5829aea2 GP |
8045 | _nodefault |
8046 | } | |
770f2a8e | 8047 | |
5829aea2 GP |
8048 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8049 | } else if ([context isEqualToString:@"trivial"]) | |
8050 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8051 | else if ([context isEqualToString:@"urlerror"]) | |
8052 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8053 | else if ([context isEqualToString:@"warning"]) { | |
8054 | switch (button) { | |
8055 | case 1: | |
8056 | [self complete]; | |
8057 | break; | |
770f2a8e | 8058 | |
5829aea2 GP |
8059 | case 0: |
8060 | break; | |
b5e7eebb | 8061 | |
5829aea2 GP |
8062 | _nodefault |
8063 | } | |
770f2a8e | 8064 | |
5829aea2 | 8065 | href_ = nil; |
b5e7eebb | 8066 | |
5829aea2 GP |
8067 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
8068 | } | |
8069 | } | |
770f2a8e | 8070 | |
fe8e721f GP |
8071 | - (void) loadView { |
8072 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
770f2a8e | 8073 | |
7b33d201 | 8074 | list_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStylePlain] autorelease]; |
fe8e721f GP |
8075 | [list_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8076 | [list_ setRowHeight:56]; | |
7b33d201 | 8077 | [(UITableView *) list_ setDataSource:self]; |
fe8e721f GP |
8078 | [list_ setDelegate:self]; |
8079 | [[self view] addSubview:list_]; | |
8080 | } | |
770f2a8e | 8081 | |
fe8e721f | 8082 | - (void) viewDidLoad { |
7d887d0b JF |
8083 | [super viewDidLoad]; |
8084 | ||
fe8e721f GP |
8085 | [[self navigationItem] setTitle:UCLocalize("SOURCES")]; |
8086 | [self updateButtonsForEditingStatus:NO animated:NO]; | |
8087 | } | |
770f2a8e | 8088 | |
fe8e721f | 8089 | - (void) releaseSubviews { |
fe8e721f GP |
8090 | list_ = nil; |
8091 | } | |
770f2a8e | 8092 | |
fe8e721f GP |
8093 | - (id) initWithDatabase:(Database *)database { |
8094 | if ((self = [super init]) != nil) { | |
8095 | database_ = database; | |
7b33d201 | 8096 | sources_ = [NSMutableArray arrayWithCapacity:16]; |
807ae6d7 JF |
8097 | } return self; |
8098 | } | |
770f2a8e | 8099 | |
807ae6d7 | 8100 | - (void) reloadData { |
fe8e721f GP |
8101 | [super reloadData]; |
8102 | ||
5829aea2 | 8103 | pkgSourceList list; |
53ca7fdd | 8104 | if ([database_ popErrorWithTitle:UCLocalize("SOURCES") forOperation:list.ReadMainList()]) |
5829aea2 GP |
8105 | return; |
8106 | ||
8107 | [sources_ removeAllObjects]; | |
8108 | [sources_ addObjectsFromArray:[database_ sources]]; | |
8109 | _trace(); | |
8110 | [sources_ sortUsingSelector:@selector(compareByNameAndType:)]; | |
8111 | _trace(); | |
8112 | ||
8113 | int count([sources_ count]); | |
8114 | offset_ = 0; | |
8115 | for (int i = 0; i != count; i++) { | |
8116 | if ([[sources_ objectAtIndex:i] record] == nil) | |
8117 | break; | |
8118 | offset_++; | |
770f2a8e | 8119 | } |
807ae6d7 | 8120 | |
5829aea2 GP |
8121 | [list_ setEditing:NO]; |
8122 | [self updateButtonsForEditingStatus:NO animated:NO]; | |
8123 | [list_ reloadData]; | |
770f2a8e JF |
8124 | } |
8125 | ||
5829aea2 GP |
8126 | - (void) showAddSourcePrompt { |
8127 | UIAlertView *alert = [[[UIAlertView alloc] | |
8128 | initWithTitle:UCLocalize("ENTER_APT_URL") | |
8129 | message:nil | |
8130 | delegate:self | |
8131 | cancelButtonTitle:UCLocalize("CANCEL") | |
1aa29546 JF |
8132 | otherButtonTitles: |
8133 | UCLocalize("ADD_SOURCE"), | |
8134 | nil | |
5829aea2 | 8135 | ] autorelease]; |
770f2a8e | 8136 | |
5829aea2 | 8137 | [alert setContext:@"source"]; |
770f2a8e | 8138 | |
5829aea2 GP |
8139 | [alert setNumberOfRows:1]; |
8140 | [alert addTextFieldWithValue:@"http://" label:@""]; | |
770f2a8e | 8141 | |
5829aea2 GP |
8142 | UITextInputTraits *traits = [[alert textField] textInputTraits]; |
8143 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
8144 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
8145 | [traits setKeyboardType:UIKeyboardTypeURL]; | |
8146 | // XXX: UIReturnKeyDone | |
8147 | [traits setReturnKeyType:UIReturnKeyNext]; | |
770f2a8e | 8148 | |
5829aea2 | 8149 | [alert show]; |
770f2a8e JF |
8150 | } |
8151 | ||
5829aea2 GP |
8152 | - (void) addButtonClicked { |
8153 | [self showAddSourcePrompt]; | |
770f2a8e JF |
8154 | } |
8155 | ||
5829aea2 GP |
8156 | - (void) updateButtonsForEditingStatus:(BOOL)editing animated:(BOOL)animated { |
8157 | [[self navigationItem] setLeftBarButtonItem:(editing ? [[[UIBarButtonItem alloc] | |
8158 | initWithTitle:UCLocalize("ADD") | |
8159 | style:UIBarButtonItemStylePlain | |
8160 | target:self | |
8161 | action:@selector(addButtonClicked) | |
8162 | ] autorelease] : [[self navigationItem] backBarButtonItem]) animated:animated]; | |
8163 | ||
8164 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] | |
8165 | initWithTitle:(editing ? UCLocalize("DONE") : UCLocalize("EDIT")) | |
8166 | style:(editing ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain) | |
8167 | target:self | |
8168 | action:@selector(editButtonClicked) | |
8169 | ] autorelease] animated:animated]; | |
8170 | ||
8171 | if (IsWildcat_ && !editing) | |
8172 | [[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
8173 | initWithTitle:UCLocalize("SETTINGS") | |
8174 | style:UIBarButtonItemStylePlain | |
8175 | target:self | |
8176 | action:@selector(settingsButtonClicked) | |
8177 | ] autorelease]]; | |
770f2a8e JF |
8178 | } |
8179 | ||
5829aea2 GP |
8180 | - (void) settingsButtonClicked { |
8181 | [delegate_ showSettings]; | |
8182 | } | |
8183 | ||
8184 | - (void) editButtonClicked { | |
8185 | [list_ setEditing:![list_ isEditing] animated:YES]; | |
8186 | ||
8187 | [self updateButtonsForEditingStatus:[list_ isEditing] animated:YES]; | |
770f2a8e JF |
8188 | } |
8189 | ||
21c6da4b GP |
8190 | @end |
8191 | /* }}} */ | |
f3e11d24 | 8192 | |
5829aea2 | 8193 | /* Settings Controller {{{ */ |
cd79e8cf | 8194 | @interface SettingsController : CyteViewController < |
c21004b9 JF |
8195 | UITableViewDataSource, |
8196 | UITableViewDelegate | |
8197 | > { | |
21c6da4b | 8198 | _transient Database *database_; |
3931b718 JF |
8199 | // XXX: ok, "roledelegate_"?... |
8200 | _transient id roledelegate_; | |
bf7c998c | 8201 | _H<UITableView, 2> table_; |
7b33d201 JF |
8202 | _H<UISegmentedControl> segment_; |
8203 | _H<UIView> container_; | |
21c6da4b | 8204 | } |
c21004b9 JF |
8205 | |
8206 | - (void) showDoneButton; | |
8207 | - (void) resizeSegmentedControl; | |
8208 | ||
21c6da4b GP |
8209 | @end |
8210 | ||
5829aea2 | 8211 | @implementation SettingsController |
fe8e721f | 8212 | |
fe8e721f GP |
8213 | - (void) loadView { |
8214 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
8215 | ||
7b33d201 | 8216 | table_ = [[[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped] autorelease]; |
fe8e721f GP |
8217 | [table_ setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
8218 | [table_ setDelegate:self]; | |
7b33d201 | 8219 | [(UITableView *) table_ setDataSource:self]; |
fe8e721f GP |
8220 | [[self view] addSubview:table_]; |
8221 | ||
8222 | NSArray *items = [NSArray arrayWithObjects: | |
8223 | UCLocalize("USER"), | |
8224 | UCLocalize("HACKER"), | |
8225 | UCLocalize("DEVELOPER"), | |
8226 | nil]; | |
7b33d201 JF |
8227 | segment_ = [[[UISegmentedControl alloc] initWithItems:items] autorelease]; |
8228 | container_ = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, [[self view] frame].size.width, 44.0f)] autorelease]; | |
fe8e721f GP |
8229 | [container_ addSubview:segment_]; |
8230 | } | |
8231 | ||
8232 | - (void) viewDidLoad { | |
7d887d0b JF |
8233 | [super viewDidLoad]; |
8234 | ||
fe8e721f GP |
8235 | [[self navigationItem] setTitle:UCLocalize("WHO_ARE_YOU")]; |
8236 | ||
8237 | int index = -1; | |
8238 | if ([Role_ isEqualToString:@"User"]) index = 0; | |
8239 | if ([Role_ isEqualToString:@"Hacker"]) index = 1; | |
8240 | if ([Role_ isEqualToString:@"Developer"]) index = 2; | |
8241 | if (index != -1) { | |
8242 | [segment_ setSelectedSegmentIndex:index]; | |
8243 | [self showDoneButton]; | |
8244 | } | |
8245 | ||
8246 | [segment_ addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged]; | |
8247 | [self resizeSegmentedControl]; | |
8248 | } | |
8249 | ||
8250 | - (void) releaseSubviews { | |
fe8e721f | 8251 | table_ = nil; |
fe8e721f | 8252 | segment_ = nil; |
fe8e721f | 8253 | container_ = nil; |
21c6da4b GP |
8254 | } |
8255 | ||
8256 | - (id) initWithDatabase:(Database *)database delegate:(id)delegate { | |
6840bff3 | 8257 | if ((self = [super init]) != nil) { |
21c6da4b GP |
8258 | database_ = database; |
8259 | roledelegate_ = delegate; | |
21c6da4b GP |
8260 | } return self; |
8261 | } | |
8262 | ||
e62f29c6 GP |
8263 | - (void) resizeSegmentedControl { |
8264 | CGFloat width = [[self view] frame].size.width; | |
8265 | [segment_ setFrame:CGRectMake(width / 32.0f, 0, width - (width / 32.0f * 2.0f), 44.0f)]; | |
8266 | } | |
8267 | ||
2bdd73bd GP |
8268 | - (void) viewWillAppear:(BOOL)animated { |
8269 | [super viewWillAppear:animated]; | |
bc11cf5b | 8270 | |
e62f29c6 GP |
8271 | [self resizeSegmentedControl]; |
8272 | } | |
8273 | ||
8274 | - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { | |
8275 | [self resizeSegmentedControl]; | |
8276 | } | |
8277 | ||
8278 | - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { | |
8279 | [self resizeSegmentedControl]; | |
2bdd73bd GP |
8280 | } |
8281 | ||
21c6da4b | 8282 | - (void) save { |
fed0d010 | 8283 | NSString *role(nil); |
bc11cf5b | 8284 | |
21c6da4b | 8285 | switch ([segment_ selectedSegmentIndex]) { |
124c6201 GP |
8286 | case 0: role = @"User"; break; |
8287 | case 1: role = @"Hacker"; break; | |
8288 | case 2: role = @"Developer"; break; | |
21c6da4b GP |
8289 | |
8290 | _nodefault | |
8291 | } | |
8292 | ||
124c6201 | 8293 | if (![role isEqualToString:Role_]) { |
fed0d010 | 8294 | bool rolling(Role_ == nil); |
124c6201 | 8295 | Role_ = role; |
bc11cf5b | 8296 | |
124c6201 GP |
8297 | Settings_ = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
8298 | Role_, @"Role", | |
8299 | nil]; | |
21c6da4b | 8300 | |
124c6201 | 8301 | [Metadata_ setObject:Settings_ forKey:@"Settings"]; |
124c6201 | 8302 | Changed_ = true; |
bc11cf5b | 8303 | |
fed0d010 JF |
8304 | if (rolling) |
8305 | [roledelegate_ loadData]; | |
8306 | else | |
8307 | [roledelegate_ updateData]; | |
124c6201 | 8308 | } |
21c6da4b GP |
8309 | } |
8310 | ||
8311 | - (void) segmentChanged:(UISegmentedControl *)control { | |
8312 | [self showDoneButton]; | |
8313 | } | |
8314 | ||
8576ab50 | 8315 | - (void) saveAndClose { |
21c6da4b | 8316 | [self save]; |
8576ab50 GP |
8317 | |
8318 | [[self navigationItem] setRightBarButtonItem:nil]; | |
21c6da4b GP |
8319 | [[self navigationController] dismissModalViewControllerAnimated:YES]; |
8320 | } | |
8321 | ||
8576ab50 GP |
8322 | - (void) doneButtonClicked { |
8323 | UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)] autorelease]; | |
8324 | [spinner startAnimating]; | |
8325 | UIBarButtonItem *spinItem = [[[UIBarButtonItem alloc] initWithCustomView:spinner] autorelease]; | |
8326 | [[self navigationItem] setRightBarButtonItem:spinItem]; | |
8327 | ||
8328 | [self performSelector:@selector(saveAndClose) withObject:nil afterDelay:0]; | |
8329 | } | |
8330 | ||
21c6da4b | 8331 | - (void) showDoneButton { |
dd9de556 | 8332 | [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] |
21c6da4b GP |
8333 | initWithTitle:UCLocalize("DONE") |
8334 | style:UIBarButtonItemStyleDone | |
8335 | target:self | |
8336 | action:@selector(doneButtonClicked) | |
dd9de556 | 8337 | ] autorelease] animated:([[self navigationItem] rightBarButtonItem] == nil)]; |
21c6da4b GP |
8338 | } |
8339 | ||
8340 | - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { | |
e62f29c6 GP |
8341 | // XXX: For not having a single cell in the table, this sure is a lot of sections. |
8342 | return 6; | |
21c6da4b GP |
8343 | } |
8344 | ||
8345 | - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
8346 | return 0; // :( | |
8347 | } | |
8348 | ||
6840bff3 | 8349 | - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
21c6da4b GP |
8350 | return nil; // This method is required by the protocol. |
8351 | } | |
8352 | ||
8353 | - (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { | |
bc11cf5b | 8354 | if (section == 1) |
21c6da4b | 8355 | return UCLocalize("ROLE_EX"); |
bc11cf5b | 8356 | if (section == 4) |
21c6da4b GP |
8357 | return [NSString stringWithFormat: |
8358 | @"%@: %@\n%@: %@\n%@: %@", | |
bc11cf5b JF |
8359 | UCLocalize("USER"), UCLocalize("USER_EX"), |
8360 | UCLocalize("HACKER"), UCLocalize("HACKER_EX"), | |
21c6da4b GP |
8361 | UCLocalize("DEVELOPER"), UCLocalize("DEVELOPER_EX") |
8362 | ]; | |
8363 | else return nil; | |
8364 | } | |
8365 | ||
8366 | - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { | |
36fbb2aa | 8367 | return section == 3 ? 44.0f : 0; |
21c6da4b GP |
8368 | } |
8369 | ||
8370 | - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { | |
36fbb2aa | 8371 | return section == 3 ? container_ : nil; |
21c6da4b GP |
8372 | } |
8373 | ||
fe8e721f GP |
8374 | - (void) reloadData { |
8375 | [super reloadData]; | |
9dac415b | 8376 | |
fe8e721f GP |
8377 | [table_ reloadData]; |
8378 | } | |
8379 | ||
f3e11d24 GP |
8380 | @end |
8381 | /* }}} */ | |
8382 | /* Stash Controller {{{ */ | |
cd79e8cf | 8383 | @interface StashController : CyteViewController { |
7b33d201 JF |
8384 | _H<UIActivityIndicatorView> spinner_; |
8385 | _H<UILabel> status_; | |
8386 | _H<UILabel> caption_; | |
f3e11d24 | 8387 | } |
6840bff3 | 8388 | |
f3e11d24 GP |
8389 | @end |
8390 | ||
5829aea2 | 8391 | @implementation StashController |
f3e11d24 | 8392 | |
fe8e721f GP |
8393 | - (void) loadView { |
8394 | [self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]]; | |
8395 | [[self view] setBackgroundColor:[UIColor viewFlipsideBackgroundColor]]; | |
8396 | ||
7b33d201 | 8397 | spinner_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease]; |
fe8e721f GP |
8398 | CGRect spinrect = [spinner_ frame]; |
8399 | spinrect.origin.x = ([[self view] frame].size.width / 2) - (spinrect.size.width / 2); | |
8400 | spinrect.origin.y = [[self view] frame].size.height - 80.0f; | |
8401 | [spinner_ setFrame:spinrect]; | |
8402 | [spinner_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin]; | |
8403 | [[self view] addSubview:spinner_]; | |
8404 | [spinner_ startAnimating]; | |
8405 | ||
8406 | CGRect captrect; | |
8407 | captrect.size.width = [[self view] frame].size.width; | |
8408 | captrect.size.height = 40.0f; | |
8409 | captrect.origin.x = 0; | |
8410 | captrect.origin.y = ([[self view] frame].size.height / 2) - (captrect.size.height * 2); | |
7b33d201 | 8411 | caption_ = [[[UILabel alloc] initWithFrame:captrect] autorelease]; |
fe8e721f GP |
8412 | [caption_ setText:UCLocalize("PREPARING_FILESYSTEM")]; |
8413 | [caption_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; | |
8414 | [caption_ setFont:[UIFont boldSystemFontOfSize:28.0f]]; | |
8415 | [caption_ setTextColor:[UIColor whiteColor]]; | |
8416 | [caption_ setBackgroundColor:[UIColor clearColor]]; | |
8417 | [caption_ setShadowColor:[UIColor blackColor]]; | |
8418 | [caption_ setTextAlignment:UITextAlignmentCenter]; | |
8419 | [[self view] addSubview:caption_]; | |
8420 | ||
8421 | CGRect statusrect; | |
8422 | statusrect.size.width = [[self view] frame].size.width; | |
8423 | statusrect.size.height = 30.0f; | |
8424 | statusrect.origin.x = 0; | |
8425 | statusrect.origin.y = ([[self view] frame].size.height / 2) - statusrect.size.height; | |
7b33d201 | 8426 | status_ = [[[UILabel alloc] initWithFrame:statusrect] autorelease]; |
fe8e721f GP |
8427 | [status_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin]; |
8428 | [status_ setText:UCLocalize("EXIT_WHEN_COMPLETE")]; | |
8429 | [status_ setFont:[UIFont systemFontOfSize:16.0f]]; | |
8430 | [status_ setTextColor:[UIColor whiteColor]]; | |
8431 | [status_ setBackgroundColor:[UIColor clearColor]]; | |
8432 | [status_ setShadowColor:[UIColor blackColor]]; | |
8433 | [status_ setTextAlignment:UITextAlignmentCenter]; | |
8434 | [[self view] addSubview:status_]; | |
8435 | } | |
8436 | ||
770f2a8e | 8437 | @end |
807ae6d7 | 8438 | /* }}} */ |
770f2a8e | 8439 | |
a06e9179 JF |
8440 | @interface CYURLCache : SDURLCache { |
8441 | } | |
8442 | ||
8443 | @end | |
8444 | ||
8445 | @implementation CYURLCache | |
8446 | ||
8447 | - (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request { | |
8448 | #if !ForRelease | |
8449 | if (false); | |
8450 | else if ([event isEqualToString:@"no-cache"]) | |
8451 | event = @"!!!"; | |
8452 | else if ([event isEqualToString:@"store"]) | |
8453 | event = @">>>"; | |
8454 | else if ([event isEqualToString:@"invalid"]) | |
8455 | event = @"???"; | |
8456 | else if ([event isEqualToString:@"memory"]) | |
8457 | event = @"mem"; | |
8458 | else if ([event isEqualToString:@"disk"]) | |
8459 | event = @"ssd"; | |
8460 | else if ([event isEqualToString:@"miss"]) | |
8461 | event = @"---"; | |
8462 | ||
8463 | NSLog(@"%@: %@", event, [[request URL] absoluteString]); | |
8464 | #endif | |
8465 | } | |
8466 | ||
5e845121 JF |
8467 | - (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request { |
8468 | if (NSURLResponse *response = [cached response]) | |
8469 | if (NSString *mime = [response MIMEType]) | |
8470 | if ([mime isEqualToString:@"text/cache-manifest"]) { | |
8471 | NSURL *url([response URL]); | |
8472 | ||
8473 | #if !ForRelease | |
8474 | NSLog(@"###: %@", [url absoluteString]); | |
8475 | #endif | |
8476 | ||
8477 | @synchronized (HostConfig_) { | |
8478 | [CachedURLs_ addObject:url]; | |
8479 | } | |
8480 | } | |
8481 | ||
8482 | [super storeCachedResponse:cached forRequest:request]; | |
8483 | } | |
8484 | ||
a06e9179 JF |
8485 | @end |
8486 | ||
2367a917 | 8487 | @interface Cydia : UIApplication < |
adb61bda | 8488 | ConfirmationControllerDelegate, |
6915b806 | 8489 | DatabaseDelegate, |
eb30da80 | 8490 | CydiaDelegate, |
68046ccc JF |
8491 | UINavigationControllerDelegate, |
8492 | UITabBarControllerDelegate | |
2367a917 | 8493 | > { |
7b33d201 JF |
8494 | _H<UIWindow> window_; |
8495 | _H<CYTabBarController> tabbar_; | |
70750ab3 | 8496 | _H<CydiaLoadingViewController> emulated_; |
3931b718 | 8497 | |
7b33d201 JF |
8498 | _H<NSMutableArray> essential_; |
8499 | _H<NSMutableArray> broken_; | |
dc5812ec JF |
8500 | |
8501 | Database *database_; | |
dc5812ec | 8502 | |
7b33d201 | 8503 | _H<NSURL> starturl_; |
54043703 | 8504 | |
235f5487 | 8505 | unsigned locked_; |
54043703 | 8506 | unsigned activity_; |
9b619239 | 8507 | |
7b33d201 | 8508 | _H<StashController> stash_; |
f3e11d24 | 8509 | |
8c02abc8 | 8510 | bool loaded_; |
dc5812ec JF |
8511 | } |
8512 | ||
fed0d010 | 8513 | - (void) loadData; |
670a0494 | 8514 | |
dc5812ec JF |
8515 | @end |
8516 | ||
8517 | @implementation Cydia | |
8518 | ||
b5e7eebb | 8519 | - (void) beginUpdate { |
7585ce66 | 8520 | [tabbar_ beginUpdate]; |
b5e7eebb GP |
8521 | } |
8522 | ||
8523 | - (BOOL) updating { | |
7585ce66 | 8524 | return [tabbar_ updating]; |
b5e7eebb GP |
8525 | } |
8526 | ||
9bedffaa JF |
8527 | - (void) _loaded { |
8528 | if ([broken_ count] != 0) { | |
8529 | int count = [broken_ count]; | |
8530 | ||
37d2b2a9 | 8531 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 8532 | initWithTitle:(count == 1 ? UCLocalize("HALFINSTALLED_PACKAGE") : [NSString stringWithFormat:UCLocalize("HALFINSTALLED_PACKAGES"), count]) |
b5e7eebb GP |
8533 | message:UCLocalize("HALFINSTALLED_PACKAGE_EX") |
8534 | delegate:self | |
8535 | cancelButtonTitle:UCLocalize("FORCIBLY_CLEAR") | |
1aa29546 JF |
8536 | otherButtonTitles: |
8537 | UCLocalize("TEMPORARY_IGNORE"), | |
8538 | nil | |
9bedffaa JF |
8539 | ] autorelease]; |
8540 | ||
37d2b2a9 | 8541 | [alert setContext:@"fixhalf"]; |
59befad5 | 8542 | [alert setNumberOfRows:2]; |
37d2b2a9 | 8543 | [alert show]; |
9bedffaa JF |
8544 | } else if (!Ignored_ && [essential_ count] != 0) { |
8545 | int count = [essential_ count]; | |
8546 | ||
37d2b2a9 | 8547 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 8548 | initWithTitle:(count == 1 ? UCLocalize("ESSENTIAL_UPGRADE") : [NSString stringWithFormat:UCLocalize("ESSENTIAL_UPGRADES"), count]) |
b5e7eebb GP |
8549 | message:UCLocalize("ESSENTIAL_UPGRADE_EX") |
8550 | delegate:self | |
8551 | cancelButtonTitle:UCLocalize("TEMPORARY_IGNORE") | |
1aa29546 JF |
8552 | otherButtonTitles: |
8553 | UCLocalize("UPGRADE_ESSENTIAL"), | |
8554 | UCLocalize("COMPLETE_UPGRADE"), | |
8555 | nil | |
9bedffaa JF |
8556 | ] autorelease]; |
8557 | ||
37d2b2a9 GP |
8558 | [alert setContext:@"upgrade"]; |
8559 | [alert show]; | |
9bedffaa JF |
8560 | } |
8561 | } | |
8562 | ||
7623f855 | 8563 | - (void) _saveConfig { |
ffbb3bd5 JF |
8564 | _trace(); |
8565 | MetaFile_.Sync(); | |
8566 | _trace(); | |
8567 | ||
7623f855 | 8568 | if (Changed_) { |
7623f855 | 8569 | NSString *error(nil); |
ffbb3bd5 | 8570 | |
7623f855 JF |
8571 | if (NSData *data = [NSPropertyListSerialization dataFromPropertyList:Metadata_ format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error]) { |
8572 | _trace(); | |
8573 | NSError *error(nil); | |
8574 | if (![data writeToFile:@"/var/lib/cydia/metadata.plist" options:NSAtomicWrite error:&error]) | |
8575 | NSLog(@"failure to save metadata data: %@", error); | |
8576 | _trace(); | |
ffbb3bd5 JF |
8577 | |
8578 | Changed_ = false; | |
7623f855 JF |
8579 | } else { |
8580 | NSLog(@"failure to serialize metadata: %@", error); | |
7623f855 | 8581 | } |
7623f855 JF |
8582 | } |
8583 | } | |
8584 | ||
89571a5b | 8585 | // Navigation controller for the queuing badge. |
15f0d613 | 8586 | - (UINavigationController *) queueNavigationController { |
89571a5b GP |
8587 | NSArray *controllers = [tabbar_ viewControllers]; |
8588 | return [controllers objectAtIndex:3]; | |
8589 | } | |
8590 | ||
302bf91c JF |
8591 | - (void) unloadData { |
8592 | [tabbar_ unloadData]; | |
8593 | } | |
8594 | ||
7623f855 JF |
8595 | - (void) _updateData { |
8596 | [self _saveConfig]; | |
8597 | ||
302bf91c | 8598 | [self unloadData]; |
f6371a33 | 8599 | |
15f0d613 | 8600 | UINavigationController *navigation = [self queueNavigationController]; |
b5e7eebb | 8601 | |
4305896c | 8602 | id queuedelegate = nil; |
89571a5b GP |
8603 | if ([[navigation viewControllers] count] > 0) |
8604 | queuedelegate = [[navigation viewControllers] objectAtIndex:0]; | |
bc11cf5b | 8605 | |
89571a5b GP |
8606 | [queuedelegate queueStatusDidChange]; |
8607 | [[navigation tabBarItem] setBadgeValue:(Queuing_ ? UCLocalize("Q_D") : nil)]; | |
7623f855 JF |
8608 | } |
8609 | ||
53fb38da | 8610 | - (void) _refreshIfPossible:(NSDate *)update { |
8c02abc8 | 8611 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
bc11cf5b | 8612 | |
45e21ffa | 8613 | bool recently = false; |
45e21ffa GP |
8614 | if (update != nil) { |
8615 | NSTimeInterval interval([update timeIntervalSinceNow]); | |
8616 | if (interval <= 0 && interval > -(15*60)) | |
8617 | recently = true; | |
8618 | } | |
8619 | ||
8620 | // Don't automatic refresh if: | |
8621 | // - We already refreshed recently. | |
8622 | // - We already auto-refreshed this launch. | |
8623 | // - Auto-refresh is disabled. | |
8624 | if (recently || loaded_ || ManualRefresh) { | |
35f0a3b5 | 8625 | // If we are cancelling, we need to make sure it knows it's already loaded. |
45e21ffa | 8626 | loaded_ = true; |
d13edf44 JF |
8627 | |
8628 | [self performSelectorOnMainThread:@selector(_loaded) withObject:nil waitUntilDone:NO]; | |
45e21ffa GP |
8629 | } else { |
8630 | // We are going to load, so remember that. | |
8631 | loaded_ = true; | |
45e21ffa | 8632 | |
d13edf44 JF |
8633 | SCNetworkReachabilityFlags flags; { |
8634 | SCNetworkReachabilityRef reachability(SCNetworkReachabilityCreateWithName(NULL, "cydia.saurik.com")); | |
8635 | SCNetworkReachabilityGetFlags(reachability, &flags); | |
8636 | CFRelease(reachability); | |
8637 | } | |
afb5333a | 8638 | |
d13edf44 JF |
8639 | // XXX: this elaborate mess is what Apple is using to determine this? :( |
8640 | // XXX: do we care if the user has to intervene? maybe that's ok? | |
8641 | bool reachable( | |
8642 | (flags & kSCNetworkReachabilityFlagsReachable) != 0 && ( | |
8643 | (flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0 || ( | |
8644 | (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 || | |
8645 | (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0 | |
8646 | ) && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0 || | |
8647 | (flags & kSCNetworkReachabilityFlagsIsWWAN) != 0 | |
8648 | ) | |
8649 | ); | |
bc11cf5b | 8650 | |
d13edf44 JF |
8651 | // If we can reach the server, auto-refresh! |
8652 | if (reachable) | |
8653 | [tabbar_ performSelectorOnMainThread:@selector(setUpdate:) withObject:update waitUntilDone:NO]; | |
8654 | } | |
9aecdc9c | 8655 | |
8c02abc8 | 8656 | [pool release]; |
9aecdc9c GP |
8657 | } |
8658 | ||
8659 | - (void) refreshIfPossible { | |
53fb38da | 8660 | [NSThread detachNewThreadSelector:@selector(_refreshIfPossible:) toTarget:self withObject:[Metadata_ objectForKey:@"LastUpdate"]]; |
9aecdc9c GP |
8661 | } |
8662 | ||
e09e1589 JF |
8663 | - (void) reloadDataWithInvocation:(NSInvocation *)invocation { |
8664 | @synchronized (self) { | |
851f4a99 GP |
8665 | UIProgressHUD *hud(loaded_ ? [self addProgressHUD] : nil); |
8666 | [hud setText:UCLocalize("RELOADING_DATA")]; | |
dc5812ec | 8667 | |
4ba8f30a | 8668 | [database_ yieldToSelector:@selector(reloadDataWithInvocation:) withObject:invocation]; |
d061f4ba | 8669 | |
36fbb2aa JF |
8670 | if (hud != nil) |
8671 | [self removeProgressHUD:hud]; | |
c25a610d | 8672 | |
36bb2ca2 | 8673 | size_t changes(0); |
9bedffaa | 8674 | |
a54b1c10 | 8675 | [essential_ removeAllObjects]; |
9bedffaa | 8676 | [broken_ removeAllObjects]; |
b4d89997 | 8677 | |
670a0494 | 8678 | NSArray *packages([database_ packages]); |
affeffc7 | 8679 | for (Package *package in packages) { |
9bedffaa JF |
8680 | if ([package half]) |
8681 | [broken_ addObject:package]; | |
31f3cfff | 8682 | if ([package upgradableAndEssential:NO]) { |
a54b1c10 JF |
8683 | if ([package essential]) |
8684 | [essential_ addObject:package]; | |
36bb2ca2 | 8685 | ++changes; |
a54b1c10 | 8686 | } |
36bb2ca2 | 8687 | } |
b4d89997 | 8688 | |
89571a5b | 8689 | UITabBarItem *changesItem = [[[tabbar_ viewControllers] objectAtIndex:2] tabBarItem]; |
36bb2ca2 | 8690 | if (changes != 0) { |
0e1784b4 | 8691 | _trace(); |
36bb2ca2 | 8692 | NSString *badge([[NSNumber numberWithInt:changes] stringValue]); |
45e21ffa | 8693 | [changesItem setBadgeValue:badge]; |
65a03a7a | 8694 | [changesItem setAnimatedBadge:([essential_ count] > 0)]; |
0e1784b4 | 8695 | [self setApplicationIconBadgeNumber:changes]; |
c25a610d | 8696 | } else { |
0e1784b4 | 8697 | _trace(); |
45e21ffa GP |
8698 | [changesItem setBadgeValue:nil]; |
8699 | [changesItem setAnimatedBadge:NO]; | |
0e1784b4 | 8700 | [self setApplicationIconBadgeNumber:0]; |
c25a610d | 8701 | } |
b4d89997 | 8702 | |
7623f855 | 8703 | [self _updateData]; |
6d9712c4 | 8704 | |
8c02abc8 | 8705 | [self refreshIfPossible]; |
e09e1589 | 8706 | } } |
6d9712c4 | 8707 | |
7b0ce2da | 8708 | - (void) updateData { |
7623f855 | 8709 | [self _updateData]; |
b4d89997 JF |
8710 | } |
8711 | ||
7b0ce2da JF |
8712 | - (void) update_ { |
8713 | [database_ update]; | |
fca2f596 | 8714 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; |
7b0ce2da JF |
8715 | } |
8716 | ||
2e9123cb JF |
8717 | - (void) disemulate { |
8718 | if (emulated_ == nil) | |
8719 | return; | |
8720 | ||
8721 | [window_ addSubview:[tabbar_ view]]; | |
8722 | [[emulated_ view] removeFromSuperview]; | |
2e9123cb JF |
8723 | emulated_ = nil; |
8724 | [window_ setUserInteractionEnabled:YES]; | |
8725 | } | |
8726 | ||
8727 | - (void) presentModalViewController:(UIViewController *)controller force:(BOOL)force { | |
15f0d613 | 8728 | UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:controller] autorelease]); |
6915b806 JF |
8729 | if (IsWildcat_) |
8730 | [navigation setModalPresentationStyle:UIModalPresentationFormSheet]; | |
2e9123cb JF |
8731 | |
8732 | UIViewController *parent; | |
8733 | if (emulated_ == nil) | |
8734 | parent = tabbar_; | |
8735 | else if (!force) | |
8736 | parent = emulated_; | |
8737 | else { | |
8738 | [self disemulate]; | |
8739 | parent = tabbar_; | |
8740 | } | |
8741 | ||
8742 | [parent presentModalViewController:navigation animated:YES]; | |
6915b806 JF |
8743 | } |
8744 | ||
8745 | - (ProgressController *) invokeNewProgress:(NSInvocation *)invocation forController:(UINavigationController *)navigation withTitle:(NSString *)title { | |
8746 | ProgressController *progress([[[ProgressController alloc] initWithDatabase:database_ delegate:self] autorelease]); | |
8747 | ||
8748 | if (navigation != nil) | |
8749 | [navigation pushViewController:progress animated:YES]; | |
8750 | else | |
2e9123cb | 8751 | [self presentModalViewController:progress force:YES]; |
6915b806 JF |
8752 | |
8753 | [progress invoke:invocation withTitle:title]; | |
8754 | return progress; | |
8755 | } | |
8756 | ||
8757 | - (void) detachNewProgressSelector:(SEL)selector toTarget:(id)target forController:(UINavigationController *)navigation title:(NSString *)title { | |
8758 | [self invokeNewProgress:[NSInvocation invocationWithSelector:selector forTarget:target] forController:navigation withTitle:title]; | |
8759 | } | |
8760 | ||
8761 | - (void) repairWithInvocation:(NSInvocation *)invocation { | |
8762 | _trace(); | |
8d5bc2ad | 8763 | [self invokeNewProgress:invocation forController:nil withTitle:@"REPAIRING"]; |
6915b806 JF |
8764 | _trace(); |
8765 | } | |
8766 | ||
8767 | - (void) repairWithSelector:(SEL)selector { | |
8768 | [self performSelectorOnMainThread:@selector(repairWithInvocation:) withObject:[NSInvocation invocationWithSelector:selector forTarget:database_] waitUntilDone:YES]; | |
8769 | } | |
8770 | ||
e09e1589 JF |
8771 | - (void) reloadData { |
8772 | [self reloadDataWithInvocation:nil]; | |
8773 | } | |
8774 | ||
7b0ce2da | 8775 | - (void) syncData { |
1fe5dc43 JF |
8776 | [self _saveConfig]; |
8777 | ||
670a0494 | 8778 | FILE *file(fopen("/etc/apt/sources.list.d/cydia.list", "w")); |
7b0ce2da JF |
8779 | _assert(file != NULL); |
8780 | ||
670a0494 JF |
8781 | for (NSString *key in [Sources_ allKeys]) { |
8782 | NSDictionary *source([Sources_ objectForKey:key]); | |
7b0ce2da JF |
8783 | |
8784 | fprintf(file, "%s %s %s\n", | |
8785 | [[source objectForKey:@"Type"] UTF8String], | |
8786 | [[source objectForKey:@"URI"] UTF8String], | |
8787 | [[source objectForKey:@"Distribution"] UTF8String] | |
8788 | ); | |
8789 | } | |
8790 | ||
8791 | fclose(file); | |
8792 | ||
8d5bc2ad | 8793 | [self detachNewProgressSelector:@selector(update_) toTarget:self forController:nil title:@"UPDATING_SOURCES"]; |
7b0ce2da JF |
8794 | } |
8795 | ||
93460555 JF |
8796 | - (void) addTrivialSource:(NSString *)href { |
8797 | [Sources_ setObject:[NSDictionary dictionaryWithObjectsAndKeys: | |
8798 | @"deb", @"Type", | |
8799 | href, @"URI", | |
8800 | @"./", @"Distribution", | |
8801 | nil] forKey:[NSString stringWithFormat:@"deb:%@:./", href]]; | |
516d1d40 JF |
8802 | |
8803 | Changed_ = true; | |
93460555 JF |
8804 | } |
8805 | ||
b4d89997 JF |
8806 | - (void) resolve { |
8807 | pkgProblemResolver *resolver = [database_ resolver]; | |
8808 | ||
8809 | resolver->InstallProtect(); | |
8810 | if (!resolver->Resolve(true)) | |
8811 | _error->Discard(); | |
2367a917 JF |
8812 | } |
8813 | ||
670a0494 | 8814 | - (bool) perform { |
d6c371f5 JF |
8815 | // XXX: this is a really crappy way of doing this. |
8816 | // like, seriously: this state machine is still broken, and cancelling this here doesn't really /fix/ that. | |
8817 | // for one, the user can still /start/ a reloading data event while they have a queue, which is stupid | |
8818 | // for two, this just means there is a race condition between the refresh completing and the confirmation controller appearing. | |
8819 | if ([tabbar_ updating]) | |
8820 | [tabbar_ cancelUpdate]; | |
8821 | ||
670a0494 JF |
8822 | if (![database_ prepare]) |
8823 | return false; | |
49048579 | 8824 | |
adb61bda | 8825 | ConfirmationController *page([[[ConfirmationController alloc] initWithDatabase:database_] autorelease]); |
affeffc7 | 8826 | [page setDelegate:self]; |
15f0d613 | 8827 | UINavigationController *confirm_([[[UINavigationController alloc] initWithRootViewController:page] autorelease]); |
49048579 | 8828 | |
36fbb2aa JF |
8829 | if (IsWildcat_) |
8830 | [confirm_ setModalPresentationStyle:UIModalPresentationFormSheet]; | |
7585ce66 | 8831 | [tabbar_ presentModalViewController:confirm_ animated:YES]; |
670a0494 JF |
8832 | |
8833 | return true; | |
3178d79b JF |
8834 | } |
8835 | ||
dc63e78f JF |
8836 | - (void) queue { |
8837 | @synchronized (self) { | |
8838 | [self perform]; | |
8839 | } | |
8840 | } | |
8841 | ||
8842 | - (void) clearPackage:(Package *)package { | |
8843 | @synchronized (self) { | |
8844 | [package clear]; | |
8845 | [self resolve]; | |
8846 | [self perform]; | |
8847 | } | |
8848 | } | |
8849 | ||
77801ff1 JF |
8850 | - (void) installPackages:(NSArray *)packages { |
8851 | @synchronized (self) { | |
8852 | for (Package *package in packages) | |
8853 | [package install]; | |
8854 | [self resolve]; | |
8855 | [self perform]; | |
8856 | } | |
8857 | } | |
8858 | ||
36bb2ca2 JF |
8859 | - (void) installPackage:(Package *)package { |
8860 | @synchronized (self) { | |
8861 | [package install]; | |
8862 | [self resolve]; | |
8863 | [self perform]; | |
8864 | } | |
8865 | } | |
8866 | ||
8867 | - (void) removePackage:(Package *)package { | |
8868 | @synchronized (self) { | |
8869 | [package remove]; | |
8870 | [self resolve]; | |
8871 | [self perform]; | |
8872 | } | |
8873 | } | |
8874 | ||
8875 | - (void) distUpgrade { | |
8876 | @synchronized (self) { | |
670a0494 JF |
8877 | if (![database_ upgrade]) |
8878 | return; | |
36bb2ca2 JF |
8879 | [self perform]; |
8880 | } | |
b4d89997 JF |
8881 | } |
8882 | ||
fca2f596 JF |
8883 | - (void) perform_ { |
8884 | [database_ perform]; | |
8885 | [self performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; | |
8886 | } | |
8887 | ||
b5e7eebb | 8888 | - (void) confirmWithNavigationController:(UINavigationController *)navigation { |
f6371a33 | 8889 | Queuing_ = false; |
235f5487 | 8890 | ++locked_; |
fca2f596 | 8891 | [self detachNewProgressSelector:@selector(perform_) toTarget:self forController:navigation title:@"RUNNING"]; |
235f5487 | 8892 | --locked_; |
dc5812ec JF |
8893 | } |
8894 | ||
21c6da4b | 8895 | - (void) showSettings { |
a54fed5b | 8896 | [self presentModalViewController:[[[SettingsController alloc] initWithDatabase:database_ delegate:self] autorelease] force:NO]; |
7b0ce2da JF |
8897 | } |
8898 | ||
54043703 JF |
8899 | - (void) retainNetworkActivityIndicator { |
8900 | if (activity_++ == 0) | |
8901 | [self setNetworkActivityIndicatorVisible:YES]; | |
848ed88b JF |
8902 | |
8903 | #if TraceLogging | |
8904 | NSLog(@"retainNetworkActivityIndicator->%d", activity_); | |
8905 | #endif | |
54043703 JF |
8906 | } |
8907 | ||
8908 | - (void) releaseNetworkActivityIndicator { | |
8909 | if (--activity_ == 0) | |
8910 | [self setNetworkActivityIndicatorVisible:NO]; | |
848ed88b JF |
8911 | |
8912 | #if TraceLogging | |
8913 | NSLog(@"releaseNetworkActivityIndicator->%d", activity_); | |
8914 | #endif | |
8915 | ||
54043703 JF |
8916 | } |
8917 | ||
674dce72 GP |
8918 | - (void) cancelAndClear:(bool)clear { |
8919 | @synchronized (self) { | |
8920 | if (clear) { | |
c6ca67ba | 8921 | [database_ clear]; |
b5e7eebb | 8922 | Queuing_ = false; |
674dce72 | 8923 | } else { |
b5e7eebb | 8924 | Queuing_ = true; |
6067f1b8 JF |
8925 | } |
8926 | ||
a4217bbb | 8927 | [self _updateData]; |
674dce72 | 8928 | } |
37d2b2a9 | 8929 | } |
7b0ce2da | 8930 | |
b5e7eebb GP |
8931 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
8932 | NSString *context([alert context]); | |
bc11cf5b | 8933 | |
6915b806 JF |
8934 | if ([context isEqualToString:@"conffile"]) { |
8935 | FILE *input = [database_ input]; | |
8936 | if (button == [alert cancelButtonIndex]) | |
8937 | fprintf(input, "N\n"); | |
8938 | else if (button == [alert firstOtherButtonIndex]) | |
8939 | fprintf(input, "Y\n"); | |
8940 | fflush(input); | |
8941 | ||
8942 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; | |
8943 | } else if ([context isEqualToString:@"fixhalf"]) { | |
efa53fa9 | 8944 | if (button == [alert cancelButtonIndex]) { |
37d2b2a9 | 8945 | @synchronized (self) { |
7b33d201 | 8946 | for (Package *broken in (id) broken_) { |
37d2b2a9 GP |
8947 | [broken remove]; |
8948 | ||
8949 | NSString *id = [broken id]; | |
8950 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.prerm", id] UTF8String]); | |
8951 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postrm", id] UTF8String]); | |
8952 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.preinst", id] UTF8String]); | |
8953 | unlink([[NSString stringWithFormat:@"/var/lib/dpkg/info/%@.postinst", id] UTF8String]); | |
8954 | } | |
7b0ce2da | 8955 | |
37d2b2a9 GP |
8956 | [self resolve]; |
8957 | [self perform]; | |
8958 | } | |
efa53fa9 | 8959 | } else if (button == [alert firstOtherButtonIndex]) { |
37d2b2a9 GP |
8960 | [broken_ removeAllObjects]; |
8961 | [self _loaded]; | |
7b0ce2da JF |
8962 | } |
8963 | ||
37d2b2a9 | 8964 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 8965 | } else if ([context isEqualToString:@"upgrade"]) { |
37d2b2a9 GP |
8966 | if (button == [alert firstOtherButtonIndex]) { |
8967 | @synchronized (self) { | |
7b33d201 | 8968 | for (Package *essential in (id) essential_) |
37d2b2a9 | 8969 | [essential install]; |
7b0ce2da | 8970 | |
37d2b2a9 GP |
8971 | [self resolve]; |
8972 | [self perform]; | |
8973 | } | |
8974 | } else if (button == [alert firstOtherButtonIndex] + 1) { | |
8975 | [self distUpgrade]; | |
8976 | } else if (button == [alert cancelButtonIndex]) { | |
8977 | Ignored_ = YES; | |
7b0ce2da JF |
8978 | } |
8979 | ||
37d2b2a9 | 8980 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
1cedb821 | 8981 | } |
7b0ce2da JF |
8982 | } |
8983 | ||
d13edf44 JF |
8984 | - (void) system:(NSString *)command { |
8985 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
8986 | ||
985d2dff | 8987 | _trace(); |
670a0494 | 8988 | system([command UTF8String]); |
985d2dff | 8989 | _trace(); |
d13edf44 JF |
8990 | |
8991 | [pool release]; | |
670a0494 JF |
8992 | } |
8993 | ||
8994 | - (void) applicationWillSuspend { | |
8995 | [database_ clean]; | |
8996 | [super applicationWillSuspend]; | |
bd150f54 JF |
8997 | } |
8998 | ||
235f5487 | 8999 | - (BOOL) isSafeToSuspend { |
5927fe03 JF |
9000 | if (locked_ != 0) { |
9001 | #if !ForRelease | |
9002 | NSLog(@"isSafeToSuspend: locked_ != 0"); | |
9003 | #endif | |
9004 | return false; | |
9005 | } | |
9006 | ||
262afe11 GP |
9007 | // Use external process status API internally. |
9008 | // This is probably a really bad idea. | |
235f5487 | 9009 | // XXX: what is the point of this? does this solve anything at all? |
262afe11 GP |
9010 | uint64_t status = 0; |
9011 | int notify_token; | |
9012 | if (notify_register_check("com.saurik.Cydia.status", ¬ify_token) == NOTIFY_STATUS_OK) { | |
9013 | notify_get_state(notify_token, &status); | |
9014 | notify_cancel(notify_token); | |
9015 | } | |
9016 | ||
5927fe03 JF |
9017 | if (status != 0) { |
9018 | #if !ForRelease | |
9019 | NSLog(@"isSafeToSuspend: status != 0"); | |
9020 | #endif | |
9021 | return false; | |
9022 | } | |
9023 | ||
9024 | #if !ForRelease | |
9025 | NSLog(@"isSafeToSuspend: -> true"); | |
9026 | #endif | |
9027 | return true; | |
235f5487 JF |
9028 | } |
9029 | ||
9030 | - (void) applicationSuspend:(__GSEvent *)event { | |
9031 | if ([self isSafeToSuspend]) | |
bd150f54 | 9032 | [super applicationSuspend:event]; |
bd150f54 JF |
9033 | } |
9034 | ||
6d9712c4 | 9035 | - (void) _animateSuspension:(BOOL)arg0 duration:(double)arg1 startTime:(double)arg2 scale:(float)arg3 { |
235f5487 | 9036 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9037 | [super _animateSuspension:arg0 duration:arg1 startTime:arg2 scale:arg3]; |
9038 | } | |
9039 | ||
9040 | - (void) _setSuspended:(BOOL)value { | |
235f5487 | 9041 | if ([self isSafeToSuspend]) |
6d9712c4 JF |
9042 | [super _setSuspended:value]; |
9043 | } | |
9044 | ||
7b0ce2da | 9045 | - (UIProgressHUD *) addProgressHUD { |
d061f4ba | 9046 | UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window_] autorelease]); |
04fe1349 JF |
9047 | [hud setAutoresizingMask:UIViewAutoresizingFlexibleBoth]; |
9048 | ||
d061f4ba | 9049 | [window_ setUserInteractionEnabled:NO]; |
534e31fc | 9050 | |
f36e5eac JF |
9051 | UIViewController *target(tabbar_); |
9052 | if (UIViewController *modal = [target modalViewController]) | |
9053 | target = modal; | |
9054 | ||
9055 | UIView *view([target view]); | |
9056 | [view addSubview:hud]; | |
9057 | ||
9058 | [hud show:YES]; | |
534e31fc | 9059 | |
235f5487 | 9060 | ++locked_; |
7b0ce2da JF |
9061 | return hud; |
9062 | } | |
9063 | ||
d061f4ba | 9064 | - (void) removeProgressHUD:(UIProgressHUD *)hud { |
f36e5eac | 9065 | --locked_; |
d061f4ba JF |
9066 | [hud show:NO]; |
9067 | [hud removeFromSuperview]; | |
9068 | [window_ setUserInteractionEnabled:YES]; | |
9069 | } | |
9070 | ||
cd79e8cf | 9071 | - (CyteViewController *) pageForPackage:(NSString *)name { |
57e8b225 | 9072 | return [[[CYPackageController alloc] initWithDatabase:database_ forPackage:name] autorelease]; |
c390d3ab JF |
9073 | } |
9074 | ||
cd79e8cf | 9075 | - (CyteViewController *) pageForURL:(NSURL *)url forExternal:(BOOL)external { |
e47c4742 | 9076 | NSString *scheme([[url scheme] lowercaseString]); |
f6e13561 | 9077 | if ([[url absoluteString] length] <= [scheme length] + 3) |
e47c4742 | 9078 | return nil; |
f6e13561 GP |
9079 | NSString *path([[url absoluteString] substringFromIndex:[scheme length] + 3]); |
9080 | NSArray *components([path pathComponents]); | |
9081 | ||
f5a17517 | 9082 | if ([scheme isEqualToString:@"apptapp"] && [components count] > 0 && [[components objectAtIndex:0] isEqualToString:@"package"]) |
f6e13561 GP |
9083 | return [self pageForPackage:[components objectAtIndex:1]]; |
9084 | ||
9085 | if ([components count] < 1 || ![scheme isEqualToString:@"cydia"]) | |
e47c4742 | 9086 | return nil; |
f6e13561 GP |
9087 | |
9088 | NSString *base([components objectAtIndex:0]); | |
9089 | ||
cd79e8cf | 9090 | CyteViewController *controller = nil; |
f5a17517 | 9091 | |
f70ea899 | 9092 | if ([base isEqualToString:@"url"]) { |
106d645f GP |
9093 | // This kind of URL can contain slashes in the argument, so we can't parse them below. |
9094 | NSString *destination = [[url absoluteString] substringFromIndex:([scheme length] + [@"://" length] + [base length] + [@"/" length])]; | |
a576488f | 9095 | controller = [[[CydiaWebViewController alloc] initWithURL:[NSURL URLWithString:destination]] autorelease]; |
028dbd1c | 9096 | } else if (!external && [components count] == 1) { |
fe8e721f GP |
9097 | if ([base isEqualToString:@"manage"]) { |
9098 | controller = [[[ManageController alloc] init] autorelease]; | |
9099 | } | |
9100 | ||
f6e13561 | 9101 | if ([base isEqualToString:@"sources"]) { |
f5a17517 | 9102 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9103 | } |
9104 | ||
9105 | if ([base isEqualToString:@"home"]) { | |
f5a17517 | 9106 | controller = [[[HomeController alloc] init] autorelease]; |
f6e13561 GP |
9107 | } |
9108 | ||
9109 | if ([base isEqualToString:@"sections"]) { | |
f5a17517 | 9110 | controller = [[[SectionsController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9111 | } |
9112 | ||
9113 | if ([base isEqualToString:@"search"]) { | |
43625891 | 9114 | controller = [[[SearchController alloc] initWithDatabase:database_ query:nil] autorelease]; |
f6e13561 GP |
9115 | } |
9116 | ||
9117 | if ([base isEqualToString:@"changes"]) { | |
ea3bb538 | 9118 | controller = [[[ChangesController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9119 | } |
9120 | ||
9121 | if ([base isEqualToString:@"installed"]) { | |
f5a17517 | 9122 | controller = [[[InstalledController alloc] initWithDatabase:database_] autorelease]; |
f6e13561 GP |
9123 | } |
9124 | } else if ([components count] == 2) { | |
9125 | NSString *argument = [components objectAtIndex:1]; | |
9126 | ||
9127 | if ([base isEqualToString:@"package"]) { | |
f5a17517 | 9128 | controller = [self pageForPackage:argument]; |
f6e13561 GP |
9129 | } |
9130 | ||
028dbd1c | 9131 | if (!external && [base isEqualToString:@"search"]) { |
43625891 | 9132 | controller = [[[SearchController alloc] initWithDatabase:database_ query:argument] autorelease]; |
f6e13561 GP |
9133 | } |
9134 | ||
028dbd1c | 9135 | if (!external && [base isEqualToString:@"sections"]) { |
f6e13561 GP |
9136 | if ([argument isEqualToString:@"all"]) |
9137 | argument = nil; | |
f5a17517 | 9138 | controller = [[[SectionController alloc] initWithDatabase:database_ section:argument] autorelease]; |
f6e13561 GP |
9139 | } |
9140 | ||
028dbd1c | 9141 | if (!external && [base isEqualToString:@"sources"]) { |
f6e13561 | 9142 | if ([argument isEqualToString:@"add"]) { |
f5a17517 GP |
9143 | controller = [[[SourcesController alloc] initWithDatabase:database_] autorelease]; |
9144 | [(SourcesController *)controller showAddSourcePrompt]; | |
f6e13561 | 9145 | } else { |
d669236d GP |
9146 | Source *source = [database_ sourceWithKey:argument]; |
9147 | controller = [[[SourceController alloc] initWithDatabase:database_ source:source] autorelease]; | |
f6e13561 GP |
9148 | } |
9149 | } | |
9150 | ||
028dbd1c | 9151 | if (!external && [base isEqualToString:@"launch"]) { |
f6e13561 | 9152 | [self launchApplicationWithIdentifier:argument suspended:NO]; |
f5a17517 | 9153 | return nil; |
f6e13561 | 9154 | } |
028dbd1c | 9155 | } else if (!external && [components count] == 3) { |
f6e13561 GP |
9156 | NSString *arg1 = [components objectAtIndex:1]; |
9157 | NSString *arg2 = [components objectAtIndex:2]; | |
9158 | ||
9159 | if ([base isEqualToString:@"package"]) { | |
9160 | if ([arg2 isEqualToString:@"settings"]) { | |
f5a17517 | 9161 | controller = [[[PackageSettingsController alloc] initWithDatabase:database_ package:arg1] autorelease]; |
f6e13561 GP |
9162 | } else if ([arg2 isEqualToString:@"files"]) { |
9163 | if (Package *package = [database_ packageWithName:arg1]) { | |
f5a17517 GP |
9164 | controller = [[[FileTable alloc] initWithDatabase:database_] autorelease]; |
9165 | [(FileTable *)controller setPackage:package]; | |
f6e13561 GP |
9166 | } |
9167 | } | |
c390d3ab JF |
9168 | } |
9169 | } | |
9170 | ||
f5a17517 GP |
9171 | [controller setDelegate:self]; |
9172 | return controller; | |
c390d3ab JF |
9173 | } |
9174 | ||
028dbd1c | 9175 | - (BOOL) openCydiaURL:(NSURL *)url forExternal:(BOOL)external { |
cd79e8cf | 9176 | CyteViewController *page([self pageForURL:url forExternal:external]); |
40364973 | 9177 | |
c713af59 | 9178 | if (page != nil) { |
15f0d613 | 9179 | UINavigationController *nav = [[[UINavigationController alloc] init] autorelease]; |
c713af59 | 9180 | [nav setViewControllers:[NSArray arrayWithObject:page]]; |
9f99f3da | 9181 | [tabbar_ setUnselectedViewController:nav]; |
c713af59 | 9182 | } |
40364973 | 9183 | |
f6e13561 | 9184 | return page != nil; |
40364973 GP |
9185 | } |
9186 | ||
c390d3ab JF |
9187 | - (void) applicationOpenURL:(NSURL *)url { |
9188 | [super applicationOpenURL:url]; | |
d817e4de | 9189 | |
7b33d201 JF |
9190 | if (!loaded_) |
9191 | starturl_ = url; | |
9192 | else | |
9193 | [self openCydiaURL:url forExternal:YES]; | |
c390d3ab JF |
9194 | } |
9195 | ||
bc11cf5b | 9196 | - (void) applicationWillResignActive:(UIApplication *)application { |
7eff7ea6 | 9197 | // Stop refreshing if you get a phone call or lock the device. |
7585ce66 JF |
9198 | if ([tabbar_ updating]) |
9199 | [tabbar_ cancelUpdate]; | |
bc11cf5b | 9200 | |
ca584c15 GP |
9201 | if ([[self superclass] instancesRespondToSelector:@selector(applicationWillResignActive:)]) |
9202 | [super applicationWillResignActive:application]; | |
7eff7ea6 GP |
9203 | } |
9204 | ||
fe8e721f | 9205 | - (void) applicationWillTerminate:(UIApplication *)application { |
35f0a3b5 GP |
9206 | Changed_ = true; |
9207 | [Metadata_ setObject:[tabbar_ navigationURLCollection] forKey:@"InterfaceState"]; | |
fe8e721f GP |
9208 | [Metadata_ setObject:[NSDate date] forKey:@"LastClosed"]; |
9209 | [Metadata_ setObject:[NSNumber numberWithInt:[tabbar_ selectedIndex]] forKey:@"InterfaceIndex"]; | |
9210 | ||
9211 | [self _saveConfig]; | |
9212 | } | |
9213 | ||
6915b806 JF |
9214 | - (void) setConfigurationData:(NSString *)data { |
9215 | static Pcre conffile_r("^'(.*)' '(.*)' ([01]) ([01])$"); | |
9216 | ||
9217 | if (!conffile_r(data)) { | |
9218 | lprintf("E:invalid conffile\n"); | |
9219 | return; | |
9220 | } | |
9221 | ||
9222 | NSString *ofile = conffile_r[1]; | |
9223 | //NSString *nfile = conffile_r[2]; | |
9224 | ||
9225 | UIAlertView *alert = [[[UIAlertView alloc] | |
9226 | initWithTitle:UCLocalize("CONFIGURATION_UPGRADE") | |
9227 | message:[NSString stringWithFormat:@"%@\n\n%@", UCLocalize("CONFIGURATION_UPGRADE_EX"), ofile] | |
9228 | delegate:self | |
9229 | cancelButtonTitle:UCLocalize("KEEP_OLD_COPY") | |
9230 | otherButtonTitles: | |
9231 | UCLocalize("ACCEPT_NEW_COPY"), | |
9232 | // XXX: UCLocalize("SEE_WHAT_CHANGED"), | |
9233 | nil | |
9234 | ] autorelease]; | |
9235 | ||
9236 | [alert setContext:@"conffile"]; | |
a08145a8 | 9237 | [alert setNumberOfRows:2]; |
6915b806 JF |
9238 | [alert show]; |
9239 | } | |
9240 | ||
f3e11d24 | 9241 | - (void) addStashController { |
235f5487 | 9242 | ++locked_; |
7b33d201 | 9243 | stash_ = [[[StashController alloc] init] autorelease]; |
f3e11d24 GP |
9244 | [window_ addSubview:[stash_ view]]; |
9245 | } | |
9246 | ||
9247 | - (void) removeStashController { | |
9248 | [[stash_ view] removeFromSuperview]; | |
7b33d201 | 9249 | stash_ = nil; |
235f5487 | 9250 | --locked_; |
f3e11d24 GP |
9251 | } |
9252 | ||
9253 | - (void) stash { | |
9254 | [self setIdleTimerDisabled:YES]; | |
9255 | ||
9e1f1e91 | 9256 | [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque]; |
f3e11d24 | 9257 | UpdateExternalStatus(1); |
f3e11d24 | 9258 | [self yieldToSelector:@selector(system:) withObject:@"/usr/libexec/cydia/free.sh"]; |
f3e11d24 | 9259 | UpdateExternalStatus(0); |
f3e11d24 GP |
9260 | |
9261 | [self removeStashController]; | |
9262 | ||
9263 | if (ExecFork() == 0) { | |
9264 | execlp("launchctl", "launchctl", "stop", "com.apple.SpringBoard", NULL); | |
9265 | perror("launchctl stop"); | |
9266 | } | |
9267 | } | |
9268 | ||
f6e13561 | 9269 | - (void) setupViewControllers { |
7b33d201 | 9270 | tabbar_ = [[[CYTabBarController alloc] initWithDatabase:database_] autorelease]; |
851f4a99 GP |
9271 | |
9272 | NSMutableArray *items([NSMutableArray arrayWithObjects: | |
89571a5b GP |
9273 | [[[UITabBarItem alloc] initWithTitle:@"Cydia" image:[UIImage applicationImageNamed:@"home.png"] tag:0] autorelease], |
9274 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SECTIONS") image:[UIImage applicationImageNamed:@"install.png"] tag:0] autorelease], | |
9275 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("CHANGES") image:[UIImage applicationImageNamed:@"changes.png"] tag:0] autorelease], | |
9276 | [[[UITabBarItem alloc] initWithTitle:UCLocalize("SEARCH") image:[UIImage applicationImageNamed:@"search.png"] tag:0] autorelease], | |
f6e13561 GP |
9277 | nil]); |
9278 | ||
851f4a99 | 9279 | if (IsWildcat_) { |
89571a5b GP |
9280 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("SOURCES") image:[UIImage applicationImageNamed:@"source.png"] tag:0] autorelease] atIndex:3]; |
9281 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("INSTALLED") image:[UIImage applicationImageNamed:@"manage.png"] tag:0] autorelease] atIndex:3]; | |
851f4a99 | 9282 | } else { |
89571a5b | 9283 | [items insertObject:[[[UITabBarItem alloc] initWithTitle:UCLocalize("MANAGE") image:[UIImage applicationImageNamed:@"manage.png"] tag:0] autorelease] atIndex:3]; |
851f4a99 GP |
9284 | } |
9285 | ||
9286 | NSMutableArray *controllers([NSMutableArray array]); | |
851f4a99 | 9287 | for (UITabBarItem *item in items) { |
15f0d613 | 9288 | UINavigationController *controller([[[UINavigationController alloc] init] autorelease]); |
851f4a99 GP |
9289 | [controller setTabBarItem:item]; |
9290 | [controllers addObject:controller]; | |
9291 | } | |
851f4a99 | 9292 | [tabbar_ setViewControllers:controllers]; |
4305896c | 9293 | |
f6e13561 | 9294 | [tabbar_ setUpdateDelegate:self]; |
851f4a99 GP |
9295 | } |
9296 | ||
bd150f54 | 9297 | - (void) applicationDidFinishLaunching:(id)unused { |
7e30ba6d | 9298 | _trace(); |
1e94d48b JF |
9299 | if ([self respondsToSelector:@selector(setApplicationSupportsShakeToEdit:)]) |
9300 | [self setApplicationSupportsShakeToEdit:NO]; | |
9301 | ||
48f1762f JF |
9302 | @synchronized (HostConfig_) { |
9303 | [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]]; | |
9304 | } | |
3171f7fe | 9305 | |
a06e9179 | 9306 | [NSURLCache setSharedURLCache:[[[CYURLCache alloc] |
71cc7be1 JF |
9307 | initWithMemoryCapacity:524288 |
9308 | diskCapacity:10485760 | |
9309 | diskPath:[NSString stringWithFormat:@"%@/Library/Caches/com.saurik.Cydia/SDURLCache", @"/var/root"] | |
9310 | ] autorelease]]; | |
9311 | ||
a576488f | 9312 | [CydiaWebViewController _initialize]; |
ea173384 | 9313 | |
bfc87a4d JF |
9314 | [NSURLProtocol registerClass:[CydiaURLProtocol class]]; |
9315 | ||
793aee35 JF |
9316 | // this would disallow http{,s} URLs from accessing this data |
9317 | //[WebView registerURLSchemeAsLocal:@"cydia"]; | |
9318 | ||
7b33d201 JF |
9319 | Font12_ = [UIFont systemFontOfSize:12]; |
9320 | Font12Bold_ = [UIFont boldSystemFontOfSize:12]; | |
9321 | Font14_ = [UIFont systemFontOfSize:14]; | |
9322 | Font18Bold_ = [UIFont boldSystemFontOfSize:18]; | |
9323 | Font22Bold_ = [UIFont boldSystemFontOfSize:22]; | |
f641a0e5 | 9324 | |
7b33d201 JF |
9325 | essential_ = [NSMutableArray arrayWithCapacity:4]; |
9326 | broken_ = [NSMutableArray arrayWithCapacity:4]; | |
bd150f54 | 9327 | |
4e89e880 | 9328 | // XXX: I really need this thing... like, seriously... I'm sorry |
a576488f | 9329 | [[[CydiaWebViewController alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/appcache/", UI_]]] reloadData]; |
4e89e880 | 9330 | |
7b33d201 | 9331 | window_ = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
f641a0e5 JF |
9332 | [window_ orderFront:self]; |
9333 | [window_ makeKey:self]; | |
c390d3ab | 9334 | [window_ setHidden:NO]; |
04fe1349 | 9335 | |
f3e11d24 GP |
9336 | if ( |
9337 | readlink("/Applications", NULL, 0) == -1 && errno == EINVAL || | |
9338 | readlink("/Library/Ringtones", NULL, 0) == -1 && errno == EINVAL || | |
9339 | readlink("/Library/Wallpaper", NULL, 0) == -1 && errno == EINVAL || | |
9340 | //readlink("/usr/bin", NULL, 0) == -1 && errno == EINVAL || | |
9341 | readlink("/usr/include", NULL, 0) == -1 && errno == EINVAL || | |
9342 | readlink("/usr/lib/pam", NULL, 0) == -1 && errno == EINVAL || | |
9343 | readlink("/usr/libexec", NULL, 0) == -1 && errno == EINVAL || | |
9344 | readlink("/usr/share", NULL, 0) == -1 && errno == EINVAL || | |
9345 | //readlink("/var/lib", NULL, 0) == -1 && errno == EINVAL || | |
9346 | false | |
9347 | ) { | |
9348 | [self addStashController]; | |
3931b718 JF |
9349 | // XXX: this would be much cleaner as a yieldToSelector: |
9350 | // that way the removeStashController could happen right here inline | |
9351 | // we also could no longer require the useless stash_ field anymore | |
f3e11d24 GP |
9352 | [self performSelector:@selector(stash) withObject:nil afterDelay:0]; |
9353 | return; | |
9354 | } | |
9355 | ||
770f2a8e | 9356 | database_ = [Database sharedInstance]; |
6915b806 | 9357 | [database_ setDelegate:self]; |
bfc87a4d | 9358 | |
89571a5b | 9359 | [window_ setUserInteractionEnabled:NO]; |
0be165c8 | 9360 | [self setupViewControllers]; |
6915b806 | 9361 | |
70750ab3 | 9362 | emulated_ = [[[CydiaLoadingViewController alloc] init] autorelease]; |
6915b806 | 9363 | [window_ addSubview:[emulated_ view]]; |
5ccb47d8 | 9364 | |
fed0d010 | 9365 | [self performSelector:@selector(loadData) withObject:nil afterDelay:0]; |
c626a63f | 9366 | _trace(); |
fed0d010 JF |
9367 | } |
9368 | ||
d3a28a81 GP |
9369 | - (NSArray *) defaultStartPages { |
9370 | NSMutableArray *standard = [NSMutableArray array]; | |
9371 | [standard addObject:[NSArray arrayWithObject:@"cydia://home"]]; | |
9372 | [standard addObject:[NSArray arrayWithObject:@"cydia://sections"]]; | |
9373 | [standard addObject:[NSArray arrayWithObject:@"cydia://changes"]]; | |
9374 | if (!IsWildcat_) { | |
9375 | [standard addObject:[NSArray arrayWithObject:@"cydia://manage"]]; | |
9376 | } else { | |
9377 | [standard addObject:[NSArray arrayWithObject:@"cydia://installed"]]; | |
9378 | [standard addObject:[NSArray arrayWithObject:@"cydia://sources"]]; | |
9379 | } | |
9380 | [standard addObject:[NSArray arrayWithObject:@"cydia://search"]]; | |
9381 | return standard; | |
9382 | } | |
9383 | ||
fed0d010 | 9384 | - (void) loadData { |
c626a63f | 9385 | _trace(); |
fed0d010 | 9386 | if (Role_ == nil) { |
0c1cb67a | 9387 | [window_ setUserInteractionEnabled:YES]; |
a54fed5b | 9388 | [self showSettings]; |
fed0d010 | 9389 | return; |
0c1cb67a | 9390 | } else { |
6915b806 JF |
9391 | if ([emulated_ modalViewController] != nil) |
9392 | [emulated_ dismissModalViewControllerAnimated:YES]; | |
0c1cb67a | 9393 | [window_ setUserInteractionEnabled:NO]; |
fed0d010 JF |
9394 | } |
9395 | ||
851f4a99 | 9396 | [self reloadData]; |
670a0494 | 9397 | PrintTimes(); |
5ccb47d8 | 9398 | |
2e9123cb | 9399 | [self disemulate]; |
5ccb47d8 | 9400 | |
d3a28a81 GP |
9401 | int savedIndex = [[Metadata_ objectForKey:@"InterfaceIndex"] intValue]; |
9402 | NSArray *saved = [[Metadata_ objectForKey:@"InterfaceState"] mutableCopy]; | |
9403 | int standardIndex = 0; | |
9404 | NSArray *standard = [self defaultStartPages]; | |
fe8e721f | 9405 | |
d3a28a81 GP |
9406 | BOOL valid = YES; |
9407 | ||
9408 | if (saved == nil) | |
9409 | valid = NO; | |
9410 | ||
9411 | NSDate *closed = [Metadata_ objectForKey:@"LastClosed"]; | |
9412 | if (valid && closed != nil) { | |
fe8e721f GP |
9413 | NSTimeInterval interval([closed timeIntervalSinceNow]); |
9414 | // XXX: Is 15 minutes the optimal time here? | |
d3a28a81 GP |
9415 | if (interval > 0 && interval <= -(15*60)) |
9416 | valid = NO; | |
fe8e721f GP |
9417 | } |
9418 | ||
d3a28a81 GP |
9419 | if (valid && [saved count] != [standard count]) |
9420 | valid = NO; | |
efa53fa9 | 9421 | |
d3a28a81 GP |
9422 | if (valid) { |
9423 | for (unsigned int i = 0; i < [standard count]; i++) { | |
9424 | NSArray *std = [standard objectAtIndex:i], *sav = [saved objectAtIndex:i]; | |
9425 | // XXX: The "hasPrefix" sanity check here could be, in theory, fooled, | |
9426 | // but it's good enough for now. | |
9427 | if ([sav count] == 0 || ![[sav objectAtIndex:0] hasPrefix:[std objectAtIndex:0]]) { | |
9428 | valid = NO; | |
9429 | break; | |
9430 | } | |
fe8e721f | 9431 | } |
fe8e721f GP |
9432 | } |
9433 | ||
d3a28a81 GP |
9434 | NSArray *items = nil; |
9435 | if (valid) { | |
9436 | [tabbar_ setSelectedIndex:savedIndex]; | |
9437 | items = saved; | |
9438 | } else { | |
9439 | [tabbar_ setSelectedIndex:standardIndex]; | |
9440 | items = standard; | |
9441 | } | |
9442 | ||
fe8e721f GP |
9443 | for (unsigned int tab = 0; tab < [[tabbar_ viewControllers] count]; tab++) { |
9444 | NSArray *stack = [items objectAtIndex:tab]; | |
15f0d613 | 9445 | UINavigationController *navigation = [[tabbar_ viewControllers] objectAtIndex:tab]; |
fe8e721f GP |
9446 | NSMutableArray *current = [NSMutableArray array]; |
9447 | ||
9448 | for (unsigned int nav = 0; nav < [stack count]; nav++) { | |
9449 | NSString *addr = [stack objectAtIndex:nav]; | |
9450 | NSURL *url = [NSURL URLWithString:addr]; | |
cd79e8cf | 9451 | CyteViewController *page = [self pageForURL:url forExternal:NO]; |
fe8e721f GP |
9452 | if (page != nil) |
9453 | [current addObject:page]; | |
9454 | } | |
9455 | ||
9456 | [navigation setViewControllers:current]; | |
9457 | } | |
f6e13561 | 9458 | |
89571a5b | 9459 | // (Try to) show the startup URL. |
f6e13561 | 9460 | if (starturl_ != nil) { |
028dbd1c | 9461 | [self openCydiaURL:starturl_ forExternal:NO]; |
f6e13561 GP |
9462 | starturl_ = nil; |
9463 | } | |
bd150f54 JF |
9464 | } |
9465 | ||
b5e7eebb | 9466 | - (void) showActionSheet:(UIActionSheet *)sheet fromItem:(UIBarButtonItem *)item { |
674dce72 | 9467 | if (item != nil && IsWildcat_) { |
b5e7eebb | 9468 | [sheet showFromBarButtonItem:item animated:YES]; |
674dce72 GP |
9469 | } else { |
9470 | [sheet showInView:window_]; | |
9471 | } | |
36bb2ca2 JF |
9472 | } |
9473 | ||
6915b806 JF |
9474 | - (void) addProgressEvent:(CydiaProgressEvent *)event forTask:(NSString *)task { |
9475 | id<ProgressDelegate> progress([database_ progressDelegate] ?: [self invokeNewProgress:nil forController:nil withTitle:task]); | |
9476 | [progress setTitle:task]; | |
9477 | [progress addProgressEvent:event]; | |
9478 | } | |
9479 | ||
9480 | - (void) addProgressEventForTask:(NSArray *)data { | |
9481 | CydiaProgressEvent *event([data objectAtIndex:0]); | |
9482 | NSString *task([data count] < 2 ? nil : [data objectAtIndex:1]); | |
9483 | [self addProgressEvent:event forTask:task]; | |
9484 | } | |
9485 | ||
9486 | - (void) addProgressEventOnMainThread:(CydiaProgressEvent *)event forTask:(NSString *)task { | |
9487 | [self performSelectorOnMainThread:@selector(addProgressEventForTask:) withObject:[NSArray arrayWithObjects:event, task, nil] waitUntilDone:YES]; | |
9488 | } | |
9489 | ||
dc5812ec JF |
9490 | @end |
9491 | ||
b4d89997 JF |
9492 | /*IMP alloc_; |
9493 | id Alloc_(id self, SEL selector) { | |
9494 | id object = alloc_(self, selector); | |
c390d3ab | 9495 | lprintf("[%s]A-%p\n", self->isa->name, object); |
b4d89997 JF |
9496 | return object; |
9497 | }*/ | |
9498 | ||
36bb2ca2 JF |
9499 | /*IMP dealloc_; |
9500 | id Dealloc_(id self, SEL selector) { | |
9501 | id object = dealloc_(self, selector); | |
c390d3ab | 9502 | lprintf("[%s]D-%p\n", self->isa->name, object); |
36bb2ca2 JF |
9503 | return object; |
9504 | }*/ | |
b4d89997 | 9505 | |
f79a4512 JF |
9506 | Class $WebDefaultUIKitDelegate; |
9507 | ||
d791dce4 | 9508 | MSHook(void, UIWebDocumentView$_setUIKitDelegate$, UIWebDocumentView *self, SEL _cmd, id delegate) { |
f79a4512 JF |
9509 | if (delegate == nil && $WebDefaultUIKitDelegate != nil) |
9510 | delegate = [$WebDefaultUIKitDelegate sharedUIKitDelegate]; | |
d791dce4 | 9511 | return _UIWebDocumentView$_setUIKitDelegate$(self, _cmd, delegate); |
f79a4512 JF |
9512 | } |
9513 | ||
baf82ed4 | 9514 | static NSSet *MobilizedFiles_; |
10d65752 | 9515 | |
baf82ed4 JF |
9516 | static NSURL *MobilizeURL(NSURL *url) { |
9517 | NSString *path([url path]); | |
9518 | if ([path hasPrefix:@"/var/root/"]) { | |
9519 | NSString *file([path substringFromIndex:10]); | |
9520 | if ([MobilizedFiles_ containsObject:file]) | |
9521 | url = [NSURL fileURLWithPath:[@"/var/mobile/" stringByAppendingString:file] isDirectory:NO]; | |
10d65752 | 9522 | } |
79b42fd1 | 9523 | |
baf82ed4 JF |
9524 | return url; |
9525 | } | |
79b42fd1 | 9526 | |
baf82ed4 JF |
9527 | Class $CFXPreferencesPropertyListSource; |
9528 | @class CFXPreferencesPropertyListSource; | |
79b42fd1 | 9529 | |
baf82ed4 JF |
9530 | MSHook(BOOL, CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync, CFXPreferencesPropertyListSource *self, SEL _cmd) { |
9531 | NSURL *&url(MSHookIvar<NSURL *>(self, "_url")), *old(url); | |
9532 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9533 | url = MobilizeURL(url); | |
9534 | BOOL value(_CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync(self, _cmd)); | |
9535 | //NSLog(@"%@ %s", [url absoluteString], value ? "YES" : "NO"); | |
9536 | url = old; | |
9537 | [pool release]; | |
9538 | return value; | |
9539 | } | |
79b42fd1 | 9540 | |
baf82ed4 JF |
9541 | MSHook(void *, CFXPreferencesPropertyListSource$createPlistFromDisk, CFXPreferencesPropertyListSource *self, SEL _cmd) { |
9542 | NSURL *&url(MSHookIvar<NSURL *>(self, "_url")), *old(url); | |
9543 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9544 | url = MobilizeURL(url); | |
9545 | void *value(_CFXPreferencesPropertyListSource$createPlistFromDisk(self, _cmd)); | |
9546 | //NSLog(@"%@ %@", [url absoluteString], value); | |
9547 | url = old; | |
9548 | [pool release]; | |
9549 | return value; | |
79b42fd1 GP |
9550 | } |
9551 | ||
30c5be06 JF |
9552 | Class $NSURLConnection; |
9553 | ||
9554 | MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) { | |
9555 | NSMutableURLRequest *copy([request mutableCopy]); | |
9556 | ||
9557 | NSURL *url([copy URL]); | |
5e845121 JF |
9558 | |
9559 | NSString *href([url absoluteString]); | |
30c5be06 | 9560 | NSString *host([url host]); |
e4b48f2f JF |
9561 | NSString *scheme([[url scheme] lowercaseString]); |
9562 | ||
9563 | NSString *compound([NSString stringWithFormat:@"%@:%@", scheme, host]); | |
30c5be06 | 9564 | |
48f1762f JF |
9565 | @synchronized (HostConfig_) { |
9566 | if ([copy respondsToSelector:@selector(setHTTPShouldUsePipelining:)]) | |
9567 | if ([PipelinedHosts_ containsObject:host] || [PipelinedHosts_ containsObject:compound]) | |
9568 | [copy setHTTPShouldUsePipelining:YES]; | |
5e845121 JF |
9569 | |
9570 | if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"]) | |
9571 | if ([control isEqualToString:@"max-age=0"]) | |
9572 | if ([CachedURLs_ containsObject:href]) { | |
9573 | #if !ForRelease | |
9574 | NSLog(@"~~~: %@", href); | |
9575 | #endif | |
9576 | ||
9577 | [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; | |
9578 | ||
9579 | [copy setValue:nil forHTTPHeaderField:@"Cache-Control"]; | |
9580 | [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"]; | |
9581 | [copy setValue:nil forHTTPHeaderField:@"If-None-Match"]; | |
9582 | } | |
48f1762f | 9583 | } |
30c5be06 JF |
9584 | |
9585 | if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) { | |
9586 | } return self; | |
9587 | } | |
9588 | ||
d13edf44 JF |
9589 | int main(int argc, char *argv[]) { |
9590 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
9591 | ||
d6dad1b4 | 9592 | _trace(); |
f79a4512 | 9593 | |
9a60abe5 JF |
9594 | UpdateExternalStatus(0); |
9595 | ||
01d93940 JF |
9596 | if (Class $UIDevice = objc_getClass("UIDevice")) { |
9597 | UIDevice *device([$UIDevice currentDevice]); | |
9598 | IsWildcat_ = [device respondsToSelector:@selector(isWildcat)] && [device isWildcat]; | |
9599 | } else | |
9600 | IsWildcat_ = false; | |
9601 | ||
57e8b225 JF |
9602 | UIScreen *screen([UIScreen mainScreen]); |
9603 | if ([screen respondsToSelector:@selector(scale)]) | |
9604 | ScreenScale_ = [screen scale]; | |
9605 | else | |
9606 | ScreenScale_ = 1; | |
9607 | ||
c138614d JF |
9608 | UIDevice *device([UIDevice currentDevice]); |
9609 | if (![device respondsToSelector:@selector(userInterfaceIdiom)]) | |
9610 | Idiom_ = @"iphone"; | |
9611 | else { | |
9612 | UIUserInterfaceIdiom idiom([device userInterfaceIdiom]); | |
9613 | if (idiom == UIUserInterfaceIdiomPhone) | |
9614 | Idiom_ = @"iphone"; | |
9615 | else if (idiom == UIUserInterfaceIdiomPad) | |
9616 | Idiom_ = @"ipad"; | |
9617 | else | |
9618 | NSLog(@"unknown UIUserInterfaceIdiom!"); | |
9619 | } | |
9620 | ||
7b33d201 | 9621 | SessionData_ = [NSMutableDictionary dictionaryWithCapacity:4]; |
ef974f52 | 9622 | |
7b33d201 | 9623 | HostConfig_ = [[[NSObject alloc] init] autorelease]; |
48f1762f JF |
9624 | @synchronized (HostConfig_) { |
9625 | BridgedHosts_ = [NSMutableSet setWithCapacity:4]; | |
9626 | PipelinedHosts_ = [NSMutableSet setWithCapacity:4]; | |
5e845121 | 9627 | CachedURLs_ = [NSMutableSet setWithCapacity:32]; |
48f1762f | 9628 | } |
5df7ecfb | 9629 | |
c138614d | 9630 | UI_ = CydiaURL([NSString stringWithFormat:@"ui/ios~%@", Idiom_]); |
57e8b225 | 9631 | |
df213583 | 9632 | PackageName = reinterpret_cast<CYString &(*)(Package *, SEL)>(method_getImplementation(class_getInstanceMethod([Package class], @selector(cyname)))); |
677b8415 | 9633 | |
baf82ed4 JF |
9634 | MobilizedFiles_ = [NSMutableSet setWithObjects: |
9635 | @"Library/Preferences/com.apple.Accessibility.plist", | |
9636 | @"Library/Preferences/com.apple.preferences.sounds.plist", | |
9637 | nil]; | |
9638 | ||
7376b55c JF |
9639 | /* Library Hacks {{{ */ |
9640 | class_addMethod(objc_getClass("DOMNodeList"), @selector(countByEnumeratingWithState:objects:count:), (IMP) &DOMNodeList$countByEnumeratingWithState$objects$count$, "I20@0:4^{NSFastEnumerationState}8^@12I16"); | |
9641 | ||
baf82ed4 JF |
9642 | $CFXPreferencesPropertyListSource = objc_getClass("CFXPreferencesPropertyListSource"); |
9643 | ||
9644 | Method CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync(class_getInstanceMethod($CFXPreferencesPropertyListSource, @selector(_backingPlistChangedSinceLastSync))); | |
9645 | if (CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync != NULL) { | |
9646 | _CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync = reinterpret_cast<BOOL (*)(CFXPreferencesPropertyListSource *, SEL)>(method_getImplementation(CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync)); | |
9647 | method_setImplementation(CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync, reinterpret_cast<IMP>(&$CFXPreferencesPropertyListSource$_backingPlistChangedSinceLastSync)); | |
9648 | } | |
9649 | ||
9650 | Method CFXPreferencesPropertyListSource$createPlistFromDisk(class_getInstanceMethod($CFXPreferencesPropertyListSource, @selector(createPlistFromDisk))); | |
9651 | if (CFXPreferencesPropertyListSource$createPlistFromDisk != NULL) { | |
9652 | _CFXPreferencesPropertyListSource$createPlistFromDisk = reinterpret_cast<void *(*)(CFXPreferencesPropertyListSource *, SEL)>(method_getImplementation(CFXPreferencesPropertyListSource$createPlistFromDisk)); | |
9653 | method_setImplementation(CFXPreferencesPropertyListSource$createPlistFromDisk, reinterpret_cast<IMP>(&$CFXPreferencesPropertyListSource$createPlistFromDisk)); | |
9654 | } | |
9655 | ||
7376b55c JF |
9656 | $WebDefaultUIKitDelegate = objc_getClass("WebDefaultUIKitDelegate"); |
9657 | Method UIWebDocumentView$_setUIKitDelegate$(class_getInstanceMethod([WebView class], @selector(_setUIKitDelegate:))); | |
9658 | if (UIWebDocumentView$_setUIKitDelegate$ != NULL) { | |
9659 | _UIWebDocumentView$_setUIKitDelegate$ = reinterpret_cast<void (*)(UIWebDocumentView *, SEL, id)>(method_getImplementation(UIWebDocumentView$_setUIKitDelegate$)); | |
9660 | method_setImplementation(UIWebDocumentView$_setUIKitDelegate$, reinterpret_cast<IMP>(&$UIWebDocumentView$_setUIKitDelegate$)); | |
9661 | } | |
10d65752 | 9662 | |
30c5be06 JF |
9663 | $NSURLConnection = objc_getClass("NSURLConnection"); |
9664 | Method NSURLConnection$init$(class_getInstanceMethod($NSURLConnection, @selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:))); | |
9665 | if (NSURLConnection$init$ != NULL) { | |
9666 | _NSURLConnection$init$ = reinterpret_cast<id (*)(NSURLConnection *, SEL, NSURLRequest *, id, BOOL, int64_t, BOOL, NSDictionary *)>(method_getImplementation(NSURLConnection$init$)); | |
9667 | method_setImplementation(NSURLConnection$init$, reinterpret_cast<IMP>(&$NSURLConnection$init$)); | |
9668 | } | |
7376b55c JF |
9669 | /* }}} */ |
9670 | /* Set Locale {{{ */ | |
f79a4512 | 9671 | Locale_ = CFLocaleCopyCurrent(); |
b1ce61ec | 9672 | Languages_ = [NSLocale preferredLanguages]; |
25fdc941 | 9673 | |
b1ce61ec | 9674 | //CFStringRef locale(CFLocaleGetIdentifier(Locale_)); |
18876387 | 9675 | //NSLog(@"%@", [Languages_ description]); |
78430d06 | 9676 | |
b1ce61ec | 9677 | const char *lang; |
25fdc941 JF |
9678 | if (Locale_ != NULL) |
9679 | lang = [(NSString *) CFLocaleGetIdentifier(Locale_) UTF8String]; | |
3caee0a4 JF |
9680 | else if (Languages_ != nil && [Languages_ count] != 0) |
9681 | lang = [[Languages_ objectAtIndex:0] UTF8String]; | |
9682 | else | |
78430d06 | 9683 | // XXX: consider just setting to C and then falling through? |
b1ce61ec | 9684 | lang = NULL; |
3caee0a4 JF |
9685 | |
9686 | if (lang != NULL) { | |
9687 | Pcre pattern("^([a-z][a-z])(?:-[A-Za-z]*)?(_[A-Z][A-Z])?$"); | |
9688 | lang = !pattern(lang) ? NULL : [pattern->*@"%1$@%2$@" UTF8String]; | |
78430d06 JF |
9689 | } |
9690 | ||
b1ce61ec | 9691 | NSLog(@"Setting Language: %s", lang); |
3caee0a4 JF |
9692 | |
9693 | if (lang != NULL) { | |
9694 | setenv("LANG", lang, true); | |
9695 | std::setlocale(LC_ALL, lang); | |
9696 | } | |
7376b55c | 9697 | /* }}} */ |
f79a4512 | 9698 | |
d791dce4 | 9699 | apr_app_initialize(&argc, const_cast<const char * const **>(&argv), NULL); |
f79a4512 | 9700 | |
7376b55c | 9701 | /* Parse Arguments {{{ */ |
de3b1ab4 JF |
9702 | bool substrate(false); |
9703 | ||
9704 | if (argc != 0) { | |
9705 | char **args(argv); | |
9706 | int arge(1); | |
9707 | ||
9708 | for (int argi(1); argi != argc; ++argi) | |
9709 | if (strcmp(argv[argi], "--") == 0) { | |
9710 | arge = argi; | |
9711 | argv[argi] = argv[0]; | |
9712 | argv += argi; | |
9713 | argc -= argi; | |
9714 | break; | |
9715 | } | |
9716 | ||
9717 | for (int argi(1); argi != arge; ++argi) | |
d791dce4 | 9718 | if (strcmp(args[argi], "--substrate") == 0) |
de3b1ab4 JF |
9719 | substrate = true; |
9720 | else | |
9721 | fprintf(stderr, "unknown argument: %s\n", args[argi]); | |
9722 | } | |
7376b55c | 9723 | /* }}} */ |
d73cede2 | 9724 | |
7376b55c | 9725 | App_ = [[NSBundle mainBundle] bundlePath]; |
d791dce4 | 9726 | Advanced_ = YES; |
7376b55c | 9727 | |
b4d89997 JF |
9728 | setuid(0); |
9729 | setgid(0); | |
9730 | ||
9731 | /*Method alloc = class_getClassMethod([NSObject class], @selector(alloc)); | |
9732 | alloc_ = alloc->method_imp; | |
9733 | alloc->method_imp = (IMP) &Alloc_;*/ | |
9734 | ||
36bb2ca2 JF |
9735 | /*Method dealloc = class_getClassMethod([NSObject class], @selector(dealloc)); |
9736 | dealloc_ = dealloc->method_imp; | |
9737 | dealloc->method_imp = (IMP) &Dealloc_;*/ | |
9738 | ||
d791dce4 | 9739 | /* System Information {{{ */ |
3178d79b | 9740 | size_t size; |
c390d3ab JF |
9741 | |
9742 | int maxproc; | |
9743 | size = sizeof(maxproc); | |
9744 | if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) | |
9745 | perror("sysctlbyname(\"kern.maxproc\", ?)"); | |
9746 | else if (maxproc < 64) { | |
9747 | maxproc = 64; | |
9748 | if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) | |
9749 | perror("sysctlbyname(\"kern.maxproc\", #)"); | |
9750 | } | |
9751 | ||
bfc87a4d JF |
9752 | sysctlbyname("kern.osversion", NULL, &size, NULL, 0); |
9753 | char *osversion = new char[size]; | |
9754 | if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) == -1) | |
9755 | perror("sysctlbyname(\"kern.osversion\", ?)"); | |
9756 | else | |
9757 | System_ = [NSString stringWithUTF8String:osversion]; | |
9758 | ||
3178d79b JF |
9759 | sysctlbyname("hw.machine", NULL, &size, NULL, 0); |
9760 | char *machine = new char[size]; | |
c390d3ab JF |
9761 | if (sysctlbyname("hw.machine", machine, &size, NULL, 0) == -1) |
9762 | perror("sysctlbyname(\"hw.machine\", ?)"); | |
9763 | else | |
9764 | Machine_ = machine; | |
3178d79b | 9765 | |
c31d7cdc JF |
9766 | SerialNumber_ = (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber"); |
9767 | ChipID_ = [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString]; | |
9768 | BBSNum_ = CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false); | |
59dbe296 | 9769 | |
87f46a96 | 9770 | UniqueID_ = [[UIDevice currentDevice] uniqueIdentifier]; |
3178d79b | 9771 | |
567e3972 JF |
9772 | CFStringRef (*$CTSIMSupportCopyMobileSubscriberCountryCode)(CFAllocatorRef); |
9773 | $CTSIMSupportCopyMobileSubscriberCountryCode = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode")); | |
9774 | CFStringRef mcc($CTSIMSupportCopyMobileSubscriberCountryCode == NULL ? NULL : (*$CTSIMSupportCopyMobileSubscriberCountryCode)(kCFAllocatorDefault)); | |
9775 | ||
9776 | CFStringRef (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(CFAllocatorRef); | |
9777 | $CTSIMSupportCopyMobileSubscriberNetworkCode = reinterpret_cast<CFStringRef (*)(CFAllocatorRef)>(dlsym(RTLD_DEFAULT, "CTSIMSupportCopyMobileSubscriberCountryCode")); | |
9778 | CFStringRef mnc($CTSIMSupportCopyMobileSubscriberNetworkCode == NULL ? NULL : (*$CTSIMSupportCopyMobileSubscriberNetworkCode)(kCFAllocatorDefault)); | |
9779 | ||
9780 | if (mcc != NULL && mnc != NULL) | |
9781 | PLMN_ = [NSString stringWithFormat:@"%@%@", mcc, mnc]; | |
9782 | ||
9783 | if (mnc != NULL) | |
9784 | CFRelease(mnc); | |
9785 | if (mcc != NULL) | |
9786 | CFRelease(mcc); | |
9787 | ||
ad5d065e JF |
9788 | if (NSDictionary *system = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"]) |
9789 | Build_ = [system objectForKey:@"ProductBuildVersion"]; | |
3e9c9e85 JF |
9790 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) { |
9791 | Product_ = [info objectForKey:@"SafariProductVersion"]; | |
9792 | Safari_ = [info objectForKey:@"CFBundleVersion"]; | |
9793 | } | |
d791dce4 | 9794 | /* }}} */ |
7376b55c | 9795 | /* Load Database {{{ */ |
d6dad1b4 | 9796 | _trace(); |
f79a4512 JF |
9797 | Metadata_ = [[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]; |
9798 | _trace(); | |
9799 | SectionMap_ = [[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sections" ofType:@"plist"]] autorelease]; | |
d6dad1b4 JF |
9800 | |
9801 | if (Metadata_ == NULL) | |
f79a4512 | 9802 | Metadata_ = [NSMutableDictionary dictionaryWithCapacity:2]; |
6d9712c4 | 9803 | else { |
2bdd73bd | 9804 | Settings_ = [Metadata_ objectForKey:@"Settings"]; |
7b0ce2da | 9805 | |
b4d89997 | 9806 | Packages_ = [Metadata_ objectForKey:@"Packages"]; |
6d9712c4 | 9807 | Sections_ = [Metadata_ objectForKey:@"Sections"]; |
7b0ce2da | 9808 | Sources_ = [Metadata_ objectForKey:@"Sources"]; |
ef055c6c JF |
9809 | |
9810 | Token_ = [Metadata_ objectForKey:@"Token"]; | |
7b0ce2da JF |
9811 | } |
9812 | ||
9813 | if (Settings_ != nil) | |
9814 | Role_ = [Settings_ objectForKey:@"Role"]; | |
9815 | ||
7b0ce2da JF |
9816 | if (Sections_ == nil) { |
9817 | Sections_ = [[[NSMutableDictionary alloc] initWithCapacity:32] autorelease]; | |
9818 | [Metadata_ setObject:Sections_ forKey:@"Sections"]; | |
9819 | } | |
9820 | ||
9821 | if (Sources_ == nil) { | |
9822 | Sources_ = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease]; | |
9823 | [Metadata_ setObject:Sources_ forKey:@"Sources"]; | |
6d9712c4 | 9824 | } |
7376b55c | 9825 | /* }}} */ |
b4d89997 | 9826 | |
94b0b3e5 JF |
9827 | _trace(); |
9828 | MetaFile_.Open("/var/lib/cydia/metadata.cb0"); | |
9829 | _trace(); | |
9830 | ||
9831 | if (Packages_ != nil) { | |
c65611b9 JF |
9832 | bool fail(false); |
9833 | CFDictionaryApplyFunction((CFDictionaryRef) Packages_, &PackageImport, &fail); | |
94b0b3e5 | 9834 | _trace(); |
c65611b9 JF |
9835 | |
9836 | if (!fail) { | |
9837 | [Metadata_ removeObjectForKey:@"Packages"]; | |
9838 | Packages_ = nil; | |
9839 | Changed_ = true; | |
9840 | } | |
94b0b3e5 JF |
9841 | } |
9842 | ||
d791dce4 JF |
9843 | Finishes_ = [NSArray arrayWithObjects:@"return", @"reopen", @"restart", @"reload", @"reboot", nil]; |
9844 | ||
d71f3a07 | 9845 | #define MobileSubstrate_(name) \ |
d13577cf JF |
9846 | if (substrate && access("/Library/MobileSubstrate/DynamicLibraries/" #name ".dylib", F_OK) == 0) { \ |
9847 | void *handle(dlopen("/Library/MobileSubstrate/DynamicLibraries/" #name ".dylib", RTLD_LAZY | RTLD_GLOBAL)); \ | |
9848 | if (handle == NULL) \ | |
9849 | NSLog(@"%s", dlerror()); \ | |
9850 | } | |
d71f3a07 JF |
9851 | |
9852 | MobileSubstrate_(Activator) | |
9853 | MobileSubstrate_(libstatusbar) | |
9854 | MobileSubstrate_(SimulatedKeyEvents) | |
9855 | MobileSubstrate_(WinterBoard) | |
9856 | ||
9dd60d81 JF |
9857 | /*if (substrate && access("/Library/MobileSubstrate/MobileSubstrate.dylib", F_OK) == 0) |
9858 | dlopen("/Library/MobileSubstrate/MobileSubstrate.dylib", RTLD_LAZY | RTLD_GLOBAL);*/ | |
dddbc481 | 9859 | |
01d93940 JF |
9860 | int version([[NSString stringWithContentsOfFile:@"/var/lib/cydia/firmware.ver"] intValue]); |
9861 | ||
3b0f10b0 | 9862 | if (access("/tmp/.cydia.fw", F_OK) == 0) { |
ea173384 | 9863 | unlink("/tmp/.cydia.fw"); |
3b0f10b0 | 9864 | goto firmware; |
a4d8c84e | 9865 | } else if (access("/User", F_OK) != 0 || version < 4) { |
3b0f10b0 | 9866 | firmware: |
d6dad1b4 | 9867 | _trace(); |
26c2dd8c | 9868 | system("/usr/libexec/cydia/firmware.sh"); |
d6dad1b4 JF |
9869 | _trace(); |
9870 | } | |
9e98e020 | 9871 | |
87f46a96 JF |
9872 | _assert([[NSFileManager defaultManager] |
9873 | createDirectoryAtPath:@"/var/cache/apt/archives/partial" | |
9874 | withIntermediateDirectories:YES | |
9875 | attributes:nil | |
9876 | error:NULL | |
9877 | ]); | |
9878 | ||
7376b55c JF |
9879 | if (access("/tmp/cydia.chk", F_OK) == 0) { |
9880 | if (unlink("/var/cache/apt/pkgcache.bin") == -1) | |
9881 | _assert(errno == ENOENT); | |
9882 | if (unlink("/var/cache/apt/srcpkgcache.bin") == -1) | |
9883 | _assert(errno == ENOENT); | |
9884 | } | |
9885 | ||
59dbe296 | 9886 | /* APT Initialization {{{ */ |
b1ce61ec JF |
9887 | _assert(pkgInitConfig(*_config)); |
9888 | _assert(pkgInitSystem(*_config, _system)); | |
9889 | ||
9890 | if (lang != NULL) | |
9891 | _config->Set("APT::Acquire::Translation", lang); | |
cb94ff21 JF |
9892 | |
9893 | // XXX: this timeout might be important :( | |
9894 | //_config->Set("Acquire::http::Timeout", 15); | |
9895 | ||
7623f855 | 9896 | _config->Set("Acquire::http::MaxParallel", 3); |
59dbe296 | 9897 | /* }}} */ |
7376b55c | 9898 | /* Color Choices {{{ */ |
36bb2ca2 JF |
9899 | space_ = CGColorSpaceCreateDeviceRGB(); |
9900 | ||
f641a0e5 | 9901 | Blue_.Set(space_, 0.2, 0.2, 1.0, 1.0); |
77fcccaf | 9902 | Blueish_.Set(space_, 0x19/255.f, 0x32/255.f, 0x50/255.f, 1.0); |
36bb2ca2 | 9903 | Black_.Set(space_, 0.0, 0.0, 0.0, 1.0); |
baf80942 | 9904 | Off_.Set(space_, 0.9, 0.9, 0.9, 1.0); |
36bb2ca2 | 9905 | White_.Set(space_, 1.0, 1.0, 1.0, 1.0); |
7b0ce2da | 9906 | Gray_.Set(space_, 0.4, 0.4, 0.4, 1.0); |
3bd1c2a2 JF |
9907 | Green_.Set(space_, 0.0, 0.5, 0.0, 1.0); |
9908 | Purple_.Set(space_, 0.0, 0.0, 0.7, 1.0); | |
9909 | Purplish_.Set(space_, 0.4, 0.4, 0.8, 1.0); | |
59dbe296 | 9910 | |
dc63e78f JF |
9911 | InstallingColor_ = [UIColor colorWithRed:0.88f green:1.00f blue:0.88f alpha:1.00f]; |
9912 | RemovingColor_ = [UIColor colorWithRed:1.00f green:0.88f blue:0.88f alpha:1.00f]; | |
7376b55c | 9913 | /* }}}*/ |
7376b55c | 9914 | /* UIKit Configuration {{{ */ |
f79a4512 JF |
9915 | void (*$GSFontSetUseLegacyFontMetrics)(BOOL)(reinterpret_cast<void (*)(BOOL)>(dlsym(RTLD_DEFAULT, "GSFontSetUseLegacyFontMetrics"))); |
9916 | if ($GSFontSetUseLegacyFontMetrics != NULL) | |
9917 | $GSFontSetUseLegacyFontMetrics(YES); | |
7b0ce2da | 9918 | |
600d005d JF |
9919 | // XXX: I have a feeling this was important |
9920 | //UIKeyboardDisableAutomaticAppearance(); | |
7376b55c | 9921 | /* }}} */ |
87f46a96 | 9922 | |
670a0494 | 9923 | Colon_ = UCLocalize("COLON_DELIMITED"); |
72fb3616 | 9924 | Elision_ = UCLocalize("ELISION"); |
670a0494 JF |
9925 | Error_ = UCLocalize("ERROR"); |
9926 | Warning_ = UCLocalize("WARNING"); | |
9927 | ||
d6dad1b4 | 9928 | _trace(); |
77df4f82 | 9929 | int value(UIApplicationMain(argc, argv, @"Cydia", @"Cydia")); |
36bb2ca2 JF |
9930 | |
9931 | CGColorSpaceRelease(space_); | |
199d0ba5 | 9932 | CFRelease(Locale_); |
36bb2ca2 | 9933 | |
d13edf44 | 9934 | [pool release]; |
36bb2ca2 | 9935 | return value; |
6d166849 | 9936 | } |